Add ELFv2 .localentry support.
[external/binutils.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3    2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4    Written by Linus Nordberg, Swox AB <info@swox.com>,
5    based on elf32-ppc.c by Ian Lance Taylor.
6    Largely rewritten by Alan Modra.
7
8    This file is part of BFD, the Binary File Descriptor library.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License along
21    with this program; if not, write to the Free Software Foundation, Inc.,
22    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
23
24
25 /* The 64-bit PowerPC ELF ABI may be found at
26    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
28
29 #include "sysdep.h"
30 #include <stdarg.h>
31 #include "bfd.h"
32 #include "bfdlink.h"
33 #include "libbfd.h"
34 #include "elf-bfd.h"
35 #include "elf/ppc64.h"
36 #include "elf64-ppc.h"
37 #include "dwarf2.h"
38
39 static bfd_reloc_status_type ppc64_elf_ha_reloc
40   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41 static bfd_reloc_status_type ppc64_elf_branch_reloc
42   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
44   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
45 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
46   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
47 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
48   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
49 static bfd_reloc_status_type ppc64_elf_toc_reloc
50   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
51 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
52   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
53 static bfd_reloc_status_type ppc64_elf_toc64_reloc
54   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
55 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
56   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
57 static bfd_vma opd_entry_value
58   (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
59
60 #define TARGET_LITTLE_SYM       bfd_elf64_powerpcle_vec
61 #define TARGET_LITTLE_NAME      "elf64-powerpcle"
62 #define TARGET_BIG_SYM          bfd_elf64_powerpc_vec
63 #define TARGET_BIG_NAME         "elf64-powerpc"
64 #define ELF_ARCH                bfd_arch_powerpc
65 #define ELF_TARGET_ID           PPC64_ELF_DATA
66 #define ELF_MACHINE_CODE        EM_PPC64
67 #define ELF_MAXPAGESIZE         0x10000
68 #define ELF_COMMONPAGESIZE      0x1000
69 #define elf_info_to_howto       ppc64_elf_info_to_howto
70
71 #define elf_backend_want_got_sym 0
72 #define elf_backend_want_plt_sym 0
73 #define elf_backend_plt_alignment 3
74 #define elf_backend_plt_not_loaded 1
75 #define elf_backend_got_header_size 8
76 #define elf_backend_can_gc_sections 1
77 #define elf_backend_can_refcount 1
78 #define elf_backend_rela_normal 1
79 #define elf_backend_default_execstack 0
80
81 #define bfd_elf64_mkobject                    ppc64_elf_mkobject
82 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
83 #define bfd_elf64_bfd_reloc_name_lookup       ppc64_elf_reloc_name_lookup
84 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
85 #define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
86 #define bfd_elf64_new_section_hook            ppc64_elf_new_section_hook
87 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
88 #define bfd_elf64_bfd_link_hash_table_free    ppc64_elf_link_hash_table_free
89 #define bfd_elf64_get_synthetic_symtab        ppc64_elf_get_synthetic_symtab
90 #define bfd_elf64_bfd_link_just_syms          ppc64_elf_link_just_syms
91
92 #define elf_backend_object_p                  ppc64_elf_object_p
93 #define elf_backend_grok_prstatus             ppc64_elf_grok_prstatus
94 #define elf_backend_grok_psinfo               ppc64_elf_grok_psinfo
95 #define elf_backend_write_core_note           ppc64_elf_write_core_note
96 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
97 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
98 #define elf_backend_add_symbol_hook           ppc64_elf_add_symbol_hook
99 #define elf_backend_check_directives          ppc64_elf_process_dot_syms
100 #define elf_backend_notice_as_needed          ppc64_elf_notice_as_needed
101 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
102 #define elf_backend_check_relocs              ppc64_elf_check_relocs
103 #define elf_backend_gc_keep                   ppc64_elf_gc_keep
104 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
105 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
106 #define elf_backend_gc_sweep_hook             ppc64_elf_gc_sweep_hook
107 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
108 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
109 #define elf_backend_maybe_function_sym        ppc64_elf_maybe_function_sym
110 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
111 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
112 #define elf_backend_init_index_section        _bfd_elf_init_2_index_sections
113 #define elf_backend_action_discarded          ppc64_elf_action_discarded
114 #define elf_backend_relocate_section          ppc64_elf_relocate_section
115 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
116 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
117 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
118 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
119 #define elf_backend_special_sections          ppc64_elf_special_sections
120 #define elf_backend_post_process_headers      _bfd_elf_set_osabi
121 #define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
122
123 /* The name of the dynamic interpreter.  This is put in the .interp
124    section.  */
125 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
126
127 /* The size in bytes of an entry in the procedure linkage table.  */
128 #define PLT_ENTRY_SIZE 24
129
130 /* The initial size of the plt reserved for the dynamic linker.  */
131 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
132
133 /* TOC base pointers offset from start of TOC.  */
134 #define TOC_BASE_OFF    0x8000
135
136 /* Offset of tp and dtp pointers from start of TLS block.  */
137 #define TP_OFFSET       0x7000
138 #define DTP_OFFSET      0x8000
139
140 /* .plt call stub instructions.  The normal stub is like this, but
141    sometimes the .plt entry crosses a 64k boundary and we need to
142    insert an addi to adjust r11.  */
143 #define PLT_CALL_STUB_SIZE (7*4)
144 #define STD_R2_40R1     0xf8410028      /* std   %r2,40(%r1)         */
145 #define ADDIS_R11_R2    0x3d620000      /* addis %r11,%r2,xxx@ha     */
146 #define LD_R12_0R11     0xe98b0000      /* ld    %r12,xxx+0@l(%r11)  */
147 #define MTCTR_R12       0x7d8903a6      /* mtctr %r12                */
148 #define LD_R2_0R11      0xe84b0000      /* ld    %r2,xxx+8@l(%r11)   */
149 #define LD_R11_0R11     0xe96b0000      /* ld    %r11,xxx+16@l(%r11) */
150 #define BCTR            0x4e800420      /* bctr                      */
151
152 #define ADDI_R11_R11    0x396b0000      /* addi %r11,%r11,off@l  */
153 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
154 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
155
156 #define XOR_R2_R12_R12  0x7d826278      /* xor   %r2,%r12,%r12   */
157 #define ADD_R11_R11_R2  0x7d6b1214      /* add   %r11,%r11,%r2   */
158 #define XOR_R11_R12_R12 0x7d8b6278      /* xor   %r11,%r12,%r12  */
159 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
160 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
161 #define BNECTR          0x4ca20420      /* bnectr+               */
162 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
163
164 #define LD_R12_0R2      0xe9820000      /* ld    %r12,xxx+0(%r2) */
165 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
166 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
167
168 #define LD_R2_40R1      0xe8410028      /* ld    %r2,40(%r1)     */
169
170 /* glink call stub instructions.  We enter with the index in R0.  */
171 #define GLINK_CALL_STUB_SIZE (16*4)
172                                         /* 0:                           */
173                                         /*  .quad plt0-1f               */
174                                         /* __glink:                     */
175 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
176 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
177                                         /* 1:                           */
178 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
179                                         /*  ld %2,(0b-1b)(%11)          */
180 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
181 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
182                                         /*  ld %12,0(%11)               */
183                                         /*  ld %2,8(%11)                */
184                                         /*  mtctr %12                   */
185                                         /*  ld %11,16(%11)              */
186                                         /*  bctr                        */
187
188 /* Pad with this.  */
189 #define NOP             0x60000000
190
191 /* Some other nops.  */
192 #define CROR_151515     0x4def7b82
193 #define CROR_313131     0x4ffffb82
194
195 /* .glink entries for the first 32k functions are two instructions.  */
196 #define LI_R0_0         0x38000000      /* li    %r0,0          */
197 #define B_DOT           0x48000000      /* b     .              */
198
199 /* After that, we need two instructions to load the index, followed by
200    a branch.  */
201 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
202 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
203
204 /* Instructions used by the save and restore reg functions.  */
205 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
206 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
207 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
208 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
209 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
210 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
211 #define LI_R12_0        0x39800000      /* li    %r12,0         */
212 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
213 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
214 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
215 #define BLR             0x4e800020      /* blr                  */
216
217 /* Since .opd is an array of descriptors and each entry will end up
218    with identical R_PPC64_RELATIVE relocs, there is really no need to
219    propagate .opd relocs;  The dynamic linker should be taught to
220    relocate .opd without reloc entries.  */
221 #ifndef NO_OPD_RELOCS
222 #define NO_OPD_RELOCS 0
223 #endif
224 \f
225 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
226
227 /* Relocation HOWTO's.  */
228 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
229
230 static reloc_howto_type ppc64_elf_howto_raw[] = {
231   /* This reloc does nothing.  */
232   HOWTO (R_PPC64_NONE,          /* type */
233          0,                     /* rightshift */
234          2,                     /* size (0 = byte, 1 = short, 2 = long) */
235          32,                    /* bitsize */
236          FALSE,                 /* pc_relative */
237          0,                     /* bitpos */
238          complain_overflow_dont, /* complain_on_overflow */
239          bfd_elf_generic_reloc, /* special_function */
240          "R_PPC64_NONE",        /* name */
241          FALSE,                 /* partial_inplace */
242          0,                     /* src_mask */
243          0,                     /* dst_mask */
244          FALSE),                /* pcrel_offset */
245
246   /* A standard 32 bit relocation.  */
247   HOWTO (R_PPC64_ADDR32,        /* type */
248          0,                     /* rightshift */
249          2,                     /* size (0 = byte, 1 = short, 2 = long) */
250          32,                    /* bitsize */
251          FALSE,                 /* pc_relative */
252          0,                     /* bitpos */
253          complain_overflow_bitfield, /* complain_on_overflow */
254          bfd_elf_generic_reloc, /* special_function */
255          "R_PPC64_ADDR32",      /* name */
256          FALSE,                 /* partial_inplace */
257          0,                     /* src_mask */
258          0xffffffff,            /* dst_mask */
259          FALSE),                /* pcrel_offset */
260
261   /* An absolute 26 bit branch; the lower two bits must be zero.
262      FIXME: we don't check that, we just clear them.  */
263   HOWTO (R_PPC64_ADDR24,        /* type */
264          0,                     /* rightshift */
265          2,                     /* size (0 = byte, 1 = short, 2 = long) */
266          26,                    /* bitsize */
267          FALSE,                 /* pc_relative */
268          0,                     /* bitpos */
269          complain_overflow_bitfield, /* complain_on_overflow */
270          bfd_elf_generic_reloc, /* special_function */
271          "R_PPC64_ADDR24",      /* name */
272          FALSE,                 /* partial_inplace */
273          0,                     /* src_mask */
274          0x03fffffc,            /* dst_mask */
275          FALSE),                /* pcrel_offset */
276
277   /* A standard 16 bit relocation.  */
278   HOWTO (R_PPC64_ADDR16,        /* type */
279          0,                     /* rightshift */
280          1,                     /* size (0 = byte, 1 = short, 2 = long) */
281          16,                    /* bitsize */
282          FALSE,                 /* pc_relative */
283          0,                     /* bitpos */
284          complain_overflow_bitfield, /* complain_on_overflow */
285          bfd_elf_generic_reloc, /* special_function */
286          "R_PPC64_ADDR16",      /* name */
287          FALSE,                 /* partial_inplace */
288          0,                     /* src_mask */
289          0xffff,                /* dst_mask */
290          FALSE),                /* pcrel_offset */
291
292   /* A 16 bit relocation without overflow.  */
293   HOWTO (R_PPC64_ADDR16_LO,     /* type */
294          0,                     /* rightshift */
295          1,                     /* size (0 = byte, 1 = short, 2 = long) */
296          16,                    /* bitsize */
297          FALSE,                 /* pc_relative */
298          0,                     /* bitpos */
299          complain_overflow_dont,/* complain_on_overflow */
300          bfd_elf_generic_reloc, /* special_function */
301          "R_PPC64_ADDR16_LO",   /* name */
302          FALSE,                 /* partial_inplace */
303          0,                     /* src_mask */
304          0xffff,                /* dst_mask */
305          FALSE),                /* pcrel_offset */
306
307   /* Bits 16-31 of an address.  */
308   HOWTO (R_PPC64_ADDR16_HI,     /* type */
309          16,                    /* rightshift */
310          1,                     /* size (0 = byte, 1 = short, 2 = long) */
311          16,                    /* bitsize */
312          FALSE,                 /* pc_relative */
313          0,                     /* bitpos */
314          complain_overflow_signed, /* complain_on_overflow */
315          bfd_elf_generic_reloc, /* special_function */
316          "R_PPC64_ADDR16_HI",   /* name */
317          FALSE,                 /* partial_inplace */
318          0,                     /* src_mask */
319          0xffff,                /* dst_mask */
320          FALSE),                /* pcrel_offset */
321
322   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
323      bits, treated as a signed number, is negative.  */
324   HOWTO (R_PPC64_ADDR16_HA,     /* type */
325          16,                    /* rightshift */
326          1,                     /* size (0 = byte, 1 = short, 2 = long) */
327          16,                    /* bitsize */
328          FALSE,                 /* pc_relative */
329          0,                     /* bitpos */
330          complain_overflow_signed, /* complain_on_overflow */
331          ppc64_elf_ha_reloc,    /* special_function */
332          "R_PPC64_ADDR16_HA",   /* name */
333          FALSE,                 /* partial_inplace */
334          0,                     /* src_mask */
335          0xffff,                /* dst_mask */
336          FALSE),                /* pcrel_offset */
337
338   /* An absolute 16 bit branch; the lower two bits must be zero.
339      FIXME: we don't check that, we just clear them.  */
340   HOWTO (R_PPC64_ADDR14,        /* type */
341          0,                     /* rightshift */
342          2,                     /* size (0 = byte, 1 = short, 2 = long) */
343          16,                    /* bitsize */
344          FALSE,                 /* pc_relative */
345          0,                     /* bitpos */
346          complain_overflow_bitfield, /* complain_on_overflow */
347          ppc64_elf_branch_reloc, /* special_function */
348          "R_PPC64_ADDR14",      /* name */
349          FALSE,                 /* partial_inplace */
350          0,                     /* src_mask */
351          0x0000fffc,            /* dst_mask */
352          FALSE),                /* pcrel_offset */
353
354   /* An absolute 16 bit branch, for which bit 10 should be set to
355      indicate that the branch is expected to be taken.  The lower two
356      bits must be zero.  */
357   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
358          0,                     /* rightshift */
359          2,                     /* size (0 = byte, 1 = short, 2 = long) */
360          16,                    /* bitsize */
361          FALSE,                 /* pc_relative */
362          0,                     /* bitpos */
363          complain_overflow_bitfield, /* complain_on_overflow */
364          ppc64_elf_brtaken_reloc, /* special_function */
365          "R_PPC64_ADDR14_BRTAKEN",/* name */
366          FALSE,                 /* partial_inplace */
367          0,                     /* src_mask */
368          0x0000fffc,            /* dst_mask */
369          FALSE),                /* pcrel_offset */
370
371   /* An absolute 16 bit branch, for which bit 10 should be set to
372      indicate that the branch is not expected to be taken.  The lower
373      two bits must be zero.  */
374   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
375          0,                     /* rightshift */
376          2,                     /* size (0 = byte, 1 = short, 2 = long) */
377          16,                    /* bitsize */
378          FALSE,                 /* pc_relative */
379          0,                     /* bitpos */
380          complain_overflow_bitfield, /* complain_on_overflow */
381          ppc64_elf_brtaken_reloc, /* special_function */
382          "R_PPC64_ADDR14_BRNTAKEN",/* name */
383          FALSE,                 /* partial_inplace */
384          0,                     /* src_mask */
385          0x0000fffc,            /* dst_mask */
386          FALSE),                /* pcrel_offset */
387
388   /* A relative 26 bit branch; the lower two bits must be zero.  */
389   HOWTO (R_PPC64_REL24,         /* type */
390          0,                     /* rightshift */
391          2,                     /* size (0 = byte, 1 = short, 2 = long) */
392          26,                    /* bitsize */
393          TRUE,                  /* pc_relative */
394          0,                     /* bitpos */
395          complain_overflow_signed, /* complain_on_overflow */
396          ppc64_elf_branch_reloc, /* special_function */
397          "R_PPC64_REL24",       /* name */
398          FALSE,                 /* partial_inplace */
399          0,                     /* src_mask */
400          0x03fffffc,            /* dst_mask */
401          TRUE),                 /* pcrel_offset */
402
403   /* A relative 16 bit branch; the lower two bits must be zero.  */
404   HOWTO (R_PPC64_REL14,         /* type */
405          0,                     /* rightshift */
406          2,                     /* size (0 = byte, 1 = short, 2 = long) */
407          16,                    /* bitsize */
408          TRUE,                  /* pc_relative */
409          0,                     /* bitpos */
410          complain_overflow_signed, /* complain_on_overflow */
411          ppc64_elf_branch_reloc, /* special_function */
412          "R_PPC64_REL14",       /* name */
413          FALSE,                 /* partial_inplace */
414          0,                     /* src_mask */
415          0x0000fffc,            /* dst_mask */
416          TRUE),                 /* pcrel_offset */
417
418   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
419      the branch is expected to be taken.  The lower two bits must be
420      zero.  */
421   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
422          0,                     /* rightshift */
423          2,                     /* size (0 = byte, 1 = short, 2 = long) */
424          16,                    /* bitsize */
425          TRUE,                  /* pc_relative */
426          0,                     /* bitpos */
427          complain_overflow_signed, /* complain_on_overflow */
428          ppc64_elf_brtaken_reloc, /* special_function */
429          "R_PPC64_REL14_BRTAKEN", /* name */
430          FALSE,                 /* partial_inplace */
431          0,                     /* src_mask */
432          0x0000fffc,            /* dst_mask */
433          TRUE),                 /* pcrel_offset */
434
435   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
436      the branch is not expected to be taken.  The lower two bits must
437      be zero.  */
438   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
439          0,                     /* rightshift */
440          2,                     /* size (0 = byte, 1 = short, 2 = long) */
441          16,                    /* bitsize */
442          TRUE,                  /* pc_relative */
443          0,                     /* bitpos */
444          complain_overflow_signed, /* complain_on_overflow */
445          ppc64_elf_brtaken_reloc, /* special_function */
446          "R_PPC64_REL14_BRNTAKEN",/* name */
447          FALSE,                 /* partial_inplace */
448          0,                     /* src_mask */
449          0x0000fffc,            /* dst_mask */
450          TRUE),                 /* pcrel_offset */
451
452   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
453      symbol.  */
454   HOWTO (R_PPC64_GOT16,         /* type */
455          0,                     /* rightshift */
456          1,                     /* size (0 = byte, 1 = short, 2 = long) */
457          16,                    /* bitsize */
458          FALSE,                 /* pc_relative */
459          0,                     /* bitpos */
460          complain_overflow_signed, /* complain_on_overflow */
461          ppc64_elf_unhandled_reloc, /* special_function */
462          "R_PPC64_GOT16",       /* name */
463          FALSE,                 /* partial_inplace */
464          0,                     /* src_mask */
465          0xffff,                /* dst_mask */
466          FALSE),                /* pcrel_offset */
467
468   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
469      the symbol.  */
470   HOWTO (R_PPC64_GOT16_LO,      /* type */
471          0,                     /* rightshift */
472          1,                     /* size (0 = byte, 1 = short, 2 = long) */
473          16,                    /* bitsize */
474          FALSE,                 /* pc_relative */
475          0,                     /* bitpos */
476          complain_overflow_dont, /* complain_on_overflow */
477          ppc64_elf_unhandled_reloc, /* special_function */
478          "R_PPC64_GOT16_LO",    /* name */
479          FALSE,                 /* partial_inplace */
480          0,                     /* src_mask */
481          0xffff,                /* dst_mask */
482          FALSE),                /* pcrel_offset */
483
484   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
485      the symbol.  */
486   HOWTO (R_PPC64_GOT16_HI,      /* type */
487          16,                    /* rightshift */
488          1,                     /* size (0 = byte, 1 = short, 2 = long) */
489          16,                    /* bitsize */
490          FALSE,                 /* pc_relative */
491          0,                     /* bitpos */
492          complain_overflow_signed,/* complain_on_overflow */
493          ppc64_elf_unhandled_reloc, /* special_function */
494          "R_PPC64_GOT16_HI",    /* name */
495          FALSE,                 /* partial_inplace */
496          0,                     /* src_mask */
497          0xffff,                /* dst_mask */
498          FALSE),                /* pcrel_offset */
499
500   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
501      the symbol.  */
502   HOWTO (R_PPC64_GOT16_HA,      /* type */
503          16,                    /* rightshift */
504          1,                     /* size (0 = byte, 1 = short, 2 = long) */
505          16,                    /* bitsize */
506          FALSE,                 /* pc_relative */
507          0,                     /* bitpos */
508          complain_overflow_signed,/* complain_on_overflow */
509          ppc64_elf_unhandled_reloc, /* special_function */
510          "R_PPC64_GOT16_HA",    /* name */
511          FALSE,                 /* partial_inplace */
512          0,                     /* src_mask */
513          0xffff,                /* dst_mask */
514          FALSE),                /* pcrel_offset */
515
516   /* This is used only by the dynamic linker.  The symbol should exist
517      both in the object being run and in some shared library.  The
518      dynamic linker copies the data addressed by the symbol from the
519      shared library into the object, because the object being
520      run has to have the data at some particular address.  */
521   HOWTO (R_PPC64_COPY,          /* type */
522          0,                     /* rightshift */
523          0,                     /* this one is variable size */
524          0,                     /* bitsize */
525          FALSE,                 /* pc_relative */
526          0,                     /* bitpos */
527          complain_overflow_dont, /* complain_on_overflow */
528          ppc64_elf_unhandled_reloc, /* special_function */
529          "R_PPC64_COPY",        /* name */
530          FALSE,                 /* partial_inplace */
531          0,                     /* src_mask */
532          0,                     /* dst_mask */
533          FALSE),                /* pcrel_offset */
534
535   /* Like R_PPC64_ADDR64, but used when setting global offset table
536      entries.  */
537   HOWTO (R_PPC64_GLOB_DAT,      /* type */
538          0,                     /* rightshift */
539          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
540          64,                    /* bitsize */
541          FALSE,                 /* pc_relative */
542          0,                     /* bitpos */
543          complain_overflow_dont, /* complain_on_overflow */
544          ppc64_elf_unhandled_reloc,  /* special_function */
545          "R_PPC64_GLOB_DAT",    /* name */
546          FALSE,                 /* partial_inplace */
547          0,                     /* src_mask */
548          ONES (64),             /* dst_mask */
549          FALSE),                /* pcrel_offset */
550
551   /* Created by the link editor.  Marks a procedure linkage table
552      entry for a symbol.  */
553   HOWTO (R_PPC64_JMP_SLOT,      /* type */
554          0,                     /* rightshift */
555          0,                     /* size (0 = byte, 1 = short, 2 = long) */
556          0,                     /* bitsize */
557          FALSE,                 /* pc_relative */
558          0,                     /* bitpos */
559          complain_overflow_dont, /* complain_on_overflow */
560          ppc64_elf_unhandled_reloc, /* special_function */
561          "R_PPC64_JMP_SLOT",    /* name */
562          FALSE,                 /* partial_inplace */
563          0,                     /* src_mask */
564          0,                     /* dst_mask */
565          FALSE),                /* pcrel_offset */
566
567   /* Used only by the dynamic linker.  When the object is run, this
568      doubleword64 is set to the load address of the object, plus the
569      addend.  */
570   HOWTO (R_PPC64_RELATIVE,      /* type */
571          0,                     /* rightshift */
572          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
573          64,                    /* bitsize */
574          FALSE,                 /* pc_relative */
575          0,                     /* bitpos */
576          complain_overflow_dont, /* complain_on_overflow */
577          bfd_elf_generic_reloc, /* special_function */
578          "R_PPC64_RELATIVE",    /* name */
579          FALSE,                 /* partial_inplace */
580          0,                     /* src_mask */
581          ONES (64),             /* dst_mask */
582          FALSE),                /* pcrel_offset */
583
584   /* Like R_PPC64_ADDR32, but may be unaligned.  */
585   HOWTO (R_PPC64_UADDR32,       /* type */
586          0,                     /* rightshift */
587          2,                     /* size (0 = byte, 1 = short, 2 = long) */
588          32,                    /* bitsize */
589          FALSE,                 /* pc_relative */
590          0,                     /* bitpos */
591          complain_overflow_bitfield, /* complain_on_overflow */
592          bfd_elf_generic_reloc, /* special_function */
593          "R_PPC64_UADDR32",     /* name */
594          FALSE,                 /* partial_inplace */
595          0,                     /* src_mask */
596          0xffffffff,            /* dst_mask */
597          FALSE),                /* pcrel_offset */
598
599   /* Like R_PPC64_ADDR16, but may be unaligned.  */
600   HOWTO (R_PPC64_UADDR16,       /* type */
601          0,                     /* rightshift */
602          1,                     /* size (0 = byte, 1 = short, 2 = long) */
603          16,                    /* bitsize */
604          FALSE,                 /* pc_relative */
605          0,                     /* bitpos */
606          complain_overflow_bitfield, /* complain_on_overflow */
607          bfd_elf_generic_reloc, /* special_function */
608          "R_PPC64_UADDR16",     /* name */
609          FALSE,                 /* partial_inplace */
610          0,                     /* src_mask */
611          0xffff,                /* dst_mask */
612          FALSE),                /* pcrel_offset */
613
614   /* 32-bit PC relative.  */
615   HOWTO (R_PPC64_REL32,         /* type */
616          0,                     /* rightshift */
617          2,                     /* size (0 = byte, 1 = short, 2 = long) */
618          32,                    /* bitsize */
619          TRUE,                  /* pc_relative */
620          0,                     /* bitpos */
621          /* FIXME: Verify.  Was complain_overflow_bitfield.  */
622          complain_overflow_signed, /* complain_on_overflow */
623          bfd_elf_generic_reloc, /* special_function */
624          "R_PPC64_REL32",       /* name */
625          FALSE,                 /* partial_inplace */
626          0,                     /* src_mask */
627          0xffffffff,            /* dst_mask */
628          TRUE),                 /* pcrel_offset */
629
630   /* 32-bit relocation to the symbol's procedure linkage table.  */
631   HOWTO (R_PPC64_PLT32,         /* type */
632          0,                     /* rightshift */
633          2,                     /* size (0 = byte, 1 = short, 2 = long) */
634          32,                    /* bitsize */
635          FALSE,                 /* pc_relative */
636          0,                     /* bitpos */
637          complain_overflow_bitfield, /* complain_on_overflow */
638          ppc64_elf_unhandled_reloc, /* special_function */
639          "R_PPC64_PLT32",       /* name */
640          FALSE,                 /* partial_inplace */
641          0,                     /* src_mask */
642          0xffffffff,            /* dst_mask */
643          FALSE),                /* pcrel_offset */
644
645   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
646      FIXME: R_PPC64_PLTREL32 not supported.  */
647   HOWTO (R_PPC64_PLTREL32,      /* type */
648          0,                     /* rightshift */
649          2,                     /* size (0 = byte, 1 = short, 2 = long) */
650          32,                    /* bitsize */
651          TRUE,                  /* pc_relative */
652          0,                     /* bitpos */
653          complain_overflow_signed, /* complain_on_overflow */
654          bfd_elf_generic_reloc, /* special_function */
655          "R_PPC64_PLTREL32",    /* name */
656          FALSE,                 /* partial_inplace */
657          0,                     /* src_mask */
658          0xffffffff,            /* dst_mask */
659          TRUE),                 /* pcrel_offset */
660
661   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
662      the symbol.  */
663   HOWTO (R_PPC64_PLT16_LO,      /* type */
664          0,                     /* rightshift */
665          1,                     /* size (0 = byte, 1 = short, 2 = long) */
666          16,                    /* bitsize */
667          FALSE,                 /* pc_relative */
668          0,                     /* bitpos */
669          complain_overflow_dont, /* complain_on_overflow */
670          ppc64_elf_unhandled_reloc, /* special_function */
671          "R_PPC64_PLT16_LO",    /* name */
672          FALSE,                 /* partial_inplace */
673          0,                     /* src_mask */
674          0xffff,                /* dst_mask */
675          FALSE),                /* pcrel_offset */
676
677   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
678      the symbol.  */
679   HOWTO (R_PPC64_PLT16_HI,      /* type */
680          16,                    /* rightshift */
681          1,                     /* size (0 = byte, 1 = short, 2 = long) */
682          16,                    /* bitsize */
683          FALSE,                 /* pc_relative */
684          0,                     /* bitpos */
685          complain_overflow_signed, /* complain_on_overflow */
686          ppc64_elf_unhandled_reloc, /* special_function */
687          "R_PPC64_PLT16_HI",    /* name */
688          FALSE,                 /* partial_inplace */
689          0,                     /* src_mask */
690          0xffff,                /* dst_mask */
691          FALSE),                /* pcrel_offset */
692
693   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
694      the symbol.  */
695   HOWTO (R_PPC64_PLT16_HA,      /* type */
696          16,                    /* rightshift */
697          1,                     /* size (0 = byte, 1 = short, 2 = long) */
698          16,                    /* bitsize */
699          FALSE,                 /* pc_relative */
700          0,                     /* bitpos */
701          complain_overflow_signed, /* complain_on_overflow */
702          ppc64_elf_unhandled_reloc, /* special_function */
703          "R_PPC64_PLT16_HA",    /* name */
704          FALSE,                 /* partial_inplace */
705          0,                     /* src_mask */
706          0xffff,                /* dst_mask */
707          FALSE),                /* pcrel_offset */
708
709   /* 16-bit section relative relocation.  */
710   HOWTO (R_PPC64_SECTOFF,       /* type */
711          0,                     /* rightshift */
712          1,                     /* size (0 = byte, 1 = short, 2 = long) */
713          16,                    /* bitsize */
714          FALSE,                 /* pc_relative */
715          0,                     /* bitpos */
716          complain_overflow_bitfield, /* complain_on_overflow */
717          ppc64_elf_sectoff_reloc, /* special_function */
718          "R_PPC64_SECTOFF",     /* name */
719          FALSE,                 /* partial_inplace */
720          0,                     /* src_mask */
721          0xffff,                /* dst_mask */
722          FALSE),                /* pcrel_offset */
723
724   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
725   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
726          0,                     /* rightshift */
727          1,                     /* size (0 = byte, 1 = short, 2 = long) */
728          16,                    /* bitsize */
729          FALSE,                 /* pc_relative */
730          0,                     /* bitpos */
731          complain_overflow_dont, /* complain_on_overflow */
732          ppc64_elf_sectoff_reloc, /* special_function */
733          "R_PPC64_SECTOFF_LO",  /* name */
734          FALSE,                 /* partial_inplace */
735          0,                     /* src_mask */
736          0xffff,                /* dst_mask */
737          FALSE),                /* pcrel_offset */
738
739   /* 16-bit upper half section relative relocation.  */
740   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
741          16,                    /* rightshift */
742          1,                     /* size (0 = byte, 1 = short, 2 = long) */
743          16,                    /* bitsize */
744          FALSE,                 /* pc_relative */
745          0,                     /* bitpos */
746          complain_overflow_signed, /* complain_on_overflow */
747          ppc64_elf_sectoff_reloc, /* special_function */
748          "R_PPC64_SECTOFF_HI",  /* name */
749          FALSE,                 /* partial_inplace */
750          0,                     /* src_mask */
751          0xffff,                /* dst_mask */
752          FALSE),                /* pcrel_offset */
753
754   /* 16-bit upper half adjusted section relative relocation.  */
755   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
756          16,                    /* rightshift */
757          1,                     /* size (0 = byte, 1 = short, 2 = long) */
758          16,                    /* bitsize */
759          FALSE,                 /* pc_relative */
760          0,                     /* bitpos */
761          complain_overflow_signed, /* complain_on_overflow */
762          ppc64_elf_sectoff_ha_reloc, /* special_function */
763          "R_PPC64_SECTOFF_HA",  /* name */
764          FALSE,                 /* partial_inplace */
765          0,                     /* src_mask */
766          0xffff,                /* dst_mask */
767          FALSE),                /* pcrel_offset */
768
769   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
770   HOWTO (R_PPC64_REL30,         /* type */
771          2,                     /* rightshift */
772          2,                     /* size (0 = byte, 1 = short, 2 = long) */
773          30,                    /* bitsize */
774          TRUE,                  /* pc_relative */
775          0,                     /* bitpos */
776          complain_overflow_dont, /* complain_on_overflow */
777          bfd_elf_generic_reloc, /* special_function */
778          "R_PPC64_REL30",       /* name */
779          FALSE,                 /* partial_inplace */
780          0,                     /* src_mask */
781          0xfffffffc,            /* dst_mask */
782          TRUE),                 /* pcrel_offset */
783
784   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
785
786   /* A standard 64-bit relocation.  */
787   HOWTO (R_PPC64_ADDR64,        /* type */
788          0,                     /* rightshift */
789          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
790          64,                    /* bitsize */
791          FALSE,                 /* pc_relative */
792          0,                     /* bitpos */
793          complain_overflow_dont, /* complain_on_overflow */
794          bfd_elf_generic_reloc, /* special_function */
795          "R_PPC64_ADDR64",      /* name */
796          FALSE,                 /* partial_inplace */
797          0,                     /* src_mask */
798          ONES (64),             /* dst_mask */
799          FALSE),                /* pcrel_offset */
800
801   /* The bits 32-47 of an address.  */
802   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
803          32,                    /* rightshift */
804          1,                     /* size (0 = byte, 1 = short, 2 = long) */
805          16,                    /* bitsize */
806          FALSE,                 /* pc_relative */
807          0,                     /* bitpos */
808          complain_overflow_dont, /* complain_on_overflow */
809          bfd_elf_generic_reloc, /* special_function */
810          "R_PPC64_ADDR16_HIGHER", /* name */
811          FALSE,                 /* partial_inplace */
812          0,                     /* src_mask */
813          0xffff,                /* dst_mask */
814          FALSE),                /* pcrel_offset */
815
816   /* The bits 32-47 of an address, plus 1 if the contents of the low
817      16 bits, treated as a signed number, is negative.  */
818   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
819          32,                    /* rightshift */
820          1,                     /* size (0 = byte, 1 = short, 2 = long) */
821          16,                    /* bitsize */
822          FALSE,                 /* pc_relative */
823          0,                     /* bitpos */
824          complain_overflow_dont, /* complain_on_overflow */
825          ppc64_elf_ha_reloc,    /* special_function */
826          "R_PPC64_ADDR16_HIGHERA", /* name */
827          FALSE,                 /* partial_inplace */
828          0,                     /* src_mask */
829          0xffff,                /* dst_mask */
830          FALSE),                /* pcrel_offset */
831
832   /* The bits 48-63 of an address.  */
833   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
834          48,                    /* rightshift */
835          1,                     /* size (0 = byte, 1 = short, 2 = long) */
836          16,                    /* bitsize */
837          FALSE,                 /* pc_relative */
838          0,                     /* bitpos */
839          complain_overflow_dont, /* complain_on_overflow */
840          bfd_elf_generic_reloc, /* special_function */
841          "R_PPC64_ADDR16_HIGHEST", /* name */
842          FALSE,                 /* partial_inplace */
843          0,                     /* src_mask */
844          0xffff,                /* dst_mask */
845          FALSE),                /* pcrel_offset */
846
847   /* The bits 48-63 of an address, plus 1 if the contents of the low
848      16 bits, treated as a signed number, is negative.  */
849   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
850          48,                    /* rightshift */
851          1,                     /* size (0 = byte, 1 = short, 2 = long) */
852          16,                    /* bitsize */
853          FALSE,                 /* pc_relative */
854          0,                     /* bitpos */
855          complain_overflow_dont, /* complain_on_overflow */
856          ppc64_elf_ha_reloc,    /* special_function */
857          "R_PPC64_ADDR16_HIGHESTA", /* name */
858          FALSE,                 /* partial_inplace */
859          0,                     /* src_mask */
860          0xffff,                /* dst_mask */
861          FALSE),                /* pcrel_offset */
862
863   /* Like ADDR64, but may be unaligned.  */
864   HOWTO (R_PPC64_UADDR64,       /* type */
865          0,                     /* rightshift */
866          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
867          64,                    /* bitsize */
868          FALSE,                 /* pc_relative */
869          0,                     /* bitpos */
870          complain_overflow_dont, /* complain_on_overflow */
871          bfd_elf_generic_reloc, /* special_function */
872          "R_PPC64_UADDR64",     /* name */
873          FALSE,                 /* partial_inplace */
874          0,                     /* src_mask */
875          ONES (64),             /* dst_mask */
876          FALSE),                /* pcrel_offset */
877
878   /* 64-bit relative relocation.  */
879   HOWTO (R_PPC64_REL64,         /* type */
880          0,                     /* rightshift */
881          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
882          64,                    /* bitsize */
883          TRUE,                  /* pc_relative */
884          0,                     /* bitpos */
885          complain_overflow_dont, /* complain_on_overflow */
886          bfd_elf_generic_reloc, /* special_function */
887          "R_PPC64_REL64",       /* name */
888          FALSE,                 /* partial_inplace */
889          0,                     /* src_mask */
890          ONES (64),             /* dst_mask */
891          TRUE),                 /* pcrel_offset */
892
893   /* 64-bit relocation to the symbol's procedure linkage table.  */
894   HOWTO (R_PPC64_PLT64,         /* type */
895          0,                     /* rightshift */
896          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
897          64,                    /* bitsize */
898          FALSE,                 /* pc_relative */
899          0,                     /* bitpos */
900          complain_overflow_dont, /* complain_on_overflow */
901          ppc64_elf_unhandled_reloc, /* special_function */
902          "R_PPC64_PLT64",       /* name */
903          FALSE,                 /* partial_inplace */
904          0,                     /* src_mask */
905          ONES (64),             /* dst_mask */
906          FALSE),                /* pcrel_offset */
907
908   /* 64-bit PC relative relocation to the symbol's procedure linkage
909      table.  */
910   /* FIXME: R_PPC64_PLTREL64 not supported.  */
911   HOWTO (R_PPC64_PLTREL64,      /* type */
912          0,                     /* rightshift */
913          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
914          64,                    /* bitsize */
915          TRUE,                  /* pc_relative */
916          0,                     /* bitpos */
917          complain_overflow_dont, /* complain_on_overflow */
918          ppc64_elf_unhandled_reloc, /* special_function */
919          "R_PPC64_PLTREL64",    /* name */
920          FALSE,                 /* partial_inplace */
921          0,                     /* src_mask */
922          ONES (64),             /* dst_mask */
923          TRUE),                 /* pcrel_offset */
924
925   /* 16 bit TOC-relative relocation.  */
926
927   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
928   HOWTO (R_PPC64_TOC16,         /* type */
929          0,                     /* rightshift */
930          1,                     /* size (0 = byte, 1 = short, 2 = long) */
931          16,                    /* bitsize */
932          FALSE,                 /* pc_relative */
933          0,                     /* bitpos */
934          complain_overflow_signed, /* complain_on_overflow */
935          ppc64_elf_toc_reloc,   /* special_function */
936          "R_PPC64_TOC16",       /* name */
937          FALSE,                 /* partial_inplace */
938          0,                     /* src_mask */
939          0xffff,                /* dst_mask */
940          FALSE),                /* pcrel_offset */
941
942   /* 16 bit TOC-relative relocation without overflow.  */
943
944   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
945   HOWTO (R_PPC64_TOC16_LO,      /* type */
946          0,                     /* rightshift */
947          1,                     /* size (0 = byte, 1 = short, 2 = long) */
948          16,                    /* bitsize */
949          FALSE,                 /* pc_relative */
950          0,                     /* bitpos */
951          complain_overflow_dont, /* complain_on_overflow */
952          ppc64_elf_toc_reloc,   /* special_function */
953          "R_PPC64_TOC16_LO",    /* name */
954          FALSE,                 /* partial_inplace */
955          0,                     /* src_mask */
956          0xffff,                /* dst_mask */
957          FALSE),                /* pcrel_offset */
958
959   /* 16 bit TOC-relative relocation, high 16 bits.  */
960
961   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
962   HOWTO (R_PPC64_TOC16_HI,      /* type */
963          16,                    /* rightshift */
964          1,                     /* size (0 = byte, 1 = short, 2 = long) */
965          16,                    /* bitsize */
966          FALSE,                 /* pc_relative */
967          0,                     /* bitpos */
968          complain_overflow_signed, /* complain_on_overflow */
969          ppc64_elf_toc_reloc,   /* special_function */
970          "R_PPC64_TOC16_HI",    /* name */
971          FALSE,                 /* partial_inplace */
972          0,                     /* src_mask */
973          0xffff,                /* dst_mask */
974          FALSE),                /* pcrel_offset */
975
976   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
977      contents of the low 16 bits, treated as a signed number, is
978      negative.  */
979
980   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
981   HOWTO (R_PPC64_TOC16_HA,      /* type */
982          16,                    /* rightshift */
983          1,                     /* size (0 = byte, 1 = short, 2 = long) */
984          16,                    /* bitsize */
985          FALSE,                 /* pc_relative */
986          0,                     /* bitpos */
987          complain_overflow_signed, /* complain_on_overflow */
988          ppc64_elf_toc_ha_reloc, /* special_function */
989          "R_PPC64_TOC16_HA",    /* name */
990          FALSE,                 /* partial_inplace */
991          0,                     /* src_mask */
992          0xffff,                /* dst_mask */
993          FALSE),                /* pcrel_offset */
994
995   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
996
997   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
998   HOWTO (R_PPC64_TOC,           /* type */
999          0,                     /* rightshift */
1000          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1001          64,                    /* bitsize */
1002          FALSE,                 /* pc_relative */
1003          0,                     /* bitpos */
1004          complain_overflow_bitfield, /* complain_on_overflow */
1005          ppc64_elf_toc64_reloc, /* special_function */
1006          "R_PPC64_TOC",         /* name */
1007          FALSE,                 /* partial_inplace */
1008          0,                     /* src_mask */
1009          ONES (64),             /* dst_mask */
1010          FALSE),                /* pcrel_offset */
1011
1012   /* Like R_PPC64_GOT16, but also informs the link editor that the
1013      value to relocate may (!) refer to a PLT entry which the link
1014      editor (a) may replace with the symbol value.  If the link editor
1015      is unable to fully resolve the symbol, it may (b) create a PLT
1016      entry and store the address to the new PLT entry in the GOT.
1017      This permits lazy resolution of function symbols at run time.
1018      The link editor may also skip all of this and just (c) emit a
1019      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1020   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1021     HOWTO (R_PPC64_PLTGOT16,    /* type */
1022          0,                     /* rightshift */
1023          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1024          16,                    /* bitsize */
1025          FALSE,                 /* pc_relative */
1026          0,                     /* bitpos */
1027          complain_overflow_signed, /* complain_on_overflow */
1028          ppc64_elf_unhandled_reloc, /* special_function */
1029          "R_PPC64_PLTGOT16",    /* name */
1030          FALSE,                 /* partial_inplace */
1031          0,                     /* src_mask */
1032          0xffff,                /* dst_mask */
1033          FALSE),                /* pcrel_offset */
1034
1035   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1036   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1037   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1038          0,                     /* rightshift */
1039          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1040          16,                    /* bitsize */
1041          FALSE,                 /* pc_relative */
1042          0,                     /* bitpos */
1043          complain_overflow_dont, /* complain_on_overflow */
1044          ppc64_elf_unhandled_reloc, /* special_function */
1045          "R_PPC64_PLTGOT16_LO", /* name */
1046          FALSE,                 /* partial_inplace */
1047          0,                     /* src_mask */
1048          0xffff,                /* dst_mask */
1049          FALSE),                /* pcrel_offset */
1050
1051   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1052   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1053   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1054          16,                    /* rightshift */
1055          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1056          16,                    /* bitsize */
1057          FALSE,                 /* pc_relative */
1058          0,                     /* bitpos */
1059          complain_overflow_signed, /* complain_on_overflow */
1060          ppc64_elf_unhandled_reloc, /* special_function */
1061          "R_PPC64_PLTGOT16_HI", /* name */
1062          FALSE,                 /* partial_inplace */
1063          0,                     /* src_mask */
1064          0xffff,                /* dst_mask */
1065          FALSE),                /* pcrel_offset */
1066
1067   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1068      1 if the contents of the low 16 bits, treated as a signed number,
1069      is negative.  */
1070   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1071   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1072          16,                    /* rightshift */
1073          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1074          16,                    /* bitsize */
1075          FALSE,                 /* pc_relative */
1076          0,                     /* bitpos */
1077          complain_overflow_signed, /* complain_on_overflow */
1078          ppc64_elf_unhandled_reloc, /* special_function */
1079          "R_PPC64_PLTGOT16_HA", /* name */
1080          FALSE,                 /* partial_inplace */
1081          0,                     /* src_mask */
1082          0xffff,                /* dst_mask */
1083          FALSE),                /* pcrel_offset */
1084
1085   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1086   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1087          0,                     /* rightshift */
1088          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1089          16,                    /* bitsize */
1090          FALSE,                 /* pc_relative */
1091          0,                     /* bitpos */
1092          complain_overflow_bitfield, /* complain_on_overflow */
1093          bfd_elf_generic_reloc, /* special_function */
1094          "R_PPC64_ADDR16_DS",   /* name */
1095          FALSE,                 /* partial_inplace */
1096          0,                     /* src_mask */
1097          0xfffc,                /* dst_mask */
1098          FALSE),                /* pcrel_offset */
1099
1100   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1101   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1102          0,                     /* rightshift */
1103          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1104          16,                    /* bitsize */
1105          FALSE,                 /* pc_relative */
1106          0,                     /* bitpos */
1107          complain_overflow_dont,/* complain_on_overflow */
1108          bfd_elf_generic_reloc, /* special_function */
1109          "R_PPC64_ADDR16_LO_DS",/* name */
1110          FALSE,                 /* partial_inplace */
1111          0,                     /* src_mask */
1112          0xfffc,                /* dst_mask */
1113          FALSE),                /* pcrel_offset */
1114
1115   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1116   HOWTO (R_PPC64_GOT16_DS,      /* type */
1117          0,                     /* rightshift */
1118          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1119          16,                    /* bitsize */
1120          FALSE,                 /* pc_relative */
1121          0,                     /* bitpos */
1122          complain_overflow_signed, /* complain_on_overflow */
1123          ppc64_elf_unhandled_reloc, /* special_function */
1124          "R_PPC64_GOT16_DS",    /* name */
1125          FALSE,                 /* partial_inplace */
1126          0,                     /* src_mask */
1127          0xfffc,                /* dst_mask */
1128          FALSE),                /* pcrel_offset */
1129
1130   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1131   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1132          0,                     /* rightshift */
1133          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1134          16,                    /* bitsize */
1135          FALSE,                 /* pc_relative */
1136          0,                     /* bitpos */
1137          complain_overflow_dont, /* complain_on_overflow */
1138          ppc64_elf_unhandled_reloc, /* special_function */
1139          "R_PPC64_GOT16_LO_DS", /* name */
1140          FALSE,                 /* partial_inplace */
1141          0,                     /* src_mask */
1142          0xfffc,                /* dst_mask */
1143          FALSE),                /* pcrel_offset */
1144
1145   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1146   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1147          0,                     /* rightshift */
1148          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1149          16,                    /* bitsize */
1150          FALSE,                 /* pc_relative */
1151          0,                     /* bitpos */
1152          complain_overflow_dont, /* complain_on_overflow */
1153          ppc64_elf_unhandled_reloc, /* special_function */
1154          "R_PPC64_PLT16_LO_DS", /* name */
1155          FALSE,                 /* partial_inplace */
1156          0,                     /* src_mask */
1157          0xfffc,                /* dst_mask */
1158          FALSE),                /* pcrel_offset */
1159
1160   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1161   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1162          0,                     /* rightshift */
1163          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1164          16,                    /* bitsize */
1165          FALSE,                 /* pc_relative */
1166          0,                     /* bitpos */
1167          complain_overflow_bitfield, /* complain_on_overflow */
1168          ppc64_elf_sectoff_reloc, /* special_function */
1169          "R_PPC64_SECTOFF_DS",  /* name */
1170          FALSE,                 /* partial_inplace */
1171          0,                     /* src_mask */
1172          0xfffc,                /* dst_mask */
1173          FALSE),                /* pcrel_offset */
1174
1175   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1176   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1177          0,                     /* rightshift */
1178          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1179          16,                    /* bitsize */
1180          FALSE,                 /* pc_relative */
1181          0,                     /* bitpos */
1182          complain_overflow_dont, /* complain_on_overflow */
1183          ppc64_elf_sectoff_reloc, /* special_function */
1184          "R_PPC64_SECTOFF_LO_DS",/* name */
1185          FALSE,                 /* partial_inplace */
1186          0,                     /* src_mask */
1187          0xfffc,                /* dst_mask */
1188          FALSE),                /* pcrel_offset */
1189
1190   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1191   HOWTO (R_PPC64_TOC16_DS,      /* type */
1192          0,                     /* rightshift */
1193          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1194          16,                    /* bitsize */
1195          FALSE,                 /* pc_relative */
1196          0,                     /* bitpos */
1197          complain_overflow_signed, /* complain_on_overflow */
1198          ppc64_elf_toc_reloc,   /* special_function */
1199          "R_PPC64_TOC16_DS",    /* name */
1200          FALSE,                 /* partial_inplace */
1201          0,                     /* src_mask */
1202          0xfffc,                /* dst_mask */
1203          FALSE),                /* pcrel_offset */
1204
1205   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1206   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1207          0,                     /* rightshift */
1208          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1209          16,                    /* bitsize */
1210          FALSE,                 /* pc_relative */
1211          0,                     /* bitpos */
1212          complain_overflow_dont, /* complain_on_overflow */
1213          ppc64_elf_toc_reloc,   /* special_function */
1214          "R_PPC64_TOC16_LO_DS", /* name */
1215          FALSE,                 /* partial_inplace */
1216          0,                     /* src_mask */
1217          0xfffc,                /* dst_mask */
1218          FALSE),                /* pcrel_offset */
1219
1220   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1221   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1222   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1223          0,                     /* rightshift */
1224          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1225          16,                    /* bitsize */
1226          FALSE,                 /* pc_relative */
1227          0,                     /* bitpos */
1228          complain_overflow_signed, /* complain_on_overflow */
1229          ppc64_elf_unhandled_reloc, /* special_function */
1230          "R_PPC64_PLTGOT16_DS", /* name */
1231          FALSE,                 /* partial_inplace */
1232          0,                     /* src_mask */
1233          0xfffc,                /* dst_mask */
1234          FALSE),                /* pcrel_offset */
1235
1236   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1237   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1238   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1239          0,                     /* rightshift */
1240          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1241          16,                    /* bitsize */
1242          FALSE,                 /* pc_relative */
1243          0,                     /* bitpos */
1244          complain_overflow_dont, /* complain_on_overflow */
1245          ppc64_elf_unhandled_reloc, /* special_function */
1246          "R_PPC64_PLTGOT16_LO_DS",/* name */
1247          FALSE,                 /* partial_inplace */
1248          0,                     /* src_mask */
1249          0xfffc,                /* dst_mask */
1250          FALSE),                /* pcrel_offset */
1251
1252   /* Marker relocs for TLS.  */
1253   HOWTO (R_PPC64_TLS,
1254          0,                     /* rightshift */
1255          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1256          32,                    /* bitsize */
1257          FALSE,                 /* pc_relative */
1258          0,                     /* bitpos */
1259          complain_overflow_dont, /* complain_on_overflow */
1260          bfd_elf_generic_reloc, /* special_function */
1261          "R_PPC64_TLS",         /* name */
1262          FALSE,                 /* partial_inplace */
1263          0,                     /* src_mask */
1264          0,                     /* dst_mask */
1265          FALSE),                /* pcrel_offset */
1266
1267   HOWTO (R_PPC64_TLSGD,
1268          0,                     /* rightshift */
1269          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1270          32,                    /* bitsize */
1271          FALSE,                 /* pc_relative */
1272          0,                     /* bitpos */
1273          complain_overflow_dont, /* complain_on_overflow */
1274          bfd_elf_generic_reloc, /* special_function */
1275          "R_PPC64_TLSGD",       /* name */
1276          FALSE,                 /* partial_inplace */
1277          0,                     /* src_mask */
1278          0,                     /* dst_mask */
1279          FALSE),                /* pcrel_offset */
1280
1281   HOWTO (R_PPC64_TLSLD,
1282          0,                     /* rightshift */
1283          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1284          32,                    /* bitsize */
1285          FALSE,                 /* pc_relative */
1286          0,                     /* bitpos */
1287          complain_overflow_dont, /* complain_on_overflow */
1288          bfd_elf_generic_reloc, /* special_function */
1289          "R_PPC64_TLSLD",       /* name */
1290          FALSE,                 /* partial_inplace */
1291          0,                     /* src_mask */
1292          0,                     /* dst_mask */
1293          FALSE),                /* pcrel_offset */
1294
1295   HOWTO (R_PPC64_TOCSAVE,
1296          0,                     /* rightshift */
1297          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1298          32,                    /* bitsize */
1299          FALSE,                 /* pc_relative */
1300          0,                     /* bitpos */
1301          complain_overflow_dont, /* complain_on_overflow */
1302          bfd_elf_generic_reloc, /* special_function */
1303          "R_PPC64_TOCSAVE",     /* name */
1304          FALSE,                 /* partial_inplace */
1305          0,                     /* src_mask */
1306          0,                     /* dst_mask */
1307          FALSE),                /* pcrel_offset */
1308
1309   /* Computes the load module index of the load module that contains the
1310      definition of its TLS sym.  */
1311   HOWTO (R_PPC64_DTPMOD64,
1312          0,                     /* rightshift */
1313          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1314          64,                    /* bitsize */
1315          FALSE,                 /* pc_relative */
1316          0,                     /* bitpos */
1317          complain_overflow_dont, /* complain_on_overflow */
1318          ppc64_elf_unhandled_reloc, /* special_function */
1319          "R_PPC64_DTPMOD64",    /* name */
1320          FALSE,                 /* partial_inplace */
1321          0,                     /* src_mask */
1322          ONES (64),             /* dst_mask */
1323          FALSE),                /* pcrel_offset */
1324
1325   /* Computes a dtv-relative displacement, the difference between the value
1326      of sym+add and the base address of the thread-local storage block that
1327      contains the definition of sym, minus 0x8000.  */
1328   HOWTO (R_PPC64_DTPREL64,
1329          0,                     /* rightshift */
1330          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1331          64,                    /* bitsize */
1332          FALSE,                 /* pc_relative */
1333          0,                     /* bitpos */
1334          complain_overflow_dont, /* complain_on_overflow */
1335          ppc64_elf_unhandled_reloc, /* special_function */
1336          "R_PPC64_DTPREL64",    /* name */
1337          FALSE,                 /* partial_inplace */
1338          0,                     /* src_mask */
1339          ONES (64),             /* dst_mask */
1340          FALSE),                /* pcrel_offset */
1341
1342   /* A 16 bit dtprel reloc.  */
1343   HOWTO (R_PPC64_DTPREL16,
1344          0,                     /* rightshift */
1345          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1346          16,                    /* bitsize */
1347          FALSE,                 /* pc_relative */
1348          0,                     /* bitpos */
1349          complain_overflow_signed, /* complain_on_overflow */
1350          ppc64_elf_unhandled_reloc, /* special_function */
1351          "R_PPC64_DTPREL16",    /* name */
1352          FALSE,                 /* partial_inplace */
1353          0,                     /* src_mask */
1354          0xffff,                /* dst_mask */
1355          FALSE),                /* pcrel_offset */
1356
1357   /* Like DTPREL16, but no overflow.  */
1358   HOWTO (R_PPC64_DTPREL16_LO,
1359          0,                     /* rightshift */
1360          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1361          16,                    /* bitsize */
1362          FALSE,                 /* pc_relative */
1363          0,                     /* bitpos */
1364          complain_overflow_dont, /* complain_on_overflow */
1365          ppc64_elf_unhandled_reloc, /* special_function */
1366          "R_PPC64_DTPREL16_LO", /* name */
1367          FALSE,                 /* partial_inplace */
1368          0,                     /* src_mask */
1369          0xffff,                /* dst_mask */
1370          FALSE),                /* pcrel_offset */
1371
1372   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1373   HOWTO (R_PPC64_DTPREL16_HI,
1374          16,                    /* rightshift */
1375          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1376          16,                    /* bitsize */
1377          FALSE,                 /* pc_relative */
1378          0,                     /* bitpos */
1379          complain_overflow_signed, /* complain_on_overflow */
1380          ppc64_elf_unhandled_reloc, /* special_function */
1381          "R_PPC64_DTPREL16_HI", /* name */
1382          FALSE,                 /* partial_inplace */
1383          0,                     /* src_mask */
1384          0xffff,                /* dst_mask */
1385          FALSE),                /* pcrel_offset */
1386
1387   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1388   HOWTO (R_PPC64_DTPREL16_HA,
1389          16,                    /* rightshift */
1390          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1391          16,                    /* bitsize */
1392          FALSE,                 /* pc_relative */
1393          0,                     /* bitpos */
1394          complain_overflow_signed, /* complain_on_overflow */
1395          ppc64_elf_unhandled_reloc, /* special_function */
1396          "R_PPC64_DTPREL16_HA", /* name */
1397          FALSE,                 /* partial_inplace */
1398          0,                     /* src_mask */
1399          0xffff,                /* dst_mask */
1400          FALSE),                /* pcrel_offset */
1401
1402   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1403   HOWTO (R_PPC64_DTPREL16_HIGHER,
1404          32,                    /* rightshift */
1405          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1406          16,                    /* bitsize */
1407          FALSE,                 /* pc_relative */
1408          0,                     /* bitpos */
1409          complain_overflow_dont, /* complain_on_overflow */
1410          ppc64_elf_unhandled_reloc, /* special_function */
1411          "R_PPC64_DTPREL16_HIGHER", /* name */
1412          FALSE,                 /* partial_inplace */
1413          0,                     /* src_mask */
1414          0xffff,                /* dst_mask */
1415          FALSE),                /* pcrel_offset */
1416
1417   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1418   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1419          32,                    /* rightshift */
1420          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1421          16,                    /* bitsize */
1422          FALSE,                 /* pc_relative */
1423          0,                     /* bitpos */
1424          complain_overflow_dont, /* complain_on_overflow */
1425          ppc64_elf_unhandled_reloc, /* special_function */
1426          "R_PPC64_DTPREL16_HIGHERA", /* name */
1427          FALSE,                 /* partial_inplace */
1428          0,                     /* src_mask */
1429          0xffff,                /* dst_mask */
1430          FALSE),                /* pcrel_offset */
1431
1432   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1433   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1434          48,                    /* rightshift */
1435          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1436          16,                    /* bitsize */
1437          FALSE,                 /* pc_relative */
1438          0,                     /* bitpos */
1439          complain_overflow_dont, /* complain_on_overflow */
1440          ppc64_elf_unhandled_reloc, /* special_function */
1441          "R_PPC64_DTPREL16_HIGHEST", /* name */
1442          FALSE,                 /* partial_inplace */
1443          0,                     /* src_mask */
1444          0xffff,                /* dst_mask */
1445          FALSE),                /* pcrel_offset */
1446
1447   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1448   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1449          48,                    /* rightshift */
1450          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1451          16,                    /* bitsize */
1452          FALSE,                 /* pc_relative */
1453          0,                     /* bitpos */
1454          complain_overflow_dont, /* complain_on_overflow */
1455          ppc64_elf_unhandled_reloc, /* special_function */
1456          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1457          FALSE,                 /* partial_inplace */
1458          0,                     /* src_mask */
1459          0xffff,                /* dst_mask */
1460          FALSE),                /* pcrel_offset */
1461
1462   /* Like DTPREL16, but for insns with a DS field.  */
1463   HOWTO (R_PPC64_DTPREL16_DS,
1464          0,                     /* rightshift */
1465          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1466          16,                    /* bitsize */
1467          FALSE,                 /* pc_relative */
1468          0,                     /* bitpos */
1469          complain_overflow_signed, /* complain_on_overflow */
1470          ppc64_elf_unhandled_reloc, /* special_function */
1471          "R_PPC64_DTPREL16_DS", /* name */
1472          FALSE,                 /* partial_inplace */
1473          0,                     /* src_mask */
1474          0xfffc,                /* dst_mask */
1475          FALSE),                /* pcrel_offset */
1476
1477   /* Like DTPREL16_DS, but no overflow.  */
1478   HOWTO (R_PPC64_DTPREL16_LO_DS,
1479          0,                     /* rightshift */
1480          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1481          16,                    /* bitsize */
1482          FALSE,                 /* pc_relative */
1483          0,                     /* bitpos */
1484          complain_overflow_dont, /* complain_on_overflow */
1485          ppc64_elf_unhandled_reloc, /* special_function */
1486          "R_PPC64_DTPREL16_LO_DS", /* name */
1487          FALSE,                 /* partial_inplace */
1488          0,                     /* src_mask */
1489          0xfffc,                /* dst_mask */
1490          FALSE),                /* pcrel_offset */
1491
1492   /* Computes a tp-relative displacement, the difference between the value of
1493      sym+add and the value of the thread pointer (r13).  */
1494   HOWTO (R_PPC64_TPREL64,
1495          0,                     /* rightshift */
1496          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1497          64,                    /* bitsize */
1498          FALSE,                 /* pc_relative */
1499          0,                     /* bitpos */
1500          complain_overflow_dont, /* complain_on_overflow */
1501          ppc64_elf_unhandled_reloc, /* special_function */
1502          "R_PPC64_TPREL64",     /* name */
1503          FALSE,                 /* partial_inplace */
1504          0,                     /* src_mask */
1505          ONES (64),             /* dst_mask */
1506          FALSE),                /* pcrel_offset */
1507
1508   /* A 16 bit tprel reloc.  */
1509   HOWTO (R_PPC64_TPREL16,
1510          0,                     /* rightshift */
1511          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1512          16,                    /* bitsize */
1513          FALSE,                 /* pc_relative */
1514          0,                     /* bitpos */
1515          complain_overflow_signed, /* complain_on_overflow */
1516          ppc64_elf_unhandled_reloc, /* special_function */
1517          "R_PPC64_TPREL16",     /* name */
1518          FALSE,                 /* partial_inplace */
1519          0,                     /* src_mask */
1520          0xffff,                /* dst_mask */
1521          FALSE),                /* pcrel_offset */
1522
1523   /* Like TPREL16, but no overflow.  */
1524   HOWTO (R_PPC64_TPREL16_LO,
1525          0,                     /* rightshift */
1526          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1527          16,                    /* bitsize */
1528          FALSE,                 /* pc_relative */
1529          0,                     /* bitpos */
1530          complain_overflow_dont, /* complain_on_overflow */
1531          ppc64_elf_unhandled_reloc, /* special_function */
1532          "R_PPC64_TPREL16_LO",  /* name */
1533          FALSE,                 /* partial_inplace */
1534          0,                     /* src_mask */
1535          0xffff,                /* dst_mask */
1536          FALSE),                /* pcrel_offset */
1537
1538   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1539   HOWTO (R_PPC64_TPREL16_HI,
1540          16,                    /* rightshift */
1541          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1542          16,                    /* bitsize */
1543          FALSE,                 /* pc_relative */
1544          0,                     /* bitpos */
1545          complain_overflow_signed, /* complain_on_overflow */
1546          ppc64_elf_unhandled_reloc, /* special_function */
1547          "R_PPC64_TPREL16_HI",  /* name */
1548          FALSE,                 /* partial_inplace */
1549          0,                     /* src_mask */
1550          0xffff,                /* dst_mask */
1551          FALSE),                /* pcrel_offset */
1552
1553   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1554   HOWTO (R_PPC64_TPREL16_HA,
1555          16,                    /* rightshift */
1556          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1557          16,                    /* bitsize */
1558          FALSE,                 /* pc_relative */
1559          0,                     /* bitpos */
1560          complain_overflow_signed, /* complain_on_overflow */
1561          ppc64_elf_unhandled_reloc, /* special_function */
1562          "R_PPC64_TPREL16_HA",  /* name */
1563          FALSE,                 /* partial_inplace */
1564          0,                     /* src_mask */
1565          0xffff,                /* dst_mask */
1566          FALSE),                /* pcrel_offset */
1567
1568   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1569   HOWTO (R_PPC64_TPREL16_HIGHER,
1570          32,                    /* rightshift */
1571          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1572          16,                    /* bitsize */
1573          FALSE,                 /* pc_relative */
1574          0,                     /* bitpos */
1575          complain_overflow_dont, /* complain_on_overflow */
1576          ppc64_elf_unhandled_reloc, /* special_function */
1577          "R_PPC64_TPREL16_HIGHER",      /* name */
1578          FALSE,                 /* partial_inplace */
1579          0,                     /* src_mask */
1580          0xffff,                /* dst_mask */
1581          FALSE),                /* pcrel_offset */
1582
1583   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1584   HOWTO (R_PPC64_TPREL16_HIGHERA,
1585          32,                    /* rightshift */
1586          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1587          16,                    /* bitsize */
1588          FALSE,                 /* pc_relative */
1589          0,                     /* bitpos */
1590          complain_overflow_dont, /* complain_on_overflow */
1591          ppc64_elf_unhandled_reloc, /* special_function */
1592          "R_PPC64_TPREL16_HIGHERA", /* name */
1593          FALSE,                 /* partial_inplace */
1594          0,                     /* src_mask */
1595          0xffff,                /* dst_mask */
1596          FALSE),                /* pcrel_offset */
1597
1598   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1599   HOWTO (R_PPC64_TPREL16_HIGHEST,
1600          48,                    /* rightshift */
1601          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1602          16,                    /* bitsize */
1603          FALSE,                 /* pc_relative */
1604          0,                     /* bitpos */
1605          complain_overflow_dont, /* complain_on_overflow */
1606          ppc64_elf_unhandled_reloc, /* special_function */
1607          "R_PPC64_TPREL16_HIGHEST", /* name */
1608          FALSE,                 /* partial_inplace */
1609          0,                     /* src_mask */
1610          0xffff,                /* dst_mask */
1611          FALSE),                /* pcrel_offset */
1612
1613   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1614   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1615          48,                    /* rightshift */
1616          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1617          16,                    /* bitsize */
1618          FALSE,                 /* pc_relative */
1619          0,                     /* bitpos */
1620          complain_overflow_dont, /* complain_on_overflow */
1621          ppc64_elf_unhandled_reloc, /* special_function */
1622          "R_PPC64_TPREL16_HIGHESTA", /* name */
1623          FALSE,                 /* partial_inplace */
1624          0,                     /* src_mask */
1625          0xffff,                /* dst_mask */
1626          FALSE),                /* pcrel_offset */
1627
1628   /* Like TPREL16, but for insns with a DS field.  */
1629   HOWTO (R_PPC64_TPREL16_DS,
1630          0,                     /* rightshift */
1631          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1632          16,                    /* bitsize */
1633          FALSE,                 /* pc_relative */
1634          0,                     /* bitpos */
1635          complain_overflow_signed, /* complain_on_overflow */
1636          ppc64_elf_unhandled_reloc, /* special_function */
1637          "R_PPC64_TPREL16_DS",  /* name */
1638          FALSE,                 /* partial_inplace */
1639          0,                     /* src_mask */
1640          0xfffc,                /* dst_mask */
1641          FALSE),                /* pcrel_offset */
1642
1643   /* Like TPREL16_DS, but no overflow.  */
1644   HOWTO (R_PPC64_TPREL16_LO_DS,
1645          0,                     /* rightshift */
1646          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1647          16,                    /* bitsize */
1648          FALSE,                 /* pc_relative */
1649          0,                     /* bitpos */
1650          complain_overflow_dont, /* complain_on_overflow */
1651          ppc64_elf_unhandled_reloc, /* special_function */
1652          "R_PPC64_TPREL16_LO_DS", /* name */
1653          FALSE,                 /* partial_inplace */
1654          0,                     /* src_mask */
1655          0xfffc,                /* dst_mask */
1656          FALSE),                /* pcrel_offset */
1657
1658   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1659      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1660      to the first entry relative to the TOC base (r2).  */
1661   HOWTO (R_PPC64_GOT_TLSGD16,
1662          0,                     /* rightshift */
1663          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1664          16,                    /* bitsize */
1665          FALSE,                 /* pc_relative */
1666          0,                     /* bitpos */
1667          complain_overflow_signed, /* complain_on_overflow */
1668          ppc64_elf_unhandled_reloc, /* special_function */
1669          "R_PPC64_GOT_TLSGD16", /* name */
1670          FALSE,                 /* partial_inplace */
1671          0,                     /* src_mask */
1672          0xffff,                /* dst_mask */
1673          FALSE),                /* pcrel_offset */
1674
1675   /* Like GOT_TLSGD16, but no overflow.  */
1676   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1677          0,                     /* rightshift */
1678          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1679          16,                    /* bitsize */
1680          FALSE,                 /* pc_relative */
1681          0,                     /* bitpos */
1682          complain_overflow_dont, /* complain_on_overflow */
1683          ppc64_elf_unhandled_reloc, /* special_function */
1684          "R_PPC64_GOT_TLSGD16_LO", /* name */
1685          FALSE,                 /* partial_inplace */
1686          0,                     /* src_mask */
1687          0xffff,                /* dst_mask */
1688          FALSE),                /* pcrel_offset */
1689
1690   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1691   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1692          16,                    /* rightshift */
1693          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1694          16,                    /* bitsize */
1695          FALSE,                 /* pc_relative */
1696          0,                     /* bitpos */
1697          complain_overflow_signed, /* complain_on_overflow */
1698          ppc64_elf_unhandled_reloc, /* special_function */
1699          "R_PPC64_GOT_TLSGD16_HI", /* name */
1700          FALSE,                 /* partial_inplace */
1701          0,                     /* src_mask */
1702          0xffff,                /* dst_mask */
1703          FALSE),                /* pcrel_offset */
1704
1705   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1706   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1707          16,                    /* rightshift */
1708          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1709          16,                    /* bitsize */
1710          FALSE,                 /* pc_relative */
1711          0,                     /* bitpos */
1712          complain_overflow_signed, /* complain_on_overflow */
1713          ppc64_elf_unhandled_reloc, /* special_function */
1714          "R_PPC64_GOT_TLSGD16_HA", /* name */
1715          FALSE,                 /* partial_inplace */
1716          0,                     /* src_mask */
1717          0xffff,                /* dst_mask */
1718          FALSE),                /* pcrel_offset */
1719
1720   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1721      with values (sym+add)@dtpmod and zero, and computes the offset to the
1722      first entry relative to the TOC base (r2).  */
1723   HOWTO (R_PPC64_GOT_TLSLD16,
1724          0,                     /* rightshift */
1725          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1726          16,                    /* bitsize */
1727          FALSE,                 /* pc_relative */
1728          0,                     /* bitpos */
1729          complain_overflow_signed, /* complain_on_overflow */
1730          ppc64_elf_unhandled_reloc, /* special_function */
1731          "R_PPC64_GOT_TLSLD16", /* name */
1732          FALSE,                 /* partial_inplace */
1733          0,                     /* src_mask */
1734          0xffff,                /* dst_mask */
1735          FALSE),                /* pcrel_offset */
1736
1737   /* Like GOT_TLSLD16, but no overflow.  */
1738   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1739          0,                     /* rightshift */
1740          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1741          16,                    /* bitsize */
1742          FALSE,                 /* pc_relative */
1743          0,                     /* bitpos */
1744          complain_overflow_dont, /* complain_on_overflow */
1745          ppc64_elf_unhandled_reloc, /* special_function */
1746          "R_PPC64_GOT_TLSLD16_LO", /* name */
1747          FALSE,                 /* partial_inplace */
1748          0,                     /* src_mask */
1749          0xffff,                /* dst_mask */
1750          FALSE),                /* pcrel_offset */
1751
1752   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1753   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1754          16,                    /* rightshift */
1755          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1756          16,                    /* bitsize */
1757          FALSE,                 /* pc_relative */
1758          0,                     /* bitpos */
1759          complain_overflow_signed, /* complain_on_overflow */
1760          ppc64_elf_unhandled_reloc, /* special_function */
1761          "R_PPC64_GOT_TLSLD16_HI", /* name */
1762          FALSE,                 /* partial_inplace */
1763          0,                     /* src_mask */
1764          0xffff,                /* dst_mask */
1765          FALSE),                /* pcrel_offset */
1766
1767   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1768   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1769          16,                    /* rightshift */
1770          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1771          16,                    /* bitsize */
1772          FALSE,                 /* pc_relative */
1773          0,                     /* bitpos */
1774          complain_overflow_signed, /* complain_on_overflow */
1775          ppc64_elf_unhandled_reloc, /* special_function */
1776          "R_PPC64_GOT_TLSLD16_HA", /* name */
1777          FALSE,                 /* partial_inplace */
1778          0,                     /* src_mask */
1779          0xffff,                /* dst_mask */
1780          FALSE),                /* pcrel_offset */
1781
1782   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1783      the offset to the entry relative to the TOC base (r2).  */
1784   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1785          0,                     /* rightshift */
1786          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1787          16,                    /* bitsize */
1788          FALSE,                 /* pc_relative */
1789          0,                     /* bitpos */
1790          complain_overflow_signed, /* complain_on_overflow */
1791          ppc64_elf_unhandled_reloc, /* special_function */
1792          "R_PPC64_GOT_DTPREL16_DS", /* name */
1793          FALSE,                 /* partial_inplace */
1794          0,                     /* src_mask */
1795          0xfffc,                /* dst_mask */
1796          FALSE),                /* pcrel_offset */
1797
1798   /* Like GOT_DTPREL16_DS, but no overflow.  */
1799   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1800          0,                     /* rightshift */
1801          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1802          16,                    /* bitsize */
1803          FALSE,                 /* pc_relative */
1804          0,                     /* bitpos */
1805          complain_overflow_dont, /* complain_on_overflow */
1806          ppc64_elf_unhandled_reloc, /* special_function */
1807          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1808          FALSE,                 /* partial_inplace */
1809          0,                     /* src_mask */
1810          0xfffc,                /* dst_mask */
1811          FALSE),                /* pcrel_offset */
1812
1813   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1814   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1815          16,                    /* rightshift */
1816          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1817          16,                    /* bitsize */
1818          FALSE,                 /* pc_relative */
1819          0,                     /* bitpos */
1820          complain_overflow_signed, /* complain_on_overflow */
1821          ppc64_elf_unhandled_reloc, /* special_function */
1822          "R_PPC64_GOT_DTPREL16_HI", /* name */
1823          FALSE,                 /* partial_inplace */
1824          0,                     /* src_mask */
1825          0xffff,                /* dst_mask */
1826          FALSE),                /* pcrel_offset */
1827
1828   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1829   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1830          16,                    /* rightshift */
1831          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1832          16,                    /* bitsize */
1833          FALSE,                 /* pc_relative */
1834          0,                     /* bitpos */
1835          complain_overflow_signed, /* complain_on_overflow */
1836          ppc64_elf_unhandled_reloc, /* special_function */
1837          "R_PPC64_GOT_DTPREL16_HA", /* name */
1838          FALSE,                 /* partial_inplace */
1839          0,                     /* src_mask */
1840          0xffff,                /* dst_mask */
1841          FALSE),                /* pcrel_offset */
1842
1843   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1844      offset to the entry relative to the TOC base (r2).  */
1845   HOWTO (R_PPC64_GOT_TPREL16_DS,
1846          0,                     /* rightshift */
1847          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1848          16,                    /* bitsize */
1849          FALSE,                 /* pc_relative */
1850          0,                     /* bitpos */
1851          complain_overflow_signed, /* complain_on_overflow */
1852          ppc64_elf_unhandled_reloc, /* special_function */
1853          "R_PPC64_GOT_TPREL16_DS", /* name */
1854          FALSE,                 /* partial_inplace */
1855          0,                     /* src_mask */
1856          0xfffc,                /* dst_mask */
1857          FALSE),                /* pcrel_offset */
1858
1859   /* Like GOT_TPREL16_DS, but no overflow.  */
1860   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1861          0,                     /* rightshift */
1862          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1863          16,                    /* bitsize */
1864          FALSE,                 /* pc_relative */
1865          0,                     /* bitpos */
1866          complain_overflow_dont, /* complain_on_overflow */
1867          ppc64_elf_unhandled_reloc, /* special_function */
1868          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1869          FALSE,                 /* partial_inplace */
1870          0,                     /* src_mask */
1871          0xfffc,                /* dst_mask */
1872          FALSE),                /* pcrel_offset */
1873
1874   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1875   HOWTO (R_PPC64_GOT_TPREL16_HI,
1876          16,                    /* rightshift */
1877          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1878          16,                    /* bitsize */
1879          FALSE,                 /* pc_relative */
1880          0,                     /* bitpos */
1881          complain_overflow_signed, /* complain_on_overflow */
1882          ppc64_elf_unhandled_reloc, /* special_function */
1883          "R_PPC64_GOT_TPREL16_HI", /* name */
1884          FALSE,                 /* partial_inplace */
1885          0,                     /* src_mask */
1886          0xffff,                /* dst_mask */
1887          FALSE),                /* pcrel_offset */
1888
1889   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1890   HOWTO (R_PPC64_GOT_TPREL16_HA,
1891          16,                    /* rightshift */
1892          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1893          16,                    /* bitsize */
1894          FALSE,                 /* pc_relative */
1895          0,                     /* bitpos */
1896          complain_overflow_signed, /* complain_on_overflow */
1897          ppc64_elf_unhandled_reloc, /* special_function */
1898          "R_PPC64_GOT_TPREL16_HA", /* name */
1899          FALSE,                 /* partial_inplace */
1900          0,                     /* src_mask */
1901          0xffff,                /* dst_mask */
1902          FALSE),                /* pcrel_offset */
1903
1904   HOWTO (R_PPC64_JMP_IREL,      /* type */
1905          0,                     /* rightshift */
1906          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1907          0,                     /* bitsize */
1908          FALSE,                 /* pc_relative */
1909          0,                     /* bitpos */
1910          complain_overflow_dont, /* complain_on_overflow */
1911          ppc64_elf_unhandled_reloc, /* special_function */
1912          "R_PPC64_JMP_IREL",    /* name */
1913          FALSE,                 /* partial_inplace */
1914          0,                     /* src_mask */
1915          0,                     /* dst_mask */
1916          FALSE),                /* pcrel_offset */
1917
1918   HOWTO (R_PPC64_IRELATIVE,     /* type */
1919          0,                     /* rightshift */
1920          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1921          64,                    /* bitsize */
1922          FALSE,                 /* pc_relative */
1923          0,                     /* bitpos */
1924          complain_overflow_dont, /* complain_on_overflow */
1925          bfd_elf_generic_reloc, /* special_function */
1926          "R_PPC64_IRELATIVE",   /* name */
1927          FALSE,                 /* partial_inplace */
1928          0,                     /* src_mask */
1929          ONES (64),             /* dst_mask */
1930          FALSE),                /* pcrel_offset */
1931
1932   /* A 16 bit relative relocation.  */
1933   HOWTO (R_PPC64_REL16,         /* type */
1934          0,                     /* rightshift */
1935          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1936          16,                    /* bitsize */
1937          TRUE,                  /* pc_relative */
1938          0,                     /* bitpos */
1939          complain_overflow_bitfield, /* complain_on_overflow */
1940          bfd_elf_generic_reloc, /* special_function */
1941          "R_PPC64_REL16",       /* name */
1942          FALSE,                 /* partial_inplace */
1943          0,                     /* src_mask */
1944          0xffff,                /* dst_mask */
1945          TRUE),                 /* pcrel_offset */
1946
1947   /* A 16 bit relative relocation without overflow.  */
1948   HOWTO (R_PPC64_REL16_LO,      /* type */
1949          0,                     /* rightshift */
1950          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1951          16,                    /* bitsize */
1952          TRUE,                  /* pc_relative */
1953          0,                     /* bitpos */
1954          complain_overflow_dont,/* complain_on_overflow */
1955          bfd_elf_generic_reloc, /* special_function */
1956          "R_PPC64_REL16_LO",    /* name */
1957          FALSE,                 /* partial_inplace */
1958          0,                     /* src_mask */
1959          0xffff,                /* dst_mask */
1960          TRUE),                 /* pcrel_offset */
1961
1962   /* The high order 16 bits of a relative address.  */
1963   HOWTO (R_PPC64_REL16_HI,      /* type */
1964          16,                    /* rightshift */
1965          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1966          16,                    /* bitsize */
1967          TRUE,                  /* pc_relative */
1968          0,                     /* bitpos */
1969          complain_overflow_signed, /* complain_on_overflow */
1970          bfd_elf_generic_reloc, /* special_function */
1971          "R_PPC64_REL16_HI",    /* name */
1972          FALSE,                 /* partial_inplace */
1973          0,                     /* src_mask */
1974          0xffff,                /* dst_mask */
1975          TRUE),                 /* pcrel_offset */
1976
1977   /* The high order 16 bits of a relative address, plus 1 if the contents of
1978      the low 16 bits, treated as a signed number, is negative.  */
1979   HOWTO (R_PPC64_REL16_HA,      /* type */
1980          16,                    /* rightshift */
1981          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1982          16,                    /* bitsize */
1983          TRUE,                  /* pc_relative */
1984          0,                     /* bitpos */
1985          complain_overflow_signed, /* complain_on_overflow */
1986          ppc64_elf_ha_reloc,    /* special_function */
1987          "R_PPC64_REL16_HA",    /* name */
1988          FALSE,                 /* partial_inplace */
1989          0,                     /* src_mask */
1990          0xffff,                /* dst_mask */
1991          TRUE),                 /* pcrel_offset */
1992
1993   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
1994   HOWTO (R_PPC64_ADDR16_HIGH,   /* type */
1995          16,                    /* rightshift */
1996          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1997          16,                    /* bitsize */
1998          FALSE,                 /* pc_relative */
1999          0,                     /* bitpos */
2000          complain_overflow_dont, /* complain_on_overflow */
2001          bfd_elf_generic_reloc, /* special_function */
2002          "R_PPC64_ADDR16_HIGH", /* name */
2003          FALSE,                 /* partial_inplace */
2004          0,                     /* src_mask */
2005          0xffff,                /* dst_mask */
2006          FALSE),                /* pcrel_offset */
2007
2008   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
2009   HOWTO (R_PPC64_ADDR16_HIGHA,  /* type */
2010          16,                    /* rightshift */
2011          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2012          16,                    /* bitsize */
2013          FALSE,                 /* pc_relative */
2014          0,                     /* bitpos */
2015          complain_overflow_dont, /* complain_on_overflow */
2016          ppc64_elf_ha_reloc,    /* special_function */
2017          "R_PPC64_ADDR16_HIGHA",        /* name */
2018          FALSE,                 /* partial_inplace */
2019          0,                     /* src_mask */
2020          0xffff,                /* dst_mask */
2021          FALSE),                /* pcrel_offset */
2022
2023   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
2024   HOWTO (R_PPC64_DTPREL16_HIGH,
2025          16,                    /* rightshift */
2026          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2027          16,                    /* bitsize */
2028          FALSE,                 /* pc_relative */
2029          0,                     /* bitpos */
2030          complain_overflow_dont, /* complain_on_overflow */
2031          ppc64_elf_unhandled_reloc, /* special_function */
2032          "R_PPC64_DTPREL16_HIGH", /* name */
2033          FALSE,                 /* partial_inplace */
2034          0,                     /* src_mask */
2035          0xffff,                /* dst_mask */
2036          FALSE),                /* pcrel_offset */
2037
2038   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
2039   HOWTO (R_PPC64_DTPREL16_HIGHA,
2040          16,                    /* rightshift */
2041          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2042          16,                    /* bitsize */
2043          FALSE,                 /* pc_relative */
2044          0,                     /* bitpos */
2045          complain_overflow_dont, /* complain_on_overflow */
2046          ppc64_elf_unhandled_reloc, /* special_function */
2047          "R_PPC64_DTPREL16_HIGHA", /* name */
2048          FALSE,                 /* partial_inplace */
2049          0,                     /* src_mask */
2050          0xffff,                /* dst_mask */
2051          FALSE),                /* pcrel_offset */
2052
2053   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
2054   HOWTO (R_PPC64_TPREL16_HIGH,
2055          16,                    /* rightshift */
2056          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2057          16,                    /* bitsize */
2058          FALSE,                 /* pc_relative */
2059          0,                     /* bitpos */
2060          complain_overflow_dont, /* complain_on_overflow */
2061          ppc64_elf_unhandled_reloc, /* special_function */
2062          "R_PPC64_TPREL16_HIGH",        /* name */
2063          FALSE,                 /* partial_inplace */
2064          0,                     /* src_mask */
2065          0xffff,                /* dst_mask */
2066          FALSE),                /* pcrel_offset */
2067
2068   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
2069   HOWTO (R_PPC64_TPREL16_HIGHA,
2070          16,                    /* rightshift */
2071          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2072          16,                    /* bitsize */
2073          FALSE,                 /* pc_relative */
2074          0,                     /* bitpos */
2075          complain_overflow_dont, /* complain_on_overflow */
2076          ppc64_elf_unhandled_reloc, /* special_function */
2077          "R_PPC64_TPREL16_HIGHA",       /* name */
2078          FALSE,                 /* partial_inplace */
2079          0,                     /* src_mask */
2080          0xffff,                /* dst_mask */
2081          FALSE),                /* pcrel_offset */
2082
2083   /* GNU extension to record C++ vtable hierarchy.  */
2084   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2085          0,                     /* rightshift */
2086          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2087          0,                     /* bitsize */
2088          FALSE,                 /* pc_relative */
2089          0,                     /* bitpos */
2090          complain_overflow_dont, /* complain_on_overflow */
2091          NULL,                  /* special_function */
2092          "R_PPC64_GNU_VTINHERIT", /* name */
2093          FALSE,                 /* partial_inplace */
2094          0,                     /* src_mask */
2095          0,                     /* dst_mask */
2096          FALSE),                /* pcrel_offset */
2097
2098   /* GNU extension to record C++ vtable member usage.  */
2099   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
2100          0,                     /* rightshift */
2101          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2102          0,                     /* bitsize */
2103          FALSE,                 /* pc_relative */
2104          0,                     /* bitpos */
2105          complain_overflow_dont, /* complain_on_overflow */
2106          NULL,                  /* special_function */
2107          "R_PPC64_GNU_VTENTRY", /* name */
2108          FALSE,                 /* partial_inplace */
2109          0,                     /* src_mask */
2110          0,                     /* dst_mask */
2111          FALSE),                /* pcrel_offset */
2112 };
2113
2114 \f
2115 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2116    be done.  */
2117
2118 static void
2119 ppc_howto_init (void)
2120 {
2121   unsigned int i, type;
2122
2123   for (i = 0;
2124        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2125        i++)
2126     {
2127       type = ppc64_elf_howto_raw[i].type;
2128       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2129                           / sizeof (ppc64_elf_howto_table[0])));
2130       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2131     }
2132 }
2133
2134 static reloc_howto_type *
2135 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2136                              bfd_reloc_code_real_type code)
2137 {
2138   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2139
2140   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2141     /* Initialize howto table if needed.  */
2142     ppc_howto_init ();
2143
2144   switch (code)
2145     {
2146     default:
2147       return NULL;
2148
2149     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2150       break;
2151     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2152       break;
2153     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2154       break;
2155     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2156       break;
2157     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2158       break;
2159     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2160       break;
2161     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
2162       break;
2163     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2164       break;
2165     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
2166       break;
2167     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2168       break;
2169     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2170       break;
2171     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2172       break;
2173     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2174       break;
2175     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2176       break;
2177     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2178       break;
2179     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2180       break;
2181     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2182       break;
2183     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2184       break;
2185     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2186       break;
2187     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2188       break;
2189     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2190       break;
2191     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2192       break;
2193     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2194       break;
2195     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2196       break;
2197     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2198       break;
2199     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2200       break;
2201     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2202       break;
2203     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2204       break;
2205     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2206       break;
2207     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2208       break;
2209     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2210       break;
2211     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2212       break;
2213     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2214       break;
2215     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2216       break;
2217     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2218       break;
2219     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2220       break;
2221     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2222       break;
2223     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2224       break;
2225     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2226       break;
2227     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2228       break;
2229     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2230       break;
2231     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2232       break;
2233     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2234       break;
2235     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2236       break;
2237     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2238       break;
2239     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2240       break;
2241     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2242       break;
2243     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2244       break;
2245     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2246       break;
2247     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2248       break;
2249     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2250       break;
2251     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2252       break;
2253     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2254       break;
2255     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2256       break;
2257     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2258       break;
2259     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2260       break;
2261     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2262       break;
2263     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2264       break;
2265     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2266       break;
2267     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2268       break;
2269     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2270       break;
2271     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2272       break;
2273     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2274       break;
2275     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2276       break;
2277     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2278       break;
2279     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2280       break;
2281     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2282       break;
2283     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2284       break;
2285     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
2286       break;
2287     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2288       break;
2289     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
2290       break;
2291     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2292       break;
2293     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2294       break;
2295     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2296       break;
2297     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2298       break;
2299     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
2300       break;
2301     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2302       break;
2303     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
2304       break;
2305     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2306       break;
2307     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2308       break;
2309     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2310       break;
2311     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2312       break;
2313     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2314       break;
2315     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2316       break;
2317     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2318       break;
2319     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2320       break;
2321     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2322       break;
2323     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2324       break;
2325     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2326       break;
2327     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2328       break;
2329     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2330       break;
2331     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2332       break;
2333     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2334       break;
2335     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2336       break;
2337     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2338       break;
2339     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2340       break;
2341     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2342       break;
2343     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2344       break;
2345     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2346       break;
2347     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2348       break;
2349     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2350       break;
2351     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2352       break;
2353     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2354       break;
2355     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2356       break;
2357     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2358       break;
2359     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2360       break;
2361     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2362       break;
2363     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2364       break;
2365     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2366       break;
2367     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2368       break;
2369     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2370       break;
2371     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2372       break;
2373     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2374       break;
2375     }
2376
2377   return ppc64_elf_howto_table[r];
2378 };
2379
2380 static reloc_howto_type *
2381 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2382                              const char *r_name)
2383 {
2384   unsigned int i;
2385
2386   for (i = 0;
2387        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2388        i++)
2389     if (ppc64_elf_howto_raw[i].name != NULL
2390         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2391       return &ppc64_elf_howto_raw[i];
2392
2393   return NULL;
2394 }
2395
2396 /* Set the howto pointer for a PowerPC ELF reloc.  */
2397
2398 static void
2399 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2400                          Elf_Internal_Rela *dst)
2401 {
2402   unsigned int type;
2403
2404   /* Initialize howto table if needed.  */
2405   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2406     ppc_howto_init ();
2407
2408   type = ELF64_R_TYPE (dst->r_info);
2409   if (type >= (sizeof (ppc64_elf_howto_table)
2410                / sizeof (ppc64_elf_howto_table[0])))
2411     {
2412       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2413                              abfd, (int) type);
2414       type = R_PPC64_NONE;
2415     }
2416   cache_ptr->howto = ppc64_elf_howto_table[type];
2417 }
2418
2419 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2420
2421 static bfd_reloc_status_type
2422 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2423                     void *data, asection *input_section,
2424                     bfd *output_bfd, char **error_message)
2425 {
2426   /* If this is a relocatable link (output_bfd test tells us), just
2427      call the generic function.  Any adjustment will be done at final
2428      link time.  */
2429   if (output_bfd != NULL)
2430     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2431                                   input_section, output_bfd, error_message);
2432
2433   /* Adjust the addend for sign extension of the low 16 bits.
2434      We won't actually be using the low 16 bits, so trashing them
2435      doesn't matter.  */
2436   reloc_entry->addend += 0x8000;
2437   return bfd_reloc_continue;
2438 }
2439
2440 static bfd_reloc_status_type
2441 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2442                         void *data, asection *input_section,
2443                         bfd *output_bfd, char **error_message)
2444 {
2445   if (output_bfd != NULL)
2446     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2447                                   input_section, output_bfd, error_message);
2448
2449   if (strcmp (symbol->section->name, ".opd") == 0
2450       && (symbol->section->owner->flags & DYNAMIC) == 0)
2451     {
2452       bfd_vma dest = opd_entry_value (symbol->section,
2453                                       symbol->value + reloc_entry->addend,
2454                                       NULL, NULL, FALSE);
2455       if (dest != (bfd_vma) -1)
2456         reloc_entry->addend = dest - (symbol->value
2457                                       + symbol->section->output_section->vma
2458                                       + symbol->section->output_offset);
2459     }
2460   return bfd_reloc_continue;
2461 }
2462
2463 static bfd_reloc_status_type
2464 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2465                          void *data, asection *input_section,
2466                          bfd *output_bfd, char **error_message)
2467 {
2468   long insn;
2469   enum elf_ppc64_reloc_type r_type;
2470   bfd_size_type octets;
2471   /* Assume 'at' branch hints.  */
2472   bfd_boolean is_isa_v2 = TRUE;
2473
2474   /* If this is a relocatable link (output_bfd test tells us), just
2475      call the generic function.  Any adjustment will be done at final
2476      link time.  */
2477   if (output_bfd != NULL)
2478     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2479                                   input_section, output_bfd, error_message);
2480
2481   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2482   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2483   insn &= ~(0x01 << 21);
2484   r_type = reloc_entry->howto->type;
2485   if (r_type == R_PPC64_ADDR14_BRTAKEN
2486       || r_type == R_PPC64_REL14_BRTAKEN)
2487     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2488
2489   if (is_isa_v2)
2490     {
2491       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2492          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2493          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2494       if ((insn & (0x14 << 21)) == (0x04 << 21))
2495         insn |= 0x02 << 21;
2496       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2497         insn |= 0x08 << 21;
2498       else
2499         goto out;
2500     }
2501   else
2502     {
2503       bfd_vma target = 0;
2504       bfd_vma from;
2505
2506       if (!bfd_is_com_section (symbol->section))
2507         target = symbol->value;
2508       target += symbol->section->output_section->vma;
2509       target += symbol->section->output_offset;
2510       target += reloc_entry->addend;
2511
2512       from = (reloc_entry->address
2513               + input_section->output_offset
2514               + input_section->output_section->vma);
2515
2516       /* Invert 'y' bit if not the default.  */
2517       if ((bfd_signed_vma) (target - from) < 0)
2518         insn ^= 0x01 << 21;
2519     }
2520   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2521  out:
2522   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2523                                  input_section, output_bfd, error_message);
2524 }
2525
2526 static bfd_reloc_status_type
2527 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2528                          void *data, asection *input_section,
2529                          bfd *output_bfd, char **error_message)
2530 {
2531   /* If this is a relocatable link (output_bfd test tells us), just
2532      call the generic function.  Any adjustment will be done at final
2533      link time.  */
2534   if (output_bfd != NULL)
2535     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2536                                   input_section, output_bfd, error_message);
2537
2538   /* Subtract the symbol section base address.  */
2539   reloc_entry->addend -= symbol->section->output_section->vma;
2540   return bfd_reloc_continue;
2541 }
2542
2543 static bfd_reloc_status_type
2544 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2545                             void *data, asection *input_section,
2546                             bfd *output_bfd, char **error_message)
2547 {
2548   /* If this is a relocatable link (output_bfd test tells us), just
2549      call the generic function.  Any adjustment will be done at final
2550      link time.  */
2551   if (output_bfd != NULL)
2552     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2553                                   input_section, output_bfd, error_message);
2554
2555   /* Subtract the symbol section base address.  */
2556   reloc_entry->addend -= symbol->section->output_section->vma;
2557
2558   /* Adjust the addend for sign extension of the low 16 bits.  */
2559   reloc_entry->addend += 0x8000;
2560   return bfd_reloc_continue;
2561 }
2562
2563 static bfd_reloc_status_type
2564 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2565                      void *data, asection *input_section,
2566                      bfd *output_bfd, char **error_message)
2567 {
2568   bfd_vma TOCstart;
2569
2570   /* If this is a relocatable link (output_bfd test tells us), just
2571      call the generic function.  Any adjustment will be done at final
2572      link time.  */
2573   if (output_bfd != NULL)
2574     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2575                                   input_section, output_bfd, error_message);
2576
2577   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2578   if (TOCstart == 0)
2579     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2580
2581   /* Subtract the TOC base address.  */
2582   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2583   return bfd_reloc_continue;
2584 }
2585
2586 static bfd_reloc_status_type
2587 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2588                         void *data, asection *input_section,
2589                         bfd *output_bfd, char **error_message)
2590 {
2591   bfd_vma TOCstart;
2592
2593   /* If this is a relocatable link (output_bfd test tells us), just
2594      call the generic function.  Any adjustment will be done at final
2595      link time.  */
2596   if (output_bfd != NULL)
2597     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2598                                   input_section, output_bfd, error_message);
2599
2600   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2601   if (TOCstart == 0)
2602     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2603
2604   /* Subtract the TOC base address.  */
2605   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2606
2607   /* Adjust the addend for sign extension of the low 16 bits.  */
2608   reloc_entry->addend += 0x8000;
2609   return bfd_reloc_continue;
2610 }
2611
2612 static bfd_reloc_status_type
2613 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2614                        void *data, asection *input_section,
2615                        bfd *output_bfd, char **error_message)
2616 {
2617   bfd_vma TOCstart;
2618   bfd_size_type octets;
2619
2620   /* If this is a relocatable link (output_bfd test tells us), just
2621      call the generic function.  Any adjustment will be done at final
2622      link time.  */
2623   if (output_bfd != NULL)
2624     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2625                                   input_section, output_bfd, error_message);
2626
2627   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2628   if (TOCstart == 0)
2629     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2630
2631   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2632   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2633   return bfd_reloc_ok;
2634 }
2635
2636 static bfd_reloc_status_type
2637 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2638                            void *data, asection *input_section,
2639                            bfd *output_bfd, char **error_message)
2640 {
2641   /* If this is a relocatable link (output_bfd test tells us), just
2642      call the generic function.  Any adjustment will be done at final
2643      link time.  */
2644   if (output_bfd != NULL)
2645     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2646                                   input_section, output_bfd, error_message);
2647
2648   if (error_message != NULL)
2649     {
2650       static char buf[60];
2651       sprintf (buf, "generic linker can't handle %s",
2652                reloc_entry->howto->name);
2653       *error_message = buf;
2654     }
2655   return bfd_reloc_dangerous;
2656 }
2657
2658 /* Track GOT entries needed for a given symbol.  We might need more
2659    than one got entry per symbol.  */
2660 struct got_entry
2661 {
2662   struct got_entry *next;
2663
2664   /* The symbol addend that we'll be placing in the GOT.  */
2665   bfd_vma addend;
2666
2667   /* Unlike other ELF targets, we use separate GOT entries for the same
2668      symbol referenced from different input files.  This is to support
2669      automatic multiple TOC/GOT sections, where the TOC base can vary
2670      from one input file to another.  After partitioning into TOC groups
2671      we merge entries within the group.
2672
2673      Point to the BFD owning this GOT entry.  */
2674   bfd *owner;
2675
2676   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2677      TLS_TPREL or TLS_DTPREL for tls entries.  */
2678   unsigned char tls_type;
2679
2680   /* Non-zero if got.ent points to real entry.  */
2681   unsigned char is_indirect;
2682
2683   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2684   union
2685     {
2686       bfd_signed_vma refcount;
2687       bfd_vma offset;
2688       struct got_entry *ent;
2689     } got;
2690 };
2691
2692 /* The same for PLT.  */
2693 struct plt_entry
2694 {
2695   struct plt_entry *next;
2696
2697   bfd_vma addend;
2698
2699   union
2700     {
2701       bfd_signed_vma refcount;
2702       bfd_vma offset;
2703     } plt;
2704 };
2705
2706 struct ppc64_elf_obj_tdata
2707 {
2708   struct elf_obj_tdata elf;
2709
2710   /* Shortcuts to dynamic linker sections.  */
2711   asection *got;
2712   asection *relgot;
2713
2714   /* Used during garbage collection.  We attach global symbols defined
2715      on removed .opd entries to this section so that the sym is removed.  */
2716   asection *deleted_section;
2717
2718   /* TLS local dynamic got entry handling.  Support for multiple GOT
2719      sections means we potentially need one of these for each input bfd.  */
2720   struct got_entry tlsld_got;
2721
2722   union {
2723     /* A copy of relocs before they are modified for --emit-relocs.  */
2724     Elf_Internal_Rela *relocs;
2725
2726     /* Section contents.  */
2727     bfd_byte *contents;
2728   } opd;
2729
2730   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2731      the reloc to be in the range -32768 to 32767.  */
2732   unsigned int has_small_toc_reloc : 1;
2733
2734   /* Set if toc/got ha relocs detected not using r2, or lo reloc
2735      instruction not one we handle.  */
2736   unsigned int unexpected_toc_insn : 1;
2737 };
2738
2739 #define ppc64_elf_tdata(bfd) \
2740   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2741
2742 #define ppc64_tlsld_got(bfd) \
2743   (&ppc64_elf_tdata (bfd)->tlsld_got)
2744
2745 #define is_ppc64_elf(bfd) \
2746   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2747    && elf_object_id (bfd) == PPC64_ELF_DATA)
2748
2749 /* Override the generic function because we store some extras.  */
2750
2751 static bfd_boolean
2752 ppc64_elf_mkobject (bfd *abfd)
2753 {
2754   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2755                                   PPC64_ELF_DATA);
2756 }
2757
2758 /* Fix bad default arch selected for a 64 bit input bfd when the
2759    default is 32 bit.  */
2760
2761 static bfd_boolean
2762 ppc64_elf_object_p (bfd *abfd)
2763 {
2764   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2765     {
2766       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2767
2768       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2769         {
2770           /* Relies on arch after 32 bit default being 64 bit default.  */
2771           abfd->arch_info = abfd->arch_info->next;
2772           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2773         }
2774     }
2775   return TRUE;
2776 }
2777
2778 /* Support for core dump NOTE sections.  */
2779
2780 static bfd_boolean
2781 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2782 {
2783   size_t offset, size;
2784
2785   if (note->descsz != 504)
2786     return FALSE;
2787
2788   /* pr_cursig */
2789   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2790
2791   /* pr_pid */
2792   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2793
2794   /* pr_reg */
2795   offset = 112;
2796   size = 384;
2797
2798   /* Make a ".reg/999" section.  */
2799   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2800                                           size, note->descpos + offset);
2801 }
2802
2803 static bfd_boolean
2804 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2805 {
2806   if (note->descsz != 136)
2807     return FALSE;
2808
2809   elf_tdata (abfd)->core->pid
2810     = bfd_get_32 (abfd, note->descdata + 24);
2811   elf_tdata (abfd)->core->program
2812     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2813   elf_tdata (abfd)->core->command
2814     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2815
2816   return TRUE;
2817 }
2818
2819 static char *
2820 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2821                            ...)
2822 {
2823   switch (note_type)
2824     {
2825     default:
2826       return NULL;
2827
2828     case NT_PRPSINFO:
2829       {
2830         char data[136];
2831         va_list ap;
2832
2833         va_start (ap, note_type);
2834         memset (data, 0, sizeof (data));
2835         strncpy (data + 40, va_arg (ap, const char *), 16);
2836         strncpy (data + 56, va_arg (ap, const char *), 80);
2837         va_end (ap);
2838         return elfcore_write_note (abfd, buf, bufsiz,
2839                                    "CORE", note_type, data, sizeof (data));
2840       }
2841
2842     case NT_PRSTATUS:
2843       {
2844         char data[504];
2845         va_list ap;
2846         long pid;
2847         int cursig;
2848         const void *greg;
2849
2850         va_start (ap, note_type);
2851         memset (data, 0, 112);
2852         pid = va_arg (ap, long);
2853         bfd_put_32 (abfd, pid, data + 32);
2854         cursig = va_arg (ap, int);
2855         bfd_put_16 (abfd, cursig, data + 12);
2856         greg = va_arg (ap, const void *);
2857         memcpy (data + 112, greg, 384);
2858         memset (data + 496, 0, 8);
2859         va_end (ap);
2860         return elfcore_write_note (abfd, buf, bufsiz,
2861                                    "CORE", note_type, data, sizeof (data));
2862       }
2863     }
2864 }
2865
2866 /* Add extra PPC sections.  */
2867
2868 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2869 {
2870   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
2871   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2872   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2873   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2874   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2875   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2876   { NULL,                     0,  0, 0,            0 }
2877 };
2878
2879 enum _ppc64_sec_type {
2880   sec_normal = 0,
2881   sec_opd = 1,
2882   sec_toc = 2
2883 };
2884
2885 struct _ppc64_elf_section_data
2886 {
2887   struct bfd_elf_section_data elf;
2888
2889   union
2890   {
2891     /* An array with one entry for each opd function descriptor.  */
2892     struct _opd_sec_data
2893     {
2894       /* Points to the function code section for local opd entries.  */
2895       asection **func_sec;
2896
2897       /* After editing .opd, adjust references to opd local syms.  */
2898       long *adjust;
2899     } opd;
2900
2901     /* An array for toc sections, indexed by offset/8.  */
2902     struct _toc_sec_data
2903     {
2904       /* Specifies the relocation symbol index used at a given toc offset.  */
2905       unsigned *symndx;
2906
2907       /* And the relocation addend.  */
2908       bfd_vma *add;
2909     } toc;
2910   } u;
2911
2912   enum _ppc64_sec_type sec_type:2;
2913
2914   /* Flag set when small branches are detected.  Used to
2915      select suitable defaults for the stub group size.  */
2916   unsigned int has_14bit_branch:1;
2917 };
2918
2919 #define ppc64_elf_section_data(sec) \
2920   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2921
2922 static bfd_boolean
2923 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2924 {
2925   if (!sec->used_by_bfd)
2926     {
2927       struct _ppc64_elf_section_data *sdata;
2928       bfd_size_type amt = sizeof (*sdata);
2929
2930       sdata = bfd_zalloc (abfd, amt);
2931       if (sdata == NULL)
2932         return FALSE;
2933       sec->used_by_bfd = sdata;
2934     }
2935
2936   return _bfd_elf_new_section_hook (abfd, sec);
2937 }
2938
2939 static struct _opd_sec_data *
2940 get_opd_info (asection * sec)
2941 {
2942   if (sec != NULL
2943       && ppc64_elf_section_data (sec) != NULL
2944       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2945     return &ppc64_elf_section_data (sec)->u.opd;
2946   return NULL;
2947 }
2948
2949 static inline int
2950 abiversion (bfd *abfd)
2951 {
2952   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
2953 }
2954
2955 static inline void
2956 set_abiversion (bfd *abfd, int ver)
2957 {
2958   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
2959   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
2960 }
2961 \f
2962 /* Parameters for the qsort hook.  */
2963 static bfd_boolean synthetic_relocatable;
2964
2965 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
2966
2967 static int
2968 compare_symbols (const void *ap, const void *bp)
2969 {
2970   const asymbol *a = * (const asymbol **) ap;
2971   const asymbol *b = * (const asymbol **) bp;
2972
2973   /* Section symbols first.  */
2974   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2975     return -1;
2976   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2977     return 1;
2978
2979   /* then .opd symbols.  */
2980   if (strcmp (a->section->name, ".opd") == 0
2981       && strcmp (b->section->name, ".opd") != 0)
2982     return -1;
2983   if (strcmp (a->section->name, ".opd") != 0
2984       && strcmp (b->section->name, ".opd") == 0)
2985     return 1;
2986
2987   /* then other code symbols.  */
2988   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2989       == (SEC_CODE | SEC_ALLOC)
2990       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2991          != (SEC_CODE | SEC_ALLOC))
2992     return -1;
2993
2994   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2995       != (SEC_CODE | SEC_ALLOC)
2996       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2997          == (SEC_CODE | SEC_ALLOC))
2998     return 1;
2999
3000   if (synthetic_relocatable)
3001     {
3002       if (a->section->id < b->section->id)
3003         return -1;
3004
3005       if (a->section->id > b->section->id)
3006         return 1;
3007     }
3008
3009   if (a->value + a->section->vma < b->value + b->section->vma)
3010     return -1;
3011
3012   if (a->value + a->section->vma > b->value + b->section->vma)
3013     return 1;
3014
3015   /* For syms with the same value, prefer strong dynamic global function
3016      syms over other syms.  */
3017   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3018     return -1;
3019
3020   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3021     return 1;
3022
3023   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3024     return -1;
3025
3026   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3027     return 1;
3028
3029   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3030     return -1;
3031
3032   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3033     return 1;
3034
3035   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3036     return -1;
3037
3038   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3039     return 1;
3040
3041   return 0;
3042 }
3043
3044 /* Search SYMS for a symbol of the given VALUE.  */
3045
3046 static asymbol *
3047 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
3048 {
3049   long mid;
3050
3051   if (id == -1)
3052     {
3053       while (lo < hi)
3054         {
3055           mid = (lo + hi) >> 1;
3056           if (syms[mid]->value + syms[mid]->section->vma < value)
3057             lo = mid + 1;
3058           else if (syms[mid]->value + syms[mid]->section->vma > value)
3059             hi = mid;
3060           else
3061             return syms[mid];
3062         }
3063     }
3064   else
3065     {
3066       while (lo < hi)
3067         {
3068           mid = (lo + hi) >> 1;
3069           if (syms[mid]->section->id < id)
3070             lo = mid + 1;
3071           else if (syms[mid]->section->id > id)
3072             hi = mid;
3073           else if (syms[mid]->value < value)
3074             lo = mid + 1;
3075           else if (syms[mid]->value > value)
3076             hi = mid;
3077           else
3078             return syms[mid];
3079         }
3080     }
3081   return NULL;
3082 }
3083
3084 static bfd_boolean
3085 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3086 {
3087   bfd_vma vma = *(bfd_vma *) ptr;
3088   return ((section->flags & SEC_ALLOC) != 0
3089           && section->vma <= vma
3090           && vma < section->vma + section->size);
3091 }
3092
3093 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3094    entry syms.  Also generate @plt symbols for the glink branch table.  */
3095
3096 static long
3097 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3098                                 long static_count, asymbol **static_syms,
3099                                 long dyn_count, asymbol **dyn_syms,
3100                                 asymbol **ret)
3101 {
3102   asymbol *s;
3103   long i;
3104   long count;
3105   char *names;
3106   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3107   asection *opd = NULL;
3108   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3109   asymbol **syms;
3110   int abi = abiversion (abfd);
3111
3112   *ret = NULL;
3113
3114   if (abi < 2)
3115     {
3116       opd = bfd_get_section_by_name (abfd, ".opd");
3117       if (opd == NULL && abi == 1)
3118         return 0;
3119     }
3120
3121   symcount = static_count;
3122   if (!relocatable)
3123     symcount += dyn_count;
3124   if (symcount == 0)
3125     return 0;
3126
3127   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3128   if (syms == NULL)
3129     return -1;
3130
3131   if (!relocatable && static_count != 0 && dyn_count != 0)
3132     {
3133       /* Use both symbol tables.  */
3134       memcpy (syms, static_syms, static_count * sizeof (*syms));
3135       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3136     }
3137   else if (!relocatable && static_count == 0)
3138     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3139   else
3140     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3141
3142   synthetic_relocatable = relocatable;
3143   qsort (syms, symcount, sizeof (*syms), compare_symbols);
3144
3145   if (!relocatable && symcount > 1)
3146     {
3147       long j;
3148       /* Trim duplicate syms, since we may have merged the normal and
3149          dynamic symbols.  Actually, we only care about syms that have
3150          different values, so trim any with the same value.  */
3151       for (i = 1, j = 1; i < symcount; ++i)
3152         if (syms[i - 1]->value + syms[i - 1]->section->vma
3153             != syms[i]->value + syms[i]->section->vma)
3154           syms[j++] = syms[i];
3155       symcount = j;
3156     }
3157
3158   i = 0;
3159   if (strcmp (syms[i]->section->name, ".opd") == 0)
3160     ++i;
3161   codesecsym = i;
3162
3163   for (; i < symcount; ++i)
3164     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3165          != (SEC_CODE | SEC_ALLOC))
3166         || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3167       break;
3168   codesecsymend = i;
3169
3170   for (; i < symcount; ++i)
3171     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3172       break;
3173   secsymend = i;
3174
3175   for (; i < symcount; ++i)
3176     if (strcmp (syms[i]->section->name, ".opd") != 0)
3177       break;
3178   opdsymend = i;
3179
3180   for (; i < symcount; ++i)
3181     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3182         != (SEC_CODE | SEC_ALLOC))
3183       break;
3184   symcount = i;
3185
3186   count = 0;
3187
3188   if (relocatable)
3189     {
3190       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3191       arelent *r;
3192       size_t size;
3193       long relcount;
3194
3195       if (opdsymend == secsymend)
3196         goto done;
3197
3198       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3199       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3200       if (relcount == 0)
3201         goto done;
3202
3203       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3204         {
3205           count = -1;
3206           goto done;
3207         }
3208
3209       size = 0;
3210       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3211         {
3212           asymbol *sym;
3213
3214           while (r < opd->relocation + relcount
3215                  && r->address < syms[i]->value + opd->vma)
3216             ++r;
3217
3218           if (r == opd->relocation + relcount)
3219             break;
3220
3221           if (r->address != syms[i]->value + opd->vma)
3222             continue;
3223
3224           if (r->howto->type != R_PPC64_ADDR64)
3225             continue;
3226
3227           sym = *r->sym_ptr_ptr;
3228           if (!sym_exists_at (syms, opdsymend, symcount,
3229                               sym->section->id, sym->value + r->addend))
3230             {
3231               ++count;
3232               size += sizeof (asymbol);
3233               size += strlen (syms[i]->name) + 2;
3234             }
3235         }
3236
3237       s = *ret = bfd_malloc (size);
3238       if (s == NULL)
3239         {
3240           count = -1;
3241           goto done;
3242         }
3243
3244       names = (char *) (s + count);
3245
3246       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3247         {
3248           asymbol *sym;
3249
3250           while (r < opd->relocation + relcount
3251                  && r->address < syms[i]->value + opd->vma)
3252             ++r;
3253
3254           if (r == opd->relocation + relcount)
3255             break;
3256
3257           if (r->address != syms[i]->value + opd->vma)
3258             continue;
3259
3260           if (r->howto->type != R_PPC64_ADDR64)
3261             continue;
3262
3263           sym = *r->sym_ptr_ptr;
3264           if (!sym_exists_at (syms, opdsymend, symcount,
3265                               sym->section->id, sym->value + r->addend))
3266             {
3267               size_t len;
3268
3269               *s = *syms[i];
3270               s->flags |= BSF_SYNTHETIC;
3271               s->section = sym->section;
3272               s->value = sym->value + r->addend;
3273               s->name = names;
3274               *names++ = '.';
3275               len = strlen (syms[i]->name);
3276               memcpy (names, syms[i]->name, len + 1);
3277               names += len + 1;
3278               /* Have udata.p point back to the original symbol this
3279                  synthetic symbol was derived from.  */
3280               s->udata.p = syms[i];
3281               s++;
3282             }
3283         }
3284     }
3285   else
3286     {
3287       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3288       bfd_byte *contents = NULL;
3289       size_t size;
3290       long plt_count = 0;
3291       bfd_vma glink_vma = 0, resolv_vma = 0;
3292       asection *dynamic, *glink = NULL, *relplt = NULL;
3293       arelent *p;
3294
3295       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3296         {
3297         free_contents_and_exit:
3298           if (contents)
3299             free (contents);
3300           count = -1;
3301           goto done;
3302         }
3303
3304       size = 0;
3305       for (i = secsymend; i < opdsymend; ++i)
3306         {
3307           bfd_vma ent;
3308
3309           /* Ignore bogus symbols.  */
3310           if (syms[i]->value > opd->size - 8)
3311             continue;
3312
3313           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3314           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3315             {
3316               ++count;
3317               size += sizeof (asymbol);
3318               size += strlen (syms[i]->name) + 2;
3319             }
3320         }
3321
3322       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3323       if (dyn_count != 0
3324           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3325         {
3326           bfd_byte *dynbuf, *extdyn, *extdynend;
3327           size_t extdynsize;
3328           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3329
3330           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3331             goto free_contents_and_exit;
3332
3333           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3334           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3335
3336           extdyn = dynbuf;
3337           extdynend = extdyn + dynamic->size;
3338           for (; extdyn < extdynend; extdyn += extdynsize)
3339             {
3340               Elf_Internal_Dyn dyn;
3341               (*swap_dyn_in) (abfd, extdyn, &dyn);
3342
3343               if (dyn.d_tag == DT_NULL)
3344                 break;
3345
3346               if (dyn.d_tag == DT_PPC64_GLINK)
3347                 {
3348                   /* The first glink stub starts at offset 32; see comment in
3349                      ppc64_elf_finish_dynamic_sections. */
3350                   glink_vma = dyn.d_un.d_val + 32;
3351                   /* The .glink section usually does not survive the final
3352                      link; search for the section (usually .text) where the
3353                      glink stubs now reside.  */
3354                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3355                                                 &glink_vma);
3356                   break;
3357                 }
3358             }
3359
3360           free (dynbuf);
3361         }
3362
3363       if (glink != NULL)
3364         {
3365           /* Determine __glink trampoline by reading the relative branch
3366              from the first glink stub.  */
3367           bfd_byte buf[4];
3368           if (bfd_get_section_contents (abfd, glink, buf,
3369                                         glink_vma + 4 - glink->vma, 4))
3370             {
3371               unsigned int insn = bfd_get_32 (abfd, buf);
3372               insn ^= B_DOT;
3373               if ((insn & ~0x3fffffc) == 0)
3374                 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3375             }
3376
3377           if (resolv_vma)
3378             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3379
3380           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3381           if (relplt != NULL)
3382             {
3383               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3384               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3385                 goto free_contents_and_exit;
3386
3387               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3388               size += plt_count * sizeof (asymbol);
3389
3390               p = relplt->relocation;
3391               for (i = 0; i < plt_count; i++, p++)
3392                 {
3393                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3394                   if (p->addend != 0)
3395                     size += sizeof ("+0x") - 1 + 16;
3396                 }
3397             }
3398         }
3399
3400       s = *ret = bfd_malloc (size);
3401       if (s == NULL)
3402         goto free_contents_and_exit;
3403
3404       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3405
3406       for (i = secsymend; i < opdsymend; ++i)
3407         {
3408           bfd_vma ent;
3409
3410           if (syms[i]->value > opd->size - 8)
3411             continue;
3412
3413           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3414           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3415             {
3416               long lo, hi;
3417               size_t len;
3418               asection *sec = abfd->sections;
3419
3420               *s = *syms[i];
3421               lo = codesecsym;
3422               hi = codesecsymend;
3423               while (lo < hi)
3424                 {
3425                   long mid = (lo + hi) >> 1;
3426                   if (syms[mid]->section->vma < ent)
3427                     lo = mid + 1;
3428                   else if (syms[mid]->section->vma > ent)
3429                     hi = mid;
3430                   else
3431                     {
3432                       sec = syms[mid]->section;
3433                       break;
3434                     }
3435                 }
3436
3437               if (lo >= hi && lo > codesecsym)
3438                 sec = syms[lo - 1]->section;
3439
3440               for (; sec != NULL; sec = sec->next)
3441                 {
3442                   if (sec->vma > ent)
3443                     break;
3444                   /* SEC_LOAD may not be set if SEC is from a separate debug
3445                      info file.  */
3446                   if ((sec->flags & SEC_ALLOC) == 0)
3447                     break;
3448                   if ((sec->flags & SEC_CODE) != 0)
3449                     s->section = sec;
3450                 }
3451               s->flags |= BSF_SYNTHETIC;
3452               s->value = ent - s->section->vma;
3453               s->name = names;
3454               *names++ = '.';
3455               len = strlen (syms[i]->name);
3456               memcpy (names, syms[i]->name, len + 1);
3457               names += len + 1;
3458               /* Have udata.p point back to the original symbol this
3459                  synthetic symbol was derived from.  */
3460               s->udata.p = syms[i];
3461               s++;
3462             }
3463         }
3464       free (contents);
3465
3466       if (glink != NULL && relplt != NULL)
3467         {
3468           if (resolv_vma)
3469             {
3470               /* Add a symbol for the main glink trampoline.  */
3471               memset (s, 0, sizeof *s);
3472               s->the_bfd = abfd;
3473               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3474               s->section = glink;
3475               s->value = resolv_vma - glink->vma;
3476               s->name = names;
3477               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3478               names += sizeof ("__glink_PLTresolve");
3479               s++;
3480               count++;
3481             }
3482
3483           /* FIXME: It would be very much nicer to put sym@plt on the
3484              stub rather than on the glink branch table entry.  The
3485              objdump disassembler would then use a sensible symbol
3486              name on plt calls.  The difficulty in doing so is
3487              a) finding the stubs, and,
3488              b) matching stubs against plt entries, and,
3489              c) there can be multiple stubs for a given plt entry.
3490
3491              Solving (a) could be done by code scanning, but older
3492              ppc64 binaries used different stubs to current code.
3493              (b) is the tricky one since you need to known the toc
3494              pointer for at least one function that uses a pic stub to
3495              be able to calculate the plt address referenced.
3496              (c) means gdb would need to set multiple breakpoints (or
3497              find the glink branch itself) when setting breakpoints
3498              for pending shared library loads.  */
3499           p = relplt->relocation;
3500           for (i = 0; i < plt_count; i++, p++)
3501             {
3502               size_t len;
3503
3504               *s = **p->sym_ptr_ptr;
3505               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3506                  we are defining a symbol, ensure one of them is set.  */
3507               if ((s->flags & BSF_LOCAL) == 0)
3508                 s->flags |= BSF_GLOBAL;
3509               s->flags |= BSF_SYNTHETIC;
3510               s->section = glink;
3511               s->value = glink_vma - glink->vma;
3512               s->name = names;
3513               s->udata.p = NULL;
3514               len = strlen ((*p->sym_ptr_ptr)->name);
3515               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3516               names += len;
3517               if (p->addend != 0)
3518                 {
3519                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3520                   names += sizeof ("+0x") - 1;
3521                   bfd_sprintf_vma (abfd, names, p->addend);
3522                   names += strlen (names);
3523                 }
3524               memcpy (names, "@plt", sizeof ("@plt"));
3525               names += sizeof ("@plt");
3526               s++;
3527               glink_vma += 8;
3528               if (i >= 0x8000)
3529                 glink_vma += 4;
3530             }
3531           count += plt_count;
3532         }
3533     }
3534
3535  done:
3536   free (syms);
3537   return count;
3538 }
3539 \f
3540 /* The following functions are specific to the ELF linker, while
3541    functions above are used generally.  Those named ppc64_elf_* are
3542    called by the main ELF linker code.  They appear in this file more
3543    or less in the order in which they are called.  eg.
3544    ppc64_elf_check_relocs is called early in the link process,
3545    ppc64_elf_finish_dynamic_sections is one of the last functions
3546    called.
3547
3548    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3549    functions have both a function code symbol and a function descriptor
3550    symbol.  A call to foo in a relocatable object file looks like:
3551
3552    .            .text
3553    .    x:
3554    .            bl      .foo
3555    .            nop
3556
3557    The function definition in another object file might be:
3558
3559    .            .section .opd
3560    .    foo:    .quad   .foo
3561    .            .quad   .TOC.@tocbase
3562    .            .quad   0
3563    .
3564    .            .text
3565    .    .foo:   blr
3566
3567    When the linker resolves the call during a static link, the branch
3568    unsurprisingly just goes to .foo and the .opd information is unused.
3569    If the function definition is in a shared library, things are a little
3570    different:  The call goes via a plt call stub, the opd information gets
3571    copied to the plt, and the linker patches the nop.
3572
3573    .    x:
3574    .            bl      .foo_stub
3575    .            ld      2,40(1)
3576    .
3577    .
3578    .    .foo_stub:
3579    .            std     2,40(1)                 # in practice, the call stub
3580    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
3581    .            addi    11,11,Lfoo@toc@l        # this is the general idea
3582    .            ld      12,0(11)
3583    .            ld      2,8(11)
3584    .            mtctr   12
3585    .            ld      11,16(11)
3586    .            bctr
3587    .
3588    .            .section .plt
3589    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3590
3591    The "reloc ()" notation is supposed to indicate that the linker emits
3592    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3593    copying.
3594
3595    What are the difficulties here?  Well, firstly, the relocations
3596    examined by the linker in check_relocs are against the function code
3597    sym .foo, while the dynamic relocation in the plt is emitted against
3598    the function descriptor symbol, foo.  Somewhere along the line, we need
3599    to carefully copy dynamic link information from one symbol to the other.
3600    Secondly, the generic part of the elf linker will make .foo a dynamic
3601    symbol as is normal for most other backends.  We need foo dynamic
3602    instead, at least for an application final link.  However, when
3603    creating a shared library containing foo, we need to have both symbols
3604    dynamic so that references to .foo are satisfied during the early
3605    stages of linking.  Otherwise the linker might decide to pull in a
3606    definition from some other object, eg. a static library.
3607
3608    Update: As of August 2004, we support a new convention.  Function
3609    calls may use the function descriptor symbol, ie. "bl foo".  This
3610    behaves exactly as "bl .foo".  */
3611
3612 /* Of those relocs that might be copied as dynamic relocs, this function
3613    selects those that must be copied when linking a shared library,
3614    even when the symbol is local.  */
3615
3616 static int
3617 must_be_dyn_reloc (struct bfd_link_info *info,
3618                    enum elf_ppc64_reloc_type r_type)
3619 {
3620   switch (r_type)
3621     {
3622     default:
3623       return 1;
3624
3625     case R_PPC64_REL32:
3626     case R_PPC64_REL64:
3627     case R_PPC64_REL30:
3628       return 0;
3629
3630     case R_PPC64_TPREL16:
3631     case R_PPC64_TPREL16_LO:
3632     case R_PPC64_TPREL16_HI:
3633     case R_PPC64_TPREL16_HA:
3634     case R_PPC64_TPREL16_DS:
3635     case R_PPC64_TPREL16_LO_DS:
3636     case R_PPC64_TPREL16_HIGH:
3637     case R_PPC64_TPREL16_HIGHA:
3638     case R_PPC64_TPREL16_HIGHER:
3639     case R_PPC64_TPREL16_HIGHERA:
3640     case R_PPC64_TPREL16_HIGHEST:
3641     case R_PPC64_TPREL16_HIGHESTA:
3642     case R_PPC64_TPREL64:
3643       return !info->executable;
3644     }
3645 }
3646
3647 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3648    copying dynamic variables from a shared lib into an app's dynbss
3649    section, and instead use a dynamic relocation to point into the
3650    shared lib.  With code that gcc generates, it's vital that this be
3651    enabled;  In the PowerPC64 ABI, the address of a function is actually
3652    the address of a function descriptor, which resides in the .opd
3653    section.  gcc uses the descriptor directly rather than going via the
3654    GOT as some other ABI's do, which means that initialized function
3655    pointers must reference the descriptor.  Thus, a function pointer
3656    initialized to the address of a function in a shared library will
3657    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3658    redefines the function descriptor symbol to point to the copy.  This
3659    presents a problem as a plt entry for that function is also
3660    initialized from the function descriptor symbol and the copy reloc
3661    may not be initialized first.  */
3662 #define ELIMINATE_COPY_RELOCS 1
3663
3664 /* Section name for stubs is the associated section name plus this
3665    string.  */
3666 #define STUB_SUFFIX ".stub"
3667
3668 /* Linker stubs.
3669    ppc_stub_long_branch:
3670    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3671    destination, but a 24 bit branch in a stub section will reach.
3672    .    b       dest
3673
3674    ppc_stub_plt_branch:
3675    Similar to the above, but a 24 bit branch in the stub section won't
3676    reach its destination.
3677    .    addis   %r11,%r2,xxx@toc@ha
3678    .    ld      %r12,xxx@toc@l(%r11)
3679    .    mtctr   %r12
3680    .    bctr
3681
3682    ppc_stub_plt_call:
3683    Used to call a function in a shared library.  If it so happens that
3684    the plt entry referenced crosses a 64k boundary, then an extra
3685    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3686    .    std     %r2,40(%r1)
3687    .    addis   %r11,%r2,xxx@toc@ha
3688    .    ld      %r12,xxx+0@toc@l(%r11)
3689    .    mtctr   %r12
3690    .    ld      %r2,xxx+8@toc@l(%r11)
3691    .    ld      %r11,xxx+16@toc@l(%r11)
3692    .    bctr
3693
3694    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3695    code to adjust the value and save r2 to support multiple toc sections.
3696    A ppc_stub_long_branch with an r2 offset looks like:
3697    .    std     %r2,40(%r1)
3698    .    addis   %r2,%r2,off@ha
3699    .    addi    %r2,%r2,off@l
3700    .    b       dest
3701
3702    A ppc_stub_plt_branch with an r2 offset looks like:
3703    .    std     %r2,40(%r1)
3704    .    addis   %r11,%r2,xxx@toc@ha
3705    .    ld      %r12,xxx@toc@l(%r11)
3706    .    addis   %r2,%r2,off@ha
3707    .    addi    %r2,%r2,off@l
3708    .    mtctr   %r12
3709    .    bctr
3710
3711    In cases where the "addis" instruction would add zero, the "addis" is
3712    omitted and following instructions modified slightly in some cases.
3713 */
3714
3715 enum ppc_stub_type {
3716   ppc_stub_none,
3717   ppc_stub_long_branch,
3718   ppc_stub_long_branch_r2off,
3719   ppc_stub_plt_branch,
3720   ppc_stub_plt_branch_r2off,
3721   ppc_stub_plt_call,
3722   ppc_stub_plt_call_r2save
3723 };
3724
3725 struct ppc_stub_hash_entry {
3726
3727   /* Base hash table entry structure.  */
3728   struct bfd_hash_entry root;
3729
3730   enum ppc_stub_type stub_type;
3731
3732   /* The stub section.  */
3733   asection *stub_sec;
3734
3735   /* Offset within stub_sec of the beginning of this stub.  */
3736   bfd_vma stub_offset;
3737
3738   /* Given the symbol's value and its section we can determine its final
3739      value when building the stubs (so the stub knows where to jump.  */
3740   bfd_vma target_value;
3741   asection *target_section;
3742
3743   /* The symbol table entry, if any, that this was derived from.  */
3744   struct ppc_link_hash_entry *h;
3745   struct plt_entry *plt_ent;
3746
3747   /* Where this stub is being called from, or, in the case of combined
3748      stub sections, the first input section in the group.  */
3749   asection *id_sec;
3750
3751   /* Symbol st_other.  */
3752   unsigned char other;
3753 };
3754
3755 struct ppc_branch_hash_entry {
3756
3757   /* Base hash table entry structure.  */
3758   struct bfd_hash_entry root;
3759
3760   /* Offset within branch lookup table.  */
3761   unsigned int offset;
3762
3763   /* Generation marker.  */
3764   unsigned int iter;
3765 };
3766
3767 /* Used to track dynamic relocations for local symbols.  */
3768 struct ppc_dyn_relocs
3769 {
3770   struct ppc_dyn_relocs *next;
3771
3772   /* The input section of the reloc.  */
3773   asection *sec;
3774
3775   /* Total number of relocs copied for the input section.  */
3776   unsigned int count : 31;
3777
3778   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
3779   unsigned int ifunc : 1;
3780 };
3781
3782 struct ppc_link_hash_entry
3783 {
3784   struct elf_link_hash_entry elf;
3785
3786   union {
3787     /* A pointer to the most recently used stub hash entry against this
3788        symbol.  */
3789     struct ppc_stub_hash_entry *stub_cache;
3790
3791     /* A pointer to the next symbol starting with a '.'  */
3792     struct ppc_link_hash_entry *next_dot_sym;
3793   } u;
3794
3795   /* Track dynamic relocs copied for this symbol.  */
3796   struct elf_dyn_relocs *dyn_relocs;
3797
3798   /* Link between function code and descriptor symbols.  */
3799   struct ppc_link_hash_entry *oh;
3800
3801   /* Flag function code and descriptor symbols.  */
3802   unsigned int is_func:1;
3803   unsigned int is_func_descriptor:1;
3804   unsigned int fake:1;
3805
3806   /* Whether global opd/toc sym has been adjusted or not.
3807      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3808      should be set for all globals defined in any opd/toc section.  */
3809   unsigned int adjust_done:1;
3810
3811   /* Set if we twiddled this symbol to weak at some stage.  */
3812   unsigned int was_undefined:1;
3813
3814   /* Contexts in which symbol is used in the GOT (or TOC).
3815      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3816      corresponding relocs are encountered during check_relocs.
3817      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3818      indicate the corresponding GOT entry type is not needed.
3819      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3820      a TPREL one.  We use a separate flag rather than setting TPREL
3821      just for convenience in distinguishing the two cases.  */
3822 #define TLS_GD           1      /* GD reloc. */
3823 #define TLS_LD           2      /* LD reloc. */
3824 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
3825 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
3826 #define TLS_TLS         16      /* Any TLS reloc.  */
3827 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
3828 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
3829 #define PLT_IFUNC      128      /* STT_GNU_IFUNC.  */
3830   unsigned char tls_mask;
3831 };
3832
3833 /* ppc64 ELF linker hash table.  */
3834
3835 struct ppc_link_hash_table
3836 {
3837   struct elf_link_hash_table elf;
3838
3839   /* The stub hash table.  */
3840   struct bfd_hash_table stub_hash_table;
3841
3842   /* Another hash table for plt_branch stubs.  */
3843   struct bfd_hash_table branch_hash_table;
3844
3845   /* Hash table for function prologue tocsave.  */
3846   htab_t tocsave_htab;
3847
3848   /* Linker stub bfd.  */
3849   bfd *stub_bfd;
3850
3851   /* Linker call-backs.  */
3852   asection * (*add_stub_section) (const char *, asection *);
3853   void (*layout_sections_again) (void);
3854
3855   /* Array to keep track of which stub sections have been created, and
3856      information on stub grouping.  */
3857   struct map_stub {
3858     /* This is the section to which stubs in the group will be attached.  */
3859     asection *link_sec;
3860     /* The stub section.  */
3861     asection *stub_sec;
3862     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
3863     bfd_vma toc_off;
3864   } *stub_group;
3865
3866   /* Temp used when calculating TOC pointers.  */
3867   bfd_vma toc_curr;
3868   bfd *toc_bfd;
3869   asection *toc_first_sec;
3870
3871   /* Highest input section id.  */
3872   int top_id;
3873
3874   /* Highest output section index.  */
3875   int top_index;
3876
3877   /* Used when adding symbols.  */
3878   struct ppc_link_hash_entry *dot_syms;
3879
3880   /* List of input sections for each output section.  */
3881   asection **input_list;
3882
3883   /* Short-cuts to get to dynamic linker sections.  */
3884   asection *got;
3885   asection *plt;
3886   asection *relplt;
3887   asection *iplt;
3888   asection *reliplt;
3889   asection *dynbss;
3890   asection *relbss;
3891   asection *glink;
3892   asection *sfpr;
3893   asection *brlt;
3894   asection *relbrlt;
3895   asection *glink_eh_frame;
3896
3897   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3898   struct ppc_link_hash_entry *tls_get_addr;
3899   struct ppc_link_hash_entry *tls_get_addr_fd;
3900
3901   /* The size of reliplt used by got entry relocs.  */
3902   bfd_size_type got_reli_size;
3903
3904   /* Statistics.  */
3905   unsigned long stub_count[ppc_stub_plt_call_r2save];
3906
3907   /* Number of stubs against global syms.  */
3908   unsigned long stub_globals;
3909
3910   /* Alignment of PLT call stubs.  */
3911   unsigned int plt_stub_align:4;
3912
3913   /* Set if we're linking code with function descriptors.  */
3914   unsigned int opd_abi:1;
3915
3916   /* Set if PLT call stubs should load r11.  */
3917   unsigned int plt_static_chain:1;
3918
3919   /* Set if PLT call stubs need a read-read barrier.  */
3920   unsigned int plt_thread_safe:1;
3921
3922   /* Set if we should emit symbols for stubs.  */
3923   unsigned int emit_stub_syms:1;
3924
3925   /* Set if __tls_get_addr optimization should not be done.  */
3926   unsigned int no_tls_get_addr_opt:1;
3927
3928   /* Support for multiple toc sections.  */
3929   unsigned int do_multi_toc:1;
3930   unsigned int multi_toc_needed:1;
3931   unsigned int second_toc_pass:1;
3932   unsigned int do_toc_opt:1;
3933
3934   /* Set on error.  */
3935   unsigned int stub_error:1;
3936
3937   /* Temp used by ppc64_elf_process_dot_syms.  */
3938   unsigned int twiddled_syms:1;
3939
3940   /* Incremented every time we size stubs.  */
3941   unsigned int stub_iteration;
3942
3943   /* Small local sym cache.  */
3944   struct sym_cache sym_cache;
3945 };
3946
3947 /* Rename some of the generic section flags to better document how they
3948    are used here.  */
3949
3950 /* Nonzero if this section has TLS related relocations.  */
3951 #define has_tls_reloc sec_flg0
3952
3953 /* Nonzero if this section has a call to __tls_get_addr.  */
3954 #define has_tls_get_addr_call sec_flg1
3955
3956 /* Nonzero if this section has any toc or got relocs.  */
3957 #define has_toc_reloc sec_flg2
3958
3959 /* Nonzero if this section has a call to another section that uses
3960    the toc or got.  */
3961 #define makes_toc_func_call sec_flg3
3962
3963 /* Recursion protection when determining above flag.  */
3964 #define call_check_in_progress sec_flg4
3965 #define call_check_done sec_flg5
3966
3967 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
3968
3969 #define ppc_hash_table(p) \
3970   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3971   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3972
3973 #define ppc_stub_hash_lookup(table, string, create, copy) \
3974   ((struct ppc_stub_hash_entry *) \
3975    bfd_hash_lookup ((table), (string), (create), (copy)))
3976
3977 #define ppc_branch_hash_lookup(table, string, create, copy) \
3978   ((struct ppc_branch_hash_entry *) \
3979    bfd_hash_lookup ((table), (string), (create), (copy)))
3980
3981 /* Create an entry in the stub hash table.  */
3982
3983 static struct bfd_hash_entry *
3984 stub_hash_newfunc (struct bfd_hash_entry *entry,
3985                    struct bfd_hash_table *table,
3986                    const char *string)
3987 {
3988   /* Allocate the structure if it has not already been allocated by a
3989      subclass.  */
3990   if (entry == NULL)
3991     {
3992       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3993       if (entry == NULL)
3994         return entry;
3995     }
3996
3997   /* Call the allocation method of the superclass.  */
3998   entry = bfd_hash_newfunc (entry, table, string);
3999   if (entry != NULL)
4000     {
4001       struct ppc_stub_hash_entry *eh;
4002
4003       /* Initialize the local fields.  */
4004       eh = (struct ppc_stub_hash_entry *) entry;
4005       eh->stub_type = ppc_stub_none;
4006       eh->stub_sec = NULL;
4007       eh->stub_offset = 0;
4008       eh->target_value = 0;
4009       eh->target_section = NULL;
4010       eh->h = NULL;
4011       eh->plt_ent = NULL;
4012       eh->id_sec = NULL;
4013       eh->other = 0;
4014     }
4015
4016   return entry;
4017 }
4018
4019 /* Create an entry in the branch hash table.  */
4020
4021 static struct bfd_hash_entry *
4022 branch_hash_newfunc (struct bfd_hash_entry *entry,
4023                      struct bfd_hash_table *table,
4024                      const char *string)
4025 {
4026   /* Allocate the structure if it has not already been allocated by a
4027      subclass.  */
4028   if (entry == NULL)
4029     {
4030       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4031       if (entry == NULL)
4032         return entry;
4033     }
4034
4035   /* Call the allocation method of the superclass.  */
4036   entry = bfd_hash_newfunc (entry, table, string);
4037   if (entry != NULL)
4038     {
4039       struct ppc_branch_hash_entry *eh;
4040
4041       /* Initialize the local fields.  */
4042       eh = (struct ppc_branch_hash_entry *) entry;
4043       eh->offset = 0;
4044       eh->iter = 0;
4045     }
4046
4047   return entry;
4048 }
4049
4050 /* Create an entry in a ppc64 ELF linker hash table.  */
4051
4052 static struct bfd_hash_entry *
4053 link_hash_newfunc (struct bfd_hash_entry *entry,
4054                    struct bfd_hash_table *table,
4055                    const char *string)
4056 {
4057   /* Allocate the structure if it has not already been allocated by a
4058      subclass.  */
4059   if (entry == NULL)
4060     {
4061       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4062       if (entry == NULL)
4063         return entry;
4064     }
4065
4066   /* Call the allocation method of the superclass.  */
4067   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4068   if (entry != NULL)
4069     {
4070       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4071
4072       memset (&eh->u.stub_cache, 0,
4073               (sizeof (struct ppc_link_hash_entry)
4074                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4075
4076       /* When making function calls, old ABI code references function entry
4077          points (dot symbols), while new ABI code references the function
4078          descriptor symbol.  We need to make any combination of reference and
4079          definition work together, without breaking archive linking.
4080
4081          For a defined function "foo" and an undefined call to "bar":
4082          An old object defines "foo" and ".foo", references ".bar" (possibly
4083          "bar" too).
4084          A new object defines "foo" and references "bar".
4085
4086          A new object thus has no problem with its undefined symbols being
4087          satisfied by definitions in an old object.  On the other hand, the
4088          old object won't have ".bar" satisfied by a new object.
4089
4090          Keep a list of newly added dot-symbols.  */
4091
4092       if (string[0] == '.')
4093         {
4094           struct ppc_link_hash_table *htab;
4095
4096           htab = (struct ppc_link_hash_table *) table;
4097           eh->u.next_dot_sym = htab->dot_syms;
4098           htab->dot_syms = eh;
4099         }
4100     }
4101
4102   return entry;
4103 }
4104
4105 struct tocsave_entry {
4106   asection *sec;
4107   bfd_vma offset;
4108 };
4109
4110 static hashval_t
4111 tocsave_htab_hash (const void *p)
4112 {
4113   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4114   return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4115 }
4116
4117 static int
4118 tocsave_htab_eq (const void *p1, const void *p2)
4119 {
4120   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4121   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4122   return e1->sec == e2->sec && e1->offset == e2->offset;
4123 }
4124
4125 /* Create a ppc64 ELF linker hash table.  */
4126
4127 static struct bfd_link_hash_table *
4128 ppc64_elf_link_hash_table_create (bfd *abfd)
4129 {
4130   struct ppc_link_hash_table *htab;
4131   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4132
4133   htab = bfd_zmalloc (amt);
4134   if (htab == NULL)
4135     return NULL;
4136
4137   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4138                                       sizeof (struct ppc_link_hash_entry),
4139                                       PPC64_ELF_DATA))
4140     {
4141       free (htab);
4142       return NULL;
4143     }
4144
4145   /* Init the stub hash table too.  */
4146   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4147                             sizeof (struct ppc_stub_hash_entry)))
4148     return NULL;
4149
4150   /* And the branch hash table.  */
4151   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4152                             sizeof (struct ppc_branch_hash_entry)))
4153     return NULL;
4154
4155   htab->tocsave_htab = htab_try_create (1024,
4156                                         tocsave_htab_hash,
4157                                         tocsave_htab_eq,
4158                                         NULL);
4159   if (htab->tocsave_htab == NULL)
4160     return NULL;
4161
4162   /* Initializing two fields of the union is just cosmetic.  We really
4163      only care about glist, but when compiled on a 32-bit host the
4164      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
4165      debugger inspection of these fields look nicer.  */
4166   htab->elf.init_got_refcount.refcount = 0;
4167   htab->elf.init_got_refcount.glist = NULL;
4168   htab->elf.init_plt_refcount.refcount = 0;
4169   htab->elf.init_plt_refcount.glist = NULL;
4170   htab->elf.init_got_offset.offset = 0;
4171   htab->elf.init_got_offset.glist = NULL;
4172   htab->elf.init_plt_offset.offset = 0;
4173   htab->elf.init_plt_offset.glist = NULL;
4174
4175   return &htab->elf.root;
4176 }
4177
4178 /* Free the derived linker hash table.  */
4179
4180 static void
4181 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
4182 {
4183   struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
4184
4185   bfd_hash_table_free (&htab->stub_hash_table);
4186   bfd_hash_table_free (&htab->branch_hash_table);
4187   if (htab->tocsave_htab)
4188     htab_delete (htab->tocsave_htab);
4189   _bfd_elf_link_hash_table_free (hash);
4190 }
4191
4192 /* Create sections for linker generated code.  */
4193
4194 static bfd_boolean
4195 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4196 {
4197   struct ppc_link_hash_table *htab;
4198   flagword flags;
4199
4200   htab = ppc_hash_table (info);
4201
4202   /* Create .sfpr for code to save and restore fp regs.  */
4203   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4204            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4205   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4206                                                    flags);
4207   if (htab->sfpr == NULL
4208       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4209     return FALSE;
4210
4211   /* Create .glink for lazy dynamic linking support.  */
4212   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4213                                                     flags);
4214   if (htab->glink == NULL
4215       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4216     return FALSE;
4217
4218   if (!info->no_ld_generated_unwind_info)
4219     {
4220       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4221                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4222       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4223                                                                  ".eh_frame",
4224                                                                  flags);
4225       if (htab->glink_eh_frame == NULL
4226           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4227         return FALSE;
4228     }
4229
4230   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4231   htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4232   if (htab->iplt == NULL
4233       || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4234     return FALSE;
4235
4236   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4237            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4238   htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4239                                                       ".rela.iplt",
4240                                                       flags);
4241   if (htab->reliplt == NULL
4242       || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4243     return FALSE;
4244
4245   /* Create branch lookup table for plt_branch stubs.  */
4246   flags = (SEC_ALLOC | SEC_LOAD
4247            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4248   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4249                                                    flags);
4250   if (htab->brlt == NULL
4251       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4252     return FALSE;
4253
4254   if (!info->shared)
4255     return TRUE;
4256
4257   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4258            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4259   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4260                                                       ".rela.branch_lt",
4261                                                       flags);
4262   if (htab->relbrlt == NULL
4263       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4264     return FALSE;
4265
4266   return TRUE;
4267 }
4268
4269 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4270
4271 bfd_boolean
4272 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4273 {
4274   struct ppc_link_hash_table *htab;
4275
4276   elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4277
4278 /* Always hook our dynamic sections into the first bfd, which is the
4279    linker created stub bfd.  This ensures that the GOT header is at
4280    the start of the output TOC section.  */
4281   htab = ppc_hash_table (info);
4282   if (htab == NULL)
4283     return FALSE;
4284   htab->stub_bfd = abfd;
4285   htab->elf.dynobj = abfd;
4286
4287   if (info->relocatable)
4288     return TRUE;
4289
4290   return create_linkage_sections (htab->elf.dynobj, info);
4291 }
4292
4293 /* Build a name for an entry in the stub hash table.  */
4294
4295 static char *
4296 ppc_stub_name (const asection *input_section,
4297                const asection *sym_sec,
4298                const struct ppc_link_hash_entry *h,
4299                const Elf_Internal_Rela *rel)
4300 {
4301   char *stub_name;
4302   ssize_t len;
4303
4304   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4305      offsets from a sym as a branch target?  In fact, we could
4306      probably assume the addend is always zero.  */
4307   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4308
4309   if (h)
4310     {
4311       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4312       stub_name = bfd_malloc (len);
4313       if (stub_name == NULL)
4314         return stub_name;
4315
4316       len = sprintf (stub_name, "%08x.%s+%x",
4317                      input_section->id & 0xffffffff,
4318                      h->elf.root.root.string,
4319                      (int) rel->r_addend & 0xffffffff);
4320     }
4321   else
4322     {
4323       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4324       stub_name = bfd_malloc (len);
4325       if (stub_name == NULL)
4326         return stub_name;
4327
4328       len = sprintf (stub_name, "%08x.%x:%x+%x",
4329                      input_section->id & 0xffffffff,
4330                      sym_sec->id & 0xffffffff,
4331                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4332                      (int) rel->r_addend & 0xffffffff);
4333     }
4334   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4335     stub_name[len - 2] = 0;
4336   return stub_name;
4337 }
4338
4339 /* Look up an entry in the stub hash.  Stub entries are cached because
4340    creating the stub name takes a bit of time.  */
4341
4342 static struct ppc_stub_hash_entry *
4343 ppc_get_stub_entry (const asection *input_section,
4344                     const asection *sym_sec,
4345                     struct ppc_link_hash_entry *h,
4346                     const Elf_Internal_Rela *rel,
4347                     struct ppc_link_hash_table *htab)
4348 {
4349   struct ppc_stub_hash_entry *stub_entry;
4350   const asection *id_sec;
4351
4352   /* If this input section is part of a group of sections sharing one
4353      stub section, then use the id of the first section in the group.
4354      Stub names need to include a section id, as there may well be
4355      more than one stub used to reach say, printf, and we need to
4356      distinguish between them.  */
4357   id_sec = htab->stub_group[input_section->id].link_sec;
4358
4359   if (h != NULL && h->u.stub_cache != NULL
4360       && h->u.stub_cache->h == h
4361       && h->u.stub_cache->id_sec == id_sec)
4362     {
4363       stub_entry = h->u.stub_cache;
4364     }
4365   else
4366     {
4367       char *stub_name;
4368
4369       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4370       if (stub_name == NULL)
4371         return NULL;
4372
4373       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4374                                          stub_name, FALSE, FALSE);
4375       if (h != NULL)
4376         h->u.stub_cache = stub_entry;
4377
4378       free (stub_name);
4379     }
4380
4381   return stub_entry;
4382 }
4383
4384 /* Add a new stub entry to the stub hash.  Not all fields of the new
4385    stub entry are initialised.  */
4386
4387 static struct ppc_stub_hash_entry *
4388 ppc_add_stub (const char *stub_name,
4389               asection *section,
4390               struct bfd_link_info *info)
4391 {
4392   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4393   asection *link_sec;
4394   asection *stub_sec;
4395   struct ppc_stub_hash_entry *stub_entry;
4396
4397   link_sec = htab->stub_group[section->id].link_sec;
4398   stub_sec = htab->stub_group[section->id].stub_sec;
4399   if (stub_sec == NULL)
4400     {
4401       stub_sec = htab->stub_group[link_sec->id].stub_sec;
4402       if (stub_sec == NULL)
4403         {
4404           size_t namelen;
4405           bfd_size_type len;
4406           char *s_name;
4407
4408           namelen = strlen (link_sec->name);
4409           len = namelen + sizeof (STUB_SUFFIX);
4410           s_name = bfd_alloc (htab->stub_bfd, len);
4411           if (s_name == NULL)
4412             return NULL;
4413
4414           memcpy (s_name, link_sec->name, namelen);
4415           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4416           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4417           if (stub_sec == NULL)
4418             return NULL;
4419           htab->stub_group[link_sec->id].stub_sec = stub_sec;
4420         }
4421       htab->stub_group[section->id].stub_sec = stub_sec;
4422     }
4423
4424   /* Enter this entry into the linker stub hash table.  */
4425   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4426                                      TRUE, FALSE);
4427   if (stub_entry == NULL)
4428     {
4429       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4430                               section->owner, stub_name);
4431       return NULL;
4432     }
4433
4434   stub_entry->stub_sec = stub_sec;
4435   stub_entry->stub_offset = 0;
4436   stub_entry->id_sec = link_sec;
4437   return stub_entry;
4438 }
4439
4440 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4441    not already done.  */
4442
4443 static bfd_boolean
4444 create_got_section (bfd *abfd, struct bfd_link_info *info)
4445 {
4446   asection *got, *relgot;
4447   flagword flags;
4448   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4449
4450   if (!is_ppc64_elf (abfd))
4451     return FALSE;
4452   if (htab == NULL)
4453     return FALSE;
4454
4455   if (!htab->got)
4456     {
4457       if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4458         return FALSE;
4459
4460       htab->got = bfd_get_linker_section (htab->elf.dynobj, ".got");
4461       if (!htab->got)
4462         abort ();
4463     }
4464
4465   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4466            | SEC_LINKER_CREATED);
4467
4468   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4469   if (!got
4470       || !bfd_set_section_alignment (abfd, got, 3))
4471     return FALSE;
4472
4473   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4474                                                flags | SEC_READONLY);
4475   if (!relgot
4476       || ! bfd_set_section_alignment (abfd, relgot, 3))
4477     return FALSE;
4478
4479   ppc64_elf_tdata (abfd)->got = got;
4480   ppc64_elf_tdata (abfd)->relgot = relgot;
4481   return TRUE;
4482 }
4483
4484 /* Create the dynamic sections, and set up shortcuts.  */
4485
4486 static bfd_boolean
4487 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4488 {
4489   struct ppc_link_hash_table *htab;
4490
4491   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4492     return FALSE;
4493
4494   htab = ppc_hash_table (info);
4495   if (htab == NULL)
4496     return FALSE;
4497
4498   if (!htab->got)
4499     htab->got = bfd_get_linker_section (dynobj, ".got");
4500   htab->plt = bfd_get_linker_section (dynobj, ".plt");
4501   htab->relplt = bfd_get_linker_section (dynobj, ".rela.plt");
4502   htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
4503   if (!info->shared)
4504     htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
4505
4506   if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4507       || (!info->shared && !htab->relbss))
4508     abort ();
4509
4510   return TRUE;
4511 }
4512
4513 /* Follow indirect and warning symbol links.  */
4514
4515 static inline struct bfd_link_hash_entry *
4516 follow_link (struct bfd_link_hash_entry *h)
4517 {
4518   while (h->type == bfd_link_hash_indirect
4519          || h->type == bfd_link_hash_warning)
4520     h = h->u.i.link;
4521   return h;
4522 }
4523
4524 static inline struct elf_link_hash_entry *
4525 elf_follow_link (struct elf_link_hash_entry *h)
4526 {
4527   return (struct elf_link_hash_entry *) follow_link (&h->root);
4528 }
4529
4530 static inline struct ppc_link_hash_entry *
4531 ppc_follow_link (struct ppc_link_hash_entry *h)
4532 {
4533   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4534 }
4535
4536 /* Merge PLT info on FROM with that on TO.  */
4537
4538 static void
4539 move_plt_plist (struct ppc_link_hash_entry *from,
4540                 struct ppc_link_hash_entry *to)
4541 {
4542   if (from->elf.plt.plist != NULL)
4543     {
4544       if (to->elf.plt.plist != NULL)
4545         {
4546           struct plt_entry **entp;
4547           struct plt_entry *ent;
4548
4549           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4550             {
4551               struct plt_entry *dent;
4552
4553               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4554                 if (dent->addend == ent->addend)
4555                   {
4556                     dent->plt.refcount += ent->plt.refcount;
4557                     *entp = ent->next;
4558                     break;
4559                   }
4560               if (dent == NULL)
4561                 entp = &ent->next;
4562             }
4563           *entp = to->elf.plt.plist;
4564         }
4565
4566       to->elf.plt.plist = from->elf.plt.plist;
4567       from->elf.plt.plist = NULL;
4568     }
4569 }
4570
4571 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4572
4573 static void
4574 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4575                                 struct elf_link_hash_entry *dir,
4576                                 struct elf_link_hash_entry *ind)
4577 {
4578   struct ppc_link_hash_entry *edir, *eind;
4579
4580   edir = (struct ppc_link_hash_entry *) dir;
4581   eind = (struct ppc_link_hash_entry *) ind;
4582
4583   edir->is_func |= eind->is_func;
4584   edir->is_func_descriptor |= eind->is_func_descriptor;
4585   edir->tls_mask |= eind->tls_mask;
4586   if (eind->oh != NULL)
4587     edir->oh = ppc_follow_link (eind->oh);
4588
4589   /* If called to transfer flags for a weakdef during processing
4590      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4591      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
4592   if (!(ELIMINATE_COPY_RELOCS
4593         && eind->elf.root.type != bfd_link_hash_indirect
4594         && edir->elf.dynamic_adjusted))
4595     edir->elf.non_got_ref |= eind->elf.non_got_ref;
4596
4597   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4598   edir->elf.ref_regular |= eind->elf.ref_regular;
4599   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4600   edir->elf.needs_plt |= eind->elf.needs_plt;
4601
4602   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4603   if (eind->dyn_relocs != NULL)
4604     {
4605       if (edir->dyn_relocs != NULL)
4606         {
4607           struct elf_dyn_relocs **pp;
4608           struct elf_dyn_relocs *p;
4609
4610           /* Add reloc counts against the indirect sym to the direct sym
4611              list.  Merge any entries against the same section.  */
4612           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4613             {
4614               struct elf_dyn_relocs *q;
4615
4616               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4617                 if (q->sec == p->sec)
4618                   {
4619                     q->pc_count += p->pc_count;
4620                     q->count += p->count;
4621                     *pp = p->next;
4622                     break;
4623                   }
4624               if (q == NULL)
4625                 pp = &p->next;
4626             }
4627           *pp = edir->dyn_relocs;
4628         }
4629
4630       edir->dyn_relocs = eind->dyn_relocs;
4631       eind->dyn_relocs = NULL;
4632     }
4633
4634   /* If we were called to copy over info for a weak sym, that's all.
4635      You might think dyn_relocs need not be copied over;  After all,
4636      both syms will be dynamic or both non-dynamic so we're just
4637      moving reloc accounting around.  However, ELIMINATE_COPY_RELOCS
4638      code in ppc64_elf_adjust_dynamic_symbol needs to check for
4639      dyn_relocs in read-only sections, and it does so on what is the
4640      DIR sym here.  */
4641   if (eind->elf.root.type != bfd_link_hash_indirect)
4642     return;
4643
4644   /* Copy over got entries that we may have already seen to the
4645      symbol which just became indirect.  */
4646   if (eind->elf.got.glist != NULL)
4647     {
4648       if (edir->elf.got.glist != NULL)
4649         {
4650           struct got_entry **entp;
4651           struct got_entry *ent;
4652
4653           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4654             {
4655               struct got_entry *dent;
4656
4657               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4658                 if (dent->addend == ent->addend
4659                     && dent->owner == ent->owner
4660                     && dent->tls_type == ent->tls_type)
4661                   {
4662                     dent->got.refcount += ent->got.refcount;
4663                     *entp = ent->next;
4664                     break;
4665                   }
4666               if (dent == NULL)
4667                 entp = &ent->next;
4668             }
4669           *entp = edir->elf.got.glist;
4670         }
4671
4672       edir->elf.got.glist = eind->elf.got.glist;
4673       eind->elf.got.glist = NULL;
4674     }
4675
4676   /* And plt entries.  */
4677   move_plt_plist (eind, edir);
4678
4679   if (eind->elf.dynindx != -1)
4680     {
4681       if (edir->elf.dynindx != -1)
4682         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4683                                 edir->elf.dynstr_index);
4684       edir->elf.dynindx = eind->elf.dynindx;
4685       edir->elf.dynstr_index = eind->elf.dynstr_index;
4686       eind->elf.dynindx = -1;
4687       eind->elf.dynstr_index = 0;
4688     }
4689 }
4690
4691 /* Find the function descriptor hash entry from the given function code
4692    hash entry FH.  Link the entries via their OH fields.  */
4693
4694 static struct ppc_link_hash_entry *
4695 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4696 {
4697   struct ppc_link_hash_entry *fdh = fh->oh;
4698
4699   if (fdh == NULL)
4700     {
4701       const char *fd_name = fh->elf.root.root.string + 1;
4702
4703       fdh = (struct ppc_link_hash_entry *)
4704         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4705       if (fdh == NULL)
4706         return fdh;
4707
4708       fdh->is_func_descriptor = 1;
4709       fdh->oh = fh;
4710       fh->is_func = 1;
4711       fh->oh = fdh;
4712     }
4713
4714   return ppc_follow_link (fdh);
4715 }
4716
4717 /* Make a fake function descriptor sym for the code sym FH.  */
4718
4719 static struct ppc_link_hash_entry *
4720 make_fdh (struct bfd_link_info *info,
4721           struct ppc_link_hash_entry *fh)
4722 {
4723   bfd *abfd;
4724   asymbol *newsym;
4725   struct bfd_link_hash_entry *bh;
4726   struct ppc_link_hash_entry *fdh;
4727
4728   abfd = fh->elf.root.u.undef.abfd;
4729   newsym = bfd_make_empty_symbol (abfd);
4730   newsym->name = fh->elf.root.root.string + 1;
4731   newsym->section = bfd_und_section_ptr;
4732   newsym->value = 0;
4733   newsym->flags = BSF_WEAK;
4734
4735   bh = NULL;
4736   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4737                                          newsym->flags, newsym->section,
4738                                          newsym->value, NULL, FALSE, FALSE,
4739                                          &bh))
4740     return NULL;
4741
4742   fdh = (struct ppc_link_hash_entry *) bh;
4743   fdh->elf.non_elf = 0;
4744   fdh->fake = 1;
4745   fdh->is_func_descriptor = 1;
4746   fdh->oh = fh;
4747   fh->is_func = 1;
4748   fh->oh = fdh;
4749   return fdh;
4750 }
4751
4752 /* Fix function descriptor symbols defined in .opd sections to be
4753    function type.  */
4754
4755 static bfd_boolean
4756 ppc64_elf_add_symbol_hook (bfd *ibfd,
4757                            struct bfd_link_info *info,
4758                            Elf_Internal_Sym *isym,
4759                            const char **name,
4760                            flagword *flags ATTRIBUTE_UNUSED,
4761                            asection **sec,
4762                            bfd_vma *value ATTRIBUTE_UNUSED)
4763 {
4764   if ((ibfd->flags & DYNAMIC) == 0
4765       && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4766     elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4767
4768   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4769     {
4770       if ((ibfd->flags & DYNAMIC) == 0)
4771         elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4772     }
4773   else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4774     ;
4775   else if (*sec != NULL
4776            && strcmp ((*sec)->name, ".opd") == 0)
4777     isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4778
4779   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4780     {
4781       if (abiversion (ibfd) == 0)
4782         set_abiversion (ibfd, 2);
4783       else if (abiversion (ibfd) == 1)
4784         {
4785           info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4786                                     " for ABI version 1\n"), name);
4787           bfd_set_error (bfd_error_bad_value);
4788           return FALSE;
4789         }
4790     }
4791
4792   return TRUE;
4793 }
4794
4795 /* Merge non-visibility st_other attributes: local entry point.  */
4796
4797 static void
4798 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4799                                   const Elf_Internal_Sym *isym,
4800                                   bfd_boolean definition,
4801                                   bfd_boolean dynamic)
4802 {
4803   if (definition && !dynamic)
4804     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4805                 | ELF_ST_VISIBILITY (h->other));
4806 }
4807
4808 /* This function makes an old ABI object reference to ".bar" cause the
4809    inclusion of a new ABI object archive that defines "bar".
4810    NAME is a symbol defined in an archive.  Return a symbol in the hash
4811    table that might be satisfied by the archive symbols.  */
4812
4813 static struct elf_link_hash_entry *
4814 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4815                                  struct bfd_link_info *info,
4816                                  const char *name)
4817 {
4818   struct elf_link_hash_entry *h;
4819   char *dot_name;
4820   size_t len;
4821
4822   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4823   if (h != NULL
4824       /* Don't return this sym if it is a fake function descriptor
4825          created by add_symbol_adjust.  */
4826       && !(h->root.type == bfd_link_hash_undefweak
4827            && ((struct ppc_link_hash_entry *) h)->fake))
4828     return h;
4829
4830   if (name[0] == '.')
4831     return h;
4832
4833   len = strlen (name);
4834   dot_name = bfd_alloc (abfd, len + 2);
4835   if (dot_name == NULL)
4836     return (struct elf_link_hash_entry *) 0 - 1;
4837   dot_name[0] = '.';
4838   memcpy (dot_name + 1, name, len + 1);
4839   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4840   bfd_release (abfd, dot_name);
4841   return h;
4842 }
4843
4844 /* This function satisfies all old ABI object references to ".bar" if a
4845    new ABI object defines "bar".  Well, at least, undefined dot symbols
4846    are made weak.  This stops later archive searches from including an
4847    object if we already have a function descriptor definition.  It also
4848    prevents the linker complaining about undefined symbols.
4849    We also check and correct mismatched symbol visibility here.  The
4850    most restrictive visibility of the function descriptor and the
4851    function entry symbol is used.  */
4852
4853 static bfd_boolean
4854 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4855 {
4856   struct ppc_link_hash_table *htab;
4857   struct ppc_link_hash_entry *fdh;
4858
4859   if (eh->elf.root.type == bfd_link_hash_indirect)
4860     return TRUE;
4861
4862   if (eh->elf.root.type == bfd_link_hash_warning)
4863     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4864
4865   if (eh->elf.root.root.string[0] != '.')
4866     abort ();
4867
4868   htab = ppc_hash_table (info);
4869   if (htab == NULL)
4870     return FALSE;
4871
4872   fdh = lookup_fdh (eh, htab);
4873   if (fdh == NULL)
4874     {
4875       if (!info->relocatable
4876           && (eh->elf.root.type == bfd_link_hash_undefined
4877               || eh->elf.root.type == bfd_link_hash_undefweak)
4878           && eh->elf.ref_regular)
4879         {
4880           /* Make an undefweak function descriptor sym, which is enough to
4881              pull in an --as-needed shared lib, but won't cause link
4882              errors.  Archives are handled elsewhere.  */
4883           fdh = make_fdh (info, eh);
4884           if (fdh == NULL)
4885             return FALSE;
4886           fdh->elf.ref_regular = 1;
4887         }
4888     }
4889   else
4890     {
4891       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4892       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4893       if (entry_vis < descr_vis)
4894         fdh->elf.other += entry_vis - descr_vis;
4895       else if (entry_vis > descr_vis)
4896         eh->elf.other += descr_vis - entry_vis;
4897
4898       if ((fdh->elf.root.type == bfd_link_hash_defined
4899            || fdh->elf.root.type == bfd_link_hash_defweak)
4900           && eh->elf.root.type == bfd_link_hash_undefined)
4901         {
4902           eh->elf.root.type = bfd_link_hash_undefweak;
4903           eh->was_undefined = 1;
4904           htab->twiddled_syms = 1;
4905         }
4906     }
4907
4908   return TRUE;
4909 }
4910
4911 /* Process list of dot-symbols we made in link_hash_newfunc.  */
4912
4913 static bfd_boolean
4914 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
4915 {
4916   struct ppc_link_hash_table *htab;
4917   struct ppc_link_hash_entry **p, *eh;
4918
4919   if (!is_ppc64_elf (info->output_bfd))
4920     return TRUE;
4921   htab = ppc_hash_table (info);
4922   if (htab == NULL)
4923     return FALSE;
4924
4925   if (is_ppc64_elf (ibfd))
4926     {
4927       p = &htab->dot_syms;
4928       while ((eh = *p) != NULL)
4929         {
4930           *p = NULL;
4931           if (&eh->elf == htab->elf.hgot)
4932             ;
4933           else if (htab->elf.hgot == NULL
4934                    && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4935             htab->elf.hgot = &eh->elf;
4936           else if (!add_symbol_adjust (eh, info))
4937             return FALSE;
4938           p = &eh->u.next_dot_sym;
4939         }
4940     }
4941
4942   /* Clear the list for non-ppc64 input files.  */
4943   p = &htab->dot_syms;
4944   while ((eh = *p) != NULL)
4945     {
4946       *p = NULL;
4947       p = &eh->u.next_dot_sym;
4948     }
4949
4950   /* We need to fix the undefs list for any syms we have twiddled to
4951      undef_weak.  */
4952   if (htab->twiddled_syms)
4953     {
4954       bfd_link_repair_undef_list (&htab->elf.root);
4955       htab->twiddled_syms = 0;
4956     }
4957   return TRUE;
4958 }
4959
4960 /* Undo hash table changes when an --as-needed input file is determined
4961    not to be needed.  */
4962
4963 static bfd_boolean
4964 ppc64_elf_notice_as_needed (bfd *ibfd,
4965                             struct bfd_link_info *info,
4966                             enum notice_asneeded_action act)
4967 {
4968   if (act == notice_not_needed)
4969     {
4970       struct ppc_link_hash_table *htab = ppc_hash_table (info);
4971
4972       if (htab == NULL)
4973         return FALSE;
4974
4975       htab->dot_syms = NULL;
4976     }
4977   return _bfd_elf_notice_as_needed (ibfd, info, act);
4978 }
4979
4980 /* If --just-symbols against a final linked binary, then assume we need
4981    toc adjusting stubs when calling functions defined there.  */
4982
4983 static void
4984 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4985 {
4986   if ((sec->flags & SEC_CODE) != 0
4987       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4988       && is_ppc64_elf (sec->owner))
4989     {
4990       asection *got = bfd_get_section_by_name (sec->owner, ".got");
4991       if (got != NULL
4992           && got->size >= elf_backend_got_header_size
4993           && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4994         sec->has_toc_reloc = 1;
4995     }
4996   _bfd_elf_link_just_syms (sec, info);
4997 }
4998
4999 static struct plt_entry **
5000 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5001                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5002 {
5003   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5004   struct plt_entry **local_plt;
5005   unsigned char *local_got_tls_masks;
5006
5007   if (local_got_ents == NULL)
5008     {
5009       bfd_size_type size = symtab_hdr->sh_info;
5010
5011       size *= (sizeof (*local_got_ents)
5012                + sizeof (*local_plt)
5013                + sizeof (*local_got_tls_masks));
5014       local_got_ents = bfd_zalloc (abfd, size);
5015       if (local_got_ents == NULL)
5016         return NULL;
5017       elf_local_got_ents (abfd) = local_got_ents;
5018     }
5019
5020   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5021     {
5022       struct got_entry *ent;
5023
5024       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5025         if (ent->addend == r_addend
5026             && ent->owner == abfd
5027             && ent->tls_type == tls_type)
5028           break;
5029       if (ent == NULL)
5030         {
5031           bfd_size_type amt = sizeof (*ent);
5032           ent = bfd_alloc (abfd, amt);
5033           if (ent == NULL)
5034             return FALSE;
5035           ent->next = local_got_ents[r_symndx];
5036           ent->addend = r_addend;
5037           ent->owner = abfd;
5038           ent->tls_type = tls_type;
5039           ent->is_indirect = FALSE;
5040           ent->got.refcount = 0;
5041           local_got_ents[r_symndx] = ent;
5042         }
5043       ent->got.refcount += 1;
5044     }
5045
5046   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5047   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5048   local_got_tls_masks[r_symndx] |= tls_type;
5049
5050   return local_plt + r_symndx;
5051 }
5052
5053 static bfd_boolean
5054 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5055 {
5056   struct plt_entry *ent;
5057
5058   for (ent = *plist; ent != NULL; ent = ent->next)
5059     if (ent->addend == addend)
5060       break;
5061   if (ent == NULL)
5062     {
5063       bfd_size_type amt = sizeof (*ent);
5064       ent = bfd_alloc (abfd, amt);
5065       if (ent == NULL)
5066         return FALSE;
5067       ent->next = *plist;
5068       ent->addend = addend;
5069       ent->plt.refcount = 0;
5070       *plist = ent;
5071     }
5072   ent->plt.refcount += 1;
5073   return TRUE;
5074 }
5075
5076 static bfd_boolean
5077 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5078 {
5079   return (r_type == R_PPC64_REL24
5080           || r_type == R_PPC64_REL14
5081           || r_type == R_PPC64_REL14_BRTAKEN
5082           || r_type == R_PPC64_REL14_BRNTAKEN
5083           || r_type == R_PPC64_ADDR24
5084           || r_type == R_PPC64_ADDR14
5085           || r_type == R_PPC64_ADDR14_BRTAKEN
5086           || r_type == R_PPC64_ADDR14_BRNTAKEN);
5087 }
5088
5089 /* Look through the relocs for a section during the first phase, and
5090    calculate needed space in the global offset table, procedure
5091    linkage table, and dynamic reloc sections.  */
5092
5093 static bfd_boolean
5094 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5095                         asection *sec, const Elf_Internal_Rela *relocs)
5096 {
5097   struct ppc_link_hash_table *htab;
5098   Elf_Internal_Shdr *symtab_hdr;
5099   struct elf_link_hash_entry **sym_hashes;
5100   const Elf_Internal_Rela *rel;
5101   const Elf_Internal_Rela *rel_end;
5102   asection *sreloc;
5103   asection **opd_sym_map;
5104   struct elf_link_hash_entry *tga, *dottga;
5105
5106   if (info->relocatable)
5107     return TRUE;
5108
5109   /* Don't do anything special with non-loaded, non-alloced sections.
5110      In particular, any relocs in such sections should not affect GOT
5111      and PLT reference counting (ie. we don't allow them to create GOT
5112      or PLT entries), there's no possibility or desire to optimize TLS
5113      relocs, and there's not much point in propagating relocs to shared
5114      libs that the dynamic linker won't relocate.  */
5115   if ((sec->flags & SEC_ALLOC) == 0)
5116     return TRUE;
5117
5118   BFD_ASSERT (is_ppc64_elf (abfd));
5119
5120   htab = ppc_hash_table (info);
5121   if (htab == NULL)
5122     return FALSE;
5123
5124   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5125                               FALSE, FALSE, TRUE);
5126   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5127                                  FALSE, FALSE, TRUE);
5128   symtab_hdr = &elf_symtab_hdr (abfd);
5129   sym_hashes = elf_sym_hashes (abfd);
5130   sreloc = NULL;
5131   opd_sym_map = NULL;
5132   if (strcmp (sec->name, ".opd") == 0)
5133     {
5134       /* Garbage collection needs some extra help with .opd sections.
5135          We don't want to necessarily keep everything referenced by
5136          relocs in .opd, as that would keep all functions.  Instead,
5137          if we reference an .opd symbol (a function descriptor), we
5138          want to keep the function code symbol's section.  This is
5139          easy for global symbols, but for local syms we need to keep
5140          information about the associated function section.  */
5141       bfd_size_type amt;
5142
5143       if (abiversion (abfd) == 0)
5144         set_abiversion (abfd, 1);
5145       else if (abiversion (abfd) == 2)
5146         {
5147           info->callbacks->einfo (_("%P: .opd not allowed in ABI version %d\n"),
5148                                   abiversion (abfd));
5149           bfd_set_error (bfd_error_bad_value);
5150           return FALSE;
5151         }
5152       amt = sec->size * sizeof (*opd_sym_map) / 8;
5153       opd_sym_map = bfd_zalloc (abfd, amt);
5154       if (opd_sym_map == NULL)
5155         return FALSE;
5156       ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
5157       BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
5158       ppc64_elf_section_data (sec)->sec_type = sec_opd;
5159     }
5160
5161   rel_end = relocs + sec->reloc_count;
5162   for (rel = relocs; rel < rel_end; rel++)
5163     {
5164       unsigned long r_symndx;
5165       struct elf_link_hash_entry *h;
5166       enum elf_ppc64_reloc_type r_type;
5167       int tls_type;
5168       struct _ppc64_elf_section_data *ppc64_sec;
5169       struct plt_entry **ifunc;
5170
5171       r_symndx = ELF64_R_SYM (rel->r_info);
5172       if (r_symndx < symtab_hdr->sh_info)
5173         h = NULL;
5174       else
5175         {
5176           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5177           h = elf_follow_link (h);
5178
5179           /* PR15323, ref flags aren't set for references in the same
5180              object.  */
5181           h->root.non_ir_ref = 1;
5182
5183           if (h == htab->elf.hgot)
5184             sec->has_toc_reloc = 1;
5185         }
5186
5187       tls_type = 0;
5188       ifunc = NULL;
5189       if (h != NULL)
5190         {
5191           if (h->type == STT_GNU_IFUNC)
5192             {
5193               h->needs_plt = 1;
5194               ifunc = &h->plt.plist;
5195             }
5196         }
5197       else
5198         {
5199           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5200                                                           abfd, r_symndx);
5201           if (isym == NULL)
5202             return FALSE;
5203
5204           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5205             {
5206               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5207                                              rel->r_addend, PLT_IFUNC);
5208               if (ifunc == NULL)
5209                 return FALSE;
5210             }
5211         }
5212       r_type = ELF64_R_TYPE (rel->r_info);
5213       if (is_branch_reloc (r_type))
5214         {
5215           if (h != NULL && (h == tga || h == dottga))
5216             {
5217               if (rel != relocs
5218                   && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5219                       || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5220                 /* We have a new-style __tls_get_addr call with a marker
5221                    reloc.  */
5222                 ;
5223               else
5224                 /* Mark this section as having an old-style call.  */
5225                 sec->has_tls_get_addr_call = 1;
5226             }
5227
5228           /* STT_GNU_IFUNC symbols must have a PLT entry.  */
5229           if (ifunc != NULL
5230               && !update_plt_info (abfd, ifunc, rel->r_addend))
5231             return FALSE;
5232         }
5233
5234       switch (r_type)
5235         {
5236         case R_PPC64_TLSGD:
5237         case R_PPC64_TLSLD:
5238           /* These special tls relocs tie a call to __tls_get_addr with
5239              its parameter symbol.  */
5240           break;
5241
5242         case R_PPC64_GOT_TLSLD16:
5243         case R_PPC64_GOT_TLSLD16_LO:
5244         case R_PPC64_GOT_TLSLD16_HI:
5245         case R_PPC64_GOT_TLSLD16_HA:
5246           tls_type = TLS_TLS | TLS_LD;
5247           goto dogottls;
5248
5249         case R_PPC64_GOT_TLSGD16:
5250         case R_PPC64_GOT_TLSGD16_LO:
5251         case R_PPC64_GOT_TLSGD16_HI:
5252         case R_PPC64_GOT_TLSGD16_HA:
5253           tls_type = TLS_TLS | TLS_GD;
5254           goto dogottls;
5255
5256         case R_PPC64_GOT_TPREL16_DS:
5257         case R_PPC64_GOT_TPREL16_LO_DS:
5258         case R_PPC64_GOT_TPREL16_HI:
5259         case R_PPC64_GOT_TPREL16_HA:
5260           if (!info->executable)
5261             info->flags |= DF_STATIC_TLS;
5262           tls_type = TLS_TLS | TLS_TPREL;
5263           goto dogottls;
5264
5265         case R_PPC64_GOT_DTPREL16_DS:
5266         case R_PPC64_GOT_DTPREL16_LO_DS:
5267         case R_PPC64_GOT_DTPREL16_HI:
5268         case R_PPC64_GOT_DTPREL16_HA:
5269           tls_type = TLS_TLS | TLS_DTPREL;
5270         dogottls:
5271           sec->has_tls_reloc = 1;
5272           /* Fall thru */
5273
5274         case R_PPC64_GOT16:
5275         case R_PPC64_GOT16_DS:
5276         case R_PPC64_GOT16_HA:
5277         case R_PPC64_GOT16_HI:
5278         case R_PPC64_GOT16_LO:
5279         case R_PPC64_GOT16_LO_DS:
5280           /* This symbol requires a global offset table entry.  */
5281           sec->has_toc_reloc = 1;
5282           if (r_type == R_PPC64_GOT_TLSLD16
5283               || r_type == R_PPC64_GOT_TLSGD16
5284               || r_type == R_PPC64_GOT_TPREL16_DS
5285               || r_type == R_PPC64_GOT_DTPREL16_DS
5286               || r_type == R_PPC64_GOT16
5287               || r_type == R_PPC64_GOT16_DS)
5288             {
5289               htab->do_multi_toc = 1;
5290               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5291             }
5292
5293           if (ppc64_elf_tdata (abfd)->got == NULL
5294               && !create_got_section (abfd, info))
5295             return FALSE;
5296
5297           if (h != NULL)
5298             {
5299               struct ppc_link_hash_entry *eh;
5300               struct got_entry *ent;
5301
5302               eh = (struct ppc_link_hash_entry *) h;
5303               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5304                 if (ent->addend == rel->r_addend
5305                     && ent->owner == abfd
5306                     && ent->tls_type == tls_type)
5307                   break;
5308               if (ent == NULL)
5309                 {
5310                   bfd_size_type amt = sizeof (*ent);
5311                   ent = bfd_alloc (abfd, amt);
5312                   if (ent == NULL)
5313                     return FALSE;
5314                   ent->next = eh->elf.got.glist;
5315                   ent->addend = rel->r_addend;
5316                   ent->owner = abfd;
5317                   ent->tls_type = tls_type;
5318                   ent->is_indirect = FALSE;
5319                   ent->got.refcount = 0;
5320                   eh->elf.got.glist = ent;
5321                 }
5322               ent->got.refcount += 1;
5323               eh->tls_mask |= tls_type;
5324             }
5325           else
5326             /* This is a global offset table entry for a local symbol.  */
5327             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5328                                         rel->r_addend, tls_type))
5329               return FALSE;
5330           break;
5331
5332         case R_PPC64_PLT16_HA:
5333         case R_PPC64_PLT16_HI:
5334         case R_PPC64_PLT16_LO:
5335         case R_PPC64_PLT32:
5336         case R_PPC64_PLT64:
5337           /* This symbol requires a procedure linkage table entry.  We
5338              actually build the entry in adjust_dynamic_symbol,
5339              because this might be a case of linking PIC code without
5340              linking in any dynamic objects, in which case we don't
5341              need to generate a procedure linkage table after all.  */
5342           if (h == NULL)
5343             {
5344               /* It does not make sense to have a procedure linkage
5345                  table entry for a local symbol.  */
5346               bfd_set_error (bfd_error_bad_value);
5347               return FALSE;
5348             }
5349           else
5350             {
5351               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5352                 return FALSE;
5353               h->needs_plt = 1;
5354               if (h->root.root.string[0] == '.'
5355                   && h->root.root.string[1] != '\0')
5356                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5357             }
5358           break;
5359
5360           /* The following relocations don't need to propagate the
5361              relocation if linking a shared object since they are
5362              section relative.  */
5363         case R_PPC64_SECTOFF:
5364         case R_PPC64_SECTOFF_LO:
5365         case R_PPC64_SECTOFF_HI:
5366         case R_PPC64_SECTOFF_HA:
5367         case R_PPC64_SECTOFF_DS:
5368         case R_PPC64_SECTOFF_LO_DS:
5369         case R_PPC64_DTPREL16:
5370         case R_PPC64_DTPREL16_LO:
5371         case R_PPC64_DTPREL16_HI:
5372         case R_PPC64_DTPREL16_HA:
5373         case R_PPC64_DTPREL16_DS:
5374         case R_PPC64_DTPREL16_LO_DS:
5375         case R_PPC64_DTPREL16_HIGH:
5376         case R_PPC64_DTPREL16_HIGHA:
5377         case R_PPC64_DTPREL16_HIGHER:
5378         case R_PPC64_DTPREL16_HIGHERA:
5379         case R_PPC64_DTPREL16_HIGHEST:
5380         case R_PPC64_DTPREL16_HIGHESTA:
5381           break;
5382
5383           /* Nor do these.  */
5384         case R_PPC64_REL16:
5385         case R_PPC64_REL16_LO:
5386         case R_PPC64_REL16_HI:
5387         case R_PPC64_REL16_HA:
5388           break;
5389
5390         case R_PPC64_TOC16:
5391         case R_PPC64_TOC16_DS:
5392           htab->do_multi_toc = 1;
5393           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5394         case R_PPC64_TOC16_LO:
5395         case R_PPC64_TOC16_HI:
5396         case R_PPC64_TOC16_HA:
5397         case R_PPC64_TOC16_LO_DS:
5398           sec->has_toc_reloc = 1;
5399           break;
5400
5401           /* This relocation describes the C++ object vtable hierarchy.
5402              Reconstruct it for later use during GC.  */
5403         case R_PPC64_GNU_VTINHERIT:
5404           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5405             return FALSE;
5406           break;
5407
5408           /* This relocation describes which C++ vtable entries are actually
5409              used.  Record for later use during GC.  */
5410         case R_PPC64_GNU_VTENTRY:
5411           BFD_ASSERT (h != NULL);
5412           if (h != NULL
5413               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5414             return FALSE;
5415           break;
5416
5417         case R_PPC64_REL14:
5418         case R_PPC64_REL14_BRTAKEN:
5419         case R_PPC64_REL14_BRNTAKEN:
5420           {
5421             asection *dest = NULL;
5422
5423             /* Heuristic: If jumping outside our section, chances are
5424                we are going to need a stub.  */
5425             if (h != NULL)
5426               {
5427                 /* If the sym is weak it may be overridden later, so
5428                    don't assume we know where a weak sym lives.  */
5429                 if (h->root.type == bfd_link_hash_defined)
5430                   dest = h->root.u.def.section;
5431               }
5432             else
5433               {
5434                 Elf_Internal_Sym *isym;
5435
5436                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5437                                               abfd, r_symndx);
5438                 if (isym == NULL)
5439                   return FALSE;
5440
5441                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5442               }
5443
5444             if (dest != sec)
5445               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5446           }
5447           /* Fall through.  */
5448
5449         case R_PPC64_REL24:
5450           if (h != NULL && ifunc == NULL)
5451             {
5452               /* We may need a .plt entry if the function this reloc
5453                  refers to is in a shared lib.  */
5454               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5455                 return FALSE;
5456               h->needs_plt = 1;
5457               if (h->root.root.string[0] == '.'
5458                   && h->root.root.string[1] != '\0')
5459                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5460               if (h == tga || h == dottga)
5461                 sec->has_tls_reloc = 1;
5462             }
5463           break;
5464
5465         case R_PPC64_TPREL64:
5466           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5467           if (!info->executable)
5468             info->flags |= DF_STATIC_TLS;
5469           goto dotlstoc;
5470
5471         case R_PPC64_DTPMOD64:
5472           if (rel + 1 < rel_end
5473               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5474               && rel[1].r_offset == rel->r_offset + 8)
5475             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5476           else
5477             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5478           goto dotlstoc;
5479
5480         case R_PPC64_DTPREL64:
5481           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5482           if (rel != relocs
5483               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5484               && rel[-1].r_offset == rel->r_offset - 8)
5485             /* This is the second reloc of a dtpmod, dtprel pair.
5486                Don't mark with TLS_DTPREL.  */
5487             goto dodyn;
5488
5489         dotlstoc:
5490           sec->has_tls_reloc = 1;
5491           if (h != NULL)
5492             {
5493               struct ppc_link_hash_entry *eh;
5494               eh = (struct ppc_link_hash_entry *) h;
5495               eh->tls_mask |= tls_type;
5496             }
5497           else
5498             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5499                                         rel->r_addend, tls_type))
5500               return FALSE;
5501
5502           ppc64_sec = ppc64_elf_section_data (sec);
5503           if (ppc64_sec->sec_type != sec_toc)
5504             {
5505               bfd_size_type amt;
5506
5507               /* One extra to simplify get_tls_mask.  */
5508               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5509               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5510               if (ppc64_sec->u.toc.symndx == NULL)
5511                 return FALSE;
5512               amt = sec->size * sizeof (bfd_vma) / 8;
5513               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5514               if (ppc64_sec->u.toc.add == NULL)
5515                 return FALSE;
5516               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5517               ppc64_sec->sec_type = sec_toc;
5518             }
5519           BFD_ASSERT (rel->r_offset % 8 == 0);
5520           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5521           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5522
5523           /* Mark the second slot of a GD or LD entry.
5524              -1 to indicate GD and -2 to indicate LD.  */
5525           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5526             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5527           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5528             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5529           goto dodyn;
5530
5531         case R_PPC64_TPREL16:
5532         case R_PPC64_TPREL16_LO:
5533         case R_PPC64_TPREL16_HI:
5534         case R_PPC64_TPREL16_HA:
5535         case R_PPC64_TPREL16_DS:
5536         case R_PPC64_TPREL16_LO_DS:
5537         case R_PPC64_TPREL16_HIGH:
5538         case R_PPC64_TPREL16_HIGHA:
5539         case R_PPC64_TPREL16_HIGHER:
5540         case R_PPC64_TPREL16_HIGHERA:
5541         case R_PPC64_TPREL16_HIGHEST:
5542         case R_PPC64_TPREL16_HIGHESTA:
5543           if (info->shared)
5544             {
5545               if (!info->executable)
5546                 info->flags |= DF_STATIC_TLS;
5547               goto dodyn;
5548             }
5549           break;
5550
5551         case R_PPC64_ADDR64:
5552           if (opd_sym_map != NULL
5553               && rel + 1 < rel_end
5554               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5555             {
5556               if (h != NULL)
5557                 {
5558                   if (h->root.root.string[0] == '.'
5559                       && h->root.root.string[1] != 0
5560                       && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5561                     ;
5562                   else
5563                     ((struct ppc_link_hash_entry *) h)->is_func = 1;
5564                 }
5565               else
5566                 {
5567                   asection *s;
5568                   Elf_Internal_Sym *isym;
5569
5570                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5571                                                 abfd, r_symndx);
5572                   if (isym == NULL)
5573                     return FALSE;
5574
5575                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5576                   if (s != NULL && s != sec)
5577                     opd_sym_map[rel->r_offset / 8] = s;
5578                 }
5579             }
5580           /* Fall through.  */
5581
5582         case R_PPC64_REL30:
5583         case R_PPC64_REL32:
5584         case R_PPC64_REL64:
5585         case R_PPC64_ADDR14:
5586         case R_PPC64_ADDR14_BRNTAKEN:
5587         case R_PPC64_ADDR14_BRTAKEN:
5588         case R_PPC64_ADDR16:
5589         case R_PPC64_ADDR16_DS:
5590         case R_PPC64_ADDR16_HA:
5591         case R_PPC64_ADDR16_HI:
5592         case R_PPC64_ADDR16_HIGH:
5593         case R_PPC64_ADDR16_HIGHA:
5594         case R_PPC64_ADDR16_HIGHER:
5595         case R_PPC64_ADDR16_HIGHERA:
5596         case R_PPC64_ADDR16_HIGHEST:
5597         case R_PPC64_ADDR16_HIGHESTA:
5598         case R_PPC64_ADDR16_LO:
5599         case R_PPC64_ADDR16_LO_DS:
5600         case R_PPC64_ADDR24:
5601         case R_PPC64_ADDR32:
5602         case R_PPC64_UADDR16:
5603         case R_PPC64_UADDR32:
5604         case R_PPC64_UADDR64:
5605         case R_PPC64_TOC:
5606           if (h != NULL && !info->shared)
5607             /* We may need a copy reloc.  */
5608             h->non_got_ref = 1;
5609
5610           /* Don't propagate .opd relocs.  */
5611           if (NO_OPD_RELOCS && opd_sym_map != NULL)
5612             break;
5613
5614           /* If we are creating a shared library, and this is a reloc
5615              against a global symbol, or a non PC relative reloc
5616              against a local symbol, then we need to copy the reloc
5617              into the shared library.  However, if we are linking with
5618              -Bsymbolic, we do not need to copy a reloc against a
5619              global symbol which is defined in an object we are
5620              including in the link (i.e., DEF_REGULAR is set).  At
5621              this point we have not seen all the input files, so it is
5622              possible that DEF_REGULAR is not set now but will be set
5623              later (it is never cleared).  In case of a weak definition,
5624              DEF_REGULAR may be cleared later by a strong definition in
5625              a shared library.  We account for that possibility below by
5626              storing information in the dyn_relocs field of the hash
5627              table entry.  A similar situation occurs when creating
5628              shared libraries and symbol visibility changes render the
5629              symbol local.
5630
5631              If on the other hand, we are creating an executable, we
5632              may need to keep relocations for symbols satisfied by a
5633              dynamic library if we manage to avoid copy relocs for the
5634              symbol.  */
5635         dodyn:
5636           if ((info->shared
5637                && (must_be_dyn_reloc (info, r_type)
5638                    || (h != NULL
5639                        && (!SYMBOLIC_BIND (info, h)
5640                            || h->root.type == bfd_link_hash_defweak
5641                            || !h->def_regular))))
5642               || (ELIMINATE_COPY_RELOCS
5643                   && !info->shared
5644                   && h != NULL
5645                   && (h->root.type == bfd_link_hash_defweak
5646                       || !h->def_regular))
5647               || (!info->shared
5648                   && ifunc != NULL))
5649             {
5650               /* We must copy these reloc types into the output file.
5651                  Create a reloc section in dynobj and make room for
5652                  this reloc.  */
5653               if (sreloc == NULL)
5654                 {
5655                   sreloc = _bfd_elf_make_dynamic_reloc_section
5656                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5657
5658                   if (sreloc == NULL)
5659                     return FALSE;
5660                 }
5661
5662               /* If this is a global symbol, we count the number of
5663                  relocations we need for this symbol.  */
5664               if (h != NULL)
5665                 {
5666                   struct elf_dyn_relocs *p;
5667                   struct elf_dyn_relocs **head;
5668
5669                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5670                   p = *head;
5671                   if (p == NULL || p->sec != sec)
5672                     {
5673                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5674                       if (p == NULL)
5675                         return FALSE;
5676                       p->next = *head;
5677                       *head = p;
5678                       p->sec = sec;
5679                       p->count = 0;
5680                       p->pc_count = 0;
5681                     }
5682                   p->count += 1;
5683                   if (!must_be_dyn_reloc (info, r_type))
5684                     p->pc_count += 1;
5685                 }
5686               else
5687                 {
5688                   /* Track dynamic relocs needed for local syms too.
5689                      We really need local syms available to do this
5690                      easily.  Oh well.  */
5691                   struct ppc_dyn_relocs *p;
5692                   struct ppc_dyn_relocs **head;
5693                   bfd_boolean is_ifunc;
5694                   asection *s;
5695                   void *vpp;
5696                   Elf_Internal_Sym *isym;
5697
5698                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5699                                                 abfd, r_symndx);
5700                   if (isym == NULL)
5701                     return FALSE;
5702
5703                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5704                   if (s == NULL)
5705                     s = sec;
5706
5707                   vpp = &elf_section_data (s)->local_dynrel;
5708                   head = (struct ppc_dyn_relocs **) vpp;
5709                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5710                   p = *head;
5711                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5712                     p = p->next;
5713                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5714                     {
5715                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5716                       if (p == NULL)
5717                         return FALSE;
5718                       p->next = *head;
5719                       *head = p;
5720                       p->sec = sec;
5721                       p->ifunc = is_ifunc;
5722                       p->count = 0;
5723                     }
5724                   p->count += 1;
5725                 }
5726             }
5727           break;
5728
5729         default:
5730           break;
5731         }
5732     }
5733
5734   return TRUE;
5735 }
5736
5737 /* Merge backend specific data from an object file to the output
5738    object file when linking.  */
5739
5740 static bfd_boolean
5741 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5742 {
5743   unsigned long iflags, oflags;
5744
5745   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5746     return TRUE;
5747
5748   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5749     return TRUE;
5750
5751   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5752     return FALSE;
5753
5754   iflags = elf_elfheader (ibfd)->e_flags;
5755   oflags = elf_elfheader (obfd)->e_flags;
5756
5757   if (!elf_flags_init (obfd) || oflags == 0)
5758     {
5759       elf_flags_init (obfd) = TRUE;
5760       elf_elfheader (obfd)->e_flags = iflags;
5761     }
5762   else if (iflags == oflags || iflags == 0)
5763     ;
5764   else if (iflags & ~EF_PPC64_ABI)
5765     {
5766       (*_bfd_error_handler)
5767         (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
5768       bfd_set_error (bfd_error_bad_value);
5769       return FALSE;
5770     }
5771   else
5772     {
5773       (*_bfd_error_handler)
5774         (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
5775          ibfd, iflags, oflags);
5776       bfd_set_error (bfd_error_bad_value);
5777       return FALSE;
5778     }
5779
5780   /* Merge Tag_compatibility attributes and any common GNU ones.  */
5781   _bfd_elf_merge_object_attributes (ibfd, obfd);
5782
5783   return TRUE;
5784 }
5785
5786 static bfd_boolean
5787 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5788 {
5789   /* Print normal ELF private data.  */
5790   _bfd_elf_print_private_bfd_data (abfd, ptr);
5791
5792   if (elf_elfheader (abfd)->e_flags != 0)
5793     {
5794       FILE *file = ptr;
5795
5796       /* xgettext:c-format */
5797       fprintf (file, _("private flags = 0x%lx:"),
5798                elf_elfheader (abfd)->e_flags);
5799
5800       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5801         fprintf (file, _(" [abiv%ld]"),
5802                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5803       fputc ('\n', file);
5804     }
5805
5806   return TRUE;
5807 }
5808
5809 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
5810    of the code entry point, and its section.  */
5811
5812 static bfd_vma
5813 opd_entry_value (asection *opd_sec,
5814                  bfd_vma offset,
5815                  asection **code_sec,
5816                  bfd_vma *code_off,
5817                  bfd_boolean in_code_sec)
5818 {
5819   bfd *opd_bfd = opd_sec->owner;
5820   Elf_Internal_Rela *relocs;
5821   Elf_Internal_Rela *lo, *hi, *look;
5822   bfd_vma val;
5823
5824   /* No relocs implies we are linking a --just-symbols object, or looking
5825      at a final linked executable with addr2line or somesuch.  */
5826   if (opd_sec->reloc_count == 0)
5827     {
5828       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5829
5830       if (contents == NULL)
5831         {
5832           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5833             return (bfd_vma) -1;
5834           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5835         }
5836
5837       val = bfd_get_64 (opd_bfd, contents + offset);
5838       if (code_sec != NULL)
5839         {
5840           asection *sec, *likely = NULL;
5841
5842           if (in_code_sec)
5843             {
5844               sec = *code_sec;
5845               if (sec->vma <= val
5846                   && val < sec->vma + sec->size)
5847                 likely = sec;
5848               else
5849                 val = -1;
5850             }
5851           else
5852             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5853               if (sec->vma <= val
5854                   && (sec->flags & SEC_LOAD) != 0
5855                   && (sec->flags & SEC_ALLOC) != 0)
5856                 likely = sec;
5857           if (likely != NULL)
5858             {
5859               *code_sec = likely;
5860               if (code_off != NULL)
5861                 *code_off = val - likely->vma;
5862             }
5863         }
5864       return val;
5865     }
5866
5867   BFD_ASSERT (is_ppc64_elf (opd_bfd));
5868
5869   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5870   if (relocs == NULL)
5871     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5872
5873   /* Go find the opd reloc at the sym address.  */
5874   lo = relocs;
5875   BFD_ASSERT (lo != NULL);
5876   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5877   val = (bfd_vma) -1;
5878   while (lo < hi)
5879     {
5880       look = lo + (hi - lo) / 2;
5881       if (look->r_offset < offset)
5882         lo = look + 1;
5883       else if (look->r_offset > offset)
5884         hi = look;
5885       else
5886         {
5887           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5888
5889           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5890               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5891             {
5892               unsigned long symndx = ELF64_R_SYM (look->r_info);
5893               asection *sec;
5894
5895               if (symndx < symtab_hdr->sh_info
5896                   || elf_sym_hashes (opd_bfd) == NULL)
5897                 {
5898                   Elf_Internal_Sym *sym;
5899
5900                   sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5901                   if (sym == NULL)
5902                     {
5903                       size_t symcnt = symtab_hdr->sh_info;
5904                       if (elf_sym_hashes (opd_bfd) == NULL)
5905                         symcnt = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
5906                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, symcnt,
5907                                                   0, NULL, NULL, NULL);
5908                       if (sym == NULL)
5909                         break;
5910                       symtab_hdr->contents = (bfd_byte *) sym;
5911                     }
5912
5913                   sym += symndx;
5914                   val = sym->st_value;
5915                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5916                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5917                 }
5918               else
5919                 {
5920                   struct elf_link_hash_entry **sym_hashes;
5921                   struct elf_link_hash_entry *rh;
5922
5923                   sym_hashes = elf_sym_hashes (opd_bfd);
5924                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
5925                   if (rh != NULL)
5926                     {
5927                       rh = elf_follow_link (rh);
5928                       BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5929                                   || rh->root.type == bfd_link_hash_defweak);
5930                       val = rh->root.u.def.value;
5931                       sec = rh->root.u.def.section;
5932                     }
5933                   else
5934                     {
5935                       /* Handle the odd case where we can be called
5936                          during bfd_elf_link_add_symbols before the
5937                          symbol hashes have been fully populated.  */
5938                       Elf_Internal_Sym *sym;
5939
5940                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, 1,
5941                                                   symndx, NULL, NULL, NULL);
5942                       if (sym == NULL)
5943                         break;
5944
5945                       val = sym->st_value;
5946                       sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5947                       free (sym);
5948                     }
5949                 }
5950               val += look->r_addend;
5951               if (code_off != NULL)
5952                 *code_off = val;
5953               if (code_sec != NULL)
5954                 {
5955                   if (in_code_sec && *code_sec != sec)
5956                     return -1;
5957                   else
5958                     *code_sec = sec;
5959                 }
5960               if (sec != NULL && sec->output_section != NULL)
5961                 val += sec->output_section->vma + sec->output_offset;
5962             }
5963           break;
5964         }
5965     }
5966
5967   return val;
5968 }
5969
5970 /* If the ELF symbol SYM might be a function in SEC, return the
5971    function size and set *CODE_OFF to the function's entry point,
5972    otherwise return zero.  */
5973
5974 static bfd_size_type
5975 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5976                               bfd_vma *code_off)
5977 {
5978   bfd_size_type size;
5979
5980   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5981                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5982     return 0;
5983
5984   size = 0;
5985   if (!(sym->flags & BSF_SYNTHETIC))
5986     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5987
5988   if (strcmp (sym->section->name, ".opd") == 0)
5989     {
5990       if (opd_entry_value (sym->section, sym->value,
5991                            &sec, code_off, TRUE) == (bfd_vma) -1)
5992         return 0;
5993       /* An old ABI binary with dot-syms has a size of 24 on the .opd
5994          symbol.  This size has nothing to do with the code size of the
5995          function, which is what we're supposed to return, but the
5996          code size isn't available without looking up the dot-sym.
5997          However, doing that would be a waste of time particularly
5998          since elf_find_function will look at the dot-sym anyway.
5999          Now, elf_find_function will keep the largest size of any
6000          function sym found at the code address of interest, so return
6001          1 here to avoid it incorrectly caching a larger function size
6002          for a small function.  This does mean we return the wrong
6003          size for a new-ABI function of size 24, but all that does is
6004          disable caching for such functions.  */
6005       if (size == 24)
6006         size = 1;
6007     }
6008   else
6009     {
6010       if (sym->section != sec)
6011         return 0;
6012       *code_off = sym->value;
6013     }
6014   if (size == 0)
6015     size = 1;
6016   return size;
6017 }
6018
6019 /* Return true if symbol is defined in a regular object file.  */
6020
6021 static bfd_boolean
6022 is_static_defined (struct elf_link_hash_entry *h)
6023 {
6024   return ((h->root.type == bfd_link_hash_defined
6025            || h->root.type == bfd_link_hash_defweak)
6026           && h->root.u.def.section != NULL
6027           && h->root.u.def.section->output_section != NULL);
6028 }
6029
6030 /* If FDH is a function descriptor symbol, return the associated code
6031    entry symbol if it is defined.  Return NULL otherwise.  */
6032
6033 static struct ppc_link_hash_entry *
6034 defined_code_entry (struct ppc_link_hash_entry *fdh)
6035 {
6036   if (fdh->is_func_descriptor)
6037     {
6038       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6039       if (fh->elf.root.type == bfd_link_hash_defined
6040           || fh->elf.root.type == bfd_link_hash_defweak)
6041         return fh;
6042     }
6043   return NULL;
6044 }
6045
6046 /* If FH is a function code entry symbol, return the associated
6047    function descriptor symbol if it is defined.  Return NULL otherwise.  */
6048
6049 static struct ppc_link_hash_entry *
6050 defined_func_desc (struct ppc_link_hash_entry *fh)
6051 {
6052   if (fh->oh != NULL
6053       && fh->oh->is_func_descriptor)
6054     {
6055       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6056       if (fdh->elf.root.type == bfd_link_hash_defined
6057           || fdh->elf.root.type == bfd_link_hash_defweak)
6058         return fdh;
6059     }
6060   return NULL;
6061 }
6062
6063 /* Mark all our entry sym sections, both opd and code section.  */
6064
6065 static void
6066 ppc64_elf_gc_keep (struct bfd_link_info *info)
6067 {
6068   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6069   struct bfd_sym_chain *sym;
6070
6071   if (htab == NULL)
6072     return;
6073
6074   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6075     {
6076       struct ppc_link_hash_entry *eh, *fh;
6077       asection *sec;
6078
6079       eh = (struct ppc_link_hash_entry *)
6080         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6081       if (eh == NULL)
6082         continue;
6083       if (eh->elf.root.type != bfd_link_hash_defined
6084           && eh->elf.root.type != bfd_link_hash_defweak)
6085         continue;
6086
6087       fh = defined_code_entry (eh);
6088       if (fh != NULL)
6089         {
6090           sec = fh->elf.root.u.def.section;
6091           sec->flags |= SEC_KEEP;
6092         }
6093       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6094                && opd_entry_value (eh->elf.root.u.def.section,
6095                                    eh->elf.root.u.def.value,
6096                                    &sec, NULL, FALSE) != (bfd_vma) -1)
6097         sec->flags |= SEC_KEEP;
6098
6099       sec = eh->elf.root.u.def.section;
6100       sec->flags |= SEC_KEEP;
6101     }
6102 }
6103
6104 /* Mark sections containing dynamically referenced symbols.  When
6105    building shared libraries, we must assume that any visible symbol is
6106    referenced.  */
6107
6108 static bfd_boolean
6109 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6110 {
6111   struct bfd_link_info *info = (struct bfd_link_info *) inf;
6112   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6113   struct ppc_link_hash_entry *fdh;
6114
6115   /* Dynamic linking info is on the func descriptor sym.  */
6116   fdh = defined_func_desc (eh);
6117   if (fdh != NULL)
6118     eh = fdh;
6119
6120   if ((eh->elf.root.type == bfd_link_hash_defined
6121        || eh->elf.root.type == bfd_link_hash_defweak)
6122       && (eh->elf.ref_dynamic
6123           || (!info->executable
6124               && eh->elf.def_regular
6125               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6126               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6127               && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6128                   || !bfd_hide_sym_by_version (info->version_info,
6129                                                eh->elf.root.root.string)))))
6130     {
6131       asection *code_sec;
6132       struct ppc_link_hash_entry *fh;
6133
6134       eh->elf.root.u.def.section->flags |= SEC_KEEP;
6135
6136       /* Function descriptor syms cause the associated
6137          function code sym section to be marked.  */
6138       fh = defined_code_entry (eh);
6139       if (fh != NULL)
6140         {
6141           code_sec = fh->elf.root.u.def.section;
6142           code_sec->flags |= SEC_KEEP;
6143         }
6144       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6145                && opd_entry_value (eh->elf.root.u.def.section,
6146                                    eh->elf.root.u.def.value,
6147                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
6148         code_sec->flags |= SEC_KEEP;
6149     }
6150
6151   return TRUE;
6152 }
6153
6154 /* Return the section that should be marked against GC for a given
6155    relocation.  */
6156
6157 static asection *
6158 ppc64_elf_gc_mark_hook (asection *sec,
6159                         struct bfd_link_info *info,
6160                         Elf_Internal_Rela *rel,
6161                         struct elf_link_hash_entry *h,
6162                         Elf_Internal_Sym *sym)
6163 {
6164   asection *rsec;
6165
6166   /* Syms return NULL if we're marking .opd, so we avoid marking all
6167      function sections, as all functions are referenced in .opd.  */
6168   rsec = NULL;
6169   if (get_opd_info (sec) != NULL)
6170     return rsec;
6171
6172   if (h != NULL)
6173     {
6174       enum elf_ppc64_reloc_type r_type;
6175       struct ppc_link_hash_entry *eh, *fh, *fdh;
6176
6177       r_type = ELF64_R_TYPE (rel->r_info);
6178       switch (r_type)
6179         {
6180         case R_PPC64_GNU_VTINHERIT:
6181         case R_PPC64_GNU_VTENTRY:
6182           break;
6183
6184         default:
6185           switch (h->root.type)
6186             {
6187             case bfd_link_hash_defined:
6188             case bfd_link_hash_defweak:
6189               eh = (struct ppc_link_hash_entry *) h;
6190               fdh = defined_func_desc (eh);
6191               if (fdh != NULL)
6192                 eh = fdh;
6193
6194               /* Function descriptor syms cause the associated
6195                  function code sym section to be marked.  */
6196               fh = defined_code_entry (eh);
6197               if (fh != NULL)
6198                 {
6199                   /* They also mark their opd section.  */
6200                   eh->elf.root.u.def.section->gc_mark = 1;
6201
6202                   rsec = fh->elf.root.u.def.section;
6203                 }
6204               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6205                        && opd_entry_value (eh->elf.root.u.def.section,
6206                                            eh->elf.root.u.def.value,
6207                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
6208                 eh->elf.root.u.def.section->gc_mark = 1;
6209               else
6210                 rsec = h->root.u.def.section;
6211               break;
6212
6213             case bfd_link_hash_common:
6214               rsec = h->root.u.c.p->section;
6215               break;
6216
6217             default:
6218               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6219             }
6220         }
6221     }
6222   else
6223     {
6224       struct _opd_sec_data *opd;
6225
6226       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6227       opd = get_opd_info (rsec);
6228       if (opd != NULL && opd->func_sec != NULL)
6229         {
6230           rsec->gc_mark = 1;
6231
6232           rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
6233         }
6234     }
6235
6236   return rsec;
6237 }
6238
6239 /* Update the .got, .plt. and dynamic reloc reference counts for the
6240    section being removed.  */
6241
6242 static bfd_boolean
6243 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6244                          asection *sec, const Elf_Internal_Rela *relocs)
6245 {
6246   struct ppc_link_hash_table *htab;
6247   Elf_Internal_Shdr *symtab_hdr;
6248   struct elf_link_hash_entry **sym_hashes;
6249   struct got_entry **local_got_ents;
6250   const Elf_Internal_Rela *rel, *relend;
6251
6252   if (info->relocatable)
6253     return TRUE;
6254
6255   if ((sec->flags & SEC_ALLOC) == 0)
6256     return TRUE;
6257
6258   elf_section_data (sec)->local_dynrel = NULL;
6259
6260   htab = ppc_hash_table (info);
6261   if (htab == NULL)
6262     return FALSE;
6263
6264   symtab_hdr = &elf_symtab_hdr (abfd);
6265   sym_hashes = elf_sym_hashes (abfd);
6266   local_got_ents = elf_local_got_ents (abfd);
6267
6268   relend = relocs + sec->reloc_count;
6269   for (rel = relocs; rel < relend; rel++)
6270     {
6271       unsigned long r_symndx;
6272       enum elf_ppc64_reloc_type r_type;
6273       struct elf_link_hash_entry *h = NULL;
6274       unsigned char tls_type = 0;
6275
6276       r_symndx = ELF64_R_SYM (rel->r_info);
6277       r_type = ELF64_R_TYPE (rel->r_info);
6278       if (r_symndx >= symtab_hdr->sh_info)
6279         {
6280           struct ppc_link_hash_entry *eh;
6281           struct elf_dyn_relocs **pp;
6282           struct elf_dyn_relocs *p;
6283
6284           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6285           h = elf_follow_link (h);
6286           eh = (struct ppc_link_hash_entry *) h;
6287
6288           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6289             if (p->sec == sec)
6290               {
6291                 /* Everything must go for SEC.  */
6292                 *pp = p->next;
6293                 break;
6294               }
6295         }
6296
6297       if (is_branch_reloc (r_type))
6298         {
6299           struct plt_entry **ifunc = NULL;
6300           if (h != NULL)
6301             {
6302               if (h->type == STT_GNU_IFUNC)
6303                 ifunc = &h->plt.plist;
6304             }
6305           else if (local_got_ents != NULL)
6306             {
6307               struct plt_entry **local_plt = (struct plt_entry **)
6308                 (local_got_ents + symtab_hdr->sh_info);
6309               unsigned char *local_got_tls_masks = (unsigned char *)
6310                 (local_plt + symtab_hdr->sh_info);
6311               if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6312                 ifunc = local_plt + r_symndx;
6313             }
6314           if (ifunc != NULL)
6315             {
6316               struct plt_entry *ent;
6317
6318               for (ent = *ifunc; ent != NULL; ent = ent->next)
6319                 if (ent->addend == rel->r_addend)
6320                   break;
6321               if (ent == NULL)
6322                 abort ();
6323               if (ent->plt.refcount > 0)
6324                 ent->plt.refcount -= 1;
6325               continue;
6326             }
6327         }
6328
6329       switch (r_type)
6330         {
6331         case R_PPC64_GOT_TLSLD16:
6332         case R_PPC64_GOT_TLSLD16_LO:
6333         case R_PPC64_GOT_TLSLD16_HI:
6334         case R_PPC64_GOT_TLSLD16_HA:
6335           tls_type = TLS_TLS | TLS_LD;
6336           goto dogot;
6337
6338         case R_PPC64_GOT_TLSGD16:
6339         case R_PPC64_GOT_TLSGD16_LO:
6340         case R_PPC64_GOT_TLSGD16_HI:
6341         case R_PPC64_GOT_TLSGD16_HA:
6342           tls_type = TLS_TLS | TLS_GD;
6343           goto dogot;
6344
6345         case R_PPC64_GOT_TPREL16_DS:
6346         case R_PPC64_GOT_TPREL16_LO_DS:
6347         case R_PPC64_GOT_TPREL16_HI:
6348         case R_PPC64_GOT_TPREL16_HA:
6349           tls_type = TLS_TLS | TLS_TPREL;
6350           goto dogot;
6351
6352         case R_PPC64_GOT_DTPREL16_DS:
6353         case R_PPC64_GOT_DTPREL16_LO_DS:
6354         case R_PPC64_GOT_DTPREL16_HI:
6355         case R_PPC64_GOT_DTPREL16_HA:
6356           tls_type = TLS_TLS | TLS_DTPREL;
6357           goto dogot;
6358
6359         case R_PPC64_GOT16:
6360         case R_PPC64_GOT16_DS:
6361         case R_PPC64_GOT16_HA:
6362         case R_PPC64_GOT16_HI:
6363         case R_PPC64_GOT16_LO:
6364         case R_PPC64_GOT16_LO_DS:
6365         dogot:
6366           {
6367             struct got_entry *ent;
6368
6369             if (h != NULL)
6370               ent = h->got.glist;
6371             else
6372               ent = local_got_ents[r_symndx];
6373
6374             for (; ent != NULL; ent = ent->next)
6375               if (ent->addend == rel->r_addend
6376                   && ent->owner == abfd
6377                   && ent->tls_type == tls_type)
6378                 break;
6379             if (ent == NULL)
6380               abort ();
6381             if (ent->got.refcount > 0)
6382               ent->got.refcount -= 1;
6383           }
6384           break;
6385
6386         case R_PPC64_PLT16_HA:
6387         case R_PPC64_PLT16_HI:
6388         case R_PPC64_PLT16_LO:
6389         case R_PPC64_PLT32:
6390         case R_PPC64_PLT64:
6391         case R_PPC64_REL14:
6392         case R_PPC64_REL14_BRNTAKEN:
6393         case R_PPC64_REL14_BRTAKEN:
6394         case R_PPC64_REL24:
6395           if (h != NULL)
6396             {
6397               struct plt_entry *ent;
6398
6399               for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6400                 if (ent->addend == rel->r_addend)
6401                   break;
6402               if (ent != NULL && ent->plt.refcount > 0)
6403                 ent->plt.refcount -= 1;
6404             }
6405           break;
6406
6407         default:
6408           break;
6409         }
6410     }
6411   return TRUE;
6412 }
6413
6414 /* The maximum size of .sfpr.  */
6415 #define SFPR_MAX (218*4)
6416
6417 struct sfpr_def_parms
6418 {
6419   const char name[12];
6420   unsigned char lo, hi;
6421   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6422   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6423 };
6424
6425 /* Auto-generate _save*, _rest* functions in .sfpr.  */
6426
6427 static bfd_boolean
6428 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6429 {
6430   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6431   unsigned int i;
6432   size_t len = strlen (parm->name);
6433   bfd_boolean writing = FALSE;
6434   char sym[16];
6435
6436   if (htab == NULL)
6437     return FALSE;
6438
6439   memcpy (sym, parm->name, len);
6440   sym[len + 2] = 0;
6441
6442   for (i = parm->lo; i <= parm->hi; i++)
6443     {
6444       struct elf_link_hash_entry *h;
6445
6446       sym[len + 0] = i / 10 + '0';
6447       sym[len + 1] = i % 10 + '0';
6448       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6449       if (h != NULL
6450           && !h->def_regular)
6451         {
6452           h->root.type = bfd_link_hash_defined;
6453           h->root.u.def.section = htab->sfpr;
6454           h->root.u.def.value = htab->sfpr->size;
6455           h->type = STT_FUNC;
6456           h->def_regular = 1;
6457           _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6458           writing = TRUE;
6459           if (htab->sfpr->contents == NULL)
6460             {
6461               htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6462               if (htab->sfpr->contents == NULL)
6463                 return FALSE;
6464             }
6465         }
6466       if (writing)
6467         {
6468           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6469           if (i != parm->hi)
6470             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6471           else
6472             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6473           htab->sfpr->size = p - htab->sfpr->contents;
6474         }
6475     }
6476
6477   return TRUE;
6478 }
6479
6480 static bfd_byte *
6481 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6482 {
6483   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6484   return p + 4;
6485 }
6486
6487 static bfd_byte *
6488 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6489 {
6490   p = savegpr0 (abfd, p, r);
6491   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6492   p = p + 4;
6493   bfd_put_32 (abfd, BLR, p);
6494   return p + 4;
6495 }
6496
6497 static bfd_byte *
6498 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6499 {
6500   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6501   return p + 4;
6502 }
6503
6504 static bfd_byte *
6505 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6506 {
6507   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6508   p = p + 4;
6509   p = restgpr0 (abfd, p, r);
6510   bfd_put_32 (abfd, MTLR_R0, p);
6511   p = p + 4;
6512   if (r == 29)
6513     {
6514       p = restgpr0 (abfd, p, 30);
6515       p = restgpr0 (abfd, p, 31);
6516     }
6517   bfd_put_32 (abfd, BLR, p);
6518   return p + 4;
6519 }
6520
6521 static bfd_byte *
6522 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6523 {
6524   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6525   return p + 4;
6526 }
6527
6528 static bfd_byte *
6529 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6530 {
6531   p = savegpr1 (abfd, p, r);
6532   bfd_put_32 (abfd, BLR, p);
6533   return p + 4;
6534 }
6535
6536 static bfd_byte *
6537 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6538 {
6539   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6540   return p + 4;
6541 }
6542
6543 static bfd_byte *
6544 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6545 {
6546   p = restgpr1 (abfd, p, r);
6547   bfd_put_32 (abfd, BLR, p);
6548   return p + 4;
6549 }
6550
6551 static bfd_byte *
6552 savefpr (bfd *abfd, bfd_byte *p, int r)
6553 {
6554   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6555   return p + 4;
6556 }
6557
6558 static bfd_byte *
6559 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6560 {
6561   p = savefpr (abfd, p, r);
6562   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6563   p = p + 4;
6564   bfd_put_32 (abfd, BLR, p);
6565   return p + 4;
6566 }
6567
6568 static bfd_byte *
6569 restfpr (bfd *abfd, bfd_byte *p, int r)
6570 {
6571   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6572   return p + 4;
6573 }
6574
6575 static bfd_byte *
6576 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6577 {
6578   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6579   p = p + 4;
6580   p = restfpr (abfd, p, r);
6581   bfd_put_32 (abfd, MTLR_R0, p);
6582   p = p + 4;
6583   if (r == 29)
6584     {
6585       p = restfpr (abfd, p, 30);
6586       p = restfpr (abfd, p, 31);
6587     }
6588   bfd_put_32 (abfd, BLR, p);
6589   return p + 4;
6590 }
6591
6592 static bfd_byte *
6593 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6594 {
6595   p = savefpr (abfd, p, r);
6596   bfd_put_32 (abfd, BLR, p);
6597   return p + 4;
6598 }
6599
6600 static bfd_byte *
6601 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6602 {
6603   p = restfpr (abfd, p, r);
6604   bfd_put_32 (abfd, BLR, p);
6605   return p + 4;
6606 }
6607
6608 static bfd_byte *
6609 savevr (bfd *abfd, bfd_byte *p, int r)
6610 {
6611   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6612   p = p + 4;
6613   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6614   return p + 4;
6615 }
6616
6617 static bfd_byte *
6618 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6619 {
6620   p = savevr (abfd, p, r);
6621   bfd_put_32 (abfd, BLR, p);
6622   return p + 4;
6623 }
6624
6625 static bfd_byte *
6626 restvr (bfd *abfd, bfd_byte *p, int r)
6627 {
6628   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6629   p = p + 4;
6630   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6631   return p + 4;
6632 }
6633
6634 static bfd_byte *
6635 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6636 {
6637   p = restvr (abfd, p, r);
6638   bfd_put_32 (abfd, BLR, p);
6639   return p + 4;
6640 }
6641
6642 /* Called via elf_link_hash_traverse to transfer dynamic linking
6643    information on function code symbol entries to their corresponding
6644    function descriptor symbol entries.  */
6645
6646 static bfd_boolean
6647 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6648 {
6649   struct bfd_link_info *info;
6650   struct ppc_link_hash_table *htab;
6651   struct plt_entry *ent;
6652   struct ppc_link_hash_entry *fh;
6653   struct ppc_link_hash_entry *fdh;
6654   bfd_boolean force_local;
6655
6656   fh = (struct ppc_link_hash_entry *) h;
6657   if (fh->elf.root.type == bfd_link_hash_indirect)
6658     return TRUE;
6659
6660   info = inf;
6661   htab = ppc_hash_table (info);
6662   if (htab == NULL)
6663     return FALSE;
6664
6665   /* Resolve undefined references to dot-symbols as the value
6666      in the function descriptor, if we have one in a regular object.
6667      This is to satisfy cases like ".quad .foo".  Calls to functions
6668      in dynamic objects are handled elsewhere.  */
6669   if (fh->elf.root.type == bfd_link_hash_undefweak
6670       && fh->was_undefined
6671       && (fdh = defined_func_desc (fh)) != NULL
6672       && get_opd_info (fdh->elf.root.u.def.section) != NULL
6673       && opd_entry_value (fdh->elf.root.u.def.section,
6674                           fdh->elf.root.u.def.value,
6675                           &fh->elf.root.u.def.section,
6676                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6677     {
6678       fh->elf.root.type = fdh->elf.root.type;
6679       fh->elf.forced_local = 1;
6680       fh->elf.def_regular = fdh->elf.def_regular;
6681       fh->elf.def_dynamic = fdh->elf.def_dynamic;
6682     }
6683
6684   /* If this is a function code symbol, transfer dynamic linking
6685      information to the function descriptor symbol.  */
6686   if (!fh->is_func)
6687     return TRUE;
6688
6689   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6690     if (ent->plt.refcount > 0)
6691       break;
6692   if (ent == NULL
6693       || fh->elf.root.root.string[0] != '.'
6694       || fh->elf.root.root.string[1] == '\0')
6695     return TRUE;
6696
6697   /* Find the corresponding function descriptor symbol.  Create it
6698      as undefined if necessary.  */
6699
6700   fdh = lookup_fdh (fh, htab);
6701   if (fdh == NULL
6702       && !info->executable
6703       && (fh->elf.root.type == bfd_link_hash_undefined
6704           || fh->elf.root.type == bfd_link_hash_undefweak))
6705     {
6706       fdh = make_fdh (info, fh);
6707       if (fdh == NULL)
6708         return FALSE;
6709     }
6710
6711   /* Fake function descriptors are made undefweak.  If the function
6712      code symbol is strong undefined, make the fake sym the same.
6713      If the function code symbol is defined, then force the fake
6714      descriptor local;  We can't support overriding of symbols in a
6715      shared library on a fake descriptor.  */
6716
6717   if (fdh != NULL
6718       && fdh->fake
6719       && fdh->elf.root.type == bfd_link_hash_undefweak)
6720     {
6721       if (fh->elf.root.type == bfd_link_hash_undefined)
6722         {
6723           fdh->elf.root.type = bfd_link_hash_undefined;
6724           bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6725         }
6726       else if (fh->elf.root.type == bfd_link_hash_defined
6727                || fh->elf.root.type == bfd_link_hash_defweak)
6728         {
6729           _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6730         }
6731     }
6732
6733   if (fdh != NULL
6734       && !fdh->elf.forced_local
6735       && (!info->executable
6736           || fdh->elf.def_dynamic
6737           || fdh->elf.ref_dynamic
6738           || (fdh->elf.root.type == bfd_link_hash_undefweak
6739               && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6740     {
6741       if (fdh->elf.dynindx == -1)
6742         if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6743           return FALSE;
6744       fdh->elf.ref_regular |= fh->elf.ref_regular;
6745       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6746       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6747       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6748       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6749         {
6750           move_plt_plist (fh, fdh);
6751           fdh->elf.needs_plt = 1;
6752         }
6753       fdh->is_func_descriptor = 1;
6754       fdh->oh = fh;
6755       fh->oh = fdh;
6756     }
6757
6758   /* Now that the info is on the function descriptor, clear the
6759      function code sym info.  Any function code syms for which we
6760      don't have a definition in a regular file, we force local.
6761      This prevents a shared library from exporting syms that have
6762      been imported from another library.  Function code syms that
6763      are really in the library we must leave global to prevent the
6764      linker dragging in a definition from a static library.  */
6765   force_local = (!fh->elf.def_regular
6766                  || fdh == NULL
6767                  || !fdh->elf.def_regular
6768                  || fdh->elf.forced_local);
6769   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6770
6771   return TRUE;
6772 }
6773
6774 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
6775    this hook to a) provide some gcc support functions, and b) transfer
6776    dynamic linking information gathered so far on function code symbol
6777    entries, to their corresponding function descriptor symbol entries.  */
6778
6779 static bfd_boolean
6780 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6781                             struct bfd_link_info *info)
6782 {
6783   struct ppc_link_hash_table *htab;
6784   unsigned int i;
6785   static const struct sfpr_def_parms funcs[] =
6786     {
6787       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6788       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6789       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6790       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6791       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6792       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6793       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6794       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6795       { "._savef", 14, 31, savefpr, savefpr1_tail },
6796       { "._restf", 14, 31, restfpr, restfpr1_tail },
6797       { "_savevr_", 20, 31, savevr, savevr_tail },
6798       { "_restvr_", 20, 31, restvr, restvr_tail }
6799     };
6800
6801   htab = ppc_hash_table (info);
6802   if (htab == NULL)
6803     return FALSE;
6804
6805   if (!info->relocatable
6806       && htab->elf.hgot != NULL)
6807     _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6808
6809   if (htab->sfpr == NULL)
6810     /* We don't have any relocs.  */
6811     return TRUE;
6812
6813   /* Provide any missing _save* and _rest* functions.  */
6814   htab->sfpr->size = 0;
6815   if (!info->relocatable)
6816     for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6817       if (!sfpr_define (info, &funcs[i]))
6818         return FALSE;
6819
6820   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6821
6822   if (htab->sfpr->size == 0)
6823     htab->sfpr->flags |= SEC_EXCLUDE;
6824
6825   return TRUE;
6826 }
6827
6828 /* Adjust a symbol defined by a dynamic object and referenced by a
6829    regular object.  The current definition is in some section of the
6830    dynamic object, but we're not including those sections.  We have to
6831    change the definition to something the rest of the link can
6832    understand.  */
6833
6834 static bfd_boolean
6835 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6836                                  struct elf_link_hash_entry *h)
6837 {
6838   struct ppc_link_hash_table *htab;
6839   asection *s;
6840
6841   htab = ppc_hash_table (info);
6842   if (htab == NULL)
6843     return FALSE;
6844
6845   /* Deal with function syms.  */
6846   if (h->type == STT_FUNC
6847       || h->type == STT_GNU_IFUNC
6848       || h->needs_plt)
6849     {
6850       /* Clear procedure linkage table information for any symbol that
6851          won't need a .plt entry.  */
6852       struct plt_entry *ent;
6853       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6854         if (ent->plt.refcount > 0)
6855           break;
6856       if (ent == NULL
6857           || (h->type != STT_GNU_IFUNC
6858               && (SYMBOL_CALLS_LOCAL (info, h)
6859                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6860                       && h->root.type == bfd_link_hash_undefweak))))
6861         {
6862           h->plt.plist = NULL;
6863           h->needs_plt = 0;
6864         }
6865     }
6866   else
6867     h->plt.plist = NULL;
6868
6869   /* If this is a weak symbol, and there is a real definition, the
6870      processor independent code will have arranged for us to see the
6871      real definition first, and we can just use the same value.  */
6872   if (h->u.weakdef != NULL)
6873     {
6874       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6875                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
6876       h->root.u.def.section = h->u.weakdef->root.u.def.section;
6877       h->root.u.def.value = h->u.weakdef->root.u.def.value;
6878       if (ELIMINATE_COPY_RELOCS)
6879         h->non_got_ref = h->u.weakdef->non_got_ref;
6880       return TRUE;
6881     }
6882
6883   /* If we are creating a shared library, we must presume that the
6884      only references to the symbol are via the global offset table.
6885      For such cases we need not do anything here; the relocations will
6886      be handled correctly by relocate_section.  */
6887   if (info->shared)
6888     return TRUE;
6889
6890   /* If there are no references to this symbol that do not use the
6891      GOT, we don't need to generate a copy reloc.  */
6892   if (!h->non_got_ref)
6893     return TRUE;
6894
6895   /* Don't generate a copy reloc for symbols defined in the executable.  */
6896   if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6897     return TRUE;
6898
6899   if (ELIMINATE_COPY_RELOCS)
6900     {
6901       struct ppc_link_hash_entry * eh;
6902       struct elf_dyn_relocs *p;
6903
6904       eh = (struct ppc_link_hash_entry *) h;
6905       for (p = eh->dyn_relocs; p != NULL; p = p->next)
6906         {
6907           s = p->sec->output_section;
6908           if (s != NULL && (s->flags & SEC_READONLY) != 0)
6909             break;
6910         }
6911
6912       /* If we didn't find any dynamic relocs in read-only sections, then
6913          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
6914       if (p == NULL)
6915         {
6916           h->non_got_ref = 0;
6917           return TRUE;
6918         }
6919     }
6920
6921   if (h->plt.plist != NULL)
6922     {
6923       /* We should never get here, but unfortunately there are versions
6924          of gcc out there that improperly (for this ABI) put initialized
6925          function pointers, vtable refs and suchlike in read-only
6926          sections.  Allow them to proceed, but warn that this might
6927          break at runtime.  */
6928       info->callbacks->einfo
6929         (_("%P: copy reloc against `%T' requires lazy plt linking; "
6930            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6931          h->root.root.string);
6932     }
6933
6934   /* This is a reference to a symbol defined by a dynamic object which
6935      is not a function.  */
6936
6937   /* We must allocate the symbol in our .dynbss section, which will
6938      become part of the .bss section of the executable.  There will be
6939      an entry for this symbol in the .dynsym section.  The dynamic
6940      object will contain position independent code, so all references
6941      from the dynamic object to this symbol will go through the global
6942      offset table.  The dynamic linker will use the .dynsym entry to
6943      determine the address it must put in the global offset table, so
6944      both the dynamic object and the regular object will refer to the
6945      same memory location for the variable.  */
6946
6947   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6948      to copy the initial value out of the dynamic object and into the
6949      runtime process image.  We need to remember the offset into the
6950      .rela.bss section we are going to use.  */
6951   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6952     {
6953       htab->relbss->size += sizeof (Elf64_External_Rela);
6954       h->needs_copy = 1;
6955     }
6956
6957   s = htab->dynbss;
6958
6959   return _bfd_elf_adjust_dynamic_copy (h, s);
6960 }
6961
6962 /* If given a function descriptor symbol, hide both the function code
6963    sym and the descriptor.  */
6964 static void
6965 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6966                        struct elf_link_hash_entry *h,
6967                        bfd_boolean force_local)
6968 {
6969   struct ppc_link_hash_entry *eh;
6970   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6971
6972   eh = (struct ppc_link_hash_entry *) h;
6973   if (eh->is_func_descriptor)
6974     {
6975       struct ppc_link_hash_entry *fh = eh->oh;
6976
6977       if (fh == NULL)
6978         {
6979           const char *p, *q;
6980           struct ppc_link_hash_table *htab;
6981           char save;
6982
6983           /* We aren't supposed to use alloca in BFD because on
6984              systems which do not have alloca the version in libiberty
6985              calls xmalloc, which might cause the program to crash
6986              when it runs out of memory.  This function doesn't have a
6987              return status, so there's no way to gracefully return an
6988              error.  So cheat.  We know that string[-1] can be safely
6989              accessed;  It's either a string in an ELF string table,
6990              or allocated in an objalloc structure.  */
6991
6992           p = eh->elf.root.root.string - 1;
6993           save = *p;
6994           *(char *) p = '.';
6995           htab = ppc_hash_table (info);
6996           if (htab == NULL)
6997             return;
6998
6999           fh = (struct ppc_link_hash_entry *)
7000             elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7001           *(char *) p = save;
7002
7003           /* Unfortunately, if it so happens that the string we were
7004              looking for was allocated immediately before this string,
7005              then we overwrote the string terminator.  That's the only
7006              reason the lookup should fail.  */
7007           if (fh == NULL)
7008             {
7009               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7010               while (q >= eh->elf.root.root.string && *q == *p)
7011                 --q, --p;
7012               if (q < eh->elf.root.root.string && *p == '.')
7013                 fh = (struct ppc_link_hash_entry *)
7014                   elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7015             }
7016           if (fh != NULL)
7017             {
7018               eh->oh = fh;
7019               fh->oh = eh;
7020             }
7021         }
7022       if (fh != NULL)
7023         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7024     }
7025 }
7026
7027 static bfd_boolean
7028 get_sym_h (struct elf_link_hash_entry **hp,
7029            Elf_Internal_Sym **symp,
7030            asection **symsecp,
7031            unsigned char **tls_maskp,
7032            Elf_Internal_Sym **locsymsp,
7033            unsigned long r_symndx,
7034            bfd *ibfd)
7035 {
7036   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7037
7038   if (r_symndx >= symtab_hdr->sh_info)
7039     {
7040       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7041       struct elf_link_hash_entry *h;
7042
7043       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7044       h = elf_follow_link (h);
7045
7046       if (hp != NULL)
7047         *hp = h;
7048
7049       if (symp != NULL)
7050         *symp = NULL;
7051
7052       if (symsecp != NULL)
7053         {
7054           asection *symsec = NULL;
7055           if (h->root.type == bfd_link_hash_defined
7056               || h->root.type == bfd_link_hash_defweak)
7057             symsec = h->root.u.def.section;
7058           *symsecp = symsec;
7059         }
7060
7061       if (tls_maskp != NULL)
7062         {
7063           struct ppc_link_hash_entry *eh;
7064
7065           eh = (struct ppc_link_hash_entry *) h;
7066           *tls_maskp = &eh->tls_mask;
7067         }
7068     }
7069   else
7070     {
7071       Elf_Internal_Sym *sym;
7072       Elf_Internal_Sym *locsyms = *locsymsp;
7073
7074       if (locsyms == NULL)
7075         {
7076           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7077           if (locsyms == NULL)
7078             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7079                                             symtab_hdr->sh_info,
7080                                             0, NULL, NULL, NULL);
7081           if (locsyms == NULL)
7082             return FALSE;
7083           *locsymsp = locsyms;
7084         }
7085       sym = locsyms + r_symndx;
7086
7087       if (hp != NULL)
7088         *hp = NULL;
7089
7090       if (symp != NULL)
7091         *symp = sym;
7092
7093       if (symsecp != NULL)
7094         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7095
7096       if (tls_maskp != NULL)
7097         {
7098           struct got_entry **lgot_ents;
7099           unsigned char *tls_mask;
7100
7101           tls_mask = NULL;
7102           lgot_ents = elf_local_got_ents (ibfd);
7103           if (lgot_ents != NULL)
7104             {
7105               struct plt_entry **local_plt = (struct plt_entry **)
7106                 (lgot_ents + symtab_hdr->sh_info);
7107               unsigned char *lgot_masks = (unsigned char *)
7108                 (local_plt + symtab_hdr->sh_info);
7109               tls_mask = &lgot_masks[r_symndx];
7110             }
7111           *tls_maskp = tls_mask;
7112         }
7113     }
7114   return TRUE;
7115 }
7116
7117 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
7118    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7119    type suitable for optimization, and 1 otherwise.  */
7120
7121 static int
7122 get_tls_mask (unsigned char **tls_maskp,
7123               unsigned long *toc_symndx,
7124               bfd_vma *toc_addend,
7125               Elf_Internal_Sym **locsymsp,
7126               const Elf_Internal_Rela *rel,
7127               bfd *ibfd)
7128 {
7129   unsigned long r_symndx;
7130   int next_r;
7131   struct elf_link_hash_entry *h;
7132   Elf_Internal_Sym *sym;
7133   asection *sec;
7134   bfd_vma off;
7135
7136   r_symndx = ELF64_R_SYM (rel->r_info);
7137   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7138     return 0;
7139
7140   if ((*tls_maskp != NULL && **tls_maskp != 0)
7141       || sec == NULL
7142       || ppc64_elf_section_data (sec) == NULL
7143       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7144     return 1;
7145
7146   /* Look inside a TOC section too.  */
7147   if (h != NULL)
7148     {
7149       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7150       off = h->root.u.def.value;
7151     }
7152   else
7153     off = sym->st_value;
7154   off += rel->r_addend;
7155   BFD_ASSERT (off % 8 == 0);
7156   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7157   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7158   if (toc_symndx != NULL)
7159     *toc_symndx = r_symndx;
7160   if (toc_addend != NULL)
7161     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7162   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7163     return 0;
7164   if ((h == NULL || is_static_defined (h))
7165       && (next_r == -1 || next_r == -2))
7166     return 1 - next_r;
7167   return 1;
7168 }
7169
7170 /* Find (or create) an entry in the tocsave hash table.  */
7171
7172 static struct tocsave_entry *
7173 tocsave_find (struct ppc_link_hash_table *htab,
7174               enum insert_option insert,
7175               Elf_Internal_Sym **local_syms,
7176               const Elf_Internal_Rela *irela,
7177               bfd *ibfd)
7178 {
7179   unsigned long r_indx;
7180   struct elf_link_hash_entry *h;
7181   Elf_Internal_Sym *sym;
7182   struct tocsave_entry ent, *p;
7183   hashval_t hash;
7184   struct tocsave_entry **slot;
7185
7186   r_indx = ELF64_R_SYM (irela->r_info);
7187   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7188     return NULL;
7189   if (ent.sec == NULL || ent.sec->output_section == NULL)
7190     {
7191       (*_bfd_error_handler)
7192         (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7193       return NULL;
7194     }
7195
7196   if (h != NULL)
7197     ent.offset = h->root.u.def.value;
7198   else
7199     ent.offset = sym->st_value;
7200   ent.offset += irela->r_addend;
7201
7202   hash = tocsave_htab_hash (&ent);
7203   slot = ((struct tocsave_entry **)
7204           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7205   if (slot == NULL)
7206     return NULL;
7207
7208   if (*slot == NULL)
7209     {
7210       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7211       if (p == NULL)
7212         return NULL;
7213       *p = ent;
7214       *slot = p;
7215     }
7216   return *slot;
7217 }
7218
7219 /* Adjust all global syms defined in opd sections.  In gcc generated
7220    code for the old ABI, these will already have been done.  */
7221
7222 static bfd_boolean
7223 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7224 {
7225   struct ppc_link_hash_entry *eh;
7226   asection *sym_sec;
7227   struct _opd_sec_data *opd;
7228
7229   if (h->root.type == bfd_link_hash_indirect)
7230     return TRUE;
7231
7232   if (h->root.type != bfd_link_hash_defined
7233       && h->root.type != bfd_link_hash_defweak)
7234     return TRUE;
7235
7236   eh = (struct ppc_link_hash_entry *) h;
7237   if (eh->adjust_done)
7238     return TRUE;
7239
7240   sym_sec = eh->elf.root.u.def.section;
7241   opd = get_opd_info (sym_sec);
7242   if (opd != NULL && opd->adjust != NULL)
7243     {
7244       long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
7245       if (adjust == -1)
7246         {
7247           /* This entry has been deleted.  */
7248           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7249           if (dsec == NULL)
7250             {
7251               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7252                 if (discarded_section (dsec))
7253                   {
7254                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7255                     break;
7256                   }
7257             }
7258           eh->elf.root.u.def.value = 0;
7259           eh->elf.root.u.def.section = dsec;
7260         }
7261       else
7262         eh->elf.root.u.def.value += adjust;
7263       eh->adjust_done = 1;
7264     }
7265   return TRUE;
7266 }
7267
7268 /* Handles decrementing dynamic reloc counts for the reloc specified by
7269    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7270    have already been determined.  */
7271
7272 static bfd_boolean
7273 dec_dynrel_count (bfd_vma r_info,
7274                   asection *sec,
7275                   struct bfd_link_info *info,
7276                   Elf_Internal_Sym **local_syms,
7277                   struct elf_link_hash_entry *h,
7278                   Elf_Internal_Sym *sym)
7279 {
7280   enum elf_ppc64_reloc_type r_type;
7281   asection *sym_sec = NULL;
7282
7283   /* Can this reloc be dynamic?  This switch, and later tests here
7284      should be kept in sync with the code in check_relocs.  */
7285   r_type = ELF64_R_TYPE (r_info);
7286   switch (r_type)
7287     {
7288     default:
7289       return TRUE;
7290
7291     case R_PPC64_TPREL16:
7292     case R_PPC64_TPREL16_LO:
7293     case R_PPC64_TPREL16_HI:
7294     case R_PPC64_TPREL16_HA:
7295     case R_PPC64_TPREL16_DS:
7296     case R_PPC64_TPREL16_LO_DS:
7297     case R_PPC64_TPREL16_HIGH:
7298     case R_PPC64_TPREL16_HIGHA:
7299     case R_PPC64_TPREL16_HIGHER:
7300     case R_PPC64_TPREL16_HIGHERA:
7301     case R_PPC64_TPREL16_HIGHEST:
7302     case R_PPC64_TPREL16_HIGHESTA:
7303       if (!info->shared)
7304         return TRUE;
7305
7306     case R_PPC64_TPREL64:
7307     case R_PPC64_DTPMOD64:
7308     case R_PPC64_DTPREL64:
7309     case R_PPC64_ADDR64:
7310     case R_PPC64_REL30:
7311     case R_PPC64_REL32:
7312     case R_PPC64_REL64:
7313     case R_PPC64_ADDR14:
7314     case R_PPC64_ADDR14_BRNTAKEN:
7315     case R_PPC64_ADDR14_BRTAKEN:
7316     case R_PPC64_ADDR16:
7317     case R_PPC64_ADDR16_DS:
7318     case R_PPC64_ADDR16_HA:
7319     case R_PPC64_ADDR16_HI:
7320     case R_PPC64_ADDR16_HIGH:
7321     case R_PPC64_ADDR16_HIGHA:
7322     case R_PPC64_ADDR16_HIGHER:
7323     case R_PPC64_ADDR16_HIGHERA:
7324     case R_PPC64_ADDR16_HIGHEST:
7325     case R_PPC64_ADDR16_HIGHESTA:
7326     case R_PPC64_ADDR16_LO:
7327     case R_PPC64_ADDR16_LO_DS:
7328     case R_PPC64_ADDR24:
7329     case R_PPC64_ADDR32:
7330     case R_PPC64_UADDR16:
7331     case R_PPC64_UADDR32:
7332     case R_PPC64_UADDR64:
7333     case R_PPC64_TOC:
7334       break;
7335     }
7336
7337   if (local_syms != NULL)
7338     {
7339       unsigned long r_symndx;
7340       bfd *ibfd = sec->owner;
7341
7342       r_symndx = ELF64_R_SYM (r_info);
7343       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7344         return FALSE;
7345     }
7346
7347   if ((info->shared
7348        && (must_be_dyn_reloc (info, r_type)
7349            || (h != NULL
7350                && (!SYMBOLIC_BIND (info, h)
7351                    || h->root.type == bfd_link_hash_defweak
7352                    || !h->def_regular))))
7353       || (ELIMINATE_COPY_RELOCS
7354           && !info->shared
7355           && h != NULL
7356           && (h->root.type == bfd_link_hash_defweak
7357               || !h->def_regular)))
7358     ;
7359   else
7360     return TRUE;
7361
7362   if (h != NULL)
7363     {
7364       struct elf_dyn_relocs *p;
7365       struct elf_dyn_relocs **pp;
7366       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7367
7368       /* elf_gc_sweep may have already removed all dyn relocs associated
7369          with local syms for a given section.  Also, symbol flags are
7370          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7371          report a dynreloc miscount.  */
7372       if (*pp == NULL && info->gc_sections)
7373         return TRUE;
7374
7375       while ((p = *pp) != NULL)
7376         {
7377           if (p->sec == sec)
7378             {
7379               if (!must_be_dyn_reloc (info, r_type))
7380                 p->pc_count -= 1;
7381               p->count -= 1;
7382               if (p->count == 0)
7383                 *pp = p->next;
7384               return TRUE;
7385             }
7386           pp = &p->next;
7387         }
7388     }
7389   else
7390     {
7391       struct ppc_dyn_relocs *p;
7392       struct ppc_dyn_relocs **pp;
7393       void *vpp;
7394       bfd_boolean is_ifunc;
7395
7396       if (local_syms == NULL)
7397         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7398       if (sym_sec == NULL)
7399         sym_sec = sec;
7400
7401       vpp = &elf_section_data (sym_sec)->local_dynrel;
7402       pp = (struct ppc_dyn_relocs **) vpp;
7403
7404       if (*pp == NULL && info->gc_sections)
7405         return TRUE;
7406
7407       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7408       while ((p = *pp) != NULL)
7409         {
7410           if (p->sec == sec && p->ifunc == is_ifunc)
7411             {
7412               p->count -= 1;
7413               if (p->count == 0)
7414                 *pp = p->next;
7415               return TRUE;
7416             }
7417           pp = &p->next;
7418         }
7419     }
7420
7421   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7422                           sec->owner, sec);
7423   bfd_set_error (bfd_error_bad_value);
7424   return FALSE;
7425 }
7426
7427 /* Remove unused Official Procedure Descriptor entries.  Currently we
7428    only remove those associated with functions in discarded link-once
7429    sections, or weakly defined functions that have been overridden.  It
7430    would be possible to remove many more entries for statically linked
7431    applications.  */
7432
7433 bfd_boolean
7434 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
7435 {
7436   bfd *ibfd;
7437   bfd_boolean some_edited = FALSE;
7438   asection *need_pad = NULL;
7439
7440   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7441     {
7442       asection *sec;
7443       Elf_Internal_Rela *relstart, *rel, *relend;
7444       Elf_Internal_Shdr *symtab_hdr;
7445       Elf_Internal_Sym *local_syms;
7446       bfd_vma offset;
7447       struct _opd_sec_data *opd;
7448       bfd_boolean need_edit, add_aux_fields;
7449       bfd_size_type cnt_16b = 0;
7450
7451       if (!is_ppc64_elf (ibfd))
7452         continue;
7453
7454       sec = bfd_get_section_by_name (ibfd, ".opd");
7455       if (sec == NULL || sec->size == 0)
7456         continue;
7457
7458       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7459         continue;
7460
7461       if (sec->output_section == bfd_abs_section_ptr)
7462         continue;
7463
7464       /* Look through the section relocs.  */
7465       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7466         continue;
7467
7468       local_syms = NULL;
7469       symtab_hdr = &elf_symtab_hdr (ibfd);
7470
7471       /* Read the relocations.  */
7472       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7473                                             info->keep_memory);
7474       if (relstart == NULL)
7475         return FALSE;
7476
7477       /* First run through the relocs to check they are sane, and to
7478          determine whether we need to edit this opd section.  */
7479       need_edit = FALSE;
7480       need_pad = sec;
7481       offset = 0;
7482       relend = relstart + sec->reloc_count;
7483       for (rel = relstart; rel < relend; )
7484         {
7485           enum elf_ppc64_reloc_type r_type;
7486           unsigned long r_symndx;
7487           asection *sym_sec;
7488           struct elf_link_hash_entry *h;
7489           Elf_Internal_Sym *sym;
7490
7491           /* .opd contains a regular array of 16 or 24 byte entries.  We're
7492              only interested in the reloc pointing to a function entry
7493              point.  */
7494           if (rel->r_offset != offset
7495               || rel + 1 >= relend
7496               || (rel + 1)->r_offset != offset + 8)
7497             {
7498               /* If someone messes with .opd alignment then after a
7499                  "ld -r" we might have padding in the middle of .opd.
7500                  Also, there's nothing to prevent someone putting
7501                  something silly in .opd with the assembler.  No .opd
7502                  optimization for them!  */
7503             broken_opd:
7504               (*_bfd_error_handler)
7505                 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7506               need_edit = FALSE;
7507               break;
7508             }
7509
7510           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7511               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7512             {
7513               (*_bfd_error_handler)
7514                 (_("%B: unexpected reloc type %u in .opd section"),
7515                  ibfd, r_type);
7516               need_edit = FALSE;
7517               break;
7518             }
7519
7520           r_symndx = ELF64_R_SYM (rel->r_info);
7521           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7522                           r_symndx, ibfd))
7523             goto error_ret;
7524
7525           if (sym_sec == NULL || sym_sec->owner == NULL)
7526             {
7527               const char *sym_name;
7528               if (h != NULL)
7529                 sym_name = h->root.root.string;
7530               else
7531                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7532                                              sym_sec);
7533
7534               (*_bfd_error_handler)
7535                 (_("%B: undefined sym `%s' in .opd section"),
7536                  ibfd, sym_name);
7537               need_edit = FALSE;
7538               break;
7539             }
7540
7541           /* opd entries are always for functions defined in the
7542              current input bfd.  If the symbol isn't defined in the
7543              input bfd, then we won't be using the function in this
7544              bfd;  It must be defined in a linkonce section in another
7545              bfd, or is weak.  It's also possible that we are
7546              discarding the function due to a linker script /DISCARD/,
7547              which we test for via the output_section.  */
7548           if (sym_sec->owner != ibfd
7549               || sym_sec->output_section == bfd_abs_section_ptr)
7550             need_edit = TRUE;
7551
7552           rel += 2;
7553           if (rel == relend
7554               || (rel + 1 == relend && rel->r_offset == offset + 16))
7555             {
7556               if (sec->size == offset + 24)
7557                 {
7558                   need_pad = NULL;
7559                   break;
7560                 }
7561               if (rel == relend && sec->size == offset + 16)
7562                 {
7563                   cnt_16b++;
7564                   break;
7565                 }
7566               goto broken_opd;
7567             }
7568
7569           if (rel->r_offset == offset + 24)
7570             offset += 24;
7571           else if (rel->r_offset != offset + 16)
7572             goto broken_opd;
7573           else if (rel + 1 < relend
7574                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7575                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7576             {
7577               offset += 16;
7578               cnt_16b++;
7579             }
7580           else if (rel + 2 < relend
7581                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7582                    && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7583             {
7584               offset += 24;
7585               rel += 1;
7586             }
7587           else
7588             goto broken_opd;
7589         }
7590
7591       add_aux_fields = non_overlapping && cnt_16b > 0;
7592
7593       if (need_edit || add_aux_fields)
7594         {
7595           Elf_Internal_Rela *write_rel;
7596           Elf_Internal_Shdr *rel_hdr;
7597           bfd_byte *rptr, *wptr;
7598           bfd_byte *new_contents;
7599           bfd_boolean skip;
7600           long opd_ent_size;
7601           bfd_size_type amt;
7602
7603           new_contents = NULL;
7604           amt = sec->size * sizeof (long) / 8;
7605           opd = &ppc64_elf_section_data (sec)->u.opd;
7606           opd->adjust = bfd_zalloc (sec->owner, amt);
7607           if (opd->adjust == NULL)
7608             return FALSE;
7609           ppc64_elf_section_data (sec)->sec_type = sec_opd;
7610
7611           /* This seems a waste of time as input .opd sections are all
7612              zeros as generated by gcc, but I suppose there's no reason
7613              this will always be so.  We might start putting something in
7614              the third word of .opd entries.  */
7615           if ((sec->flags & SEC_IN_MEMORY) == 0)
7616             {
7617               bfd_byte *loc;
7618               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7619                 {
7620                   if (loc != NULL)
7621                     free (loc);
7622                 error_ret:
7623                   if (local_syms != NULL
7624                       && symtab_hdr->contents != (unsigned char *) local_syms)
7625                     free (local_syms);
7626                   if (elf_section_data (sec)->relocs != relstart)
7627                     free (relstart);
7628                   return FALSE;
7629                 }
7630               sec->contents = loc;
7631               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7632             }
7633
7634           elf_section_data (sec)->relocs = relstart;
7635
7636           new_contents = sec->contents;
7637           if (add_aux_fields)
7638             {
7639               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7640               if (new_contents == NULL)
7641                 return FALSE;
7642               need_pad = FALSE;
7643             }
7644           wptr = new_contents;
7645           rptr = sec->contents;
7646
7647           write_rel = relstart;
7648           skip = FALSE;
7649           offset = 0;
7650           opd_ent_size = 0;
7651           for (rel = relstart; rel < relend; rel++)
7652             {
7653               unsigned long r_symndx;
7654               asection *sym_sec;
7655               struct elf_link_hash_entry *h;
7656               Elf_Internal_Sym *sym;
7657
7658               r_symndx = ELF64_R_SYM (rel->r_info);
7659               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7660                               r_symndx, ibfd))
7661                 goto error_ret;
7662
7663               if (rel->r_offset == offset)
7664                 {
7665                   struct ppc_link_hash_entry *fdh = NULL;
7666
7667                   /* See if the .opd entry is full 24 byte or
7668                      16 byte (with fd_aux entry overlapped with next
7669                      fd_func).  */
7670                   opd_ent_size = 24;
7671                   if ((rel + 2 == relend && sec->size == offset + 16)
7672                       || (rel + 3 < relend
7673                           && rel[2].r_offset == offset + 16
7674                           && rel[3].r_offset == offset + 24
7675                           && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7676                           && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7677                     opd_ent_size = 16;
7678
7679                   if (h != NULL
7680                       && h->root.root.string[0] == '.')
7681                     {
7682                       struct ppc_link_hash_table *htab;
7683
7684                       htab = ppc_hash_table (info);
7685                       if (htab != NULL)
7686                         fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7687                                           htab);
7688                       if (fdh != NULL
7689                           && fdh->elf.root.type != bfd_link_hash_defined
7690                           && fdh->elf.root.type != bfd_link_hash_defweak)
7691                         fdh = NULL;
7692                     }
7693
7694                   skip = (sym_sec->owner != ibfd
7695                           || sym_sec->output_section == bfd_abs_section_ptr);
7696                   if (skip)
7697                     {
7698                       if (fdh != NULL && sym_sec->owner == ibfd)
7699                         {
7700                           /* Arrange for the function descriptor sym
7701                              to be dropped.  */
7702                           fdh->elf.root.u.def.value = 0;
7703                           fdh->elf.root.u.def.section = sym_sec;
7704                         }
7705                       opd->adjust[rel->r_offset / 8] = -1;
7706                     }
7707                   else
7708                     {
7709                       /* We'll be keeping this opd entry.  */
7710
7711                       if (fdh != NULL)
7712                         {
7713                           /* Redefine the function descriptor symbol to
7714                              this location in the opd section.  It is
7715                              necessary to update the value here rather
7716                              than using an array of adjustments as we do
7717                              for local symbols, because various places
7718                              in the generic ELF code use the value
7719                              stored in u.def.value.  */
7720                           fdh->elf.root.u.def.value = wptr - new_contents;
7721                           fdh->adjust_done = 1;
7722                         }
7723
7724                       /* Local syms are a bit tricky.  We could
7725                          tweak them as they can be cached, but
7726                          we'd need to look through the local syms
7727                          for the function descriptor sym which we
7728                          don't have at the moment.  So keep an
7729                          array of adjustments.  */
7730                       opd->adjust[rel->r_offset / 8]
7731                         = (wptr - new_contents) - (rptr - sec->contents);
7732
7733                       if (wptr != rptr)
7734                         memcpy (wptr, rptr, opd_ent_size);
7735                       wptr += opd_ent_size;
7736                       if (add_aux_fields && opd_ent_size == 16)
7737                         {
7738                           memset (wptr, '\0', 8);
7739                           wptr += 8;
7740                         }
7741                     }
7742                   rptr += opd_ent_size;
7743                   offset += opd_ent_size;
7744                 }
7745
7746               if (skip)
7747                 {
7748                   if (!NO_OPD_RELOCS
7749                       && !info->relocatable
7750                       && !dec_dynrel_count (rel->r_info, sec, info,
7751                                             NULL, h, sym))
7752                     goto error_ret;
7753                 }
7754               else
7755                 {
7756                   /* We need to adjust any reloc offsets to point to the
7757                      new opd entries.  While we're at it, we may as well
7758                      remove redundant relocs.  */
7759                   rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
7760                   if (write_rel != rel)
7761                     memcpy (write_rel, rel, sizeof (*rel));
7762                   ++write_rel;
7763                 }
7764             }
7765
7766           sec->size = wptr - new_contents;
7767           sec->reloc_count = write_rel - relstart;
7768           if (add_aux_fields)
7769             {
7770               free (sec->contents);
7771               sec->contents = new_contents;
7772             }
7773
7774           /* Fudge the header size too, as this is used later in
7775              elf_bfd_final_link if we are emitting relocs.  */
7776           rel_hdr = _bfd_elf_single_rel_hdr (sec);
7777           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7778           some_edited = TRUE;
7779         }
7780       else if (elf_section_data (sec)->relocs != relstart)
7781         free (relstart);
7782
7783       if (local_syms != NULL
7784           && symtab_hdr->contents != (unsigned char *) local_syms)
7785         {
7786           if (!info->keep_memory)
7787             free (local_syms);
7788           else
7789             symtab_hdr->contents = (unsigned char *) local_syms;
7790         }
7791     }
7792
7793   if (some_edited)
7794     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7795
7796   /* If we are doing a final link and the last .opd entry is just 16 byte
7797      long, add a 8 byte padding after it.  */
7798   if (need_pad != NULL && !info->relocatable)
7799     {
7800       bfd_byte *p;
7801
7802       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7803         {
7804           BFD_ASSERT (need_pad->size > 0);
7805
7806           p = bfd_malloc (need_pad->size + 8);
7807           if (p == NULL)
7808             return FALSE;
7809
7810           if (! bfd_get_section_contents (need_pad->owner, need_pad,
7811                                           p, 0, need_pad->size))
7812             return FALSE;
7813
7814           need_pad->contents = p;
7815           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7816         }
7817       else
7818         {
7819           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7820           if (p == NULL)
7821             return FALSE;
7822
7823           need_pad->contents = p;
7824         }
7825
7826       memset (need_pad->contents + need_pad->size, 0, 8);
7827       need_pad->size += 8;
7828     }
7829
7830   return TRUE;
7831 }
7832
7833 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
7834
7835 asection *
7836 ppc64_elf_tls_setup (struct bfd_link_info *info,
7837                      int no_tls_get_addr_opt,
7838                      int *no_multi_toc)
7839 {
7840   struct ppc_link_hash_table *htab;
7841
7842   htab = ppc_hash_table (info);
7843   if (htab == NULL)
7844     return NULL;
7845
7846   if (abiversion (info->output_bfd) == 1)
7847     htab->opd_abi = 1;
7848
7849   if (*no_multi_toc)
7850     htab->do_multi_toc = 0;
7851   else if (!htab->do_multi_toc)
7852     *no_multi_toc = 1;
7853
7854   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7855                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7856                                               FALSE, FALSE, TRUE));
7857   /* Move dynamic linking info to the function descriptor sym.  */
7858   if (htab->tls_get_addr != NULL)
7859     func_desc_adjust (&htab->tls_get_addr->elf, info);
7860   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7861                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7862                                                  FALSE, FALSE, TRUE));
7863   if (!no_tls_get_addr_opt)
7864     {
7865       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7866
7867       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7868                                   FALSE, FALSE, TRUE);
7869       if (opt != NULL)
7870         func_desc_adjust (opt, info);
7871       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7872                                      FALSE, FALSE, TRUE);
7873       if (opt_fd != NULL
7874           && (opt_fd->root.type == bfd_link_hash_defined
7875               || opt_fd->root.type == bfd_link_hash_defweak))
7876         {
7877           /* If glibc supports an optimized __tls_get_addr call stub,
7878              signalled by the presence of __tls_get_addr_opt, and we'll
7879              be calling __tls_get_addr via a plt call stub, then
7880              make __tls_get_addr point to __tls_get_addr_opt.  */
7881           tga_fd = &htab->tls_get_addr_fd->elf;
7882           if (htab->elf.dynamic_sections_created
7883               && tga_fd != NULL
7884               && (tga_fd->type == STT_FUNC
7885                   || tga_fd->needs_plt)
7886               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7887                    || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7888                        && tga_fd->root.type == bfd_link_hash_undefweak)))
7889             {
7890               struct plt_entry *ent;
7891
7892               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7893                 if (ent->plt.refcount > 0)
7894                   break;
7895               if (ent != NULL)
7896                 {
7897                   tga_fd->root.type = bfd_link_hash_indirect;
7898                   tga_fd->root.u.i.link = &opt_fd->root;
7899                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7900                   if (opt_fd->dynindx != -1)
7901                     {
7902                       /* Use __tls_get_addr_opt in dynamic relocations.  */
7903                       opt_fd->dynindx = -1;
7904                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7905                                               opt_fd->dynstr_index);
7906                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7907                         return NULL;
7908                     }
7909                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7910                   tga = &htab->tls_get_addr->elf;
7911                   if (opt != NULL && tga != NULL)
7912                     {
7913                       tga->root.type = bfd_link_hash_indirect;
7914                       tga->root.u.i.link = &opt->root;
7915                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
7916                       _bfd_elf_link_hash_hide_symbol (info, opt,
7917                                                       tga->forced_local);
7918                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7919                     }
7920                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7921                   htab->tls_get_addr_fd->is_func_descriptor = 1;
7922                   if (htab->tls_get_addr != NULL)
7923                     {
7924                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7925                       htab->tls_get_addr->is_func = 1;
7926                     }
7927                 }
7928             }
7929         }
7930       else
7931         no_tls_get_addr_opt = TRUE;
7932     }
7933   htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
7934   return _bfd_elf_tls_setup (info->output_bfd, info);
7935 }
7936
7937 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7938    HASH1 or HASH2.  */
7939
7940 static bfd_boolean
7941 branch_reloc_hash_match (const bfd *ibfd,
7942                          const Elf_Internal_Rela *rel,
7943                          const struct ppc_link_hash_entry *hash1,
7944                          const struct ppc_link_hash_entry *hash2)
7945 {
7946   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7947   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7948   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7949
7950   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7951     {
7952       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7953       struct elf_link_hash_entry *h;
7954
7955       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7956       h = elf_follow_link (h);
7957       if (h == &hash1->elf || h == &hash2->elf)
7958         return TRUE;
7959     }
7960   return FALSE;
7961 }
7962
7963 /* Run through all the TLS relocs looking for optimization
7964    opportunities.  The linker has been hacked (see ppc64elf.em) to do
7965    a preliminary section layout so that we know the TLS segment
7966    offsets.  We can't optimize earlier because some optimizations need
7967    to know the tp offset, and we need to optimize before allocating
7968    dynamic relocations.  */
7969
7970 bfd_boolean
7971 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7972 {
7973   bfd *ibfd;
7974   asection *sec;
7975   struct ppc_link_hash_table *htab;
7976   unsigned char *toc_ref;
7977   int pass;
7978
7979   if (info->relocatable || !info->executable)
7980     return TRUE;
7981
7982   htab = ppc_hash_table (info);
7983   if (htab == NULL)
7984     return FALSE;
7985
7986   /* Make two passes over the relocs.  On the first pass, mark toc
7987      entries involved with tls relocs, and check that tls relocs
7988      involved in setting up a tls_get_addr call are indeed followed by
7989      such a call.  If they are not, we can't do any tls optimization.
7990      On the second pass twiddle tls_mask flags to notify
7991      relocate_section that optimization can be done, and adjust got
7992      and plt refcounts.  */
7993   toc_ref = NULL;
7994   for (pass = 0; pass < 2; ++pass)
7995     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7996       {
7997         Elf_Internal_Sym *locsyms = NULL;
7998         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7999
8000         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8001           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8002             {
8003               Elf_Internal_Rela *relstart, *rel, *relend;
8004               bfd_boolean found_tls_get_addr_arg = 0;
8005
8006               /* Read the relocations.  */
8007               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8008                                                     info->keep_memory);
8009               if (relstart == NULL)
8010                 return FALSE;
8011
8012               relend = relstart + sec->reloc_count;
8013               for (rel = relstart; rel < relend; rel++)
8014                 {
8015                   enum elf_ppc64_reloc_type r_type;
8016                   unsigned long r_symndx;
8017                   struct elf_link_hash_entry *h;
8018                   Elf_Internal_Sym *sym;
8019                   asection *sym_sec;
8020                   unsigned char *tls_mask;
8021                   unsigned char tls_set, tls_clear, tls_type = 0;
8022                   bfd_vma value;
8023                   bfd_boolean ok_tprel, is_local;
8024                   long toc_ref_index = 0;
8025                   int expecting_tls_get_addr = 0;
8026                   bfd_boolean ret = FALSE;
8027
8028                   r_symndx = ELF64_R_SYM (rel->r_info);
8029                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8030                                   r_symndx, ibfd))
8031                     {
8032                     err_free_rel:
8033                       if (elf_section_data (sec)->relocs != relstart)
8034                         free (relstart);
8035                       if (toc_ref != NULL)
8036                         free (toc_ref);
8037                       if (locsyms != NULL
8038                           && (elf_symtab_hdr (ibfd).contents
8039                               != (unsigned char *) locsyms))
8040                         free (locsyms);
8041                       return ret;
8042                     }
8043
8044                   if (h != NULL)
8045                     {
8046                       if (h->root.type == bfd_link_hash_defined
8047                           || h->root.type == bfd_link_hash_defweak)
8048                         value = h->root.u.def.value;
8049                       else if (h->root.type == bfd_link_hash_undefweak)
8050                         value = 0;
8051                       else
8052                         {
8053                           found_tls_get_addr_arg = 0;
8054                           continue;
8055                         }
8056                     }
8057                   else
8058                     /* Symbols referenced by TLS relocs must be of type
8059                        STT_TLS.  So no need for .opd local sym adjust.  */
8060                     value = sym->st_value;
8061
8062                   ok_tprel = FALSE;
8063                   is_local = FALSE;
8064                   if (h == NULL
8065                       || !h->def_dynamic)
8066                     {
8067                       is_local = TRUE;
8068                       if (h != NULL
8069                           && h->root.type == bfd_link_hash_undefweak)
8070                         ok_tprel = TRUE;
8071                       else
8072                         {
8073                           value += sym_sec->output_offset;
8074                           value += sym_sec->output_section->vma;
8075                           value -= htab->elf.tls_sec->vma;
8076                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8077                                       < (bfd_vma) 1 << 32);
8078                         }
8079                     }
8080
8081                   r_type = ELF64_R_TYPE (rel->r_info);
8082                   /* If this section has old-style __tls_get_addr calls
8083                      without marker relocs, then check that each
8084                      __tls_get_addr call reloc is preceded by a reloc
8085                      that conceivably belongs to the __tls_get_addr arg
8086                      setup insn.  If we don't find matching arg setup
8087                      relocs, don't do any tls optimization.  */
8088                   if (pass == 0
8089                       && sec->has_tls_get_addr_call
8090                       && h != NULL
8091                       && (h == &htab->tls_get_addr->elf
8092                           || h == &htab->tls_get_addr_fd->elf)
8093                       && !found_tls_get_addr_arg
8094                       && is_branch_reloc (r_type))
8095                     {
8096                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8097                                                 "TLS optimization disabled\n"),
8098                                               ibfd, sec, rel->r_offset);
8099                       ret = TRUE;
8100                       goto err_free_rel;
8101                     }
8102
8103                   found_tls_get_addr_arg = 0;
8104                   switch (r_type)
8105                     {
8106                     case R_PPC64_GOT_TLSLD16:
8107                     case R_PPC64_GOT_TLSLD16_LO:
8108                       expecting_tls_get_addr = 1;
8109                       found_tls_get_addr_arg = 1;
8110                       /* Fall thru */
8111
8112                     case R_PPC64_GOT_TLSLD16_HI:
8113                     case R_PPC64_GOT_TLSLD16_HA:
8114                       /* These relocs should never be against a symbol
8115                          defined in a shared lib.  Leave them alone if
8116                          that turns out to be the case.  */
8117                       if (!is_local)
8118                         continue;
8119
8120                       /* LD -> LE */
8121                       tls_set = 0;
8122                       tls_clear = TLS_LD;
8123                       tls_type = TLS_TLS | TLS_LD;
8124                       break;
8125
8126                     case R_PPC64_GOT_TLSGD16:
8127                     case R_PPC64_GOT_TLSGD16_LO:
8128                       expecting_tls_get_addr = 1;
8129                       found_tls_get_addr_arg = 1;
8130                       /* Fall thru */
8131
8132                     case R_PPC64_GOT_TLSGD16_HI:
8133                     case R_PPC64_GOT_TLSGD16_HA:
8134                       if (ok_tprel)
8135                         /* GD -> LE */
8136                         tls_set = 0;
8137                       else
8138                         /* GD -> IE */
8139                         tls_set = TLS_TLS | TLS_TPRELGD;
8140                       tls_clear = TLS_GD;
8141                       tls_type = TLS_TLS | TLS_GD;
8142                       break;
8143
8144                     case R_PPC64_GOT_TPREL16_DS:
8145                     case R_PPC64_GOT_TPREL16_LO_DS:
8146                     case R_PPC64_GOT_TPREL16_HI:
8147                     case R_PPC64_GOT_TPREL16_HA:
8148                       if (ok_tprel)
8149                         {
8150                           /* IE -> LE */
8151                           tls_set = 0;
8152                           tls_clear = TLS_TPREL;
8153                           tls_type = TLS_TLS | TLS_TPREL;
8154                           break;
8155                         }
8156                       continue;
8157
8158                     case R_PPC64_TLSGD:
8159                     case R_PPC64_TLSLD:
8160                       found_tls_get_addr_arg = 1;
8161                       /* Fall thru */
8162
8163                     case R_PPC64_TLS:
8164                     case R_PPC64_TOC16:
8165                     case R_PPC64_TOC16_LO:
8166                       if (sym_sec == NULL || sym_sec != toc)
8167                         continue;
8168
8169                       /* Mark this toc entry as referenced by a TLS
8170                          code sequence.  We can do that now in the
8171                          case of R_PPC64_TLS, and after checking for
8172                          tls_get_addr for the TOC16 relocs.  */
8173                       if (toc_ref == NULL)
8174                         toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8175                       if (toc_ref == NULL)
8176                         goto err_free_rel;
8177
8178                       if (h != NULL)
8179                         value = h->root.u.def.value;
8180                       else
8181                         value = sym->st_value;
8182                       value += rel->r_addend;
8183                       BFD_ASSERT (value < toc->size && value % 8 == 0);
8184                       toc_ref_index = (value + toc->output_offset) / 8;
8185                       if (r_type == R_PPC64_TLS
8186                           || r_type == R_PPC64_TLSGD
8187                           || r_type == R_PPC64_TLSLD)
8188                         {
8189                           toc_ref[toc_ref_index] = 1;
8190                           continue;
8191                         }
8192
8193                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
8194                         continue;
8195
8196                       tls_set = 0;
8197                       tls_clear = 0;
8198                       expecting_tls_get_addr = 2;
8199                       break;
8200
8201                     case R_PPC64_TPREL64:
8202                       if (pass == 0
8203                           || sec != toc
8204                           || toc_ref == NULL
8205                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8206                         continue;
8207                       if (ok_tprel)
8208                         {
8209                           /* IE -> LE */
8210                           tls_set = TLS_EXPLICIT;
8211                           tls_clear = TLS_TPREL;
8212                           break;
8213                         }
8214                       continue;
8215
8216                     case R_PPC64_DTPMOD64:
8217                       if (pass == 0
8218                           || sec != toc
8219                           || toc_ref == NULL
8220                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8221                         continue;
8222                       if (rel + 1 < relend
8223                           && (rel[1].r_info
8224                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8225                           && rel[1].r_offset == rel->r_offset + 8)
8226                         {
8227                           if (ok_tprel)
8228                             /* GD -> LE */
8229                             tls_set = TLS_EXPLICIT | TLS_GD;
8230                           else
8231                             /* GD -> IE */
8232                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8233                           tls_clear = TLS_GD;
8234                         }
8235                       else
8236                         {
8237                           if (!is_local)
8238                             continue;
8239
8240                           /* LD -> LE */
8241                           tls_set = TLS_EXPLICIT;
8242                           tls_clear = TLS_LD;
8243                         }
8244                       break;
8245
8246                     default:
8247                       continue;
8248                     }
8249
8250                   if (pass == 0)
8251                     {
8252                       if (!expecting_tls_get_addr
8253                           || !sec->has_tls_get_addr_call)
8254                         continue;
8255
8256                       if (rel + 1 < relend
8257                           && branch_reloc_hash_match (ibfd, rel + 1,
8258                                                       htab->tls_get_addr,
8259                                                       htab->tls_get_addr_fd))
8260                         {
8261                           if (expecting_tls_get_addr == 2)
8262                             {
8263                               /* Check for toc tls entries.  */
8264                               unsigned char *toc_tls;
8265                               int retval;
8266
8267                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8268                                                      &locsyms,
8269                                                      rel, ibfd);
8270                               if (retval == 0)
8271                                 goto err_free_rel;
8272                               if (toc_tls != NULL)
8273                                 {
8274                                   if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8275                                     found_tls_get_addr_arg = 1;
8276                                   if (retval > 1)
8277                                     toc_ref[toc_ref_index] = 1;
8278                                 }
8279                             }
8280                           continue;
8281                         }
8282
8283                       if (expecting_tls_get_addr != 1)
8284                         continue;
8285
8286                       /* Uh oh, we didn't find the expected call.  We
8287                          could just mark this symbol to exclude it
8288                          from tls optimization but it's safer to skip
8289                          the entire optimization.  */
8290                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8291                                                 "TLS optimization disabled\n"),
8292                                               ibfd, sec, rel->r_offset);
8293                       ret = TRUE;
8294                       goto err_free_rel;
8295                     }
8296
8297                   if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8298                     {
8299                       struct plt_entry *ent;
8300                       for (ent = htab->tls_get_addr->elf.plt.plist;
8301                            ent != NULL;
8302                            ent = ent->next)
8303                         if (ent->addend == 0)
8304                           {
8305                             if (ent->plt.refcount > 0)
8306                               {
8307                                 ent->plt.refcount -= 1;
8308                                 expecting_tls_get_addr = 0;
8309                               }
8310                             break;
8311                           }
8312                     }
8313
8314                   if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8315                     {
8316                       struct plt_entry *ent;
8317                       for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8318                            ent != NULL;
8319                            ent = ent->next)
8320                         if (ent->addend == 0)
8321                           {
8322                             if (ent->plt.refcount > 0)
8323                               ent->plt.refcount -= 1;
8324                             break;
8325                           }
8326                     }
8327
8328                   if (tls_clear == 0)
8329                     continue;
8330
8331                   if ((tls_set & TLS_EXPLICIT) == 0)
8332                     {
8333                       struct got_entry *ent;
8334
8335                       /* Adjust got entry for this reloc.  */
8336                       if (h != NULL)
8337                         ent = h->got.glist;
8338                       else
8339                         ent = elf_local_got_ents (ibfd)[r_symndx];
8340
8341                       for (; ent != NULL; ent = ent->next)
8342                         if (ent->addend == rel->r_addend
8343                             && ent->owner == ibfd
8344                             && ent->tls_type == tls_type)
8345                           break;
8346                       if (ent == NULL)
8347                         abort ();
8348
8349                       if (tls_set == 0)
8350                         {
8351                           /* We managed to get rid of a got entry.  */
8352                           if (ent->got.refcount > 0)
8353                             ent->got.refcount -= 1;
8354                         }
8355                     }
8356                   else
8357                     {
8358                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
8359                          we'll lose one or two dyn relocs.  */
8360                       if (!dec_dynrel_count (rel->r_info, sec, info,
8361                                              NULL, h, sym))
8362                         return FALSE;
8363
8364                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
8365                         {
8366                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8367                                                  NULL, h, sym))
8368                             return FALSE;
8369                         }
8370                     }
8371
8372                   *tls_mask |= tls_set;
8373                   *tls_mask &= ~tls_clear;
8374                 }
8375
8376               if (elf_section_data (sec)->relocs != relstart)
8377                 free (relstart);
8378             }
8379
8380         if (locsyms != NULL
8381             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8382           {
8383             if (!info->keep_memory)
8384               free (locsyms);
8385             else
8386               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8387           }
8388       }
8389
8390   if (toc_ref != NULL)
8391     free (toc_ref);
8392   return TRUE;
8393 }
8394
8395 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8396    the values of any global symbols in a toc section that has been
8397    edited.  Globals in toc sections should be a rarity, so this function
8398    sets a flag if any are found in toc sections other than the one just
8399    edited, so that futher hash table traversals can be avoided.  */
8400
8401 struct adjust_toc_info
8402 {
8403   asection *toc;
8404   unsigned long *skip;
8405   bfd_boolean global_toc_syms;
8406 };
8407
8408 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8409
8410 static bfd_boolean
8411 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8412 {
8413   struct ppc_link_hash_entry *eh;
8414   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8415   unsigned long i;
8416
8417   if (h->root.type != bfd_link_hash_defined
8418       && h->root.type != bfd_link_hash_defweak)
8419     return TRUE;
8420
8421   eh = (struct ppc_link_hash_entry *) h;
8422   if (eh->adjust_done)
8423     return TRUE;
8424
8425   if (eh->elf.root.u.def.section == toc_inf->toc)
8426     {
8427       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8428         i = toc_inf->toc->rawsize >> 3;
8429       else
8430         i = eh->elf.root.u.def.value >> 3;
8431
8432       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8433         {
8434           (*_bfd_error_handler)
8435             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8436           do
8437             ++i;
8438           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8439           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8440         }
8441
8442       eh->elf.root.u.def.value -= toc_inf->skip[i];
8443       eh->adjust_done = 1;
8444     }
8445   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8446     toc_inf->global_toc_syms = TRUE;
8447
8448   return TRUE;
8449 }
8450
8451 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc.  */
8452
8453 static bfd_boolean
8454 ok_lo_toc_insn (unsigned int insn)
8455 {
8456   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8457           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8458           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8459           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8460           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8461           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8462           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8463           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8464           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8465           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8466           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8467           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8468           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8469           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8470           || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8471               && (insn & 3) != 1)
8472           || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8473               && ((insn & 3) == 0 || (insn & 3) == 3))
8474           || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8475 }
8476
8477 /* Examine all relocs referencing .toc sections in order to remove
8478    unused .toc entries.  */
8479
8480 bfd_boolean
8481 ppc64_elf_edit_toc (struct bfd_link_info *info)
8482 {
8483   bfd *ibfd;
8484   struct adjust_toc_info toc_inf;
8485   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8486
8487   htab->do_toc_opt = 1;
8488   toc_inf.global_toc_syms = TRUE;
8489   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8490     {
8491       asection *toc, *sec;
8492       Elf_Internal_Shdr *symtab_hdr;
8493       Elf_Internal_Sym *local_syms;
8494       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8495       unsigned long *skip, *drop;
8496       unsigned char *used;
8497       unsigned char *keep, last, some_unused;
8498
8499       if (!is_ppc64_elf (ibfd))
8500         continue;
8501
8502       toc = bfd_get_section_by_name (ibfd, ".toc");
8503       if (toc == NULL
8504           || toc->size == 0
8505           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8506           || discarded_section (toc))
8507         continue;
8508
8509       toc_relocs = NULL;
8510       local_syms = NULL;
8511       symtab_hdr = &elf_symtab_hdr (ibfd);
8512
8513       /* Look at sections dropped from the final link.  */
8514       skip = NULL;
8515       relstart = NULL;
8516       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8517         {
8518           if (sec->reloc_count == 0
8519               || !discarded_section (sec)
8520               || get_opd_info (sec)
8521               || (sec->flags & SEC_ALLOC) == 0
8522               || (sec->flags & SEC_DEBUGGING) != 0)
8523             continue;
8524
8525           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8526           if (relstart == NULL)
8527             goto error_ret;
8528
8529           /* Run through the relocs to see which toc entries might be
8530              unused.  */
8531           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8532             {
8533               enum elf_ppc64_reloc_type r_type;
8534               unsigned long r_symndx;
8535               asection *sym_sec;
8536               struct elf_link_hash_entry *h;
8537               Elf_Internal_Sym *sym;
8538               bfd_vma val;
8539
8540               r_type = ELF64_R_TYPE (rel->r_info);
8541               switch (r_type)
8542                 {
8543                 default:
8544                   continue;
8545
8546                 case R_PPC64_TOC16:
8547                 case R_PPC64_TOC16_LO:
8548                 case R_PPC64_TOC16_HI:
8549                 case R_PPC64_TOC16_HA:
8550                 case R_PPC64_TOC16_DS:
8551                 case R_PPC64_TOC16_LO_DS:
8552                   break;
8553                 }
8554
8555               r_symndx = ELF64_R_SYM (rel->r_info);
8556               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8557                               r_symndx, ibfd))
8558                 goto error_ret;
8559
8560               if (sym_sec != toc)
8561                 continue;
8562
8563               if (h != NULL)
8564                 val = h->root.u.def.value;
8565               else
8566                 val = sym->st_value;
8567               val += rel->r_addend;
8568
8569               if (val >= toc->size)
8570                 continue;
8571
8572               /* Anything in the toc ought to be aligned to 8 bytes.
8573                  If not, don't mark as unused.  */
8574               if (val & 7)
8575                 continue;
8576
8577               if (skip == NULL)
8578                 {
8579                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8580                   if (skip == NULL)
8581                     goto error_ret;
8582                 }
8583
8584               skip[val >> 3] = ref_from_discarded;
8585             }
8586
8587           if (elf_section_data (sec)->relocs != relstart)
8588             free (relstart);
8589         }
8590
8591       /* For largetoc loads of address constants, we can convert
8592          .  addis rx,2,addr@got@ha
8593          .  ld ry,addr@got@l(rx)
8594          to
8595          .  addis rx,2,addr@toc@ha
8596          .  addi ry,rx,addr@toc@l
8597          when addr is within 2G of the toc pointer.  This then means
8598          that the word storing "addr" in the toc is no longer needed.  */
8599
8600       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8601           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8602           && toc->reloc_count != 0)
8603         {
8604           /* Read toc relocs.  */
8605           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8606                                                   info->keep_memory);
8607           if (toc_relocs == NULL)
8608             goto error_ret;
8609
8610           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8611             {
8612               enum elf_ppc64_reloc_type r_type;
8613               unsigned long r_symndx;
8614               asection *sym_sec;
8615               struct elf_link_hash_entry *h;
8616               Elf_Internal_Sym *sym;
8617               bfd_vma val, addr;
8618
8619               r_type = ELF64_R_TYPE (rel->r_info);
8620               if (r_type != R_PPC64_ADDR64)
8621                 continue;
8622
8623               r_symndx = ELF64_R_SYM (rel->r_info);
8624               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8625                               r_symndx, ibfd))
8626                 goto error_ret;
8627
8628               if (sym_sec == NULL
8629                   || discarded_section (sym_sec))
8630                 continue;
8631
8632               if (!SYMBOL_CALLS_LOCAL (info, h))
8633                 continue;
8634
8635               if (h != NULL)
8636                 {
8637                   if (h->type == STT_GNU_IFUNC)
8638                     continue;
8639                   val = h->root.u.def.value;
8640                 }
8641               else
8642                 {
8643                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8644                     continue;
8645                   val = sym->st_value;
8646                 }
8647               val += rel->r_addend;
8648               val += sym_sec->output_section->vma + sym_sec->output_offset;
8649
8650               /* We don't yet know the exact toc pointer value, but we
8651                  know it will be somewhere in the toc section.  Don't
8652                  optimize if the difference from any possible toc
8653                  pointer is outside [ff..f80008000, 7fff7fff].  */
8654               addr = toc->output_section->vma + TOC_BASE_OFF;
8655               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8656                 continue;
8657
8658               addr = toc->output_section->vma + toc->output_section->rawsize;
8659               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8660                 continue;
8661
8662               if (skip == NULL)
8663                 {
8664                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8665                   if (skip == NULL)
8666                     goto error_ret;
8667                 }
8668
8669               skip[rel->r_offset >> 3]
8670                 |= can_optimize | ((rel - toc_relocs) << 2);
8671             }
8672         }
8673
8674       if (skip == NULL)
8675         continue;
8676
8677       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8678       if (used == NULL)
8679         {
8680         error_ret:
8681           if (local_syms != NULL
8682               && symtab_hdr->contents != (unsigned char *) local_syms)
8683             free (local_syms);
8684           if (sec != NULL
8685               && relstart != NULL
8686               && elf_section_data (sec)->relocs != relstart)
8687             free (relstart);
8688           if (toc_relocs != NULL
8689               && elf_section_data (toc)->relocs != toc_relocs)
8690             free (toc_relocs);
8691           if (skip != NULL)
8692             free (skip);
8693           return FALSE;
8694         }
8695
8696       /* Now check all kept sections that might reference the toc.
8697          Check the toc itself last.  */
8698       for (sec = (ibfd->sections == toc && toc->next ? toc->next
8699                   : ibfd->sections);
8700            sec != NULL;
8701            sec = (sec == toc ? NULL
8702                   : sec->next == NULL ? toc
8703                   : sec->next == toc && toc->next ? toc->next
8704                   : sec->next))
8705         {
8706           int repeat;
8707
8708           if (sec->reloc_count == 0
8709               || discarded_section (sec)
8710               || get_opd_info (sec)
8711               || (sec->flags & SEC_ALLOC) == 0
8712               || (sec->flags & SEC_DEBUGGING) != 0)
8713             continue;
8714
8715           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8716                                                 info->keep_memory);
8717           if (relstart == NULL)
8718             goto error_ret;
8719
8720           /* Mark toc entries referenced as used.  */
8721           do
8722             {
8723               repeat = 0;
8724               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8725                 {
8726                   enum elf_ppc64_reloc_type r_type;
8727                   unsigned long r_symndx;
8728                   asection *sym_sec;
8729                   struct elf_link_hash_entry *h;
8730                   Elf_Internal_Sym *sym;
8731                   bfd_vma val;
8732                   enum {no_check, check_lo, check_ha} insn_check;
8733
8734                   r_type = ELF64_R_TYPE (rel->r_info);
8735                   switch (r_type)
8736                     {
8737                     default:
8738                       insn_check = no_check;
8739                       break;
8740
8741                     case R_PPC64_GOT_TLSLD16_HA:
8742                     case R_PPC64_GOT_TLSGD16_HA:
8743                     case R_PPC64_GOT_TPREL16_HA:
8744                     case R_PPC64_GOT_DTPREL16_HA:
8745                     case R_PPC64_GOT16_HA:
8746                     case R_PPC64_TOC16_HA:
8747                       insn_check = check_ha;
8748                       break;
8749
8750                     case R_PPC64_GOT_TLSLD16_LO:
8751                     case R_PPC64_GOT_TLSGD16_LO:
8752                     case R_PPC64_GOT_TPREL16_LO_DS:
8753                     case R_PPC64_GOT_DTPREL16_LO_DS:
8754                     case R_PPC64_GOT16_LO:
8755                     case R_PPC64_GOT16_LO_DS:
8756                     case R_PPC64_TOC16_LO:
8757                     case R_PPC64_TOC16_LO_DS:
8758                       insn_check = check_lo;
8759                       break;
8760                     }
8761
8762                   if (insn_check != no_check)
8763                     {
8764                       bfd_vma off = rel->r_offset & ~3;
8765                       unsigned char buf[4];
8766                       unsigned int insn;
8767
8768                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8769                         {
8770                           free (used);
8771                           goto error_ret;
8772                         }
8773                       insn = bfd_get_32 (ibfd, buf);
8774                       if (insn_check == check_lo
8775                           ? !ok_lo_toc_insn (insn)
8776                           : ((insn & ((0x3f << 26) | 0x1f << 16))
8777                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8778                         {
8779                           char str[12];
8780
8781                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8782                           sprintf (str, "%#08x", insn);
8783                           info->callbacks->einfo
8784                             (_("%P: %H: toc optimization is not supported for"
8785                                " %s instruction.\n"),
8786                              ibfd, sec, rel->r_offset & ~3, str);
8787                         }
8788                     }
8789
8790                   switch (r_type)
8791                     {
8792                     case R_PPC64_TOC16:
8793                     case R_PPC64_TOC16_LO:
8794                     case R_PPC64_TOC16_HI:
8795                     case R_PPC64_TOC16_HA:
8796                     case R_PPC64_TOC16_DS:
8797                     case R_PPC64_TOC16_LO_DS:
8798                       /* In case we're taking addresses of toc entries.  */
8799                     case R_PPC64_ADDR64:
8800                       break;
8801
8802                     default:
8803                       continue;
8804                     }
8805
8806                   r_symndx = ELF64_R_SYM (rel->r_info);
8807                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8808                                   r_symndx, ibfd))
8809                     {
8810                       free (used);
8811                       goto error_ret;
8812                     }
8813
8814                   if (sym_sec != toc)
8815                     continue;
8816
8817                   if (h != NULL)
8818                     val = h->root.u.def.value;
8819                   else
8820                     val = sym->st_value;
8821                   val += rel->r_addend;
8822
8823                   if (val >= toc->size)
8824                     continue;
8825
8826                   if ((skip[val >> 3] & can_optimize) != 0)
8827                     {
8828                       bfd_vma off;
8829                       unsigned char opc;
8830
8831                       switch (r_type)
8832                         {
8833                         case R_PPC64_TOC16_HA:
8834                           break;
8835
8836                         case R_PPC64_TOC16_LO_DS:
8837                           off = rel->r_offset;
8838                           off += (bfd_big_endian (ibfd) ? -2 : 3);
8839                           if (!bfd_get_section_contents (ibfd, sec, &opc,
8840                                                          off, 1))
8841                             {
8842                               free (used);
8843                               goto error_ret;
8844                             }
8845                           if ((opc & (0x3f << 2)) == (58u << 2))
8846                             break;
8847                           /* Fall thru */
8848
8849                         default:
8850                           /* Wrong sort of reloc, or not a ld.  We may
8851                              as well clear ref_from_discarded too.  */
8852                           skip[val >> 3] = 0;
8853                         }
8854                     }
8855
8856                   if (sec != toc)
8857                     used[val >> 3] = 1;
8858                   /* For the toc section, we only mark as used if this
8859                      entry itself isn't unused.  */
8860                   else if ((used[rel->r_offset >> 3]
8861                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8862                            && !used[val >> 3])
8863                     {
8864                       /* Do all the relocs again, to catch reference
8865                          chains.  */
8866                       repeat = 1;
8867                       used[val >> 3] = 1;
8868                     }
8869                 }
8870             }
8871           while (repeat);
8872
8873           if (elf_section_data (sec)->relocs != relstart)
8874             free (relstart);
8875         }
8876
8877       /* Merge the used and skip arrays.  Assume that TOC
8878          doublewords not appearing as either used or unused belong
8879          to to an entry more than one doubleword in size.  */
8880       for (drop = skip, keep = used, last = 0, some_unused = 0;
8881            drop < skip + (toc->size + 7) / 8;
8882            ++drop, ++keep)
8883         {
8884           if (*keep)
8885             {
8886               *drop &= ~ref_from_discarded;
8887               if ((*drop & can_optimize) != 0)
8888                 some_unused = 1;
8889               last = 0;
8890             }
8891           else if ((*drop & ref_from_discarded) != 0)
8892             {
8893               some_unused = 1;
8894               last = ref_from_discarded;
8895             }
8896           else
8897             *drop = last;
8898         }
8899
8900       free (used);
8901
8902       if (some_unused)
8903         {
8904           bfd_byte *contents, *src;
8905           unsigned long off;
8906           Elf_Internal_Sym *sym;
8907           bfd_boolean local_toc_syms = FALSE;
8908
8909           /* Shuffle the toc contents, and at the same time convert the
8910              skip array from booleans into offsets.  */
8911           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8912             goto error_ret;
8913
8914           elf_section_data (toc)->this_hdr.contents = contents;
8915
8916           for (src = contents, off = 0, drop = skip;
8917                src < contents + toc->size;
8918                src += 8, ++drop)
8919             {
8920               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8921                 off += 8;
8922               else if (off != 0)
8923                 {
8924                   *drop = off;
8925                   memcpy (src - off, src, 8);
8926                 }
8927             }
8928           *drop = off;
8929           toc->rawsize = toc->size;
8930           toc->size = src - contents - off;
8931
8932           /* Adjust addends for relocs against the toc section sym,
8933              and optimize any accesses we can.  */
8934           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8935             {
8936               if (sec->reloc_count == 0
8937                   || discarded_section (sec))
8938                 continue;
8939
8940               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8941                                                     info->keep_memory);
8942               if (relstart == NULL)
8943                 goto error_ret;
8944
8945               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8946                 {
8947                   enum elf_ppc64_reloc_type r_type;
8948                   unsigned long r_symndx;
8949                   asection *sym_sec;
8950                   struct elf_link_hash_entry *h;
8951                   bfd_vma val;
8952
8953                   r_type = ELF64_R_TYPE (rel->r_info);
8954                   switch (r_type)
8955                     {
8956                     default:
8957                       continue;
8958
8959                     case R_PPC64_TOC16:
8960                     case R_PPC64_TOC16_LO:
8961                     case R_PPC64_TOC16_HI:
8962                     case R_PPC64_TOC16_HA:
8963                     case R_PPC64_TOC16_DS:
8964                     case R_PPC64_TOC16_LO_DS:
8965                     case R_PPC64_ADDR64:
8966                       break;
8967                     }
8968
8969                   r_symndx = ELF64_R_SYM (rel->r_info);
8970                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8971                                   r_symndx, ibfd))
8972                     goto error_ret;
8973
8974                   if (sym_sec != toc)
8975                     continue;
8976
8977                   if (h != NULL)
8978                     val = h->root.u.def.value;
8979                   else
8980                     {
8981                       val = sym->st_value;
8982                       if (val != 0)
8983                         local_toc_syms = TRUE;
8984                     }
8985
8986                   val += rel->r_addend;
8987
8988                   if (val > toc->rawsize)
8989                     val = toc->rawsize;
8990                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
8991                     continue;
8992                   else if ((skip[val >> 3] & can_optimize) != 0)
8993                     {
8994                       Elf_Internal_Rela *tocrel
8995                         = toc_relocs + (skip[val >> 3] >> 2);
8996                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8997
8998                       switch (r_type)
8999                         {
9000                         case R_PPC64_TOC16_HA:
9001                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9002                           break;
9003
9004                         case R_PPC64_TOC16_LO_DS:
9005                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9006                           break;
9007
9008                         default:
9009                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9010                             ppc_howto_init ();
9011                           info->callbacks->einfo
9012                             (_("%P: %H: %s references "
9013                                "optimized away TOC entry\n"),
9014                              ibfd, sec, rel->r_offset,
9015                              ppc64_elf_howto_table[r_type]->name);
9016                           bfd_set_error (bfd_error_bad_value);
9017                           goto error_ret;
9018                         }
9019                       rel->r_addend = tocrel->r_addend;
9020                       elf_section_data (sec)->relocs = relstart;
9021                       continue;
9022                     }
9023
9024                   if (h != NULL || sym->st_value != 0)
9025                     continue;
9026
9027                   rel->r_addend -= skip[val >> 3];
9028                   elf_section_data (sec)->relocs = relstart;
9029                 }
9030
9031               if (elf_section_data (sec)->relocs != relstart)
9032                 free (relstart);
9033             }
9034
9035           /* We shouldn't have local or global symbols defined in the TOC,
9036              but handle them anyway.  */
9037           if (local_syms != NULL)
9038             for (sym = local_syms;
9039                  sym < local_syms + symtab_hdr->sh_info;
9040                  ++sym)
9041               if (sym->st_value != 0
9042                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9043                 {
9044                   unsigned long i;
9045
9046                   if (sym->st_value > toc->rawsize)
9047                     i = toc->rawsize >> 3;
9048                   else
9049                     i = sym->st_value >> 3;
9050
9051                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9052                     {
9053                       if (local_toc_syms)
9054                         (*_bfd_error_handler)
9055                           (_("%s defined on removed toc entry"),
9056                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9057                       do
9058                         ++i;
9059                       while ((skip[i] & (ref_from_discarded | can_optimize)));
9060                       sym->st_value = (bfd_vma) i << 3;
9061                     }
9062
9063                   sym->st_value -= skip[i];
9064                   symtab_hdr->contents = (unsigned char *) local_syms;
9065                 }
9066
9067           /* Adjust any global syms defined in this toc input section.  */
9068           if (toc_inf.global_toc_syms)
9069             {
9070               toc_inf.toc = toc;
9071               toc_inf.skip = skip;
9072               toc_inf.global_toc_syms = FALSE;
9073               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9074                                       &toc_inf);
9075             }
9076
9077           if (toc->reloc_count != 0)
9078             {
9079               Elf_Internal_Shdr *rel_hdr;
9080               Elf_Internal_Rela *wrel;
9081               bfd_size_type sz;
9082
9083               /* Remove unused toc relocs, and adjust those we keep.  */
9084               if (toc_relocs == NULL)
9085                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9086                                                         info->keep_memory);
9087               if (toc_relocs == NULL)
9088                 goto error_ret;
9089
9090               wrel = toc_relocs;
9091               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9092                 if ((skip[rel->r_offset >> 3]
9093                      & (ref_from_discarded | can_optimize)) == 0)
9094                   {
9095                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9096                     wrel->r_info = rel->r_info;
9097                     wrel->r_addend = rel->r_addend;
9098                     ++wrel;
9099                   }
9100                 else if (!dec_dynrel_count (rel->r_info, toc, info,
9101                                             &local_syms, NULL, NULL))
9102                   goto error_ret;
9103
9104               elf_section_data (toc)->relocs = toc_relocs;
9105               toc->reloc_count = wrel - toc_relocs;
9106               rel_hdr = _bfd_elf_single_rel_hdr (toc);
9107               sz = rel_hdr->sh_entsize;
9108               rel_hdr->sh_size = toc->reloc_count * sz;
9109             }
9110         }
9111       else if (toc_relocs != NULL
9112                && elf_section_data (toc)->relocs != toc_relocs)
9113         free (toc_relocs);
9114
9115       if (local_syms != NULL
9116           && symtab_hdr->contents != (unsigned char *) local_syms)
9117         {
9118           if (!info->keep_memory)
9119             free (local_syms);
9120           else
9121             symtab_hdr->contents = (unsigned char *) local_syms;
9122         }
9123       free (skip);
9124     }
9125
9126   return TRUE;
9127 }
9128
9129 /* Return true iff input section I references the TOC using
9130    instructions limited to +/-32k offsets.  */
9131
9132 bfd_boolean
9133 ppc64_elf_has_small_toc_reloc (asection *i)
9134 {
9135   return (is_ppc64_elf (i->owner)
9136           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9137 }
9138
9139 /* Allocate space for one GOT entry.  */
9140
9141 static void
9142 allocate_got (struct elf_link_hash_entry *h,
9143               struct bfd_link_info *info,
9144               struct got_entry *gent)
9145 {
9146   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9147   bfd_boolean dyn;
9148   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9149   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9150                  ? 16 : 8);
9151   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9152                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9153   asection *got = ppc64_elf_tdata (gent->owner)->got;
9154
9155   gent->got.offset = got->size;
9156   got->size += entsize;
9157
9158   dyn = htab->elf.dynamic_sections_created;
9159   if (h->type == STT_GNU_IFUNC)
9160     {
9161       htab->reliplt->size += rentsize;
9162       htab->got_reli_size += rentsize;
9163     }
9164   else if ((info->shared
9165             || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9166            && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9167                || h->root.type != bfd_link_hash_undefweak))
9168     {
9169       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9170       relgot->size += rentsize;
9171     }
9172 }
9173
9174 /* This function merges got entries in the same toc group.  */
9175
9176 static void
9177 merge_got_entries (struct got_entry **pent)
9178 {
9179   struct got_entry *ent, *ent2;
9180
9181   for (ent = *pent; ent != NULL; ent = ent->next)
9182     if (!ent->is_indirect)
9183       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9184         if (!ent2->is_indirect
9185             && ent2->addend == ent->addend
9186             && ent2->tls_type == ent->tls_type
9187             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9188           {
9189             ent2->is_indirect = TRUE;
9190             ent2->got.ent = ent;
9191           }
9192 }
9193
9194 /* Allocate space in .plt, .got and associated reloc sections for
9195    dynamic relocs.  */
9196
9197 static bfd_boolean
9198 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9199 {
9200   struct bfd_link_info *info;
9201   struct ppc_link_hash_table *htab;
9202   asection *s;
9203   struct ppc_link_hash_entry *eh;
9204   struct elf_dyn_relocs *p;
9205   struct got_entry **pgent, *gent;
9206
9207   if (h->root.type == bfd_link_hash_indirect)
9208     return TRUE;
9209
9210   info = (struct bfd_link_info *) inf;
9211   htab = ppc_hash_table (info);
9212   if (htab == NULL)
9213     return FALSE;
9214
9215   if ((htab->elf.dynamic_sections_created
9216        && h->dynindx != -1
9217        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
9218       || h->type == STT_GNU_IFUNC)
9219     {
9220       struct plt_entry *pent;
9221       bfd_boolean doneone = FALSE;
9222       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9223         if (pent->plt.refcount > 0)
9224           {
9225             if (!htab->elf.dynamic_sections_created
9226                 || h->dynindx == -1)
9227               {
9228                 s = htab->iplt;
9229                 pent->plt.offset = s->size;
9230                 s->size += PLT_ENTRY_SIZE;
9231                 s = htab->reliplt;
9232               }
9233             else
9234               {
9235                 /* If this is the first .plt entry, make room for the special
9236                    first entry.  */
9237                 s = htab->plt;
9238                 if (s->size == 0)
9239                   s->size += PLT_INITIAL_ENTRY_SIZE;
9240
9241                 pent->plt.offset = s->size;
9242
9243                 /* Make room for this entry.  */
9244                 s->size += PLT_ENTRY_SIZE;
9245
9246                 /* Make room for the .glink code.  */
9247                 s = htab->glink;
9248                 if (s->size == 0)
9249                   s->size += GLINK_CALL_STUB_SIZE;
9250                 /* We need bigger stubs past index 32767.  */
9251                 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9252                   s->size += 4;
9253                 s->size += 2*4;
9254
9255                 /* We also need to make an entry in the .rela.plt section.  */
9256                 s = htab->relplt;
9257               }
9258             s->size += sizeof (Elf64_External_Rela);
9259             doneone = TRUE;
9260           }
9261         else
9262           pent->plt.offset = (bfd_vma) -1;
9263       if (!doneone)
9264         {
9265           h->plt.plist = NULL;
9266           h->needs_plt = 0;
9267         }
9268     }
9269   else
9270     {
9271       h->plt.plist = NULL;
9272       h->needs_plt = 0;
9273     }
9274
9275   eh = (struct ppc_link_hash_entry *) h;
9276   /* Run through the TLS GD got entries first if we're changing them
9277      to TPREL.  */
9278   if ((eh->tls_mask & TLS_TPRELGD) != 0)
9279     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9280       if (gent->got.refcount > 0
9281           && (gent->tls_type & TLS_GD) != 0)
9282         {
9283           /* This was a GD entry that has been converted to TPREL.  If
9284              there happens to be a TPREL entry we can use that one.  */
9285           struct got_entry *ent;
9286           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9287             if (ent->got.refcount > 0
9288                 && (ent->tls_type & TLS_TPREL) != 0
9289                 && ent->addend == gent->addend
9290                 && ent->owner == gent->owner)
9291               {
9292                 gent->got.refcount = 0;
9293                 break;
9294               }
9295
9296           /* If not, then we'll be using our own TPREL entry.  */
9297           if (gent->got.refcount != 0)
9298             gent->tls_type = TLS_TLS | TLS_TPREL;
9299         }
9300
9301   /* Remove any list entry that won't generate a word in the GOT before
9302      we call merge_got_entries.  Otherwise we risk merging to empty
9303      entries.  */
9304   pgent = &h->got.glist;
9305   while ((gent = *pgent) != NULL)
9306     if (gent->got.refcount > 0)
9307       {
9308         if ((gent->tls_type & TLS_LD) != 0
9309             && !h->def_dynamic)
9310           {
9311             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9312             *pgent = gent->next;
9313           }
9314         else
9315           pgent = &gent->next;
9316       }
9317     else
9318       *pgent = gent->next;
9319
9320   if (!htab->do_multi_toc)
9321     merge_got_entries (&h->got.glist);
9322
9323   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9324     if (!gent->is_indirect)
9325       {
9326         /* Make sure this symbol is output as a dynamic symbol.
9327            Undefined weak syms won't yet be marked as dynamic,
9328            nor will all TLS symbols.  */
9329         if (h->dynindx == -1
9330             && !h->forced_local
9331             && h->type != STT_GNU_IFUNC
9332             && htab->elf.dynamic_sections_created)
9333           {
9334             if (! bfd_elf_link_record_dynamic_symbol (info, h))
9335               return FALSE;
9336           }
9337
9338         if (!is_ppc64_elf (gent->owner))
9339           abort ();
9340
9341         allocate_got (h, info, gent);
9342       }
9343
9344   if (eh->dyn_relocs == NULL
9345       || (!htab->elf.dynamic_sections_created
9346           && h->type != STT_GNU_IFUNC))
9347     return TRUE;
9348
9349   /* In the shared -Bsymbolic case, discard space allocated for
9350      dynamic pc-relative relocs against symbols which turn out to be
9351      defined in regular objects.  For the normal shared case, discard
9352      space for relocs that have become local due to symbol visibility
9353      changes.  */
9354
9355   if (info->shared)
9356     {
9357       /* Relocs that use pc_count are those that appear on a call insn,
9358          or certain REL relocs (see must_be_dyn_reloc) that can be
9359          generated via assembly.  We want calls to protected symbols to
9360          resolve directly to the function rather than going via the plt.
9361          If people want function pointer comparisons to work as expected
9362          then they should avoid writing weird assembly.  */
9363       if (SYMBOL_CALLS_LOCAL (info, h))
9364         {
9365           struct elf_dyn_relocs **pp;
9366
9367           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9368             {
9369               p->count -= p->pc_count;
9370               p->pc_count = 0;
9371               if (p->count == 0)
9372                 *pp = p->next;
9373               else
9374                 pp = &p->next;
9375             }
9376         }
9377
9378       /* Also discard relocs on undefined weak syms with non-default
9379          visibility.  */
9380       if (eh->dyn_relocs != NULL
9381           && h->root.type == bfd_link_hash_undefweak)
9382         {
9383           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9384             eh->dyn_relocs = NULL;
9385
9386           /* Make sure this symbol is output as a dynamic symbol.
9387              Undefined weak syms won't yet be marked as dynamic.  */
9388           else if (h->dynindx == -1
9389                    && !h->forced_local)
9390             {
9391               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9392                 return FALSE;
9393             }
9394         }
9395     }
9396   else if (h->type == STT_GNU_IFUNC)
9397     {
9398       if (!h->non_got_ref)
9399         eh->dyn_relocs = NULL;
9400     }
9401   else if (ELIMINATE_COPY_RELOCS)
9402     {
9403       /* For the non-shared case, discard space for relocs against
9404          symbols which turn out to need copy relocs or are not
9405          dynamic.  */
9406
9407       if (!h->non_got_ref
9408           && !h->def_regular)
9409         {
9410           /* Make sure this symbol is output as a dynamic symbol.
9411              Undefined weak syms won't yet be marked as dynamic.  */
9412           if (h->dynindx == -1
9413               && !h->forced_local)
9414             {
9415               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9416                 return FALSE;
9417             }
9418
9419           /* If that succeeded, we know we'll be keeping all the
9420              relocs.  */
9421           if (h->dynindx != -1)
9422             goto keep;
9423         }
9424
9425       eh->dyn_relocs = NULL;
9426
9427     keep: ;
9428     }
9429
9430   /* Finally, allocate space.  */
9431   for (p = eh->dyn_relocs; p != NULL; p = p->next)
9432     {
9433       asection *sreloc = elf_section_data (p->sec)->sreloc;
9434       if (eh->elf.type == STT_GNU_IFUNC)
9435         sreloc = htab->reliplt;
9436       sreloc->size += p->count * sizeof (Elf64_External_Rela);
9437     }
9438
9439   return TRUE;
9440 }
9441
9442 /* Find any dynamic relocs that apply to read-only sections.  */
9443
9444 static bfd_boolean
9445 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9446 {
9447   struct ppc_link_hash_entry *eh;
9448   struct elf_dyn_relocs *p;
9449
9450   eh = (struct ppc_link_hash_entry *) h;
9451   for (p = eh->dyn_relocs; p != NULL; p = p->next)
9452     {
9453       asection *s = p->sec->output_section;
9454
9455       if (s != NULL && (s->flags & SEC_READONLY) != 0)
9456         {
9457           struct bfd_link_info *info = inf;
9458
9459           info->flags |= DF_TEXTREL;
9460
9461           /* Not an error, just cut short the traversal.  */
9462           return FALSE;
9463         }
9464     }
9465   return TRUE;
9466 }
9467
9468 /* Set the sizes of the dynamic sections.  */
9469
9470 static bfd_boolean
9471 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9472                                  struct bfd_link_info *info)
9473 {
9474   struct ppc_link_hash_table *htab;
9475   bfd *dynobj;
9476   asection *s;
9477   bfd_boolean relocs;
9478   bfd *ibfd;
9479   struct got_entry *first_tlsld;
9480
9481   htab = ppc_hash_table (info);
9482   if (htab == NULL)
9483     return FALSE;
9484
9485   dynobj = htab->elf.dynobj;
9486   if (dynobj == NULL)
9487     abort ();
9488
9489   if (htab->elf.dynamic_sections_created)
9490     {
9491       /* Set the contents of the .interp section to the interpreter.  */
9492       if (info->executable)
9493         {
9494           s = bfd_get_linker_section (dynobj, ".interp");
9495           if (s == NULL)
9496             abort ();
9497           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9498           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9499         }
9500     }
9501
9502   /* Set up .got offsets for local syms, and space for local dynamic
9503      relocs.  */
9504   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9505     {
9506       struct got_entry **lgot_ents;
9507       struct got_entry **end_lgot_ents;
9508       struct plt_entry **local_plt;
9509       struct plt_entry **end_local_plt;
9510       unsigned char *lgot_masks;
9511       bfd_size_type locsymcount;
9512       Elf_Internal_Shdr *symtab_hdr;
9513
9514       if (!is_ppc64_elf (ibfd))
9515         continue;
9516
9517       for (s = ibfd->sections; s != NULL; s = s->next)
9518         {
9519           struct ppc_dyn_relocs *p;
9520
9521           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9522             {
9523               if (!bfd_is_abs_section (p->sec)
9524                   && bfd_is_abs_section (p->sec->output_section))
9525                 {
9526                   /* Input section has been discarded, either because
9527                      it is a copy of a linkonce section or due to
9528                      linker script /DISCARD/, so we'll be discarding
9529                      the relocs too.  */
9530                 }
9531               else if (p->count != 0)
9532                 {
9533                   asection *srel = elf_section_data (p->sec)->sreloc;
9534                   if (p->ifunc)
9535                     srel = htab->reliplt;
9536                   srel->size += p->count * sizeof (Elf64_External_Rela);
9537                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9538                     info->flags |= DF_TEXTREL;
9539                 }
9540             }
9541         }
9542
9543       lgot_ents = elf_local_got_ents (ibfd);
9544       if (!lgot_ents)
9545         continue;
9546
9547       symtab_hdr = &elf_symtab_hdr (ibfd);
9548       locsymcount = symtab_hdr->sh_info;
9549       end_lgot_ents = lgot_ents + locsymcount;
9550       local_plt = (struct plt_entry **) end_lgot_ents;
9551       end_local_plt = local_plt + locsymcount;
9552       lgot_masks = (unsigned char *) end_local_plt;
9553       s = ppc64_elf_tdata (ibfd)->got;
9554       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9555         {
9556           struct got_entry **pent, *ent;
9557
9558           pent = lgot_ents;
9559           while ((ent = *pent) != NULL)
9560             if (ent->got.refcount > 0)
9561               {
9562                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9563                   {
9564                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
9565                     *pent = ent->next;
9566                   }
9567                 else
9568                   {
9569                     unsigned int ent_size = 8;
9570                     unsigned int rel_size = sizeof (Elf64_External_Rela);
9571
9572                     ent->got.offset = s->size;
9573                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9574                       {
9575                         ent_size *= 2;
9576                         rel_size *= 2;
9577                       }
9578                     s->size += ent_size;
9579                     if ((*lgot_masks & PLT_IFUNC) != 0)
9580                       {
9581                         htab->reliplt->size += rel_size;
9582                         htab->got_reli_size += rel_size;
9583                       }
9584                     else if (info->shared)
9585                       {
9586                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9587                         srel->size += rel_size;
9588                       }
9589                     pent = &ent->next;
9590                   }
9591               }
9592             else
9593               *pent = ent->next;
9594         }
9595
9596       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
9597       for (; local_plt < end_local_plt; ++local_plt)
9598         {
9599           struct plt_entry *ent;
9600
9601           for (ent = *local_plt; ent != NULL; ent = ent->next)
9602             if (ent->plt.refcount > 0)
9603               {
9604                 s = htab->iplt;
9605                 ent->plt.offset = s->size;
9606                 s->size += PLT_ENTRY_SIZE;
9607
9608                 htab->reliplt->size += sizeof (Elf64_External_Rela);
9609               }
9610             else
9611               ent->plt.offset = (bfd_vma) -1;
9612         }
9613     }
9614
9615   /* Allocate global sym .plt and .got entries, and space for global
9616      sym dynamic relocs.  */
9617   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9618
9619   first_tlsld = NULL;
9620   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9621     {
9622       struct got_entry *ent;
9623
9624       if (!is_ppc64_elf (ibfd))
9625         continue;
9626
9627       ent = ppc64_tlsld_got (ibfd);
9628       if (ent->got.refcount > 0)
9629         {
9630           if (!htab->do_multi_toc && first_tlsld != NULL)
9631             {
9632               ent->is_indirect = TRUE;
9633               ent->got.ent = first_tlsld;
9634             }
9635           else
9636             {
9637               if (first_tlsld == NULL)
9638                 first_tlsld = ent;
9639               s = ppc64_elf_tdata (ibfd)->got;
9640               ent->got.offset = s->size;
9641               ent->owner = ibfd;
9642               s->size += 16;
9643               if (info->shared)
9644                 {
9645                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9646                   srel->size += sizeof (Elf64_External_Rela);
9647                 }
9648             }
9649         }
9650       else
9651         ent->got.offset = (bfd_vma) -1;
9652     }
9653
9654   /* We now have determined the sizes of the various dynamic sections.
9655      Allocate memory for them.  */
9656   relocs = FALSE;
9657   for (s = dynobj->sections; s != NULL; s = s->next)
9658     {
9659       if ((s->flags & SEC_LINKER_CREATED) == 0)
9660         continue;
9661
9662       if (s == htab->brlt || s == htab->relbrlt)
9663         /* These haven't been allocated yet;  don't strip.  */
9664         continue;
9665       else if (s == htab->got
9666                || s == htab->plt
9667                || s == htab->iplt
9668                || s == htab->glink
9669                || s == htab->dynbss)
9670         {
9671           /* Strip this section if we don't need it; see the
9672              comment below.  */
9673         }
9674       else if (s == htab->glink_eh_frame)
9675         {
9676           if (!bfd_is_abs_section (s->output_section))
9677             /* Not sized yet.  */
9678             continue;
9679         }
9680       else if (CONST_STRNEQ (s->name, ".rela"))
9681         {
9682           if (s->size != 0)
9683             {
9684               if (s != htab->relplt)
9685                 relocs = TRUE;
9686
9687               /* We use the reloc_count field as a counter if we need
9688                  to copy relocs into the output file.  */
9689               s->reloc_count = 0;
9690             }
9691         }
9692       else
9693         {
9694           /* It's not one of our sections, so don't allocate space.  */
9695           continue;
9696         }
9697
9698       if (s->size == 0)
9699         {
9700           /* If we don't need this section, strip it from the
9701              output file.  This is mostly to handle .rela.bss and
9702              .rela.plt.  We must create both sections in
9703              create_dynamic_sections, because they must be created
9704              before the linker maps input sections to output
9705              sections.  The linker does that before
9706              adjust_dynamic_symbol is called, and it is that
9707              function which decides whether anything needs to go
9708              into these sections.  */
9709           s->flags |= SEC_EXCLUDE;
9710           continue;
9711         }
9712
9713       if ((s->flags & SEC_HAS_CONTENTS) == 0)
9714         continue;
9715
9716       /* Allocate memory for the section contents.  We use bfd_zalloc
9717          here in case unused entries are not reclaimed before the
9718          section's contents are written out.  This should not happen,
9719          but this way if it does we get a R_PPC64_NONE reloc in .rela
9720          sections instead of garbage.
9721          We also rely on the section contents being zero when writing
9722          the GOT.  */
9723       s->contents = bfd_zalloc (dynobj, s->size);
9724       if (s->contents == NULL)
9725         return FALSE;
9726     }
9727
9728   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9729     {
9730       if (!is_ppc64_elf (ibfd))
9731         continue;
9732
9733       s = ppc64_elf_tdata (ibfd)->got;
9734       if (s != NULL && s != htab->got)
9735         {
9736           if (s->size == 0)
9737             s->flags |= SEC_EXCLUDE;
9738           else
9739             {
9740               s->contents = bfd_zalloc (ibfd, s->size);
9741               if (s->contents == NULL)
9742                 return FALSE;
9743             }
9744         }
9745       s = ppc64_elf_tdata (ibfd)->relgot;
9746       if (s != NULL)
9747         {
9748           if (s->size == 0)
9749             s->flags |= SEC_EXCLUDE;
9750           else
9751             {
9752               s->contents = bfd_zalloc (ibfd, s->size);
9753               if (s->contents == NULL)
9754                 return FALSE;
9755               relocs = TRUE;
9756               s->reloc_count = 0;
9757             }
9758         }
9759     }
9760
9761   if (htab->elf.dynamic_sections_created)
9762     {
9763       /* Add some entries to the .dynamic section.  We fill in the
9764          values later, in ppc64_elf_finish_dynamic_sections, but we
9765          must add the entries now so that we get the correct size for
9766          the .dynamic section.  The DT_DEBUG entry is filled in by the
9767          dynamic linker and used by the debugger.  */
9768 #define add_dynamic_entry(TAG, VAL) \
9769   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9770
9771       if (info->executable)
9772         {
9773           if (!add_dynamic_entry (DT_DEBUG, 0))
9774             return FALSE;
9775         }
9776
9777       if (htab->plt != NULL && htab->plt->size != 0)
9778         {
9779           if (!add_dynamic_entry (DT_PLTGOT, 0)
9780               || !add_dynamic_entry (DT_PLTRELSZ, 0)
9781               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9782               || !add_dynamic_entry (DT_JMPREL, 0)
9783               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9784             return FALSE;
9785         }
9786
9787       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
9788         {
9789           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9790               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9791             return FALSE;
9792         }
9793
9794       if (!htab->no_tls_get_addr_opt
9795           && htab->tls_get_addr_fd != NULL
9796           && htab->tls_get_addr_fd->elf.plt.plist != NULL
9797           && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9798         return FALSE;
9799
9800       if (relocs)
9801         {
9802           if (!add_dynamic_entry (DT_RELA, 0)
9803               || !add_dynamic_entry (DT_RELASZ, 0)
9804               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9805             return FALSE;
9806
9807           /* If any dynamic relocs apply to a read-only section,
9808              then we need a DT_TEXTREL entry.  */
9809           if ((info->flags & DF_TEXTREL) == 0)
9810             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
9811
9812           if ((info->flags & DF_TEXTREL) != 0)
9813             {
9814               if (!add_dynamic_entry (DT_TEXTREL, 0))
9815                 return FALSE;
9816             }
9817         }
9818     }
9819 #undef add_dynamic_entry
9820
9821   return TRUE;
9822 }
9823
9824 /* Determine the type of stub needed, if any, for a call.  */
9825
9826 static inline enum ppc_stub_type
9827 ppc_type_of_stub (asection *input_sec,
9828                   const Elf_Internal_Rela *rel,
9829                   struct ppc_link_hash_entry **hash,
9830                   struct plt_entry **plt_ent,
9831                   bfd_vma destination,
9832                   unsigned long local_off)
9833 {
9834   struct ppc_link_hash_entry *h = *hash;
9835   bfd_vma location;
9836   bfd_vma branch_offset;
9837   bfd_vma max_branch_offset;
9838   enum elf_ppc64_reloc_type r_type;
9839
9840   if (h != NULL)
9841     {
9842       struct plt_entry *ent;
9843       struct ppc_link_hash_entry *fdh = h;
9844       if (h->oh != NULL
9845           && h->oh->is_func_descriptor)
9846         {
9847           fdh = ppc_follow_link (h->oh);
9848           *hash = fdh;
9849         }
9850
9851       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9852         if (ent->addend == rel->r_addend
9853             && ent->plt.offset != (bfd_vma) -1)
9854           {
9855             *plt_ent = ent;
9856             return ppc_stub_plt_call;
9857           }
9858
9859       /* Here, we know we don't have a plt entry.  If we don't have a
9860          either a defined function descriptor or a defined entry symbol
9861          in a regular object file, then it is pointless trying to make
9862          any other type of stub.  */
9863       if (!is_static_defined (&fdh->elf)
9864           && !is_static_defined (&h->elf))
9865         return ppc_stub_none;
9866     }
9867   else if (elf_local_got_ents (input_sec->owner) != NULL)
9868     {
9869       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9870       struct plt_entry **local_plt = (struct plt_entry **)
9871         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9872       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9873
9874       if (local_plt[r_symndx] != NULL)
9875         {
9876           struct plt_entry *ent;
9877
9878           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9879             if (ent->addend == rel->r_addend
9880                 && ent->plt.offset != (bfd_vma) -1)
9881               {
9882                 *plt_ent = ent;
9883                 return ppc_stub_plt_call;
9884               }
9885         }
9886     }
9887
9888   /* Determine where the call point is.  */
9889   location = (input_sec->output_offset
9890               + input_sec->output_section->vma
9891               + rel->r_offset);
9892
9893   branch_offset = destination - location;
9894   r_type = ELF64_R_TYPE (rel->r_info);
9895
9896   /* Determine if a long branch stub is needed.  */
9897   max_branch_offset = 1 << 25;
9898   if (r_type != R_PPC64_REL24)
9899     max_branch_offset = 1 << 15;
9900
9901   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
9902     /* We need a stub.  Figure out whether a long_branch or plt_branch
9903        is needed later.  */
9904     return ppc_stub_long_branch;
9905
9906   return ppc_stub_none;
9907 }
9908
9909 /* With power7 weakly ordered memory model, it is possible for ld.so
9910    to update a plt entry in one thread and have another thread see a
9911    stale zero toc entry.  To avoid this we need some sort of acquire
9912    barrier in the call stub.  One solution is to make the load of the
9913    toc word seem to appear to depend on the load of the function entry
9914    word.  Another solution is to test for r2 being zero, and branch to
9915    the appropriate glink entry if so.
9916
9917    .    fake dep barrier        compare
9918    .    ld 12,xxx(2)            ld 12,xxx(2)
9919    .    mtctr 12                mtctr 12
9920    .    xor 11,12,12            ld 2,xxx+8(2)
9921    .    add 2,2,11              cmpldi 2,0
9922    .    ld 2,xxx+8(2)           bnectr+
9923    .    bctr                    b <glink_entry>
9924
9925    The solution involving the compare turns out to be faster, so
9926    that's what we use unless the branch won't reach.  */
9927
9928 #define ALWAYS_USE_FAKE_DEP 0
9929 #define ALWAYS_EMIT_R2SAVE 0
9930
9931 #define PPC_LO(v) ((v) & 0xffff)
9932 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9933 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9934
9935 static inline unsigned int
9936 plt_stub_size (struct ppc_link_hash_table *htab,
9937                struct ppc_stub_hash_entry *stub_entry,
9938                bfd_vma off)
9939 {
9940   unsigned size = PLT_CALL_STUB_SIZE;
9941
9942   if (!(ALWAYS_EMIT_R2SAVE
9943         || stub_entry->stub_type == ppc_stub_plt_call_r2save))
9944     size -= 4;
9945   if (!htab->plt_static_chain)
9946     size -= 4;
9947   if (htab->plt_thread_safe)
9948     size += 8;
9949   if (PPC_HA (off) == 0)
9950     size -= 4;
9951   if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
9952     size += 4;
9953   if (stub_entry->h != NULL
9954       && (stub_entry->h == htab->tls_get_addr_fd
9955           || stub_entry->h == htab->tls_get_addr)
9956       && !htab->no_tls_get_addr_opt)
9957     size += 13 * 4;
9958   return size;
9959 }
9960
9961 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
9962    then return the padding needed to do so.  */
9963 static inline unsigned int
9964 plt_stub_pad (struct ppc_link_hash_table *htab,
9965               struct ppc_stub_hash_entry *stub_entry,
9966               bfd_vma plt_off)
9967 {
9968   int stub_align = 1 << htab->plt_stub_align;
9969   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
9970   bfd_vma stub_off = stub_entry->stub_sec->size;
9971
9972   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
9973       > (stub_size & -stub_align))
9974     return stub_align - (stub_off & (stub_align - 1));
9975   return 0;
9976 }
9977
9978 /* Build a .plt call stub.  */
9979
9980 static inline bfd_byte *
9981 build_plt_stub (struct ppc_link_hash_table *htab,
9982                 struct ppc_stub_hash_entry *stub_entry,
9983                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9984 {
9985   bfd *obfd = htab->stub_bfd;
9986   bfd_boolean plt_static_chain = htab->plt_static_chain;
9987   bfd_boolean plt_thread_safe = htab->plt_thread_safe;
9988   bfd_boolean use_fake_dep = plt_thread_safe;
9989   bfd_vma cmp_branch_off = 0;
9990
9991   if (!ALWAYS_USE_FAKE_DEP
9992       && plt_thread_safe
9993       && !(stub_entry->h != NULL
9994            && (stub_entry->h == htab->tls_get_addr_fd
9995                || stub_entry->h == htab->tls_get_addr)
9996            && !htab->no_tls_get_addr_opt))
9997     {
9998       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
9999       bfd_vma pltindex = (pltoff - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE;
10000       bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10001       bfd_vma to, from;
10002
10003       if (pltindex > 32768)
10004         glinkoff += (pltindex - 32768) * 4;
10005       to = (glinkoff
10006             + htab->glink->output_offset
10007             + htab->glink->output_section->vma);
10008       from = (p - stub_entry->stub_sec->contents
10009               + 4 * (ALWAYS_EMIT_R2SAVE
10010                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10011               + 4 * (PPC_HA (offset) != 0)
10012               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10013                      != PPC_HA (offset))
10014               + 4 * (plt_static_chain != 0)
10015               + 20
10016               + stub_entry->stub_sec->output_offset
10017               + stub_entry->stub_sec->output_section->vma);
10018       cmp_branch_off = to - from;
10019       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10020     }
10021
10022   if (PPC_HA (offset) != 0)
10023     {
10024       if (r != NULL)
10025         {
10026           if (ALWAYS_EMIT_R2SAVE
10027               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10028             r[0].r_offset += 4;
10029           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10030           r[1].r_offset = r[0].r_offset + 4;
10031           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10032           r[1].r_addend = r[0].r_addend;
10033           if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10034             {
10035               r[2].r_offset = r[1].r_offset + 4;
10036               r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10037               r[2].r_addend = r[0].r_addend;
10038             }
10039           else
10040             {
10041               r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10042               r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10043               r[2].r_addend = r[0].r_addend + 8;
10044               if (plt_static_chain)
10045                 {
10046                   r[3].r_offset = r[2].r_offset + 4;
10047                   r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10048                   r[3].r_addend = r[0].r_addend + 16;
10049                 }
10050             }
10051         }
10052       if (ALWAYS_EMIT_R2SAVE
10053           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10054         bfd_put_32 (obfd, STD_R2_40R1, p),                      p += 4;
10055       bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p),     p += 4;
10056       bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),      p += 4;
10057       if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10058         {
10059           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10060           offset = 0;
10061         }
10062       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10063       if (use_fake_dep)
10064         {
10065           bfd_put_32 (obfd, XOR_R2_R12_R12, p),                 p += 4;
10066           bfd_put_32 (obfd, ADD_R11_R11_R2, p),                 p += 4;
10067         }
10068       bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p),   p += 4;
10069       if (plt_static_chain)
10070         bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10071     }
10072   else
10073     {
10074       if (r != NULL)
10075         {
10076           if (ALWAYS_EMIT_R2SAVE
10077               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10078             r[0].r_offset += 4;
10079           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10080           if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10081             {
10082               r[1].r_offset = r[0].r_offset + 4;
10083               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10084               r[1].r_addend = r[0].r_addend;
10085             }
10086           else
10087             {
10088               r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10089               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10090               r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10091               if (plt_static_chain)
10092                 {
10093                   r[2].r_offset = r[1].r_offset + 4;
10094                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10095                   r[2].r_addend = r[0].r_addend + 8;
10096                 }
10097             }
10098         }
10099       if (ALWAYS_EMIT_R2SAVE
10100           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10101         bfd_put_32 (obfd, STD_R2_40R1, p),                      p += 4;
10102       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10103       if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10104         {
10105           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10106           offset = 0;
10107         }
10108       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10109       if (use_fake_dep)
10110         {
10111           bfd_put_32 (obfd, XOR_R11_R12_R12, p),                p += 4;
10112           bfd_put_32 (obfd, ADD_R2_R2_R11, p),                  p += 4;
10113         }
10114       if (plt_static_chain)
10115         bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10116       bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p),    p += 4;
10117     }
10118   if (plt_thread_safe && !use_fake_dep)
10119     {
10120       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
10121       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
10122       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10123     }
10124   else
10125     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
10126   return p;
10127 }
10128
10129 /* Build a special .plt call stub for __tls_get_addr.  */
10130
10131 #define LD_R11_0R3      0xe9630000
10132 #define LD_R12_0R3      0xe9830000
10133 #define MR_R0_R3        0x7c601b78
10134 #define CMPDI_R11_0     0x2c2b0000
10135 #define ADD_R3_R12_R13  0x7c6c6a14
10136 #define BEQLR           0x4d820020
10137 #define MR_R3_R0        0x7c030378
10138 #define MFLR_R11        0x7d6802a6
10139 #define STD_R11_0R1     0xf9610000
10140 #define BCTRL           0x4e800421
10141 #define LD_R11_0R1      0xe9610000
10142 #define LD_R2_0R1       0xe8410000
10143 #define MTLR_R11        0x7d6803a6
10144
10145 static inline bfd_byte *
10146 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10147                          struct ppc_stub_hash_entry *stub_entry,
10148                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10149 {
10150   bfd *obfd = htab->stub_bfd;
10151
10152   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
10153   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
10154   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
10155   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
10156   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
10157   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
10158   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
10159   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
10160   bfd_put_32 (obfd, STD_R11_0R1 + 32, p),       p += 4;
10161
10162   if (r != NULL)
10163     r[0].r_offset += 9 * 4;
10164   p = build_plt_stub (htab, stub_entry, p, offset, r);
10165   bfd_put_32 (obfd, BCTRL, p - 4);
10166
10167   bfd_put_32 (obfd, LD_R11_0R1 + 32, p),        p += 4;
10168   bfd_put_32 (obfd, LD_R2_0R1 + 40, p),         p += 4;
10169   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
10170   bfd_put_32 (obfd, BLR, p),                    p += 4;
10171
10172   return p;
10173 }
10174
10175 static Elf_Internal_Rela *
10176 get_relocs (asection *sec, int count)
10177 {
10178   Elf_Internal_Rela *relocs;
10179   struct bfd_elf_section_data *elfsec_data;
10180
10181   elfsec_data = elf_section_data (sec);
10182   relocs = elfsec_data->relocs;
10183   if (relocs == NULL)
10184     {
10185       bfd_size_type relsize;
10186       relsize = sec->reloc_count * sizeof (*relocs);
10187       relocs = bfd_alloc (sec->owner, relsize);
10188       if (relocs == NULL)
10189         return NULL;
10190       elfsec_data->relocs = relocs;
10191       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10192                                           sizeof (Elf_Internal_Shdr));
10193       if (elfsec_data->rela.hdr == NULL)
10194         return NULL;
10195       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10196                                         * sizeof (Elf64_External_Rela));
10197       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10198       sec->reloc_count = 0;
10199     }
10200   relocs += sec->reloc_count;
10201   sec->reloc_count += count;
10202   return relocs;
10203 }
10204
10205 static bfd_vma
10206 get_r2off (struct bfd_link_info *info,
10207            struct ppc_stub_hash_entry *stub_entry)
10208 {
10209   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10210   bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
10211
10212   if (r2off == 0)
10213     {
10214       /* Support linking -R objects.  Get the toc pointer from the
10215          opd entry.  */
10216       char buf[8];
10217       asection *opd = stub_entry->h->elf.root.u.def.section;
10218       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10219
10220       if (strcmp (opd->name, ".opd") != 0
10221           || opd->reloc_count != 0)
10222         {
10223           info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10224                                   stub_entry->h->elf.root.root.string);
10225           bfd_set_error (bfd_error_bad_value);
10226           return 0;
10227         }
10228       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10229         return 0;
10230       r2off = bfd_get_64 (opd->owner, buf);
10231       r2off -= elf_gp (info->output_bfd);
10232     }
10233   r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
10234   return r2off;
10235 }
10236
10237 static bfd_boolean
10238 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10239 {
10240   struct ppc_stub_hash_entry *stub_entry;
10241   struct ppc_branch_hash_entry *br_entry;
10242   struct bfd_link_info *info;
10243   struct ppc_link_hash_table *htab;
10244   bfd_byte *loc;
10245   bfd_byte *p;
10246   bfd_vma dest, off;
10247   int size;
10248   Elf_Internal_Rela *r;
10249   asection *plt;
10250
10251   /* Massage our args to the form they really have.  */
10252   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10253   info = in_arg;
10254
10255   htab = ppc_hash_table (info);
10256   if (htab == NULL)
10257     return FALSE;
10258
10259   /* Make a note of the offset within the stubs for this entry.  */
10260   stub_entry->stub_offset = stub_entry->stub_sec->size;
10261   loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
10262
10263   htab->stub_count[stub_entry->stub_type - 1] += 1;
10264   switch (stub_entry->stub_type)
10265     {
10266     case ppc_stub_long_branch:
10267     case ppc_stub_long_branch_r2off:
10268       /* Branches are relative.  This is where we are going to.  */
10269       dest = (stub_entry->target_value
10270               + stub_entry->target_section->output_offset
10271               + stub_entry->target_section->output_section->vma);
10272       dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10273       off = dest;
10274
10275       /* And this is where we are coming from.  */
10276       off -= (stub_entry->stub_offset
10277               + stub_entry->stub_sec->output_offset
10278               + stub_entry->stub_sec->output_section->vma);
10279
10280       size = 4;
10281       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10282         {
10283           bfd_vma r2off = get_r2off (info, stub_entry);
10284
10285           if (r2off == 0)
10286             {
10287               htab->stub_error = TRUE;
10288               return FALSE;
10289             }
10290           bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
10291           loc += 4;
10292           size = 12;
10293           if (PPC_HA (r2off) != 0)
10294             {
10295               size = 16;
10296               bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10297               loc += 4;
10298             }
10299           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
10300           loc += 4;
10301           off -= size - 4;
10302         }
10303       bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10304
10305       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10306         {
10307           info->callbacks->einfo
10308             (_("%P: long branch stub `%s' offset overflow\n"),
10309              stub_entry->root.string);
10310           htab->stub_error = TRUE;
10311           return FALSE;
10312         }
10313
10314       if (info->emitrelocations)
10315         {
10316           r = get_relocs (stub_entry->stub_sec, 1);
10317           if (r == NULL)
10318             return FALSE;
10319           r->r_offset = loc - stub_entry->stub_sec->contents;
10320           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10321           r->r_addend = dest;
10322           if (stub_entry->h != NULL)
10323             {
10324               struct elf_link_hash_entry **hashes;
10325               unsigned long symndx;
10326               struct ppc_link_hash_entry *h;
10327
10328               hashes = elf_sym_hashes (htab->stub_bfd);
10329               if (hashes == NULL)
10330                 {
10331                   bfd_size_type hsize;
10332
10333                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10334                   hashes = bfd_zalloc (htab->stub_bfd, hsize);
10335                   if (hashes == NULL)
10336                     return FALSE;
10337                   elf_sym_hashes (htab->stub_bfd) = hashes;
10338                   htab->stub_globals = 1;
10339                 }
10340               symndx = htab->stub_globals++;
10341               h = stub_entry->h;
10342               hashes[symndx] = &h->elf;
10343               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10344               if (h->oh != NULL && h->oh->is_func)
10345                 h = ppc_follow_link (h->oh);
10346               if (h->elf.root.u.def.section != stub_entry->target_section)
10347                 /* H is an opd symbol.  The addend must be zero.  */
10348                 r->r_addend = 0;
10349               else
10350                 {
10351                   off = (h->elf.root.u.def.value
10352                          + h->elf.root.u.def.section->output_offset
10353                          + h->elf.root.u.def.section->output_section->vma);
10354                   r->r_addend -= off;
10355                 }
10356             }
10357         }
10358       break;
10359
10360     case ppc_stub_plt_branch:
10361     case ppc_stub_plt_branch_r2off:
10362       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10363                                          stub_entry->root.string + 9,
10364                                          FALSE, FALSE);
10365       if (br_entry == NULL)
10366         {
10367           info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10368                                   stub_entry->root.string);
10369           htab->stub_error = TRUE;
10370           return FALSE;
10371         }
10372
10373       dest = (stub_entry->target_value
10374               + stub_entry->target_section->output_offset
10375               + stub_entry->target_section->output_section->vma);
10376       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10377         dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10378
10379       bfd_put_64 (htab->brlt->owner, dest,
10380                   htab->brlt->contents + br_entry->offset);
10381
10382       if (br_entry->iter == htab->stub_iteration)
10383         {
10384           br_entry->iter = 0;
10385
10386           if (htab->relbrlt != NULL)
10387             {
10388               /* Create a reloc for the branch lookup table entry.  */
10389               Elf_Internal_Rela rela;
10390               bfd_byte *rl;
10391
10392               rela.r_offset = (br_entry->offset
10393                                + htab->brlt->output_offset
10394                                + htab->brlt->output_section->vma);
10395               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10396               rela.r_addend = dest;
10397
10398               rl = htab->relbrlt->contents;
10399               rl += (htab->relbrlt->reloc_count++
10400                      * sizeof (Elf64_External_Rela));
10401               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10402             }
10403           else if (info->emitrelocations)
10404             {
10405               r = get_relocs (htab->brlt, 1);
10406               if (r == NULL)
10407                 return FALSE;
10408               /* brlt, being SEC_LINKER_CREATED does not go through the
10409                  normal reloc processing.  Symbols and offsets are not
10410                  translated from input file to output file form, so
10411                  set up the offset per the output file.  */
10412               r->r_offset = (br_entry->offset
10413                              + htab->brlt->output_offset
10414                              + htab->brlt->output_section->vma);
10415               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10416               r->r_addend = dest;
10417             }
10418         }
10419
10420       dest = (br_entry->offset
10421               + htab->brlt->output_offset
10422               + htab->brlt->output_section->vma);
10423
10424       off = (dest
10425              - elf_gp (htab->brlt->output_section->owner)
10426              - htab->stub_group[stub_entry->id_sec->id].toc_off);
10427
10428       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10429         {
10430           info->callbacks->einfo
10431             (_("%P: linkage table error against `%T'\n"),
10432              stub_entry->root.string);
10433           bfd_set_error (bfd_error_bad_value);
10434           htab->stub_error = TRUE;
10435           return FALSE;
10436         }
10437
10438       if (info->emitrelocations)
10439         {
10440           r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
10441           if (r == NULL)
10442             return FALSE;
10443           r[0].r_offset = loc - stub_entry->stub_sec->contents;
10444           if (bfd_big_endian (info->output_bfd))
10445             r[0].r_offset += 2;
10446           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10447             r[0].r_offset += 4;
10448           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10449           r[0].r_addend = dest;
10450           if (PPC_HA (off) != 0)
10451             {
10452               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10453               r[1].r_offset = r[0].r_offset + 4;
10454               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10455               r[1].r_addend = r[0].r_addend;
10456             }
10457         }
10458
10459       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10460         {
10461           if (PPC_HA (off) != 0)
10462             {
10463               size = 16;
10464               bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
10465               loc += 4;
10466               bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
10467             }
10468           else
10469             {
10470               size = 12;
10471               bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
10472             }
10473         }
10474       else
10475         {
10476           bfd_vma r2off = get_r2off (info, stub_entry);
10477
10478           if (r2off == 0)
10479             {
10480               htab->stub_error = TRUE;
10481               return FALSE;
10482             }
10483
10484           bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
10485           loc += 4;
10486           size = 20;
10487           if (PPC_HA (off) != 0)
10488             {
10489               size += 4;
10490               bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
10491               loc += 4;
10492               bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
10493               loc += 4;
10494             }
10495           else
10496             {
10497               bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
10498               loc += 4;
10499             }
10500
10501           if (PPC_HA (r2off) != 0)
10502             {
10503               size += 4;
10504               bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10505               loc += 4;
10506             }
10507           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
10508         }
10509       loc += 4;
10510       bfd_put_32 (htab->stub_bfd, MTCTR_R12, loc);
10511       loc += 4;
10512       bfd_put_32 (htab->stub_bfd, BCTR, loc);
10513       break;
10514
10515     case ppc_stub_plt_call:
10516     case ppc_stub_plt_call_r2save:
10517       if (stub_entry->h != NULL
10518           && stub_entry->h->is_func_descriptor
10519           && stub_entry->h->oh != NULL)
10520         {
10521           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10522
10523           /* If the old-ABI "dot-symbol" is undefined make it weak so
10524              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
10525              FIXME: We used to define the symbol on one of the call
10526              stubs instead, which is why we test symbol section id
10527              against htab->top_id in various places.  Likely all
10528              these checks could now disappear.  */
10529           if (fh->elf.root.type == bfd_link_hash_undefined)
10530             fh->elf.root.type = bfd_link_hash_undefweak;
10531           /* Stop undo_symbol_twiddle changing it back to undefined.  */
10532           fh->was_undefined = 0;
10533         }
10534
10535       /* Now build the stub.  */
10536       dest = stub_entry->plt_ent->plt.offset & ~1;
10537       if (dest >= (bfd_vma) -2)
10538         abort ();
10539
10540       plt = htab->plt;
10541       if (!htab->elf.dynamic_sections_created
10542           || stub_entry->h == NULL
10543           || stub_entry->h->elf.dynindx == -1)
10544         plt = htab->iplt;
10545
10546       dest += plt->output_offset + plt->output_section->vma;
10547
10548       if (stub_entry->h == NULL
10549           && (stub_entry->plt_ent->plt.offset & 1) == 0)
10550         {
10551           Elf_Internal_Rela rela;
10552           bfd_byte *rl;
10553
10554           rela.r_offset = dest;
10555           if (htab->opd_abi)
10556             rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10557           else
10558             rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
10559           rela.r_addend = (stub_entry->target_value
10560                            + stub_entry->target_section->output_offset
10561                            + stub_entry->target_section->output_section->vma);
10562
10563           rl = (htab->reliplt->contents
10564                 + (htab->reliplt->reloc_count++
10565                    * sizeof (Elf64_External_Rela)));
10566           bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
10567           stub_entry->plt_ent->plt.offset |= 1;
10568         }
10569
10570       off = (dest
10571              - elf_gp (plt->output_section->owner)
10572              - htab->stub_group[stub_entry->id_sec->id].toc_off);
10573
10574       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10575         {
10576           info->callbacks->einfo
10577             (_("%P: linkage table error against `%T'\n"),
10578              stub_entry->h != NULL
10579              ? stub_entry->h->elf.root.root.string
10580              : "<local sym>");
10581           bfd_set_error (bfd_error_bad_value);
10582           htab->stub_error = TRUE;
10583           return FALSE;
10584         }
10585
10586       if (htab->plt_stub_align != 0)
10587         {
10588           unsigned pad = plt_stub_pad (htab, stub_entry, off);
10589
10590           stub_entry->stub_sec->size += pad;
10591           stub_entry->stub_offset = stub_entry->stub_sec->size;
10592           loc += pad;
10593         }
10594
10595       r = NULL;
10596       if (info->emitrelocations)
10597         {
10598           r = get_relocs (stub_entry->stub_sec,
10599                           (2
10600                            + (PPC_HA (off) != 0)
10601                            + (htab->plt_static_chain
10602                               && PPC_HA (off + 16) == PPC_HA (off))));
10603           if (r == NULL)
10604             return FALSE;
10605           r[0].r_offset = loc - stub_entry->stub_sec->contents;
10606           if (bfd_big_endian (info->output_bfd))
10607             r[0].r_offset += 2;
10608           r[0].r_addend = dest;
10609         }
10610       if (stub_entry->h != NULL
10611           && (stub_entry->h == htab->tls_get_addr_fd
10612               || stub_entry->h == htab->tls_get_addr)
10613           && !htab->no_tls_get_addr_opt)
10614         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10615       else
10616         p = build_plt_stub (htab, stub_entry, loc, off, r);
10617       size = p - loc;
10618       break;
10619
10620     default:
10621       BFD_FAIL ();
10622       return FALSE;
10623     }
10624
10625   stub_entry->stub_sec->size += size;
10626
10627   if (htab->emit_stub_syms)
10628     {
10629       struct elf_link_hash_entry *h;
10630       size_t len1, len2;
10631       char *name;
10632       const char *const stub_str[] = { "long_branch",
10633                                        "long_branch_r2off",
10634                                        "plt_branch",
10635                                        "plt_branch_r2off",
10636                                        "plt_call",
10637                                        "plt_call" };
10638
10639       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10640       len2 = strlen (stub_entry->root.string);
10641       name = bfd_malloc (len1 + len2 + 2);
10642       if (name == NULL)
10643         return FALSE;
10644       memcpy (name, stub_entry->root.string, 9);
10645       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10646       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10647       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10648       if (h == NULL)
10649         return FALSE;
10650       if (h->root.type == bfd_link_hash_new)
10651         {
10652           h->root.type = bfd_link_hash_defined;
10653           h->root.u.def.section = stub_entry->stub_sec;
10654           h->root.u.def.value = stub_entry->stub_offset;
10655           h->ref_regular = 1;
10656           h->def_regular = 1;
10657           h->ref_regular_nonweak = 1;
10658           h->forced_local = 1;
10659           h->non_elf = 0;
10660         }
10661     }
10662
10663   return TRUE;
10664 }
10665
10666 /* As above, but don't actually build the stub.  Just bump offset so
10667    we know stub section sizes, and select plt_branch stubs where
10668    long_branch stubs won't do.  */
10669
10670 static bfd_boolean
10671 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10672 {
10673   struct ppc_stub_hash_entry *stub_entry;
10674   struct bfd_link_info *info;
10675   struct ppc_link_hash_table *htab;
10676   bfd_vma off;
10677   int size;
10678
10679   /* Massage our args to the form they really have.  */
10680   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10681   info = in_arg;
10682
10683   htab = ppc_hash_table (info);
10684   if (htab == NULL)
10685     return FALSE;
10686
10687   if (stub_entry->stub_type == ppc_stub_plt_call
10688       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10689     {
10690       asection *plt;
10691       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
10692       if (off >= (bfd_vma) -2)
10693         abort ();
10694       plt = htab->plt;
10695       if (!htab->elf.dynamic_sections_created
10696           || stub_entry->h == NULL
10697           || stub_entry->h->elf.dynindx == -1)
10698         plt = htab->iplt;
10699       off += (plt->output_offset
10700               + plt->output_section->vma
10701               - elf_gp (plt->output_section->owner)
10702               - htab->stub_group[stub_entry->id_sec->id].toc_off);
10703
10704       size = plt_stub_size (htab, stub_entry, off);
10705       if (htab->plt_stub_align)
10706         size += plt_stub_pad (htab, stub_entry, off);
10707       if (info->emitrelocations)
10708         {
10709           stub_entry->stub_sec->reloc_count
10710             += (2
10711                 + (PPC_HA (off) != 0)
10712                 + (htab->plt_static_chain
10713                    && PPC_HA (off + 16) == PPC_HA (off)));
10714           stub_entry->stub_sec->flags |= SEC_RELOC;
10715         }
10716     }
10717   else
10718     {
10719       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10720          variants.  */
10721       bfd_vma r2off = 0;
10722       bfd_vma local_off = 0;
10723
10724       off = (stub_entry->target_value
10725              + stub_entry->target_section->output_offset
10726              + stub_entry->target_section->output_section->vma);
10727       off -= (stub_entry->stub_sec->size
10728               + stub_entry->stub_sec->output_offset
10729               + stub_entry->stub_sec->output_section->vma);
10730
10731       /* Reset the stub type from the plt variant in case we now
10732          can reach with a shorter stub.  */
10733       if (stub_entry->stub_type >= ppc_stub_plt_branch)
10734         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10735
10736       size = 4;
10737       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10738         {
10739           r2off = get_r2off (info, stub_entry);
10740           if (r2off == 0)
10741             {
10742               htab->stub_error = TRUE;
10743               return FALSE;
10744             }
10745           size = 12;
10746           if (PPC_HA (r2off) != 0)
10747             size = 16;
10748           off -= size - 4;
10749         }
10750
10751       local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10752
10753       /* If the branch offset if too big, use a ppc_stub_plt_branch.  */
10754       if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off)
10755         {
10756           struct ppc_branch_hash_entry *br_entry;
10757
10758           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10759                                              stub_entry->root.string + 9,
10760                                              TRUE, FALSE);
10761           if (br_entry == NULL)
10762             {
10763               info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
10764                                       stub_entry->root.string);
10765               htab->stub_error = TRUE;
10766               return FALSE;
10767             }
10768
10769           if (br_entry->iter != htab->stub_iteration)
10770             {
10771               br_entry->iter = htab->stub_iteration;
10772               br_entry->offset = htab->brlt->size;
10773               htab->brlt->size += 8;
10774
10775               if (htab->relbrlt != NULL)
10776                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10777               else if (info->emitrelocations)
10778                 {
10779                   htab->brlt->reloc_count += 1;
10780                   htab->brlt->flags |= SEC_RELOC;
10781                 }
10782             }
10783
10784           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
10785           off = (br_entry->offset
10786                  + htab->brlt->output_offset
10787                  + htab->brlt->output_section->vma
10788                  - elf_gp (htab->brlt->output_section->owner)
10789                  - htab->stub_group[stub_entry->id_sec->id].toc_off);
10790
10791           if (info->emitrelocations)
10792             {
10793               stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10794               stub_entry->stub_sec->flags |= SEC_RELOC;
10795             }
10796
10797           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10798             {
10799               size = 12;
10800               if (PPC_HA (off) != 0)
10801                 size = 16;
10802             }
10803           else
10804             {
10805               size = 20;
10806               if (PPC_HA (off) != 0)
10807                 size += 4;
10808
10809               if (PPC_HA (r2off) != 0)
10810                 size += 4;
10811             }
10812         }
10813       else if (info->emitrelocations)
10814         {
10815           stub_entry->stub_sec->reloc_count += 1;
10816           stub_entry->stub_sec->flags |= SEC_RELOC;
10817         }
10818     }
10819
10820   stub_entry->stub_sec->size += size;
10821   return TRUE;
10822 }
10823
10824 /* Set up various things so that we can make a list of input sections
10825    for each output section included in the link.  Returns -1 on error,
10826    0 when no stubs will be needed, and 1 on success.  */
10827
10828 int
10829 ppc64_elf_setup_section_lists
10830   (struct bfd_link_info *info,
10831    asection *(*add_stub_section) (const char *, asection *),
10832    void (*layout_sections_again) (void))
10833 {
10834   bfd *input_bfd;
10835   int top_id, top_index, id;
10836   asection *section;
10837   asection **input_list;
10838   bfd_size_type amt;
10839   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10840
10841   if (htab == NULL)
10842     return -1;
10843   /* Stash our params away.  */
10844   htab->add_stub_section = add_stub_section;
10845   htab->layout_sections_again = layout_sections_again;
10846
10847   /* Find the top input section id.  */
10848   for (input_bfd = info->input_bfds, top_id = 3;
10849        input_bfd != NULL;
10850        input_bfd = input_bfd->link_next)
10851     {
10852       for (section = input_bfd->sections;
10853            section != NULL;
10854            section = section->next)
10855         {
10856           if (top_id < section->id)
10857             top_id = section->id;
10858         }
10859     }
10860
10861   htab->top_id = top_id;
10862   amt = sizeof (struct map_stub) * (top_id + 1);
10863   htab->stub_group = bfd_zmalloc (amt);
10864   if (htab->stub_group == NULL)
10865     return -1;
10866
10867   /* Set toc_off for com, und, abs and ind sections.  */
10868   for (id = 0; id < 3; id++)
10869     htab->stub_group[id].toc_off = TOC_BASE_OFF;
10870
10871   /* We can't use output_bfd->section_count here to find the top output
10872      section index as some sections may have been removed, and
10873      strip_excluded_output_sections doesn't renumber the indices.  */
10874   for (section = info->output_bfd->sections, top_index = 0;
10875        section != NULL;
10876        section = section->next)
10877     {
10878       if (top_index < section->index)
10879         top_index = section->index;
10880     }
10881
10882   htab->top_index = top_index;
10883   amt = sizeof (asection *) * (top_index + 1);
10884   input_list = bfd_zmalloc (amt);
10885   htab->input_list = input_list;
10886   if (input_list == NULL)
10887     return -1;
10888
10889   return 1;
10890 }
10891
10892 /* Set up for first pass at multitoc partitioning.  */
10893
10894 void
10895 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10896 {
10897   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10898
10899   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
10900   htab->toc_bfd = NULL;
10901   htab->toc_first_sec = NULL;
10902 }
10903
10904 /* The linker repeatedly calls this function for each TOC input section
10905    and linker generated GOT section.  Group input bfds such that the toc
10906    within a group is less than 64k in size.  */
10907
10908 bfd_boolean
10909 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10910 {
10911   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10912   bfd_vma addr, off, limit;
10913
10914   if (htab == NULL)
10915     return FALSE;
10916
10917   if (!htab->second_toc_pass)
10918     {
10919       /* Keep track of the first .toc or .got section for this input bfd.  */
10920       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
10921
10922       if (new_bfd)
10923         {
10924           htab->toc_bfd = isec->owner;
10925           htab->toc_first_sec = isec;
10926         }
10927
10928       addr = isec->output_offset + isec->output_section->vma;
10929       off = addr - htab->toc_curr;
10930       limit = 0x80008000;
10931       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10932         limit = 0x10000;
10933       if (off + isec->size > limit)
10934         {
10935           addr = (htab->toc_first_sec->output_offset
10936                   + htab->toc_first_sec->output_section->vma);
10937           htab->toc_curr = addr;
10938         }
10939
10940       /* toc_curr is the base address of this toc group.  Set elf_gp
10941          for the input section to be the offset relative to the
10942          output toc base plus 0x8000.  Making the input elf_gp an
10943          offset allows us to move the toc as a whole without
10944          recalculating input elf_gp.  */
10945       off = htab->toc_curr - elf_gp (isec->output_section->owner);
10946       off += TOC_BASE_OFF;
10947
10948       /* Die if someone uses a linker script that doesn't keep input
10949          file .toc and .got together.  */
10950       if (new_bfd
10951           && elf_gp (isec->owner) != 0
10952           && elf_gp (isec->owner) != off)
10953         return FALSE;
10954
10955       elf_gp (isec->owner) = off;
10956       return TRUE;
10957     }
10958
10959   /* During the second pass toc_first_sec points to the start of
10960      a toc group, and toc_curr is used to track the old elf_gp.
10961      We use toc_bfd to ensure we only look at each bfd once.  */
10962   if (htab->toc_bfd == isec->owner)
10963     return TRUE;
10964   htab->toc_bfd = isec->owner;
10965
10966   if (htab->toc_first_sec == NULL
10967       || htab->toc_curr != elf_gp (isec->owner))
10968     {
10969       htab->toc_curr = elf_gp (isec->owner);
10970       htab->toc_first_sec = isec;
10971     }
10972   addr = (htab->toc_first_sec->output_offset
10973           + htab->toc_first_sec->output_section->vma);
10974   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10975   elf_gp (isec->owner) = off;
10976
10977   return TRUE;
10978 }
10979
10980 /* Called via elf_link_hash_traverse to merge GOT entries for global
10981    symbol H.  */
10982
10983 static bfd_boolean
10984 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10985 {
10986   if (h->root.type == bfd_link_hash_indirect)
10987     return TRUE;
10988
10989   merge_got_entries (&h->got.glist);
10990
10991   return TRUE;
10992 }
10993
10994 /* Called via elf_link_hash_traverse to allocate GOT entries for global
10995    symbol H.  */
10996
10997 static bfd_boolean
10998 reallocate_got (struct elf_link_hash_entry *h, void *inf)
10999 {
11000   struct got_entry *gent;
11001
11002   if (h->root.type == bfd_link_hash_indirect)
11003     return TRUE;
11004
11005   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11006     if (!gent->is_indirect)
11007       allocate_got (h, (struct bfd_link_info *) inf, gent);
11008   return TRUE;
11009 }
11010
11011 /* Called on the first multitoc pass after the last call to
11012    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11013    entries.  */
11014
11015 bfd_boolean
11016 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11017 {
11018   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11019   struct bfd *ibfd, *ibfd2;
11020   bfd_boolean done_something;
11021
11022   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11023
11024   if (!htab->do_multi_toc)
11025     return FALSE;
11026
11027   /* Merge global sym got entries within a toc group.  */
11028   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11029
11030   /* And tlsld_got.  */
11031   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11032     {
11033       struct got_entry *ent, *ent2;
11034
11035       if (!is_ppc64_elf (ibfd))
11036         continue;
11037
11038       ent = ppc64_tlsld_got (ibfd);
11039       if (!ent->is_indirect
11040           && ent->got.offset != (bfd_vma) -1)
11041         {
11042           for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
11043             {
11044               if (!is_ppc64_elf (ibfd2))
11045                 continue;
11046
11047               ent2 = ppc64_tlsld_got (ibfd2);
11048               if (!ent2->is_indirect
11049                   && ent2->got.offset != (bfd_vma) -1
11050                   && elf_gp (ibfd2) == elf_gp (ibfd))
11051                 {
11052                   ent2->is_indirect = TRUE;
11053                   ent2->got.ent = ent;
11054                 }
11055             }
11056         }
11057     }
11058
11059   /* Zap sizes of got sections.  */
11060   htab->reliplt->rawsize = htab->reliplt->size;
11061   htab->reliplt->size -= htab->got_reli_size;
11062   htab->got_reli_size = 0;
11063
11064   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11065     {
11066       asection *got, *relgot;
11067
11068       if (!is_ppc64_elf (ibfd))
11069         continue;
11070
11071       got = ppc64_elf_tdata (ibfd)->got;
11072       if (got != NULL)
11073         {
11074           got->rawsize = got->size;
11075           got->size = 0;
11076           relgot = ppc64_elf_tdata (ibfd)->relgot;
11077           relgot->rawsize = relgot->size;
11078           relgot->size = 0;
11079         }
11080     }
11081
11082   /* Now reallocate the got, local syms first.  We don't need to
11083      allocate section contents again since we never increase size.  */
11084   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11085     {
11086       struct got_entry **lgot_ents;
11087       struct got_entry **end_lgot_ents;
11088       struct plt_entry **local_plt;
11089       struct plt_entry **end_local_plt;
11090       unsigned char *lgot_masks;
11091       bfd_size_type locsymcount;
11092       Elf_Internal_Shdr *symtab_hdr;
11093       asection *s;
11094
11095       if (!is_ppc64_elf (ibfd))
11096         continue;
11097
11098       lgot_ents = elf_local_got_ents (ibfd);
11099       if (!lgot_ents)
11100         continue;
11101
11102       symtab_hdr = &elf_symtab_hdr (ibfd);
11103       locsymcount = symtab_hdr->sh_info;
11104       end_lgot_ents = lgot_ents + locsymcount;
11105       local_plt = (struct plt_entry **) end_lgot_ents;
11106       end_local_plt = local_plt + locsymcount;
11107       lgot_masks = (unsigned char *) end_local_plt;
11108       s = ppc64_elf_tdata (ibfd)->got;
11109       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11110         {
11111           struct got_entry *ent;
11112
11113           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11114             {
11115               unsigned int ent_size = 8;
11116               unsigned int rel_size = sizeof (Elf64_External_Rela);
11117
11118               ent->got.offset = s->size;
11119               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11120                 {
11121                   ent_size *= 2;
11122                   rel_size *= 2;
11123                 }
11124               s->size += ent_size;
11125               if ((*lgot_masks & PLT_IFUNC) != 0)
11126                 {
11127                   htab->reliplt->size += rel_size;
11128                   htab->got_reli_size += rel_size;
11129                 }
11130               else if (info->shared)
11131                 {
11132                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11133                   srel->size += rel_size;
11134                 }
11135             }
11136         }
11137     }
11138
11139   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11140
11141   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11142     {
11143       struct got_entry *ent;
11144
11145       if (!is_ppc64_elf (ibfd))
11146         continue;
11147
11148       ent = ppc64_tlsld_got (ibfd);
11149       if (!ent->is_indirect
11150           && ent->got.offset != (bfd_vma) -1)
11151         {
11152           asection *s = ppc64_elf_tdata (ibfd)->got;
11153           ent->got.offset = s->size;
11154           s->size += 16;
11155           if (info->shared)
11156             {
11157               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11158               srel->size += sizeof (Elf64_External_Rela);
11159             }
11160         }
11161     }
11162
11163   done_something = htab->reliplt->rawsize != htab->reliplt->size;
11164   if (!done_something)
11165     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11166       {
11167         asection *got;
11168
11169         if (!is_ppc64_elf (ibfd))
11170           continue;
11171
11172         got = ppc64_elf_tdata (ibfd)->got;
11173         if (got != NULL)
11174           {
11175             done_something = got->rawsize != got->size;
11176             if (done_something)
11177               break;
11178           }
11179       }
11180
11181   if (done_something)
11182     (*htab->layout_sections_again) ();
11183
11184   /* Set up for second pass over toc sections to recalculate elf_gp
11185      on input sections.  */
11186   htab->toc_bfd = NULL;
11187   htab->toc_first_sec = NULL;
11188   htab->second_toc_pass = TRUE;
11189   return done_something;
11190 }
11191
11192 /* Called after second pass of multitoc partitioning.  */
11193
11194 void
11195 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11196 {
11197   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11198
11199   /* After the second pass, toc_curr tracks the TOC offset used
11200      for code sections below in ppc64_elf_next_input_section.  */
11201   htab->toc_curr = TOC_BASE_OFF;
11202 }
11203
11204 /* No toc references were found in ISEC.  If the code in ISEC makes no
11205    calls, then there's no need to use toc adjusting stubs when branching
11206    into ISEC.  Actually, indirect calls from ISEC are OK as they will
11207    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
11208    needed, and 2 if a cyclical call-graph was found but no other reason
11209    for a stub was detected.  If called from the top level, a return of
11210    2 means the same as a return of 0.  */
11211
11212 static int
11213 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11214 {
11215   int ret;
11216
11217   /* Mark this section as checked.  */
11218   isec->call_check_done = 1;
11219
11220   /* We know none of our code bearing sections will need toc stubs.  */
11221   if ((isec->flags & SEC_LINKER_CREATED) != 0)
11222     return 0;
11223
11224   if (isec->size == 0)
11225     return 0;
11226
11227   if (isec->output_section == NULL)
11228     return 0;
11229
11230   ret = 0;
11231   if (isec->reloc_count != 0)
11232     {
11233       Elf_Internal_Rela *relstart, *rel;
11234       Elf_Internal_Sym *local_syms;
11235       struct ppc_link_hash_table *htab;
11236
11237       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11238                                             info->keep_memory);
11239       if (relstart == NULL)
11240         return -1;
11241
11242       /* Look for branches to outside of this section.  */
11243       local_syms = NULL;
11244       htab = ppc_hash_table (info);
11245       if (htab == NULL)
11246         return -1;
11247
11248       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11249         {
11250           enum elf_ppc64_reloc_type r_type;
11251           unsigned long r_symndx;
11252           struct elf_link_hash_entry *h;
11253           struct ppc_link_hash_entry *eh;
11254           Elf_Internal_Sym *sym;
11255           asection *sym_sec;
11256           struct _opd_sec_data *opd;
11257           bfd_vma sym_value;
11258           bfd_vma dest;
11259
11260           r_type = ELF64_R_TYPE (rel->r_info);
11261           if (r_type != R_PPC64_REL24
11262               && r_type != R_PPC64_REL14
11263               && r_type != R_PPC64_REL14_BRTAKEN
11264               && r_type != R_PPC64_REL14_BRNTAKEN)
11265             continue;
11266
11267           r_symndx = ELF64_R_SYM (rel->r_info);
11268           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11269                           isec->owner))
11270             {
11271               ret = -1;
11272               break;
11273             }
11274
11275           /* Calls to dynamic lib functions go through a plt call stub
11276              that uses r2.  */
11277           eh = (struct ppc_link_hash_entry *) h;
11278           if (eh != NULL
11279               && (eh->elf.plt.plist != NULL
11280                   || (eh->oh != NULL
11281                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11282             {
11283               ret = 1;
11284               break;
11285             }
11286
11287           if (sym_sec == NULL)
11288             /* Ignore other undefined symbols.  */
11289             continue;
11290
11291           /* Assume branches to other sections not included in the
11292              link need stubs too, to cover -R and absolute syms.  */
11293           if (sym_sec->output_section == NULL)
11294             {
11295               ret = 1;
11296               break;
11297             }
11298
11299           if (h == NULL)
11300             sym_value = sym->st_value;
11301           else
11302             {
11303               if (h->root.type != bfd_link_hash_defined
11304                   && h->root.type != bfd_link_hash_defweak)
11305                 abort ();
11306               sym_value = h->root.u.def.value;
11307             }
11308           sym_value += rel->r_addend;
11309
11310           /* If this branch reloc uses an opd sym, find the code section.  */
11311           opd = get_opd_info (sym_sec);
11312           if (opd != NULL)
11313             {
11314               if (h == NULL && opd->adjust != NULL)
11315                 {
11316                   long adjust;
11317
11318                   adjust = opd->adjust[sym->st_value / 8];
11319                   if (adjust == -1)
11320                     /* Assume deleted functions won't ever be called.  */
11321                     continue;
11322                   sym_value += adjust;
11323                 }
11324
11325               dest = opd_entry_value (sym_sec, sym_value,
11326                                       &sym_sec, NULL, FALSE);
11327               if (dest == (bfd_vma) -1)
11328                 continue;
11329             }
11330           else
11331             dest = (sym_value
11332                     + sym_sec->output_offset
11333                     + sym_sec->output_section->vma);
11334
11335           /* Ignore branch to self.  */
11336           if (sym_sec == isec)
11337             continue;
11338
11339           /* If the called function uses the toc, we need a stub.  */
11340           if (sym_sec->has_toc_reloc
11341               || sym_sec->makes_toc_func_call)
11342             {
11343               ret = 1;
11344               break;
11345             }
11346
11347           /* Assume any branch that needs a long branch stub might in fact
11348              need a plt_branch stub.  A plt_branch stub uses r2.  */
11349           else if (dest - (isec->output_offset
11350                            + isec->output_section->vma
11351                            + rel->r_offset) + (1 << 25)
11352                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11353                                                              ? h->other
11354                                                              : sym->st_other))
11355             {
11356               ret = 1;
11357               break;
11358             }
11359
11360           /* If calling back to a section in the process of being
11361              tested, we can't say for sure that no toc adjusting stubs
11362              are needed, so don't return zero.  */
11363           else if (sym_sec->call_check_in_progress)
11364             ret = 2;
11365
11366           /* Branches to another section that itself doesn't have any TOC
11367              references are OK.  Recursively call ourselves to check.  */
11368           else if (!sym_sec->call_check_done)
11369             {
11370               int recur;
11371
11372               /* Mark current section as indeterminate, so that other
11373                  sections that call back to current won't be marked as
11374                  known.  */
11375               isec->call_check_in_progress = 1;
11376               recur = toc_adjusting_stub_needed (info, sym_sec);
11377               isec->call_check_in_progress = 0;
11378
11379               if (recur != 0)
11380                 {
11381                   ret = recur;
11382                   if (recur != 2)
11383                     break;
11384                 }
11385             }
11386         }
11387
11388       if (local_syms != NULL
11389           && (elf_symtab_hdr (isec->owner).contents
11390               != (unsigned char *) local_syms))
11391         free (local_syms);
11392       if (elf_section_data (isec)->relocs != relstart)
11393         free (relstart);
11394     }
11395
11396   if ((ret & 1) == 0
11397       && isec->map_head.s != NULL
11398       && (strcmp (isec->output_section->name, ".init") == 0
11399           || strcmp (isec->output_section->name, ".fini") == 0))
11400     {
11401       if (isec->map_head.s->has_toc_reloc
11402           || isec->map_head.s->makes_toc_func_call)
11403         ret = 1;
11404       else if (!isec->map_head.s->call_check_done)
11405         {
11406           int recur;
11407           isec->call_check_in_progress = 1;
11408           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11409           isec->call_check_in_progress = 0;
11410           if (recur != 0)
11411             ret = recur;
11412         }
11413     }
11414
11415   if (ret == 1)
11416     isec->makes_toc_func_call = 1;
11417
11418   return ret;
11419 }
11420
11421 /* The linker repeatedly calls this function for each input section,
11422    in the order that input sections are linked into output sections.
11423    Build lists of input sections to determine groupings between which
11424    we may insert linker stubs.  */
11425
11426 bfd_boolean
11427 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11428 {
11429   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11430
11431   if (htab == NULL)
11432     return FALSE;
11433
11434   if ((isec->output_section->flags & SEC_CODE) != 0
11435       && isec->output_section->index <= htab->top_index)
11436     {
11437       asection **list = htab->input_list + isec->output_section->index;
11438       /* Steal the link_sec pointer for our list.  */
11439 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
11440       /* This happens to make the list in reverse order,
11441          which is what we want.  */
11442       PREV_SEC (isec) = *list;
11443       *list = isec;
11444     }
11445
11446   if (htab->multi_toc_needed)
11447     {
11448       /* If a code section has a function that uses the TOC then we need
11449          to use the right TOC (obviously).  Also, make sure that .opd gets
11450          the correct TOC value for R_PPC64_TOC relocs that don't have or
11451          can't find their function symbol (shouldn't ever happen now).
11452          Also specially treat .fixup for the linux kernel.  .fixup
11453          contains branches, but only back to the function that hit an
11454          exception.  */
11455       if (isec->has_toc_reloc
11456           || (isec->flags & SEC_CODE) == 0
11457           || strcmp (isec->name, ".fixup") == 0)
11458         {
11459           if (elf_gp (isec->owner) != 0)
11460             htab->toc_curr = elf_gp (isec->owner);
11461         }
11462       else
11463         {
11464           if (!isec->call_check_done
11465               && toc_adjusting_stub_needed (info, isec) < 0)
11466             return FALSE;
11467           /* If we make a local call from this section, ie. a branch
11468              without a following nop, then we have no place to put a
11469              toc restoring insn.  We must use the same toc group as
11470              the callee.
11471              Testing makes_toc_func_call actually tests for *any*
11472              calls to functions that need a good toc pointer.  A more
11473              precise test would be better, as this one will set
11474              incorrect values for pasted .init/.fini fragments.
11475              (Fixed later in check_pasted_section.)  */
11476           if (isec->makes_toc_func_call
11477               && elf_gp (isec->owner) != 0)
11478             htab->toc_curr = elf_gp (isec->owner);
11479         }
11480     }
11481
11482   /* Functions that don't use the TOC can belong in any TOC group.
11483      Use the last TOC base.  */
11484   htab->stub_group[isec->id].toc_off = htab->toc_curr;
11485   return TRUE;
11486 }
11487
11488 /* Check that all .init and .fini sections use the same toc, if they
11489    have toc relocs.  */
11490
11491 static bfd_boolean
11492 check_pasted_section (struct bfd_link_info *info, const char *name)
11493 {
11494   asection *o = bfd_get_section_by_name (info->output_bfd, name);
11495
11496   if (o != NULL)
11497     {
11498       struct ppc_link_hash_table *htab = ppc_hash_table (info);
11499       bfd_vma toc_off = 0;
11500       asection *i;
11501
11502       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11503         if (i->has_toc_reloc)
11504           {
11505             if (toc_off == 0)
11506               toc_off = htab->stub_group[i->id].toc_off;
11507             else if (toc_off != htab->stub_group[i->id].toc_off)
11508               return FALSE;
11509           }
11510
11511       if (toc_off == 0)
11512         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11513           if (i->makes_toc_func_call)
11514             {
11515               toc_off = htab->stub_group[i->id].toc_off;
11516               break;
11517             }
11518
11519       /* Make sure the whole pasted function uses the same toc offset.  */
11520       if (toc_off != 0)
11521         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11522           htab->stub_group[i->id].toc_off = toc_off;
11523     }
11524   return TRUE;
11525 }
11526
11527 bfd_boolean
11528 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11529 {
11530   return (check_pasted_section (info, ".init")
11531           & check_pasted_section (info, ".fini"));
11532 }
11533
11534 /* See whether we can group stub sections together.  Grouping stub
11535    sections may result in fewer stubs.  More importantly, we need to
11536    put all .init* and .fini* stubs at the beginning of the .init or
11537    .fini output sections respectively, because glibc splits the
11538    _init and _fini functions into multiple parts.  Putting a stub in
11539    the middle of a function is not a good idea.  */
11540
11541 static void
11542 group_sections (struct ppc_link_hash_table *htab,
11543                 bfd_size_type stub_group_size,
11544                 bfd_boolean stubs_always_before_branch)
11545 {
11546   asection **list;
11547   bfd_size_type stub14_group_size;
11548   bfd_boolean suppress_size_errors;
11549
11550   suppress_size_errors = FALSE;
11551   stub14_group_size = stub_group_size;
11552   if (stub_group_size == 1)
11553     {
11554       /* Default values.  */
11555       if (stubs_always_before_branch)
11556         {
11557           stub_group_size = 0x1e00000;
11558           stub14_group_size = 0x7800;
11559         }
11560       else
11561         {
11562           stub_group_size = 0x1c00000;
11563           stub14_group_size = 0x7000;
11564         }
11565       suppress_size_errors = TRUE;
11566     }
11567
11568   list = htab->input_list + htab->top_index;
11569   do
11570     {
11571       asection *tail = *list;
11572       while (tail != NULL)
11573         {
11574           asection *curr;
11575           asection *prev;
11576           bfd_size_type total;
11577           bfd_boolean big_sec;
11578           bfd_vma curr_toc;
11579
11580           curr = tail;
11581           total = tail->size;
11582           big_sec = total > (ppc64_elf_section_data (tail) != NULL
11583                              && ppc64_elf_section_data (tail)->has_14bit_branch
11584                              ? stub14_group_size : stub_group_size);
11585           if (big_sec && !suppress_size_errors)
11586             (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11587                                      tail->owner, tail);
11588           curr_toc = htab->stub_group[tail->id].toc_off;
11589
11590           while ((prev = PREV_SEC (curr)) != NULL
11591                  && ((total += curr->output_offset - prev->output_offset)
11592                      < (ppc64_elf_section_data (prev) != NULL
11593                         && ppc64_elf_section_data (prev)->has_14bit_branch
11594                         ? stub14_group_size : stub_group_size))
11595                  && htab->stub_group[prev->id].toc_off == curr_toc)
11596             curr = prev;
11597
11598           /* OK, the size from the start of CURR to the end is less
11599              than stub_group_size and thus can be handled by one stub
11600              section.  (or the tail section is itself larger than
11601              stub_group_size, in which case we may be toast.)  We
11602              should really be keeping track of the total size of stubs
11603              added here, as stubs contribute to the final output
11604              section size.  That's a little tricky, and this way will
11605              only break if stubs added make the total size more than
11606              2^25, ie. for the default stub_group_size, if stubs total
11607              more than 2097152 bytes, or nearly 75000 plt call stubs.  */
11608           do
11609             {
11610               prev = PREV_SEC (tail);
11611               /* Set up this stub group.  */
11612               htab->stub_group[tail->id].link_sec = curr;
11613             }
11614           while (tail != curr && (tail = prev) != NULL);
11615
11616           /* But wait, there's more!  Input sections up to stub_group_size
11617              bytes before the stub section can be handled by it too.
11618              Don't do this if we have a really large section after the
11619              stubs, as adding more stubs increases the chance that
11620              branches may not reach into the stub section.  */
11621           if (!stubs_always_before_branch && !big_sec)
11622             {
11623               total = 0;
11624               while (prev != NULL
11625                      && ((total += tail->output_offset - prev->output_offset)
11626                          < (ppc64_elf_section_data (prev) != NULL
11627                             && ppc64_elf_section_data (prev)->has_14bit_branch
11628                             ? stub14_group_size : stub_group_size))
11629                      && htab->stub_group[prev->id].toc_off == curr_toc)
11630                 {
11631                   tail = prev;
11632                   prev = PREV_SEC (tail);
11633                   htab->stub_group[tail->id].link_sec = curr;
11634                 }
11635             }
11636           tail = prev;
11637         }
11638     }
11639   while (list-- != htab->input_list);
11640   free (htab->input_list);
11641 #undef PREV_SEC
11642 }
11643
11644 static const unsigned char glink_eh_frame_cie[] =
11645 {
11646   0, 0, 0, 16,                          /* length.  */
11647   0, 0, 0, 0,                           /* id.  */
11648   1,                                    /* CIE version.  */
11649   'z', 'R', 0,                          /* Augmentation string.  */
11650   4,                                    /* Code alignment.  */
11651   0x78,                                 /* Data alignment.  */
11652   65,                                   /* RA reg.  */
11653   1,                                    /* Augmentation size.  */
11654   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
11655   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
11656 };
11657
11658 /* Stripping output sections is normally done before dynamic section
11659    symbols have been allocated.  This function is called later, and
11660    handles cases like htab->brlt which is mapped to its own output
11661    section.  */
11662
11663 static void
11664 maybe_strip_output (struct bfd_link_info *info, asection *isec)
11665 {
11666   if (isec->size == 0
11667       && isec->output_section->size == 0
11668       && !(isec->output_section->flags & SEC_KEEP)
11669       && !bfd_section_removed_from_list (info->output_bfd,
11670                                          isec->output_section)
11671       && elf_section_data (isec->output_section)->dynindx == 0)
11672     {
11673       isec->output_section->flags |= SEC_EXCLUDE;
11674       bfd_section_list_remove (info->output_bfd, isec->output_section);
11675       info->output_bfd->section_count--;
11676     }
11677 }
11678
11679 /* Determine and set the size of the stub section for a final link.
11680
11681    The basic idea here is to examine all the relocations looking for
11682    PC-relative calls to a target that is unreachable with a "bl"
11683    instruction.  */
11684
11685 bfd_boolean
11686 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
11687                       bfd_boolean plt_static_chain, int plt_thread_safe,
11688                       int plt_stub_align)
11689 {
11690   bfd_size_type stub_group_size;
11691   bfd_boolean stubs_always_before_branch;
11692   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11693
11694   if (htab == NULL)
11695     return FALSE;
11696
11697   htab->plt_static_chain = plt_static_chain;
11698   htab->plt_stub_align = plt_stub_align;
11699   if (plt_thread_safe == -1 && !info->executable)
11700     plt_thread_safe = 1;
11701   if (plt_thread_safe == -1)
11702     {
11703       static const char *const thread_starter[] =
11704         {
11705           "pthread_create",
11706           /* libstdc++ */
11707           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11708           /* librt */
11709           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11710           "mq_notify", "create_timer",
11711           /* libanl */
11712           "getaddrinfo_a",
11713           /* libgomp */
11714           "GOMP_parallel_start",
11715           "GOMP_parallel_loop_static_start",
11716           "GOMP_parallel_loop_dynamic_start",
11717           "GOMP_parallel_loop_guided_start",
11718           "GOMP_parallel_loop_runtime_start",
11719           "GOMP_parallel_sections_start",
11720         };
11721       unsigned i;
11722
11723       for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
11724         {
11725           struct elf_link_hash_entry *h;
11726           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11727                                     FALSE, FALSE, TRUE);
11728           plt_thread_safe = h != NULL && h->ref_regular;
11729           if (plt_thread_safe)
11730             break;
11731         }
11732     }
11733   htab->plt_thread_safe = plt_thread_safe;
11734   stubs_always_before_branch = group_size < 0;
11735   if (group_size < 0)
11736     stub_group_size = -group_size;
11737   else
11738     stub_group_size = group_size;
11739
11740   group_sections (htab, stub_group_size, stubs_always_before_branch);
11741
11742   while (1)
11743     {
11744       bfd *input_bfd;
11745       unsigned int bfd_indx;
11746       asection *stub_sec;
11747
11748       htab->stub_iteration += 1;
11749
11750       for (input_bfd = info->input_bfds, bfd_indx = 0;
11751            input_bfd != NULL;
11752            input_bfd = input_bfd->link_next, bfd_indx++)
11753         {
11754           Elf_Internal_Shdr *symtab_hdr;
11755           asection *section;
11756           Elf_Internal_Sym *local_syms = NULL;
11757
11758           if (!is_ppc64_elf (input_bfd))
11759             continue;
11760
11761           /* We'll need the symbol table in a second.  */
11762           symtab_hdr = &elf_symtab_hdr (input_bfd);
11763           if (symtab_hdr->sh_info == 0)
11764             continue;
11765
11766           /* Walk over each section attached to the input bfd.  */
11767           for (section = input_bfd->sections;
11768                section != NULL;
11769                section = section->next)
11770             {
11771               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
11772
11773               /* If there aren't any relocs, then there's nothing more
11774                  to do.  */
11775               if ((section->flags & SEC_RELOC) == 0
11776                   || (section->flags & SEC_ALLOC) == 0
11777                   || (section->flags & SEC_LOAD) == 0
11778                   || (section->flags & SEC_CODE) == 0
11779                   || section->reloc_count == 0)
11780                 continue;
11781
11782               /* If this section is a link-once section that will be
11783                  discarded, then don't create any stubs.  */
11784               if (section->output_section == NULL
11785                   || section->output_section->owner != info->output_bfd)
11786                 continue;
11787
11788               /* Get the relocs.  */
11789               internal_relocs
11790                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
11791                                              info->keep_memory);
11792               if (internal_relocs == NULL)
11793                 goto error_ret_free_local;
11794
11795               /* Now examine each relocation.  */
11796               irela = internal_relocs;
11797               irelaend = irela + section->reloc_count;
11798               for (; irela < irelaend; irela++)
11799                 {
11800                   enum elf_ppc64_reloc_type r_type;
11801                   unsigned int r_indx;
11802                   enum ppc_stub_type stub_type;
11803                   struct ppc_stub_hash_entry *stub_entry;
11804                   asection *sym_sec, *code_sec;
11805                   bfd_vma sym_value, code_value;
11806                   bfd_vma destination;
11807                   unsigned long local_off;
11808                   bfd_boolean ok_dest;
11809                   struct ppc_link_hash_entry *hash;
11810                   struct ppc_link_hash_entry *fdh;
11811                   struct elf_link_hash_entry *h;
11812                   Elf_Internal_Sym *sym;
11813                   char *stub_name;
11814                   const asection *id_sec;
11815                   struct _opd_sec_data *opd;
11816                   struct plt_entry *plt_ent;
11817
11818                   r_type = ELF64_R_TYPE (irela->r_info);
11819                   r_indx = ELF64_R_SYM (irela->r_info);
11820
11821                   if (r_type >= R_PPC64_max)
11822                     {
11823                       bfd_set_error (bfd_error_bad_value);
11824                       goto error_ret_free_internal;
11825                     }
11826
11827                   /* Only look for stubs on branch instructions.  */
11828                   if (r_type != R_PPC64_REL24
11829                       && r_type != R_PPC64_REL14
11830                       && r_type != R_PPC64_REL14_BRTAKEN
11831                       && r_type != R_PPC64_REL14_BRNTAKEN)
11832                     continue;
11833
11834                   /* Now determine the call target, its name, value,
11835                      section.  */
11836                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11837                                   r_indx, input_bfd))
11838                     goto error_ret_free_internal;
11839                   hash = (struct ppc_link_hash_entry *) h;
11840
11841                   ok_dest = FALSE;
11842                   fdh = NULL;
11843                   sym_value = 0;
11844                   if (hash == NULL)
11845                     {
11846                       sym_value = sym->st_value;
11847                       ok_dest = TRUE;
11848                     }
11849                   else if (hash->elf.root.type == bfd_link_hash_defined
11850                            || hash->elf.root.type == bfd_link_hash_defweak)
11851                     {
11852                       sym_value = hash->elf.root.u.def.value;
11853                       if (sym_sec->output_section != NULL)
11854                         ok_dest = TRUE;
11855                     }
11856                   else if (hash->elf.root.type == bfd_link_hash_undefweak
11857                            || hash->elf.root.type == bfd_link_hash_undefined)
11858                     {
11859                       /* Recognise an old ABI func code entry sym, and
11860                          use the func descriptor sym instead if it is
11861                          defined.  */
11862                       if (hash->elf.root.root.string[0] == '.'
11863                           && (fdh = lookup_fdh (hash, htab)) != NULL)
11864                         {
11865                           if (fdh->elf.root.type == bfd_link_hash_defined
11866                               || fdh->elf.root.type == bfd_link_hash_defweak)
11867                             {
11868                               sym_sec = fdh->elf.root.u.def.section;
11869                               sym_value = fdh->elf.root.u.def.value;
11870                               if (sym_sec->output_section != NULL)
11871                                 ok_dest = TRUE;
11872                             }
11873                           else
11874                             fdh = NULL;
11875                         }
11876                     }
11877                   else
11878                     {
11879                       bfd_set_error (bfd_error_bad_value);
11880                       goto error_ret_free_internal;
11881                     }
11882
11883                   destination = 0;
11884                   local_off = 0;
11885                   if (ok_dest)
11886                     {
11887                       sym_value += irela->r_addend;
11888                       destination = (sym_value
11889                                      + sym_sec->output_offset
11890                                      + sym_sec->output_section->vma);
11891                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
11892                                                             ? hash->elf.other
11893                                                             : sym->st_other);
11894                     }
11895
11896                   code_sec = sym_sec;
11897                   code_value = sym_value;
11898                   opd = get_opd_info (sym_sec);
11899                   if (opd != NULL)
11900                     {
11901                       bfd_vma dest;
11902
11903                       if (hash == NULL && opd->adjust != NULL)
11904                         {
11905                           long adjust = opd->adjust[sym_value / 8];
11906                           if (adjust == -1)
11907                             continue;
11908                           code_value += adjust;
11909                           sym_value += adjust;
11910                         }
11911                       dest = opd_entry_value (sym_sec, sym_value,
11912                                               &code_sec, &code_value, FALSE);
11913                       if (dest != (bfd_vma) -1)
11914                         {
11915                           destination = dest;
11916                           if (fdh != NULL)
11917                             {
11918                               /* Fixup old ABI sym to point at code
11919                                  entry.  */
11920                               hash->elf.root.type = bfd_link_hash_defweak;
11921                               hash->elf.root.u.def.section = code_sec;
11922                               hash->elf.root.u.def.value = code_value;
11923                             }
11924                         }
11925                     }
11926
11927                   /* Determine what (if any) linker stub is needed.  */
11928                   plt_ent = NULL;
11929                   stub_type = ppc_type_of_stub (section, irela, &hash,
11930                                                 &plt_ent, destination,
11931                                                 local_off);
11932
11933                   if (stub_type != ppc_stub_plt_call)
11934                     {
11935                       /* Check whether we need a TOC adjusting stub.
11936                          Since the linker pastes together pieces from
11937                          different object files when creating the
11938                          _init and _fini functions, it may be that a
11939                          call to what looks like a local sym is in
11940                          fact a call needing a TOC adjustment.  */
11941                       if (code_sec != NULL
11942                           && code_sec->output_section != NULL
11943                           && (htab->stub_group[code_sec->id].toc_off
11944                               != htab->stub_group[section->id].toc_off)
11945                           && (code_sec->has_toc_reloc
11946                               || code_sec->makes_toc_func_call))
11947                         stub_type = ppc_stub_long_branch_r2off;
11948                     }
11949
11950                   if (stub_type == ppc_stub_none)
11951                     continue;
11952
11953                   /* __tls_get_addr calls might be eliminated.  */
11954                   if (stub_type != ppc_stub_plt_call
11955                       && hash != NULL
11956                       && (hash == htab->tls_get_addr
11957                           || hash == htab->tls_get_addr_fd)
11958                       && section->has_tls_reloc
11959                       && irela != internal_relocs)
11960                     {
11961                       /* Get tls info.  */
11962                       unsigned char *tls_mask;
11963
11964                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
11965                                          irela - 1, input_bfd))
11966                         goto error_ret_free_internal;
11967                       if (*tls_mask != 0)
11968                         continue;
11969                     }
11970
11971                   if (stub_type == ppc_stub_plt_call
11972                       && irela + 1 < irelaend
11973                       && irela[1].r_offset == irela->r_offset + 4
11974                       && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
11975                     {
11976                       if (!tocsave_find (htab, INSERT,
11977                                          &local_syms, irela + 1, input_bfd))
11978                         goto error_ret_free_internal;
11979                     }
11980                   else if (stub_type == ppc_stub_plt_call)
11981                     stub_type = ppc_stub_plt_call_r2save;
11982
11983                   /* Support for grouping stub sections.  */
11984                   id_sec = htab->stub_group[section->id].link_sec;
11985
11986                   /* Get the name of this stub.  */
11987                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11988                   if (!stub_name)
11989                     goto error_ret_free_internal;
11990
11991                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
11992                                                      stub_name, FALSE, FALSE);
11993                   if (stub_entry != NULL)
11994                     {
11995                       /* The proper stub has already been created.  */
11996                       free (stub_name);
11997                       if (stub_type == ppc_stub_plt_call_r2save)
11998                         stub_entry->stub_type = stub_type;
11999                       continue;
12000                     }
12001
12002                   stub_entry = ppc_add_stub (stub_name, section, info);
12003                   if (stub_entry == NULL)
12004                     {
12005                       free (stub_name);
12006                     error_ret_free_internal:
12007                       if (elf_section_data (section)->relocs == NULL)
12008                         free (internal_relocs);
12009                     error_ret_free_local:
12010                       if (local_syms != NULL
12011                           && (symtab_hdr->contents
12012                               != (unsigned char *) local_syms))
12013                         free (local_syms);
12014                       return FALSE;
12015                     }
12016
12017                   stub_entry->stub_type = stub_type;
12018                   if (stub_type != ppc_stub_plt_call
12019                       && stub_type != ppc_stub_plt_call_r2save)
12020                     {
12021                       stub_entry->target_value = code_value;
12022                       stub_entry->target_section = code_sec;
12023                     }
12024                   else
12025                     {
12026                       stub_entry->target_value = sym_value;
12027                       stub_entry->target_section = sym_sec;
12028                     }
12029                   stub_entry->h = hash;
12030                   stub_entry->plt_ent = plt_ent;
12031                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
12032
12033                   if (stub_entry->h != NULL)
12034                     htab->stub_globals += 1;
12035                 }
12036
12037               /* We're done with the internal relocs, free them.  */
12038               if (elf_section_data (section)->relocs != internal_relocs)
12039                 free (internal_relocs);
12040             }
12041
12042           if (local_syms != NULL
12043               && symtab_hdr->contents != (unsigned char *) local_syms)
12044             {
12045               if (!info->keep_memory)
12046                 free (local_syms);
12047               else
12048                 symtab_hdr->contents = (unsigned char *) local_syms;
12049             }
12050         }
12051
12052       /* We may have added some stubs.  Find out the new size of the
12053          stub sections.  */
12054       for (stub_sec = htab->stub_bfd->sections;
12055            stub_sec != NULL;
12056            stub_sec = stub_sec->next)
12057         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12058           {
12059             stub_sec->rawsize = stub_sec->size;
12060             stub_sec->size = 0;
12061             stub_sec->reloc_count = 0;
12062             stub_sec->flags &= ~SEC_RELOC;
12063           }
12064
12065       htab->brlt->size = 0;
12066       htab->brlt->reloc_count = 0;
12067       htab->brlt->flags &= ~SEC_RELOC;
12068       if (htab->relbrlt != NULL)
12069         htab->relbrlt->size = 0;
12070
12071       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12072
12073       if (info->emitrelocations
12074           && htab->glink != NULL && htab->glink->size != 0)
12075         {
12076           htab->glink->reloc_count = 1;
12077           htab->glink->flags |= SEC_RELOC;
12078         }
12079
12080       if (htab->glink_eh_frame != NULL
12081           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12082           && htab->glink_eh_frame->output_section->size != 0)
12083         {
12084           size_t size = 0, align;
12085
12086           for (stub_sec = htab->stub_bfd->sections;
12087                stub_sec != NULL;
12088                stub_sec = stub_sec->next)
12089             if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12090               size += 20;
12091           if (htab->glink != NULL && htab->glink->size != 0)
12092             size += 24;
12093           if (size != 0)
12094             size += sizeof (glink_eh_frame_cie);
12095           align = 1;
12096           align <<= htab->glink_eh_frame->output_section->alignment_power;
12097           align -= 1;
12098           size = (size + align) & ~align;
12099           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12100           htab->glink_eh_frame->size = size;
12101         }
12102
12103       if (htab->plt_stub_align != 0)
12104         for (stub_sec = htab->stub_bfd->sections;
12105              stub_sec != NULL;
12106              stub_sec = stub_sec->next)
12107           if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12108             stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12109                               & (-1 << htab->plt_stub_align));
12110
12111       for (stub_sec = htab->stub_bfd->sections;
12112            stub_sec != NULL;
12113            stub_sec = stub_sec->next)
12114         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12115             && stub_sec->rawsize != stub_sec->size)
12116           break;
12117
12118       /* Exit from this loop when no stubs have been added, and no stubs
12119          have changed size.  */
12120       if (stub_sec == NULL
12121           && (htab->glink_eh_frame == NULL
12122               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12123         break;
12124
12125       /* Ask the linker to do its stuff.  */
12126       (*htab->layout_sections_again) ();
12127     }
12128
12129   maybe_strip_output (info, htab->brlt);
12130   if (htab->glink_eh_frame != NULL)
12131     maybe_strip_output (info, htab->glink_eh_frame);
12132
12133   return TRUE;
12134 }
12135
12136 /* Called after we have determined section placement.  If sections
12137    move, we'll be called again.  Provide a value for TOCstart.  */
12138
12139 bfd_vma
12140 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12141 {
12142   asection *s;
12143   bfd_vma TOCstart;
12144
12145   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12146      order.  The TOC starts where the first of these sections starts.  */
12147   s = bfd_get_section_by_name (obfd, ".got");
12148   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12149     s = bfd_get_section_by_name (obfd, ".toc");
12150   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12151     s = bfd_get_section_by_name (obfd, ".tocbss");
12152   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12153     s = bfd_get_section_by_name (obfd, ".plt");
12154   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12155     {
12156       /* This may happen for
12157          o  references to TOC base (SYM@toc / TOC[tc0]) without a
12158          .toc directive
12159          o  bad linker script
12160          o --gc-sections and empty TOC sections
12161
12162          FIXME: Warn user?  */
12163
12164       /* Look for a likely section.  We probably won't even be
12165          using TOCstart.  */
12166       for (s = obfd->sections; s != NULL; s = s->next)
12167         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12168                          | SEC_EXCLUDE))
12169             == (SEC_ALLOC | SEC_SMALL_DATA))
12170           break;
12171       if (s == NULL)
12172         for (s = obfd->sections; s != NULL; s = s->next)
12173           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12174               == (SEC_ALLOC | SEC_SMALL_DATA))
12175             break;
12176       if (s == NULL)
12177         for (s = obfd->sections; s != NULL; s = s->next)
12178           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12179               == SEC_ALLOC)
12180             break;
12181       if (s == NULL)
12182         for (s = obfd->sections; s != NULL; s = s->next)
12183           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12184             break;
12185     }
12186
12187   TOCstart = 0;
12188   if (s != NULL)
12189     TOCstart = s->output_section->vma + s->output_offset;
12190
12191   _bfd_set_gp_value (obfd, TOCstart);
12192
12193   if (info != NULL && s != NULL && is_ppc64_elf (obfd))
12194     {
12195       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12196
12197       if (htab != NULL
12198           && htab->elf.hgot != NULL)
12199         {
12200           htab->elf.hgot->type = STT_OBJECT;
12201           htab->elf.hgot->root.type = bfd_link_hash_defined;
12202           htab->elf.hgot->root.u.def.value = TOC_BASE_OFF;
12203           htab->elf.hgot->root.u.def.section = s;
12204         }
12205     }
12206   return TOCstart;
12207 }
12208
12209 /* Build all the stubs associated with the current output file.
12210    The stubs are kept in a hash table attached to the main linker
12211    hash table.  This function is called via gldelf64ppc_finish.  */
12212
12213 bfd_boolean
12214 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
12215                        struct bfd_link_info *info,
12216                        char **stats)
12217 {
12218   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12219   asection *stub_sec;
12220   bfd_byte *p;
12221   int stub_sec_count = 0;
12222
12223   if (htab == NULL)
12224     return FALSE;
12225
12226   htab->emit_stub_syms = emit_stub_syms;
12227
12228   /* Allocate memory to hold the linker stubs.  */
12229   for (stub_sec = htab->stub_bfd->sections;
12230        stub_sec != NULL;
12231        stub_sec = stub_sec->next)
12232     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12233         && stub_sec->size != 0)
12234       {
12235         stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
12236         if (stub_sec->contents == NULL)
12237           return FALSE;
12238         /* We want to check that built size is the same as calculated
12239            size.  rawsize is a convenient location to use.  */
12240         stub_sec->rawsize = stub_sec->size;
12241         stub_sec->size = 0;
12242       }
12243
12244   if (htab->glink != NULL && htab->glink->size != 0)
12245     {
12246       unsigned int indx;
12247       bfd_vma plt0;
12248
12249       /* Build the .glink plt call stub.  */
12250       if (htab->emit_stub_syms)
12251         {
12252           struct elf_link_hash_entry *h;
12253           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12254                                     TRUE, FALSE, FALSE);
12255           if (h == NULL)
12256             return FALSE;
12257           if (h->root.type == bfd_link_hash_new)
12258             {
12259               h->root.type = bfd_link_hash_defined;
12260               h->root.u.def.section = htab->glink;
12261               h->root.u.def.value = 8;
12262               h->ref_regular = 1;
12263               h->def_regular = 1;
12264               h->ref_regular_nonweak = 1;
12265               h->forced_local = 1;
12266               h->non_elf = 0;
12267             }
12268         }
12269       plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
12270       if (info->emitrelocations)
12271         {
12272           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12273           if (r == NULL)
12274             return FALSE;
12275           r->r_offset = (htab->glink->output_offset
12276                          + htab->glink->output_section->vma);
12277           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12278           r->r_addend = plt0;
12279         }
12280       p = htab->glink->contents;
12281       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
12282       bfd_put_64 (htab->glink->owner, plt0, p);
12283       p += 8;
12284       bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12285       p += 4;
12286       bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12287       p += 4;
12288       bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12289       p += 4;
12290       bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12291       p += 4;
12292       bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12293       p += 4;
12294       bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12295       p += 4;
12296       bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12297       p += 4;
12298       bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12299       p += 4;
12300       bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12301       p += 4;
12302       bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12303       p += 4;
12304       bfd_put_32 (htab->glink->owner, BCTR, p);
12305       p += 4;
12306       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
12307         {
12308           bfd_put_32 (htab->glink->owner, NOP, p);
12309           p += 4;
12310         }
12311
12312       /* Build the .glink lazy link call stubs.  */
12313       indx = 0;
12314       while (p < htab->glink->contents + htab->glink->size)
12315         {
12316           if (indx < 0x8000)
12317             {
12318               bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12319               p += 4;
12320             }
12321           else
12322             {
12323               bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12324               p += 4;
12325               bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
12326               p += 4;
12327             }
12328           bfd_put_32 (htab->glink->owner,
12329                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
12330           indx++;
12331           p += 4;
12332         }
12333       htab->glink->rawsize = p - htab->glink->contents;
12334     }
12335
12336   if (htab->brlt->size != 0)
12337     {
12338       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
12339                                          htab->brlt->size);
12340       if (htab->brlt->contents == NULL)
12341         return FALSE;
12342     }
12343   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
12344     {
12345       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
12346                                             htab->relbrlt->size);
12347       if (htab->relbrlt->contents == NULL)
12348         return FALSE;
12349     }
12350
12351   if (htab->glink_eh_frame != NULL
12352       && htab->glink_eh_frame->size != 0)
12353     {
12354       bfd_vma val;
12355       bfd_byte *last_fde;
12356       size_t last_fde_len, size, align, pad;
12357
12358       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12359       if (p == NULL)
12360         return FALSE;
12361       htab->glink_eh_frame->contents = p;
12362       last_fde = p;
12363
12364       htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12365
12366       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12367       /* CIE length (rewrite in case little-endian).  */
12368       last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12369       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12370       p += sizeof (glink_eh_frame_cie);
12371
12372       for (stub_sec = htab->stub_bfd->sections;
12373            stub_sec != NULL;
12374            stub_sec = stub_sec->next)
12375         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12376           {
12377             last_fde = p;
12378             last_fde_len = 16;
12379             /* FDE length.  */
12380             bfd_put_32 (htab->elf.dynobj, 16, p);
12381             p += 4;
12382             /* CIE pointer.  */
12383             val = p - htab->glink_eh_frame->contents;
12384             bfd_put_32 (htab->elf.dynobj, val, p);
12385             p += 4;
12386             /* Offset to stub section.  */
12387             val = (stub_sec->output_section->vma
12388                    + stub_sec->output_offset);
12389             val -= (htab->glink_eh_frame->output_section->vma
12390                     + htab->glink_eh_frame->output_offset);
12391             val -= p - htab->glink_eh_frame->contents;
12392             if (val + 0x80000000 > 0xffffffff)
12393               {
12394                 info->callbacks->einfo
12395                   (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
12396                    stub_sec->name);
12397                 return FALSE;
12398               }
12399             bfd_put_32 (htab->elf.dynobj, val, p);
12400             p += 4;
12401             /* stub section size.  */
12402             bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
12403             p += 4;
12404             /* Augmentation.  */
12405             p += 1;
12406             /* Pad.  */
12407             p += 3;
12408           }
12409       if (htab->glink != NULL && htab->glink->size != 0)
12410         {
12411           last_fde = p;
12412           last_fde_len = 20;
12413           /* FDE length.  */
12414           bfd_put_32 (htab->elf.dynobj, 20, p);
12415           p += 4;
12416           /* CIE pointer.  */
12417           val = p - htab->glink_eh_frame->contents;
12418           bfd_put_32 (htab->elf.dynobj, val, p);
12419           p += 4;
12420           /* Offset to .glink.  */
12421           val = (htab->glink->output_section->vma
12422                  + htab->glink->output_offset
12423                  + 8);
12424           val -= (htab->glink_eh_frame->output_section->vma
12425                   + htab->glink_eh_frame->output_offset);
12426           val -= p - htab->glink_eh_frame->contents;
12427           if (val + 0x80000000 > 0xffffffff)
12428             {
12429               info->callbacks->einfo
12430                 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
12431                  htab->glink->name);
12432               return FALSE;
12433             }
12434           bfd_put_32 (htab->elf.dynobj, val, p);
12435           p += 4;
12436           /* .glink size.  */
12437           bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
12438           p += 4;
12439           /* Augmentation.  */
12440           p += 1;
12441
12442           *p++ = DW_CFA_advance_loc + 1;
12443           *p++ = DW_CFA_register;
12444           *p++ = 65;
12445           *p++ = 12;
12446           *p++ = DW_CFA_advance_loc + 4;
12447           *p++ = DW_CFA_restore_extended;
12448           *p++ = 65;
12449         }
12450       /* Subsume any padding into the last FDE if user .eh_frame
12451          sections are aligned more than glink_eh_frame.  Otherwise any
12452          zero padding will be seen as a terminator.  */
12453       size = p - htab->glink_eh_frame->contents;
12454       align = 1;
12455       align <<= htab->glink_eh_frame->output_section->alignment_power;
12456       align -= 1;
12457       pad = ((size + align) & ~align) - size;
12458       htab->glink_eh_frame->size = size + pad;
12459       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12460     }
12461
12462   /* Build the stubs as directed by the stub hash table.  */
12463   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
12464
12465   if (htab->relbrlt != NULL)
12466     htab->relbrlt->reloc_count = 0;
12467
12468   if (htab->plt_stub_align != 0)
12469     for (stub_sec = htab->stub_bfd->sections;
12470          stub_sec != NULL;
12471          stub_sec = stub_sec->next)
12472       if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12473         stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12474                           & (-1 << htab->plt_stub_align));
12475
12476   for (stub_sec = htab->stub_bfd->sections;
12477        stub_sec != NULL;
12478        stub_sec = stub_sec->next)
12479     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12480       {
12481         stub_sec_count += 1;
12482         if (stub_sec->rawsize != stub_sec->size)
12483           break;
12484       }
12485
12486   if (stub_sec != NULL
12487       || htab->glink->rawsize != htab->glink->size
12488       || (htab->glink_eh_frame != NULL
12489           && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
12490     {
12491       htab->stub_error = TRUE;
12492       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
12493     }
12494
12495   if (htab->stub_error)
12496     return FALSE;
12497
12498   if (stats != NULL)
12499     {
12500       *stats = bfd_malloc (500);
12501       if (*stats == NULL)
12502         return FALSE;
12503
12504       sprintf (*stats, _("linker stubs in %u group%s\n"
12505                          "  branch       %lu\n"
12506                          "  toc adjust   %lu\n"
12507                          "  long branch  %lu\n"
12508                          "  long toc adj %lu\n"
12509                          "  plt call     %lu\n"
12510                          "  plt call toc %lu"),
12511                stub_sec_count,
12512                stub_sec_count == 1 ? "" : "s",
12513                htab->stub_count[ppc_stub_long_branch - 1],
12514                htab->stub_count[ppc_stub_long_branch_r2off - 1],
12515                htab->stub_count[ppc_stub_plt_branch - 1],
12516                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
12517                htab->stub_count[ppc_stub_plt_call - 1],
12518                htab->stub_count[ppc_stub_plt_call_r2save - 1]);
12519     }
12520   return TRUE;
12521 }
12522
12523 /* This function undoes the changes made by add_symbol_adjust.  */
12524
12525 static bfd_boolean
12526 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12527 {
12528   struct ppc_link_hash_entry *eh;
12529
12530   if (h->root.type == bfd_link_hash_indirect)
12531     return TRUE;
12532
12533   eh = (struct ppc_link_hash_entry *) h;
12534   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
12535     return TRUE;
12536
12537   eh->elf.root.type = bfd_link_hash_undefined;
12538   return TRUE;
12539 }
12540
12541 void
12542 ppc64_elf_restore_symbols (struct bfd_link_info *info)
12543 {
12544   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12545
12546   if (htab != NULL)
12547     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
12548 }
12549
12550 /* What to do when ld finds relocations against symbols defined in
12551    discarded sections.  */
12552
12553 static unsigned int
12554 ppc64_elf_action_discarded (asection *sec)
12555 {
12556   if (strcmp (".opd", sec->name) == 0)
12557     return 0;
12558
12559   if (strcmp (".toc", sec->name) == 0)
12560     return 0;
12561
12562   if (strcmp (".toc1", sec->name) == 0)
12563     return 0;
12564
12565   return _bfd_elf_default_action_discarded (sec);
12566 }
12567
12568 /* The RELOCATE_SECTION function is called by the ELF backend linker
12569    to handle the relocations for a section.
12570
12571    The relocs are always passed as Rela structures; if the section
12572    actually uses Rel structures, the r_addend field will always be
12573    zero.
12574
12575    This function is responsible for adjust the section contents as
12576    necessary, and (if using Rela relocs and generating a
12577    relocatable output file) adjusting the reloc addend as
12578    necessary.
12579
12580    This function does not have to worry about setting the reloc
12581    address or the reloc symbol index.
12582
12583    LOCAL_SYMS is a pointer to the swapped in local symbols.
12584
12585    LOCAL_SECTIONS is an array giving the section in the input file
12586    corresponding to the st_shndx field of each local symbol.
12587
12588    The global hash table entry for the global symbols can be found
12589    via elf_sym_hashes (input_bfd).
12590
12591    When generating relocatable output, this function must handle
12592    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
12593    going to be the section symbol corresponding to the output
12594    section, which means that the addend must be adjusted
12595    accordingly.  */
12596
12597 static bfd_boolean
12598 ppc64_elf_relocate_section (bfd *output_bfd,
12599                             struct bfd_link_info *info,
12600                             bfd *input_bfd,
12601                             asection *input_section,
12602                             bfd_byte *contents,
12603                             Elf_Internal_Rela *relocs,
12604                             Elf_Internal_Sym *local_syms,
12605                             asection **local_sections)
12606 {
12607   struct ppc_link_hash_table *htab;
12608   Elf_Internal_Shdr *symtab_hdr;
12609   struct elf_link_hash_entry **sym_hashes;
12610   Elf_Internal_Rela *rel;
12611   Elf_Internal_Rela *relend;
12612   Elf_Internal_Rela outrel;
12613   bfd_byte *loc;
12614   struct got_entry **local_got_ents;
12615   bfd_vma TOCstart;
12616   bfd_boolean ret = TRUE;
12617   bfd_boolean is_opd;
12618   /* Assume 'at' branch hints.  */
12619   bfd_boolean is_isa_v2 = TRUE;
12620   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
12621
12622   /* Initialize howto table if needed.  */
12623   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
12624     ppc_howto_init ();
12625
12626   htab = ppc_hash_table (info);
12627   if (htab == NULL)
12628     return FALSE;
12629
12630   /* Don't relocate stub sections.  */
12631   if (input_section->owner == htab->stub_bfd)
12632     return TRUE;
12633
12634   BFD_ASSERT (is_ppc64_elf (input_bfd));
12635
12636   local_got_ents = elf_local_got_ents (input_bfd);
12637   TOCstart = elf_gp (output_bfd);
12638   symtab_hdr = &elf_symtab_hdr (input_bfd);
12639   sym_hashes = elf_sym_hashes (input_bfd);
12640   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
12641
12642   rel = relocs;
12643   relend = relocs + input_section->reloc_count;
12644   for (; rel < relend; rel++)
12645     {
12646       enum elf_ppc64_reloc_type r_type;
12647       bfd_vma addend;
12648       bfd_reloc_status_type r;
12649       Elf_Internal_Sym *sym;
12650       asection *sec;
12651       struct elf_link_hash_entry *h_elf;
12652       struct ppc_link_hash_entry *h;
12653       struct ppc_link_hash_entry *fdh;
12654       const char *sym_name;
12655       unsigned long r_symndx, toc_symndx;
12656       bfd_vma toc_addend;
12657       unsigned char tls_mask, tls_gd, tls_type;
12658       unsigned char sym_type;
12659       bfd_vma relocation;
12660       bfd_boolean unresolved_reloc;
12661       bfd_boolean warned;
12662       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
12663       unsigned int insn;
12664       unsigned int mask;
12665       struct ppc_stub_hash_entry *stub_entry;
12666       bfd_vma max_br_offset;
12667       bfd_vma from;
12668       const Elf_Internal_Rela orig_rel = *rel;
12669
12670       r_type = ELF64_R_TYPE (rel->r_info);
12671       r_symndx = ELF64_R_SYM (rel->r_info);
12672
12673       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
12674          symbol of the previous ADDR64 reloc.  The symbol gives us the
12675          proper TOC base to use.  */
12676       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
12677           && rel != relocs
12678           && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
12679           && is_opd)
12680         r_symndx = ELF64_R_SYM (rel[-1].r_info);
12681
12682       sym = NULL;
12683       sec = NULL;
12684       h_elf = NULL;
12685       sym_name = NULL;
12686       unresolved_reloc = FALSE;
12687       warned = FALSE;
12688
12689       if (r_symndx < symtab_hdr->sh_info)
12690         {
12691           /* It's a local symbol.  */
12692           struct _opd_sec_data *opd;
12693
12694           sym = local_syms + r_symndx;
12695           sec = local_sections[r_symndx];
12696           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
12697           sym_type = ELF64_ST_TYPE (sym->st_info);
12698           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
12699           opd = get_opd_info (sec);
12700           if (opd != NULL && opd->adjust != NULL)
12701             {
12702               long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
12703               if (adjust == -1)
12704                 relocation = 0;
12705               else
12706                 {
12707                   /* If this is a relocation against the opd section sym
12708                      and we have edited .opd, adjust the reloc addend so
12709                      that ld -r and ld --emit-relocs output is correct.
12710                      If it is a reloc against some other .opd symbol,
12711                      then the symbol value will be adjusted later.  */
12712                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
12713                     rel->r_addend += adjust;
12714                   else
12715                     relocation += adjust;
12716                 }
12717             }
12718         }
12719       else
12720         {
12721           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
12722                                    r_symndx, symtab_hdr, sym_hashes,
12723                                    h_elf, sec, relocation,
12724                                    unresolved_reloc, warned);
12725           sym_name = h_elf->root.root.string;
12726           sym_type = h_elf->type;
12727           if (sec != NULL
12728               && sec->owner == output_bfd
12729               && strcmp (sec->name, ".opd") == 0)
12730             {
12731               /* This is a symbol defined in a linker script.  All
12732                  such are defined in output sections, even those
12733                  defined by simple assignment from a symbol defined in
12734                  an input section.  Transfer the symbol to an
12735                  appropriate input .opd section, so that a branch to
12736                  this symbol will be mapped to the location specified
12737                  by the opd entry.  */
12738               struct bfd_link_order *lo;
12739               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
12740                 if (lo->type == bfd_indirect_link_order)
12741                   {
12742                     asection *isec = lo->u.indirect.section;
12743                     if (h_elf->root.u.def.value >= isec->output_offset
12744                         && h_elf->root.u.def.value < (isec->output_offset
12745                                                       + isec->size))
12746                       {
12747                         h_elf->root.u.def.value -= isec->output_offset;
12748                         h_elf->root.u.def.section = isec;
12749                         sec = isec;
12750                         break;
12751                       }
12752                   }
12753             }
12754         }
12755       h = (struct ppc_link_hash_entry *) h_elf;
12756
12757       if (sec != NULL && discarded_section (sec))
12758         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
12759                                          rel, 1, relend,
12760                                          ppc64_elf_howto_table[r_type], 0,
12761                                          contents);
12762
12763       if (info->relocatable)
12764         continue;
12765
12766       if (h != NULL && &h->elf == htab->elf.hgot)
12767         {
12768           relocation = (TOCstart
12769                         + htab->stub_group[input_section->id].toc_off);
12770           sec = bfd_abs_section_ptr;
12771           unresolved_reloc = FALSE;
12772         }
12773
12774       /* TLS optimizations.  Replace instruction sequences and relocs
12775          based on information we collected in tls_optimize.  We edit
12776          RELOCS so that --emit-relocs will output something sensible
12777          for the final instruction stream.  */
12778       tls_mask = 0;
12779       tls_gd = 0;
12780       toc_symndx = 0;
12781       if (h != NULL)
12782         tls_mask = h->tls_mask;
12783       else if (local_got_ents != NULL)
12784         {
12785           struct plt_entry **local_plt = (struct plt_entry **)
12786             (local_got_ents + symtab_hdr->sh_info);
12787           unsigned char *lgot_masks = (unsigned char *)
12788             (local_plt + symtab_hdr->sh_info);
12789           tls_mask = lgot_masks[r_symndx];
12790         }
12791       if (tls_mask == 0
12792           && (r_type == R_PPC64_TLS
12793               || r_type == R_PPC64_TLSGD
12794               || r_type == R_PPC64_TLSLD))
12795         {
12796           /* Check for toc tls entries.  */
12797           unsigned char *toc_tls;
12798
12799           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12800                              &local_syms, rel, input_bfd))
12801             return FALSE;
12802
12803           if (toc_tls)
12804             tls_mask = *toc_tls;
12805         }
12806
12807       /* Check that tls relocs are used with tls syms, and non-tls
12808          relocs are used with non-tls syms.  */
12809       if (r_symndx != STN_UNDEF
12810           && r_type != R_PPC64_NONE
12811           && (h == NULL
12812               || h->elf.root.type == bfd_link_hash_defined
12813               || h->elf.root.type == bfd_link_hash_defweak)
12814           && (IS_PPC64_TLS_RELOC (r_type)
12815               != (sym_type == STT_TLS
12816                   || (sym_type == STT_SECTION
12817                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
12818         {
12819           if (tls_mask != 0
12820               && (r_type == R_PPC64_TLS
12821                   || r_type == R_PPC64_TLSGD
12822                   || r_type == R_PPC64_TLSLD))
12823             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
12824             ;
12825           else
12826             info->callbacks->einfo
12827               (!IS_PPC64_TLS_RELOC (r_type)
12828                ? _("%P: %H: %s used with TLS symbol `%T'\n")
12829                : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
12830                input_bfd, input_section, rel->r_offset,
12831                ppc64_elf_howto_table[r_type]->name,
12832                sym_name);
12833         }
12834
12835       /* Ensure reloc mapping code below stays sane.  */
12836       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
12837           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
12838           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
12839           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
12840           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
12841           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
12842           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
12843           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
12844           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
12845           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
12846         abort ();
12847
12848       switch (r_type)
12849         {
12850         default:
12851           break;
12852
12853         case R_PPC64_LO_DS_OPT:
12854           insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12855           if ((insn & (0x3f << 26)) != 58u << 26)
12856             abort ();
12857           insn += (14u << 26) - (58u << 26);
12858           bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12859           r_type = R_PPC64_TOC16_LO;
12860           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12861           break;
12862
12863         case R_PPC64_TOC16:
12864         case R_PPC64_TOC16_LO:
12865         case R_PPC64_TOC16_DS:
12866         case R_PPC64_TOC16_LO_DS:
12867           {
12868             /* Check for toc tls entries.  */
12869             unsigned char *toc_tls;
12870             int retval;
12871
12872             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12873                                    &local_syms, rel, input_bfd);
12874             if (retval == 0)
12875               return FALSE;
12876
12877             if (toc_tls)
12878               {
12879                 tls_mask = *toc_tls;
12880                 if (r_type == R_PPC64_TOC16_DS
12881                     || r_type == R_PPC64_TOC16_LO_DS)
12882                   {
12883                     if (tls_mask != 0
12884                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
12885                       goto toctprel;
12886                   }
12887                 else
12888                   {
12889                     /* If we found a GD reloc pair, then we might be
12890                        doing a GD->IE transition.  */
12891                     if (retval == 2)
12892                       {
12893                         tls_gd = TLS_TPRELGD;
12894                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12895                           goto tls_ldgd_opt;
12896                       }
12897                     else if (retval == 3)
12898                       {
12899                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12900                           goto tls_ldgd_opt;
12901                       }
12902                   }
12903               }
12904           }
12905           break;
12906
12907         case R_PPC64_GOT_TPREL16_HI:
12908         case R_PPC64_GOT_TPREL16_HA:
12909           if (tls_mask != 0
12910               && (tls_mask & TLS_TPREL) == 0)
12911             {
12912               rel->r_offset -= d_offset;
12913               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12914               r_type = R_PPC64_NONE;
12915               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12916             }
12917           break;
12918
12919         case R_PPC64_GOT_TPREL16_DS:
12920         case R_PPC64_GOT_TPREL16_LO_DS:
12921           if (tls_mask != 0
12922               && (tls_mask & TLS_TPREL) == 0)
12923             {
12924             toctprel:
12925               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12926               insn &= 31 << 21;
12927               insn |= 0x3c0d0000;       /* addis 0,13,0 */
12928               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12929               r_type = R_PPC64_TPREL16_HA;
12930               if (toc_symndx != 0)
12931                 {
12932                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
12933                   rel->r_addend = toc_addend;
12934                   /* We changed the symbol.  Start over in order to
12935                      get h, sym, sec etc. right.  */
12936                   rel--;
12937                   continue;
12938                 }
12939               else
12940                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12941             }
12942           break;
12943
12944         case R_PPC64_TLS:
12945           if (tls_mask != 0
12946               && (tls_mask & TLS_TPREL) == 0)
12947             {
12948               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
12949               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
12950               if (insn == 0)
12951                 abort ();
12952               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12953               /* Was PPC64_TLS which sits on insn boundary, now
12954                  PPC64_TPREL16_LO which is at low-order half-word.  */
12955               rel->r_offset += d_offset;
12956               r_type = R_PPC64_TPREL16_LO;
12957               if (toc_symndx != 0)
12958                 {
12959                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
12960                   rel->r_addend = toc_addend;
12961                   /* We changed the symbol.  Start over in order to
12962                      get h, sym, sec etc. right.  */
12963                   rel--;
12964                   continue;
12965                 }
12966               else
12967                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12968             }
12969           break;
12970
12971         case R_PPC64_GOT_TLSGD16_HI:
12972         case R_PPC64_GOT_TLSGD16_HA:
12973           tls_gd = TLS_TPRELGD;
12974           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12975             goto tls_gdld_hi;
12976           break;
12977
12978         case R_PPC64_GOT_TLSLD16_HI:
12979         case R_PPC64_GOT_TLSLD16_HA:
12980           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12981             {
12982             tls_gdld_hi:
12983               if ((tls_mask & tls_gd) != 0)
12984                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12985                           + R_PPC64_GOT_TPREL16_DS);
12986               else
12987                 {
12988                   rel->r_offset -= d_offset;
12989                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12990                   r_type = R_PPC64_NONE;
12991                 }
12992               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12993             }
12994           break;
12995
12996         case R_PPC64_GOT_TLSGD16:
12997         case R_PPC64_GOT_TLSGD16_LO:
12998           tls_gd = TLS_TPRELGD;
12999           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13000             goto tls_ldgd_opt;
13001           break;
13002
13003         case R_PPC64_GOT_TLSLD16:
13004         case R_PPC64_GOT_TLSLD16_LO:
13005           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13006             {
13007               unsigned int insn1, insn2, insn3;
13008               bfd_vma offset;
13009
13010             tls_ldgd_opt:
13011               offset = (bfd_vma) -1;
13012               /* If not using the newer R_PPC64_TLSGD/LD to mark
13013                  __tls_get_addr calls, we must trust that the call
13014                  stays with its arg setup insns, ie. that the next
13015                  reloc is the __tls_get_addr call associated with
13016                  the current reloc.  Edit both insns.  */
13017               if (input_section->has_tls_get_addr_call
13018                   && rel + 1 < relend
13019                   && branch_reloc_hash_match (input_bfd, rel + 1,
13020                                               htab->tls_get_addr,
13021                                               htab->tls_get_addr_fd))
13022                 offset = rel[1].r_offset;
13023               if ((tls_mask & tls_gd) != 0)
13024                 {
13025                   /* IE */
13026                   insn1 = bfd_get_32 (output_bfd,
13027                                       contents + rel->r_offset - d_offset);
13028                   insn1 &= (1 << 26) - (1 << 2);
13029                   insn1 |= 58 << 26;    /* ld */
13030                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13031                   if (offset != (bfd_vma) -1)
13032                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13033                   if ((tls_mask & TLS_EXPLICIT) == 0)
13034                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13035                               + R_PPC64_GOT_TPREL16_DS);
13036                   else
13037                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13038                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13039                 }
13040               else
13041                 {
13042                   /* LE */
13043                   insn1 = 0x3c6d0000;   /* addis 3,13,0 */
13044                   insn2 = 0x38630000;   /* addi 3,3,0 */
13045                   if (tls_gd == 0)
13046                     {
13047                       /* Was an LD reloc.  */
13048                       if (toc_symndx)
13049                         sec = local_sections[toc_symndx];
13050                       for (r_symndx = 0;
13051                            r_symndx < symtab_hdr->sh_info;
13052                            r_symndx++)
13053                         if (local_sections[r_symndx] == sec)
13054                           break;
13055                       if (r_symndx >= symtab_hdr->sh_info)
13056                         r_symndx = STN_UNDEF;
13057                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13058                       if (r_symndx != STN_UNDEF)
13059                         rel->r_addend -= (local_syms[r_symndx].st_value
13060                                           + sec->output_offset
13061                                           + sec->output_section->vma);
13062                     }
13063                   else if (toc_symndx != 0)
13064                     {
13065                       r_symndx = toc_symndx;
13066                       rel->r_addend = toc_addend;
13067                     }
13068                   r_type = R_PPC64_TPREL16_HA;
13069                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13070                   if (offset != (bfd_vma) -1)
13071                     {
13072                       rel[1].r_info = ELF64_R_INFO (r_symndx,
13073                                                     R_PPC64_TPREL16_LO);
13074                       rel[1].r_offset = offset + d_offset;
13075                       rel[1].r_addend = rel->r_addend;
13076                     }
13077                 }
13078               bfd_put_32 (output_bfd, insn1,
13079                           contents + rel->r_offset - d_offset);
13080               if (offset != (bfd_vma) -1)
13081                 {
13082                   insn3 = bfd_get_32 (output_bfd,
13083                                       contents + offset + 4);
13084                   if (insn3 == NOP
13085                       || insn3 == CROR_151515 || insn3 == CROR_313131)
13086                     {
13087                       rel[1].r_offset += 4;
13088                       bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13089                       insn2 = NOP;
13090                     }
13091                   bfd_put_32 (output_bfd, insn2, contents + offset);
13092                 }
13093               if ((tls_mask & tls_gd) == 0
13094                   && (tls_gd == 0 || toc_symndx != 0))
13095                 {
13096                   /* We changed the symbol.  Start over in order
13097                      to get h, sym, sec etc. right.  */
13098                   rel--;
13099                   continue;
13100                 }
13101             }
13102           break;
13103
13104         case R_PPC64_TLSGD:
13105           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13106             {
13107               unsigned int insn2, insn3;
13108               bfd_vma offset = rel->r_offset;
13109
13110               if ((tls_mask & TLS_TPRELGD) != 0)
13111                 {
13112                   /* IE */
13113                   r_type = R_PPC64_NONE;
13114                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13115                 }
13116               else
13117                 {
13118                   /* LE */
13119                   if (toc_symndx != 0)
13120                     {
13121                       r_symndx = toc_symndx;
13122                       rel->r_addend = toc_addend;
13123                     }
13124                   r_type = R_PPC64_TPREL16_LO;
13125                   rel->r_offset = offset + d_offset;
13126                   insn2 = 0x38630000;   /* addi 3,3,0 */
13127                 }
13128               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13129               /* Zap the reloc on the _tls_get_addr call too.  */
13130               BFD_ASSERT (offset == rel[1].r_offset);
13131               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13132               insn3 = bfd_get_32 (output_bfd,
13133                                   contents + offset + 4);
13134               if (insn3 == NOP
13135                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13136                 {
13137                   rel->r_offset += 4;
13138                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13139                   insn2 = NOP;
13140                 }
13141               bfd_put_32 (output_bfd, insn2, contents + offset);
13142               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13143                 {
13144                   rel--;
13145                   continue;
13146                 }
13147             }
13148           break;
13149
13150         case R_PPC64_TLSLD:
13151           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13152             {
13153               unsigned int insn2, insn3;
13154               bfd_vma offset = rel->r_offset;
13155
13156               if (toc_symndx)
13157                 sec = local_sections[toc_symndx];
13158               for (r_symndx = 0;
13159                    r_symndx < symtab_hdr->sh_info;
13160                    r_symndx++)
13161                 if (local_sections[r_symndx] == sec)
13162                   break;
13163               if (r_symndx >= symtab_hdr->sh_info)
13164                 r_symndx = STN_UNDEF;
13165               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13166               if (r_symndx != STN_UNDEF)
13167                 rel->r_addend -= (local_syms[r_symndx].st_value
13168                                   + sec->output_offset
13169                                   + sec->output_section->vma);
13170
13171               r_type = R_PPC64_TPREL16_LO;
13172               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13173               rel->r_offset = offset + d_offset;
13174               /* Zap the reloc on the _tls_get_addr call too.  */
13175               BFD_ASSERT (offset == rel[1].r_offset);
13176               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13177               insn2 = 0x38630000;       /* addi 3,3,0 */
13178               insn3 = bfd_get_32 (output_bfd,
13179                                   contents + offset + 4);
13180               if (insn3 == NOP
13181                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13182                 {
13183                   rel->r_offset += 4;
13184                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13185                   insn2 = NOP;
13186                 }
13187               bfd_put_32 (output_bfd, insn2, contents + offset);
13188               rel--;
13189               continue;
13190             }
13191           break;
13192
13193         case R_PPC64_DTPMOD64:
13194           if (rel + 1 < relend
13195               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13196               && rel[1].r_offset == rel->r_offset + 8)
13197             {
13198               if ((tls_mask & TLS_GD) == 0)
13199                 {
13200                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13201                   if ((tls_mask & TLS_TPRELGD) != 0)
13202                     r_type = R_PPC64_TPREL64;
13203                   else
13204                     {
13205                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13206                       r_type = R_PPC64_NONE;
13207                     }
13208                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13209                 }
13210             }
13211           else
13212             {
13213               if ((tls_mask & TLS_LD) == 0)
13214                 {
13215                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13216                   r_type = R_PPC64_NONE;
13217                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13218                 }
13219             }
13220           break;
13221
13222         case R_PPC64_TPREL64:
13223           if ((tls_mask & TLS_TPREL) == 0)
13224             {
13225               r_type = R_PPC64_NONE;
13226               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13227             }
13228           break;
13229         }
13230
13231       /* Handle other relocations that tweak non-addend part of insn.  */
13232       insn = 0;
13233       max_br_offset = 1 << 25;
13234       addend = rel->r_addend;
13235       reloc_dest = DEST_NORMAL;
13236       switch (r_type)
13237         {
13238         default:
13239           break;
13240
13241         case R_PPC64_TOCSAVE:
13242           if (relocation + addend == (rel->r_offset
13243                                       + input_section->output_offset
13244                                       + input_section->output_section->vma)
13245               && tocsave_find (htab, NO_INSERT,
13246                                &local_syms, rel, input_bfd))
13247             {
13248               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13249               if (insn == NOP
13250                   || insn == CROR_151515 || insn == CROR_313131)
13251                 bfd_put_32 (input_bfd, STD_R2_40R1,
13252                             contents + rel->r_offset);
13253             }
13254           break;
13255
13256           /* Branch taken prediction relocations.  */
13257         case R_PPC64_ADDR14_BRTAKEN:
13258         case R_PPC64_REL14_BRTAKEN:
13259           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
13260           /* Fall thru.  */
13261
13262           /* Branch not taken prediction relocations.  */
13263         case R_PPC64_ADDR14_BRNTAKEN:
13264         case R_PPC64_REL14_BRNTAKEN:
13265           insn |= bfd_get_32 (output_bfd,
13266                               contents + rel->r_offset) & ~(0x01 << 21);
13267           /* Fall thru.  */
13268
13269         case R_PPC64_REL14:
13270           max_br_offset = 1 << 15;
13271           /* Fall thru.  */
13272
13273         case R_PPC64_REL24:
13274           /* Calls to functions with a different TOC, such as calls to
13275              shared objects, need to alter the TOC pointer.  This is
13276              done using a linkage stub.  A REL24 branching to these
13277              linkage stubs needs to be followed by a nop, as the nop
13278              will be replaced with an instruction to restore the TOC
13279              base pointer.  */
13280           fdh = h;
13281           if (h != NULL
13282               && h->oh != NULL
13283               && h->oh->is_func_descriptor)
13284             fdh = ppc_follow_link (h->oh);
13285           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13286                                            htab);
13287           if (stub_entry != NULL
13288               && (stub_entry->stub_type == ppc_stub_plt_call
13289                   || stub_entry->stub_type == ppc_stub_plt_call_r2save
13290                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13291                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
13292             {
13293               bfd_boolean can_plt_call = FALSE;
13294
13295               /* All of these stubs will modify r2, so there must be a
13296                  branch and link followed by a nop.  The nop is
13297                  replaced by an insn to restore r2.  */
13298               if (rel->r_offset + 8 <= input_section->size)
13299                 {
13300                   unsigned long br;
13301
13302                   br = bfd_get_32 (input_bfd,
13303                                    contents + rel->r_offset);
13304                   if ((br & 1) != 0)
13305                     {
13306                       unsigned long nop;
13307
13308                       nop = bfd_get_32 (input_bfd,
13309                                         contents + rel->r_offset + 4);
13310                       if (nop == NOP
13311                           || nop == CROR_151515 || nop == CROR_313131)
13312                         {
13313                           if (h != NULL
13314                               && (h == htab->tls_get_addr_fd
13315                                   || h == htab->tls_get_addr)
13316                               && !htab->no_tls_get_addr_opt)
13317                             {
13318                               /* Special stub used, leave nop alone.  */
13319                             }
13320                           else
13321                             bfd_put_32 (input_bfd, LD_R2_40R1,
13322                                         contents + rel->r_offset + 4);
13323                           can_plt_call = TRUE;
13324                         }
13325                     }
13326                 }
13327
13328               if (!can_plt_call && h != NULL)
13329                 {
13330                   const char *name = h->elf.root.root.string;
13331
13332                   if (*name == '.')
13333                     ++name;
13334
13335                   if (strncmp (name, "__libc_start_main", 17) == 0
13336                       && (name[17] == 0 || name[17] == '@'))
13337                     {
13338                       /* Allow crt1 branch to go via a toc adjusting
13339                          stub.  Other calls that never return could do
13340                          the same, if we could detect such.  */
13341                       can_plt_call = TRUE;
13342                     }
13343                 }
13344
13345               if (!can_plt_call)
13346                 {
13347                   /* g++ as of 20130507 emits self-calls without a
13348                      following nop.  This is arguably wrong since we
13349                      have conflicting information.  On the one hand a
13350                      global symbol and on the other a local call
13351                      sequence, but don't error for this special case.
13352                      It isn't possible to cheaply verify we have
13353                      exactly such a call.  Allow all calls to the same
13354                      section.  */
13355                   asection *code_sec = sec;
13356
13357                   if (get_opd_info (sec) != NULL)
13358                     {
13359                       bfd_vma off = (relocation + addend
13360                                      - sec->output_section->vma
13361                                      - sec->output_offset);
13362
13363                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
13364                     }
13365                   if (code_sec == input_section)
13366                     can_plt_call = TRUE;
13367                 }
13368
13369               if (!can_plt_call)
13370                 {
13371                   info->callbacks->einfo
13372                     (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13373                        "recompile with -fPIC"),
13374                      input_bfd, input_section, rel->r_offset, sym_name);
13375
13376                   bfd_set_error (bfd_error_bad_value);
13377                   ret = FALSE;
13378                 }
13379
13380               if (can_plt_call
13381                   && (stub_entry->stub_type == ppc_stub_plt_call
13382                       || stub_entry->stub_type == ppc_stub_plt_call_r2save))
13383                 unresolved_reloc = FALSE;
13384             }
13385
13386           if ((stub_entry == NULL
13387                || stub_entry->stub_type == ppc_stub_long_branch
13388                || stub_entry->stub_type == ppc_stub_plt_branch)
13389               && get_opd_info (sec) != NULL)
13390             {
13391               /* The branch destination is the value of the opd entry. */
13392               bfd_vma off = (relocation + addend
13393                              - sec->output_section->vma
13394                              - sec->output_offset);
13395               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
13396               if (dest != (bfd_vma) -1)
13397                 {
13398                   relocation = dest;
13399                   addend = 0;
13400                   reloc_dest = DEST_OPD;
13401                 }
13402             }
13403
13404           /* If the branch is out of reach we ought to have a long
13405              branch stub.  */
13406           from = (rel->r_offset
13407                   + input_section->output_offset
13408                   + input_section->output_section->vma);
13409
13410           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
13411                                                   ? fdh->elf.other
13412                                                   : sym->st_other);
13413
13414           if (stub_entry != NULL
13415               && (stub_entry->stub_type == ppc_stub_long_branch
13416                   || stub_entry->stub_type == ppc_stub_plt_branch)
13417               && (r_type == R_PPC64_ADDR14_BRTAKEN
13418                   || r_type == R_PPC64_ADDR14_BRNTAKEN
13419                   || (relocation + addend - from + max_br_offset
13420                       < 2 * max_br_offset)))
13421             /* Don't use the stub if this branch is in range.  */
13422             stub_entry = NULL;
13423
13424           if (stub_entry != NULL)
13425             {
13426               /* Munge up the value and addend so that we call the stub
13427                  rather than the procedure directly.  */
13428               relocation = (stub_entry->stub_offset
13429                             + stub_entry->stub_sec->output_offset
13430                             + stub_entry->stub_sec->output_section->vma);
13431               addend = 0;
13432               reloc_dest = DEST_STUB;
13433
13434               if ((stub_entry->stub_type == ppc_stub_plt_call
13435                    || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13436                   && (ALWAYS_EMIT_R2SAVE
13437                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13438                   && rel + 1 < relend
13439                   && rel[1].r_offset == rel->r_offset + 4
13440                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
13441                 relocation += 4;
13442             }
13443
13444           if (insn != 0)
13445             {
13446               if (is_isa_v2)
13447                 {
13448                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
13449                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
13450                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
13451                   if ((insn & (0x14 << 21)) == (0x04 << 21))
13452                     insn |= 0x02 << 21;
13453                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
13454                     insn |= 0x08 << 21;
13455                   else
13456                     break;
13457                 }
13458               else
13459                 {
13460                   /* Invert 'y' bit if not the default.  */
13461                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
13462                     insn ^= 0x01 << 21;
13463                 }
13464
13465               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
13466             }
13467
13468           /* NOP out calls to undefined weak functions.
13469              We can thus call a weak function without first
13470              checking whether the function is defined.  */
13471           else if (h != NULL
13472                    && h->elf.root.type == bfd_link_hash_undefweak
13473                    && h->elf.dynindx == -1
13474                    && r_type == R_PPC64_REL24
13475                    && relocation == 0
13476                    && addend == 0)
13477             {
13478               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13479               continue;
13480             }
13481           break;
13482         }
13483
13484       /* Set `addend'.  */
13485       tls_type = 0;
13486       switch (r_type)
13487         {
13488         default:
13489           info->callbacks->einfo
13490             (_("%P: %B: unknown relocation type %d for `%T'\n"),
13491              input_bfd, (int) r_type, sym_name);
13492
13493           bfd_set_error (bfd_error_bad_value);
13494           ret = FALSE;
13495           continue;
13496
13497         case R_PPC64_NONE:
13498         case R_PPC64_TLS:
13499         case R_PPC64_TLSGD:
13500         case R_PPC64_TLSLD:
13501         case R_PPC64_TOCSAVE:
13502         case R_PPC64_GNU_VTINHERIT:
13503         case R_PPC64_GNU_VTENTRY:
13504           continue;
13505
13506           /* GOT16 relocations.  Like an ADDR16 using the symbol's
13507              address in the GOT as relocation value instead of the
13508              symbol's value itself.  Also, create a GOT entry for the
13509              symbol and put the symbol value there.  */
13510         case R_PPC64_GOT_TLSGD16:
13511         case R_PPC64_GOT_TLSGD16_LO:
13512         case R_PPC64_GOT_TLSGD16_HI:
13513         case R_PPC64_GOT_TLSGD16_HA:
13514           tls_type = TLS_TLS | TLS_GD;
13515           goto dogot;
13516
13517         case R_PPC64_GOT_TLSLD16:
13518         case R_PPC64_GOT_TLSLD16_LO:
13519         case R_PPC64_GOT_TLSLD16_HI:
13520         case R_PPC64_GOT_TLSLD16_HA:
13521           tls_type = TLS_TLS | TLS_LD;
13522           goto dogot;
13523
13524         case R_PPC64_GOT_TPREL16_DS:
13525         case R_PPC64_GOT_TPREL16_LO_DS:
13526         case R_PPC64_GOT_TPREL16_HI:
13527         case R_PPC64_GOT_TPREL16_HA:
13528           tls_type = TLS_TLS | TLS_TPREL;
13529           goto dogot;
13530
13531         case R_PPC64_GOT_DTPREL16_DS:
13532         case R_PPC64_GOT_DTPREL16_LO_DS:
13533         case R_PPC64_GOT_DTPREL16_HI:
13534         case R_PPC64_GOT_DTPREL16_HA:
13535           tls_type = TLS_TLS | TLS_DTPREL;
13536           goto dogot;
13537
13538         case R_PPC64_GOT16:
13539         case R_PPC64_GOT16_LO:
13540         case R_PPC64_GOT16_HI:
13541         case R_PPC64_GOT16_HA:
13542         case R_PPC64_GOT16_DS:
13543         case R_PPC64_GOT16_LO_DS:
13544         dogot:
13545           {
13546             /* Relocation is to the entry for this symbol in the global
13547                offset table.  */
13548             asection *got;
13549             bfd_vma *offp;
13550             bfd_vma off;
13551             unsigned long indx = 0;
13552             struct got_entry *ent;
13553
13554             if (tls_type == (TLS_TLS | TLS_LD)
13555                 && (h == NULL
13556                     || !h->elf.def_dynamic))
13557               ent = ppc64_tlsld_got (input_bfd);
13558             else
13559               {
13560
13561                 if (h != NULL)
13562                   {
13563                     bfd_boolean dyn = htab->elf.dynamic_sections_created;
13564                     if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
13565                                                           &h->elf)
13566                         || (info->shared
13567                             && SYMBOL_CALLS_LOCAL (info, &h->elf)))
13568                       /* This is actually a static link, or it is a
13569                          -Bsymbolic link and the symbol is defined
13570                          locally, or the symbol was forced to be local
13571                          because of a version file.  */
13572                       ;
13573                     else
13574                       {
13575                         BFD_ASSERT (h->elf.dynindx != -1);
13576                         indx = h->elf.dynindx;
13577                         unresolved_reloc = FALSE;
13578                       }
13579                     ent = h->elf.got.glist;
13580                   }
13581                 else
13582                   {
13583                     if (local_got_ents == NULL)
13584                       abort ();
13585                     ent = local_got_ents[r_symndx];
13586                   }
13587
13588                 for (; ent != NULL; ent = ent->next)
13589                   if (ent->addend == orig_rel.r_addend
13590                       && ent->owner == input_bfd
13591                       && ent->tls_type == tls_type)
13592                     break;
13593               }
13594
13595             if (ent == NULL)
13596               abort ();
13597             if (ent->is_indirect)
13598               ent = ent->got.ent;
13599             offp = &ent->got.offset;
13600             got = ppc64_elf_tdata (ent->owner)->got;
13601             if (got == NULL)
13602               abort ();
13603
13604             /* The offset must always be a multiple of 8.  We use the
13605                least significant bit to record whether we have already
13606                processed this entry.  */
13607             off = *offp;
13608             if ((off & 1) != 0)
13609               off &= ~1;
13610             else
13611               {
13612                 /* Generate relocs for the dynamic linker, except in
13613                    the case of TLSLD where we'll use one entry per
13614                    module.  */
13615                 asection *relgot;
13616                 bfd_boolean ifunc;
13617
13618                 *offp = off | 1;
13619                 relgot = NULL;
13620                 ifunc = (h != NULL
13621                          ? h->elf.type == STT_GNU_IFUNC
13622                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
13623                 if (ifunc)
13624                   relgot = htab->reliplt;
13625                 else if ((info->shared || indx != 0)
13626                          && (h == NULL
13627                              || (tls_type == (TLS_TLS | TLS_LD)
13628                                  && !h->elf.def_dynamic)
13629                              || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13630                              || h->elf.root.type != bfd_link_hash_undefweak))
13631                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
13632                 if (relgot != NULL)
13633                   {
13634                     outrel.r_offset = (got->output_section->vma
13635                                        + got->output_offset
13636                                        + off);
13637                     outrel.r_addend = addend;
13638                     if (tls_type & (TLS_LD | TLS_GD))
13639                       {
13640                         outrel.r_addend = 0;
13641                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
13642                         if (tls_type == (TLS_TLS | TLS_GD))
13643                           {
13644                             loc = relgot->contents;
13645                             loc += (relgot->reloc_count++
13646                                     * sizeof (Elf64_External_Rela));
13647                             bfd_elf64_swap_reloca_out (output_bfd,
13648                                                        &outrel, loc);
13649                             outrel.r_offset += 8;
13650                             outrel.r_addend = addend;
13651                             outrel.r_info
13652                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13653                           }
13654                       }
13655                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
13656                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13657                     else if (tls_type == (TLS_TLS | TLS_TPREL))
13658                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
13659                     else if (indx != 0)
13660                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
13661                     else
13662                       {
13663                         if (ifunc)
13664                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13665                         else
13666                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13667
13668                         /* Write the .got section contents for the sake
13669                            of prelink.  */
13670                         loc = got->contents + off;
13671                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
13672                                     loc);
13673                       }
13674
13675                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
13676                       {
13677                         outrel.r_addend += relocation;
13678                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
13679                           outrel.r_addend -= htab->elf.tls_sec->vma;
13680                       }
13681                     loc = relgot->contents;
13682                     loc += (relgot->reloc_count++
13683                             * sizeof (Elf64_External_Rela));
13684                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13685                   }
13686
13687                 /* Init the .got section contents here if we're not
13688                    emitting a reloc.  */
13689                 else
13690                   {
13691                     relocation += addend;
13692                     if (tls_type == (TLS_TLS | TLS_LD))
13693                       relocation = 1;
13694                     else if (tls_type != 0)
13695                       {
13696                         relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
13697                         if (tls_type == (TLS_TLS | TLS_TPREL))
13698                           relocation += DTP_OFFSET - TP_OFFSET;
13699
13700                         if (tls_type == (TLS_TLS | TLS_GD))
13701                           {
13702                             bfd_put_64 (output_bfd, relocation,
13703                                         got->contents + off + 8);
13704                             relocation = 1;
13705                           }
13706                       }
13707
13708                     bfd_put_64 (output_bfd, relocation,
13709                                 got->contents + off);
13710                   }
13711               }
13712
13713             if (off >= (bfd_vma) -2)
13714               abort ();
13715
13716             relocation = got->output_section->vma + got->output_offset + off;
13717             addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
13718           }
13719           break;
13720
13721         case R_PPC64_PLT16_HA:
13722         case R_PPC64_PLT16_HI:
13723         case R_PPC64_PLT16_LO:
13724         case R_PPC64_PLT32:
13725         case R_PPC64_PLT64:
13726           /* Relocation is to the entry for this symbol in the
13727              procedure linkage table.  */
13728
13729           /* Resolve a PLT reloc against a local symbol directly,
13730              without using the procedure linkage table.  */
13731           if (h == NULL)
13732             break;
13733
13734           /* It's possible that we didn't make a PLT entry for this
13735              symbol.  This happens when statically linking PIC code,
13736              or when using -Bsymbolic.  Go find a match if there is a
13737              PLT entry.  */
13738           if (htab->plt != NULL)
13739             {
13740               struct plt_entry *ent;
13741               for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
13742                 if (ent->addend == orig_rel.r_addend
13743                     && ent->plt.offset != (bfd_vma) -1)
13744                   {
13745                     relocation = (htab->plt->output_section->vma
13746                                   + htab->plt->output_offset
13747                                   + ent->plt.offset);
13748                     unresolved_reloc = FALSE;
13749                   }
13750             }
13751           break;
13752
13753         case R_PPC64_TOC:
13754           /* Relocation value is TOC base.  */
13755           relocation = TOCstart;
13756           if (r_symndx == STN_UNDEF)
13757             relocation += htab->stub_group[input_section->id].toc_off;
13758           else if (unresolved_reloc)
13759             ;
13760           else if (sec != NULL && sec->id <= htab->top_id)
13761             relocation += htab->stub_group[sec->id].toc_off;
13762           else
13763             unresolved_reloc = TRUE;
13764           goto dodyn;
13765
13766           /* TOC16 relocs.  We want the offset relative to the TOC base,
13767              which is the address of the start of the TOC plus 0x8000.
13768              The TOC consists of sections .got, .toc, .tocbss, and .plt,
13769              in this order.  */
13770         case R_PPC64_TOC16:
13771         case R_PPC64_TOC16_LO:
13772         case R_PPC64_TOC16_HI:
13773         case R_PPC64_TOC16_DS:
13774         case R_PPC64_TOC16_LO_DS:
13775         case R_PPC64_TOC16_HA:
13776           addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
13777           break;
13778
13779           /* Relocate against the beginning of the section.  */
13780         case R_PPC64_SECTOFF:
13781         case R_PPC64_SECTOFF_LO:
13782         case R_PPC64_SECTOFF_HI:
13783         case R_PPC64_SECTOFF_DS:
13784         case R_PPC64_SECTOFF_LO_DS:
13785         case R_PPC64_SECTOFF_HA:
13786           if (sec != NULL)
13787             addend -= sec->output_section->vma;
13788           break;
13789
13790         case R_PPC64_REL16:
13791         case R_PPC64_REL16_LO:
13792         case R_PPC64_REL16_HI:
13793         case R_PPC64_REL16_HA:
13794           break;
13795
13796         case R_PPC64_REL14:
13797         case R_PPC64_REL14_BRNTAKEN:
13798         case R_PPC64_REL14_BRTAKEN:
13799         case R_PPC64_REL24:
13800           break;
13801
13802         case R_PPC64_TPREL16:
13803         case R_PPC64_TPREL16_LO:
13804         case R_PPC64_TPREL16_HI:
13805         case R_PPC64_TPREL16_HA:
13806         case R_PPC64_TPREL16_DS:
13807         case R_PPC64_TPREL16_LO_DS:
13808         case R_PPC64_TPREL16_HIGH:
13809         case R_PPC64_TPREL16_HIGHA:
13810         case R_PPC64_TPREL16_HIGHER:
13811         case R_PPC64_TPREL16_HIGHERA:
13812         case R_PPC64_TPREL16_HIGHEST:
13813         case R_PPC64_TPREL16_HIGHESTA:
13814           if (h != NULL
13815               && h->elf.root.type == bfd_link_hash_undefweak
13816               && h->elf.dynindx == -1)
13817             {
13818               /* Make this relocation against an undefined weak symbol
13819                  resolve to zero.  This is really just a tweak, since
13820                  code using weak externs ought to check that they are
13821                  defined before using them.  */
13822               bfd_byte *p = contents + rel->r_offset - d_offset;
13823
13824               insn = bfd_get_32 (output_bfd, p);
13825               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
13826               if (insn != 0)
13827                 bfd_put_32 (output_bfd, insn, p);
13828               break;
13829             }
13830           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13831           if (info->shared)
13832             /* The TPREL16 relocs shouldn't really be used in shared
13833                libs as they will result in DT_TEXTREL being set, but
13834                support them anyway.  */
13835             goto dodyn;
13836           break;
13837
13838         case R_PPC64_DTPREL16:
13839         case R_PPC64_DTPREL16_LO:
13840         case R_PPC64_DTPREL16_HI:
13841         case R_PPC64_DTPREL16_HA:
13842         case R_PPC64_DTPREL16_DS:
13843         case R_PPC64_DTPREL16_LO_DS:
13844         case R_PPC64_DTPREL16_HIGH:
13845         case R_PPC64_DTPREL16_HIGHA:
13846         case R_PPC64_DTPREL16_HIGHER:
13847         case R_PPC64_DTPREL16_HIGHERA:
13848         case R_PPC64_DTPREL16_HIGHEST:
13849         case R_PPC64_DTPREL16_HIGHESTA:
13850           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13851           break;
13852
13853         case R_PPC64_DTPMOD64:
13854           relocation = 1;
13855           addend = 0;
13856           goto dodyn;
13857
13858         case R_PPC64_TPREL64:
13859           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13860           goto dodyn;
13861
13862         case R_PPC64_DTPREL64:
13863           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13864           /* Fall thru */
13865
13866           /* Relocations that may need to be propagated if this is a
13867              dynamic object.  */
13868         case R_PPC64_REL30:
13869         case R_PPC64_REL32:
13870         case R_PPC64_REL64:
13871         case R_PPC64_ADDR14:
13872         case R_PPC64_ADDR14_BRNTAKEN:
13873         case R_PPC64_ADDR14_BRTAKEN:
13874         case R_PPC64_ADDR16:
13875         case R_PPC64_ADDR16_DS:
13876         case R_PPC64_ADDR16_HA:
13877         case R_PPC64_ADDR16_HI:
13878         case R_PPC64_ADDR16_HIGH:
13879         case R_PPC64_ADDR16_HIGHA:
13880         case R_PPC64_ADDR16_HIGHER:
13881         case R_PPC64_ADDR16_HIGHERA:
13882         case R_PPC64_ADDR16_HIGHEST:
13883         case R_PPC64_ADDR16_HIGHESTA:
13884         case R_PPC64_ADDR16_LO:
13885         case R_PPC64_ADDR16_LO_DS:
13886         case R_PPC64_ADDR24:
13887         case R_PPC64_ADDR32:
13888         case R_PPC64_ADDR64:
13889         case R_PPC64_UADDR16:
13890         case R_PPC64_UADDR32:
13891         case R_PPC64_UADDR64:
13892         dodyn:
13893           if ((input_section->flags & SEC_ALLOC) == 0)
13894             break;
13895
13896           if (NO_OPD_RELOCS && is_opd)
13897             break;
13898
13899           if ((info->shared
13900                && (h == NULL
13901                    || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13902                    || h->elf.root.type != bfd_link_hash_undefweak)
13903                && (must_be_dyn_reloc (info, r_type)
13904                    || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
13905               || (ELIMINATE_COPY_RELOCS
13906                   && !info->shared
13907                   && h != NULL
13908                   && h->elf.dynindx != -1
13909                   && !h->elf.non_got_ref
13910                   && !h->elf.def_regular)
13911               || (!info->shared
13912                   && (h != NULL
13913                       ? h->elf.type == STT_GNU_IFUNC
13914                       : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
13915             {
13916               bfd_boolean skip, relocate;
13917               asection *sreloc;
13918               bfd_vma out_off;
13919
13920               /* When generating a dynamic object, these relocations
13921                  are copied into the output file to be resolved at run
13922                  time.  */
13923
13924               skip = FALSE;
13925               relocate = FALSE;
13926
13927               out_off = _bfd_elf_section_offset (output_bfd, info,
13928                                                  input_section, rel->r_offset);
13929               if (out_off == (bfd_vma) -1)
13930                 skip = TRUE;
13931               else if (out_off == (bfd_vma) -2)
13932                 skip = TRUE, relocate = TRUE;
13933               out_off += (input_section->output_section->vma
13934                           + input_section->output_offset);
13935               outrel.r_offset = out_off;
13936               outrel.r_addend = rel->r_addend;
13937
13938               /* Optimize unaligned reloc use.  */
13939               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
13940                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
13941                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
13942               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
13943                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
13944                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
13945               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
13946                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
13947                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
13948
13949               if (skip)
13950                 memset (&outrel, 0, sizeof outrel);
13951               else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
13952                        && !is_opd
13953                        && r_type != R_PPC64_TOC)
13954                 {
13955                   BFD_ASSERT (h->elf.dynindx != -1);
13956                   outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
13957                 }
13958               else
13959                 {
13960                   /* This symbol is local, or marked to become local,
13961                      or this is an opd section reloc which must point
13962                      at a local function.  */
13963                   outrel.r_addend += relocation;
13964                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13965                     {
13966                       if (is_opd && h != NULL)
13967                         {
13968                           /* Lie about opd entries.  This case occurs
13969                              when building shared libraries and we
13970                              reference a function in another shared
13971                              lib.  The same thing happens for a weak
13972                              definition in an application that's
13973                              overridden by a strong definition in a
13974                              shared lib.  (I believe this is a generic
13975                              bug in binutils handling of weak syms.)
13976                              In these cases we won't use the opd
13977                              entry in this lib.  */
13978                           unresolved_reloc = FALSE;
13979                         }
13980                       if (!is_opd
13981                           && r_type == R_PPC64_ADDR64
13982                           && (h != NULL
13983                               ? h->elf.type == STT_GNU_IFUNC
13984                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
13985                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13986                       else
13987                         {
13988                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13989
13990                           /* We need to relocate .opd contents for ld.so.
13991                              Prelink also wants simple and consistent rules
13992                              for relocs.  This make all RELATIVE relocs have
13993                              *r_offset equal to r_addend.  */
13994                           relocate = TRUE;
13995                         }
13996                     }
13997                   else
13998                     {
13999                       long indx = 0;
14000
14001                       if (h != NULL
14002                           ? h->elf.type == STT_GNU_IFUNC
14003                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14004                         {
14005                           info->callbacks->einfo
14006                             (_("%P: %H: %s for indirect "
14007                                "function `%T' unsupported\n"),
14008                              input_bfd, input_section, rel->r_offset,
14009                              ppc64_elf_howto_table[r_type]->name,
14010                              sym_name);
14011                           ret = FALSE;
14012                         }
14013                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14014                         ;
14015                       else if (sec == NULL || sec->owner == NULL)
14016                         {
14017                           bfd_set_error (bfd_error_bad_value);
14018                           return FALSE;
14019                         }
14020                       else
14021                         {
14022                           asection *osec;
14023
14024                           osec = sec->output_section;
14025                           indx = elf_section_data (osec)->dynindx;
14026
14027                           if (indx == 0)
14028                             {
14029                               if ((osec->flags & SEC_READONLY) == 0
14030                                   && htab->elf.data_index_section != NULL)
14031                                 osec = htab->elf.data_index_section;
14032                               else
14033                                 osec = htab->elf.text_index_section;
14034                               indx = elf_section_data (osec)->dynindx;
14035                             }
14036                           BFD_ASSERT (indx != 0);
14037
14038                           /* We are turning this relocation into one
14039                              against a section symbol, so subtract out
14040                              the output section's address but not the
14041                              offset of the input section in the output
14042                              section.  */
14043                           outrel.r_addend -= osec->vma;
14044                         }
14045
14046                       outrel.r_info = ELF64_R_INFO (indx, r_type);
14047                     }
14048                 }
14049
14050               sreloc = elf_section_data (input_section)->sreloc;
14051               if (h != NULL
14052                   ? h->elf.type == STT_GNU_IFUNC
14053                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14054                 sreloc = htab->reliplt;
14055               if (sreloc == NULL)
14056                 abort ();
14057
14058               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14059                   >= sreloc->size)
14060                 abort ();
14061               loc = sreloc->contents;
14062               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14063               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14064
14065               /* If this reloc is against an external symbol, it will
14066                  be computed at runtime, so there's no need to do
14067                  anything now.  However, for the sake of prelink ensure
14068                  that the section contents are a known value.  */
14069               if (! relocate)
14070                 {
14071                   unresolved_reloc = FALSE;
14072                   /* The value chosen here is quite arbitrary as ld.so
14073                      ignores section contents except for the special
14074                      case of .opd where the contents might be accessed
14075                      before relocation.  Choose zero, as that won't
14076                      cause reloc overflow.  */
14077                   relocation = 0;
14078                   addend = 0;
14079                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14080                      to improve backward compatibility with older
14081                      versions of ld.  */
14082                   if (r_type == R_PPC64_ADDR64)
14083                     addend = outrel.r_addend;
14084                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
14085                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
14086                     addend = (input_section->output_section->vma
14087                               + input_section->output_offset
14088                               + rel->r_offset);
14089                 }
14090             }
14091           break;
14092
14093         case R_PPC64_COPY:
14094         case R_PPC64_GLOB_DAT:
14095         case R_PPC64_JMP_SLOT:
14096         case R_PPC64_JMP_IREL:
14097         case R_PPC64_RELATIVE:
14098           /* We shouldn't ever see these dynamic relocs in relocatable
14099              files.  */
14100           /* Fall through.  */
14101
14102         case R_PPC64_PLTGOT16:
14103         case R_PPC64_PLTGOT16_DS:
14104         case R_PPC64_PLTGOT16_HA:
14105         case R_PPC64_PLTGOT16_HI:
14106         case R_PPC64_PLTGOT16_LO:
14107         case R_PPC64_PLTGOT16_LO_DS:
14108         case R_PPC64_PLTREL32:
14109         case R_PPC64_PLTREL64:
14110           /* These ones haven't been implemented yet.  */
14111
14112           info->callbacks->einfo
14113             (_("%P: %B: %s is not supported for `%T'\n"),
14114              input_bfd,
14115              ppc64_elf_howto_table[r_type]->name, sym_name);
14116
14117           bfd_set_error (bfd_error_invalid_operation);
14118           ret = FALSE;
14119           continue;
14120         }
14121
14122       /* Multi-instruction sequences that access the TOC can be
14123          optimized, eg. addis ra,r2,0; addi rb,ra,x;
14124          to             nop;           addi rb,r2,x;  */
14125       switch (r_type)
14126         {
14127         default:
14128           break;
14129
14130         case R_PPC64_GOT_TLSLD16_HI:
14131         case R_PPC64_GOT_TLSGD16_HI:
14132         case R_PPC64_GOT_TPREL16_HI:
14133         case R_PPC64_GOT_DTPREL16_HI:
14134         case R_PPC64_GOT16_HI:
14135         case R_PPC64_TOC16_HI:
14136           /* These relocs would only be useful if building up an
14137              offset to later add to r2, perhaps in an indexed
14138              addressing mode instruction.  Don't try to optimize.
14139              Unfortunately, the possibility of someone building up an
14140              offset like this or even with the HA relocs, means that
14141              we need to check the high insn when optimizing the low
14142              insn.  */
14143           break;
14144
14145         case R_PPC64_GOT_TLSLD16_HA:
14146         case R_PPC64_GOT_TLSGD16_HA:
14147         case R_PPC64_GOT_TPREL16_HA:
14148         case R_PPC64_GOT_DTPREL16_HA:
14149         case R_PPC64_GOT16_HA:
14150         case R_PPC64_TOC16_HA:
14151           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14152               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14153             {
14154               bfd_byte *p = contents + (rel->r_offset & ~3);
14155               bfd_put_32 (input_bfd, NOP, p);
14156             }
14157           break;
14158
14159         case R_PPC64_GOT_TLSLD16_LO:
14160         case R_PPC64_GOT_TLSGD16_LO:
14161         case R_PPC64_GOT_TPREL16_LO_DS:
14162         case R_PPC64_GOT_DTPREL16_LO_DS:
14163         case R_PPC64_GOT16_LO:
14164         case R_PPC64_GOT16_LO_DS:
14165         case R_PPC64_TOC16_LO:
14166         case R_PPC64_TOC16_LO_DS:
14167           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14168               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14169             {
14170               bfd_byte *p = contents + (rel->r_offset & ~3);
14171               insn = bfd_get_32 (input_bfd, p);
14172               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14173                 {
14174                   /* Transform addic to addi when we change reg.  */
14175                   insn &= ~((0x3f << 26) | (0x1f << 16));
14176                   insn |= (14u << 26) | (2 << 16);
14177                 }
14178               else
14179                 {
14180                   insn &= ~(0x1f << 16);
14181                   insn |= 2 << 16;
14182                 }
14183               bfd_put_32 (input_bfd, insn, p);
14184             }
14185           break;
14186         }
14187
14188       /* Do any further special processing.  */
14189       switch (r_type)
14190         {
14191         default:
14192           break;
14193
14194         case R_PPC64_REL16_HA:
14195         case R_PPC64_ADDR16_HA:
14196         case R_PPC64_ADDR16_HIGHA:
14197         case R_PPC64_ADDR16_HIGHERA:
14198         case R_PPC64_ADDR16_HIGHESTA:
14199         case R_PPC64_TOC16_HA:
14200         case R_PPC64_SECTOFF_HA:
14201         case R_PPC64_TPREL16_HA:
14202         case R_PPC64_TPREL16_HIGHA:
14203         case R_PPC64_TPREL16_HIGHERA:
14204         case R_PPC64_TPREL16_HIGHESTA:
14205         case R_PPC64_DTPREL16_HA:
14206         case R_PPC64_DTPREL16_HIGHA:
14207         case R_PPC64_DTPREL16_HIGHERA:
14208         case R_PPC64_DTPREL16_HIGHESTA:
14209           /* It's just possible that this symbol is a weak symbol
14210              that's not actually defined anywhere. In that case,
14211              'sec' would be NULL, and we should leave the symbol
14212              alone (it will be set to zero elsewhere in the link).  */
14213           if (sec == NULL)
14214             break;
14215           /* Fall thru */
14216
14217         case R_PPC64_GOT16_HA:
14218         case R_PPC64_PLTGOT16_HA:
14219         case R_PPC64_PLT16_HA:
14220         case R_PPC64_GOT_TLSGD16_HA:
14221         case R_PPC64_GOT_TLSLD16_HA:
14222         case R_PPC64_GOT_TPREL16_HA:
14223         case R_PPC64_GOT_DTPREL16_HA:
14224           /* Add 0x10000 if sign bit in 0:15 is set.
14225              Bits 0:15 are not used.  */
14226           addend += 0x8000;
14227           break;
14228
14229         case R_PPC64_ADDR16_DS:
14230         case R_PPC64_ADDR16_LO_DS:
14231         case R_PPC64_GOT16_DS:
14232         case R_PPC64_GOT16_LO_DS:
14233         case R_PPC64_PLT16_LO_DS:
14234         case R_PPC64_SECTOFF_DS:
14235         case R_PPC64_SECTOFF_LO_DS:
14236         case R_PPC64_TOC16_DS:
14237         case R_PPC64_TOC16_LO_DS:
14238         case R_PPC64_PLTGOT16_DS:
14239         case R_PPC64_PLTGOT16_LO_DS:
14240         case R_PPC64_GOT_TPREL16_DS:
14241         case R_PPC64_GOT_TPREL16_LO_DS:
14242         case R_PPC64_GOT_DTPREL16_DS:
14243         case R_PPC64_GOT_DTPREL16_LO_DS:
14244         case R_PPC64_TPREL16_DS:
14245         case R_PPC64_TPREL16_LO_DS:
14246         case R_PPC64_DTPREL16_DS:
14247         case R_PPC64_DTPREL16_LO_DS:
14248           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14249           mask = 3;
14250           /* If this reloc is against an lq insn, then the value must be
14251              a multiple of 16.  This is somewhat of a hack, but the
14252              "correct" way to do this by defining _DQ forms of all the
14253              _DS relocs bloats all reloc switches in this file.  It
14254              doesn't seem to make much sense to use any of these relocs
14255              in data, so testing the insn should be safe.  */
14256           if ((insn & (0x3f << 26)) == (56u << 26))
14257             mask = 15;
14258           if (((relocation + addend) & mask) != 0)
14259             {
14260               info->callbacks->einfo
14261                 (_("%P: %H: error: %s not a multiple of %u\n"),
14262                  input_bfd, input_section, rel->r_offset,
14263                  ppc64_elf_howto_table[r_type]->name,
14264                  mask + 1);
14265               bfd_set_error (bfd_error_bad_value);
14266               ret = FALSE;
14267               continue;
14268             }
14269           break;
14270         }
14271
14272       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
14273          because such sections are not SEC_ALLOC and thus ld.so will
14274          not process them.  */
14275       if (unresolved_reloc
14276           && !((input_section->flags & SEC_DEBUGGING) != 0
14277                && h->elf.def_dynamic)
14278           && _bfd_elf_section_offset (output_bfd, info, input_section,
14279                                       rel->r_offset) != (bfd_vma) -1)
14280         {
14281           info->callbacks->einfo
14282             (_("%P: %H: unresolvable %s against `%T'\n"),
14283              input_bfd, input_section, rel->r_offset,
14284              ppc64_elf_howto_table[(int) r_type]->name,
14285              h->elf.root.root.string);
14286           ret = FALSE;
14287         }
14288
14289       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
14290                                     input_bfd,
14291                                     input_section,
14292                                     contents,
14293                                     rel->r_offset,
14294                                     relocation,
14295                                     addend);
14296
14297       if (r != bfd_reloc_ok)
14298         {
14299           char *more_info = NULL;
14300           const char *reloc_name = ppc64_elf_howto_table[r_type]->name;
14301
14302           if (reloc_dest != DEST_NORMAL)
14303             {
14304               more_info = bfd_malloc (strlen (reloc_name) + 8);
14305               if (more_info != NULL)
14306                 {
14307                   strcpy (more_info, reloc_name);
14308                   strcat (more_info, (reloc_dest == DEST_OPD
14309                                       ? " (OPD)" : " (stub)"));
14310                   reloc_name = more_info;
14311                 }
14312             }
14313
14314           if (r == bfd_reloc_overflow)
14315             {
14316               if (warned)
14317                 continue;
14318               if (h != NULL
14319                   && h->elf.root.type == bfd_link_hash_undefweak
14320                   && ppc64_elf_howto_table[r_type]->pc_relative)
14321                 {
14322                   /* Assume this is a call protected by other code that
14323                      detects the symbol is undefined.  If this is the case,
14324                      we can safely ignore the overflow.  If not, the
14325                      program is hosed anyway, and a little warning isn't
14326                      going to help.  */
14327
14328                   continue;
14329                 }
14330
14331               if (!((*info->callbacks->reloc_overflow)
14332                     (info, &h->elf.root, sym_name,
14333                      reloc_name, orig_rel.r_addend,
14334                      input_bfd, input_section, rel->r_offset)))
14335                 return FALSE;
14336             }
14337           else
14338             {
14339               info->callbacks->einfo
14340                 (_("%P: %H: %s against `%T': error %d\n"),
14341                  input_bfd, input_section, rel->r_offset,
14342                  reloc_name, sym_name, (int) r);
14343               ret = FALSE;
14344             }
14345           if (more_info != NULL)
14346             free (more_info);
14347         }
14348     }
14349
14350   /* If we're emitting relocations, then shortly after this function
14351      returns, reloc offsets and addends for this section will be
14352      adjusted.  Worse, reloc symbol indices will be for the output
14353      file rather than the input.  Save a copy of the relocs for
14354      opd_entry_value.  */
14355   if (is_opd && (info->emitrelocations || info->relocatable))
14356     {
14357       bfd_size_type amt;
14358       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
14359       rel = bfd_alloc (input_bfd, amt);
14360       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
14361       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
14362       if (rel == NULL)
14363         return FALSE;
14364       memcpy (rel, relocs, amt);
14365     }
14366   return ret;
14367 }
14368
14369 /* Adjust the value of any local symbols in opd sections.  */
14370
14371 static int
14372 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
14373                               const char *name ATTRIBUTE_UNUSED,
14374                               Elf_Internal_Sym *elfsym,
14375                               asection *input_sec,
14376                               struct elf_link_hash_entry *h)
14377 {
14378   struct _opd_sec_data *opd;
14379   long adjust;
14380   bfd_vma value;
14381
14382   if (h != NULL)
14383     return 1;
14384
14385   opd = get_opd_info (input_sec);
14386   if (opd == NULL || opd->adjust == NULL)
14387     return 1;
14388
14389   value = elfsym->st_value - input_sec->output_offset;
14390   if (!info->relocatable)
14391     value -= input_sec->output_section->vma;
14392
14393   adjust = opd->adjust[value / 8];
14394   if (adjust == -1)
14395     return 2;
14396
14397   elfsym->st_value += adjust;
14398   return 1;
14399 }
14400
14401 /* Finish up dynamic symbol handling.  We set the contents of various
14402    dynamic sections here.  */
14403
14404 static bfd_boolean
14405 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
14406                                  struct bfd_link_info *info,
14407                                  struct elf_link_hash_entry *h,
14408                                  Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
14409 {
14410   struct ppc_link_hash_table *htab;
14411   struct plt_entry *ent;
14412   Elf_Internal_Rela rela;
14413   bfd_byte *loc;
14414
14415   htab = ppc_hash_table (info);
14416   if (htab == NULL)
14417     return FALSE;
14418
14419   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14420     if (ent->plt.offset != (bfd_vma) -1)
14421       {
14422         /* This symbol has an entry in the procedure linkage
14423            table.  Set it up.  */
14424         if (!htab->elf.dynamic_sections_created
14425             || h->dynindx == -1)
14426           {
14427             BFD_ASSERT (h->type == STT_GNU_IFUNC
14428                         && h->def_regular
14429                         && (h->root.type == bfd_link_hash_defined
14430                             || h->root.type == bfd_link_hash_defweak));
14431             rela.r_offset = (htab->iplt->output_section->vma
14432                              + htab->iplt->output_offset
14433                              + ent->plt.offset);
14434             if (htab->opd_abi)
14435               rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14436             else
14437               rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14438             rela.r_addend = (h->root.u.def.value
14439                              + h->root.u.def.section->output_offset
14440                              + h->root.u.def.section->output_section->vma
14441                              + ent->addend);
14442             loc = (htab->reliplt->contents
14443                    + (htab->reliplt->reloc_count++
14444                       * sizeof (Elf64_External_Rela)));
14445           }
14446         else
14447           {
14448             rela.r_offset = (htab->plt->output_section->vma
14449                              + htab->plt->output_offset
14450                              + ent->plt.offset);
14451             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14452             rela.r_addend = ent->addend;
14453             loc = (htab->relplt->contents
14454                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
14455                       / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
14456           }
14457         bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14458       }
14459
14460   if (h->needs_copy)
14461     {
14462       /* This symbol needs a copy reloc.  Set it up.  */
14463
14464       if (h->dynindx == -1
14465           || (h->root.type != bfd_link_hash_defined
14466               && h->root.type != bfd_link_hash_defweak)
14467           || htab->relbss == NULL)
14468         abort ();
14469
14470       rela.r_offset = (h->root.u.def.value
14471                        + h->root.u.def.section->output_section->vma
14472                        + h->root.u.def.section->output_offset);
14473       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
14474       rela.r_addend = 0;
14475       loc = htab->relbss->contents;
14476       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
14477       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14478     }
14479
14480   return TRUE;
14481 }
14482
14483 /* Used to decide how to sort relocs in an optimal manner for the
14484    dynamic linker, before writing them out.  */
14485
14486 static enum elf_reloc_type_class
14487 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
14488                             const asection *rel_sec,
14489                             const Elf_Internal_Rela *rela)
14490 {
14491   enum elf_ppc64_reloc_type r_type;
14492   struct ppc_link_hash_table *htab = ppc_hash_table (info);
14493
14494   if (rel_sec == htab->reliplt)
14495     return reloc_class_ifunc;
14496
14497   r_type = ELF64_R_TYPE (rela->r_info);
14498   switch (r_type)
14499     {
14500     case R_PPC64_RELATIVE:
14501       return reloc_class_relative;
14502     case R_PPC64_JMP_SLOT:
14503       return reloc_class_plt;
14504     case R_PPC64_COPY:
14505       return reloc_class_copy;
14506     default:
14507       return reloc_class_normal;
14508     }
14509 }
14510
14511 /* Finish up the dynamic sections.  */
14512
14513 static bfd_boolean
14514 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
14515                                    struct bfd_link_info *info)
14516 {
14517   struct ppc_link_hash_table *htab;
14518   bfd *dynobj;
14519   asection *sdyn;
14520
14521   htab = ppc_hash_table (info);
14522   if (htab == NULL)
14523     return FALSE;
14524
14525   dynobj = htab->elf.dynobj;
14526   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
14527
14528   if (htab->elf.dynamic_sections_created)
14529     {
14530       Elf64_External_Dyn *dyncon, *dynconend;
14531
14532       if (sdyn == NULL || htab->got == NULL)
14533         abort ();
14534
14535       dyncon = (Elf64_External_Dyn *) sdyn->contents;
14536       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
14537       for (; dyncon < dynconend; dyncon++)
14538         {
14539           Elf_Internal_Dyn dyn;
14540           asection *s;
14541
14542           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
14543
14544           switch (dyn.d_tag)
14545             {
14546             default:
14547               continue;
14548
14549             case DT_PPC64_GLINK:
14550               s = htab->glink;
14551               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14552               /* We stupidly defined DT_PPC64_GLINK to be the start
14553                  of glink rather than the first entry point, which is
14554                  what ld.so needs, and now have a bigger stub to
14555                  support automatic multiple TOCs.  */
14556               dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
14557               break;
14558
14559             case DT_PPC64_OPD:
14560               s = bfd_get_section_by_name (output_bfd, ".opd");
14561               if (s == NULL)
14562                 continue;
14563               dyn.d_un.d_ptr = s->vma;
14564               break;
14565
14566             case DT_PPC64_OPDSZ:
14567               s = bfd_get_section_by_name (output_bfd, ".opd");
14568               if (s == NULL)
14569                 continue;
14570               dyn.d_un.d_val = s->size;
14571               break;
14572
14573             case DT_PLTGOT:
14574               s = htab->plt;
14575               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14576               break;
14577
14578             case DT_JMPREL:
14579               s = htab->relplt;
14580               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14581               break;
14582
14583             case DT_PLTRELSZ:
14584               dyn.d_un.d_val = htab->relplt->size;
14585               break;
14586
14587             case DT_RELASZ:
14588               /* Don't count procedure linkage table relocs in the
14589                  overall reloc count.  */
14590               s = htab->relplt;
14591               if (s == NULL)
14592                 continue;
14593               dyn.d_un.d_val -= s->size;
14594               break;
14595
14596             case DT_RELA:
14597               /* We may not be using the standard ELF linker script.
14598                  If .rela.plt is the first .rela section, we adjust
14599                  DT_RELA to not include it.  */
14600               s = htab->relplt;
14601               if (s == NULL)
14602                 continue;
14603               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
14604                 continue;
14605               dyn.d_un.d_ptr += s->size;
14606               break;
14607             }
14608
14609           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
14610         }
14611     }
14612
14613   if (htab->got != NULL && htab->got->size != 0)
14614     {
14615       /* Fill in the first entry in the global offset table.
14616          We use it to hold the link-time TOCbase.  */
14617       bfd_put_64 (output_bfd,
14618                   elf_gp (output_bfd) + TOC_BASE_OFF,
14619                   htab->got->contents);
14620
14621       /* Set .got entry size.  */
14622       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
14623     }
14624
14625   if (htab->plt != NULL && htab->plt->size != 0)
14626     {
14627       /* Set .plt entry size.  */
14628       elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
14629         = PLT_ENTRY_SIZE;
14630     }
14631
14632   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
14633      brlt ourselves if emitrelocations.  */
14634   if (htab->brlt != NULL
14635       && htab->brlt->reloc_count != 0
14636       && !_bfd_elf_link_output_relocs (output_bfd,
14637                                        htab->brlt,
14638                                        elf_section_data (htab->brlt)->rela.hdr,
14639                                        elf_section_data (htab->brlt)->relocs,
14640                                        NULL))
14641     return FALSE;
14642
14643   if (htab->glink != NULL
14644       && htab->glink->reloc_count != 0
14645       && !_bfd_elf_link_output_relocs (output_bfd,
14646                                        htab->glink,
14647                                        elf_section_data (htab->glink)->rela.hdr,
14648                                        elf_section_data (htab->glink)->relocs,
14649                                        NULL))
14650     return FALSE;
14651
14652
14653   if (htab->glink_eh_frame != NULL
14654       && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
14655       && !_bfd_elf_write_section_eh_frame (output_bfd, info,
14656                                            htab->glink_eh_frame,
14657                                            htab->glink_eh_frame->contents))
14658     return FALSE;
14659
14660   /* We need to handle writing out multiple GOT sections ourselves,
14661      since we didn't add them to DYNOBJ.  We know dynobj is the first
14662      bfd.  */
14663   while ((dynobj = dynobj->link_next) != NULL)
14664     {
14665       asection *s;
14666
14667       if (!is_ppc64_elf (dynobj))
14668         continue;
14669
14670       s = ppc64_elf_tdata (dynobj)->got;
14671       if (s != NULL
14672           && s->size != 0
14673           && s->output_section != bfd_abs_section_ptr
14674           && !bfd_set_section_contents (output_bfd, s->output_section,
14675                                         s->contents, s->output_offset,
14676                                         s->size))
14677         return FALSE;
14678       s = ppc64_elf_tdata (dynobj)->relgot;
14679       if (s != NULL
14680           && s->size != 0
14681           && s->output_section != bfd_abs_section_ptr
14682           && !bfd_set_section_contents (output_bfd, s->output_section,
14683                                         s->contents, s->output_offset,
14684                                         s->size))
14685         return FALSE;
14686     }
14687
14688   return TRUE;
14689 }
14690
14691 #include "elf64-target.h"
14692
14693 /* FreeBSD support */
14694
14695 #undef  TARGET_LITTLE_SYM
14696 #undef  TARGET_LITTLE_NAME
14697
14698 #undef  TARGET_BIG_SYM
14699 #define TARGET_BIG_SYM  bfd_elf64_powerpc_freebsd_vec
14700 #undef  TARGET_BIG_NAME
14701 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
14702
14703 #undef  ELF_OSABI
14704 #define ELF_OSABI       ELFOSABI_FREEBSD
14705
14706 #undef  elf64_bed
14707 #define elf64_bed       elf64_powerpc_fbsd_bed
14708
14709 #include "elf64-target.h"
14710