daily update
[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 *, bfd_boolean);
59
60 #define TARGET_LITTLE_SYM       bfd_elf64_powerpcle_vec
61 #define TARGET_LITTLE_NAME      "elf64-powerpcle"
62 #define TARGET_BIG_SYM          bfd_elf64_powerpc_vec
63 #define TARGET_BIG_NAME         "elf64-powerpc"
64 #define ELF_ARCH                bfd_arch_powerpc
65 #define ELF_TARGET_ID           PPC64_ELF_DATA
66 #define ELF_MACHINE_CODE        EM_PPC64
67 #define ELF_MAXPAGESIZE         0x10000
68 #define ELF_COMMONPAGESIZE      0x1000
69 #define elf_info_to_howto       ppc64_elf_info_to_howto
70
71 #define elf_backend_want_got_sym 0
72 #define elf_backend_want_plt_sym 0
73 #define elf_backend_plt_alignment 3
74 #define elf_backend_plt_not_loaded 1
75 #define elf_backend_got_header_size 8
76 #define elf_backend_can_gc_sections 1
77 #define elf_backend_can_refcount 1
78 #define elf_backend_rela_normal 1
79 #define elf_backend_default_execstack 0
80
81 #define bfd_elf64_mkobject                    ppc64_elf_mkobject
82 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
83 #define bfd_elf64_bfd_reloc_name_lookup       ppc64_elf_reloc_name_lookup
84 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
85 #define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
86 #define bfd_elf64_new_section_hook            ppc64_elf_new_section_hook
87 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
88 #define bfd_elf64_bfd_link_hash_table_free    ppc64_elf_link_hash_table_free
89 #define bfd_elf64_get_synthetic_symtab        ppc64_elf_get_synthetic_symtab
90 #define bfd_elf64_bfd_link_just_syms          ppc64_elf_link_just_syms
91
92 #define elf_backend_object_p                  ppc64_elf_object_p
93 #define elf_backend_grok_prstatus             ppc64_elf_grok_prstatus
94 #define elf_backend_grok_psinfo               ppc64_elf_grok_psinfo
95 #define elf_backend_write_core_note           ppc64_elf_write_core_note
96 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
97 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
98 #define elf_backend_add_symbol_hook           ppc64_elf_add_symbol_hook
99 #define elf_backend_check_directives          ppc64_elf_process_dot_syms
100 #define elf_backend_notice_as_needed          ppc64_elf_notice_as_needed
101 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
102 #define elf_backend_check_relocs              ppc64_elf_check_relocs
103 #define elf_backend_gc_keep                   ppc64_elf_gc_keep
104 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
105 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
106 #define elf_backend_gc_sweep_hook             ppc64_elf_gc_sweep_hook
107 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
108 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
109 #define elf_backend_maybe_function_sym        ppc64_elf_maybe_function_sym
110 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
111 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
112 #define elf_backend_init_index_section        _bfd_elf_init_2_index_sections
113 #define elf_backend_action_discarded          ppc64_elf_action_discarded
114 #define elf_backend_relocate_section          ppc64_elf_relocate_section
115 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
116 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
117 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
118 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
119 #define elf_backend_special_sections          ppc64_elf_special_sections
120 #define elf_backend_post_process_headers      _bfd_elf_set_osabi
121 #define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
122
123 /* The name of the dynamic interpreter.  This is put in the .interp
124    section.  */
125 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
126
127 /* The size in bytes of an entry in the procedure linkage table.  */
128 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
129
130 /* The initial size of the plt reserved for the dynamic linker.  */
131 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
132
133 /* Offsets to some stack save slots.  */
134 #define STK_LR 16
135 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
136 /* This one is dodgy.  ABIv2 does not have a linker word, so use the
137    CR save slot.  Used only by optimised __tls_get_addr call stub,
138    relying on __tls_get_addr_opt not saving CR..  */
139 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
140
141 /* TOC base pointers offset from start of TOC.  */
142 #define TOC_BASE_OFF    0x8000
143
144 /* Offset of tp and dtp pointers from start of TLS block.  */
145 #define TP_OFFSET       0x7000
146 #define DTP_OFFSET      0x8000
147
148 /* .plt call stub instructions.  The normal stub is like this, but
149    sometimes the .plt entry crosses a 64k boundary and we need to
150    insert an addi to adjust r11.  */
151 #define STD_R2_0R1      0xf8410000      /* std   %r2,0+40(%r1)       */
152 #define ADDIS_R11_R2    0x3d620000      /* addis %r11,%r2,xxx@ha     */
153 #define LD_R12_0R11     0xe98b0000      /* ld    %r12,xxx+0@l(%r11)  */
154 #define MTCTR_R12       0x7d8903a6      /* mtctr %r12                */
155 #define LD_R2_0R11      0xe84b0000      /* ld    %r2,xxx+8@l(%r11)   */
156 #define LD_R11_0R11     0xe96b0000      /* ld    %r11,xxx+16@l(%r11) */
157 #define BCTR            0x4e800420      /* bctr                      */
158
159 #define ADDI_R11_R11    0x396b0000      /* addi %r11,%r11,off@l  */
160 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
161 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
162
163 #define XOR_R2_R12_R12  0x7d826278      /* xor   %r2,%r12,%r12   */
164 #define ADD_R11_R11_R2  0x7d6b1214      /* add   %r11,%r11,%r2   */
165 #define XOR_R11_R12_R12 0x7d8b6278      /* xor   %r11,%r12,%r12  */
166 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
167 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
168 #define BNECTR          0x4ca20420      /* bnectr+               */
169 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
170
171 #define LD_R12_0R2      0xe9820000      /* ld    %r12,xxx+0(%r2) */
172 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
173 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
174
175 #define LD_R2_0R1       0xe8410000      /* ld    %r2,0(%r1)      */
176
177 /* glink call stub instructions.  We enter with the index in R0.  */
178 #define GLINK_CALL_STUB_SIZE (16*4)
179                                         /* 0:                           */
180                                         /*  .quad plt0-1f               */
181                                         /* __glink:                     */
182 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
183 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
184                                         /* 1:                           */
185 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
186                                         /*  ld %2,(0b-1b)(%11)          */
187 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
188 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
189                                         /*  ld %12,0(%11)               */
190                                         /*  ld %2,8(%11)                */
191                                         /*  mtctr %12                   */
192                                         /*  ld %11,16(%11)              */
193                                         /*  bctr                        */
194 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
195 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
196 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
197 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
198 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
199
200 /* Pad with this.  */
201 #define NOP             0x60000000
202
203 /* Some other nops.  */
204 #define CROR_151515     0x4def7b82
205 #define CROR_313131     0x4ffffb82
206
207 /* .glink entries for the first 32k functions are two instructions.  */
208 #define LI_R0_0         0x38000000      /* li    %r0,0          */
209 #define B_DOT           0x48000000      /* b     .              */
210
211 /* After that, we need two instructions to load the index, followed by
212    a branch.  */
213 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
214 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
215
216 /* Instructions used by the save and restore reg functions.  */
217 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
218 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
219 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
220 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
221 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
222 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
223 #define LI_R12_0        0x39800000      /* li    %r12,0         */
224 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
225 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
226 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
227 #define BLR             0x4e800020      /* blr                  */
228
229 /* Since .opd is an array of descriptors and each entry will end up
230    with identical R_PPC64_RELATIVE relocs, there is really no need to
231    propagate .opd relocs;  The dynamic linker should be taught to
232    relocate .opd without reloc entries.  */
233 #ifndef NO_OPD_RELOCS
234 #define NO_OPD_RELOCS 0
235 #endif
236 \f
237 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
238
239 /* Relocation HOWTO's.  */
240 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
241
242 static reloc_howto_type ppc64_elf_howto_raw[] = {
243   /* This reloc does nothing.  */
244   HOWTO (R_PPC64_NONE,          /* type */
245          0,                     /* rightshift */
246          2,                     /* size (0 = byte, 1 = short, 2 = long) */
247          32,                    /* bitsize */
248          FALSE,                 /* pc_relative */
249          0,                     /* bitpos */
250          complain_overflow_dont, /* complain_on_overflow */
251          bfd_elf_generic_reloc, /* special_function */
252          "R_PPC64_NONE",        /* name */
253          FALSE,                 /* partial_inplace */
254          0,                     /* src_mask */
255          0,                     /* dst_mask */
256          FALSE),                /* pcrel_offset */
257
258   /* A standard 32 bit relocation.  */
259   HOWTO (R_PPC64_ADDR32,        /* type */
260          0,                     /* rightshift */
261          2,                     /* size (0 = byte, 1 = short, 2 = long) */
262          32,                    /* bitsize */
263          FALSE,                 /* pc_relative */
264          0,                     /* bitpos */
265          complain_overflow_bitfield, /* complain_on_overflow */
266          bfd_elf_generic_reloc, /* special_function */
267          "R_PPC64_ADDR32",      /* name */
268          FALSE,                 /* partial_inplace */
269          0,                     /* src_mask */
270          0xffffffff,            /* dst_mask */
271          FALSE),                /* pcrel_offset */
272
273   /* An absolute 26 bit branch; the lower two bits must be zero.
274      FIXME: we don't check that, we just clear them.  */
275   HOWTO (R_PPC64_ADDR24,        /* type */
276          0,                     /* rightshift */
277          2,                     /* size (0 = byte, 1 = short, 2 = long) */
278          26,                    /* bitsize */
279          FALSE,                 /* pc_relative */
280          0,                     /* bitpos */
281          complain_overflow_bitfield, /* complain_on_overflow */
282          bfd_elf_generic_reloc, /* special_function */
283          "R_PPC64_ADDR24",      /* name */
284          FALSE,                 /* partial_inplace */
285          0,                     /* src_mask */
286          0x03fffffc,            /* dst_mask */
287          FALSE),                /* pcrel_offset */
288
289   /* A standard 16 bit relocation.  */
290   HOWTO (R_PPC64_ADDR16,        /* type */
291          0,                     /* rightshift */
292          1,                     /* size (0 = byte, 1 = short, 2 = long) */
293          16,                    /* bitsize */
294          FALSE,                 /* pc_relative */
295          0,                     /* bitpos */
296          complain_overflow_bitfield, /* complain_on_overflow */
297          bfd_elf_generic_reloc, /* special_function */
298          "R_PPC64_ADDR16",      /* name */
299          FALSE,                 /* partial_inplace */
300          0,                     /* src_mask */
301          0xffff,                /* dst_mask */
302          FALSE),                /* pcrel_offset */
303
304   /* A 16 bit relocation without overflow.  */
305   HOWTO (R_PPC64_ADDR16_LO,     /* type */
306          0,                     /* rightshift */
307          1,                     /* size (0 = byte, 1 = short, 2 = long) */
308          16,                    /* bitsize */
309          FALSE,                 /* pc_relative */
310          0,                     /* bitpos */
311          complain_overflow_dont,/* complain_on_overflow */
312          bfd_elf_generic_reloc, /* special_function */
313          "R_PPC64_ADDR16_LO",   /* name */
314          FALSE,                 /* partial_inplace */
315          0,                     /* src_mask */
316          0xffff,                /* dst_mask */
317          FALSE),                /* pcrel_offset */
318
319   /* Bits 16-31 of an address.  */
320   HOWTO (R_PPC64_ADDR16_HI,     /* type */
321          16,                    /* rightshift */
322          1,                     /* size (0 = byte, 1 = short, 2 = long) */
323          16,                    /* bitsize */
324          FALSE,                 /* pc_relative */
325          0,                     /* bitpos */
326          complain_overflow_signed, /* complain_on_overflow */
327          bfd_elf_generic_reloc, /* special_function */
328          "R_PPC64_ADDR16_HI",   /* name */
329          FALSE,                 /* partial_inplace */
330          0,                     /* src_mask */
331          0xffff,                /* dst_mask */
332          FALSE),                /* pcrel_offset */
333
334   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
335      bits, treated as a signed number, is negative.  */
336   HOWTO (R_PPC64_ADDR16_HA,     /* type */
337          16,                    /* rightshift */
338          1,                     /* size (0 = byte, 1 = short, 2 = long) */
339          16,                    /* bitsize */
340          FALSE,                 /* pc_relative */
341          0,                     /* bitpos */
342          complain_overflow_signed, /* complain_on_overflow */
343          ppc64_elf_ha_reloc,    /* special_function */
344          "R_PPC64_ADDR16_HA",   /* name */
345          FALSE,                 /* partial_inplace */
346          0,                     /* src_mask */
347          0xffff,                /* dst_mask */
348          FALSE),                /* pcrel_offset */
349
350   /* An absolute 16 bit branch; the lower two bits must be zero.
351      FIXME: we don't check that, we just clear them.  */
352   HOWTO (R_PPC64_ADDR14,        /* type */
353          0,                     /* rightshift */
354          2,                     /* size (0 = byte, 1 = short, 2 = long) */
355          16,                    /* bitsize */
356          FALSE,                 /* pc_relative */
357          0,                     /* bitpos */
358          complain_overflow_bitfield, /* complain_on_overflow */
359          ppc64_elf_branch_reloc, /* special_function */
360          "R_PPC64_ADDR14",      /* name */
361          FALSE,                 /* partial_inplace */
362          0,                     /* src_mask */
363          0x0000fffc,            /* dst_mask */
364          FALSE),                /* pcrel_offset */
365
366   /* An absolute 16 bit branch, for which bit 10 should be set to
367      indicate that the branch is expected to be taken.  The lower two
368      bits must be zero.  */
369   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
370          0,                     /* rightshift */
371          2,                     /* size (0 = byte, 1 = short, 2 = long) */
372          16,                    /* bitsize */
373          FALSE,                 /* pc_relative */
374          0,                     /* bitpos */
375          complain_overflow_bitfield, /* complain_on_overflow */
376          ppc64_elf_brtaken_reloc, /* special_function */
377          "R_PPC64_ADDR14_BRTAKEN",/* name */
378          FALSE,                 /* partial_inplace */
379          0,                     /* src_mask */
380          0x0000fffc,            /* dst_mask */
381          FALSE),                /* pcrel_offset */
382
383   /* An absolute 16 bit branch, for which bit 10 should be set to
384      indicate that the branch is not expected to be taken.  The lower
385      two bits must be zero.  */
386   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
387          0,                     /* rightshift */
388          2,                     /* size (0 = byte, 1 = short, 2 = long) */
389          16,                    /* bitsize */
390          FALSE,                 /* pc_relative */
391          0,                     /* bitpos */
392          complain_overflow_bitfield, /* complain_on_overflow */
393          ppc64_elf_brtaken_reloc, /* special_function */
394          "R_PPC64_ADDR14_BRNTAKEN",/* name */
395          FALSE,                 /* partial_inplace */
396          0,                     /* src_mask */
397          0x0000fffc,            /* dst_mask */
398          FALSE),                /* pcrel_offset */
399
400   /* A relative 26 bit branch; the lower two bits must be zero.  */
401   HOWTO (R_PPC64_REL24,         /* type */
402          0,                     /* rightshift */
403          2,                     /* size (0 = byte, 1 = short, 2 = long) */
404          26,                    /* bitsize */
405          TRUE,                  /* pc_relative */
406          0,                     /* bitpos */
407          complain_overflow_signed, /* complain_on_overflow */
408          ppc64_elf_branch_reloc, /* special_function */
409          "R_PPC64_REL24",       /* name */
410          FALSE,                 /* partial_inplace */
411          0,                     /* src_mask */
412          0x03fffffc,            /* dst_mask */
413          TRUE),                 /* pcrel_offset */
414
415   /* A relative 16 bit branch; the lower two bits must be zero.  */
416   HOWTO (R_PPC64_REL14,         /* type */
417          0,                     /* rightshift */
418          2,                     /* size (0 = byte, 1 = short, 2 = long) */
419          16,                    /* bitsize */
420          TRUE,                  /* pc_relative */
421          0,                     /* bitpos */
422          complain_overflow_signed, /* complain_on_overflow */
423          ppc64_elf_branch_reloc, /* special_function */
424          "R_PPC64_REL14",       /* name */
425          FALSE,                 /* partial_inplace */
426          0,                     /* src_mask */
427          0x0000fffc,            /* dst_mask */
428          TRUE),                 /* pcrel_offset */
429
430   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
431      the branch is expected to be taken.  The lower two bits must be
432      zero.  */
433   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
434          0,                     /* rightshift */
435          2,                     /* size (0 = byte, 1 = short, 2 = long) */
436          16,                    /* bitsize */
437          TRUE,                  /* pc_relative */
438          0,                     /* bitpos */
439          complain_overflow_signed, /* complain_on_overflow */
440          ppc64_elf_brtaken_reloc, /* special_function */
441          "R_PPC64_REL14_BRTAKEN", /* name */
442          FALSE,                 /* partial_inplace */
443          0,                     /* src_mask */
444          0x0000fffc,            /* dst_mask */
445          TRUE),                 /* pcrel_offset */
446
447   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
448      the branch is not expected to be taken.  The lower two bits must
449      be zero.  */
450   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
451          0,                     /* rightshift */
452          2,                     /* size (0 = byte, 1 = short, 2 = long) */
453          16,                    /* bitsize */
454          TRUE,                  /* pc_relative */
455          0,                     /* bitpos */
456          complain_overflow_signed, /* complain_on_overflow */
457          ppc64_elf_brtaken_reloc, /* special_function */
458          "R_PPC64_REL14_BRNTAKEN",/* name */
459          FALSE,                 /* partial_inplace */
460          0,                     /* src_mask */
461          0x0000fffc,            /* dst_mask */
462          TRUE),                 /* pcrel_offset */
463
464   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
465      symbol.  */
466   HOWTO (R_PPC64_GOT16,         /* type */
467          0,                     /* rightshift */
468          1,                     /* size (0 = byte, 1 = short, 2 = long) */
469          16,                    /* bitsize */
470          FALSE,                 /* pc_relative */
471          0,                     /* bitpos */
472          complain_overflow_signed, /* complain_on_overflow */
473          ppc64_elf_unhandled_reloc, /* special_function */
474          "R_PPC64_GOT16",       /* name */
475          FALSE,                 /* partial_inplace */
476          0,                     /* src_mask */
477          0xffff,                /* dst_mask */
478          FALSE),                /* pcrel_offset */
479
480   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
481      the symbol.  */
482   HOWTO (R_PPC64_GOT16_LO,      /* type */
483          0,                     /* rightshift */
484          1,                     /* size (0 = byte, 1 = short, 2 = long) */
485          16,                    /* bitsize */
486          FALSE,                 /* pc_relative */
487          0,                     /* bitpos */
488          complain_overflow_dont, /* complain_on_overflow */
489          ppc64_elf_unhandled_reloc, /* special_function */
490          "R_PPC64_GOT16_LO",    /* name */
491          FALSE,                 /* partial_inplace */
492          0,                     /* src_mask */
493          0xffff,                /* dst_mask */
494          FALSE),                /* pcrel_offset */
495
496   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
497      the symbol.  */
498   HOWTO (R_PPC64_GOT16_HI,      /* type */
499          16,                    /* rightshift */
500          1,                     /* size (0 = byte, 1 = short, 2 = long) */
501          16,                    /* bitsize */
502          FALSE,                 /* pc_relative */
503          0,                     /* bitpos */
504          complain_overflow_signed,/* complain_on_overflow */
505          ppc64_elf_unhandled_reloc, /* special_function */
506          "R_PPC64_GOT16_HI",    /* name */
507          FALSE,                 /* partial_inplace */
508          0,                     /* src_mask */
509          0xffff,                /* dst_mask */
510          FALSE),                /* pcrel_offset */
511
512   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
513      the symbol.  */
514   HOWTO (R_PPC64_GOT16_HA,      /* type */
515          16,                    /* rightshift */
516          1,                     /* size (0 = byte, 1 = short, 2 = long) */
517          16,                    /* bitsize */
518          FALSE,                 /* pc_relative */
519          0,                     /* bitpos */
520          complain_overflow_signed,/* complain_on_overflow */
521          ppc64_elf_unhandled_reloc, /* special_function */
522          "R_PPC64_GOT16_HA",    /* name */
523          FALSE,                 /* partial_inplace */
524          0,                     /* src_mask */
525          0xffff,                /* dst_mask */
526          FALSE),                /* pcrel_offset */
527
528   /* This is used only by the dynamic linker.  The symbol should exist
529      both in the object being run and in some shared library.  The
530      dynamic linker copies the data addressed by the symbol from the
531      shared library into the object, because the object being
532      run has to have the data at some particular address.  */
533   HOWTO (R_PPC64_COPY,          /* type */
534          0,                     /* rightshift */
535          0,                     /* this one is variable size */
536          0,                     /* bitsize */
537          FALSE,                 /* pc_relative */
538          0,                     /* bitpos */
539          complain_overflow_dont, /* complain_on_overflow */
540          ppc64_elf_unhandled_reloc, /* special_function */
541          "R_PPC64_COPY",        /* name */
542          FALSE,                 /* partial_inplace */
543          0,                     /* src_mask */
544          0,                     /* dst_mask */
545          FALSE),                /* pcrel_offset */
546
547   /* Like R_PPC64_ADDR64, but used when setting global offset table
548      entries.  */
549   HOWTO (R_PPC64_GLOB_DAT,      /* type */
550          0,                     /* rightshift */
551          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
552          64,                    /* bitsize */
553          FALSE,                 /* pc_relative */
554          0,                     /* bitpos */
555          complain_overflow_dont, /* complain_on_overflow */
556          ppc64_elf_unhandled_reloc,  /* special_function */
557          "R_PPC64_GLOB_DAT",    /* name */
558          FALSE,                 /* partial_inplace */
559          0,                     /* src_mask */
560          ONES (64),             /* dst_mask */
561          FALSE),                /* pcrel_offset */
562
563   /* Created by the link editor.  Marks a procedure linkage table
564      entry for a symbol.  */
565   HOWTO (R_PPC64_JMP_SLOT,      /* type */
566          0,                     /* rightshift */
567          0,                     /* size (0 = byte, 1 = short, 2 = long) */
568          0,                     /* bitsize */
569          FALSE,                 /* pc_relative */
570          0,                     /* bitpos */
571          complain_overflow_dont, /* complain_on_overflow */
572          ppc64_elf_unhandled_reloc, /* special_function */
573          "R_PPC64_JMP_SLOT",    /* name */
574          FALSE,                 /* partial_inplace */
575          0,                     /* src_mask */
576          0,                     /* dst_mask */
577          FALSE),                /* pcrel_offset */
578
579   /* Used only by the dynamic linker.  When the object is run, this
580      doubleword64 is set to the load address of the object, plus the
581      addend.  */
582   HOWTO (R_PPC64_RELATIVE,      /* type */
583          0,                     /* rightshift */
584          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
585          64,                    /* bitsize */
586          FALSE,                 /* pc_relative */
587          0,                     /* bitpos */
588          complain_overflow_dont, /* complain_on_overflow */
589          bfd_elf_generic_reloc, /* special_function */
590          "R_PPC64_RELATIVE",    /* name */
591          FALSE,                 /* partial_inplace */
592          0,                     /* src_mask */
593          ONES (64),             /* dst_mask */
594          FALSE),                /* pcrel_offset */
595
596   /* Like R_PPC64_ADDR32, but may be unaligned.  */
597   HOWTO (R_PPC64_UADDR32,       /* type */
598          0,                     /* rightshift */
599          2,                     /* size (0 = byte, 1 = short, 2 = long) */
600          32,                    /* bitsize */
601          FALSE,                 /* pc_relative */
602          0,                     /* bitpos */
603          complain_overflow_bitfield, /* complain_on_overflow */
604          bfd_elf_generic_reloc, /* special_function */
605          "R_PPC64_UADDR32",     /* name */
606          FALSE,                 /* partial_inplace */
607          0,                     /* src_mask */
608          0xffffffff,            /* dst_mask */
609          FALSE),                /* pcrel_offset */
610
611   /* Like R_PPC64_ADDR16, but may be unaligned.  */
612   HOWTO (R_PPC64_UADDR16,       /* type */
613          0,                     /* rightshift */
614          1,                     /* size (0 = byte, 1 = short, 2 = long) */
615          16,                    /* bitsize */
616          FALSE,                 /* pc_relative */
617          0,                     /* bitpos */
618          complain_overflow_bitfield, /* complain_on_overflow */
619          bfd_elf_generic_reloc, /* special_function */
620          "R_PPC64_UADDR16",     /* name */
621          FALSE,                 /* partial_inplace */
622          0,                     /* src_mask */
623          0xffff,                /* dst_mask */
624          FALSE),                /* pcrel_offset */
625
626   /* 32-bit PC relative.  */
627   HOWTO (R_PPC64_REL32,         /* type */
628          0,                     /* rightshift */
629          2,                     /* size (0 = byte, 1 = short, 2 = long) */
630          32,                    /* bitsize */
631          TRUE,                  /* pc_relative */
632          0,                     /* bitpos */
633          /* FIXME: Verify.  Was complain_overflow_bitfield.  */
634          complain_overflow_signed, /* complain_on_overflow */
635          bfd_elf_generic_reloc, /* special_function */
636          "R_PPC64_REL32",       /* name */
637          FALSE,                 /* partial_inplace */
638          0,                     /* src_mask */
639          0xffffffff,            /* dst_mask */
640          TRUE),                 /* pcrel_offset */
641
642   /* 32-bit relocation to the symbol's procedure linkage table.  */
643   HOWTO (R_PPC64_PLT32,         /* type */
644          0,                     /* rightshift */
645          2,                     /* size (0 = byte, 1 = short, 2 = long) */
646          32,                    /* bitsize */
647          FALSE,                 /* pc_relative */
648          0,                     /* bitpos */
649          complain_overflow_bitfield, /* complain_on_overflow */
650          ppc64_elf_unhandled_reloc, /* special_function */
651          "R_PPC64_PLT32",       /* name */
652          FALSE,                 /* partial_inplace */
653          0,                     /* src_mask */
654          0xffffffff,            /* dst_mask */
655          FALSE),                /* pcrel_offset */
656
657   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
658      FIXME: R_PPC64_PLTREL32 not supported.  */
659   HOWTO (R_PPC64_PLTREL32,      /* type */
660          0,                     /* rightshift */
661          2,                     /* size (0 = byte, 1 = short, 2 = long) */
662          32,                    /* bitsize */
663          TRUE,                  /* pc_relative */
664          0,                     /* bitpos */
665          complain_overflow_signed, /* complain_on_overflow */
666          bfd_elf_generic_reloc, /* special_function */
667          "R_PPC64_PLTREL32",    /* name */
668          FALSE,                 /* partial_inplace */
669          0,                     /* src_mask */
670          0xffffffff,            /* dst_mask */
671          TRUE),                 /* pcrel_offset */
672
673   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
674      the symbol.  */
675   HOWTO (R_PPC64_PLT16_LO,      /* type */
676          0,                     /* rightshift */
677          1,                     /* size (0 = byte, 1 = short, 2 = long) */
678          16,                    /* bitsize */
679          FALSE,                 /* pc_relative */
680          0,                     /* bitpos */
681          complain_overflow_dont, /* complain_on_overflow */
682          ppc64_elf_unhandled_reloc, /* special_function */
683          "R_PPC64_PLT16_LO",    /* name */
684          FALSE,                 /* partial_inplace */
685          0,                     /* src_mask */
686          0xffff,                /* dst_mask */
687          FALSE),                /* pcrel_offset */
688
689   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
690      the symbol.  */
691   HOWTO (R_PPC64_PLT16_HI,      /* type */
692          16,                    /* rightshift */
693          1,                     /* size (0 = byte, 1 = short, 2 = long) */
694          16,                    /* bitsize */
695          FALSE,                 /* pc_relative */
696          0,                     /* bitpos */
697          complain_overflow_signed, /* complain_on_overflow */
698          ppc64_elf_unhandled_reloc, /* special_function */
699          "R_PPC64_PLT16_HI",    /* name */
700          FALSE,                 /* partial_inplace */
701          0,                     /* src_mask */
702          0xffff,                /* dst_mask */
703          FALSE),                /* pcrel_offset */
704
705   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
706      the symbol.  */
707   HOWTO (R_PPC64_PLT16_HA,      /* type */
708          16,                    /* rightshift */
709          1,                     /* size (0 = byte, 1 = short, 2 = long) */
710          16,                    /* bitsize */
711          FALSE,                 /* pc_relative */
712          0,                     /* bitpos */
713          complain_overflow_signed, /* complain_on_overflow */
714          ppc64_elf_unhandled_reloc, /* special_function */
715          "R_PPC64_PLT16_HA",    /* name */
716          FALSE,                 /* partial_inplace */
717          0,                     /* src_mask */
718          0xffff,                /* dst_mask */
719          FALSE),                /* pcrel_offset */
720
721   /* 16-bit section relative relocation.  */
722   HOWTO (R_PPC64_SECTOFF,       /* type */
723          0,                     /* rightshift */
724          1,                     /* size (0 = byte, 1 = short, 2 = long) */
725          16,                    /* bitsize */
726          FALSE,                 /* pc_relative */
727          0,                     /* bitpos */
728          complain_overflow_bitfield, /* complain_on_overflow */
729          ppc64_elf_sectoff_reloc, /* special_function */
730          "R_PPC64_SECTOFF",     /* name */
731          FALSE,                 /* partial_inplace */
732          0,                     /* src_mask */
733          0xffff,                /* dst_mask */
734          FALSE),                /* pcrel_offset */
735
736   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
737   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
738          0,                     /* rightshift */
739          1,                     /* size (0 = byte, 1 = short, 2 = long) */
740          16,                    /* bitsize */
741          FALSE,                 /* pc_relative */
742          0,                     /* bitpos */
743          complain_overflow_dont, /* complain_on_overflow */
744          ppc64_elf_sectoff_reloc, /* special_function */
745          "R_PPC64_SECTOFF_LO",  /* name */
746          FALSE,                 /* partial_inplace */
747          0,                     /* src_mask */
748          0xffff,                /* dst_mask */
749          FALSE),                /* pcrel_offset */
750
751   /* 16-bit upper half section relative relocation.  */
752   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
753          16,                    /* rightshift */
754          1,                     /* size (0 = byte, 1 = short, 2 = long) */
755          16,                    /* bitsize */
756          FALSE,                 /* pc_relative */
757          0,                     /* bitpos */
758          complain_overflow_signed, /* complain_on_overflow */
759          ppc64_elf_sectoff_reloc, /* special_function */
760          "R_PPC64_SECTOFF_HI",  /* name */
761          FALSE,                 /* partial_inplace */
762          0,                     /* src_mask */
763          0xffff,                /* dst_mask */
764          FALSE),                /* pcrel_offset */
765
766   /* 16-bit upper half adjusted section relative relocation.  */
767   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
768          16,                    /* rightshift */
769          1,                     /* size (0 = byte, 1 = short, 2 = long) */
770          16,                    /* bitsize */
771          FALSE,                 /* pc_relative */
772          0,                     /* bitpos */
773          complain_overflow_signed, /* complain_on_overflow */
774          ppc64_elf_sectoff_ha_reloc, /* special_function */
775          "R_PPC64_SECTOFF_HA",  /* name */
776          FALSE,                 /* partial_inplace */
777          0,                     /* src_mask */
778          0xffff,                /* dst_mask */
779          FALSE),                /* pcrel_offset */
780
781   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
782   HOWTO (R_PPC64_REL30,         /* type */
783          2,                     /* rightshift */
784          2,                     /* size (0 = byte, 1 = short, 2 = long) */
785          30,                    /* bitsize */
786          TRUE,                  /* pc_relative */
787          0,                     /* bitpos */
788          complain_overflow_dont, /* complain_on_overflow */
789          bfd_elf_generic_reloc, /* special_function */
790          "R_PPC64_REL30",       /* name */
791          FALSE,                 /* partial_inplace */
792          0,                     /* src_mask */
793          0xfffffffc,            /* dst_mask */
794          TRUE),                 /* pcrel_offset */
795
796   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
797
798   /* A standard 64-bit relocation.  */
799   HOWTO (R_PPC64_ADDR64,        /* type */
800          0,                     /* rightshift */
801          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
802          64,                    /* bitsize */
803          FALSE,                 /* pc_relative */
804          0,                     /* bitpos */
805          complain_overflow_dont, /* complain_on_overflow */
806          bfd_elf_generic_reloc, /* special_function */
807          "R_PPC64_ADDR64",      /* name */
808          FALSE,                 /* partial_inplace */
809          0,                     /* src_mask */
810          ONES (64),             /* dst_mask */
811          FALSE),                /* pcrel_offset */
812
813   /* The bits 32-47 of an address.  */
814   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
815          32,                    /* rightshift */
816          1,                     /* size (0 = byte, 1 = short, 2 = long) */
817          16,                    /* bitsize */
818          FALSE,                 /* pc_relative */
819          0,                     /* bitpos */
820          complain_overflow_dont, /* complain_on_overflow */
821          bfd_elf_generic_reloc, /* special_function */
822          "R_PPC64_ADDR16_HIGHER", /* name */
823          FALSE,                 /* partial_inplace */
824          0,                     /* src_mask */
825          0xffff,                /* dst_mask */
826          FALSE),                /* pcrel_offset */
827
828   /* The bits 32-47 of an address, plus 1 if the contents of the low
829      16 bits, treated as a signed number, is negative.  */
830   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
831          32,                    /* rightshift */
832          1,                     /* size (0 = byte, 1 = short, 2 = long) */
833          16,                    /* bitsize */
834          FALSE,                 /* pc_relative */
835          0,                     /* bitpos */
836          complain_overflow_dont, /* complain_on_overflow */
837          ppc64_elf_ha_reloc,    /* special_function */
838          "R_PPC64_ADDR16_HIGHERA", /* name */
839          FALSE,                 /* partial_inplace */
840          0,                     /* src_mask */
841          0xffff,                /* dst_mask */
842          FALSE),                /* pcrel_offset */
843
844   /* The bits 48-63 of an address.  */
845   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
846          48,                    /* rightshift */
847          1,                     /* size (0 = byte, 1 = short, 2 = long) */
848          16,                    /* bitsize */
849          FALSE,                 /* pc_relative */
850          0,                     /* bitpos */
851          complain_overflow_dont, /* complain_on_overflow */
852          bfd_elf_generic_reloc, /* special_function */
853          "R_PPC64_ADDR16_HIGHEST", /* name */
854          FALSE,                 /* partial_inplace */
855          0,                     /* src_mask */
856          0xffff,                /* dst_mask */
857          FALSE),                /* pcrel_offset */
858
859   /* The bits 48-63 of an address, plus 1 if the contents of the low
860      16 bits, treated as a signed number, is negative.  */
861   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
862          48,                    /* rightshift */
863          1,                     /* size (0 = byte, 1 = short, 2 = long) */
864          16,                    /* bitsize */
865          FALSE,                 /* pc_relative */
866          0,                     /* bitpos */
867          complain_overflow_dont, /* complain_on_overflow */
868          ppc64_elf_ha_reloc,    /* special_function */
869          "R_PPC64_ADDR16_HIGHESTA", /* name */
870          FALSE,                 /* partial_inplace */
871          0,                     /* src_mask */
872          0xffff,                /* dst_mask */
873          FALSE),                /* pcrel_offset */
874
875   /* Like ADDR64, but may be unaligned.  */
876   HOWTO (R_PPC64_UADDR64,       /* type */
877          0,                     /* rightshift */
878          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
879          64,                    /* bitsize */
880          FALSE,                 /* pc_relative */
881          0,                     /* bitpos */
882          complain_overflow_dont, /* complain_on_overflow */
883          bfd_elf_generic_reloc, /* special_function */
884          "R_PPC64_UADDR64",     /* name */
885          FALSE,                 /* partial_inplace */
886          0,                     /* src_mask */
887          ONES (64),             /* dst_mask */
888          FALSE),                /* pcrel_offset */
889
890   /* 64-bit relative relocation.  */
891   HOWTO (R_PPC64_REL64,         /* type */
892          0,                     /* rightshift */
893          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
894          64,                    /* bitsize */
895          TRUE,                  /* pc_relative */
896          0,                     /* bitpos */
897          complain_overflow_dont, /* complain_on_overflow */
898          bfd_elf_generic_reloc, /* special_function */
899          "R_PPC64_REL64",       /* name */
900          FALSE,                 /* partial_inplace */
901          0,                     /* src_mask */
902          ONES (64),             /* dst_mask */
903          TRUE),                 /* pcrel_offset */
904
905   /* 64-bit relocation to the symbol's procedure linkage table.  */
906   HOWTO (R_PPC64_PLT64,         /* type */
907          0,                     /* rightshift */
908          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
909          64,                    /* bitsize */
910          FALSE,                 /* pc_relative */
911          0,                     /* bitpos */
912          complain_overflow_dont, /* complain_on_overflow */
913          ppc64_elf_unhandled_reloc, /* special_function */
914          "R_PPC64_PLT64",       /* name */
915          FALSE,                 /* partial_inplace */
916          0,                     /* src_mask */
917          ONES (64),             /* dst_mask */
918          FALSE),                /* pcrel_offset */
919
920   /* 64-bit PC relative relocation to the symbol's procedure linkage
921      table.  */
922   /* FIXME: R_PPC64_PLTREL64 not supported.  */
923   HOWTO (R_PPC64_PLTREL64,      /* type */
924          0,                     /* rightshift */
925          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
926          64,                    /* bitsize */
927          TRUE,                  /* pc_relative */
928          0,                     /* bitpos */
929          complain_overflow_dont, /* complain_on_overflow */
930          ppc64_elf_unhandled_reloc, /* special_function */
931          "R_PPC64_PLTREL64",    /* name */
932          FALSE,                 /* partial_inplace */
933          0,                     /* src_mask */
934          ONES (64),             /* dst_mask */
935          TRUE),                 /* pcrel_offset */
936
937   /* 16 bit TOC-relative relocation.  */
938
939   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
940   HOWTO (R_PPC64_TOC16,         /* type */
941          0,                     /* rightshift */
942          1,                     /* size (0 = byte, 1 = short, 2 = long) */
943          16,                    /* bitsize */
944          FALSE,                 /* pc_relative */
945          0,                     /* bitpos */
946          complain_overflow_signed, /* complain_on_overflow */
947          ppc64_elf_toc_reloc,   /* special_function */
948          "R_PPC64_TOC16",       /* name */
949          FALSE,                 /* partial_inplace */
950          0,                     /* src_mask */
951          0xffff,                /* dst_mask */
952          FALSE),                /* pcrel_offset */
953
954   /* 16 bit TOC-relative relocation without overflow.  */
955
956   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
957   HOWTO (R_PPC64_TOC16_LO,      /* type */
958          0,                     /* rightshift */
959          1,                     /* size (0 = byte, 1 = short, 2 = long) */
960          16,                    /* bitsize */
961          FALSE,                 /* pc_relative */
962          0,                     /* bitpos */
963          complain_overflow_dont, /* complain_on_overflow */
964          ppc64_elf_toc_reloc,   /* special_function */
965          "R_PPC64_TOC16_LO",    /* name */
966          FALSE,                 /* partial_inplace */
967          0,                     /* src_mask */
968          0xffff,                /* dst_mask */
969          FALSE),                /* pcrel_offset */
970
971   /* 16 bit TOC-relative relocation, high 16 bits.  */
972
973   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
974   HOWTO (R_PPC64_TOC16_HI,      /* type */
975          16,                    /* rightshift */
976          1,                     /* size (0 = byte, 1 = short, 2 = long) */
977          16,                    /* bitsize */
978          FALSE,                 /* pc_relative */
979          0,                     /* bitpos */
980          complain_overflow_signed, /* complain_on_overflow */
981          ppc64_elf_toc_reloc,   /* special_function */
982          "R_PPC64_TOC16_HI",    /* name */
983          FALSE,                 /* partial_inplace */
984          0,                     /* src_mask */
985          0xffff,                /* dst_mask */
986          FALSE),                /* pcrel_offset */
987
988   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
989      contents of the low 16 bits, treated as a signed number, is
990      negative.  */
991
992   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
993   HOWTO (R_PPC64_TOC16_HA,      /* type */
994          16,                    /* rightshift */
995          1,                     /* size (0 = byte, 1 = short, 2 = long) */
996          16,                    /* bitsize */
997          FALSE,                 /* pc_relative */
998          0,                     /* bitpos */
999          complain_overflow_signed, /* complain_on_overflow */
1000          ppc64_elf_toc_ha_reloc, /* special_function */
1001          "R_PPC64_TOC16_HA",    /* name */
1002          FALSE,                 /* partial_inplace */
1003          0,                     /* src_mask */
1004          0xffff,                /* dst_mask */
1005          FALSE),                /* pcrel_offset */
1006
1007   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
1008
1009   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
1010   HOWTO (R_PPC64_TOC,           /* type */
1011          0,                     /* rightshift */
1012          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1013          64,                    /* bitsize */
1014          FALSE,                 /* pc_relative */
1015          0,                     /* bitpos */
1016          complain_overflow_bitfield, /* complain_on_overflow */
1017          ppc64_elf_toc64_reloc, /* special_function */
1018          "R_PPC64_TOC",         /* name */
1019          FALSE,                 /* partial_inplace */
1020          0,                     /* src_mask */
1021          ONES (64),             /* dst_mask */
1022          FALSE),                /* pcrel_offset */
1023
1024   /* Like R_PPC64_GOT16, but also informs the link editor that the
1025      value to relocate may (!) refer to a PLT entry which the link
1026      editor (a) may replace with the symbol value.  If the link editor
1027      is unable to fully resolve the symbol, it may (b) create a PLT
1028      entry and store the address to the new PLT entry in the GOT.
1029      This permits lazy resolution of function symbols at run time.
1030      The link editor may also skip all of this and just (c) emit a
1031      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1032   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1033     HOWTO (R_PPC64_PLTGOT16,    /* type */
1034          0,                     /* rightshift */
1035          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1036          16,                    /* bitsize */
1037          FALSE,                 /* pc_relative */
1038          0,                     /* bitpos */
1039          complain_overflow_signed, /* complain_on_overflow */
1040          ppc64_elf_unhandled_reloc, /* special_function */
1041          "R_PPC64_PLTGOT16",    /* name */
1042          FALSE,                 /* partial_inplace */
1043          0,                     /* src_mask */
1044          0xffff,                /* dst_mask */
1045          FALSE),                /* pcrel_offset */
1046
1047   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1048   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1049   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1050          0,                     /* rightshift */
1051          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1052          16,                    /* bitsize */
1053          FALSE,                 /* pc_relative */
1054          0,                     /* bitpos */
1055          complain_overflow_dont, /* complain_on_overflow */
1056          ppc64_elf_unhandled_reloc, /* special_function */
1057          "R_PPC64_PLTGOT16_LO", /* name */
1058          FALSE,                 /* partial_inplace */
1059          0,                     /* src_mask */
1060          0xffff,                /* dst_mask */
1061          FALSE),                /* pcrel_offset */
1062
1063   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1064   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1065   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1066          16,                    /* rightshift */
1067          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1068          16,                    /* bitsize */
1069          FALSE,                 /* pc_relative */
1070          0,                     /* bitpos */
1071          complain_overflow_signed, /* complain_on_overflow */
1072          ppc64_elf_unhandled_reloc, /* special_function */
1073          "R_PPC64_PLTGOT16_HI", /* name */
1074          FALSE,                 /* partial_inplace */
1075          0,                     /* src_mask */
1076          0xffff,                /* dst_mask */
1077          FALSE),                /* pcrel_offset */
1078
1079   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1080      1 if the contents of the low 16 bits, treated as a signed number,
1081      is negative.  */
1082   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1083   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1084          16,                    /* rightshift */
1085          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1086          16,                    /* bitsize */
1087          FALSE,                 /* pc_relative */
1088          0,                     /* bitpos */
1089          complain_overflow_signed, /* complain_on_overflow */
1090          ppc64_elf_unhandled_reloc, /* special_function */
1091          "R_PPC64_PLTGOT16_HA", /* name */
1092          FALSE,                 /* partial_inplace */
1093          0,                     /* src_mask */
1094          0xffff,                /* dst_mask */
1095          FALSE),                /* pcrel_offset */
1096
1097   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1098   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1099          0,                     /* rightshift */
1100          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1101          16,                    /* bitsize */
1102          FALSE,                 /* pc_relative */
1103          0,                     /* bitpos */
1104          complain_overflow_bitfield, /* complain_on_overflow */
1105          bfd_elf_generic_reloc, /* special_function */
1106          "R_PPC64_ADDR16_DS",   /* name */
1107          FALSE,                 /* partial_inplace */
1108          0,                     /* src_mask */
1109          0xfffc,                /* dst_mask */
1110          FALSE),                /* pcrel_offset */
1111
1112   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1113   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1114          0,                     /* rightshift */
1115          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1116          16,                    /* bitsize */
1117          FALSE,                 /* pc_relative */
1118          0,                     /* bitpos */
1119          complain_overflow_dont,/* complain_on_overflow */
1120          bfd_elf_generic_reloc, /* special_function */
1121          "R_PPC64_ADDR16_LO_DS",/* name */
1122          FALSE,                 /* partial_inplace */
1123          0,                     /* src_mask */
1124          0xfffc,                /* dst_mask */
1125          FALSE),                /* pcrel_offset */
1126
1127   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1128   HOWTO (R_PPC64_GOT16_DS,      /* type */
1129          0,                     /* rightshift */
1130          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1131          16,                    /* bitsize */
1132          FALSE,                 /* pc_relative */
1133          0,                     /* bitpos */
1134          complain_overflow_signed, /* complain_on_overflow */
1135          ppc64_elf_unhandled_reloc, /* special_function */
1136          "R_PPC64_GOT16_DS",    /* name */
1137          FALSE,                 /* partial_inplace */
1138          0,                     /* src_mask */
1139          0xfffc,                /* dst_mask */
1140          FALSE),                /* pcrel_offset */
1141
1142   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1143   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1144          0,                     /* rightshift */
1145          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1146          16,                    /* bitsize */
1147          FALSE,                 /* pc_relative */
1148          0,                     /* bitpos */
1149          complain_overflow_dont, /* complain_on_overflow */
1150          ppc64_elf_unhandled_reloc, /* special_function */
1151          "R_PPC64_GOT16_LO_DS", /* name */
1152          FALSE,                 /* partial_inplace */
1153          0,                     /* src_mask */
1154          0xfffc,                /* dst_mask */
1155          FALSE),                /* pcrel_offset */
1156
1157   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1158   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1159          0,                     /* rightshift */
1160          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1161          16,                    /* bitsize */
1162          FALSE,                 /* pc_relative */
1163          0,                     /* bitpos */
1164          complain_overflow_dont, /* complain_on_overflow */
1165          ppc64_elf_unhandled_reloc, /* special_function */
1166          "R_PPC64_PLT16_LO_DS", /* name */
1167          FALSE,                 /* partial_inplace */
1168          0,                     /* src_mask */
1169          0xfffc,                /* dst_mask */
1170          FALSE),                /* pcrel_offset */
1171
1172   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1173   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1174          0,                     /* rightshift */
1175          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1176          16,                    /* bitsize */
1177          FALSE,                 /* pc_relative */
1178          0,                     /* bitpos */
1179          complain_overflow_bitfield, /* complain_on_overflow */
1180          ppc64_elf_sectoff_reloc, /* special_function */
1181          "R_PPC64_SECTOFF_DS",  /* name */
1182          FALSE,                 /* partial_inplace */
1183          0,                     /* src_mask */
1184          0xfffc,                /* dst_mask */
1185          FALSE),                /* pcrel_offset */
1186
1187   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1188   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1189          0,                     /* rightshift */
1190          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1191          16,                    /* bitsize */
1192          FALSE,                 /* pc_relative */
1193          0,                     /* bitpos */
1194          complain_overflow_dont, /* complain_on_overflow */
1195          ppc64_elf_sectoff_reloc, /* special_function */
1196          "R_PPC64_SECTOFF_LO_DS",/* name */
1197          FALSE,                 /* partial_inplace */
1198          0,                     /* src_mask */
1199          0xfffc,                /* dst_mask */
1200          FALSE),                /* pcrel_offset */
1201
1202   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1203   HOWTO (R_PPC64_TOC16_DS,      /* type */
1204          0,                     /* rightshift */
1205          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1206          16,                    /* bitsize */
1207          FALSE,                 /* pc_relative */
1208          0,                     /* bitpos */
1209          complain_overflow_signed, /* complain_on_overflow */
1210          ppc64_elf_toc_reloc,   /* special_function */
1211          "R_PPC64_TOC16_DS",    /* name */
1212          FALSE,                 /* partial_inplace */
1213          0,                     /* src_mask */
1214          0xfffc,                /* dst_mask */
1215          FALSE),                /* pcrel_offset */
1216
1217   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1218   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1219          0,                     /* rightshift */
1220          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1221          16,                    /* bitsize */
1222          FALSE,                 /* pc_relative */
1223          0,                     /* bitpos */
1224          complain_overflow_dont, /* complain_on_overflow */
1225          ppc64_elf_toc_reloc,   /* special_function */
1226          "R_PPC64_TOC16_LO_DS", /* name */
1227          FALSE,                 /* partial_inplace */
1228          0,                     /* src_mask */
1229          0xfffc,                /* dst_mask */
1230          FALSE),                /* pcrel_offset */
1231
1232   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1233   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1234   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1235          0,                     /* rightshift */
1236          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1237          16,                    /* bitsize */
1238          FALSE,                 /* pc_relative */
1239          0,                     /* bitpos */
1240          complain_overflow_signed, /* complain_on_overflow */
1241          ppc64_elf_unhandled_reloc, /* special_function */
1242          "R_PPC64_PLTGOT16_DS", /* name */
1243          FALSE,                 /* partial_inplace */
1244          0,                     /* src_mask */
1245          0xfffc,                /* dst_mask */
1246          FALSE),                /* pcrel_offset */
1247
1248   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1249   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1250   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1251          0,                     /* rightshift */
1252          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1253          16,                    /* bitsize */
1254          FALSE,                 /* pc_relative */
1255          0,                     /* bitpos */
1256          complain_overflow_dont, /* complain_on_overflow */
1257          ppc64_elf_unhandled_reloc, /* special_function */
1258          "R_PPC64_PLTGOT16_LO_DS",/* name */
1259          FALSE,                 /* partial_inplace */
1260          0,                     /* src_mask */
1261          0xfffc,                /* dst_mask */
1262          FALSE),                /* pcrel_offset */
1263
1264   /* Marker relocs for TLS.  */
1265   HOWTO (R_PPC64_TLS,
1266          0,                     /* rightshift */
1267          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1268          32,                    /* bitsize */
1269          FALSE,                 /* pc_relative */
1270          0,                     /* bitpos */
1271          complain_overflow_dont, /* complain_on_overflow */
1272          bfd_elf_generic_reloc, /* special_function */
1273          "R_PPC64_TLS",         /* name */
1274          FALSE,                 /* partial_inplace */
1275          0,                     /* src_mask */
1276          0,                     /* dst_mask */
1277          FALSE),                /* pcrel_offset */
1278
1279   HOWTO (R_PPC64_TLSGD,
1280          0,                     /* rightshift */
1281          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1282          32,                    /* bitsize */
1283          FALSE,                 /* pc_relative */
1284          0,                     /* bitpos */
1285          complain_overflow_dont, /* complain_on_overflow */
1286          bfd_elf_generic_reloc, /* special_function */
1287          "R_PPC64_TLSGD",       /* name */
1288          FALSE,                 /* partial_inplace */
1289          0,                     /* src_mask */
1290          0,                     /* dst_mask */
1291          FALSE),                /* pcrel_offset */
1292
1293   HOWTO (R_PPC64_TLSLD,
1294          0,                     /* rightshift */
1295          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1296          32,                    /* bitsize */
1297          FALSE,                 /* pc_relative */
1298          0,                     /* bitpos */
1299          complain_overflow_dont, /* complain_on_overflow */
1300          bfd_elf_generic_reloc, /* special_function */
1301          "R_PPC64_TLSLD",       /* name */
1302          FALSE,                 /* partial_inplace */
1303          0,                     /* src_mask */
1304          0,                     /* dst_mask */
1305          FALSE),                /* pcrel_offset */
1306
1307   HOWTO (R_PPC64_TOCSAVE,
1308          0,                     /* rightshift */
1309          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1310          32,                    /* bitsize */
1311          FALSE,                 /* pc_relative */
1312          0,                     /* bitpos */
1313          complain_overflow_dont, /* complain_on_overflow */
1314          bfd_elf_generic_reloc, /* special_function */
1315          "R_PPC64_TOCSAVE",     /* name */
1316          FALSE,                 /* partial_inplace */
1317          0,                     /* src_mask */
1318          0,                     /* dst_mask */
1319          FALSE),                /* pcrel_offset */
1320
1321   /* Computes the load module index of the load module that contains the
1322      definition of its TLS sym.  */
1323   HOWTO (R_PPC64_DTPMOD64,
1324          0,                     /* rightshift */
1325          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1326          64,                    /* bitsize */
1327          FALSE,                 /* pc_relative */
1328          0,                     /* bitpos */
1329          complain_overflow_dont, /* complain_on_overflow */
1330          ppc64_elf_unhandled_reloc, /* special_function */
1331          "R_PPC64_DTPMOD64",    /* name */
1332          FALSE,                 /* partial_inplace */
1333          0,                     /* src_mask */
1334          ONES (64),             /* dst_mask */
1335          FALSE),                /* pcrel_offset */
1336
1337   /* Computes a dtv-relative displacement, the difference between the value
1338      of sym+add and the base address of the thread-local storage block that
1339      contains the definition of sym, minus 0x8000.  */
1340   HOWTO (R_PPC64_DTPREL64,
1341          0,                     /* rightshift */
1342          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1343          64,                    /* bitsize */
1344          FALSE,                 /* pc_relative */
1345          0,                     /* bitpos */
1346          complain_overflow_dont, /* complain_on_overflow */
1347          ppc64_elf_unhandled_reloc, /* special_function */
1348          "R_PPC64_DTPREL64",    /* name */
1349          FALSE,                 /* partial_inplace */
1350          0,                     /* src_mask */
1351          ONES (64),             /* dst_mask */
1352          FALSE),                /* pcrel_offset */
1353
1354   /* A 16 bit dtprel reloc.  */
1355   HOWTO (R_PPC64_DTPREL16,
1356          0,                     /* rightshift */
1357          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1358          16,                    /* bitsize */
1359          FALSE,                 /* pc_relative */
1360          0,                     /* bitpos */
1361          complain_overflow_signed, /* complain_on_overflow */
1362          ppc64_elf_unhandled_reloc, /* special_function */
1363          "R_PPC64_DTPREL16",    /* name */
1364          FALSE,                 /* partial_inplace */
1365          0,                     /* src_mask */
1366          0xffff,                /* dst_mask */
1367          FALSE),                /* pcrel_offset */
1368
1369   /* Like DTPREL16, but no overflow.  */
1370   HOWTO (R_PPC64_DTPREL16_LO,
1371          0,                     /* rightshift */
1372          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1373          16,                    /* bitsize */
1374          FALSE,                 /* pc_relative */
1375          0,                     /* bitpos */
1376          complain_overflow_dont, /* complain_on_overflow */
1377          ppc64_elf_unhandled_reloc, /* special_function */
1378          "R_PPC64_DTPREL16_LO", /* name */
1379          FALSE,                 /* partial_inplace */
1380          0,                     /* src_mask */
1381          0xffff,                /* dst_mask */
1382          FALSE),                /* pcrel_offset */
1383
1384   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1385   HOWTO (R_PPC64_DTPREL16_HI,
1386          16,                    /* rightshift */
1387          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1388          16,                    /* bitsize */
1389          FALSE,                 /* pc_relative */
1390          0,                     /* bitpos */
1391          complain_overflow_signed, /* complain_on_overflow */
1392          ppc64_elf_unhandled_reloc, /* special_function */
1393          "R_PPC64_DTPREL16_HI", /* name */
1394          FALSE,                 /* partial_inplace */
1395          0,                     /* src_mask */
1396          0xffff,                /* dst_mask */
1397          FALSE),                /* pcrel_offset */
1398
1399   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1400   HOWTO (R_PPC64_DTPREL16_HA,
1401          16,                    /* rightshift */
1402          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1403          16,                    /* bitsize */
1404          FALSE,                 /* pc_relative */
1405          0,                     /* bitpos */
1406          complain_overflow_signed, /* complain_on_overflow */
1407          ppc64_elf_unhandled_reloc, /* special_function */
1408          "R_PPC64_DTPREL16_HA", /* name */
1409          FALSE,                 /* partial_inplace */
1410          0,                     /* src_mask */
1411          0xffff,                /* dst_mask */
1412          FALSE),                /* pcrel_offset */
1413
1414   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1415   HOWTO (R_PPC64_DTPREL16_HIGHER,
1416          32,                    /* rightshift */
1417          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1418          16,                    /* bitsize */
1419          FALSE,                 /* pc_relative */
1420          0,                     /* bitpos */
1421          complain_overflow_dont, /* complain_on_overflow */
1422          ppc64_elf_unhandled_reloc, /* special_function */
1423          "R_PPC64_DTPREL16_HIGHER", /* name */
1424          FALSE,                 /* partial_inplace */
1425          0,                     /* src_mask */
1426          0xffff,                /* dst_mask */
1427          FALSE),                /* pcrel_offset */
1428
1429   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1430   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1431          32,                    /* rightshift */
1432          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1433          16,                    /* bitsize */
1434          FALSE,                 /* pc_relative */
1435          0,                     /* bitpos */
1436          complain_overflow_dont, /* complain_on_overflow */
1437          ppc64_elf_unhandled_reloc, /* special_function */
1438          "R_PPC64_DTPREL16_HIGHERA", /* name */
1439          FALSE,                 /* partial_inplace */
1440          0,                     /* src_mask */
1441          0xffff,                /* dst_mask */
1442          FALSE),                /* pcrel_offset */
1443
1444   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1445   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1446          48,                    /* rightshift */
1447          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1448          16,                    /* bitsize */
1449          FALSE,                 /* pc_relative */
1450          0,                     /* bitpos */
1451          complain_overflow_dont, /* complain_on_overflow */
1452          ppc64_elf_unhandled_reloc, /* special_function */
1453          "R_PPC64_DTPREL16_HIGHEST", /* name */
1454          FALSE,                 /* partial_inplace */
1455          0,                     /* src_mask */
1456          0xffff,                /* dst_mask */
1457          FALSE),                /* pcrel_offset */
1458
1459   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1460   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1461          48,                    /* rightshift */
1462          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1463          16,                    /* bitsize */
1464          FALSE,                 /* pc_relative */
1465          0,                     /* bitpos */
1466          complain_overflow_dont, /* complain_on_overflow */
1467          ppc64_elf_unhandled_reloc, /* special_function */
1468          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1469          FALSE,                 /* partial_inplace */
1470          0,                     /* src_mask */
1471          0xffff,                /* dst_mask */
1472          FALSE),                /* pcrel_offset */
1473
1474   /* Like DTPREL16, but for insns with a DS field.  */
1475   HOWTO (R_PPC64_DTPREL16_DS,
1476          0,                     /* rightshift */
1477          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1478          16,                    /* bitsize */
1479          FALSE,                 /* pc_relative */
1480          0,                     /* bitpos */
1481          complain_overflow_signed, /* complain_on_overflow */
1482          ppc64_elf_unhandled_reloc, /* special_function */
1483          "R_PPC64_DTPREL16_DS", /* name */
1484          FALSE,                 /* partial_inplace */
1485          0,                     /* src_mask */
1486          0xfffc,                /* dst_mask */
1487          FALSE),                /* pcrel_offset */
1488
1489   /* Like DTPREL16_DS, but no overflow.  */
1490   HOWTO (R_PPC64_DTPREL16_LO_DS,
1491          0,                     /* rightshift */
1492          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1493          16,                    /* bitsize */
1494          FALSE,                 /* pc_relative */
1495          0,                     /* bitpos */
1496          complain_overflow_dont, /* complain_on_overflow */
1497          ppc64_elf_unhandled_reloc, /* special_function */
1498          "R_PPC64_DTPREL16_LO_DS", /* name */
1499          FALSE,                 /* partial_inplace */
1500          0,                     /* src_mask */
1501          0xfffc,                /* dst_mask */
1502          FALSE),                /* pcrel_offset */
1503
1504   /* Computes a tp-relative displacement, the difference between the value of
1505      sym+add and the value of the thread pointer (r13).  */
1506   HOWTO (R_PPC64_TPREL64,
1507          0,                     /* rightshift */
1508          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1509          64,                    /* bitsize */
1510          FALSE,                 /* pc_relative */
1511          0,                     /* bitpos */
1512          complain_overflow_dont, /* complain_on_overflow */
1513          ppc64_elf_unhandled_reloc, /* special_function */
1514          "R_PPC64_TPREL64",     /* name */
1515          FALSE,                 /* partial_inplace */
1516          0,                     /* src_mask */
1517          ONES (64),             /* dst_mask */
1518          FALSE),                /* pcrel_offset */
1519
1520   /* A 16 bit tprel reloc.  */
1521   HOWTO (R_PPC64_TPREL16,
1522          0,                     /* rightshift */
1523          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1524          16,                    /* bitsize */
1525          FALSE,                 /* pc_relative */
1526          0,                     /* bitpos */
1527          complain_overflow_signed, /* complain_on_overflow */
1528          ppc64_elf_unhandled_reloc, /* special_function */
1529          "R_PPC64_TPREL16",     /* name */
1530          FALSE,                 /* partial_inplace */
1531          0,                     /* src_mask */
1532          0xffff,                /* dst_mask */
1533          FALSE),                /* pcrel_offset */
1534
1535   /* Like TPREL16, but no overflow.  */
1536   HOWTO (R_PPC64_TPREL16_LO,
1537          0,                     /* rightshift */
1538          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1539          16,                    /* bitsize */
1540          FALSE,                 /* pc_relative */
1541          0,                     /* bitpos */
1542          complain_overflow_dont, /* complain_on_overflow */
1543          ppc64_elf_unhandled_reloc, /* special_function */
1544          "R_PPC64_TPREL16_LO",  /* name */
1545          FALSE,                 /* partial_inplace */
1546          0,                     /* src_mask */
1547          0xffff,                /* dst_mask */
1548          FALSE),                /* pcrel_offset */
1549
1550   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1551   HOWTO (R_PPC64_TPREL16_HI,
1552          16,                    /* rightshift */
1553          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1554          16,                    /* bitsize */
1555          FALSE,                 /* pc_relative */
1556          0,                     /* bitpos */
1557          complain_overflow_signed, /* complain_on_overflow */
1558          ppc64_elf_unhandled_reloc, /* special_function */
1559          "R_PPC64_TPREL16_HI",  /* name */
1560          FALSE,                 /* partial_inplace */
1561          0,                     /* src_mask */
1562          0xffff,                /* dst_mask */
1563          FALSE),                /* pcrel_offset */
1564
1565   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1566   HOWTO (R_PPC64_TPREL16_HA,
1567          16,                    /* rightshift */
1568          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1569          16,                    /* bitsize */
1570          FALSE,                 /* pc_relative */
1571          0,                     /* bitpos */
1572          complain_overflow_signed, /* complain_on_overflow */
1573          ppc64_elf_unhandled_reloc, /* special_function */
1574          "R_PPC64_TPREL16_HA",  /* name */
1575          FALSE,                 /* partial_inplace */
1576          0,                     /* src_mask */
1577          0xffff,                /* dst_mask */
1578          FALSE),                /* pcrel_offset */
1579
1580   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1581   HOWTO (R_PPC64_TPREL16_HIGHER,
1582          32,                    /* rightshift */
1583          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1584          16,                    /* bitsize */
1585          FALSE,                 /* pc_relative */
1586          0,                     /* bitpos */
1587          complain_overflow_dont, /* complain_on_overflow */
1588          ppc64_elf_unhandled_reloc, /* special_function */
1589          "R_PPC64_TPREL16_HIGHER",      /* name */
1590          FALSE,                 /* partial_inplace */
1591          0,                     /* src_mask */
1592          0xffff,                /* dst_mask */
1593          FALSE),                /* pcrel_offset */
1594
1595   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1596   HOWTO (R_PPC64_TPREL16_HIGHERA,
1597          32,                    /* rightshift */
1598          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1599          16,                    /* bitsize */
1600          FALSE,                 /* pc_relative */
1601          0,                     /* bitpos */
1602          complain_overflow_dont, /* complain_on_overflow */
1603          ppc64_elf_unhandled_reloc, /* special_function */
1604          "R_PPC64_TPREL16_HIGHERA", /* name */
1605          FALSE,                 /* partial_inplace */
1606          0,                     /* src_mask */
1607          0xffff,                /* dst_mask */
1608          FALSE),                /* pcrel_offset */
1609
1610   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1611   HOWTO (R_PPC64_TPREL16_HIGHEST,
1612          48,                    /* rightshift */
1613          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1614          16,                    /* bitsize */
1615          FALSE,                 /* pc_relative */
1616          0,                     /* bitpos */
1617          complain_overflow_dont, /* complain_on_overflow */
1618          ppc64_elf_unhandled_reloc, /* special_function */
1619          "R_PPC64_TPREL16_HIGHEST", /* name */
1620          FALSE,                 /* partial_inplace */
1621          0,                     /* src_mask */
1622          0xffff,                /* dst_mask */
1623          FALSE),                /* pcrel_offset */
1624
1625   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1626   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1627          48,                    /* rightshift */
1628          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1629          16,                    /* bitsize */
1630          FALSE,                 /* pc_relative */
1631          0,                     /* bitpos */
1632          complain_overflow_dont, /* complain_on_overflow */
1633          ppc64_elf_unhandled_reloc, /* special_function */
1634          "R_PPC64_TPREL16_HIGHESTA", /* name */
1635          FALSE,                 /* partial_inplace */
1636          0,                     /* src_mask */
1637          0xffff,                /* dst_mask */
1638          FALSE),                /* pcrel_offset */
1639
1640   /* Like TPREL16, but for insns with a DS field.  */
1641   HOWTO (R_PPC64_TPREL16_DS,
1642          0,                     /* rightshift */
1643          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1644          16,                    /* bitsize */
1645          FALSE,                 /* pc_relative */
1646          0,                     /* bitpos */
1647          complain_overflow_signed, /* complain_on_overflow */
1648          ppc64_elf_unhandled_reloc, /* special_function */
1649          "R_PPC64_TPREL16_DS",  /* name */
1650          FALSE,                 /* partial_inplace */
1651          0,                     /* src_mask */
1652          0xfffc,                /* dst_mask */
1653          FALSE),                /* pcrel_offset */
1654
1655   /* Like TPREL16_DS, but no overflow.  */
1656   HOWTO (R_PPC64_TPREL16_LO_DS,
1657          0,                     /* rightshift */
1658          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1659          16,                    /* bitsize */
1660          FALSE,                 /* pc_relative */
1661          0,                     /* bitpos */
1662          complain_overflow_dont, /* complain_on_overflow */
1663          ppc64_elf_unhandled_reloc, /* special_function */
1664          "R_PPC64_TPREL16_LO_DS", /* name */
1665          FALSE,                 /* partial_inplace */
1666          0,                     /* src_mask */
1667          0xfffc,                /* dst_mask */
1668          FALSE),                /* pcrel_offset */
1669
1670   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1671      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1672      to the first entry relative to the TOC base (r2).  */
1673   HOWTO (R_PPC64_GOT_TLSGD16,
1674          0,                     /* rightshift */
1675          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1676          16,                    /* bitsize */
1677          FALSE,                 /* pc_relative */
1678          0,                     /* bitpos */
1679          complain_overflow_signed, /* complain_on_overflow */
1680          ppc64_elf_unhandled_reloc, /* special_function */
1681          "R_PPC64_GOT_TLSGD16", /* name */
1682          FALSE,                 /* partial_inplace */
1683          0,                     /* src_mask */
1684          0xffff,                /* dst_mask */
1685          FALSE),                /* pcrel_offset */
1686
1687   /* Like GOT_TLSGD16, but no overflow.  */
1688   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1689          0,                     /* rightshift */
1690          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1691          16,                    /* bitsize */
1692          FALSE,                 /* pc_relative */
1693          0,                     /* bitpos */
1694          complain_overflow_dont, /* complain_on_overflow */
1695          ppc64_elf_unhandled_reloc, /* special_function */
1696          "R_PPC64_GOT_TLSGD16_LO", /* name */
1697          FALSE,                 /* partial_inplace */
1698          0,                     /* src_mask */
1699          0xffff,                /* dst_mask */
1700          FALSE),                /* pcrel_offset */
1701
1702   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1703   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1704          16,                    /* rightshift */
1705          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1706          16,                    /* bitsize */
1707          FALSE,                 /* pc_relative */
1708          0,                     /* bitpos */
1709          complain_overflow_signed, /* complain_on_overflow */
1710          ppc64_elf_unhandled_reloc, /* special_function */
1711          "R_PPC64_GOT_TLSGD16_HI", /* name */
1712          FALSE,                 /* partial_inplace */
1713          0,                     /* src_mask */
1714          0xffff,                /* dst_mask */
1715          FALSE),                /* pcrel_offset */
1716
1717   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1718   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1719          16,                    /* rightshift */
1720          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1721          16,                    /* bitsize */
1722          FALSE,                 /* pc_relative */
1723          0,                     /* bitpos */
1724          complain_overflow_signed, /* complain_on_overflow */
1725          ppc64_elf_unhandled_reloc, /* special_function */
1726          "R_PPC64_GOT_TLSGD16_HA", /* name */
1727          FALSE,                 /* partial_inplace */
1728          0,                     /* src_mask */
1729          0xffff,                /* dst_mask */
1730          FALSE),                /* pcrel_offset */
1731
1732   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1733      with values (sym+add)@dtpmod and zero, and computes the offset to the
1734      first entry relative to the TOC base (r2).  */
1735   HOWTO (R_PPC64_GOT_TLSLD16,
1736          0,                     /* rightshift */
1737          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1738          16,                    /* bitsize */
1739          FALSE,                 /* pc_relative */
1740          0,                     /* bitpos */
1741          complain_overflow_signed, /* complain_on_overflow */
1742          ppc64_elf_unhandled_reloc, /* special_function */
1743          "R_PPC64_GOT_TLSLD16", /* name */
1744          FALSE,                 /* partial_inplace */
1745          0,                     /* src_mask */
1746          0xffff,                /* dst_mask */
1747          FALSE),                /* pcrel_offset */
1748
1749   /* Like GOT_TLSLD16, but no overflow.  */
1750   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1751          0,                     /* rightshift */
1752          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1753          16,                    /* bitsize */
1754          FALSE,                 /* pc_relative */
1755          0,                     /* bitpos */
1756          complain_overflow_dont, /* complain_on_overflow */
1757          ppc64_elf_unhandled_reloc, /* special_function */
1758          "R_PPC64_GOT_TLSLD16_LO", /* name */
1759          FALSE,                 /* partial_inplace */
1760          0,                     /* src_mask */
1761          0xffff,                /* dst_mask */
1762          FALSE),                /* pcrel_offset */
1763
1764   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1765   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1766          16,                    /* rightshift */
1767          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1768          16,                    /* bitsize */
1769          FALSE,                 /* pc_relative */
1770          0,                     /* bitpos */
1771          complain_overflow_signed, /* complain_on_overflow */
1772          ppc64_elf_unhandled_reloc, /* special_function */
1773          "R_PPC64_GOT_TLSLD16_HI", /* name */
1774          FALSE,                 /* partial_inplace */
1775          0,                     /* src_mask */
1776          0xffff,                /* dst_mask */
1777          FALSE),                /* pcrel_offset */
1778
1779   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1780   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1781          16,                    /* rightshift */
1782          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1783          16,                    /* bitsize */
1784          FALSE,                 /* pc_relative */
1785          0,                     /* bitpos */
1786          complain_overflow_signed, /* complain_on_overflow */
1787          ppc64_elf_unhandled_reloc, /* special_function */
1788          "R_PPC64_GOT_TLSLD16_HA", /* name */
1789          FALSE,                 /* partial_inplace */
1790          0,                     /* src_mask */
1791          0xffff,                /* dst_mask */
1792          FALSE),                /* pcrel_offset */
1793
1794   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1795      the offset to the entry relative to the TOC base (r2).  */
1796   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1797          0,                     /* rightshift */
1798          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1799          16,                    /* bitsize */
1800          FALSE,                 /* pc_relative */
1801          0,                     /* bitpos */
1802          complain_overflow_signed, /* complain_on_overflow */
1803          ppc64_elf_unhandled_reloc, /* special_function */
1804          "R_PPC64_GOT_DTPREL16_DS", /* name */
1805          FALSE,                 /* partial_inplace */
1806          0,                     /* src_mask */
1807          0xfffc,                /* dst_mask */
1808          FALSE),                /* pcrel_offset */
1809
1810   /* Like GOT_DTPREL16_DS, but no overflow.  */
1811   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1812          0,                     /* rightshift */
1813          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1814          16,                    /* bitsize */
1815          FALSE,                 /* pc_relative */
1816          0,                     /* bitpos */
1817          complain_overflow_dont, /* complain_on_overflow */
1818          ppc64_elf_unhandled_reloc, /* special_function */
1819          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1820          FALSE,                 /* partial_inplace */
1821          0,                     /* src_mask */
1822          0xfffc,                /* dst_mask */
1823          FALSE),                /* pcrel_offset */
1824
1825   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1826   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1827          16,                    /* rightshift */
1828          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1829          16,                    /* bitsize */
1830          FALSE,                 /* pc_relative */
1831          0,                     /* bitpos */
1832          complain_overflow_signed, /* complain_on_overflow */
1833          ppc64_elf_unhandled_reloc, /* special_function */
1834          "R_PPC64_GOT_DTPREL16_HI", /* name */
1835          FALSE,                 /* partial_inplace */
1836          0,                     /* src_mask */
1837          0xffff,                /* dst_mask */
1838          FALSE),                /* pcrel_offset */
1839
1840   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1841   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1842          16,                    /* rightshift */
1843          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1844          16,                    /* bitsize */
1845          FALSE,                 /* pc_relative */
1846          0,                     /* bitpos */
1847          complain_overflow_signed, /* complain_on_overflow */
1848          ppc64_elf_unhandled_reloc, /* special_function */
1849          "R_PPC64_GOT_DTPREL16_HA", /* name */
1850          FALSE,                 /* partial_inplace */
1851          0,                     /* src_mask */
1852          0xffff,                /* dst_mask */
1853          FALSE),                /* pcrel_offset */
1854
1855   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1856      offset to the entry relative to the TOC base (r2).  */
1857   HOWTO (R_PPC64_GOT_TPREL16_DS,
1858          0,                     /* rightshift */
1859          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1860          16,                    /* bitsize */
1861          FALSE,                 /* pc_relative */
1862          0,                     /* bitpos */
1863          complain_overflow_signed, /* complain_on_overflow */
1864          ppc64_elf_unhandled_reloc, /* special_function */
1865          "R_PPC64_GOT_TPREL16_DS", /* name */
1866          FALSE,                 /* partial_inplace */
1867          0,                     /* src_mask */
1868          0xfffc,                /* dst_mask */
1869          FALSE),                /* pcrel_offset */
1870
1871   /* Like GOT_TPREL16_DS, but no overflow.  */
1872   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1873          0,                     /* rightshift */
1874          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1875          16,                    /* bitsize */
1876          FALSE,                 /* pc_relative */
1877          0,                     /* bitpos */
1878          complain_overflow_dont, /* complain_on_overflow */
1879          ppc64_elf_unhandled_reloc, /* special_function */
1880          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1881          FALSE,                 /* partial_inplace */
1882          0,                     /* src_mask */
1883          0xfffc,                /* dst_mask */
1884          FALSE),                /* pcrel_offset */
1885
1886   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1887   HOWTO (R_PPC64_GOT_TPREL16_HI,
1888          16,                    /* rightshift */
1889          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1890          16,                    /* bitsize */
1891          FALSE,                 /* pc_relative */
1892          0,                     /* bitpos */
1893          complain_overflow_signed, /* complain_on_overflow */
1894          ppc64_elf_unhandled_reloc, /* special_function */
1895          "R_PPC64_GOT_TPREL16_HI", /* name */
1896          FALSE,                 /* partial_inplace */
1897          0,                     /* src_mask */
1898          0xffff,                /* dst_mask */
1899          FALSE),                /* pcrel_offset */
1900
1901   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1902   HOWTO (R_PPC64_GOT_TPREL16_HA,
1903          16,                    /* rightshift */
1904          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1905          16,                    /* bitsize */
1906          FALSE,                 /* pc_relative */
1907          0,                     /* bitpos */
1908          complain_overflow_signed, /* complain_on_overflow */
1909          ppc64_elf_unhandled_reloc, /* special_function */
1910          "R_PPC64_GOT_TPREL16_HA", /* name */
1911          FALSE,                 /* partial_inplace */
1912          0,                     /* src_mask */
1913          0xffff,                /* dst_mask */
1914          FALSE),                /* pcrel_offset */
1915
1916   HOWTO (R_PPC64_JMP_IREL,      /* type */
1917          0,                     /* rightshift */
1918          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1919          0,                     /* bitsize */
1920          FALSE,                 /* pc_relative */
1921          0,                     /* bitpos */
1922          complain_overflow_dont, /* complain_on_overflow */
1923          ppc64_elf_unhandled_reloc, /* special_function */
1924          "R_PPC64_JMP_IREL",    /* name */
1925          FALSE,                 /* partial_inplace */
1926          0,                     /* src_mask */
1927          0,                     /* dst_mask */
1928          FALSE),                /* pcrel_offset */
1929
1930   HOWTO (R_PPC64_IRELATIVE,     /* type */
1931          0,                     /* rightshift */
1932          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1933          64,                    /* bitsize */
1934          FALSE,                 /* pc_relative */
1935          0,                     /* bitpos */
1936          complain_overflow_dont, /* complain_on_overflow */
1937          bfd_elf_generic_reloc, /* special_function */
1938          "R_PPC64_IRELATIVE",   /* name */
1939          FALSE,                 /* partial_inplace */
1940          0,                     /* src_mask */
1941          ONES (64),             /* dst_mask */
1942          FALSE),                /* pcrel_offset */
1943
1944   /* A 16 bit relative relocation.  */
1945   HOWTO (R_PPC64_REL16,         /* type */
1946          0,                     /* rightshift */
1947          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1948          16,                    /* bitsize */
1949          TRUE,                  /* pc_relative */
1950          0,                     /* bitpos */
1951          complain_overflow_bitfield, /* complain_on_overflow */
1952          bfd_elf_generic_reloc, /* special_function */
1953          "R_PPC64_REL16",       /* name */
1954          FALSE,                 /* partial_inplace */
1955          0,                     /* src_mask */
1956          0xffff,                /* dst_mask */
1957          TRUE),                 /* pcrel_offset */
1958
1959   /* A 16 bit relative relocation without overflow.  */
1960   HOWTO (R_PPC64_REL16_LO,      /* type */
1961          0,                     /* rightshift */
1962          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1963          16,                    /* bitsize */
1964          TRUE,                  /* pc_relative */
1965          0,                     /* bitpos */
1966          complain_overflow_dont,/* complain_on_overflow */
1967          bfd_elf_generic_reloc, /* special_function */
1968          "R_PPC64_REL16_LO",    /* name */
1969          FALSE,                 /* partial_inplace */
1970          0,                     /* src_mask */
1971          0xffff,                /* dst_mask */
1972          TRUE),                 /* pcrel_offset */
1973
1974   /* The high order 16 bits of a relative address.  */
1975   HOWTO (R_PPC64_REL16_HI,      /* type */
1976          16,                    /* rightshift */
1977          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1978          16,                    /* bitsize */
1979          TRUE,                  /* pc_relative */
1980          0,                     /* bitpos */
1981          complain_overflow_signed, /* complain_on_overflow */
1982          bfd_elf_generic_reloc, /* special_function */
1983          "R_PPC64_REL16_HI",    /* name */
1984          FALSE,                 /* partial_inplace */
1985          0,                     /* src_mask */
1986          0xffff,                /* dst_mask */
1987          TRUE),                 /* pcrel_offset */
1988
1989   /* The high order 16 bits of a relative address, plus 1 if the contents of
1990      the low 16 bits, treated as a signed number, is negative.  */
1991   HOWTO (R_PPC64_REL16_HA,      /* type */
1992          16,                    /* rightshift */
1993          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1994          16,                    /* bitsize */
1995          TRUE,                  /* pc_relative */
1996          0,                     /* bitpos */
1997          complain_overflow_signed, /* complain_on_overflow */
1998          ppc64_elf_ha_reloc,    /* special_function */
1999          "R_PPC64_REL16_HA",    /* name */
2000          FALSE,                 /* partial_inplace */
2001          0,                     /* src_mask */
2002          0xffff,                /* dst_mask */
2003          TRUE),                 /* pcrel_offset */
2004
2005   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
2006   HOWTO (R_PPC64_ADDR16_HIGH,   /* type */
2007          16,                    /* rightshift */
2008          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2009          16,                    /* bitsize */
2010          FALSE,                 /* pc_relative */
2011          0,                     /* bitpos */
2012          complain_overflow_dont, /* complain_on_overflow */
2013          bfd_elf_generic_reloc, /* special_function */
2014          "R_PPC64_ADDR16_HIGH", /* name */
2015          FALSE,                 /* partial_inplace */
2016          0,                     /* src_mask */
2017          0xffff,                /* dst_mask */
2018          FALSE),                /* pcrel_offset */
2019
2020   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
2021   HOWTO (R_PPC64_ADDR16_HIGHA,  /* type */
2022          16,                    /* rightshift */
2023          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2024          16,                    /* bitsize */
2025          FALSE,                 /* pc_relative */
2026          0,                     /* bitpos */
2027          complain_overflow_dont, /* complain_on_overflow */
2028          ppc64_elf_ha_reloc,    /* special_function */
2029          "R_PPC64_ADDR16_HIGHA",        /* name */
2030          FALSE,                 /* partial_inplace */
2031          0,                     /* src_mask */
2032          0xffff,                /* dst_mask */
2033          FALSE),                /* pcrel_offset */
2034
2035   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
2036   HOWTO (R_PPC64_DTPREL16_HIGH,
2037          16,                    /* rightshift */
2038          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2039          16,                    /* bitsize */
2040          FALSE,                 /* pc_relative */
2041          0,                     /* bitpos */
2042          complain_overflow_dont, /* complain_on_overflow */
2043          ppc64_elf_unhandled_reloc, /* special_function */
2044          "R_PPC64_DTPREL16_HIGH", /* name */
2045          FALSE,                 /* partial_inplace */
2046          0,                     /* src_mask */
2047          0xffff,                /* dst_mask */
2048          FALSE),                /* pcrel_offset */
2049
2050   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
2051   HOWTO (R_PPC64_DTPREL16_HIGHA,
2052          16,                    /* rightshift */
2053          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2054          16,                    /* bitsize */
2055          FALSE,                 /* pc_relative */
2056          0,                     /* bitpos */
2057          complain_overflow_dont, /* complain_on_overflow */
2058          ppc64_elf_unhandled_reloc, /* special_function */
2059          "R_PPC64_DTPREL16_HIGHA", /* name */
2060          FALSE,                 /* partial_inplace */
2061          0,                     /* src_mask */
2062          0xffff,                /* dst_mask */
2063          FALSE),                /* pcrel_offset */
2064
2065   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
2066   HOWTO (R_PPC64_TPREL16_HIGH,
2067          16,                    /* rightshift */
2068          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2069          16,                    /* bitsize */
2070          FALSE,                 /* pc_relative */
2071          0,                     /* bitpos */
2072          complain_overflow_dont, /* complain_on_overflow */
2073          ppc64_elf_unhandled_reloc, /* special_function */
2074          "R_PPC64_TPREL16_HIGH",        /* name */
2075          FALSE,                 /* partial_inplace */
2076          0,                     /* src_mask */
2077          0xffff,                /* dst_mask */
2078          FALSE),                /* pcrel_offset */
2079
2080   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
2081   HOWTO (R_PPC64_TPREL16_HIGHA,
2082          16,                    /* rightshift */
2083          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2084          16,                    /* bitsize */
2085          FALSE,                 /* pc_relative */
2086          0,                     /* bitpos */
2087          complain_overflow_dont, /* complain_on_overflow */
2088          ppc64_elf_unhandled_reloc, /* special_function */
2089          "R_PPC64_TPREL16_HIGHA",       /* name */
2090          FALSE,                 /* partial_inplace */
2091          0,                     /* src_mask */
2092          0xffff,                /* dst_mask */
2093          FALSE),                /* pcrel_offset */
2094
2095   /* GNU extension to record C++ vtable hierarchy.  */
2096   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2097          0,                     /* rightshift */
2098          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2099          0,                     /* bitsize */
2100          FALSE,                 /* pc_relative */
2101          0,                     /* bitpos */
2102          complain_overflow_dont, /* complain_on_overflow */
2103          NULL,                  /* special_function */
2104          "R_PPC64_GNU_VTINHERIT", /* name */
2105          FALSE,                 /* partial_inplace */
2106          0,                     /* src_mask */
2107          0,                     /* dst_mask */
2108          FALSE),                /* pcrel_offset */
2109
2110   /* GNU extension to record C++ vtable member usage.  */
2111   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
2112          0,                     /* rightshift */
2113          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2114          0,                     /* bitsize */
2115          FALSE,                 /* pc_relative */
2116          0,                     /* bitpos */
2117          complain_overflow_dont, /* complain_on_overflow */
2118          NULL,                  /* special_function */
2119          "R_PPC64_GNU_VTENTRY", /* name */
2120          FALSE,                 /* partial_inplace */
2121          0,                     /* src_mask */
2122          0,                     /* dst_mask */
2123          FALSE),                /* pcrel_offset */
2124 };
2125
2126 \f
2127 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2128    be done.  */
2129
2130 static void
2131 ppc_howto_init (void)
2132 {
2133   unsigned int i, type;
2134
2135   for (i = 0;
2136        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2137        i++)
2138     {
2139       type = ppc64_elf_howto_raw[i].type;
2140       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2141                           / sizeof (ppc64_elf_howto_table[0])));
2142       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2143     }
2144 }
2145
2146 static reloc_howto_type *
2147 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2148                              bfd_reloc_code_real_type code)
2149 {
2150   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2151
2152   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2153     /* Initialize howto table if needed.  */
2154     ppc_howto_init ();
2155
2156   switch (code)
2157     {
2158     default:
2159       return NULL;
2160
2161     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2162       break;
2163     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2164       break;
2165     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2166       break;
2167     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2168       break;
2169     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2170       break;
2171     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2172       break;
2173     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
2174       break;
2175     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2176       break;
2177     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
2178       break;
2179     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2180       break;
2181     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2182       break;
2183     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2184       break;
2185     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2186       break;
2187     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2188       break;
2189     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2190       break;
2191     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2192       break;
2193     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2194       break;
2195     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2196       break;
2197     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2198       break;
2199     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2200       break;
2201     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2202       break;
2203     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2204       break;
2205     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2206       break;
2207     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2208       break;
2209     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2210       break;
2211     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2212       break;
2213     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2214       break;
2215     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2216       break;
2217     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2218       break;
2219     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2220       break;
2221     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2222       break;
2223     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2224       break;
2225     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2226       break;
2227     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2228       break;
2229     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2230       break;
2231     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2232       break;
2233     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2234       break;
2235     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2236       break;
2237     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2238       break;
2239     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2240       break;
2241     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2242       break;
2243     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2244       break;
2245     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2246       break;
2247     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2248       break;
2249     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2250       break;
2251     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2252       break;
2253     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2254       break;
2255     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2256       break;
2257     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2258       break;
2259     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2260       break;
2261     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2262       break;
2263     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2264       break;
2265     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2266       break;
2267     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2268       break;
2269     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2270       break;
2271     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2272       break;
2273     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2274       break;
2275     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2276       break;
2277     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2278       break;
2279     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2280       break;
2281     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2282       break;
2283     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2284       break;
2285     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2286       break;
2287     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2288       break;
2289     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2290       break;
2291     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2292       break;
2293     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2294       break;
2295     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2296       break;
2297     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
2298       break;
2299     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2300       break;
2301     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
2302       break;
2303     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2304       break;
2305     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2306       break;
2307     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2308       break;
2309     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2310       break;
2311     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
2312       break;
2313     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2314       break;
2315     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
2316       break;
2317     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2318       break;
2319     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2320       break;
2321     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2322       break;
2323     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2324       break;
2325     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2326       break;
2327     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2328       break;
2329     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2330       break;
2331     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2332       break;
2333     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2334       break;
2335     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2336       break;
2337     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2338       break;
2339     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2340       break;
2341     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2342       break;
2343     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2344       break;
2345     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2346       break;
2347     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2348       break;
2349     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2350       break;
2351     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2352       break;
2353     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2354       break;
2355     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2356       break;
2357     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2358       break;
2359     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2360       break;
2361     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2362       break;
2363     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2364       break;
2365     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2366       break;
2367     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2368       break;
2369     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2370       break;
2371     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2372       break;
2373     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2374       break;
2375     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2376       break;
2377     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2378       break;
2379     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2380       break;
2381     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2382       break;
2383     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2384       break;
2385     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2386       break;
2387     }
2388
2389   return ppc64_elf_howto_table[r];
2390 };
2391
2392 static reloc_howto_type *
2393 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2394                              const char *r_name)
2395 {
2396   unsigned int i;
2397
2398   for (i = 0;
2399        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2400        i++)
2401     if (ppc64_elf_howto_raw[i].name != NULL
2402         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2403       return &ppc64_elf_howto_raw[i];
2404
2405   return NULL;
2406 }
2407
2408 /* Set the howto pointer for a PowerPC ELF reloc.  */
2409
2410 static void
2411 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2412                          Elf_Internal_Rela *dst)
2413 {
2414   unsigned int type;
2415
2416   /* Initialize howto table if needed.  */
2417   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2418     ppc_howto_init ();
2419
2420   type = ELF64_R_TYPE (dst->r_info);
2421   if (type >= (sizeof (ppc64_elf_howto_table)
2422                / sizeof (ppc64_elf_howto_table[0])))
2423     {
2424       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2425                              abfd, (int) type);
2426       type = R_PPC64_NONE;
2427     }
2428   cache_ptr->howto = ppc64_elf_howto_table[type];
2429 }
2430
2431 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2432
2433 static bfd_reloc_status_type
2434 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2435                     void *data, asection *input_section,
2436                     bfd *output_bfd, char **error_message)
2437 {
2438   /* If this is a relocatable link (output_bfd test tells us), just
2439      call the generic function.  Any adjustment will be done at final
2440      link time.  */
2441   if (output_bfd != NULL)
2442     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2443                                   input_section, output_bfd, error_message);
2444
2445   /* Adjust the addend for sign extension of the low 16 bits.
2446      We won't actually be using the low 16 bits, so trashing them
2447      doesn't matter.  */
2448   reloc_entry->addend += 0x8000;
2449   return bfd_reloc_continue;
2450 }
2451
2452 static bfd_reloc_status_type
2453 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2454                         void *data, asection *input_section,
2455                         bfd *output_bfd, char **error_message)
2456 {
2457   if (output_bfd != NULL)
2458     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2459                                   input_section, output_bfd, error_message);
2460
2461   if (strcmp (symbol->section->name, ".opd") == 0
2462       && (symbol->section->owner->flags & DYNAMIC) == 0)
2463     {
2464       bfd_vma dest = opd_entry_value (symbol->section,
2465                                       symbol->value + reloc_entry->addend,
2466                                       NULL, NULL, FALSE);
2467       if (dest != (bfd_vma) -1)
2468         reloc_entry->addend = dest - (symbol->value
2469                                       + symbol->section->output_section->vma
2470                                       + symbol->section->output_offset);
2471     }
2472   return bfd_reloc_continue;
2473 }
2474
2475 static bfd_reloc_status_type
2476 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2477                          void *data, asection *input_section,
2478                          bfd *output_bfd, char **error_message)
2479 {
2480   long insn;
2481   enum elf_ppc64_reloc_type r_type;
2482   bfd_size_type octets;
2483   /* Assume 'at' branch hints.  */
2484   bfd_boolean is_isa_v2 = TRUE;
2485
2486   /* If this is a relocatable link (output_bfd test tells us), just
2487      call the generic function.  Any adjustment will be done at final
2488      link time.  */
2489   if (output_bfd != NULL)
2490     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2491                                   input_section, output_bfd, error_message);
2492
2493   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2494   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2495   insn &= ~(0x01 << 21);
2496   r_type = reloc_entry->howto->type;
2497   if (r_type == R_PPC64_ADDR14_BRTAKEN
2498       || r_type == R_PPC64_REL14_BRTAKEN)
2499     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2500
2501   if (is_isa_v2)
2502     {
2503       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2504          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2505          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2506       if ((insn & (0x14 << 21)) == (0x04 << 21))
2507         insn |= 0x02 << 21;
2508       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2509         insn |= 0x08 << 21;
2510       else
2511         goto out;
2512     }
2513   else
2514     {
2515       bfd_vma target = 0;
2516       bfd_vma from;
2517
2518       if (!bfd_is_com_section (symbol->section))
2519         target = symbol->value;
2520       target += symbol->section->output_section->vma;
2521       target += symbol->section->output_offset;
2522       target += reloc_entry->addend;
2523
2524       from = (reloc_entry->address
2525               + input_section->output_offset
2526               + input_section->output_section->vma);
2527
2528       /* Invert 'y' bit if not the default.  */
2529       if ((bfd_signed_vma) (target - from) < 0)
2530         insn ^= 0x01 << 21;
2531     }
2532   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2533  out:
2534   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2535                                  input_section, output_bfd, error_message);
2536 }
2537
2538 static bfd_reloc_status_type
2539 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2540                          void *data, asection *input_section,
2541                          bfd *output_bfd, char **error_message)
2542 {
2543   /* If this is a relocatable link (output_bfd test tells us), just
2544      call the generic function.  Any adjustment will be done at final
2545      link time.  */
2546   if (output_bfd != NULL)
2547     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2548                                   input_section, output_bfd, error_message);
2549
2550   /* Subtract the symbol section base address.  */
2551   reloc_entry->addend -= symbol->section->output_section->vma;
2552   return bfd_reloc_continue;
2553 }
2554
2555 static bfd_reloc_status_type
2556 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2557                             void *data, asection *input_section,
2558                             bfd *output_bfd, char **error_message)
2559 {
2560   /* If this is a relocatable link (output_bfd test tells us), just
2561      call the generic function.  Any adjustment will be done at final
2562      link time.  */
2563   if (output_bfd != NULL)
2564     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2565                                   input_section, output_bfd, error_message);
2566
2567   /* Subtract the symbol section base address.  */
2568   reloc_entry->addend -= symbol->section->output_section->vma;
2569
2570   /* Adjust the addend for sign extension of the low 16 bits.  */
2571   reloc_entry->addend += 0x8000;
2572   return bfd_reloc_continue;
2573 }
2574
2575 static bfd_reloc_status_type
2576 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2577                      void *data, asection *input_section,
2578                      bfd *output_bfd, char **error_message)
2579 {
2580   bfd_vma TOCstart;
2581
2582   /* If this is a relocatable link (output_bfd test tells us), just
2583      call the generic function.  Any adjustment will be done at final
2584      link time.  */
2585   if (output_bfd != NULL)
2586     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2587                                   input_section, output_bfd, error_message);
2588
2589   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2590   if (TOCstart == 0)
2591     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2592
2593   /* Subtract the TOC base address.  */
2594   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2595   return bfd_reloc_continue;
2596 }
2597
2598 static bfd_reloc_status_type
2599 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2600                         void *data, asection *input_section,
2601                         bfd *output_bfd, char **error_message)
2602 {
2603   bfd_vma TOCstart;
2604
2605   /* If this is a relocatable link (output_bfd test tells us), just
2606      call the generic function.  Any adjustment will be done at final
2607      link time.  */
2608   if (output_bfd != NULL)
2609     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2610                                   input_section, output_bfd, error_message);
2611
2612   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2613   if (TOCstart == 0)
2614     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2615
2616   /* Subtract the TOC base address.  */
2617   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2618
2619   /* Adjust the addend for sign extension of the low 16 bits.  */
2620   reloc_entry->addend += 0x8000;
2621   return bfd_reloc_continue;
2622 }
2623
2624 static bfd_reloc_status_type
2625 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2626                        void *data, asection *input_section,
2627                        bfd *output_bfd, char **error_message)
2628 {
2629   bfd_vma TOCstart;
2630   bfd_size_type octets;
2631
2632   /* If this is a relocatable link (output_bfd test tells us), just
2633      call the generic function.  Any adjustment will be done at final
2634      link time.  */
2635   if (output_bfd != NULL)
2636     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2637                                   input_section, output_bfd, error_message);
2638
2639   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2640   if (TOCstart == 0)
2641     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2642
2643   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2644   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2645   return bfd_reloc_ok;
2646 }
2647
2648 static bfd_reloc_status_type
2649 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2650                            void *data, asection *input_section,
2651                            bfd *output_bfd, char **error_message)
2652 {
2653   /* If this is a relocatable link (output_bfd test tells us), just
2654      call the generic function.  Any adjustment will be done at final
2655      link time.  */
2656   if (output_bfd != NULL)
2657     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2658                                   input_section, output_bfd, error_message);
2659
2660   if (error_message != NULL)
2661     {
2662       static char buf[60];
2663       sprintf (buf, "generic linker can't handle %s",
2664                reloc_entry->howto->name);
2665       *error_message = buf;
2666     }
2667   return bfd_reloc_dangerous;
2668 }
2669
2670 /* Track GOT entries needed for a given symbol.  We might need more
2671    than one got entry per symbol.  */
2672 struct got_entry
2673 {
2674   struct got_entry *next;
2675
2676   /* The symbol addend that we'll be placing in the GOT.  */
2677   bfd_vma addend;
2678
2679   /* Unlike other ELF targets, we use separate GOT entries for the same
2680      symbol referenced from different input files.  This is to support
2681      automatic multiple TOC/GOT sections, where the TOC base can vary
2682      from one input file to another.  After partitioning into TOC groups
2683      we merge entries within the group.
2684
2685      Point to the BFD owning this GOT entry.  */
2686   bfd *owner;
2687
2688   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2689      TLS_TPREL or TLS_DTPREL for tls entries.  */
2690   unsigned char tls_type;
2691
2692   /* Non-zero if got.ent points to real entry.  */
2693   unsigned char is_indirect;
2694
2695   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2696   union
2697     {
2698       bfd_signed_vma refcount;
2699       bfd_vma offset;
2700       struct got_entry *ent;
2701     } got;
2702 };
2703
2704 /* The same for PLT.  */
2705 struct plt_entry
2706 {
2707   struct plt_entry *next;
2708
2709   bfd_vma addend;
2710
2711   union
2712     {
2713       bfd_signed_vma refcount;
2714       bfd_vma offset;
2715     } plt;
2716 };
2717
2718 struct ppc64_elf_obj_tdata
2719 {
2720   struct elf_obj_tdata elf;
2721
2722   /* Shortcuts to dynamic linker sections.  */
2723   asection *got;
2724   asection *relgot;
2725
2726   /* Used during garbage collection.  We attach global symbols defined
2727      on removed .opd entries to this section so that the sym is removed.  */
2728   asection *deleted_section;
2729
2730   /* TLS local dynamic got entry handling.  Support for multiple GOT
2731      sections means we potentially need one of these for each input bfd.  */
2732   struct got_entry tlsld_got;
2733
2734   union {
2735     /* A copy of relocs before they are modified for --emit-relocs.  */
2736     Elf_Internal_Rela *relocs;
2737
2738     /* Section contents.  */
2739     bfd_byte *contents;
2740   } opd;
2741
2742   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2743      the reloc to be in the range -32768 to 32767.  */
2744   unsigned int has_small_toc_reloc : 1;
2745
2746   /* Set if toc/got ha relocs detected not using r2, or lo reloc
2747      instruction not one we handle.  */
2748   unsigned int unexpected_toc_insn : 1;
2749 };
2750
2751 #define ppc64_elf_tdata(bfd) \
2752   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2753
2754 #define ppc64_tlsld_got(bfd) \
2755   (&ppc64_elf_tdata (bfd)->tlsld_got)
2756
2757 #define is_ppc64_elf(bfd) \
2758   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2759    && elf_object_id (bfd) == PPC64_ELF_DATA)
2760
2761 /* Override the generic function because we store some extras.  */
2762
2763 static bfd_boolean
2764 ppc64_elf_mkobject (bfd *abfd)
2765 {
2766   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2767                                   PPC64_ELF_DATA);
2768 }
2769
2770 /* Fix bad default arch selected for a 64 bit input bfd when the
2771    default is 32 bit.  */
2772
2773 static bfd_boolean
2774 ppc64_elf_object_p (bfd *abfd)
2775 {
2776   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2777     {
2778       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2779
2780       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2781         {
2782           /* Relies on arch after 32 bit default being 64 bit default.  */
2783           abfd->arch_info = abfd->arch_info->next;
2784           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2785         }
2786     }
2787   return TRUE;
2788 }
2789
2790 /* Support for core dump NOTE sections.  */
2791
2792 static bfd_boolean
2793 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2794 {
2795   size_t offset, size;
2796
2797   if (note->descsz != 504)
2798     return FALSE;
2799
2800   /* pr_cursig */
2801   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2802
2803   /* pr_pid */
2804   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2805
2806   /* pr_reg */
2807   offset = 112;
2808   size = 384;
2809
2810   /* Make a ".reg/999" section.  */
2811   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2812                                           size, note->descpos + offset);
2813 }
2814
2815 static bfd_boolean
2816 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2817 {
2818   if (note->descsz != 136)
2819     return FALSE;
2820
2821   elf_tdata (abfd)->core->pid
2822     = bfd_get_32 (abfd, note->descdata + 24);
2823   elf_tdata (abfd)->core->program
2824     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2825   elf_tdata (abfd)->core->command
2826     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2827
2828   return TRUE;
2829 }
2830
2831 static char *
2832 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2833                            ...)
2834 {
2835   switch (note_type)
2836     {
2837     default:
2838       return NULL;
2839
2840     case NT_PRPSINFO:
2841       {
2842         char data[136];
2843         va_list ap;
2844
2845         va_start (ap, note_type);
2846         memset (data, 0, sizeof (data));
2847         strncpy (data + 40, va_arg (ap, const char *), 16);
2848         strncpy (data + 56, va_arg (ap, const char *), 80);
2849         va_end (ap);
2850         return elfcore_write_note (abfd, buf, bufsiz,
2851                                    "CORE", note_type, data, sizeof (data));
2852       }
2853
2854     case NT_PRSTATUS:
2855       {
2856         char data[504];
2857         va_list ap;
2858         long pid;
2859         int cursig;
2860         const void *greg;
2861
2862         va_start (ap, note_type);
2863         memset (data, 0, 112);
2864         pid = va_arg (ap, long);
2865         bfd_put_32 (abfd, pid, data + 32);
2866         cursig = va_arg (ap, int);
2867         bfd_put_16 (abfd, cursig, data + 12);
2868         greg = va_arg (ap, const void *);
2869         memcpy (data + 112, greg, 384);
2870         memset (data + 496, 0, 8);
2871         va_end (ap);
2872         return elfcore_write_note (abfd, buf, bufsiz,
2873                                    "CORE", note_type, data, sizeof (data));
2874       }
2875     }
2876 }
2877
2878 /* Add extra PPC sections.  */
2879
2880 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2881 {
2882   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
2883   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2884   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2885   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2886   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2887   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2888   { NULL,                     0,  0, 0,            0 }
2889 };
2890
2891 enum _ppc64_sec_type {
2892   sec_normal = 0,
2893   sec_opd = 1,
2894   sec_toc = 2
2895 };
2896
2897 struct _ppc64_elf_section_data
2898 {
2899   struct bfd_elf_section_data elf;
2900
2901   union
2902   {
2903     /* An array with one entry for each opd function descriptor.  */
2904     struct _opd_sec_data
2905     {
2906       /* Points to the function code section for local opd entries.  */
2907       asection **func_sec;
2908
2909       /* After editing .opd, adjust references to opd local syms.  */
2910       long *adjust;
2911     } opd;
2912
2913     /* An array for toc sections, indexed by offset/8.  */
2914     struct _toc_sec_data
2915     {
2916       /* Specifies the relocation symbol index used at a given toc offset.  */
2917       unsigned *symndx;
2918
2919       /* And the relocation addend.  */
2920       bfd_vma *add;
2921     } toc;
2922   } u;
2923
2924   enum _ppc64_sec_type sec_type:2;
2925
2926   /* Flag set when small branches are detected.  Used to
2927      select suitable defaults for the stub group size.  */
2928   unsigned int has_14bit_branch:1;
2929 };
2930
2931 #define ppc64_elf_section_data(sec) \
2932   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2933
2934 static bfd_boolean
2935 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2936 {
2937   if (!sec->used_by_bfd)
2938     {
2939       struct _ppc64_elf_section_data *sdata;
2940       bfd_size_type amt = sizeof (*sdata);
2941
2942       sdata = bfd_zalloc (abfd, amt);
2943       if (sdata == NULL)
2944         return FALSE;
2945       sec->used_by_bfd = sdata;
2946     }
2947
2948   return _bfd_elf_new_section_hook (abfd, sec);
2949 }
2950
2951 static struct _opd_sec_data *
2952 get_opd_info (asection * sec)
2953 {
2954   if (sec != NULL
2955       && ppc64_elf_section_data (sec) != NULL
2956       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2957     return &ppc64_elf_section_data (sec)->u.opd;
2958   return NULL;
2959 }
2960
2961 static inline int
2962 abiversion (bfd *abfd)
2963 {
2964   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
2965 }
2966
2967 static inline void
2968 set_abiversion (bfd *abfd, int ver)
2969 {
2970   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
2971   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
2972 }
2973 \f
2974 /* Parameters for the qsort hook.  */
2975 static bfd_boolean synthetic_relocatable;
2976
2977 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
2978
2979 static int
2980 compare_symbols (const void *ap, const void *bp)
2981 {
2982   const asymbol *a = * (const asymbol **) ap;
2983   const asymbol *b = * (const asymbol **) bp;
2984
2985   /* Section symbols first.  */
2986   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2987     return -1;
2988   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2989     return 1;
2990
2991   /* then .opd symbols.  */
2992   if (strcmp (a->section->name, ".opd") == 0
2993       && strcmp (b->section->name, ".opd") != 0)
2994     return -1;
2995   if (strcmp (a->section->name, ".opd") != 0
2996       && strcmp (b->section->name, ".opd") == 0)
2997     return 1;
2998
2999   /* then other code symbols.  */
3000   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3001       == (SEC_CODE | SEC_ALLOC)
3002       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3003          != (SEC_CODE | SEC_ALLOC))
3004     return -1;
3005
3006   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3007       != (SEC_CODE | SEC_ALLOC)
3008       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3009          == (SEC_CODE | SEC_ALLOC))
3010     return 1;
3011
3012   if (synthetic_relocatable)
3013     {
3014       if (a->section->id < b->section->id)
3015         return -1;
3016
3017       if (a->section->id > b->section->id)
3018         return 1;
3019     }
3020
3021   if (a->value + a->section->vma < b->value + b->section->vma)
3022     return -1;
3023
3024   if (a->value + a->section->vma > b->value + b->section->vma)
3025     return 1;
3026
3027   /* For syms with the same value, prefer strong dynamic global function
3028      syms over other syms.  */
3029   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3030     return -1;
3031
3032   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3033     return 1;
3034
3035   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3036     return -1;
3037
3038   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3039     return 1;
3040
3041   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3042     return -1;
3043
3044   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3045     return 1;
3046
3047   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3048     return -1;
3049
3050   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3051     return 1;
3052
3053   return 0;
3054 }
3055
3056 /* Search SYMS for a symbol of the given VALUE.  */
3057
3058 static asymbol *
3059 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
3060 {
3061   long mid;
3062
3063   if (id == -1)
3064     {
3065       while (lo < hi)
3066         {
3067           mid = (lo + hi) >> 1;
3068           if (syms[mid]->value + syms[mid]->section->vma < value)
3069             lo = mid + 1;
3070           else if (syms[mid]->value + syms[mid]->section->vma > value)
3071             hi = mid;
3072           else
3073             return syms[mid];
3074         }
3075     }
3076   else
3077     {
3078       while (lo < hi)
3079         {
3080           mid = (lo + hi) >> 1;
3081           if (syms[mid]->section->id < id)
3082             lo = mid + 1;
3083           else if (syms[mid]->section->id > id)
3084             hi = mid;
3085           else if (syms[mid]->value < value)
3086             lo = mid + 1;
3087           else if (syms[mid]->value > value)
3088             hi = mid;
3089           else
3090             return syms[mid];
3091         }
3092     }
3093   return NULL;
3094 }
3095
3096 static bfd_boolean
3097 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3098 {
3099   bfd_vma vma = *(bfd_vma *) ptr;
3100   return ((section->flags & SEC_ALLOC) != 0
3101           && section->vma <= vma
3102           && vma < section->vma + section->size);
3103 }
3104
3105 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3106    entry syms.  Also generate @plt symbols for the glink branch table.  */
3107
3108 static long
3109 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3110                                 long static_count, asymbol **static_syms,
3111                                 long dyn_count, asymbol **dyn_syms,
3112                                 asymbol **ret)
3113 {
3114   asymbol *s;
3115   long i;
3116   long count;
3117   char *names;
3118   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3119   asection *opd = NULL;
3120   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3121   asymbol **syms;
3122   int abi = abiversion (abfd);
3123
3124   *ret = NULL;
3125
3126   if (abi < 2)
3127     {
3128       opd = bfd_get_section_by_name (abfd, ".opd");
3129       if (opd == NULL && abi == 1)
3130         return 0;
3131     }
3132
3133   symcount = static_count;
3134   if (!relocatable)
3135     symcount += dyn_count;
3136   if (symcount == 0)
3137     return 0;
3138
3139   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3140   if (syms == NULL)
3141     return -1;
3142
3143   if (!relocatable && static_count != 0 && dyn_count != 0)
3144     {
3145       /* Use both symbol tables.  */
3146       memcpy (syms, static_syms, static_count * sizeof (*syms));
3147       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3148     }
3149   else if (!relocatable && static_count == 0)
3150     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3151   else
3152     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3153
3154   synthetic_relocatable = relocatable;
3155   qsort (syms, symcount, sizeof (*syms), compare_symbols);
3156
3157   if (!relocatable && symcount > 1)
3158     {
3159       long j;
3160       /* Trim duplicate syms, since we may have merged the normal and
3161          dynamic symbols.  Actually, we only care about syms that have
3162          different values, so trim any with the same value.  */
3163       for (i = 1, j = 1; i < symcount; ++i)
3164         if (syms[i - 1]->value + syms[i - 1]->section->vma
3165             != syms[i]->value + syms[i]->section->vma)
3166           syms[j++] = syms[i];
3167       symcount = j;
3168     }
3169
3170   i = 0;
3171   if (strcmp (syms[i]->section->name, ".opd") == 0)
3172     ++i;
3173   codesecsym = i;
3174
3175   for (; i < symcount; ++i)
3176     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3177          != (SEC_CODE | SEC_ALLOC))
3178         || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3179       break;
3180   codesecsymend = i;
3181
3182   for (; i < symcount; ++i)
3183     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3184       break;
3185   secsymend = i;
3186
3187   for (; i < symcount; ++i)
3188     if (strcmp (syms[i]->section->name, ".opd") != 0)
3189       break;
3190   opdsymend = i;
3191
3192   for (; i < symcount; ++i)
3193     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3194         != (SEC_CODE | SEC_ALLOC))
3195       break;
3196   symcount = i;
3197
3198   count = 0;
3199
3200   if (relocatable)
3201     {
3202       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3203       arelent *r;
3204       size_t size;
3205       long relcount;
3206
3207       if (opdsymend == secsymend)
3208         goto done;
3209
3210       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3211       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3212       if (relcount == 0)
3213         goto done;
3214
3215       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3216         {
3217           count = -1;
3218           goto done;
3219         }
3220
3221       size = 0;
3222       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3223         {
3224           asymbol *sym;
3225
3226           while (r < opd->relocation + relcount
3227                  && r->address < syms[i]->value + opd->vma)
3228             ++r;
3229
3230           if (r == opd->relocation + relcount)
3231             break;
3232
3233           if (r->address != syms[i]->value + opd->vma)
3234             continue;
3235
3236           if (r->howto->type != R_PPC64_ADDR64)
3237             continue;
3238
3239           sym = *r->sym_ptr_ptr;
3240           if (!sym_exists_at (syms, opdsymend, symcount,
3241                               sym->section->id, sym->value + r->addend))
3242             {
3243               ++count;
3244               size += sizeof (asymbol);
3245               size += strlen (syms[i]->name) + 2;
3246             }
3247         }
3248
3249       s = *ret = bfd_malloc (size);
3250       if (s == NULL)
3251         {
3252           count = -1;
3253           goto done;
3254         }
3255
3256       names = (char *) (s + count);
3257
3258       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3259         {
3260           asymbol *sym;
3261
3262           while (r < opd->relocation + relcount
3263                  && r->address < syms[i]->value + opd->vma)
3264             ++r;
3265
3266           if (r == opd->relocation + relcount)
3267             break;
3268
3269           if (r->address != syms[i]->value + opd->vma)
3270             continue;
3271
3272           if (r->howto->type != R_PPC64_ADDR64)
3273             continue;
3274
3275           sym = *r->sym_ptr_ptr;
3276           if (!sym_exists_at (syms, opdsymend, symcount,
3277                               sym->section->id, sym->value + r->addend))
3278             {
3279               size_t len;
3280
3281               *s = *syms[i];
3282               s->flags |= BSF_SYNTHETIC;
3283               s->section = sym->section;
3284               s->value = sym->value + r->addend;
3285               s->name = names;
3286               *names++ = '.';
3287               len = strlen (syms[i]->name);
3288               memcpy (names, syms[i]->name, len + 1);
3289               names += len + 1;
3290               /* Have udata.p point back to the original symbol this
3291                  synthetic symbol was derived from.  */
3292               s->udata.p = syms[i];
3293               s++;
3294             }
3295         }
3296     }
3297   else
3298     {
3299       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3300       bfd_byte *contents = NULL;
3301       size_t size;
3302       long plt_count = 0;
3303       bfd_vma glink_vma = 0, resolv_vma = 0;
3304       asection *dynamic, *glink = NULL, *relplt = NULL;
3305       arelent *p;
3306
3307       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3308         {
3309         free_contents_and_exit:
3310           if (contents)
3311             free (contents);
3312           count = -1;
3313           goto done;
3314         }
3315
3316       size = 0;
3317       for (i = secsymend; i < opdsymend; ++i)
3318         {
3319           bfd_vma ent;
3320
3321           /* Ignore bogus symbols.  */
3322           if (syms[i]->value > opd->size - 8)
3323             continue;
3324
3325           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3326           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3327             {
3328               ++count;
3329               size += sizeof (asymbol);
3330               size += strlen (syms[i]->name) + 2;
3331             }
3332         }
3333
3334       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3335       if (dyn_count != 0
3336           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3337         {
3338           bfd_byte *dynbuf, *extdyn, *extdynend;
3339           size_t extdynsize;
3340           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3341
3342           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3343             goto free_contents_and_exit;
3344
3345           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3346           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3347
3348           extdyn = dynbuf;
3349           extdynend = extdyn + dynamic->size;
3350           for (; extdyn < extdynend; extdyn += extdynsize)
3351             {
3352               Elf_Internal_Dyn dyn;
3353               (*swap_dyn_in) (abfd, extdyn, &dyn);
3354
3355               if (dyn.d_tag == DT_NULL)
3356                 break;
3357
3358               if (dyn.d_tag == DT_PPC64_GLINK)
3359                 {
3360                   /* The first glink stub starts at offset 32; see
3361                      comment in ppc64_elf_finish_dynamic_sections. */
3362                   glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
3363                   /* The .glink section usually does not survive the final
3364                      link; search for the section (usually .text) where the
3365                      glink stubs now reside.  */
3366                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3367                                                 &glink_vma);
3368                   break;
3369                 }
3370             }
3371
3372           free (dynbuf);
3373         }
3374
3375       if (glink != NULL)
3376         {
3377           /* Determine __glink trampoline by reading the relative branch
3378              from the first glink stub.  */
3379           bfd_byte buf[4];
3380           unsigned int off = 0;
3381
3382           while (bfd_get_section_contents (abfd, glink, buf,
3383                                            glink_vma + off - glink->vma, 4))
3384             {
3385               unsigned int insn = bfd_get_32 (abfd, buf);
3386               insn ^= B_DOT;
3387               if ((insn & ~0x3fffffc) == 0)
3388                 {
3389                   resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3390                   break;
3391                 }
3392               off += 4;
3393               if (off > 4)
3394                 break;
3395             }
3396
3397           if (resolv_vma)
3398             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3399
3400           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3401           if (relplt != NULL)
3402             {
3403               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3404               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3405                 goto free_contents_and_exit;
3406
3407               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3408               size += plt_count * sizeof (asymbol);
3409
3410               p = relplt->relocation;
3411               for (i = 0; i < plt_count; i++, p++)
3412                 {
3413                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3414                   if (p->addend != 0)
3415                     size += sizeof ("+0x") - 1 + 16;
3416                 }
3417             }
3418         }
3419
3420       s = *ret = bfd_malloc (size);
3421       if (s == NULL)
3422         goto free_contents_and_exit;
3423
3424       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3425
3426       for (i = secsymend; i < opdsymend; ++i)
3427         {
3428           bfd_vma ent;
3429
3430           if (syms[i]->value > opd->size - 8)
3431             continue;
3432
3433           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3434           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3435             {
3436               long lo, hi;
3437               size_t len;
3438               asection *sec = abfd->sections;
3439
3440               *s = *syms[i];
3441               lo = codesecsym;
3442               hi = codesecsymend;
3443               while (lo < hi)
3444                 {
3445                   long mid = (lo + hi) >> 1;
3446                   if (syms[mid]->section->vma < ent)
3447                     lo = mid + 1;
3448                   else if (syms[mid]->section->vma > ent)
3449                     hi = mid;
3450                   else
3451                     {
3452                       sec = syms[mid]->section;
3453                       break;
3454                     }
3455                 }
3456
3457               if (lo >= hi && lo > codesecsym)
3458                 sec = syms[lo - 1]->section;
3459
3460               for (; sec != NULL; sec = sec->next)
3461                 {
3462                   if (sec->vma > ent)
3463                     break;
3464                   /* SEC_LOAD may not be set if SEC is from a separate debug
3465                      info file.  */
3466                   if ((sec->flags & SEC_ALLOC) == 0)
3467                     break;
3468                   if ((sec->flags & SEC_CODE) != 0)
3469                     s->section = sec;
3470                 }
3471               s->flags |= BSF_SYNTHETIC;
3472               s->value = ent - s->section->vma;
3473               s->name = names;
3474               *names++ = '.';
3475               len = strlen (syms[i]->name);
3476               memcpy (names, syms[i]->name, len + 1);
3477               names += len + 1;
3478               /* Have udata.p point back to the original symbol this
3479                  synthetic symbol was derived from.  */
3480               s->udata.p = syms[i];
3481               s++;
3482             }
3483         }
3484       free (contents);
3485
3486       if (glink != NULL && relplt != NULL)
3487         {
3488           if (resolv_vma)
3489             {
3490               /* Add a symbol for the main glink trampoline.  */
3491               memset (s, 0, sizeof *s);
3492               s->the_bfd = abfd;
3493               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3494               s->section = glink;
3495               s->value = resolv_vma - glink->vma;
3496               s->name = names;
3497               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3498               names += sizeof ("__glink_PLTresolve");
3499               s++;
3500               count++;
3501             }
3502
3503           /* FIXME: It would be very much nicer to put sym@plt on the
3504              stub rather than on the glink branch table entry.  The
3505              objdump disassembler would then use a sensible symbol
3506              name on plt calls.  The difficulty in doing so is
3507              a) finding the stubs, and,
3508              b) matching stubs against plt entries, and,
3509              c) there can be multiple stubs for a given plt entry.
3510
3511              Solving (a) could be done by code scanning, but older
3512              ppc64 binaries used different stubs to current code.
3513              (b) is the tricky one since you need to known the toc
3514              pointer for at least one function that uses a pic stub to
3515              be able to calculate the plt address referenced.
3516              (c) means gdb would need to set multiple breakpoints (or
3517              find the glink branch itself) when setting breakpoints
3518              for pending shared library loads.  */
3519           p = relplt->relocation;
3520           for (i = 0; i < plt_count; i++, p++)
3521             {
3522               size_t len;
3523
3524               *s = **p->sym_ptr_ptr;
3525               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3526                  we are defining a symbol, ensure one of them is set.  */
3527               if ((s->flags & BSF_LOCAL) == 0)
3528                 s->flags |= BSF_GLOBAL;
3529               s->flags |= BSF_SYNTHETIC;
3530               s->section = glink;
3531               s->value = glink_vma - glink->vma;
3532               s->name = names;
3533               s->udata.p = NULL;
3534               len = strlen ((*p->sym_ptr_ptr)->name);
3535               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3536               names += len;
3537               if (p->addend != 0)
3538                 {
3539                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3540                   names += sizeof ("+0x") - 1;
3541                   bfd_sprintf_vma (abfd, names, p->addend);
3542                   names += strlen (names);
3543                 }
3544               memcpy (names, "@plt", sizeof ("@plt"));
3545               names += sizeof ("@plt");
3546               s++;
3547               if (abi < 2)
3548                 {
3549                   glink_vma += 8;
3550                   if (i >= 0x8000)
3551                     glink_vma += 4;
3552                 }
3553               else
3554                 glink_vma += 4;
3555             }
3556           count += plt_count;
3557         }
3558     }
3559
3560  done:
3561   free (syms);
3562   return count;
3563 }
3564 \f
3565 /* The following functions are specific to the ELF linker, while
3566    functions above are used generally.  Those named ppc64_elf_* are
3567    called by the main ELF linker code.  They appear in this file more
3568    or less in the order in which they are called.  eg.
3569    ppc64_elf_check_relocs is called early in the link process,
3570    ppc64_elf_finish_dynamic_sections is one of the last functions
3571    called.
3572
3573    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3574    functions have both a function code symbol and a function descriptor
3575    symbol.  A call to foo in a relocatable object file looks like:
3576
3577    .            .text
3578    .    x:
3579    .            bl      .foo
3580    .            nop
3581
3582    The function definition in another object file might be:
3583
3584    .            .section .opd
3585    .    foo:    .quad   .foo
3586    .            .quad   .TOC.@tocbase
3587    .            .quad   0
3588    .
3589    .            .text
3590    .    .foo:   blr
3591
3592    When the linker resolves the call during a static link, the branch
3593    unsurprisingly just goes to .foo and the .opd information is unused.
3594    If the function definition is in a shared library, things are a little
3595    different:  The call goes via a plt call stub, the opd information gets
3596    copied to the plt, and the linker patches the nop.
3597
3598    .    x:
3599    .            bl      .foo_stub
3600    .            ld      2,40(1)
3601    .
3602    .
3603    .    .foo_stub:
3604    .            std     2,40(1)                 # in practice, the call stub
3605    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
3606    .            addi    11,11,Lfoo@toc@l        # this is the general idea
3607    .            ld      12,0(11)
3608    .            ld      2,8(11)
3609    .            mtctr   12
3610    .            ld      11,16(11)
3611    .            bctr
3612    .
3613    .            .section .plt
3614    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3615
3616    The "reloc ()" notation is supposed to indicate that the linker emits
3617    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3618    copying.
3619
3620    What are the difficulties here?  Well, firstly, the relocations
3621    examined by the linker in check_relocs are against the function code
3622    sym .foo, while the dynamic relocation in the plt is emitted against
3623    the function descriptor symbol, foo.  Somewhere along the line, we need
3624    to carefully copy dynamic link information from one symbol to the other.
3625    Secondly, the generic part of the elf linker will make .foo a dynamic
3626    symbol as is normal for most other backends.  We need foo dynamic
3627    instead, at least for an application final link.  However, when
3628    creating a shared library containing foo, we need to have both symbols
3629    dynamic so that references to .foo are satisfied during the early
3630    stages of linking.  Otherwise the linker might decide to pull in a
3631    definition from some other object, eg. a static library.
3632
3633    Update: As of August 2004, we support a new convention.  Function
3634    calls may use the function descriptor symbol, ie. "bl foo".  This
3635    behaves exactly as "bl .foo".  */
3636
3637 /* Of those relocs that might be copied as dynamic relocs, this function
3638    selects those that must be copied when linking a shared library,
3639    even when the symbol is local.  */
3640
3641 static int
3642 must_be_dyn_reloc (struct bfd_link_info *info,
3643                    enum elf_ppc64_reloc_type r_type)
3644 {
3645   switch (r_type)
3646     {
3647     default:
3648       return 1;
3649
3650     case R_PPC64_REL32:
3651     case R_PPC64_REL64:
3652     case R_PPC64_REL30:
3653       return 0;
3654
3655     case R_PPC64_TPREL16:
3656     case R_PPC64_TPREL16_LO:
3657     case R_PPC64_TPREL16_HI:
3658     case R_PPC64_TPREL16_HA:
3659     case R_PPC64_TPREL16_DS:
3660     case R_PPC64_TPREL16_LO_DS:
3661     case R_PPC64_TPREL16_HIGH:
3662     case R_PPC64_TPREL16_HIGHA:
3663     case R_PPC64_TPREL16_HIGHER:
3664     case R_PPC64_TPREL16_HIGHERA:
3665     case R_PPC64_TPREL16_HIGHEST:
3666     case R_PPC64_TPREL16_HIGHESTA:
3667     case R_PPC64_TPREL64:
3668       return !info->executable;
3669     }
3670 }
3671
3672 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3673    copying dynamic variables from a shared lib into an app's dynbss
3674    section, and instead use a dynamic relocation to point into the
3675    shared lib.  With code that gcc generates, it's vital that this be
3676    enabled;  In the PowerPC64 ABI, the address of a function is actually
3677    the address of a function descriptor, which resides in the .opd
3678    section.  gcc uses the descriptor directly rather than going via the
3679    GOT as some other ABI's do, which means that initialized function
3680    pointers must reference the descriptor.  Thus, a function pointer
3681    initialized to the address of a function in a shared library will
3682    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3683    redefines the function descriptor symbol to point to the copy.  This
3684    presents a problem as a plt entry for that function is also
3685    initialized from the function descriptor symbol and the copy reloc
3686    may not be initialized first.  */
3687 #define ELIMINATE_COPY_RELOCS 1
3688
3689 /* Section name for stubs is the associated section name plus this
3690    string.  */
3691 #define STUB_SUFFIX ".stub"
3692
3693 /* Linker stubs.
3694    ppc_stub_long_branch:
3695    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3696    destination, but a 24 bit branch in a stub section will reach.
3697    .    b       dest
3698
3699    ppc_stub_plt_branch:
3700    Similar to the above, but a 24 bit branch in the stub section won't
3701    reach its destination.
3702    .    addis   %r11,%r2,xxx@toc@ha
3703    .    ld      %r12,xxx@toc@l(%r11)
3704    .    mtctr   %r12
3705    .    bctr
3706
3707    ppc_stub_plt_call:
3708    Used to call a function in a shared library.  If it so happens that
3709    the plt entry referenced crosses a 64k boundary, then an extra
3710    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3711    .    std     %r2,40(%r1)
3712    .    addis   %r11,%r2,xxx@toc@ha
3713    .    ld      %r12,xxx+0@toc@l(%r11)
3714    .    mtctr   %r12
3715    .    ld      %r2,xxx+8@toc@l(%r11)
3716    .    ld      %r11,xxx+16@toc@l(%r11)
3717    .    bctr
3718
3719    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3720    code to adjust the value and save r2 to support multiple toc sections.
3721    A ppc_stub_long_branch with an r2 offset looks like:
3722    .    std     %r2,40(%r1)
3723    .    addis   %r2,%r2,off@ha
3724    .    addi    %r2,%r2,off@l
3725    .    b       dest
3726
3727    A ppc_stub_plt_branch with an r2 offset looks like:
3728    .    std     %r2,40(%r1)
3729    .    addis   %r11,%r2,xxx@toc@ha
3730    .    ld      %r12,xxx@toc@l(%r11)
3731    .    addis   %r2,%r2,off@ha
3732    .    addi    %r2,%r2,off@l
3733    .    mtctr   %r12
3734    .    bctr
3735
3736    In cases where the "addis" instruction would add zero, the "addis" is
3737    omitted and following instructions modified slightly in some cases.
3738 */
3739
3740 enum ppc_stub_type {
3741   ppc_stub_none,
3742   ppc_stub_long_branch,
3743   ppc_stub_long_branch_r2off,
3744   ppc_stub_plt_branch,
3745   ppc_stub_plt_branch_r2off,
3746   ppc_stub_plt_call,
3747   ppc_stub_plt_call_r2save
3748 };
3749
3750 struct ppc_stub_hash_entry {
3751
3752   /* Base hash table entry structure.  */
3753   struct bfd_hash_entry root;
3754
3755   enum ppc_stub_type stub_type;
3756
3757   /* The stub section.  */
3758   asection *stub_sec;
3759
3760   /* Offset within stub_sec of the beginning of this stub.  */
3761   bfd_vma stub_offset;
3762
3763   /* Given the symbol's value and its section we can determine its final
3764      value when building the stubs (so the stub knows where to jump.  */
3765   bfd_vma target_value;
3766   asection *target_section;
3767
3768   /* The symbol table entry, if any, that this was derived from.  */
3769   struct ppc_link_hash_entry *h;
3770   struct plt_entry *plt_ent;
3771
3772   /* Where this stub is being called from, or, in the case of combined
3773      stub sections, the first input section in the group.  */
3774   asection *id_sec;
3775
3776   /* Symbol st_other.  */
3777   unsigned char other;
3778 };
3779
3780 struct ppc_branch_hash_entry {
3781
3782   /* Base hash table entry structure.  */
3783   struct bfd_hash_entry root;
3784
3785   /* Offset within branch lookup table.  */
3786   unsigned int offset;
3787
3788   /* Generation marker.  */
3789   unsigned int iter;
3790 };
3791
3792 /* Used to track dynamic relocations for local symbols.  */
3793 struct ppc_dyn_relocs
3794 {
3795   struct ppc_dyn_relocs *next;
3796
3797   /* The input section of the reloc.  */
3798   asection *sec;
3799
3800   /* Total number of relocs copied for the input section.  */
3801   unsigned int count : 31;
3802
3803   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
3804   unsigned int ifunc : 1;
3805 };
3806
3807 struct ppc_link_hash_entry
3808 {
3809   struct elf_link_hash_entry elf;
3810
3811   union {
3812     /* A pointer to the most recently used stub hash entry against this
3813        symbol.  */
3814     struct ppc_stub_hash_entry *stub_cache;
3815
3816     /* A pointer to the next symbol starting with a '.'  */
3817     struct ppc_link_hash_entry *next_dot_sym;
3818   } u;
3819
3820   /* Track dynamic relocs copied for this symbol.  */
3821   struct elf_dyn_relocs *dyn_relocs;
3822
3823   /* Link between function code and descriptor symbols.  */
3824   struct ppc_link_hash_entry *oh;
3825
3826   /* Flag function code and descriptor symbols.  */
3827   unsigned int is_func:1;
3828   unsigned int is_func_descriptor:1;
3829   unsigned int fake:1;
3830
3831   /* Whether global opd/toc sym has been adjusted or not.
3832      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3833      should be set for all globals defined in any opd/toc section.  */
3834   unsigned int adjust_done:1;
3835
3836   /* Set if we twiddled this symbol to weak at some stage.  */
3837   unsigned int was_undefined:1;
3838
3839   /* Contexts in which symbol is used in the GOT (or TOC).
3840      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3841      corresponding relocs are encountered during check_relocs.
3842      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3843      indicate the corresponding GOT entry type is not needed.
3844      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3845      a TPREL one.  We use a separate flag rather than setting TPREL
3846      just for convenience in distinguishing the two cases.  */
3847 #define TLS_GD           1      /* GD reloc. */
3848 #define TLS_LD           2      /* LD reloc. */
3849 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
3850 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
3851 #define TLS_TLS         16      /* Any TLS reloc.  */
3852 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
3853 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
3854 #define PLT_IFUNC      128      /* STT_GNU_IFUNC.  */
3855   unsigned char tls_mask;
3856 };
3857
3858 /* ppc64 ELF linker hash table.  */
3859
3860 struct ppc_link_hash_table
3861 {
3862   struct elf_link_hash_table elf;
3863
3864   /* The stub hash table.  */
3865   struct bfd_hash_table stub_hash_table;
3866
3867   /* Another hash table for plt_branch stubs.  */
3868   struct bfd_hash_table branch_hash_table;
3869
3870   /* Hash table for function prologue tocsave.  */
3871   htab_t tocsave_htab;
3872
3873   /* Linker stub bfd.  */
3874   bfd *stub_bfd;
3875
3876   /* Linker call-backs.  */
3877   asection * (*add_stub_section) (const char *, asection *);
3878   void (*layout_sections_again) (void);
3879
3880   /* Array to keep track of which stub sections have been created, and
3881      information on stub grouping.  */
3882   struct map_stub {
3883     /* This is the section to which stubs in the group will be attached.  */
3884     asection *link_sec;
3885     /* The stub section.  */
3886     asection *stub_sec;
3887     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
3888     bfd_vma toc_off;
3889   } *stub_group;
3890
3891   /* Temp used when calculating TOC pointers.  */
3892   bfd_vma toc_curr;
3893   bfd *toc_bfd;
3894   asection *toc_first_sec;
3895
3896   /* Highest input section id.  */
3897   int top_id;
3898
3899   /* Highest output section index.  */
3900   int top_index;
3901
3902   /* Used when adding symbols.  */
3903   struct ppc_link_hash_entry *dot_syms;
3904
3905   /* List of input sections for each output section.  */
3906   asection **input_list;
3907
3908   /* Short-cuts to get to dynamic linker sections.  */
3909   asection *got;
3910   asection *plt;
3911   asection *relplt;
3912   asection *iplt;
3913   asection *reliplt;
3914   asection *dynbss;
3915   asection *relbss;
3916   asection *glink;
3917   asection *sfpr;
3918   asection *brlt;
3919   asection *relbrlt;
3920   asection *glink_eh_frame;
3921
3922   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3923   struct ppc_link_hash_entry *tls_get_addr;
3924   struct ppc_link_hash_entry *tls_get_addr_fd;
3925
3926   /* The size of reliplt used by got entry relocs.  */
3927   bfd_size_type got_reli_size;
3928
3929   /* Statistics.  */
3930   unsigned long stub_count[ppc_stub_plt_call_r2save];
3931
3932   /* Number of stubs against global syms.  */
3933   unsigned long stub_globals;
3934
3935   /* Alignment of PLT call stubs.  */
3936   unsigned int plt_stub_align:4;
3937
3938   /* Set if we're linking code with function descriptors.  */
3939   unsigned int opd_abi:1;
3940
3941   /* Set if PLT call stubs should load r11.  */
3942   unsigned int plt_static_chain:1;
3943
3944   /* Set if PLT call stubs need a read-read barrier.  */
3945   unsigned int plt_thread_safe:1;
3946
3947   /* Set if we should emit symbols for stubs.  */
3948   unsigned int emit_stub_syms:1;
3949
3950   /* Set if __tls_get_addr optimization should not be done.  */
3951   unsigned int no_tls_get_addr_opt:1;
3952
3953   /* Support for multiple toc sections.  */
3954   unsigned int do_multi_toc:1;
3955   unsigned int multi_toc_needed:1;
3956   unsigned int second_toc_pass:1;
3957   unsigned int do_toc_opt:1;
3958
3959   /* Set on error.  */
3960   unsigned int stub_error:1;
3961
3962   /* Temp used by ppc64_elf_process_dot_syms.  */
3963   unsigned int twiddled_syms:1;
3964
3965   /* Incremented every time we size stubs.  */
3966   unsigned int stub_iteration;
3967
3968   /* Small local sym cache.  */
3969   struct sym_cache sym_cache;
3970 };
3971
3972 /* Rename some of the generic section flags to better document how they
3973    are used here.  */
3974
3975 /* Nonzero if this section has TLS related relocations.  */
3976 #define has_tls_reloc sec_flg0
3977
3978 /* Nonzero if this section has a call to __tls_get_addr.  */
3979 #define has_tls_get_addr_call sec_flg1
3980
3981 /* Nonzero if this section has any toc or got relocs.  */
3982 #define has_toc_reloc sec_flg2
3983
3984 /* Nonzero if this section has a call to another section that uses
3985    the toc or got.  */
3986 #define makes_toc_func_call sec_flg3
3987
3988 /* Recursion protection when determining above flag.  */
3989 #define call_check_in_progress sec_flg4
3990 #define call_check_done sec_flg5
3991
3992 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
3993
3994 #define ppc_hash_table(p) \
3995   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3996   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3997
3998 #define ppc_stub_hash_lookup(table, string, create, copy) \
3999   ((struct ppc_stub_hash_entry *) \
4000    bfd_hash_lookup ((table), (string), (create), (copy)))
4001
4002 #define ppc_branch_hash_lookup(table, string, create, copy) \
4003   ((struct ppc_branch_hash_entry *) \
4004    bfd_hash_lookup ((table), (string), (create), (copy)))
4005
4006 /* Create an entry in the stub hash table.  */
4007
4008 static struct bfd_hash_entry *
4009 stub_hash_newfunc (struct bfd_hash_entry *entry,
4010                    struct bfd_hash_table *table,
4011                    const char *string)
4012 {
4013   /* Allocate the structure if it has not already been allocated by a
4014      subclass.  */
4015   if (entry == NULL)
4016     {
4017       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4018       if (entry == NULL)
4019         return entry;
4020     }
4021
4022   /* Call the allocation method of the superclass.  */
4023   entry = bfd_hash_newfunc (entry, table, string);
4024   if (entry != NULL)
4025     {
4026       struct ppc_stub_hash_entry *eh;
4027
4028       /* Initialize the local fields.  */
4029       eh = (struct ppc_stub_hash_entry *) entry;
4030       eh->stub_type = ppc_stub_none;
4031       eh->stub_sec = NULL;
4032       eh->stub_offset = 0;
4033       eh->target_value = 0;
4034       eh->target_section = NULL;
4035       eh->h = NULL;
4036       eh->plt_ent = NULL;
4037       eh->id_sec = NULL;
4038       eh->other = 0;
4039     }
4040
4041   return entry;
4042 }
4043
4044 /* Create an entry in the branch hash table.  */
4045
4046 static struct bfd_hash_entry *
4047 branch_hash_newfunc (struct bfd_hash_entry *entry,
4048                      struct bfd_hash_table *table,
4049                      const char *string)
4050 {
4051   /* Allocate the structure if it has not already been allocated by a
4052      subclass.  */
4053   if (entry == NULL)
4054     {
4055       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4056       if (entry == NULL)
4057         return entry;
4058     }
4059
4060   /* Call the allocation method of the superclass.  */
4061   entry = bfd_hash_newfunc (entry, table, string);
4062   if (entry != NULL)
4063     {
4064       struct ppc_branch_hash_entry *eh;
4065
4066       /* Initialize the local fields.  */
4067       eh = (struct ppc_branch_hash_entry *) entry;
4068       eh->offset = 0;
4069       eh->iter = 0;
4070     }
4071
4072   return entry;
4073 }
4074
4075 /* Create an entry in a ppc64 ELF linker hash table.  */
4076
4077 static struct bfd_hash_entry *
4078 link_hash_newfunc (struct bfd_hash_entry *entry,
4079                    struct bfd_hash_table *table,
4080                    const char *string)
4081 {
4082   /* Allocate the structure if it has not already been allocated by a
4083      subclass.  */
4084   if (entry == NULL)
4085     {
4086       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4087       if (entry == NULL)
4088         return entry;
4089     }
4090
4091   /* Call the allocation method of the superclass.  */
4092   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4093   if (entry != NULL)
4094     {
4095       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4096
4097       memset (&eh->u.stub_cache, 0,
4098               (sizeof (struct ppc_link_hash_entry)
4099                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4100
4101       /* When making function calls, old ABI code references function entry
4102          points (dot symbols), while new ABI code references the function
4103          descriptor symbol.  We need to make any combination of reference and
4104          definition work together, without breaking archive linking.
4105
4106          For a defined function "foo" and an undefined call to "bar":
4107          An old object defines "foo" and ".foo", references ".bar" (possibly
4108          "bar" too).
4109          A new object defines "foo" and references "bar".
4110
4111          A new object thus has no problem with its undefined symbols being
4112          satisfied by definitions in an old object.  On the other hand, the
4113          old object won't have ".bar" satisfied by a new object.
4114
4115          Keep a list of newly added dot-symbols.  */
4116
4117       if (string[0] == '.')
4118         {
4119           struct ppc_link_hash_table *htab;
4120
4121           htab = (struct ppc_link_hash_table *) table;
4122           eh->u.next_dot_sym = htab->dot_syms;
4123           htab->dot_syms = eh;
4124         }
4125     }
4126
4127   return entry;
4128 }
4129
4130 struct tocsave_entry {
4131   asection *sec;
4132   bfd_vma offset;
4133 };
4134
4135 static hashval_t
4136 tocsave_htab_hash (const void *p)
4137 {
4138   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4139   return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4140 }
4141
4142 static int
4143 tocsave_htab_eq (const void *p1, const void *p2)
4144 {
4145   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4146   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4147   return e1->sec == e2->sec && e1->offset == e2->offset;
4148 }
4149
4150 /* Create a ppc64 ELF linker hash table.  */
4151
4152 static struct bfd_link_hash_table *
4153 ppc64_elf_link_hash_table_create (bfd *abfd)
4154 {
4155   struct ppc_link_hash_table *htab;
4156   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4157
4158   htab = bfd_zmalloc (amt);
4159   if (htab == NULL)
4160     return NULL;
4161
4162   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4163                                       sizeof (struct ppc_link_hash_entry),
4164                                       PPC64_ELF_DATA))
4165     {
4166       free (htab);
4167       return NULL;
4168     }
4169
4170   /* Init the stub hash table too.  */
4171   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4172                             sizeof (struct ppc_stub_hash_entry)))
4173     return NULL;
4174
4175   /* And the branch hash table.  */
4176   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4177                             sizeof (struct ppc_branch_hash_entry)))
4178     return NULL;
4179
4180   htab->tocsave_htab = htab_try_create (1024,
4181                                         tocsave_htab_hash,
4182                                         tocsave_htab_eq,
4183                                         NULL);
4184   if (htab->tocsave_htab == NULL)
4185     return NULL;
4186
4187   /* Initializing two fields of the union is just cosmetic.  We really
4188      only care about glist, but when compiled on a 32-bit host the
4189      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
4190      debugger inspection of these fields look nicer.  */
4191   htab->elf.init_got_refcount.refcount = 0;
4192   htab->elf.init_got_refcount.glist = NULL;
4193   htab->elf.init_plt_refcount.refcount = 0;
4194   htab->elf.init_plt_refcount.glist = NULL;
4195   htab->elf.init_got_offset.offset = 0;
4196   htab->elf.init_got_offset.glist = NULL;
4197   htab->elf.init_plt_offset.offset = 0;
4198   htab->elf.init_plt_offset.glist = NULL;
4199
4200   return &htab->elf.root;
4201 }
4202
4203 /* Free the derived linker hash table.  */
4204
4205 static void
4206 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
4207 {
4208   struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
4209
4210   bfd_hash_table_free (&htab->stub_hash_table);
4211   bfd_hash_table_free (&htab->branch_hash_table);
4212   if (htab->tocsave_htab)
4213     htab_delete (htab->tocsave_htab);
4214   _bfd_elf_link_hash_table_free (hash);
4215 }
4216
4217 /* Create sections for linker generated code.  */
4218
4219 static bfd_boolean
4220 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4221 {
4222   struct ppc_link_hash_table *htab;
4223   flagword flags;
4224
4225   htab = ppc_hash_table (info);
4226
4227   /* Create .sfpr for code to save and restore fp regs.  */
4228   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4229            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4230   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4231                                                    flags);
4232   if (htab->sfpr == NULL
4233       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4234     return FALSE;
4235
4236   /* Create .glink for lazy dynamic linking support.  */
4237   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4238                                                     flags);
4239   if (htab->glink == NULL
4240       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4241     return FALSE;
4242
4243   if (!info->no_ld_generated_unwind_info)
4244     {
4245       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4246                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4247       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4248                                                                  ".eh_frame",
4249                                                                  flags);
4250       if (htab->glink_eh_frame == NULL
4251           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4252         return FALSE;
4253     }
4254
4255   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4256   htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4257   if (htab->iplt == NULL
4258       || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4259     return FALSE;
4260
4261   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4262            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4263   htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4264                                                       ".rela.iplt",
4265                                                       flags);
4266   if (htab->reliplt == NULL
4267       || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4268     return FALSE;
4269
4270   /* Create branch lookup table for plt_branch stubs.  */
4271   flags = (SEC_ALLOC | SEC_LOAD
4272            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4273   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4274                                                    flags);
4275   if (htab->brlt == NULL
4276       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4277     return FALSE;
4278
4279   if (!info->shared)
4280     return TRUE;
4281
4282   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4283            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4284   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4285                                                       ".rela.branch_lt",
4286                                                       flags);
4287   if (htab->relbrlt == NULL
4288       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4289     return FALSE;
4290
4291   return TRUE;
4292 }
4293
4294 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4295
4296 bfd_boolean
4297 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4298 {
4299   struct ppc_link_hash_table *htab;
4300
4301   elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4302
4303 /* Always hook our dynamic sections into the first bfd, which is the
4304    linker created stub bfd.  This ensures that the GOT header is at
4305    the start of the output TOC section.  */
4306   htab = ppc_hash_table (info);
4307   if (htab == NULL)
4308     return FALSE;
4309   htab->stub_bfd = abfd;
4310   htab->elf.dynobj = abfd;
4311
4312   if (info->relocatable)
4313     return TRUE;
4314
4315   return create_linkage_sections (htab->elf.dynobj, info);
4316 }
4317
4318 /* Build a name for an entry in the stub hash table.  */
4319
4320 static char *
4321 ppc_stub_name (const asection *input_section,
4322                const asection *sym_sec,
4323                const struct ppc_link_hash_entry *h,
4324                const Elf_Internal_Rela *rel)
4325 {
4326   char *stub_name;
4327   ssize_t len;
4328
4329   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4330      offsets from a sym as a branch target?  In fact, we could
4331      probably assume the addend is always zero.  */
4332   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4333
4334   if (h)
4335     {
4336       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4337       stub_name = bfd_malloc (len);
4338       if (stub_name == NULL)
4339         return stub_name;
4340
4341       len = sprintf (stub_name, "%08x.%s+%x",
4342                      input_section->id & 0xffffffff,
4343                      h->elf.root.root.string,
4344                      (int) rel->r_addend & 0xffffffff);
4345     }
4346   else
4347     {
4348       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4349       stub_name = bfd_malloc (len);
4350       if (stub_name == NULL)
4351         return stub_name;
4352
4353       len = sprintf (stub_name, "%08x.%x:%x+%x",
4354                      input_section->id & 0xffffffff,
4355                      sym_sec->id & 0xffffffff,
4356                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4357                      (int) rel->r_addend & 0xffffffff);
4358     }
4359   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4360     stub_name[len - 2] = 0;
4361   return stub_name;
4362 }
4363
4364 /* Look up an entry in the stub hash.  Stub entries are cached because
4365    creating the stub name takes a bit of time.  */
4366
4367 static struct ppc_stub_hash_entry *
4368 ppc_get_stub_entry (const asection *input_section,
4369                     const asection *sym_sec,
4370                     struct ppc_link_hash_entry *h,
4371                     const Elf_Internal_Rela *rel,
4372                     struct ppc_link_hash_table *htab)
4373 {
4374   struct ppc_stub_hash_entry *stub_entry;
4375   const asection *id_sec;
4376
4377   /* If this input section is part of a group of sections sharing one
4378      stub section, then use the id of the first section in the group.
4379      Stub names need to include a section id, as there may well be
4380      more than one stub used to reach say, printf, and we need to
4381      distinguish between them.  */
4382   id_sec = htab->stub_group[input_section->id].link_sec;
4383
4384   if (h != NULL && h->u.stub_cache != NULL
4385       && h->u.stub_cache->h == h
4386       && h->u.stub_cache->id_sec == id_sec)
4387     {
4388       stub_entry = h->u.stub_cache;
4389     }
4390   else
4391     {
4392       char *stub_name;
4393
4394       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4395       if (stub_name == NULL)
4396         return NULL;
4397
4398       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4399                                          stub_name, FALSE, FALSE);
4400       if (h != NULL)
4401         h->u.stub_cache = stub_entry;
4402
4403       free (stub_name);
4404     }
4405
4406   return stub_entry;
4407 }
4408
4409 /* Add a new stub entry to the stub hash.  Not all fields of the new
4410    stub entry are initialised.  */
4411
4412 static struct ppc_stub_hash_entry *
4413 ppc_add_stub (const char *stub_name,
4414               asection *section,
4415               struct bfd_link_info *info)
4416 {
4417   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4418   asection *link_sec;
4419   asection *stub_sec;
4420   struct ppc_stub_hash_entry *stub_entry;
4421
4422   link_sec = htab->stub_group[section->id].link_sec;
4423   stub_sec = htab->stub_group[section->id].stub_sec;
4424   if (stub_sec == NULL)
4425     {
4426       stub_sec = htab->stub_group[link_sec->id].stub_sec;
4427       if (stub_sec == NULL)
4428         {
4429           size_t namelen;
4430           bfd_size_type len;
4431           char *s_name;
4432
4433           namelen = strlen (link_sec->name);
4434           len = namelen + sizeof (STUB_SUFFIX);
4435           s_name = bfd_alloc (htab->stub_bfd, len);
4436           if (s_name == NULL)
4437             return NULL;
4438
4439           memcpy (s_name, link_sec->name, namelen);
4440           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4441           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4442           if (stub_sec == NULL)
4443             return NULL;
4444           htab->stub_group[link_sec->id].stub_sec = stub_sec;
4445         }
4446       htab->stub_group[section->id].stub_sec = stub_sec;
4447     }
4448
4449   /* Enter this entry into the linker stub hash table.  */
4450   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4451                                      TRUE, FALSE);
4452   if (stub_entry == NULL)
4453     {
4454       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4455                               section->owner, stub_name);
4456       return NULL;
4457     }
4458
4459   stub_entry->stub_sec = stub_sec;
4460   stub_entry->stub_offset = 0;
4461   stub_entry->id_sec = link_sec;
4462   return stub_entry;
4463 }
4464
4465 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4466    not already done.  */
4467
4468 static bfd_boolean
4469 create_got_section (bfd *abfd, struct bfd_link_info *info)
4470 {
4471   asection *got, *relgot;
4472   flagword flags;
4473   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4474
4475   if (!is_ppc64_elf (abfd))
4476     return FALSE;
4477   if (htab == NULL)
4478     return FALSE;
4479
4480   if (!htab->got)
4481     {
4482       if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4483         return FALSE;
4484
4485       htab->got = bfd_get_linker_section (htab->elf.dynobj, ".got");
4486       if (!htab->got)
4487         abort ();
4488     }
4489
4490   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4491            | SEC_LINKER_CREATED);
4492
4493   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4494   if (!got
4495       || !bfd_set_section_alignment (abfd, got, 3))
4496     return FALSE;
4497
4498   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4499                                                flags | SEC_READONLY);
4500   if (!relgot
4501       || ! bfd_set_section_alignment (abfd, relgot, 3))
4502     return FALSE;
4503
4504   ppc64_elf_tdata (abfd)->got = got;
4505   ppc64_elf_tdata (abfd)->relgot = relgot;
4506   return TRUE;
4507 }
4508
4509 /* Create the dynamic sections, and set up shortcuts.  */
4510
4511 static bfd_boolean
4512 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4513 {
4514   struct ppc_link_hash_table *htab;
4515
4516   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4517     return FALSE;
4518
4519   htab = ppc_hash_table (info);
4520   if (htab == NULL)
4521     return FALSE;
4522
4523   if (!htab->got)
4524     htab->got = bfd_get_linker_section (dynobj, ".got");
4525   htab->plt = bfd_get_linker_section (dynobj, ".plt");
4526   htab->relplt = bfd_get_linker_section (dynobj, ".rela.plt");
4527   htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
4528   if (!info->shared)
4529     htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
4530
4531   if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4532       || (!info->shared && !htab->relbss))
4533     abort ();
4534
4535   return TRUE;
4536 }
4537
4538 /* Follow indirect and warning symbol links.  */
4539
4540 static inline struct bfd_link_hash_entry *
4541 follow_link (struct bfd_link_hash_entry *h)
4542 {
4543   while (h->type == bfd_link_hash_indirect
4544          || h->type == bfd_link_hash_warning)
4545     h = h->u.i.link;
4546   return h;
4547 }
4548
4549 static inline struct elf_link_hash_entry *
4550 elf_follow_link (struct elf_link_hash_entry *h)
4551 {
4552   return (struct elf_link_hash_entry *) follow_link (&h->root);
4553 }
4554
4555 static inline struct ppc_link_hash_entry *
4556 ppc_follow_link (struct ppc_link_hash_entry *h)
4557 {
4558   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4559 }
4560
4561 /* Merge PLT info on FROM with that on TO.  */
4562
4563 static void
4564 move_plt_plist (struct ppc_link_hash_entry *from,
4565                 struct ppc_link_hash_entry *to)
4566 {
4567   if (from->elf.plt.plist != NULL)
4568     {
4569       if (to->elf.plt.plist != NULL)
4570         {
4571           struct plt_entry **entp;
4572           struct plt_entry *ent;
4573
4574           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4575             {
4576               struct plt_entry *dent;
4577
4578               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4579                 if (dent->addend == ent->addend)
4580                   {
4581                     dent->plt.refcount += ent->plt.refcount;
4582                     *entp = ent->next;
4583                     break;
4584                   }
4585               if (dent == NULL)
4586                 entp = &ent->next;
4587             }
4588           *entp = to->elf.plt.plist;
4589         }
4590
4591       to->elf.plt.plist = from->elf.plt.plist;
4592       from->elf.plt.plist = NULL;
4593     }
4594 }
4595
4596 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4597
4598 static void
4599 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4600                                 struct elf_link_hash_entry *dir,
4601                                 struct elf_link_hash_entry *ind)
4602 {
4603   struct ppc_link_hash_entry *edir, *eind;
4604
4605   edir = (struct ppc_link_hash_entry *) dir;
4606   eind = (struct ppc_link_hash_entry *) ind;
4607
4608   edir->is_func |= eind->is_func;
4609   edir->is_func_descriptor |= eind->is_func_descriptor;
4610   edir->tls_mask |= eind->tls_mask;
4611   if (eind->oh != NULL)
4612     edir->oh = ppc_follow_link (eind->oh);
4613
4614   /* If called to transfer flags for a weakdef during processing
4615      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4616      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
4617   if (!(ELIMINATE_COPY_RELOCS
4618         && eind->elf.root.type != bfd_link_hash_indirect
4619         && edir->elf.dynamic_adjusted))
4620     edir->elf.non_got_ref |= eind->elf.non_got_ref;
4621
4622   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4623   edir->elf.ref_regular |= eind->elf.ref_regular;
4624   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4625   edir->elf.needs_plt |= eind->elf.needs_plt;
4626
4627   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4628   if (eind->dyn_relocs != NULL)
4629     {
4630       if (edir->dyn_relocs != NULL)
4631         {
4632           struct elf_dyn_relocs **pp;
4633           struct elf_dyn_relocs *p;
4634
4635           /* Add reloc counts against the indirect sym to the direct sym
4636              list.  Merge any entries against the same section.  */
4637           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4638             {
4639               struct elf_dyn_relocs *q;
4640
4641               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4642                 if (q->sec == p->sec)
4643                   {
4644                     q->pc_count += p->pc_count;
4645                     q->count += p->count;
4646                     *pp = p->next;
4647                     break;
4648                   }
4649               if (q == NULL)
4650                 pp = &p->next;
4651             }
4652           *pp = edir->dyn_relocs;
4653         }
4654
4655       edir->dyn_relocs = eind->dyn_relocs;
4656       eind->dyn_relocs = NULL;
4657     }
4658
4659   /* If we were called to copy over info for a weak sym, that's all.
4660      You might think dyn_relocs need not be copied over;  After all,
4661      both syms will be dynamic or both non-dynamic so we're just
4662      moving reloc accounting around.  However, ELIMINATE_COPY_RELOCS
4663      code in ppc64_elf_adjust_dynamic_symbol needs to check for
4664      dyn_relocs in read-only sections, and it does so on what is the
4665      DIR sym here.  */
4666   if (eind->elf.root.type != bfd_link_hash_indirect)
4667     return;
4668
4669   /* Copy over got entries that we may have already seen to the
4670      symbol which just became indirect.  */
4671   if (eind->elf.got.glist != NULL)
4672     {
4673       if (edir->elf.got.glist != NULL)
4674         {
4675           struct got_entry **entp;
4676           struct got_entry *ent;
4677
4678           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4679             {
4680               struct got_entry *dent;
4681
4682               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4683                 if (dent->addend == ent->addend
4684                     && dent->owner == ent->owner
4685                     && dent->tls_type == ent->tls_type)
4686                   {
4687                     dent->got.refcount += ent->got.refcount;
4688                     *entp = ent->next;
4689                     break;
4690                   }
4691               if (dent == NULL)
4692                 entp = &ent->next;
4693             }
4694           *entp = edir->elf.got.glist;
4695         }
4696
4697       edir->elf.got.glist = eind->elf.got.glist;
4698       eind->elf.got.glist = NULL;
4699     }
4700
4701   /* And plt entries.  */
4702   move_plt_plist (eind, edir);
4703
4704   if (eind->elf.dynindx != -1)
4705     {
4706       if (edir->elf.dynindx != -1)
4707         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4708                                 edir->elf.dynstr_index);
4709       edir->elf.dynindx = eind->elf.dynindx;
4710       edir->elf.dynstr_index = eind->elf.dynstr_index;
4711       eind->elf.dynindx = -1;
4712       eind->elf.dynstr_index = 0;
4713     }
4714 }
4715
4716 /* Find the function descriptor hash entry from the given function code
4717    hash entry FH.  Link the entries via their OH fields.  */
4718
4719 static struct ppc_link_hash_entry *
4720 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4721 {
4722   struct ppc_link_hash_entry *fdh = fh->oh;
4723
4724   if (fdh == NULL)
4725     {
4726       const char *fd_name = fh->elf.root.root.string + 1;
4727
4728       fdh = (struct ppc_link_hash_entry *)
4729         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4730       if (fdh == NULL)
4731         return fdh;
4732
4733       fdh->is_func_descriptor = 1;
4734       fdh->oh = fh;
4735       fh->is_func = 1;
4736       fh->oh = fdh;
4737     }
4738
4739   return ppc_follow_link (fdh);
4740 }
4741
4742 /* Make a fake function descriptor sym for the code sym FH.  */
4743
4744 static struct ppc_link_hash_entry *
4745 make_fdh (struct bfd_link_info *info,
4746           struct ppc_link_hash_entry *fh)
4747 {
4748   bfd *abfd;
4749   asymbol *newsym;
4750   struct bfd_link_hash_entry *bh;
4751   struct ppc_link_hash_entry *fdh;
4752
4753   abfd = fh->elf.root.u.undef.abfd;
4754   newsym = bfd_make_empty_symbol (abfd);
4755   newsym->name = fh->elf.root.root.string + 1;
4756   newsym->section = bfd_und_section_ptr;
4757   newsym->value = 0;
4758   newsym->flags = BSF_WEAK;
4759
4760   bh = NULL;
4761   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4762                                          newsym->flags, newsym->section,
4763                                          newsym->value, NULL, FALSE, FALSE,
4764                                          &bh))
4765     return NULL;
4766
4767   fdh = (struct ppc_link_hash_entry *) bh;
4768   fdh->elf.non_elf = 0;
4769   fdh->fake = 1;
4770   fdh->is_func_descriptor = 1;
4771   fdh->oh = fh;
4772   fh->is_func = 1;
4773   fh->oh = fdh;
4774   return fdh;
4775 }
4776
4777 /* Fix function descriptor symbols defined in .opd sections to be
4778    function type.  */
4779
4780 static bfd_boolean
4781 ppc64_elf_add_symbol_hook (bfd *ibfd,
4782                            struct bfd_link_info *info,
4783                            Elf_Internal_Sym *isym,
4784                            const char **name,
4785                            flagword *flags ATTRIBUTE_UNUSED,
4786                            asection **sec,
4787                            bfd_vma *value ATTRIBUTE_UNUSED)
4788 {
4789   if ((ibfd->flags & DYNAMIC) == 0
4790       && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4791     elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4792
4793   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4794     {
4795       if ((ibfd->flags & DYNAMIC) == 0)
4796         elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4797     }
4798   else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4799     ;
4800   else if (*sec != NULL
4801            && strcmp ((*sec)->name, ".opd") == 0)
4802     isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4803
4804   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4805     {
4806       if (abiversion (ibfd) == 0)
4807         set_abiversion (ibfd, 2);
4808       else if (abiversion (ibfd) == 1)
4809         {
4810           info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4811                                     " for ABI version 1\n"), name);
4812           bfd_set_error (bfd_error_bad_value);
4813           return FALSE;
4814         }
4815     }
4816
4817   return TRUE;
4818 }
4819
4820 /* Merge non-visibility st_other attributes: local entry point.  */
4821
4822 static void
4823 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4824                                   const Elf_Internal_Sym *isym,
4825                                   bfd_boolean definition,
4826                                   bfd_boolean dynamic)
4827 {
4828   if (definition && !dynamic)
4829     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4830                 | ELF_ST_VISIBILITY (h->other));
4831 }
4832
4833 /* This function makes an old ABI object reference to ".bar" cause the
4834    inclusion of a new ABI object archive that defines "bar".
4835    NAME is a symbol defined in an archive.  Return a symbol in the hash
4836    table that might be satisfied by the archive symbols.  */
4837
4838 static struct elf_link_hash_entry *
4839 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4840                                  struct bfd_link_info *info,
4841                                  const char *name)
4842 {
4843   struct elf_link_hash_entry *h;
4844   char *dot_name;
4845   size_t len;
4846
4847   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4848   if (h != NULL
4849       /* Don't return this sym if it is a fake function descriptor
4850          created by add_symbol_adjust.  */
4851       && !(h->root.type == bfd_link_hash_undefweak
4852            && ((struct ppc_link_hash_entry *) h)->fake))
4853     return h;
4854
4855   if (name[0] == '.')
4856     return h;
4857
4858   len = strlen (name);
4859   dot_name = bfd_alloc (abfd, len + 2);
4860   if (dot_name == NULL)
4861     return (struct elf_link_hash_entry *) 0 - 1;
4862   dot_name[0] = '.';
4863   memcpy (dot_name + 1, name, len + 1);
4864   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4865   bfd_release (abfd, dot_name);
4866   return h;
4867 }
4868
4869 /* This function satisfies all old ABI object references to ".bar" if a
4870    new ABI object defines "bar".  Well, at least, undefined dot symbols
4871    are made weak.  This stops later archive searches from including an
4872    object if we already have a function descriptor definition.  It also
4873    prevents the linker complaining about undefined symbols.
4874    We also check and correct mismatched symbol visibility here.  The
4875    most restrictive visibility of the function descriptor and the
4876    function entry symbol is used.  */
4877
4878 static bfd_boolean
4879 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4880 {
4881   struct ppc_link_hash_table *htab;
4882   struct ppc_link_hash_entry *fdh;
4883
4884   if (eh->elf.root.type == bfd_link_hash_indirect)
4885     return TRUE;
4886
4887   if (eh->elf.root.type == bfd_link_hash_warning)
4888     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4889
4890   if (eh->elf.root.root.string[0] != '.')
4891     abort ();
4892
4893   htab = ppc_hash_table (info);
4894   if (htab == NULL)
4895     return FALSE;
4896
4897   fdh = lookup_fdh (eh, htab);
4898   if (fdh == NULL)
4899     {
4900       if (!info->relocatable
4901           && (eh->elf.root.type == bfd_link_hash_undefined
4902               || eh->elf.root.type == bfd_link_hash_undefweak)
4903           && eh->elf.ref_regular)
4904         {
4905           /* Make an undefweak function descriptor sym, which is enough to
4906              pull in an --as-needed shared lib, but won't cause link
4907              errors.  Archives are handled elsewhere.  */
4908           fdh = make_fdh (info, eh);
4909           if (fdh == NULL)
4910             return FALSE;
4911           fdh->elf.ref_regular = 1;
4912         }
4913     }
4914   else
4915     {
4916       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4917       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4918       if (entry_vis < descr_vis)
4919         fdh->elf.other += entry_vis - descr_vis;
4920       else if (entry_vis > descr_vis)
4921         eh->elf.other += descr_vis - entry_vis;
4922
4923       if ((fdh->elf.root.type == bfd_link_hash_defined
4924            || fdh->elf.root.type == bfd_link_hash_defweak)
4925           && eh->elf.root.type == bfd_link_hash_undefined)
4926         {
4927           eh->elf.root.type = bfd_link_hash_undefweak;
4928           eh->was_undefined = 1;
4929           htab->twiddled_syms = 1;
4930         }
4931     }
4932
4933   return TRUE;
4934 }
4935
4936 /* Process list of dot-symbols we made in link_hash_newfunc.  */
4937
4938 static bfd_boolean
4939 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
4940 {
4941   struct ppc_link_hash_table *htab;
4942   struct ppc_link_hash_entry **p, *eh;
4943
4944   if (!is_ppc64_elf (info->output_bfd))
4945     return TRUE;
4946   htab = ppc_hash_table (info);
4947   if (htab == NULL)
4948     return FALSE;
4949
4950   if (is_ppc64_elf (ibfd))
4951     {
4952       p = &htab->dot_syms;
4953       while ((eh = *p) != NULL)
4954         {
4955           *p = NULL;
4956           if (&eh->elf == htab->elf.hgot)
4957             ;
4958           else if (htab->elf.hgot == NULL
4959                    && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4960             htab->elf.hgot = &eh->elf;
4961           else if (!add_symbol_adjust (eh, info))
4962             return FALSE;
4963           p = &eh->u.next_dot_sym;
4964         }
4965     }
4966
4967   /* Clear the list for non-ppc64 input files.  */
4968   p = &htab->dot_syms;
4969   while ((eh = *p) != NULL)
4970     {
4971       *p = NULL;
4972       p = &eh->u.next_dot_sym;
4973     }
4974
4975   /* We need to fix the undefs list for any syms we have twiddled to
4976      undef_weak.  */
4977   if (htab->twiddled_syms)
4978     {
4979       bfd_link_repair_undef_list (&htab->elf.root);
4980       htab->twiddled_syms = 0;
4981     }
4982   return TRUE;
4983 }
4984
4985 /* Undo hash table changes when an --as-needed input file is determined
4986    not to be needed.  */
4987
4988 static bfd_boolean
4989 ppc64_elf_notice_as_needed (bfd *ibfd,
4990                             struct bfd_link_info *info,
4991                             enum notice_asneeded_action act)
4992 {
4993   if (act == notice_not_needed)
4994     {
4995       struct ppc_link_hash_table *htab = ppc_hash_table (info);
4996
4997       if (htab == NULL)
4998         return FALSE;
4999
5000       htab->dot_syms = NULL;
5001     }
5002   return _bfd_elf_notice_as_needed (ibfd, info, act);
5003 }
5004
5005 /* If --just-symbols against a final linked binary, then assume we need
5006    toc adjusting stubs when calling functions defined there.  */
5007
5008 static void
5009 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5010 {
5011   if ((sec->flags & SEC_CODE) != 0
5012       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5013       && is_ppc64_elf (sec->owner))
5014     {
5015       asection *got = bfd_get_section_by_name (sec->owner, ".got");
5016       if (got != NULL
5017           && got->size >= elf_backend_got_header_size
5018           && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5019         sec->has_toc_reloc = 1;
5020     }
5021   _bfd_elf_link_just_syms (sec, info);
5022 }
5023
5024 static struct plt_entry **
5025 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5026                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5027 {
5028   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5029   struct plt_entry **local_plt;
5030   unsigned char *local_got_tls_masks;
5031
5032   if (local_got_ents == NULL)
5033     {
5034       bfd_size_type size = symtab_hdr->sh_info;
5035
5036       size *= (sizeof (*local_got_ents)
5037                + sizeof (*local_plt)
5038                + sizeof (*local_got_tls_masks));
5039       local_got_ents = bfd_zalloc (abfd, size);
5040       if (local_got_ents == NULL)
5041         return NULL;
5042       elf_local_got_ents (abfd) = local_got_ents;
5043     }
5044
5045   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5046     {
5047       struct got_entry *ent;
5048
5049       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5050         if (ent->addend == r_addend
5051             && ent->owner == abfd
5052             && ent->tls_type == tls_type)
5053           break;
5054       if (ent == NULL)
5055         {
5056           bfd_size_type amt = sizeof (*ent);
5057           ent = bfd_alloc (abfd, amt);
5058           if (ent == NULL)
5059             return FALSE;
5060           ent->next = local_got_ents[r_symndx];
5061           ent->addend = r_addend;
5062           ent->owner = abfd;
5063           ent->tls_type = tls_type;
5064           ent->is_indirect = FALSE;
5065           ent->got.refcount = 0;
5066           local_got_ents[r_symndx] = ent;
5067         }
5068       ent->got.refcount += 1;
5069     }
5070
5071   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5072   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5073   local_got_tls_masks[r_symndx] |= tls_type;
5074
5075   return local_plt + r_symndx;
5076 }
5077
5078 static bfd_boolean
5079 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5080 {
5081   struct plt_entry *ent;
5082
5083   for (ent = *plist; ent != NULL; ent = ent->next)
5084     if (ent->addend == addend)
5085       break;
5086   if (ent == NULL)
5087     {
5088       bfd_size_type amt = sizeof (*ent);
5089       ent = bfd_alloc (abfd, amt);
5090       if (ent == NULL)
5091         return FALSE;
5092       ent->next = *plist;
5093       ent->addend = addend;
5094       ent->plt.refcount = 0;
5095       *plist = ent;
5096     }
5097   ent->plt.refcount += 1;
5098   return TRUE;
5099 }
5100
5101 static bfd_boolean
5102 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5103 {
5104   return (r_type == R_PPC64_REL24
5105           || r_type == R_PPC64_REL14
5106           || r_type == R_PPC64_REL14_BRTAKEN
5107           || r_type == R_PPC64_REL14_BRNTAKEN
5108           || r_type == R_PPC64_ADDR24
5109           || r_type == R_PPC64_ADDR14
5110           || r_type == R_PPC64_ADDR14_BRTAKEN
5111           || r_type == R_PPC64_ADDR14_BRNTAKEN);
5112 }
5113
5114 /* Look through the relocs for a section during the first phase, and
5115    calculate needed space in the global offset table, procedure
5116    linkage table, and dynamic reloc sections.  */
5117
5118 static bfd_boolean
5119 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5120                         asection *sec, const Elf_Internal_Rela *relocs)
5121 {
5122   struct ppc_link_hash_table *htab;
5123   Elf_Internal_Shdr *symtab_hdr;
5124   struct elf_link_hash_entry **sym_hashes;
5125   const Elf_Internal_Rela *rel;
5126   const Elf_Internal_Rela *rel_end;
5127   asection *sreloc;
5128   asection **opd_sym_map;
5129   struct elf_link_hash_entry *tga, *dottga;
5130
5131   if (info->relocatable)
5132     return TRUE;
5133
5134   /* Don't do anything special with non-loaded, non-alloced sections.
5135      In particular, any relocs in such sections should not affect GOT
5136      and PLT reference counting (ie. we don't allow them to create GOT
5137      or PLT entries), there's no possibility or desire to optimize TLS
5138      relocs, and there's not much point in propagating relocs to shared
5139      libs that the dynamic linker won't relocate.  */
5140   if ((sec->flags & SEC_ALLOC) == 0)
5141     return TRUE;
5142
5143   BFD_ASSERT (is_ppc64_elf (abfd));
5144
5145   htab = ppc_hash_table (info);
5146   if (htab == NULL)
5147     return FALSE;
5148
5149   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5150                               FALSE, FALSE, TRUE);
5151   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5152                                  FALSE, FALSE, TRUE);
5153   symtab_hdr = &elf_symtab_hdr (abfd);
5154   sym_hashes = elf_sym_hashes (abfd);
5155   sreloc = NULL;
5156   opd_sym_map = NULL;
5157   if (strcmp (sec->name, ".opd") == 0)
5158     {
5159       /* Garbage collection needs some extra help with .opd sections.
5160          We don't want to necessarily keep everything referenced by
5161          relocs in .opd, as that would keep all functions.  Instead,
5162          if we reference an .opd symbol (a function descriptor), we
5163          want to keep the function code symbol's section.  This is
5164          easy for global symbols, but for local syms we need to keep
5165          information about the associated function section.  */
5166       bfd_size_type amt;
5167
5168       if (abiversion (abfd) == 0)
5169         set_abiversion (abfd, 1);
5170       else if (abiversion (abfd) == 2)
5171         {
5172           info->callbacks->einfo (_("%P: .opd not allowed in ABI version %d\n"),
5173                                   abiversion (abfd));
5174           bfd_set_error (bfd_error_bad_value);
5175           return FALSE;
5176         }
5177       amt = sec->size * sizeof (*opd_sym_map) / 8;
5178       opd_sym_map = bfd_zalloc (abfd, amt);
5179       if (opd_sym_map == NULL)
5180         return FALSE;
5181       ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
5182       BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
5183       ppc64_elf_section_data (sec)->sec_type = sec_opd;
5184     }
5185
5186   rel_end = relocs + sec->reloc_count;
5187   for (rel = relocs; rel < rel_end; rel++)
5188     {
5189       unsigned long r_symndx;
5190       struct elf_link_hash_entry *h;
5191       enum elf_ppc64_reloc_type r_type;
5192       int tls_type;
5193       struct _ppc64_elf_section_data *ppc64_sec;
5194       struct plt_entry **ifunc;
5195
5196       r_symndx = ELF64_R_SYM (rel->r_info);
5197       if (r_symndx < symtab_hdr->sh_info)
5198         h = NULL;
5199       else
5200         {
5201           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5202           h = elf_follow_link (h);
5203
5204           /* PR15323, ref flags aren't set for references in the same
5205              object.  */
5206           h->root.non_ir_ref = 1;
5207
5208           if (h == htab->elf.hgot)
5209             sec->has_toc_reloc = 1;
5210         }
5211
5212       tls_type = 0;
5213       ifunc = NULL;
5214       if (h != NULL)
5215         {
5216           if (h->type == STT_GNU_IFUNC)
5217             {
5218               h->needs_plt = 1;
5219               ifunc = &h->plt.plist;
5220             }
5221         }
5222       else
5223         {
5224           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5225                                                           abfd, r_symndx);
5226           if (isym == NULL)
5227             return FALSE;
5228
5229           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5230             {
5231               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5232                                              rel->r_addend, PLT_IFUNC);
5233               if (ifunc == NULL)
5234                 return FALSE;
5235             }
5236         }
5237       r_type = ELF64_R_TYPE (rel->r_info);
5238       if (is_branch_reloc (r_type))
5239         {
5240           if (h != NULL && (h == tga || h == dottga))
5241             {
5242               if (rel != relocs
5243                   && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5244                       || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5245                 /* We have a new-style __tls_get_addr call with a marker
5246                    reloc.  */
5247                 ;
5248               else
5249                 /* Mark this section as having an old-style call.  */
5250                 sec->has_tls_get_addr_call = 1;
5251             }
5252
5253           /* STT_GNU_IFUNC symbols must have a PLT entry.  */
5254           if (ifunc != NULL
5255               && !update_plt_info (abfd, ifunc, rel->r_addend))
5256             return FALSE;
5257         }
5258
5259       switch (r_type)
5260         {
5261         case R_PPC64_TLSGD:
5262         case R_PPC64_TLSLD:
5263           /* These special tls relocs tie a call to __tls_get_addr with
5264              its parameter symbol.  */
5265           break;
5266
5267         case R_PPC64_GOT_TLSLD16:
5268         case R_PPC64_GOT_TLSLD16_LO:
5269         case R_PPC64_GOT_TLSLD16_HI:
5270         case R_PPC64_GOT_TLSLD16_HA:
5271           tls_type = TLS_TLS | TLS_LD;
5272           goto dogottls;
5273
5274         case R_PPC64_GOT_TLSGD16:
5275         case R_PPC64_GOT_TLSGD16_LO:
5276         case R_PPC64_GOT_TLSGD16_HI:
5277         case R_PPC64_GOT_TLSGD16_HA:
5278           tls_type = TLS_TLS | TLS_GD;
5279           goto dogottls;
5280
5281         case R_PPC64_GOT_TPREL16_DS:
5282         case R_PPC64_GOT_TPREL16_LO_DS:
5283         case R_PPC64_GOT_TPREL16_HI:
5284         case R_PPC64_GOT_TPREL16_HA:
5285           if (!info->executable)
5286             info->flags |= DF_STATIC_TLS;
5287           tls_type = TLS_TLS | TLS_TPREL;
5288           goto dogottls;
5289
5290         case R_PPC64_GOT_DTPREL16_DS:
5291         case R_PPC64_GOT_DTPREL16_LO_DS:
5292         case R_PPC64_GOT_DTPREL16_HI:
5293         case R_PPC64_GOT_DTPREL16_HA:
5294           tls_type = TLS_TLS | TLS_DTPREL;
5295         dogottls:
5296           sec->has_tls_reloc = 1;
5297           /* Fall thru */
5298
5299         case R_PPC64_GOT16:
5300         case R_PPC64_GOT16_DS:
5301         case R_PPC64_GOT16_HA:
5302         case R_PPC64_GOT16_HI:
5303         case R_PPC64_GOT16_LO:
5304         case R_PPC64_GOT16_LO_DS:
5305           /* This symbol requires a global offset table entry.  */
5306           sec->has_toc_reloc = 1;
5307           if (r_type == R_PPC64_GOT_TLSLD16
5308               || r_type == R_PPC64_GOT_TLSGD16
5309               || r_type == R_PPC64_GOT_TPREL16_DS
5310               || r_type == R_PPC64_GOT_DTPREL16_DS
5311               || r_type == R_PPC64_GOT16
5312               || r_type == R_PPC64_GOT16_DS)
5313             {
5314               htab->do_multi_toc = 1;
5315               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5316             }
5317
5318           if (ppc64_elf_tdata (abfd)->got == NULL
5319               && !create_got_section (abfd, info))
5320             return FALSE;
5321
5322           if (h != NULL)
5323             {
5324               struct ppc_link_hash_entry *eh;
5325               struct got_entry *ent;
5326
5327               eh = (struct ppc_link_hash_entry *) h;
5328               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5329                 if (ent->addend == rel->r_addend
5330                     && ent->owner == abfd
5331                     && ent->tls_type == tls_type)
5332                   break;
5333               if (ent == NULL)
5334                 {
5335                   bfd_size_type amt = sizeof (*ent);
5336                   ent = bfd_alloc (abfd, amt);
5337                   if (ent == NULL)
5338                     return FALSE;
5339                   ent->next = eh->elf.got.glist;
5340                   ent->addend = rel->r_addend;
5341                   ent->owner = abfd;
5342                   ent->tls_type = tls_type;
5343                   ent->is_indirect = FALSE;
5344                   ent->got.refcount = 0;
5345                   eh->elf.got.glist = ent;
5346                 }
5347               ent->got.refcount += 1;
5348               eh->tls_mask |= tls_type;
5349             }
5350           else
5351             /* This is a global offset table entry for a local symbol.  */
5352             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5353                                         rel->r_addend, tls_type))
5354               return FALSE;
5355           break;
5356
5357         case R_PPC64_PLT16_HA:
5358         case R_PPC64_PLT16_HI:
5359         case R_PPC64_PLT16_LO:
5360         case R_PPC64_PLT32:
5361         case R_PPC64_PLT64:
5362           /* This symbol requires a procedure linkage table entry.  We
5363              actually build the entry in adjust_dynamic_symbol,
5364              because this might be a case of linking PIC code without
5365              linking in any dynamic objects, in which case we don't
5366              need to generate a procedure linkage table after all.  */
5367           if (h == NULL)
5368             {
5369               /* It does not make sense to have a procedure linkage
5370                  table entry for a local symbol.  */
5371               bfd_set_error (bfd_error_bad_value);
5372               return FALSE;
5373             }
5374           else
5375             {
5376               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5377                 return FALSE;
5378               h->needs_plt = 1;
5379               if (h->root.root.string[0] == '.'
5380                   && h->root.root.string[1] != '\0')
5381                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5382             }
5383           break;
5384
5385           /* The following relocations don't need to propagate the
5386              relocation if linking a shared object since they are
5387              section relative.  */
5388         case R_PPC64_SECTOFF:
5389         case R_PPC64_SECTOFF_LO:
5390         case R_PPC64_SECTOFF_HI:
5391         case R_PPC64_SECTOFF_HA:
5392         case R_PPC64_SECTOFF_DS:
5393         case R_PPC64_SECTOFF_LO_DS:
5394         case R_PPC64_DTPREL16:
5395         case R_PPC64_DTPREL16_LO:
5396         case R_PPC64_DTPREL16_HI:
5397         case R_PPC64_DTPREL16_HA:
5398         case R_PPC64_DTPREL16_DS:
5399         case R_PPC64_DTPREL16_LO_DS:
5400         case R_PPC64_DTPREL16_HIGH:
5401         case R_PPC64_DTPREL16_HIGHA:
5402         case R_PPC64_DTPREL16_HIGHER:
5403         case R_PPC64_DTPREL16_HIGHERA:
5404         case R_PPC64_DTPREL16_HIGHEST:
5405         case R_PPC64_DTPREL16_HIGHESTA:
5406           break;
5407
5408           /* Nor do these.  */
5409         case R_PPC64_REL16:
5410         case R_PPC64_REL16_LO:
5411         case R_PPC64_REL16_HI:
5412         case R_PPC64_REL16_HA:
5413           break;
5414
5415         case R_PPC64_TOC16:
5416         case R_PPC64_TOC16_DS:
5417           htab->do_multi_toc = 1;
5418           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5419         case R_PPC64_TOC16_LO:
5420         case R_PPC64_TOC16_HI:
5421         case R_PPC64_TOC16_HA:
5422         case R_PPC64_TOC16_LO_DS:
5423           sec->has_toc_reloc = 1;
5424           break;
5425
5426           /* This relocation describes the C++ object vtable hierarchy.
5427              Reconstruct it for later use during GC.  */
5428         case R_PPC64_GNU_VTINHERIT:
5429           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5430             return FALSE;
5431           break;
5432
5433           /* This relocation describes which C++ vtable entries are actually
5434              used.  Record for later use during GC.  */
5435         case R_PPC64_GNU_VTENTRY:
5436           BFD_ASSERT (h != NULL);
5437           if (h != NULL
5438               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5439             return FALSE;
5440           break;
5441
5442         case R_PPC64_REL14:
5443         case R_PPC64_REL14_BRTAKEN:
5444         case R_PPC64_REL14_BRNTAKEN:
5445           {
5446             asection *dest = NULL;
5447
5448             /* Heuristic: If jumping outside our section, chances are
5449                we are going to need a stub.  */
5450             if (h != NULL)
5451               {
5452                 /* If the sym is weak it may be overridden later, so
5453                    don't assume we know where a weak sym lives.  */
5454                 if (h->root.type == bfd_link_hash_defined)
5455                   dest = h->root.u.def.section;
5456               }
5457             else
5458               {
5459                 Elf_Internal_Sym *isym;
5460
5461                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5462                                               abfd, r_symndx);
5463                 if (isym == NULL)
5464                   return FALSE;
5465
5466                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5467               }
5468
5469             if (dest != sec)
5470               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5471           }
5472           /* Fall through.  */
5473
5474         case R_PPC64_REL24:
5475           if (h != NULL && ifunc == NULL)
5476             {
5477               /* We may need a .plt entry if the function this reloc
5478                  refers to is in a shared lib.  */
5479               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5480                 return FALSE;
5481               h->needs_plt = 1;
5482               if (h->root.root.string[0] == '.'
5483                   && h->root.root.string[1] != '\0')
5484                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5485               if (h == tga || h == dottga)
5486                 sec->has_tls_reloc = 1;
5487             }
5488           break;
5489
5490         case R_PPC64_TPREL64:
5491           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5492           if (!info->executable)
5493             info->flags |= DF_STATIC_TLS;
5494           goto dotlstoc;
5495
5496         case R_PPC64_DTPMOD64:
5497           if (rel + 1 < rel_end
5498               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5499               && rel[1].r_offset == rel->r_offset + 8)
5500             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5501           else
5502             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5503           goto dotlstoc;
5504
5505         case R_PPC64_DTPREL64:
5506           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5507           if (rel != relocs
5508               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5509               && rel[-1].r_offset == rel->r_offset - 8)
5510             /* This is the second reloc of a dtpmod, dtprel pair.
5511                Don't mark with TLS_DTPREL.  */
5512             goto dodyn;
5513
5514         dotlstoc:
5515           sec->has_tls_reloc = 1;
5516           if (h != NULL)
5517             {
5518               struct ppc_link_hash_entry *eh;
5519               eh = (struct ppc_link_hash_entry *) h;
5520               eh->tls_mask |= tls_type;
5521             }
5522           else
5523             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5524                                         rel->r_addend, tls_type))
5525               return FALSE;
5526
5527           ppc64_sec = ppc64_elf_section_data (sec);
5528           if (ppc64_sec->sec_type != sec_toc)
5529             {
5530               bfd_size_type amt;
5531
5532               /* One extra to simplify get_tls_mask.  */
5533               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5534               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5535               if (ppc64_sec->u.toc.symndx == NULL)
5536                 return FALSE;
5537               amt = sec->size * sizeof (bfd_vma) / 8;
5538               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5539               if (ppc64_sec->u.toc.add == NULL)
5540                 return FALSE;
5541               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5542               ppc64_sec->sec_type = sec_toc;
5543             }
5544           BFD_ASSERT (rel->r_offset % 8 == 0);
5545           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5546           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5547
5548           /* Mark the second slot of a GD or LD entry.
5549              -1 to indicate GD and -2 to indicate LD.  */
5550           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5551             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5552           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5553             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5554           goto dodyn;
5555
5556         case R_PPC64_TPREL16:
5557         case R_PPC64_TPREL16_LO:
5558         case R_PPC64_TPREL16_HI:
5559         case R_PPC64_TPREL16_HA:
5560         case R_PPC64_TPREL16_DS:
5561         case R_PPC64_TPREL16_LO_DS:
5562         case R_PPC64_TPREL16_HIGH:
5563         case R_PPC64_TPREL16_HIGHA:
5564         case R_PPC64_TPREL16_HIGHER:
5565         case R_PPC64_TPREL16_HIGHERA:
5566         case R_PPC64_TPREL16_HIGHEST:
5567         case R_PPC64_TPREL16_HIGHESTA:
5568           if (info->shared)
5569             {
5570               if (!info->executable)
5571                 info->flags |= DF_STATIC_TLS;
5572               goto dodyn;
5573             }
5574           break;
5575
5576         case R_PPC64_ADDR64:
5577           if (opd_sym_map != NULL
5578               && rel + 1 < rel_end
5579               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5580             {
5581               if (h != NULL)
5582                 {
5583                   if (h->root.root.string[0] == '.'
5584                       && h->root.root.string[1] != 0
5585                       && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5586                     ;
5587                   else
5588                     ((struct ppc_link_hash_entry *) h)->is_func = 1;
5589                 }
5590               else
5591                 {
5592                   asection *s;
5593                   Elf_Internal_Sym *isym;
5594
5595                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5596                                                 abfd, r_symndx);
5597                   if (isym == NULL)
5598                     return FALSE;
5599
5600                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5601                   if (s != NULL && s != sec)
5602                     opd_sym_map[rel->r_offset / 8] = s;
5603                 }
5604             }
5605           /* Fall through.  */
5606
5607         case R_PPC64_REL30:
5608         case R_PPC64_REL32:
5609         case R_PPC64_REL64:
5610         case R_PPC64_ADDR14:
5611         case R_PPC64_ADDR14_BRNTAKEN:
5612         case R_PPC64_ADDR14_BRTAKEN:
5613         case R_PPC64_ADDR16:
5614         case R_PPC64_ADDR16_DS:
5615         case R_PPC64_ADDR16_HA:
5616         case R_PPC64_ADDR16_HI:
5617         case R_PPC64_ADDR16_HIGH:
5618         case R_PPC64_ADDR16_HIGHA:
5619         case R_PPC64_ADDR16_HIGHER:
5620         case R_PPC64_ADDR16_HIGHERA:
5621         case R_PPC64_ADDR16_HIGHEST:
5622         case R_PPC64_ADDR16_HIGHESTA:
5623         case R_PPC64_ADDR16_LO:
5624         case R_PPC64_ADDR16_LO_DS:
5625         case R_PPC64_ADDR24:
5626         case R_PPC64_ADDR32:
5627         case R_PPC64_UADDR16:
5628         case R_PPC64_UADDR32:
5629         case R_PPC64_UADDR64:
5630         case R_PPC64_TOC:
5631           if (h != NULL && !info->shared)
5632             /* We may need a copy reloc.  */
5633             h->non_got_ref = 1;
5634
5635           /* Don't propagate .opd relocs.  */
5636           if (NO_OPD_RELOCS && opd_sym_map != NULL)
5637             break;
5638
5639           /* If we are creating a shared library, and this is a reloc
5640              against a global symbol, or a non PC relative reloc
5641              against a local symbol, then we need to copy the reloc
5642              into the shared library.  However, if we are linking with
5643              -Bsymbolic, we do not need to copy a reloc against a
5644              global symbol which is defined in an object we are
5645              including in the link (i.e., DEF_REGULAR is set).  At
5646              this point we have not seen all the input files, so it is
5647              possible that DEF_REGULAR is not set now but will be set
5648              later (it is never cleared).  In case of a weak definition,
5649              DEF_REGULAR may be cleared later by a strong definition in
5650              a shared library.  We account for that possibility below by
5651              storing information in the dyn_relocs field of the hash
5652              table entry.  A similar situation occurs when creating
5653              shared libraries and symbol visibility changes render the
5654              symbol local.
5655
5656              If on the other hand, we are creating an executable, we
5657              may need to keep relocations for symbols satisfied by a
5658              dynamic library if we manage to avoid copy relocs for the
5659              symbol.  */
5660         dodyn:
5661           if ((info->shared
5662                && (must_be_dyn_reloc (info, r_type)
5663                    || (h != NULL
5664                        && (!SYMBOLIC_BIND (info, h)
5665                            || h->root.type == bfd_link_hash_defweak
5666                            || !h->def_regular))))
5667               || (ELIMINATE_COPY_RELOCS
5668                   && !info->shared
5669                   && h != NULL
5670                   && (h->root.type == bfd_link_hash_defweak
5671                       || !h->def_regular))
5672               || (!info->shared
5673                   && ifunc != NULL))
5674             {
5675               /* We must copy these reloc types into the output file.
5676                  Create a reloc section in dynobj and make room for
5677                  this reloc.  */
5678               if (sreloc == NULL)
5679                 {
5680                   sreloc = _bfd_elf_make_dynamic_reloc_section
5681                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5682
5683                   if (sreloc == NULL)
5684                     return FALSE;
5685                 }
5686
5687               /* If this is a global symbol, we count the number of
5688                  relocations we need for this symbol.  */
5689               if (h != NULL)
5690                 {
5691                   struct elf_dyn_relocs *p;
5692                   struct elf_dyn_relocs **head;
5693
5694                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5695                   p = *head;
5696                   if (p == NULL || p->sec != sec)
5697                     {
5698                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5699                       if (p == NULL)
5700                         return FALSE;
5701                       p->next = *head;
5702                       *head = p;
5703                       p->sec = sec;
5704                       p->count = 0;
5705                       p->pc_count = 0;
5706                     }
5707                   p->count += 1;
5708                   if (!must_be_dyn_reloc (info, r_type))
5709                     p->pc_count += 1;
5710                 }
5711               else
5712                 {
5713                   /* Track dynamic relocs needed for local syms too.
5714                      We really need local syms available to do this
5715                      easily.  Oh well.  */
5716                   struct ppc_dyn_relocs *p;
5717                   struct ppc_dyn_relocs **head;
5718                   bfd_boolean is_ifunc;
5719                   asection *s;
5720                   void *vpp;
5721                   Elf_Internal_Sym *isym;
5722
5723                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5724                                                 abfd, r_symndx);
5725                   if (isym == NULL)
5726                     return FALSE;
5727
5728                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5729                   if (s == NULL)
5730                     s = sec;
5731
5732                   vpp = &elf_section_data (s)->local_dynrel;
5733                   head = (struct ppc_dyn_relocs **) vpp;
5734                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5735                   p = *head;
5736                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5737                     p = p->next;
5738                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5739                     {
5740                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5741                       if (p == NULL)
5742                         return FALSE;
5743                       p->next = *head;
5744                       *head = p;
5745                       p->sec = sec;
5746                       p->ifunc = is_ifunc;
5747                       p->count = 0;
5748                     }
5749                   p->count += 1;
5750                 }
5751             }
5752           break;
5753
5754         default:
5755           break;
5756         }
5757     }
5758
5759   return TRUE;
5760 }
5761
5762 /* Merge backend specific data from an object file to the output
5763    object file when linking.  */
5764
5765 static bfd_boolean
5766 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5767 {
5768   unsigned long iflags, oflags;
5769
5770   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5771     return TRUE;
5772
5773   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5774     return TRUE;
5775
5776   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5777     return FALSE;
5778
5779   iflags = elf_elfheader (ibfd)->e_flags;
5780   oflags = elf_elfheader (obfd)->e_flags;
5781
5782   if (!elf_flags_init (obfd) || oflags == 0)
5783     {
5784       elf_flags_init (obfd) = TRUE;
5785       elf_elfheader (obfd)->e_flags = iflags;
5786     }
5787   else if (iflags == oflags || iflags == 0)
5788     ;
5789   else if (iflags & ~EF_PPC64_ABI)
5790     {
5791       (*_bfd_error_handler)
5792         (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
5793       bfd_set_error (bfd_error_bad_value);
5794       return FALSE;
5795     }
5796   else
5797     {
5798       (*_bfd_error_handler)
5799         (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
5800          ibfd, iflags, oflags);
5801       bfd_set_error (bfd_error_bad_value);
5802       return FALSE;
5803     }
5804
5805   /* Merge Tag_compatibility attributes and any common GNU ones.  */
5806   _bfd_elf_merge_object_attributes (ibfd, obfd);
5807
5808   return TRUE;
5809 }
5810
5811 static bfd_boolean
5812 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5813 {
5814   /* Print normal ELF private data.  */
5815   _bfd_elf_print_private_bfd_data (abfd, ptr);
5816
5817   if (elf_elfheader (abfd)->e_flags != 0)
5818     {
5819       FILE *file = ptr;
5820
5821       /* xgettext:c-format */
5822       fprintf (file, _("private flags = 0x%lx:"),
5823                elf_elfheader (abfd)->e_flags);
5824
5825       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5826         fprintf (file, _(" [abiv%ld]"),
5827                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5828       fputc ('\n', file);
5829     }
5830
5831   return TRUE;
5832 }
5833
5834 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
5835    of the code entry point, and its section.  */
5836
5837 static bfd_vma
5838 opd_entry_value (asection *opd_sec,
5839                  bfd_vma offset,
5840                  asection **code_sec,
5841                  bfd_vma *code_off,
5842                  bfd_boolean in_code_sec)
5843 {
5844   bfd *opd_bfd = opd_sec->owner;
5845   Elf_Internal_Rela *relocs;
5846   Elf_Internal_Rela *lo, *hi, *look;
5847   bfd_vma val;
5848
5849   /* No relocs implies we are linking a --just-symbols object, or looking
5850      at a final linked executable with addr2line or somesuch.  */
5851   if (opd_sec->reloc_count == 0)
5852     {
5853       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5854
5855       if (contents == NULL)
5856         {
5857           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5858             return (bfd_vma) -1;
5859           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5860         }
5861
5862       val = bfd_get_64 (opd_bfd, contents + offset);
5863       if (code_sec != NULL)
5864         {
5865           asection *sec, *likely = NULL;
5866
5867           if (in_code_sec)
5868             {
5869               sec = *code_sec;
5870               if (sec->vma <= val
5871                   && val < sec->vma + sec->size)
5872                 likely = sec;
5873               else
5874                 val = -1;
5875             }
5876           else
5877             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5878               if (sec->vma <= val
5879                   && (sec->flags & SEC_LOAD) != 0
5880                   && (sec->flags & SEC_ALLOC) != 0)
5881                 likely = sec;
5882           if (likely != NULL)
5883             {
5884               *code_sec = likely;
5885               if (code_off != NULL)
5886                 *code_off = val - likely->vma;
5887             }
5888         }
5889       return val;
5890     }
5891
5892   BFD_ASSERT (is_ppc64_elf (opd_bfd));
5893
5894   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5895   if (relocs == NULL)
5896     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5897
5898   /* Go find the opd reloc at the sym address.  */
5899   lo = relocs;
5900   BFD_ASSERT (lo != NULL);
5901   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5902   val = (bfd_vma) -1;
5903   while (lo < hi)
5904     {
5905       look = lo + (hi - lo) / 2;
5906       if (look->r_offset < offset)
5907         lo = look + 1;
5908       else if (look->r_offset > offset)
5909         hi = look;
5910       else
5911         {
5912           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5913
5914           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5915               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5916             {
5917               unsigned long symndx = ELF64_R_SYM (look->r_info);
5918               asection *sec;
5919
5920               if (symndx < symtab_hdr->sh_info
5921                   || elf_sym_hashes (opd_bfd) == NULL)
5922                 {
5923                   Elf_Internal_Sym *sym;
5924
5925                   sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5926                   if (sym == NULL)
5927                     {
5928                       size_t symcnt = symtab_hdr->sh_info;
5929                       if (elf_sym_hashes (opd_bfd) == NULL)
5930                         symcnt = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
5931                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, symcnt,
5932                                                   0, NULL, NULL, NULL);
5933                       if (sym == NULL)
5934                         break;
5935                       symtab_hdr->contents = (bfd_byte *) sym;
5936                     }
5937
5938                   sym += symndx;
5939                   val = sym->st_value;
5940                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5941                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5942                 }
5943               else
5944                 {
5945                   struct elf_link_hash_entry **sym_hashes;
5946                   struct elf_link_hash_entry *rh;
5947
5948                   sym_hashes = elf_sym_hashes (opd_bfd);
5949                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
5950                   if (rh != NULL)
5951                     {
5952                       rh = elf_follow_link (rh);
5953                       BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5954                                   || rh->root.type == bfd_link_hash_defweak);
5955                       val = rh->root.u.def.value;
5956                       sec = rh->root.u.def.section;
5957                     }
5958                   else
5959                     {
5960                       /* Handle the odd case where we can be called
5961                          during bfd_elf_link_add_symbols before the
5962                          symbol hashes have been fully populated.  */
5963                       Elf_Internal_Sym *sym;
5964
5965                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, 1,
5966                                                   symndx, NULL, NULL, NULL);
5967                       if (sym == NULL)
5968                         break;
5969
5970                       val = sym->st_value;
5971                       sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5972                       free (sym);
5973                     }
5974                 }
5975               val += look->r_addend;
5976               if (code_off != NULL)
5977                 *code_off = val;
5978               if (code_sec != NULL)
5979                 {
5980                   if (in_code_sec && *code_sec != sec)
5981                     return -1;
5982                   else
5983                     *code_sec = sec;
5984                 }
5985               if (sec != NULL && sec->output_section != NULL)
5986                 val += sec->output_section->vma + sec->output_offset;
5987             }
5988           break;
5989         }
5990     }
5991
5992   return val;
5993 }
5994
5995 /* If the ELF symbol SYM might be a function in SEC, return the
5996    function size and set *CODE_OFF to the function's entry point,
5997    otherwise return zero.  */
5998
5999 static bfd_size_type
6000 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6001                               bfd_vma *code_off)
6002 {
6003   bfd_size_type size;
6004
6005   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6006                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6007     return 0;
6008
6009   size = 0;
6010   if (!(sym->flags & BSF_SYNTHETIC))
6011     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6012
6013   if (strcmp (sym->section->name, ".opd") == 0)
6014     {
6015       if (opd_entry_value (sym->section, sym->value,
6016                            &sec, code_off, TRUE) == (bfd_vma) -1)
6017         return 0;
6018       /* An old ABI binary with dot-syms has a size of 24 on the .opd
6019          symbol.  This size has nothing to do with the code size of the
6020          function, which is what we're supposed to return, but the
6021          code size isn't available without looking up the dot-sym.
6022          However, doing that would be a waste of time particularly
6023          since elf_find_function will look at the dot-sym anyway.
6024          Now, elf_find_function will keep the largest size of any
6025          function sym found at the code address of interest, so return
6026          1 here to avoid it incorrectly caching a larger function size
6027          for a small function.  This does mean we return the wrong
6028          size for a new-ABI function of size 24, but all that does is
6029          disable caching for such functions.  */
6030       if (size == 24)
6031         size = 1;
6032     }
6033   else
6034     {
6035       if (sym->section != sec)
6036         return 0;
6037       *code_off = sym->value;
6038     }
6039   if (size == 0)
6040     size = 1;
6041   return size;
6042 }
6043
6044 /* Return true if symbol is defined in a regular object file.  */
6045
6046 static bfd_boolean
6047 is_static_defined (struct elf_link_hash_entry *h)
6048 {
6049   return ((h->root.type == bfd_link_hash_defined
6050            || h->root.type == bfd_link_hash_defweak)
6051           && h->root.u.def.section != NULL
6052           && h->root.u.def.section->output_section != NULL);
6053 }
6054
6055 /* If FDH is a function descriptor symbol, return the associated code
6056    entry symbol if it is defined.  Return NULL otherwise.  */
6057
6058 static struct ppc_link_hash_entry *
6059 defined_code_entry (struct ppc_link_hash_entry *fdh)
6060 {
6061   if (fdh->is_func_descriptor)
6062     {
6063       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6064       if (fh->elf.root.type == bfd_link_hash_defined
6065           || fh->elf.root.type == bfd_link_hash_defweak)
6066         return fh;
6067     }
6068   return NULL;
6069 }
6070
6071 /* If FH is a function code entry symbol, return the associated
6072    function descriptor symbol if it is defined.  Return NULL otherwise.  */
6073
6074 static struct ppc_link_hash_entry *
6075 defined_func_desc (struct ppc_link_hash_entry *fh)
6076 {
6077   if (fh->oh != NULL
6078       && fh->oh->is_func_descriptor)
6079     {
6080       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6081       if (fdh->elf.root.type == bfd_link_hash_defined
6082           || fdh->elf.root.type == bfd_link_hash_defweak)
6083         return fdh;
6084     }
6085   return NULL;
6086 }
6087
6088 /* Mark all our entry sym sections, both opd and code section.  */
6089
6090 static void
6091 ppc64_elf_gc_keep (struct bfd_link_info *info)
6092 {
6093   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6094   struct bfd_sym_chain *sym;
6095
6096   if (htab == NULL)
6097     return;
6098
6099   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6100     {
6101       struct ppc_link_hash_entry *eh, *fh;
6102       asection *sec;
6103
6104       eh = (struct ppc_link_hash_entry *)
6105         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6106       if (eh == NULL)
6107         continue;
6108       if (eh->elf.root.type != bfd_link_hash_defined
6109           && eh->elf.root.type != bfd_link_hash_defweak)
6110         continue;
6111
6112       fh = defined_code_entry (eh);
6113       if (fh != NULL)
6114         {
6115           sec = fh->elf.root.u.def.section;
6116           sec->flags |= SEC_KEEP;
6117         }
6118       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6119                && opd_entry_value (eh->elf.root.u.def.section,
6120                                    eh->elf.root.u.def.value,
6121                                    &sec, NULL, FALSE) != (bfd_vma) -1)
6122         sec->flags |= SEC_KEEP;
6123
6124       sec = eh->elf.root.u.def.section;
6125       sec->flags |= SEC_KEEP;
6126     }
6127 }
6128
6129 /* Mark sections containing dynamically referenced symbols.  When
6130    building shared libraries, we must assume that any visible symbol is
6131    referenced.  */
6132
6133 static bfd_boolean
6134 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6135 {
6136   struct bfd_link_info *info = (struct bfd_link_info *) inf;
6137   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6138   struct ppc_link_hash_entry *fdh;
6139
6140   /* Dynamic linking info is on the func descriptor sym.  */
6141   fdh = defined_func_desc (eh);
6142   if (fdh != NULL)
6143     eh = fdh;
6144
6145   if ((eh->elf.root.type == bfd_link_hash_defined
6146        || eh->elf.root.type == bfd_link_hash_defweak)
6147       && (eh->elf.ref_dynamic
6148           || (!info->executable
6149               && eh->elf.def_regular
6150               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6151               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6152               && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6153                   || !bfd_hide_sym_by_version (info->version_info,
6154                                                eh->elf.root.root.string)))))
6155     {
6156       asection *code_sec;
6157       struct ppc_link_hash_entry *fh;
6158
6159       eh->elf.root.u.def.section->flags |= SEC_KEEP;
6160
6161       /* Function descriptor syms cause the associated
6162          function code sym section to be marked.  */
6163       fh = defined_code_entry (eh);
6164       if (fh != NULL)
6165         {
6166           code_sec = fh->elf.root.u.def.section;
6167           code_sec->flags |= SEC_KEEP;
6168         }
6169       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6170                && opd_entry_value (eh->elf.root.u.def.section,
6171                                    eh->elf.root.u.def.value,
6172                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
6173         code_sec->flags |= SEC_KEEP;
6174     }
6175
6176   return TRUE;
6177 }
6178
6179 /* Return the section that should be marked against GC for a given
6180    relocation.  */
6181
6182 static asection *
6183 ppc64_elf_gc_mark_hook (asection *sec,
6184                         struct bfd_link_info *info,
6185                         Elf_Internal_Rela *rel,
6186                         struct elf_link_hash_entry *h,
6187                         Elf_Internal_Sym *sym)
6188 {
6189   asection *rsec;
6190
6191   /* Syms return NULL if we're marking .opd, so we avoid marking all
6192      function sections, as all functions are referenced in .opd.  */
6193   rsec = NULL;
6194   if (get_opd_info (sec) != NULL)
6195     return rsec;
6196
6197   if (h != NULL)
6198     {
6199       enum elf_ppc64_reloc_type r_type;
6200       struct ppc_link_hash_entry *eh, *fh, *fdh;
6201
6202       r_type = ELF64_R_TYPE (rel->r_info);
6203       switch (r_type)
6204         {
6205         case R_PPC64_GNU_VTINHERIT:
6206         case R_PPC64_GNU_VTENTRY:
6207           break;
6208
6209         default:
6210           switch (h->root.type)
6211             {
6212             case bfd_link_hash_defined:
6213             case bfd_link_hash_defweak:
6214               eh = (struct ppc_link_hash_entry *) h;
6215               fdh = defined_func_desc (eh);
6216               if (fdh != NULL)
6217                 eh = fdh;
6218
6219               /* Function descriptor syms cause the associated
6220                  function code sym section to be marked.  */
6221               fh = defined_code_entry (eh);
6222               if (fh != NULL)
6223                 {
6224                   /* They also mark their opd section.  */
6225                   eh->elf.root.u.def.section->gc_mark = 1;
6226
6227                   rsec = fh->elf.root.u.def.section;
6228                 }
6229               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6230                        && opd_entry_value (eh->elf.root.u.def.section,
6231                                            eh->elf.root.u.def.value,
6232                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
6233                 eh->elf.root.u.def.section->gc_mark = 1;
6234               else
6235                 rsec = h->root.u.def.section;
6236               break;
6237
6238             case bfd_link_hash_common:
6239               rsec = h->root.u.c.p->section;
6240               break;
6241
6242             default:
6243               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6244             }
6245         }
6246     }
6247   else
6248     {
6249       struct _opd_sec_data *opd;
6250
6251       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6252       opd = get_opd_info (rsec);
6253       if (opd != NULL && opd->func_sec != NULL)
6254         {
6255           rsec->gc_mark = 1;
6256
6257           rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
6258         }
6259     }
6260
6261   return rsec;
6262 }
6263
6264 /* Update the .got, .plt. and dynamic reloc reference counts for the
6265    section being removed.  */
6266
6267 static bfd_boolean
6268 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6269                          asection *sec, const Elf_Internal_Rela *relocs)
6270 {
6271   struct ppc_link_hash_table *htab;
6272   Elf_Internal_Shdr *symtab_hdr;
6273   struct elf_link_hash_entry **sym_hashes;
6274   struct got_entry **local_got_ents;
6275   const Elf_Internal_Rela *rel, *relend;
6276
6277   if (info->relocatable)
6278     return TRUE;
6279
6280   if ((sec->flags & SEC_ALLOC) == 0)
6281     return TRUE;
6282
6283   elf_section_data (sec)->local_dynrel = NULL;
6284
6285   htab = ppc_hash_table (info);
6286   if (htab == NULL)
6287     return FALSE;
6288
6289   symtab_hdr = &elf_symtab_hdr (abfd);
6290   sym_hashes = elf_sym_hashes (abfd);
6291   local_got_ents = elf_local_got_ents (abfd);
6292
6293   relend = relocs + sec->reloc_count;
6294   for (rel = relocs; rel < relend; rel++)
6295     {
6296       unsigned long r_symndx;
6297       enum elf_ppc64_reloc_type r_type;
6298       struct elf_link_hash_entry *h = NULL;
6299       unsigned char tls_type = 0;
6300
6301       r_symndx = ELF64_R_SYM (rel->r_info);
6302       r_type = ELF64_R_TYPE (rel->r_info);
6303       if (r_symndx >= symtab_hdr->sh_info)
6304         {
6305           struct ppc_link_hash_entry *eh;
6306           struct elf_dyn_relocs **pp;
6307           struct elf_dyn_relocs *p;
6308
6309           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6310           h = elf_follow_link (h);
6311           eh = (struct ppc_link_hash_entry *) h;
6312
6313           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6314             if (p->sec == sec)
6315               {
6316                 /* Everything must go for SEC.  */
6317                 *pp = p->next;
6318                 break;
6319               }
6320         }
6321
6322       if (is_branch_reloc (r_type))
6323         {
6324           struct plt_entry **ifunc = NULL;
6325           if (h != NULL)
6326             {
6327               if (h->type == STT_GNU_IFUNC)
6328                 ifunc = &h->plt.plist;
6329             }
6330           else if (local_got_ents != NULL)
6331             {
6332               struct plt_entry **local_plt = (struct plt_entry **)
6333                 (local_got_ents + symtab_hdr->sh_info);
6334               unsigned char *local_got_tls_masks = (unsigned char *)
6335                 (local_plt + symtab_hdr->sh_info);
6336               if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6337                 ifunc = local_plt + r_symndx;
6338             }
6339           if (ifunc != NULL)
6340             {
6341               struct plt_entry *ent;
6342
6343               for (ent = *ifunc; ent != NULL; ent = ent->next)
6344                 if (ent->addend == rel->r_addend)
6345                   break;
6346               if (ent == NULL)
6347                 abort ();
6348               if (ent->plt.refcount > 0)
6349                 ent->plt.refcount -= 1;
6350               continue;
6351             }
6352         }
6353
6354       switch (r_type)
6355         {
6356         case R_PPC64_GOT_TLSLD16:
6357         case R_PPC64_GOT_TLSLD16_LO:
6358         case R_PPC64_GOT_TLSLD16_HI:
6359         case R_PPC64_GOT_TLSLD16_HA:
6360           tls_type = TLS_TLS | TLS_LD;
6361           goto dogot;
6362
6363         case R_PPC64_GOT_TLSGD16:
6364         case R_PPC64_GOT_TLSGD16_LO:
6365         case R_PPC64_GOT_TLSGD16_HI:
6366         case R_PPC64_GOT_TLSGD16_HA:
6367           tls_type = TLS_TLS | TLS_GD;
6368           goto dogot;
6369
6370         case R_PPC64_GOT_TPREL16_DS:
6371         case R_PPC64_GOT_TPREL16_LO_DS:
6372         case R_PPC64_GOT_TPREL16_HI:
6373         case R_PPC64_GOT_TPREL16_HA:
6374           tls_type = TLS_TLS | TLS_TPREL;
6375           goto dogot;
6376
6377         case R_PPC64_GOT_DTPREL16_DS:
6378         case R_PPC64_GOT_DTPREL16_LO_DS:
6379         case R_PPC64_GOT_DTPREL16_HI:
6380         case R_PPC64_GOT_DTPREL16_HA:
6381           tls_type = TLS_TLS | TLS_DTPREL;
6382           goto dogot;
6383
6384         case R_PPC64_GOT16:
6385         case R_PPC64_GOT16_DS:
6386         case R_PPC64_GOT16_HA:
6387         case R_PPC64_GOT16_HI:
6388         case R_PPC64_GOT16_LO:
6389         case R_PPC64_GOT16_LO_DS:
6390         dogot:
6391           {
6392             struct got_entry *ent;
6393
6394             if (h != NULL)
6395               ent = h->got.glist;
6396             else
6397               ent = local_got_ents[r_symndx];
6398
6399             for (; ent != NULL; ent = ent->next)
6400               if (ent->addend == rel->r_addend
6401                   && ent->owner == abfd
6402                   && ent->tls_type == tls_type)
6403                 break;
6404             if (ent == NULL)
6405               abort ();
6406             if (ent->got.refcount > 0)
6407               ent->got.refcount -= 1;
6408           }
6409           break;
6410
6411         case R_PPC64_PLT16_HA:
6412         case R_PPC64_PLT16_HI:
6413         case R_PPC64_PLT16_LO:
6414         case R_PPC64_PLT32:
6415         case R_PPC64_PLT64:
6416         case R_PPC64_REL14:
6417         case R_PPC64_REL14_BRNTAKEN:
6418         case R_PPC64_REL14_BRTAKEN:
6419         case R_PPC64_REL24:
6420           if (h != NULL)
6421             {
6422               struct plt_entry *ent;
6423
6424               for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6425                 if (ent->addend == rel->r_addend)
6426                   break;
6427               if (ent != NULL && ent->plt.refcount > 0)
6428                 ent->plt.refcount -= 1;
6429             }
6430           break;
6431
6432         default:
6433           break;
6434         }
6435     }
6436   return TRUE;
6437 }
6438
6439 /* The maximum size of .sfpr.  */
6440 #define SFPR_MAX (218*4)
6441
6442 struct sfpr_def_parms
6443 {
6444   const char name[12];
6445   unsigned char lo, hi;
6446   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6447   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6448 };
6449
6450 /* Auto-generate _save*, _rest* functions in .sfpr.  */
6451
6452 static bfd_boolean
6453 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6454 {
6455   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6456   unsigned int i;
6457   size_t len = strlen (parm->name);
6458   bfd_boolean writing = FALSE;
6459   char sym[16];
6460
6461   if (htab == NULL)
6462     return FALSE;
6463
6464   memcpy (sym, parm->name, len);
6465   sym[len + 2] = 0;
6466
6467   for (i = parm->lo; i <= parm->hi; i++)
6468     {
6469       struct elf_link_hash_entry *h;
6470
6471       sym[len + 0] = i / 10 + '0';
6472       sym[len + 1] = i % 10 + '0';
6473       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6474       if (h != NULL
6475           && !h->def_regular)
6476         {
6477           h->root.type = bfd_link_hash_defined;
6478           h->root.u.def.section = htab->sfpr;
6479           h->root.u.def.value = htab->sfpr->size;
6480           h->type = STT_FUNC;
6481           h->def_regular = 1;
6482           _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6483           writing = TRUE;
6484           if (htab->sfpr->contents == NULL)
6485             {
6486               htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6487               if (htab->sfpr->contents == NULL)
6488                 return FALSE;
6489             }
6490         }
6491       if (writing)
6492         {
6493           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6494           if (i != parm->hi)
6495             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6496           else
6497             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6498           htab->sfpr->size = p - htab->sfpr->contents;
6499         }
6500     }
6501
6502   return TRUE;
6503 }
6504
6505 static bfd_byte *
6506 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6507 {
6508   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6509   return p + 4;
6510 }
6511
6512 static bfd_byte *
6513 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6514 {
6515   p = savegpr0 (abfd, p, r);
6516   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6517   p = p + 4;
6518   bfd_put_32 (abfd, BLR, p);
6519   return p + 4;
6520 }
6521
6522 static bfd_byte *
6523 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6524 {
6525   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6526   return p + 4;
6527 }
6528
6529 static bfd_byte *
6530 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6531 {
6532   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6533   p = p + 4;
6534   p = restgpr0 (abfd, p, r);
6535   bfd_put_32 (abfd, MTLR_R0, p);
6536   p = p + 4;
6537   if (r == 29)
6538     {
6539       p = restgpr0 (abfd, p, 30);
6540       p = restgpr0 (abfd, p, 31);
6541     }
6542   bfd_put_32 (abfd, BLR, p);
6543   return p + 4;
6544 }
6545
6546 static bfd_byte *
6547 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6548 {
6549   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6550   return p + 4;
6551 }
6552
6553 static bfd_byte *
6554 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6555 {
6556   p = savegpr1 (abfd, p, r);
6557   bfd_put_32 (abfd, BLR, p);
6558   return p + 4;
6559 }
6560
6561 static bfd_byte *
6562 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6563 {
6564   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6565   return p + 4;
6566 }
6567
6568 static bfd_byte *
6569 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6570 {
6571   p = restgpr1 (abfd, p, r);
6572   bfd_put_32 (abfd, BLR, p);
6573   return p + 4;
6574 }
6575
6576 static bfd_byte *
6577 savefpr (bfd *abfd, bfd_byte *p, int r)
6578 {
6579   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6580   return p + 4;
6581 }
6582
6583 static bfd_byte *
6584 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6585 {
6586   p = savefpr (abfd, p, r);
6587   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6588   p = p + 4;
6589   bfd_put_32 (abfd, BLR, p);
6590   return p + 4;
6591 }
6592
6593 static bfd_byte *
6594 restfpr (bfd *abfd, bfd_byte *p, int r)
6595 {
6596   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6597   return p + 4;
6598 }
6599
6600 static bfd_byte *
6601 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6602 {
6603   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6604   p = p + 4;
6605   p = restfpr (abfd, p, r);
6606   bfd_put_32 (abfd, MTLR_R0, p);
6607   p = p + 4;
6608   if (r == 29)
6609     {
6610       p = restfpr (abfd, p, 30);
6611       p = restfpr (abfd, p, 31);
6612     }
6613   bfd_put_32 (abfd, BLR, p);
6614   return p + 4;
6615 }
6616
6617 static bfd_byte *
6618 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6619 {
6620   p = savefpr (abfd, p, r);
6621   bfd_put_32 (abfd, BLR, p);
6622   return p + 4;
6623 }
6624
6625 static bfd_byte *
6626 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6627 {
6628   p = restfpr (abfd, p, r);
6629   bfd_put_32 (abfd, BLR, p);
6630   return p + 4;
6631 }
6632
6633 static bfd_byte *
6634 savevr (bfd *abfd, bfd_byte *p, int r)
6635 {
6636   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6637   p = p + 4;
6638   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6639   return p + 4;
6640 }
6641
6642 static bfd_byte *
6643 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6644 {
6645   p = savevr (abfd, p, r);
6646   bfd_put_32 (abfd, BLR, p);
6647   return p + 4;
6648 }
6649
6650 static bfd_byte *
6651 restvr (bfd *abfd, bfd_byte *p, int r)
6652 {
6653   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6654   p = p + 4;
6655   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6656   return p + 4;
6657 }
6658
6659 static bfd_byte *
6660 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6661 {
6662   p = restvr (abfd, p, r);
6663   bfd_put_32 (abfd, BLR, p);
6664   return p + 4;
6665 }
6666
6667 /* Called via elf_link_hash_traverse to transfer dynamic linking
6668    information on function code symbol entries to their corresponding
6669    function descriptor symbol entries.  */
6670
6671 static bfd_boolean
6672 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6673 {
6674   struct bfd_link_info *info;
6675   struct ppc_link_hash_table *htab;
6676   struct plt_entry *ent;
6677   struct ppc_link_hash_entry *fh;
6678   struct ppc_link_hash_entry *fdh;
6679   bfd_boolean force_local;
6680
6681   fh = (struct ppc_link_hash_entry *) h;
6682   if (fh->elf.root.type == bfd_link_hash_indirect)
6683     return TRUE;
6684
6685   info = inf;
6686   htab = ppc_hash_table (info);
6687   if (htab == NULL)
6688     return FALSE;
6689
6690   /* Resolve undefined references to dot-symbols as the value
6691      in the function descriptor, if we have one in a regular object.
6692      This is to satisfy cases like ".quad .foo".  Calls to functions
6693      in dynamic objects are handled elsewhere.  */
6694   if (fh->elf.root.type == bfd_link_hash_undefweak
6695       && fh->was_undefined
6696       && (fdh = defined_func_desc (fh)) != NULL
6697       && get_opd_info (fdh->elf.root.u.def.section) != NULL
6698       && opd_entry_value (fdh->elf.root.u.def.section,
6699                           fdh->elf.root.u.def.value,
6700                           &fh->elf.root.u.def.section,
6701                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6702     {
6703       fh->elf.root.type = fdh->elf.root.type;
6704       fh->elf.forced_local = 1;
6705       fh->elf.def_regular = fdh->elf.def_regular;
6706       fh->elf.def_dynamic = fdh->elf.def_dynamic;
6707     }
6708
6709   /* If this is a function code symbol, transfer dynamic linking
6710      information to the function descriptor symbol.  */
6711   if (!fh->is_func)
6712     return TRUE;
6713
6714   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6715     if (ent->plt.refcount > 0)
6716       break;
6717   if (ent == NULL
6718       || fh->elf.root.root.string[0] != '.'
6719       || fh->elf.root.root.string[1] == '\0')
6720     return TRUE;
6721
6722   /* Find the corresponding function descriptor symbol.  Create it
6723      as undefined if necessary.  */
6724
6725   fdh = lookup_fdh (fh, htab);
6726   if (fdh == NULL
6727       && !info->executable
6728       && (fh->elf.root.type == bfd_link_hash_undefined
6729           || fh->elf.root.type == bfd_link_hash_undefweak))
6730     {
6731       fdh = make_fdh (info, fh);
6732       if (fdh == NULL)
6733         return FALSE;
6734     }
6735
6736   /* Fake function descriptors are made undefweak.  If the function
6737      code symbol is strong undefined, make the fake sym the same.
6738      If the function code symbol is defined, then force the fake
6739      descriptor local;  We can't support overriding of symbols in a
6740      shared library on a fake descriptor.  */
6741
6742   if (fdh != NULL
6743       && fdh->fake
6744       && fdh->elf.root.type == bfd_link_hash_undefweak)
6745     {
6746       if (fh->elf.root.type == bfd_link_hash_undefined)
6747         {
6748           fdh->elf.root.type = bfd_link_hash_undefined;
6749           bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6750         }
6751       else if (fh->elf.root.type == bfd_link_hash_defined
6752                || fh->elf.root.type == bfd_link_hash_defweak)
6753         {
6754           _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6755         }
6756     }
6757
6758   if (fdh != NULL
6759       && !fdh->elf.forced_local
6760       && (!info->executable
6761           || fdh->elf.def_dynamic
6762           || fdh->elf.ref_dynamic
6763           || (fdh->elf.root.type == bfd_link_hash_undefweak
6764               && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6765     {
6766       if (fdh->elf.dynindx == -1)
6767         if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6768           return FALSE;
6769       fdh->elf.ref_regular |= fh->elf.ref_regular;
6770       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6771       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6772       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6773       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6774         {
6775           move_plt_plist (fh, fdh);
6776           fdh->elf.needs_plt = 1;
6777         }
6778       fdh->is_func_descriptor = 1;
6779       fdh->oh = fh;
6780       fh->oh = fdh;
6781     }
6782
6783   /* Now that the info is on the function descriptor, clear the
6784      function code sym info.  Any function code syms for which we
6785      don't have a definition in a regular file, we force local.
6786      This prevents a shared library from exporting syms that have
6787      been imported from another library.  Function code syms that
6788      are really in the library we must leave global to prevent the
6789      linker dragging in a definition from a static library.  */
6790   force_local = (!fh->elf.def_regular
6791                  || fdh == NULL
6792                  || !fdh->elf.def_regular
6793                  || fdh->elf.forced_local);
6794   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6795
6796   return TRUE;
6797 }
6798
6799 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
6800    this hook to a) provide some gcc support functions, and b) transfer
6801    dynamic linking information gathered so far on function code symbol
6802    entries, to their corresponding function descriptor symbol entries.  */
6803
6804 static bfd_boolean
6805 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6806                             struct bfd_link_info *info)
6807 {
6808   struct ppc_link_hash_table *htab;
6809   unsigned int i;
6810   static const struct sfpr_def_parms funcs[] =
6811     {
6812       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6813       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6814       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6815       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6816       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6817       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6818       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6819       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6820       { "._savef", 14, 31, savefpr, savefpr1_tail },
6821       { "._restf", 14, 31, restfpr, restfpr1_tail },
6822       { "_savevr_", 20, 31, savevr, savevr_tail },
6823       { "_restvr_", 20, 31, restvr, restvr_tail }
6824     };
6825
6826   htab = ppc_hash_table (info);
6827   if (htab == NULL)
6828     return FALSE;
6829
6830   if (!info->relocatable
6831       && htab->elf.hgot != NULL)
6832     _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6833
6834   if (htab->sfpr == NULL)
6835     /* We don't have any relocs.  */
6836     return TRUE;
6837
6838   /* Provide any missing _save* and _rest* functions.  */
6839   htab->sfpr->size = 0;
6840   if (!info->relocatable)
6841     for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6842       if (!sfpr_define (info, &funcs[i]))
6843         return FALSE;
6844
6845   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6846
6847   if (htab->sfpr->size == 0)
6848     htab->sfpr->flags |= SEC_EXCLUDE;
6849
6850   return TRUE;
6851 }
6852
6853 /* Adjust a symbol defined by a dynamic object and referenced by a
6854    regular object.  The current definition is in some section of the
6855    dynamic object, but we're not including those sections.  We have to
6856    change the definition to something the rest of the link can
6857    understand.  */
6858
6859 static bfd_boolean
6860 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6861                                  struct elf_link_hash_entry *h)
6862 {
6863   struct ppc_link_hash_table *htab;
6864   asection *s;
6865
6866   htab = ppc_hash_table (info);
6867   if (htab == NULL)
6868     return FALSE;
6869
6870   /* Deal with function syms.  */
6871   if (h->type == STT_FUNC
6872       || h->type == STT_GNU_IFUNC
6873       || h->needs_plt)
6874     {
6875       /* Clear procedure linkage table information for any symbol that
6876          won't need a .plt entry.  */
6877       struct plt_entry *ent;
6878       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6879         if (ent->plt.refcount > 0)
6880           break;
6881       if (ent == NULL
6882           || (h->type != STT_GNU_IFUNC
6883               && (SYMBOL_CALLS_LOCAL (info, h)
6884                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6885                       && h->root.type == bfd_link_hash_undefweak))))
6886         {
6887           h->plt.plist = NULL;
6888           h->needs_plt = 0;
6889         }
6890     }
6891   else
6892     h->plt.plist = NULL;
6893
6894   /* If this is a weak symbol, and there is a real definition, the
6895      processor independent code will have arranged for us to see the
6896      real definition first, and we can just use the same value.  */
6897   if (h->u.weakdef != NULL)
6898     {
6899       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6900                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
6901       h->root.u.def.section = h->u.weakdef->root.u.def.section;
6902       h->root.u.def.value = h->u.weakdef->root.u.def.value;
6903       if (ELIMINATE_COPY_RELOCS)
6904         h->non_got_ref = h->u.weakdef->non_got_ref;
6905       return TRUE;
6906     }
6907
6908   /* If we are creating a shared library, we must presume that the
6909      only references to the symbol are via the global offset table.
6910      For such cases we need not do anything here; the relocations will
6911      be handled correctly by relocate_section.  */
6912   if (info->shared)
6913     return TRUE;
6914
6915   /* If there are no references to this symbol that do not use the
6916      GOT, we don't need to generate a copy reloc.  */
6917   if (!h->non_got_ref)
6918     return TRUE;
6919
6920   /* Don't generate a copy reloc for symbols defined in the executable.  */
6921   if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6922     return TRUE;
6923
6924   if (ELIMINATE_COPY_RELOCS)
6925     {
6926       struct ppc_link_hash_entry * eh;
6927       struct elf_dyn_relocs *p;
6928
6929       eh = (struct ppc_link_hash_entry *) h;
6930       for (p = eh->dyn_relocs; p != NULL; p = p->next)
6931         {
6932           s = p->sec->output_section;
6933           if (s != NULL && (s->flags & SEC_READONLY) != 0)
6934             break;
6935         }
6936
6937       /* If we didn't find any dynamic relocs in read-only sections, then
6938          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
6939       if (p == NULL)
6940         {
6941           h->non_got_ref = 0;
6942           return TRUE;
6943         }
6944     }
6945
6946   if (h->plt.plist != NULL)
6947     {
6948       /* We should never get here, but unfortunately there are versions
6949          of gcc out there that improperly (for this ABI) put initialized
6950          function pointers, vtable refs and suchlike in read-only
6951          sections.  Allow them to proceed, but warn that this might
6952          break at runtime.  */
6953       info->callbacks->einfo
6954         (_("%P: copy reloc against `%T' requires lazy plt linking; "
6955            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6956          h->root.root.string);
6957     }
6958
6959   /* This is a reference to a symbol defined by a dynamic object which
6960      is not a function.  */
6961
6962   /* We must allocate the symbol in our .dynbss section, which will
6963      become part of the .bss section of the executable.  There will be
6964      an entry for this symbol in the .dynsym section.  The dynamic
6965      object will contain position independent code, so all references
6966      from the dynamic object to this symbol will go through the global
6967      offset table.  The dynamic linker will use the .dynsym entry to
6968      determine the address it must put in the global offset table, so
6969      both the dynamic object and the regular object will refer to the
6970      same memory location for the variable.  */
6971
6972   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6973      to copy the initial value out of the dynamic object and into the
6974      runtime process image.  We need to remember the offset into the
6975      .rela.bss section we are going to use.  */
6976   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6977     {
6978       htab->relbss->size += sizeof (Elf64_External_Rela);
6979       h->needs_copy = 1;
6980     }
6981
6982   s = htab->dynbss;
6983
6984   return _bfd_elf_adjust_dynamic_copy (h, s);
6985 }
6986
6987 /* If given a function descriptor symbol, hide both the function code
6988    sym and the descriptor.  */
6989 static void
6990 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6991                        struct elf_link_hash_entry *h,
6992                        bfd_boolean force_local)
6993 {
6994   struct ppc_link_hash_entry *eh;
6995   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6996
6997   eh = (struct ppc_link_hash_entry *) h;
6998   if (eh->is_func_descriptor)
6999     {
7000       struct ppc_link_hash_entry *fh = eh->oh;
7001
7002       if (fh == NULL)
7003         {
7004           const char *p, *q;
7005           struct ppc_link_hash_table *htab;
7006           char save;
7007
7008           /* We aren't supposed to use alloca in BFD because on
7009              systems which do not have alloca the version in libiberty
7010              calls xmalloc, which might cause the program to crash
7011              when it runs out of memory.  This function doesn't have a
7012              return status, so there's no way to gracefully return an
7013              error.  So cheat.  We know that string[-1] can be safely
7014              accessed;  It's either a string in an ELF string table,
7015              or allocated in an objalloc structure.  */
7016
7017           p = eh->elf.root.root.string - 1;
7018           save = *p;
7019           *(char *) p = '.';
7020           htab = ppc_hash_table (info);
7021           if (htab == NULL)
7022             return;
7023
7024           fh = (struct ppc_link_hash_entry *)
7025             elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7026           *(char *) p = save;
7027
7028           /* Unfortunately, if it so happens that the string we were
7029              looking for was allocated immediately before this string,
7030              then we overwrote the string terminator.  That's the only
7031              reason the lookup should fail.  */
7032           if (fh == NULL)
7033             {
7034               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7035               while (q >= eh->elf.root.root.string && *q == *p)
7036                 --q, --p;
7037               if (q < eh->elf.root.root.string && *p == '.')
7038                 fh = (struct ppc_link_hash_entry *)
7039                   elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7040             }
7041           if (fh != NULL)
7042             {
7043               eh->oh = fh;
7044               fh->oh = eh;
7045             }
7046         }
7047       if (fh != NULL)
7048         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7049     }
7050 }
7051
7052 static bfd_boolean
7053 get_sym_h (struct elf_link_hash_entry **hp,
7054            Elf_Internal_Sym **symp,
7055            asection **symsecp,
7056            unsigned char **tls_maskp,
7057            Elf_Internal_Sym **locsymsp,
7058            unsigned long r_symndx,
7059            bfd *ibfd)
7060 {
7061   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7062
7063   if (r_symndx >= symtab_hdr->sh_info)
7064     {
7065       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7066       struct elf_link_hash_entry *h;
7067
7068       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7069       h = elf_follow_link (h);
7070
7071       if (hp != NULL)
7072         *hp = h;
7073
7074       if (symp != NULL)
7075         *symp = NULL;
7076
7077       if (symsecp != NULL)
7078         {
7079           asection *symsec = NULL;
7080           if (h->root.type == bfd_link_hash_defined
7081               || h->root.type == bfd_link_hash_defweak)
7082             symsec = h->root.u.def.section;
7083           *symsecp = symsec;
7084         }
7085
7086       if (tls_maskp != NULL)
7087         {
7088           struct ppc_link_hash_entry *eh;
7089
7090           eh = (struct ppc_link_hash_entry *) h;
7091           *tls_maskp = &eh->tls_mask;
7092         }
7093     }
7094   else
7095     {
7096       Elf_Internal_Sym *sym;
7097       Elf_Internal_Sym *locsyms = *locsymsp;
7098
7099       if (locsyms == NULL)
7100         {
7101           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7102           if (locsyms == NULL)
7103             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7104                                             symtab_hdr->sh_info,
7105                                             0, NULL, NULL, NULL);
7106           if (locsyms == NULL)
7107             return FALSE;
7108           *locsymsp = locsyms;
7109         }
7110       sym = locsyms + r_symndx;
7111
7112       if (hp != NULL)
7113         *hp = NULL;
7114
7115       if (symp != NULL)
7116         *symp = sym;
7117
7118       if (symsecp != NULL)
7119         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7120
7121       if (tls_maskp != NULL)
7122         {
7123           struct got_entry **lgot_ents;
7124           unsigned char *tls_mask;
7125
7126           tls_mask = NULL;
7127           lgot_ents = elf_local_got_ents (ibfd);
7128           if (lgot_ents != NULL)
7129             {
7130               struct plt_entry **local_plt = (struct plt_entry **)
7131                 (lgot_ents + symtab_hdr->sh_info);
7132               unsigned char *lgot_masks = (unsigned char *)
7133                 (local_plt + symtab_hdr->sh_info);
7134               tls_mask = &lgot_masks[r_symndx];
7135             }
7136           *tls_maskp = tls_mask;
7137         }
7138     }
7139   return TRUE;
7140 }
7141
7142 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
7143    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7144    type suitable for optimization, and 1 otherwise.  */
7145
7146 static int
7147 get_tls_mask (unsigned char **tls_maskp,
7148               unsigned long *toc_symndx,
7149               bfd_vma *toc_addend,
7150               Elf_Internal_Sym **locsymsp,
7151               const Elf_Internal_Rela *rel,
7152               bfd *ibfd)
7153 {
7154   unsigned long r_symndx;
7155   int next_r;
7156   struct elf_link_hash_entry *h;
7157   Elf_Internal_Sym *sym;
7158   asection *sec;
7159   bfd_vma off;
7160
7161   r_symndx = ELF64_R_SYM (rel->r_info);
7162   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7163     return 0;
7164
7165   if ((*tls_maskp != NULL && **tls_maskp != 0)
7166       || sec == NULL
7167       || ppc64_elf_section_data (sec) == NULL
7168       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7169     return 1;
7170
7171   /* Look inside a TOC section too.  */
7172   if (h != NULL)
7173     {
7174       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7175       off = h->root.u.def.value;
7176     }
7177   else
7178     off = sym->st_value;
7179   off += rel->r_addend;
7180   BFD_ASSERT (off % 8 == 0);
7181   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7182   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7183   if (toc_symndx != NULL)
7184     *toc_symndx = r_symndx;
7185   if (toc_addend != NULL)
7186     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7187   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7188     return 0;
7189   if ((h == NULL || is_static_defined (h))
7190       && (next_r == -1 || next_r == -2))
7191     return 1 - next_r;
7192   return 1;
7193 }
7194
7195 /* Find (or create) an entry in the tocsave hash table.  */
7196
7197 static struct tocsave_entry *
7198 tocsave_find (struct ppc_link_hash_table *htab,
7199               enum insert_option insert,
7200               Elf_Internal_Sym **local_syms,
7201               const Elf_Internal_Rela *irela,
7202               bfd *ibfd)
7203 {
7204   unsigned long r_indx;
7205   struct elf_link_hash_entry *h;
7206   Elf_Internal_Sym *sym;
7207   struct tocsave_entry ent, *p;
7208   hashval_t hash;
7209   struct tocsave_entry **slot;
7210
7211   r_indx = ELF64_R_SYM (irela->r_info);
7212   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7213     return NULL;
7214   if (ent.sec == NULL || ent.sec->output_section == NULL)
7215     {
7216       (*_bfd_error_handler)
7217         (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7218       return NULL;
7219     }
7220
7221   if (h != NULL)
7222     ent.offset = h->root.u.def.value;
7223   else
7224     ent.offset = sym->st_value;
7225   ent.offset += irela->r_addend;
7226
7227   hash = tocsave_htab_hash (&ent);
7228   slot = ((struct tocsave_entry **)
7229           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7230   if (slot == NULL)
7231     return NULL;
7232
7233   if (*slot == NULL)
7234     {
7235       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7236       if (p == NULL)
7237         return NULL;
7238       *p = ent;
7239       *slot = p;
7240     }
7241   return *slot;
7242 }
7243
7244 /* Adjust all global syms defined in opd sections.  In gcc generated
7245    code for the old ABI, these will already have been done.  */
7246
7247 static bfd_boolean
7248 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7249 {
7250   struct ppc_link_hash_entry *eh;
7251   asection *sym_sec;
7252   struct _opd_sec_data *opd;
7253
7254   if (h->root.type == bfd_link_hash_indirect)
7255     return TRUE;
7256
7257   if (h->root.type != bfd_link_hash_defined
7258       && h->root.type != bfd_link_hash_defweak)
7259     return TRUE;
7260
7261   eh = (struct ppc_link_hash_entry *) h;
7262   if (eh->adjust_done)
7263     return TRUE;
7264
7265   sym_sec = eh->elf.root.u.def.section;
7266   opd = get_opd_info (sym_sec);
7267   if (opd != NULL && opd->adjust != NULL)
7268     {
7269       long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
7270       if (adjust == -1)
7271         {
7272           /* This entry has been deleted.  */
7273           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7274           if (dsec == NULL)
7275             {
7276               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7277                 if (discarded_section (dsec))
7278                   {
7279                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7280                     break;
7281                   }
7282             }
7283           eh->elf.root.u.def.value = 0;
7284           eh->elf.root.u.def.section = dsec;
7285         }
7286       else
7287         eh->elf.root.u.def.value += adjust;
7288       eh->adjust_done = 1;
7289     }
7290   return TRUE;
7291 }
7292
7293 /* Handles decrementing dynamic reloc counts for the reloc specified by
7294    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7295    have already been determined.  */
7296
7297 static bfd_boolean
7298 dec_dynrel_count (bfd_vma r_info,
7299                   asection *sec,
7300                   struct bfd_link_info *info,
7301                   Elf_Internal_Sym **local_syms,
7302                   struct elf_link_hash_entry *h,
7303                   Elf_Internal_Sym *sym)
7304 {
7305   enum elf_ppc64_reloc_type r_type;
7306   asection *sym_sec = NULL;
7307
7308   /* Can this reloc be dynamic?  This switch, and later tests here
7309      should be kept in sync with the code in check_relocs.  */
7310   r_type = ELF64_R_TYPE (r_info);
7311   switch (r_type)
7312     {
7313     default:
7314       return TRUE;
7315
7316     case R_PPC64_TPREL16:
7317     case R_PPC64_TPREL16_LO:
7318     case R_PPC64_TPREL16_HI:
7319     case R_PPC64_TPREL16_HA:
7320     case R_PPC64_TPREL16_DS:
7321     case R_PPC64_TPREL16_LO_DS:
7322     case R_PPC64_TPREL16_HIGH:
7323     case R_PPC64_TPREL16_HIGHA:
7324     case R_PPC64_TPREL16_HIGHER:
7325     case R_PPC64_TPREL16_HIGHERA:
7326     case R_PPC64_TPREL16_HIGHEST:
7327     case R_PPC64_TPREL16_HIGHESTA:
7328       if (!info->shared)
7329         return TRUE;
7330
7331     case R_PPC64_TPREL64:
7332     case R_PPC64_DTPMOD64:
7333     case R_PPC64_DTPREL64:
7334     case R_PPC64_ADDR64:
7335     case R_PPC64_REL30:
7336     case R_PPC64_REL32:
7337     case R_PPC64_REL64:
7338     case R_PPC64_ADDR14:
7339     case R_PPC64_ADDR14_BRNTAKEN:
7340     case R_PPC64_ADDR14_BRTAKEN:
7341     case R_PPC64_ADDR16:
7342     case R_PPC64_ADDR16_DS:
7343     case R_PPC64_ADDR16_HA:
7344     case R_PPC64_ADDR16_HI:
7345     case R_PPC64_ADDR16_HIGH:
7346     case R_PPC64_ADDR16_HIGHA:
7347     case R_PPC64_ADDR16_HIGHER:
7348     case R_PPC64_ADDR16_HIGHERA:
7349     case R_PPC64_ADDR16_HIGHEST:
7350     case R_PPC64_ADDR16_HIGHESTA:
7351     case R_PPC64_ADDR16_LO:
7352     case R_PPC64_ADDR16_LO_DS:
7353     case R_PPC64_ADDR24:
7354     case R_PPC64_ADDR32:
7355     case R_PPC64_UADDR16:
7356     case R_PPC64_UADDR32:
7357     case R_PPC64_UADDR64:
7358     case R_PPC64_TOC:
7359       break;
7360     }
7361
7362   if (local_syms != NULL)
7363     {
7364       unsigned long r_symndx;
7365       bfd *ibfd = sec->owner;
7366
7367       r_symndx = ELF64_R_SYM (r_info);
7368       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7369         return FALSE;
7370     }
7371
7372   if ((info->shared
7373        && (must_be_dyn_reloc (info, r_type)
7374            || (h != NULL
7375                && (!SYMBOLIC_BIND (info, h)
7376                    || h->root.type == bfd_link_hash_defweak
7377                    || !h->def_regular))))
7378       || (ELIMINATE_COPY_RELOCS
7379           && !info->shared
7380           && h != NULL
7381           && (h->root.type == bfd_link_hash_defweak
7382               || !h->def_regular)))
7383     ;
7384   else
7385     return TRUE;
7386
7387   if (h != NULL)
7388     {
7389       struct elf_dyn_relocs *p;
7390       struct elf_dyn_relocs **pp;
7391       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7392
7393       /* elf_gc_sweep may have already removed all dyn relocs associated
7394          with local syms for a given section.  Also, symbol flags are
7395          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7396          report a dynreloc miscount.  */
7397       if (*pp == NULL && info->gc_sections)
7398         return TRUE;
7399
7400       while ((p = *pp) != NULL)
7401         {
7402           if (p->sec == sec)
7403             {
7404               if (!must_be_dyn_reloc (info, r_type))
7405                 p->pc_count -= 1;
7406               p->count -= 1;
7407               if (p->count == 0)
7408                 *pp = p->next;
7409               return TRUE;
7410             }
7411           pp = &p->next;
7412         }
7413     }
7414   else
7415     {
7416       struct ppc_dyn_relocs *p;
7417       struct ppc_dyn_relocs **pp;
7418       void *vpp;
7419       bfd_boolean is_ifunc;
7420
7421       if (local_syms == NULL)
7422         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7423       if (sym_sec == NULL)
7424         sym_sec = sec;
7425
7426       vpp = &elf_section_data (sym_sec)->local_dynrel;
7427       pp = (struct ppc_dyn_relocs **) vpp;
7428
7429       if (*pp == NULL && info->gc_sections)
7430         return TRUE;
7431
7432       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7433       while ((p = *pp) != NULL)
7434         {
7435           if (p->sec == sec && p->ifunc == is_ifunc)
7436             {
7437               p->count -= 1;
7438               if (p->count == 0)
7439                 *pp = p->next;
7440               return TRUE;
7441             }
7442           pp = &p->next;
7443         }
7444     }
7445
7446   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7447                           sec->owner, sec);
7448   bfd_set_error (bfd_error_bad_value);
7449   return FALSE;
7450 }
7451
7452 /* Remove unused Official Procedure Descriptor entries.  Currently we
7453    only remove those associated with functions in discarded link-once
7454    sections, or weakly defined functions that have been overridden.  It
7455    would be possible to remove many more entries for statically linked
7456    applications.  */
7457
7458 bfd_boolean
7459 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
7460 {
7461   bfd *ibfd;
7462   bfd_boolean some_edited = FALSE;
7463   asection *need_pad = NULL;
7464
7465   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7466     {
7467       asection *sec;
7468       Elf_Internal_Rela *relstart, *rel, *relend;
7469       Elf_Internal_Shdr *symtab_hdr;
7470       Elf_Internal_Sym *local_syms;
7471       bfd_vma offset;
7472       struct _opd_sec_data *opd;
7473       bfd_boolean need_edit, add_aux_fields;
7474       bfd_size_type cnt_16b = 0;
7475
7476       if (!is_ppc64_elf (ibfd))
7477         continue;
7478
7479       sec = bfd_get_section_by_name (ibfd, ".opd");
7480       if (sec == NULL || sec->size == 0)
7481         continue;
7482
7483       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7484         continue;
7485
7486       if (sec->output_section == bfd_abs_section_ptr)
7487         continue;
7488
7489       /* Look through the section relocs.  */
7490       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7491         continue;
7492
7493       local_syms = NULL;
7494       symtab_hdr = &elf_symtab_hdr (ibfd);
7495
7496       /* Read the relocations.  */
7497       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7498                                             info->keep_memory);
7499       if (relstart == NULL)
7500         return FALSE;
7501
7502       /* First run through the relocs to check they are sane, and to
7503          determine whether we need to edit this opd section.  */
7504       need_edit = FALSE;
7505       need_pad = sec;
7506       offset = 0;
7507       relend = relstart + sec->reloc_count;
7508       for (rel = relstart; rel < relend; )
7509         {
7510           enum elf_ppc64_reloc_type r_type;
7511           unsigned long r_symndx;
7512           asection *sym_sec;
7513           struct elf_link_hash_entry *h;
7514           Elf_Internal_Sym *sym;
7515
7516           /* .opd contains a regular array of 16 or 24 byte entries.  We're
7517              only interested in the reloc pointing to a function entry
7518              point.  */
7519           if (rel->r_offset != offset
7520               || rel + 1 >= relend
7521               || (rel + 1)->r_offset != offset + 8)
7522             {
7523               /* If someone messes with .opd alignment then after a
7524                  "ld -r" we might have padding in the middle of .opd.
7525                  Also, there's nothing to prevent someone putting
7526                  something silly in .opd with the assembler.  No .opd
7527                  optimization for them!  */
7528             broken_opd:
7529               (*_bfd_error_handler)
7530                 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7531               need_edit = FALSE;
7532               break;
7533             }
7534
7535           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7536               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7537             {
7538               (*_bfd_error_handler)
7539                 (_("%B: unexpected reloc type %u in .opd section"),
7540                  ibfd, r_type);
7541               need_edit = FALSE;
7542               break;
7543             }
7544
7545           r_symndx = ELF64_R_SYM (rel->r_info);
7546           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7547                           r_symndx, ibfd))
7548             goto error_ret;
7549
7550           if (sym_sec == NULL || sym_sec->owner == NULL)
7551             {
7552               const char *sym_name;
7553               if (h != NULL)
7554                 sym_name = h->root.root.string;
7555               else
7556                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7557                                              sym_sec);
7558
7559               (*_bfd_error_handler)
7560                 (_("%B: undefined sym `%s' in .opd section"),
7561                  ibfd, sym_name);
7562               need_edit = FALSE;
7563               break;
7564             }
7565
7566           /* opd entries are always for functions defined in the
7567              current input bfd.  If the symbol isn't defined in the
7568              input bfd, then we won't be using the function in this
7569              bfd;  It must be defined in a linkonce section in another
7570              bfd, or is weak.  It's also possible that we are
7571              discarding the function due to a linker script /DISCARD/,
7572              which we test for via the output_section.  */
7573           if (sym_sec->owner != ibfd
7574               || sym_sec->output_section == bfd_abs_section_ptr)
7575             need_edit = TRUE;
7576
7577           rel += 2;
7578           if (rel == relend
7579               || (rel + 1 == relend && rel->r_offset == offset + 16))
7580             {
7581               if (sec->size == offset + 24)
7582                 {
7583                   need_pad = NULL;
7584                   break;
7585                 }
7586               if (rel == relend && sec->size == offset + 16)
7587                 {
7588                   cnt_16b++;
7589                   break;
7590                 }
7591               goto broken_opd;
7592             }
7593
7594           if (rel->r_offset == offset + 24)
7595             offset += 24;
7596           else if (rel->r_offset != offset + 16)
7597             goto broken_opd;
7598           else if (rel + 1 < relend
7599                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7600                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7601             {
7602               offset += 16;
7603               cnt_16b++;
7604             }
7605           else if (rel + 2 < relend
7606                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7607                    && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7608             {
7609               offset += 24;
7610               rel += 1;
7611             }
7612           else
7613             goto broken_opd;
7614         }
7615
7616       add_aux_fields = non_overlapping && cnt_16b > 0;
7617
7618       if (need_edit || add_aux_fields)
7619         {
7620           Elf_Internal_Rela *write_rel;
7621           Elf_Internal_Shdr *rel_hdr;
7622           bfd_byte *rptr, *wptr;
7623           bfd_byte *new_contents;
7624           bfd_boolean skip;
7625           long opd_ent_size;
7626           bfd_size_type amt;
7627
7628           new_contents = NULL;
7629           amt = sec->size * sizeof (long) / 8;
7630           opd = &ppc64_elf_section_data (sec)->u.opd;
7631           opd->adjust = bfd_zalloc (sec->owner, amt);
7632           if (opd->adjust == NULL)
7633             return FALSE;
7634           ppc64_elf_section_data (sec)->sec_type = sec_opd;
7635
7636           /* This seems a waste of time as input .opd sections are all
7637              zeros as generated by gcc, but I suppose there's no reason
7638              this will always be so.  We might start putting something in
7639              the third word of .opd entries.  */
7640           if ((sec->flags & SEC_IN_MEMORY) == 0)
7641             {
7642               bfd_byte *loc;
7643               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7644                 {
7645                   if (loc != NULL)
7646                     free (loc);
7647                 error_ret:
7648                   if (local_syms != NULL
7649                       && symtab_hdr->contents != (unsigned char *) local_syms)
7650                     free (local_syms);
7651                   if (elf_section_data (sec)->relocs != relstart)
7652                     free (relstart);
7653                   return FALSE;
7654                 }
7655               sec->contents = loc;
7656               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7657             }
7658
7659           elf_section_data (sec)->relocs = relstart;
7660
7661           new_contents = sec->contents;
7662           if (add_aux_fields)
7663             {
7664               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7665               if (new_contents == NULL)
7666                 return FALSE;
7667               need_pad = FALSE;
7668             }
7669           wptr = new_contents;
7670           rptr = sec->contents;
7671
7672           write_rel = relstart;
7673           skip = FALSE;
7674           offset = 0;
7675           opd_ent_size = 0;
7676           for (rel = relstart; rel < relend; rel++)
7677             {
7678               unsigned long r_symndx;
7679               asection *sym_sec;
7680               struct elf_link_hash_entry *h;
7681               Elf_Internal_Sym *sym;
7682
7683               r_symndx = ELF64_R_SYM (rel->r_info);
7684               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7685                               r_symndx, ibfd))
7686                 goto error_ret;
7687
7688               if (rel->r_offset == offset)
7689                 {
7690                   struct ppc_link_hash_entry *fdh = NULL;
7691
7692                   /* See if the .opd entry is full 24 byte or
7693                      16 byte (with fd_aux entry overlapped with next
7694                      fd_func).  */
7695                   opd_ent_size = 24;
7696                   if ((rel + 2 == relend && sec->size == offset + 16)
7697                       || (rel + 3 < relend
7698                           && rel[2].r_offset == offset + 16
7699                           && rel[3].r_offset == offset + 24
7700                           && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7701                           && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7702                     opd_ent_size = 16;
7703
7704                   if (h != NULL
7705                       && h->root.root.string[0] == '.')
7706                     {
7707                       struct ppc_link_hash_table *htab;
7708
7709                       htab = ppc_hash_table (info);
7710                       if (htab != NULL)
7711                         fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7712                                           htab);
7713                       if (fdh != NULL
7714                           && fdh->elf.root.type != bfd_link_hash_defined
7715                           && fdh->elf.root.type != bfd_link_hash_defweak)
7716                         fdh = NULL;
7717                     }
7718
7719                   skip = (sym_sec->owner != ibfd
7720                           || sym_sec->output_section == bfd_abs_section_ptr);
7721                   if (skip)
7722                     {
7723                       if (fdh != NULL && sym_sec->owner == ibfd)
7724                         {
7725                           /* Arrange for the function descriptor sym
7726                              to be dropped.  */
7727                           fdh->elf.root.u.def.value = 0;
7728                           fdh->elf.root.u.def.section = sym_sec;
7729                         }
7730                       opd->adjust[rel->r_offset / 8] = -1;
7731                     }
7732                   else
7733                     {
7734                       /* We'll be keeping this opd entry.  */
7735
7736                       if (fdh != NULL)
7737                         {
7738                           /* Redefine the function descriptor symbol to
7739                              this location in the opd section.  It is
7740                              necessary to update the value here rather
7741                              than using an array of adjustments as we do
7742                              for local symbols, because various places
7743                              in the generic ELF code use the value
7744                              stored in u.def.value.  */
7745                           fdh->elf.root.u.def.value = wptr - new_contents;
7746                           fdh->adjust_done = 1;
7747                         }
7748
7749                       /* Local syms are a bit tricky.  We could
7750                          tweak them as they can be cached, but
7751                          we'd need to look through the local syms
7752                          for the function descriptor sym which we
7753                          don't have at the moment.  So keep an
7754                          array of adjustments.  */
7755                       opd->adjust[rel->r_offset / 8]
7756                         = (wptr - new_contents) - (rptr - sec->contents);
7757
7758                       if (wptr != rptr)
7759                         memcpy (wptr, rptr, opd_ent_size);
7760                       wptr += opd_ent_size;
7761                       if (add_aux_fields && opd_ent_size == 16)
7762                         {
7763                           memset (wptr, '\0', 8);
7764                           wptr += 8;
7765                         }
7766                     }
7767                   rptr += opd_ent_size;
7768                   offset += opd_ent_size;
7769                 }
7770
7771               if (skip)
7772                 {
7773                   if (!NO_OPD_RELOCS
7774                       && !info->relocatable
7775                       && !dec_dynrel_count (rel->r_info, sec, info,
7776                                             NULL, h, sym))
7777                     goto error_ret;
7778                 }
7779               else
7780                 {
7781                   /* We need to adjust any reloc offsets to point to the
7782                      new opd entries.  While we're at it, we may as well
7783                      remove redundant relocs.  */
7784                   rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
7785                   if (write_rel != rel)
7786                     memcpy (write_rel, rel, sizeof (*rel));
7787                   ++write_rel;
7788                 }
7789             }
7790
7791           sec->size = wptr - new_contents;
7792           sec->reloc_count = write_rel - relstart;
7793           if (add_aux_fields)
7794             {
7795               free (sec->contents);
7796               sec->contents = new_contents;
7797             }
7798
7799           /* Fudge the header size too, as this is used later in
7800              elf_bfd_final_link if we are emitting relocs.  */
7801           rel_hdr = _bfd_elf_single_rel_hdr (sec);
7802           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7803           some_edited = TRUE;
7804         }
7805       else if (elf_section_data (sec)->relocs != relstart)
7806         free (relstart);
7807
7808       if (local_syms != NULL
7809           && symtab_hdr->contents != (unsigned char *) local_syms)
7810         {
7811           if (!info->keep_memory)
7812             free (local_syms);
7813           else
7814             symtab_hdr->contents = (unsigned char *) local_syms;
7815         }
7816     }
7817
7818   if (some_edited)
7819     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7820
7821   /* If we are doing a final link and the last .opd entry is just 16 byte
7822      long, add a 8 byte padding after it.  */
7823   if (need_pad != NULL && !info->relocatable)
7824     {
7825       bfd_byte *p;
7826
7827       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7828         {
7829           BFD_ASSERT (need_pad->size > 0);
7830
7831           p = bfd_malloc (need_pad->size + 8);
7832           if (p == NULL)
7833             return FALSE;
7834
7835           if (! bfd_get_section_contents (need_pad->owner, need_pad,
7836                                           p, 0, need_pad->size))
7837             return FALSE;
7838
7839           need_pad->contents = p;
7840           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7841         }
7842       else
7843         {
7844           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7845           if (p == NULL)
7846             return FALSE;
7847
7848           need_pad->contents = p;
7849         }
7850
7851       memset (need_pad->contents + need_pad->size, 0, 8);
7852       need_pad->size += 8;
7853     }
7854
7855   return TRUE;
7856 }
7857
7858 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
7859
7860 asection *
7861 ppc64_elf_tls_setup (struct bfd_link_info *info,
7862                      int no_tls_get_addr_opt,
7863                      int *no_multi_toc)
7864 {
7865   struct ppc_link_hash_table *htab;
7866
7867   htab = ppc_hash_table (info);
7868   if (htab == NULL)
7869     return NULL;
7870
7871   if (abiversion (info->output_bfd) == 1)
7872     htab->opd_abi = 1;
7873
7874   if (*no_multi_toc)
7875     htab->do_multi_toc = 0;
7876   else if (!htab->do_multi_toc)
7877     *no_multi_toc = 1;
7878
7879   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7880                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7881                                               FALSE, FALSE, TRUE));
7882   /* Move dynamic linking info to the function descriptor sym.  */
7883   if (htab->tls_get_addr != NULL)
7884     func_desc_adjust (&htab->tls_get_addr->elf, info);
7885   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7886                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7887                                                  FALSE, FALSE, TRUE));
7888   if (!no_tls_get_addr_opt)
7889     {
7890       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7891
7892       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7893                                   FALSE, FALSE, TRUE);
7894       if (opt != NULL)
7895         func_desc_adjust (opt, info);
7896       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7897                                      FALSE, FALSE, TRUE);
7898       if (opt_fd != NULL
7899           && (opt_fd->root.type == bfd_link_hash_defined
7900               || opt_fd->root.type == bfd_link_hash_defweak))
7901         {
7902           /* If glibc supports an optimized __tls_get_addr call stub,
7903              signalled by the presence of __tls_get_addr_opt, and we'll
7904              be calling __tls_get_addr via a plt call stub, then
7905              make __tls_get_addr point to __tls_get_addr_opt.  */
7906           tga_fd = &htab->tls_get_addr_fd->elf;
7907           if (htab->elf.dynamic_sections_created
7908               && tga_fd != NULL
7909               && (tga_fd->type == STT_FUNC
7910                   || tga_fd->needs_plt)
7911               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7912                    || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7913                        && tga_fd->root.type == bfd_link_hash_undefweak)))
7914             {
7915               struct plt_entry *ent;
7916
7917               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7918                 if (ent->plt.refcount > 0)
7919                   break;
7920               if (ent != NULL)
7921                 {
7922                   tga_fd->root.type = bfd_link_hash_indirect;
7923                   tga_fd->root.u.i.link = &opt_fd->root;
7924                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7925                   if (opt_fd->dynindx != -1)
7926                     {
7927                       /* Use __tls_get_addr_opt in dynamic relocations.  */
7928                       opt_fd->dynindx = -1;
7929                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7930                                               opt_fd->dynstr_index);
7931                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7932                         return NULL;
7933                     }
7934                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7935                   tga = &htab->tls_get_addr->elf;
7936                   if (opt != NULL && tga != NULL)
7937                     {
7938                       tga->root.type = bfd_link_hash_indirect;
7939                       tga->root.u.i.link = &opt->root;
7940                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
7941                       _bfd_elf_link_hash_hide_symbol (info, opt,
7942                                                       tga->forced_local);
7943                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7944                     }
7945                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7946                   htab->tls_get_addr_fd->is_func_descriptor = 1;
7947                   if (htab->tls_get_addr != NULL)
7948                     {
7949                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7950                       htab->tls_get_addr->is_func = 1;
7951                     }
7952                 }
7953             }
7954         }
7955       else
7956         no_tls_get_addr_opt = TRUE;
7957     }
7958   htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
7959   return _bfd_elf_tls_setup (info->output_bfd, info);
7960 }
7961
7962 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7963    HASH1 or HASH2.  */
7964
7965 static bfd_boolean
7966 branch_reloc_hash_match (const bfd *ibfd,
7967                          const Elf_Internal_Rela *rel,
7968                          const struct ppc_link_hash_entry *hash1,
7969                          const struct ppc_link_hash_entry *hash2)
7970 {
7971   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7972   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7973   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7974
7975   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7976     {
7977       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7978       struct elf_link_hash_entry *h;
7979
7980       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7981       h = elf_follow_link (h);
7982       if (h == &hash1->elf || h == &hash2->elf)
7983         return TRUE;
7984     }
7985   return FALSE;
7986 }
7987
7988 /* Run through all the TLS relocs looking for optimization
7989    opportunities.  The linker has been hacked (see ppc64elf.em) to do
7990    a preliminary section layout so that we know the TLS segment
7991    offsets.  We can't optimize earlier because some optimizations need
7992    to know the tp offset, and we need to optimize before allocating
7993    dynamic relocations.  */
7994
7995 bfd_boolean
7996 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7997 {
7998   bfd *ibfd;
7999   asection *sec;
8000   struct ppc_link_hash_table *htab;
8001   unsigned char *toc_ref;
8002   int pass;
8003
8004   if (info->relocatable || !info->executable)
8005     return TRUE;
8006
8007   htab = ppc_hash_table (info);
8008   if (htab == NULL)
8009     return FALSE;
8010
8011   /* Make two passes over the relocs.  On the first pass, mark toc
8012      entries involved with tls relocs, and check that tls relocs
8013      involved in setting up a tls_get_addr call are indeed followed by
8014      such a call.  If they are not, we can't do any tls optimization.
8015      On the second pass twiddle tls_mask flags to notify
8016      relocate_section that optimization can be done, and adjust got
8017      and plt refcounts.  */
8018   toc_ref = NULL;
8019   for (pass = 0; pass < 2; ++pass)
8020     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8021       {
8022         Elf_Internal_Sym *locsyms = NULL;
8023         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8024
8025         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8026           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8027             {
8028               Elf_Internal_Rela *relstart, *rel, *relend;
8029               bfd_boolean found_tls_get_addr_arg = 0;
8030
8031               /* Read the relocations.  */
8032               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8033                                                     info->keep_memory);
8034               if (relstart == NULL)
8035                 return FALSE;
8036
8037               relend = relstart + sec->reloc_count;
8038               for (rel = relstart; rel < relend; rel++)
8039                 {
8040                   enum elf_ppc64_reloc_type r_type;
8041                   unsigned long r_symndx;
8042                   struct elf_link_hash_entry *h;
8043                   Elf_Internal_Sym *sym;
8044                   asection *sym_sec;
8045                   unsigned char *tls_mask;
8046                   unsigned char tls_set, tls_clear, tls_type = 0;
8047                   bfd_vma value;
8048                   bfd_boolean ok_tprel, is_local;
8049                   long toc_ref_index = 0;
8050                   int expecting_tls_get_addr = 0;
8051                   bfd_boolean ret = FALSE;
8052
8053                   r_symndx = ELF64_R_SYM (rel->r_info);
8054                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8055                                   r_symndx, ibfd))
8056                     {
8057                     err_free_rel:
8058                       if (elf_section_data (sec)->relocs != relstart)
8059                         free (relstart);
8060                       if (toc_ref != NULL)
8061                         free (toc_ref);
8062                       if (locsyms != NULL
8063                           && (elf_symtab_hdr (ibfd).contents
8064                               != (unsigned char *) locsyms))
8065                         free (locsyms);
8066                       return ret;
8067                     }
8068
8069                   if (h != NULL)
8070                     {
8071                       if (h->root.type == bfd_link_hash_defined
8072                           || h->root.type == bfd_link_hash_defweak)
8073                         value = h->root.u.def.value;
8074                       else if (h->root.type == bfd_link_hash_undefweak)
8075                         value = 0;
8076                       else
8077                         {
8078                           found_tls_get_addr_arg = 0;
8079                           continue;
8080                         }
8081                     }
8082                   else
8083                     /* Symbols referenced by TLS relocs must be of type
8084                        STT_TLS.  So no need for .opd local sym adjust.  */
8085                     value = sym->st_value;
8086
8087                   ok_tprel = FALSE;
8088                   is_local = FALSE;
8089                   if (h == NULL
8090                       || !h->def_dynamic)
8091                     {
8092                       is_local = TRUE;
8093                       if (h != NULL
8094                           && h->root.type == bfd_link_hash_undefweak)
8095                         ok_tprel = TRUE;
8096                       else
8097                         {
8098                           value += sym_sec->output_offset;
8099                           value += sym_sec->output_section->vma;
8100                           value -= htab->elf.tls_sec->vma;
8101                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8102                                       < (bfd_vma) 1 << 32);
8103                         }
8104                     }
8105
8106                   r_type = ELF64_R_TYPE (rel->r_info);
8107                   /* If this section has old-style __tls_get_addr calls
8108                      without marker relocs, then check that each
8109                      __tls_get_addr call reloc is preceded by a reloc
8110                      that conceivably belongs to the __tls_get_addr arg
8111                      setup insn.  If we don't find matching arg setup
8112                      relocs, don't do any tls optimization.  */
8113                   if (pass == 0
8114                       && sec->has_tls_get_addr_call
8115                       && h != NULL
8116                       && (h == &htab->tls_get_addr->elf
8117                           || h == &htab->tls_get_addr_fd->elf)
8118                       && !found_tls_get_addr_arg
8119                       && is_branch_reloc (r_type))
8120                     {
8121                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8122                                                 "TLS optimization disabled\n"),
8123                                               ibfd, sec, rel->r_offset);
8124                       ret = TRUE;
8125                       goto err_free_rel;
8126                     }
8127
8128                   found_tls_get_addr_arg = 0;
8129                   switch (r_type)
8130                     {
8131                     case R_PPC64_GOT_TLSLD16:
8132                     case R_PPC64_GOT_TLSLD16_LO:
8133                       expecting_tls_get_addr = 1;
8134                       found_tls_get_addr_arg = 1;
8135                       /* Fall thru */
8136
8137                     case R_PPC64_GOT_TLSLD16_HI:
8138                     case R_PPC64_GOT_TLSLD16_HA:
8139                       /* These relocs should never be against a symbol
8140                          defined in a shared lib.  Leave them alone if
8141                          that turns out to be the case.  */
8142                       if (!is_local)
8143                         continue;
8144
8145                       /* LD -> LE */
8146                       tls_set = 0;
8147                       tls_clear = TLS_LD;
8148                       tls_type = TLS_TLS | TLS_LD;
8149                       break;
8150
8151                     case R_PPC64_GOT_TLSGD16:
8152                     case R_PPC64_GOT_TLSGD16_LO:
8153                       expecting_tls_get_addr = 1;
8154                       found_tls_get_addr_arg = 1;
8155                       /* Fall thru */
8156
8157                     case R_PPC64_GOT_TLSGD16_HI:
8158                     case R_PPC64_GOT_TLSGD16_HA:
8159                       if (ok_tprel)
8160                         /* GD -> LE */
8161                         tls_set = 0;
8162                       else
8163                         /* GD -> IE */
8164                         tls_set = TLS_TLS | TLS_TPRELGD;
8165                       tls_clear = TLS_GD;
8166                       tls_type = TLS_TLS | TLS_GD;
8167                       break;
8168
8169                     case R_PPC64_GOT_TPREL16_DS:
8170                     case R_PPC64_GOT_TPREL16_LO_DS:
8171                     case R_PPC64_GOT_TPREL16_HI:
8172                     case R_PPC64_GOT_TPREL16_HA:
8173                       if (ok_tprel)
8174                         {
8175                           /* IE -> LE */
8176                           tls_set = 0;
8177                           tls_clear = TLS_TPREL;
8178                           tls_type = TLS_TLS | TLS_TPREL;
8179                           break;
8180                         }
8181                       continue;
8182
8183                     case R_PPC64_TLSGD:
8184                     case R_PPC64_TLSLD:
8185                       found_tls_get_addr_arg = 1;
8186                       /* Fall thru */
8187
8188                     case R_PPC64_TLS:
8189                     case R_PPC64_TOC16:
8190                     case R_PPC64_TOC16_LO:
8191                       if (sym_sec == NULL || sym_sec != toc)
8192                         continue;
8193
8194                       /* Mark this toc entry as referenced by a TLS
8195                          code sequence.  We can do that now in the
8196                          case of R_PPC64_TLS, and after checking for
8197                          tls_get_addr for the TOC16 relocs.  */
8198                       if (toc_ref == NULL)
8199                         toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8200                       if (toc_ref == NULL)
8201                         goto err_free_rel;
8202
8203                       if (h != NULL)
8204                         value = h->root.u.def.value;
8205                       else
8206                         value = sym->st_value;
8207                       value += rel->r_addend;
8208                       BFD_ASSERT (value < toc->size && value % 8 == 0);
8209                       toc_ref_index = (value + toc->output_offset) / 8;
8210                       if (r_type == R_PPC64_TLS
8211                           || r_type == R_PPC64_TLSGD
8212                           || r_type == R_PPC64_TLSLD)
8213                         {
8214                           toc_ref[toc_ref_index] = 1;
8215                           continue;
8216                         }
8217
8218                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
8219                         continue;
8220
8221                       tls_set = 0;
8222                       tls_clear = 0;
8223                       expecting_tls_get_addr = 2;
8224                       break;
8225
8226                     case R_PPC64_TPREL64:
8227                       if (pass == 0
8228                           || sec != toc
8229                           || toc_ref == NULL
8230                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8231                         continue;
8232                       if (ok_tprel)
8233                         {
8234                           /* IE -> LE */
8235                           tls_set = TLS_EXPLICIT;
8236                           tls_clear = TLS_TPREL;
8237                           break;
8238                         }
8239                       continue;
8240
8241                     case R_PPC64_DTPMOD64:
8242                       if (pass == 0
8243                           || sec != toc
8244                           || toc_ref == NULL
8245                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8246                         continue;
8247                       if (rel + 1 < relend
8248                           && (rel[1].r_info
8249                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8250                           && rel[1].r_offset == rel->r_offset + 8)
8251                         {
8252                           if (ok_tprel)
8253                             /* GD -> LE */
8254                             tls_set = TLS_EXPLICIT | TLS_GD;
8255                           else
8256                             /* GD -> IE */
8257                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8258                           tls_clear = TLS_GD;
8259                         }
8260                       else
8261                         {
8262                           if (!is_local)
8263                             continue;
8264
8265                           /* LD -> LE */
8266                           tls_set = TLS_EXPLICIT;
8267                           tls_clear = TLS_LD;
8268                         }
8269                       break;
8270
8271                     default:
8272                       continue;
8273                     }
8274
8275                   if (pass == 0)
8276                     {
8277                       if (!expecting_tls_get_addr
8278                           || !sec->has_tls_get_addr_call)
8279                         continue;
8280
8281                       if (rel + 1 < relend
8282                           && branch_reloc_hash_match (ibfd, rel + 1,
8283                                                       htab->tls_get_addr,
8284                                                       htab->tls_get_addr_fd))
8285                         {
8286                           if (expecting_tls_get_addr == 2)
8287                             {
8288                               /* Check for toc tls entries.  */
8289                               unsigned char *toc_tls;
8290                               int retval;
8291
8292                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8293                                                      &locsyms,
8294                                                      rel, ibfd);
8295                               if (retval == 0)
8296                                 goto err_free_rel;
8297                               if (toc_tls != NULL)
8298                                 {
8299                                   if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8300                                     found_tls_get_addr_arg = 1;
8301                                   if (retval > 1)
8302                                     toc_ref[toc_ref_index] = 1;
8303                                 }
8304                             }
8305                           continue;
8306                         }
8307
8308                       if (expecting_tls_get_addr != 1)
8309                         continue;
8310
8311                       /* Uh oh, we didn't find the expected call.  We
8312                          could just mark this symbol to exclude it
8313                          from tls optimization but it's safer to skip
8314                          the entire optimization.  */
8315                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8316                                                 "TLS optimization disabled\n"),
8317                                               ibfd, sec, rel->r_offset);
8318                       ret = TRUE;
8319                       goto err_free_rel;
8320                     }
8321
8322                   if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8323                     {
8324                       struct plt_entry *ent;
8325                       for (ent = htab->tls_get_addr->elf.plt.plist;
8326                            ent != NULL;
8327                            ent = ent->next)
8328                         if (ent->addend == 0)
8329                           {
8330                             if (ent->plt.refcount > 0)
8331                               {
8332                                 ent->plt.refcount -= 1;
8333                                 expecting_tls_get_addr = 0;
8334                               }
8335                             break;
8336                           }
8337                     }
8338
8339                   if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8340                     {
8341                       struct plt_entry *ent;
8342                       for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8343                            ent != NULL;
8344                            ent = ent->next)
8345                         if (ent->addend == 0)
8346                           {
8347                             if (ent->plt.refcount > 0)
8348                               ent->plt.refcount -= 1;
8349                             break;
8350                           }
8351                     }
8352
8353                   if (tls_clear == 0)
8354                     continue;
8355
8356                   if ((tls_set & TLS_EXPLICIT) == 0)
8357                     {
8358                       struct got_entry *ent;
8359
8360                       /* Adjust got entry for this reloc.  */
8361                       if (h != NULL)
8362                         ent = h->got.glist;
8363                       else
8364                         ent = elf_local_got_ents (ibfd)[r_symndx];
8365
8366                       for (; ent != NULL; ent = ent->next)
8367                         if (ent->addend == rel->r_addend
8368                             && ent->owner == ibfd
8369                             && ent->tls_type == tls_type)
8370                           break;
8371                       if (ent == NULL)
8372                         abort ();
8373
8374                       if (tls_set == 0)
8375                         {
8376                           /* We managed to get rid of a got entry.  */
8377                           if (ent->got.refcount > 0)
8378                             ent->got.refcount -= 1;
8379                         }
8380                     }
8381                   else
8382                     {
8383                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
8384                          we'll lose one or two dyn relocs.  */
8385                       if (!dec_dynrel_count (rel->r_info, sec, info,
8386                                              NULL, h, sym))
8387                         return FALSE;
8388
8389                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
8390                         {
8391                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8392                                                  NULL, h, sym))
8393                             return FALSE;
8394                         }
8395                     }
8396
8397                   *tls_mask |= tls_set;
8398                   *tls_mask &= ~tls_clear;
8399                 }
8400
8401               if (elf_section_data (sec)->relocs != relstart)
8402                 free (relstart);
8403             }
8404
8405         if (locsyms != NULL
8406             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8407           {
8408             if (!info->keep_memory)
8409               free (locsyms);
8410             else
8411               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8412           }
8413       }
8414
8415   if (toc_ref != NULL)
8416     free (toc_ref);
8417   return TRUE;
8418 }
8419
8420 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8421    the values of any global symbols in a toc section that has been
8422    edited.  Globals in toc sections should be a rarity, so this function
8423    sets a flag if any are found in toc sections other than the one just
8424    edited, so that futher hash table traversals can be avoided.  */
8425
8426 struct adjust_toc_info
8427 {
8428   asection *toc;
8429   unsigned long *skip;
8430   bfd_boolean global_toc_syms;
8431 };
8432
8433 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8434
8435 static bfd_boolean
8436 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8437 {
8438   struct ppc_link_hash_entry *eh;
8439   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8440   unsigned long i;
8441
8442   if (h->root.type != bfd_link_hash_defined
8443       && h->root.type != bfd_link_hash_defweak)
8444     return TRUE;
8445
8446   eh = (struct ppc_link_hash_entry *) h;
8447   if (eh->adjust_done)
8448     return TRUE;
8449
8450   if (eh->elf.root.u.def.section == toc_inf->toc)
8451     {
8452       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8453         i = toc_inf->toc->rawsize >> 3;
8454       else
8455         i = eh->elf.root.u.def.value >> 3;
8456
8457       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8458         {
8459           (*_bfd_error_handler)
8460             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8461           do
8462             ++i;
8463           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8464           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8465         }
8466
8467       eh->elf.root.u.def.value -= toc_inf->skip[i];
8468       eh->adjust_done = 1;
8469     }
8470   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8471     toc_inf->global_toc_syms = TRUE;
8472
8473   return TRUE;
8474 }
8475
8476 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc.  */
8477
8478 static bfd_boolean
8479 ok_lo_toc_insn (unsigned int insn)
8480 {
8481   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8482           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8483           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8484           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8485           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8486           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8487           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8488           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8489           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8490           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8491           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8492           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8493           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8494           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8495           || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8496               && (insn & 3) != 1)
8497           || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8498               && ((insn & 3) == 0 || (insn & 3) == 3))
8499           || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8500 }
8501
8502 /* Examine all relocs referencing .toc sections in order to remove
8503    unused .toc entries.  */
8504
8505 bfd_boolean
8506 ppc64_elf_edit_toc (struct bfd_link_info *info)
8507 {
8508   bfd *ibfd;
8509   struct adjust_toc_info toc_inf;
8510   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8511
8512   htab->do_toc_opt = 1;
8513   toc_inf.global_toc_syms = TRUE;
8514   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8515     {
8516       asection *toc, *sec;
8517       Elf_Internal_Shdr *symtab_hdr;
8518       Elf_Internal_Sym *local_syms;
8519       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8520       unsigned long *skip, *drop;
8521       unsigned char *used;
8522       unsigned char *keep, last, some_unused;
8523
8524       if (!is_ppc64_elf (ibfd))
8525         continue;
8526
8527       toc = bfd_get_section_by_name (ibfd, ".toc");
8528       if (toc == NULL
8529           || toc->size == 0
8530           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8531           || discarded_section (toc))
8532         continue;
8533
8534       toc_relocs = NULL;
8535       local_syms = NULL;
8536       symtab_hdr = &elf_symtab_hdr (ibfd);
8537
8538       /* Look at sections dropped from the final link.  */
8539       skip = NULL;
8540       relstart = NULL;
8541       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8542         {
8543           if (sec->reloc_count == 0
8544               || !discarded_section (sec)
8545               || get_opd_info (sec)
8546               || (sec->flags & SEC_ALLOC) == 0
8547               || (sec->flags & SEC_DEBUGGING) != 0)
8548             continue;
8549
8550           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8551           if (relstart == NULL)
8552             goto error_ret;
8553
8554           /* Run through the relocs to see which toc entries might be
8555              unused.  */
8556           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8557             {
8558               enum elf_ppc64_reloc_type r_type;
8559               unsigned long r_symndx;
8560               asection *sym_sec;
8561               struct elf_link_hash_entry *h;
8562               Elf_Internal_Sym *sym;
8563               bfd_vma val;
8564
8565               r_type = ELF64_R_TYPE (rel->r_info);
8566               switch (r_type)
8567                 {
8568                 default:
8569                   continue;
8570
8571                 case R_PPC64_TOC16:
8572                 case R_PPC64_TOC16_LO:
8573                 case R_PPC64_TOC16_HI:
8574                 case R_PPC64_TOC16_HA:
8575                 case R_PPC64_TOC16_DS:
8576                 case R_PPC64_TOC16_LO_DS:
8577                   break;
8578                 }
8579
8580               r_symndx = ELF64_R_SYM (rel->r_info);
8581               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8582                               r_symndx, ibfd))
8583                 goto error_ret;
8584
8585               if (sym_sec != toc)
8586                 continue;
8587
8588               if (h != NULL)
8589                 val = h->root.u.def.value;
8590               else
8591                 val = sym->st_value;
8592               val += rel->r_addend;
8593
8594               if (val >= toc->size)
8595                 continue;
8596
8597               /* Anything in the toc ought to be aligned to 8 bytes.
8598                  If not, don't mark as unused.  */
8599               if (val & 7)
8600                 continue;
8601
8602               if (skip == NULL)
8603                 {
8604                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8605                   if (skip == NULL)
8606                     goto error_ret;
8607                 }
8608
8609               skip[val >> 3] = ref_from_discarded;
8610             }
8611
8612           if (elf_section_data (sec)->relocs != relstart)
8613             free (relstart);
8614         }
8615
8616       /* For largetoc loads of address constants, we can convert
8617          .  addis rx,2,addr@got@ha
8618          .  ld ry,addr@got@l(rx)
8619          to
8620          .  addis rx,2,addr@toc@ha
8621          .  addi ry,rx,addr@toc@l
8622          when addr is within 2G of the toc pointer.  This then means
8623          that the word storing "addr" in the toc is no longer needed.  */
8624
8625       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8626           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8627           && toc->reloc_count != 0)
8628         {
8629           /* Read toc relocs.  */
8630           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8631                                                   info->keep_memory);
8632           if (toc_relocs == NULL)
8633             goto error_ret;
8634
8635           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8636             {
8637               enum elf_ppc64_reloc_type r_type;
8638               unsigned long r_symndx;
8639               asection *sym_sec;
8640               struct elf_link_hash_entry *h;
8641               Elf_Internal_Sym *sym;
8642               bfd_vma val, addr;
8643
8644               r_type = ELF64_R_TYPE (rel->r_info);
8645               if (r_type != R_PPC64_ADDR64)
8646                 continue;
8647
8648               r_symndx = ELF64_R_SYM (rel->r_info);
8649               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8650                               r_symndx, ibfd))
8651                 goto error_ret;
8652
8653               if (sym_sec == NULL
8654                   || discarded_section (sym_sec))
8655                 continue;
8656
8657               if (!SYMBOL_CALLS_LOCAL (info, h))
8658                 continue;
8659
8660               if (h != NULL)
8661                 {
8662                   if (h->type == STT_GNU_IFUNC)
8663                     continue;
8664                   val = h->root.u.def.value;
8665                 }
8666               else
8667                 {
8668                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8669                     continue;
8670                   val = sym->st_value;
8671                 }
8672               val += rel->r_addend;
8673               val += sym_sec->output_section->vma + sym_sec->output_offset;
8674
8675               /* We don't yet know the exact toc pointer value, but we
8676                  know it will be somewhere in the toc section.  Don't
8677                  optimize if the difference from any possible toc
8678                  pointer is outside [ff..f80008000, 7fff7fff].  */
8679               addr = toc->output_section->vma + TOC_BASE_OFF;
8680               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8681                 continue;
8682
8683               addr = toc->output_section->vma + toc->output_section->rawsize;
8684               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8685                 continue;
8686
8687               if (skip == NULL)
8688                 {
8689                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8690                   if (skip == NULL)
8691                     goto error_ret;
8692                 }
8693
8694               skip[rel->r_offset >> 3]
8695                 |= can_optimize | ((rel - toc_relocs) << 2);
8696             }
8697         }
8698
8699       if (skip == NULL)
8700         continue;
8701
8702       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8703       if (used == NULL)
8704         {
8705         error_ret:
8706           if (local_syms != NULL
8707               && symtab_hdr->contents != (unsigned char *) local_syms)
8708             free (local_syms);
8709           if (sec != NULL
8710               && relstart != NULL
8711               && elf_section_data (sec)->relocs != relstart)
8712             free (relstart);
8713           if (toc_relocs != NULL
8714               && elf_section_data (toc)->relocs != toc_relocs)
8715             free (toc_relocs);
8716           if (skip != NULL)
8717             free (skip);
8718           return FALSE;
8719         }
8720
8721       /* Now check all kept sections that might reference the toc.
8722          Check the toc itself last.  */
8723       for (sec = (ibfd->sections == toc && toc->next ? toc->next
8724                   : ibfd->sections);
8725            sec != NULL;
8726            sec = (sec == toc ? NULL
8727                   : sec->next == NULL ? toc
8728                   : sec->next == toc && toc->next ? toc->next
8729                   : sec->next))
8730         {
8731           int repeat;
8732
8733           if (sec->reloc_count == 0
8734               || discarded_section (sec)
8735               || get_opd_info (sec)
8736               || (sec->flags & SEC_ALLOC) == 0
8737               || (sec->flags & SEC_DEBUGGING) != 0)
8738             continue;
8739
8740           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8741                                                 info->keep_memory);
8742           if (relstart == NULL)
8743             goto error_ret;
8744
8745           /* Mark toc entries referenced as used.  */
8746           do
8747             {
8748               repeat = 0;
8749               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8750                 {
8751                   enum elf_ppc64_reloc_type r_type;
8752                   unsigned long r_symndx;
8753                   asection *sym_sec;
8754                   struct elf_link_hash_entry *h;
8755                   Elf_Internal_Sym *sym;
8756                   bfd_vma val;
8757                   enum {no_check, check_lo, check_ha} insn_check;
8758
8759                   r_type = ELF64_R_TYPE (rel->r_info);
8760                   switch (r_type)
8761                     {
8762                     default:
8763                       insn_check = no_check;
8764                       break;
8765
8766                     case R_PPC64_GOT_TLSLD16_HA:
8767                     case R_PPC64_GOT_TLSGD16_HA:
8768                     case R_PPC64_GOT_TPREL16_HA:
8769                     case R_PPC64_GOT_DTPREL16_HA:
8770                     case R_PPC64_GOT16_HA:
8771                     case R_PPC64_TOC16_HA:
8772                       insn_check = check_ha;
8773                       break;
8774
8775                     case R_PPC64_GOT_TLSLD16_LO:
8776                     case R_PPC64_GOT_TLSGD16_LO:
8777                     case R_PPC64_GOT_TPREL16_LO_DS:
8778                     case R_PPC64_GOT_DTPREL16_LO_DS:
8779                     case R_PPC64_GOT16_LO:
8780                     case R_PPC64_GOT16_LO_DS:
8781                     case R_PPC64_TOC16_LO:
8782                     case R_PPC64_TOC16_LO_DS:
8783                       insn_check = check_lo;
8784                       break;
8785                     }
8786
8787                   if (insn_check != no_check)
8788                     {
8789                       bfd_vma off = rel->r_offset & ~3;
8790                       unsigned char buf[4];
8791                       unsigned int insn;
8792
8793                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8794                         {
8795                           free (used);
8796                           goto error_ret;
8797                         }
8798                       insn = bfd_get_32 (ibfd, buf);
8799                       if (insn_check == check_lo
8800                           ? !ok_lo_toc_insn (insn)
8801                           : ((insn & ((0x3f << 26) | 0x1f << 16))
8802                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8803                         {
8804                           char str[12];
8805
8806                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8807                           sprintf (str, "%#08x", insn);
8808                           info->callbacks->einfo
8809                             (_("%P: %H: toc optimization is not supported for"
8810                                " %s instruction.\n"),
8811                              ibfd, sec, rel->r_offset & ~3, str);
8812                         }
8813                     }
8814
8815                   switch (r_type)
8816                     {
8817                     case R_PPC64_TOC16:
8818                     case R_PPC64_TOC16_LO:
8819                     case R_PPC64_TOC16_HI:
8820                     case R_PPC64_TOC16_HA:
8821                     case R_PPC64_TOC16_DS:
8822                     case R_PPC64_TOC16_LO_DS:
8823                       /* In case we're taking addresses of toc entries.  */
8824                     case R_PPC64_ADDR64:
8825                       break;
8826
8827                     default:
8828                       continue;
8829                     }
8830
8831                   r_symndx = ELF64_R_SYM (rel->r_info);
8832                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8833                                   r_symndx, ibfd))
8834                     {
8835                       free (used);
8836                       goto error_ret;
8837                     }
8838
8839                   if (sym_sec != toc)
8840                     continue;
8841
8842                   if (h != NULL)
8843                     val = h->root.u.def.value;
8844                   else
8845                     val = sym->st_value;
8846                   val += rel->r_addend;
8847
8848                   if (val >= toc->size)
8849                     continue;
8850
8851                   if ((skip[val >> 3] & can_optimize) != 0)
8852                     {
8853                       bfd_vma off;
8854                       unsigned char opc;
8855
8856                       switch (r_type)
8857                         {
8858                         case R_PPC64_TOC16_HA:
8859                           break;
8860
8861                         case R_PPC64_TOC16_LO_DS:
8862                           off = rel->r_offset;
8863                           off += (bfd_big_endian (ibfd) ? -2 : 3);
8864                           if (!bfd_get_section_contents (ibfd, sec, &opc,
8865                                                          off, 1))
8866                             {
8867                               free (used);
8868                               goto error_ret;
8869                             }
8870                           if ((opc & (0x3f << 2)) == (58u << 2))
8871                             break;
8872                           /* Fall thru */
8873
8874                         default:
8875                           /* Wrong sort of reloc, or not a ld.  We may
8876                              as well clear ref_from_discarded too.  */
8877                           skip[val >> 3] = 0;
8878                         }
8879                     }
8880
8881                   if (sec != toc)
8882                     used[val >> 3] = 1;
8883                   /* For the toc section, we only mark as used if this
8884                      entry itself isn't unused.  */
8885                   else if ((used[rel->r_offset >> 3]
8886                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8887                            && !used[val >> 3])
8888                     {
8889                       /* Do all the relocs again, to catch reference
8890                          chains.  */
8891                       repeat = 1;
8892                       used[val >> 3] = 1;
8893                     }
8894                 }
8895             }
8896           while (repeat);
8897
8898           if (elf_section_data (sec)->relocs != relstart)
8899             free (relstart);
8900         }
8901
8902       /* Merge the used and skip arrays.  Assume that TOC
8903          doublewords not appearing as either used or unused belong
8904          to to an entry more than one doubleword in size.  */
8905       for (drop = skip, keep = used, last = 0, some_unused = 0;
8906            drop < skip + (toc->size + 7) / 8;
8907            ++drop, ++keep)
8908         {
8909           if (*keep)
8910             {
8911               *drop &= ~ref_from_discarded;
8912               if ((*drop & can_optimize) != 0)
8913                 some_unused = 1;
8914               last = 0;
8915             }
8916           else if ((*drop & ref_from_discarded) != 0)
8917             {
8918               some_unused = 1;
8919               last = ref_from_discarded;
8920             }
8921           else
8922             *drop = last;
8923         }
8924
8925       free (used);
8926
8927       if (some_unused)
8928         {
8929           bfd_byte *contents, *src;
8930           unsigned long off;
8931           Elf_Internal_Sym *sym;
8932           bfd_boolean local_toc_syms = FALSE;
8933
8934           /* Shuffle the toc contents, and at the same time convert the
8935              skip array from booleans into offsets.  */
8936           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8937             goto error_ret;
8938
8939           elf_section_data (toc)->this_hdr.contents = contents;
8940
8941           for (src = contents, off = 0, drop = skip;
8942                src < contents + toc->size;
8943                src += 8, ++drop)
8944             {
8945               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8946                 off += 8;
8947               else if (off != 0)
8948                 {
8949                   *drop = off;
8950                   memcpy (src - off, src, 8);
8951                 }
8952             }
8953           *drop = off;
8954           toc->rawsize = toc->size;
8955           toc->size = src - contents - off;
8956
8957           /* Adjust addends for relocs against the toc section sym,
8958              and optimize any accesses we can.  */
8959           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8960             {
8961               if (sec->reloc_count == 0
8962                   || discarded_section (sec))
8963                 continue;
8964
8965               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8966                                                     info->keep_memory);
8967               if (relstart == NULL)
8968                 goto error_ret;
8969
8970               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8971                 {
8972                   enum elf_ppc64_reloc_type r_type;
8973                   unsigned long r_symndx;
8974                   asection *sym_sec;
8975                   struct elf_link_hash_entry *h;
8976                   bfd_vma val;
8977
8978                   r_type = ELF64_R_TYPE (rel->r_info);
8979                   switch (r_type)
8980                     {
8981                     default:
8982                       continue;
8983
8984                     case R_PPC64_TOC16:
8985                     case R_PPC64_TOC16_LO:
8986                     case R_PPC64_TOC16_HI:
8987                     case R_PPC64_TOC16_HA:
8988                     case R_PPC64_TOC16_DS:
8989                     case R_PPC64_TOC16_LO_DS:
8990                     case R_PPC64_ADDR64:
8991                       break;
8992                     }
8993
8994                   r_symndx = ELF64_R_SYM (rel->r_info);
8995                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8996                                   r_symndx, ibfd))
8997                     goto error_ret;
8998
8999                   if (sym_sec != toc)
9000                     continue;
9001
9002                   if (h != NULL)
9003                     val = h->root.u.def.value;
9004                   else
9005                     {
9006                       val = sym->st_value;
9007                       if (val != 0)
9008                         local_toc_syms = TRUE;
9009                     }
9010
9011                   val += rel->r_addend;
9012
9013                   if (val > toc->rawsize)
9014                     val = toc->rawsize;
9015                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
9016                     continue;
9017                   else if ((skip[val >> 3] & can_optimize) != 0)
9018                     {
9019                       Elf_Internal_Rela *tocrel
9020                         = toc_relocs + (skip[val >> 3] >> 2);
9021                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9022
9023                       switch (r_type)
9024                         {
9025                         case R_PPC64_TOC16_HA:
9026                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9027                           break;
9028
9029                         case R_PPC64_TOC16_LO_DS:
9030                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9031                           break;
9032
9033                         default:
9034                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9035                             ppc_howto_init ();
9036                           info->callbacks->einfo
9037                             (_("%P: %H: %s references "
9038                                "optimized away TOC entry\n"),
9039                              ibfd, sec, rel->r_offset,
9040                              ppc64_elf_howto_table[r_type]->name);
9041                           bfd_set_error (bfd_error_bad_value);
9042                           goto error_ret;
9043                         }
9044                       rel->r_addend = tocrel->r_addend;
9045                       elf_section_data (sec)->relocs = relstart;
9046                       continue;
9047                     }
9048
9049                   if (h != NULL || sym->st_value != 0)
9050                     continue;
9051
9052                   rel->r_addend -= skip[val >> 3];
9053                   elf_section_data (sec)->relocs = relstart;
9054                 }
9055
9056               if (elf_section_data (sec)->relocs != relstart)
9057                 free (relstart);
9058             }
9059
9060           /* We shouldn't have local or global symbols defined in the TOC,
9061              but handle them anyway.  */
9062           if (local_syms != NULL)
9063             for (sym = local_syms;
9064                  sym < local_syms + symtab_hdr->sh_info;
9065                  ++sym)
9066               if (sym->st_value != 0
9067                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9068                 {
9069                   unsigned long i;
9070
9071                   if (sym->st_value > toc->rawsize)
9072                     i = toc->rawsize >> 3;
9073                   else
9074                     i = sym->st_value >> 3;
9075
9076                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9077                     {
9078                       if (local_toc_syms)
9079                         (*_bfd_error_handler)
9080                           (_("%s defined on removed toc entry"),
9081                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9082                       do
9083                         ++i;
9084                       while ((skip[i] & (ref_from_discarded | can_optimize)));
9085                       sym->st_value = (bfd_vma) i << 3;
9086                     }
9087
9088                   sym->st_value -= skip[i];
9089                   symtab_hdr->contents = (unsigned char *) local_syms;
9090                 }
9091
9092           /* Adjust any global syms defined in this toc input section.  */
9093           if (toc_inf.global_toc_syms)
9094             {
9095               toc_inf.toc = toc;
9096               toc_inf.skip = skip;
9097               toc_inf.global_toc_syms = FALSE;
9098               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9099                                       &toc_inf);
9100             }
9101
9102           if (toc->reloc_count != 0)
9103             {
9104               Elf_Internal_Shdr *rel_hdr;
9105               Elf_Internal_Rela *wrel;
9106               bfd_size_type sz;
9107
9108               /* Remove unused toc relocs, and adjust those we keep.  */
9109               if (toc_relocs == NULL)
9110                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9111                                                         info->keep_memory);
9112               if (toc_relocs == NULL)
9113                 goto error_ret;
9114
9115               wrel = toc_relocs;
9116               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9117                 if ((skip[rel->r_offset >> 3]
9118                      & (ref_from_discarded | can_optimize)) == 0)
9119                   {
9120                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9121                     wrel->r_info = rel->r_info;
9122                     wrel->r_addend = rel->r_addend;
9123                     ++wrel;
9124                   }
9125                 else if (!dec_dynrel_count (rel->r_info, toc, info,
9126                                             &local_syms, NULL, NULL))
9127                   goto error_ret;
9128
9129               elf_section_data (toc)->relocs = toc_relocs;
9130               toc->reloc_count = wrel - toc_relocs;
9131               rel_hdr = _bfd_elf_single_rel_hdr (toc);
9132               sz = rel_hdr->sh_entsize;
9133               rel_hdr->sh_size = toc->reloc_count * sz;
9134             }
9135         }
9136       else if (toc_relocs != NULL
9137                && elf_section_data (toc)->relocs != toc_relocs)
9138         free (toc_relocs);
9139
9140       if (local_syms != NULL
9141           && symtab_hdr->contents != (unsigned char *) local_syms)
9142         {
9143           if (!info->keep_memory)
9144             free (local_syms);
9145           else
9146             symtab_hdr->contents = (unsigned char *) local_syms;
9147         }
9148       free (skip);
9149     }
9150
9151   return TRUE;
9152 }
9153
9154 /* Return true iff input section I references the TOC using
9155    instructions limited to +/-32k offsets.  */
9156
9157 bfd_boolean
9158 ppc64_elf_has_small_toc_reloc (asection *i)
9159 {
9160   return (is_ppc64_elf (i->owner)
9161           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9162 }
9163
9164 /* Allocate space for one GOT entry.  */
9165
9166 static void
9167 allocate_got (struct elf_link_hash_entry *h,
9168               struct bfd_link_info *info,
9169               struct got_entry *gent)
9170 {
9171   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9172   bfd_boolean dyn;
9173   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9174   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9175                  ? 16 : 8);
9176   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9177                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9178   asection *got = ppc64_elf_tdata (gent->owner)->got;
9179
9180   gent->got.offset = got->size;
9181   got->size += entsize;
9182
9183   dyn = htab->elf.dynamic_sections_created;
9184   if (h->type == STT_GNU_IFUNC)
9185     {
9186       htab->reliplt->size += rentsize;
9187       htab->got_reli_size += rentsize;
9188     }
9189   else if ((info->shared
9190             || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9191            && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9192                || h->root.type != bfd_link_hash_undefweak))
9193     {
9194       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9195       relgot->size += rentsize;
9196     }
9197 }
9198
9199 /* This function merges got entries in the same toc group.  */
9200
9201 static void
9202 merge_got_entries (struct got_entry **pent)
9203 {
9204   struct got_entry *ent, *ent2;
9205
9206   for (ent = *pent; ent != NULL; ent = ent->next)
9207     if (!ent->is_indirect)
9208       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9209         if (!ent2->is_indirect
9210             && ent2->addend == ent->addend
9211             && ent2->tls_type == ent->tls_type
9212             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9213           {
9214             ent2->is_indirect = TRUE;
9215             ent2->got.ent = ent;
9216           }
9217 }
9218
9219 /* Allocate space in .plt, .got and associated reloc sections for
9220    dynamic relocs.  */
9221
9222 static bfd_boolean
9223 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9224 {
9225   struct bfd_link_info *info;
9226   struct ppc_link_hash_table *htab;
9227   asection *s;
9228   struct ppc_link_hash_entry *eh;
9229   struct elf_dyn_relocs *p;
9230   struct got_entry **pgent, *gent;
9231
9232   if (h->root.type == bfd_link_hash_indirect)
9233     return TRUE;
9234
9235   info = (struct bfd_link_info *) inf;
9236   htab = ppc_hash_table (info);
9237   if (htab == NULL)
9238     return FALSE;
9239
9240   if ((htab->elf.dynamic_sections_created
9241        && h->dynindx != -1
9242        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
9243       || h->type == STT_GNU_IFUNC)
9244     {
9245       struct plt_entry *pent;
9246       bfd_boolean doneone = FALSE;
9247       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9248         if (pent->plt.refcount > 0)
9249           {
9250             if (!htab->elf.dynamic_sections_created
9251                 || h->dynindx == -1)
9252               {
9253                 s = htab->iplt;
9254                 pent->plt.offset = s->size;
9255                 s->size += PLT_ENTRY_SIZE (htab);
9256                 s = htab->reliplt;
9257               }
9258             else
9259               {
9260                 /* If this is the first .plt entry, make room for the special
9261                    first entry.  */
9262                 s = htab->plt;
9263                 if (s->size == 0)
9264                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9265
9266                 pent->plt.offset = s->size;
9267
9268                 /* Make room for this entry.  */
9269                 s->size += PLT_ENTRY_SIZE (htab);
9270
9271                 /* Make room for the .glink code.  */
9272                 s = htab->glink;
9273                 if (s->size == 0)
9274                   s->size += GLINK_CALL_STUB_SIZE;
9275                 if (htab->opd_abi)
9276                   {
9277                     /* We need bigger stubs past index 32767.  */
9278                     if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9279                       s->size += 4;
9280                     s->size += 2*4;
9281                   }
9282                 else
9283                   s->size += 4;
9284
9285                 /* We also need to make an entry in the .rela.plt section.  */
9286                 s = htab->relplt;
9287               }
9288             s->size += sizeof (Elf64_External_Rela);
9289             doneone = TRUE;
9290           }
9291         else
9292           pent->plt.offset = (bfd_vma) -1;
9293       if (!doneone)
9294         {
9295           h->plt.plist = NULL;
9296           h->needs_plt = 0;
9297         }
9298     }
9299   else
9300     {
9301       h->plt.plist = NULL;
9302       h->needs_plt = 0;
9303     }
9304
9305   eh = (struct ppc_link_hash_entry *) h;
9306   /* Run through the TLS GD got entries first if we're changing them
9307      to TPREL.  */
9308   if ((eh->tls_mask & TLS_TPRELGD) != 0)
9309     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9310       if (gent->got.refcount > 0
9311           && (gent->tls_type & TLS_GD) != 0)
9312         {
9313           /* This was a GD entry that has been converted to TPREL.  If
9314              there happens to be a TPREL entry we can use that one.  */
9315           struct got_entry *ent;
9316           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9317             if (ent->got.refcount > 0
9318                 && (ent->tls_type & TLS_TPREL) != 0
9319                 && ent->addend == gent->addend
9320                 && ent->owner == gent->owner)
9321               {
9322                 gent->got.refcount = 0;
9323                 break;
9324               }
9325
9326           /* If not, then we'll be using our own TPREL entry.  */
9327           if (gent->got.refcount != 0)
9328             gent->tls_type = TLS_TLS | TLS_TPREL;
9329         }
9330
9331   /* Remove any list entry that won't generate a word in the GOT before
9332      we call merge_got_entries.  Otherwise we risk merging to empty
9333      entries.  */
9334   pgent = &h->got.glist;
9335   while ((gent = *pgent) != NULL)
9336     if (gent->got.refcount > 0)
9337       {
9338         if ((gent->tls_type & TLS_LD) != 0
9339             && !h->def_dynamic)
9340           {
9341             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9342             *pgent = gent->next;
9343           }
9344         else
9345           pgent = &gent->next;
9346       }
9347     else
9348       *pgent = gent->next;
9349
9350   if (!htab->do_multi_toc)
9351     merge_got_entries (&h->got.glist);
9352
9353   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9354     if (!gent->is_indirect)
9355       {
9356         /* Make sure this symbol is output as a dynamic symbol.
9357            Undefined weak syms won't yet be marked as dynamic,
9358            nor will all TLS symbols.  */
9359         if (h->dynindx == -1
9360             && !h->forced_local
9361             && h->type != STT_GNU_IFUNC
9362             && htab->elf.dynamic_sections_created)
9363           {
9364             if (! bfd_elf_link_record_dynamic_symbol (info, h))
9365               return FALSE;
9366           }
9367
9368         if (!is_ppc64_elf (gent->owner))
9369           abort ();
9370
9371         allocate_got (h, info, gent);
9372       }
9373
9374   if (eh->dyn_relocs == NULL
9375       || (!htab->elf.dynamic_sections_created
9376           && h->type != STT_GNU_IFUNC))
9377     return TRUE;
9378
9379   /* In the shared -Bsymbolic case, discard space allocated for
9380      dynamic pc-relative relocs against symbols which turn out to be
9381      defined in regular objects.  For the normal shared case, discard
9382      space for relocs that have become local due to symbol visibility
9383      changes.  */
9384
9385   if (info->shared)
9386     {
9387       /* Relocs that use pc_count are those that appear on a call insn,
9388          or certain REL relocs (see must_be_dyn_reloc) that can be
9389          generated via assembly.  We want calls to protected symbols to
9390          resolve directly to the function rather than going via the plt.
9391          If people want function pointer comparisons to work as expected
9392          then they should avoid writing weird assembly.  */
9393       if (SYMBOL_CALLS_LOCAL (info, h))
9394         {
9395           struct elf_dyn_relocs **pp;
9396
9397           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9398             {
9399               p->count -= p->pc_count;
9400               p->pc_count = 0;
9401               if (p->count == 0)
9402                 *pp = p->next;
9403               else
9404                 pp = &p->next;
9405             }
9406         }
9407
9408       /* Also discard relocs on undefined weak syms with non-default
9409          visibility.  */
9410       if (eh->dyn_relocs != NULL
9411           && h->root.type == bfd_link_hash_undefweak)
9412         {
9413           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9414             eh->dyn_relocs = NULL;
9415
9416           /* Make sure this symbol is output as a dynamic symbol.
9417              Undefined weak syms won't yet be marked as dynamic.  */
9418           else if (h->dynindx == -1
9419                    && !h->forced_local)
9420             {
9421               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9422                 return FALSE;
9423             }
9424         }
9425     }
9426   else if (h->type == STT_GNU_IFUNC)
9427     {
9428       if (!h->non_got_ref)
9429         eh->dyn_relocs = NULL;
9430     }
9431   else if (ELIMINATE_COPY_RELOCS)
9432     {
9433       /* For the non-shared case, discard space for relocs against
9434          symbols which turn out to need copy relocs or are not
9435          dynamic.  */
9436
9437       if (!h->non_got_ref
9438           && !h->def_regular)
9439         {
9440           /* Make sure this symbol is output as a dynamic symbol.
9441              Undefined weak syms won't yet be marked as dynamic.  */
9442           if (h->dynindx == -1
9443               && !h->forced_local)
9444             {
9445               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9446                 return FALSE;
9447             }
9448
9449           /* If that succeeded, we know we'll be keeping all the
9450              relocs.  */
9451           if (h->dynindx != -1)
9452             goto keep;
9453         }
9454
9455       eh->dyn_relocs = NULL;
9456
9457     keep: ;
9458     }
9459
9460   /* Finally, allocate space.  */
9461   for (p = eh->dyn_relocs; p != NULL; p = p->next)
9462     {
9463       asection *sreloc = elf_section_data (p->sec)->sreloc;
9464       if (eh->elf.type == STT_GNU_IFUNC)
9465         sreloc = htab->reliplt;
9466       sreloc->size += p->count * sizeof (Elf64_External_Rela);
9467     }
9468
9469   return TRUE;
9470 }
9471
9472 /* Find any dynamic relocs that apply to read-only sections.  */
9473
9474 static bfd_boolean
9475 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9476 {
9477   struct ppc_link_hash_entry *eh;
9478   struct elf_dyn_relocs *p;
9479
9480   eh = (struct ppc_link_hash_entry *) h;
9481   for (p = eh->dyn_relocs; p != NULL; p = p->next)
9482     {
9483       asection *s = p->sec->output_section;
9484
9485       if (s != NULL && (s->flags & SEC_READONLY) != 0)
9486         {
9487           struct bfd_link_info *info = inf;
9488
9489           info->flags |= DF_TEXTREL;
9490
9491           /* Not an error, just cut short the traversal.  */
9492           return FALSE;
9493         }
9494     }
9495   return TRUE;
9496 }
9497
9498 /* Set the sizes of the dynamic sections.  */
9499
9500 static bfd_boolean
9501 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9502                                  struct bfd_link_info *info)
9503 {
9504   struct ppc_link_hash_table *htab;
9505   bfd *dynobj;
9506   asection *s;
9507   bfd_boolean relocs;
9508   bfd *ibfd;
9509   struct got_entry *first_tlsld;
9510
9511   htab = ppc_hash_table (info);
9512   if (htab == NULL)
9513     return FALSE;
9514
9515   dynobj = htab->elf.dynobj;
9516   if (dynobj == NULL)
9517     abort ();
9518
9519   if (htab->elf.dynamic_sections_created)
9520     {
9521       /* Set the contents of the .interp section to the interpreter.  */
9522       if (info->executable)
9523         {
9524           s = bfd_get_linker_section (dynobj, ".interp");
9525           if (s == NULL)
9526             abort ();
9527           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9528           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9529         }
9530     }
9531
9532   /* Set up .got offsets for local syms, and space for local dynamic
9533      relocs.  */
9534   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9535     {
9536       struct got_entry **lgot_ents;
9537       struct got_entry **end_lgot_ents;
9538       struct plt_entry **local_plt;
9539       struct plt_entry **end_local_plt;
9540       unsigned char *lgot_masks;
9541       bfd_size_type locsymcount;
9542       Elf_Internal_Shdr *symtab_hdr;
9543
9544       if (!is_ppc64_elf (ibfd))
9545         continue;
9546
9547       for (s = ibfd->sections; s != NULL; s = s->next)
9548         {
9549           struct ppc_dyn_relocs *p;
9550
9551           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9552             {
9553               if (!bfd_is_abs_section (p->sec)
9554                   && bfd_is_abs_section (p->sec->output_section))
9555                 {
9556                   /* Input section has been discarded, either because
9557                      it is a copy of a linkonce section or due to
9558                      linker script /DISCARD/, so we'll be discarding
9559                      the relocs too.  */
9560                 }
9561               else if (p->count != 0)
9562                 {
9563                   asection *srel = elf_section_data (p->sec)->sreloc;
9564                   if (p->ifunc)
9565                     srel = htab->reliplt;
9566                   srel->size += p->count * sizeof (Elf64_External_Rela);
9567                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9568                     info->flags |= DF_TEXTREL;
9569                 }
9570             }
9571         }
9572
9573       lgot_ents = elf_local_got_ents (ibfd);
9574       if (!lgot_ents)
9575         continue;
9576
9577       symtab_hdr = &elf_symtab_hdr (ibfd);
9578       locsymcount = symtab_hdr->sh_info;
9579       end_lgot_ents = lgot_ents + locsymcount;
9580       local_plt = (struct plt_entry **) end_lgot_ents;
9581       end_local_plt = local_plt + locsymcount;
9582       lgot_masks = (unsigned char *) end_local_plt;
9583       s = ppc64_elf_tdata (ibfd)->got;
9584       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9585         {
9586           struct got_entry **pent, *ent;
9587
9588           pent = lgot_ents;
9589           while ((ent = *pent) != NULL)
9590             if (ent->got.refcount > 0)
9591               {
9592                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9593                   {
9594                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
9595                     *pent = ent->next;
9596                   }
9597                 else
9598                   {
9599                     unsigned int ent_size = 8;
9600                     unsigned int rel_size = sizeof (Elf64_External_Rela);
9601
9602                     ent->got.offset = s->size;
9603                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9604                       {
9605                         ent_size *= 2;
9606                         rel_size *= 2;
9607                       }
9608                     s->size += ent_size;
9609                     if ((*lgot_masks & PLT_IFUNC) != 0)
9610                       {
9611                         htab->reliplt->size += rel_size;
9612                         htab->got_reli_size += rel_size;
9613                       }
9614                     else if (info->shared)
9615                       {
9616                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9617                         srel->size += rel_size;
9618                       }
9619                     pent = &ent->next;
9620                   }
9621               }
9622             else
9623               *pent = ent->next;
9624         }
9625
9626       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
9627       for (; local_plt < end_local_plt; ++local_plt)
9628         {
9629           struct plt_entry *ent;
9630
9631           for (ent = *local_plt; ent != NULL; ent = ent->next)
9632             if (ent->plt.refcount > 0)
9633               {
9634                 s = htab->iplt;
9635                 ent->plt.offset = s->size;
9636                 s->size += PLT_ENTRY_SIZE (htab);
9637
9638                 htab->reliplt->size += sizeof (Elf64_External_Rela);
9639               }
9640             else
9641               ent->plt.offset = (bfd_vma) -1;
9642         }
9643     }
9644
9645   /* Allocate global sym .plt and .got entries, and space for global
9646      sym dynamic relocs.  */
9647   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9648
9649   first_tlsld = NULL;
9650   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9651     {
9652       struct got_entry *ent;
9653
9654       if (!is_ppc64_elf (ibfd))
9655         continue;
9656
9657       ent = ppc64_tlsld_got (ibfd);
9658       if (ent->got.refcount > 0)
9659         {
9660           if (!htab->do_multi_toc && first_tlsld != NULL)
9661             {
9662               ent->is_indirect = TRUE;
9663               ent->got.ent = first_tlsld;
9664             }
9665           else
9666             {
9667               if (first_tlsld == NULL)
9668                 first_tlsld = ent;
9669               s = ppc64_elf_tdata (ibfd)->got;
9670               ent->got.offset = s->size;
9671               ent->owner = ibfd;
9672               s->size += 16;
9673               if (info->shared)
9674                 {
9675                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9676                   srel->size += sizeof (Elf64_External_Rela);
9677                 }
9678             }
9679         }
9680       else
9681         ent->got.offset = (bfd_vma) -1;
9682     }
9683
9684   /* We now have determined the sizes of the various dynamic sections.
9685      Allocate memory for them.  */
9686   relocs = FALSE;
9687   for (s = dynobj->sections; s != NULL; s = s->next)
9688     {
9689       if ((s->flags & SEC_LINKER_CREATED) == 0)
9690         continue;
9691
9692       if (s == htab->brlt || s == htab->relbrlt)
9693         /* These haven't been allocated yet;  don't strip.  */
9694         continue;
9695       else if (s == htab->got
9696                || s == htab->plt
9697                || s == htab->iplt
9698                || s == htab->glink
9699                || s == htab->dynbss)
9700         {
9701           /* Strip this section if we don't need it; see the
9702              comment below.  */
9703         }
9704       else if (s == htab->glink_eh_frame)
9705         {
9706           if (!bfd_is_abs_section (s->output_section))
9707             /* Not sized yet.  */
9708             continue;
9709         }
9710       else if (CONST_STRNEQ (s->name, ".rela"))
9711         {
9712           if (s->size != 0)
9713             {
9714               if (s != htab->relplt)
9715                 relocs = TRUE;
9716
9717               /* We use the reloc_count field as a counter if we need
9718                  to copy relocs into the output file.  */
9719               s->reloc_count = 0;
9720             }
9721         }
9722       else
9723         {
9724           /* It's not one of our sections, so don't allocate space.  */
9725           continue;
9726         }
9727
9728       if (s->size == 0)
9729         {
9730           /* If we don't need this section, strip it from the
9731              output file.  This is mostly to handle .rela.bss and
9732              .rela.plt.  We must create both sections in
9733              create_dynamic_sections, because they must be created
9734              before the linker maps input sections to output
9735              sections.  The linker does that before
9736              adjust_dynamic_symbol is called, and it is that
9737              function which decides whether anything needs to go
9738              into these sections.  */
9739           s->flags |= SEC_EXCLUDE;
9740           continue;
9741         }
9742
9743       if ((s->flags & SEC_HAS_CONTENTS) == 0)
9744         continue;
9745
9746       /* Allocate memory for the section contents.  We use bfd_zalloc
9747          here in case unused entries are not reclaimed before the
9748          section's contents are written out.  This should not happen,
9749          but this way if it does we get a R_PPC64_NONE reloc in .rela
9750          sections instead of garbage.
9751          We also rely on the section contents being zero when writing
9752          the GOT.  */
9753       s->contents = bfd_zalloc (dynobj, s->size);
9754       if (s->contents == NULL)
9755         return FALSE;
9756     }
9757
9758   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9759     {
9760       if (!is_ppc64_elf (ibfd))
9761         continue;
9762
9763       s = ppc64_elf_tdata (ibfd)->got;
9764       if (s != NULL && s != htab->got)
9765         {
9766           if (s->size == 0)
9767             s->flags |= SEC_EXCLUDE;
9768           else
9769             {
9770               s->contents = bfd_zalloc (ibfd, s->size);
9771               if (s->contents == NULL)
9772                 return FALSE;
9773             }
9774         }
9775       s = ppc64_elf_tdata (ibfd)->relgot;
9776       if (s != NULL)
9777         {
9778           if (s->size == 0)
9779             s->flags |= SEC_EXCLUDE;
9780           else
9781             {
9782               s->contents = bfd_zalloc (ibfd, s->size);
9783               if (s->contents == NULL)
9784                 return FALSE;
9785               relocs = TRUE;
9786               s->reloc_count = 0;
9787             }
9788         }
9789     }
9790
9791   if (htab->elf.dynamic_sections_created)
9792     {
9793       bfd_boolean tls_opt;
9794
9795       /* Add some entries to the .dynamic section.  We fill in the
9796          values later, in ppc64_elf_finish_dynamic_sections, but we
9797          must add the entries now so that we get the correct size for
9798          the .dynamic section.  The DT_DEBUG entry is filled in by the
9799          dynamic linker and used by the debugger.  */
9800 #define add_dynamic_entry(TAG, VAL) \
9801   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9802
9803       if (info->executable)
9804         {
9805           if (!add_dynamic_entry (DT_DEBUG, 0))
9806             return FALSE;
9807         }
9808
9809       if (htab->plt != NULL && htab->plt->size != 0)
9810         {
9811           if (!add_dynamic_entry (DT_PLTGOT, 0)
9812               || !add_dynamic_entry (DT_PLTRELSZ, 0)
9813               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9814               || !add_dynamic_entry (DT_JMPREL, 0)
9815               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9816             return FALSE;
9817         }
9818
9819       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
9820         {
9821           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9822               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9823             return FALSE;
9824         }
9825
9826       tls_opt = (!htab->no_tls_get_addr_opt
9827                  && htab->tls_get_addr_fd != NULL
9828                  && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9829       if (tls_opt || !htab->opd_abi)
9830         {
9831           if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9832             return FALSE;
9833         }
9834
9835       if (relocs)
9836         {
9837           if (!add_dynamic_entry (DT_RELA, 0)
9838               || !add_dynamic_entry (DT_RELASZ, 0)
9839               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9840             return FALSE;
9841
9842           /* If any dynamic relocs apply to a read-only section,
9843              then we need a DT_TEXTREL entry.  */
9844           if ((info->flags & DF_TEXTREL) == 0)
9845             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
9846
9847           if ((info->flags & DF_TEXTREL) != 0)
9848             {
9849               if (!add_dynamic_entry (DT_TEXTREL, 0))
9850                 return FALSE;
9851             }
9852         }
9853     }
9854 #undef add_dynamic_entry
9855
9856   return TRUE;
9857 }
9858
9859 /* Determine the type of stub needed, if any, for a call.  */
9860
9861 static inline enum ppc_stub_type
9862 ppc_type_of_stub (asection *input_sec,
9863                   const Elf_Internal_Rela *rel,
9864                   struct ppc_link_hash_entry **hash,
9865                   struct plt_entry **plt_ent,
9866                   bfd_vma destination,
9867                   unsigned long local_off)
9868 {
9869   struct ppc_link_hash_entry *h = *hash;
9870   bfd_vma location;
9871   bfd_vma branch_offset;
9872   bfd_vma max_branch_offset;
9873   enum elf_ppc64_reloc_type r_type;
9874
9875   if (h != NULL)
9876     {
9877       struct plt_entry *ent;
9878       struct ppc_link_hash_entry *fdh = h;
9879       if (h->oh != NULL
9880           && h->oh->is_func_descriptor)
9881         {
9882           fdh = ppc_follow_link (h->oh);
9883           *hash = fdh;
9884         }
9885
9886       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9887         if (ent->addend == rel->r_addend
9888             && ent->plt.offset != (bfd_vma) -1)
9889           {
9890             *plt_ent = ent;
9891             return ppc_stub_plt_call;
9892           }
9893
9894       /* Here, we know we don't have a plt entry.  If we don't have a
9895          either a defined function descriptor or a defined entry symbol
9896          in a regular object file, then it is pointless trying to make
9897          any other type of stub.  */
9898       if (!is_static_defined (&fdh->elf)
9899           && !is_static_defined (&h->elf))
9900         return ppc_stub_none;
9901     }
9902   else if (elf_local_got_ents (input_sec->owner) != NULL)
9903     {
9904       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9905       struct plt_entry **local_plt = (struct plt_entry **)
9906         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9907       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9908
9909       if (local_plt[r_symndx] != NULL)
9910         {
9911           struct plt_entry *ent;
9912
9913           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9914             if (ent->addend == rel->r_addend
9915                 && ent->plt.offset != (bfd_vma) -1)
9916               {
9917                 *plt_ent = ent;
9918                 return ppc_stub_plt_call;
9919               }
9920         }
9921     }
9922
9923   /* Determine where the call point is.  */
9924   location = (input_sec->output_offset
9925               + input_sec->output_section->vma
9926               + rel->r_offset);
9927
9928   branch_offset = destination - location;
9929   r_type = ELF64_R_TYPE (rel->r_info);
9930
9931   /* Determine if a long branch stub is needed.  */
9932   max_branch_offset = 1 << 25;
9933   if (r_type != R_PPC64_REL24)
9934     max_branch_offset = 1 << 15;
9935
9936   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
9937     /* We need a stub.  Figure out whether a long_branch or plt_branch
9938        is needed later.  */
9939     return ppc_stub_long_branch;
9940
9941   return ppc_stub_none;
9942 }
9943
9944 /* With power7 weakly ordered memory model, it is possible for ld.so
9945    to update a plt entry in one thread and have another thread see a
9946    stale zero toc entry.  To avoid this we need some sort of acquire
9947    barrier in the call stub.  One solution is to make the load of the
9948    toc word seem to appear to depend on the load of the function entry
9949    word.  Another solution is to test for r2 being zero, and branch to
9950    the appropriate glink entry if so.
9951
9952    .    fake dep barrier        compare
9953    .    ld 12,xxx(2)            ld 12,xxx(2)
9954    .    mtctr 12                mtctr 12
9955    .    xor 11,12,12            ld 2,xxx+8(2)
9956    .    add 2,2,11              cmpldi 2,0
9957    .    ld 2,xxx+8(2)           bnectr+
9958    .    bctr                    b <glink_entry>
9959
9960    The solution involving the compare turns out to be faster, so
9961    that's what we use unless the branch won't reach.  */
9962
9963 #define ALWAYS_USE_FAKE_DEP 0
9964 #define ALWAYS_EMIT_R2SAVE 0
9965
9966 #define PPC_LO(v) ((v) & 0xffff)
9967 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9968 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9969
9970 static inline unsigned int
9971 plt_stub_size (struct ppc_link_hash_table *htab,
9972                struct ppc_stub_hash_entry *stub_entry,
9973                bfd_vma off)
9974 {
9975   unsigned size = 12;
9976
9977   if (ALWAYS_EMIT_R2SAVE
9978       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9979     size += 4;
9980   if (PPC_HA (off) != 0)
9981     size += 4;
9982   if (htab->opd_abi)
9983     {
9984       size += 4;
9985       if (htab->plt_static_chain)
9986         size += 4;
9987       if (htab->plt_thread_safe)
9988         size += 8;
9989       if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
9990         size += 4;
9991     }
9992   if (stub_entry->h != NULL
9993       && (stub_entry->h == htab->tls_get_addr_fd
9994           || stub_entry->h == htab->tls_get_addr)
9995       && !htab->no_tls_get_addr_opt)
9996     size += 13 * 4;
9997   return size;
9998 }
9999
10000 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10001    then return the padding needed to do so.  */
10002 static inline unsigned int
10003 plt_stub_pad (struct ppc_link_hash_table *htab,
10004               struct ppc_stub_hash_entry *stub_entry,
10005               bfd_vma plt_off)
10006 {
10007   int stub_align = 1 << htab->plt_stub_align;
10008   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10009   bfd_vma stub_off = stub_entry->stub_sec->size;
10010
10011   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10012       > (stub_size & -stub_align))
10013     return stub_align - (stub_off & (stub_align - 1));
10014   return 0;
10015 }
10016
10017 /* Build a .plt call stub.  */
10018
10019 static inline bfd_byte *
10020 build_plt_stub (struct ppc_link_hash_table *htab,
10021                 struct ppc_stub_hash_entry *stub_entry,
10022                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10023 {
10024   bfd *obfd = htab->stub_bfd;
10025   bfd_boolean plt_load_toc = htab->opd_abi;
10026   bfd_boolean plt_static_chain = htab->plt_static_chain;
10027   bfd_boolean plt_thread_safe = htab->plt_thread_safe;
10028   bfd_boolean use_fake_dep = plt_thread_safe;
10029   bfd_vma cmp_branch_off = 0;
10030
10031   if (!ALWAYS_USE_FAKE_DEP
10032       && plt_load_toc
10033       && plt_thread_safe
10034       && !(stub_entry->h != NULL
10035            && (stub_entry->h == htab->tls_get_addr_fd
10036                || stub_entry->h == htab->tls_get_addr)
10037            && !htab->no_tls_get_addr_opt))
10038     {
10039       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10040       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10041                           / PLT_ENTRY_SIZE (htab));
10042       bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10043       bfd_vma to, from;
10044
10045       if (pltindex > 32768)
10046         glinkoff += (pltindex - 32768) * 4;
10047       to = (glinkoff
10048             + htab->glink->output_offset
10049             + htab->glink->output_section->vma);
10050       from = (p - stub_entry->stub_sec->contents
10051               + 4 * (ALWAYS_EMIT_R2SAVE
10052                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10053               + 4 * (PPC_HA (offset) != 0)
10054               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10055                      != PPC_HA (offset))
10056               + 4 * (plt_static_chain != 0)
10057               + 20
10058               + stub_entry->stub_sec->output_offset
10059               + stub_entry->stub_sec->output_section->vma);
10060       cmp_branch_off = to - from;
10061       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10062     }
10063
10064   if (PPC_HA (offset) != 0)
10065     {
10066       if (r != NULL)
10067         {
10068           if (ALWAYS_EMIT_R2SAVE
10069               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10070             r[0].r_offset += 4;
10071           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10072           r[1].r_offset = r[0].r_offset + 4;
10073           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10074           r[1].r_addend = r[0].r_addend;
10075           if (plt_load_toc)
10076             {
10077               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10078                 {
10079                   r[2].r_offset = r[1].r_offset + 4;
10080                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10081                   r[2].r_addend = r[0].r_addend;
10082                 }
10083               else
10084                 {
10085                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10086                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10087                   r[2].r_addend = r[0].r_addend + 8;
10088                   if (plt_static_chain)
10089                     {
10090                       r[3].r_offset = r[2].r_offset + 4;
10091                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10092                       r[3].r_addend = r[0].r_addend + 16;
10093                     }
10094                 }
10095             }
10096         }
10097       if (ALWAYS_EMIT_R2SAVE
10098           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10099         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10100       bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p),     p += 4;
10101       bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),      p += 4;
10102       if (plt_load_toc
10103           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10104         {
10105           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10106           offset = 0;
10107         }
10108       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10109       if (plt_load_toc)
10110         {
10111           if (use_fake_dep)
10112             {
10113               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
10114               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
10115             }
10116           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10117           if (plt_static_chain)
10118             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10119         }
10120     }
10121   else
10122     {
10123       if (r != NULL)
10124         {
10125           if (ALWAYS_EMIT_R2SAVE
10126               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10127             r[0].r_offset += 4;
10128           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10129           if (plt_load_toc)
10130             {
10131               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10132                 {
10133                   r[1].r_offset = r[0].r_offset + 4;
10134                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10135                   r[1].r_addend = r[0].r_addend;
10136                 }
10137               else
10138                 {
10139                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10140                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10141                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10142                   if (plt_static_chain)
10143                     {
10144                       r[2].r_offset = r[1].r_offset + 4;
10145                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10146                       r[2].r_addend = r[0].r_addend + 8;
10147                     }
10148                 }
10149             }
10150         }
10151       if (ALWAYS_EMIT_R2SAVE
10152           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10153         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10154       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10155       if (plt_load_toc
10156           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10157         {
10158           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10159           offset = 0;
10160         }
10161       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10162       if (plt_load_toc)
10163         {
10164           if (use_fake_dep)
10165             {
10166               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
10167               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
10168             }
10169           if (plt_static_chain)
10170             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10171           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10172         }
10173     }
10174   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10175     {
10176       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
10177       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
10178       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10179     }
10180   else
10181     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
10182   return p;
10183 }
10184
10185 /* Build a special .plt call stub for __tls_get_addr.  */
10186
10187 #define LD_R11_0R3      0xe9630000
10188 #define LD_R12_0R3      0xe9830000
10189 #define MR_R0_R3        0x7c601b78
10190 #define CMPDI_R11_0     0x2c2b0000
10191 #define ADD_R3_R12_R13  0x7c6c6a14
10192 #define BEQLR           0x4d820020
10193 #define MR_R3_R0        0x7c030378
10194 #define STD_R11_0R1     0xf9610000
10195 #define BCTRL           0x4e800421
10196 #define LD_R11_0R1      0xe9610000
10197 #define MTLR_R11        0x7d6803a6
10198
10199 static inline bfd_byte *
10200 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10201                          struct ppc_stub_hash_entry *stub_entry,
10202                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10203 {
10204   bfd *obfd = htab->stub_bfd;
10205
10206   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
10207   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
10208   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
10209   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
10210   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
10211   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
10212   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
10213   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
10214   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10215
10216   if (r != NULL)
10217     r[0].r_offset += 9 * 4;
10218   p = build_plt_stub (htab, stub_entry, p, offset, r);
10219   bfd_put_32 (obfd, BCTRL, p - 4);
10220
10221   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10222   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),     p += 4;
10223   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
10224   bfd_put_32 (obfd, BLR, p),                    p += 4;
10225
10226   return p;
10227 }
10228
10229 static Elf_Internal_Rela *
10230 get_relocs (asection *sec, int count)
10231 {
10232   Elf_Internal_Rela *relocs;
10233   struct bfd_elf_section_data *elfsec_data;
10234
10235   elfsec_data = elf_section_data (sec);
10236   relocs = elfsec_data->relocs;
10237   if (relocs == NULL)
10238     {
10239       bfd_size_type relsize;
10240       relsize = sec->reloc_count * sizeof (*relocs);
10241       relocs = bfd_alloc (sec->owner, relsize);
10242       if (relocs == NULL)
10243         return NULL;
10244       elfsec_data->relocs = relocs;
10245       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10246                                           sizeof (Elf_Internal_Shdr));
10247       if (elfsec_data->rela.hdr == NULL)
10248         return NULL;
10249       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10250                                         * sizeof (Elf64_External_Rela));
10251       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10252       sec->reloc_count = 0;
10253     }
10254   relocs += sec->reloc_count;
10255   sec->reloc_count += count;
10256   return relocs;
10257 }
10258
10259 static bfd_vma
10260 get_r2off (struct bfd_link_info *info,
10261            struct ppc_stub_hash_entry *stub_entry)
10262 {
10263   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10264   bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
10265
10266   if (r2off == 0)
10267     {
10268       /* Support linking -R objects.  Get the toc pointer from the
10269          opd entry.  */
10270       char buf[8];
10271       if (!htab->opd_abi)
10272         return r2off;
10273       asection *opd = stub_entry->h->elf.root.u.def.section;
10274       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10275
10276       if (strcmp (opd->name, ".opd") != 0
10277           || opd->reloc_count != 0)
10278         {
10279           info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10280                                   stub_entry->h->elf.root.root.string);
10281           bfd_set_error (bfd_error_bad_value);
10282           return 0;
10283         }
10284       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10285         return 0;
10286       r2off = bfd_get_64 (opd->owner, buf);
10287       r2off -= elf_gp (info->output_bfd);
10288     }
10289   r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
10290   return r2off;
10291 }
10292
10293 static bfd_boolean
10294 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10295 {
10296   struct ppc_stub_hash_entry *stub_entry;
10297   struct ppc_branch_hash_entry *br_entry;
10298   struct bfd_link_info *info;
10299   struct ppc_link_hash_table *htab;
10300   bfd_byte *loc;
10301   bfd_byte *p;
10302   bfd_vma dest, off;
10303   int size;
10304   Elf_Internal_Rela *r;
10305   asection *plt;
10306
10307   /* Massage our args to the form they really have.  */
10308   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10309   info = in_arg;
10310
10311   htab = ppc_hash_table (info);
10312   if (htab == NULL)
10313     return FALSE;
10314
10315   /* Make a note of the offset within the stubs for this entry.  */
10316   stub_entry->stub_offset = stub_entry->stub_sec->size;
10317   loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
10318
10319   htab->stub_count[stub_entry->stub_type - 1] += 1;
10320   switch (stub_entry->stub_type)
10321     {
10322     case ppc_stub_long_branch:
10323     case ppc_stub_long_branch_r2off:
10324       /* Branches are relative.  This is where we are going to.  */
10325       dest = (stub_entry->target_value
10326               + stub_entry->target_section->output_offset
10327               + stub_entry->target_section->output_section->vma);
10328       dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10329       off = dest;
10330
10331       /* And this is where we are coming from.  */
10332       off -= (stub_entry->stub_offset
10333               + stub_entry->stub_sec->output_offset
10334               + stub_entry->stub_sec->output_section->vma);
10335
10336       size = 4;
10337       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10338         {
10339           bfd_vma r2off = get_r2off (info, stub_entry);
10340
10341           if (r2off == 0)
10342             {
10343               htab->stub_error = TRUE;
10344               return FALSE;
10345             }
10346           bfd_put_32 (htab->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10347           loc += 4;
10348           size = 12;
10349           if (PPC_HA (r2off) != 0)
10350             {
10351               size = 16;
10352               bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10353               loc += 4;
10354             }
10355           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
10356           loc += 4;
10357           off -= size - 4;
10358         }
10359       bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10360
10361       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10362         {
10363           info->callbacks->einfo
10364             (_("%P: long branch stub `%s' offset overflow\n"),
10365              stub_entry->root.string);
10366           htab->stub_error = TRUE;
10367           return FALSE;
10368         }
10369
10370       if (info->emitrelocations)
10371         {
10372           r = get_relocs (stub_entry->stub_sec, 1);
10373           if (r == NULL)
10374             return FALSE;
10375           r->r_offset = loc - stub_entry->stub_sec->contents;
10376           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10377           r->r_addend = dest;
10378           if (stub_entry->h != NULL)
10379             {
10380               struct elf_link_hash_entry **hashes;
10381               unsigned long symndx;
10382               struct ppc_link_hash_entry *h;
10383
10384               hashes = elf_sym_hashes (htab->stub_bfd);
10385               if (hashes == NULL)
10386                 {
10387                   bfd_size_type hsize;
10388
10389                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10390                   hashes = bfd_zalloc (htab->stub_bfd, hsize);
10391                   if (hashes == NULL)
10392                     return FALSE;
10393                   elf_sym_hashes (htab->stub_bfd) = hashes;
10394                   htab->stub_globals = 1;
10395                 }
10396               symndx = htab->stub_globals++;
10397               h = stub_entry->h;
10398               hashes[symndx] = &h->elf;
10399               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10400               if (h->oh != NULL && h->oh->is_func)
10401                 h = ppc_follow_link (h->oh);
10402               if (h->elf.root.u.def.section != stub_entry->target_section)
10403                 /* H is an opd symbol.  The addend must be zero.  */
10404                 r->r_addend = 0;
10405               else
10406                 {
10407                   off = (h->elf.root.u.def.value
10408                          + h->elf.root.u.def.section->output_offset
10409                          + h->elf.root.u.def.section->output_section->vma);
10410                   r->r_addend -= off;
10411                 }
10412             }
10413         }
10414       break;
10415
10416     case ppc_stub_plt_branch:
10417     case ppc_stub_plt_branch_r2off:
10418       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10419                                          stub_entry->root.string + 9,
10420                                          FALSE, FALSE);
10421       if (br_entry == NULL)
10422         {
10423           info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10424                                   stub_entry->root.string);
10425           htab->stub_error = TRUE;
10426           return FALSE;
10427         }
10428
10429       dest = (stub_entry->target_value
10430               + stub_entry->target_section->output_offset
10431               + stub_entry->target_section->output_section->vma);
10432       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10433         dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10434
10435       bfd_put_64 (htab->brlt->owner, dest,
10436                   htab->brlt->contents + br_entry->offset);
10437
10438       if (br_entry->iter == htab->stub_iteration)
10439         {
10440           br_entry->iter = 0;
10441
10442           if (htab->relbrlt != NULL)
10443             {
10444               /* Create a reloc for the branch lookup table entry.  */
10445               Elf_Internal_Rela rela;
10446               bfd_byte *rl;
10447
10448               rela.r_offset = (br_entry->offset
10449                                + htab->brlt->output_offset
10450                                + htab->brlt->output_section->vma);
10451               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10452               rela.r_addend = dest;
10453
10454               rl = htab->relbrlt->contents;
10455               rl += (htab->relbrlt->reloc_count++
10456                      * sizeof (Elf64_External_Rela));
10457               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10458             }
10459           else if (info->emitrelocations)
10460             {
10461               r = get_relocs (htab->brlt, 1);
10462               if (r == NULL)
10463                 return FALSE;
10464               /* brlt, being SEC_LINKER_CREATED does not go through the
10465                  normal reloc processing.  Symbols and offsets are not
10466                  translated from input file to output file form, so
10467                  set up the offset per the output file.  */
10468               r->r_offset = (br_entry->offset
10469                              + htab->brlt->output_offset
10470                              + htab->brlt->output_section->vma);
10471               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10472               r->r_addend = dest;
10473             }
10474         }
10475
10476       dest = (br_entry->offset
10477               + htab->brlt->output_offset
10478               + htab->brlt->output_section->vma);
10479
10480       off = (dest
10481              - elf_gp (htab->brlt->output_section->owner)
10482              - htab->stub_group[stub_entry->id_sec->id].toc_off);
10483
10484       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10485         {
10486           info->callbacks->einfo
10487             (_("%P: linkage table error against `%T'\n"),
10488              stub_entry->root.string);
10489           bfd_set_error (bfd_error_bad_value);
10490           htab->stub_error = TRUE;
10491           return FALSE;
10492         }
10493
10494       if (info->emitrelocations)
10495         {
10496           r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
10497           if (r == NULL)
10498             return FALSE;
10499           r[0].r_offset = loc - stub_entry->stub_sec->contents;
10500           if (bfd_big_endian (info->output_bfd))
10501             r[0].r_offset += 2;
10502           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off
10503               && htab->opd_abi)
10504             r[0].r_offset += 4;
10505           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10506           r[0].r_addend = dest;
10507           if (PPC_HA (off) != 0)
10508             {
10509               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10510               r[1].r_offset = r[0].r_offset + 4;
10511               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10512               r[1].r_addend = r[0].r_addend;
10513             }
10514         }
10515
10516       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off
10517           || !htab->opd_abi)
10518         {
10519           if (PPC_HA (off) != 0)
10520             {
10521               size = 16;
10522               bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
10523               loc += 4;
10524               bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
10525             }
10526           else
10527             {
10528               size = 12;
10529               bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
10530             }
10531         }
10532       else
10533         {
10534           bfd_vma r2off = get_r2off (info, stub_entry);
10535
10536           if (r2off == 0)
10537             {
10538               htab->stub_error = TRUE;
10539               return FALSE;
10540             }
10541
10542           bfd_put_32 (htab->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10543           loc += 4;
10544           size = 20;
10545           if (PPC_HA (off) != 0)
10546             {
10547               size += 4;
10548               bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
10549               loc += 4;
10550               bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
10551               loc += 4;
10552             }
10553           else
10554             {
10555               bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
10556               loc += 4;
10557             }
10558
10559           if (PPC_HA (r2off) != 0)
10560             {
10561               size += 4;
10562               bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10563               loc += 4;
10564             }
10565           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
10566         }
10567       loc += 4;
10568       bfd_put_32 (htab->stub_bfd, MTCTR_R12, loc);
10569       loc += 4;
10570       bfd_put_32 (htab->stub_bfd, BCTR, loc);
10571       break;
10572
10573     case ppc_stub_plt_call:
10574     case ppc_stub_plt_call_r2save:
10575       if (stub_entry->h != NULL
10576           && stub_entry->h->is_func_descriptor
10577           && stub_entry->h->oh != NULL)
10578         {
10579           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10580
10581           /* If the old-ABI "dot-symbol" is undefined make it weak so
10582              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
10583              FIXME: We used to define the symbol on one of the call
10584              stubs instead, which is why we test symbol section id
10585              against htab->top_id in various places.  Likely all
10586              these checks could now disappear.  */
10587           if (fh->elf.root.type == bfd_link_hash_undefined)
10588             fh->elf.root.type = bfd_link_hash_undefweak;
10589           /* Stop undo_symbol_twiddle changing it back to undefined.  */
10590           fh->was_undefined = 0;
10591         }
10592
10593       /* Now build the stub.  */
10594       dest = stub_entry->plt_ent->plt.offset & ~1;
10595       if (dest >= (bfd_vma) -2)
10596         abort ();
10597
10598       plt = htab->plt;
10599       if (!htab->elf.dynamic_sections_created
10600           || stub_entry->h == NULL
10601           || stub_entry->h->elf.dynindx == -1)
10602         plt = htab->iplt;
10603
10604       dest += plt->output_offset + plt->output_section->vma;
10605
10606       if (stub_entry->h == NULL
10607           && (stub_entry->plt_ent->plt.offset & 1) == 0)
10608         {
10609           Elf_Internal_Rela rela;
10610           bfd_byte *rl;
10611
10612           rela.r_offset = dest;
10613           if (htab->opd_abi)
10614             rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10615           else
10616             rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
10617           rela.r_addend = (stub_entry->target_value
10618                            + stub_entry->target_section->output_offset
10619                            + stub_entry->target_section->output_section->vma);
10620
10621           rl = (htab->reliplt->contents
10622                 + (htab->reliplt->reloc_count++
10623                    * sizeof (Elf64_External_Rela)));
10624           bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
10625           stub_entry->plt_ent->plt.offset |= 1;
10626         }
10627
10628       off = (dest
10629              - elf_gp (plt->output_section->owner)
10630              - htab->stub_group[stub_entry->id_sec->id].toc_off);
10631
10632       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10633         {
10634           info->callbacks->einfo
10635             (_("%P: linkage table error against `%T'\n"),
10636              stub_entry->h != NULL
10637              ? stub_entry->h->elf.root.root.string
10638              : "<local sym>");
10639           bfd_set_error (bfd_error_bad_value);
10640           htab->stub_error = TRUE;
10641           return FALSE;
10642         }
10643
10644       if (htab->plt_stub_align != 0)
10645         {
10646           unsigned pad = plt_stub_pad (htab, stub_entry, off);
10647
10648           stub_entry->stub_sec->size += pad;
10649           stub_entry->stub_offset = stub_entry->stub_sec->size;
10650           loc += pad;
10651         }
10652
10653       r = NULL;
10654       if (info->emitrelocations)
10655         {
10656           r = get_relocs (stub_entry->stub_sec,
10657                           (2
10658                            + (PPC_HA (off) != 0)
10659                            + (htab->plt_static_chain
10660                               && PPC_HA (off + 16) == PPC_HA (off))));
10661           if (r == NULL)
10662             return FALSE;
10663           r[0].r_offset = loc - stub_entry->stub_sec->contents;
10664           if (bfd_big_endian (info->output_bfd))
10665             r[0].r_offset += 2;
10666           r[0].r_addend = dest;
10667         }
10668       if (stub_entry->h != NULL
10669           && (stub_entry->h == htab->tls_get_addr_fd
10670               || stub_entry->h == htab->tls_get_addr)
10671           && !htab->no_tls_get_addr_opt)
10672         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10673       else
10674         p = build_plt_stub (htab, stub_entry, loc, off, r);
10675       size = p - loc;
10676       break;
10677
10678     default:
10679       BFD_FAIL ();
10680       return FALSE;
10681     }
10682
10683   stub_entry->stub_sec->size += size;
10684
10685   if (htab->emit_stub_syms)
10686     {
10687       struct elf_link_hash_entry *h;
10688       size_t len1, len2;
10689       char *name;
10690       const char *const stub_str[] = { "long_branch",
10691                                        "long_branch_r2off",
10692                                        "plt_branch",
10693                                        "plt_branch_r2off",
10694                                        "plt_call",
10695                                        "plt_call" };
10696
10697       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10698       len2 = strlen (stub_entry->root.string);
10699       name = bfd_malloc (len1 + len2 + 2);
10700       if (name == NULL)
10701         return FALSE;
10702       memcpy (name, stub_entry->root.string, 9);
10703       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10704       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10705       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10706       if (h == NULL)
10707         return FALSE;
10708       if (h->root.type == bfd_link_hash_new)
10709         {
10710           h->root.type = bfd_link_hash_defined;
10711           h->root.u.def.section = stub_entry->stub_sec;
10712           h->root.u.def.value = stub_entry->stub_offset;
10713           h->ref_regular = 1;
10714           h->def_regular = 1;
10715           h->ref_regular_nonweak = 1;
10716           h->forced_local = 1;
10717           h->non_elf = 0;
10718         }
10719     }
10720
10721   return TRUE;
10722 }
10723
10724 /* As above, but don't actually build the stub.  Just bump offset so
10725    we know stub section sizes, and select plt_branch stubs where
10726    long_branch stubs won't do.  */
10727
10728 static bfd_boolean
10729 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10730 {
10731   struct ppc_stub_hash_entry *stub_entry;
10732   struct bfd_link_info *info;
10733   struct ppc_link_hash_table *htab;
10734   bfd_vma off;
10735   int size;
10736
10737   /* Massage our args to the form they really have.  */
10738   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10739   info = in_arg;
10740
10741   htab = ppc_hash_table (info);
10742   if (htab == NULL)
10743     return FALSE;
10744
10745   if (stub_entry->stub_type == ppc_stub_plt_call
10746       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10747     {
10748       asection *plt;
10749       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
10750       if (off >= (bfd_vma) -2)
10751         abort ();
10752       plt = htab->plt;
10753       if (!htab->elf.dynamic_sections_created
10754           || stub_entry->h == NULL
10755           || stub_entry->h->elf.dynindx == -1)
10756         plt = htab->iplt;
10757       off += (plt->output_offset
10758               + plt->output_section->vma
10759               - elf_gp (plt->output_section->owner)
10760               - htab->stub_group[stub_entry->id_sec->id].toc_off);
10761
10762       size = plt_stub_size (htab, stub_entry, off);
10763       if (htab->plt_stub_align)
10764         size += plt_stub_pad (htab, stub_entry, off);
10765       if (info->emitrelocations)
10766         {
10767           stub_entry->stub_sec->reloc_count
10768             += ((PPC_HA (off) != 0)
10769                 + (htab->opd_abi
10770                    ? 2 + (htab->plt_static_chain
10771                           && PPC_HA (off + 16) == PPC_HA (off))
10772                    : 1));
10773           stub_entry->stub_sec->flags |= SEC_RELOC;
10774         }
10775     }
10776   else
10777     {
10778       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10779          variants.  */
10780       bfd_vma r2off = 0;
10781       bfd_vma local_off = 0;
10782
10783       off = (stub_entry->target_value
10784              + stub_entry->target_section->output_offset
10785              + stub_entry->target_section->output_section->vma);
10786       off -= (stub_entry->stub_sec->size
10787               + stub_entry->stub_sec->output_offset
10788               + stub_entry->stub_sec->output_section->vma);
10789
10790       /* Reset the stub type from the plt variant in case we now
10791          can reach with a shorter stub.  */
10792       if (stub_entry->stub_type >= ppc_stub_plt_branch)
10793         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10794
10795       size = 4;
10796       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10797         {
10798           r2off = get_r2off (info, stub_entry);
10799           if (r2off == 0 && htab->opd_abi)
10800             {
10801               htab->stub_error = TRUE;
10802               return FALSE;
10803             }
10804           size = 12;
10805           if (PPC_HA (r2off) != 0)
10806             size = 16;
10807           off -= size - 4;
10808         }
10809
10810       local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10811
10812       /* If the branch offset if too big, use a ppc_stub_plt_branch.
10813          Do the same for -R objects without function descriptors.  */
10814       if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
10815           || (stub_entry->stub_type == ppc_stub_long_branch_r2off
10816               && r2off == 0))
10817         {
10818           struct ppc_branch_hash_entry *br_entry;
10819
10820           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10821                                              stub_entry->root.string + 9,
10822                                              TRUE, FALSE);
10823           if (br_entry == NULL)
10824             {
10825               info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
10826                                       stub_entry->root.string);
10827               htab->stub_error = TRUE;
10828               return FALSE;
10829             }
10830
10831           if (br_entry->iter != htab->stub_iteration)
10832             {
10833               br_entry->iter = htab->stub_iteration;
10834               br_entry->offset = htab->brlt->size;
10835               htab->brlt->size += 8;
10836
10837               if (htab->relbrlt != NULL)
10838                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10839               else if (info->emitrelocations)
10840                 {
10841                   htab->brlt->reloc_count += 1;
10842                   htab->brlt->flags |= SEC_RELOC;
10843                 }
10844             }
10845
10846           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
10847           off = (br_entry->offset
10848                  + htab->brlt->output_offset
10849                  + htab->brlt->output_section->vma
10850                  - elf_gp (htab->brlt->output_section->owner)
10851                  - htab->stub_group[stub_entry->id_sec->id].toc_off);
10852
10853           if (info->emitrelocations)
10854             {
10855               stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10856               stub_entry->stub_sec->flags |= SEC_RELOC;
10857             }
10858
10859           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off
10860               || !htab->opd_abi)
10861             {
10862               size = 12;
10863               if (PPC_HA (off) != 0)
10864                 size = 16;
10865             }
10866           else
10867             {
10868               size = 20;
10869               if (PPC_HA (off) != 0)
10870                 size += 4;
10871
10872               if (PPC_HA (r2off) != 0)
10873                 size += 4;
10874             }
10875         }
10876       else if (info->emitrelocations)
10877         {
10878           stub_entry->stub_sec->reloc_count += 1;
10879           stub_entry->stub_sec->flags |= SEC_RELOC;
10880         }
10881     }
10882
10883   stub_entry->stub_sec->size += size;
10884   return TRUE;
10885 }
10886
10887 /* Set up various things so that we can make a list of input sections
10888    for each output section included in the link.  Returns -1 on error,
10889    0 when no stubs will be needed, and 1 on success.  */
10890
10891 int
10892 ppc64_elf_setup_section_lists
10893   (struct bfd_link_info *info,
10894    asection *(*add_stub_section) (const char *, asection *),
10895    void (*layout_sections_again) (void))
10896 {
10897   bfd *input_bfd;
10898   int top_id, top_index, id;
10899   asection *section;
10900   asection **input_list;
10901   bfd_size_type amt;
10902   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10903
10904   if (htab == NULL)
10905     return -1;
10906   /* Stash our params away.  */
10907   htab->add_stub_section = add_stub_section;
10908   htab->layout_sections_again = layout_sections_again;
10909
10910   /* Find the top input section id.  */
10911   for (input_bfd = info->input_bfds, top_id = 3;
10912        input_bfd != NULL;
10913        input_bfd = input_bfd->link_next)
10914     {
10915       for (section = input_bfd->sections;
10916            section != NULL;
10917            section = section->next)
10918         {
10919           if (top_id < section->id)
10920             top_id = section->id;
10921         }
10922     }
10923
10924   htab->top_id = top_id;
10925   amt = sizeof (struct map_stub) * (top_id + 1);
10926   htab->stub_group = bfd_zmalloc (amt);
10927   if (htab->stub_group == NULL)
10928     return -1;
10929
10930   /* Set toc_off for com, und, abs and ind sections.  */
10931   for (id = 0; id < 3; id++)
10932     htab->stub_group[id].toc_off = TOC_BASE_OFF;
10933
10934   /* We can't use output_bfd->section_count here to find the top output
10935      section index as some sections may have been removed, and
10936      strip_excluded_output_sections doesn't renumber the indices.  */
10937   for (section = info->output_bfd->sections, top_index = 0;
10938        section != NULL;
10939        section = section->next)
10940     {
10941       if (top_index < section->index)
10942         top_index = section->index;
10943     }
10944
10945   htab->top_index = top_index;
10946   amt = sizeof (asection *) * (top_index + 1);
10947   input_list = bfd_zmalloc (amt);
10948   htab->input_list = input_list;
10949   if (input_list == NULL)
10950     return -1;
10951
10952   return 1;
10953 }
10954
10955 /* Set up for first pass at multitoc partitioning.  */
10956
10957 void
10958 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10959 {
10960   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10961
10962   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
10963   htab->toc_bfd = NULL;
10964   htab->toc_first_sec = NULL;
10965 }
10966
10967 /* The linker repeatedly calls this function for each TOC input section
10968    and linker generated GOT section.  Group input bfds such that the toc
10969    within a group is less than 64k in size.  */
10970
10971 bfd_boolean
10972 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10973 {
10974   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10975   bfd_vma addr, off, limit;
10976
10977   if (htab == NULL)
10978     return FALSE;
10979
10980   if (!htab->second_toc_pass)
10981     {
10982       /* Keep track of the first .toc or .got section for this input bfd.  */
10983       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
10984
10985       if (new_bfd)
10986         {
10987           htab->toc_bfd = isec->owner;
10988           htab->toc_first_sec = isec;
10989         }
10990
10991       addr = isec->output_offset + isec->output_section->vma;
10992       off = addr - htab->toc_curr;
10993       limit = 0x80008000;
10994       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10995         limit = 0x10000;
10996       if (off + isec->size > limit)
10997         {
10998           addr = (htab->toc_first_sec->output_offset
10999                   + htab->toc_first_sec->output_section->vma);
11000           htab->toc_curr = addr;
11001         }
11002
11003       /* toc_curr is the base address of this toc group.  Set elf_gp
11004          for the input section to be the offset relative to the
11005          output toc base plus 0x8000.  Making the input elf_gp an
11006          offset allows us to move the toc as a whole without
11007          recalculating input elf_gp.  */
11008       off = htab->toc_curr - elf_gp (isec->output_section->owner);
11009       off += TOC_BASE_OFF;
11010
11011       /* Die if someone uses a linker script that doesn't keep input
11012          file .toc and .got together.  */
11013       if (new_bfd
11014           && elf_gp (isec->owner) != 0
11015           && elf_gp (isec->owner) != off)
11016         return FALSE;
11017
11018       elf_gp (isec->owner) = off;
11019       return TRUE;
11020     }
11021
11022   /* During the second pass toc_first_sec points to the start of
11023      a toc group, and toc_curr is used to track the old elf_gp.
11024      We use toc_bfd to ensure we only look at each bfd once.  */
11025   if (htab->toc_bfd == isec->owner)
11026     return TRUE;
11027   htab->toc_bfd = isec->owner;
11028
11029   if (htab->toc_first_sec == NULL
11030       || htab->toc_curr != elf_gp (isec->owner))
11031     {
11032       htab->toc_curr = elf_gp (isec->owner);
11033       htab->toc_first_sec = isec;
11034     }
11035   addr = (htab->toc_first_sec->output_offset
11036           + htab->toc_first_sec->output_section->vma);
11037   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11038   elf_gp (isec->owner) = off;
11039
11040   return TRUE;
11041 }
11042
11043 /* Called via elf_link_hash_traverse to merge GOT entries for global
11044    symbol H.  */
11045
11046 static bfd_boolean
11047 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11048 {
11049   if (h->root.type == bfd_link_hash_indirect)
11050     return TRUE;
11051
11052   merge_got_entries (&h->got.glist);
11053
11054   return TRUE;
11055 }
11056
11057 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11058    symbol H.  */
11059
11060 static bfd_boolean
11061 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11062 {
11063   struct got_entry *gent;
11064
11065   if (h->root.type == bfd_link_hash_indirect)
11066     return TRUE;
11067
11068   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11069     if (!gent->is_indirect)
11070       allocate_got (h, (struct bfd_link_info *) inf, gent);
11071   return TRUE;
11072 }
11073
11074 /* Called on the first multitoc pass after the last call to
11075    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11076    entries.  */
11077
11078 bfd_boolean
11079 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11080 {
11081   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11082   struct bfd *ibfd, *ibfd2;
11083   bfd_boolean done_something;
11084
11085   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11086
11087   if (!htab->do_multi_toc)
11088     return FALSE;
11089
11090   /* Merge global sym got entries within a toc group.  */
11091   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11092
11093   /* And tlsld_got.  */
11094   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11095     {
11096       struct got_entry *ent, *ent2;
11097
11098       if (!is_ppc64_elf (ibfd))
11099         continue;
11100
11101       ent = ppc64_tlsld_got (ibfd);
11102       if (!ent->is_indirect
11103           && ent->got.offset != (bfd_vma) -1)
11104         {
11105           for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
11106             {
11107               if (!is_ppc64_elf (ibfd2))
11108                 continue;
11109
11110               ent2 = ppc64_tlsld_got (ibfd2);
11111               if (!ent2->is_indirect
11112                   && ent2->got.offset != (bfd_vma) -1
11113                   && elf_gp (ibfd2) == elf_gp (ibfd))
11114                 {
11115                   ent2->is_indirect = TRUE;
11116                   ent2->got.ent = ent;
11117                 }
11118             }
11119         }
11120     }
11121
11122   /* Zap sizes of got sections.  */
11123   htab->reliplt->rawsize = htab->reliplt->size;
11124   htab->reliplt->size -= htab->got_reli_size;
11125   htab->got_reli_size = 0;
11126
11127   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11128     {
11129       asection *got, *relgot;
11130
11131       if (!is_ppc64_elf (ibfd))
11132         continue;
11133
11134       got = ppc64_elf_tdata (ibfd)->got;
11135       if (got != NULL)
11136         {
11137           got->rawsize = got->size;
11138           got->size = 0;
11139           relgot = ppc64_elf_tdata (ibfd)->relgot;
11140           relgot->rawsize = relgot->size;
11141           relgot->size = 0;
11142         }
11143     }
11144
11145   /* Now reallocate the got, local syms first.  We don't need to
11146      allocate section contents again since we never increase size.  */
11147   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11148     {
11149       struct got_entry **lgot_ents;
11150       struct got_entry **end_lgot_ents;
11151       struct plt_entry **local_plt;
11152       struct plt_entry **end_local_plt;
11153       unsigned char *lgot_masks;
11154       bfd_size_type locsymcount;
11155       Elf_Internal_Shdr *symtab_hdr;
11156       asection *s;
11157
11158       if (!is_ppc64_elf (ibfd))
11159         continue;
11160
11161       lgot_ents = elf_local_got_ents (ibfd);
11162       if (!lgot_ents)
11163         continue;
11164
11165       symtab_hdr = &elf_symtab_hdr (ibfd);
11166       locsymcount = symtab_hdr->sh_info;
11167       end_lgot_ents = lgot_ents + locsymcount;
11168       local_plt = (struct plt_entry **) end_lgot_ents;
11169       end_local_plt = local_plt + locsymcount;
11170       lgot_masks = (unsigned char *) end_local_plt;
11171       s = ppc64_elf_tdata (ibfd)->got;
11172       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11173         {
11174           struct got_entry *ent;
11175
11176           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11177             {
11178               unsigned int ent_size = 8;
11179               unsigned int rel_size = sizeof (Elf64_External_Rela);
11180
11181               ent->got.offset = s->size;
11182               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11183                 {
11184                   ent_size *= 2;
11185                   rel_size *= 2;
11186                 }
11187               s->size += ent_size;
11188               if ((*lgot_masks & PLT_IFUNC) != 0)
11189                 {
11190                   htab->reliplt->size += rel_size;
11191                   htab->got_reli_size += rel_size;
11192                 }
11193               else if (info->shared)
11194                 {
11195                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11196                   srel->size += rel_size;
11197                 }
11198             }
11199         }
11200     }
11201
11202   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11203
11204   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11205     {
11206       struct got_entry *ent;
11207
11208       if (!is_ppc64_elf (ibfd))
11209         continue;
11210
11211       ent = ppc64_tlsld_got (ibfd);
11212       if (!ent->is_indirect
11213           && ent->got.offset != (bfd_vma) -1)
11214         {
11215           asection *s = ppc64_elf_tdata (ibfd)->got;
11216           ent->got.offset = s->size;
11217           s->size += 16;
11218           if (info->shared)
11219             {
11220               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11221               srel->size += sizeof (Elf64_External_Rela);
11222             }
11223         }
11224     }
11225
11226   done_something = htab->reliplt->rawsize != htab->reliplt->size;
11227   if (!done_something)
11228     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11229       {
11230         asection *got;
11231
11232         if (!is_ppc64_elf (ibfd))
11233           continue;
11234
11235         got = ppc64_elf_tdata (ibfd)->got;
11236         if (got != NULL)
11237           {
11238             done_something = got->rawsize != got->size;
11239             if (done_something)
11240               break;
11241           }
11242       }
11243
11244   if (done_something)
11245     (*htab->layout_sections_again) ();
11246
11247   /* Set up for second pass over toc sections to recalculate elf_gp
11248      on input sections.  */
11249   htab->toc_bfd = NULL;
11250   htab->toc_first_sec = NULL;
11251   htab->second_toc_pass = TRUE;
11252   return done_something;
11253 }
11254
11255 /* Called after second pass of multitoc partitioning.  */
11256
11257 void
11258 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11259 {
11260   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11261
11262   /* After the second pass, toc_curr tracks the TOC offset used
11263      for code sections below in ppc64_elf_next_input_section.  */
11264   htab->toc_curr = TOC_BASE_OFF;
11265 }
11266
11267 /* No toc references were found in ISEC.  If the code in ISEC makes no
11268    calls, then there's no need to use toc adjusting stubs when branching
11269    into ISEC.  Actually, indirect calls from ISEC are OK as they will
11270    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
11271    needed, and 2 if a cyclical call-graph was found but no other reason
11272    for a stub was detected.  If called from the top level, a return of
11273    2 means the same as a return of 0.  */
11274
11275 static int
11276 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11277 {
11278   int ret;
11279
11280   /* Mark this section as checked.  */
11281   isec->call_check_done = 1;
11282
11283   /* We know none of our code bearing sections will need toc stubs.  */
11284   if ((isec->flags & SEC_LINKER_CREATED) != 0)
11285     return 0;
11286
11287   if (isec->size == 0)
11288     return 0;
11289
11290   if (isec->output_section == NULL)
11291     return 0;
11292
11293   ret = 0;
11294   if (isec->reloc_count != 0)
11295     {
11296       Elf_Internal_Rela *relstart, *rel;
11297       Elf_Internal_Sym *local_syms;
11298       struct ppc_link_hash_table *htab;
11299
11300       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11301                                             info->keep_memory);
11302       if (relstart == NULL)
11303         return -1;
11304
11305       /* Look for branches to outside of this section.  */
11306       local_syms = NULL;
11307       htab = ppc_hash_table (info);
11308       if (htab == NULL)
11309         return -1;
11310
11311       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11312         {
11313           enum elf_ppc64_reloc_type r_type;
11314           unsigned long r_symndx;
11315           struct elf_link_hash_entry *h;
11316           struct ppc_link_hash_entry *eh;
11317           Elf_Internal_Sym *sym;
11318           asection *sym_sec;
11319           struct _opd_sec_data *opd;
11320           bfd_vma sym_value;
11321           bfd_vma dest;
11322
11323           r_type = ELF64_R_TYPE (rel->r_info);
11324           if (r_type != R_PPC64_REL24
11325               && r_type != R_PPC64_REL14
11326               && r_type != R_PPC64_REL14_BRTAKEN
11327               && r_type != R_PPC64_REL14_BRNTAKEN)
11328             continue;
11329
11330           r_symndx = ELF64_R_SYM (rel->r_info);
11331           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11332                           isec->owner))
11333             {
11334               ret = -1;
11335               break;
11336             }
11337
11338           /* Calls to dynamic lib functions go through a plt call stub
11339              that uses r2.  */
11340           eh = (struct ppc_link_hash_entry *) h;
11341           if (eh != NULL
11342               && (eh->elf.plt.plist != NULL
11343                   || (eh->oh != NULL
11344                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11345             {
11346               ret = 1;
11347               break;
11348             }
11349
11350           if (sym_sec == NULL)
11351             /* Ignore other undefined symbols.  */
11352             continue;
11353
11354           /* Assume branches to other sections not included in the
11355              link need stubs too, to cover -R and absolute syms.  */
11356           if (sym_sec->output_section == NULL)
11357             {
11358               ret = 1;
11359               break;
11360             }
11361
11362           if (h == NULL)
11363             sym_value = sym->st_value;
11364           else
11365             {
11366               if (h->root.type != bfd_link_hash_defined
11367                   && h->root.type != bfd_link_hash_defweak)
11368                 abort ();
11369               sym_value = h->root.u.def.value;
11370             }
11371           sym_value += rel->r_addend;
11372
11373           /* If this branch reloc uses an opd sym, find the code section.  */
11374           opd = get_opd_info (sym_sec);
11375           if (opd != NULL)
11376             {
11377               if (h == NULL && opd->adjust != NULL)
11378                 {
11379                   long adjust;
11380
11381                   adjust = opd->adjust[sym->st_value / 8];
11382                   if (adjust == -1)
11383                     /* Assume deleted functions won't ever be called.  */
11384                     continue;
11385                   sym_value += adjust;
11386                 }
11387
11388               dest = opd_entry_value (sym_sec, sym_value,
11389                                       &sym_sec, NULL, FALSE);
11390               if (dest == (bfd_vma) -1)
11391                 continue;
11392             }
11393           else
11394             dest = (sym_value
11395                     + sym_sec->output_offset
11396                     + sym_sec->output_section->vma);
11397
11398           /* Ignore branch to self.  */
11399           if (sym_sec == isec)
11400             continue;
11401
11402           /* If the called function uses the toc, we need a stub.  */
11403           if (sym_sec->has_toc_reloc
11404               || sym_sec->makes_toc_func_call)
11405             {
11406               ret = 1;
11407               break;
11408             }
11409
11410           /* Assume any branch that needs a long branch stub might in fact
11411              need a plt_branch stub.  A plt_branch stub uses r2.  */
11412           else if (dest - (isec->output_offset
11413                            + isec->output_section->vma
11414                            + rel->r_offset) + (1 << 25)
11415                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11416                                                              ? h->other
11417                                                              : sym->st_other))
11418             {
11419               ret = 1;
11420               break;
11421             }
11422
11423           /* If calling back to a section in the process of being
11424              tested, we can't say for sure that no toc adjusting stubs
11425              are needed, so don't return zero.  */
11426           else if (sym_sec->call_check_in_progress)
11427             ret = 2;
11428
11429           /* Branches to another section that itself doesn't have any TOC
11430              references are OK.  Recursively call ourselves to check.  */
11431           else if (!sym_sec->call_check_done)
11432             {
11433               int recur;
11434
11435               /* Mark current section as indeterminate, so that other
11436                  sections that call back to current won't be marked as
11437                  known.  */
11438               isec->call_check_in_progress = 1;
11439               recur = toc_adjusting_stub_needed (info, sym_sec);
11440               isec->call_check_in_progress = 0;
11441
11442               if (recur != 0)
11443                 {
11444                   ret = recur;
11445                   if (recur != 2)
11446                     break;
11447                 }
11448             }
11449         }
11450
11451       if (local_syms != NULL
11452           && (elf_symtab_hdr (isec->owner).contents
11453               != (unsigned char *) local_syms))
11454         free (local_syms);
11455       if (elf_section_data (isec)->relocs != relstart)
11456         free (relstart);
11457     }
11458
11459   if ((ret & 1) == 0
11460       && isec->map_head.s != NULL
11461       && (strcmp (isec->output_section->name, ".init") == 0
11462           || strcmp (isec->output_section->name, ".fini") == 0))
11463     {
11464       if (isec->map_head.s->has_toc_reloc
11465           || isec->map_head.s->makes_toc_func_call)
11466         ret = 1;
11467       else if (!isec->map_head.s->call_check_done)
11468         {
11469           int recur;
11470           isec->call_check_in_progress = 1;
11471           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11472           isec->call_check_in_progress = 0;
11473           if (recur != 0)
11474             ret = recur;
11475         }
11476     }
11477
11478   if (ret == 1)
11479     isec->makes_toc_func_call = 1;
11480
11481   return ret;
11482 }
11483
11484 /* The linker repeatedly calls this function for each input section,
11485    in the order that input sections are linked into output sections.
11486    Build lists of input sections to determine groupings between which
11487    we may insert linker stubs.  */
11488
11489 bfd_boolean
11490 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11491 {
11492   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11493
11494   if (htab == NULL)
11495     return FALSE;
11496
11497   if ((isec->output_section->flags & SEC_CODE) != 0
11498       && isec->output_section->index <= htab->top_index)
11499     {
11500       asection **list = htab->input_list + isec->output_section->index;
11501       /* Steal the link_sec pointer for our list.  */
11502 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
11503       /* This happens to make the list in reverse order,
11504          which is what we want.  */
11505       PREV_SEC (isec) = *list;
11506       *list = isec;
11507     }
11508
11509   if (htab->multi_toc_needed)
11510     {
11511       /* If a code section has a function that uses the TOC then we need
11512          to use the right TOC (obviously).  Also, make sure that .opd gets
11513          the correct TOC value for R_PPC64_TOC relocs that don't have or
11514          can't find their function symbol (shouldn't ever happen now).
11515          Also specially treat .fixup for the linux kernel.  .fixup
11516          contains branches, but only back to the function that hit an
11517          exception.  */
11518       if (isec->has_toc_reloc
11519           || (isec->flags & SEC_CODE) == 0
11520           || strcmp (isec->name, ".fixup") == 0)
11521         {
11522           if (elf_gp (isec->owner) != 0)
11523             htab->toc_curr = elf_gp (isec->owner);
11524         }
11525       else
11526         {
11527           if (!isec->call_check_done
11528               && toc_adjusting_stub_needed (info, isec) < 0)
11529             return FALSE;
11530           /* If we make a local call from this section, ie. a branch
11531              without a following nop, then we have no place to put a
11532              toc restoring insn.  We must use the same toc group as
11533              the callee.
11534              Testing makes_toc_func_call actually tests for *any*
11535              calls to functions that need a good toc pointer.  A more
11536              precise test would be better, as this one will set
11537              incorrect values for pasted .init/.fini fragments.
11538              (Fixed later in check_pasted_section.)  */
11539           if (isec->makes_toc_func_call
11540               && elf_gp (isec->owner) != 0)
11541             htab->toc_curr = elf_gp (isec->owner);
11542         }
11543     }
11544
11545   /* Functions that don't use the TOC can belong in any TOC group.
11546      Use the last TOC base.  */
11547   htab->stub_group[isec->id].toc_off = htab->toc_curr;
11548   return TRUE;
11549 }
11550
11551 /* Check that all .init and .fini sections use the same toc, if they
11552    have toc relocs.  */
11553
11554 static bfd_boolean
11555 check_pasted_section (struct bfd_link_info *info, const char *name)
11556 {
11557   asection *o = bfd_get_section_by_name (info->output_bfd, name);
11558
11559   if (o != NULL)
11560     {
11561       struct ppc_link_hash_table *htab = ppc_hash_table (info);
11562       bfd_vma toc_off = 0;
11563       asection *i;
11564
11565       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11566         if (i->has_toc_reloc)
11567           {
11568             if (toc_off == 0)
11569               toc_off = htab->stub_group[i->id].toc_off;
11570             else if (toc_off != htab->stub_group[i->id].toc_off)
11571               return FALSE;
11572           }
11573
11574       if (toc_off == 0)
11575         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11576           if (i->makes_toc_func_call)
11577             {
11578               toc_off = htab->stub_group[i->id].toc_off;
11579               break;
11580             }
11581
11582       /* Make sure the whole pasted function uses the same toc offset.  */
11583       if (toc_off != 0)
11584         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11585           htab->stub_group[i->id].toc_off = toc_off;
11586     }
11587   return TRUE;
11588 }
11589
11590 bfd_boolean
11591 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11592 {
11593   return (check_pasted_section (info, ".init")
11594           & check_pasted_section (info, ".fini"));
11595 }
11596
11597 /* See whether we can group stub sections together.  Grouping stub
11598    sections may result in fewer stubs.  More importantly, we need to
11599    put all .init* and .fini* stubs at the beginning of the .init or
11600    .fini output sections respectively, because glibc splits the
11601    _init and _fini functions into multiple parts.  Putting a stub in
11602    the middle of a function is not a good idea.  */
11603
11604 static void
11605 group_sections (struct ppc_link_hash_table *htab,
11606                 bfd_size_type stub_group_size,
11607                 bfd_boolean stubs_always_before_branch)
11608 {
11609   asection **list;
11610   bfd_size_type stub14_group_size;
11611   bfd_boolean suppress_size_errors;
11612
11613   suppress_size_errors = FALSE;
11614   stub14_group_size = stub_group_size;
11615   if (stub_group_size == 1)
11616     {
11617       /* Default values.  */
11618       if (stubs_always_before_branch)
11619         {
11620           stub_group_size = 0x1e00000;
11621           stub14_group_size = 0x7800;
11622         }
11623       else
11624         {
11625           stub_group_size = 0x1c00000;
11626           stub14_group_size = 0x7000;
11627         }
11628       suppress_size_errors = TRUE;
11629     }
11630
11631   list = htab->input_list + htab->top_index;
11632   do
11633     {
11634       asection *tail = *list;
11635       while (tail != NULL)
11636         {
11637           asection *curr;
11638           asection *prev;
11639           bfd_size_type total;
11640           bfd_boolean big_sec;
11641           bfd_vma curr_toc;
11642
11643           curr = tail;
11644           total = tail->size;
11645           big_sec = total > (ppc64_elf_section_data (tail) != NULL
11646                              && ppc64_elf_section_data (tail)->has_14bit_branch
11647                              ? stub14_group_size : stub_group_size);
11648           if (big_sec && !suppress_size_errors)
11649             (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11650                                      tail->owner, tail);
11651           curr_toc = htab->stub_group[tail->id].toc_off;
11652
11653           while ((prev = PREV_SEC (curr)) != NULL
11654                  && ((total += curr->output_offset - prev->output_offset)
11655                      < (ppc64_elf_section_data (prev) != NULL
11656                         && ppc64_elf_section_data (prev)->has_14bit_branch
11657                         ? stub14_group_size : stub_group_size))
11658                  && htab->stub_group[prev->id].toc_off == curr_toc)
11659             curr = prev;
11660
11661           /* OK, the size from the start of CURR to the end is less
11662              than stub_group_size and thus can be handled by one stub
11663              section.  (or the tail section is itself larger than
11664              stub_group_size, in which case we may be toast.)  We
11665              should really be keeping track of the total size of stubs
11666              added here, as stubs contribute to the final output
11667              section size.  That's a little tricky, and this way will
11668              only break if stubs added make the total size more than
11669              2^25, ie. for the default stub_group_size, if stubs total
11670              more than 2097152 bytes, or nearly 75000 plt call stubs.  */
11671           do
11672             {
11673               prev = PREV_SEC (tail);
11674               /* Set up this stub group.  */
11675               htab->stub_group[tail->id].link_sec = curr;
11676             }
11677           while (tail != curr && (tail = prev) != NULL);
11678
11679           /* But wait, there's more!  Input sections up to stub_group_size
11680              bytes before the stub section can be handled by it too.
11681              Don't do this if we have a really large section after the
11682              stubs, as adding more stubs increases the chance that
11683              branches may not reach into the stub section.  */
11684           if (!stubs_always_before_branch && !big_sec)
11685             {
11686               total = 0;
11687               while (prev != NULL
11688                      && ((total += tail->output_offset - prev->output_offset)
11689                          < (ppc64_elf_section_data (prev) != NULL
11690                             && ppc64_elf_section_data (prev)->has_14bit_branch
11691                             ? stub14_group_size : stub_group_size))
11692                      && htab->stub_group[prev->id].toc_off == curr_toc)
11693                 {
11694                   tail = prev;
11695                   prev = PREV_SEC (tail);
11696                   htab->stub_group[tail->id].link_sec = curr;
11697                 }
11698             }
11699           tail = prev;
11700         }
11701     }
11702   while (list-- != htab->input_list);
11703   free (htab->input_list);
11704 #undef PREV_SEC
11705 }
11706
11707 static const unsigned char glink_eh_frame_cie[] =
11708 {
11709   0, 0, 0, 16,                          /* length.  */
11710   0, 0, 0, 0,                           /* id.  */
11711   1,                                    /* CIE version.  */
11712   'z', 'R', 0,                          /* Augmentation string.  */
11713   4,                                    /* Code alignment.  */
11714   0x78,                                 /* Data alignment.  */
11715   65,                                   /* RA reg.  */
11716   1,                                    /* Augmentation size.  */
11717   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
11718   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
11719 };
11720
11721 /* Stripping output sections is normally done before dynamic section
11722    symbols have been allocated.  This function is called later, and
11723    handles cases like htab->brlt which is mapped to its own output
11724    section.  */
11725
11726 static void
11727 maybe_strip_output (struct bfd_link_info *info, asection *isec)
11728 {
11729   if (isec->size == 0
11730       && isec->output_section->size == 0
11731       && !(isec->output_section->flags & SEC_KEEP)
11732       && !bfd_section_removed_from_list (info->output_bfd,
11733                                          isec->output_section)
11734       && elf_section_data (isec->output_section)->dynindx == 0)
11735     {
11736       isec->output_section->flags |= SEC_EXCLUDE;
11737       bfd_section_list_remove (info->output_bfd, isec->output_section);
11738       info->output_bfd->section_count--;
11739     }
11740 }
11741
11742 /* Determine and set the size of the stub section for a final link.
11743
11744    The basic idea here is to examine all the relocations looking for
11745    PC-relative calls to a target that is unreachable with a "bl"
11746    instruction.  */
11747
11748 bfd_boolean
11749 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
11750                       bfd_boolean plt_static_chain, int plt_thread_safe,
11751                       int plt_stub_align)
11752 {
11753   bfd_size_type stub_group_size;
11754   bfd_boolean stubs_always_before_branch;
11755   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11756
11757   if (htab == NULL)
11758     return FALSE;
11759
11760   htab->plt_static_chain = plt_static_chain;
11761   htab->plt_stub_align = plt_stub_align;
11762   if (plt_thread_safe == -1 && !info->executable)
11763     plt_thread_safe = 1;
11764   if (!htab->opd_abi)
11765     plt_thread_safe = 0;
11766   else if (plt_thread_safe == -1)
11767     {
11768       static const char *const thread_starter[] =
11769         {
11770           "pthread_create",
11771           /* libstdc++ */
11772           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11773           /* librt */
11774           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11775           "mq_notify", "create_timer",
11776           /* libanl */
11777           "getaddrinfo_a",
11778           /* libgomp */
11779           "GOMP_parallel_start",
11780           "GOMP_parallel_loop_static_start",
11781           "GOMP_parallel_loop_dynamic_start",
11782           "GOMP_parallel_loop_guided_start",
11783           "GOMP_parallel_loop_runtime_start",
11784           "GOMP_parallel_sections_start",
11785         };
11786       unsigned i;
11787
11788       for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
11789         {
11790           struct elf_link_hash_entry *h;
11791           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11792                                     FALSE, FALSE, TRUE);
11793           plt_thread_safe = h != NULL && h->ref_regular;
11794           if (plt_thread_safe)
11795             break;
11796         }
11797     }
11798   htab->plt_thread_safe = plt_thread_safe;
11799   stubs_always_before_branch = group_size < 0;
11800   if (group_size < 0)
11801     stub_group_size = -group_size;
11802   else
11803     stub_group_size = group_size;
11804
11805   group_sections (htab, stub_group_size, stubs_always_before_branch);
11806
11807   while (1)
11808     {
11809       bfd *input_bfd;
11810       unsigned int bfd_indx;
11811       asection *stub_sec;
11812
11813       htab->stub_iteration += 1;
11814
11815       for (input_bfd = info->input_bfds, bfd_indx = 0;
11816            input_bfd != NULL;
11817            input_bfd = input_bfd->link_next, bfd_indx++)
11818         {
11819           Elf_Internal_Shdr *symtab_hdr;
11820           asection *section;
11821           Elf_Internal_Sym *local_syms = NULL;
11822
11823           if (!is_ppc64_elf (input_bfd))
11824             continue;
11825
11826           /* We'll need the symbol table in a second.  */
11827           symtab_hdr = &elf_symtab_hdr (input_bfd);
11828           if (symtab_hdr->sh_info == 0)
11829             continue;
11830
11831           /* Walk over each section attached to the input bfd.  */
11832           for (section = input_bfd->sections;
11833                section != NULL;
11834                section = section->next)
11835             {
11836               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
11837
11838               /* If there aren't any relocs, then there's nothing more
11839                  to do.  */
11840               if ((section->flags & SEC_RELOC) == 0
11841                   || (section->flags & SEC_ALLOC) == 0
11842                   || (section->flags & SEC_LOAD) == 0
11843                   || (section->flags & SEC_CODE) == 0
11844                   || section->reloc_count == 0)
11845                 continue;
11846
11847               /* If this section is a link-once section that will be
11848                  discarded, then don't create any stubs.  */
11849               if (section->output_section == NULL
11850                   || section->output_section->owner != info->output_bfd)
11851                 continue;
11852
11853               /* Get the relocs.  */
11854               internal_relocs
11855                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
11856                                              info->keep_memory);
11857               if (internal_relocs == NULL)
11858                 goto error_ret_free_local;
11859
11860               /* Now examine each relocation.  */
11861               irela = internal_relocs;
11862               irelaend = irela + section->reloc_count;
11863               for (; irela < irelaend; irela++)
11864                 {
11865                   enum elf_ppc64_reloc_type r_type;
11866                   unsigned int r_indx;
11867                   enum ppc_stub_type stub_type;
11868                   struct ppc_stub_hash_entry *stub_entry;
11869                   asection *sym_sec, *code_sec;
11870                   bfd_vma sym_value, code_value;
11871                   bfd_vma destination;
11872                   unsigned long local_off;
11873                   bfd_boolean ok_dest;
11874                   struct ppc_link_hash_entry *hash;
11875                   struct ppc_link_hash_entry *fdh;
11876                   struct elf_link_hash_entry *h;
11877                   Elf_Internal_Sym *sym;
11878                   char *stub_name;
11879                   const asection *id_sec;
11880                   struct _opd_sec_data *opd;
11881                   struct plt_entry *plt_ent;
11882
11883                   r_type = ELF64_R_TYPE (irela->r_info);
11884                   r_indx = ELF64_R_SYM (irela->r_info);
11885
11886                   if (r_type >= R_PPC64_max)
11887                     {
11888                       bfd_set_error (bfd_error_bad_value);
11889                       goto error_ret_free_internal;
11890                     }
11891
11892                   /* Only look for stubs on branch instructions.  */
11893                   if (r_type != R_PPC64_REL24
11894                       && r_type != R_PPC64_REL14
11895                       && r_type != R_PPC64_REL14_BRTAKEN
11896                       && r_type != R_PPC64_REL14_BRNTAKEN)
11897                     continue;
11898
11899                   /* Now determine the call target, its name, value,
11900                      section.  */
11901                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11902                                   r_indx, input_bfd))
11903                     goto error_ret_free_internal;
11904                   hash = (struct ppc_link_hash_entry *) h;
11905
11906                   ok_dest = FALSE;
11907                   fdh = NULL;
11908                   sym_value = 0;
11909                   if (hash == NULL)
11910                     {
11911                       sym_value = sym->st_value;
11912                       ok_dest = TRUE;
11913                     }
11914                   else if (hash->elf.root.type == bfd_link_hash_defined
11915                            || hash->elf.root.type == bfd_link_hash_defweak)
11916                     {
11917                       sym_value = hash->elf.root.u.def.value;
11918                       if (sym_sec->output_section != NULL)
11919                         ok_dest = TRUE;
11920                     }
11921                   else if (hash->elf.root.type == bfd_link_hash_undefweak
11922                            || hash->elf.root.type == bfd_link_hash_undefined)
11923                     {
11924                       /* Recognise an old ABI func code entry sym, and
11925                          use the func descriptor sym instead if it is
11926                          defined.  */
11927                       if (hash->elf.root.root.string[0] == '.'
11928                           && (fdh = lookup_fdh (hash, htab)) != NULL)
11929                         {
11930                           if (fdh->elf.root.type == bfd_link_hash_defined
11931                               || fdh->elf.root.type == bfd_link_hash_defweak)
11932                             {
11933                               sym_sec = fdh->elf.root.u.def.section;
11934                               sym_value = fdh->elf.root.u.def.value;
11935                               if (sym_sec->output_section != NULL)
11936                                 ok_dest = TRUE;
11937                             }
11938                           else
11939                             fdh = NULL;
11940                         }
11941                     }
11942                   else
11943                     {
11944                       bfd_set_error (bfd_error_bad_value);
11945                       goto error_ret_free_internal;
11946                     }
11947
11948                   destination = 0;
11949                   local_off = 0;
11950                   if (ok_dest)
11951                     {
11952                       sym_value += irela->r_addend;
11953                       destination = (sym_value
11954                                      + sym_sec->output_offset
11955                                      + sym_sec->output_section->vma);
11956                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
11957                                                             ? hash->elf.other
11958                                                             : sym->st_other);
11959                     }
11960
11961                   code_sec = sym_sec;
11962                   code_value = sym_value;
11963                   opd = get_opd_info (sym_sec);
11964                   if (opd != NULL)
11965                     {
11966                       bfd_vma dest;
11967
11968                       if (hash == NULL && opd->adjust != NULL)
11969                         {
11970                           long adjust = opd->adjust[sym_value / 8];
11971                           if (adjust == -1)
11972                             continue;
11973                           code_value += adjust;
11974                           sym_value += adjust;
11975                         }
11976                       dest = opd_entry_value (sym_sec, sym_value,
11977                                               &code_sec, &code_value, FALSE);
11978                       if (dest != (bfd_vma) -1)
11979                         {
11980                           destination = dest;
11981                           if (fdh != NULL)
11982                             {
11983                               /* Fixup old ABI sym to point at code
11984                                  entry.  */
11985                               hash->elf.root.type = bfd_link_hash_defweak;
11986                               hash->elf.root.u.def.section = code_sec;
11987                               hash->elf.root.u.def.value = code_value;
11988                             }
11989                         }
11990                     }
11991
11992                   /* Determine what (if any) linker stub is needed.  */
11993                   plt_ent = NULL;
11994                   stub_type = ppc_type_of_stub (section, irela, &hash,
11995                                                 &plt_ent, destination,
11996                                                 local_off);
11997
11998                   if (stub_type != ppc_stub_plt_call)
11999                     {
12000                       /* Check whether we need a TOC adjusting stub.
12001                          Since the linker pastes together pieces from
12002                          different object files when creating the
12003                          _init and _fini functions, it may be that a
12004                          call to what looks like a local sym is in
12005                          fact a call needing a TOC adjustment.  */
12006                       if (code_sec != NULL
12007                           && code_sec->output_section != NULL
12008                           && (htab->stub_group[code_sec->id].toc_off
12009                               != htab->stub_group[section->id].toc_off)
12010                           && (code_sec->has_toc_reloc
12011                               || code_sec->makes_toc_func_call))
12012                         stub_type = ppc_stub_long_branch_r2off;
12013                     }
12014
12015                   if (stub_type == ppc_stub_none)
12016                     continue;
12017
12018                   /* __tls_get_addr calls might be eliminated.  */
12019                   if (stub_type != ppc_stub_plt_call
12020                       && hash != NULL
12021                       && (hash == htab->tls_get_addr
12022                           || hash == htab->tls_get_addr_fd)
12023                       && section->has_tls_reloc
12024                       && irela != internal_relocs)
12025                     {
12026                       /* Get tls info.  */
12027                       unsigned char *tls_mask;
12028
12029                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12030                                          irela - 1, input_bfd))
12031                         goto error_ret_free_internal;
12032                       if (*tls_mask != 0)
12033                         continue;
12034                     }
12035
12036                   if (stub_type == ppc_stub_plt_call
12037                       && irela + 1 < irelaend
12038                       && irela[1].r_offset == irela->r_offset + 4
12039                       && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12040                     {
12041                       if (!tocsave_find (htab, INSERT,
12042                                          &local_syms, irela + 1, input_bfd))
12043                         goto error_ret_free_internal;
12044                     }
12045                   else if (stub_type == ppc_stub_plt_call)
12046                     stub_type = ppc_stub_plt_call_r2save;
12047
12048                   /* Support for grouping stub sections.  */
12049                   id_sec = htab->stub_group[section->id].link_sec;
12050
12051                   /* Get the name of this stub.  */
12052                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12053                   if (!stub_name)
12054                     goto error_ret_free_internal;
12055
12056                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12057                                                      stub_name, FALSE, FALSE);
12058                   if (stub_entry != NULL)
12059                     {
12060                       /* The proper stub has already been created.  */
12061                       free (stub_name);
12062                       if (stub_type == ppc_stub_plt_call_r2save)
12063                         stub_entry->stub_type = stub_type;
12064                       continue;
12065                     }
12066
12067                   stub_entry = ppc_add_stub (stub_name, section, info);
12068                   if (stub_entry == NULL)
12069                     {
12070                       free (stub_name);
12071                     error_ret_free_internal:
12072                       if (elf_section_data (section)->relocs == NULL)
12073                         free (internal_relocs);
12074                     error_ret_free_local:
12075                       if (local_syms != NULL
12076                           && (symtab_hdr->contents
12077                               != (unsigned char *) local_syms))
12078                         free (local_syms);
12079                       return FALSE;
12080                     }
12081
12082                   stub_entry->stub_type = stub_type;
12083                   if (stub_type != ppc_stub_plt_call
12084                       && stub_type != ppc_stub_plt_call_r2save)
12085                     {
12086                       stub_entry->target_value = code_value;
12087                       stub_entry->target_section = code_sec;
12088                     }
12089                   else
12090                     {
12091                       stub_entry->target_value = sym_value;
12092                       stub_entry->target_section = sym_sec;
12093                     }
12094                   stub_entry->h = hash;
12095                   stub_entry->plt_ent = plt_ent;
12096                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
12097
12098                   if (stub_entry->h != NULL)
12099                     htab->stub_globals += 1;
12100                 }
12101
12102               /* We're done with the internal relocs, free them.  */
12103               if (elf_section_data (section)->relocs != internal_relocs)
12104                 free (internal_relocs);
12105             }
12106
12107           if (local_syms != NULL
12108               && symtab_hdr->contents != (unsigned char *) local_syms)
12109             {
12110               if (!info->keep_memory)
12111                 free (local_syms);
12112               else
12113                 symtab_hdr->contents = (unsigned char *) local_syms;
12114             }
12115         }
12116
12117       /* We may have added some stubs.  Find out the new size of the
12118          stub sections.  */
12119       for (stub_sec = htab->stub_bfd->sections;
12120            stub_sec != NULL;
12121            stub_sec = stub_sec->next)
12122         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12123           {
12124             stub_sec->rawsize = stub_sec->size;
12125             stub_sec->size = 0;
12126             stub_sec->reloc_count = 0;
12127             stub_sec->flags &= ~SEC_RELOC;
12128           }
12129
12130       htab->brlt->size = 0;
12131       htab->brlt->reloc_count = 0;
12132       htab->brlt->flags &= ~SEC_RELOC;
12133       if (htab->relbrlt != NULL)
12134         htab->relbrlt->size = 0;
12135
12136       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12137
12138       if (info->emitrelocations
12139           && htab->glink != NULL && htab->glink->size != 0)
12140         {
12141           htab->glink->reloc_count = 1;
12142           htab->glink->flags |= SEC_RELOC;
12143         }
12144
12145       if (htab->glink_eh_frame != NULL
12146           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12147           && htab->glink_eh_frame->output_section->size != 0)
12148         {
12149           size_t size = 0, align;
12150
12151           for (stub_sec = htab->stub_bfd->sections;
12152                stub_sec != NULL;
12153                stub_sec = stub_sec->next)
12154             if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12155               size += 20;
12156           if (htab->glink != NULL && htab->glink->size != 0)
12157             size += 24;
12158           if (size != 0)
12159             size += sizeof (glink_eh_frame_cie);
12160           align = 1;
12161           align <<= htab->glink_eh_frame->output_section->alignment_power;
12162           align -= 1;
12163           size = (size + align) & ~align;
12164           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12165           htab->glink_eh_frame->size = size;
12166         }
12167
12168       if (htab->plt_stub_align != 0)
12169         for (stub_sec = htab->stub_bfd->sections;
12170              stub_sec != NULL;
12171              stub_sec = stub_sec->next)
12172           if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12173             stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12174                               & (-1 << htab->plt_stub_align));
12175
12176       for (stub_sec = htab->stub_bfd->sections;
12177            stub_sec != NULL;
12178            stub_sec = stub_sec->next)
12179         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12180             && stub_sec->rawsize != stub_sec->size)
12181           break;
12182
12183       /* Exit from this loop when no stubs have been added, and no stubs
12184          have changed size.  */
12185       if (stub_sec == NULL
12186           && (htab->glink_eh_frame == NULL
12187               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12188         break;
12189
12190       /* Ask the linker to do its stuff.  */
12191       (*htab->layout_sections_again) ();
12192     }
12193
12194   maybe_strip_output (info, htab->brlt);
12195   if (htab->glink_eh_frame != NULL)
12196     maybe_strip_output (info, htab->glink_eh_frame);
12197
12198   return TRUE;
12199 }
12200
12201 /* Called after we have determined section placement.  If sections
12202    move, we'll be called again.  Provide a value for TOCstart.  */
12203
12204 bfd_vma
12205 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12206 {
12207   asection *s;
12208   bfd_vma TOCstart;
12209
12210   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12211      order.  The TOC starts where the first of these sections starts.  */
12212   s = bfd_get_section_by_name (obfd, ".got");
12213   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12214     s = bfd_get_section_by_name (obfd, ".toc");
12215   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12216     s = bfd_get_section_by_name (obfd, ".tocbss");
12217   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12218     s = bfd_get_section_by_name (obfd, ".plt");
12219   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12220     {
12221       /* This may happen for
12222          o  references to TOC base (SYM@toc / TOC[tc0]) without a
12223          .toc directive
12224          o  bad linker script
12225          o --gc-sections and empty TOC sections
12226
12227          FIXME: Warn user?  */
12228
12229       /* Look for a likely section.  We probably won't even be
12230          using TOCstart.  */
12231       for (s = obfd->sections; s != NULL; s = s->next)
12232         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12233                          | SEC_EXCLUDE))
12234             == (SEC_ALLOC | SEC_SMALL_DATA))
12235           break;
12236       if (s == NULL)
12237         for (s = obfd->sections; s != NULL; s = s->next)
12238           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12239               == (SEC_ALLOC | SEC_SMALL_DATA))
12240             break;
12241       if (s == NULL)
12242         for (s = obfd->sections; s != NULL; s = s->next)
12243           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12244               == SEC_ALLOC)
12245             break;
12246       if (s == NULL)
12247         for (s = obfd->sections; s != NULL; s = s->next)
12248           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12249             break;
12250     }
12251
12252   TOCstart = 0;
12253   if (s != NULL)
12254     TOCstart = s->output_section->vma + s->output_offset;
12255
12256   _bfd_set_gp_value (obfd, TOCstart);
12257
12258   if (info != NULL && s != NULL && is_ppc64_elf (obfd))
12259     {
12260       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12261
12262       if (htab != NULL
12263           && htab->elf.hgot != NULL)
12264         {
12265           htab->elf.hgot->type = STT_OBJECT;
12266           htab->elf.hgot->root.type = bfd_link_hash_defined;
12267           htab->elf.hgot->root.u.def.value = TOC_BASE_OFF;
12268           htab->elf.hgot->root.u.def.section = s;
12269         }
12270     }
12271   return TOCstart;
12272 }
12273
12274 /* Build all the stubs associated with the current output file.
12275    The stubs are kept in a hash table attached to the main linker
12276    hash table.  This function is called via gldelf64ppc_finish.  */
12277
12278 bfd_boolean
12279 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
12280                        struct bfd_link_info *info,
12281                        char **stats)
12282 {
12283   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12284   asection *stub_sec;
12285   bfd_byte *p;
12286   int stub_sec_count = 0;
12287
12288   if (htab == NULL)
12289     return FALSE;
12290
12291   htab->emit_stub_syms = emit_stub_syms;
12292
12293   /* Allocate memory to hold the linker stubs.  */
12294   for (stub_sec = htab->stub_bfd->sections;
12295        stub_sec != NULL;
12296        stub_sec = stub_sec->next)
12297     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12298         && stub_sec->size != 0)
12299       {
12300         stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
12301         if (stub_sec->contents == NULL)
12302           return FALSE;
12303         /* We want to check that built size is the same as calculated
12304            size.  rawsize is a convenient location to use.  */
12305         stub_sec->rawsize = stub_sec->size;
12306         stub_sec->size = 0;
12307       }
12308
12309   if (htab->glink != NULL && htab->glink->size != 0)
12310     {
12311       unsigned int indx;
12312       bfd_vma plt0;
12313
12314       /* Build the .glink plt call stub.  */
12315       if (htab->emit_stub_syms)
12316         {
12317           struct elf_link_hash_entry *h;
12318           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12319                                     TRUE, FALSE, FALSE);
12320           if (h == NULL)
12321             return FALSE;
12322           if (h->root.type == bfd_link_hash_new)
12323             {
12324               h->root.type = bfd_link_hash_defined;
12325               h->root.u.def.section = htab->glink;
12326               h->root.u.def.value = 8;
12327               h->ref_regular = 1;
12328               h->def_regular = 1;
12329               h->ref_regular_nonweak = 1;
12330               h->forced_local = 1;
12331               h->non_elf = 0;
12332             }
12333         }
12334       plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
12335       if (info->emitrelocations)
12336         {
12337           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12338           if (r == NULL)
12339             return FALSE;
12340           r->r_offset = (htab->glink->output_offset
12341                          + htab->glink->output_section->vma);
12342           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12343           r->r_addend = plt0;
12344         }
12345       p = htab->glink->contents;
12346       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
12347       bfd_put_64 (htab->glink->owner, plt0, p);
12348       p += 8;
12349       if (htab->opd_abi)
12350         {
12351           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12352           p += 4;
12353           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12354           p += 4;
12355           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12356           p += 4;
12357           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12358           p += 4;
12359           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12360           p += 4;
12361           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12362           p += 4;
12363           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12364           p += 4;
12365           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12366           p += 4;
12367           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12368           p += 4;
12369           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12370           p += 4;
12371         }
12372       else
12373         {
12374           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
12375           p += 4;
12376           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12377           p += 4;
12378           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12379           p += 4;
12380           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12381           p += 4;
12382           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
12383           p += 4;
12384           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
12385           p += 4;
12386           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12387           p += 4;
12388           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
12389           p += 4;
12390           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12391           p += 4;
12392           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
12393           p += 4;
12394           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12395           p += 4;
12396           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
12397           p += 4;
12398         }
12399       bfd_put_32 (htab->glink->owner, BCTR, p);
12400       p += 4;
12401       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
12402         {
12403           bfd_put_32 (htab->glink->owner, NOP, p);
12404           p += 4;
12405         }
12406
12407       /* Build the .glink lazy link call stubs.  */
12408       indx = 0;
12409       while (p < htab->glink->contents + htab->glink->size)
12410         {
12411           if (htab->opd_abi)
12412             {
12413               if (indx < 0x8000)
12414                 {
12415                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12416                   p += 4;
12417                 }
12418               else
12419                 {
12420                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12421                   p += 4;
12422                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
12423                               p);
12424                   p += 4;
12425                 }
12426             }
12427           bfd_put_32 (htab->glink->owner,
12428                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
12429           indx++;
12430           p += 4;
12431         }
12432       htab->glink->rawsize = p - htab->glink->contents;
12433     }
12434
12435   if (htab->brlt->size != 0)
12436     {
12437       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
12438                                          htab->brlt->size);
12439       if (htab->brlt->contents == NULL)
12440         return FALSE;
12441     }
12442   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
12443     {
12444       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
12445                                             htab->relbrlt->size);
12446       if (htab->relbrlt->contents == NULL)
12447         return FALSE;
12448     }
12449
12450   if (htab->glink_eh_frame != NULL
12451       && htab->glink_eh_frame->size != 0)
12452     {
12453       bfd_vma val;
12454       bfd_byte *last_fde;
12455       size_t last_fde_len, size, align, pad;
12456
12457       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12458       if (p == NULL)
12459         return FALSE;
12460       htab->glink_eh_frame->contents = p;
12461       last_fde = p;
12462
12463       htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12464
12465       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12466       /* CIE length (rewrite in case little-endian).  */
12467       last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12468       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12469       p += sizeof (glink_eh_frame_cie);
12470
12471       for (stub_sec = htab->stub_bfd->sections;
12472            stub_sec != NULL;
12473            stub_sec = stub_sec->next)
12474         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12475           {
12476             last_fde = p;
12477             last_fde_len = 16;
12478             /* FDE length.  */
12479             bfd_put_32 (htab->elf.dynobj, 16, p);
12480             p += 4;
12481             /* CIE pointer.  */
12482             val = p - htab->glink_eh_frame->contents;
12483             bfd_put_32 (htab->elf.dynobj, val, p);
12484             p += 4;
12485             /* Offset to stub section.  */
12486             val = (stub_sec->output_section->vma
12487                    + stub_sec->output_offset);
12488             val -= (htab->glink_eh_frame->output_section->vma
12489                     + htab->glink_eh_frame->output_offset);
12490             val -= p - htab->glink_eh_frame->contents;
12491             if (val + 0x80000000 > 0xffffffff)
12492               {
12493                 info->callbacks->einfo
12494                   (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
12495                    stub_sec->name);
12496                 return FALSE;
12497               }
12498             bfd_put_32 (htab->elf.dynobj, val, p);
12499             p += 4;
12500             /* stub section size.  */
12501             bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
12502             p += 4;
12503             /* Augmentation.  */
12504             p += 1;
12505             /* Pad.  */
12506             p += 3;
12507           }
12508       if (htab->glink != NULL && htab->glink->size != 0)
12509         {
12510           last_fde = p;
12511           last_fde_len = 20;
12512           /* FDE length.  */
12513           bfd_put_32 (htab->elf.dynobj, 20, p);
12514           p += 4;
12515           /* CIE pointer.  */
12516           val = p - htab->glink_eh_frame->contents;
12517           bfd_put_32 (htab->elf.dynobj, val, p);
12518           p += 4;
12519           /* Offset to .glink.  */
12520           val = (htab->glink->output_section->vma
12521                  + htab->glink->output_offset
12522                  + 8);
12523           val -= (htab->glink_eh_frame->output_section->vma
12524                   + htab->glink_eh_frame->output_offset);
12525           val -= p - htab->glink_eh_frame->contents;
12526           if (val + 0x80000000 > 0xffffffff)
12527             {
12528               info->callbacks->einfo
12529                 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
12530                  htab->glink->name);
12531               return FALSE;
12532             }
12533           bfd_put_32 (htab->elf.dynobj, val, p);
12534           p += 4;
12535           /* .glink size.  */
12536           bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
12537           p += 4;
12538           /* Augmentation.  */
12539           p += 1;
12540
12541           *p++ = DW_CFA_advance_loc + 1;
12542           *p++ = DW_CFA_register;
12543           *p++ = 65;
12544           *p++ = 12;
12545           *p++ = DW_CFA_advance_loc + 4;
12546           *p++ = DW_CFA_restore_extended;
12547           *p++ = 65;
12548         }
12549       /* Subsume any padding into the last FDE if user .eh_frame
12550          sections are aligned more than glink_eh_frame.  Otherwise any
12551          zero padding will be seen as a terminator.  */
12552       size = p - htab->glink_eh_frame->contents;
12553       align = 1;
12554       align <<= htab->glink_eh_frame->output_section->alignment_power;
12555       align -= 1;
12556       pad = ((size + align) & ~align) - size;
12557       htab->glink_eh_frame->size = size + pad;
12558       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12559     }
12560
12561   /* Build the stubs as directed by the stub hash table.  */
12562   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
12563
12564   if (htab->relbrlt != NULL)
12565     htab->relbrlt->reloc_count = 0;
12566
12567   if (htab->plt_stub_align != 0)
12568     for (stub_sec = htab->stub_bfd->sections;
12569          stub_sec != NULL;
12570          stub_sec = stub_sec->next)
12571       if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12572         stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12573                           & (-1 << htab->plt_stub_align));
12574
12575   for (stub_sec = htab->stub_bfd->sections;
12576        stub_sec != NULL;
12577        stub_sec = stub_sec->next)
12578     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12579       {
12580         stub_sec_count += 1;
12581         if (stub_sec->rawsize != stub_sec->size)
12582           break;
12583       }
12584
12585   if (stub_sec != NULL
12586       || htab->glink->rawsize != htab->glink->size
12587       || (htab->glink_eh_frame != NULL
12588           && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
12589     {
12590       htab->stub_error = TRUE;
12591       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
12592     }
12593
12594   if (htab->stub_error)
12595     return FALSE;
12596
12597   if (stats != NULL)
12598     {
12599       *stats = bfd_malloc (500);
12600       if (*stats == NULL)
12601         return FALSE;
12602
12603       sprintf (*stats, _("linker stubs in %u group%s\n"
12604                          "  branch       %lu\n"
12605                          "  toc adjust   %lu\n"
12606                          "  long branch  %lu\n"
12607                          "  long toc adj %lu\n"
12608                          "  plt call     %lu\n"
12609                          "  plt call toc %lu"),
12610                stub_sec_count,
12611                stub_sec_count == 1 ? "" : "s",
12612                htab->stub_count[ppc_stub_long_branch - 1],
12613                htab->stub_count[ppc_stub_long_branch_r2off - 1],
12614                htab->stub_count[ppc_stub_plt_branch - 1],
12615                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
12616                htab->stub_count[ppc_stub_plt_call - 1],
12617                htab->stub_count[ppc_stub_plt_call_r2save - 1]);
12618     }
12619   return TRUE;
12620 }
12621
12622 /* This function undoes the changes made by add_symbol_adjust.  */
12623
12624 static bfd_boolean
12625 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12626 {
12627   struct ppc_link_hash_entry *eh;
12628
12629   if (h->root.type == bfd_link_hash_indirect)
12630     return TRUE;
12631
12632   eh = (struct ppc_link_hash_entry *) h;
12633   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
12634     return TRUE;
12635
12636   eh->elf.root.type = bfd_link_hash_undefined;
12637   return TRUE;
12638 }
12639
12640 void
12641 ppc64_elf_restore_symbols (struct bfd_link_info *info)
12642 {
12643   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12644
12645   if (htab != NULL)
12646     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
12647 }
12648
12649 /* What to do when ld finds relocations against symbols defined in
12650    discarded sections.  */
12651
12652 static unsigned int
12653 ppc64_elf_action_discarded (asection *sec)
12654 {
12655   if (strcmp (".opd", sec->name) == 0)
12656     return 0;
12657
12658   if (strcmp (".toc", sec->name) == 0)
12659     return 0;
12660
12661   if (strcmp (".toc1", sec->name) == 0)
12662     return 0;
12663
12664   return _bfd_elf_default_action_discarded (sec);
12665 }
12666
12667 /* The RELOCATE_SECTION function is called by the ELF backend linker
12668    to handle the relocations for a section.
12669
12670    The relocs are always passed as Rela structures; if the section
12671    actually uses Rel structures, the r_addend field will always be
12672    zero.
12673
12674    This function is responsible for adjust the section contents as
12675    necessary, and (if using Rela relocs and generating a
12676    relocatable output file) adjusting the reloc addend as
12677    necessary.
12678
12679    This function does not have to worry about setting the reloc
12680    address or the reloc symbol index.
12681
12682    LOCAL_SYMS is a pointer to the swapped in local symbols.
12683
12684    LOCAL_SECTIONS is an array giving the section in the input file
12685    corresponding to the st_shndx field of each local symbol.
12686
12687    The global hash table entry for the global symbols can be found
12688    via elf_sym_hashes (input_bfd).
12689
12690    When generating relocatable output, this function must handle
12691    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
12692    going to be the section symbol corresponding to the output
12693    section, which means that the addend must be adjusted
12694    accordingly.  */
12695
12696 static bfd_boolean
12697 ppc64_elf_relocate_section (bfd *output_bfd,
12698                             struct bfd_link_info *info,
12699                             bfd *input_bfd,
12700                             asection *input_section,
12701                             bfd_byte *contents,
12702                             Elf_Internal_Rela *relocs,
12703                             Elf_Internal_Sym *local_syms,
12704                             asection **local_sections)
12705 {
12706   struct ppc_link_hash_table *htab;
12707   Elf_Internal_Shdr *symtab_hdr;
12708   struct elf_link_hash_entry **sym_hashes;
12709   Elf_Internal_Rela *rel;
12710   Elf_Internal_Rela *relend;
12711   Elf_Internal_Rela outrel;
12712   bfd_byte *loc;
12713   struct got_entry **local_got_ents;
12714   bfd_vma TOCstart;
12715   bfd_boolean ret = TRUE;
12716   bfd_boolean is_opd;
12717   /* Assume 'at' branch hints.  */
12718   bfd_boolean is_isa_v2 = TRUE;
12719   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
12720
12721   /* Initialize howto table if needed.  */
12722   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
12723     ppc_howto_init ();
12724
12725   htab = ppc_hash_table (info);
12726   if (htab == NULL)
12727     return FALSE;
12728
12729   /* Don't relocate stub sections.  */
12730   if (input_section->owner == htab->stub_bfd)
12731     return TRUE;
12732
12733   BFD_ASSERT (is_ppc64_elf (input_bfd));
12734
12735   local_got_ents = elf_local_got_ents (input_bfd);
12736   TOCstart = elf_gp (output_bfd);
12737   symtab_hdr = &elf_symtab_hdr (input_bfd);
12738   sym_hashes = elf_sym_hashes (input_bfd);
12739   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
12740
12741   rel = relocs;
12742   relend = relocs + input_section->reloc_count;
12743   for (; rel < relend; rel++)
12744     {
12745       enum elf_ppc64_reloc_type r_type;
12746       bfd_vma addend;
12747       bfd_reloc_status_type r;
12748       Elf_Internal_Sym *sym;
12749       asection *sec;
12750       struct elf_link_hash_entry *h_elf;
12751       struct ppc_link_hash_entry *h;
12752       struct ppc_link_hash_entry *fdh;
12753       const char *sym_name;
12754       unsigned long r_symndx, toc_symndx;
12755       bfd_vma toc_addend;
12756       unsigned char tls_mask, tls_gd, tls_type;
12757       unsigned char sym_type;
12758       bfd_vma relocation;
12759       bfd_boolean unresolved_reloc;
12760       bfd_boolean warned;
12761       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
12762       unsigned int insn;
12763       unsigned int mask;
12764       struct ppc_stub_hash_entry *stub_entry;
12765       bfd_vma max_br_offset;
12766       bfd_vma from;
12767       const Elf_Internal_Rela orig_rel = *rel;
12768
12769       r_type = ELF64_R_TYPE (rel->r_info);
12770       r_symndx = ELF64_R_SYM (rel->r_info);
12771
12772       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
12773          symbol of the previous ADDR64 reloc.  The symbol gives us the
12774          proper TOC base to use.  */
12775       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
12776           && rel != relocs
12777           && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
12778           && is_opd)
12779         r_symndx = ELF64_R_SYM (rel[-1].r_info);
12780
12781       sym = NULL;
12782       sec = NULL;
12783       h_elf = NULL;
12784       sym_name = NULL;
12785       unresolved_reloc = FALSE;
12786       warned = FALSE;
12787
12788       if (r_symndx < symtab_hdr->sh_info)
12789         {
12790           /* It's a local symbol.  */
12791           struct _opd_sec_data *opd;
12792
12793           sym = local_syms + r_symndx;
12794           sec = local_sections[r_symndx];
12795           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
12796           sym_type = ELF64_ST_TYPE (sym->st_info);
12797           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
12798           opd = get_opd_info (sec);
12799           if (opd != NULL && opd->adjust != NULL)
12800             {
12801               long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
12802               if (adjust == -1)
12803                 relocation = 0;
12804               else
12805                 {
12806                   /* If this is a relocation against the opd section sym
12807                      and we have edited .opd, adjust the reloc addend so
12808                      that ld -r and ld --emit-relocs output is correct.
12809                      If it is a reloc against some other .opd symbol,
12810                      then the symbol value will be adjusted later.  */
12811                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
12812                     rel->r_addend += adjust;
12813                   else
12814                     relocation += adjust;
12815                 }
12816             }
12817         }
12818       else
12819         {
12820           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
12821                                    r_symndx, symtab_hdr, sym_hashes,
12822                                    h_elf, sec, relocation,
12823                                    unresolved_reloc, warned);
12824           sym_name = h_elf->root.root.string;
12825           sym_type = h_elf->type;
12826           if (sec != NULL
12827               && sec->owner == output_bfd
12828               && strcmp (sec->name, ".opd") == 0)
12829             {
12830               /* This is a symbol defined in a linker script.  All
12831                  such are defined in output sections, even those
12832                  defined by simple assignment from a symbol defined in
12833                  an input section.  Transfer the symbol to an
12834                  appropriate input .opd section, so that a branch to
12835                  this symbol will be mapped to the location specified
12836                  by the opd entry.  */
12837               struct bfd_link_order *lo;
12838               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
12839                 if (lo->type == bfd_indirect_link_order)
12840                   {
12841                     asection *isec = lo->u.indirect.section;
12842                     if (h_elf->root.u.def.value >= isec->output_offset
12843                         && h_elf->root.u.def.value < (isec->output_offset
12844                                                       + isec->size))
12845                       {
12846                         h_elf->root.u.def.value -= isec->output_offset;
12847                         h_elf->root.u.def.section = isec;
12848                         sec = isec;
12849                         break;
12850                       }
12851                   }
12852             }
12853         }
12854       h = (struct ppc_link_hash_entry *) h_elf;
12855
12856       if (sec != NULL && discarded_section (sec))
12857         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
12858                                          rel, 1, relend,
12859                                          ppc64_elf_howto_table[r_type], 0,
12860                                          contents);
12861
12862       if (info->relocatable)
12863         continue;
12864
12865       if (h != NULL && &h->elf == htab->elf.hgot)
12866         {
12867           relocation = (TOCstart
12868                         + htab->stub_group[input_section->id].toc_off);
12869           sec = bfd_abs_section_ptr;
12870           unresolved_reloc = FALSE;
12871         }
12872
12873       /* TLS optimizations.  Replace instruction sequences and relocs
12874          based on information we collected in tls_optimize.  We edit
12875          RELOCS so that --emit-relocs will output something sensible
12876          for the final instruction stream.  */
12877       tls_mask = 0;
12878       tls_gd = 0;
12879       toc_symndx = 0;
12880       if (h != NULL)
12881         tls_mask = h->tls_mask;
12882       else if (local_got_ents != NULL)
12883         {
12884           struct plt_entry **local_plt = (struct plt_entry **)
12885             (local_got_ents + symtab_hdr->sh_info);
12886           unsigned char *lgot_masks = (unsigned char *)
12887             (local_plt + symtab_hdr->sh_info);
12888           tls_mask = lgot_masks[r_symndx];
12889         }
12890       if (tls_mask == 0
12891           && (r_type == R_PPC64_TLS
12892               || r_type == R_PPC64_TLSGD
12893               || r_type == R_PPC64_TLSLD))
12894         {
12895           /* Check for toc tls entries.  */
12896           unsigned char *toc_tls;
12897
12898           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12899                              &local_syms, rel, input_bfd))
12900             return FALSE;
12901
12902           if (toc_tls)
12903             tls_mask = *toc_tls;
12904         }
12905
12906       /* Check that tls relocs are used with tls syms, and non-tls
12907          relocs are used with non-tls syms.  */
12908       if (r_symndx != STN_UNDEF
12909           && r_type != R_PPC64_NONE
12910           && (h == NULL
12911               || h->elf.root.type == bfd_link_hash_defined
12912               || h->elf.root.type == bfd_link_hash_defweak)
12913           && (IS_PPC64_TLS_RELOC (r_type)
12914               != (sym_type == STT_TLS
12915                   || (sym_type == STT_SECTION
12916                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
12917         {
12918           if (tls_mask != 0
12919               && (r_type == R_PPC64_TLS
12920                   || r_type == R_PPC64_TLSGD
12921                   || r_type == R_PPC64_TLSLD))
12922             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
12923             ;
12924           else
12925             info->callbacks->einfo
12926               (!IS_PPC64_TLS_RELOC (r_type)
12927                ? _("%P: %H: %s used with TLS symbol `%T'\n")
12928                : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
12929                input_bfd, input_section, rel->r_offset,
12930                ppc64_elf_howto_table[r_type]->name,
12931                sym_name);
12932         }
12933
12934       /* Ensure reloc mapping code below stays sane.  */
12935       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
12936           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
12937           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
12938           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
12939           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
12940           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
12941           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
12942           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
12943           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
12944           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
12945         abort ();
12946
12947       switch (r_type)
12948         {
12949         default:
12950           break;
12951
12952         case R_PPC64_LO_DS_OPT:
12953           insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12954           if ((insn & (0x3f << 26)) != 58u << 26)
12955             abort ();
12956           insn += (14u << 26) - (58u << 26);
12957           bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12958           r_type = R_PPC64_TOC16_LO;
12959           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12960           break;
12961
12962         case R_PPC64_TOC16:
12963         case R_PPC64_TOC16_LO:
12964         case R_PPC64_TOC16_DS:
12965         case R_PPC64_TOC16_LO_DS:
12966           {
12967             /* Check for toc tls entries.  */
12968             unsigned char *toc_tls;
12969             int retval;
12970
12971             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12972                                    &local_syms, rel, input_bfd);
12973             if (retval == 0)
12974               return FALSE;
12975
12976             if (toc_tls)
12977               {
12978                 tls_mask = *toc_tls;
12979                 if (r_type == R_PPC64_TOC16_DS
12980                     || r_type == R_PPC64_TOC16_LO_DS)
12981                   {
12982                     if (tls_mask != 0
12983                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
12984                       goto toctprel;
12985                   }
12986                 else
12987                   {
12988                     /* If we found a GD reloc pair, then we might be
12989                        doing a GD->IE transition.  */
12990                     if (retval == 2)
12991                       {
12992                         tls_gd = TLS_TPRELGD;
12993                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12994                           goto tls_ldgd_opt;
12995                       }
12996                     else if (retval == 3)
12997                       {
12998                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12999                           goto tls_ldgd_opt;
13000                       }
13001                   }
13002               }
13003           }
13004           break;
13005
13006         case R_PPC64_GOT_TPREL16_HI:
13007         case R_PPC64_GOT_TPREL16_HA:
13008           if (tls_mask != 0
13009               && (tls_mask & TLS_TPREL) == 0)
13010             {
13011               rel->r_offset -= d_offset;
13012               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13013               r_type = R_PPC64_NONE;
13014               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13015             }
13016           break;
13017
13018         case R_PPC64_GOT_TPREL16_DS:
13019         case R_PPC64_GOT_TPREL16_LO_DS:
13020           if (tls_mask != 0
13021               && (tls_mask & TLS_TPREL) == 0)
13022             {
13023             toctprel:
13024               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
13025               insn &= 31 << 21;
13026               insn |= 0x3c0d0000;       /* addis 0,13,0 */
13027               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
13028               r_type = R_PPC64_TPREL16_HA;
13029               if (toc_symndx != 0)
13030                 {
13031                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13032                   rel->r_addend = toc_addend;
13033                   /* We changed the symbol.  Start over in order to
13034                      get h, sym, sec etc. right.  */
13035                   rel--;
13036                   continue;
13037                 }
13038               else
13039                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13040             }
13041           break;
13042
13043         case R_PPC64_TLS:
13044           if (tls_mask != 0
13045               && (tls_mask & TLS_TPREL) == 0)
13046             {
13047               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
13048               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13049               if (insn == 0)
13050                 abort ();
13051               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
13052               /* Was PPC64_TLS which sits on insn boundary, now
13053                  PPC64_TPREL16_LO which is at low-order half-word.  */
13054               rel->r_offset += d_offset;
13055               r_type = R_PPC64_TPREL16_LO;
13056               if (toc_symndx != 0)
13057                 {
13058                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13059                   rel->r_addend = toc_addend;
13060                   /* We changed the symbol.  Start over in order to
13061                      get h, sym, sec etc. right.  */
13062                   rel--;
13063                   continue;
13064                 }
13065               else
13066                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13067             }
13068           break;
13069
13070         case R_PPC64_GOT_TLSGD16_HI:
13071         case R_PPC64_GOT_TLSGD16_HA:
13072           tls_gd = TLS_TPRELGD;
13073           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13074             goto tls_gdld_hi;
13075           break;
13076
13077         case R_PPC64_GOT_TLSLD16_HI:
13078         case R_PPC64_GOT_TLSLD16_HA:
13079           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13080             {
13081             tls_gdld_hi:
13082               if ((tls_mask & tls_gd) != 0)
13083                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13084                           + R_PPC64_GOT_TPREL16_DS);
13085               else
13086                 {
13087                   rel->r_offset -= d_offset;
13088                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13089                   r_type = R_PPC64_NONE;
13090                 }
13091               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13092             }
13093           break;
13094
13095         case R_PPC64_GOT_TLSGD16:
13096         case R_PPC64_GOT_TLSGD16_LO:
13097           tls_gd = TLS_TPRELGD;
13098           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13099             goto tls_ldgd_opt;
13100           break;
13101
13102         case R_PPC64_GOT_TLSLD16:
13103         case R_PPC64_GOT_TLSLD16_LO:
13104           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13105             {
13106               unsigned int insn1, insn2, insn3;
13107               bfd_vma offset;
13108
13109             tls_ldgd_opt:
13110               offset = (bfd_vma) -1;
13111               /* If not using the newer R_PPC64_TLSGD/LD to mark
13112                  __tls_get_addr calls, we must trust that the call
13113                  stays with its arg setup insns, ie. that the next
13114                  reloc is the __tls_get_addr call associated with
13115                  the current reloc.  Edit both insns.  */
13116               if (input_section->has_tls_get_addr_call
13117                   && rel + 1 < relend
13118                   && branch_reloc_hash_match (input_bfd, rel + 1,
13119                                               htab->tls_get_addr,
13120                                               htab->tls_get_addr_fd))
13121                 offset = rel[1].r_offset;
13122               if ((tls_mask & tls_gd) != 0)
13123                 {
13124                   /* IE */
13125                   insn1 = bfd_get_32 (output_bfd,
13126                                       contents + rel->r_offset - d_offset);
13127                   insn1 &= (1 << 26) - (1 << 2);
13128                   insn1 |= 58 << 26;    /* ld */
13129                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13130                   if (offset != (bfd_vma) -1)
13131                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13132                   if ((tls_mask & TLS_EXPLICIT) == 0)
13133                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13134                               + R_PPC64_GOT_TPREL16_DS);
13135                   else
13136                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13137                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13138                 }
13139               else
13140                 {
13141                   /* LE */
13142                   insn1 = 0x3c6d0000;   /* addis 3,13,0 */
13143                   insn2 = 0x38630000;   /* addi 3,3,0 */
13144                   if (tls_gd == 0)
13145                     {
13146                       /* Was an LD reloc.  */
13147                       if (toc_symndx)
13148                         sec = local_sections[toc_symndx];
13149                       for (r_symndx = 0;
13150                            r_symndx < symtab_hdr->sh_info;
13151                            r_symndx++)
13152                         if (local_sections[r_symndx] == sec)
13153                           break;
13154                       if (r_symndx >= symtab_hdr->sh_info)
13155                         r_symndx = STN_UNDEF;
13156                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13157                       if (r_symndx != STN_UNDEF)
13158                         rel->r_addend -= (local_syms[r_symndx].st_value
13159                                           + sec->output_offset
13160                                           + sec->output_section->vma);
13161                     }
13162                   else if (toc_symndx != 0)
13163                     {
13164                       r_symndx = toc_symndx;
13165                       rel->r_addend = toc_addend;
13166                     }
13167                   r_type = R_PPC64_TPREL16_HA;
13168                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13169                   if (offset != (bfd_vma) -1)
13170                     {
13171                       rel[1].r_info = ELF64_R_INFO (r_symndx,
13172                                                     R_PPC64_TPREL16_LO);
13173                       rel[1].r_offset = offset + d_offset;
13174                       rel[1].r_addend = rel->r_addend;
13175                     }
13176                 }
13177               bfd_put_32 (output_bfd, insn1,
13178                           contents + rel->r_offset - d_offset);
13179               if (offset != (bfd_vma) -1)
13180                 {
13181                   insn3 = bfd_get_32 (output_bfd,
13182                                       contents + offset + 4);
13183                   if (insn3 == NOP
13184                       || insn3 == CROR_151515 || insn3 == CROR_313131)
13185                     {
13186                       rel[1].r_offset += 4;
13187                       bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13188                       insn2 = NOP;
13189                     }
13190                   bfd_put_32 (output_bfd, insn2, contents + offset);
13191                 }
13192               if ((tls_mask & tls_gd) == 0
13193                   && (tls_gd == 0 || toc_symndx != 0))
13194                 {
13195                   /* We changed the symbol.  Start over in order
13196                      to get h, sym, sec etc. right.  */
13197                   rel--;
13198                   continue;
13199                 }
13200             }
13201           break;
13202
13203         case R_PPC64_TLSGD:
13204           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13205             {
13206               unsigned int insn2, insn3;
13207               bfd_vma offset = rel->r_offset;
13208
13209               if ((tls_mask & TLS_TPRELGD) != 0)
13210                 {
13211                   /* IE */
13212                   r_type = R_PPC64_NONE;
13213                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13214                 }
13215               else
13216                 {
13217                   /* LE */
13218                   if (toc_symndx != 0)
13219                     {
13220                       r_symndx = toc_symndx;
13221                       rel->r_addend = toc_addend;
13222                     }
13223                   r_type = R_PPC64_TPREL16_LO;
13224                   rel->r_offset = offset + d_offset;
13225                   insn2 = 0x38630000;   /* addi 3,3,0 */
13226                 }
13227               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13228               /* Zap the reloc on the _tls_get_addr call too.  */
13229               BFD_ASSERT (offset == rel[1].r_offset);
13230               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13231               insn3 = bfd_get_32 (output_bfd,
13232                                   contents + offset + 4);
13233               if (insn3 == NOP
13234                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13235                 {
13236                   rel->r_offset += 4;
13237                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13238                   insn2 = NOP;
13239                 }
13240               bfd_put_32 (output_bfd, insn2, contents + offset);
13241               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13242                 {
13243                   rel--;
13244                   continue;
13245                 }
13246             }
13247           break;
13248
13249         case R_PPC64_TLSLD:
13250           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13251             {
13252               unsigned int insn2, insn3;
13253               bfd_vma offset = rel->r_offset;
13254
13255               if (toc_symndx)
13256                 sec = local_sections[toc_symndx];
13257               for (r_symndx = 0;
13258                    r_symndx < symtab_hdr->sh_info;
13259                    r_symndx++)
13260                 if (local_sections[r_symndx] == sec)
13261                   break;
13262               if (r_symndx >= symtab_hdr->sh_info)
13263                 r_symndx = STN_UNDEF;
13264               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13265               if (r_symndx != STN_UNDEF)
13266                 rel->r_addend -= (local_syms[r_symndx].st_value
13267                                   + sec->output_offset
13268                                   + sec->output_section->vma);
13269
13270               r_type = R_PPC64_TPREL16_LO;
13271               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13272               rel->r_offset = offset + d_offset;
13273               /* Zap the reloc on the _tls_get_addr call too.  */
13274               BFD_ASSERT (offset == rel[1].r_offset);
13275               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13276               insn2 = 0x38630000;       /* addi 3,3,0 */
13277               insn3 = bfd_get_32 (output_bfd,
13278                                   contents + offset + 4);
13279               if (insn3 == NOP
13280                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13281                 {
13282                   rel->r_offset += 4;
13283                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13284                   insn2 = NOP;
13285                 }
13286               bfd_put_32 (output_bfd, insn2, contents + offset);
13287               rel--;
13288               continue;
13289             }
13290           break;
13291
13292         case R_PPC64_DTPMOD64:
13293           if (rel + 1 < relend
13294               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13295               && rel[1].r_offset == rel->r_offset + 8)
13296             {
13297               if ((tls_mask & TLS_GD) == 0)
13298                 {
13299                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13300                   if ((tls_mask & TLS_TPRELGD) != 0)
13301                     r_type = R_PPC64_TPREL64;
13302                   else
13303                     {
13304                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13305                       r_type = R_PPC64_NONE;
13306                     }
13307                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13308                 }
13309             }
13310           else
13311             {
13312               if ((tls_mask & TLS_LD) == 0)
13313                 {
13314                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13315                   r_type = R_PPC64_NONE;
13316                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13317                 }
13318             }
13319           break;
13320
13321         case R_PPC64_TPREL64:
13322           if ((tls_mask & TLS_TPREL) == 0)
13323             {
13324               r_type = R_PPC64_NONE;
13325               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13326             }
13327           break;
13328         }
13329
13330       /* Handle other relocations that tweak non-addend part of insn.  */
13331       insn = 0;
13332       max_br_offset = 1 << 25;
13333       addend = rel->r_addend;
13334       reloc_dest = DEST_NORMAL;
13335       switch (r_type)
13336         {
13337         default:
13338           break;
13339
13340         case R_PPC64_TOCSAVE:
13341           if (relocation + addend == (rel->r_offset
13342                                       + input_section->output_offset
13343                                       + input_section->output_section->vma)
13344               && tocsave_find (htab, NO_INSERT,
13345                                &local_syms, rel, input_bfd))
13346             {
13347               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13348               if (insn == NOP
13349                   || insn == CROR_151515 || insn == CROR_313131)
13350                 bfd_put_32 (input_bfd,
13351                             STD_R2_0R1 + STK_TOC (htab),
13352                             contents + rel->r_offset);
13353             }
13354           break;
13355
13356           /* Branch taken prediction relocations.  */
13357         case R_PPC64_ADDR14_BRTAKEN:
13358         case R_PPC64_REL14_BRTAKEN:
13359           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
13360           /* Fall thru.  */
13361
13362           /* Branch not taken prediction relocations.  */
13363         case R_PPC64_ADDR14_BRNTAKEN:
13364         case R_PPC64_REL14_BRNTAKEN:
13365           insn |= bfd_get_32 (output_bfd,
13366                               contents + rel->r_offset) & ~(0x01 << 21);
13367           /* Fall thru.  */
13368
13369         case R_PPC64_REL14:
13370           max_br_offset = 1 << 15;
13371           /* Fall thru.  */
13372
13373         case R_PPC64_REL24:
13374           /* Calls to functions with a different TOC, such as calls to
13375              shared objects, need to alter the TOC pointer.  This is
13376              done using a linkage stub.  A REL24 branching to these
13377              linkage stubs needs to be followed by a nop, as the nop
13378              will be replaced with an instruction to restore the TOC
13379              base pointer.  */
13380           fdh = h;
13381           if (h != NULL
13382               && h->oh != NULL
13383               && h->oh->is_func_descriptor)
13384             fdh = ppc_follow_link (h->oh);
13385           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13386                                            htab);
13387           if (stub_entry != NULL
13388               && (stub_entry->stub_type == ppc_stub_plt_call
13389                   || stub_entry->stub_type == ppc_stub_plt_call_r2save
13390                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13391                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
13392             {
13393               bfd_boolean can_plt_call = FALSE;
13394
13395               /* All of these stubs will modify r2, so there must be a
13396                  branch and link followed by a nop.  The nop is
13397                  replaced by an insn to restore r2.  */
13398               if (rel->r_offset + 8 <= input_section->size)
13399                 {
13400                   unsigned long br;
13401
13402                   br = bfd_get_32 (input_bfd,
13403                                    contents + rel->r_offset);
13404                   if ((br & 1) != 0)
13405                     {
13406                       unsigned long nop;
13407
13408                       nop = bfd_get_32 (input_bfd,
13409                                         contents + rel->r_offset + 4);
13410                       if (nop == NOP
13411                           || nop == CROR_151515 || nop == CROR_313131)
13412                         {
13413                           if (h != NULL
13414                               && (h == htab->tls_get_addr_fd
13415                                   || h == htab->tls_get_addr)
13416                               && !htab->no_tls_get_addr_opt)
13417                             {
13418                               /* Special stub used, leave nop alone.  */
13419                             }
13420                           else
13421                             bfd_put_32 (input_bfd,
13422                                         LD_R2_0R1 + STK_TOC (htab),
13423                                         contents + rel->r_offset + 4);
13424                           can_plt_call = TRUE;
13425                         }
13426                     }
13427                 }
13428
13429               if (!can_plt_call && h != NULL)
13430                 {
13431                   const char *name = h->elf.root.root.string;
13432
13433                   if (*name == '.')
13434                     ++name;
13435
13436                   if (strncmp (name, "__libc_start_main", 17) == 0
13437                       && (name[17] == 0 || name[17] == '@'))
13438                     {
13439                       /* Allow crt1 branch to go via a toc adjusting
13440                          stub.  Other calls that never return could do
13441                          the same, if we could detect such.  */
13442                       can_plt_call = TRUE;
13443                     }
13444                 }
13445
13446               if (!can_plt_call)
13447                 {
13448                   /* g++ as of 20130507 emits self-calls without a
13449                      following nop.  This is arguably wrong since we
13450                      have conflicting information.  On the one hand a
13451                      global symbol and on the other a local call
13452                      sequence, but don't error for this special case.
13453                      It isn't possible to cheaply verify we have
13454                      exactly such a call.  Allow all calls to the same
13455                      section.  */
13456                   asection *code_sec = sec;
13457
13458                   if (get_opd_info (sec) != NULL)
13459                     {
13460                       bfd_vma off = (relocation + addend
13461                                      - sec->output_section->vma
13462                                      - sec->output_offset);
13463
13464                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
13465                     }
13466                   if (code_sec == input_section)
13467                     can_plt_call = TRUE;
13468                 }
13469
13470               if (!can_plt_call)
13471                 {
13472                   info->callbacks->einfo
13473                     (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13474                        "recompile with -fPIC"),
13475                      input_bfd, input_section, rel->r_offset, sym_name);
13476
13477                   bfd_set_error (bfd_error_bad_value);
13478                   ret = FALSE;
13479                 }
13480
13481               if (can_plt_call
13482                   && (stub_entry->stub_type == ppc_stub_plt_call
13483                       || stub_entry->stub_type == ppc_stub_plt_call_r2save))
13484                 unresolved_reloc = FALSE;
13485             }
13486
13487           if ((stub_entry == NULL
13488                || stub_entry->stub_type == ppc_stub_long_branch
13489                || stub_entry->stub_type == ppc_stub_plt_branch)
13490               && get_opd_info (sec) != NULL)
13491             {
13492               /* The branch destination is the value of the opd entry. */
13493               bfd_vma off = (relocation + addend
13494                              - sec->output_section->vma
13495                              - sec->output_offset);
13496               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
13497               if (dest != (bfd_vma) -1)
13498                 {
13499                   relocation = dest;
13500                   addend = 0;
13501                   reloc_dest = DEST_OPD;
13502                 }
13503             }
13504
13505           /* If the branch is out of reach we ought to have a long
13506              branch stub.  */
13507           from = (rel->r_offset
13508                   + input_section->output_offset
13509                   + input_section->output_section->vma);
13510
13511           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
13512                                                   ? fdh->elf.other
13513                                                   : sym->st_other);
13514
13515           if (stub_entry != NULL
13516               && (stub_entry->stub_type == ppc_stub_long_branch
13517                   || stub_entry->stub_type == ppc_stub_plt_branch)
13518               && (r_type == R_PPC64_ADDR14_BRTAKEN
13519                   || r_type == R_PPC64_ADDR14_BRNTAKEN
13520                   || (relocation + addend - from + max_br_offset
13521                       < 2 * max_br_offset)))
13522             /* Don't use the stub if this branch is in range.  */
13523             stub_entry = NULL;
13524
13525           if (stub_entry != NULL)
13526             {
13527               /* Munge up the value and addend so that we call the stub
13528                  rather than the procedure directly.  */
13529               relocation = (stub_entry->stub_offset
13530                             + stub_entry->stub_sec->output_offset
13531                             + stub_entry->stub_sec->output_section->vma);
13532               addend = 0;
13533               reloc_dest = DEST_STUB;
13534
13535               if ((stub_entry->stub_type == ppc_stub_plt_call
13536                    || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13537                   && (ALWAYS_EMIT_R2SAVE
13538                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13539                   && rel + 1 < relend
13540                   && rel[1].r_offset == rel->r_offset + 4
13541                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
13542                 relocation += 4;
13543             }
13544
13545           if (insn != 0)
13546             {
13547               if (is_isa_v2)
13548                 {
13549                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
13550                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
13551                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
13552                   if ((insn & (0x14 << 21)) == (0x04 << 21))
13553                     insn |= 0x02 << 21;
13554                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
13555                     insn |= 0x08 << 21;
13556                   else
13557                     break;
13558                 }
13559               else
13560                 {
13561                   /* Invert 'y' bit if not the default.  */
13562                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
13563                     insn ^= 0x01 << 21;
13564                 }
13565
13566               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
13567             }
13568
13569           /* NOP out calls to undefined weak functions.
13570              We can thus call a weak function without first
13571              checking whether the function is defined.  */
13572           else if (h != NULL
13573                    && h->elf.root.type == bfd_link_hash_undefweak
13574                    && h->elf.dynindx == -1
13575                    && r_type == R_PPC64_REL24
13576                    && relocation == 0
13577                    && addend == 0)
13578             {
13579               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13580               continue;
13581             }
13582           break;
13583         }
13584
13585       /* Set `addend'.  */
13586       tls_type = 0;
13587       switch (r_type)
13588         {
13589         default:
13590           info->callbacks->einfo
13591             (_("%P: %B: unknown relocation type %d for `%T'\n"),
13592              input_bfd, (int) r_type, sym_name);
13593
13594           bfd_set_error (bfd_error_bad_value);
13595           ret = FALSE;
13596           continue;
13597
13598         case R_PPC64_NONE:
13599         case R_PPC64_TLS:
13600         case R_PPC64_TLSGD:
13601         case R_PPC64_TLSLD:
13602         case R_PPC64_TOCSAVE:
13603         case R_PPC64_GNU_VTINHERIT:
13604         case R_PPC64_GNU_VTENTRY:
13605           continue;
13606
13607           /* GOT16 relocations.  Like an ADDR16 using the symbol's
13608              address in the GOT as relocation value instead of the
13609              symbol's value itself.  Also, create a GOT entry for the
13610              symbol and put the symbol value there.  */
13611         case R_PPC64_GOT_TLSGD16:
13612         case R_PPC64_GOT_TLSGD16_LO:
13613         case R_PPC64_GOT_TLSGD16_HI:
13614         case R_PPC64_GOT_TLSGD16_HA:
13615           tls_type = TLS_TLS | TLS_GD;
13616           goto dogot;
13617
13618         case R_PPC64_GOT_TLSLD16:
13619         case R_PPC64_GOT_TLSLD16_LO:
13620         case R_PPC64_GOT_TLSLD16_HI:
13621         case R_PPC64_GOT_TLSLD16_HA:
13622           tls_type = TLS_TLS | TLS_LD;
13623           goto dogot;
13624
13625         case R_PPC64_GOT_TPREL16_DS:
13626         case R_PPC64_GOT_TPREL16_LO_DS:
13627         case R_PPC64_GOT_TPREL16_HI:
13628         case R_PPC64_GOT_TPREL16_HA:
13629           tls_type = TLS_TLS | TLS_TPREL;
13630           goto dogot;
13631
13632         case R_PPC64_GOT_DTPREL16_DS:
13633         case R_PPC64_GOT_DTPREL16_LO_DS:
13634         case R_PPC64_GOT_DTPREL16_HI:
13635         case R_PPC64_GOT_DTPREL16_HA:
13636           tls_type = TLS_TLS | TLS_DTPREL;
13637           goto dogot;
13638
13639         case R_PPC64_GOT16:
13640         case R_PPC64_GOT16_LO:
13641         case R_PPC64_GOT16_HI:
13642         case R_PPC64_GOT16_HA:
13643         case R_PPC64_GOT16_DS:
13644         case R_PPC64_GOT16_LO_DS:
13645         dogot:
13646           {
13647             /* Relocation is to the entry for this symbol in the global
13648                offset table.  */
13649             asection *got;
13650             bfd_vma *offp;
13651             bfd_vma off;
13652             unsigned long indx = 0;
13653             struct got_entry *ent;
13654
13655             if (tls_type == (TLS_TLS | TLS_LD)
13656                 && (h == NULL
13657                     || !h->elf.def_dynamic))
13658               ent = ppc64_tlsld_got (input_bfd);
13659             else
13660               {
13661
13662                 if (h != NULL)
13663                   {
13664                     bfd_boolean dyn = htab->elf.dynamic_sections_created;
13665                     if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
13666                                                           &h->elf)
13667                         || (info->shared
13668                             && SYMBOL_CALLS_LOCAL (info, &h->elf)))
13669                       /* This is actually a static link, or it is a
13670                          -Bsymbolic link and the symbol is defined
13671                          locally, or the symbol was forced to be local
13672                          because of a version file.  */
13673                       ;
13674                     else
13675                       {
13676                         BFD_ASSERT (h->elf.dynindx != -1);
13677                         indx = h->elf.dynindx;
13678                         unresolved_reloc = FALSE;
13679                       }
13680                     ent = h->elf.got.glist;
13681                   }
13682                 else
13683                   {
13684                     if (local_got_ents == NULL)
13685                       abort ();
13686                     ent = local_got_ents[r_symndx];
13687                   }
13688
13689                 for (; ent != NULL; ent = ent->next)
13690                   if (ent->addend == orig_rel.r_addend
13691                       && ent->owner == input_bfd
13692                       && ent->tls_type == tls_type)
13693                     break;
13694               }
13695
13696             if (ent == NULL)
13697               abort ();
13698             if (ent->is_indirect)
13699               ent = ent->got.ent;
13700             offp = &ent->got.offset;
13701             got = ppc64_elf_tdata (ent->owner)->got;
13702             if (got == NULL)
13703               abort ();
13704
13705             /* The offset must always be a multiple of 8.  We use the
13706                least significant bit to record whether we have already
13707                processed this entry.  */
13708             off = *offp;
13709             if ((off & 1) != 0)
13710               off &= ~1;
13711             else
13712               {
13713                 /* Generate relocs for the dynamic linker, except in
13714                    the case of TLSLD where we'll use one entry per
13715                    module.  */
13716                 asection *relgot;
13717                 bfd_boolean ifunc;
13718
13719                 *offp = off | 1;
13720                 relgot = NULL;
13721                 ifunc = (h != NULL
13722                          ? h->elf.type == STT_GNU_IFUNC
13723                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
13724                 if (ifunc)
13725                   relgot = htab->reliplt;
13726                 else if ((info->shared || indx != 0)
13727                          && (h == NULL
13728                              || (tls_type == (TLS_TLS | TLS_LD)
13729                                  && !h->elf.def_dynamic)
13730                              || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13731                              || h->elf.root.type != bfd_link_hash_undefweak))
13732                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
13733                 if (relgot != NULL)
13734                   {
13735                     outrel.r_offset = (got->output_section->vma
13736                                        + got->output_offset
13737                                        + off);
13738                     outrel.r_addend = addend;
13739                     if (tls_type & (TLS_LD | TLS_GD))
13740                       {
13741                         outrel.r_addend = 0;
13742                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
13743                         if (tls_type == (TLS_TLS | TLS_GD))
13744                           {
13745                             loc = relgot->contents;
13746                             loc += (relgot->reloc_count++
13747                                     * sizeof (Elf64_External_Rela));
13748                             bfd_elf64_swap_reloca_out (output_bfd,
13749                                                        &outrel, loc);
13750                             outrel.r_offset += 8;
13751                             outrel.r_addend = addend;
13752                             outrel.r_info
13753                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13754                           }
13755                       }
13756                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
13757                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13758                     else if (tls_type == (TLS_TLS | TLS_TPREL))
13759                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
13760                     else if (indx != 0)
13761                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
13762                     else
13763                       {
13764                         if (ifunc)
13765                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13766                         else
13767                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13768
13769                         /* Write the .got section contents for the sake
13770                            of prelink.  */
13771                         loc = got->contents + off;
13772                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
13773                                     loc);
13774                       }
13775
13776                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
13777                       {
13778                         outrel.r_addend += relocation;
13779                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
13780                           outrel.r_addend -= htab->elf.tls_sec->vma;
13781                       }
13782                     loc = relgot->contents;
13783                     loc += (relgot->reloc_count++
13784                             * sizeof (Elf64_External_Rela));
13785                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13786                   }
13787
13788                 /* Init the .got section contents here if we're not
13789                    emitting a reloc.  */
13790                 else
13791                   {
13792                     relocation += addend;
13793                     if (tls_type == (TLS_TLS | TLS_LD))
13794                       relocation = 1;
13795                     else if (tls_type != 0)
13796                       {
13797                         relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
13798                         if (tls_type == (TLS_TLS | TLS_TPREL))
13799                           relocation += DTP_OFFSET - TP_OFFSET;
13800
13801                         if (tls_type == (TLS_TLS | TLS_GD))
13802                           {
13803                             bfd_put_64 (output_bfd, relocation,
13804                                         got->contents + off + 8);
13805                             relocation = 1;
13806                           }
13807                       }
13808
13809                     bfd_put_64 (output_bfd, relocation,
13810                                 got->contents + off);
13811                   }
13812               }
13813
13814             if (off >= (bfd_vma) -2)
13815               abort ();
13816
13817             relocation = got->output_section->vma + got->output_offset + off;
13818             addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
13819           }
13820           break;
13821
13822         case R_PPC64_PLT16_HA:
13823         case R_PPC64_PLT16_HI:
13824         case R_PPC64_PLT16_LO:
13825         case R_PPC64_PLT32:
13826         case R_PPC64_PLT64:
13827           /* Relocation is to the entry for this symbol in the
13828              procedure linkage table.  */
13829
13830           /* Resolve a PLT reloc against a local symbol directly,
13831              without using the procedure linkage table.  */
13832           if (h == NULL)
13833             break;
13834
13835           /* It's possible that we didn't make a PLT entry for this
13836              symbol.  This happens when statically linking PIC code,
13837              or when using -Bsymbolic.  Go find a match if there is a
13838              PLT entry.  */
13839           if (htab->plt != NULL)
13840             {
13841               struct plt_entry *ent;
13842               for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
13843                 if (ent->addend == orig_rel.r_addend
13844                     && ent->plt.offset != (bfd_vma) -1)
13845                   {
13846                     relocation = (htab->plt->output_section->vma
13847                                   + htab->plt->output_offset
13848                                   + ent->plt.offset);
13849                     unresolved_reloc = FALSE;
13850                   }
13851             }
13852           break;
13853
13854         case R_PPC64_TOC:
13855           /* Relocation value is TOC base.  */
13856           relocation = TOCstart;
13857           if (r_symndx == STN_UNDEF)
13858             relocation += htab->stub_group[input_section->id].toc_off;
13859           else if (unresolved_reloc)
13860             ;
13861           else if (sec != NULL && sec->id <= htab->top_id)
13862             relocation += htab->stub_group[sec->id].toc_off;
13863           else
13864             unresolved_reloc = TRUE;
13865           goto dodyn;
13866
13867           /* TOC16 relocs.  We want the offset relative to the TOC base,
13868              which is the address of the start of the TOC plus 0x8000.
13869              The TOC consists of sections .got, .toc, .tocbss, and .plt,
13870              in this order.  */
13871         case R_PPC64_TOC16:
13872         case R_PPC64_TOC16_LO:
13873         case R_PPC64_TOC16_HI:
13874         case R_PPC64_TOC16_DS:
13875         case R_PPC64_TOC16_LO_DS:
13876         case R_PPC64_TOC16_HA:
13877           addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
13878           break;
13879
13880           /* Relocate against the beginning of the section.  */
13881         case R_PPC64_SECTOFF:
13882         case R_PPC64_SECTOFF_LO:
13883         case R_PPC64_SECTOFF_HI:
13884         case R_PPC64_SECTOFF_DS:
13885         case R_PPC64_SECTOFF_LO_DS:
13886         case R_PPC64_SECTOFF_HA:
13887           if (sec != NULL)
13888             addend -= sec->output_section->vma;
13889           break;
13890
13891         case R_PPC64_REL16:
13892         case R_PPC64_REL16_LO:
13893         case R_PPC64_REL16_HI:
13894         case R_PPC64_REL16_HA:
13895           break;
13896
13897         case R_PPC64_REL14:
13898         case R_PPC64_REL14_BRNTAKEN:
13899         case R_PPC64_REL14_BRTAKEN:
13900         case R_PPC64_REL24:
13901           break;
13902
13903         case R_PPC64_TPREL16:
13904         case R_PPC64_TPREL16_LO:
13905         case R_PPC64_TPREL16_HI:
13906         case R_PPC64_TPREL16_HA:
13907         case R_PPC64_TPREL16_DS:
13908         case R_PPC64_TPREL16_LO_DS:
13909         case R_PPC64_TPREL16_HIGH:
13910         case R_PPC64_TPREL16_HIGHA:
13911         case R_PPC64_TPREL16_HIGHER:
13912         case R_PPC64_TPREL16_HIGHERA:
13913         case R_PPC64_TPREL16_HIGHEST:
13914         case R_PPC64_TPREL16_HIGHESTA:
13915           if (h != NULL
13916               && h->elf.root.type == bfd_link_hash_undefweak
13917               && h->elf.dynindx == -1)
13918             {
13919               /* Make this relocation against an undefined weak symbol
13920                  resolve to zero.  This is really just a tweak, since
13921                  code using weak externs ought to check that they are
13922                  defined before using them.  */
13923               bfd_byte *p = contents + rel->r_offset - d_offset;
13924
13925               insn = bfd_get_32 (output_bfd, p);
13926               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
13927               if (insn != 0)
13928                 bfd_put_32 (output_bfd, insn, p);
13929               break;
13930             }
13931           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13932           if (info->shared)
13933             /* The TPREL16 relocs shouldn't really be used in shared
13934                libs as they will result in DT_TEXTREL being set, but
13935                support them anyway.  */
13936             goto dodyn;
13937           break;
13938
13939         case R_PPC64_DTPREL16:
13940         case R_PPC64_DTPREL16_LO:
13941         case R_PPC64_DTPREL16_HI:
13942         case R_PPC64_DTPREL16_HA:
13943         case R_PPC64_DTPREL16_DS:
13944         case R_PPC64_DTPREL16_LO_DS:
13945         case R_PPC64_DTPREL16_HIGH:
13946         case R_PPC64_DTPREL16_HIGHA:
13947         case R_PPC64_DTPREL16_HIGHER:
13948         case R_PPC64_DTPREL16_HIGHERA:
13949         case R_PPC64_DTPREL16_HIGHEST:
13950         case R_PPC64_DTPREL16_HIGHESTA:
13951           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13952           break;
13953
13954         case R_PPC64_DTPMOD64:
13955           relocation = 1;
13956           addend = 0;
13957           goto dodyn;
13958
13959         case R_PPC64_TPREL64:
13960           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13961           goto dodyn;
13962
13963         case R_PPC64_DTPREL64:
13964           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13965           /* Fall thru */
13966
13967           /* Relocations that may need to be propagated if this is a
13968              dynamic object.  */
13969         case R_PPC64_REL30:
13970         case R_PPC64_REL32:
13971         case R_PPC64_REL64:
13972         case R_PPC64_ADDR14:
13973         case R_PPC64_ADDR14_BRNTAKEN:
13974         case R_PPC64_ADDR14_BRTAKEN:
13975         case R_PPC64_ADDR16:
13976         case R_PPC64_ADDR16_DS:
13977         case R_PPC64_ADDR16_HA:
13978         case R_PPC64_ADDR16_HI:
13979         case R_PPC64_ADDR16_HIGH:
13980         case R_PPC64_ADDR16_HIGHA:
13981         case R_PPC64_ADDR16_HIGHER:
13982         case R_PPC64_ADDR16_HIGHERA:
13983         case R_PPC64_ADDR16_HIGHEST:
13984         case R_PPC64_ADDR16_HIGHESTA:
13985         case R_PPC64_ADDR16_LO:
13986         case R_PPC64_ADDR16_LO_DS:
13987         case R_PPC64_ADDR24:
13988         case R_PPC64_ADDR32:
13989         case R_PPC64_ADDR64:
13990         case R_PPC64_UADDR16:
13991         case R_PPC64_UADDR32:
13992         case R_PPC64_UADDR64:
13993         dodyn:
13994           if ((input_section->flags & SEC_ALLOC) == 0)
13995             break;
13996
13997           if (NO_OPD_RELOCS && is_opd)
13998             break;
13999
14000           if ((info->shared
14001                && (h == NULL
14002                    || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14003                    || h->elf.root.type != bfd_link_hash_undefweak)
14004                && (must_be_dyn_reloc (info, r_type)
14005                    || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
14006               || (ELIMINATE_COPY_RELOCS
14007                   && !info->shared
14008                   && h != NULL
14009                   && h->elf.dynindx != -1
14010                   && !h->elf.non_got_ref
14011                   && !h->elf.def_regular)
14012               || (!info->shared
14013                   && (h != NULL
14014                       ? h->elf.type == STT_GNU_IFUNC
14015                       : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
14016             {
14017               bfd_boolean skip, relocate;
14018               asection *sreloc;
14019               bfd_vma out_off;
14020
14021               /* When generating a dynamic object, these relocations
14022                  are copied into the output file to be resolved at run
14023                  time.  */
14024
14025               skip = FALSE;
14026               relocate = FALSE;
14027
14028               out_off = _bfd_elf_section_offset (output_bfd, info,
14029                                                  input_section, rel->r_offset);
14030               if (out_off == (bfd_vma) -1)
14031                 skip = TRUE;
14032               else if (out_off == (bfd_vma) -2)
14033                 skip = TRUE, relocate = TRUE;
14034               out_off += (input_section->output_section->vma
14035                           + input_section->output_offset);
14036               outrel.r_offset = out_off;
14037               outrel.r_addend = rel->r_addend;
14038
14039               /* Optimize unaligned reloc use.  */
14040               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14041                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14042                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14043               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14044                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14045                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14046               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14047                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14048                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14049
14050               if (skip)
14051                 memset (&outrel, 0, sizeof outrel);
14052               else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
14053                        && !is_opd
14054                        && r_type != R_PPC64_TOC)
14055                 {
14056                   BFD_ASSERT (h->elf.dynindx != -1);
14057                   outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14058                 }
14059               else
14060                 {
14061                   /* This symbol is local, or marked to become local,
14062                      or this is an opd section reloc which must point
14063                      at a local function.  */
14064                   outrel.r_addend += relocation;
14065                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14066                     {
14067                       if (is_opd && h != NULL)
14068                         {
14069                           /* Lie about opd entries.  This case occurs
14070                              when building shared libraries and we
14071                              reference a function in another shared
14072                              lib.  The same thing happens for a weak
14073                              definition in an application that's
14074                              overridden by a strong definition in a
14075                              shared lib.  (I believe this is a generic
14076                              bug in binutils handling of weak syms.)
14077                              In these cases we won't use the opd
14078                              entry in this lib.  */
14079                           unresolved_reloc = FALSE;
14080                         }
14081                       if (!is_opd
14082                           && r_type == R_PPC64_ADDR64
14083                           && (h != NULL
14084                               ? h->elf.type == STT_GNU_IFUNC
14085                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14086                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14087                       else
14088                         {
14089                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14090
14091                           /* We need to relocate .opd contents for ld.so.
14092                              Prelink also wants simple and consistent rules
14093                              for relocs.  This make all RELATIVE relocs have
14094                              *r_offset equal to r_addend.  */
14095                           relocate = TRUE;
14096                         }
14097                     }
14098                   else
14099                     {
14100                       long indx = 0;
14101
14102                       if (h != NULL
14103                           ? h->elf.type == STT_GNU_IFUNC
14104                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14105                         {
14106                           info->callbacks->einfo
14107                             (_("%P: %H: %s for indirect "
14108                                "function `%T' unsupported\n"),
14109                              input_bfd, input_section, rel->r_offset,
14110                              ppc64_elf_howto_table[r_type]->name,
14111                              sym_name);
14112                           ret = FALSE;
14113                         }
14114                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14115                         ;
14116                       else if (sec == NULL || sec->owner == NULL)
14117                         {
14118                           bfd_set_error (bfd_error_bad_value);
14119                           return FALSE;
14120                         }
14121                       else
14122                         {
14123                           asection *osec;
14124
14125                           osec = sec->output_section;
14126                           indx = elf_section_data (osec)->dynindx;
14127
14128                           if (indx == 0)
14129                             {
14130                               if ((osec->flags & SEC_READONLY) == 0
14131                                   && htab->elf.data_index_section != NULL)
14132                                 osec = htab->elf.data_index_section;
14133                               else
14134                                 osec = htab->elf.text_index_section;
14135                               indx = elf_section_data (osec)->dynindx;
14136                             }
14137                           BFD_ASSERT (indx != 0);
14138
14139                           /* We are turning this relocation into one
14140                              against a section symbol, so subtract out
14141                              the output section's address but not the
14142                              offset of the input section in the output
14143                              section.  */
14144                           outrel.r_addend -= osec->vma;
14145                         }
14146
14147                       outrel.r_info = ELF64_R_INFO (indx, r_type);
14148                     }
14149                 }
14150
14151               sreloc = elf_section_data (input_section)->sreloc;
14152               if (h != NULL
14153                   ? h->elf.type == STT_GNU_IFUNC
14154                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14155                 sreloc = htab->reliplt;
14156               if (sreloc == NULL)
14157                 abort ();
14158
14159               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14160                   >= sreloc->size)
14161                 abort ();
14162               loc = sreloc->contents;
14163               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14164               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14165
14166               /* If this reloc is against an external symbol, it will
14167                  be computed at runtime, so there's no need to do
14168                  anything now.  However, for the sake of prelink ensure
14169                  that the section contents are a known value.  */
14170               if (! relocate)
14171                 {
14172                   unresolved_reloc = FALSE;
14173                   /* The value chosen here is quite arbitrary as ld.so
14174                      ignores section contents except for the special
14175                      case of .opd where the contents might be accessed
14176                      before relocation.  Choose zero, as that won't
14177                      cause reloc overflow.  */
14178                   relocation = 0;
14179                   addend = 0;
14180                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14181                      to improve backward compatibility with older
14182                      versions of ld.  */
14183                   if (r_type == R_PPC64_ADDR64)
14184                     addend = outrel.r_addend;
14185                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
14186                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
14187                     addend = (input_section->output_section->vma
14188                               + input_section->output_offset
14189                               + rel->r_offset);
14190                 }
14191             }
14192           break;
14193
14194         case R_PPC64_COPY:
14195         case R_PPC64_GLOB_DAT:
14196         case R_PPC64_JMP_SLOT:
14197         case R_PPC64_JMP_IREL:
14198         case R_PPC64_RELATIVE:
14199           /* We shouldn't ever see these dynamic relocs in relocatable
14200              files.  */
14201           /* Fall through.  */
14202
14203         case R_PPC64_PLTGOT16:
14204         case R_PPC64_PLTGOT16_DS:
14205         case R_PPC64_PLTGOT16_HA:
14206         case R_PPC64_PLTGOT16_HI:
14207         case R_PPC64_PLTGOT16_LO:
14208         case R_PPC64_PLTGOT16_LO_DS:
14209         case R_PPC64_PLTREL32:
14210         case R_PPC64_PLTREL64:
14211           /* These ones haven't been implemented yet.  */
14212
14213           info->callbacks->einfo
14214             (_("%P: %B: %s is not supported for `%T'\n"),
14215              input_bfd,
14216              ppc64_elf_howto_table[r_type]->name, sym_name);
14217
14218           bfd_set_error (bfd_error_invalid_operation);
14219           ret = FALSE;
14220           continue;
14221         }
14222
14223       /* Multi-instruction sequences that access the TOC can be
14224          optimized, eg. addis ra,r2,0; addi rb,ra,x;
14225          to             nop;           addi rb,r2,x;  */
14226       switch (r_type)
14227         {
14228         default:
14229           break;
14230
14231         case R_PPC64_GOT_TLSLD16_HI:
14232         case R_PPC64_GOT_TLSGD16_HI:
14233         case R_PPC64_GOT_TPREL16_HI:
14234         case R_PPC64_GOT_DTPREL16_HI:
14235         case R_PPC64_GOT16_HI:
14236         case R_PPC64_TOC16_HI:
14237           /* These relocs would only be useful if building up an
14238              offset to later add to r2, perhaps in an indexed
14239              addressing mode instruction.  Don't try to optimize.
14240              Unfortunately, the possibility of someone building up an
14241              offset like this or even with the HA relocs, means that
14242              we need to check the high insn when optimizing the low
14243              insn.  */
14244           break;
14245
14246         case R_PPC64_GOT_TLSLD16_HA:
14247         case R_PPC64_GOT_TLSGD16_HA:
14248         case R_PPC64_GOT_TPREL16_HA:
14249         case R_PPC64_GOT_DTPREL16_HA:
14250         case R_PPC64_GOT16_HA:
14251         case R_PPC64_TOC16_HA:
14252           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14253               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14254             {
14255               bfd_byte *p = contents + (rel->r_offset & ~3);
14256               bfd_put_32 (input_bfd, NOP, p);
14257             }
14258           break;
14259
14260         case R_PPC64_GOT_TLSLD16_LO:
14261         case R_PPC64_GOT_TLSGD16_LO:
14262         case R_PPC64_GOT_TPREL16_LO_DS:
14263         case R_PPC64_GOT_DTPREL16_LO_DS:
14264         case R_PPC64_GOT16_LO:
14265         case R_PPC64_GOT16_LO_DS:
14266         case R_PPC64_TOC16_LO:
14267         case R_PPC64_TOC16_LO_DS:
14268           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14269               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14270             {
14271               bfd_byte *p = contents + (rel->r_offset & ~3);
14272               insn = bfd_get_32 (input_bfd, p);
14273               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14274                 {
14275                   /* Transform addic to addi when we change reg.  */
14276                   insn &= ~((0x3f << 26) | (0x1f << 16));
14277                   insn |= (14u << 26) | (2 << 16);
14278                 }
14279               else
14280                 {
14281                   insn &= ~(0x1f << 16);
14282                   insn |= 2 << 16;
14283                 }
14284               bfd_put_32 (input_bfd, insn, p);
14285             }
14286           break;
14287         }
14288
14289       /* Do any further special processing.  */
14290       switch (r_type)
14291         {
14292         default:
14293           break;
14294
14295         case R_PPC64_REL16_HA:
14296         case R_PPC64_ADDR16_HA:
14297         case R_PPC64_ADDR16_HIGHA:
14298         case R_PPC64_ADDR16_HIGHERA:
14299         case R_PPC64_ADDR16_HIGHESTA:
14300         case R_PPC64_TOC16_HA:
14301         case R_PPC64_SECTOFF_HA:
14302         case R_PPC64_TPREL16_HA:
14303         case R_PPC64_TPREL16_HIGHA:
14304         case R_PPC64_TPREL16_HIGHERA:
14305         case R_PPC64_TPREL16_HIGHESTA:
14306         case R_PPC64_DTPREL16_HA:
14307         case R_PPC64_DTPREL16_HIGHA:
14308         case R_PPC64_DTPREL16_HIGHERA:
14309         case R_PPC64_DTPREL16_HIGHESTA:
14310           /* It's just possible that this symbol is a weak symbol
14311              that's not actually defined anywhere. In that case,
14312              'sec' would be NULL, and we should leave the symbol
14313              alone (it will be set to zero elsewhere in the link).  */
14314           if (sec == NULL)
14315             break;
14316           /* Fall thru */
14317
14318         case R_PPC64_GOT16_HA:
14319         case R_PPC64_PLTGOT16_HA:
14320         case R_PPC64_PLT16_HA:
14321         case R_PPC64_GOT_TLSGD16_HA:
14322         case R_PPC64_GOT_TLSLD16_HA:
14323         case R_PPC64_GOT_TPREL16_HA:
14324         case R_PPC64_GOT_DTPREL16_HA:
14325           /* Add 0x10000 if sign bit in 0:15 is set.
14326              Bits 0:15 are not used.  */
14327           addend += 0x8000;
14328           break;
14329
14330         case R_PPC64_ADDR16_DS:
14331         case R_PPC64_ADDR16_LO_DS:
14332         case R_PPC64_GOT16_DS:
14333         case R_PPC64_GOT16_LO_DS:
14334         case R_PPC64_PLT16_LO_DS:
14335         case R_PPC64_SECTOFF_DS:
14336         case R_PPC64_SECTOFF_LO_DS:
14337         case R_PPC64_TOC16_DS:
14338         case R_PPC64_TOC16_LO_DS:
14339         case R_PPC64_PLTGOT16_DS:
14340         case R_PPC64_PLTGOT16_LO_DS:
14341         case R_PPC64_GOT_TPREL16_DS:
14342         case R_PPC64_GOT_TPREL16_LO_DS:
14343         case R_PPC64_GOT_DTPREL16_DS:
14344         case R_PPC64_GOT_DTPREL16_LO_DS:
14345         case R_PPC64_TPREL16_DS:
14346         case R_PPC64_TPREL16_LO_DS:
14347         case R_PPC64_DTPREL16_DS:
14348         case R_PPC64_DTPREL16_LO_DS:
14349           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14350           mask = 3;
14351           /* If this reloc is against an lq insn, then the value must be
14352              a multiple of 16.  This is somewhat of a hack, but the
14353              "correct" way to do this by defining _DQ forms of all the
14354              _DS relocs bloats all reloc switches in this file.  It
14355              doesn't seem to make much sense to use any of these relocs
14356              in data, so testing the insn should be safe.  */
14357           if ((insn & (0x3f << 26)) == (56u << 26))
14358             mask = 15;
14359           if (((relocation + addend) & mask) != 0)
14360             {
14361               info->callbacks->einfo
14362                 (_("%P: %H: error: %s not a multiple of %u\n"),
14363                  input_bfd, input_section, rel->r_offset,
14364                  ppc64_elf_howto_table[r_type]->name,
14365                  mask + 1);
14366               bfd_set_error (bfd_error_bad_value);
14367               ret = FALSE;
14368               continue;
14369             }
14370           break;
14371         }
14372
14373       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
14374          because such sections are not SEC_ALLOC and thus ld.so will
14375          not process them.  */
14376       if (unresolved_reloc
14377           && !((input_section->flags & SEC_DEBUGGING) != 0
14378                && h->elf.def_dynamic)
14379           && _bfd_elf_section_offset (output_bfd, info, input_section,
14380                                       rel->r_offset) != (bfd_vma) -1)
14381         {
14382           info->callbacks->einfo
14383             (_("%P: %H: unresolvable %s against `%T'\n"),
14384              input_bfd, input_section, rel->r_offset,
14385              ppc64_elf_howto_table[(int) r_type]->name,
14386              h->elf.root.root.string);
14387           ret = FALSE;
14388         }
14389
14390       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
14391                                     input_bfd,
14392                                     input_section,
14393                                     contents,
14394                                     rel->r_offset,
14395                                     relocation,
14396                                     addend);
14397
14398       if (r != bfd_reloc_ok)
14399         {
14400           char *more_info = NULL;
14401           const char *reloc_name = ppc64_elf_howto_table[r_type]->name;
14402
14403           if (reloc_dest != DEST_NORMAL)
14404             {
14405               more_info = bfd_malloc (strlen (reloc_name) + 8);
14406               if (more_info != NULL)
14407                 {
14408                   strcpy (more_info, reloc_name);
14409                   strcat (more_info, (reloc_dest == DEST_OPD
14410                                       ? " (OPD)" : " (stub)"));
14411                   reloc_name = more_info;
14412                 }
14413             }
14414
14415           if (r == bfd_reloc_overflow)
14416             {
14417               if (warned)
14418                 continue;
14419               if (h != NULL
14420                   && h->elf.root.type == bfd_link_hash_undefweak
14421                   && ppc64_elf_howto_table[r_type]->pc_relative)
14422                 {
14423                   /* Assume this is a call protected by other code that
14424                      detects the symbol is undefined.  If this is the case,
14425                      we can safely ignore the overflow.  If not, the
14426                      program is hosed anyway, and a little warning isn't
14427                      going to help.  */
14428
14429                   continue;
14430                 }
14431
14432               if (!((*info->callbacks->reloc_overflow)
14433                     (info, &h->elf.root, sym_name,
14434                      reloc_name, orig_rel.r_addend,
14435                      input_bfd, input_section, rel->r_offset)))
14436                 return FALSE;
14437             }
14438           else
14439             {
14440               info->callbacks->einfo
14441                 (_("%P: %H: %s against `%T': error %d\n"),
14442                  input_bfd, input_section, rel->r_offset,
14443                  reloc_name, sym_name, (int) r);
14444               ret = FALSE;
14445             }
14446           if (more_info != NULL)
14447             free (more_info);
14448         }
14449     }
14450
14451   /* If we're emitting relocations, then shortly after this function
14452      returns, reloc offsets and addends for this section will be
14453      adjusted.  Worse, reloc symbol indices will be for the output
14454      file rather than the input.  Save a copy of the relocs for
14455      opd_entry_value.  */
14456   if (is_opd && (info->emitrelocations || info->relocatable))
14457     {
14458       bfd_size_type amt;
14459       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
14460       rel = bfd_alloc (input_bfd, amt);
14461       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
14462       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
14463       if (rel == NULL)
14464         return FALSE;
14465       memcpy (rel, relocs, amt);
14466     }
14467   return ret;
14468 }
14469
14470 /* Adjust the value of any local symbols in opd sections.  */
14471
14472 static int
14473 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
14474                               const char *name ATTRIBUTE_UNUSED,
14475                               Elf_Internal_Sym *elfsym,
14476                               asection *input_sec,
14477                               struct elf_link_hash_entry *h)
14478 {
14479   struct _opd_sec_data *opd;
14480   long adjust;
14481   bfd_vma value;
14482
14483   if (h != NULL)
14484     return 1;
14485
14486   opd = get_opd_info (input_sec);
14487   if (opd == NULL || opd->adjust == NULL)
14488     return 1;
14489
14490   value = elfsym->st_value - input_sec->output_offset;
14491   if (!info->relocatable)
14492     value -= input_sec->output_section->vma;
14493
14494   adjust = opd->adjust[value / 8];
14495   if (adjust == -1)
14496     return 2;
14497
14498   elfsym->st_value += adjust;
14499   return 1;
14500 }
14501
14502 /* Finish up dynamic symbol handling.  We set the contents of various
14503    dynamic sections here.  */
14504
14505 static bfd_boolean
14506 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
14507                                  struct bfd_link_info *info,
14508                                  struct elf_link_hash_entry *h,
14509                                  Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
14510 {
14511   struct ppc_link_hash_table *htab;
14512   struct plt_entry *ent;
14513   Elf_Internal_Rela rela;
14514   bfd_byte *loc;
14515
14516   htab = ppc_hash_table (info);
14517   if (htab == NULL)
14518     return FALSE;
14519
14520   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14521     if (ent->plt.offset != (bfd_vma) -1)
14522       {
14523         /* This symbol has an entry in the procedure linkage
14524            table.  Set it up.  */
14525         if (!htab->elf.dynamic_sections_created
14526             || h->dynindx == -1)
14527           {
14528             BFD_ASSERT (h->type == STT_GNU_IFUNC
14529                         && h->def_regular
14530                         && (h->root.type == bfd_link_hash_defined
14531                             || h->root.type == bfd_link_hash_defweak));
14532             rela.r_offset = (htab->iplt->output_section->vma
14533                              + htab->iplt->output_offset
14534                              + ent->plt.offset);
14535             if (htab->opd_abi)
14536               rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14537             else
14538               rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14539             rela.r_addend = (h->root.u.def.value
14540                              + h->root.u.def.section->output_offset
14541                              + h->root.u.def.section->output_section->vma
14542                              + ent->addend);
14543             loc = (htab->reliplt->contents
14544                    + (htab->reliplt->reloc_count++
14545                       * sizeof (Elf64_External_Rela)));
14546           }
14547         else
14548           {
14549             rela.r_offset = (htab->plt->output_section->vma
14550                              + htab->plt->output_offset
14551                              + ent->plt.offset);
14552             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14553             rela.r_addend = ent->addend;
14554             loc = (htab->relplt->contents
14555                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14556                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14557           }
14558         bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14559       }
14560
14561   if (h->needs_copy)
14562     {
14563       /* This symbol needs a copy reloc.  Set it up.  */
14564
14565       if (h->dynindx == -1
14566           || (h->root.type != bfd_link_hash_defined
14567               && h->root.type != bfd_link_hash_defweak)
14568           || htab->relbss == NULL)
14569         abort ();
14570
14571       rela.r_offset = (h->root.u.def.value
14572                        + h->root.u.def.section->output_section->vma
14573                        + h->root.u.def.section->output_offset);
14574       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
14575       rela.r_addend = 0;
14576       loc = htab->relbss->contents;
14577       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
14578       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14579     }
14580
14581   return TRUE;
14582 }
14583
14584 /* Used to decide how to sort relocs in an optimal manner for the
14585    dynamic linker, before writing them out.  */
14586
14587 static enum elf_reloc_type_class
14588 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
14589                             const asection *rel_sec,
14590                             const Elf_Internal_Rela *rela)
14591 {
14592   enum elf_ppc64_reloc_type r_type;
14593   struct ppc_link_hash_table *htab = ppc_hash_table (info);
14594
14595   if (rel_sec == htab->reliplt)
14596     return reloc_class_ifunc;
14597
14598   r_type = ELF64_R_TYPE (rela->r_info);
14599   switch (r_type)
14600     {
14601     case R_PPC64_RELATIVE:
14602       return reloc_class_relative;
14603     case R_PPC64_JMP_SLOT:
14604       return reloc_class_plt;
14605     case R_PPC64_COPY:
14606       return reloc_class_copy;
14607     default:
14608       return reloc_class_normal;
14609     }
14610 }
14611
14612 /* Finish up the dynamic sections.  */
14613
14614 static bfd_boolean
14615 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
14616                                    struct bfd_link_info *info)
14617 {
14618   struct ppc_link_hash_table *htab;
14619   bfd *dynobj;
14620   asection *sdyn;
14621
14622   htab = ppc_hash_table (info);
14623   if (htab == NULL)
14624     return FALSE;
14625
14626   dynobj = htab->elf.dynobj;
14627   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
14628
14629   if (htab->elf.dynamic_sections_created)
14630     {
14631       Elf64_External_Dyn *dyncon, *dynconend;
14632
14633       if (sdyn == NULL || htab->got == NULL)
14634         abort ();
14635
14636       dyncon = (Elf64_External_Dyn *) sdyn->contents;
14637       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
14638       for (; dyncon < dynconend; dyncon++)
14639         {
14640           Elf_Internal_Dyn dyn;
14641           asection *s;
14642
14643           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
14644
14645           switch (dyn.d_tag)
14646             {
14647             default:
14648               continue;
14649
14650             case DT_PPC64_GLINK:
14651               s = htab->glink;
14652               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14653               /* We stupidly defined DT_PPC64_GLINK to be the start
14654                  of glink rather than the first entry point, which is
14655                  what ld.so needs, and now have a bigger stub to
14656                  support automatic multiple TOCs.  */
14657               dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
14658               break;
14659
14660             case DT_PPC64_OPD:
14661               s = bfd_get_section_by_name (output_bfd, ".opd");
14662               if (s == NULL)
14663                 continue;
14664               dyn.d_un.d_ptr = s->vma;
14665               break;
14666
14667             case DT_PPC64_OPT:
14668               if (htab->do_multi_toc && htab->multi_toc_needed)
14669                 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
14670               break;
14671
14672             case DT_PPC64_OPDSZ:
14673               s = bfd_get_section_by_name (output_bfd, ".opd");
14674               if (s == NULL)
14675                 continue;
14676               dyn.d_un.d_val = s->size;
14677               break;
14678
14679             case DT_PLTGOT:
14680               s = htab->plt;
14681               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14682               break;
14683
14684             case DT_JMPREL:
14685               s = htab->relplt;
14686               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14687               break;
14688
14689             case DT_PLTRELSZ:
14690               dyn.d_un.d_val = htab->relplt->size;
14691               break;
14692
14693             case DT_RELASZ:
14694               /* Don't count procedure linkage table relocs in the
14695                  overall reloc count.  */
14696               s = htab->relplt;
14697               if (s == NULL)
14698                 continue;
14699               dyn.d_un.d_val -= s->size;
14700               break;
14701
14702             case DT_RELA:
14703               /* We may not be using the standard ELF linker script.
14704                  If .rela.plt is the first .rela section, we adjust
14705                  DT_RELA to not include it.  */
14706               s = htab->relplt;
14707               if (s == NULL)
14708                 continue;
14709               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
14710                 continue;
14711               dyn.d_un.d_ptr += s->size;
14712               break;
14713             }
14714
14715           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
14716         }
14717     }
14718
14719   if (htab->got != NULL && htab->got->size != 0)
14720     {
14721       /* Fill in the first entry in the global offset table.
14722          We use it to hold the link-time TOCbase.  */
14723       bfd_put_64 (output_bfd,
14724                   elf_gp (output_bfd) + TOC_BASE_OFF,
14725                   htab->got->contents);
14726
14727       /* Set .got entry size.  */
14728       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
14729     }
14730
14731   if (htab->plt != NULL && htab->plt->size != 0)
14732     {
14733       /* Set .plt entry size.  */
14734       elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
14735         = PLT_ENTRY_SIZE (htab);
14736     }
14737
14738   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
14739      brlt ourselves if emitrelocations.  */
14740   if (htab->brlt != NULL
14741       && htab->brlt->reloc_count != 0
14742       && !_bfd_elf_link_output_relocs (output_bfd,
14743                                        htab->brlt,
14744                                        elf_section_data (htab->brlt)->rela.hdr,
14745                                        elf_section_data (htab->brlt)->relocs,
14746                                        NULL))
14747     return FALSE;
14748
14749   if (htab->glink != NULL
14750       && htab->glink->reloc_count != 0
14751       && !_bfd_elf_link_output_relocs (output_bfd,
14752                                        htab->glink,
14753                                        elf_section_data (htab->glink)->rela.hdr,
14754                                        elf_section_data (htab->glink)->relocs,
14755                                        NULL))
14756     return FALSE;
14757
14758
14759   if (htab->glink_eh_frame != NULL
14760       && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
14761       && !_bfd_elf_write_section_eh_frame (output_bfd, info,
14762                                            htab->glink_eh_frame,
14763                                            htab->glink_eh_frame->contents))
14764     return FALSE;
14765
14766   /* We need to handle writing out multiple GOT sections ourselves,
14767      since we didn't add them to DYNOBJ.  We know dynobj is the first
14768      bfd.  */
14769   while ((dynobj = dynobj->link_next) != NULL)
14770     {
14771       asection *s;
14772
14773       if (!is_ppc64_elf (dynobj))
14774         continue;
14775
14776       s = ppc64_elf_tdata (dynobj)->got;
14777       if (s != NULL
14778           && s->size != 0
14779           && s->output_section != bfd_abs_section_ptr
14780           && !bfd_set_section_contents (output_bfd, s->output_section,
14781                                         s->contents, s->output_offset,
14782                                         s->size))
14783         return FALSE;
14784       s = ppc64_elf_tdata (dynobj)->relgot;
14785       if (s != NULL
14786           && s->size != 0
14787           && s->output_section != bfd_abs_section_ptr
14788           && !bfd_set_section_contents (output_bfd, s->output_section,
14789                                         s->contents, s->output_offset,
14790                                         s->size))
14791         return FALSE;
14792     }
14793
14794   return TRUE;
14795 }
14796
14797 #include "elf64-target.h"
14798
14799 /* FreeBSD support */
14800
14801 #undef  TARGET_LITTLE_SYM
14802 #undef  TARGET_LITTLE_NAME
14803
14804 #undef  TARGET_BIG_SYM
14805 #define TARGET_BIG_SYM  bfd_elf64_powerpc_freebsd_vec
14806 #undef  TARGET_BIG_NAME
14807 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
14808
14809 #undef  ELF_OSABI
14810 #define ELF_OSABI       ELFOSABI_FREEBSD
14811
14812 #undef  elf64_bed
14813 #define elf64_bed       elf64_powerpc_fbsd_bed
14814
14815 #include "elf64-target.h"
14816