* elf32-ppc.c (ppc_elf_write_core_note <NT_PRPSINFO>): Don't leave
[platform/upstream/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 *);
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_always_size_sections      ppc64_elf_func_desc_adjust
109 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
110 #define elf_backend_init_index_section        _bfd_elf_init_2_index_sections
111 #define elf_backend_action_discarded          ppc64_elf_action_discarded
112 #define elf_backend_relocate_section          ppc64_elf_relocate_section
113 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
114 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
115 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
116 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
117 #define elf_backend_special_sections          ppc64_elf_special_sections
118 #define elf_backend_post_process_headers      _bfd_elf_set_osabi
119
120 /* The name of the dynamic interpreter.  This is put in the .interp
121    section.  */
122 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
123
124 /* The size in bytes of an entry in the procedure linkage table.  */
125 #define PLT_ENTRY_SIZE 24
126
127 /* The initial size of the plt reserved for the dynamic linker.  */
128 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
129
130 /* TOC base pointers offset from start of TOC.  */
131 #define TOC_BASE_OFF    0x8000
132
133 /* Offset of tp and dtp pointers from start of TLS block.  */
134 #define TP_OFFSET       0x7000
135 #define DTP_OFFSET      0x8000
136
137 /* .plt call stub instructions.  The normal stub is like this, but
138    sometimes the .plt entry crosses a 64k boundary and we need to
139    insert an addi to adjust r12.  */
140 #define PLT_CALL_STUB_SIZE (7*4)
141 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha     */
142 #define STD_R2_40R1     0xf8410028      /* std   %r2,40(%r1)         */
143 #define LD_R11_0R12     0xe96c0000      /* ld    %r11,xxx+0@l(%r12)  */
144 #define MTCTR_R11       0x7d6903a6      /* mtctr %r11                */
145 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,xxx+8@l(%r12)   */
146                                         /* ld    %r11,xxx+16@l(%r12) */
147 #define BCTR            0x4e800420      /* bctr                      */
148
149
150 #define ADDIS_R12_R12   0x3d8c0000      /* addis %r12,%r12,off@ha  */
151 #define ADDI_R12_R12    0x398c0000      /* addi %r12,%r12,off@l  */
152 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
153 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
154
155 #define XOR_R11_R11_R11 0x7d6b5a78      /* xor   %r11,%r11,%r11  */
156 #define ADD_R12_R12_R11 0x7d8c5a14      /* add   %r12,%r12,%r11  */
157 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
158 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
159 #define BNECTR          0x4ca20420      /* bnectr+               */
160 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
161
162 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
163 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
164
165 #define LD_R2_40R1      0xe8410028      /* ld    %r2,40(%r1)     */
166
167 /* glink call stub instructions.  We enter with the index in R0.  */
168 #define GLINK_CALL_STUB_SIZE (16*4)
169                                         /* 0:                           */
170                                         /*  .quad plt0-1f               */
171                                         /* __glink:                     */
172 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
173 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
174                                         /* 1:                           */
175 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
176 #define LD_R2_M16R11    0xe84bfff0      /*  ld %2,(0b-1b)(%11)          */
177 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
178 #define ADD_R12_R2_R11  0x7d825a14      /*  add %12,%2,%11              */
179                                         /*  ld %11,0(%12)               */
180                                         /*  ld %2,8(%12)                */
181                                         /*  mtctr %11                   */
182                                         /*  ld %11,16(%12)              */
183                                         /*  bctr                        */
184
185 /* Pad with this.  */
186 #define NOP             0x60000000
187
188 /* Some other nops.  */
189 #define CROR_151515     0x4def7b82
190 #define CROR_313131     0x4ffffb82
191
192 /* .glink entries for the first 32k functions are two instructions.  */
193 #define LI_R0_0         0x38000000      /* li    %r0,0          */
194 #define B_DOT           0x48000000      /* b     .              */
195
196 /* After that, we need two instructions to load the index, followed by
197    a branch.  */
198 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
199 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
200
201 /* Instructions used by the save and restore reg functions.  */
202 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
203 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
204 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
205 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
206 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
207 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
208 #define LI_R12_0        0x39800000      /* li    %r12,0         */
209 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
210 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
211 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
212 #define BLR             0x4e800020      /* blr                  */
213
214 /* Since .opd is an array of descriptors and each entry will end up
215    with identical R_PPC64_RELATIVE relocs, there is really no need to
216    propagate .opd relocs;  The dynamic linker should be taught to
217    relocate .opd without reloc entries.  */
218 #ifndef NO_OPD_RELOCS
219 #define NO_OPD_RELOCS 0
220 #endif
221 \f
222 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
223
224 /* Relocation HOWTO's.  */
225 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
226
227 static reloc_howto_type ppc64_elf_howto_raw[] = {
228   /* This reloc does nothing.  */
229   HOWTO (R_PPC64_NONE,          /* type */
230          0,                     /* rightshift */
231          2,                     /* size (0 = byte, 1 = short, 2 = long) */
232          32,                    /* bitsize */
233          FALSE,                 /* pc_relative */
234          0,                     /* bitpos */
235          complain_overflow_dont, /* complain_on_overflow */
236          bfd_elf_generic_reloc, /* special_function */
237          "R_PPC64_NONE",        /* name */
238          FALSE,                 /* partial_inplace */
239          0,                     /* src_mask */
240          0,                     /* dst_mask */
241          FALSE),                /* pcrel_offset */
242
243   /* A standard 32 bit relocation.  */
244   HOWTO (R_PPC64_ADDR32,        /* type */
245          0,                     /* rightshift */
246          2,                     /* size (0 = byte, 1 = short, 2 = long) */
247          32,                    /* bitsize */
248          FALSE,                 /* pc_relative */
249          0,                     /* bitpos */
250          complain_overflow_bitfield, /* complain_on_overflow */
251          bfd_elf_generic_reloc, /* special_function */
252          "R_PPC64_ADDR32",      /* name */
253          FALSE,                 /* partial_inplace */
254          0,                     /* src_mask */
255          0xffffffff,            /* dst_mask */
256          FALSE),                /* pcrel_offset */
257
258   /* An absolute 26 bit branch; the lower two bits must be zero.
259      FIXME: we don't check that, we just clear them.  */
260   HOWTO (R_PPC64_ADDR24,        /* type */
261          0,                     /* rightshift */
262          2,                     /* size (0 = byte, 1 = short, 2 = long) */
263          26,                    /* bitsize */
264          FALSE,                 /* pc_relative */
265          0,                     /* bitpos */
266          complain_overflow_bitfield, /* complain_on_overflow */
267          bfd_elf_generic_reloc, /* special_function */
268          "R_PPC64_ADDR24",      /* name */
269          FALSE,                 /* partial_inplace */
270          0,                     /* src_mask */
271          0x03fffffc,            /* dst_mask */
272          FALSE),                /* pcrel_offset */
273
274   /* A standard 16 bit relocation.  */
275   HOWTO (R_PPC64_ADDR16,        /* type */
276          0,                     /* rightshift */
277          1,                     /* size (0 = byte, 1 = short, 2 = long) */
278          16,                    /* bitsize */
279          FALSE,                 /* pc_relative */
280          0,                     /* bitpos */
281          complain_overflow_bitfield, /* complain_on_overflow */
282          bfd_elf_generic_reloc, /* special_function */
283          "R_PPC64_ADDR16",      /* name */
284          FALSE,                 /* partial_inplace */
285          0,                     /* src_mask */
286          0xffff,                /* dst_mask */
287          FALSE),                /* pcrel_offset */
288
289   /* A 16 bit relocation without overflow.  */
290   HOWTO (R_PPC64_ADDR16_LO,     /* type */
291          0,                     /* rightshift */
292          1,                     /* size (0 = byte, 1 = short, 2 = long) */
293          16,                    /* bitsize */
294          FALSE,                 /* pc_relative */
295          0,                     /* bitpos */
296          complain_overflow_dont,/* complain_on_overflow */
297          bfd_elf_generic_reloc, /* special_function */
298          "R_PPC64_ADDR16_LO",   /* name */
299          FALSE,                 /* partial_inplace */
300          0,                     /* src_mask */
301          0xffff,                /* dst_mask */
302          FALSE),                /* pcrel_offset */
303
304   /* Bits 16-31 of an address.  */
305   HOWTO (R_PPC64_ADDR16_HI,     /* type */
306          16,                    /* rightshift */
307          1,                     /* size (0 = byte, 1 = short, 2 = long) */
308          16,                    /* bitsize */
309          FALSE,                 /* pc_relative */
310          0,                     /* bitpos */
311          complain_overflow_dont, /* complain_on_overflow */
312          bfd_elf_generic_reloc, /* special_function */
313          "R_PPC64_ADDR16_HI",   /* name */
314          FALSE,                 /* partial_inplace */
315          0,                     /* src_mask */
316          0xffff,                /* dst_mask */
317          FALSE),                /* pcrel_offset */
318
319   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
320      bits, treated as a signed number, is negative.  */
321   HOWTO (R_PPC64_ADDR16_HA,     /* type */
322          16,                    /* rightshift */
323          1,                     /* size (0 = byte, 1 = short, 2 = long) */
324          16,                    /* bitsize */
325          FALSE,                 /* pc_relative */
326          0,                     /* bitpos */
327          complain_overflow_dont, /* complain_on_overflow */
328          ppc64_elf_ha_reloc,    /* special_function */
329          "R_PPC64_ADDR16_HA",   /* name */
330          FALSE,                 /* partial_inplace */
331          0,                     /* src_mask */
332          0xffff,                /* dst_mask */
333          FALSE),                /* pcrel_offset */
334
335   /* An absolute 16 bit branch; the lower two bits must be zero.
336      FIXME: we don't check that, we just clear them.  */
337   HOWTO (R_PPC64_ADDR14,        /* type */
338          0,                     /* rightshift */
339          2,                     /* size (0 = byte, 1 = short, 2 = long) */
340          16,                    /* bitsize */
341          FALSE,                 /* pc_relative */
342          0,                     /* bitpos */
343          complain_overflow_bitfield, /* complain_on_overflow */
344          ppc64_elf_branch_reloc, /* special_function */
345          "R_PPC64_ADDR14",      /* name */
346          FALSE,                 /* partial_inplace */
347          0,                     /* src_mask */
348          0x0000fffc,            /* dst_mask */
349          FALSE),                /* pcrel_offset */
350
351   /* An absolute 16 bit branch, for which bit 10 should be set to
352      indicate that the branch is expected to be taken.  The lower two
353      bits must be zero.  */
354   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
355          0,                     /* rightshift */
356          2,                     /* size (0 = byte, 1 = short, 2 = long) */
357          16,                    /* bitsize */
358          FALSE,                 /* pc_relative */
359          0,                     /* bitpos */
360          complain_overflow_bitfield, /* complain_on_overflow */
361          ppc64_elf_brtaken_reloc, /* special_function */
362          "R_PPC64_ADDR14_BRTAKEN",/* name */
363          FALSE,                 /* partial_inplace */
364          0,                     /* src_mask */
365          0x0000fffc,            /* dst_mask */
366          FALSE),                /* pcrel_offset */
367
368   /* An absolute 16 bit branch, for which bit 10 should be set to
369      indicate that the branch is not expected to be taken.  The lower
370      two bits must be zero.  */
371   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
372          0,                     /* rightshift */
373          2,                     /* size (0 = byte, 1 = short, 2 = long) */
374          16,                    /* bitsize */
375          FALSE,                 /* pc_relative */
376          0,                     /* bitpos */
377          complain_overflow_bitfield, /* complain_on_overflow */
378          ppc64_elf_brtaken_reloc, /* special_function */
379          "R_PPC64_ADDR14_BRNTAKEN",/* name */
380          FALSE,                 /* partial_inplace */
381          0,                     /* src_mask */
382          0x0000fffc,            /* dst_mask */
383          FALSE),                /* pcrel_offset */
384
385   /* A relative 26 bit branch; the lower two bits must be zero.  */
386   HOWTO (R_PPC64_REL24,         /* type */
387          0,                     /* rightshift */
388          2,                     /* size (0 = byte, 1 = short, 2 = long) */
389          26,                    /* bitsize */
390          TRUE,                  /* pc_relative */
391          0,                     /* bitpos */
392          complain_overflow_signed, /* complain_on_overflow */
393          ppc64_elf_branch_reloc, /* special_function */
394          "R_PPC64_REL24",       /* name */
395          FALSE,                 /* partial_inplace */
396          0,                     /* src_mask */
397          0x03fffffc,            /* dst_mask */
398          TRUE),                 /* pcrel_offset */
399
400   /* A relative 16 bit branch; the lower two bits must be zero.  */
401   HOWTO (R_PPC64_REL14,         /* type */
402          0,                     /* rightshift */
403          2,                     /* size (0 = byte, 1 = short, 2 = long) */
404          16,                    /* bitsize */
405          TRUE,                  /* pc_relative */
406          0,                     /* bitpos */
407          complain_overflow_signed, /* complain_on_overflow */
408          ppc64_elf_branch_reloc, /* special_function */
409          "R_PPC64_REL14",       /* name */
410          FALSE,                 /* partial_inplace */
411          0,                     /* src_mask */
412          0x0000fffc,            /* dst_mask */
413          TRUE),                 /* pcrel_offset */
414
415   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
416      the branch is expected to be taken.  The lower two bits must be
417      zero.  */
418   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
419          0,                     /* rightshift */
420          2,                     /* size (0 = byte, 1 = short, 2 = long) */
421          16,                    /* bitsize */
422          TRUE,                  /* pc_relative */
423          0,                     /* bitpos */
424          complain_overflow_signed, /* complain_on_overflow */
425          ppc64_elf_brtaken_reloc, /* special_function */
426          "R_PPC64_REL14_BRTAKEN", /* name */
427          FALSE,                 /* partial_inplace */
428          0,                     /* src_mask */
429          0x0000fffc,            /* dst_mask */
430          TRUE),                 /* pcrel_offset */
431
432   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
433      the branch is not expected to be taken.  The lower two bits must
434      be zero.  */
435   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
436          0,                     /* rightshift */
437          2,                     /* size (0 = byte, 1 = short, 2 = long) */
438          16,                    /* bitsize */
439          TRUE,                  /* pc_relative */
440          0,                     /* bitpos */
441          complain_overflow_signed, /* complain_on_overflow */
442          ppc64_elf_brtaken_reloc, /* special_function */
443          "R_PPC64_REL14_BRNTAKEN",/* name */
444          FALSE,                 /* partial_inplace */
445          0,                     /* src_mask */
446          0x0000fffc,            /* dst_mask */
447          TRUE),                 /* pcrel_offset */
448
449   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
450      symbol.  */
451   HOWTO (R_PPC64_GOT16,         /* type */
452          0,                     /* rightshift */
453          1,                     /* size (0 = byte, 1 = short, 2 = long) */
454          16,                    /* bitsize */
455          FALSE,                 /* pc_relative */
456          0,                     /* bitpos */
457          complain_overflow_signed, /* complain_on_overflow */
458          ppc64_elf_unhandled_reloc, /* special_function */
459          "R_PPC64_GOT16",       /* name */
460          FALSE,                 /* partial_inplace */
461          0,                     /* src_mask */
462          0xffff,                /* dst_mask */
463          FALSE),                /* pcrel_offset */
464
465   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
466      the symbol.  */
467   HOWTO (R_PPC64_GOT16_LO,      /* type */
468          0,                     /* rightshift */
469          1,                     /* size (0 = byte, 1 = short, 2 = long) */
470          16,                    /* bitsize */
471          FALSE,                 /* pc_relative */
472          0,                     /* bitpos */
473          complain_overflow_dont, /* complain_on_overflow */
474          ppc64_elf_unhandled_reloc, /* special_function */
475          "R_PPC64_GOT16_LO",    /* name */
476          FALSE,                 /* partial_inplace */
477          0,                     /* src_mask */
478          0xffff,                /* dst_mask */
479          FALSE),                /* pcrel_offset */
480
481   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
482      the symbol.  */
483   HOWTO (R_PPC64_GOT16_HI,      /* type */
484          16,                    /* rightshift */
485          1,                     /* size (0 = byte, 1 = short, 2 = long) */
486          16,                    /* bitsize */
487          FALSE,                 /* pc_relative */
488          0,                     /* bitpos */
489          complain_overflow_dont,/* complain_on_overflow */
490          ppc64_elf_unhandled_reloc, /* special_function */
491          "R_PPC64_GOT16_HI",    /* name */
492          FALSE,                 /* partial_inplace */
493          0,                     /* src_mask */
494          0xffff,                /* dst_mask */
495          FALSE),                /* pcrel_offset */
496
497   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
498      the symbol.  */
499   HOWTO (R_PPC64_GOT16_HA,      /* type */
500          16,                    /* rightshift */
501          1,                     /* size (0 = byte, 1 = short, 2 = long) */
502          16,                    /* bitsize */
503          FALSE,                 /* pc_relative */
504          0,                     /* bitpos */
505          complain_overflow_dont,/* complain_on_overflow */
506          ppc64_elf_unhandled_reloc, /* special_function */
507          "R_PPC64_GOT16_HA",    /* name */
508          FALSE,                 /* partial_inplace */
509          0,                     /* src_mask */
510          0xffff,                /* dst_mask */
511          FALSE),                /* pcrel_offset */
512
513   /* This is used only by the dynamic linker.  The symbol should exist
514      both in the object being run and in some shared library.  The
515      dynamic linker copies the data addressed by the symbol from the
516      shared library into the object, because the object being
517      run has to have the data at some particular address.  */
518   HOWTO (R_PPC64_COPY,          /* type */
519          0,                     /* rightshift */
520          0,                     /* this one is variable size */
521          0,                     /* bitsize */
522          FALSE,                 /* pc_relative */
523          0,                     /* bitpos */
524          complain_overflow_dont, /* complain_on_overflow */
525          ppc64_elf_unhandled_reloc, /* special_function */
526          "R_PPC64_COPY",        /* name */
527          FALSE,                 /* partial_inplace */
528          0,                     /* src_mask */
529          0,                     /* dst_mask */
530          FALSE),                /* pcrel_offset */
531
532   /* Like R_PPC64_ADDR64, but used when setting global offset table
533      entries.  */
534   HOWTO (R_PPC64_GLOB_DAT,      /* type */
535          0,                     /* rightshift */
536          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
537          64,                    /* bitsize */
538          FALSE,                 /* pc_relative */
539          0,                     /* bitpos */
540          complain_overflow_dont, /* complain_on_overflow */
541          ppc64_elf_unhandled_reloc,  /* special_function */
542          "R_PPC64_GLOB_DAT",    /* name */
543          FALSE,                 /* partial_inplace */
544          0,                     /* src_mask */
545          ONES (64),             /* dst_mask */
546          FALSE),                /* pcrel_offset */
547
548   /* Created by the link editor.  Marks a procedure linkage table
549      entry for a symbol.  */
550   HOWTO (R_PPC64_JMP_SLOT,      /* type */
551          0,                     /* rightshift */
552          0,                     /* size (0 = byte, 1 = short, 2 = long) */
553          0,                     /* bitsize */
554          FALSE,                 /* pc_relative */
555          0,                     /* bitpos */
556          complain_overflow_dont, /* complain_on_overflow */
557          ppc64_elf_unhandled_reloc, /* special_function */
558          "R_PPC64_JMP_SLOT",    /* name */
559          FALSE,                 /* partial_inplace */
560          0,                     /* src_mask */
561          0,                     /* dst_mask */
562          FALSE),                /* pcrel_offset */
563
564   /* Used only by the dynamic linker.  When the object is run, this
565      doubleword64 is set to the load address of the object, plus the
566      addend.  */
567   HOWTO (R_PPC64_RELATIVE,      /* type */
568          0,                     /* rightshift */
569          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
570          64,                    /* bitsize */
571          FALSE,                 /* pc_relative */
572          0,                     /* bitpos */
573          complain_overflow_dont, /* complain_on_overflow */
574          bfd_elf_generic_reloc, /* special_function */
575          "R_PPC64_RELATIVE",    /* name */
576          FALSE,                 /* partial_inplace */
577          0,                     /* src_mask */
578          ONES (64),             /* dst_mask */
579          FALSE),                /* pcrel_offset */
580
581   /* Like R_PPC64_ADDR32, but may be unaligned.  */
582   HOWTO (R_PPC64_UADDR32,       /* type */
583          0,                     /* rightshift */
584          2,                     /* size (0 = byte, 1 = short, 2 = long) */
585          32,                    /* bitsize */
586          FALSE,                 /* pc_relative */
587          0,                     /* bitpos */
588          complain_overflow_bitfield, /* complain_on_overflow */
589          bfd_elf_generic_reloc, /* special_function */
590          "R_PPC64_UADDR32",     /* name */
591          FALSE,                 /* partial_inplace */
592          0,                     /* src_mask */
593          0xffffffff,            /* dst_mask */
594          FALSE),                /* pcrel_offset */
595
596   /* Like R_PPC64_ADDR16, but may be unaligned.  */
597   HOWTO (R_PPC64_UADDR16,       /* type */
598          0,                     /* rightshift */
599          1,                     /* size (0 = byte, 1 = short, 2 = long) */
600          16,                    /* bitsize */
601          FALSE,                 /* pc_relative */
602          0,                     /* bitpos */
603          complain_overflow_bitfield, /* complain_on_overflow */
604          bfd_elf_generic_reloc, /* special_function */
605          "R_PPC64_UADDR16",     /* name */
606          FALSE,                 /* partial_inplace */
607          0,                     /* src_mask */
608          0xffff,                /* dst_mask */
609          FALSE),                /* pcrel_offset */
610
611   /* 32-bit PC relative.  */
612   HOWTO (R_PPC64_REL32,         /* type */
613          0,                     /* rightshift */
614          2,                     /* size (0 = byte, 1 = short, 2 = long) */
615          32,                    /* bitsize */
616          TRUE,                  /* pc_relative */
617          0,                     /* bitpos */
618          /* FIXME: Verify.  Was complain_overflow_bitfield.  */
619          complain_overflow_signed, /* complain_on_overflow */
620          bfd_elf_generic_reloc, /* special_function */
621          "R_PPC64_REL32",       /* name */
622          FALSE,                 /* partial_inplace */
623          0,                     /* src_mask */
624          0xffffffff,            /* dst_mask */
625          TRUE),                 /* pcrel_offset */
626
627   /* 32-bit relocation to the symbol's procedure linkage table.  */
628   HOWTO (R_PPC64_PLT32,         /* type */
629          0,                     /* rightshift */
630          2,                     /* size (0 = byte, 1 = short, 2 = long) */
631          32,                    /* bitsize */
632          FALSE,                 /* pc_relative */
633          0,                     /* bitpos */
634          complain_overflow_bitfield, /* complain_on_overflow */
635          ppc64_elf_unhandled_reloc, /* special_function */
636          "R_PPC64_PLT32",       /* name */
637          FALSE,                 /* partial_inplace */
638          0,                     /* src_mask */
639          0xffffffff,            /* dst_mask */
640          FALSE),                /* pcrel_offset */
641
642   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
643      FIXME: R_PPC64_PLTREL32 not supported.  */
644   HOWTO (R_PPC64_PLTREL32,      /* type */
645          0,                     /* rightshift */
646          2,                     /* size (0 = byte, 1 = short, 2 = long) */
647          32,                    /* bitsize */
648          TRUE,                  /* pc_relative */
649          0,                     /* bitpos */
650          complain_overflow_signed, /* complain_on_overflow */
651          bfd_elf_generic_reloc, /* special_function */
652          "R_PPC64_PLTREL32",    /* name */
653          FALSE,                 /* partial_inplace */
654          0,                     /* src_mask */
655          0xffffffff,            /* dst_mask */
656          TRUE),                 /* pcrel_offset */
657
658   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
659      the symbol.  */
660   HOWTO (R_PPC64_PLT16_LO,      /* type */
661          0,                     /* rightshift */
662          1,                     /* size (0 = byte, 1 = short, 2 = long) */
663          16,                    /* bitsize */
664          FALSE,                 /* pc_relative */
665          0,                     /* bitpos */
666          complain_overflow_dont, /* complain_on_overflow */
667          ppc64_elf_unhandled_reloc, /* special_function */
668          "R_PPC64_PLT16_LO",    /* name */
669          FALSE,                 /* partial_inplace */
670          0,                     /* src_mask */
671          0xffff,                /* dst_mask */
672          FALSE),                /* pcrel_offset */
673
674   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
675      the symbol.  */
676   HOWTO (R_PPC64_PLT16_HI,      /* type */
677          16,                    /* rightshift */
678          1,                     /* size (0 = byte, 1 = short, 2 = long) */
679          16,                    /* bitsize */
680          FALSE,                 /* pc_relative */
681          0,                     /* bitpos */
682          complain_overflow_dont, /* complain_on_overflow */
683          ppc64_elf_unhandled_reloc, /* special_function */
684          "R_PPC64_PLT16_HI",    /* name */
685          FALSE,                 /* partial_inplace */
686          0,                     /* src_mask */
687          0xffff,                /* dst_mask */
688          FALSE),                /* pcrel_offset */
689
690   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
691      the symbol.  */
692   HOWTO (R_PPC64_PLT16_HA,      /* type */
693          16,                    /* rightshift */
694          1,                     /* size (0 = byte, 1 = short, 2 = long) */
695          16,                    /* bitsize */
696          FALSE,                 /* pc_relative */
697          0,                     /* bitpos */
698          complain_overflow_dont, /* complain_on_overflow */
699          ppc64_elf_unhandled_reloc, /* special_function */
700          "R_PPC64_PLT16_HA",    /* name */
701          FALSE,                 /* partial_inplace */
702          0,                     /* src_mask */
703          0xffff,                /* dst_mask */
704          FALSE),                /* pcrel_offset */
705
706   /* 16-bit section relative relocation.  */
707   HOWTO (R_PPC64_SECTOFF,       /* type */
708          0,                     /* rightshift */
709          1,                     /* size (0 = byte, 1 = short, 2 = long) */
710          16,                    /* bitsize */
711          FALSE,                 /* pc_relative */
712          0,                     /* bitpos */
713          complain_overflow_bitfield, /* complain_on_overflow */
714          ppc64_elf_sectoff_reloc, /* special_function */
715          "R_PPC64_SECTOFF",     /* name */
716          FALSE,                 /* partial_inplace */
717          0,                     /* src_mask */
718          0xffff,                /* dst_mask */
719          FALSE),                /* pcrel_offset */
720
721   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
722   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
723          0,                     /* rightshift */
724          1,                     /* size (0 = byte, 1 = short, 2 = long) */
725          16,                    /* bitsize */
726          FALSE,                 /* pc_relative */
727          0,                     /* bitpos */
728          complain_overflow_dont, /* complain_on_overflow */
729          ppc64_elf_sectoff_reloc, /* special_function */
730          "R_PPC64_SECTOFF_LO",  /* name */
731          FALSE,                 /* partial_inplace */
732          0,                     /* src_mask */
733          0xffff,                /* dst_mask */
734          FALSE),                /* pcrel_offset */
735
736   /* 16-bit upper half section relative relocation.  */
737   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
738          16,                    /* rightshift */
739          1,                     /* size (0 = byte, 1 = short, 2 = long) */
740          16,                    /* bitsize */
741          FALSE,                 /* pc_relative */
742          0,                     /* bitpos */
743          complain_overflow_dont, /* complain_on_overflow */
744          ppc64_elf_sectoff_reloc, /* special_function */
745          "R_PPC64_SECTOFF_HI",  /* name */
746          FALSE,                 /* partial_inplace */
747          0,                     /* src_mask */
748          0xffff,                /* dst_mask */
749          FALSE),                /* pcrel_offset */
750
751   /* 16-bit upper half adjusted section relative relocation.  */
752   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
753          16,                    /* rightshift */
754          1,                     /* size (0 = byte, 1 = short, 2 = long) */
755          16,                    /* bitsize */
756          FALSE,                 /* pc_relative */
757          0,                     /* bitpos */
758          complain_overflow_dont, /* complain_on_overflow */
759          ppc64_elf_sectoff_ha_reloc, /* special_function */
760          "R_PPC64_SECTOFF_HA",  /* name */
761          FALSE,                 /* partial_inplace */
762          0,                     /* src_mask */
763          0xffff,                /* dst_mask */
764          FALSE),                /* pcrel_offset */
765
766   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
767   HOWTO (R_PPC64_REL30,         /* type */
768          2,                     /* rightshift */
769          2,                     /* size (0 = byte, 1 = short, 2 = long) */
770          30,                    /* bitsize */
771          TRUE,                  /* pc_relative */
772          0,                     /* bitpos */
773          complain_overflow_dont, /* complain_on_overflow */
774          bfd_elf_generic_reloc, /* special_function */
775          "R_PPC64_REL30",       /* name */
776          FALSE,                 /* partial_inplace */
777          0,                     /* src_mask */
778          0xfffffffc,            /* dst_mask */
779          TRUE),                 /* pcrel_offset */
780
781   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
782
783   /* A standard 64-bit relocation.  */
784   HOWTO (R_PPC64_ADDR64,        /* type */
785          0,                     /* rightshift */
786          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
787          64,                    /* bitsize */
788          FALSE,                 /* pc_relative */
789          0,                     /* bitpos */
790          complain_overflow_dont, /* complain_on_overflow */
791          bfd_elf_generic_reloc, /* special_function */
792          "R_PPC64_ADDR64",      /* name */
793          FALSE,                 /* partial_inplace */
794          0,                     /* src_mask */
795          ONES (64),             /* dst_mask */
796          FALSE),                /* pcrel_offset */
797
798   /* The bits 32-47 of an address.  */
799   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
800          32,                    /* rightshift */
801          1,                     /* size (0 = byte, 1 = short, 2 = long) */
802          16,                    /* bitsize */
803          FALSE,                 /* pc_relative */
804          0,                     /* bitpos */
805          complain_overflow_dont, /* complain_on_overflow */
806          bfd_elf_generic_reloc, /* special_function */
807          "R_PPC64_ADDR16_HIGHER", /* name */
808          FALSE,                 /* partial_inplace */
809          0,                     /* src_mask */
810          0xffff,                /* dst_mask */
811          FALSE),                /* pcrel_offset */
812
813   /* The bits 32-47 of an address, plus 1 if the contents of the low
814      16 bits, treated as a signed number, is negative.  */
815   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
816          32,                    /* rightshift */
817          1,                     /* size (0 = byte, 1 = short, 2 = long) */
818          16,                    /* bitsize */
819          FALSE,                 /* pc_relative */
820          0,                     /* bitpos */
821          complain_overflow_dont, /* complain_on_overflow */
822          ppc64_elf_ha_reloc,    /* special_function */
823          "R_PPC64_ADDR16_HIGHERA", /* name */
824          FALSE,                 /* partial_inplace */
825          0,                     /* src_mask */
826          0xffff,                /* dst_mask */
827          FALSE),                /* pcrel_offset */
828
829   /* The bits 48-63 of an address.  */
830   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
831          48,                    /* rightshift */
832          1,                     /* size (0 = byte, 1 = short, 2 = long) */
833          16,                    /* bitsize */
834          FALSE,                 /* pc_relative */
835          0,                     /* bitpos */
836          complain_overflow_dont, /* complain_on_overflow */
837          bfd_elf_generic_reloc, /* special_function */
838          "R_PPC64_ADDR16_HIGHEST", /* name */
839          FALSE,                 /* partial_inplace */
840          0,                     /* src_mask */
841          0xffff,                /* dst_mask */
842          FALSE),                /* pcrel_offset */
843
844   /* The bits 48-63 of an address, plus 1 if the contents of the low
845      16 bits, treated as a signed number, is negative.  */
846   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
847          48,                    /* rightshift */
848          1,                     /* size (0 = byte, 1 = short, 2 = long) */
849          16,                    /* bitsize */
850          FALSE,                 /* pc_relative */
851          0,                     /* bitpos */
852          complain_overflow_dont, /* complain_on_overflow */
853          ppc64_elf_ha_reloc,    /* special_function */
854          "R_PPC64_ADDR16_HIGHESTA", /* name */
855          FALSE,                 /* partial_inplace */
856          0,                     /* src_mask */
857          0xffff,                /* dst_mask */
858          FALSE),                /* pcrel_offset */
859
860   /* Like ADDR64, but may be unaligned.  */
861   HOWTO (R_PPC64_UADDR64,       /* type */
862          0,                     /* rightshift */
863          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
864          64,                    /* bitsize */
865          FALSE,                 /* pc_relative */
866          0,                     /* bitpos */
867          complain_overflow_dont, /* complain_on_overflow */
868          bfd_elf_generic_reloc, /* special_function */
869          "R_PPC64_UADDR64",     /* name */
870          FALSE,                 /* partial_inplace */
871          0,                     /* src_mask */
872          ONES (64),             /* dst_mask */
873          FALSE),                /* pcrel_offset */
874
875   /* 64-bit relative relocation.  */
876   HOWTO (R_PPC64_REL64,         /* type */
877          0,                     /* rightshift */
878          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
879          64,                    /* bitsize */
880          TRUE,                  /* pc_relative */
881          0,                     /* bitpos */
882          complain_overflow_dont, /* complain_on_overflow */
883          bfd_elf_generic_reloc, /* special_function */
884          "R_PPC64_REL64",       /* name */
885          FALSE,                 /* partial_inplace */
886          0,                     /* src_mask */
887          ONES (64),             /* dst_mask */
888          TRUE),                 /* pcrel_offset */
889
890   /* 64-bit relocation to the symbol's procedure linkage table.  */
891   HOWTO (R_PPC64_PLT64,         /* type */
892          0,                     /* rightshift */
893          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
894          64,                    /* bitsize */
895          FALSE,                 /* pc_relative */
896          0,                     /* bitpos */
897          complain_overflow_dont, /* complain_on_overflow */
898          ppc64_elf_unhandled_reloc, /* special_function */
899          "R_PPC64_PLT64",       /* name */
900          FALSE,                 /* partial_inplace */
901          0,                     /* src_mask */
902          ONES (64),             /* dst_mask */
903          FALSE),                /* pcrel_offset */
904
905   /* 64-bit PC relative relocation to the symbol's procedure linkage
906      table.  */
907   /* FIXME: R_PPC64_PLTREL64 not supported.  */
908   HOWTO (R_PPC64_PLTREL64,      /* type */
909          0,                     /* rightshift */
910          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
911          64,                    /* bitsize */
912          TRUE,                  /* pc_relative */
913          0,                     /* bitpos */
914          complain_overflow_dont, /* complain_on_overflow */
915          ppc64_elf_unhandled_reloc, /* special_function */
916          "R_PPC64_PLTREL64",    /* name */
917          FALSE,                 /* partial_inplace */
918          0,                     /* src_mask */
919          ONES (64),             /* dst_mask */
920          TRUE),                 /* pcrel_offset */
921
922   /* 16 bit TOC-relative relocation.  */
923
924   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
925   HOWTO (R_PPC64_TOC16,         /* type */
926          0,                     /* rightshift */
927          1,                     /* size (0 = byte, 1 = short, 2 = long) */
928          16,                    /* bitsize */
929          FALSE,                 /* pc_relative */
930          0,                     /* bitpos */
931          complain_overflow_signed, /* complain_on_overflow */
932          ppc64_elf_toc_reloc,   /* special_function */
933          "R_PPC64_TOC16",       /* name */
934          FALSE,                 /* partial_inplace */
935          0,                     /* src_mask */
936          0xffff,                /* dst_mask */
937          FALSE),                /* pcrel_offset */
938
939   /* 16 bit TOC-relative relocation without overflow.  */
940
941   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
942   HOWTO (R_PPC64_TOC16_LO,      /* type */
943          0,                     /* rightshift */
944          1,                     /* size (0 = byte, 1 = short, 2 = long) */
945          16,                    /* bitsize */
946          FALSE,                 /* pc_relative */
947          0,                     /* bitpos */
948          complain_overflow_dont, /* complain_on_overflow */
949          ppc64_elf_toc_reloc,   /* special_function */
950          "R_PPC64_TOC16_LO",    /* name */
951          FALSE,                 /* partial_inplace */
952          0,                     /* src_mask */
953          0xffff,                /* dst_mask */
954          FALSE),                /* pcrel_offset */
955
956   /* 16 bit TOC-relative relocation, high 16 bits.  */
957
958   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
959   HOWTO (R_PPC64_TOC16_HI,      /* type */
960          16,                    /* rightshift */
961          1,                     /* size (0 = byte, 1 = short, 2 = long) */
962          16,                    /* bitsize */
963          FALSE,                 /* pc_relative */
964          0,                     /* bitpos */
965          complain_overflow_dont, /* complain_on_overflow */
966          ppc64_elf_toc_reloc,   /* special_function */
967          "R_PPC64_TOC16_HI",    /* name */
968          FALSE,                 /* partial_inplace */
969          0,                     /* src_mask */
970          0xffff,                /* dst_mask */
971          FALSE),                /* pcrel_offset */
972
973   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
974      contents of the low 16 bits, treated as a signed number, is
975      negative.  */
976
977   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
978   HOWTO (R_PPC64_TOC16_HA,      /* type */
979          16,                    /* rightshift */
980          1,                     /* size (0 = byte, 1 = short, 2 = long) */
981          16,                    /* bitsize */
982          FALSE,                 /* pc_relative */
983          0,                     /* bitpos */
984          complain_overflow_dont, /* complain_on_overflow */
985          ppc64_elf_toc_ha_reloc, /* special_function */
986          "R_PPC64_TOC16_HA",    /* name */
987          FALSE,                 /* partial_inplace */
988          0,                     /* src_mask */
989          0xffff,                /* dst_mask */
990          FALSE),                /* pcrel_offset */
991
992   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
993
994   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
995   HOWTO (R_PPC64_TOC,           /* type */
996          0,                     /* rightshift */
997          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
998          64,                    /* bitsize */
999          FALSE,                 /* pc_relative */
1000          0,                     /* bitpos */
1001          complain_overflow_bitfield, /* complain_on_overflow */
1002          ppc64_elf_toc64_reloc, /* special_function */
1003          "R_PPC64_TOC",         /* name */
1004          FALSE,                 /* partial_inplace */
1005          0,                     /* src_mask */
1006          ONES (64),             /* dst_mask */
1007          FALSE),                /* pcrel_offset */
1008
1009   /* Like R_PPC64_GOT16, but also informs the link editor that the
1010      value to relocate may (!) refer to a PLT entry which the link
1011      editor (a) may replace with the symbol value.  If the link editor
1012      is unable to fully resolve the symbol, it may (b) create a PLT
1013      entry and store the address to the new PLT entry in the GOT.
1014      This permits lazy resolution of function symbols at run time.
1015      The link editor may also skip all of this and just (c) emit a
1016      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1017   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1018     HOWTO (R_PPC64_PLTGOT16,    /* type */
1019          0,                     /* rightshift */
1020          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1021          16,                    /* bitsize */
1022          FALSE,                 /* pc_relative */
1023          0,                     /* bitpos */
1024          complain_overflow_signed, /* complain_on_overflow */
1025          ppc64_elf_unhandled_reloc, /* special_function */
1026          "R_PPC64_PLTGOT16",    /* name */
1027          FALSE,                 /* partial_inplace */
1028          0,                     /* src_mask */
1029          0xffff,                /* dst_mask */
1030          FALSE),                /* pcrel_offset */
1031
1032   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1033   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1034   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1035          0,                     /* rightshift */
1036          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1037          16,                    /* bitsize */
1038          FALSE,                 /* pc_relative */
1039          0,                     /* bitpos */
1040          complain_overflow_dont, /* complain_on_overflow */
1041          ppc64_elf_unhandled_reloc, /* special_function */
1042          "R_PPC64_PLTGOT16_LO", /* name */
1043          FALSE,                 /* partial_inplace */
1044          0,                     /* src_mask */
1045          0xffff,                /* dst_mask */
1046          FALSE),                /* pcrel_offset */
1047
1048   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1049   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1050   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1051          16,                    /* rightshift */
1052          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1053          16,                    /* bitsize */
1054          FALSE,                 /* pc_relative */
1055          0,                     /* bitpos */
1056          complain_overflow_dont, /* complain_on_overflow */
1057          ppc64_elf_unhandled_reloc, /* special_function */
1058          "R_PPC64_PLTGOT16_HI", /* name */
1059          FALSE,                 /* partial_inplace */
1060          0,                     /* src_mask */
1061          0xffff,                /* dst_mask */
1062          FALSE),                /* pcrel_offset */
1063
1064   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1065      1 if the contents of the low 16 bits, treated as a signed number,
1066      is negative.  */
1067   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1068   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1069          16,                    /* rightshift */
1070          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1071          16,                    /* bitsize */
1072          FALSE,                 /* pc_relative */
1073          0,                     /* bitpos */
1074          complain_overflow_dont,/* complain_on_overflow */
1075          ppc64_elf_unhandled_reloc, /* special_function */
1076          "R_PPC64_PLTGOT16_HA", /* name */
1077          FALSE,                 /* partial_inplace */
1078          0,                     /* src_mask */
1079          0xffff,                /* dst_mask */
1080          FALSE),                /* pcrel_offset */
1081
1082   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1083   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1084          0,                     /* rightshift */
1085          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1086          16,                    /* bitsize */
1087          FALSE,                 /* pc_relative */
1088          0,                     /* bitpos */
1089          complain_overflow_bitfield, /* complain_on_overflow */
1090          bfd_elf_generic_reloc, /* special_function */
1091          "R_PPC64_ADDR16_DS",   /* name */
1092          FALSE,                 /* partial_inplace */
1093          0,                     /* src_mask */
1094          0xfffc,                /* dst_mask */
1095          FALSE),                /* pcrel_offset */
1096
1097   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1098   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1099          0,                     /* rightshift */
1100          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1101          16,                    /* bitsize */
1102          FALSE,                 /* pc_relative */
1103          0,                     /* bitpos */
1104          complain_overflow_dont,/* complain_on_overflow */
1105          bfd_elf_generic_reloc, /* special_function */
1106          "R_PPC64_ADDR16_LO_DS",/* name */
1107          FALSE,                 /* partial_inplace */
1108          0,                     /* src_mask */
1109          0xfffc,                /* dst_mask */
1110          FALSE),                /* pcrel_offset */
1111
1112   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1113   HOWTO (R_PPC64_GOT16_DS,      /* type */
1114          0,                     /* rightshift */
1115          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1116          16,                    /* bitsize */
1117          FALSE,                 /* pc_relative */
1118          0,                     /* bitpos */
1119          complain_overflow_signed, /* complain_on_overflow */
1120          ppc64_elf_unhandled_reloc, /* special_function */
1121          "R_PPC64_GOT16_DS",    /* name */
1122          FALSE,                 /* partial_inplace */
1123          0,                     /* src_mask */
1124          0xfffc,                /* dst_mask */
1125          FALSE),                /* pcrel_offset */
1126
1127   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1128   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1129          0,                     /* rightshift */
1130          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1131          16,                    /* bitsize */
1132          FALSE,                 /* pc_relative */
1133          0,                     /* bitpos */
1134          complain_overflow_dont, /* complain_on_overflow */
1135          ppc64_elf_unhandled_reloc, /* special_function */
1136          "R_PPC64_GOT16_LO_DS", /* name */
1137          FALSE,                 /* partial_inplace */
1138          0,                     /* src_mask */
1139          0xfffc,                /* dst_mask */
1140          FALSE),                /* pcrel_offset */
1141
1142   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1143   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1144          0,                     /* rightshift */
1145          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1146          16,                    /* bitsize */
1147          FALSE,                 /* pc_relative */
1148          0,                     /* bitpos */
1149          complain_overflow_dont, /* complain_on_overflow */
1150          ppc64_elf_unhandled_reloc, /* special_function */
1151          "R_PPC64_PLT16_LO_DS", /* name */
1152          FALSE,                 /* partial_inplace */
1153          0,                     /* src_mask */
1154          0xfffc,                /* dst_mask */
1155          FALSE),                /* pcrel_offset */
1156
1157   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1158   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1159          0,                     /* rightshift */
1160          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1161          16,                    /* bitsize */
1162          FALSE,                 /* pc_relative */
1163          0,                     /* bitpos */
1164          complain_overflow_bitfield, /* complain_on_overflow */
1165          ppc64_elf_sectoff_reloc, /* special_function */
1166          "R_PPC64_SECTOFF_DS",  /* name */
1167          FALSE,                 /* partial_inplace */
1168          0,                     /* src_mask */
1169          0xfffc,                /* dst_mask */
1170          FALSE),                /* pcrel_offset */
1171
1172   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1173   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1174          0,                     /* rightshift */
1175          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1176          16,                    /* bitsize */
1177          FALSE,                 /* pc_relative */
1178          0,                     /* bitpos */
1179          complain_overflow_dont, /* complain_on_overflow */
1180          ppc64_elf_sectoff_reloc, /* special_function */
1181          "R_PPC64_SECTOFF_LO_DS",/* name */
1182          FALSE,                 /* partial_inplace */
1183          0,                     /* src_mask */
1184          0xfffc,                /* dst_mask */
1185          FALSE),                /* pcrel_offset */
1186
1187   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1188   HOWTO (R_PPC64_TOC16_DS,      /* type */
1189          0,                     /* rightshift */
1190          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1191          16,                    /* bitsize */
1192          FALSE,                 /* pc_relative */
1193          0,                     /* bitpos */
1194          complain_overflow_signed, /* complain_on_overflow */
1195          ppc64_elf_toc_reloc,   /* special_function */
1196          "R_PPC64_TOC16_DS",    /* name */
1197          FALSE,                 /* partial_inplace */
1198          0,                     /* src_mask */
1199          0xfffc,                /* dst_mask */
1200          FALSE),                /* pcrel_offset */
1201
1202   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1203   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1204          0,                     /* rightshift */
1205          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1206          16,                    /* bitsize */
1207          FALSE,                 /* pc_relative */
1208          0,                     /* bitpos */
1209          complain_overflow_dont, /* complain_on_overflow */
1210          ppc64_elf_toc_reloc,   /* special_function */
1211          "R_PPC64_TOC16_LO_DS", /* name */
1212          FALSE,                 /* partial_inplace */
1213          0,                     /* src_mask */
1214          0xfffc,                /* dst_mask */
1215          FALSE),                /* pcrel_offset */
1216
1217   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1218   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1219   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1220          0,                     /* rightshift */
1221          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1222          16,                    /* bitsize */
1223          FALSE,                 /* pc_relative */
1224          0,                     /* bitpos */
1225          complain_overflow_signed, /* complain_on_overflow */
1226          ppc64_elf_unhandled_reloc, /* special_function */
1227          "R_PPC64_PLTGOT16_DS", /* name */
1228          FALSE,                 /* partial_inplace */
1229          0,                     /* src_mask */
1230          0xfffc,                /* dst_mask */
1231          FALSE),                /* pcrel_offset */
1232
1233   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1234   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1235   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1236          0,                     /* rightshift */
1237          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1238          16,                    /* bitsize */
1239          FALSE,                 /* pc_relative */
1240          0,                     /* bitpos */
1241          complain_overflow_dont, /* complain_on_overflow */
1242          ppc64_elf_unhandled_reloc, /* special_function */
1243          "R_PPC64_PLTGOT16_LO_DS",/* name */
1244          FALSE,                 /* partial_inplace */
1245          0,                     /* src_mask */
1246          0xfffc,                /* dst_mask */
1247          FALSE),                /* pcrel_offset */
1248
1249   /* Marker relocs for TLS.  */
1250   HOWTO (R_PPC64_TLS,
1251          0,                     /* rightshift */
1252          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1253          32,                    /* bitsize */
1254          FALSE,                 /* pc_relative */
1255          0,                     /* bitpos */
1256          complain_overflow_dont, /* complain_on_overflow */
1257          bfd_elf_generic_reloc, /* special_function */
1258          "R_PPC64_TLS",         /* name */
1259          FALSE,                 /* partial_inplace */
1260          0,                     /* src_mask */
1261          0,                     /* dst_mask */
1262          FALSE),                /* pcrel_offset */
1263
1264   HOWTO (R_PPC64_TLSGD,
1265          0,                     /* rightshift */
1266          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1267          32,                    /* bitsize */
1268          FALSE,                 /* pc_relative */
1269          0,                     /* bitpos */
1270          complain_overflow_dont, /* complain_on_overflow */
1271          bfd_elf_generic_reloc, /* special_function */
1272          "R_PPC64_TLSGD",       /* name */
1273          FALSE,                 /* partial_inplace */
1274          0,                     /* src_mask */
1275          0,                     /* dst_mask */
1276          FALSE),                /* pcrel_offset */
1277
1278   HOWTO (R_PPC64_TLSLD,
1279          0,                     /* rightshift */
1280          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1281          32,                    /* bitsize */
1282          FALSE,                 /* pc_relative */
1283          0,                     /* bitpos */
1284          complain_overflow_dont, /* complain_on_overflow */
1285          bfd_elf_generic_reloc, /* special_function */
1286          "R_PPC64_TLSLD",       /* name */
1287          FALSE,                 /* partial_inplace */
1288          0,                     /* src_mask */
1289          0,                     /* dst_mask */
1290          FALSE),                /* pcrel_offset */
1291
1292   HOWTO (R_PPC64_TOCSAVE,
1293          0,                     /* rightshift */
1294          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1295          32,                    /* bitsize */
1296          FALSE,                 /* pc_relative */
1297          0,                     /* bitpos */
1298          complain_overflow_dont, /* complain_on_overflow */
1299          bfd_elf_generic_reloc, /* special_function */
1300          "R_PPC64_TOCSAVE",     /* name */
1301          FALSE,                 /* partial_inplace */
1302          0,                     /* src_mask */
1303          0,                     /* dst_mask */
1304          FALSE),                /* pcrel_offset */
1305
1306   /* Computes the load module index of the load module that contains the
1307      definition of its TLS sym.  */
1308   HOWTO (R_PPC64_DTPMOD64,
1309          0,                     /* rightshift */
1310          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1311          64,                    /* bitsize */
1312          FALSE,                 /* pc_relative */
1313          0,                     /* bitpos */
1314          complain_overflow_dont, /* complain_on_overflow */
1315          ppc64_elf_unhandled_reloc, /* special_function */
1316          "R_PPC64_DTPMOD64",    /* name */
1317          FALSE,                 /* partial_inplace */
1318          0,                     /* src_mask */
1319          ONES (64),             /* dst_mask */
1320          FALSE),                /* pcrel_offset */
1321
1322   /* Computes a dtv-relative displacement, the difference between the value
1323      of sym+add and the base address of the thread-local storage block that
1324      contains the definition of sym, minus 0x8000.  */
1325   HOWTO (R_PPC64_DTPREL64,
1326          0,                     /* rightshift */
1327          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1328          64,                    /* bitsize */
1329          FALSE,                 /* pc_relative */
1330          0,                     /* bitpos */
1331          complain_overflow_dont, /* complain_on_overflow */
1332          ppc64_elf_unhandled_reloc, /* special_function */
1333          "R_PPC64_DTPREL64",    /* name */
1334          FALSE,                 /* partial_inplace */
1335          0,                     /* src_mask */
1336          ONES (64),             /* dst_mask */
1337          FALSE),                /* pcrel_offset */
1338
1339   /* A 16 bit dtprel reloc.  */
1340   HOWTO (R_PPC64_DTPREL16,
1341          0,                     /* rightshift */
1342          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1343          16,                    /* bitsize */
1344          FALSE,                 /* pc_relative */
1345          0,                     /* bitpos */
1346          complain_overflow_signed, /* complain_on_overflow */
1347          ppc64_elf_unhandled_reloc, /* special_function */
1348          "R_PPC64_DTPREL16",    /* name */
1349          FALSE,                 /* partial_inplace */
1350          0,                     /* src_mask */
1351          0xffff,                /* dst_mask */
1352          FALSE),                /* pcrel_offset */
1353
1354   /* Like DTPREL16, but no overflow.  */
1355   HOWTO (R_PPC64_DTPREL16_LO,
1356          0,                     /* rightshift */
1357          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1358          16,                    /* bitsize */
1359          FALSE,                 /* pc_relative */
1360          0,                     /* bitpos */
1361          complain_overflow_dont, /* complain_on_overflow */
1362          ppc64_elf_unhandled_reloc, /* special_function */
1363          "R_PPC64_DTPREL16_LO", /* name */
1364          FALSE,                 /* partial_inplace */
1365          0,                     /* src_mask */
1366          0xffff,                /* dst_mask */
1367          FALSE),                /* pcrel_offset */
1368
1369   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1370   HOWTO (R_PPC64_DTPREL16_HI,
1371          16,                    /* rightshift */
1372          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1373          16,                    /* bitsize */
1374          FALSE,                 /* pc_relative */
1375          0,                     /* bitpos */
1376          complain_overflow_dont, /* complain_on_overflow */
1377          ppc64_elf_unhandled_reloc, /* special_function */
1378          "R_PPC64_DTPREL16_HI", /* name */
1379          FALSE,                 /* partial_inplace */
1380          0,                     /* src_mask */
1381          0xffff,                /* dst_mask */
1382          FALSE),                /* pcrel_offset */
1383
1384   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1385   HOWTO (R_PPC64_DTPREL16_HA,
1386          16,                    /* rightshift */
1387          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1388          16,                    /* bitsize */
1389          FALSE,                 /* pc_relative */
1390          0,                     /* bitpos */
1391          complain_overflow_dont, /* complain_on_overflow */
1392          ppc64_elf_unhandled_reloc, /* special_function */
1393          "R_PPC64_DTPREL16_HA", /* name */
1394          FALSE,                 /* partial_inplace */
1395          0,                     /* src_mask */
1396          0xffff,                /* dst_mask */
1397          FALSE),                /* pcrel_offset */
1398
1399   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1400   HOWTO (R_PPC64_DTPREL16_HIGHER,
1401          32,                    /* rightshift */
1402          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1403          16,                    /* bitsize */
1404          FALSE,                 /* pc_relative */
1405          0,                     /* bitpos */
1406          complain_overflow_dont, /* complain_on_overflow */
1407          ppc64_elf_unhandled_reloc, /* special_function */
1408          "R_PPC64_DTPREL16_HIGHER", /* name */
1409          FALSE,                 /* partial_inplace */
1410          0,                     /* src_mask */
1411          0xffff,                /* dst_mask */
1412          FALSE),                /* pcrel_offset */
1413
1414   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1415   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1416          32,                    /* rightshift */
1417          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1418          16,                    /* bitsize */
1419          FALSE,                 /* pc_relative */
1420          0,                     /* bitpos */
1421          complain_overflow_dont, /* complain_on_overflow */
1422          ppc64_elf_unhandled_reloc, /* special_function */
1423          "R_PPC64_DTPREL16_HIGHERA", /* name */
1424          FALSE,                 /* partial_inplace */
1425          0,                     /* src_mask */
1426          0xffff,                /* dst_mask */
1427          FALSE),                /* pcrel_offset */
1428
1429   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1430   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1431          48,                    /* rightshift */
1432          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1433          16,                    /* bitsize */
1434          FALSE,                 /* pc_relative */
1435          0,                     /* bitpos */
1436          complain_overflow_dont, /* complain_on_overflow */
1437          ppc64_elf_unhandled_reloc, /* special_function */
1438          "R_PPC64_DTPREL16_HIGHEST", /* name */
1439          FALSE,                 /* partial_inplace */
1440          0,                     /* src_mask */
1441          0xffff,                /* dst_mask */
1442          FALSE),                /* pcrel_offset */
1443
1444   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1445   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1446          48,                    /* rightshift */
1447          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1448          16,                    /* bitsize */
1449          FALSE,                 /* pc_relative */
1450          0,                     /* bitpos */
1451          complain_overflow_dont, /* complain_on_overflow */
1452          ppc64_elf_unhandled_reloc, /* special_function */
1453          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1454          FALSE,                 /* partial_inplace */
1455          0,                     /* src_mask */
1456          0xffff,                /* dst_mask */
1457          FALSE),                /* pcrel_offset */
1458
1459   /* Like DTPREL16, but for insns with a DS field.  */
1460   HOWTO (R_PPC64_DTPREL16_DS,
1461          0,                     /* rightshift */
1462          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1463          16,                    /* bitsize */
1464          FALSE,                 /* pc_relative */
1465          0,                     /* bitpos */
1466          complain_overflow_signed, /* complain_on_overflow */
1467          ppc64_elf_unhandled_reloc, /* special_function */
1468          "R_PPC64_DTPREL16_DS", /* name */
1469          FALSE,                 /* partial_inplace */
1470          0,                     /* src_mask */
1471          0xfffc,                /* dst_mask */
1472          FALSE),                /* pcrel_offset */
1473
1474   /* Like DTPREL16_DS, but no overflow.  */
1475   HOWTO (R_PPC64_DTPREL16_LO_DS,
1476          0,                     /* rightshift */
1477          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1478          16,                    /* bitsize */
1479          FALSE,                 /* pc_relative */
1480          0,                     /* bitpos */
1481          complain_overflow_dont, /* complain_on_overflow */
1482          ppc64_elf_unhandled_reloc, /* special_function */
1483          "R_PPC64_DTPREL16_LO_DS", /* name */
1484          FALSE,                 /* partial_inplace */
1485          0,                     /* src_mask */
1486          0xfffc,                /* dst_mask */
1487          FALSE),                /* pcrel_offset */
1488
1489   /* Computes a tp-relative displacement, the difference between the value of
1490      sym+add and the value of the thread pointer (r13).  */
1491   HOWTO (R_PPC64_TPREL64,
1492          0,                     /* rightshift */
1493          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1494          64,                    /* bitsize */
1495          FALSE,                 /* pc_relative */
1496          0,                     /* bitpos */
1497          complain_overflow_dont, /* complain_on_overflow */
1498          ppc64_elf_unhandled_reloc, /* special_function */
1499          "R_PPC64_TPREL64",     /* name */
1500          FALSE,                 /* partial_inplace */
1501          0,                     /* src_mask */
1502          ONES (64),             /* dst_mask */
1503          FALSE),                /* pcrel_offset */
1504
1505   /* A 16 bit tprel reloc.  */
1506   HOWTO (R_PPC64_TPREL16,
1507          0,                     /* rightshift */
1508          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1509          16,                    /* bitsize */
1510          FALSE,                 /* pc_relative */
1511          0,                     /* bitpos */
1512          complain_overflow_signed, /* complain_on_overflow */
1513          ppc64_elf_unhandled_reloc, /* special_function */
1514          "R_PPC64_TPREL16",     /* name */
1515          FALSE,                 /* partial_inplace */
1516          0,                     /* src_mask */
1517          0xffff,                /* dst_mask */
1518          FALSE),                /* pcrel_offset */
1519
1520   /* Like TPREL16, but no overflow.  */
1521   HOWTO (R_PPC64_TPREL16_LO,
1522          0,                     /* rightshift */
1523          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1524          16,                    /* bitsize */
1525          FALSE,                 /* pc_relative */
1526          0,                     /* bitpos */
1527          complain_overflow_dont, /* complain_on_overflow */
1528          ppc64_elf_unhandled_reloc, /* special_function */
1529          "R_PPC64_TPREL16_LO",  /* name */
1530          FALSE,                 /* partial_inplace */
1531          0,                     /* src_mask */
1532          0xffff,                /* dst_mask */
1533          FALSE),                /* pcrel_offset */
1534
1535   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1536   HOWTO (R_PPC64_TPREL16_HI,
1537          16,                    /* rightshift */
1538          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1539          16,                    /* bitsize */
1540          FALSE,                 /* pc_relative */
1541          0,                     /* bitpos */
1542          complain_overflow_dont, /* complain_on_overflow */
1543          ppc64_elf_unhandled_reloc, /* special_function */
1544          "R_PPC64_TPREL16_HI",  /* name */
1545          FALSE,                 /* partial_inplace */
1546          0,                     /* src_mask */
1547          0xffff,                /* dst_mask */
1548          FALSE),                /* pcrel_offset */
1549
1550   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1551   HOWTO (R_PPC64_TPREL16_HA,
1552          16,                    /* rightshift */
1553          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1554          16,                    /* bitsize */
1555          FALSE,                 /* pc_relative */
1556          0,                     /* bitpos */
1557          complain_overflow_dont, /* complain_on_overflow */
1558          ppc64_elf_unhandled_reloc, /* special_function */
1559          "R_PPC64_TPREL16_HA",  /* name */
1560          FALSE,                 /* partial_inplace */
1561          0,                     /* src_mask */
1562          0xffff,                /* dst_mask */
1563          FALSE),                /* pcrel_offset */
1564
1565   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1566   HOWTO (R_PPC64_TPREL16_HIGHER,
1567          32,                    /* rightshift */
1568          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1569          16,                    /* bitsize */
1570          FALSE,                 /* pc_relative */
1571          0,                     /* bitpos */
1572          complain_overflow_dont, /* complain_on_overflow */
1573          ppc64_elf_unhandled_reloc, /* special_function */
1574          "R_PPC64_TPREL16_HIGHER",      /* name */
1575          FALSE,                 /* partial_inplace */
1576          0,                     /* src_mask */
1577          0xffff,                /* dst_mask */
1578          FALSE),                /* pcrel_offset */
1579
1580   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1581   HOWTO (R_PPC64_TPREL16_HIGHERA,
1582          32,                    /* rightshift */
1583          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1584          16,                    /* bitsize */
1585          FALSE,                 /* pc_relative */
1586          0,                     /* bitpos */
1587          complain_overflow_dont, /* complain_on_overflow */
1588          ppc64_elf_unhandled_reloc, /* special_function */
1589          "R_PPC64_TPREL16_HIGHERA", /* name */
1590          FALSE,                 /* partial_inplace */
1591          0,                     /* src_mask */
1592          0xffff,                /* dst_mask */
1593          FALSE),                /* pcrel_offset */
1594
1595   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1596   HOWTO (R_PPC64_TPREL16_HIGHEST,
1597          48,                    /* rightshift */
1598          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1599          16,                    /* bitsize */
1600          FALSE,                 /* pc_relative */
1601          0,                     /* bitpos */
1602          complain_overflow_dont, /* complain_on_overflow */
1603          ppc64_elf_unhandled_reloc, /* special_function */
1604          "R_PPC64_TPREL16_HIGHEST", /* name */
1605          FALSE,                 /* partial_inplace */
1606          0,                     /* src_mask */
1607          0xffff,                /* dst_mask */
1608          FALSE),                /* pcrel_offset */
1609
1610   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1611   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1612          48,                    /* rightshift */
1613          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1614          16,                    /* bitsize */
1615          FALSE,                 /* pc_relative */
1616          0,                     /* bitpos */
1617          complain_overflow_dont, /* complain_on_overflow */
1618          ppc64_elf_unhandled_reloc, /* special_function */
1619          "R_PPC64_TPREL16_HIGHESTA", /* name */
1620          FALSE,                 /* partial_inplace */
1621          0,                     /* src_mask */
1622          0xffff,                /* dst_mask */
1623          FALSE),                /* pcrel_offset */
1624
1625   /* Like TPREL16, but for insns with a DS field.  */
1626   HOWTO (R_PPC64_TPREL16_DS,
1627          0,                     /* rightshift */
1628          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1629          16,                    /* bitsize */
1630          FALSE,                 /* pc_relative */
1631          0,                     /* bitpos */
1632          complain_overflow_signed, /* complain_on_overflow */
1633          ppc64_elf_unhandled_reloc, /* special_function */
1634          "R_PPC64_TPREL16_DS",  /* name */
1635          FALSE,                 /* partial_inplace */
1636          0,                     /* src_mask */
1637          0xfffc,                /* dst_mask */
1638          FALSE),                /* pcrel_offset */
1639
1640   /* Like TPREL16_DS, but no overflow.  */
1641   HOWTO (R_PPC64_TPREL16_LO_DS,
1642          0,                     /* rightshift */
1643          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1644          16,                    /* bitsize */
1645          FALSE,                 /* pc_relative */
1646          0,                     /* bitpos */
1647          complain_overflow_dont, /* complain_on_overflow */
1648          ppc64_elf_unhandled_reloc, /* special_function */
1649          "R_PPC64_TPREL16_LO_DS", /* name */
1650          FALSE,                 /* partial_inplace */
1651          0,                     /* src_mask */
1652          0xfffc,                /* dst_mask */
1653          FALSE),                /* pcrel_offset */
1654
1655   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1656      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1657      to the first entry relative to the TOC base (r2).  */
1658   HOWTO (R_PPC64_GOT_TLSGD16,
1659          0,                     /* rightshift */
1660          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1661          16,                    /* bitsize */
1662          FALSE,                 /* pc_relative */
1663          0,                     /* bitpos */
1664          complain_overflow_signed, /* complain_on_overflow */
1665          ppc64_elf_unhandled_reloc, /* special_function */
1666          "R_PPC64_GOT_TLSGD16", /* name */
1667          FALSE,                 /* partial_inplace */
1668          0,                     /* src_mask */
1669          0xffff,                /* dst_mask */
1670          FALSE),                /* pcrel_offset */
1671
1672   /* Like GOT_TLSGD16, but no overflow.  */
1673   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1674          0,                     /* rightshift */
1675          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1676          16,                    /* bitsize */
1677          FALSE,                 /* pc_relative */
1678          0,                     /* bitpos */
1679          complain_overflow_dont, /* complain_on_overflow */
1680          ppc64_elf_unhandled_reloc, /* special_function */
1681          "R_PPC64_GOT_TLSGD16_LO", /* name */
1682          FALSE,                 /* partial_inplace */
1683          0,                     /* src_mask */
1684          0xffff,                /* dst_mask */
1685          FALSE),                /* pcrel_offset */
1686
1687   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1688   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1689          16,                    /* rightshift */
1690          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1691          16,                    /* bitsize */
1692          FALSE,                 /* pc_relative */
1693          0,                     /* bitpos */
1694          complain_overflow_dont, /* complain_on_overflow */
1695          ppc64_elf_unhandled_reloc, /* special_function */
1696          "R_PPC64_GOT_TLSGD16_HI", /* name */
1697          FALSE,                 /* partial_inplace */
1698          0,                     /* src_mask */
1699          0xffff,                /* dst_mask */
1700          FALSE),                /* pcrel_offset */
1701
1702   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1703   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1704          16,                    /* rightshift */
1705          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1706          16,                    /* bitsize */
1707          FALSE,                 /* pc_relative */
1708          0,                     /* bitpos */
1709          complain_overflow_dont, /* complain_on_overflow */
1710          ppc64_elf_unhandled_reloc, /* special_function */
1711          "R_PPC64_GOT_TLSGD16_HA", /* name */
1712          FALSE,                 /* partial_inplace */
1713          0,                     /* src_mask */
1714          0xffff,                /* dst_mask */
1715          FALSE),                /* pcrel_offset */
1716
1717   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1718      with values (sym+add)@dtpmod and zero, and computes the offset to the
1719      first entry relative to the TOC base (r2).  */
1720   HOWTO (R_PPC64_GOT_TLSLD16,
1721          0,                     /* rightshift */
1722          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1723          16,                    /* bitsize */
1724          FALSE,                 /* pc_relative */
1725          0,                     /* bitpos */
1726          complain_overflow_signed, /* complain_on_overflow */
1727          ppc64_elf_unhandled_reloc, /* special_function */
1728          "R_PPC64_GOT_TLSLD16", /* name */
1729          FALSE,                 /* partial_inplace */
1730          0,                     /* src_mask */
1731          0xffff,                /* dst_mask */
1732          FALSE),                /* pcrel_offset */
1733
1734   /* Like GOT_TLSLD16, but no overflow.  */
1735   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1736          0,                     /* rightshift */
1737          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1738          16,                    /* bitsize */
1739          FALSE,                 /* pc_relative */
1740          0,                     /* bitpos */
1741          complain_overflow_dont, /* complain_on_overflow */
1742          ppc64_elf_unhandled_reloc, /* special_function */
1743          "R_PPC64_GOT_TLSLD16_LO", /* name */
1744          FALSE,                 /* partial_inplace */
1745          0,                     /* src_mask */
1746          0xffff,                /* dst_mask */
1747          FALSE),                /* pcrel_offset */
1748
1749   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1750   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1751          16,                    /* rightshift */
1752          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1753          16,                    /* bitsize */
1754          FALSE,                 /* pc_relative */
1755          0,                     /* bitpos */
1756          complain_overflow_dont, /* complain_on_overflow */
1757          ppc64_elf_unhandled_reloc, /* special_function */
1758          "R_PPC64_GOT_TLSLD16_HI", /* name */
1759          FALSE,                 /* partial_inplace */
1760          0,                     /* src_mask */
1761          0xffff,                /* dst_mask */
1762          FALSE),                /* pcrel_offset */
1763
1764   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1765   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1766          16,                    /* rightshift */
1767          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1768          16,                    /* bitsize */
1769          FALSE,                 /* pc_relative */
1770          0,                     /* bitpos */
1771          complain_overflow_dont, /* complain_on_overflow */
1772          ppc64_elf_unhandled_reloc, /* special_function */
1773          "R_PPC64_GOT_TLSLD16_HA", /* name */
1774          FALSE,                 /* partial_inplace */
1775          0,                     /* src_mask */
1776          0xffff,                /* dst_mask */
1777          FALSE),                /* pcrel_offset */
1778
1779   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1780      the offset to the entry relative to the TOC base (r2).  */
1781   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1782          0,                     /* rightshift */
1783          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1784          16,                    /* bitsize */
1785          FALSE,                 /* pc_relative */
1786          0,                     /* bitpos */
1787          complain_overflow_signed, /* complain_on_overflow */
1788          ppc64_elf_unhandled_reloc, /* special_function */
1789          "R_PPC64_GOT_DTPREL16_DS", /* name */
1790          FALSE,                 /* partial_inplace */
1791          0,                     /* src_mask */
1792          0xfffc,                /* dst_mask */
1793          FALSE),                /* pcrel_offset */
1794
1795   /* Like GOT_DTPREL16_DS, but no overflow.  */
1796   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1797          0,                     /* rightshift */
1798          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1799          16,                    /* bitsize */
1800          FALSE,                 /* pc_relative */
1801          0,                     /* bitpos */
1802          complain_overflow_dont, /* complain_on_overflow */
1803          ppc64_elf_unhandled_reloc, /* special_function */
1804          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1805          FALSE,                 /* partial_inplace */
1806          0,                     /* src_mask */
1807          0xfffc,                /* dst_mask */
1808          FALSE),                /* pcrel_offset */
1809
1810   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1811   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1812          16,                    /* rightshift */
1813          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1814          16,                    /* bitsize */
1815          FALSE,                 /* pc_relative */
1816          0,                     /* bitpos */
1817          complain_overflow_dont, /* complain_on_overflow */
1818          ppc64_elf_unhandled_reloc, /* special_function */
1819          "R_PPC64_GOT_DTPREL16_HI", /* name */
1820          FALSE,                 /* partial_inplace */
1821          0,                     /* src_mask */
1822          0xffff,                /* dst_mask */
1823          FALSE),                /* pcrel_offset */
1824
1825   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1826   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1827          16,                    /* rightshift */
1828          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1829          16,                    /* bitsize */
1830          FALSE,                 /* pc_relative */
1831          0,                     /* bitpos */
1832          complain_overflow_dont, /* complain_on_overflow */
1833          ppc64_elf_unhandled_reloc, /* special_function */
1834          "R_PPC64_GOT_DTPREL16_HA", /* name */
1835          FALSE,                 /* partial_inplace */
1836          0,                     /* src_mask */
1837          0xffff,                /* dst_mask */
1838          FALSE),                /* pcrel_offset */
1839
1840   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1841      offset to the entry relative to the TOC base (r2).  */
1842   HOWTO (R_PPC64_GOT_TPREL16_DS,
1843          0,                     /* rightshift */
1844          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1845          16,                    /* bitsize */
1846          FALSE,                 /* pc_relative */
1847          0,                     /* bitpos */
1848          complain_overflow_signed, /* complain_on_overflow */
1849          ppc64_elf_unhandled_reloc, /* special_function */
1850          "R_PPC64_GOT_TPREL16_DS", /* name */
1851          FALSE,                 /* partial_inplace */
1852          0,                     /* src_mask */
1853          0xfffc,                /* dst_mask */
1854          FALSE),                /* pcrel_offset */
1855
1856   /* Like GOT_TPREL16_DS, but no overflow.  */
1857   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1858          0,                     /* rightshift */
1859          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1860          16,                    /* bitsize */
1861          FALSE,                 /* pc_relative */
1862          0,                     /* bitpos */
1863          complain_overflow_dont, /* complain_on_overflow */
1864          ppc64_elf_unhandled_reloc, /* special_function */
1865          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1866          FALSE,                 /* partial_inplace */
1867          0,                     /* src_mask */
1868          0xfffc,                /* dst_mask */
1869          FALSE),                /* pcrel_offset */
1870
1871   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1872   HOWTO (R_PPC64_GOT_TPREL16_HI,
1873          16,                    /* rightshift */
1874          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1875          16,                    /* bitsize */
1876          FALSE,                 /* pc_relative */
1877          0,                     /* bitpos */
1878          complain_overflow_dont, /* complain_on_overflow */
1879          ppc64_elf_unhandled_reloc, /* special_function */
1880          "R_PPC64_GOT_TPREL16_HI", /* name */
1881          FALSE,                 /* partial_inplace */
1882          0,                     /* src_mask */
1883          0xffff,                /* dst_mask */
1884          FALSE),                /* pcrel_offset */
1885
1886   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1887   HOWTO (R_PPC64_GOT_TPREL16_HA,
1888          16,                    /* rightshift */
1889          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1890          16,                    /* bitsize */
1891          FALSE,                 /* pc_relative */
1892          0,                     /* bitpos */
1893          complain_overflow_dont, /* complain_on_overflow */
1894          ppc64_elf_unhandled_reloc, /* special_function */
1895          "R_PPC64_GOT_TPREL16_HA", /* name */
1896          FALSE,                 /* partial_inplace */
1897          0,                     /* src_mask */
1898          0xffff,                /* dst_mask */
1899          FALSE),                /* pcrel_offset */
1900
1901   HOWTO (R_PPC64_JMP_IREL,      /* type */
1902          0,                     /* rightshift */
1903          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1904          0,                     /* bitsize */
1905          FALSE,                 /* pc_relative */
1906          0,                     /* bitpos */
1907          complain_overflow_dont, /* complain_on_overflow */
1908          ppc64_elf_unhandled_reloc, /* special_function */
1909          "R_PPC64_JMP_IREL",    /* name */
1910          FALSE,                 /* partial_inplace */
1911          0,                     /* src_mask */
1912          0,                     /* dst_mask */
1913          FALSE),                /* pcrel_offset */
1914
1915   HOWTO (R_PPC64_IRELATIVE,     /* type */
1916          0,                     /* rightshift */
1917          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1918          64,                    /* bitsize */
1919          FALSE,                 /* pc_relative */
1920          0,                     /* bitpos */
1921          complain_overflow_dont, /* complain_on_overflow */
1922          bfd_elf_generic_reloc, /* special_function */
1923          "R_PPC64_IRELATIVE",   /* name */
1924          FALSE,                 /* partial_inplace */
1925          0,                     /* src_mask */
1926          ONES (64),             /* dst_mask */
1927          FALSE),                /* pcrel_offset */
1928
1929   /* A 16 bit relative relocation.  */
1930   HOWTO (R_PPC64_REL16,         /* type */
1931          0,                     /* rightshift */
1932          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1933          16,                    /* bitsize */
1934          TRUE,                  /* pc_relative */
1935          0,                     /* bitpos */
1936          complain_overflow_bitfield, /* complain_on_overflow */
1937          bfd_elf_generic_reloc, /* special_function */
1938          "R_PPC64_REL16",       /* name */
1939          FALSE,                 /* partial_inplace */
1940          0,                     /* src_mask */
1941          0xffff,                /* dst_mask */
1942          TRUE),                 /* pcrel_offset */
1943
1944   /* A 16 bit relative relocation without overflow.  */
1945   HOWTO (R_PPC64_REL16_LO,      /* type */
1946          0,                     /* rightshift */
1947          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1948          16,                    /* bitsize */
1949          TRUE,                  /* pc_relative */
1950          0,                     /* bitpos */
1951          complain_overflow_dont,/* complain_on_overflow */
1952          bfd_elf_generic_reloc, /* special_function */
1953          "R_PPC64_REL16_LO",    /* name */
1954          FALSE,                 /* partial_inplace */
1955          0,                     /* src_mask */
1956          0xffff,                /* dst_mask */
1957          TRUE),                 /* pcrel_offset */
1958
1959   /* The high order 16 bits of a relative address.  */
1960   HOWTO (R_PPC64_REL16_HI,      /* type */
1961          16,                    /* rightshift */
1962          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1963          16,                    /* bitsize */
1964          TRUE,                  /* pc_relative */
1965          0,                     /* bitpos */
1966          complain_overflow_dont, /* complain_on_overflow */
1967          bfd_elf_generic_reloc, /* special_function */
1968          "R_PPC64_REL16_HI",    /* name */
1969          FALSE,                 /* partial_inplace */
1970          0,                     /* src_mask */
1971          0xffff,                /* dst_mask */
1972          TRUE),                 /* pcrel_offset */
1973
1974   /* The high order 16 bits of a relative address, plus 1 if the contents of
1975      the low 16 bits, treated as a signed number, is negative.  */
1976   HOWTO (R_PPC64_REL16_HA,      /* type */
1977          16,                    /* rightshift */
1978          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1979          16,                    /* bitsize */
1980          TRUE,                  /* pc_relative */
1981          0,                     /* bitpos */
1982          complain_overflow_dont, /* complain_on_overflow */
1983          ppc64_elf_ha_reloc,    /* special_function */
1984          "R_PPC64_REL16_HA",    /* name */
1985          FALSE,                 /* partial_inplace */
1986          0,                     /* src_mask */
1987          0xffff,                /* dst_mask */
1988          TRUE),                 /* pcrel_offset */
1989
1990   /* GNU extension to record C++ vtable hierarchy.  */
1991   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1992          0,                     /* rightshift */
1993          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1994          0,                     /* bitsize */
1995          FALSE,                 /* pc_relative */
1996          0,                     /* bitpos */
1997          complain_overflow_dont, /* complain_on_overflow */
1998          NULL,                  /* special_function */
1999          "R_PPC64_GNU_VTINHERIT", /* name */
2000          FALSE,                 /* partial_inplace */
2001          0,                     /* src_mask */
2002          0,                     /* dst_mask */
2003          FALSE),                /* pcrel_offset */
2004
2005   /* GNU extension to record C++ vtable member usage.  */
2006   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
2007          0,                     /* rightshift */
2008          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2009          0,                     /* bitsize */
2010          FALSE,                 /* pc_relative */
2011          0,                     /* bitpos */
2012          complain_overflow_dont, /* complain_on_overflow */
2013          NULL,                  /* special_function */
2014          "R_PPC64_GNU_VTENTRY", /* name */
2015          FALSE,                 /* partial_inplace */
2016          0,                     /* src_mask */
2017          0,                     /* dst_mask */
2018          FALSE),                /* pcrel_offset */
2019 };
2020
2021 \f
2022 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2023    be done.  */
2024
2025 static void
2026 ppc_howto_init (void)
2027 {
2028   unsigned int i, type;
2029
2030   for (i = 0;
2031        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2032        i++)
2033     {
2034       type = ppc64_elf_howto_raw[i].type;
2035       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2036                           / sizeof (ppc64_elf_howto_table[0])));
2037       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2038     }
2039 }
2040
2041 static reloc_howto_type *
2042 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2043                              bfd_reloc_code_real_type code)
2044 {
2045   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2046
2047   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2048     /* Initialize howto table if needed.  */
2049     ppc_howto_init ();
2050
2051   switch (code)
2052     {
2053     default:
2054       return NULL;
2055
2056     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2057       break;
2058     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2059       break;
2060     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2061       break;
2062     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2063       break;
2064     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2065       break;
2066     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2067       break;
2068     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2069       break;
2070     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2071       break;
2072     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2073       break;
2074     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2075       break;
2076     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2077       break;
2078     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2079       break;
2080     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2081       break;
2082     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2083       break;
2084     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2085       break;
2086     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2087       break;
2088     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2089       break;
2090     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2091       break;
2092     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2093       break;
2094     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2095       break;
2096     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2097       break;
2098     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2099       break;
2100     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2101       break;
2102     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2103       break;
2104     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2105       break;
2106     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2107       break;
2108     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2109       break;
2110     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2111       break;
2112     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2113       break;
2114     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2115       break;
2116     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2117       break;
2118     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2119       break;
2120     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2121       break;
2122     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2123       break;
2124     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2125       break;
2126     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2127       break;
2128     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2129       break;
2130     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2131       break;
2132     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2133       break;
2134     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2135       break;
2136     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2137       break;
2138     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2139       break;
2140     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2141       break;
2142     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2143       break;
2144     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2145       break;
2146     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2147       break;
2148     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2149       break;
2150     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2151       break;
2152     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2153       break;
2154     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2155       break;
2156     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2157       break;
2158     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2159       break;
2160     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2161       break;
2162     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2163       break;
2164     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2165       break;
2166     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2167       break;
2168     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2169       break;
2170     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2171       break;
2172     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2173       break;
2174     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2175       break;
2176     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2177       break;
2178     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2179       break;
2180     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2181       break;
2182     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2183       break;
2184     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2185       break;
2186     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2187       break;
2188     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2189       break;
2190     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2191       break;
2192     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2193       break;
2194     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2195       break;
2196     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2197       break;
2198     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2199       break;
2200     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2201       break;
2202     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2203       break;
2204     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2205       break;
2206     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2207       break;
2208     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2209       break;
2210     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2211       break;
2212     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2213       break;
2214     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2215       break;
2216     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2217       break;
2218     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2219       break;
2220     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2221       break;
2222     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2223       break;
2224     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2225       break;
2226     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2227       break;
2228     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2229       break;
2230     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2231       break;
2232     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2233       break;
2234     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2235       break;
2236     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2237       break;
2238     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2239       break;
2240     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2241       break;
2242     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2243       break;
2244     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2245       break;
2246     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2247       break;
2248     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2249       break;
2250     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2251       break;
2252     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2253       break;
2254     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2255       break;
2256     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2257       break;
2258     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2259       break;
2260     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2261       break;
2262     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2263       break;
2264     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2265       break;
2266     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2267       break;
2268     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2269       break;
2270     }
2271
2272   return ppc64_elf_howto_table[r];
2273 };
2274
2275 static reloc_howto_type *
2276 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2277                              const char *r_name)
2278 {
2279   unsigned int i;
2280
2281   for (i = 0;
2282        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2283        i++)
2284     if (ppc64_elf_howto_raw[i].name != NULL
2285         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2286       return &ppc64_elf_howto_raw[i];
2287
2288   return NULL;
2289 }
2290
2291 /* Set the howto pointer for a PowerPC ELF reloc.  */
2292
2293 static void
2294 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2295                          Elf_Internal_Rela *dst)
2296 {
2297   unsigned int type;
2298
2299   /* Initialize howto table if needed.  */
2300   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2301     ppc_howto_init ();
2302
2303   type = ELF64_R_TYPE (dst->r_info);
2304   if (type >= (sizeof (ppc64_elf_howto_table)
2305                / sizeof (ppc64_elf_howto_table[0])))
2306     {
2307       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2308                              abfd, (int) type);
2309       type = R_PPC64_NONE;
2310     }
2311   cache_ptr->howto = ppc64_elf_howto_table[type];
2312 }
2313
2314 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2315
2316 static bfd_reloc_status_type
2317 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2318                     void *data, asection *input_section,
2319                     bfd *output_bfd, char **error_message)
2320 {
2321   /* If this is a relocatable link (output_bfd test tells us), just
2322      call the generic function.  Any adjustment will be done at final
2323      link time.  */
2324   if (output_bfd != NULL)
2325     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2326                                   input_section, output_bfd, error_message);
2327
2328   /* Adjust the addend for sign extension of the low 16 bits.
2329      We won't actually be using the low 16 bits, so trashing them
2330      doesn't matter.  */
2331   reloc_entry->addend += 0x8000;
2332   return bfd_reloc_continue;
2333 }
2334
2335 static bfd_reloc_status_type
2336 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2337                         void *data, asection *input_section,
2338                         bfd *output_bfd, char **error_message)
2339 {
2340   if (output_bfd != NULL)
2341     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2342                                   input_section, output_bfd, error_message);
2343
2344   if (strcmp (symbol->section->name, ".opd") == 0
2345       && (symbol->section->owner->flags & DYNAMIC) == 0)
2346     {
2347       bfd_vma dest = opd_entry_value (symbol->section,
2348                                       symbol->value + reloc_entry->addend,
2349                                       NULL, NULL);
2350       if (dest != (bfd_vma) -1)
2351         reloc_entry->addend = dest - (symbol->value
2352                                       + symbol->section->output_section->vma
2353                                       + symbol->section->output_offset);
2354     }
2355   return bfd_reloc_continue;
2356 }
2357
2358 static bfd_reloc_status_type
2359 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2360                          void *data, asection *input_section,
2361                          bfd *output_bfd, char **error_message)
2362 {
2363   long insn;
2364   enum elf_ppc64_reloc_type r_type;
2365   bfd_size_type octets;
2366   /* Assume 'at' branch hints.  */
2367   bfd_boolean is_isa_v2 = TRUE;
2368
2369   /* If this is a relocatable link (output_bfd test tells us), just
2370      call the generic function.  Any adjustment will be done at final
2371      link time.  */
2372   if (output_bfd != NULL)
2373     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2374                                   input_section, output_bfd, error_message);
2375
2376   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2377   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2378   insn &= ~(0x01 << 21);
2379   r_type = reloc_entry->howto->type;
2380   if (r_type == R_PPC64_ADDR14_BRTAKEN
2381       || r_type == R_PPC64_REL14_BRTAKEN)
2382     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2383
2384   if (is_isa_v2)
2385     {
2386       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2387          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2388          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2389       if ((insn & (0x14 << 21)) == (0x04 << 21))
2390         insn |= 0x02 << 21;
2391       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2392         insn |= 0x08 << 21;
2393       else
2394         goto out;
2395     }
2396   else
2397     {
2398       bfd_vma target = 0;
2399       bfd_vma from;
2400
2401       if (!bfd_is_com_section (symbol->section))
2402         target = symbol->value;
2403       target += symbol->section->output_section->vma;
2404       target += symbol->section->output_offset;
2405       target += reloc_entry->addend;
2406
2407       from = (reloc_entry->address
2408               + input_section->output_offset
2409               + input_section->output_section->vma);
2410
2411       /* Invert 'y' bit if not the default.  */
2412       if ((bfd_signed_vma) (target - from) < 0)
2413         insn ^= 0x01 << 21;
2414     }
2415   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2416  out:
2417   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2418                                  input_section, output_bfd, error_message);
2419 }
2420
2421 static bfd_reloc_status_type
2422 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2423                          void *data, asection *input_section,
2424                          bfd *output_bfd, char **error_message)
2425 {
2426   /* If this is a relocatable link (output_bfd test tells us), just
2427      call the generic function.  Any adjustment will be done at final
2428      link time.  */
2429   if (output_bfd != NULL)
2430     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2431                                   input_section, output_bfd, error_message);
2432
2433   /* Subtract the symbol section base address.  */
2434   reloc_entry->addend -= symbol->section->output_section->vma;
2435   return bfd_reloc_continue;
2436 }
2437
2438 static bfd_reloc_status_type
2439 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2440                             void *data, asection *input_section,
2441                             bfd *output_bfd, char **error_message)
2442 {
2443   /* If this is a relocatable link (output_bfd test tells us), just
2444      call the generic function.  Any adjustment will be done at final
2445      link time.  */
2446   if (output_bfd != NULL)
2447     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2448                                   input_section, output_bfd, error_message);
2449
2450   /* Subtract the symbol section base address.  */
2451   reloc_entry->addend -= symbol->section->output_section->vma;
2452
2453   /* Adjust the addend for sign extension of the low 16 bits.  */
2454   reloc_entry->addend += 0x8000;
2455   return bfd_reloc_continue;
2456 }
2457
2458 static bfd_reloc_status_type
2459 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2460                      void *data, asection *input_section,
2461                      bfd *output_bfd, char **error_message)
2462 {
2463   bfd_vma TOCstart;
2464
2465   /* If this is a relocatable link (output_bfd test tells us), just
2466      call the generic function.  Any adjustment will be done at final
2467      link time.  */
2468   if (output_bfd != NULL)
2469     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2470                                   input_section, output_bfd, error_message);
2471
2472   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2473   if (TOCstart == 0)
2474     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2475
2476   /* Subtract the TOC base address.  */
2477   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2478   return bfd_reloc_continue;
2479 }
2480
2481 static bfd_reloc_status_type
2482 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2483                         void *data, asection *input_section,
2484                         bfd *output_bfd, char **error_message)
2485 {
2486   bfd_vma TOCstart;
2487
2488   /* If this is a relocatable link (output_bfd test tells us), just
2489      call the generic function.  Any adjustment will be done at final
2490      link time.  */
2491   if (output_bfd != NULL)
2492     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2493                                   input_section, output_bfd, error_message);
2494
2495   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2496   if (TOCstart == 0)
2497     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2498
2499   /* Subtract the TOC base address.  */
2500   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2501
2502   /* Adjust the addend for sign extension of the low 16 bits.  */
2503   reloc_entry->addend += 0x8000;
2504   return bfd_reloc_continue;
2505 }
2506
2507 static bfd_reloc_status_type
2508 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2509                        void *data, asection *input_section,
2510                        bfd *output_bfd, char **error_message)
2511 {
2512   bfd_vma TOCstart;
2513   bfd_size_type octets;
2514
2515   /* If this is a relocatable link (output_bfd test tells us), just
2516      call the generic function.  Any adjustment will be done at final
2517      link time.  */
2518   if (output_bfd != NULL)
2519     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2520                                   input_section, output_bfd, error_message);
2521
2522   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2523   if (TOCstart == 0)
2524     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2525
2526   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2527   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2528   return bfd_reloc_ok;
2529 }
2530
2531 static bfd_reloc_status_type
2532 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2533                            void *data, asection *input_section,
2534                            bfd *output_bfd, char **error_message)
2535 {
2536   /* If this is a relocatable link (output_bfd test tells us), just
2537      call the generic function.  Any adjustment will be done at final
2538      link time.  */
2539   if (output_bfd != NULL)
2540     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2541                                   input_section, output_bfd, error_message);
2542
2543   if (error_message != NULL)
2544     {
2545       static char buf[60];
2546       sprintf (buf, "generic linker can't handle %s",
2547                reloc_entry->howto->name);
2548       *error_message = buf;
2549     }
2550   return bfd_reloc_dangerous;
2551 }
2552
2553 /* Track GOT entries needed for a given symbol.  We might need more
2554    than one got entry per symbol.  */
2555 struct got_entry
2556 {
2557   struct got_entry *next;
2558
2559   /* The symbol addend that we'll be placing in the GOT.  */
2560   bfd_vma addend;
2561
2562   /* Unlike other ELF targets, we use separate GOT entries for the same
2563      symbol referenced from different input files.  This is to support
2564      automatic multiple TOC/GOT sections, where the TOC base can vary
2565      from one input file to another.  After partitioning into TOC groups
2566      we merge entries within the group.
2567
2568      Point to the BFD owning this GOT entry.  */
2569   bfd *owner;
2570
2571   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2572      TLS_TPREL or TLS_DTPREL for tls entries.  */
2573   unsigned char tls_type;
2574
2575   /* Non-zero if got.ent points to real entry.  */
2576   unsigned char is_indirect;
2577
2578   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2579   union
2580     {
2581       bfd_signed_vma refcount;
2582       bfd_vma offset;
2583       struct got_entry *ent;
2584     } got;
2585 };
2586
2587 /* The same for PLT.  */
2588 struct plt_entry
2589 {
2590   struct plt_entry *next;
2591
2592   bfd_vma addend;
2593
2594   union
2595     {
2596       bfd_signed_vma refcount;
2597       bfd_vma offset;
2598     } plt;
2599 };
2600
2601 struct ppc64_elf_obj_tdata
2602 {
2603   struct elf_obj_tdata elf;
2604
2605   /* Shortcuts to dynamic linker sections.  */
2606   asection *got;
2607   asection *relgot;
2608
2609   /* Used during garbage collection.  We attach global symbols defined
2610      on removed .opd entries to this section so that the sym is removed.  */
2611   asection *deleted_section;
2612
2613   /* TLS local dynamic got entry handling.  Support for multiple GOT
2614      sections means we potentially need one of these for each input bfd.  */
2615   struct got_entry tlsld_got;
2616
2617   /* A copy of relocs before they are modified for --emit-relocs.  */
2618   Elf_Internal_Rela *opd_relocs;
2619
2620   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2621      the reloc to be in the range -32768 to 32767.  */
2622   unsigned int has_small_toc_reloc : 1;
2623
2624   /* Set if toc/got ha relocs detected not using r2, or lo reloc
2625      instruction not one we handle.  */
2626   unsigned int unexpected_toc_insn : 1;
2627 };
2628
2629 #define ppc64_elf_tdata(bfd) \
2630   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2631
2632 #define ppc64_tlsld_got(bfd) \
2633   (&ppc64_elf_tdata (bfd)->tlsld_got)
2634
2635 #define is_ppc64_elf(bfd) \
2636   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2637    && elf_object_id (bfd) == PPC64_ELF_DATA)
2638
2639 /* Override the generic function because we store some extras.  */
2640
2641 static bfd_boolean
2642 ppc64_elf_mkobject (bfd *abfd)
2643 {
2644   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2645                                   PPC64_ELF_DATA);
2646 }
2647
2648 /* Fix bad default arch selected for a 64 bit input bfd when the
2649    default is 32 bit.  */
2650
2651 static bfd_boolean
2652 ppc64_elf_object_p (bfd *abfd)
2653 {
2654   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2655     {
2656       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2657
2658       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2659         {
2660           /* Relies on arch after 32 bit default being 64 bit default.  */
2661           abfd->arch_info = abfd->arch_info->next;
2662           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2663         }
2664     }
2665   return TRUE;
2666 }
2667
2668 /* Support for core dump NOTE sections.  */
2669
2670 static bfd_boolean
2671 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2672 {
2673   size_t offset, size;
2674
2675   if (note->descsz != 504)
2676     return FALSE;
2677
2678   /* pr_cursig */
2679   elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2680
2681   /* pr_pid */
2682   elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
2683
2684   /* pr_reg */
2685   offset = 112;
2686   size = 384;
2687
2688   /* Make a ".reg/999" section.  */
2689   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2690                                           size, note->descpos + offset);
2691 }
2692
2693 static bfd_boolean
2694 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2695 {
2696   if (note->descsz != 136)
2697     return FALSE;
2698
2699   elf_tdata (abfd)->core_pid
2700     = bfd_get_32 (abfd, note->descdata + 24);
2701   elf_tdata (abfd)->core_program
2702     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2703   elf_tdata (abfd)->core_command
2704     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2705
2706   return TRUE;
2707 }
2708
2709 static char *
2710 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2711                            ...)
2712 {
2713   switch (note_type)
2714     {
2715     default:
2716       return NULL;
2717
2718     case NT_PRPSINFO:
2719       {
2720         char data[136];
2721         va_list ap;
2722
2723         va_start (ap, note_type);
2724         memset (data, 0, sizeof (data));
2725         strncpy (data + 40, va_arg (ap, const char *), 16);
2726         strncpy (data + 56, va_arg (ap, const char *), 80);
2727         va_end (ap);
2728         return elfcore_write_note (abfd, buf, bufsiz,
2729                                    "CORE", note_type, data, sizeof (data));
2730       }
2731
2732     case NT_PRSTATUS:
2733       {
2734         char data[504];
2735         va_list ap;
2736         long pid;
2737         int cursig;
2738         const void *greg;
2739
2740         va_start (ap, note_type);
2741         memset (data, 0, 112);
2742         pid = va_arg (ap, long);
2743         bfd_put_32 (abfd, pid, data + 32);
2744         cursig = va_arg (ap, int);
2745         bfd_put_16 (abfd, cursig, data + 12);
2746         greg = va_arg (ap, const void *);
2747         memcpy (data + 112, greg, 384);
2748         memset (data + 496, 0, 8);
2749         va_end (ap);
2750         return elfcore_write_note (abfd, buf, bufsiz,
2751                                    "CORE", note_type, data, sizeof (data));
2752       }
2753     }
2754 }
2755
2756 /* Add extra PPC sections.  */
2757
2758 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2759 {
2760   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
2761   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2762   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2763   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2764   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2765   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2766   { NULL,                     0,  0, 0,            0 }
2767 };
2768
2769 enum _ppc64_sec_type {
2770   sec_normal = 0,
2771   sec_opd = 1,
2772   sec_toc = 2
2773 };
2774
2775 struct _ppc64_elf_section_data
2776 {
2777   struct bfd_elf_section_data elf;
2778
2779   union
2780   {
2781     /* An array with one entry for each opd function descriptor.  */
2782     struct _opd_sec_data
2783     {
2784       /* Points to the function code section for local opd entries.  */
2785       asection **func_sec;
2786
2787       /* After editing .opd, adjust references to opd local syms.  */
2788       long *adjust;
2789     } opd;
2790
2791     /* An array for toc sections, indexed by offset/8.  */
2792     struct _toc_sec_data
2793     {
2794       /* Specifies the relocation symbol index used at a given toc offset.  */
2795       unsigned *symndx;
2796
2797       /* And the relocation addend.  */
2798       bfd_vma *add;
2799     } toc;
2800   } u;
2801
2802   enum _ppc64_sec_type sec_type:2;
2803
2804   /* Flag set when small branches are detected.  Used to
2805      select suitable defaults for the stub group size.  */
2806   unsigned int has_14bit_branch:1;
2807 };
2808
2809 #define ppc64_elf_section_data(sec) \
2810   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2811
2812 static bfd_boolean
2813 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2814 {
2815   if (!sec->used_by_bfd)
2816     {
2817       struct _ppc64_elf_section_data *sdata;
2818       bfd_size_type amt = sizeof (*sdata);
2819
2820       sdata = bfd_zalloc (abfd, amt);
2821       if (sdata == NULL)
2822         return FALSE;
2823       sec->used_by_bfd = sdata;
2824     }
2825
2826   return _bfd_elf_new_section_hook (abfd, sec);
2827 }
2828
2829 static struct _opd_sec_data *
2830 get_opd_info (asection * sec)
2831 {
2832   if (sec != NULL
2833       && ppc64_elf_section_data (sec) != NULL
2834       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2835     return &ppc64_elf_section_data (sec)->u.opd;
2836   return NULL;
2837 }
2838 \f
2839 /* Parameters for the qsort hook.  */
2840 static bfd_boolean synthetic_relocatable;
2841
2842 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
2843
2844 static int
2845 compare_symbols (const void *ap, const void *bp)
2846 {
2847   const asymbol *a = * (const asymbol **) ap;
2848   const asymbol *b = * (const asymbol **) bp;
2849
2850   /* Section symbols first.  */
2851   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2852     return -1;
2853   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2854     return 1;
2855
2856   /* then .opd symbols.  */
2857   if (strcmp (a->section->name, ".opd") == 0
2858       && strcmp (b->section->name, ".opd") != 0)
2859     return -1;
2860   if (strcmp (a->section->name, ".opd") != 0
2861       && strcmp (b->section->name, ".opd") == 0)
2862     return 1;
2863
2864   /* then other code symbols.  */
2865   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2866       == (SEC_CODE | SEC_ALLOC)
2867       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2868          != (SEC_CODE | SEC_ALLOC))
2869     return -1;
2870
2871   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2872       != (SEC_CODE | SEC_ALLOC)
2873       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2874          == (SEC_CODE | SEC_ALLOC))
2875     return 1;
2876
2877   if (synthetic_relocatable)
2878     {
2879       if (a->section->id < b->section->id)
2880         return -1;
2881
2882       if (a->section->id > b->section->id)
2883         return 1;
2884     }
2885
2886   if (a->value + a->section->vma < b->value + b->section->vma)
2887     return -1;
2888
2889   if (a->value + a->section->vma > b->value + b->section->vma)
2890     return 1;
2891
2892   /* For syms with the same value, prefer strong dynamic global function
2893      syms over other syms.  */
2894   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2895     return -1;
2896
2897   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2898     return 1;
2899
2900   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2901     return -1;
2902
2903   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2904     return 1;
2905
2906   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2907     return -1;
2908
2909   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2910     return 1;
2911
2912   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2913     return -1;
2914
2915   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2916     return 1;
2917
2918   return 0;
2919 }
2920
2921 /* Search SYMS for a symbol of the given VALUE.  */
2922
2923 static asymbol *
2924 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
2925 {
2926   long mid;
2927
2928   if (id == -1)
2929     {
2930       while (lo < hi)
2931         {
2932           mid = (lo + hi) >> 1;
2933           if (syms[mid]->value + syms[mid]->section->vma < value)
2934             lo = mid + 1;
2935           else if (syms[mid]->value + syms[mid]->section->vma > value)
2936             hi = mid;
2937           else
2938             return syms[mid];
2939         }
2940     }
2941   else
2942     {
2943       while (lo < hi)
2944         {
2945           mid = (lo + hi) >> 1;
2946           if (syms[mid]->section->id < id)
2947             lo = mid + 1;
2948           else if (syms[mid]->section->id > id)
2949             hi = mid;
2950           else if (syms[mid]->value < value)
2951             lo = mid + 1;
2952           else if (syms[mid]->value > value)
2953             hi = mid;
2954           else
2955             return syms[mid];
2956         }
2957     }
2958   return NULL;
2959 }
2960
2961 static bfd_boolean
2962 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2963 {
2964   bfd_vma vma = *(bfd_vma *) ptr;
2965   return ((section->flags & SEC_ALLOC) != 0
2966           && section->vma <= vma
2967           && vma < section->vma + section->size);
2968 }
2969
2970 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2971    entry syms.  Also generate @plt symbols for the glink branch table.  */
2972
2973 static long
2974 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2975                                 long static_count, asymbol **static_syms,
2976                                 long dyn_count, asymbol **dyn_syms,
2977                                 asymbol **ret)
2978 {
2979   asymbol *s;
2980   long i;
2981   long count;
2982   char *names;
2983   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2984   asection *opd;
2985   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2986   asymbol **syms;
2987
2988   *ret = NULL;
2989
2990   opd = bfd_get_section_by_name (abfd, ".opd");
2991   if (opd == NULL)
2992     return 0;
2993
2994   symcount = static_count;
2995   if (!relocatable)
2996     symcount += dyn_count;
2997   if (symcount == 0)
2998     return 0;
2999
3000   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3001   if (syms == NULL)
3002     return -1;
3003
3004   if (!relocatable && static_count != 0 && dyn_count != 0)
3005     {
3006       /* Use both symbol tables.  */
3007       memcpy (syms, static_syms, static_count * sizeof (*syms));
3008       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3009     }
3010   else if (!relocatable && static_count == 0)
3011     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3012   else
3013     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3014
3015   synthetic_relocatable = relocatable;
3016   qsort (syms, symcount, sizeof (*syms), compare_symbols);
3017
3018   if (!relocatable && symcount > 1)
3019     {
3020       long j;
3021       /* Trim duplicate syms, since we may have merged the normal and
3022          dynamic symbols.  Actually, we only care about syms that have
3023          different values, so trim any with the same value.  */
3024       for (i = 1, j = 1; i < symcount; ++i)
3025         if (syms[i - 1]->value + syms[i - 1]->section->vma
3026             != syms[i]->value + syms[i]->section->vma)
3027           syms[j++] = syms[i];
3028       symcount = j;
3029     }
3030
3031   i = 0;
3032   if (strcmp (syms[i]->section->name, ".opd") == 0)
3033     ++i;
3034   codesecsym = i;
3035
3036   for (; i < symcount; ++i)
3037     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3038          != (SEC_CODE | SEC_ALLOC))
3039         || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3040       break;
3041   codesecsymend = i;
3042
3043   for (; i < symcount; ++i)
3044     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3045       break;
3046   secsymend = i;
3047
3048   for (; i < symcount; ++i)
3049     if (strcmp (syms[i]->section->name, ".opd") != 0)
3050       break;
3051   opdsymend = i;
3052
3053   for (; i < symcount; ++i)
3054     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3055         != (SEC_CODE | SEC_ALLOC))
3056       break;
3057   symcount = i;
3058
3059   count = 0;
3060
3061   if (relocatable)
3062     {
3063       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3064       arelent *r;
3065       size_t size;
3066       long relcount;
3067
3068       if (opdsymend == secsymend)
3069         goto done;
3070
3071       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3072       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3073       if (relcount == 0)
3074         goto done;
3075
3076       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3077         {
3078           count = -1;
3079           goto done;
3080         }
3081
3082       size = 0;
3083       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3084         {
3085           asymbol *sym;
3086
3087           while (r < opd->relocation + relcount
3088                  && r->address < syms[i]->value + opd->vma)
3089             ++r;
3090
3091           if (r == opd->relocation + relcount)
3092             break;
3093
3094           if (r->address != syms[i]->value + opd->vma)
3095             continue;
3096
3097           if (r->howto->type != R_PPC64_ADDR64)
3098             continue;
3099
3100           sym = *r->sym_ptr_ptr;
3101           if (!sym_exists_at (syms, opdsymend, symcount,
3102                               sym->section->id, sym->value + r->addend))
3103             {
3104               ++count;
3105               size += sizeof (asymbol);
3106               size += strlen (syms[i]->name) + 2;
3107             }
3108         }
3109
3110       s = *ret = bfd_malloc (size);
3111       if (s == NULL)
3112         {
3113           count = -1;
3114           goto done;
3115         }
3116
3117       names = (char *) (s + count);
3118
3119       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3120         {
3121           asymbol *sym;
3122
3123           while (r < opd->relocation + relcount
3124                  && r->address < syms[i]->value + opd->vma)
3125             ++r;
3126
3127           if (r == opd->relocation + relcount)
3128             break;
3129
3130           if (r->address != syms[i]->value + opd->vma)
3131             continue;
3132
3133           if (r->howto->type != R_PPC64_ADDR64)
3134             continue;
3135
3136           sym = *r->sym_ptr_ptr;
3137           if (!sym_exists_at (syms, opdsymend, symcount,
3138                               sym->section->id, sym->value + r->addend))
3139             {
3140               size_t len;
3141
3142               *s = *syms[i];
3143               s->flags |= BSF_SYNTHETIC;
3144               s->section = sym->section;
3145               s->value = sym->value + r->addend;
3146               s->name = names;
3147               *names++ = '.';
3148               len = strlen (syms[i]->name);
3149               memcpy (names, syms[i]->name, len + 1);
3150               names += len + 1;
3151               /* Have udata.p point back to the original symbol this
3152                  synthetic symbol was derived from.  */
3153               s->udata.p = syms[i];
3154               s++;
3155             }
3156         }
3157     }
3158   else
3159     {
3160       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3161       bfd_byte *contents;
3162       size_t size;
3163       long plt_count = 0;
3164       bfd_vma glink_vma = 0, resolv_vma = 0;
3165       asection *dynamic, *glink = NULL, *relplt = NULL;
3166       arelent *p;
3167
3168       if (!bfd_malloc_and_get_section (abfd, opd, &contents))
3169         {
3170           if (contents)
3171             {
3172             free_contents_and_exit:
3173               free (contents);
3174             }
3175           count = -1;
3176           goto done;
3177         }
3178
3179       size = 0;
3180       for (i = secsymend; i < opdsymend; ++i)
3181         {
3182           bfd_vma ent;
3183
3184           /* Ignore bogus symbols.  */
3185           if (syms[i]->value > opd->size - 8)
3186             continue;
3187
3188           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3189           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3190             {
3191               ++count;
3192               size += sizeof (asymbol);
3193               size += strlen (syms[i]->name) + 2;
3194             }
3195         }
3196
3197       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3198       if (dyn_count != 0
3199           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3200         {
3201           bfd_byte *dynbuf, *extdyn, *extdynend;
3202           size_t extdynsize;
3203           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3204
3205           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3206             goto free_contents_and_exit;
3207
3208           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3209           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3210
3211           extdyn = dynbuf;
3212           extdynend = extdyn + dynamic->size;
3213           for (; extdyn < extdynend; extdyn += extdynsize)
3214             {
3215               Elf_Internal_Dyn dyn;
3216               (*swap_dyn_in) (abfd, extdyn, &dyn);
3217
3218               if (dyn.d_tag == DT_NULL)
3219                 break;
3220
3221               if (dyn.d_tag == DT_PPC64_GLINK)
3222                 {
3223                   /* The first glink stub starts at offset 32; see comment in
3224                      ppc64_elf_finish_dynamic_sections. */
3225                   glink_vma = dyn.d_un.d_val + 32;
3226                   /* The .glink section usually does not survive the final
3227                      link; search for the section (usually .text) where the
3228                      glink stubs now reside.  */
3229                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3230                                                 &glink_vma);
3231                   break;
3232                 }
3233             }
3234
3235           free (dynbuf);
3236         }
3237
3238       if (glink != NULL)
3239         {
3240           /* Determine __glink trampoline by reading the relative branch
3241              from the first glink stub.  */
3242           bfd_byte buf[4];
3243           if (bfd_get_section_contents (abfd, glink, buf,
3244                                         glink_vma + 4 - glink->vma, 4))
3245             {
3246               unsigned int insn = bfd_get_32 (abfd, buf);
3247               insn ^= B_DOT;
3248               if ((insn & ~0x3fffffc) == 0)
3249                 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3250             }
3251
3252           if (resolv_vma)
3253             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3254
3255           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3256           if (relplt != NULL)
3257             {
3258               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3259               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3260                 goto free_contents_and_exit;
3261         
3262               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3263               size += plt_count * sizeof (asymbol);
3264
3265               p = relplt->relocation;
3266               for (i = 0; i < plt_count; i++, p++)
3267                 {
3268                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3269                   if (p->addend != 0)
3270                     size += sizeof ("+0x") - 1 + 16;
3271                 }
3272             }
3273         }
3274
3275       s = *ret = bfd_malloc (size);
3276       if (s == NULL)
3277         goto free_contents_and_exit;
3278
3279       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3280
3281       for (i = secsymend; i < opdsymend; ++i)
3282         {
3283           bfd_vma ent;
3284
3285           if (syms[i]->value > opd->size - 8)
3286             continue;
3287
3288           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3289           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3290             {
3291               long lo, hi;
3292               size_t len;
3293               asection *sec = abfd->sections;
3294
3295               *s = *syms[i];
3296               lo = codesecsym;
3297               hi = codesecsymend;
3298               while (lo < hi)
3299                 {
3300                   long mid = (lo + hi) >> 1;
3301                   if (syms[mid]->section->vma < ent)
3302                     lo = mid + 1;
3303                   else if (syms[mid]->section->vma > ent)
3304                     hi = mid;
3305                   else
3306                     {
3307                       sec = syms[mid]->section;
3308                       break;
3309                     }
3310                 }
3311
3312               if (lo >= hi && lo > codesecsym)
3313                 sec = syms[lo - 1]->section;
3314
3315               for (; sec != NULL; sec = sec->next)
3316                 {
3317                   if (sec->vma > ent)
3318                     break;
3319                   /* SEC_LOAD may not be set if SEC is from a separate debug
3320                      info file.  */
3321                   if ((sec->flags & SEC_ALLOC) == 0)
3322                     break;
3323                   if ((sec->flags & SEC_CODE) != 0)
3324                     s->section = sec;
3325                 }
3326               s->flags |= BSF_SYNTHETIC;
3327               s->value = ent - s->section->vma;
3328               s->name = names;
3329               *names++ = '.';
3330               len = strlen (syms[i]->name);
3331               memcpy (names, syms[i]->name, len + 1);
3332               names += len + 1;
3333               /* Have udata.p point back to the original symbol this
3334                  synthetic symbol was derived from.  */
3335               s->udata.p = syms[i];
3336               s++;
3337             }
3338         }
3339       free (contents);
3340
3341       if (glink != NULL && relplt != NULL)
3342         {
3343           if (resolv_vma)
3344             {
3345               /* Add a symbol for the main glink trampoline.  */
3346               memset (s, 0, sizeof *s);
3347               s->the_bfd = abfd;
3348               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3349               s->section = glink;
3350               s->value = resolv_vma - glink->vma;
3351               s->name = names;
3352               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3353               names += sizeof ("__glink_PLTresolve");
3354               s++;
3355               count++;
3356             }
3357
3358           /* FIXME: It would be very much nicer to put sym@plt on the
3359              stub rather than on the glink branch table entry.  The
3360              objdump disassembler would then use a sensible symbol
3361              name on plt calls.  The difficulty in doing so is
3362              a) finding the stubs, and,
3363              b) matching stubs against plt entries, and,
3364              c) there can be multiple stubs for a given plt entry.
3365
3366              Solving (a) could be done by code scanning, but older
3367              ppc64 binaries used different stubs to current code.
3368              (b) is the tricky one since you need to known the toc
3369              pointer for at least one function that uses a pic stub to
3370              be able to calculate the plt address referenced.
3371              (c) means gdb would need to set multiple breakpoints (or
3372              find the glink branch itself) when setting breakpoints
3373              for pending shared library loads.  */
3374           p = relplt->relocation;
3375           for (i = 0; i < plt_count; i++, p++)
3376             {
3377               size_t len;
3378
3379               *s = **p->sym_ptr_ptr;
3380               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3381                  we are defining a symbol, ensure one of them is set.  */
3382               if ((s->flags & BSF_LOCAL) == 0)
3383                 s->flags |= BSF_GLOBAL;
3384               s->flags |= BSF_SYNTHETIC;
3385               s->section = glink;
3386               s->value = glink_vma - glink->vma;
3387               s->name = names;
3388               s->udata.p = NULL;
3389               len = strlen ((*p->sym_ptr_ptr)->name);
3390               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3391               names += len;
3392               if (p->addend != 0)
3393                 {
3394                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3395                   names += sizeof ("+0x") - 1;
3396                   bfd_sprintf_vma (abfd, names, p->addend);
3397                   names += strlen (names);
3398                 }
3399               memcpy (names, "@plt", sizeof ("@plt"));
3400               names += sizeof ("@plt");
3401               s++;
3402               glink_vma += 8;
3403               if (i >= 0x8000)
3404                 glink_vma += 4;
3405             }
3406           count += plt_count;
3407         }
3408     }
3409
3410  done:
3411   free (syms);
3412   return count;
3413 }
3414 \f
3415 /* The following functions are specific to the ELF linker, while
3416    functions above are used generally.  Those named ppc64_elf_* are
3417    called by the main ELF linker code.  They appear in this file more
3418    or less in the order in which they are called.  eg.
3419    ppc64_elf_check_relocs is called early in the link process,
3420    ppc64_elf_finish_dynamic_sections is one of the last functions
3421    called.
3422
3423    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3424    functions have both a function code symbol and a function descriptor
3425    symbol.  A call to foo in a relocatable object file looks like:
3426
3427    .            .text
3428    .    x:
3429    .            bl      .foo
3430    .            nop
3431
3432    The function definition in another object file might be:
3433
3434    .            .section .opd
3435    .    foo:    .quad   .foo
3436    .            .quad   .TOC.@tocbase
3437    .            .quad   0
3438    .
3439    .            .text
3440    .    .foo:   blr
3441
3442    When the linker resolves the call during a static link, the branch
3443    unsurprisingly just goes to .foo and the .opd information is unused.
3444    If the function definition is in a shared library, things are a little
3445    different:  The call goes via a plt call stub, the opd information gets
3446    copied to the plt, and the linker patches the nop.
3447
3448    .    x:
3449    .            bl      .foo_stub
3450    .            ld      2,40(1)
3451    .
3452    .
3453    .    .foo_stub:
3454    .            addis   12,2,Lfoo@toc@ha        # in practice, the call stub
3455    .            addi    12,12,Lfoo@toc@l        # is slightly optimized, but
3456    .            std     2,40(1)                 # this is the general idea
3457    .            ld      11,0(12)
3458    .            ld      2,8(12)
3459    .            mtctr   11
3460    .            ld      11,16(12)
3461    .            bctr
3462    .
3463    .            .section .plt
3464    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3465
3466    The "reloc ()" notation is supposed to indicate that the linker emits
3467    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3468    copying.
3469
3470    What are the difficulties here?  Well, firstly, the relocations
3471    examined by the linker in check_relocs are against the function code
3472    sym .foo, while the dynamic relocation in the plt is emitted against
3473    the function descriptor symbol, foo.  Somewhere along the line, we need
3474    to carefully copy dynamic link information from one symbol to the other.
3475    Secondly, the generic part of the elf linker will make .foo a dynamic
3476    symbol as is normal for most other backends.  We need foo dynamic
3477    instead, at least for an application final link.  However, when
3478    creating a shared library containing foo, we need to have both symbols
3479    dynamic so that references to .foo are satisfied during the early
3480    stages of linking.  Otherwise the linker might decide to pull in a
3481    definition from some other object, eg. a static library.
3482
3483    Update: As of August 2004, we support a new convention.  Function
3484    calls may use the function descriptor symbol, ie. "bl foo".  This
3485    behaves exactly as "bl .foo".  */
3486
3487 /* Of those relocs that might be copied as dynamic relocs, this function
3488    selects those that must be copied when linking a shared library,
3489    even when the symbol is local.  */
3490
3491 static int
3492 must_be_dyn_reloc (struct bfd_link_info *info,
3493                    enum elf_ppc64_reloc_type r_type)
3494 {
3495   switch (r_type)
3496     {
3497     default:
3498       return 1;
3499
3500     case R_PPC64_REL32:
3501     case R_PPC64_REL64:
3502     case R_PPC64_REL30:
3503       return 0;
3504
3505     case R_PPC64_TPREL16:
3506     case R_PPC64_TPREL16_LO:
3507     case R_PPC64_TPREL16_HI:
3508     case R_PPC64_TPREL16_HA:
3509     case R_PPC64_TPREL16_DS:
3510     case R_PPC64_TPREL16_LO_DS:
3511     case R_PPC64_TPREL16_HIGHER:
3512     case R_PPC64_TPREL16_HIGHERA:
3513     case R_PPC64_TPREL16_HIGHEST:
3514     case R_PPC64_TPREL16_HIGHESTA:
3515     case R_PPC64_TPREL64:
3516       return !info->executable;
3517     }
3518 }
3519
3520 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3521    copying dynamic variables from a shared lib into an app's dynbss
3522    section, and instead use a dynamic relocation to point into the
3523    shared lib.  With code that gcc generates, it's vital that this be
3524    enabled;  In the PowerPC64 ABI, the address of a function is actually
3525    the address of a function descriptor, which resides in the .opd
3526    section.  gcc uses the descriptor directly rather than going via the
3527    GOT as some other ABI's do, which means that initialized function
3528    pointers must reference the descriptor.  Thus, a function pointer
3529    initialized to the address of a function in a shared library will
3530    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3531    redefines the function descriptor symbol to point to the copy.  This
3532    presents a problem as a plt entry for that function is also
3533    initialized from the function descriptor symbol and the copy reloc
3534    may not be initialized first.  */
3535 #define ELIMINATE_COPY_RELOCS 1
3536
3537 /* Section name for stubs is the associated section name plus this
3538    string.  */
3539 #define STUB_SUFFIX ".stub"
3540
3541 /* Linker stubs.
3542    ppc_stub_long_branch:
3543    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3544    destination, but a 24 bit branch in a stub section will reach.
3545    .    b       dest
3546
3547    ppc_stub_plt_branch:
3548    Similar to the above, but a 24 bit branch in the stub section won't
3549    reach its destination.
3550    .    addis   %r12,%r2,xxx@toc@ha
3551    .    ld      %r11,xxx@toc@l(%r12)
3552    .    mtctr   %r11
3553    .    bctr
3554
3555    ppc_stub_plt_call:
3556    Used to call a function in a shared library.  If it so happens that
3557    the plt entry referenced crosses a 64k boundary, then an extra
3558    "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
3559    .    addis   %r12,%r2,xxx@toc@ha
3560    .    std     %r2,40(%r1)
3561    .    ld      %r11,xxx+0@toc@l(%r12)
3562    .    mtctr   %r11
3563    .    ld      %r2,xxx+8@toc@l(%r12)
3564    .    ld      %r11,xxx+16@toc@l(%r12)
3565    .    bctr
3566
3567    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3568    code to adjust the value and save r2 to support multiple toc sections.
3569    A ppc_stub_long_branch with an r2 offset looks like:
3570    .    std     %r2,40(%r1)
3571    .    addis   %r2,%r2,off@ha
3572    .    addi    %r2,%r2,off@l
3573    .    b       dest
3574
3575    A ppc_stub_plt_branch with an r2 offset looks like:
3576    .    std     %r2,40(%r1)
3577    .    addis   %r12,%r2,xxx@toc@ha
3578    .    ld      %r11,xxx@toc@l(%r12)
3579    .    addis   %r2,%r2,off@ha
3580    .    addi    %r2,%r2,off@l
3581    .    mtctr   %r11
3582    .    bctr
3583
3584    In cases where the "addis" instruction would add zero, the "addis" is
3585    omitted and following instructions modified slightly in some cases.
3586 */
3587
3588 enum ppc_stub_type {
3589   ppc_stub_none,
3590   ppc_stub_long_branch,
3591   ppc_stub_long_branch_r2off,
3592   ppc_stub_plt_branch,
3593   ppc_stub_plt_branch_r2off,
3594   ppc_stub_plt_call,
3595   ppc_stub_plt_call_r2save
3596 };
3597
3598 struct ppc_stub_hash_entry {
3599
3600   /* Base hash table entry structure.  */
3601   struct bfd_hash_entry root;
3602
3603   enum ppc_stub_type stub_type;
3604
3605   /* The stub section.  */
3606   asection *stub_sec;
3607
3608   /* Offset within stub_sec of the beginning of this stub.  */
3609   bfd_vma stub_offset;
3610
3611   /* Given the symbol's value and its section we can determine its final
3612      value when building the stubs (so the stub knows where to jump.  */
3613   bfd_vma target_value;
3614   asection *target_section;
3615
3616   /* The symbol table entry, if any, that this was derived from.  */
3617   struct ppc_link_hash_entry *h;
3618   struct plt_entry *plt_ent;
3619
3620   /* And the reloc addend that this was derived from.  */
3621   bfd_vma addend;
3622
3623   /* Where this stub is being called from, or, in the case of combined
3624      stub sections, the first input section in the group.  */
3625   asection *id_sec;
3626 };
3627
3628 struct ppc_branch_hash_entry {
3629
3630   /* Base hash table entry structure.  */
3631   struct bfd_hash_entry root;
3632
3633   /* Offset within branch lookup table.  */
3634   unsigned int offset;
3635
3636   /* Generation marker.  */
3637   unsigned int iter;
3638 };
3639
3640 struct ppc_link_hash_entry
3641 {
3642   struct elf_link_hash_entry elf;
3643
3644   union {
3645     /* A pointer to the most recently used stub hash entry against this
3646        symbol.  */
3647     struct ppc_stub_hash_entry *stub_cache;
3648
3649     /* A pointer to the next symbol starting with a '.'  */
3650     struct ppc_link_hash_entry *next_dot_sym;
3651   } u;
3652
3653   /* Track dynamic relocs copied for this symbol.  */
3654   struct elf_dyn_relocs *dyn_relocs;
3655
3656   /* Link between function code and descriptor symbols.  */
3657   struct ppc_link_hash_entry *oh;
3658
3659   /* Flag function code and descriptor symbols.  */
3660   unsigned int is_func:1;
3661   unsigned int is_func_descriptor:1;
3662   unsigned int fake:1;
3663
3664   /* Whether global opd/toc sym has been adjusted or not.
3665      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3666      should be set for all globals defined in any opd/toc section.  */
3667   unsigned int adjust_done:1;
3668
3669   /* Set if we twiddled this symbol to weak at some stage.  */
3670   unsigned int was_undefined:1;
3671
3672   /* Contexts in which symbol is used in the GOT (or TOC).
3673      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3674      corresponding relocs are encountered during check_relocs.
3675      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3676      indicate the corresponding GOT entry type is not needed.
3677      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3678      a TPREL one.  We use a separate flag rather than setting TPREL
3679      just for convenience in distinguishing the two cases.  */
3680 #define TLS_GD           1      /* GD reloc. */
3681 #define TLS_LD           2      /* LD reloc. */
3682 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
3683 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
3684 #define TLS_TLS         16      /* Any TLS reloc.  */
3685 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
3686 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
3687 #define PLT_IFUNC      128      /* STT_GNU_IFUNC.  */
3688   unsigned char tls_mask;
3689 };
3690
3691 /* ppc64 ELF linker hash table.  */
3692
3693 struct ppc_link_hash_table
3694 {
3695   struct elf_link_hash_table elf;
3696
3697   /* The stub hash table.  */
3698   struct bfd_hash_table stub_hash_table;
3699
3700   /* Another hash table for plt_branch stubs.  */
3701   struct bfd_hash_table branch_hash_table;
3702
3703   /* Hash table for function prologue tocsave.  */
3704   htab_t tocsave_htab;
3705
3706   /* Linker stub bfd.  */
3707   bfd *stub_bfd;
3708
3709   /* Linker call-backs.  */
3710   asection * (*add_stub_section) (const char *, asection *);
3711   void (*layout_sections_again) (void);
3712
3713   /* Array to keep track of which stub sections have been created, and
3714      information on stub grouping.  */
3715   struct map_stub {
3716     /* This is the section to which stubs in the group will be attached.  */
3717     asection *link_sec;
3718     /* The stub section.  */
3719     asection *stub_sec;
3720     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
3721     bfd_vma toc_off;
3722   } *stub_group;
3723
3724   /* Temp used when calculating TOC pointers.  */
3725   bfd_vma toc_curr;
3726   bfd *toc_bfd;
3727   asection *toc_first_sec;
3728
3729   /* Highest input section id.  */
3730   int top_id;
3731
3732   /* Highest output section index.  */
3733   int top_index;
3734
3735   /* Used when adding symbols.  */
3736   struct ppc_link_hash_entry *dot_syms;
3737
3738   /* List of input sections for each output section.  */
3739   asection **input_list;
3740
3741   /* Short-cuts to get to dynamic linker sections.  */
3742   asection *got;
3743   asection *plt;
3744   asection *relplt;
3745   asection *iplt;
3746   asection *reliplt;
3747   asection *dynbss;
3748   asection *relbss;
3749   asection *glink;
3750   asection *sfpr;
3751   asection *brlt;
3752   asection *relbrlt;
3753   asection *glink_eh_frame;
3754
3755   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3756   struct ppc_link_hash_entry *tls_get_addr;
3757   struct ppc_link_hash_entry *tls_get_addr_fd;
3758
3759   /* The size of reliplt used by got entry relocs.  */
3760   bfd_size_type got_reli_size;
3761
3762   /* Statistics.  */
3763   unsigned long stub_count[ppc_stub_plt_call_r2save];
3764
3765   /* Number of stubs against global syms.  */
3766   unsigned long stub_globals;
3767
3768   /* Alignment of PLT call stubs.  */
3769   unsigned int plt_stub_align:4;
3770
3771   /* Set if PLT call stubs should load r11.  */
3772   unsigned int plt_static_chain:1;
3773
3774   /* Set if PLT call stubs need a read-read barrier.  */
3775   unsigned int plt_thread_safe:1;
3776
3777   /* Set if we should emit symbols for stubs.  */
3778   unsigned int emit_stub_syms:1;
3779
3780   /* Set if __tls_get_addr optimization should not be done.  */
3781   unsigned int no_tls_get_addr_opt:1;
3782
3783   /* Support for multiple toc sections.  */
3784   unsigned int do_multi_toc:1;
3785   unsigned int multi_toc_needed:1;
3786   unsigned int second_toc_pass:1;
3787   unsigned int do_toc_opt:1;
3788
3789   /* Set on error.  */
3790   unsigned int stub_error:1;
3791
3792   /* Temp used by ppc64_elf_process_dot_syms.  */
3793   unsigned int twiddled_syms:1;
3794
3795   /* Incremented every time we size stubs.  */
3796   unsigned int stub_iteration;
3797
3798   /* Small local sym cache.  */
3799   struct sym_cache sym_cache;
3800 };
3801
3802 /* Rename some of the generic section flags to better document how they
3803    are used here.  */
3804
3805 /* Nonzero if this section has TLS related relocations.  */
3806 #define has_tls_reloc sec_flg0
3807
3808 /* Nonzero if this section has a call to __tls_get_addr.  */
3809 #define has_tls_get_addr_call sec_flg1
3810
3811 /* Nonzero if this section has any toc or got relocs.  */
3812 #define has_toc_reloc sec_flg2
3813
3814 /* Nonzero if this section has a call to another section that uses
3815    the toc or got.  */
3816 #define makes_toc_func_call sec_flg3
3817
3818 /* Recursion protection when determining above flag.  */
3819 #define call_check_in_progress sec_flg4
3820 #define call_check_done sec_flg5
3821
3822 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
3823
3824 #define ppc_hash_table(p) \
3825   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3826   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3827
3828 #define ppc_stub_hash_lookup(table, string, create, copy) \
3829   ((struct ppc_stub_hash_entry *) \
3830    bfd_hash_lookup ((table), (string), (create), (copy)))
3831
3832 #define ppc_branch_hash_lookup(table, string, create, copy) \
3833   ((struct ppc_branch_hash_entry *) \
3834    bfd_hash_lookup ((table), (string), (create), (copy)))
3835
3836 /* Create an entry in the stub hash table.  */
3837
3838 static struct bfd_hash_entry *
3839 stub_hash_newfunc (struct bfd_hash_entry *entry,
3840                    struct bfd_hash_table *table,
3841                    const char *string)
3842 {
3843   /* Allocate the structure if it has not already been allocated by a
3844      subclass.  */
3845   if (entry == NULL)
3846     {
3847       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3848       if (entry == NULL)
3849         return entry;
3850     }
3851
3852   /* Call the allocation method of the superclass.  */
3853   entry = bfd_hash_newfunc (entry, table, string);
3854   if (entry != NULL)
3855     {
3856       struct ppc_stub_hash_entry *eh;
3857
3858       /* Initialize the local fields.  */
3859       eh = (struct ppc_stub_hash_entry *) entry;
3860       eh->stub_type = ppc_stub_none;
3861       eh->stub_sec = NULL;
3862       eh->stub_offset = 0;
3863       eh->target_value = 0;
3864       eh->target_section = NULL;
3865       eh->h = NULL;
3866       eh->id_sec = NULL;
3867     }
3868
3869   return entry;
3870 }
3871
3872 /* Create an entry in the branch hash table.  */
3873
3874 static struct bfd_hash_entry *
3875 branch_hash_newfunc (struct bfd_hash_entry *entry,
3876                      struct bfd_hash_table *table,
3877                      const char *string)
3878 {
3879   /* Allocate the structure if it has not already been allocated by a
3880      subclass.  */
3881   if (entry == NULL)
3882     {
3883       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3884       if (entry == NULL)
3885         return entry;
3886     }
3887
3888   /* Call the allocation method of the superclass.  */
3889   entry = bfd_hash_newfunc (entry, table, string);
3890   if (entry != NULL)
3891     {
3892       struct ppc_branch_hash_entry *eh;
3893
3894       /* Initialize the local fields.  */
3895       eh = (struct ppc_branch_hash_entry *) entry;
3896       eh->offset = 0;
3897       eh->iter = 0;
3898     }
3899
3900   return entry;
3901 }
3902
3903 /* Create an entry in a ppc64 ELF linker hash table.  */
3904
3905 static struct bfd_hash_entry *
3906 link_hash_newfunc (struct bfd_hash_entry *entry,
3907                    struct bfd_hash_table *table,
3908                    const char *string)
3909 {
3910   /* Allocate the structure if it has not already been allocated by a
3911      subclass.  */
3912   if (entry == NULL)
3913     {
3914       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3915       if (entry == NULL)
3916         return entry;
3917     }
3918
3919   /* Call the allocation method of the superclass.  */
3920   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3921   if (entry != NULL)
3922     {
3923       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3924
3925       memset (&eh->u.stub_cache, 0,
3926               (sizeof (struct ppc_link_hash_entry)
3927                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3928
3929       /* When making function calls, old ABI code references function entry
3930          points (dot symbols), while new ABI code references the function
3931          descriptor symbol.  We need to make any combination of reference and
3932          definition work together, without breaking archive linking.
3933
3934          For a defined function "foo" and an undefined call to "bar":
3935          An old object defines "foo" and ".foo", references ".bar" (possibly
3936          "bar" too).
3937          A new object defines "foo" and references "bar".
3938
3939          A new object thus has no problem with its undefined symbols being
3940          satisfied by definitions in an old object.  On the other hand, the
3941          old object won't have ".bar" satisfied by a new object.
3942
3943          Keep a list of newly added dot-symbols.  */
3944
3945       if (string[0] == '.')
3946         {
3947           struct ppc_link_hash_table *htab;
3948
3949           htab = (struct ppc_link_hash_table *) table;
3950           eh->u.next_dot_sym = htab->dot_syms;
3951           htab->dot_syms = eh;
3952         }
3953     }
3954
3955   return entry;
3956 }
3957
3958 struct tocsave_entry {
3959   asection *sec;
3960   bfd_vma offset;
3961 };
3962
3963 static hashval_t
3964 tocsave_htab_hash (const void *p)
3965 {
3966   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3967   return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
3968 }
3969
3970 static int
3971 tocsave_htab_eq (const void *p1, const void *p2)
3972 {
3973   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3974   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3975   return e1->sec == e2->sec && e1->offset == e2->offset;
3976 }
3977
3978 /* Create a ppc64 ELF linker hash table.  */
3979
3980 static struct bfd_link_hash_table *
3981 ppc64_elf_link_hash_table_create (bfd *abfd)
3982 {
3983   struct ppc_link_hash_table *htab;
3984   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3985
3986   htab = bfd_zmalloc (amt);
3987   if (htab == NULL)
3988     return NULL;
3989
3990   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3991                                       sizeof (struct ppc_link_hash_entry),
3992                                       PPC64_ELF_DATA))
3993     {
3994       free (htab);
3995       return NULL;
3996     }
3997
3998   /* Init the stub hash table too.  */
3999   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4000                             sizeof (struct ppc_stub_hash_entry)))
4001     return NULL;
4002
4003   /* And the branch hash table.  */
4004   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4005                             sizeof (struct ppc_branch_hash_entry)))
4006     return NULL;
4007
4008   htab->tocsave_htab = htab_try_create (1024,
4009                                         tocsave_htab_hash,
4010                                         tocsave_htab_eq,
4011                                         NULL);
4012   if (htab->tocsave_htab == NULL)
4013     return NULL;
4014
4015   /* Initializing two fields of the union is just cosmetic.  We really
4016      only care about glist, but when compiled on a 32-bit host the
4017      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
4018      debugger inspection of these fields look nicer.  */
4019   htab->elf.init_got_refcount.refcount = 0;
4020   htab->elf.init_got_refcount.glist = NULL;
4021   htab->elf.init_plt_refcount.refcount = 0;
4022   htab->elf.init_plt_refcount.glist = NULL;
4023   htab->elf.init_got_offset.offset = 0;
4024   htab->elf.init_got_offset.glist = NULL;
4025   htab->elf.init_plt_offset.offset = 0;
4026   htab->elf.init_plt_offset.glist = NULL;
4027
4028   return &htab->elf.root;
4029 }
4030
4031 /* Free the derived linker hash table.  */
4032
4033 static void
4034 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
4035 {
4036   struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
4037
4038   bfd_hash_table_free (&htab->stub_hash_table);
4039   bfd_hash_table_free (&htab->branch_hash_table);
4040   if (htab->tocsave_htab)
4041     htab_delete (htab->tocsave_htab);
4042   _bfd_generic_link_hash_table_free (hash);
4043 }
4044
4045 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4046
4047 void
4048 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4049 {
4050   struct ppc_link_hash_table *htab;
4051
4052   elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4053
4054 /* Always hook our dynamic sections into the first bfd, which is the
4055    linker created stub bfd.  This ensures that the GOT header is at
4056    the start of the output TOC section.  */
4057   htab = ppc_hash_table (info);
4058   if (htab == NULL)
4059     return;
4060   htab->stub_bfd = abfd;
4061   htab->elf.dynobj = abfd;
4062 }
4063
4064 /* Build a name for an entry in the stub hash table.  */
4065
4066 static char *
4067 ppc_stub_name (const asection *input_section,
4068                const asection *sym_sec,
4069                const struct ppc_link_hash_entry *h,
4070                const Elf_Internal_Rela *rel)
4071 {
4072   char *stub_name;
4073   bfd_size_type len;
4074
4075   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4076      offsets from a sym as a branch target?  In fact, we could
4077      probably assume the addend is always zero.  */
4078   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4079
4080   if (h)
4081     {
4082       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4083       stub_name = bfd_malloc (len);
4084       if (stub_name == NULL)
4085         return stub_name;
4086
4087       sprintf (stub_name, "%08x.%s+%x",
4088                input_section->id & 0xffffffff,
4089                h->elf.root.root.string,
4090                (int) rel->r_addend & 0xffffffff);
4091     }
4092   else
4093     {
4094       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4095       stub_name = bfd_malloc (len);
4096       if (stub_name == NULL)
4097         return stub_name;
4098
4099       sprintf (stub_name, "%08x.%x:%x+%x",
4100                input_section->id & 0xffffffff,
4101                sym_sec->id & 0xffffffff,
4102                (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4103                (int) rel->r_addend & 0xffffffff);
4104     }
4105   if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4106     stub_name[len - 2] = 0;
4107   return stub_name;
4108 }
4109
4110 /* Look up an entry in the stub hash.  Stub entries are cached because
4111    creating the stub name takes a bit of time.  */
4112
4113 static struct ppc_stub_hash_entry *
4114 ppc_get_stub_entry (const asection *input_section,
4115                     const asection *sym_sec,
4116                     struct ppc_link_hash_entry *h,
4117                     const Elf_Internal_Rela *rel,
4118                     struct ppc_link_hash_table *htab)
4119 {
4120   struct ppc_stub_hash_entry *stub_entry;
4121   const asection *id_sec;
4122
4123   /* If this input section is part of a group of sections sharing one
4124      stub section, then use the id of the first section in the group.
4125      Stub names need to include a section id, as there may well be
4126      more than one stub used to reach say, printf, and we need to
4127      distinguish between them.  */
4128   id_sec = htab->stub_group[input_section->id].link_sec;
4129
4130   if (h != NULL && h->u.stub_cache != NULL
4131       && h->u.stub_cache->h == h
4132       && h->u.stub_cache->id_sec == id_sec)
4133     {
4134       stub_entry = h->u.stub_cache;
4135     }
4136   else
4137     {
4138       char *stub_name;
4139
4140       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4141       if (stub_name == NULL)
4142         return NULL;
4143
4144       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4145                                          stub_name, FALSE, FALSE);
4146       if (h != NULL)
4147         h->u.stub_cache = stub_entry;
4148
4149       free (stub_name);
4150     }
4151
4152   return stub_entry;
4153 }
4154
4155 /* Add a new stub entry to the stub hash.  Not all fields of the new
4156    stub entry are initialised.  */
4157
4158 static struct ppc_stub_hash_entry *
4159 ppc_add_stub (const char *stub_name,
4160               asection *section,
4161               struct bfd_link_info *info)
4162 {
4163   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4164   asection *link_sec;
4165   asection *stub_sec;
4166   struct ppc_stub_hash_entry *stub_entry;
4167
4168   link_sec = htab->stub_group[section->id].link_sec;
4169   stub_sec = htab->stub_group[section->id].stub_sec;
4170   if (stub_sec == NULL)
4171     {
4172       stub_sec = htab->stub_group[link_sec->id].stub_sec;
4173       if (stub_sec == NULL)
4174         {
4175           size_t namelen;
4176           bfd_size_type len;
4177           char *s_name;
4178
4179           namelen = strlen (link_sec->name);
4180           len = namelen + sizeof (STUB_SUFFIX);
4181           s_name = bfd_alloc (htab->stub_bfd, len);
4182           if (s_name == NULL)
4183             return NULL;
4184
4185           memcpy (s_name, link_sec->name, namelen);
4186           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4187           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4188           if (stub_sec == NULL)
4189             return NULL;
4190           htab->stub_group[link_sec->id].stub_sec = stub_sec;
4191         }
4192       htab->stub_group[section->id].stub_sec = stub_sec;
4193     }
4194
4195   /* Enter this entry into the linker stub hash table.  */
4196   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4197                                      TRUE, FALSE);
4198   if (stub_entry == NULL)
4199     {
4200       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4201                               section->owner, stub_name);
4202       return NULL;
4203     }
4204
4205   stub_entry->stub_sec = stub_sec;
4206   stub_entry->stub_offset = 0;
4207   stub_entry->id_sec = link_sec;
4208   return stub_entry;
4209 }
4210
4211 /* Create sections for linker generated code.  */
4212
4213 static bfd_boolean
4214 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4215 {
4216   struct ppc_link_hash_table *htab;
4217   flagword flags;
4218
4219   htab = ppc_hash_table (info);
4220   if (htab == NULL)
4221     return FALSE;
4222
4223   /* Create .sfpr for code to save and restore fp regs.  */
4224   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4225            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4226   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4227                                                    flags);
4228   if (htab->sfpr == NULL
4229       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4230     return FALSE;
4231
4232   /* Create .glink for lazy dynamic linking support.  */
4233   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4234                                                     flags);
4235   if (htab->glink == NULL
4236       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4237     return FALSE;
4238
4239   if (!info->no_ld_generated_unwind_info)
4240     {
4241       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4242                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4243       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4244                                                                  ".eh_frame",
4245                                                                  flags);
4246       if (htab->glink_eh_frame == NULL
4247           || !bfd_set_section_alignment (abfd, htab->glink_eh_frame, 2))
4248         return FALSE;
4249     }
4250
4251   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4252   htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4253   if (htab->iplt == NULL
4254       || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4255     return FALSE;
4256
4257   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4258            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4259   htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4260                                                       ".rela.iplt",
4261                                                       flags);
4262   if (htab->reliplt == NULL
4263       || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4264     return FALSE;
4265
4266   /* Create branch lookup table for plt_branch stubs.  */
4267   flags = (SEC_ALLOC | SEC_LOAD
4268            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4269   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4270                                                    flags);
4271   if (htab->brlt == NULL
4272       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4273     return FALSE;
4274
4275   if (!info->shared)
4276     return TRUE;
4277
4278   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4279            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4280   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4281                                                       ".rela.branch_lt",
4282                                                       flags);
4283   if (htab->relbrlt == NULL
4284       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4285     return FALSE;
4286
4287   return TRUE;
4288 }
4289
4290 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4291    not already done.  */
4292
4293 static bfd_boolean
4294 create_got_section (bfd *abfd, struct bfd_link_info *info)
4295 {
4296   asection *got, *relgot;
4297   flagword flags;
4298   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4299
4300   if (!is_ppc64_elf (abfd))
4301     return FALSE;
4302   if (htab == NULL)
4303     return FALSE;
4304
4305   if (!htab->got)
4306     {
4307       if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4308         return FALSE;
4309
4310       htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4311       if (!htab->got)
4312         abort ();
4313     }
4314
4315   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4316            | SEC_LINKER_CREATED);
4317
4318   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4319   if (!got
4320       || !bfd_set_section_alignment (abfd, got, 3))
4321     return FALSE;
4322
4323   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4324                                                flags | SEC_READONLY);
4325   if (!relgot
4326       || ! bfd_set_section_alignment (abfd, relgot, 3))
4327     return FALSE;
4328
4329   ppc64_elf_tdata (abfd)->got = got;
4330   ppc64_elf_tdata (abfd)->relgot = relgot;
4331   return TRUE;
4332 }
4333
4334 /* Create the dynamic sections, and set up shortcuts.  */
4335
4336 static bfd_boolean
4337 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4338 {
4339   struct ppc_link_hash_table *htab;
4340
4341   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4342     return FALSE;
4343
4344   htab = ppc_hash_table (info);
4345   if (htab == NULL)
4346     return FALSE;
4347
4348   if (!htab->got)
4349     htab->got = bfd_get_section_by_name (dynobj, ".got");
4350   htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4351   htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4352   htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
4353   if (!info->shared)
4354     htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
4355
4356   if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4357       || (!info->shared && !htab->relbss))
4358     abort ();
4359
4360   return TRUE;
4361 }
4362
4363 /* Follow indirect and warning symbol links.  */
4364
4365 static inline struct bfd_link_hash_entry *
4366 follow_link (struct bfd_link_hash_entry *h)
4367 {
4368   while (h->type == bfd_link_hash_indirect
4369          || h->type == bfd_link_hash_warning)
4370     h = h->u.i.link;
4371   return h;
4372 }
4373
4374 static inline struct elf_link_hash_entry *
4375 elf_follow_link (struct elf_link_hash_entry *h)
4376 {
4377   return (struct elf_link_hash_entry *) follow_link (&h->root);
4378 }
4379
4380 static inline struct ppc_link_hash_entry *
4381 ppc_follow_link (struct ppc_link_hash_entry *h)
4382 {
4383   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4384 }
4385
4386 /* Merge PLT info on FROM with that on TO.  */
4387
4388 static void
4389 move_plt_plist (struct ppc_link_hash_entry *from,
4390                 struct ppc_link_hash_entry *to)
4391 {
4392   if (from->elf.plt.plist != NULL)
4393     {
4394       if (to->elf.plt.plist != NULL)
4395         {
4396           struct plt_entry **entp;
4397           struct plt_entry *ent;
4398
4399           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4400             {
4401               struct plt_entry *dent;
4402
4403               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4404                 if (dent->addend == ent->addend)
4405                   {
4406                     dent->plt.refcount += ent->plt.refcount;
4407                     *entp = ent->next;
4408                     break;
4409                   }
4410               if (dent == NULL)
4411                 entp = &ent->next;
4412             }
4413           *entp = to->elf.plt.plist;
4414         }
4415
4416       to->elf.plt.plist = from->elf.plt.plist;
4417       from->elf.plt.plist = NULL;
4418     }
4419 }
4420
4421 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4422
4423 static void
4424 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4425                                 struct elf_link_hash_entry *dir,
4426                                 struct elf_link_hash_entry *ind)
4427 {
4428   struct ppc_link_hash_entry *edir, *eind;
4429
4430   edir = (struct ppc_link_hash_entry *) dir;
4431   eind = (struct ppc_link_hash_entry *) ind;
4432
4433   edir->is_func |= eind->is_func;
4434   edir->is_func_descriptor |= eind->is_func_descriptor;
4435   edir->tls_mask |= eind->tls_mask;
4436   if (eind->oh != NULL)
4437     edir->oh = ppc_follow_link (eind->oh);
4438
4439   /* If called to transfer flags for a weakdef during processing
4440      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4441      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
4442   if (!(ELIMINATE_COPY_RELOCS
4443         && eind->elf.root.type != bfd_link_hash_indirect
4444         && edir->elf.dynamic_adjusted))
4445     edir->elf.non_got_ref |= eind->elf.non_got_ref;
4446
4447   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4448   edir->elf.ref_regular |= eind->elf.ref_regular;
4449   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4450   edir->elf.needs_plt |= eind->elf.needs_plt;
4451
4452   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4453   if (eind->dyn_relocs != NULL)
4454     {
4455       if (edir->dyn_relocs != NULL)
4456         {
4457           struct elf_dyn_relocs **pp;
4458           struct elf_dyn_relocs *p;
4459
4460           /* Add reloc counts against the indirect sym to the direct sym
4461              list.  Merge any entries against the same section.  */
4462           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4463             {
4464               struct elf_dyn_relocs *q;
4465
4466               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4467                 if (q->sec == p->sec)
4468                   {
4469                     q->pc_count += p->pc_count;
4470                     q->count += p->count;
4471                     *pp = p->next;
4472                     break;
4473                   }
4474               if (q == NULL)
4475                 pp = &p->next;
4476             }
4477           *pp = edir->dyn_relocs;
4478         }
4479
4480       edir->dyn_relocs = eind->dyn_relocs;
4481       eind->dyn_relocs = NULL;
4482     }
4483
4484   /* If we were called to copy over info for a weak sym, that's all.
4485      You might think dyn_relocs need not be copied over;  After all,
4486      both syms will be dynamic or both non-dynamic so we're just
4487      moving reloc accounting around.  However, ELIMINATE_COPY_RELOCS 
4488      code in ppc64_elf_adjust_dynamic_symbol needs to check for
4489      dyn_relocs in read-only sections, and it does so on what is the
4490      DIR sym here.  */
4491   if (eind->elf.root.type != bfd_link_hash_indirect)
4492     return;
4493
4494   /* Copy over got entries that we may have already seen to the
4495      symbol which just became indirect.  */
4496   if (eind->elf.got.glist != NULL)
4497     {
4498       if (edir->elf.got.glist != NULL)
4499         {
4500           struct got_entry **entp;
4501           struct got_entry *ent;
4502
4503           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4504             {
4505               struct got_entry *dent;
4506
4507               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4508                 if (dent->addend == ent->addend
4509                     && dent->owner == ent->owner
4510                     && dent->tls_type == ent->tls_type)
4511                   {
4512                     dent->got.refcount += ent->got.refcount;
4513                     *entp = ent->next;
4514                     break;
4515                   }
4516               if (dent == NULL)
4517                 entp = &ent->next;
4518             }
4519           *entp = edir->elf.got.glist;
4520         }
4521
4522       edir->elf.got.glist = eind->elf.got.glist;
4523       eind->elf.got.glist = NULL;
4524     }
4525
4526   /* And plt entries.  */
4527   move_plt_plist (eind, edir);
4528
4529   if (eind->elf.dynindx != -1)
4530     {
4531       if (edir->elf.dynindx != -1)
4532         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4533                                 edir->elf.dynstr_index);
4534       edir->elf.dynindx = eind->elf.dynindx;
4535       edir->elf.dynstr_index = eind->elf.dynstr_index;
4536       eind->elf.dynindx = -1;
4537       eind->elf.dynstr_index = 0;
4538     }
4539 }
4540
4541 /* Find the function descriptor hash entry from the given function code
4542    hash entry FH.  Link the entries via their OH fields.  */
4543
4544 static struct ppc_link_hash_entry *
4545 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4546 {
4547   struct ppc_link_hash_entry *fdh = fh->oh;
4548
4549   if (fdh == NULL)
4550     {
4551       const char *fd_name = fh->elf.root.root.string + 1;
4552
4553       fdh = (struct ppc_link_hash_entry *)
4554         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4555       if (fdh == NULL)
4556         return fdh;
4557
4558       fdh->is_func_descriptor = 1;
4559       fdh->oh = fh;
4560       fh->is_func = 1;
4561       fh->oh = fdh;
4562     }
4563
4564   return ppc_follow_link (fdh);
4565 }
4566
4567 /* Make a fake function descriptor sym for the code sym FH.  */
4568
4569 static struct ppc_link_hash_entry *
4570 make_fdh (struct bfd_link_info *info,
4571           struct ppc_link_hash_entry *fh)
4572 {
4573   bfd *abfd;
4574   asymbol *newsym;
4575   struct bfd_link_hash_entry *bh;
4576   struct ppc_link_hash_entry *fdh;
4577
4578   abfd = fh->elf.root.u.undef.abfd;
4579   newsym = bfd_make_empty_symbol (abfd);
4580   newsym->name = fh->elf.root.root.string + 1;
4581   newsym->section = bfd_und_section_ptr;
4582   newsym->value = 0;
4583   newsym->flags = BSF_WEAK;
4584
4585   bh = NULL;
4586   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4587                                          newsym->flags, newsym->section,
4588                                          newsym->value, NULL, FALSE, FALSE,
4589                                          &bh))
4590     return NULL;
4591
4592   fdh = (struct ppc_link_hash_entry *) bh;
4593   fdh->elf.non_elf = 0;
4594   fdh->fake = 1;
4595   fdh->is_func_descriptor = 1;
4596   fdh->oh = fh;
4597   fh->is_func = 1;
4598   fh->oh = fdh;
4599   return fdh;
4600 }
4601
4602 /* Fix function descriptor symbols defined in .opd sections to be
4603    function type.  */
4604
4605 static bfd_boolean
4606 ppc64_elf_add_symbol_hook (bfd *ibfd,
4607                            struct bfd_link_info *info,
4608                            Elf_Internal_Sym *isym,
4609                            const char **name ATTRIBUTE_UNUSED,
4610                            flagword *flags ATTRIBUTE_UNUSED,
4611                            asection **sec,
4612                            bfd_vma *value ATTRIBUTE_UNUSED)
4613 {
4614   if ((ibfd->flags & DYNAMIC) == 0
4615       && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4616     elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4617
4618   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4619     {
4620       if ((ibfd->flags & DYNAMIC) == 0)
4621         elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4622     }
4623   else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4624     ;
4625   else if (*sec != NULL
4626            && strcmp ((*sec)->name, ".opd") == 0)
4627     isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4628
4629   return TRUE;
4630 }
4631
4632 /* This function makes an old ABI object reference to ".bar" cause the
4633    inclusion of a new ABI object archive that defines "bar".
4634    NAME is a symbol defined in an archive.  Return a symbol in the hash
4635    table that might be satisfied by the archive symbols.  */
4636
4637 static struct elf_link_hash_entry *
4638 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4639                                  struct bfd_link_info *info,
4640                                  const char *name)
4641 {
4642   struct elf_link_hash_entry *h;
4643   char *dot_name;
4644   size_t len;
4645
4646   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4647   if (h != NULL
4648       /* Don't return this sym if it is a fake function descriptor
4649          created by add_symbol_adjust.  */
4650       && !(h->root.type == bfd_link_hash_undefweak
4651            && ((struct ppc_link_hash_entry *) h)->fake))
4652     return h;
4653
4654   if (name[0] == '.')
4655     return h;
4656
4657   len = strlen (name);
4658   dot_name = bfd_alloc (abfd, len + 2);
4659   if (dot_name == NULL)
4660     return (struct elf_link_hash_entry *) 0 - 1;
4661   dot_name[0] = '.';
4662   memcpy (dot_name + 1, name, len + 1);
4663   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4664   bfd_release (abfd, dot_name);
4665   return h;
4666 }
4667
4668 /* This function satisfies all old ABI object references to ".bar" if a
4669    new ABI object defines "bar".  Well, at least, undefined dot symbols
4670    are made weak.  This stops later archive searches from including an
4671    object if we already have a function descriptor definition.  It also
4672    prevents the linker complaining about undefined symbols.
4673    We also check and correct mismatched symbol visibility here.  The
4674    most restrictive visibility of the function descriptor and the
4675    function entry symbol is used.  */
4676
4677 static bfd_boolean
4678 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4679 {
4680   struct ppc_link_hash_table *htab;
4681   struct ppc_link_hash_entry *fdh;
4682
4683   if (eh->elf.root.type == bfd_link_hash_indirect)
4684     return TRUE;
4685
4686   if (eh->elf.root.type == bfd_link_hash_warning)
4687     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4688
4689   if (eh->elf.root.root.string[0] != '.')
4690     abort ();
4691
4692   htab = ppc_hash_table (info);
4693   if (htab == NULL)
4694     return FALSE;
4695
4696   fdh = lookup_fdh (eh, htab);
4697   if (fdh == NULL)
4698     {
4699       if (!info->relocatable
4700           && (eh->elf.root.type == bfd_link_hash_undefined
4701               || eh->elf.root.type == bfd_link_hash_undefweak)
4702           && eh->elf.ref_regular)
4703         {
4704           /* Make an undefweak function descriptor sym, which is enough to
4705              pull in an --as-needed shared lib, but won't cause link
4706              errors.  Archives are handled elsewhere.  */
4707           fdh = make_fdh (info, eh);
4708           if (fdh == NULL)
4709             return FALSE;
4710           fdh->elf.ref_regular = 1;
4711         }
4712     }
4713   else
4714     {
4715       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4716       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4717       if (entry_vis < descr_vis)
4718         fdh->elf.other += entry_vis - descr_vis;
4719       else if (entry_vis > descr_vis)
4720         eh->elf.other += descr_vis - entry_vis;
4721
4722       if ((fdh->elf.root.type == bfd_link_hash_defined
4723            || fdh->elf.root.type == bfd_link_hash_defweak)
4724           && eh->elf.root.type == bfd_link_hash_undefined)
4725         {
4726           eh->elf.root.type = bfd_link_hash_undefweak;
4727           eh->was_undefined = 1;
4728           htab->twiddled_syms = 1;
4729         }
4730     }
4731
4732   return TRUE;
4733 }
4734
4735 /* Process list of dot-symbols we made in link_hash_newfunc.  */
4736
4737 static bfd_boolean
4738 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
4739 {
4740   struct ppc_link_hash_table *htab;
4741   struct ppc_link_hash_entry **p, *eh;
4742
4743   if (!is_ppc64_elf (info->output_bfd))
4744     return TRUE;
4745   htab = ppc_hash_table (info);
4746   if (htab == NULL)
4747     return FALSE;
4748
4749   if (is_ppc64_elf (ibfd))
4750     {
4751       p = &htab->dot_syms;
4752       while ((eh = *p) != NULL)
4753         {
4754           *p = NULL;
4755           if (!add_symbol_adjust (eh, info))
4756             return FALSE;
4757           p = &eh->u.next_dot_sym;
4758         }
4759     }
4760
4761   /* Clear the list for non-ppc64 input files.  */
4762   p = &htab->dot_syms;
4763   while ((eh = *p) != NULL)
4764     {
4765       *p = NULL;
4766       p = &eh->u.next_dot_sym;
4767     }
4768
4769   /* We need to fix the undefs list for any syms we have twiddled to
4770      undef_weak.  */
4771   if (htab->twiddled_syms)
4772     {
4773       bfd_link_repair_undef_list (&htab->elf.root);
4774       htab->twiddled_syms = 0;
4775     }
4776   return TRUE;
4777 }
4778
4779 /* Undo hash table changes when an --as-needed input file is determined
4780    not to be needed.  */
4781
4782 static bfd_boolean
4783 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4784                              struct bfd_link_info *info)
4785 {
4786   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4787
4788   if (htab == NULL)
4789     return FALSE;
4790
4791   htab->dot_syms = NULL;
4792   return TRUE;
4793 }
4794
4795 /* If --just-symbols against a final linked binary, then assume we need
4796    toc adjusting stubs when calling functions defined there.  */
4797
4798 static void
4799 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4800 {
4801   if ((sec->flags & SEC_CODE) != 0
4802       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4803       && is_ppc64_elf (sec->owner))
4804     {
4805       asection *got = bfd_get_section_by_name (sec->owner, ".got");
4806       if (got != NULL
4807           && got->size >= elf_backend_got_header_size
4808           && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4809         sec->has_toc_reloc = 1;
4810     }
4811   _bfd_elf_link_just_syms (sec, info);
4812 }
4813
4814 static struct plt_entry **
4815 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4816                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4817 {
4818   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4819   struct plt_entry **local_plt;
4820   unsigned char *local_got_tls_masks;
4821
4822   if (local_got_ents == NULL)
4823     {
4824       bfd_size_type size = symtab_hdr->sh_info;
4825
4826       size *= (sizeof (*local_got_ents)
4827                + sizeof (*local_plt)
4828                + sizeof (*local_got_tls_masks));
4829       local_got_ents = bfd_zalloc (abfd, size);
4830       if (local_got_ents == NULL)
4831         return NULL;
4832       elf_local_got_ents (abfd) = local_got_ents;
4833     }
4834
4835   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
4836     {
4837       struct got_entry *ent;
4838
4839       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4840         if (ent->addend == r_addend
4841             && ent->owner == abfd
4842             && ent->tls_type == tls_type)
4843           break;
4844       if (ent == NULL)
4845         {
4846           bfd_size_type amt = sizeof (*ent);
4847           ent = bfd_alloc (abfd, amt);
4848           if (ent == NULL)
4849             return FALSE;
4850           ent->next = local_got_ents[r_symndx];
4851           ent->addend = r_addend;
4852           ent->owner = abfd;
4853           ent->tls_type = tls_type;
4854           ent->is_indirect = FALSE;
4855           ent->got.refcount = 0;
4856           local_got_ents[r_symndx] = ent;
4857         }
4858       ent->got.refcount += 1;
4859     }
4860
4861   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4862   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4863   local_got_tls_masks[r_symndx] |= tls_type;
4864
4865   return local_plt + r_symndx;
4866 }
4867
4868 static bfd_boolean
4869 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4870 {
4871   struct plt_entry *ent;
4872
4873   for (ent = *plist; ent != NULL; ent = ent->next)
4874     if (ent->addend == addend)
4875       break;
4876   if (ent == NULL)
4877     {
4878       bfd_size_type amt = sizeof (*ent);
4879       ent = bfd_alloc (abfd, amt);
4880       if (ent == NULL)
4881         return FALSE;
4882       ent->next = *plist;
4883       ent->addend = addend;
4884       ent->plt.refcount = 0;
4885       *plist = ent;
4886     }
4887   ent->plt.refcount += 1;
4888   return TRUE;
4889 }
4890
4891 static bfd_boolean
4892 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4893 {
4894   return (r_type == R_PPC64_REL24
4895           || r_type == R_PPC64_REL14
4896           || r_type == R_PPC64_REL14_BRTAKEN
4897           || r_type == R_PPC64_REL14_BRNTAKEN
4898           || r_type == R_PPC64_ADDR24
4899           || r_type == R_PPC64_ADDR14
4900           || r_type == R_PPC64_ADDR14_BRTAKEN
4901           || r_type == R_PPC64_ADDR14_BRNTAKEN);
4902 }
4903
4904 /* Look through the relocs for a section during the first phase, and
4905    calculate needed space in the global offset table, procedure
4906    linkage table, and dynamic reloc sections.  */
4907
4908 static bfd_boolean
4909 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4910                         asection *sec, const Elf_Internal_Rela *relocs)
4911 {
4912   struct ppc_link_hash_table *htab;
4913   Elf_Internal_Shdr *symtab_hdr;
4914   struct elf_link_hash_entry **sym_hashes;
4915   const Elf_Internal_Rela *rel;
4916   const Elf_Internal_Rela *rel_end;
4917   asection *sreloc;
4918   asection **opd_sym_map;
4919   struct elf_link_hash_entry *tga, *dottga;
4920
4921   if (info->relocatable)
4922     return TRUE;
4923
4924   /* Don't do anything special with non-loaded, non-alloced sections.
4925      In particular, any relocs in such sections should not affect GOT
4926      and PLT reference counting (ie. we don't allow them to create GOT
4927      or PLT entries), there's no possibility or desire to optimize TLS
4928      relocs, and there's not much point in propagating relocs to shared
4929      libs that the dynamic linker won't relocate.  */
4930   if ((sec->flags & SEC_ALLOC) == 0)
4931     return TRUE;
4932
4933   BFD_ASSERT (is_ppc64_elf (abfd));
4934
4935   htab = ppc_hash_table (info);
4936   if (htab == NULL)
4937     return FALSE;
4938
4939   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4940                               FALSE, FALSE, TRUE);
4941   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4942                                  FALSE, FALSE, TRUE);
4943   symtab_hdr = &elf_symtab_hdr (abfd);
4944   sym_hashes = elf_sym_hashes (abfd);
4945   sreloc = NULL;
4946   opd_sym_map = NULL;
4947   if (strcmp (sec->name, ".opd") == 0)
4948     {
4949       /* Garbage collection needs some extra help with .opd sections.
4950          We don't want to necessarily keep everything referenced by
4951          relocs in .opd, as that would keep all functions.  Instead,
4952          if we reference an .opd symbol (a function descriptor), we
4953          want to keep the function code symbol's section.  This is
4954          easy for global symbols, but for local syms we need to keep
4955          information about the associated function section.  */
4956       bfd_size_type amt;
4957
4958       amt = sec->size * sizeof (*opd_sym_map) / 8;
4959       opd_sym_map = bfd_zalloc (abfd, amt);
4960       if (opd_sym_map == NULL)
4961         return FALSE;
4962       ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
4963       BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4964       ppc64_elf_section_data (sec)->sec_type = sec_opd;
4965     }
4966
4967   if (htab->sfpr == NULL
4968       && !create_linkage_sections (htab->elf.dynobj, info))
4969     return FALSE;
4970
4971   rel_end = relocs + sec->reloc_count;
4972   for (rel = relocs; rel < rel_end; rel++)
4973     {
4974       unsigned long r_symndx;
4975       struct elf_link_hash_entry *h;
4976       enum elf_ppc64_reloc_type r_type;
4977       int tls_type;
4978       struct _ppc64_elf_section_data *ppc64_sec;
4979       struct plt_entry **ifunc;
4980
4981       r_symndx = ELF64_R_SYM (rel->r_info);
4982       if (r_symndx < symtab_hdr->sh_info)
4983         h = NULL;
4984       else
4985         {
4986           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4987           h = elf_follow_link (h);
4988         }
4989
4990       tls_type = 0;
4991       ifunc = NULL;
4992       if (h != NULL)
4993         {
4994           if (h->type == STT_GNU_IFUNC)
4995             {
4996               h->needs_plt = 1;
4997               ifunc = &h->plt.plist;
4998             }
4999         }
5000       else
5001         {
5002           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5003                                                           abfd, r_symndx);
5004           if (isym == NULL)
5005             return FALSE;
5006
5007           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5008             {
5009               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5010                                              rel->r_addend, PLT_IFUNC);
5011               if (ifunc == NULL)
5012                 return FALSE;
5013             }
5014         }
5015       r_type = ELF64_R_TYPE (rel->r_info);
5016       if (is_branch_reloc (r_type))
5017         {
5018           if (h != NULL && (h == tga || h == dottga))
5019             {
5020               if (rel != relocs
5021                   && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5022                       || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5023                 /* We have a new-style __tls_get_addr call with a marker
5024                    reloc.  */
5025                 ;
5026               else
5027                 /* Mark this section as having an old-style call.  */
5028                 sec->has_tls_get_addr_call = 1;
5029             }
5030
5031           /* STT_GNU_IFUNC symbols must have a PLT entry.  */
5032           if (ifunc != NULL
5033               && !update_plt_info (abfd, ifunc, rel->r_addend))
5034             return FALSE;
5035         }
5036
5037       switch (r_type)
5038         {
5039         case R_PPC64_TLSGD:
5040         case R_PPC64_TLSLD:
5041           /* These special tls relocs tie a call to __tls_get_addr with
5042              its parameter symbol.  */
5043           break;
5044
5045         case R_PPC64_GOT_TLSLD16:
5046         case R_PPC64_GOT_TLSLD16_LO:
5047         case R_PPC64_GOT_TLSLD16_HI:
5048         case R_PPC64_GOT_TLSLD16_HA:
5049           tls_type = TLS_TLS | TLS_LD;
5050           goto dogottls;
5051
5052         case R_PPC64_GOT_TLSGD16:
5053         case R_PPC64_GOT_TLSGD16_LO:
5054         case R_PPC64_GOT_TLSGD16_HI:
5055         case R_PPC64_GOT_TLSGD16_HA:
5056           tls_type = TLS_TLS | TLS_GD;
5057           goto dogottls;
5058
5059         case R_PPC64_GOT_TPREL16_DS:
5060         case R_PPC64_GOT_TPREL16_LO_DS:
5061         case R_PPC64_GOT_TPREL16_HI:
5062         case R_PPC64_GOT_TPREL16_HA:
5063           if (!info->executable)
5064             info->flags |= DF_STATIC_TLS;
5065           tls_type = TLS_TLS | TLS_TPREL;
5066           goto dogottls;
5067
5068         case R_PPC64_GOT_DTPREL16_DS:
5069         case R_PPC64_GOT_DTPREL16_LO_DS:
5070         case R_PPC64_GOT_DTPREL16_HI:
5071         case R_PPC64_GOT_DTPREL16_HA:
5072           tls_type = TLS_TLS | TLS_DTPREL;
5073         dogottls:
5074           sec->has_tls_reloc = 1;
5075           /* Fall thru */
5076
5077         case R_PPC64_GOT16:
5078         case R_PPC64_GOT16_DS:
5079         case R_PPC64_GOT16_HA:
5080         case R_PPC64_GOT16_HI:
5081         case R_PPC64_GOT16_LO:
5082         case R_PPC64_GOT16_LO_DS:
5083           /* This symbol requires a global offset table entry.  */
5084           sec->has_toc_reloc = 1;
5085           if (r_type == R_PPC64_GOT_TLSLD16
5086               || r_type == R_PPC64_GOT_TLSGD16
5087               || r_type == R_PPC64_GOT_TPREL16_DS
5088               || r_type == R_PPC64_GOT_DTPREL16_DS
5089               || r_type == R_PPC64_GOT16
5090               || r_type == R_PPC64_GOT16_DS)
5091             {
5092               htab->do_multi_toc = 1;
5093               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5094             }
5095
5096           if (ppc64_elf_tdata (abfd)->got == NULL
5097               && !create_got_section (abfd, info))
5098             return FALSE;
5099
5100           if (h != NULL)
5101             {
5102               struct ppc_link_hash_entry *eh;
5103               struct got_entry *ent;
5104
5105               eh = (struct ppc_link_hash_entry *) h;
5106               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5107                 if (ent->addend == rel->r_addend
5108                     && ent->owner == abfd
5109                     && ent->tls_type == tls_type)
5110                   break;
5111               if (ent == NULL)
5112                 {
5113                   bfd_size_type amt = sizeof (*ent);
5114                   ent = bfd_alloc (abfd, amt);
5115                   if (ent == NULL)
5116                     return FALSE;
5117                   ent->next = eh->elf.got.glist;
5118                   ent->addend = rel->r_addend;
5119                   ent->owner = abfd;
5120                   ent->tls_type = tls_type;
5121                   ent->is_indirect = FALSE;
5122                   ent->got.refcount = 0;
5123                   eh->elf.got.glist = ent;
5124                 }
5125               ent->got.refcount += 1;
5126               eh->tls_mask |= tls_type;
5127             }
5128           else
5129             /* This is a global offset table entry for a local symbol.  */
5130             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5131                                         rel->r_addend, tls_type))
5132               return FALSE;
5133           break;
5134
5135         case R_PPC64_PLT16_HA:
5136         case R_PPC64_PLT16_HI:
5137         case R_PPC64_PLT16_LO:
5138         case R_PPC64_PLT32:
5139         case R_PPC64_PLT64:
5140           /* This symbol requires a procedure linkage table entry.  We
5141              actually build the entry in adjust_dynamic_symbol,
5142              because this might be a case of linking PIC code without
5143              linking in any dynamic objects, in which case we don't
5144              need to generate a procedure linkage table after all.  */
5145           if (h == NULL)
5146             {
5147               /* It does not make sense to have a procedure linkage
5148                  table entry for a local symbol.  */
5149               bfd_set_error (bfd_error_bad_value);
5150               return FALSE;
5151             }
5152           else
5153             {
5154               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5155                 return FALSE;
5156               h->needs_plt = 1;
5157               if (h->root.root.string[0] == '.'
5158                   && h->root.root.string[1] != '\0')
5159                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5160             }
5161           break;
5162
5163           /* The following relocations don't need to propagate the
5164              relocation if linking a shared object since they are
5165              section relative.  */
5166         case R_PPC64_SECTOFF:
5167         case R_PPC64_SECTOFF_LO:
5168         case R_PPC64_SECTOFF_HI:
5169         case R_PPC64_SECTOFF_HA:
5170         case R_PPC64_SECTOFF_DS:
5171         case R_PPC64_SECTOFF_LO_DS:
5172         case R_PPC64_DTPREL16:
5173         case R_PPC64_DTPREL16_LO:
5174         case R_PPC64_DTPREL16_HI:
5175         case R_PPC64_DTPREL16_HA:
5176         case R_PPC64_DTPREL16_DS:
5177         case R_PPC64_DTPREL16_LO_DS:
5178         case R_PPC64_DTPREL16_HIGHER:
5179         case R_PPC64_DTPREL16_HIGHERA:
5180         case R_PPC64_DTPREL16_HIGHEST:
5181         case R_PPC64_DTPREL16_HIGHESTA:
5182           break;
5183
5184           /* Nor do these.  */
5185         case R_PPC64_REL16:
5186         case R_PPC64_REL16_LO:
5187         case R_PPC64_REL16_HI:
5188         case R_PPC64_REL16_HA:
5189           break;
5190
5191         case R_PPC64_TOC16:
5192         case R_PPC64_TOC16_DS:
5193           htab->do_multi_toc = 1;
5194           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5195         case R_PPC64_TOC16_LO:
5196         case R_PPC64_TOC16_HI:
5197         case R_PPC64_TOC16_HA:
5198         case R_PPC64_TOC16_LO_DS:
5199           sec->has_toc_reloc = 1;
5200           break;
5201
5202           /* This relocation describes the C++ object vtable hierarchy.
5203              Reconstruct it for later use during GC.  */
5204         case R_PPC64_GNU_VTINHERIT:
5205           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5206             return FALSE;
5207           break;
5208
5209           /* This relocation describes which C++ vtable entries are actually
5210              used.  Record for later use during GC.  */
5211         case R_PPC64_GNU_VTENTRY:
5212           BFD_ASSERT (h != NULL);
5213           if (h != NULL
5214               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5215             return FALSE;
5216           break;
5217
5218         case R_PPC64_REL14:
5219         case R_PPC64_REL14_BRTAKEN:
5220         case R_PPC64_REL14_BRNTAKEN:
5221           {
5222             asection *dest = NULL;
5223
5224             /* Heuristic: If jumping outside our section, chances are
5225                we are going to need a stub.  */
5226             if (h != NULL)
5227               {
5228                 /* If the sym is weak it may be overridden later, so
5229                    don't assume we know where a weak sym lives.  */
5230                 if (h->root.type == bfd_link_hash_defined)
5231                   dest = h->root.u.def.section;
5232               }
5233             else
5234               {
5235                 Elf_Internal_Sym *isym;
5236
5237                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5238                                               abfd, r_symndx);
5239                 if (isym == NULL)
5240                   return FALSE;
5241
5242                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5243               }
5244
5245             if (dest != sec)
5246               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5247           }
5248           /* Fall through.  */
5249
5250         case R_PPC64_REL24:
5251           if (h != NULL && ifunc == NULL)
5252             {
5253               /* We may need a .plt entry if the function this reloc
5254                  refers to is in a shared lib.  */
5255               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5256                 return FALSE;
5257               h->needs_plt = 1;
5258               if (h->root.root.string[0] == '.'
5259                   && h->root.root.string[1] != '\0')
5260                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5261               if (h == tga || h == dottga)
5262                 sec->has_tls_reloc = 1;
5263             }
5264           break;
5265
5266         case R_PPC64_TPREL64:
5267           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5268           if (!info->executable)
5269             info->flags |= DF_STATIC_TLS;
5270           goto dotlstoc;
5271
5272         case R_PPC64_DTPMOD64:
5273           if (rel + 1 < rel_end
5274               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5275               && rel[1].r_offset == rel->r_offset + 8)
5276             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5277           else
5278             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5279           goto dotlstoc;
5280
5281         case R_PPC64_DTPREL64:
5282           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5283           if (rel != relocs
5284               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5285               && rel[-1].r_offset == rel->r_offset - 8)
5286             /* This is the second reloc of a dtpmod, dtprel pair.
5287                Don't mark with TLS_DTPREL.  */
5288             goto dodyn;
5289
5290         dotlstoc:
5291           sec->has_tls_reloc = 1;
5292           if (h != NULL)
5293             {
5294               struct ppc_link_hash_entry *eh;
5295               eh = (struct ppc_link_hash_entry *) h;
5296               eh->tls_mask |= tls_type;
5297             }
5298           else
5299             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5300                                         rel->r_addend, tls_type))
5301               return FALSE;
5302
5303           ppc64_sec = ppc64_elf_section_data (sec);
5304           if (ppc64_sec->sec_type != sec_toc)
5305             {
5306               bfd_size_type amt;
5307
5308               /* One extra to simplify get_tls_mask.  */
5309               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5310               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5311               if (ppc64_sec->u.toc.symndx == NULL)
5312                 return FALSE;
5313               amt = sec->size * sizeof (bfd_vma) / 8;
5314               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5315               if (ppc64_sec->u.toc.add == NULL)
5316                 return FALSE;
5317               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5318               ppc64_sec->sec_type = sec_toc;
5319             }
5320           BFD_ASSERT (rel->r_offset % 8 == 0);
5321           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5322           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5323
5324           /* Mark the second slot of a GD or LD entry.
5325              -1 to indicate GD and -2 to indicate LD.  */
5326           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5327             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5328           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5329             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5330           goto dodyn;
5331
5332         case R_PPC64_TPREL16:
5333         case R_PPC64_TPREL16_LO:
5334         case R_PPC64_TPREL16_HI:
5335         case R_PPC64_TPREL16_HA:
5336         case R_PPC64_TPREL16_DS:
5337         case R_PPC64_TPREL16_LO_DS:
5338         case R_PPC64_TPREL16_HIGHER:
5339         case R_PPC64_TPREL16_HIGHERA:
5340         case R_PPC64_TPREL16_HIGHEST:
5341         case R_PPC64_TPREL16_HIGHESTA:
5342           if (info->shared)
5343             {
5344               if (!info->executable)
5345                 info->flags |= DF_STATIC_TLS;
5346               goto dodyn;
5347             }
5348           break;
5349
5350         case R_PPC64_ADDR64:
5351           if (opd_sym_map != NULL
5352               && rel + 1 < rel_end
5353               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5354             {
5355               if (h != NULL)
5356                 {
5357                   if (h->root.root.string[0] == '.'
5358                       && h->root.root.string[1] != 0
5359                       && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5360                     ;
5361                   else
5362                     ((struct ppc_link_hash_entry *) h)->is_func = 1;
5363                 }
5364               else
5365                 {
5366                   asection *s;
5367                   Elf_Internal_Sym *isym;
5368
5369                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5370                                                 abfd, r_symndx);
5371                   if (isym == NULL)
5372                     return FALSE;
5373
5374                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5375                   if (s != NULL && s != sec)
5376                     opd_sym_map[rel->r_offset / 8] = s;
5377                 }
5378             }
5379           /* Fall through.  */
5380
5381         case R_PPC64_REL30:
5382         case R_PPC64_REL32:
5383         case R_PPC64_REL64:
5384         case R_PPC64_ADDR14:
5385         case R_PPC64_ADDR14_BRNTAKEN:
5386         case R_PPC64_ADDR14_BRTAKEN:
5387         case R_PPC64_ADDR16:
5388         case R_PPC64_ADDR16_DS:
5389         case R_PPC64_ADDR16_HA:
5390         case R_PPC64_ADDR16_HI:
5391         case R_PPC64_ADDR16_HIGHER:
5392         case R_PPC64_ADDR16_HIGHERA:
5393         case R_PPC64_ADDR16_HIGHEST:
5394         case R_PPC64_ADDR16_HIGHESTA:
5395         case R_PPC64_ADDR16_LO:
5396         case R_PPC64_ADDR16_LO_DS:
5397         case R_PPC64_ADDR24:
5398         case R_PPC64_ADDR32:
5399         case R_PPC64_UADDR16:
5400         case R_PPC64_UADDR32:
5401         case R_PPC64_UADDR64:
5402         case R_PPC64_TOC:
5403           if (h != NULL && !info->shared)
5404             /* We may need a copy reloc.  */
5405             h->non_got_ref = 1;
5406
5407           /* Don't propagate .opd relocs.  */
5408           if (NO_OPD_RELOCS && opd_sym_map != NULL)
5409             break;
5410
5411           /* If we are creating a shared library, and this is a reloc
5412              against a global symbol, or a non PC relative reloc
5413              against a local symbol, then we need to copy the reloc
5414              into the shared library.  However, if we are linking with
5415              -Bsymbolic, we do not need to copy a reloc against a
5416              global symbol which is defined in an object we are
5417              including in the link (i.e., DEF_REGULAR is set).  At
5418              this point we have not seen all the input files, so it is
5419              possible that DEF_REGULAR is not set now but will be set
5420              later (it is never cleared).  In case of a weak definition,
5421              DEF_REGULAR may be cleared later by a strong definition in
5422              a shared library.  We account for that possibility below by
5423              storing information in the dyn_relocs field of the hash
5424              table entry.  A similar situation occurs when creating
5425              shared libraries and symbol visibility changes render the
5426              symbol local.
5427
5428              If on the other hand, we are creating an executable, we
5429              may need to keep relocations for symbols satisfied by a
5430              dynamic library if we manage to avoid copy relocs for the
5431              symbol.  */
5432         dodyn:
5433           if ((info->shared
5434                && (must_be_dyn_reloc (info, r_type)
5435                    || (h != NULL
5436                        && (! info->symbolic
5437                            || h->root.type == bfd_link_hash_defweak
5438                            || !h->def_regular))))
5439               || (ELIMINATE_COPY_RELOCS
5440                   && !info->shared
5441                   && h != NULL
5442                   && (h->root.type == bfd_link_hash_defweak
5443                       || !h->def_regular))
5444               || (!info->shared
5445                   && ifunc != NULL))
5446             {
5447               struct elf_dyn_relocs *p;
5448               struct elf_dyn_relocs **head;
5449
5450               /* We must copy these reloc types into the output file.
5451                  Create a reloc section in dynobj and make room for
5452                  this reloc.  */
5453               if (sreloc == NULL)
5454                 {
5455                   sreloc = _bfd_elf_make_dynamic_reloc_section
5456                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5457
5458                   if (sreloc == NULL)
5459                     return FALSE;
5460                 }
5461
5462               /* If this is a global symbol, we count the number of
5463                  relocations we need for this symbol.  */
5464               if (h != NULL)
5465                 {
5466                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5467                 }
5468               else
5469                 {
5470                   /* Track dynamic relocs needed for local syms too.
5471                      We really need local syms available to do this
5472                      easily.  Oh well.  */
5473                   asection *s;
5474                   void *vpp;
5475                   Elf_Internal_Sym *isym;
5476
5477                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5478                                                 abfd, r_symndx);
5479                   if (isym == NULL)
5480                     return FALSE;
5481
5482                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5483                   if (s == NULL)
5484                     s = sec;
5485
5486                   vpp = &elf_section_data (s)->local_dynrel;
5487                   head = (struct elf_dyn_relocs **) vpp;
5488                 }
5489
5490               p = *head;
5491               if (p == NULL || p->sec != sec)
5492                 {
5493                   p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5494                   if (p == NULL)
5495                     return FALSE;
5496                   p->next = *head;
5497                   *head = p;
5498                   p->sec = sec;
5499                   p->count = 0;
5500                   p->pc_count = 0;
5501                 }
5502
5503               p->count += 1;
5504               if (!must_be_dyn_reloc (info, r_type))
5505                 p->pc_count += 1;
5506             }
5507           break;
5508
5509         default:
5510           break;
5511         }
5512     }
5513
5514   return TRUE;
5515 }
5516
5517 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
5518    of the code entry point, and its section.  */
5519
5520 static bfd_vma
5521 opd_entry_value (asection *opd_sec,
5522                  bfd_vma offset,
5523                  asection **code_sec,
5524                  bfd_vma *code_off)
5525 {
5526   bfd *opd_bfd = opd_sec->owner;
5527   Elf_Internal_Rela *relocs;
5528   Elf_Internal_Rela *lo, *hi, *look;
5529   bfd_vma val;
5530
5531   /* No relocs implies we are linking a --just-symbols object.  */
5532   if (opd_sec->reloc_count == 0)
5533     {
5534       char buf[8];
5535
5536       if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
5537         return (bfd_vma) -1;
5538
5539       val = bfd_get_64 (opd_bfd, buf);
5540       if (code_sec != NULL)
5541         {
5542           asection *sec, *likely = NULL;
5543           for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5544             if (sec->vma <= val
5545                 && (sec->flags & SEC_LOAD) != 0
5546                 && (sec->flags & SEC_ALLOC) != 0)
5547               likely = sec;
5548           if (likely != NULL)
5549             {
5550               *code_sec = likely;
5551               if (code_off != NULL)
5552                 *code_off = val - likely->vma;
5553             }
5554         }
5555       return val;
5556     }
5557
5558   BFD_ASSERT (is_ppc64_elf (opd_bfd));
5559
5560   relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5561   if (relocs == NULL)
5562     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5563
5564   /* Go find the opd reloc at the sym address.  */
5565   lo = relocs;
5566   BFD_ASSERT (lo != NULL);
5567   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5568   val = (bfd_vma) -1;
5569   while (lo < hi)
5570     {
5571       look = lo + (hi - lo) / 2;
5572       if (look->r_offset < offset)
5573         lo = look + 1;
5574       else if (look->r_offset > offset)
5575         hi = look;
5576       else
5577         {
5578           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5579
5580           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5581               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5582             {
5583               unsigned long symndx = ELF64_R_SYM (look->r_info);
5584               asection *sec;
5585
5586               if (symndx < symtab_hdr->sh_info)
5587                 {
5588                   Elf_Internal_Sym *sym;
5589
5590                   sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5591                   if (sym == NULL)
5592                     {
5593                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5594                                                   symtab_hdr->sh_info,
5595                                                   0, NULL, NULL, NULL);
5596                       if (sym == NULL)
5597                         break;
5598                       symtab_hdr->contents = (bfd_byte *) sym;
5599                     }
5600
5601                   sym += symndx;
5602                   val = sym->st_value;
5603                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5604                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5605                 }
5606               else
5607                 {
5608                   struct elf_link_hash_entry **sym_hashes;
5609                   struct elf_link_hash_entry *rh;
5610
5611                   sym_hashes = elf_sym_hashes (opd_bfd);
5612                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
5613                   rh = elf_follow_link (rh);
5614                   BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5615                               || rh->root.type == bfd_link_hash_defweak);
5616                   val = rh->root.u.def.value;
5617                   sec = rh->root.u.def.section;
5618                 }
5619               val += look->r_addend;
5620               if (code_off != NULL)
5621                 *code_off = val;
5622               if (code_sec != NULL)
5623                 *code_sec = sec;
5624               if (sec != NULL && sec->output_section != NULL)
5625                 val += sec->output_section->vma + sec->output_offset;
5626             }
5627           break;
5628         }
5629     }
5630
5631   return val;
5632 }
5633
5634 /* Return true if symbol is defined in a regular object file.  */
5635
5636 static bfd_boolean
5637 is_static_defined (struct elf_link_hash_entry *h)
5638 {
5639   return ((h->root.type == bfd_link_hash_defined
5640            || h->root.type == bfd_link_hash_defweak)
5641           && h->root.u.def.section != NULL
5642           && h->root.u.def.section->output_section != NULL);
5643 }
5644
5645 /* If FDH is a function descriptor symbol, return the associated code
5646    entry symbol if it is defined.  Return NULL otherwise.  */
5647
5648 static struct ppc_link_hash_entry *
5649 defined_code_entry (struct ppc_link_hash_entry *fdh)
5650 {
5651   if (fdh->is_func_descriptor)
5652     {
5653       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5654       if (fh->elf.root.type == bfd_link_hash_defined
5655           || fh->elf.root.type == bfd_link_hash_defweak)
5656         return fh;
5657     }
5658   return NULL;
5659 }
5660
5661 /* If FH is a function code entry symbol, return the associated
5662    function descriptor symbol if it is defined.  Return NULL otherwise.  */
5663
5664 static struct ppc_link_hash_entry *
5665 defined_func_desc (struct ppc_link_hash_entry *fh)
5666 {
5667   if (fh->oh != NULL
5668       && fh->oh->is_func_descriptor)
5669     {
5670       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5671       if (fdh->elf.root.type == bfd_link_hash_defined
5672           || fdh->elf.root.type == bfd_link_hash_defweak)
5673         return fdh;
5674     }
5675   return NULL;
5676 }
5677
5678 /* Mark all our entry sym sections, both opd and code section.  */
5679
5680 static void
5681 ppc64_elf_gc_keep (struct bfd_link_info *info)
5682 {
5683   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5684   struct bfd_sym_chain *sym;
5685
5686   if (htab == NULL)
5687     return;
5688
5689   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5690     {
5691       struct ppc_link_hash_entry *eh, *fh;
5692       asection *sec;
5693
5694       eh = (struct ppc_link_hash_entry *)
5695         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5696       if (eh == NULL)
5697         continue;
5698       if (eh->elf.root.type != bfd_link_hash_defined
5699           && eh->elf.root.type != bfd_link_hash_defweak)
5700         continue;
5701
5702       fh = defined_code_entry (eh);
5703       if (fh != NULL)
5704         {
5705           sec = fh->elf.root.u.def.section;
5706           sec->flags |= SEC_KEEP;
5707         }
5708       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5709                && opd_entry_value (eh->elf.root.u.def.section,
5710                                    eh->elf.root.u.def.value,
5711                                    &sec, NULL) != (bfd_vma) -1)
5712         sec->flags |= SEC_KEEP;
5713
5714       sec = eh->elf.root.u.def.section;
5715       sec->flags |= SEC_KEEP;
5716     }
5717 }
5718
5719 /* Mark sections containing dynamically referenced symbols.  When
5720    building shared libraries, we must assume that any visible symbol is
5721    referenced.  */
5722
5723 static bfd_boolean
5724 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5725 {
5726   struct bfd_link_info *info = (struct bfd_link_info *) inf;
5727   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5728   struct ppc_link_hash_entry *fdh;
5729
5730   /* Dynamic linking info is on the func descriptor sym.  */
5731   fdh = defined_func_desc (eh);
5732   if (fdh != NULL)
5733     eh = fdh;
5734
5735   if ((eh->elf.root.type == bfd_link_hash_defined
5736        || eh->elf.root.type == bfd_link_hash_defweak)
5737       && (eh->elf.ref_dynamic
5738           || (!info->executable
5739               && eh->elf.def_regular
5740               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5741               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5742               && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
5743                   || !bfd_hide_sym_by_version (info->version_info,
5744                                                eh->elf.root.root.string)))))
5745     {
5746       asection *code_sec;
5747       struct ppc_link_hash_entry *fh;
5748
5749       eh->elf.root.u.def.section->flags |= SEC_KEEP;
5750
5751       /* Function descriptor syms cause the associated
5752          function code sym section to be marked.  */
5753       fh = defined_code_entry (eh);
5754       if (fh != NULL)
5755         {
5756           code_sec = fh->elf.root.u.def.section;
5757           code_sec->flags |= SEC_KEEP;
5758         }
5759       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5760                && opd_entry_value (eh->elf.root.u.def.section,
5761                                    eh->elf.root.u.def.value,
5762                                    &code_sec, NULL) != (bfd_vma) -1)
5763         code_sec->flags |= SEC_KEEP;
5764     }
5765
5766   return TRUE;
5767 }
5768
5769 /* Return the section that should be marked against GC for a given
5770    relocation.  */
5771
5772 static asection *
5773 ppc64_elf_gc_mark_hook (asection *sec,
5774                         struct bfd_link_info *info,
5775                         Elf_Internal_Rela *rel,
5776                         struct elf_link_hash_entry *h,
5777                         Elf_Internal_Sym *sym)
5778 {
5779   asection *rsec;
5780
5781   /* Syms return NULL if we're marking .opd, so we avoid marking all
5782      function sections, as all functions are referenced in .opd.  */
5783   rsec = NULL;
5784   if (get_opd_info (sec) != NULL)
5785     return rsec;
5786
5787   if (h != NULL)
5788     {
5789       enum elf_ppc64_reloc_type r_type;
5790       struct ppc_link_hash_entry *eh, *fh, *fdh;
5791
5792       r_type = ELF64_R_TYPE (rel->r_info);
5793       switch (r_type)
5794         {
5795         case R_PPC64_GNU_VTINHERIT:
5796         case R_PPC64_GNU_VTENTRY:
5797           break;
5798
5799         default:
5800           switch (h->root.type)
5801             {
5802             case bfd_link_hash_defined:
5803             case bfd_link_hash_defweak:
5804               eh = (struct ppc_link_hash_entry *) h;
5805               fdh = defined_func_desc (eh);
5806               if (fdh != NULL)
5807                 eh = fdh;
5808
5809               /* Function descriptor syms cause the associated
5810                  function code sym section to be marked.  */
5811               fh = defined_code_entry (eh);
5812               if (fh != NULL)
5813                 {
5814                   /* They also mark their opd section.  */
5815                   eh->elf.root.u.def.section->gc_mark = 1;
5816
5817                   rsec = fh->elf.root.u.def.section;
5818                 }
5819               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5820                        && opd_entry_value (eh->elf.root.u.def.section,
5821                                            eh->elf.root.u.def.value,
5822                                            &rsec, NULL) != (bfd_vma) -1)
5823                 eh->elf.root.u.def.section->gc_mark = 1;
5824               else
5825                 rsec = h->root.u.def.section;
5826               break;
5827
5828             case bfd_link_hash_common:
5829               rsec = h->root.u.c.p->section;
5830               break;
5831
5832             default:
5833               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5834             }
5835         }
5836     }
5837   else
5838     {
5839       struct _opd_sec_data *opd;
5840
5841       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5842       opd = get_opd_info (rsec);
5843       if (opd != NULL && opd->func_sec != NULL)
5844         {
5845           rsec->gc_mark = 1;
5846
5847           rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
5848         }
5849     }
5850
5851   return rsec;
5852 }
5853
5854 /* Update the .got, .plt. and dynamic reloc reference counts for the
5855    section being removed.  */
5856
5857 static bfd_boolean
5858 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5859                          asection *sec, const Elf_Internal_Rela *relocs)
5860 {
5861   struct ppc_link_hash_table *htab;
5862   Elf_Internal_Shdr *symtab_hdr;
5863   struct elf_link_hash_entry **sym_hashes;
5864   struct got_entry **local_got_ents;
5865   const Elf_Internal_Rela *rel, *relend;
5866
5867   if (info->relocatable)
5868     return TRUE;
5869
5870   if ((sec->flags & SEC_ALLOC) == 0)
5871     return TRUE;
5872
5873   elf_section_data (sec)->local_dynrel = NULL;
5874
5875   htab = ppc_hash_table (info);
5876   if (htab == NULL)
5877     return FALSE;
5878
5879   symtab_hdr = &elf_symtab_hdr (abfd);
5880   sym_hashes = elf_sym_hashes (abfd);
5881   local_got_ents = elf_local_got_ents (abfd);
5882
5883   relend = relocs + sec->reloc_count;
5884   for (rel = relocs; rel < relend; rel++)
5885     {
5886       unsigned long r_symndx;
5887       enum elf_ppc64_reloc_type r_type;
5888       struct elf_link_hash_entry *h = NULL;
5889       unsigned char tls_type = 0;
5890
5891       r_symndx = ELF64_R_SYM (rel->r_info);
5892       r_type = ELF64_R_TYPE (rel->r_info);
5893       if (r_symndx >= symtab_hdr->sh_info)
5894         {
5895           struct ppc_link_hash_entry *eh;
5896           struct elf_dyn_relocs **pp;
5897           struct elf_dyn_relocs *p;
5898
5899           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5900           h = elf_follow_link (h);
5901           eh = (struct ppc_link_hash_entry *) h;
5902
5903           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5904             if (p->sec == sec)
5905               {
5906                 /* Everything must go for SEC.  */
5907                 *pp = p->next;
5908                 break;
5909               }
5910         }
5911
5912       if (is_branch_reloc (r_type))
5913         {
5914           struct plt_entry **ifunc = NULL;
5915           if (h != NULL)
5916             {
5917               if (h->type == STT_GNU_IFUNC)
5918                 ifunc = &h->plt.plist;
5919             }
5920           else if (local_got_ents != NULL)
5921             {
5922               struct plt_entry **local_plt = (struct plt_entry **)
5923                 (local_got_ents + symtab_hdr->sh_info);
5924               unsigned char *local_got_tls_masks = (unsigned char *)
5925                 (local_plt + symtab_hdr->sh_info);
5926               if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5927                 ifunc = local_plt + r_symndx;
5928             }
5929           if (ifunc != NULL)
5930             {
5931               struct plt_entry *ent;
5932
5933               for (ent = *ifunc; ent != NULL; ent = ent->next)
5934                 if (ent->addend == rel->r_addend)
5935                   break;
5936               if (ent == NULL)
5937                 abort ();
5938               if (ent->plt.refcount > 0)
5939                 ent->plt.refcount -= 1;
5940               continue;
5941             }
5942         }
5943
5944       switch (r_type)
5945         {
5946         case R_PPC64_GOT_TLSLD16:
5947         case R_PPC64_GOT_TLSLD16_LO:
5948         case R_PPC64_GOT_TLSLD16_HI:
5949         case R_PPC64_GOT_TLSLD16_HA:
5950           tls_type = TLS_TLS | TLS_LD;
5951           goto dogot;
5952
5953         case R_PPC64_GOT_TLSGD16:
5954         case R_PPC64_GOT_TLSGD16_LO:
5955         case R_PPC64_GOT_TLSGD16_HI:
5956         case R_PPC64_GOT_TLSGD16_HA:
5957           tls_type = TLS_TLS | TLS_GD;
5958           goto dogot;
5959
5960         case R_PPC64_GOT_TPREL16_DS:
5961         case R_PPC64_GOT_TPREL16_LO_DS:
5962         case R_PPC64_GOT_TPREL16_HI:
5963         case R_PPC64_GOT_TPREL16_HA:
5964           tls_type = TLS_TLS | TLS_TPREL;
5965           goto dogot;
5966
5967         case R_PPC64_GOT_DTPREL16_DS:
5968         case R_PPC64_GOT_DTPREL16_LO_DS:
5969         case R_PPC64_GOT_DTPREL16_HI:
5970         case R_PPC64_GOT_DTPREL16_HA:
5971           tls_type = TLS_TLS | TLS_DTPREL;
5972           goto dogot;
5973
5974         case R_PPC64_GOT16:
5975         case R_PPC64_GOT16_DS:
5976         case R_PPC64_GOT16_HA:
5977         case R_PPC64_GOT16_HI:
5978         case R_PPC64_GOT16_LO:
5979         case R_PPC64_GOT16_LO_DS:
5980         dogot:
5981           {
5982             struct got_entry *ent;
5983
5984             if (h != NULL)
5985               ent = h->got.glist;
5986             else
5987               ent = local_got_ents[r_symndx];
5988
5989             for (; ent != NULL; ent = ent->next)
5990               if (ent->addend == rel->r_addend
5991                   && ent->owner == abfd
5992                   && ent->tls_type == tls_type)
5993                 break;
5994             if (ent == NULL)
5995               abort ();
5996             if (ent->got.refcount > 0)
5997               ent->got.refcount -= 1;
5998           }
5999           break;
6000
6001         case R_PPC64_PLT16_HA:
6002         case R_PPC64_PLT16_HI:
6003         case R_PPC64_PLT16_LO:
6004         case R_PPC64_PLT32:
6005         case R_PPC64_PLT64:
6006         case R_PPC64_REL14:
6007         case R_PPC64_REL14_BRNTAKEN:
6008         case R_PPC64_REL14_BRTAKEN:
6009         case R_PPC64_REL24:
6010           if (h != NULL)
6011             {
6012               struct plt_entry *ent;
6013
6014               for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6015                 if (ent->addend == rel->r_addend)
6016                   break;
6017               if (ent != NULL && ent->plt.refcount > 0)
6018                 ent->plt.refcount -= 1;
6019             }
6020           break;
6021
6022         default:
6023           break;
6024         }
6025     }
6026   return TRUE;
6027 }
6028
6029 /* The maximum size of .sfpr.  */
6030 #define SFPR_MAX (218*4)
6031
6032 struct sfpr_def_parms
6033 {
6034   const char name[12];
6035   unsigned char lo, hi;
6036   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6037   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6038 };
6039
6040 /* Auto-generate _save*, _rest* functions in .sfpr.  */
6041
6042 static bfd_boolean
6043 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6044 {
6045   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6046   unsigned int i;
6047   size_t len = strlen (parm->name);
6048   bfd_boolean writing = FALSE;
6049   char sym[16];
6050
6051   if (htab == NULL)
6052     return FALSE;
6053
6054   memcpy (sym, parm->name, len);
6055   sym[len + 2] = 0;
6056
6057   for (i = parm->lo; i <= parm->hi; i++)
6058     {
6059       struct elf_link_hash_entry *h;
6060
6061       sym[len + 0] = i / 10 + '0';
6062       sym[len + 1] = i % 10 + '0';
6063       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6064       if (h != NULL
6065           && !h->def_regular)
6066         {
6067           h->root.type = bfd_link_hash_defined;
6068           h->root.u.def.section = htab->sfpr;
6069           h->root.u.def.value = htab->sfpr->size;
6070           h->type = STT_FUNC;
6071           h->def_regular = 1;
6072           _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6073           writing = TRUE;
6074           if (htab->sfpr->contents == NULL)
6075             {
6076               htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6077               if (htab->sfpr->contents == NULL)
6078                 return FALSE;
6079             }
6080         }
6081       if (writing)
6082         {
6083           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6084           if (i != parm->hi)
6085             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6086           else
6087             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6088           htab->sfpr->size = p - htab->sfpr->contents;
6089         }
6090     }
6091
6092   return TRUE;
6093 }
6094
6095 static bfd_byte *
6096 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6097 {
6098   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6099   return p + 4;
6100 }
6101
6102 static bfd_byte *
6103 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6104 {
6105   p = savegpr0 (abfd, p, r);
6106   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6107   p = p + 4;
6108   bfd_put_32 (abfd, BLR, p);
6109   return p + 4;
6110 }
6111
6112 static bfd_byte *
6113 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6114 {
6115   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6116   return p + 4;
6117 }
6118
6119 static bfd_byte *
6120 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6121 {
6122   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6123   p = p + 4;
6124   p = restgpr0 (abfd, p, r);
6125   bfd_put_32 (abfd, MTLR_R0, p);
6126   p = p + 4;
6127   if (r == 29)
6128     {
6129       p = restgpr0 (abfd, p, 30);
6130       p = restgpr0 (abfd, p, 31);
6131     }
6132   bfd_put_32 (abfd, BLR, p);
6133   return p + 4;
6134 }
6135
6136 static bfd_byte *
6137 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6138 {
6139   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6140   return p + 4;
6141 }
6142
6143 static bfd_byte *
6144 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6145 {
6146   p = savegpr1 (abfd, p, r);
6147   bfd_put_32 (abfd, BLR, p);
6148   return p + 4;
6149 }
6150
6151 static bfd_byte *
6152 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6153 {
6154   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6155   return p + 4;
6156 }
6157
6158 static bfd_byte *
6159 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6160 {
6161   p = restgpr1 (abfd, p, r);
6162   bfd_put_32 (abfd, BLR, p);
6163   return p + 4;
6164 }
6165
6166 static bfd_byte *
6167 savefpr (bfd *abfd, bfd_byte *p, int r)
6168 {
6169   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6170   return p + 4;
6171 }
6172
6173 static bfd_byte *
6174 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6175 {
6176   p = savefpr (abfd, p, r);
6177   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6178   p = p + 4;
6179   bfd_put_32 (abfd, BLR, p);
6180   return p + 4;
6181 }
6182
6183 static bfd_byte *
6184 restfpr (bfd *abfd, bfd_byte *p, int r)
6185 {
6186   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6187   return p + 4;
6188 }
6189
6190 static bfd_byte *
6191 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6192 {
6193   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6194   p = p + 4;
6195   p = restfpr (abfd, p, r);
6196   bfd_put_32 (abfd, MTLR_R0, p);
6197   p = p + 4;
6198   if (r == 29)
6199     {
6200       p = restfpr (abfd, p, 30);
6201       p = restfpr (abfd, p, 31);
6202     }
6203   bfd_put_32 (abfd, BLR, p);
6204   return p + 4;
6205 }
6206
6207 static bfd_byte *
6208 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6209 {
6210   p = savefpr (abfd, p, r);
6211   bfd_put_32 (abfd, BLR, p);
6212   return p + 4;
6213 }
6214
6215 static bfd_byte *
6216 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6217 {
6218   p = restfpr (abfd, p, r);
6219   bfd_put_32 (abfd, BLR, p);
6220   return p + 4;
6221 }
6222
6223 static bfd_byte *
6224 savevr (bfd *abfd, bfd_byte *p, int r)
6225 {
6226   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6227   p = p + 4;
6228   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6229   return p + 4;
6230 }
6231
6232 static bfd_byte *
6233 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6234 {
6235   p = savevr (abfd, p, r);
6236   bfd_put_32 (abfd, BLR, p);
6237   return p + 4;
6238 }
6239
6240 static bfd_byte *
6241 restvr (bfd *abfd, bfd_byte *p, int r)
6242 {
6243   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6244   p = p + 4;
6245   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6246   return p + 4;
6247 }
6248
6249 static bfd_byte *
6250 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6251 {
6252   p = restvr (abfd, p, r);
6253   bfd_put_32 (abfd, BLR, p);
6254   return p + 4;
6255 }
6256
6257 /* Called via elf_link_hash_traverse to transfer dynamic linking
6258    information on function code symbol entries to their corresponding
6259    function descriptor symbol entries.  */
6260
6261 static bfd_boolean
6262 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6263 {
6264   struct bfd_link_info *info;
6265   struct ppc_link_hash_table *htab;
6266   struct plt_entry *ent;
6267   struct ppc_link_hash_entry *fh;
6268   struct ppc_link_hash_entry *fdh;
6269   bfd_boolean force_local;
6270
6271   fh = (struct ppc_link_hash_entry *) h;
6272   if (fh->elf.root.type == bfd_link_hash_indirect)
6273     return TRUE;
6274
6275   info = inf;
6276   htab = ppc_hash_table (info);
6277   if (htab == NULL)
6278     return FALSE;
6279
6280   /* Resolve undefined references to dot-symbols as the value
6281      in the function descriptor, if we have one in a regular object.
6282      This is to satisfy cases like ".quad .foo".  Calls to functions
6283      in dynamic objects are handled elsewhere.  */
6284   if (fh->elf.root.type == bfd_link_hash_undefweak
6285       && fh->was_undefined
6286       && (fdh = defined_func_desc (fh)) != NULL
6287       && get_opd_info (fdh->elf.root.u.def.section) != NULL
6288       && opd_entry_value (fdh->elf.root.u.def.section,
6289                           fdh->elf.root.u.def.value,
6290                           &fh->elf.root.u.def.section,
6291                           &fh->elf.root.u.def.value) != (bfd_vma) -1)
6292     {
6293       fh->elf.root.type = fdh->elf.root.type;
6294       fh->elf.forced_local = 1;
6295       fh->elf.def_regular = fdh->elf.def_regular;
6296       fh->elf.def_dynamic = fdh->elf.def_dynamic;
6297     }
6298
6299   /* If this is a function code symbol, transfer dynamic linking
6300      information to the function descriptor symbol.  */
6301   if (!fh->is_func)
6302     return TRUE;
6303
6304   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6305     if (ent->plt.refcount > 0)
6306       break;
6307   if (ent == NULL
6308       || fh->elf.root.root.string[0] != '.'
6309       || fh->elf.root.root.string[1] == '\0')
6310     return TRUE;
6311
6312   /* Find the corresponding function descriptor symbol.  Create it
6313      as undefined if necessary.  */
6314
6315   fdh = lookup_fdh (fh, htab);
6316   if (fdh == NULL
6317       && !info->executable
6318       && (fh->elf.root.type == bfd_link_hash_undefined
6319           || fh->elf.root.type == bfd_link_hash_undefweak))
6320     {
6321       fdh = make_fdh (info, fh);
6322       if (fdh == NULL)
6323         return FALSE;
6324     }
6325
6326   /* Fake function descriptors are made undefweak.  If the function
6327      code symbol is strong undefined, make the fake sym the same.
6328      If the function code symbol is defined, then force the fake
6329      descriptor local;  We can't support overriding of symbols in a
6330      shared library on a fake descriptor.  */
6331
6332   if (fdh != NULL
6333       && fdh->fake
6334       && fdh->elf.root.type == bfd_link_hash_undefweak)
6335     {
6336       if (fh->elf.root.type == bfd_link_hash_undefined)
6337         {
6338           fdh->elf.root.type = bfd_link_hash_undefined;
6339           bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6340         }
6341       else if (fh->elf.root.type == bfd_link_hash_defined
6342                || fh->elf.root.type == bfd_link_hash_defweak)
6343         {
6344           _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6345         }
6346     }
6347
6348   if (fdh != NULL
6349       && !fdh->elf.forced_local
6350       && (!info->executable
6351           || fdh->elf.def_dynamic
6352           || fdh->elf.ref_dynamic
6353           || (fdh->elf.root.type == bfd_link_hash_undefweak
6354               && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6355     {
6356       if (fdh->elf.dynindx == -1)
6357         if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6358           return FALSE;
6359       fdh->elf.ref_regular |= fh->elf.ref_regular;
6360       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6361       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6362       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6363       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6364         {
6365           move_plt_plist (fh, fdh);
6366           fdh->elf.needs_plt = 1;
6367         }
6368       fdh->is_func_descriptor = 1;
6369       fdh->oh = fh;
6370       fh->oh = fdh;
6371     }
6372
6373   /* Now that the info is on the function descriptor, clear the
6374      function code sym info.  Any function code syms for which we
6375      don't have a definition in a regular file, we force local.
6376      This prevents a shared library from exporting syms that have
6377      been imported from another library.  Function code syms that
6378      are really in the library we must leave global to prevent the
6379      linker dragging in a definition from a static library.  */
6380   force_local = (!fh->elf.def_regular
6381                  || fdh == NULL
6382                  || !fdh->elf.def_regular
6383                  || fdh->elf.forced_local);
6384   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6385
6386   return TRUE;
6387 }
6388
6389 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
6390    this hook to a) provide some gcc support functions, and b) transfer
6391    dynamic linking information gathered so far on function code symbol
6392    entries, to their corresponding function descriptor symbol entries.  */
6393
6394 static bfd_boolean
6395 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6396                             struct bfd_link_info *info)
6397 {
6398   struct ppc_link_hash_table *htab;
6399   unsigned int i;
6400   const struct sfpr_def_parms funcs[] =
6401     {
6402       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6403       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6404       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6405       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6406       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6407       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6408       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6409       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6410       { "._savef", 14, 31, savefpr, savefpr1_tail },
6411       { "._restf", 14, 31, restfpr, restfpr1_tail },
6412       { "_savevr_", 20, 31, savevr, savevr_tail },
6413       { "_restvr_", 20, 31, restvr, restvr_tail }
6414     };
6415
6416   htab = ppc_hash_table (info);
6417   if (htab == NULL)
6418     return FALSE;
6419
6420   if (htab->sfpr == NULL)
6421     /* We don't have any relocs.  */
6422     return TRUE;
6423
6424   /* Provide any missing _save* and _rest* functions.  */
6425   htab->sfpr->size = 0;
6426   for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6427     if (!sfpr_define (info, &funcs[i]))
6428       return FALSE;
6429
6430   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6431
6432   if (htab->sfpr->size == 0)
6433     htab->sfpr->flags |= SEC_EXCLUDE;
6434
6435   return TRUE;
6436 }
6437
6438 /* Adjust a symbol defined by a dynamic object and referenced by a
6439    regular object.  The current definition is in some section of the
6440    dynamic object, but we're not including those sections.  We have to
6441    change the definition to something the rest of the link can
6442    understand.  */
6443
6444 static bfd_boolean
6445 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6446                                  struct elf_link_hash_entry *h)
6447 {
6448   struct ppc_link_hash_table *htab;
6449   asection *s;
6450
6451   htab = ppc_hash_table (info);
6452   if (htab == NULL)
6453     return FALSE;
6454
6455   /* Deal with function syms.  */
6456   if (h->type == STT_FUNC
6457       || h->type == STT_GNU_IFUNC
6458       || h->needs_plt)
6459     {
6460       /* Clear procedure linkage table information for any symbol that
6461          won't need a .plt entry.  */
6462       struct plt_entry *ent;
6463       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6464         if (ent->plt.refcount > 0)
6465           break;
6466       if (ent == NULL
6467           || (h->type != STT_GNU_IFUNC
6468               && (SYMBOL_CALLS_LOCAL (info, h)
6469                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6470                       && h->root.type == bfd_link_hash_undefweak))))
6471         {
6472           h->plt.plist = NULL;
6473           h->needs_plt = 0;
6474         }
6475     }
6476   else
6477     h->plt.plist = NULL;
6478
6479   /* If this is a weak symbol, and there is a real definition, the
6480      processor independent code will have arranged for us to see the
6481      real definition first, and we can just use the same value.  */
6482   if (h->u.weakdef != NULL)
6483     {
6484       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6485                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
6486       h->root.u.def.section = h->u.weakdef->root.u.def.section;
6487       h->root.u.def.value = h->u.weakdef->root.u.def.value;
6488       if (ELIMINATE_COPY_RELOCS)
6489         h->non_got_ref = h->u.weakdef->non_got_ref;
6490       return TRUE;
6491     }
6492
6493   /* If we are creating a shared library, we must presume that the
6494      only references to the symbol are via the global offset table.
6495      For such cases we need not do anything here; the relocations will
6496      be handled correctly by relocate_section.  */
6497   if (info->shared)
6498     return TRUE;
6499
6500   /* If there are no references to this symbol that do not use the
6501      GOT, we don't need to generate a copy reloc.  */
6502   if (!h->non_got_ref)
6503     return TRUE;
6504
6505   /* Don't generate a copy reloc for symbols defined in the executable.  */
6506   if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6507     return TRUE;
6508
6509   if (ELIMINATE_COPY_RELOCS)
6510     {
6511       struct ppc_link_hash_entry * eh;
6512       struct elf_dyn_relocs *p;
6513
6514       eh = (struct ppc_link_hash_entry *) h;
6515       for (p = eh->dyn_relocs; p != NULL; p = p->next)
6516         {
6517           s = p->sec->output_section;
6518           if (s != NULL && (s->flags & SEC_READONLY) != 0)
6519             break;
6520         }
6521
6522       /* If we didn't find any dynamic relocs in read-only sections, then
6523          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
6524       if (p == NULL)
6525         {
6526           h->non_got_ref = 0;
6527           return TRUE;
6528         }
6529     }
6530
6531   if (h->plt.plist != NULL)
6532     {
6533       /* We should never get here, but unfortunately there are versions
6534          of gcc out there that improperly (for this ABI) put initialized
6535          function pointers, vtable refs and suchlike in read-only
6536          sections.  Allow them to proceed, but warn that this might
6537          break at runtime.  */
6538       info->callbacks->einfo
6539         (_("%P: copy reloc against `%s' requires lazy plt linking; "
6540            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6541          h->root.root.string);
6542     }
6543
6544   /* This is a reference to a symbol defined by a dynamic object which
6545      is not a function.  */
6546
6547   if (h->size == 0)
6548     {
6549       info->callbacks->einfo (_("%P: dynamic variable `%s' is zero size\n"),
6550                               h->root.root.string);
6551       return TRUE;
6552     }
6553
6554   /* We must allocate the symbol in our .dynbss section, which will
6555      become part of the .bss section of the executable.  There will be
6556      an entry for this symbol in the .dynsym section.  The dynamic
6557      object will contain position independent code, so all references
6558      from the dynamic object to this symbol will go through the global
6559      offset table.  The dynamic linker will use the .dynsym entry to
6560      determine the address it must put in the global offset table, so
6561      both the dynamic object and the regular object will refer to the
6562      same memory location for the variable.  */
6563
6564   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6565      to copy the initial value out of the dynamic object and into the
6566      runtime process image.  We need to remember the offset into the
6567      .rela.bss section we are going to use.  */
6568   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6569     {
6570       htab->relbss->size += sizeof (Elf64_External_Rela);
6571       h->needs_copy = 1;
6572     }
6573
6574   s = htab->dynbss;
6575
6576   return _bfd_elf_adjust_dynamic_copy (h, s);
6577 }
6578
6579 /* If given a function descriptor symbol, hide both the function code
6580    sym and the descriptor.  */
6581 static void
6582 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6583                        struct elf_link_hash_entry *h,
6584                        bfd_boolean force_local)
6585 {
6586   struct ppc_link_hash_entry *eh;
6587   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6588
6589   eh = (struct ppc_link_hash_entry *) h;
6590   if (eh->is_func_descriptor)
6591     {
6592       struct ppc_link_hash_entry *fh = eh->oh;
6593
6594       if (fh == NULL)
6595         {
6596           const char *p, *q;
6597           struct ppc_link_hash_table *htab;
6598           char save;
6599
6600           /* We aren't supposed to use alloca in BFD because on
6601              systems which do not have alloca the version in libiberty
6602              calls xmalloc, which might cause the program to crash
6603              when it runs out of memory.  This function doesn't have a
6604              return status, so there's no way to gracefully return an
6605              error.  So cheat.  We know that string[-1] can be safely
6606              accessed;  It's either a string in an ELF string table,
6607              or allocated in an objalloc structure.  */
6608
6609           p = eh->elf.root.root.string - 1;
6610           save = *p;
6611           *(char *) p = '.';
6612           htab = ppc_hash_table (info);
6613           if (htab == NULL)
6614             return;
6615
6616           fh = (struct ppc_link_hash_entry *)
6617             elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6618           *(char *) p = save;
6619
6620           /* Unfortunately, if it so happens that the string we were
6621              looking for was allocated immediately before this string,
6622              then we overwrote the string terminator.  That's the only
6623              reason the lookup should fail.  */
6624           if (fh == NULL)
6625             {
6626               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6627               while (q >= eh->elf.root.root.string && *q == *p)
6628                 --q, --p;
6629               if (q < eh->elf.root.root.string && *p == '.')
6630                 fh = (struct ppc_link_hash_entry *)
6631                   elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6632             }
6633           if (fh != NULL)
6634             {
6635               eh->oh = fh;
6636               fh->oh = eh;
6637             }
6638         }
6639       if (fh != NULL)
6640         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6641     }
6642 }
6643
6644 static bfd_boolean
6645 get_sym_h (struct elf_link_hash_entry **hp,
6646            Elf_Internal_Sym **symp,
6647            asection **symsecp,
6648            unsigned char **tls_maskp,
6649            Elf_Internal_Sym **locsymsp,
6650            unsigned long r_symndx,
6651            bfd *ibfd)
6652 {
6653   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6654
6655   if (r_symndx >= symtab_hdr->sh_info)
6656     {
6657       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6658       struct elf_link_hash_entry *h;
6659
6660       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6661       h = elf_follow_link (h);
6662
6663       if (hp != NULL)
6664         *hp = h;
6665
6666       if (symp != NULL)
6667         *symp = NULL;
6668
6669       if (symsecp != NULL)
6670         {
6671           asection *symsec = NULL;
6672           if (h->root.type == bfd_link_hash_defined
6673               || h->root.type == bfd_link_hash_defweak)
6674             symsec = h->root.u.def.section;
6675           *symsecp = symsec;
6676         }
6677
6678       if (tls_maskp != NULL)
6679         {
6680           struct ppc_link_hash_entry *eh;
6681
6682           eh = (struct ppc_link_hash_entry *) h;
6683           *tls_maskp = &eh->tls_mask;
6684         }
6685     }
6686   else
6687     {
6688       Elf_Internal_Sym *sym;
6689       Elf_Internal_Sym *locsyms = *locsymsp;
6690
6691       if (locsyms == NULL)
6692         {
6693           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6694           if (locsyms == NULL)
6695             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6696                                             symtab_hdr->sh_info,
6697                                             0, NULL, NULL, NULL);
6698           if (locsyms == NULL)
6699             return FALSE;
6700           *locsymsp = locsyms;
6701         }
6702       sym = locsyms + r_symndx;
6703
6704       if (hp != NULL)
6705         *hp = NULL;
6706
6707       if (symp != NULL)
6708         *symp = sym;
6709
6710       if (symsecp != NULL)
6711         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6712
6713       if (tls_maskp != NULL)
6714         {
6715           struct got_entry **lgot_ents;
6716           unsigned char *tls_mask;
6717
6718           tls_mask = NULL;
6719           lgot_ents = elf_local_got_ents (ibfd);
6720           if (lgot_ents != NULL)
6721             {
6722               struct plt_entry **local_plt = (struct plt_entry **)
6723                 (lgot_ents + symtab_hdr->sh_info);
6724               unsigned char *lgot_masks = (unsigned char *)
6725                 (local_plt + symtab_hdr->sh_info);
6726               tls_mask = &lgot_masks[r_symndx];
6727             }
6728           *tls_maskp = tls_mask;
6729         }
6730     }
6731   return TRUE;
6732 }
6733
6734 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
6735    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6736    type suitable for optimization, and 1 otherwise.  */
6737
6738 static int
6739 get_tls_mask (unsigned char **tls_maskp,
6740               unsigned long *toc_symndx,
6741               bfd_vma *toc_addend,
6742               Elf_Internal_Sym **locsymsp,
6743               const Elf_Internal_Rela *rel,
6744               bfd *ibfd)
6745 {
6746   unsigned long r_symndx;
6747   int next_r;
6748   struct elf_link_hash_entry *h;
6749   Elf_Internal_Sym *sym;
6750   asection *sec;
6751   bfd_vma off;
6752
6753   r_symndx = ELF64_R_SYM (rel->r_info);
6754   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6755     return 0;
6756
6757   if ((*tls_maskp != NULL && **tls_maskp != 0)
6758       || sec == NULL
6759       || ppc64_elf_section_data (sec) == NULL
6760       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6761     return 1;
6762
6763   /* Look inside a TOC section too.  */
6764   if (h != NULL)
6765     {
6766       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6767       off = h->root.u.def.value;
6768     }
6769   else
6770     off = sym->st_value;
6771   off += rel->r_addend;
6772   BFD_ASSERT (off % 8 == 0);
6773   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6774   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6775   if (toc_symndx != NULL)
6776     *toc_symndx = r_symndx;
6777   if (toc_addend != NULL)
6778     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6779   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6780     return 0;
6781   if ((h == NULL || is_static_defined (h))
6782       && (next_r == -1 || next_r == -2))
6783     return 1 - next_r;
6784   return 1;
6785 }
6786
6787 /* Find (or create) an entry in the tocsave hash table.  */
6788
6789 static struct tocsave_entry *
6790 tocsave_find (struct ppc_link_hash_table *htab,
6791               enum insert_option insert,
6792               Elf_Internal_Sym **local_syms,
6793               const Elf_Internal_Rela *irela,
6794               bfd *ibfd)
6795 {
6796   unsigned long r_indx;
6797   struct elf_link_hash_entry *h;
6798   Elf_Internal_Sym *sym;
6799   struct tocsave_entry ent, *p;
6800   hashval_t hash;
6801   struct tocsave_entry **slot;
6802
6803   r_indx = ELF64_R_SYM (irela->r_info);
6804   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6805     return NULL;
6806   if (ent.sec == NULL || ent.sec->output_section == NULL)
6807     {
6808       (*_bfd_error_handler)
6809         (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
6810       return NULL;
6811     }
6812
6813   if (h != NULL)
6814     ent.offset = h->root.u.def.value;
6815   else
6816     ent.offset = sym->st_value;
6817   ent.offset += irela->r_addend;
6818
6819   hash = tocsave_htab_hash (&ent);
6820   slot = ((struct tocsave_entry **)
6821           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6822   if (slot == NULL)
6823     return NULL;
6824
6825   if (*slot == NULL)
6826     {
6827       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6828       if (p == NULL)
6829         return NULL;
6830       *p = ent;
6831       *slot = p;
6832     }
6833   return *slot;
6834 }
6835
6836 /* Adjust all global syms defined in opd sections.  In gcc generated
6837    code for the old ABI, these will already have been done.  */
6838
6839 static bfd_boolean
6840 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6841 {
6842   struct ppc_link_hash_entry *eh;
6843   asection *sym_sec;
6844   struct _opd_sec_data *opd;
6845
6846   if (h->root.type == bfd_link_hash_indirect)
6847     return TRUE;
6848
6849   if (h->root.type != bfd_link_hash_defined
6850       && h->root.type != bfd_link_hash_defweak)
6851     return TRUE;
6852
6853   eh = (struct ppc_link_hash_entry *) h;
6854   if (eh->adjust_done)
6855     return TRUE;
6856
6857   sym_sec = eh->elf.root.u.def.section;
6858   opd = get_opd_info (sym_sec);
6859   if (opd != NULL && opd->adjust != NULL)
6860     {
6861       long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
6862       if (adjust == -1)
6863         {
6864           /* This entry has been deleted.  */
6865           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6866           if (dsec == NULL)
6867             {
6868               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6869                 if (elf_discarded_section (dsec))
6870                   {
6871                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6872                     break;
6873                   }
6874             }
6875           eh->elf.root.u.def.value = 0;
6876           eh->elf.root.u.def.section = dsec;
6877         }
6878       else
6879         eh->elf.root.u.def.value += adjust;
6880       eh->adjust_done = 1;
6881     }
6882   return TRUE;
6883 }
6884
6885 /* Handles decrementing dynamic reloc counts for the reloc specified by
6886    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM_SEC
6887    have already been determined.  */
6888
6889 static bfd_boolean
6890 dec_dynrel_count (bfd_vma r_info,
6891                   asection *sec,
6892                   struct bfd_link_info *info,
6893                   Elf_Internal_Sym **local_syms,
6894                   struct elf_link_hash_entry *h,
6895                   asection *sym_sec)
6896 {
6897   enum elf_ppc64_reloc_type r_type;
6898   struct elf_dyn_relocs *p;
6899   struct elf_dyn_relocs **pp;
6900
6901   /* Can this reloc be dynamic?  This switch, and later tests here
6902      should be kept in sync with the code in check_relocs.  */
6903   r_type = ELF64_R_TYPE (r_info);
6904   switch (r_type)
6905     {
6906     default:
6907       return TRUE;
6908
6909     case R_PPC64_TPREL16:
6910     case R_PPC64_TPREL16_LO:
6911     case R_PPC64_TPREL16_HI:
6912     case R_PPC64_TPREL16_HA:
6913     case R_PPC64_TPREL16_DS:
6914     case R_PPC64_TPREL16_LO_DS:
6915     case R_PPC64_TPREL16_HIGHER:
6916     case R_PPC64_TPREL16_HIGHERA:
6917     case R_PPC64_TPREL16_HIGHEST:
6918     case R_PPC64_TPREL16_HIGHESTA:
6919       if (!info->shared)
6920         return TRUE;
6921
6922     case R_PPC64_TPREL64:
6923     case R_PPC64_DTPMOD64:
6924     case R_PPC64_DTPREL64:
6925     case R_PPC64_ADDR64:
6926     case R_PPC64_REL30:
6927     case R_PPC64_REL32:
6928     case R_PPC64_REL64:
6929     case R_PPC64_ADDR14:
6930     case R_PPC64_ADDR14_BRNTAKEN:
6931     case R_PPC64_ADDR14_BRTAKEN:
6932     case R_PPC64_ADDR16:
6933     case R_PPC64_ADDR16_DS:
6934     case R_PPC64_ADDR16_HA:
6935     case R_PPC64_ADDR16_HI:
6936     case R_PPC64_ADDR16_HIGHER:
6937     case R_PPC64_ADDR16_HIGHERA:
6938     case R_PPC64_ADDR16_HIGHEST:
6939     case R_PPC64_ADDR16_HIGHESTA:
6940     case R_PPC64_ADDR16_LO:
6941     case R_PPC64_ADDR16_LO_DS:
6942     case R_PPC64_ADDR24:
6943     case R_PPC64_ADDR32:
6944     case R_PPC64_UADDR16:
6945     case R_PPC64_UADDR32:
6946     case R_PPC64_UADDR64:
6947     case R_PPC64_TOC:
6948       break;
6949     }
6950
6951   if (local_syms != NULL)
6952     {
6953       unsigned long r_symndx;
6954       Elf_Internal_Sym *sym;
6955       bfd *ibfd = sec->owner;
6956
6957       r_symndx = ELF64_R_SYM (r_info);
6958       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6959         return FALSE;
6960     }
6961
6962   if ((info->shared
6963        && (must_be_dyn_reloc (info, r_type)
6964            || (h != NULL
6965                && (!info->symbolic
6966                    || h->root.type == bfd_link_hash_defweak
6967                    || !h->def_regular))))
6968       || (ELIMINATE_COPY_RELOCS
6969           && !info->shared
6970           && h != NULL
6971           && (h->root.type == bfd_link_hash_defweak
6972               || !h->def_regular)))
6973     ;
6974   else
6975     return TRUE;
6976
6977   if (h != NULL)
6978     pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6979   else
6980     {
6981       if (sym_sec != NULL)
6982         {
6983           void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6984           pp = (struct elf_dyn_relocs **) vpp;
6985         }
6986       else
6987         {
6988           void *vpp = &elf_section_data (sec)->local_dynrel;
6989           pp = (struct elf_dyn_relocs **) vpp;
6990         }
6991
6992       /* elf_gc_sweep may have already removed all dyn relocs associated
6993          with local syms for a given section.  Don't report a dynreloc
6994          miscount.  */
6995       if (*pp == NULL)
6996         return TRUE;
6997     }
6998
6999   while ((p = *pp) != NULL)
7000     {
7001       if (p->sec == sec)
7002         {
7003           if (!must_be_dyn_reloc (info, r_type))
7004             p->pc_count -= 1;
7005           p->count -= 1;
7006           if (p->count == 0)
7007             *pp = p->next;
7008           return TRUE;
7009         }
7010       pp = &p->next;
7011     }
7012
7013   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7014                           sec->owner, sec);
7015   bfd_set_error (bfd_error_bad_value);
7016   return FALSE;
7017 }
7018
7019 /* Remove unused Official Procedure Descriptor entries.  Currently we
7020    only remove those associated with functions in discarded link-once
7021    sections, or weakly defined functions that have been overridden.  It
7022    would be possible to remove many more entries for statically linked
7023    applications.  */
7024
7025 bfd_boolean
7026 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
7027 {
7028   bfd *ibfd;
7029   bfd_boolean some_edited = FALSE;
7030   asection *need_pad = NULL;
7031
7032   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7033     {
7034       asection *sec;
7035       Elf_Internal_Rela *relstart, *rel, *relend;
7036       Elf_Internal_Shdr *symtab_hdr;
7037       Elf_Internal_Sym *local_syms;
7038       bfd_vma offset;
7039       struct _opd_sec_data *opd;
7040       bfd_boolean need_edit, add_aux_fields;
7041       bfd_size_type cnt_16b = 0;
7042
7043       if (!is_ppc64_elf (ibfd))
7044         continue;
7045
7046       sec = bfd_get_section_by_name (ibfd, ".opd");
7047       if (sec == NULL || sec->size == 0)
7048         continue;
7049
7050       if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
7051         continue;
7052
7053       if (sec->output_section == bfd_abs_section_ptr)
7054         continue;
7055
7056       /* Look through the section relocs.  */
7057       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7058         continue;
7059
7060       local_syms = NULL;
7061       symtab_hdr = &elf_symtab_hdr (ibfd);
7062
7063       /* Read the relocations.  */
7064       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7065                                             info->keep_memory);
7066       if (relstart == NULL)
7067         return FALSE;
7068
7069       /* First run through the relocs to check they are sane, and to
7070          determine whether we need to edit this opd section.  */
7071       need_edit = FALSE;
7072       need_pad = sec;
7073       offset = 0;
7074       relend = relstart + sec->reloc_count;
7075       for (rel = relstart; rel < relend; )
7076         {
7077           enum elf_ppc64_reloc_type r_type;
7078           unsigned long r_symndx;
7079           asection *sym_sec;
7080           struct elf_link_hash_entry *h;
7081           Elf_Internal_Sym *sym;
7082
7083           /* .opd contains a regular array of 16 or 24 byte entries.  We're
7084              only interested in the reloc pointing to a function entry
7085              point.  */
7086           if (rel->r_offset != offset
7087               || rel + 1 >= relend
7088               || (rel + 1)->r_offset != offset + 8)
7089             {
7090               /* If someone messes with .opd alignment then after a
7091                  "ld -r" we might have padding in the middle of .opd.
7092                  Also, there's nothing to prevent someone putting
7093                  something silly in .opd with the assembler.  No .opd
7094                  optimization for them!  */
7095             broken_opd:
7096               (*_bfd_error_handler)
7097                 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7098               need_edit = FALSE;
7099               break;
7100             }
7101
7102           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7103               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7104             {
7105               (*_bfd_error_handler)
7106                 (_("%B: unexpected reloc type %u in .opd section"),
7107                  ibfd, r_type);
7108               need_edit = FALSE;
7109               break;
7110             }
7111
7112           r_symndx = ELF64_R_SYM (rel->r_info);
7113           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7114                           r_symndx, ibfd))
7115             goto error_ret;
7116
7117           if (sym_sec == NULL || sym_sec->owner == NULL)
7118             {
7119               const char *sym_name;
7120               if (h != NULL)
7121                 sym_name = h->root.root.string;
7122               else
7123                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7124                                              sym_sec);
7125
7126               (*_bfd_error_handler)
7127                 (_("%B: undefined sym `%s' in .opd section"),
7128                  ibfd, sym_name);
7129               need_edit = FALSE;
7130               break;
7131             }
7132
7133           /* opd entries are always for functions defined in the
7134              current input bfd.  If the symbol isn't defined in the
7135              input bfd, then we won't be using the function in this
7136              bfd;  It must be defined in a linkonce section in another
7137              bfd, or is weak.  It's also possible that we are
7138              discarding the function due to a linker script /DISCARD/,
7139              which we test for via the output_section.  */
7140           if (sym_sec->owner != ibfd
7141               || sym_sec->output_section == bfd_abs_section_ptr)
7142             need_edit = TRUE;
7143
7144           rel += 2;
7145           if (rel == relend
7146               || (rel + 1 == relend && rel->r_offset == offset + 16))
7147             {
7148               if (sec->size == offset + 24)
7149                 {
7150                   need_pad = NULL;
7151                   break;
7152                 }
7153               if (rel == relend && sec->size == offset + 16)
7154                 {
7155                   cnt_16b++;
7156                   break;
7157                 }
7158               goto broken_opd;
7159             }
7160
7161           if (rel->r_offset == offset + 24)
7162             offset += 24;
7163           else if (rel->r_offset != offset + 16)
7164             goto broken_opd;
7165           else if (rel + 1 < relend
7166                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7167                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7168             {
7169               offset += 16;
7170               cnt_16b++;
7171             }
7172           else if (rel + 2 < relend
7173                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7174                    && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7175             {
7176               offset += 24;
7177               rel += 1;
7178             }
7179           else
7180             goto broken_opd;
7181         }
7182
7183       add_aux_fields = non_overlapping && cnt_16b > 0;
7184
7185       if (need_edit || add_aux_fields)
7186         {
7187           Elf_Internal_Rela *write_rel;
7188           Elf_Internal_Shdr *rel_hdr;
7189           bfd_byte *rptr, *wptr;
7190           bfd_byte *new_contents;
7191           bfd_boolean skip;
7192           long opd_ent_size;
7193           bfd_size_type amt;
7194
7195           new_contents = NULL;
7196           amt = sec->size * sizeof (long) / 8;
7197           opd = &ppc64_elf_section_data (sec)->u.opd;
7198           opd->adjust = bfd_zalloc (sec->owner, amt);
7199           if (opd->adjust == NULL)
7200             return FALSE;
7201           ppc64_elf_section_data (sec)->sec_type = sec_opd;
7202
7203           /* This seems a waste of time as input .opd sections are all
7204              zeros as generated by gcc, but I suppose there's no reason
7205              this will always be so.  We might start putting something in
7206              the third word of .opd entries.  */
7207           if ((sec->flags & SEC_IN_MEMORY) == 0)
7208             {
7209               bfd_byte *loc;
7210               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7211                 {
7212                   if (loc != NULL)
7213                     free (loc);
7214                 error_ret:
7215                   if (local_syms != NULL
7216                       && symtab_hdr->contents != (unsigned char *) local_syms)
7217                     free (local_syms);
7218                   if (elf_section_data (sec)->relocs != relstart)
7219                     free (relstart);
7220                   return FALSE;
7221                 }
7222               sec->contents = loc;
7223               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7224             }
7225
7226           elf_section_data (sec)->relocs = relstart;
7227
7228           new_contents = sec->contents;
7229           if (add_aux_fields)
7230             {
7231               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7232               if (new_contents == NULL)
7233                 return FALSE;
7234               need_pad = FALSE;
7235             }
7236           wptr = new_contents;
7237           rptr = sec->contents;
7238
7239           write_rel = relstart;
7240           skip = FALSE;
7241           offset = 0;
7242           opd_ent_size = 0;
7243           for (rel = relstart; rel < relend; rel++)
7244             {
7245               unsigned long r_symndx;
7246               asection *sym_sec;
7247               struct elf_link_hash_entry *h;
7248               Elf_Internal_Sym *sym;
7249
7250               r_symndx = ELF64_R_SYM (rel->r_info);
7251               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7252                               r_symndx, ibfd))
7253                 goto error_ret;
7254
7255               if (rel->r_offset == offset)
7256                 {
7257                   struct ppc_link_hash_entry *fdh = NULL;
7258
7259                   /* See if the .opd entry is full 24 byte or
7260                      16 byte (with fd_aux entry overlapped with next
7261                      fd_func).  */
7262                   opd_ent_size = 24;
7263                   if ((rel + 2 == relend && sec->size == offset + 16)
7264                       || (rel + 3 < relend
7265                           && rel[2].r_offset == offset + 16
7266                           && rel[3].r_offset == offset + 24
7267                           && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7268                           && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7269                     opd_ent_size = 16;
7270
7271                   if (h != NULL
7272                       && h->root.root.string[0] == '.')
7273                     {
7274                       struct ppc_link_hash_table *htab;
7275
7276                       htab = ppc_hash_table (info);
7277                       if (htab != NULL)
7278                         fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7279                                           htab);
7280                       if (fdh != NULL
7281                           && fdh->elf.root.type != bfd_link_hash_defined
7282                           && fdh->elf.root.type != bfd_link_hash_defweak)
7283                         fdh = NULL;
7284                     }
7285
7286                   skip = (sym_sec->owner != ibfd
7287                           || sym_sec->output_section == bfd_abs_section_ptr);
7288                   if (skip)
7289                     {
7290                       if (fdh != NULL && sym_sec->owner == ibfd)
7291                         {
7292                           /* Arrange for the function descriptor sym
7293                              to be dropped.  */
7294                           fdh->elf.root.u.def.value = 0;
7295                           fdh->elf.root.u.def.section = sym_sec;
7296                         }
7297                       opd->adjust[rel->r_offset / 8] = -1;
7298                     }
7299                   else
7300                     {
7301                       /* We'll be keeping this opd entry.  */
7302
7303                       if (fdh != NULL)
7304                         {
7305                           /* Redefine the function descriptor symbol to
7306                              this location in the opd section.  It is
7307                              necessary to update the value here rather
7308                              than using an array of adjustments as we do
7309                              for local symbols, because various places
7310                              in the generic ELF code use the value
7311                              stored in u.def.value.  */
7312                           fdh->elf.root.u.def.value = wptr - new_contents;
7313                           fdh->adjust_done = 1;
7314                         }
7315
7316                       /* Local syms are a bit tricky.  We could
7317                          tweak them as they can be cached, but
7318                          we'd need to look through the local syms
7319                          for the function descriptor sym which we
7320                          don't have at the moment.  So keep an
7321                          array of adjustments.  */
7322                       opd->adjust[rel->r_offset / 8]
7323                         = (wptr - new_contents) - (rptr - sec->contents);
7324
7325                       if (wptr != rptr)
7326                         memcpy (wptr, rptr, opd_ent_size);
7327                       wptr += opd_ent_size;
7328                       if (add_aux_fields && opd_ent_size == 16)
7329                         {
7330                           memset (wptr, '\0', 8);
7331                           wptr += 8;
7332                         }
7333                     }
7334                   rptr += opd_ent_size;
7335                   offset += opd_ent_size;
7336                 }
7337
7338               if (skip)
7339                 {
7340                   if (!NO_OPD_RELOCS
7341                       && !info->relocatable
7342                       && !dec_dynrel_count (rel->r_info, sec, info,
7343                                             NULL, h, sym_sec))
7344                     goto error_ret;
7345                 }
7346               else
7347                 {
7348                   /* We need to adjust any reloc offsets to point to the
7349                      new opd entries.  While we're at it, we may as well
7350                      remove redundant relocs.  */
7351                   rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
7352                   if (write_rel != rel)
7353                     memcpy (write_rel, rel, sizeof (*rel));
7354                   ++write_rel;
7355                 }
7356             }
7357
7358           sec->size = wptr - new_contents;
7359           sec->reloc_count = write_rel - relstart;
7360           if (add_aux_fields)
7361             {
7362               free (sec->contents);
7363               sec->contents = new_contents;
7364             }
7365
7366           /* Fudge the header size too, as this is used later in
7367              elf_bfd_final_link if we are emitting relocs.  */
7368           rel_hdr = _bfd_elf_single_rel_hdr (sec);
7369           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7370           some_edited = TRUE;
7371         }
7372       else if (elf_section_data (sec)->relocs != relstart)
7373         free (relstart);
7374
7375       if (local_syms != NULL
7376           && symtab_hdr->contents != (unsigned char *) local_syms)
7377         {
7378           if (!info->keep_memory)
7379             free (local_syms);
7380           else
7381             symtab_hdr->contents = (unsigned char *) local_syms;
7382         }
7383     }
7384
7385   if (some_edited)
7386     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7387
7388   /* If we are doing a final link and the last .opd entry is just 16 byte
7389      long, add a 8 byte padding after it.  */
7390   if (need_pad != NULL && !info->relocatable)
7391     {
7392       bfd_byte *p;
7393
7394       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7395         {
7396           BFD_ASSERT (need_pad->size > 0);
7397
7398           p = bfd_malloc (need_pad->size + 8);
7399           if (p == NULL)
7400             return FALSE;
7401
7402           if (! bfd_get_section_contents (need_pad->owner, need_pad,
7403                                           p, 0, need_pad->size))
7404             return FALSE;
7405
7406           need_pad->contents = p;
7407           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7408         }
7409       else
7410         {
7411           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7412           if (p == NULL)
7413             return FALSE;
7414
7415           need_pad->contents = p;
7416         }
7417
7418       memset (need_pad->contents + need_pad->size, 0, 8);
7419       need_pad->size += 8;
7420     }
7421
7422   return TRUE;
7423 }
7424
7425 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
7426
7427 asection *
7428 ppc64_elf_tls_setup (struct bfd_link_info *info,
7429                      int no_tls_get_addr_opt,
7430                      int *no_multi_toc)
7431 {
7432   struct ppc_link_hash_table *htab;
7433
7434   htab = ppc_hash_table (info);
7435   if (htab == NULL)
7436     return NULL;
7437
7438   if (*no_multi_toc)
7439     htab->do_multi_toc = 0;
7440   else if (!htab->do_multi_toc)
7441     *no_multi_toc = 1;
7442
7443   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7444                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7445                                               FALSE, FALSE, TRUE));
7446   /* Move dynamic linking info to the function descriptor sym.  */
7447   if (htab->tls_get_addr != NULL)
7448     func_desc_adjust (&htab->tls_get_addr->elf, info);
7449   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7450                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7451                                                  FALSE, FALSE, TRUE));
7452   if (!no_tls_get_addr_opt)
7453     {
7454       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7455
7456       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7457                                   FALSE, FALSE, TRUE);
7458       if (opt != NULL)
7459         func_desc_adjust (opt, info);
7460       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7461                                      FALSE, FALSE, TRUE);
7462       if (opt_fd != NULL
7463           && (opt_fd->root.type == bfd_link_hash_defined
7464               || opt_fd->root.type == bfd_link_hash_defweak))
7465         {
7466           /* If glibc supports an optimized __tls_get_addr call stub,
7467              signalled by the presence of __tls_get_addr_opt, and we'll
7468              be calling __tls_get_addr via a plt call stub, then
7469              make __tls_get_addr point to __tls_get_addr_opt.  */
7470           tga_fd = &htab->tls_get_addr_fd->elf;
7471           if (htab->elf.dynamic_sections_created
7472               && tga_fd != NULL
7473               && (tga_fd->type == STT_FUNC
7474                   || tga_fd->needs_plt)
7475               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7476                    || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7477                        && tga_fd->root.type == bfd_link_hash_undefweak)))
7478             {
7479               struct plt_entry *ent;
7480
7481               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7482                 if (ent->plt.refcount > 0)
7483                   break;
7484               if (ent != NULL)
7485                 {
7486                   tga_fd->root.type = bfd_link_hash_indirect;
7487                   tga_fd->root.u.i.link = &opt_fd->root;
7488                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7489                   if (opt_fd->dynindx != -1)
7490                     {
7491                       /* Use __tls_get_addr_opt in dynamic relocations.  */
7492                       opt_fd->dynindx = -1;
7493                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7494                                               opt_fd->dynstr_index);
7495                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7496                         return NULL;
7497                     }
7498                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7499                   tga = &htab->tls_get_addr->elf;
7500                   if (opt != NULL && tga != NULL)
7501                     {
7502                       tga->root.type = bfd_link_hash_indirect;
7503                       tga->root.u.i.link = &opt->root;
7504                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
7505                       _bfd_elf_link_hash_hide_symbol (info, opt,
7506                                                       tga->forced_local);
7507                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7508                     }
7509                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7510                   htab->tls_get_addr_fd->is_func_descriptor = 1;
7511                   if (htab->tls_get_addr != NULL)
7512                     {
7513                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7514                       htab->tls_get_addr->is_func = 1;
7515                     }
7516                 }
7517             }
7518         }
7519       else
7520         no_tls_get_addr_opt = TRUE;
7521     }
7522   htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
7523   return _bfd_elf_tls_setup (info->output_bfd, info);
7524 }
7525
7526 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7527    HASH1 or HASH2.  */
7528
7529 static bfd_boolean
7530 branch_reloc_hash_match (const bfd *ibfd,
7531                          const Elf_Internal_Rela *rel,
7532                          const struct ppc_link_hash_entry *hash1,
7533                          const struct ppc_link_hash_entry *hash2)
7534 {
7535   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7536   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7537   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7538
7539   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7540     {
7541       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7542       struct elf_link_hash_entry *h;
7543
7544       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7545       h = elf_follow_link (h);
7546       if (h == &hash1->elf || h == &hash2->elf)
7547         return TRUE;
7548     }
7549   return FALSE;
7550 }
7551
7552 /* Run through all the TLS relocs looking for optimization
7553    opportunities.  The linker has been hacked (see ppc64elf.em) to do
7554    a preliminary section layout so that we know the TLS segment
7555    offsets.  We can't optimize earlier because some optimizations need
7556    to know the tp offset, and we need to optimize before allocating
7557    dynamic relocations.  */
7558
7559 bfd_boolean
7560 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7561 {
7562   bfd *ibfd;
7563   asection *sec;
7564   struct ppc_link_hash_table *htab;
7565   unsigned char *toc_ref;
7566   int pass;
7567
7568   if (info->relocatable || !info->executable)
7569     return TRUE;
7570
7571   htab = ppc_hash_table (info);
7572   if (htab == NULL)
7573     return FALSE;
7574
7575   /* Make two passes over the relocs.  On the first pass, mark toc
7576      entries involved with tls relocs, and check that tls relocs
7577      involved in setting up a tls_get_addr call are indeed followed by
7578      such a call.  If they are not, we can't do any tls optimization.
7579      On the second pass twiddle tls_mask flags to notify
7580      relocate_section that optimization can be done, and adjust got
7581      and plt refcounts.  */
7582   toc_ref = NULL;
7583   for (pass = 0; pass < 2; ++pass)
7584     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7585       {
7586         Elf_Internal_Sym *locsyms = NULL;
7587         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7588
7589         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7590           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7591             {
7592               Elf_Internal_Rela *relstart, *rel, *relend;
7593               bfd_boolean found_tls_get_addr_arg = 0;
7594
7595               /* Read the relocations.  */
7596               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7597                                                     info->keep_memory);
7598               if (relstart == NULL)
7599                 return FALSE;
7600
7601               relend = relstart + sec->reloc_count;
7602               for (rel = relstart; rel < relend; rel++)
7603                 {
7604                   enum elf_ppc64_reloc_type r_type;
7605                   unsigned long r_symndx;
7606                   struct elf_link_hash_entry *h;
7607                   Elf_Internal_Sym *sym;
7608                   asection *sym_sec;
7609                   unsigned char *tls_mask;
7610                   unsigned char tls_set, tls_clear, tls_type = 0;
7611                   bfd_vma value;
7612                   bfd_boolean ok_tprel, is_local;
7613                   long toc_ref_index = 0;
7614                   int expecting_tls_get_addr = 0;
7615                   bfd_boolean ret = FALSE;
7616
7617                   r_symndx = ELF64_R_SYM (rel->r_info);
7618                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7619                                   r_symndx, ibfd))
7620                     {
7621                     err_free_rel:
7622                       if (elf_section_data (sec)->relocs != relstart)
7623                         free (relstart);
7624                       if (toc_ref != NULL)
7625                         free (toc_ref);
7626                       if (locsyms != NULL
7627                           && (elf_symtab_hdr (ibfd).contents
7628                               != (unsigned char *) locsyms))
7629                         free (locsyms);
7630                       return ret;
7631                     }
7632
7633                   if (h != NULL)
7634                     {
7635                       if (h->root.type == bfd_link_hash_defined
7636                           || h->root.type == bfd_link_hash_defweak)
7637                         value = h->root.u.def.value;
7638                       else if (h->root.type == bfd_link_hash_undefweak)
7639                         value = 0;
7640                       else
7641                         {
7642                           found_tls_get_addr_arg = 0;
7643                           continue;
7644                         }
7645                     }
7646                   else
7647                     /* Symbols referenced by TLS relocs must be of type
7648                        STT_TLS.  So no need for .opd local sym adjust.  */
7649                     value = sym->st_value;
7650
7651                   ok_tprel = FALSE;
7652                   is_local = FALSE;
7653                   if (h == NULL
7654                       || !h->def_dynamic)
7655                     {
7656                       is_local = TRUE;
7657                       if (h != NULL
7658                           && h->root.type == bfd_link_hash_undefweak)
7659                         ok_tprel = TRUE;
7660                       else
7661                         {
7662                           value += sym_sec->output_offset;
7663                           value += sym_sec->output_section->vma;
7664                           value -= htab->elf.tls_sec->vma;
7665                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7666                                       < (bfd_vma) 1 << 32);
7667                         }
7668                     }
7669
7670                   r_type = ELF64_R_TYPE (rel->r_info);
7671                   /* If this section has old-style __tls_get_addr calls
7672                      without marker relocs, then check that each
7673                      __tls_get_addr call reloc is preceded by a reloc
7674                      that conceivably belongs to the __tls_get_addr arg
7675                      setup insn.  If we don't find matching arg setup
7676                      relocs, don't do any tls optimization.  */
7677                   if (pass == 0
7678                       && sec->has_tls_get_addr_call
7679                       && h != NULL
7680                       && (h == &htab->tls_get_addr->elf
7681                           || h == &htab->tls_get_addr_fd->elf)
7682                       && !found_tls_get_addr_arg
7683                       && is_branch_reloc (r_type))
7684                     {
7685                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7686                                                 "TLS optimization disabled\n"),
7687                                               ibfd, sec, rel->r_offset);
7688                       ret = TRUE;
7689                       goto err_free_rel;
7690                     }
7691
7692                   found_tls_get_addr_arg = 0;
7693                   switch (r_type)
7694                     {
7695                     case R_PPC64_GOT_TLSLD16:
7696                     case R_PPC64_GOT_TLSLD16_LO:
7697                       expecting_tls_get_addr = 1;
7698                       found_tls_get_addr_arg = 1;
7699                       /* Fall thru */
7700
7701                     case R_PPC64_GOT_TLSLD16_HI:
7702                     case R_PPC64_GOT_TLSLD16_HA:
7703                       /* These relocs should never be against a symbol
7704                          defined in a shared lib.  Leave them alone if
7705                          that turns out to be the case.  */
7706                       if (!is_local)
7707                         continue;
7708
7709                       /* LD -> LE */
7710                       tls_set = 0;
7711                       tls_clear = TLS_LD;
7712                       tls_type = TLS_TLS | TLS_LD;
7713                       break;
7714
7715                     case R_PPC64_GOT_TLSGD16:
7716                     case R_PPC64_GOT_TLSGD16_LO:
7717                       expecting_tls_get_addr = 1;
7718                       found_tls_get_addr_arg = 1;
7719                       /* Fall thru */
7720
7721                     case R_PPC64_GOT_TLSGD16_HI:
7722                     case R_PPC64_GOT_TLSGD16_HA:
7723                       if (ok_tprel)
7724                         /* GD -> LE */
7725                         tls_set = 0;
7726                       else
7727                         /* GD -> IE */
7728                         tls_set = TLS_TLS | TLS_TPRELGD;
7729                       tls_clear = TLS_GD;
7730                       tls_type = TLS_TLS | TLS_GD;
7731                       break;
7732
7733                     case R_PPC64_GOT_TPREL16_DS:
7734                     case R_PPC64_GOT_TPREL16_LO_DS:
7735                     case R_PPC64_GOT_TPREL16_HI:
7736                     case R_PPC64_GOT_TPREL16_HA:
7737                       if (ok_tprel)
7738                         {
7739                           /* IE -> LE */
7740                           tls_set = 0;
7741                           tls_clear = TLS_TPREL;
7742                           tls_type = TLS_TLS | TLS_TPREL;
7743                           break;
7744                         }
7745                       continue;
7746
7747                     case R_PPC64_TLSGD:
7748                     case R_PPC64_TLSLD:
7749                       found_tls_get_addr_arg = 1;
7750                       /* Fall thru */
7751
7752                     case R_PPC64_TLS:
7753                     case R_PPC64_TOC16:
7754                     case R_PPC64_TOC16_LO:
7755                       if (sym_sec == NULL || sym_sec != toc)
7756                         continue;
7757
7758                       /* Mark this toc entry as referenced by a TLS
7759                          code sequence.  We can do that now in the
7760                          case of R_PPC64_TLS, and after checking for
7761                          tls_get_addr for the TOC16 relocs.  */
7762                       if (toc_ref == NULL)
7763                         toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
7764                       if (toc_ref == NULL)
7765                         goto err_free_rel;
7766
7767                       if (h != NULL)
7768                         value = h->root.u.def.value;
7769                       else
7770                         value = sym->st_value;
7771                       value += rel->r_addend;
7772                       BFD_ASSERT (value < toc->size && value % 8 == 0);
7773                       toc_ref_index = (value + toc->output_offset) / 8;
7774                       if (r_type == R_PPC64_TLS
7775                           || r_type == R_PPC64_TLSGD
7776                           || r_type == R_PPC64_TLSLD)
7777                         {
7778                           toc_ref[toc_ref_index] = 1;
7779                           continue;
7780                         }
7781
7782                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
7783                         continue;
7784
7785                       tls_set = 0;
7786                       tls_clear = 0;
7787                       expecting_tls_get_addr = 2;
7788                       break;
7789
7790                     case R_PPC64_TPREL64:
7791                       if (pass == 0
7792                           || sec != toc
7793                           || toc_ref == NULL
7794                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7795                         continue;
7796                       if (ok_tprel)
7797                         {
7798                           /* IE -> LE */
7799                           tls_set = TLS_EXPLICIT;
7800                           tls_clear = TLS_TPREL;
7801                           break;
7802                         }
7803                       continue;
7804
7805                     case R_PPC64_DTPMOD64:
7806                       if (pass == 0
7807                           || sec != toc
7808                           || toc_ref == NULL
7809                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7810                         continue;
7811                       if (rel + 1 < relend
7812                           && (rel[1].r_info
7813                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7814                           && rel[1].r_offset == rel->r_offset + 8)
7815                         {
7816                           if (ok_tprel)
7817                             /* GD -> LE */
7818                             tls_set = TLS_EXPLICIT | TLS_GD;
7819                           else
7820                             /* GD -> IE */
7821                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7822                           tls_clear = TLS_GD;
7823                         }
7824                       else
7825                         {
7826                           if (!is_local)
7827                             continue;
7828
7829                           /* LD -> LE */
7830                           tls_set = TLS_EXPLICIT;
7831                           tls_clear = TLS_LD;
7832                         }
7833                       break;
7834
7835                     default:
7836                       continue;
7837                     }
7838
7839                   if (pass == 0)
7840                     {
7841                       if (!expecting_tls_get_addr
7842                           || !sec->has_tls_get_addr_call)
7843                         continue;
7844
7845                       if (rel + 1 < relend
7846                           && branch_reloc_hash_match (ibfd, rel + 1,
7847                                                       htab->tls_get_addr,
7848                                                       htab->tls_get_addr_fd))
7849                         {
7850                           if (expecting_tls_get_addr == 2)
7851                             {
7852                               /* Check for toc tls entries.  */
7853                               unsigned char *toc_tls;
7854                               int retval;
7855
7856                               retval = get_tls_mask (&toc_tls, NULL, NULL,
7857                                                      &locsyms,
7858                                                      rel, ibfd);
7859                               if (retval == 0)
7860                                 goto err_free_rel;
7861                               if (toc_tls != NULL)
7862                                 {
7863                                   if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
7864                                     found_tls_get_addr_arg = 1;
7865                                   if (retval > 1)
7866                                     toc_ref[toc_ref_index] = 1;
7867                                 }
7868                             }
7869                           continue;
7870                         }
7871
7872                       if (expecting_tls_get_addr != 1)
7873                         continue;
7874
7875                       /* Uh oh, we didn't find the expected call.  We
7876                          could just mark this symbol to exclude it
7877                          from tls optimization but it's safer to skip
7878                          the entire optimization.  */
7879                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
7880                                                 "TLS optimization disabled\n"),
7881                                               ibfd, sec, rel->r_offset);
7882                       ret = TRUE;
7883                       goto err_free_rel;
7884                     }
7885
7886                   if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
7887                     {
7888                       struct plt_entry *ent;
7889                       for (ent = htab->tls_get_addr->elf.plt.plist;
7890                            ent != NULL;
7891                            ent = ent->next)
7892                         if (ent->addend == 0)
7893                           {
7894                             if (ent->plt.refcount > 0)
7895                               {
7896                                 ent->plt.refcount -= 1;
7897                                 expecting_tls_get_addr = 0;
7898                               }
7899                             break;
7900                           }
7901                     }
7902
7903                   if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
7904                     {
7905                       struct plt_entry *ent;
7906                       for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7907                            ent != NULL;
7908                            ent = ent->next)
7909                         if (ent->addend == 0)
7910                           {
7911                             if (ent->plt.refcount > 0)
7912                               ent->plt.refcount -= 1;
7913                             break;
7914                           }
7915                     }
7916
7917                   if (tls_clear == 0)
7918                     continue;
7919
7920                   if ((tls_set & TLS_EXPLICIT) == 0)
7921                     {
7922                       struct got_entry *ent;
7923
7924                       /* Adjust got entry for this reloc.  */
7925                       if (h != NULL)
7926                         ent = h->got.glist;
7927                       else
7928                         ent = elf_local_got_ents (ibfd)[r_symndx];
7929
7930                       for (; ent != NULL; ent = ent->next)
7931                         if (ent->addend == rel->r_addend
7932                             && ent->owner == ibfd
7933                             && ent->tls_type == tls_type)
7934                           break;
7935                       if (ent == NULL)
7936                         abort ();
7937
7938                       if (tls_set == 0)
7939                         {
7940                           /* We managed to get rid of a got entry.  */
7941                           if (ent->got.refcount > 0)
7942                             ent->got.refcount -= 1;
7943                         }
7944                     }
7945                   else
7946                     {
7947                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
7948                          we'll lose one or two dyn relocs.  */
7949                       if (!dec_dynrel_count (rel->r_info, sec, info,
7950                                              NULL, h, sym_sec))
7951                         return FALSE;
7952
7953                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
7954                         {
7955                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7956                                                  NULL, h, sym_sec))
7957                             return FALSE;
7958                         }
7959                     }
7960
7961                   *tls_mask |= tls_set;
7962                   *tls_mask &= ~tls_clear;
7963                 }
7964
7965               if (elf_section_data (sec)->relocs != relstart)
7966                 free (relstart);
7967             }
7968
7969         if (locsyms != NULL
7970             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7971           {
7972             if (!info->keep_memory)
7973               free (locsyms);
7974             else
7975               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7976           }
7977       }
7978
7979   if (toc_ref != NULL)
7980     free (toc_ref);
7981   return TRUE;
7982 }
7983
7984 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7985    the values of any global symbols in a toc section that has been
7986    edited.  Globals in toc sections should be a rarity, so this function
7987    sets a flag if any are found in toc sections other than the one just
7988    edited, so that futher hash table traversals can be avoided.  */
7989
7990 struct adjust_toc_info
7991 {
7992   asection *toc;
7993   unsigned long *skip;
7994   bfd_boolean global_toc_syms;
7995 };
7996
7997 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7998
7999 static bfd_boolean
8000 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8001 {
8002   struct ppc_link_hash_entry *eh;
8003   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8004   unsigned long i;
8005
8006   if (h->root.type != bfd_link_hash_defined
8007       && h->root.type != bfd_link_hash_defweak)
8008     return TRUE;
8009
8010   eh = (struct ppc_link_hash_entry *) h;
8011   if (eh->adjust_done)
8012     return TRUE;
8013
8014   if (eh->elf.root.u.def.section == toc_inf->toc)
8015     {
8016       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8017         i = toc_inf->toc->rawsize >> 3;
8018       else
8019         i = eh->elf.root.u.def.value >> 3;
8020
8021       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8022         {
8023           (*_bfd_error_handler)
8024             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8025           do
8026             ++i;
8027           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8028           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8029         }
8030
8031       eh->elf.root.u.def.value -= toc_inf->skip[i];
8032       eh->adjust_done = 1;
8033     }
8034   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8035     toc_inf->global_toc_syms = TRUE;
8036
8037   return TRUE;
8038 }
8039
8040 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc.  */
8041
8042 static bfd_boolean
8043 ok_lo_toc_insn (unsigned int insn)
8044 {
8045   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8046           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8047           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8048           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8049           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8050           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8051           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8052           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8053           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8054           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8055           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8056           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8057           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8058           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8059           || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8060               && (insn & 3) != 1)
8061           || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8062               && ((insn & 3) == 0 || (insn & 3) == 3))
8063           || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8064 }
8065
8066 /* Examine all relocs referencing .toc sections in order to remove
8067    unused .toc entries.  */
8068
8069 bfd_boolean
8070 ppc64_elf_edit_toc (struct bfd_link_info *info)
8071 {
8072   bfd *ibfd;
8073   struct adjust_toc_info toc_inf;
8074   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8075
8076   htab->do_toc_opt = 1;
8077   toc_inf.global_toc_syms = TRUE;
8078   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8079     {
8080       asection *toc, *sec;
8081       Elf_Internal_Shdr *symtab_hdr;
8082       Elf_Internal_Sym *local_syms;
8083       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8084       unsigned long *skip, *drop;
8085       unsigned char *used;
8086       unsigned char *keep, last, some_unused;
8087
8088       if (!is_ppc64_elf (ibfd))
8089         continue;
8090
8091       toc = bfd_get_section_by_name (ibfd, ".toc");
8092       if (toc == NULL
8093           || toc->size == 0
8094           || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
8095           || elf_discarded_section (toc))
8096         continue;
8097
8098       toc_relocs = NULL;
8099       local_syms = NULL;
8100       symtab_hdr = &elf_symtab_hdr (ibfd);
8101
8102       /* Look at sections dropped from the final link.  */
8103       skip = NULL;
8104       relstart = NULL;
8105       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8106         {
8107           if (sec->reloc_count == 0
8108               || !elf_discarded_section (sec)
8109               || get_opd_info (sec)
8110               || (sec->flags & SEC_ALLOC) == 0
8111               || (sec->flags & SEC_DEBUGGING) != 0)
8112             continue;
8113
8114           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8115           if (relstart == NULL)
8116             goto error_ret;
8117
8118           /* Run through the relocs to see which toc entries might be
8119              unused.  */
8120           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8121             {
8122               enum elf_ppc64_reloc_type r_type;
8123               unsigned long r_symndx;
8124               asection *sym_sec;
8125               struct elf_link_hash_entry *h;
8126               Elf_Internal_Sym *sym;
8127               bfd_vma val;
8128
8129               r_type = ELF64_R_TYPE (rel->r_info);
8130               switch (r_type)
8131                 {
8132                 default:
8133                   continue;
8134
8135                 case R_PPC64_TOC16:
8136                 case R_PPC64_TOC16_LO:
8137                 case R_PPC64_TOC16_HI:
8138                 case R_PPC64_TOC16_HA:
8139                 case R_PPC64_TOC16_DS:
8140                 case R_PPC64_TOC16_LO_DS:
8141                   break;
8142                 }
8143
8144               r_symndx = ELF64_R_SYM (rel->r_info);
8145               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8146                               r_symndx, ibfd))
8147                 goto error_ret;
8148
8149               if (sym_sec != toc)
8150                 continue;
8151
8152               if (h != NULL)
8153                 val = h->root.u.def.value;
8154               else
8155                 val = sym->st_value;
8156               val += rel->r_addend;
8157
8158               if (val >= toc->size)
8159                 continue;
8160
8161               /* Anything in the toc ought to be aligned to 8 bytes.
8162                  If not, don't mark as unused.  */
8163               if (val & 7)
8164                 continue;
8165
8166               if (skip == NULL)
8167                 {
8168                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8169                   if (skip == NULL)
8170                     goto error_ret;
8171                 }
8172
8173               skip[val >> 3] = ref_from_discarded;
8174             }
8175
8176           if (elf_section_data (sec)->relocs != relstart)
8177             free (relstart);
8178         }
8179
8180       /* For largetoc loads of address constants, we can convert
8181          .  addis rx,2,addr@got@ha
8182          .  ld ry,addr@got@l(rx)
8183          to
8184          .  addis rx,2,addr@toc@ha
8185          .  addi ry,rx,addr@toc@l
8186          when addr is within 2G of the toc pointer.  This then means
8187          that the word storing "addr" in the toc is no longer needed.  */
8188          
8189       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8190           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8191           && toc->reloc_count != 0)
8192         {
8193           /* Read toc relocs.  */
8194           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8195                                                   info->keep_memory);
8196           if (toc_relocs == NULL)
8197             goto error_ret;
8198
8199           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8200             {
8201               enum elf_ppc64_reloc_type r_type;
8202               unsigned long r_symndx;
8203               asection *sym_sec;
8204               struct elf_link_hash_entry *h;
8205               Elf_Internal_Sym *sym;
8206               bfd_vma val, addr;
8207
8208               r_type = ELF64_R_TYPE (rel->r_info);
8209               if (r_type != R_PPC64_ADDR64)
8210                 continue;
8211
8212               r_symndx = ELF64_R_SYM (rel->r_info);
8213               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8214                               r_symndx, ibfd))
8215                 goto error_ret;
8216
8217               if (sym_sec == NULL
8218                   || elf_discarded_section (sym_sec))
8219                 continue;
8220
8221               if (!SYMBOL_CALLS_LOCAL (info, h))
8222                 continue;
8223
8224               if (h != NULL)
8225                 {
8226                   if (h->type == STT_GNU_IFUNC)
8227                     continue;
8228                   val = h->root.u.def.value;
8229                 }
8230               else
8231                 {
8232                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8233                     continue;
8234                   val = sym->st_value;
8235                 }
8236               val += rel->r_addend;
8237               val += sym_sec->output_section->vma + sym_sec->output_offset;
8238
8239               /* We don't yet know the exact toc pointer value, but we
8240                  know it will be somewhere in the toc section.  Don't
8241                  optimize if the difference from any possible toc
8242                  pointer is outside [ff..f80008000, 7fff7fff].  */
8243               addr = toc->output_section->vma + TOC_BASE_OFF;
8244               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8245                 continue;
8246
8247               addr = toc->output_section->vma + toc->output_section->rawsize;
8248               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8249                 continue;
8250
8251               if (skip == NULL)
8252                 {
8253                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8254                   if (skip == NULL)
8255                     goto error_ret;
8256                 }
8257
8258               skip[rel->r_offset >> 3]
8259                 |= can_optimize | ((rel - toc_relocs) << 2);
8260             }
8261         }
8262
8263       if (skip == NULL)
8264         continue;
8265
8266       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8267       if (used == NULL)
8268         {
8269         error_ret:
8270           if (local_syms != NULL
8271               && symtab_hdr->contents != (unsigned char *) local_syms)
8272             free (local_syms);
8273           if (sec != NULL
8274               && relstart != NULL
8275               && elf_section_data (sec)->relocs != relstart)
8276             free (relstart);
8277           if (toc_relocs != NULL
8278               && elf_section_data (toc)->relocs != toc_relocs)
8279             free (toc_relocs);
8280           if (skip != NULL)
8281             free (skip);
8282           return FALSE;
8283         }
8284
8285       /* Now check all kept sections that might reference the toc.
8286          Check the toc itself last.  */
8287       for (sec = (ibfd->sections == toc && toc->next ? toc->next
8288                   : ibfd->sections);
8289            sec != NULL;
8290            sec = (sec == toc ? NULL
8291                   : sec->next == NULL ? toc
8292                   : sec->next == toc && toc->next ? toc->next
8293                   : sec->next))
8294         {
8295           int repeat;
8296
8297           if (sec->reloc_count == 0
8298               || elf_discarded_section (sec)
8299               || get_opd_info (sec)
8300               || (sec->flags & SEC_ALLOC) == 0
8301               || (sec->flags & SEC_DEBUGGING) != 0)
8302             continue;
8303
8304           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8305                                                 info->keep_memory);
8306           if (relstart == NULL)
8307             goto error_ret;
8308
8309           /* Mark toc entries referenced as used.  */
8310           repeat = 0;
8311           do
8312             for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8313               {
8314                 enum elf_ppc64_reloc_type r_type;
8315                 unsigned long r_symndx;
8316                 asection *sym_sec;
8317                 struct elf_link_hash_entry *h;
8318                 Elf_Internal_Sym *sym;
8319                 bfd_vma val;
8320                 enum {no_check, check_lo, check_ha} insn_check;
8321
8322                 r_type = ELF64_R_TYPE (rel->r_info);
8323                 switch (r_type)
8324                   {
8325                   default:
8326                     insn_check = no_check;
8327                     break;
8328
8329                   case R_PPC64_GOT_TLSLD16_HA:
8330                   case R_PPC64_GOT_TLSGD16_HA:
8331                   case R_PPC64_GOT_TPREL16_HA:
8332                   case R_PPC64_GOT_DTPREL16_HA:
8333                   case R_PPC64_GOT16_HA:
8334                   case R_PPC64_TOC16_HA:
8335                     insn_check = check_ha;
8336                     break;
8337
8338                   case R_PPC64_GOT_TLSLD16_LO:
8339                   case R_PPC64_GOT_TLSGD16_LO:
8340                   case R_PPC64_GOT_TPREL16_LO_DS:
8341                   case R_PPC64_GOT_DTPREL16_LO_DS:
8342                   case R_PPC64_GOT16_LO:
8343                   case R_PPC64_GOT16_LO_DS:
8344                   case R_PPC64_TOC16_LO:
8345                   case R_PPC64_TOC16_LO_DS:
8346                     insn_check = check_lo;
8347                     break;
8348                   }
8349
8350                 if (insn_check != no_check)
8351                   {
8352                     bfd_vma off = rel->r_offset & ~3;
8353                     unsigned char buf[4];
8354                     unsigned int insn;
8355
8356                     if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8357                       {
8358                         free (used);
8359                         goto error_ret;
8360                       }
8361                     insn = bfd_get_32 (ibfd, buf);
8362                     if (insn_check == check_lo
8363                         ? !ok_lo_toc_insn (insn)
8364                         : ((insn & ((0x3f << 26) | 0x1f << 16))
8365                            != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8366                       {
8367                         char str[12];
8368
8369                         ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8370                         sprintf (str, "%#08x", insn);
8371                         info->callbacks->einfo
8372                           (_("%P: %H: toc optimization is not supported for"
8373                              " %s instruction.\n"),
8374                            ibfd, sec, rel->r_offset & ~3, str);
8375                       }
8376                   }
8377
8378                 switch (r_type)
8379                   {
8380                   case R_PPC64_TOC16:
8381                   case R_PPC64_TOC16_LO:
8382                   case R_PPC64_TOC16_HI:
8383                   case R_PPC64_TOC16_HA:
8384                   case R_PPC64_TOC16_DS:
8385                   case R_PPC64_TOC16_LO_DS:
8386                     /* In case we're taking addresses of toc entries.  */
8387                   case R_PPC64_ADDR64:
8388                     break;
8389
8390                   default:
8391                     continue;
8392                   }
8393
8394                 r_symndx = ELF64_R_SYM (rel->r_info);
8395                 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8396                                 r_symndx, ibfd))
8397                   {
8398                     free (used);
8399                     goto error_ret;
8400                   }
8401
8402                 if (sym_sec != toc)
8403                   continue;
8404
8405                 if (h != NULL)
8406                   val = h->root.u.def.value;
8407                 else
8408                   val = sym->st_value;
8409                 val += rel->r_addend;
8410
8411                 if (val >= toc->size)
8412                   continue;
8413
8414                 if ((skip[val >> 3] & can_optimize) != 0)
8415                   {
8416                     bfd_vma off;
8417                     unsigned char opc;
8418
8419                     switch (r_type)
8420                       {
8421                       case R_PPC64_TOC16_HA:
8422                         break;
8423
8424                       case R_PPC64_TOC16_LO_DS:
8425                         off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8426                         if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
8427                           {
8428                             free (used);
8429                             goto error_ret;
8430                           }
8431                         if ((opc & (0x3f << 2)) == (58u << 2))
8432                           break;
8433                         /* Fall thru */
8434
8435                       default:
8436                         /* Wrong sort of reloc, or not a ld.  We may
8437                            as well clear ref_from_discarded too.  */
8438                         skip[val >> 3] = 0;
8439                       }
8440                   }
8441
8442                 /* For the toc section, we only mark as used if
8443                    this entry itself isn't unused.  */
8444                 if (sec == toc
8445                     && !used[val >> 3]
8446                     && (used[rel->r_offset >> 3]
8447                         || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
8448                   /* Do all the relocs again, to catch reference
8449                      chains.  */
8450                   repeat = 1;
8451
8452                 used[val >> 3] = 1;
8453               }
8454           while (repeat);
8455
8456           if (elf_section_data (sec)->relocs != relstart)
8457             free (relstart);
8458         }
8459
8460       /* Merge the used and skip arrays.  Assume that TOC
8461          doublewords not appearing as either used or unused belong
8462          to to an entry more than one doubleword in size.  */
8463       for (drop = skip, keep = used, last = 0, some_unused = 0;
8464            drop < skip + (toc->size + 7) / 8;
8465            ++drop, ++keep)
8466         {
8467           if (*keep)
8468             {
8469               *drop &= ~ref_from_discarded;
8470               if ((*drop & can_optimize) != 0)
8471                 some_unused = 1;
8472               last = 0;
8473             }
8474           else if ((*drop & ref_from_discarded) != 0)
8475             {
8476               some_unused = 1;
8477               last = ref_from_discarded;
8478             }
8479           else
8480             *drop = last;
8481         }
8482
8483       free (used);
8484
8485       if (some_unused)
8486         {
8487           bfd_byte *contents, *src;
8488           unsigned long off;
8489           Elf_Internal_Sym *sym;
8490           bfd_boolean local_toc_syms = FALSE;
8491
8492           /* Shuffle the toc contents, and at the same time convert the
8493              skip array from booleans into offsets.  */
8494           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8495             goto error_ret;
8496
8497           elf_section_data (toc)->this_hdr.contents = contents;
8498
8499           for (src = contents, off = 0, drop = skip;
8500                src < contents + toc->size;
8501                src += 8, ++drop)
8502             {
8503               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8504                 off += 8;
8505               else if (off != 0)
8506                 {
8507                   *drop = off;
8508                   memcpy (src - off, src, 8);
8509                 }
8510             }
8511           *drop = off;
8512           toc->rawsize = toc->size;
8513           toc->size = src - contents - off;
8514
8515           /* Adjust addends for relocs against the toc section sym,
8516              and optimize any accesses we can.  */
8517           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8518             {
8519               if (sec->reloc_count == 0
8520                   || elf_discarded_section (sec))
8521                 continue;
8522
8523               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8524                                                     info->keep_memory);
8525               if (relstart == NULL)
8526                 goto error_ret;
8527
8528               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8529                 {
8530                   enum elf_ppc64_reloc_type r_type;
8531                   unsigned long r_symndx;
8532                   asection *sym_sec;
8533                   struct elf_link_hash_entry *h;
8534                   bfd_vma val;
8535
8536                   r_type = ELF64_R_TYPE (rel->r_info);
8537                   switch (r_type)
8538                     {
8539                     default:
8540                       continue;
8541
8542                     case R_PPC64_TOC16:
8543                     case R_PPC64_TOC16_LO:
8544                     case R_PPC64_TOC16_HI:
8545                     case R_PPC64_TOC16_HA:
8546                     case R_PPC64_TOC16_DS:
8547                     case R_PPC64_TOC16_LO_DS:
8548                     case R_PPC64_ADDR64:
8549                       break;
8550                     }
8551
8552                   r_symndx = ELF64_R_SYM (rel->r_info);
8553                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8554                                   r_symndx, ibfd))
8555                     goto error_ret;
8556
8557                   if (sym_sec != toc)
8558                     continue;
8559
8560                   if (h != NULL)
8561                     val = h->root.u.def.value;
8562                   else
8563                     {
8564                       val = sym->st_value;
8565                       if (val != 0)
8566                         local_toc_syms = TRUE;
8567                     }
8568
8569                   val += rel->r_addend;
8570
8571                   if (val > toc->rawsize)
8572                     val = toc->rawsize;
8573                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
8574                     continue;
8575                   else if ((skip[val >> 3] & can_optimize) != 0)
8576                     {
8577                       Elf_Internal_Rela *tocrel
8578                         = toc_relocs + (skip[val >> 3] >> 2);
8579                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8580
8581                       switch (r_type)
8582                         {
8583                         case R_PPC64_TOC16_HA:
8584                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8585                           break;
8586
8587                         case R_PPC64_TOC16_LO_DS:
8588                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8589                           break;
8590
8591                         default:
8592                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8593                             ppc_howto_init ();
8594                           info->callbacks->einfo
8595                             (_("%P: %H: %s relocation references "
8596                                "optimized away TOC entry\n"),
8597                              ibfd, sec, rel->r_offset,
8598                              ppc64_elf_howto_table[r_type]->name);
8599                           bfd_set_error (bfd_error_bad_value);
8600                           goto error_ret;
8601                         }
8602                       rel->r_addend = tocrel->r_addend;
8603                       elf_section_data (sec)->relocs = relstart;
8604                       continue;
8605                     }
8606
8607                   if (h != NULL || sym->st_value != 0)
8608                     continue;
8609
8610                   rel->r_addend -= skip[val >> 3];
8611                   elf_section_data (sec)->relocs = relstart;
8612                 }
8613
8614               if (elf_section_data (sec)->relocs != relstart)
8615                 free (relstart);
8616             }
8617
8618           /* We shouldn't have local or global symbols defined in the TOC,
8619              but handle them anyway.  */
8620           if (local_syms != NULL)
8621             for (sym = local_syms;
8622                  sym < local_syms + symtab_hdr->sh_info;
8623                  ++sym)
8624               if (sym->st_value != 0
8625                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8626                 {
8627                   unsigned long i;
8628
8629                   if (sym->st_value > toc->rawsize)
8630                     i = toc->rawsize >> 3;
8631                   else
8632                     i = sym->st_value >> 3;
8633
8634                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8635                     {
8636                       if (local_toc_syms)
8637                         (*_bfd_error_handler)
8638                           (_("%s defined on removed toc entry"),
8639                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8640                       do
8641                         ++i;
8642                       while ((skip[i] & (ref_from_discarded | can_optimize)));
8643                       sym->st_value = (bfd_vma) i << 3;
8644                     }
8645
8646                   sym->st_value -= skip[i];
8647                   symtab_hdr->contents = (unsigned char *) local_syms;
8648                 }
8649
8650           /* Adjust any global syms defined in this toc input section.  */
8651           if (toc_inf.global_toc_syms)
8652             {
8653               toc_inf.toc = toc;
8654               toc_inf.skip = skip;
8655               toc_inf.global_toc_syms = FALSE;
8656               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8657                                       &toc_inf);
8658             }
8659
8660           if (toc->reloc_count != 0)
8661             {
8662               Elf_Internal_Shdr *rel_hdr;
8663               Elf_Internal_Rela *wrel;
8664               bfd_size_type sz;
8665
8666               /* Remove unused toc relocs, and adjust those we keep.  */
8667               if (toc_relocs == NULL)
8668                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8669                                                         info->keep_memory);
8670               if (toc_relocs == NULL)
8671                 goto error_ret;
8672
8673               wrel = toc_relocs;
8674               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8675                 if ((skip[rel->r_offset >> 3]
8676                      & (ref_from_discarded | can_optimize)) == 0)
8677                   {
8678                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8679                     wrel->r_info = rel->r_info;
8680                     wrel->r_addend = rel->r_addend;
8681                     ++wrel;
8682                   }
8683                 else if (!dec_dynrel_count (rel->r_info, toc, info,
8684                                             &local_syms, NULL, NULL))
8685                   goto error_ret;
8686
8687               elf_section_data (toc)->relocs = toc_relocs;
8688               toc->reloc_count = wrel - toc_relocs;
8689               rel_hdr = _bfd_elf_single_rel_hdr (toc);
8690               sz = rel_hdr->sh_entsize;
8691               rel_hdr->sh_size = toc->reloc_count * sz;
8692             }
8693         }
8694       else if (toc_relocs != NULL
8695                && elf_section_data (toc)->relocs != toc_relocs)
8696         free (toc_relocs);
8697
8698       if (local_syms != NULL
8699           && symtab_hdr->contents != (unsigned char *) local_syms)
8700         {
8701           if (!info->keep_memory)
8702             free (local_syms);
8703           else
8704             symtab_hdr->contents = (unsigned char *) local_syms;
8705         }
8706       free (skip);
8707     }
8708
8709   return TRUE;
8710 }
8711
8712 /* Return true iff input section I references the TOC using
8713    instructions limited to +/-32k offsets.  */
8714
8715 bfd_boolean
8716 ppc64_elf_has_small_toc_reloc (asection *i)
8717 {
8718   return (is_ppc64_elf (i->owner)
8719           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8720 }
8721
8722 /* Allocate space for one GOT entry.  */
8723
8724 static void
8725 allocate_got (struct elf_link_hash_entry *h,
8726               struct bfd_link_info *info,
8727               struct got_entry *gent)
8728 {
8729   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8730   bfd_boolean dyn;
8731   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8732   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8733                  ? 16 : 8);
8734   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8735                   ? 2 : 1) * sizeof (Elf64_External_Rela);
8736   asection *got = ppc64_elf_tdata (gent->owner)->got;
8737
8738   gent->got.offset = got->size;
8739   got->size += entsize;
8740
8741   dyn = htab->elf.dynamic_sections_created;
8742   if ((info->shared
8743        || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8744             && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8745                 || h->root.type != bfd_link_hash_undefweak))
8746     {
8747       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8748       relgot->size += rentsize;
8749     }
8750   else if (h->type == STT_GNU_IFUNC)
8751     {
8752       asection *relgot = htab->reliplt;
8753       relgot->size += rentsize;
8754       htab->got_reli_size += rentsize;
8755     }
8756 }
8757
8758 /* This function merges got entries in the same toc group.  */
8759
8760 static void
8761 merge_got_entries (struct got_entry **pent)
8762 {
8763   struct got_entry *ent, *ent2;
8764
8765   for (ent = *pent; ent != NULL; ent = ent->next)
8766     if (!ent->is_indirect)
8767       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8768         if (!ent2->is_indirect
8769             && ent2->addend == ent->addend
8770             && ent2->tls_type == ent->tls_type
8771             && elf_gp (ent2->owner) == elf_gp (ent->owner))
8772           {
8773             ent2->is_indirect = TRUE;
8774             ent2->got.ent = ent;
8775           }
8776 }
8777
8778 /* Allocate space in .plt, .got and associated reloc sections for
8779    dynamic relocs.  */
8780
8781 static bfd_boolean
8782 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8783 {
8784   struct bfd_link_info *info;
8785   struct ppc_link_hash_table *htab;
8786   asection *s;
8787   struct ppc_link_hash_entry *eh;
8788   struct elf_dyn_relocs *p;
8789   struct got_entry **pgent, *gent;
8790
8791   if (h->root.type == bfd_link_hash_indirect)
8792     return TRUE;
8793
8794   info = (struct bfd_link_info *) inf;
8795   htab = ppc_hash_table (info);
8796   if (htab == NULL)
8797     return FALSE;
8798
8799   if ((htab->elf.dynamic_sections_created
8800        && h->dynindx != -1
8801        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8802       || h->type == STT_GNU_IFUNC)
8803     {
8804       struct plt_entry *pent;
8805       bfd_boolean doneone = FALSE;
8806       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8807         if (pent->plt.refcount > 0)
8808           {
8809             if (!htab->elf.dynamic_sections_created
8810                 || h->dynindx == -1)
8811               {
8812                 s = htab->iplt;
8813                 pent->plt.offset = s->size;
8814                 s->size += PLT_ENTRY_SIZE;
8815                 s = htab->reliplt;
8816               }
8817             else
8818               {
8819                 /* If this is the first .plt entry, make room for the special
8820                    first entry.  */
8821                 s = htab->plt;
8822                 if (s->size == 0)
8823                   s->size += PLT_INITIAL_ENTRY_SIZE;
8824
8825                 pent->plt.offset = s->size;
8826
8827                 /* Make room for this entry.  */
8828                 s->size += PLT_ENTRY_SIZE;
8829
8830                 /* Make room for the .glink code.  */
8831                 s = htab->glink;
8832                 if (s->size == 0)
8833                   s->size += GLINK_CALL_STUB_SIZE;
8834                 /* We need bigger stubs past index 32767.  */
8835                 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8836                   s->size += 4;
8837                 s->size += 2*4;
8838
8839                 /* We also need to make an entry in the .rela.plt section.  */
8840                 s = htab->relplt;
8841               }
8842             s->size += sizeof (Elf64_External_Rela);
8843             doneone = TRUE;
8844           }
8845         else
8846           pent->plt.offset = (bfd_vma) -1;
8847       if (!doneone)
8848         {
8849           h->plt.plist = NULL;
8850           h->needs_plt = 0;
8851         }
8852     }
8853   else
8854     {
8855       h->plt.plist = NULL;
8856       h->needs_plt = 0;
8857     }
8858
8859   eh = (struct ppc_link_hash_entry *) h;
8860   /* Run through the TLS GD got entries first if we're changing them
8861      to TPREL.  */
8862   if ((eh->tls_mask & TLS_TPRELGD) != 0)
8863     for (gent = h->got.glist; gent != NULL; gent = gent->next)
8864       if (gent->got.refcount > 0
8865           && (gent->tls_type & TLS_GD) != 0)
8866         {
8867           /* This was a GD entry that has been converted to TPREL.  If
8868              there happens to be a TPREL entry we can use that one.  */
8869           struct got_entry *ent;
8870           for (ent = h->got.glist; ent != NULL; ent = ent->next)
8871             if (ent->got.refcount > 0
8872                 && (ent->tls_type & TLS_TPREL) != 0
8873                 && ent->addend == gent->addend
8874                 && ent->owner == gent->owner)
8875               {
8876                 gent->got.refcount = 0;
8877                 break;
8878               }
8879
8880           /* If not, then we'll be using our own TPREL entry.  */
8881           if (gent->got.refcount != 0)
8882             gent->tls_type = TLS_TLS | TLS_TPREL;
8883         }
8884
8885   /* Remove any list entry that won't generate a word in the GOT before
8886      we call merge_got_entries.  Otherwise we risk merging to empty
8887      entries.  */
8888   pgent = &h->got.glist;
8889   while ((gent = *pgent) != NULL)
8890     if (gent->got.refcount > 0)
8891       {
8892         if ((gent->tls_type & TLS_LD) != 0
8893             && !h->def_dynamic)
8894           {
8895             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8896             *pgent = gent->next;
8897           }
8898         else
8899           pgent = &gent->next;
8900       }
8901     else
8902       *pgent = gent->next;
8903
8904   if (!htab->do_multi_toc)
8905     merge_got_entries (&h->got.glist);
8906
8907   for (gent = h->got.glist; gent != NULL; gent = gent->next)
8908     if (!gent->is_indirect)
8909       {
8910         /* Make sure this symbol is output as a dynamic symbol.
8911            Undefined weak syms won't yet be marked as dynamic,
8912            nor will all TLS symbols.  */
8913         if (h->dynindx == -1
8914             && !h->forced_local
8915             && h->type != STT_GNU_IFUNC
8916             && htab->elf.dynamic_sections_created)
8917           {
8918             if (! bfd_elf_link_record_dynamic_symbol (info, h))
8919               return FALSE;
8920           }
8921
8922         if (!is_ppc64_elf (gent->owner))
8923           abort ();
8924
8925         allocate_got (h, info, gent);
8926       }
8927
8928   if (eh->dyn_relocs == NULL
8929       || (!htab->elf.dynamic_sections_created
8930           && h->type != STT_GNU_IFUNC))
8931     return TRUE;
8932
8933   /* In the shared -Bsymbolic case, discard space allocated for
8934      dynamic pc-relative relocs against symbols which turn out to be
8935      defined in regular objects.  For the normal shared case, discard
8936      space for relocs that have become local due to symbol visibility
8937      changes.  */
8938
8939   if (info->shared)
8940     {
8941       /* Relocs that use pc_count are those that appear on a call insn,
8942          or certain REL relocs (see must_be_dyn_reloc) that can be
8943          generated via assembly.  We want calls to protected symbols to
8944          resolve directly to the function rather than going via the plt.
8945          If people want function pointer comparisons to work as expected
8946          then they should avoid writing weird assembly.  */
8947       if (SYMBOL_CALLS_LOCAL (info, h))
8948         {
8949           struct elf_dyn_relocs **pp;
8950
8951           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8952             {
8953               p->count -= p->pc_count;
8954               p->pc_count = 0;
8955               if (p->count == 0)
8956                 *pp = p->next;
8957               else
8958                 pp = &p->next;
8959             }
8960         }
8961
8962       /* Also discard relocs on undefined weak syms with non-default
8963          visibility.  */
8964       if (eh->dyn_relocs != NULL
8965           && h->root.type == bfd_link_hash_undefweak)
8966         {
8967           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8968             eh->dyn_relocs = NULL;
8969
8970           /* Make sure this symbol is output as a dynamic symbol.
8971              Undefined weak syms won't yet be marked as dynamic.  */
8972           else if (h->dynindx == -1
8973                    && !h->forced_local)
8974             {
8975               if (! bfd_elf_link_record_dynamic_symbol (info, h))
8976                 return FALSE;
8977             }
8978         }
8979     }
8980   else if (h->type == STT_GNU_IFUNC)
8981     {
8982       if (!h->non_got_ref)
8983         eh->dyn_relocs = NULL;
8984     }
8985   else if (ELIMINATE_COPY_RELOCS)
8986     {
8987       /* For the non-shared case, discard space for relocs against
8988          symbols which turn out to need copy relocs or are not
8989          dynamic.  */
8990
8991       if (!h->non_got_ref
8992           && !h->def_regular)
8993         {
8994           /* Make sure this symbol is output as a dynamic symbol.
8995              Undefined weak syms won't yet be marked as dynamic.  */
8996           if (h->dynindx == -1
8997               && !h->forced_local)
8998             {
8999               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9000                 return FALSE;
9001             }
9002
9003           /* If that succeeded, we know we'll be keeping all the
9004              relocs.  */
9005           if (h->dynindx != -1)
9006             goto keep;
9007         }
9008
9009       eh->dyn_relocs = NULL;
9010
9011     keep: ;
9012     }
9013
9014   /* Finally, allocate space.  */
9015   for (p = eh->dyn_relocs; p != NULL; p = p->next)
9016     {
9017       asection *sreloc = elf_section_data (p->sec)->sreloc;
9018       if (!htab->elf.dynamic_sections_created)
9019         sreloc = htab->reliplt;
9020       sreloc->size += p->count * sizeof (Elf64_External_Rela);
9021     }
9022
9023   return TRUE;
9024 }
9025
9026 /* Find any dynamic relocs that apply to read-only sections.  */
9027
9028 static bfd_boolean
9029 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9030 {
9031   struct ppc_link_hash_entry *eh;
9032   struct elf_dyn_relocs *p;
9033
9034   eh = (struct ppc_link_hash_entry *) h;
9035   for (p = eh->dyn_relocs; p != NULL; p = p->next)
9036     {
9037       asection *s = p->sec->output_section;
9038
9039       if (s != NULL && (s->flags & SEC_READONLY) != 0)
9040         {
9041           struct bfd_link_info *info = inf;
9042
9043           info->flags |= DF_TEXTREL;
9044
9045           /* Not an error, just cut short the traversal.  */
9046           return FALSE;
9047         }
9048     }
9049   return TRUE;
9050 }
9051
9052 /* Set the sizes of the dynamic sections.  */
9053
9054 static bfd_boolean
9055 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
9056                                  struct bfd_link_info *info)
9057 {
9058   struct ppc_link_hash_table *htab;
9059   bfd *dynobj;
9060   asection *s;
9061   bfd_boolean relocs;
9062   bfd *ibfd;
9063   struct got_entry *first_tlsld;
9064
9065   htab = ppc_hash_table (info);
9066   if (htab == NULL)
9067     return FALSE;
9068
9069   dynobj = htab->elf.dynobj;
9070   if (dynobj == NULL)
9071     abort ();
9072
9073   if (htab->elf.dynamic_sections_created)
9074     {
9075       /* Set the contents of the .interp section to the interpreter.  */
9076       if (info->executable)
9077         {
9078           s = bfd_get_section_by_name (dynobj, ".interp");
9079           if (s == NULL)
9080             abort ();
9081           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9082           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9083         }
9084     }
9085
9086   /* Set up .got offsets for local syms, and space for local dynamic
9087      relocs.  */
9088   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9089     {
9090       struct got_entry **lgot_ents;
9091       struct got_entry **end_lgot_ents;
9092       struct plt_entry **local_plt;
9093       struct plt_entry **end_local_plt;
9094       unsigned char *lgot_masks;
9095       bfd_size_type locsymcount;
9096       Elf_Internal_Shdr *symtab_hdr;
9097       asection *srel;
9098
9099       if (!is_ppc64_elf (ibfd))
9100         continue;
9101
9102       for (s = ibfd->sections; s != NULL; s = s->next)
9103         {
9104           struct elf_dyn_relocs *p;
9105
9106           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9107             {
9108               if (!bfd_is_abs_section (p->sec)
9109                   && bfd_is_abs_section (p->sec->output_section))
9110                 {
9111                   /* Input section has been discarded, either because
9112                      it is a copy of a linkonce section or due to
9113                      linker script /DISCARD/, so we'll be discarding
9114                      the relocs too.  */
9115                 }
9116               else if (p->count != 0)
9117                 {
9118                   srel = elf_section_data (p->sec)->sreloc;
9119                   if (!htab->elf.dynamic_sections_created)
9120                     srel = htab->reliplt;
9121                   srel->size += p->count * sizeof (Elf64_External_Rela);
9122                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9123                     info->flags |= DF_TEXTREL;
9124                 }
9125             }
9126         }
9127
9128       lgot_ents = elf_local_got_ents (ibfd);
9129       if (!lgot_ents)
9130         continue;
9131
9132       symtab_hdr = &elf_symtab_hdr (ibfd);
9133       locsymcount = symtab_hdr->sh_info;
9134       end_lgot_ents = lgot_ents + locsymcount;
9135       local_plt = (struct plt_entry **) end_lgot_ents;
9136       end_local_plt = local_plt + locsymcount;
9137       lgot_masks = (unsigned char *) end_local_plt;
9138       s = ppc64_elf_tdata (ibfd)->got;
9139       srel = ppc64_elf_tdata (ibfd)->relgot;
9140       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9141         {
9142           struct got_entry **pent, *ent;
9143
9144           pent = lgot_ents;
9145           while ((ent = *pent) != NULL)
9146             if (ent->got.refcount > 0)
9147               {
9148                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9149                   {
9150                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
9151                     *pent = ent->next;
9152                   }
9153                 else
9154                   {
9155                     unsigned int num = 1;
9156                     ent->got.offset = s->size;
9157                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9158                       num = 2;
9159                     s->size += num * 8;
9160                     if (info->shared)
9161                       srel->size += num * sizeof (Elf64_External_Rela);
9162                     else if ((*lgot_masks & PLT_IFUNC) != 0)
9163                       {
9164                         htab->reliplt->size
9165                           += num * sizeof (Elf64_External_Rela);
9166                         htab->got_reli_size
9167                           += num * sizeof (Elf64_External_Rela);
9168                       }
9169                     pent = &ent->next;
9170                   }
9171               }
9172             else
9173               *pent = ent->next;
9174         }
9175
9176       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
9177       for (; local_plt < end_local_plt; ++local_plt)
9178         {
9179           struct plt_entry *ent;
9180
9181           for (ent = *local_plt; ent != NULL; ent = ent->next)
9182             if (ent->plt.refcount > 0)
9183               {
9184                 s = htab->iplt;
9185                 ent->plt.offset = s->size;
9186                 s->size += PLT_ENTRY_SIZE;
9187
9188                 htab->reliplt->size += sizeof (Elf64_External_Rela);
9189               }
9190             else
9191               ent->plt.offset = (bfd_vma) -1;
9192         }
9193     }
9194
9195   /* Allocate global sym .plt and .got entries, and space for global
9196      sym dynamic relocs.  */
9197   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9198
9199   first_tlsld = NULL;
9200   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9201     {
9202       struct got_entry *ent;
9203
9204       if (!is_ppc64_elf (ibfd))
9205         continue;
9206
9207       ent = ppc64_tlsld_got (ibfd);
9208       if (ent->got.refcount > 0)
9209         {
9210           if (!htab->do_multi_toc && first_tlsld != NULL)
9211             {
9212               ent->is_indirect = TRUE;
9213               ent->got.ent = first_tlsld;
9214             }
9215           else
9216             {
9217               if (first_tlsld == NULL)
9218                 first_tlsld = ent;
9219               s = ppc64_elf_tdata (ibfd)->got;
9220               ent->got.offset = s->size;
9221               ent->owner = ibfd;
9222               s->size += 16;
9223               if (info->shared)
9224                 {
9225                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9226                   srel->size += sizeof (Elf64_External_Rela);
9227                 }
9228             }
9229         }
9230       else
9231         ent->got.offset = (bfd_vma) -1;
9232     }
9233
9234   /* We now have determined the sizes of the various dynamic sections.
9235      Allocate memory for them.  */
9236   relocs = FALSE;
9237   for (s = dynobj->sections; s != NULL; s = s->next)
9238     {
9239       if ((s->flags & SEC_LINKER_CREATED) == 0)
9240         continue;
9241
9242       if (s == htab->brlt || s == htab->relbrlt)
9243         /* These haven't been allocated yet;  don't strip.  */
9244         continue;
9245       else if (s == htab->got
9246                || s == htab->plt
9247                || s == htab->iplt
9248                || s == htab->glink
9249                || s == htab->dynbss)
9250         {
9251           /* Strip this section if we don't need it; see the
9252              comment below.  */
9253         }
9254       else if (s == htab->glink_eh_frame)
9255         {
9256           if (!bfd_is_abs_section (s->output_section))
9257             /* Not sized yet.  */
9258             continue;
9259         }
9260       else if (CONST_STRNEQ (s->name, ".rela"))
9261         {
9262           if (s->size != 0)
9263             {
9264               if (s != htab->relplt)
9265                 relocs = TRUE;
9266
9267               /* We use the reloc_count field as a counter if we need
9268                  to copy relocs into the output file.  */
9269               s->reloc_count = 0;
9270             }
9271         }
9272       else
9273         {
9274           /* It's not one of our sections, so don't allocate space.  */
9275           continue;
9276         }
9277
9278       if (s->size == 0)
9279         {
9280           /* If we don't need this section, strip it from the
9281              output file.  This is mostly to handle .rela.bss and
9282              .rela.plt.  We must create both sections in
9283              create_dynamic_sections, because they must be created
9284              before the linker maps input sections to output
9285              sections.  The linker does that before
9286              adjust_dynamic_symbol is called, and it is that
9287              function which decides whether anything needs to go
9288              into these sections.  */
9289           s->flags |= SEC_EXCLUDE;
9290           continue;
9291         }
9292
9293       if ((s->flags & SEC_HAS_CONTENTS) == 0)
9294         continue;
9295
9296       /* Allocate memory for the section contents.  We use bfd_zalloc
9297          here in case unused entries are not reclaimed before the
9298          section's contents are written out.  This should not happen,
9299          but this way if it does we get a R_PPC64_NONE reloc in .rela
9300          sections instead of garbage.
9301          We also rely on the section contents being zero when writing
9302          the GOT.  */
9303       s->contents = bfd_zalloc (dynobj, s->size);
9304       if (s->contents == NULL)
9305         return FALSE;
9306     }
9307
9308   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9309     {
9310       if (!is_ppc64_elf (ibfd))
9311         continue;
9312
9313       s = ppc64_elf_tdata (ibfd)->got;
9314       if (s != NULL && s != htab->got)
9315         {
9316           if (s->size == 0)
9317             s->flags |= SEC_EXCLUDE;
9318           else
9319             {
9320               s->contents = bfd_zalloc (ibfd, s->size);
9321               if (s->contents == NULL)
9322                 return FALSE;
9323             }
9324         }
9325       s = ppc64_elf_tdata (ibfd)->relgot;
9326       if (s != NULL)
9327         {
9328           if (s->size == 0)
9329             s->flags |= SEC_EXCLUDE;
9330           else
9331             {
9332               s->contents = bfd_zalloc (ibfd, s->size);
9333               if (s->contents == NULL)
9334                 return FALSE;
9335               relocs = TRUE;
9336               s->reloc_count = 0;
9337             }
9338         }
9339     }
9340
9341   if (htab->elf.dynamic_sections_created)
9342     {
9343       /* Add some entries to the .dynamic section.  We fill in the
9344          values later, in ppc64_elf_finish_dynamic_sections, but we
9345          must add the entries now so that we get the correct size for
9346          the .dynamic section.  The DT_DEBUG entry is filled in by the
9347          dynamic linker and used by the debugger.  */
9348 #define add_dynamic_entry(TAG, VAL) \
9349   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9350
9351       if (info->executable)
9352         {
9353           if (!add_dynamic_entry (DT_DEBUG, 0))
9354             return FALSE;
9355         }
9356
9357       if (htab->plt != NULL && htab->plt->size != 0)
9358         {
9359           if (!add_dynamic_entry (DT_PLTGOT, 0)
9360               || !add_dynamic_entry (DT_PLTRELSZ, 0)
9361               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9362               || !add_dynamic_entry (DT_JMPREL, 0)
9363               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9364             return FALSE;
9365         }
9366
9367       if (NO_OPD_RELOCS)
9368         {
9369           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9370               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9371             return FALSE;
9372         }
9373
9374       if (!htab->no_tls_get_addr_opt
9375           && htab->tls_get_addr_fd != NULL
9376           && htab->tls_get_addr_fd->elf.plt.plist != NULL
9377           && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9378         return FALSE;
9379
9380       if (relocs)
9381         {
9382           if (!add_dynamic_entry (DT_RELA, 0)
9383               || !add_dynamic_entry (DT_RELASZ, 0)
9384               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9385             return FALSE;
9386
9387           /* If any dynamic relocs apply to a read-only section,
9388              then we need a DT_TEXTREL entry.  */
9389           if ((info->flags & DF_TEXTREL) == 0)
9390             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
9391
9392           if ((info->flags & DF_TEXTREL) != 0)
9393             {
9394               if (!add_dynamic_entry (DT_TEXTREL, 0))
9395                 return FALSE;
9396             }
9397         }
9398     }
9399 #undef add_dynamic_entry
9400
9401   return TRUE;
9402 }
9403
9404 /* Determine the type of stub needed, if any, for a call.  */
9405
9406 static inline enum ppc_stub_type
9407 ppc_type_of_stub (asection *input_sec,
9408                   const Elf_Internal_Rela *rel,
9409                   struct ppc_link_hash_entry **hash,
9410                   struct plt_entry **plt_ent,
9411                   bfd_vma destination)
9412 {
9413   struct ppc_link_hash_entry *h = *hash;
9414   bfd_vma location;
9415   bfd_vma branch_offset;
9416   bfd_vma max_branch_offset;
9417   enum elf_ppc64_reloc_type r_type;
9418
9419   if (h != NULL)
9420     {
9421       struct plt_entry *ent;
9422       struct ppc_link_hash_entry *fdh = h;
9423       if (h->oh != NULL
9424           && h->oh->is_func_descriptor)
9425         {
9426           fdh = ppc_follow_link (h->oh);
9427           *hash = fdh;
9428         }
9429
9430       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9431         if (ent->addend == rel->r_addend
9432             && ent->plt.offset != (bfd_vma) -1)
9433           {
9434             *plt_ent = ent;
9435             return ppc_stub_plt_call;
9436           }
9437
9438       /* Here, we know we don't have a plt entry.  If we don't have a
9439          either a defined function descriptor or a defined entry symbol
9440          in a regular object file, then it is pointless trying to make
9441          any other type of stub.  */
9442       if (!is_static_defined (&fdh->elf)
9443           && !is_static_defined (&h->elf))
9444         return ppc_stub_none;
9445     }
9446   else if (elf_local_got_ents (input_sec->owner) != NULL)
9447     {
9448       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9449       struct plt_entry **local_plt = (struct plt_entry **)
9450         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9451       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9452
9453       if (local_plt[r_symndx] != NULL)
9454         {
9455           struct plt_entry *ent;
9456
9457           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9458             if (ent->addend == rel->r_addend
9459                 && ent->plt.offset != (bfd_vma) -1)
9460               {
9461                 *plt_ent = ent;
9462                 return ppc_stub_plt_call;
9463               }
9464         }
9465     }
9466
9467   /* Determine where the call point is.  */
9468   location = (input_sec->output_offset
9469               + input_sec->output_section->vma
9470               + rel->r_offset);
9471
9472   branch_offset = destination - location;
9473   r_type = ELF64_R_TYPE (rel->r_info);
9474
9475   /* Determine if a long branch stub is needed.  */
9476   max_branch_offset = 1 << 25;
9477   if (r_type != R_PPC64_REL24)
9478     max_branch_offset = 1 << 15;
9479
9480   if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9481     /* We need a stub.  Figure out whether a long_branch or plt_branch
9482        is needed later.  */
9483     return ppc_stub_long_branch;
9484
9485   return ppc_stub_none;
9486 }
9487
9488 /* With power7 weakly ordered memory model, it is possible for ld.so
9489    to update a plt entry in one thread and have another thread see a
9490    stale zero toc entry.  To avoid this we need some sort of acquire
9491    barrier in the call stub.  One solution is to make the load of the
9492    toc word seem to appear to depend on the load of the function entry
9493    word.  Another solution is to test for r2 being zero, and branch to
9494    the appropriate glink entry if so.
9495
9496    .    fake dep barrier        compare
9497    .    ld 11,xxx(2)            ld 11,xxx(2)
9498    .    mtctr 11                mtctr 11
9499    .    xor 11,11,11            ld 2,xxx+8(2)
9500    .    add 2,2,11              cmpldi 2,0
9501    .    ld 2,xxx+8(2)           bnectr+
9502    .    bctr                    b <glink_entry>
9503
9504    The solution involving the compare turns out to be faster, so
9505    that's what we use unless the branch won't reach.  */
9506
9507 #define ALWAYS_USE_FAKE_DEP 0
9508 #define ALWAYS_EMIT_R2SAVE 0
9509
9510 #define PPC_LO(v) ((v) & 0xffff)
9511 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9512 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9513
9514 static inline unsigned int
9515 plt_stub_size (struct ppc_link_hash_table *htab,
9516                struct ppc_stub_hash_entry *stub_entry,
9517                bfd_vma off)
9518 {
9519   unsigned size = PLT_CALL_STUB_SIZE;
9520
9521   if (!(ALWAYS_EMIT_R2SAVE
9522         || stub_entry->stub_type == ppc_stub_plt_call_r2save))
9523     size -= 4;
9524   if (!htab->plt_static_chain)
9525     size -= 4;
9526   if (htab->plt_thread_safe)
9527     size += 8;
9528   if (PPC_HA (off) == 0)
9529     size -= 4;
9530   if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
9531     size += 4;
9532   if (stub_entry->h != NULL
9533       && (stub_entry->h == htab->tls_get_addr_fd
9534           || stub_entry->h == htab->tls_get_addr)
9535       && !htab->no_tls_get_addr_opt)
9536     size += 13 * 4;
9537   return size;
9538 }
9539
9540 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
9541    then return the padding needed to do so.  */
9542 static inline unsigned int
9543 plt_stub_pad (struct ppc_link_hash_table *htab,
9544               struct ppc_stub_hash_entry *stub_entry,
9545               bfd_vma plt_off)
9546 {
9547   int stub_align = 1 << htab->plt_stub_align;
9548   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
9549   bfd_vma stub_off = stub_entry->stub_sec->size;
9550
9551   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
9552       > (stub_size & -stub_align))
9553     return stub_align - (stub_off & (stub_align - 1));
9554   return 0;
9555 }
9556
9557 /* Build a .plt call stub.  */
9558
9559 static inline bfd_byte *
9560 build_plt_stub (struct ppc_link_hash_table *htab,
9561                 struct ppc_stub_hash_entry *stub_entry,
9562                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9563 {
9564   bfd *obfd = htab->stub_bfd;
9565   bfd_boolean plt_static_chain = htab->plt_static_chain;
9566   bfd_boolean plt_thread_safe = htab->plt_thread_safe;
9567   bfd_boolean use_fake_dep = plt_thread_safe;
9568   bfd_vma cmp_branch_off = 0;
9569
9570   if (!ALWAYS_USE_FAKE_DEP
9571       && plt_thread_safe
9572       && !(stub_entry->h != NULL
9573            && (stub_entry->h == htab->tls_get_addr_fd
9574                || stub_entry->h == htab->tls_get_addr)
9575            && !htab->no_tls_get_addr_opt))
9576     {
9577       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
9578       bfd_vma pltindex = (pltoff - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE;
9579       bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
9580       bfd_vma to, from;
9581
9582       if (pltindex > 32767)
9583         glinkoff += (pltindex - 32767) * 4;
9584       to = (glinkoff
9585             + htab->glink->output_offset
9586             + htab->glink->output_section->vma);
9587       from = (p - stub_entry->stub_sec->contents
9588               + 4 * (ALWAYS_EMIT_R2SAVE
9589                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9590               + 4 * (PPC_HA (offset) != 0)
9591               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
9592                      != PPC_HA (offset))
9593               + 4 * (plt_static_chain != 0)
9594               + 20
9595               + stub_entry->stub_sec->output_offset
9596               + stub_entry->stub_sec->output_section->vma);
9597       cmp_branch_off = to - from;
9598       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
9599     }
9600
9601   if (PPC_HA (offset) != 0)
9602     {
9603       if (r != NULL)
9604         {
9605           if (ALWAYS_EMIT_R2SAVE
9606               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9607             r[0].r_offset += 4;
9608           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9609           r[1].r_offset = r[0].r_offset + 4;
9610           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9611           r[1].r_addend = r[0].r_addend;
9612           if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9613             {
9614               r[2].r_offset = r[1].r_offset + 4;
9615               r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9616               r[2].r_addend = r[0].r_addend;
9617             }
9618           else
9619             {
9620               r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
9621               r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9622               r[2].r_addend = r[0].r_addend + 8;
9623               if (plt_static_chain)
9624                 {
9625                   r[3].r_offset = r[2].r_offset + 4;
9626                   r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9627                   r[3].r_addend = r[0].r_addend + 16;
9628                 }
9629             }
9630         }
9631       if (ALWAYS_EMIT_R2SAVE
9632           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9633         bfd_put_32 (obfd, STD_R2_40R1, p),                      p += 4;
9634       bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p),     p += 4;
9635       bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),      p += 4;
9636       if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9637         {
9638           bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9639           offset = 0;
9640         }
9641       bfd_put_32 (obfd, MTCTR_R11, p),                          p += 4;
9642       if (use_fake_dep)
9643         {
9644           bfd_put_32 (obfd, XOR_R11_R11_R11, p),                p += 4;
9645           bfd_put_32 (obfd, ADD_R12_R12_R11, p),                p += 4;
9646         }
9647       bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p),   p += 4;
9648       if (plt_static_chain)
9649         bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
9650     }
9651   else
9652     {
9653       if (r != NULL)
9654         {
9655           if (ALWAYS_EMIT_R2SAVE
9656               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9657             r[0].r_offset += 4;
9658           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9659           if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9660             {
9661               r[1].r_offset = r[0].r_offset + 4;
9662               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9663               r[1].r_addend = r[0].r_addend;
9664             }
9665           else
9666             {
9667               r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
9668               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9669               r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
9670               if (plt_static_chain)
9671                 {
9672                   r[2].r_offset = r[1].r_offset + 4;
9673                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9674                   r[2].r_addend = r[0].r_addend + 8;
9675                 }
9676             }
9677         }
9678       if (ALWAYS_EMIT_R2SAVE
9679           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9680         bfd_put_32 (obfd, STD_R2_40R1, p),                      p += 4;
9681       bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p),       p += 4;
9682       if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9683         {
9684           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
9685           offset = 0;
9686         }
9687       bfd_put_32 (obfd, MTCTR_R11, p),                          p += 4;
9688       if (use_fake_dep)
9689         {
9690           bfd_put_32 (obfd, XOR_R11_R11_R11, p),                p += 4;
9691           bfd_put_32 (obfd, ADD_R2_R2_R11, p),                  p += 4;
9692         }
9693       if (plt_static_chain)
9694         bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9695       bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p),    p += 4;
9696     }
9697   if (plt_thread_safe && !use_fake_dep)
9698     {
9699       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
9700       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
9701       bfd_put_32 (obfd, B_DOT + cmp_branch_off, p),             p += 4;
9702     }
9703   else
9704     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
9705   return p;
9706 }
9707
9708 /* Build a special .plt call stub for __tls_get_addr.  */
9709
9710 #define LD_R11_0R3      0xe9630000
9711 #define LD_R12_0R3      0xe9830000
9712 #define MR_R0_R3        0x7c601b78
9713 #define CMPDI_R11_0     0x2c2b0000
9714 #define ADD_R3_R12_R13  0x7c6c6a14
9715 #define BEQLR           0x4d820020
9716 #define MR_R3_R0        0x7c030378
9717 #define MFLR_R11        0x7d6802a6
9718 #define STD_R11_0R1     0xf9610000
9719 #define BCTRL           0x4e800421
9720 #define LD_R11_0R1      0xe9610000
9721 #define LD_R2_0R1       0xe8410000
9722 #define MTLR_R11        0x7d6803a6
9723
9724 static inline bfd_byte *
9725 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
9726                          struct ppc_stub_hash_entry *stub_entry,
9727                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9728 {
9729   bfd *obfd = htab->stub_bfd;
9730
9731   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
9732   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
9733   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
9734   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
9735   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
9736   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
9737   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
9738   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
9739   bfd_put_32 (obfd, STD_R11_0R1 + 32, p),       p += 4;
9740
9741   if (r != NULL)
9742     r[0].r_offset += 9 * 4;
9743   p = build_plt_stub (htab, stub_entry, p, offset, r);
9744   bfd_put_32 (obfd, BCTRL, p - 4);
9745
9746   bfd_put_32 (obfd, LD_R11_0R1 + 32, p),        p += 4;
9747   bfd_put_32 (obfd, LD_R2_0R1 + 40, p),         p += 4;
9748   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
9749   bfd_put_32 (obfd, BLR, p),                    p += 4;
9750
9751   return p;
9752 }
9753
9754 static Elf_Internal_Rela *
9755 get_relocs (asection *sec, int count)
9756 {
9757   Elf_Internal_Rela *relocs;
9758   struct bfd_elf_section_data *elfsec_data;
9759
9760   elfsec_data = elf_section_data (sec);
9761   relocs = elfsec_data->relocs;
9762   if (relocs == NULL)
9763     {
9764       bfd_size_type relsize;
9765       relsize = sec->reloc_count * sizeof (*relocs);
9766       relocs = bfd_alloc (sec->owner, relsize);
9767       if (relocs == NULL)
9768         return NULL;
9769       elfsec_data->relocs = relocs;
9770       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9771                                           sizeof (Elf_Internal_Shdr));
9772       if (elfsec_data->rela.hdr == NULL)
9773         return NULL;
9774       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9775                                         * sizeof (Elf64_External_Rela));
9776       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
9777       sec->reloc_count = 0;
9778     }
9779   relocs += sec->reloc_count;
9780   sec->reloc_count += count;
9781   return relocs;
9782 }
9783
9784 static bfd_vma
9785 get_r2off (struct bfd_link_info *info,
9786            struct ppc_stub_hash_entry *stub_entry)
9787 {
9788   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9789   bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
9790
9791   if (r2off == 0)
9792     {
9793       /* Support linking -R objects.  Get the toc pointer from the
9794          opd entry.  */
9795       char buf[8];
9796       asection *opd = stub_entry->h->elf.root.u.def.section;
9797       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
9798
9799       if (strcmp (opd->name, ".opd") != 0
9800           || opd->reloc_count != 0)
9801         {
9802           info->callbacks->einfo (_("%P: cannot find opd entry toc for %s\n"),
9803                                   stub_entry->h->elf.root.root.string);
9804           bfd_set_error (bfd_error_bad_value);
9805           return 0;
9806         }
9807       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
9808         return 0;
9809       r2off = bfd_get_64 (opd->owner, buf);
9810       r2off -= elf_gp (info->output_bfd);
9811     }
9812   r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
9813   return r2off;
9814 }
9815
9816 static bfd_boolean
9817 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9818 {
9819   struct ppc_stub_hash_entry *stub_entry;
9820   struct ppc_branch_hash_entry *br_entry;
9821   struct bfd_link_info *info;
9822   struct ppc_link_hash_table *htab;
9823   bfd_byte *loc;
9824   bfd_byte *p;
9825   bfd_vma dest, off;
9826   int size;
9827   Elf_Internal_Rela *r;
9828   asection *plt;
9829
9830   /* Massage our args to the form they really have.  */
9831   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9832   info = in_arg;
9833
9834   htab = ppc_hash_table (info);
9835   if (htab == NULL)
9836     return FALSE;
9837
9838   /* Make a note of the offset within the stubs for this entry.  */
9839   stub_entry->stub_offset = stub_entry->stub_sec->size;
9840   loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
9841
9842   htab->stub_count[stub_entry->stub_type - 1] += 1;
9843   switch (stub_entry->stub_type)
9844     {
9845     case ppc_stub_long_branch:
9846     case ppc_stub_long_branch_r2off:
9847       /* Branches are relative.  This is where we are going to.  */
9848       off = dest = (stub_entry->target_value
9849                     + stub_entry->target_section->output_offset
9850                     + stub_entry->target_section->output_section->vma);
9851
9852       /* And this is where we are coming from.  */
9853       off -= (stub_entry->stub_offset
9854               + stub_entry->stub_sec->output_offset
9855               + stub_entry->stub_sec->output_section->vma);
9856
9857       size = 4;
9858       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9859         {
9860           bfd_vma r2off = get_r2off (info, stub_entry);
9861
9862           if (r2off == 0)
9863             {
9864               htab->stub_error = TRUE;
9865               return FALSE;
9866             }
9867           bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9868           loc += 4;
9869           size = 12;
9870           if (PPC_HA (r2off) != 0)
9871             {
9872               size = 16;
9873               bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9874               loc += 4;
9875             }
9876           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9877           loc += 4;
9878           off -= size - 4;
9879         }
9880       bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
9881
9882       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9883         {
9884           info->callbacks->einfo (_("%P: long branch stub `%s' offset overflow\n"),
9885                                   stub_entry->root.string);
9886           htab->stub_error = TRUE;
9887           return FALSE;
9888         }
9889
9890       if (info->emitrelocations)
9891         {
9892           r = get_relocs (stub_entry->stub_sec, 1);
9893           if (r == NULL)
9894             return FALSE;
9895           r->r_offset = loc - stub_entry->stub_sec->contents;
9896           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9897           r->r_addend = dest;
9898           if (stub_entry->h != NULL)
9899             {
9900               struct elf_link_hash_entry **hashes;
9901               unsigned long symndx;
9902               struct ppc_link_hash_entry *h;
9903
9904               hashes = elf_sym_hashes (htab->stub_bfd);
9905               if (hashes == NULL)
9906                 {
9907                   bfd_size_type hsize;
9908
9909                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9910                   hashes = bfd_zalloc (htab->stub_bfd, hsize);
9911                   if (hashes == NULL)
9912                     return FALSE;
9913                   elf_sym_hashes (htab->stub_bfd) = hashes;
9914                   htab->stub_globals = 1;
9915                 }
9916               symndx = htab->stub_globals++;
9917               h = stub_entry->h;
9918               hashes[symndx] = &h->elf;
9919               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9920               if (h->oh != NULL && h->oh->is_func)
9921                 h = ppc_follow_link (h->oh);
9922               if (h->elf.root.u.def.section != stub_entry->target_section)
9923                 /* H is an opd symbol.  The addend must be zero.  */
9924                 r->r_addend = 0;
9925               else
9926                 {
9927                   off = (h->elf.root.u.def.value
9928                          + h->elf.root.u.def.section->output_offset
9929                          + h->elf.root.u.def.section->output_section->vma);
9930                   r->r_addend -= off;
9931                 }
9932             }
9933         }
9934       break;
9935
9936     case ppc_stub_plt_branch:
9937     case ppc_stub_plt_branch_r2off:
9938       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9939                                          stub_entry->root.string + 9,
9940                                          FALSE, FALSE);
9941       if (br_entry == NULL)
9942         {
9943           info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
9944                                   stub_entry->root.string);
9945           htab->stub_error = TRUE;
9946           return FALSE;
9947         }
9948
9949       dest = (stub_entry->target_value
9950               + stub_entry->target_section->output_offset
9951               + stub_entry->target_section->output_section->vma);
9952
9953       bfd_put_64 (htab->brlt->owner, dest,
9954                   htab->brlt->contents + br_entry->offset);
9955
9956       if (br_entry->iter == htab->stub_iteration)
9957         {
9958           br_entry->iter = 0;
9959
9960           if (htab->relbrlt != NULL)
9961             {
9962               /* Create a reloc for the branch lookup table entry.  */
9963               Elf_Internal_Rela rela;
9964               bfd_byte *rl;
9965
9966               rela.r_offset = (br_entry->offset
9967                                + htab->brlt->output_offset
9968                                + htab->brlt->output_section->vma);
9969               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9970               rela.r_addend = dest;
9971
9972               rl = htab->relbrlt->contents;
9973               rl += (htab->relbrlt->reloc_count++
9974                      * sizeof (Elf64_External_Rela));
9975               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9976             }
9977           else if (info->emitrelocations)
9978             {
9979               r = get_relocs (htab->brlt, 1);
9980               if (r == NULL)
9981                 return FALSE;
9982               /* brlt, being SEC_LINKER_CREATED does not go through the
9983                  normal reloc processing.  Symbols and offsets are not
9984                  translated from input file to output file form, so
9985                  set up the offset per the output file.  */
9986               r->r_offset = (br_entry->offset
9987                              + htab->brlt->output_offset
9988                              + htab->brlt->output_section->vma);
9989               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9990               r->r_addend = dest;
9991             }
9992         }
9993
9994       dest = (br_entry->offset
9995               + htab->brlt->output_offset
9996               + htab->brlt->output_section->vma);
9997
9998       off = (dest
9999              - elf_gp (htab->brlt->output_section->owner)
10000              - htab->stub_group[stub_entry->id_sec->id].toc_off);
10001
10002       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10003         {
10004           info->callbacks->einfo
10005             (_("%P: linkage table error against `%s'\n"),
10006              stub_entry->root.string);
10007           bfd_set_error (bfd_error_bad_value);
10008           htab->stub_error = TRUE;
10009           return FALSE;
10010         }
10011
10012       if (info->emitrelocations)
10013         {
10014           r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
10015           if (r == NULL)
10016             return FALSE;
10017           r[0].r_offset = loc - stub_entry->stub_sec->contents;
10018           if (bfd_big_endian (info->output_bfd))
10019             r[0].r_offset += 2;
10020           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10021             r[0].r_offset += 4;
10022           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10023           r[0].r_addend = dest;
10024           if (PPC_HA (off) != 0)
10025             {
10026               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10027               r[1].r_offset = r[0].r_offset + 4;
10028               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10029               r[1].r_addend = r[0].r_addend;
10030             }
10031         }
10032
10033       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10034         {
10035           if (PPC_HA (off) != 0)
10036             {
10037               size = 16;
10038               bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
10039               loc += 4;
10040               bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
10041             }
10042           else
10043             {
10044               size = 12;
10045               bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
10046             }
10047         }
10048       else
10049         {
10050           bfd_vma r2off = get_r2off (info, stub_entry);
10051
10052           if (r2off == 0)
10053             {
10054               htab->stub_error = TRUE;
10055               return FALSE;
10056             }
10057
10058           bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
10059           loc += 4;
10060           size = 20;
10061           if (PPC_HA (off) != 0)
10062             {
10063               size += 4;
10064               bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
10065               loc += 4;
10066               bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
10067               loc += 4;
10068             }
10069           else
10070             {
10071               bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
10072               loc += 4;
10073             }
10074
10075           if (PPC_HA (r2off) != 0)
10076             {
10077               size += 4;
10078               bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10079               loc += 4;
10080             }
10081           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
10082         }
10083       loc += 4;
10084       bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
10085       loc += 4;
10086       bfd_put_32 (htab->stub_bfd, BCTR, loc);
10087       break;
10088
10089     case ppc_stub_plt_call:
10090     case ppc_stub_plt_call_r2save:
10091       if (stub_entry->h != NULL
10092           && stub_entry->h->is_func_descriptor
10093           && stub_entry->h->oh != NULL)
10094         {
10095           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10096
10097           /* If the old-ABI "dot-symbol" is undefined make it weak so
10098              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
10099              FIXME: We used to define the symbol on one of the call
10100              stubs instead, which is why we test symbol section id
10101              against htab->top_id in various places.  Likely all
10102              these checks could now disappear.  */
10103           if (fh->elf.root.type == bfd_link_hash_undefined)
10104             fh->elf.root.type = bfd_link_hash_undefweak;
10105           /* Stop undo_symbol_twiddle changing it back to undefined.  */
10106           fh->was_undefined = 0;
10107         }
10108
10109       /* Now build the stub.  */
10110       dest = stub_entry->plt_ent->plt.offset & ~1;
10111       if (dest >= (bfd_vma) -2)
10112         abort ();
10113
10114       plt = htab->plt;
10115       if (!htab->elf.dynamic_sections_created
10116           || stub_entry->h == NULL
10117           || stub_entry->h->elf.dynindx == -1)
10118         plt = htab->iplt;
10119
10120       dest += plt->output_offset + plt->output_section->vma;
10121
10122       if (stub_entry->h == NULL
10123           && (stub_entry->plt_ent->plt.offset & 1) == 0)
10124         {
10125           Elf_Internal_Rela rela;
10126           bfd_byte *rl;
10127
10128           rela.r_offset = dest;
10129           rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10130           rela.r_addend = (stub_entry->target_value
10131                            + stub_entry->target_section->output_offset
10132                            + stub_entry->target_section->output_section->vma);
10133
10134           rl = (htab->reliplt->contents
10135                 + (htab->reliplt->reloc_count++
10136                    * sizeof (Elf64_External_Rela)));
10137           bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
10138           stub_entry->plt_ent->plt.offset |= 1;
10139         }
10140
10141       off = (dest
10142              - elf_gp (plt->output_section->owner)
10143              - htab->stub_group[stub_entry->id_sec->id].toc_off);
10144
10145       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10146         {
10147           info->callbacks->einfo
10148             (_("%P: linkage table error against `%s'\n"),
10149              stub_entry->h != NULL
10150              ? stub_entry->h->elf.root.root.string
10151              : "<local sym>");
10152           bfd_set_error (bfd_error_bad_value);
10153           htab->stub_error = TRUE;
10154           return FALSE;
10155         }
10156
10157       if (htab->plt_stub_align != 0)
10158         {
10159           unsigned pad = plt_stub_pad (htab, stub_entry, off);
10160
10161           stub_entry->stub_sec->size += pad;
10162           stub_entry->stub_offset = stub_entry->stub_sec->size;
10163           loc += pad;
10164         }
10165
10166       r = NULL;
10167       if (info->emitrelocations)
10168         {
10169           r = get_relocs (stub_entry->stub_sec,
10170                           (2
10171                            + (PPC_HA (off) != 0)
10172                            + (htab->plt_static_chain
10173                               && PPC_HA (off + 16) == PPC_HA (off))));
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           r[0].r_addend = dest;
10180         }
10181       if (stub_entry->h != NULL
10182           && (stub_entry->h == htab->tls_get_addr_fd
10183               || stub_entry->h == htab->tls_get_addr)
10184           && !htab->no_tls_get_addr_opt)
10185         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10186       else
10187         p = build_plt_stub (htab, stub_entry, loc, off, r);
10188       size = p - loc;
10189       break;
10190
10191     default:
10192       BFD_FAIL ();
10193       return FALSE;
10194     }
10195
10196   stub_entry->stub_sec->size += size;
10197
10198   if (htab->emit_stub_syms)
10199     {
10200       struct elf_link_hash_entry *h;
10201       size_t len1, len2;
10202       char *name;
10203       const char *const stub_str[] = { "long_branch",
10204                                        "long_branch_r2off",
10205                                        "plt_branch",
10206                                        "plt_branch_r2off",
10207                                        "plt_call",
10208                                        "plt_call" };
10209
10210       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10211       len2 = strlen (stub_entry->root.string);
10212       name = bfd_malloc (len1 + len2 + 2);
10213       if (name == NULL)
10214         return FALSE;
10215       memcpy (name, stub_entry->root.string, 9);
10216       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10217       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10218       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10219       if (h == NULL)
10220         return FALSE;
10221       if (h->root.type == bfd_link_hash_new)
10222         {
10223           h->root.type = bfd_link_hash_defined;
10224           h->root.u.def.section = stub_entry->stub_sec;
10225           h->root.u.def.value = stub_entry->stub_offset;
10226           h->ref_regular = 1;
10227           h->def_regular = 1;
10228           h->ref_regular_nonweak = 1;
10229           h->forced_local = 1;
10230           h->non_elf = 0;
10231         }
10232     }
10233
10234   return TRUE;
10235 }
10236
10237 /* As above, but don't actually build the stub.  Just bump offset so
10238    we know stub section sizes, and select plt_branch stubs where
10239    long_branch stubs won't do.  */
10240
10241 static bfd_boolean
10242 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10243 {
10244   struct ppc_stub_hash_entry *stub_entry;
10245   struct bfd_link_info *info;
10246   struct ppc_link_hash_table *htab;
10247   bfd_vma off;
10248   int size;
10249
10250   /* Massage our args to the form they really have.  */
10251   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10252   info = in_arg;
10253
10254   htab = ppc_hash_table (info);
10255   if (htab == NULL)
10256     return FALSE;
10257
10258   if (stub_entry->stub_type == ppc_stub_plt_call
10259       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10260     {
10261       asection *plt;
10262       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
10263       if (off >= (bfd_vma) -2)
10264         abort ();
10265       plt = htab->plt;
10266       if (!htab->elf.dynamic_sections_created
10267           || stub_entry->h == NULL
10268           || stub_entry->h->elf.dynindx == -1)
10269         plt = htab->iplt;
10270       off += (plt->output_offset
10271               + plt->output_section->vma
10272               - elf_gp (plt->output_section->owner)
10273               - htab->stub_group[stub_entry->id_sec->id].toc_off);
10274
10275       size = plt_stub_size (htab, stub_entry, off);
10276       if (htab->plt_stub_align)
10277         size += plt_stub_pad (htab, stub_entry, off);
10278       if (info->emitrelocations)
10279         {
10280           stub_entry->stub_sec->reloc_count
10281             += (2
10282                 + (PPC_HA (off) != 0)
10283                 + (htab->plt_static_chain
10284                    && PPC_HA (off + 16) == PPC_HA (off)));
10285           stub_entry->stub_sec->flags |= SEC_RELOC;
10286         }
10287     }
10288   else
10289     {
10290       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10291          variants.  */
10292       bfd_vma r2off = 0;
10293
10294       off = (stub_entry->target_value
10295              + stub_entry->target_section->output_offset
10296              + stub_entry->target_section->output_section->vma);
10297       off -= (stub_entry->stub_sec->size
10298               + stub_entry->stub_sec->output_offset
10299               + stub_entry->stub_sec->output_section->vma);
10300
10301       /* Reset the stub type from the plt variant in case we now
10302          can reach with a shorter stub.  */
10303       if (stub_entry->stub_type >= ppc_stub_plt_branch)
10304         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10305
10306       size = 4;
10307       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10308         {
10309           r2off = get_r2off (info, stub_entry);
10310           if (r2off == 0)
10311             {
10312               htab->stub_error = TRUE;
10313               return FALSE;
10314             }
10315           size = 12;
10316           if (PPC_HA (r2off) != 0)
10317             size = 16;
10318           off -= size - 4;
10319         }
10320
10321       /* If the branch offset if too big, use a ppc_stub_plt_branch.  */
10322       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10323         {
10324           struct ppc_branch_hash_entry *br_entry;
10325
10326           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10327                                              stub_entry->root.string + 9,
10328                                              TRUE, FALSE);
10329           if (br_entry == NULL)
10330             {
10331               info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
10332                                       stub_entry->root.string);
10333               htab->stub_error = TRUE;
10334               return FALSE;
10335             }
10336
10337           if (br_entry->iter != htab->stub_iteration)
10338             {
10339               br_entry->iter = htab->stub_iteration;
10340               br_entry->offset = htab->brlt->size;
10341               htab->brlt->size += 8;
10342
10343               if (htab->relbrlt != NULL)
10344                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10345               else if (info->emitrelocations)
10346                 {
10347                   htab->brlt->reloc_count += 1;
10348                   htab->brlt->flags |= SEC_RELOC;
10349                 }
10350             }
10351
10352           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
10353           off = (br_entry->offset
10354                  + htab->brlt->output_offset
10355                  + htab->brlt->output_section->vma
10356                  - elf_gp (htab->brlt->output_section->owner)
10357                  - htab->stub_group[stub_entry->id_sec->id].toc_off);
10358
10359           if (info->emitrelocations)
10360             {
10361               stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10362               stub_entry->stub_sec->flags |= SEC_RELOC;
10363             }
10364
10365           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10366             {
10367               size = 12;
10368               if (PPC_HA (off) != 0)
10369                 size = 16;
10370             }
10371           else
10372             {
10373               size = 20;
10374               if (PPC_HA (off) != 0)
10375                 size += 4;
10376
10377               if (PPC_HA (r2off) != 0)
10378                 size += 4;
10379             }
10380         }
10381       else if (info->emitrelocations)
10382         {
10383           stub_entry->stub_sec->reloc_count += 1;
10384           stub_entry->stub_sec->flags |= SEC_RELOC;
10385         }
10386     }
10387
10388   stub_entry->stub_sec->size += size;
10389   return TRUE;
10390 }
10391
10392 /* Set up various things so that we can make a list of input sections
10393    for each output section included in the link.  Returns -1 on error,
10394    0 when no stubs will be needed, and 1 on success.  */
10395
10396 int
10397 ppc64_elf_setup_section_lists
10398   (struct bfd_link_info *info,
10399    asection *(*add_stub_section) (const char *, asection *),
10400    void (*layout_sections_again) (void))
10401 {
10402   bfd *input_bfd;
10403   int top_id, top_index, id;
10404   asection *section;
10405   asection **input_list;
10406   bfd_size_type amt;
10407   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10408
10409   if (htab == NULL)
10410     return -1;
10411   /* Stash our params away.  */
10412   htab->add_stub_section = add_stub_section;
10413   htab->layout_sections_again = layout_sections_again;
10414
10415   if (htab->brlt == NULL)
10416     return 0;
10417
10418   /* Find the top input section id.  */
10419   for (input_bfd = info->input_bfds, top_id = 3;
10420        input_bfd != NULL;
10421        input_bfd = input_bfd->link_next)
10422     {
10423       for (section = input_bfd->sections;
10424            section != NULL;
10425            section = section->next)
10426         {
10427           if (top_id < section->id)
10428             top_id = section->id;
10429         }
10430     }
10431
10432   htab->top_id = top_id;
10433   amt = sizeof (struct map_stub) * (top_id + 1);
10434   htab->stub_group = bfd_zmalloc (amt);
10435   if (htab->stub_group == NULL)
10436     return -1;
10437
10438   /* Set toc_off for com, und, abs and ind sections.  */
10439   for (id = 0; id < 3; id++)
10440     htab->stub_group[id].toc_off = TOC_BASE_OFF;
10441
10442   /* We can't use output_bfd->section_count here to find the top output
10443      section index as some sections may have been removed, and
10444      strip_excluded_output_sections doesn't renumber the indices.  */
10445   for (section = info->output_bfd->sections, top_index = 0;
10446        section != NULL;
10447        section = section->next)
10448     {
10449       if (top_index < section->index)
10450         top_index = section->index;
10451     }
10452
10453   htab->top_index = top_index;
10454   amt = sizeof (asection *) * (top_index + 1);
10455   input_list = bfd_zmalloc (amt);
10456   htab->input_list = input_list;
10457   if (input_list == NULL)
10458     return -1;
10459
10460   return 1;
10461 }
10462
10463 /* Set up for first pass at multitoc partitioning.  */
10464
10465 void
10466 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10467 {
10468   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10469
10470   elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10471   htab->toc_curr = elf_gp (info->output_bfd);
10472   htab->toc_bfd = NULL;
10473   htab->toc_first_sec = NULL;
10474 }
10475
10476 /* The linker repeatedly calls this function for each TOC input section
10477    and linker generated GOT section.  Group input bfds such that the toc
10478    within a group is less than 64k in size.  */
10479
10480 bfd_boolean
10481 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10482 {
10483   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10484   bfd_vma addr, off, limit;
10485
10486   if (htab == NULL)
10487     return FALSE;
10488
10489   if (!htab->second_toc_pass)
10490     {
10491       /* Keep track of the first .toc or .got section for this input bfd.  */
10492       if (htab->toc_bfd != isec->owner)
10493         {
10494           htab->toc_bfd = isec->owner;
10495           htab->toc_first_sec = isec;
10496         }
10497
10498       addr = isec->output_offset + isec->output_section->vma;
10499       off = addr - htab->toc_curr;
10500       limit = 0x80008000;
10501       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10502         limit = 0x10000;
10503       if (off + isec->size > limit)
10504         {
10505           addr = (htab->toc_first_sec->output_offset
10506                   + htab->toc_first_sec->output_section->vma);
10507           htab->toc_curr = addr;
10508         }
10509
10510       /* toc_curr is the base address of this toc group.  Set elf_gp
10511          for the input section to be the offset relative to the
10512          output toc base plus 0x8000.  Making the input elf_gp an
10513          offset allows us to move the toc as a whole without
10514          recalculating input elf_gp.  */
10515       off = htab->toc_curr - elf_gp (isec->output_section->owner);
10516       off += TOC_BASE_OFF;
10517
10518       /* Die if someone uses a linker script that doesn't keep input
10519          file .toc and .got together.  */
10520       if (elf_gp (isec->owner) != 0
10521           && elf_gp (isec->owner) != off)
10522         return FALSE;
10523
10524       elf_gp (isec->owner) = off;
10525       return TRUE;
10526     }
10527
10528   /* During the second pass toc_first_sec points to the start of
10529      a toc group, and toc_curr is used to track the old elf_gp.
10530      We use toc_bfd to ensure we only look at each bfd once.  */
10531   if (htab->toc_bfd == isec->owner)
10532     return TRUE;
10533   htab->toc_bfd = isec->owner;
10534
10535   if (htab->toc_first_sec == NULL
10536       || htab->toc_curr != elf_gp (isec->owner))
10537     {
10538       htab->toc_curr = elf_gp (isec->owner);
10539       htab->toc_first_sec = isec;
10540     }
10541   addr = (htab->toc_first_sec->output_offset
10542           + htab->toc_first_sec->output_section->vma);
10543   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10544   elf_gp (isec->owner) = off;
10545
10546   return TRUE;
10547 }
10548
10549 /* Called via elf_link_hash_traverse to merge GOT entries for global
10550    symbol H.  */
10551
10552 static bfd_boolean
10553 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10554 {
10555   if (h->root.type == bfd_link_hash_indirect)
10556     return TRUE;
10557
10558   merge_got_entries (&h->got.glist);
10559
10560   return TRUE;
10561 }
10562
10563 /* Called via elf_link_hash_traverse to allocate GOT entries for global
10564    symbol H.  */
10565
10566 static bfd_boolean
10567 reallocate_got (struct elf_link_hash_entry *h, void *inf)
10568 {
10569   struct got_entry *gent;
10570
10571   if (h->root.type == bfd_link_hash_indirect)
10572     return TRUE;
10573
10574   for (gent = h->got.glist; gent != NULL; gent = gent->next)
10575     if (!gent->is_indirect)
10576       allocate_got (h, (struct bfd_link_info *) inf, gent);
10577   return TRUE;
10578 }
10579
10580 /* Called on the first multitoc pass after the last call to
10581    ppc64_elf_next_toc_section.  This function removes duplicate GOT
10582    entries.  */
10583
10584 bfd_boolean
10585 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
10586 {
10587   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10588   struct bfd *ibfd, *ibfd2;
10589   bfd_boolean done_something;
10590
10591   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
10592
10593   if (!htab->do_multi_toc)
10594     return FALSE;
10595
10596   /* Merge global sym got entries within a toc group.  */
10597   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10598
10599   /* And tlsld_got.  */
10600   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10601     {
10602       struct got_entry *ent, *ent2;
10603
10604       if (!is_ppc64_elf (ibfd))
10605         continue;
10606
10607       ent = ppc64_tlsld_got (ibfd);
10608       if (!ent->is_indirect
10609           && ent->got.offset != (bfd_vma) -1)
10610         {
10611           for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10612             {
10613               if (!is_ppc64_elf (ibfd2))
10614                 continue;
10615
10616               ent2 = ppc64_tlsld_got (ibfd2);
10617               if (!ent2->is_indirect
10618                   && ent2->got.offset != (bfd_vma) -1
10619                   && elf_gp (ibfd2) == elf_gp (ibfd))
10620                 {
10621                   ent2->is_indirect = TRUE;
10622                   ent2->got.ent = ent;
10623                 }
10624             }
10625         }
10626     }
10627
10628   /* Zap sizes of got sections.  */
10629   htab->reliplt->rawsize = htab->reliplt->size;
10630   htab->reliplt->size -= htab->got_reli_size;
10631   htab->got_reli_size = 0;
10632
10633   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10634     {
10635       asection *got, *relgot;
10636
10637       if (!is_ppc64_elf (ibfd))
10638         continue;
10639
10640       got = ppc64_elf_tdata (ibfd)->got;
10641       if (got != NULL)
10642         {
10643           got->rawsize = got->size;
10644           got->size = 0;
10645           relgot = ppc64_elf_tdata (ibfd)->relgot;
10646           relgot->rawsize = relgot->size;
10647           relgot->size = 0;
10648         }
10649     }
10650
10651   /* Now reallocate the got, local syms first.  We don't need to
10652      allocate section contents again since we never increase size.  */
10653   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10654     {
10655       struct got_entry **lgot_ents;
10656       struct got_entry **end_lgot_ents;
10657       struct plt_entry **local_plt;
10658       struct plt_entry **end_local_plt;
10659       unsigned char *lgot_masks;
10660       bfd_size_type locsymcount;
10661       Elf_Internal_Shdr *symtab_hdr;
10662       asection *s, *srel;
10663
10664       if (!is_ppc64_elf (ibfd))
10665         continue;
10666
10667       lgot_ents = elf_local_got_ents (ibfd);
10668       if (!lgot_ents)
10669         continue;
10670
10671       symtab_hdr = &elf_symtab_hdr (ibfd);
10672       locsymcount = symtab_hdr->sh_info;
10673       end_lgot_ents = lgot_ents + locsymcount;
10674       local_plt = (struct plt_entry **) end_lgot_ents;
10675       end_local_plt = local_plt + locsymcount;
10676       lgot_masks = (unsigned char *) end_local_plt;
10677       s = ppc64_elf_tdata (ibfd)->got;
10678       srel = ppc64_elf_tdata (ibfd)->relgot;
10679       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10680         {
10681           struct got_entry *ent;
10682
10683           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
10684             {
10685               unsigned int num = 1;
10686               ent->got.offset = s->size;
10687               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10688                 num = 2;
10689               s->size += num * 8;
10690               if (info->shared)
10691                 srel->size += num * sizeof (Elf64_External_Rela);
10692               else if ((*lgot_masks & PLT_IFUNC) != 0)
10693                 {
10694                   htab->reliplt->size
10695                     += num * sizeof (Elf64_External_Rela);
10696                   htab->got_reli_size
10697                     += num * sizeof (Elf64_External_Rela);
10698                 }
10699             }
10700         }
10701     }
10702
10703   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10704
10705   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10706     {
10707       struct got_entry *ent;
10708
10709       if (!is_ppc64_elf (ibfd))
10710         continue;
10711
10712       ent = ppc64_tlsld_got (ibfd);
10713       if (!ent->is_indirect
10714           && ent->got.offset != (bfd_vma) -1)
10715         {
10716           asection *s = ppc64_elf_tdata (ibfd)->got;
10717           ent->got.offset = s->size;
10718           s->size += 16;
10719           if (info->shared)
10720             {
10721               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10722               srel->size += sizeof (Elf64_External_Rela);
10723             }
10724         }
10725     }
10726
10727   done_something = htab->reliplt->rawsize != htab->reliplt->size;
10728   if (!done_something)
10729     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10730       {
10731         asection *got;
10732
10733         if (!is_ppc64_elf (ibfd))
10734           continue;
10735
10736         got = ppc64_elf_tdata (ibfd)->got;
10737         if (got != NULL)
10738           {
10739             done_something = got->rawsize != got->size;
10740             if (done_something)
10741               break;
10742           }
10743       }
10744
10745   if (done_something)
10746     (*htab->layout_sections_again) ();
10747
10748   /* Set up for second pass over toc sections to recalculate elf_gp
10749      on input sections.  */
10750   htab->toc_bfd = NULL;
10751   htab->toc_first_sec = NULL;
10752   htab->second_toc_pass = TRUE;
10753   return done_something;
10754 }
10755
10756 /* Called after second pass of multitoc partitioning.  */
10757
10758 void
10759 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10760 {
10761   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10762
10763   /* After the second pass, toc_curr tracks the TOC offset used
10764      for code sections below in ppc64_elf_next_input_section.  */
10765   htab->toc_curr = TOC_BASE_OFF;
10766 }
10767
10768 /* No toc references were found in ISEC.  If the code in ISEC makes no
10769    calls, then there's no need to use toc adjusting stubs when branching
10770    into ISEC.  Actually, indirect calls from ISEC are OK as they will
10771    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
10772    needed, and 2 if a cyclical call-graph was found but no other reason
10773    for a stub was detected.  If called from the top level, a return of
10774    2 means the same as a return of 0.  */
10775
10776 static int
10777 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
10778 {
10779   int ret;
10780
10781   /* Mark this section as checked.  */
10782   isec->call_check_done = 1;
10783
10784   /* We know none of our code bearing sections will need toc stubs.  */
10785   if ((isec->flags & SEC_LINKER_CREATED) != 0)
10786     return 0;
10787
10788   if (isec->size == 0)
10789     return 0;
10790
10791   if (isec->output_section == NULL)
10792     return 0;
10793
10794   ret = 0;
10795   if (isec->reloc_count != 0)
10796     {
10797       Elf_Internal_Rela *relstart, *rel;
10798       Elf_Internal_Sym *local_syms;
10799       struct ppc_link_hash_table *htab;
10800
10801       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10802                                             info->keep_memory);
10803       if (relstart == NULL)
10804         return -1;
10805
10806       /* Look for branches to outside of this section.  */
10807       local_syms = NULL;
10808       htab = ppc_hash_table (info);
10809       if (htab == NULL)
10810         return -1;
10811
10812       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
10813         {
10814           enum elf_ppc64_reloc_type r_type;
10815           unsigned long r_symndx;
10816           struct elf_link_hash_entry *h;
10817           struct ppc_link_hash_entry *eh;
10818           Elf_Internal_Sym *sym;
10819           asection *sym_sec;
10820           struct _opd_sec_data *opd;
10821           bfd_vma sym_value;
10822           bfd_vma dest;
10823
10824           r_type = ELF64_R_TYPE (rel->r_info);
10825           if (r_type != R_PPC64_REL24
10826               && r_type != R_PPC64_REL14
10827               && r_type != R_PPC64_REL14_BRTAKEN
10828               && r_type != R_PPC64_REL14_BRNTAKEN)
10829             continue;
10830
10831           r_symndx = ELF64_R_SYM (rel->r_info);
10832           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10833                           isec->owner))
10834             {
10835               ret = -1;
10836               break;
10837             }
10838
10839           /* Calls to dynamic lib functions go through a plt call stub
10840              that uses r2.  */
10841           eh = (struct ppc_link_hash_entry *) h;
10842           if (eh != NULL
10843               && (eh->elf.plt.plist != NULL
10844                   || (eh->oh != NULL
10845                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10846             {
10847               ret = 1;
10848               break;
10849             }
10850
10851           if (sym_sec == NULL)
10852             /* Ignore other undefined symbols.  */
10853             continue;
10854
10855           /* Assume branches to other sections not included in the
10856              link need stubs too, to cover -R and absolute syms.  */
10857           if (sym_sec->output_section == NULL)
10858             {
10859               ret = 1;
10860               break;
10861             }
10862
10863           if (h == NULL)
10864             sym_value = sym->st_value;
10865           else
10866             {
10867               if (h->root.type != bfd_link_hash_defined
10868                   && h->root.type != bfd_link_hash_defweak)
10869                 abort ();
10870               sym_value = h->root.u.def.value;
10871             }
10872           sym_value += rel->r_addend;
10873
10874           /* If this branch reloc uses an opd sym, find the code section.  */
10875           opd = get_opd_info (sym_sec);
10876           if (opd != NULL)
10877             {
10878               if (h == NULL && opd->adjust != NULL)
10879                 {
10880                   long adjust;
10881
10882                   adjust = opd->adjust[sym->st_value / 8];
10883                   if (adjust == -1)
10884                     /* Assume deleted functions won't ever be called.  */
10885                     continue;
10886                   sym_value += adjust;
10887                 }
10888
10889               dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10890               if (dest == (bfd_vma) -1)
10891                 continue;
10892             }
10893           else
10894             dest = (sym_value
10895                     + sym_sec->output_offset
10896                     + sym_sec->output_section->vma);
10897
10898           /* Ignore branch to self.  */
10899           if (sym_sec == isec)
10900             continue;
10901
10902           /* If the called function uses the toc, we need a stub.  */
10903           if (sym_sec->has_toc_reloc
10904               || sym_sec->makes_toc_func_call)
10905             {
10906               ret = 1;
10907               break;
10908             }
10909
10910           /* Assume any branch that needs a long branch stub might in fact
10911              need a plt_branch stub.  A plt_branch stub uses r2.  */
10912           else if (dest - (isec->output_offset
10913                            + isec->output_section->vma
10914                            + rel->r_offset) + (1 << 25) >= (2 << 25))
10915             {
10916               ret = 1;
10917               break;
10918             }
10919
10920           /* If calling back to a section in the process of being
10921              tested, we can't say for sure that no toc adjusting stubs
10922              are needed, so don't return zero.  */
10923           else if (sym_sec->call_check_in_progress)
10924             ret = 2;
10925
10926           /* Branches to another section that itself doesn't have any TOC
10927              references are OK.  Recursively call ourselves to check.  */
10928           else if (!sym_sec->call_check_done)
10929             {
10930               int recur;
10931
10932               /* Mark current section as indeterminate, so that other
10933                  sections that call back to current won't be marked as
10934                  known.  */
10935               isec->call_check_in_progress = 1;
10936               recur = toc_adjusting_stub_needed (info, sym_sec);
10937               isec->call_check_in_progress = 0;
10938
10939               if (recur != 0)
10940                 {
10941                   ret = recur;
10942                   if (recur != 2)
10943                     break;
10944                 }
10945             }
10946         }
10947
10948       if (local_syms != NULL
10949           && (elf_symtab_hdr (isec->owner).contents
10950               != (unsigned char *) local_syms))
10951         free (local_syms);
10952       if (elf_section_data (isec)->relocs != relstart)
10953         free (relstart);
10954     }
10955
10956   if ((ret & 1) == 0
10957       && isec->map_head.s != NULL
10958       && (strcmp (isec->output_section->name, ".init") == 0
10959           || strcmp (isec->output_section->name, ".fini") == 0))
10960     {
10961       if (isec->map_head.s->has_toc_reloc
10962           || isec->map_head.s->makes_toc_func_call)
10963         ret = 1;
10964       else if (!isec->map_head.s->call_check_done)
10965         {
10966           int recur;
10967           isec->call_check_in_progress = 1;
10968           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10969           isec->call_check_in_progress = 0;
10970           if (recur != 0)
10971             ret = recur;
10972         }
10973     }
10974
10975   if (ret == 1)
10976     isec->makes_toc_func_call = 1;
10977
10978   return ret;
10979 }
10980
10981 /* The linker repeatedly calls this function for each input section,
10982    in the order that input sections are linked into output sections.
10983    Build lists of input sections to determine groupings between which
10984    we may insert linker stubs.  */
10985
10986 bfd_boolean
10987 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
10988 {
10989   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10990
10991   if (htab == NULL)
10992     return FALSE;
10993
10994   if ((isec->output_section->flags & SEC_CODE) != 0
10995       && isec->output_section->index <= htab->top_index)
10996     {
10997       asection **list = htab->input_list + isec->output_section->index;
10998       /* Steal the link_sec pointer for our list.  */
10999 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
11000       /* This happens to make the list in reverse order,
11001          which is what we want.  */
11002       PREV_SEC (isec) = *list;
11003       *list = isec;
11004     }
11005
11006   if (htab->multi_toc_needed)
11007     {
11008       /* If a code section has a function that uses the TOC then we need
11009          to use the right TOC (obviously).  Also, make sure that .opd gets
11010          the correct TOC value for R_PPC64_TOC relocs that don't have or
11011          can't find their function symbol (shouldn't ever happen now).
11012          Also specially treat .fixup for the linux kernel.  .fixup
11013          contains branches, but only back to the function that hit an
11014          exception.  */
11015       if (isec->has_toc_reloc
11016           || (isec->flags & SEC_CODE) == 0
11017           || strcmp (isec->name, ".fixup") == 0)
11018         {
11019           if (elf_gp (isec->owner) != 0)
11020             htab->toc_curr = elf_gp (isec->owner);
11021         }
11022       else
11023         {
11024           if (!isec->call_check_done
11025               && toc_adjusting_stub_needed (info, isec) < 0)
11026             return FALSE;
11027           /* If we make a local call from this section, ie. a branch
11028              without a following nop, then we have no place to put a
11029              toc restoring insn.  We must use the same toc group as
11030              the callee.
11031              Testing makes_toc_func_call actually tests for *any*
11032              calls to functions that need a good toc pointer.  A more
11033              precise test would be better, as this one will set
11034              incorrect values for pasted .init/.fini fragments.
11035              (Fixed later in check_pasted_section.)  */
11036           if (isec->makes_toc_func_call
11037               && elf_gp (isec->owner) != 0)
11038             htab->toc_curr = elf_gp (isec->owner);
11039         }
11040     }
11041
11042   /* Functions that don't use the TOC can belong in any TOC group.
11043      Use the last TOC base.  */
11044   htab->stub_group[isec->id].toc_off = htab->toc_curr;
11045   return TRUE;
11046 }
11047
11048 /* Check that all .init and .fini sections use the same toc, if they
11049    have toc relocs.  */
11050
11051 static bfd_boolean
11052 check_pasted_section (struct bfd_link_info *info, const char *name)
11053 {
11054   asection *o = bfd_get_section_by_name (info->output_bfd, name);
11055
11056   if (o != NULL)
11057     {
11058       struct ppc_link_hash_table *htab = ppc_hash_table (info);
11059       bfd_vma toc_off = 0;
11060       asection *i;
11061
11062       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11063         if (i->has_toc_reloc)
11064           {
11065             if (toc_off == 0)
11066               toc_off = htab->stub_group[i->id].toc_off;
11067             else if (toc_off != htab->stub_group[i->id].toc_off)
11068               return FALSE;
11069           }
11070
11071       if (toc_off == 0)
11072         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11073           if (i->makes_toc_func_call)
11074             {
11075               toc_off = htab->stub_group[i->id].toc_off;
11076               break;
11077             }
11078
11079       /* Make sure the whole pasted function uses the same toc offset.  */
11080       if (toc_off != 0)
11081         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11082           htab->stub_group[i->id].toc_off = toc_off;
11083     }
11084   return TRUE;
11085 }
11086
11087 bfd_boolean
11088 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11089 {
11090   return (check_pasted_section (info, ".init")
11091           & check_pasted_section (info, ".fini"));
11092 }
11093
11094 /* See whether we can group stub sections together.  Grouping stub
11095    sections may result in fewer stubs.  More importantly, we need to
11096    put all .init* and .fini* stubs at the beginning of the .init or
11097    .fini output sections respectively, because glibc splits the
11098    _init and _fini functions into multiple parts.  Putting a stub in
11099    the middle of a function is not a good idea.  */
11100
11101 static void
11102 group_sections (struct ppc_link_hash_table *htab,
11103                 bfd_size_type stub_group_size,
11104                 bfd_boolean stubs_always_before_branch)
11105 {
11106   asection **list;
11107   bfd_size_type stub14_group_size;
11108   bfd_boolean suppress_size_errors;
11109
11110   suppress_size_errors = FALSE;
11111   stub14_group_size = stub_group_size;
11112   if (stub_group_size == 1)
11113     {
11114       /* Default values.  */
11115       if (stubs_always_before_branch)
11116         {
11117           stub_group_size = 0x1e00000;
11118           stub14_group_size = 0x7800;
11119         }
11120       else
11121         {
11122           stub_group_size = 0x1c00000;
11123           stub14_group_size = 0x7000;
11124         }
11125       suppress_size_errors = TRUE;
11126     }
11127
11128   list = htab->input_list + htab->top_index;
11129   do
11130     {
11131       asection *tail = *list;
11132       while (tail != NULL)
11133         {
11134           asection *curr;
11135           asection *prev;
11136           bfd_size_type total;
11137           bfd_boolean big_sec;
11138           bfd_vma curr_toc;
11139
11140           curr = tail;
11141           total = tail->size;
11142           big_sec = total > (ppc64_elf_section_data (tail) != NULL
11143                              && ppc64_elf_section_data (tail)->has_14bit_branch
11144                              ? stub14_group_size : stub_group_size);
11145           if (big_sec && !suppress_size_errors)
11146             (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11147                                      tail->owner, tail);
11148           curr_toc = htab->stub_group[tail->id].toc_off;
11149
11150           while ((prev = PREV_SEC (curr)) != NULL
11151                  && ((total += curr->output_offset - prev->output_offset)
11152                      < (ppc64_elf_section_data (prev) != NULL
11153                         && ppc64_elf_section_data (prev)->has_14bit_branch
11154                         ? stub14_group_size : stub_group_size))
11155                  && htab->stub_group[prev->id].toc_off == curr_toc)
11156             curr = prev;
11157
11158           /* OK, the size from the start of CURR to the end is less
11159              than stub_group_size and thus can be handled by one stub
11160              section.  (or the tail section is itself larger than
11161              stub_group_size, in which case we may be toast.)  We
11162              should really be keeping track of the total size of stubs
11163              added here, as stubs contribute to the final output
11164              section size.  That's a little tricky, and this way will
11165              only break if stubs added make the total size more than
11166              2^25, ie. for the default stub_group_size, if stubs total
11167              more than 2097152 bytes, or nearly 75000 plt call stubs.  */
11168           do
11169             {
11170               prev = PREV_SEC (tail);
11171               /* Set up this stub group.  */
11172               htab->stub_group[tail->id].link_sec = curr;
11173             }
11174           while (tail != curr && (tail = prev) != NULL);
11175
11176           /* But wait, there's more!  Input sections up to stub_group_size
11177              bytes before the stub section can be handled by it too.
11178              Don't do this if we have a really large section after the
11179              stubs, as adding more stubs increases the chance that
11180              branches may not reach into the stub section.  */
11181           if (!stubs_always_before_branch && !big_sec)
11182             {
11183               total = 0;
11184               while (prev != NULL
11185                      && ((total += tail->output_offset - prev->output_offset)
11186                          < (ppc64_elf_section_data (prev) != NULL
11187                             && ppc64_elf_section_data (prev)->has_14bit_branch
11188                             ? stub14_group_size : stub_group_size))
11189                      && htab->stub_group[prev->id].toc_off == curr_toc)
11190                 {
11191                   tail = prev;
11192                   prev = PREV_SEC (tail);
11193                   htab->stub_group[tail->id].link_sec = curr;
11194                 }
11195             }
11196           tail = prev;
11197         }
11198     }
11199   while (list-- != htab->input_list);
11200   free (htab->input_list);
11201 #undef PREV_SEC
11202 }
11203
11204 static const unsigned char glink_eh_frame_cie[] =
11205 {
11206   0, 0, 0, 16,                          /* length.  */
11207   0, 0, 0, 0,                           /* id.  */
11208   1,                                    /* CIE version.  */
11209   'z', 'R', 0,                          /* Augmentation string.  */
11210   4,                                    /* Code alignment.  */
11211   0x78,                                 /* Data alignment.  */
11212   65,                                   /* RA reg.  */
11213   1,                                    /* Augmentation size.  */
11214   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
11215   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
11216 };
11217
11218 /* Stripping output sections is normally done before dynamic section
11219    symbols have been allocated.  This function is called later, and
11220    handles cases like htab->brlt which is mapped to its own output
11221    section.  */
11222
11223 static void
11224 maybe_strip_output (struct bfd_link_info *info, asection *isec)
11225 {
11226   if (isec->size == 0
11227       && isec->output_section->size == 0
11228       && !bfd_section_removed_from_list (info->output_bfd,
11229                                          isec->output_section)
11230       && elf_section_data (isec->output_section)->dynindx == 0)
11231     {
11232       isec->output_section->flags |= SEC_EXCLUDE;
11233       bfd_section_list_remove (info->output_bfd, isec->output_section);
11234       info->output_bfd->section_count--;
11235     }
11236 }
11237
11238 /* Determine and set the size of the stub section for a final link.
11239
11240    The basic idea here is to examine all the relocations looking for
11241    PC-relative calls to a target that is unreachable with a "bl"
11242    instruction.  */
11243
11244 bfd_boolean
11245 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
11246                       bfd_boolean plt_static_chain, int plt_thread_safe,
11247                       int plt_stub_align)
11248 {
11249   bfd_size_type stub_group_size;
11250   bfd_boolean stubs_always_before_branch;
11251   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11252
11253   if (htab == NULL)
11254     return FALSE;
11255
11256   htab->plt_static_chain = plt_static_chain;
11257   htab->plt_stub_align = plt_stub_align;
11258   if (plt_thread_safe == -1)
11259     {
11260       const char *const thread_starter[] =
11261         {
11262           "pthread_create",
11263           /* libstdc++ */
11264           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11265           /* librt */
11266           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11267           "mq_notify", "create_timer",
11268           /* libanl */
11269           "getaddrinfo_a",
11270           /* libgomp */
11271           "GOMP_parallel_start",
11272           "GOMP_parallel_loop_static_start",
11273           "GOMP_parallel_loop_dynamic_start",
11274           "GOMP_parallel_loop_guided_start",
11275           "GOMP_parallel_loop_runtime_start",
11276           "GOMP_parallel_sections_start", 
11277         };
11278       unsigned i;
11279
11280       for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
11281         {
11282           struct elf_link_hash_entry *h;
11283           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11284                                     FALSE, FALSE, TRUE);
11285           plt_thread_safe = h != NULL && h->ref_regular;
11286           if (plt_thread_safe)
11287             break;
11288         }
11289     }
11290   htab->plt_thread_safe = plt_thread_safe;
11291   stubs_always_before_branch = group_size < 0;
11292   if (group_size < 0)
11293     stub_group_size = -group_size;
11294   else
11295     stub_group_size = group_size;
11296
11297   group_sections (htab, stub_group_size, stubs_always_before_branch);
11298
11299   while (1)
11300     {
11301       bfd *input_bfd;
11302       unsigned int bfd_indx;
11303       asection *stub_sec;
11304
11305       htab->stub_iteration += 1;
11306
11307       for (input_bfd = info->input_bfds, bfd_indx = 0;
11308            input_bfd != NULL;
11309            input_bfd = input_bfd->link_next, bfd_indx++)
11310         {
11311           Elf_Internal_Shdr *symtab_hdr;
11312           asection *section;
11313           Elf_Internal_Sym *local_syms = NULL;
11314
11315           if (!is_ppc64_elf (input_bfd))
11316             continue;
11317
11318           /* We'll need the symbol table in a second.  */
11319           symtab_hdr = &elf_symtab_hdr (input_bfd);
11320           if (symtab_hdr->sh_info == 0)
11321             continue;
11322
11323           /* Walk over each section attached to the input bfd.  */
11324           for (section = input_bfd->sections;
11325                section != NULL;
11326                section = section->next)
11327             {
11328               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
11329
11330               /* If there aren't any relocs, then there's nothing more
11331                  to do.  */
11332               if ((section->flags & SEC_RELOC) == 0
11333                   || (section->flags & SEC_ALLOC) == 0
11334                   || (section->flags & SEC_LOAD) == 0
11335                   || (section->flags & SEC_CODE) == 0
11336                   || section->reloc_count == 0)
11337                 continue;
11338
11339               /* If this section is a link-once section that will be
11340                  discarded, then don't create any stubs.  */
11341               if (section->output_section == NULL
11342                   || section->output_section->owner != info->output_bfd)
11343                 continue;
11344
11345               /* Get the relocs.  */
11346               internal_relocs
11347                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
11348                                              info->keep_memory);
11349               if (internal_relocs == NULL)
11350                 goto error_ret_free_local;
11351
11352               /* Now examine each relocation.  */
11353               irela = internal_relocs;
11354               irelaend = irela + section->reloc_count;
11355               for (; irela < irelaend; irela++)
11356                 {
11357                   enum elf_ppc64_reloc_type r_type;
11358                   unsigned int r_indx;
11359                   enum ppc_stub_type stub_type;
11360                   struct ppc_stub_hash_entry *stub_entry;
11361                   asection *sym_sec, *code_sec;
11362                   bfd_vma sym_value, code_value;
11363                   bfd_vma destination;
11364                   bfd_boolean ok_dest;
11365                   struct ppc_link_hash_entry *hash;
11366                   struct ppc_link_hash_entry *fdh;
11367                   struct elf_link_hash_entry *h;
11368                   Elf_Internal_Sym *sym;
11369                   char *stub_name;
11370                   const asection *id_sec;
11371                   struct _opd_sec_data *opd;
11372                   struct plt_entry *plt_ent;
11373
11374                   r_type = ELF64_R_TYPE (irela->r_info);
11375                   r_indx = ELF64_R_SYM (irela->r_info);
11376
11377                   if (r_type >= R_PPC64_max)
11378                     {
11379                       bfd_set_error (bfd_error_bad_value);
11380                       goto error_ret_free_internal;
11381                     }
11382
11383                   /* Only look for stubs on branch instructions.  */
11384                   if (r_type != R_PPC64_REL24
11385                       && r_type != R_PPC64_REL14
11386                       && r_type != R_PPC64_REL14_BRTAKEN
11387                       && r_type != R_PPC64_REL14_BRNTAKEN)
11388                     continue;
11389
11390                   /* Now determine the call target, its name, value,
11391                      section.  */
11392                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11393                                   r_indx, input_bfd))
11394                     goto error_ret_free_internal;
11395                   hash = (struct ppc_link_hash_entry *) h;
11396
11397                   ok_dest = FALSE;
11398                   fdh = NULL;
11399                   sym_value = 0;
11400                   if (hash == NULL)
11401                     {
11402                       sym_value = sym->st_value;
11403                       ok_dest = TRUE;
11404                     }
11405                   else if (hash->elf.root.type == bfd_link_hash_defined
11406                            || hash->elf.root.type == bfd_link_hash_defweak)
11407                     {
11408                       sym_value = hash->elf.root.u.def.value;
11409                       if (sym_sec->output_section != NULL)
11410                         ok_dest = TRUE;
11411                     }
11412                   else if (hash->elf.root.type == bfd_link_hash_undefweak
11413                            || hash->elf.root.type == bfd_link_hash_undefined)
11414                     {
11415                       /* Recognise an old ABI func code entry sym, and
11416                          use the func descriptor sym instead if it is
11417                          defined.  */
11418                       if (hash->elf.root.root.string[0] == '.'
11419                           && (fdh = lookup_fdh (hash, htab)) != NULL)
11420                         {
11421                           if (fdh->elf.root.type == bfd_link_hash_defined
11422                               || fdh->elf.root.type == bfd_link_hash_defweak)
11423                             {
11424                               sym_sec = fdh->elf.root.u.def.section;
11425                               sym_value = fdh->elf.root.u.def.value;
11426                               if (sym_sec->output_section != NULL)
11427                                 ok_dest = TRUE;
11428                             }
11429                           else
11430                             fdh = NULL;
11431                         }
11432                     }
11433                   else
11434                     {
11435                       bfd_set_error (bfd_error_bad_value);
11436                       goto error_ret_free_internal;
11437                     }
11438
11439                   destination = 0;
11440                   if (ok_dest)
11441                     {
11442                       sym_value += irela->r_addend;
11443                       destination = (sym_value
11444                                      + sym_sec->output_offset
11445                                      + sym_sec->output_section->vma);
11446                     }
11447
11448                   code_sec = sym_sec;
11449                   code_value = sym_value;
11450                   opd = get_opd_info (sym_sec);
11451                   if (opd != NULL)
11452                     {
11453                       bfd_vma dest;
11454
11455                       if (hash == NULL && opd->adjust != NULL)
11456                         {
11457                           long adjust = opd->adjust[sym_value / 8];
11458                           if (adjust == -1)
11459                             continue;
11460                           code_value += adjust;
11461                           sym_value += adjust;
11462                         }
11463                       dest = opd_entry_value (sym_sec, sym_value,
11464                                               &code_sec, &code_value);
11465                       if (dest != (bfd_vma) -1)
11466                         {
11467                           destination = dest;
11468                           if (fdh != NULL)
11469                             {
11470                               /* Fixup old ABI sym to point at code
11471                                  entry.  */
11472                               hash->elf.root.type = bfd_link_hash_defweak;
11473                               hash->elf.root.u.def.section = code_sec;
11474                               hash->elf.root.u.def.value = code_value;
11475                             }
11476                         }
11477                     }
11478
11479                   /* Determine what (if any) linker stub is needed.  */
11480                   plt_ent = NULL;
11481                   stub_type = ppc_type_of_stub (section, irela, &hash,
11482                                                 &plt_ent, destination);
11483
11484                   if (stub_type != ppc_stub_plt_call)
11485                     {
11486                       /* Check whether we need a TOC adjusting stub.
11487                          Since the linker pastes together pieces from
11488                          different object files when creating the
11489                          _init and _fini functions, it may be that a
11490                          call to what looks like a local sym is in
11491                          fact a call needing a TOC adjustment.  */
11492                       if (code_sec != NULL
11493                           && code_sec->output_section != NULL
11494                           && (htab->stub_group[code_sec->id].toc_off
11495                               != htab->stub_group[section->id].toc_off)
11496                           && (code_sec->has_toc_reloc
11497                               || code_sec->makes_toc_func_call))
11498                         stub_type = ppc_stub_long_branch_r2off;
11499                     }
11500
11501                   if (stub_type == ppc_stub_none)
11502                     continue;
11503
11504                   /* __tls_get_addr calls might be eliminated.  */
11505                   if (stub_type != ppc_stub_plt_call
11506                       && hash != NULL
11507                       && (hash == htab->tls_get_addr
11508                           || hash == htab->tls_get_addr_fd)
11509                       && section->has_tls_reloc
11510                       && irela != internal_relocs)
11511                     {
11512                       /* Get tls info.  */
11513                       unsigned char *tls_mask;
11514
11515                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
11516                                          irela - 1, input_bfd))
11517                         goto error_ret_free_internal;
11518                       if (*tls_mask != 0)
11519                         continue;
11520                     }
11521
11522                   if (stub_type == ppc_stub_plt_call
11523                       && irela + 1 < irelaend
11524                       && irela[1].r_offset == irela->r_offset + 4
11525                       && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
11526                     {
11527                       if (!tocsave_find (htab, INSERT,
11528                                          &local_syms, irela + 1, input_bfd))
11529                         goto error_ret_free_internal;
11530                     }
11531                   else if (stub_type == ppc_stub_plt_call)
11532                     stub_type = ppc_stub_plt_call_r2save;
11533
11534                   /* Support for grouping stub sections.  */
11535                   id_sec = htab->stub_group[section->id].link_sec;
11536
11537                   /* Get the name of this stub.  */
11538                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11539                   if (!stub_name)
11540                     goto error_ret_free_internal;
11541
11542                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
11543                                                      stub_name, FALSE, FALSE);
11544                   if (stub_entry != NULL)
11545                     {
11546                       /* The proper stub has already been created.  */
11547                       free (stub_name);
11548                       if (stub_type == ppc_stub_plt_call_r2save)
11549                         stub_entry->stub_type = stub_type;
11550                       continue;
11551                     }
11552
11553                   stub_entry = ppc_add_stub (stub_name, section, info);
11554                   if (stub_entry == NULL)
11555                     {
11556                       free (stub_name);
11557                     error_ret_free_internal:
11558                       if (elf_section_data (section)->relocs == NULL)
11559                         free (internal_relocs);
11560                     error_ret_free_local:
11561                       if (local_syms != NULL
11562                           && (symtab_hdr->contents
11563                               != (unsigned char *) local_syms))
11564                         free (local_syms);
11565                       return FALSE;
11566                     }
11567
11568                   stub_entry->stub_type = stub_type;
11569                   if (stub_type != ppc_stub_plt_call
11570                       && stub_type != ppc_stub_plt_call_r2save)
11571                     {
11572                       stub_entry->target_value = code_value;
11573                       stub_entry->target_section = code_sec;
11574                     }
11575                   else
11576                     {
11577                       stub_entry->target_value = sym_value;
11578                       stub_entry->target_section = sym_sec;
11579                     }
11580                   stub_entry->h = hash;
11581                   stub_entry->plt_ent = plt_ent;
11582                   stub_entry->addend = irela->r_addend;
11583
11584                   if (stub_entry->h != NULL)
11585                     htab->stub_globals += 1;
11586                 }
11587
11588               /* We're done with the internal relocs, free them.  */
11589               if (elf_section_data (section)->relocs != internal_relocs)
11590                 free (internal_relocs);
11591             }
11592
11593           if (local_syms != NULL
11594               && symtab_hdr->contents != (unsigned char *) local_syms)
11595             {
11596               if (!info->keep_memory)
11597                 free (local_syms);
11598               else
11599                 symtab_hdr->contents = (unsigned char *) local_syms;
11600             }
11601         }
11602
11603       /* We may have added some stubs.  Find out the new size of the
11604          stub sections.  */
11605       for (stub_sec = htab->stub_bfd->sections;
11606            stub_sec != NULL;
11607            stub_sec = stub_sec->next)
11608         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11609           {
11610             stub_sec->rawsize = stub_sec->size;
11611             stub_sec->size = 0;
11612             stub_sec->reloc_count = 0;
11613             stub_sec->flags &= ~SEC_RELOC;
11614           }
11615
11616       htab->brlt->size = 0;
11617       htab->brlt->reloc_count = 0;
11618       htab->brlt->flags &= ~SEC_RELOC;
11619       if (htab->relbrlt != NULL)
11620         htab->relbrlt->size = 0;
11621
11622       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
11623
11624       if (info->emitrelocations
11625           && htab->glink != NULL && htab->glink->size != 0)
11626         {
11627           htab->glink->reloc_count = 1;
11628           htab->glink->flags |= SEC_RELOC;
11629         }
11630
11631       if (htab->glink_eh_frame != NULL
11632           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
11633           && (htab->glink_eh_frame->flags & SEC_EXCLUDE) == 0)
11634         {
11635           bfd_size_type size = 0;
11636
11637           for (stub_sec = htab->stub_bfd->sections;
11638                stub_sec != NULL;
11639                stub_sec = stub_sec->next)
11640             if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11641               size += 20;
11642           if (htab->glink != NULL && htab->glink->size != 0)
11643             size += 24;
11644           if (size != 0)
11645             size += sizeof (glink_eh_frame_cie);
11646           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
11647           htab->glink_eh_frame->size = size;
11648         }
11649
11650       if (htab->plt_stub_align != 0)
11651         for (stub_sec = htab->stub_bfd->sections;
11652              stub_sec != NULL;
11653              stub_sec = stub_sec->next)
11654           if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11655             stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
11656                               & (-1 << htab->plt_stub_align));
11657
11658       for (stub_sec = htab->stub_bfd->sections;
11659            stub_sec != NULL;
11660            stub_sec = stub_sec->next)
11661         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11662             && stub_sec->rawsize != stub_sec->size)
11663           break;
11664
11665       /* Exit from this loop when no stubs have been added, and no stubs
11666          have changed size.  */
11667       if (stub_sec == NULL
11668           && (htab->glink_eh_frame == NULL
11669               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
11670         break;
11671
11672       /* Ask the linker to do its stuff.  */
11673       (*htab->layout_sections_again) ();
11674     }
11675
11676   maybe_strip_output (info, htab->brlt);
11677   if (htab->glink_eh_frame != NULL)
11678     maybe_strip_output (info, htab->glink_eh_frame);
11679
11680   return TRUE;
11681 }
11682
11683 /* Called after we have determined section placement.  If sections
11684    move, we'll be called again.  Provide a value for TOCstart.  */
11685
11686 bfd_vma
11687 ppc64_elf_toc (bfd *obfd)
11688 {
11689   asection *s;
11690   bfd_vma TOCstart;
11691
11692   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11693      order.  The TOC starts where the first of these sections starts.  */
11694   s = bfd_get_section_by_name (obfd, ".got");
11695   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11696     s = bfd_get_section_by_name (obfd, ".toc");
11697   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11698     s = bfd_get_section_by_name (obfd, ".tocbss");
11699   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11700     s = bfd_get_section_by_name (obfd, ".plt");
11701   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11702     {
11703       /* This may happen for
11704          o  references to TOC base (SYM@toc / TOC[tc0]) without a
11705          .toc directive
11706          o  bad linker script
11707          o --gc-sections and empty TOC sections
11708
11709          FIXME: Warn user?  */
11710
11711       /* Look for a likely section.  We probably won't even be
11712          using TOCstart.  */
11713       for (s = obfd->sections; s != NULL; s = s->next)
11714         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11715                          | SEC_EXCLUDE))
11716             == (SEC_ALLOC | SEC_SMALL_DATA))
11717           break;
11718       if (s == NULL)
11719         for (s = obfd->sections; s != NULL; s = s->next)
11720           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
11721               == (SEC_ALLOC | SEC_SMALL_DATA))
11722             break;
11723       if (s == NULL)
11724         for (s = obfd->sections; s != NULL; s = s->next)
11725           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11726               == SEC_ALLOC)
11727             break;
11728       if (s == NULL)
11729         for (s = obfd->sections; s != NULL; s = s->next)
11730           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
11731             break;
11732     }
11733
11734   TOCstart = 0;
11735   if (s != NULL)
11736     TOCstart = s->output_section->vma + s->output_offset;
11737
11738   return TOCstart;
11739 }
11740
11741 /* Build all the stubs associated with the current output file.
11742    The stubs are kept in a hash table attached to the main linker
11743    hash table.  This function is called via gldelf64ppc_finish.  */
11744
11745 bfd_boolean
11746 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11747                        struct bfd_link_info *info,
11748                        char **stats)
11749 {
11750   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11751   asection *stub_sec;
11752   bfd_byte *p;
11753   int stub_sec_count = 0;
11754
11755   if (htab == NULL)
11756     return FALSE;
11757
11758   htab->emit_stub_syms = emit_stub_syms;
11759
11760   /* Allocate memory to hold the linker stubs.  */
11761   for (stub_sec = htab->stub_bfd->sections;
11762        stub_sec != NULL;
11763        stub_sec = stub_sec->next)
11764     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11765         && stub_sec->size != 0)
11766       {
11767         stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11768         if (stub_sec->contents == NULL)
11769           return FALSE;
11770         /* We want to check that built size is the same as calculated
11771            size.  rawsize is a convenient location to use.  */
11772         stub_sec->rawsize = stub_sec->size;
11773         stub_sec->size = 0;
11774       }
11775
11776   if (htab->glink != NULL && htab->glink->size != 0)
11777     {
11778       unsigned int indx;
11779       bfd_vma plt0;
11780
11781       /* Build the .glink plt call stub.  */
11782       if (htab->emit_stub_syms)
11783         {
11784           struct elf_link_hash_entry *h;
11785           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11786                                     TRUE, FALSE, FALSE);
11787           if (h == NULL)
11788             return FALSE;
11789           if (h->root.type == bfd_link_hash_new)
11790             {
11791               h->root.type = bfd_link_hash_defined;
11792               h->root.u.def.section = htab->glink;
11793               h->root.u.def.value = 8;
11794               h->ref_regular = 1;
11795               h->def_regular = 1;
11796               h->ref_regular_nonweak = 1;
11797               h->forced_local = 1;
11798               h->non_elf = 0;
11799             }
11800         }
11801       plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11802       if (info->emitrelocations)
11803         {
11804           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11805           if (r == NULL)
11806             return FALSE;
11807           r->r_offset = (htab->glink->output_offset
11808                          + htab->glink->output_section->vma);
11809           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11810           r->r_addend = plt0;
11811         }
11812       p = htab->glink->contents;
11813       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
11814       bfd_put_64 (htab->glink->owner, plt0, p);
11815       p += 8;
11816       bfd_put_32 (htab->glink->owner, MFLR_R12, p);
11817       p += 4;
11818       bfd_put_32 (htab->glink->owner, BCL_20_31, p);
11819       p += 4;
11820       bfd_put_32 (htab->glink->owner, MFLR_R11, p);
11821       p += 4;
11822       bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
11823       p += 4;
11824       bfd_put_32 (htab->glink->owner, MTLR_R12, p);
11825       p += 4;
11826       bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
11827       p += 4;
11828       bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
11829       p += 4;
11830       bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
11831       p += 4;
11832       bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
11833       p += 4;
11834       bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
11835       p += 4;
11836       bfd_put_32 (htab->glink->owner, BCTR, p);
11837       p += 4;
11838       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11839         {
11840           bfd_put_32 (htab->glink->owner, NOP, p);
11841           p += 4;
11842         }
11843
11844       /* Build the .glink lazy link call stubs.  */
11845       indx = 0;
11846       while (p < htab->glink->contents + htab->glink->size)
11847         {
11848           if (indx < 0x8000)
11849             {
11850               bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
11851               p += 4;
11852             }
11853           else
11854             {
11855               bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
11856               p += 4;
11857               bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
11858               p += 4;
11859             }
11860           bfd_put_32 (htab->glink->owner,
11861                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
11862           indx++;
11863           p += 4;
11864         }
11865       htab->glink->rawsize = p - htab->glink->contents;
11866     }
11867
11868   if (htab->brlt->size != 0)
11869     {
11870       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
11871                                          htab->brlt->size);
11872       if (htab->brlt->contents == NULL)
11873         return FALSE;
11874     }
11875   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
11876     {
11877       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
11878                                             htab->relbrlt->size);
11879       if (htab->relbrlt->contents == NULL)
11880         return FALSE;
11881     }
11882
11883   if (htab->glink_eh_frame != NULL
11884       && htab->glink_eh_frame->size != 0)
11885     {
11886       bfd_vma val;
11887
11888       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
11889       if (p == NULL)
11890         return FALSE;
11891       htab->glink_eh_frame->contents = p;
11892
11893       htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
11894
11895       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
11896       /* CIE length (rewrite in case little-endian).  */
11897       bfd_put_32 (htab->elf.dynobj, sizeof (glink_eh_frame_cie) - 4, p);
11898       p += sizeof (glink_eh_frame_cie);
11899
11900       for (stub_sec = htab->stub_bfd->sections;
11901            stub_sec != NULL;
11902            stub_sec = stub_sec->next)
11903         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11904           {
11905             /* FDE length.  */
11906             bfd_put_32 (htab->elf.dynobj, 16, p);
11907             p += 4;
11908             /* CIE pointer.  */
11909             val = p - htab->glink_eh_frame->contents;
11910             bfd_put_32 (htab->elf.dynobj, val, p);
11911             p += 4;
11912             /* Offset to stub section.  */
11913             val = (stub_sec->output_section->vma
11914                    + stub_sec->output_offset);
11915             val -= (htab->glink_eh_frame->output_section->vma
11916                     + htab->glink_eh_frame->output_offset);
11917             val -= p - htab->glink_eh_frame->contents;
11918             if (val + 0x80000000 > 0xffffffff)
11919               {
11920                 info->callbacks->einfo
11921                   (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
11922                    stub_sec->name);
11923                 return FALSE;
11924               }
11925             bfd_put_32 (htab->elf.dynobj, val, p);
11926             p += 4;
11927             /* stub section size.  */
11928             bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
11929             p += 4;
11930             /* Augmentation.  */
11931             p += 1;
11932             /* Pad.  */
11933             p += 3;
11934           }
11935       if (htab->glink != NULL && htab->glink->size != 0)
11936         {
11937           /* FDE length.  */
11938           bfd_put_32 (htab->elf.dynobj, 20, p);
11939           p += 4;
11940           /* CIE pointer.  */
11941           val = p - htab->glink_eh_frame->contents;
11942           bfd_put_32 (htab->elf.dynobj, val, p);
11943           p += 4;
11944           /* Offset to .glink.  */
11945           val = (htab->glink->output_section->vma
11946                  + htab->glink->output_offset
11947                  + 8);
11948           val -= (htab->glink_eh_frame->output_section->vma
11949                   + htab->glink_eh_frame->output_offset);
11950           val -= p - htab->glink_eh_frame->contents;
11951           if (val + 0x80000000 > 0xffffffff)
11952             {
11953               info->callbacks->einfo
11954                 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
11955                  htab->glink->name);
11956               return FALSE;
11957             }
11958           bfd_put_32 (htab->elf.dynobj, val, p);
11959           p += 4;
11960           /* .glink size.  */
11961           bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
11962           p += 4;
11963           /* Augmentation.  */
11964           p += 1;
11965
11966           *p++ = DW_CFA_advance_loc + 1;
11967           *p++ = DW_CFA_register;
11968           *p++ = 65;
11969           *p++ = 12;
11970           *p++ = DW_CFA_advance_loc + 4;
11971           *p++ = DW_CFA_restore_extended;
11972           *p++ = 65;
11973         }
11974       htab->glink_eh_frame->size = p - htab->glink_eh_frame->contents;
11975     }
11976
11977   /* Build the stubs as directed by the stub hash table.  */
11978   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
11979
11980   if (htab->relbrlt != NULL)
11981     htab->relbrlt->reloc_count = 0;
11982
11983   if (htab->plt_stub_align != 0)
11984     for (stub_sec = htab->stub_bfd->sections;
11985          stub_sec != NULL;
11986          stub_sec = stub_sec->next)
11987       if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11988         stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
11989                           & (-1 << htab->plt_stub_align));
11990
11991   for (stub_sec = htab->stub_bfd->sections;
11992        stub_sec != NULL;
11993        stub_sec = stub_sec->next)
11994     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11995       {
11996         stub_sec_count += 1;
11997         if (stub_sec->rawsize != stub_sec->size)
11998           break;
11999       }
12000
12001   if (stub_sec != NULL
12002       || htab->glink->rawsize != htab->glink->size
12003       || (htab->glink_eh_frame != NULL
12004           && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
12005     {
12006       htab->stub_error = TRUE;
12007       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
12008     }
12009
12010   if (htab->stub_error)
12011     return FALSE;
12012
12013   if (stats != NULL)
12014     {
12015       *stats = bfd_malloc (500);
12016       if (*stats == NULL)
12017         return FALSE;
12018
12019       sprintf (*stats, _("linker stubs in %u group%s\n"
12020                          "  branch       %lu\n"
12021                          "  toc adjust   %lu\n"
12022                          "  long branch  %lu\n"
12023                          "  long toc adj %lu\n"
12024                          "  plt call     %lu\n"
12025                          "  plt call toc %lu"),
12026                stub_sec_count,
12027                stub_sec_count == 1 ? "" : "s",
12028                htab->stub_count[ppc_stub_long_branch - 1],
12029                htab->stub_count[ppc_stub_long_branch_r2off - 1],
12030                htab->stub_count[ppc_stub_plt_branch - 1],
12031                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
12032                htab->stub_count[ppc_stub_plt_call - 1],
12033                htab->stub_count[ppc_stub_plt_call_r2save - 1]);
12034     }
12035   return TRUE;
12036 }
12037
12038 /* This function undoes the changes made by add_symbol_adjust.  */
12039
12040 static bfd_boolean
12041 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12042 {
12043   struct ppc_link_hash_entry *eh;
12044
12045   if (h->root.type == bfd_link_hash_indirect)
12046     return TRUE;
12047
12048   eh = (struct ppc_link_hash_entry *) h;
12049   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
12050     return TRUE;
12051
12052   eh->elf.root.type = bfd_link_hash_undefined;
12053   return TRUE;
12054 }
12055
12056 void
12057 ppc64_elf_restore_symbols (struct bfd_link_info *info)
12058 {
12059   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12060
12061   if (htab != NULL)
12062     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
12063 }
12064
12065 /* What to do when ld finds relocations against symbols defined in
12066    discarded sections.  */
12067
12068 static unsigned int
12069 ppc64_elf_action_discarded (asection *sec)
12070 {
12071   if (strcmp (".opd", sec->name) == 0)
12072     return 0;
12073
12074   if (strcmp (".toc", sec->name) == 0)
12075     return 0;
12076
12077   if (strcmp (".toc1", sec->name) == 0)
12078     return 0;
12079
12080   return _bfd_elf_default_action_discarded (sec);
12081 }
12082
12083 /* The RELOCATE_SECTION function is called by the ELF backend linker
12084    to handle the relocations for a section.
12085
12086    The relocs are always passed as Rela structures; if the section
12087    actually uses Rel structures, the r_addend field will always be
12088    zero.
12089
12090    This function is responsible for adjust the section contents as
12091    necessary, and (if using Rela relocs and generating a
12092    relocatable output file) adjusting the reloc addend as
12093    necessary.
12094
12095    This function does not have to worry about setting the reloc
12096    address or the reloc symbol index.
12097
12098    LOCAL_SYMS is a pointer to the swapped in local symbols.
12099
12100    LOCAL_SECTIONS is an array giving the section in the input file
12101    corresponding to the st_shndx field of each local symbol.
12102
12103    The global hash table entry for the global symbols can be found
12104    via elf_sym_hashes (input_bfd).
12105
12106    When generating relocatable output, this function must handle
12107    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
12108    going to be the section symbol corresponding to the output
12109    section, which means that the addend must be adjusted
12110    accordingly.  */
12111
12112 static bfd_boolean
12113 ppc64_elf_relocate_section (bfd *output_bfd,
12114                             struct bfd_link_info *info,
12115                             bfd *input_bfd,
12116                             asection *input_section,
12117                             bfd_byte *contents,
12118                             Elf_Internal_Rela *relocs,
12119                             Elf_Internal_Sym *local_syms,
12120                             asection **local_sections)
12121 {
12122   struct ppc_link_hash_table *htab;
12123   Elf_Internal_Shdr *symtab_hdr;
12124   struct elf_link_hash_entry **sym_hashes;
12125   Elf_Internal_Rela *rel;
12126   Elf_Internal_Rela *relend;
12127   Elf_Internal_Rela outrel;
12128   bfd_byte *loc;
12129   struct got_entry **local_got_ents;
12130   bfd_vma TOCstart;
12131   bfd_boolean ret = TRUE;
12132   bfd_boolean is_opd;
12133   /* Assume 'at' branch hints.  */
12134   bfd_boolean is_isa_v2 = TRUE;
12135   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
12136
12137   /* Initialize howto table if needed.  */
12138   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
12139     ppc_howto_init ();
12140
12141   htab = ppc_hash_table (info);
12142   if (htab == NULL)
12143     return FALSE;
12144
12145   /* Don't relocate stub sections.  */
12146   if (input_section->owner == htab->stub_bfd)
12147     return TRUE;
12148
12149   BFD_ASSERT (is_ppc64_elf (input_bfd));
12150
12151   local_got_ents = elf_local_got_ents (input_bfd);
12152   TOCstart = elf_gp (output_bfd);
12153   symtab_hdr = &elf_symtab_hdr (input_bfd);
12154   sym_hashes = elf_sym_hashes (input_bfd);
12155   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
12156
12157   rel = relocs;
12158   relend = relocs + input_section->reloc_count;
12159   for (; rel < relend; rel++)
12160     {
12161       enum elf_ppc64_reloc_type r_type;
12162       bfd_vma addend, orig_addend;
12163       bfd_reloc_status_type r;
12164       Elf_Internal_Sym *sym;
12165       asection *sec;
12166       struct elf_link_hash_entry *h_elf;
12167       struct ppc_link_hash_entry *h;
12168       struct ppc_link_hash_entry *fdh;
12169       const char *sym_name;
12170       unsigned long r_symndx, toc_symndx;
12171       bfd_vma toc_addend;
12172       unsigned char tls_mask, tls_gd, tls_type;
12173       unsigned char sym_type;
12174       bfd_vma relocation;
12175       bfd_boolean unresolved_reloc;
12176       bfd_boolean warned;
12177       unsigned int insn;
12178       unsigned int mask;
12179       struct ppc_stub_hash_entry *stub_entry;
12180       bfd_vma max_br_offset;
12181       bfd_vma from;
12182
12183       r_type = ELF64_R_TYPE (rel->r_info);
12184       r_symndx = ELF64_R_SYM (rel->r_info);
12185
12186       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
12187          symbol of the previous ADDR64 reloc.  The symbol gives us the
12188          proper TOC base to use.  */
12189       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
12190           && rel != relocs
12191           && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
12192           && is_opd)
12193         r_symndx = ELF64_R_SYM (rel[-1].r_info);
12194
12195       sym = NULL;
12196       sec = NULL;
12197       h_elf = NULL;
12198       sym_name = NULL;
12199       unresolved_reloc = FALSE;
12200       warned = FALSE;
12201       orig_addend = rel->r_addend;
12202
12203       if (r_symndx < symtab_hdr->sh_info)
12204         {
12205           /* It's a local symbol.  */
12206           struct _opd_sec_data *opd;
12207
12208           sym = local_syms + r_symndx;
12209           sec = local_sections[r_symndx];
12210           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
12211           sym_type = ELF64_ST_TYPE (sym->st_info);
12212           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
12213           opd = get_opd_info (sec);
12214           if (opd != NULL && opd->adjust != NULL)
12215             {
12216               long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
12217               if (adjust == -1)
12218                 relocation = 0;
12219               else
12220                 {
12221                   /* If this is a relocation against the opd section sym
12222                      and we have edited .opd, adjust the reloc addend so
12223                      that ld -r and ld --emit-relocs output is correct.
12224                      If it is a reloc against some other .opd symbol,
12225                      then the symbol value will be adjusted later.  */
12226                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
12227                     rel->r_addend += adjust;
12228                   else
12229                     relocation += adjust;
12230                 }
12231             }
12232         }
12233       else
12234         {
12235           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
12236                                    r_symndx, symtab_hdr, sym_hashes,
12237                                    h_elf, sec, relocation,
12238                                    unresolved_reloc, warned);
12239           sym_name = h_elf->root.root.string;
12240           sym_type = h_elf->type;
12241         }
12242       h = (struct ppc_link_hash_entry *) h_elf;
12243
12244       if (sec != NULL && elf_discarded_section (sec))
12245         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
12246                                          rel, relend,
12247                                          ppc64_elf_howto_table[r_type],
12248                                          contents);
12249
12250       if (info->relocatable)
12251         continue;
12252
12253       /* TLS optimizations.  Replace instruction sequences and relocs
12254          based on information we collected in tls_optimize.  We edit
12255          RELOCS so that --emit-relocs will output something sensible
12256          for the final instruction stream.  */
12257       tls_mask = 0;
12258       tls_gd = 0;
12259       toc_symndx = 0;
12260       if (h != NULL)
12261         tls_mask = h->tls_mask;
12262       else if (local_got_ents != NULL)
12263         {
12264           struct plt_entry **local_plt = (struct plt_entry **)
12265             (local_got_ents + symtab_hdr->sh_info);
12266           unsigned char *lgot_masks = (unsigned char *)
12267             (local_plt + symtab_hdr->sh_info);
12268           tls_mask = lgot_masks[r_symndx];
12269         }
12270       if (tls_mask == 0
12271           && (r_type == R_PPC64_TLS
12272               || r_type == R_PPC64_TLSGD
12273               || r_type == R_PPC64_TLSLD))
12274         {
12275           /* Check for toc tls entries.  */
12276           unsigned char *toc_tls;
12277
12278           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12279                              &local_syms, rel, input_bfd))
12280             return FALSE;
12281
12282           if (toc_tls)
12283             tls_mask = *toc_tls;
12284         }
12285
12286       /* Check that tls relocs are used with tls syms, and non-tls
12287          relocs are used with non-tls syms.  */
12288       if (r_symndx != STN_UNDEF
12289           && r_type != R_PPC64_NONE
12290           && (h == NULL
12291               || h->elf.root.type == bfd_link_hash_defined
12292               || h->elf.root.type == bfd_link_hash_defweak)
12293           && (IS_PPC64_TLS_RELOC (r_type)
12294               != (sym_type == STT_TLS
12295                   || (sym_type == STT_SECTION
12296                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
12297         {
12298           if (tls_mask != 0
12299               && (r_type == R_PPC64_TLS
12300                   || r_type == R_PPC64_TLSGD
12301                   || r_type == R_PPC64_TLSLD))
12302             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
12303             ;
12304           else
12305             info->callbacks->einfo
12306               (!IS_PPC64_TLS_RELOC (r_type)
12307                ? _("%P: %H: %s used with TLS symbol %s\n")
12308                : _("%P: %H: %s used with non-TLS symbol %s\n"),
12309                input_bfd, input_section, rel->r_offset,
12310                ppc64_elf_howto_table[r_type]->name,
12311                sym_name);
12312         }
12313
12314       /* Ensure reloc mapping code below stays sane.  */
12315       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
12316           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
12317           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
12318           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
12319           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
12320           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
12321           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
12322           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
12323           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
12324           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
12325         abort ();
12326
12327       switch (r_type)
12328         {
12329         default:
12330           break;
12331
12332         case R_PPC64_LO_DS_OPT:
12333           insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12334           if ((insn & (0x3f << 26)) != 58u << 26)
12335             abort ();
12336           insn += (14u << 26) - (58u << 26);
12337           bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12338           r_type = R_PPC64_TOC16_LO;
12339           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12340           break;
12341
12342         case R_PPC64_TOC16:
12343         case R_PPC64_TOC16_LO:
12344         case R_PPC64_TOC16_DS:
12345         case R_PPC64_TOC16_LO_DS:
12346           {
12347             /* Check for toc tls entries.  */
12348             unsigned char *toc_tls;
12349             int retval;
12350
12351             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12352                                    &local_syms, rel, input_bfd);
12353             if (retval == 0)
12354               return FALSE;
12355
12356             if (toc_tls)
12357               {
12358                 tls_mask = *toc_tls;
12359                 if (r_type == R_PPC64_TOC16_DS
12360                     || r_type == R_PPC64_TOC16_LO_DS)
12361                   {
12362                     if (tls_mask != 0
12363                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
12364                       goto toctprel;
12365                   }
12366                 else
12367                   {
12368                     /* If we found a GD reloc pair, then we might be
12369                        doing a GD->IE transition.  */
12370                     if (retval == 2)
12371                       {
12372                         tls_gd = TLS_TPRELGD;
12373                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12374                           goto tls_ldgd_opt;
12375                       }
12376                     else if (retval == 3)
12377                       {
12378                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12379                           goto tls_ldgd_opt;
12380                       }
12381                   }
12382               }
12383           }
12384           break;
12385
12386         case R_PPC64_GOT_TPREL16_HI:
12387         case R_PPC64_GOT_TPREL16_HA:
12388           if (tls_mask != 0
12389               && (tls_mask & TLS_TPREL) == 0)
12390             {
12391               rel->r_offset -= d_offset;
12392               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12393               r_type = R_PPC64_NONE;
12394               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12395             }
12396           break;
12397
12398         case R_PPC64_GOT_TPREL16_DS:
12399         case R_PPC64_GOT_TPREL16_LO_DS:
12400           if (tls_mask != 0
12401               && (tls_mask & TLS_TPREL) == 0)
12402             {
12403             toctprel:
12404               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12405               insn &= 31 << 21;
12406               insn |= 0x3c0d0000;       /* addis 0,13,0 */
12407               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12408               r_type = R_PPC64_TPREL16_HA;
12409               if (toc_symndx != 0)
12410                 {
12411                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
12412                   rel->r_addend = toc_addend;
12413                   /* We changed the symbol.  Start over in order to
12414                      get h, sym, sec etc. right.  */
12415                   rel--;
12416                   continue;
12417                 }
12418               else
12419                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12420             }
12421           break;
12422
12423         case R_PPC64_TLS:
12424           if (tls_mask != 0
12425               && (tls_mask & TLS_TPREL) == 0)
12426             {
12427               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
12428               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
12429               if (insn == 0)
12430                 abort ();
12431               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12432               /* Was PPC64_TLS which sits on insn boundary, now
12433                  PPC64_TPREL16_LO which is at low-order half-word.  */
12434               rel->r_offset += d_offset;
12435               r_type = R_PPC64_TPREL16_LO;
12436               if (toc_symndx != 0)
12437                 {
12438                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
12439                   rel->r_addend = toc_addend;
12440                   /* We changed the symbol.  Start over in order to
12441                      get h, sym, sec etc. right.  */
12442                   rel--;
12443                   continue;
12444                 }
12445               else
12446                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12447             }
12448           break;
12449
12450         case R_PPC64_GOT_TLSGD16_HI:
12451         case R_PPC64_GOT_TLSGD16_HA:
12452           tls_gd = TLS_TPRELGD;
12453           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12454             goto tls_gdld_hi;
12455           break;
12456
12457         case R_PPC64_GOT_TLSLD16_HI:
12458         case R_PPC64_GOT_TLSLD16_HA:
12459           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12460             {
12461             tls_gdld_hi:
12462               if ((tls_mask & tls_gd) != 0)
12463                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12464                           + R_PPC64_GOT_TPREL16_DS);
12465               else
12466                 {
12467                   rel->r_offset -= d_offset;
12468                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12469                   r_type = R_PPC64_NONE;
12470                 }
12471               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12472             }
12473           break;
12474
12475         case R_PPC64_GOT_TLSGD16:
12476         case R_PPC64_GOT_TLSGD16_LO:
12477           tls_gd = TLS_TPRELGD;
12478           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12479             goto tls_ldgd_opt;
12480           break;
12481
12482         case R_PPC64_GOT_TLSLD16:
12483         case R_PPC64_GOT_TLSLD16_LO:
12484           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12485             {
12486               unsigned int insn1, insn2, insn3;
12487               bfd_vma offset;
12488
12489             tls_ldgd_opt:
12490               offset = (bfd_vma) -1;
12491               /* If not using the newer R_PPC64_TLSGD/LD to mark
12492                  __tls_get_addr calls, we must trust that the call
12493                  stays with its arg setup insns, ie. that the next
12494                  reloc is the __tls_get_addr call associated with
12495                  the current reloc.  Edit both insns.  */
12496               if (input_section->has_tls_get_addr_call
12497                   && rel + 1 < relend
12498                   && branch_reloc_hash_match (input_bfd, rel + 1,
12499                                               htab->tls_get_addr,
12500                                               htab->tls_get_addr_fd))
12501                 offset = rel[1].r_offset;
12502               if ((tls_mask & tls_gd) != 0)
12503                 {
12504                   /* IE */
12505                   insn1 = bfd_get_32 (output_bfd,
12506                                       contents + rel->r_offset - d_offset);
12507                   insn1 &= (1 << 26) - (1 << 2);
12508                   insn1 |= 58 << 26;    /* ld */
12509                   insn2 = 0x7c636a14;   /* add 3,3,13 */
12510                   if (offset != (bfd_vma) -1)
12511                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12512                   if ((tls_mask & TLS_EXPLICIT) == 0)
12513                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12514                               + R_PPC64_GOT_TPREL16_DS);
12515                   else
12516                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
12517                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12518                 }
12519               else
12520                 {
12521                   /* LE */
12522                   insn1 = 0x3c6d0000;   /* addis 3,13,0 */
12523                   insn2 = 0x38630000;   /* addi 3,3,0 */
12524                   if (tls_gd == 0)
12525                     {
12526                       /* Was an LD reloc.  */
12527                       if (toc_symndx)
12528                         sec = local_sections[toc_symndx];
12529                       for (r_symndx = 0;
12530                            r_symndx < symtab_hdr->sh_info;
12531                            r_symndx++)
12532                         if (local_sections[r_symndx] == sec)
12533                           break;
12534                       if (r_symndx >= symtab_hdr->sh_info)
12535                         r_symndx = STN_UNDEF;
12536                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12537                       if (r_symndx != STN_UNDEF)
12538                         rel->r_addend -= (local_syms[r_symndx].st_value
12539                                           + sec->output_offset
12540                                           + sec->output_section->vma);
12541                     }
12542                   else if (toc_symndx != 0)
12543                     {
12544                       r_symndx = toc_symndx;
12545                       rel->r_addend = toc_addend;
12546                     }
12547                   r_type = R_PPC64_TPREL16_HA;
12548                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12549                   if (offset != (bfd_vma) -1)
12550                     {
12551                       rel[1].r_info = ELF64_R_INFO (r_symndx,
12552                                                     R_PPC64_TPREL16_LO);
12553                       rel[1].r_offset = offset + d_offset;
12554                       rel[1].r_addend = rel->r_addend;
12555                     }
12556                 }
12557               bfd_put_32 (output_bfd, insn1,
12558                           contents + rel->r_offset - d_offset);
12559               if (offset != (bfd_vma) -1)
12560                 {
12561                   insn3 = bfd_get_32 (output_bfd,
12562                                       contents + offset + 4);
12563                   if (insn3 == NOP
12564                       || insn3 == CROR_151515 || insn3 == CROR_313131)
12565                     {
12566                       rel[1].r_offset += 4;
12567                       bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12568                       insn2 = NOP;
12569                     }
12570                   bfd_put_32 (output_bfd, insn2, contents + offset);
12571                 }
12572               if ((tls_mask & tls_gd) == 0
12573                   && (tls_gd == 0 || toc_symndx != 0))
12574                 {
12575                   /* We changed the symbol.  Start over in order
12576                      to get h, sym, sec etc. right.  */
12577                   rel--;
12578                   continue;
12579                 }
12580             }
12581           break;
12582
12583         case R_PPC64_TLSGD:
12584           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12585             {
12586               unsigned int insn2, insn3;
12587               bfd_vma offset = rel->r_offset;
12588
12589               if ((tls_mask & TLS_TPRELGD) != 0)
12590                 {
12591                   /* IE */
12592                   r_type = R_PPC64_NONE;
12593                   insn2 = 0x7c636a14;   /* add 3,3,13 */
12594                 }
12595               else
12596                 {
12597                   /* LE */
12598                   if (toc_symndx != 0)
12599                     {
12600                       r_symndx = toc_symndx;
12601                       rel->r_addend = toc_addend;
12602                     }
12603                   r_type = R_PPC64_TPREL16_LO;
12604                   rel->r_offset = offset + d_offset;
12605                   insn2 = 0x38630000;   /* addi 3,3,0 */
12606                 }
12607               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12608               /* Zap the reloc on the _tls_get_addr call too.  */
12609               BFD_ASSERT (offset == rel[1].r_offset);
12610               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12611               insn3 = bfd_get_32 (output_bfd,
12612                                   contents + offset + 4);
12613               if (insn3 == NOP
12614                   || insn3 == CROR_151515 || insn3 == CROR_313131)
12615                 {
12616                   rel->r_offset += 4;
12617                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12618                   insn2 = NOP;
12619                 }
12620               bfd_put_32 (output_bfd, insn2, contents + offset);
12621               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
12622                 {
12623                   rel--;
12624                   continue;
12625                 }
12626             }
12627           break;
12628
12629         case R_PPC64_TLSLD:
12630           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12631             {
12632               unsigned int insn2, insn3;
12633               bfd_vma offset = rel->r_offset;
12634
12635               if (toc_symndx)
12636                 sec = local_sections[toc_symndx];
12637               for (r_symndx = 0;
12638                    r_symndx < symtab_hdr->sh_info;
12639                    r_symndx++)
12640                 if (local_sections[r_symndx] == sec)
12641                   break;
12642               if (r_symndx >= symtab_hdr->sh_info)
12643                 r_symndx = STN_UNDEF;
12644               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12645               if (r_symndx != STN_UNDEF)
12646                 rel->r_addend -= (local_syms[r_symndx].st_value
12647                                   + sec->output_offset
12648                                   + sec->output_section->vma);
12649
12650               r_type = R_PPC64_TPREL16_LO;
12651               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12652               rel->r_offset = offset + d_offset;
12653               /* Zap the reloc on the _tls_get_addr call too.  */
12654               BFD_ASSERT (offset == rel[1].r_offset);
12655               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12656               insn2 = 0x38630000;       /* addi 3,3,0 */
12657               insn3 = bfd_get_32 (output_bfd,
12658                                   contents + offset + 4);
12659               if (insn3 == NOP
12660                   || insn3 == CROR_151515 || insn3 == CROR_313131)
12661                 {
12662                   rel->r_offset += 4;
12663                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12664                   insn2 = NOP;
12665                 }
12666               bfd_put_32 (output_bfd, insn2, contents + offset);
12667               rel--;
12668               continue;
12669             }
12670           break;
12671
12672         case R_PPC64_DTPMOD64:
12673           if (rel + 1 < relend
12674               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12675               && rel[1].r_offset == rel->r_offset + 8)
12676             {
12677               if ((tls_mask & TLS_GD) == 0)
12678                 {
12679                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12680                   if ((tls_mask & TLS_TPRELGD) != 0)
12681                     r_type = R_PPC64_TPREL64;
12682                   else
12683                     {
12684                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12685                       r_type = R_PPC64_NONE;
12686                     }
12687                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12688                 }
12689             }
12690           else
12691             {
12692               if ((tls_mask & TLS_LD) == 0)
12693                 {
12694                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12695                   r_type = R_PPC64_NONE;
12696                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12697                 }
12698             }
12699           break;
12700
12701         case R_PPC64_TPREL64:
12702           if ((tls_mask & TLS_TPREL) == 0)
12703             {
12704               r_type = R_PPC64_NONE;
12705               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12706             }
12707           break;
12708         }
12709
12710       /* Handle other relocations that tweak non-addend part of insn.  */
12711       insn = 0;
12712       max_br_offset = 1 << 25;
12713       addend = rel->r_addend;
12714       switch (r_type)
12715         {
12716         default:
12717           break;
12718
12719         case R_PPC64_TOCSAVE:
12720           if (relocation + addend == (rel->r_offset
12721                                       + input_section->output_offset
12722                                       + input_section->output_section->vma)
12723               && tocsave_find (htab, NO_INSERT,
12724                                &local_syms, rel, input_bfd))
12725             {
12726               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
12727               if (insn == NOP
12728                   || insn == CROR_151515 || insn == CROR_313131)
12729                 bfd_put_32 (input_bfd, STD_R2_40R1,
12730                             contents + rel->r_offset);
12731             }
12732           break;
12733
12734           /* Branch taken prediction relocations.  */
12735         case R_PPC64_ADDR14_BRTAKEN:
12736         case R_PPC64_REL14_BRTAKEN:
12737           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
12738           /* Fall thru.  */
12739
12740           /* Branch not taken prediction relocations.  */
12741         case R_PPC64_ADDR14_BRNTAKEN:
12742         case R_PPC64_REL14_BRNTAKEN:
12743           insn |= bfd_get_32 (output_bfd,
12744                               contents + rel->r_offset) & ~(0x01 << 21);
12745           /* Fall thru.  */
12746
12747         case R_PPC64_REL14:
12748           max_br_offset = 1 << 15;
12749           /* Fall thru.  */
12750
12751         case R_PPC64_REL24:
12752           /* Calls to functions with a different TOC, such as calls to
12753              shared objects, need to alter the TOC pointer.  This is
12754              done using a linkage stub.  A REL24 branching to these
12755              linkage stubs needs to be followed by a nop, as the nop
12756              will be replaced with an instruction to restore the TOC
12757              base pointer.  */
12758           fdh = h;
12759           if (h != NULL
12760               && h->oh != NULL
12761               && h->oh->is_func_descriptor)
12762             fdh = ppc_follow_link (h->oh);
12763           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12764           if (stub_entry != NULL
12765               && (stub_entry->stub_type == ppc_stub_plt_call
12766                   || stub_entry->stub_type == ppc_stub_plt_call_r2save
12767                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12768                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
12769             {
12770               bfd_boolean can_plt_call = FALSE;
12771
12772               if (rel->r_offset + 8 <= input_section->size)
12773                 {
12774                   unsigned long nop;
12775                   nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12776                   if (nop == NOP
12777                       || nop == CROR_151515 || nop == CROR_313131)
12778                     {
12779                       if (h != NULL
12780                           && (h == htab->tls_get_addr_fd
12781                               || h == htab->tls_get_addr)
12782                           && !htab->no_tls_get_addr_opt)
12783                         {
12784                           /* Special stub used, leave nop alone.  */
12785                         }
12786                       else
12787                         bfd_put_32 (input_bfd, LD_R2_40R1,
12788                                     contents + rel->r_offset + 4);
12789                       can_plt_call = TRUE;
12790                     }
12791                 }
12792
12793               if (!can_plt_call)
12794                 {
12795                   if (stub_entry->stub_type == ppc_stub_plt_call
12796                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
12797                     {
12798                       /* If this is a plain branch rather than a branch
12799                          and link, don't require a nop.  However, don't
12800                          allow tail calls in a shared library as they
12801                          will result in r2 being corrupted.  */
12802                       unsigned long br;
12803                       br = bfd_get_32 (input_bfd, contents + rel->r_offset);
12804                       if (info->executable && (br & 1) == 0)
12805                         can_plt_call = TRUE;
12806                       else
12807                         stub_entry = NULL;
12808                     }
12809                   else if (h != NULL
12810                            && strcmp (h->elf.root.root.string,
12811                                       ".__libc_start_main") == 0)
12812                     {
12813                       /* Allow crt1 branch to go via a toc adjusting stub.  */
12814                       can_plt_call = TRUE;
12815                     }
12816                   else
12817                     {
12818                       if (strcmp (input_section->output_section->name,
12819                                   ".init") == 0
12820                           || strcmp (input_section->output_section->name,
12821                                      ".fini") == 0)
12822                         info->callbacks->einfo
12823                           (_("%P: %H: automatic multiple TOCs "
12824                              "not supported using your crt files; "
12825                              "recompile with -mminimal-toc or upgrade gcc\n"),
12826                            input_bfd, input_section, rel->r_offset);
12827                       else
12828                         info->callbacks->einfo
12829                           (_("%P: %H: sibling call optimization to `%s' "
12830                              "does not allow automatic multiple TOCs; "
12831                              "recompile with -mminimal-toc or "
12832                              "-fno-optimize-sibling-calls, "
12833                              "or make `%s' extern\n"),
12834                            input_bfd, input_section, rel->r_offset,
12835                            sym_name,
12836                            sym_name);
12837                       bfd_set_error (bfd_error_bad_value);
12838                       ret = FALSE;
12839                     }
12840                 }
12841
12842               if (can_plt_call
12843                   && (stub_entry->stub_type == ppc_stub_plt_call
12844                       || stub_entry->stub_type == ppc_stub_plt_call_r2save))
12845                 unresolved_reloc = FALSE;
12846             }
12847
12848           if ((stub_entry == NULL
12849                || stub_entry->stub_type == ppc_stub_long_branch
12850                || stub_entry->stub_type == ppc_stub_plt_branch)
12851               && get_opd_info (sec) != NULL)
12852             {
12853               /* The branch destination is the value of the opd entry. */
12854               bfd_vma off = (relocation + addend
12855                              - sec->output_section->vma
12856                              - sec->output_offset);
12857               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12858               if (dest != (bfd_vma) -1)
12859                 {
12860                   relocation = dest;
12861                   addend = 0;
12862                 }
12863             }
12864
12865           /* If the branch is out of reach we ought to have a long
12866              branch stub.  */
12867           from = (rel->r_offset
12868                   + input_section->output_offset
12869                   + input_section->output_section->vma);
12870
12871           if (stub_entry != NULL
12872               && (stub_entry->stub_type == ppc_stub_long_branch
12873                   || stub_entry->stub_type == ppc_stub_plt_branch)
12874               && (r_type == R_PPC64_ADDR14_BRTAKEN
12875                   || r_type == R_PPC64_ADDR14_BRNTAKEN
12876                   || (relocation + addend - from + max_br_offset
12877                       < 2 * max_br_offset)))
12878             /* Don't use the stub if this branch is in range.  */
12879             stub_entry = NULL;
12880
12881           if (stub_entry != NULL)
12882             {
12883               /* Munge up the value and addend so that we call the stub
12884                  rather than the procedure directly.  */
12885               relocation = (stub_entry->stub_offset
12886                             + stub_entry->stub_sec->output_offset
12887                             + stub_entry->stub_sec->output_section->vma);
12888               addend = 0;
12889
12890               if ((stub_entry->stub_type == ppc_stub_plt_call
12891                    || stub_entry->stub_type == ppc_stub_plt_call_r2save)
12892                   && (ALWAYS_EMIT_R2SAVE
12893                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
12894                   && rel + 1 < relend
12895                   && rel[1].r_offset == rel->r_offset + 4
12896                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
12897                 relocation += 4;
12898             }
12899
12900           if (insn != 0)
12901             {
12902               if (is_isa_v2)
12903                 {
12904                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
12905                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
12906                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
12907                   if ((insn & (0x14 << 21)) == (0x04 << 21))
12908                     insn |= 0x02 << 21;
12909                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
12910                     insn |= 0x08 << 21;
12911                   else
12912                     break;
12913                 }
12914               else
12915                 {
12916                   /* Invert 'y' bit if not the default.  */
12917                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
12918                     insn ^= 0x01 << 21;
12919                 }
12920
12921               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12922             }
12923
12924           /* NOP out calls to undefined weak functions.
12925              We can thus call a weak function without first
12926              checking whether the function is defined.  */
12927           else if (h != NULL
12928                    && h->elf.root.type == bfd_link_hash_undefweak
12929                    && h->elf.dynindx == -1
12930                    && r_type == R_PPC64_REL24
12931                    && relocation == 0
12932                    && addend == 0)
12933             {
12934               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12935               continue;
12936             }
12937           break;
12938         }
12939
12940       /* Set `addend'.  */
12941       tls_type = 0;
12942       switch (r_type)
12943         {
12944         default:
12945           info->callbacks->einfo
12946             (_("%P: %B: unknown relocation type %d for symbol %s\n"),
12947              input_bfd, (int) r_type, sym_name);
12948
12949           bfd_set_error (bfd_error_bad_value);
12950           ret = FALSE;
12951           continue;
12952
12953         case R_PPC64_NONE:
12954         case R_PPC64_TLS:
12955         case R_PPC64_TLSGD:
12956         case R_PPC64_TLSLD:
12957         case R_PPC64_TOCSAVE:
12958         case R_PPC64_GNU_VTINHERIT:
12959         case R_PPC64_GNU_VTENTRY:
12960           continue;
12961
12962           /* GOT16 relocations.  Like an ADDR16 using the symbol's
12963              address in the GOT as relocation value instead of the
12964              symbol's value itself.  Also, create a GOT entry for the
12965              symbol and put the symbol value there.  */
12966         case R_PPC64_GOT_TLSGD16:
12967         case R_PPC64_GOT_TLSGD16_LO:
12968         case R_PPC64_GOT_TLSGD16_HI:
12969         case R_PPC64_GOT_TLSGD16_HA:
12970           tls_type = TLS_TLS | TLS_GD;
12971           goto dogot;
12972
12973         case R_PPC64_GOT_TLSLD16:
12974         case R_PPC64_GOT_TLSLD16_LO:
12975         case R_PPC64_GOT_TLSLD16_HI:
12976         case R_PPC64_GOT_TLSLD16_HA:
12977           tls_type = TLS_TLS | TLS_LD;
12978           goto dogot;
12979
12980         case R_PPC64_GOT_TPREL16_DS:
12981         case R_PPC64_GOT_TPREL16_LO_DS:
12982         case R_PPC64_GOT_TPREL16_HI:
12983         case R_PPC64_GOT_TPREL16_HA:
12984           tls_type = TLS_TLS | TLS_TPREL;
12985           goto dogot;
12986
12987         case R_PPC64_GOT_DTPREL16_DS:
12988         case R_PPC64_GOT_DTPREL16_LO_DS:
12989         case R_PPC64_GOT_DTPREL16_HI:
12990         case R_PPC64_GOT_DTPREL16_HA:
12991           tls_type = TLS_TLS | TLS_DTPREL;
12992           goto dogot;
12993
12994         case R_PPC64_GOT16:
12995         case R_PPC64_GOT16_LO:
12996         case R_PPC64_GOT16_HI:
12997         case R_PPC64_GOT16_HA:
12998         case R_PPC64_GOT16_DS:
12999         case R_PPC64_GOT16_LO_DS:
13000         dogot:
13001           {
13002             /* Relocation is to the entry for this symbol in the global
13003                offset table.  */
13004             asection *got;
13005             bfd_vma *offp;
13006             bfd_vma off;
13007             unsigned long indx = 0;
13008             struct got_entry *ent;
13009
13010             if (tls_type == (TLS_TLS | TLS_LD)
13011                 && (h == NULL
13012                     || !h->elf.def_dynamic))
13013               ent = ppc64_tlsld_got (input_bfd);
13014             else
13015               {
13016
13017                 if (h != NULL)
13018                   {
13019                     bfd_boolean dyn = htab->elf.dynamic_sections_created;
13020                     if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
13021                                                           &h->elf)
13022                         || (info->shared
13023                             && SYMBOL_CALLS_LOCAL (info, &h->elf)))
13024                       /* This is actually a static link, or it is a
13025                          -Bsymbolic link and the symbol is defined
13026                          locally, or the symbol was forced to be local
13027                          because of a version file.  */
13028                       ;
13029                     else
13030                       {
13031                         indx = h->elf.dynindx;
13032                         unresolved_reloc = FALSE;
13033                       }
13034                     ent = h->elf.got.glist;
13035                   }
13036                 else
13037                   {
13038                     if (local_got_ents == NULL)
13039                       abort ();
13040                     ent = local_got_ents[r_symndx];
13041                   }
13042
13043                 for (; ent != NULL; ent = ent->next)
13044                   if (ent->addend == orig_addend
13045                       && ent->owner == input_bfd
13046                       && ent->tls_type == tls_type)
13047                     break;
13048               }
13049
13050             if (ent == NULL)
13051               abort ();
13052             if (ent->is_indirect)
13053               ent = ent->got.ent;
13054             offp = &ent->got.offset;
13055             got = ppc64_elf_tdata (ent->owner)->got;
13056             if (got == NULL)
13057               abort ();
13058
13059             /* The offset must always be a multiple of 8.  We use the
13060                least significant bit to record whether we have already
13061                processed this entry.  */
13062             off = *offp;
13063             if ((off & 1) != 0)
13064               off &= ~1;
13065             else
13066               {
13067                 /* Generate relocs for the dynamic linker, except in
13068                    the case of TLSLD where we'll use one entry per
13069                    module.  */
13070                 asection *relgot;
13071                 bfd_boolean ifunc;
13072
13073                 *offp = off | 1;
13074                 relgot = NULL;
13075                 ifunc = (h != NULL
13076                          ? h->elf.type == STT_GNU_IFUNC
13077                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
13078                 if ((info->shared || indx != 0)
13079                     && (h == NULL
13080                         || (tls_type == (TLS_TLS | TLS_LD)
13081                             && !h->elf.def_dynamic)
13082                         || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13083                         || h->elf.root.type != bfd_link_hash_undefweak))
13084                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
13085                 else if (ifunc)
13086                   relgot = htab->reliplt;
13087                 if (relgot != NULL)
13088                   {
13089                     outrel.r_offset = (got->output_section->vma
13090                                        + got->output_offset
13091                                        + off);
13092                     outrel.r_addend = addend;
13093                     if (tls_type & (TLS_LD | TLS_GD))
13094                       {
13095                         outrel.r_addend = 0;
13096                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
13097                         if (tls_type == (TLS_TLS | TLS_GD))
13098                           {
13099                             loc = relgot->contents;
13100                             loc += (relgot->reloc_count++
13101                                     * sizeof (Elf64_External_Rela));
13102                             bfd_elf64_swap_reloca_out (output_bfd,
13103                                                        &outrel, loc);
13104                             outrel.r_offset += 8;
13105                             outrel.r_addend = addend;
13106                             outrel.r_info
13107                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13108                           }
13109                       }
13110                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
13111                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13112                     else if (tls_type == (TLS_TLS | TLS_TPREL))
13113                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
13114                     else if (indx != 0)
13115                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
13116                     else
13117                       {
13118                         if (ifunc)
13119                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13120                         else
13121                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13122
13123                         /* Write the .got section contents for the sake
13124                            of prelink.  */
13125                         loc = got->contents + off;
13126                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
13127                                     loc);
13128                       }
13129
13130                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
13131                       {
13132                         outrel.r_addend += relocation;
13133                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
13134                           outrel.r_addend -= htab->elf.tls_sec->vma;
13135                       }
13136                     loc = relgot->contents;
13137                     loc += (relgot->reloc_count++
13138                             * sizeof (Elf64_External_Rela));
13139                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13140                   }
13141
13142                 /* Init the .got section contents here if we're not
13143                    emitting a reloc.  */
13144                 else
13145                   {
13146                     relocation += addend;
13147                     if (tls_type == (TLS_TLS | TLS_LD))
13148                       relocation = 1;
13149                     else if (tls_type != 0)
13150                       {
13151                         relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
13152                         if (tls_type == (TLS_TLS | TLS_TPREL))
13153                           relocation += DTP_OFFSET - TP_OFFSET;
13154
13155                         if (tls_type == (TLS_TLS | TLS_GD))
13156                           {
13157                             bfd_put_64 (output_bfd, relocation,
13158                                         got->contents + off + 8);
13159                             relocation = 1;
13160                           }
13161                       }
13162
13163                     bfd_put_64 (output_bfd, relocation,
13164                                 got->contents + off);
13165                   }
13166               }
13167
13168             if (off >= (bfd_vma) -2)
13169               abort ();
13170
13171             relocation = got->output_section->vma + got->output_offset + off;
13172             addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
13173           }
13174           break;
13175
13176         case R_PPC64_PLT16_HA:
13177         case R_PPC64_PLT16_HI:
13178         case R_PPC64_PLT16_LO:
13179         case R_PPC64_PLT32:
13180         case R_PPC64_PLT64:
13181           /* Relocation is to the entry for this symbol in the
13182              procedure linkage table.  */
13183
13184           /* Resolve a PLT reloc against a local symbol directly,
13185              without using the procedure linkage table.  */
13186           if (h == NULL)
13187             break;
13188
13189           /* It's possible that we didn't make a PLT entry for this
13190              symbol.  This happens when statically linking PIC code,
13191              or when using -Bsymbolic.  Go find a match if there is a
13192              PLT entry.  */
13193           if (htab->plt != NULL)
13194             {
13195               struct plt_entry *ent;
13196               for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
13197                 if (ent->addend == orig_addend
13198                     && ent->plt.offset != (bfd_vma) -1)
13199                   {
13200                     relocation = (htab->plt->output_section->vma
13201                                   + htab->plt->output_offset
13202                                   + ent->plt.offset);
13203                     unresolved_reloc = FALSE;
13204                   }
13205             }
13206           break;
13207
13208         case R_PPC64_TOC:
13209           /* Relocation value is TOC base.  */
13210           relocation = TOCstart;
13211           if (r_symndx == STN_UNDEF)
13212             relocation += htab->stub_group[input_section->id].toc_off;
13213           else if (unresolved_reloc)
13214             ;
13215           else if (sec != NULL && sec->id <= htab->top_id)
13216             relocation += htab->stub_group[sec->id].toc_off;
13217           else
13218             unresolved_reloc = TRUE;
13219           goto dodyn;
13220
13221           /* TOC16 relocs.  We want the offset relative to the TOC base,
13222              which is the address of the start of the TOC plus 0x8000.
13223              The TOC consists of sections .got, .toc, .tocbss, and .plt,
13224              in this order.  */
13225         case R_PPC64_TOC16:
13226         case R_PPC64_TOC16_LO:
13227         case R_PPC64_TOC16_HI:
13228         case R_PPC64_TOC16_DS:
13229         case R_PPC64_TOC16_LO_DS:
13230         case R_PPC64_TOC16_HA:
13231           addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
13232           break;
13233
13234           /* Relocate against the beginning of the section.  */
13235         case R_PPC64_SECTOFF:
13236         case R_PPC64_SECTOFF_LO:
13237         case R_PPC64_SECTOFF_HI:
13238         case R_PPC64_SECTOFF_DS:
13239         case R_PPC64_SECTOFF_LO_DS:
13240         case R_PPC64_SECTOFF_HA:
13241           if (sec != NULL)
13242             addend -= sec->output_section->vma;
13243           break;
13244
13245         case R_PPC64_REL16:
13246         case R_PPC64_REL16_LO:
13247         case R_PPC64_REL16_HI:
13248         case R_PPC64_REL16_HA:
13249           break;
13250
13251         case R_PPC64_REL14:
13252         case R_PPC64_REL14_BRNTAKEN:
13253         case R_PPC64_REL14_BRTAKEN:
13254         case R_PPC64_REL24:
13255           break;
13256
13257         case R_PPC64_TPREL16:
13258         case R_PPC64_TPREL16_LO:
13259         case R_PPC64_TPREL16_HI:
13260         case R_PPC64_TPREL16_HA:
13261         case R_PPC64_TPREL16_DS:
13262         case R_PPC64_TPREL16_LO_DS:
13263         case R_PPC64_TPREL16_HIGHER:
13264         case R_PPC64_TPREL16_HIGHERA:
13265         case R_PPC64_TPREL16_HIGHEST:
13266         case R_PPC64_TPREL16_HIGHESTA:
13267           if (h != NULL
13268               && h->elf.root.type == bfd_link_hash_undefweak
13269               && h->elf.dynindx == -1)
13270             {
13271               /* Make this relocation against an undefined weak symbol
13272                  resolve to zero.  This is really just a tweak, since
13273                  code using weak externs ought to check that they are
13274                  defined before using them.  */
13275               bfd_byte *p = contents + rel->r_offset - d_offset;
13276
13277               insn = bfd_get_32 (output_bfd, p);
13278               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
13279               if (insn != 0)
13280                 bfd_put_32 (output_bfd, insn, p);
13281               break;
13282             }
13283           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13284           if (info->shared)
13285             /* The TPREL16 relocs shouldn't really be used in shared
13286                libs as they will result in DT_TEXTREL being set, but
13287                support them anyway.  */
13288             goto dodyn;
13289           break;
13290
13291         case R_PPC64_DTPREL16:
13292         case R_PPC64_DTPREL16_LO:
13293         case R_PPC64_DTPREL16_HI:
13294         case R_PPC64_DTPREL16_HA:
13295         case R_PPC64_DTPREL16_DS:
13296         case R_PPC64_DTPREL16_LO_DS:
13297         case R_PPC64_DTPREL16_HIGHER:
13298         case R_PPC64_DTPREL16_HIGHERA:
13299         case R_PPC64_DTPREL16_HIGHEST:
13300         case R_PPC64_DTPREL16_HIGHESTA:
13301           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13302           break;
13303
13304         case R_PPC64_DTPMOD64:
13305           relocation = 1;
13306           addend = 0;
13307           goto dodyn;
13308
13309         case R_PPC64_TPREL64:
13310           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13311           goto dodyn;
13312
13313         case R_PPC64_DTPREL64:
13314           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13315           /* Fall thru */
13316
13317           /* Relocations that may need to be propagated if this is a
13318              dynamic object.  */
13319         case R_PPC64_REL30:
13320         case R_PPC64_REL32:
13321         case R_PPC64_REL64:
13322         case R_PPC64_ADDR14:
13323         case R_PPC64_ADDR14_BRNTAKEN:
13324         case R_PPC64_ADDR14_BRTAKEN:
13325         case R_PPC64_ADDR16:
13326         case R_PPC64_ADDR16_DS:
13327         case R_PPC64_ADDR16_HA:
13328         case R_PPC64_ADDR16_HI:
13329         case R_PPC64_ADDR16_HIGHER:
13330         case R_PPC64_ADDR16_HIGHERA:
13331         case R_PPC64_ADDR16_HIGHEST:
13332         case R_PPC64_ADDR16_HIGHESTA:
13333         case R_PPC64_ADDR16_LO:
13334         case R_PPC64_ADDR16_LO_DS:
13335         case R_PPC64_ADDR24:
13336         case R_PPC64_ADDR32:
13337         case R_PPC64_ADDR64:
13338         case R_PPC64_UADDR16:
13339         case R_PPC64_UADDR32:
13340         case R_PPC64_UADDR64:
13341         dodyn:
13342           if ((input_section->flags & SEC_ALLOC) == 0)
13343             break;
13344
13345           if (NO_OPD_RELOCS && is_opd)
13346             break;
13347
13348           if ((info->shared
13349                && (h == NULL
13350                    || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13351                    || h->elf.root.type != bfd_link_hash_undefweak)
13352                && (must_be_dyn_reloc (info, r_type)
13353                    || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
13354               || (ELIMINATE_COPY_RELOCS
13355                   && !info->shared
13356                   && h != NULL
13357                   && h->elf.dynindx != -1
13358                   && !h->elf.non_got_ref
13359                   && !h->elf.def_regular)
13360               || (!info->shared
13361                   && (h != NULL
13362                       ? h->elf.type == STT_GNU_IFUNC
13363                       : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
13364             {
13365               bfd_boolean skip, relocate;
13366               asection *sreloc;
13367               bfd_vma out_off;
13368
13369               /* When generating a dynamic object, these relocations
13370                  are copied into the output file to be resolved at run
13371                  time.  */
13372
13373               skip = FALSE;
13374               relocate = FALSE;
13375
13376               out_off = _bfd_elf_section_offset (output_bfd, info,
13377                                                  input_section, rel->r_offset);
13378               if (out_off == (bfd_vma) -1)
13379                 skip = TRUE;
13380               else if (out_off == (bfd_vma) -2)
13381                 skip = TRUE, relocate = TRUE;
13382               out_off += (input_section->output_section->vma
13383                           + input_section->output_offset);
13384               outrel.r_offset = out_off;
13385               outrel.r_addend = rel->r_addend;
13386
13387               /* Optimize unaligned reloc use.  */
13388               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
13389                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
13390                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
13391               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
13392                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
13393                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
13394               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
13395                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
13396                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
13397
13398               if (skip)
13399                 memset (&outrel, 0, sizeof outrel);
13400               else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
13401                        && !is_opd
13402                        && r_type != R_PPC64_TOC)
13403                 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
13404               else
13405                 {
13406                   /* This symbol is local, or marked to become local,
13407                      or this is an opd section reloc which must point
13408                      at a local function.  */
13409                   outrel.r_addend += relocation;
13410                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13411                     {
13412                       if (is_opd && h != NULL)
13413                         {
13414                           /* Lie about opd entries.  This case occurs
13415                              when building shared libraries and we
13416                              reference a function in another shared
13417                              lib.  The same thing happens for a weak
13418                              definition in an application that's
13419                              overridden by a strong definition in a
13420                              shared lib.  (I believe this is a generic
13421                              bug in binutils handling of weak syms.)
13422                              In these cases we won't use the opd
13423                              entry in this lib.  */
13424                           unresolved_reloc = FALSE;
13425                         }
13426                       if (!is_opd
13427                           && r_type == R_PPC64_ADDR64
13428                           && (h != NULL
13429                               ? h->elf.type == STT_GNU_IFUNC
13430                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
13431                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13432                       else
13433                         {
13434                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13435
13436                           /* We need to relocate .opd contents for ld.so.
13437                              Prelink also wants simple and consistent rules
13438                              for relocs.  This make all RELATIVE relocs have
13439                              *r_offset equal to r_addend.  */
13440                           relocate = TRUE;
13441                         }
13442                     }
13443                   else
13444                     {
13445                       long indx = 0;
13446
13447                       if (h != NULL
13448                           ? h->elf.type == STT_GNU_IFUNC
13449                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13450                         {
13451                           info->callbacks->einfo
13452                             (_("%P: %H: relocation %s for indirect "
13453                                "function %s unsupported\n"),
13454                              input_bfd, input_section, rel->r_offset,
13455                              ppc64_elf_howto_table[r_type]->name,
13456                              sym_name);
13457                           ret = FALSE;
13458                         }
13459                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
13460                         ;
13461                       else if (sec == NULL || sec->owner == NULL)
13462                         {
13463                           bfd_set_error (bfd_error_bad_value);
13464                           return FALSE;
13465                         }
13466                       else
13467                         {
13468                           asection *osec;
13469
13470                           osec = sec->output_section;
13471                           indx = elf_section_data (osec)->dynindx;
13472
13473                           if (indx == 0)
13474                             {
13475                               if ((osec->flags & SEC_READONLY) == 0
13476                                   && htab->elf.data_index_section != NULL)
13477                                 osec = htab->elf.data_index_section;
13478                               else
13479                                 osec = htab->elf.text_index_section;
13480                               indx = elf_section_data (osec)->dynindx;
13481                             }
13482                           BFD_ASSERT (indx != 0);
13483
13484                           /* We are turning this relocation into one
13485                              against a section symbol, so subtract out
13486                              the output section's address but not the
13487                              offset of the input section in the output
13488                              section.  */
13489                           outrel.r_addend -= osec->vma;
13490                         }
13491
13492                       outrel.r_info = ELF64_R_INFO (indx, r_type);
13493                     }
13494                 }
13495
13496               sreloc = elf_section_data (input_section)->sreloc;
13497               if (!htab->elf.dynamic_sections_created)
13498                 sreloc = htab->reliplt;
13499               if (sreloc == NULL)
13500                 abort ();
13501
13502               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
13503                   >= sreloc->size)
13504                 abort ();
13505               loc = sreloc->contents;
13506               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
13507               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13508
13509               /* If this reloc is against an external symbol, it will
13510                  be computed at runtime, so there's no need to do
13511                  anything now.  However, for the sake of prelink ensure
13512                  that the section contents are a known value.  */
13513               if (! relocate)
13514                 {
13515                   unresolved_reloc = FALSE;
13516                   /* The value chosen here is quite arbitrary as ld.so
13517                      ignores section contents except for the special
13518                      case of .opd where the contents might be accessed
13519                      before relocation.  Choose zero, as that won't
13520                      cause reloc overflow.  */
13521                   relocation = 0;
13522                   addend = 0;
13523                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
13524                      to improve backward compatibility with older
13525                      versions of ld.  */
13526                   if (r_type == R_PPC64_ADDR64)
13527                     addend = outrel.r_addend;
13528                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
13529                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
13530                     addend = (input_section->output_section->vma
13531                               + input_section->output_offset
13532                               + rel->r_offset);
13533                 }
13534             }
13535           break;
13536
13537         case R_PPC64_COPY:
13538         case R_PPC64_GLOB_DAT:
13539         case R_PPC64_JMP_SLOT:
13540         case R_PPC64_JMP_IREL:
13541         case R_PPC64_RELATIVE:
13542           /* We shouldn't ever see these dynamic relocs in relocatable
13543              files.  */
13544           /* Fall through.  */
13545
13546         case R_PPC64_PLTGOT16:
13547         case R_PPC64_PLTGOT16_DS:
13548         case R_PPC64_PLTGOT16_HA:
13549         case R_PPC64_PLTGOT16_HI:
13550         case R_PPC64_PLTGOT16_LO:
13551         case R_PPC64_PLTGOT16_LO_DS:
13552         case R_PPC64_PLTREL32:
13553         case R_PPC64_PLTREL64:
13554           /* These ones haven't been implemented yet.  */
13555
13556           info->callbacks->einfo
13557             (_("%P: %B: relocation %s is not supported for symbol %s\n"),
13558              input_bfd,
13559              ppc64_elf_howto_table[r_type]->name, sym_name);
13560
13561           bfd_set_error (bfd_error_invalid_operation);
13562           ret = FALSE;
13563           continue;
13564         }
13565
13566       /* Multi-instruction sequences that access the TOC can be
13567          optimized, eg. addis ra,r2,0; addi rb,ra,x;
13568          to             nop;           addi rb,r2,x;  */
13569       switch (r_type)
13570         {
13571         default:
13572           break;
13573
13574         case R_PPC64_GOT_TLSLD16_HI:
13575         case R_PPC64_GOT_TLSGD16_HI:
13576         case R_PPC64_GOT_TPREL16_HI:
13577         case R_PPC64_GOT_DTPREL16_HI:
13578         case R_PPC64_GOT16_HI:
13579         case R_PPC64_TOC16_HI:
13580           /* These relocs would only be useful if building up an
13581              offset to later add to r2, perhaps in an indexed
13582              addressing mode instruction.  Don't try to optimize.
13583              Unfortunately, the possibility of someone building up an
13584              offset like this or even with the HA relocs, means that
13585              we need to check the high insn when optimizing the low
13586              insn.  */
13587           break;
13588
13589         case R_PPC64_GOT_TLSLD16_HA:
13590         case R_PPC64_GOT_TLSGD16_HA:
13591         case R_PPC64_GOT_TPREL16_HA:
13592         case R_PPC64_GOT_DTPREL16_HA:
13593         case R_PPC64_GOT16_HA:
13594         case R_PPC64_TOC16_HA:
13595           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
13596               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
13597             {
13598               bfd_byte *p = contents + (rel->r_offset & ~3);
13599               bfd_put_32 (input_bfd, NOP, p);
13600             }
13601           break;
13602
13603         case R_PPC64_GOT_TLSLD16_LO:
13604         case R_PPC64_GOT_TLSGD16_LO:
13605         case R_PPC64_GOT_TPREL16_LO_DS:
13606         case R_PPC64_GOT_DTPREL16_LO_DS:
13607         case R_PPC64_GOT16_LO:
13608         case R_PPC64_GOT16_LO_DS:
13609         case R_PPC64_TOC16_LO:
13610         case R_PPC64_TOC16_LO_DS:
13611           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
13612               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
13613             {
13614               bfd_byte *p = contents + (rel->r_offset & ~3);
13615               insn = bfd_get_32 (input_bfd, p);
13616               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
13617                 {
13618                   /* Transform addic to addi when we change reg.  */
13619                   insn &= ~((0x3f << 26) | (0x1f << 16));
13620                   insn |= (14u << 26) | (2 << 16);
13621                 }
13622               else
13623                 {
13624                   insn &= ~(0x1f << 16);
13625                   insn |= 2 << 16;
13626                 }
13627               bfd_put_32 (input_bfd, insn, p);
13628             }
13629           break;
13630         }
13631
13632       /* Do any further special processing.  */
13633       switch (r_type)
13634         {
13635         default:
13636           break;
13637
13638         case R_PPC64_ADDR16_HA:
13639         case R_PPC64_REL16_HA:
13640         case R_PPC64_ADDR16_HIGHERA:
13641         case R_PPC64_ADDR16_HIGHESTA:
13642         case R_PPC64_TOC16_HA:
13643         case R_PPC64_SECTOFF_HA:
13644         case R_PPC64_TPREL16_HA:
13645         case R_PPC64_DTPREL16_HA:
13646         case R_PPC64_TPREL16_HIGHER:
13647         case R_PPC64_TPREL16_HIGHERA:
13648         case R_PPC64_TPREL16_HIGHEST:
13649         case R_PPC64_TPREL16_HIGHESTA:
13650         case R_PPC64_DTPREL16_HIGHER:
13651         case R_PPC64_DTPREL16_HIGHERA:
13652         case R_PPC64_DTPREL16_HIGHEST:
13653         case R_PPC64_DTPREL16_HIGHESTA:
13654           /* It's just possible that this symbol is a weak symbol
13655              that's not actually defined anywhere. In that case,
13656              'sec' would be NULL, and we should leave the symbol
13657              alone (it will be set to zero elsewhere in the link).  */
13658           if (sec == NULL)
13659             break;
13660           /* Fall thru */
13661
13662         case R_PPC64_GOT16_HA:
13663         case R_PPC64_PLTGOT16_HA:
13664         case R_PPC64_PLT16_HA:
13665         case R_PPC64_GOT_TLSGD16_HA:
13666         case R_PPC64_GOT_TLSLD16_HA:
13667         case R_PPC64_GOT_TPREL16_HA:
13668         case R_PPC64_GOT_DTPREL16_HA:
13669           /* Add 0x10000 if sign bit in 0:15 is set.
13670              Bits 0:15 are not used.  */
13671           addend += 0x8000;
13672           break;
13673
13674         case R_PPC64_ADDR16_DS:
13675         case R_PPC64_ADDR16_LO_DS:
13676         case R_PPC64_GOT16_DS:
13677         case R_PPC64_GOT16_LO_DS:
13678         case R_PPC64_PLT16_LO_DS:
13679         case R_PPC64_SECTOFF_DS:
13680         case R_PPC64_SECTOFF_LO_DS:
13681         case R_PPC64_TOC16_DS:
13682         case R_PPC64_TOC16_LO_DS:
13683         case R_PPC64_PLTGOT16_DS:
13684         case R_PPC64_PLTGOT16_LO_DS:
13685         case R_PPC64_GOT_TPREL16_DS:
13686         case R_PPC64_GOT_TPREL16_LO_DS:
13687         case R_PPC64_GOT_DTPREL16_DS:
13688         case R_PPC64_GOT_DTPREL16_LO_DS:
13689         case R_PPC64_TPREL16_DS:
13690         case R_PPC64_TPREL16_LO_DS:
13691         case R_PPC64_DTPREL16_DS:
13692         case R_PPC64_DTPREL16_LO_DS:
13693           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13694           mask = 3;
13695           /* If this reloc is against an lq insn, then the value must be
13696              a multiple of 16.  This is somewhat of a hack, but the
13697              "correct" way to do this by defining _DQ forms of all the
13698              _DS relocs bloats all reloc switches in this file.  It
13699              doesn't seem to make much sense to use any of these relocs
13700              in data, so testing the insn should be safe.  */
13701           if ((insn & (0x3f << 26)) == (56u << 26))
13702             mask = 15;
13703           if (((relocation + addend) & mask) != 0)
13704             {
13705               info->callbacks->einfo
13706                 (_("%P: %H: error: %s not a multiple of %u\n"),
13707                  input_bfd, input_section, rel->r_offset,
13708                  ppc64_elf_howto_table[r_type]->name,
13709                  mask + 1);
13710               bfd_set_error (bfd_error_bad_value);
13711               ret = FALSE;
13712               continue;
13713             }
13714           break;
13715         }
13716
13717       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13718          because such sections are not SEC_ALLOC and thus ld.so will
13719          not process them.  */
13720       if (unresolved_reloc
13721           && !((input_section->flags & SEC_DEBUGGING) != 0
13722                && h->elf.def_dynamic)
13723           && _bfd_elf_section_offset (output_bfd, info, input_section,
13724                                       rel->r_offset) != (bfd_vma) -1)
13725         {
13726           info->callbacks->einfo
13727             (_("%P: %H: unresolvable %s relocation against symbol `%s'\n"),
13728              input_bfd, input_section, rel->r_offset,
13729              ppc64_elf_howto_table[(int) r_type]->name,
13730              h->elf.root.root.string);
13731           ret = FALSE;
13732         }
13733
13734       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
13735                                     input_bfd,
13736                                     input_section,
13737                                     contents,
13738                                     rel->r_offset,
13739                                     relocation,
13740                                     addend);
13741
13742       if (r != bfd_reloc_ok)
13743         {
13744           if (sym_name == NULL)
13745             sym_name = "(null)";
13746           if (r == bfd_reloc_overflow)
13747             {
13748               if (warned)
13749                 continue;
13750               if (h != NULL
13751                   && h->elf.root.type == bfd_link_hash_undefweak
13752                   && ppc64_elf_howto_table[r_type]->pc_relative)
13753                 {
13754                   /* Assume this is a call protected by other code that
13755                      detects the symbol is undefined.  If this is the case,
13756                      we can safely ignore the overflow.  If not, the
13757                      program is hosed anyway, and a little warning isn't
13758                      going to help.  */
13759
13760                   continue;
13761                 }
13762
13763               if (!((*info->callbacks->reloc_overflow)
13764                     (info, (h ? &h->elf.root : NULL), sym_name,
13765                      ppc64_elf_howto_table[r_type]->name,
13766                      orig_addend, input_bfd, input_section, rel->r_offset)))
13767                 return FALSE;
13768             }
13769           else
13770             {
13771               info->callbacks->einfo
13772                 (_("%P: %H: %s reloc against `%s': error %d\n"),
13773                  input_bfd, input_section, rel->r_offset,
13774                  ppc64_elf_howto_table[r_type]->name,
13775                  sym_name,
13776                  (int) r);
13777               ret = FALSE;
13778             }
13779         }
13780     }
13781
13782   /* If we're emitting relocations, then shortly after this function
13783      returns, reloc offsets and addends for this section will be
13784      adjusted.  Worse, reloc symbol indices will be for the output
13785      file rather than the input.  Save a copy of the relocs for
13786      opd_entry_value.  */
13787   if (is_opd && (info->emitrelocations || info->relocatable))
13788     {
13789       bfd_size_type amt;
13790       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13791       rel = bfd_alloc (input_bfd, amt);
13792       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13793       ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13794       if (rel == NULL)
13795         return FALSE;
13796       memcpy (rel, relocs, amt);
13797     }
13798   return ret;
13799 }
13800
13801 /* Adjust the value of any local symbols in opd sections.  */
13802
13803 static int
13804 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13805                               const char *name ATTRIBUTE_UNUSED,
13806                               Elf_Internal_Sym *elfsym,
13807                               asection *input_sec,
13808                               struct elf_link_hash_entry *h)
13809 {
13810   struct _opd_sec_data *opd;
13811   long adjust;
13812   bfd_vma value;
13813
13814   if (h != NULL)
13815     return 1;
13816
13817   opd = get_opd_info (input_sec);
13818   if (opd == NULL || opd->adjust == NULL)
13819     return 1;
13820
13821   value = elfsym->st_value - input_sec->output_offset;
13822   if (!info->relocatable)
13823     value -= input_sec->output_section->vma;
13824
13825   adjust = opd->adjust[value / 8];
13826   if (adjust == -1)
13827     return 2;
13828
13829   elfsym->st_value += adjust;
13830   return 1;
13831 }
13832
13833 /* Finish up dynamic symbol handling.  We set the contents of various
13834    dynamic sections here.  */
13835
13836 static bfd_boolean
13837 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13838                                  struct bfd_link_info *info,
13839                                  struct elf_link_hash_entry *h,
13840                                  Elf_Internal_Sym *sym)
13841 {
13842   struct ppc_link_hash_table *htab;
13843   struct plt_entry *ent;
13844   Elf_Internal_Rela rela;
13845   bfd_byte *loc;
13846
13847   htab = ppc_hash_table (info);
13848   if (htab == NULL)
13849     return FALSE;
13850
13851   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13852     if (ent->plt.offset != (bfd_vma) -1)
13853       {
13854         /* This symbol has an entry in the procedure linkage
13855            table.  Set it up.  */
13856         if (!htab->elf.dynamic_sections_created
13857             || h->dynindx == -1)
13858           {
13859             BFD_ASSERT (h->type == STT_GNU_IFUNC
13860                         && h->def_regular
13861                         && (h->root.type == bfd_link_hash_defined
13862                             || h->root.type == bfd_link_hash_defweak));
13863             rela.r_offset = (htab->iplt->output_section->vma
13864                              + htab->iplt->output_offset
13865                              + ent->plt.offset);
13866             rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13867             rela.r_addend = (h->root.u.def.value
13868                              + h->root.u.def.section->output_offset
13869                              + h->root.u.def.section->output_section->vma
13870                              + ent->addend);
13871             loc = (htab->reliplt->contents
13872                    + (htab->reliplt->reloc_count++
13873                       * sizeof (Elf64_External_Rela)));
13874           }
13875         else
13876           {
13877             rela.r_offset = (htab->plt->output_section->vma
13878                              + htab->plt->output_offset
13879                              + ent->plt.offset);
13880             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13881             rela.r_addend = ent->addend;
13882             loc = (htab->relplt->contents
13883                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13884                       / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
13885           }
13886         bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13887       }
13888
13889   if (h->needs_copy)
13890     {
13891       /* This symbol needs a copy reloc.  Set it up.  */
13892
13893       if (h->dynindx == -1
13894           || (h->root.type != bfd_link_hash_defined
13895               && h->root.type != bfd_link_hash_defweak)
13896           || htab->relbss == NULL)
13897         abort ();
13898
13899       rela.r_offset = (h->root.u.def.value
13900                        + h->root.u.def.section->output_section->vma
13901                        + h->root.u.def.section->output_offset);
13902       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13903       rela.r_addend = 0;
13904       loc = htab->relbss->contents;
13905       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
13906       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13907     }
13908
13909   /* Mark some specially defined symbols as absolute.  */
13910   if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13911     sym->st_shndx = SHN_ABS;
13912
13913   return TRUE;
13914 }
13915
13916 /* Used to decide how to sort relocs in an optimal manner for the
13917    dynamic linker, before writing them out.  */
13918
13919 static enum elf_reloc_type_class
13920 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
13921 {
13922   enum elf_ppc64_reloc_type r_type;
13923
13924   r_type = ELF64_R_TYPE (rela->r_info);
13925   switch (r_type)
13926     {
13927     case R_PPC64_RELATIVE:
13928       return reloc_class_relative;
13929     case R_PPC64_JMP_SLOT:
13930       return reloc_class_plt;
13931     case R_PPC64_COPY:
13932       return reloc_class_copy;
13933     default:
13934       return reloc_class_normal;
13935     }
13936 }
13937
13938 /* Finish up the dynamic sections.  */
13939
13940 static bfd_boolean
13941 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13942                                    struct bfd_link_info *info)
13943 {
13944   struct ppc_link_hash_table *htab;
13945   bfd *dynobj;
13946   asection *sdyn;
13947
13948   htab = ppc_hash_table (info);
13949   if (htab == NULL)
13950     return FALSE;
13951
13952   dynobj = htab->elf.dynobj;
13953   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13954
13955   if (htab->elf.dynamic_sections_created)
13956     {
13957       Elf64_External_Dyn *dyncon, *dynconend;
13958
13959       if (sdyn == NULL || htab->got == NULL)
13960         abort ();
13961
13962       dyncon = (Elf64_External_Dyn *) sdyn->contents;
13963       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
13964       for (; dyncon < dynconend; dyncon++)
13965         {
13966           Elf_Internal_Dyn dyn;
13967           asection *s;
13968
13969           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13970
13971           switch (dyn.d_tag)
13972             {
13973             default:
13974               continue;
13975
13976             case DT_PPC64_GLINK:
13977               s = htab->glink;
13978               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13979               /* We stupidly defined DT_PPC64_GLINK to be the start
13980                  of glink rather than the first entry point, which is
13981                  what ld.so needs, and now have a bigger stub to
13982                  support automatic multiple TOCs.  */
13983               dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
13984               break;
13985
13986             case DT_PPC64_OPD:
13987               s = bfd_get_section_by_name (output_bfd, ".opd");
13988               if (s == NULL)
13989                 continue;
13990               dyn.d_un.d_ptr = s->vma;
13991               break;
13992
13993             case DT_PPC64_OPDSZ:
13994               s = bfd_get_section_by_name (output_bfd, ".opd");
13995               if (s == NULL)
13996                 continue;
13997               dyn.d_un.d_val = s->size;
13998               break;
13999
14000             case DT_PLTGOT:
14001               s = htab->plt;
14002               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14003               break;
14004
14005             case DT_JMPREL:
14006               s = htab->relplt;
14007               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14008               break;
14009
14010             case DT_PLTRELSZ:
14011               dyn.d_un.d_val = htab->relplt->size;
14012               break;
14013
14014             case DT_RELASZ:
14015               /* Don't count procedure linkage table relocs in the
14016                  overall reloc count.  */
14017               s = htab->relplt;
14018               if (s == NULL)
14019                 continue;
14020               dyn.d_un.d_val -= s->size;
14021               break;
14022
14023             case DT_RELA:
14024               /* We may not be using the standard ELF linker script.
14025                  If .rela.plt is the first .rela section, we adjust
14026                  DT_RELA to not include it.  */
14027               s = htab->relplt;
14028               if (s == NULL)
14029                 continue;
14030               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
14031                 continue;
14032               dyn.d_un.d_ptr += s->size;
14033               break;
14034             }
14035
14036           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
14037         }
14038     }
14039
14040   if (htab->got != NULL && htab->got->size != 0)
14041     {
14042       /* Fill in the first entry in the global offset table.
14043          We use it to hold the link-time TOCbase.  */
14044       bfd_put_64 (output_bfd,
14045                   elf_gp (output_bfd) + TOC_BASE_OFF,
14046                   htab->got->contents);
14047
14048       /* Set .got entry size.  */
14049       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
14050     }
14051
14052   if (htab->plt != NULL && htab->plt->size != 0)
14053     {
14054       /* Set .plt entry size.  */
14055       elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
14056         = PLT_ENTRY_SIZE;
14057     }
14058
14059   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
14060      brlt ourselves if emitrelocations.  */
14061   if (htab->brlt != NULL
14062       && htab->brlt->reloc_count != 0
14063       && !_bfd_elf_link_output_relocs (output_bfd,
14064                                        htab->brlt,
14065                                        elf_section_data (htab->brlt)->rela.hdr,
14066                                        elf_section_data (htab->brlt)->relocs,
14067                                        NULL))
14068     return FALSE;
14069
14070   if (htab->glink != NULL
14071       && htab->glink->reloc_count != 0
14072       && !_bfd_elf_link_output_relocs (output_bfd,
14073                                        htab->glink,
14074                                        elf_section_data (htab->glink)->rela.hdr,
14075                                        elf_section_data (htab->glink)->relocs,
14076                                        NULL))
14077     return FALSE;
14078
14079
14080   if (htab->glink_eh_frame != NULL
14081       && htab->glink_eh_frame->sec_info_type == ELF_INFO_TYPE_EH_FRAME
14082       && !_bfd_elf_write_section_eh_frame (output_bfd, info,
14083                                            htab->glink_eh_frame,
14084                                            htab->glink_eh_frame->contents))
14085     return FALSE;
14086
14087   /* We need to handle writing out multiple GOT sections ourselves,
14088      since we didn't add them to DYNOBJ.  We know dynobj is the first
14089      bfd.  */
14090   while ((dynobj = dynobj->link_next) != NULL)
14091     {
14092       asection *s;
14093
14094       if (!is_ppc64_elf (dynobj))
14095         continue;
14096
14097       s = ppc64_elf_tdata (dynobj)->got;
14098       if (s != NULL
14099           && s->size != 0
14100           && s->output_section != bfd_abs_section_ptr
14101           && !bfd_set_section_contents (output_bfd, s->output_section,
14102                                         s->contents, s->output_offset,
14103                                         s->size))
14104         return FALSE;
14105       s = ppc64_elf_tdata (dynobj)->relgot;
14106       if (s != NULL
14107           && s->size != 0
14108           && s->output_section != bfd_abs_section_ptr
14109           && !bfd_set_section_contents (output_bfd, s->output_section,
14110                                         s->contents, s->output_offset,
14111                                         s->size))
14112         return FALSE;
14113     }
14114
14115   return TRUE;
14116 }
14117
14118 #include "elf64-target.h"
14119
14120 /* FreeBSD support */
14121
14122 #undef  TARGET_LITTLE_SYM
14123 #undef  TARGET_LITTLE_NAME
14124
14125 #undef  TARGET_BIG_SYM
14126 #define TARGET_BIG_SYM  bfd_elf64_powerpc_freebsd_vec
14127 #undef  TARGET_BIG_NAME
14128 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
14129
14130 #undef  ELF_OSABI
14131 #define ELF_OSABI       ELFOSABI_FREEBSD
14132
14133 #undef  elf64_bed
14134 #define elf64_bed       elf64_powerpc_fbsd_bed
14135
14136 #include "elf64-target.h"
14137