]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/binutils/bfd/elf64-ppc.c
Merge ^vendor/binutils/dist@214082 into contrib/binutils.
[FreeBSD/FreeBSD.git] / contrib / binutils / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
3    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 <amodra@bigpond.net.au>
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 2 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 /* The 64-bit PowerPC ELF ABI may be found at
25    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
27
28 #include "bfd.h"
29 #include "sysdep.h"
30 #include "bfdlink.h"
31 #include "libbfd.h"
32 #include "elf-bfd.h"
33 #include "elf/ppc64.h"
34 #include "elf64-ppc.h"
35
36 static bfd_reloc_status_type ppc64_elf_ha_reloc
37   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
38 static bfd_reloc_status_type ppc64_elf_branch_reloc
39   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
41   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
43   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
45   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_toc_reloc
47   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
49   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc64_reloc
51   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
53   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_vma opd_entry_value
55   (asection *, bfd_vma, asection **, bfd_vma *);
56
57 #define TARGET_LITTLE_SYM       bfd_elf64_powerpcle_vec
58 #define TARGET_LITTLE_NAME      "elf64-powerpcle"
59 #define TARGET_BIG_SYM          bfd_elf64_powerpc_vec
60 #define TARGET_BIG_NAME         "elf64-powerpc"
61 #define ELF_ARCH                bfd_arch_powerpc
62 #define ELF_MACHINE_CODE        EM_PPC64
63 #define ELF_MAXPAGESIZE         0x10000
64 #define elf_info_to_howto       ppc64_elf_info_to_howto
65
66 #define elf_backend_want_got_sym 0
67 #define elf_backend_want_plt_sym 0
68 #define elf_backend_plt_alignment 3
69 #define elf_backend_plt_not_loaded 1
70 #define elf_backend_got_header_size 8
71 #define elf_backend_can_gc_sections 1
72 #define elf_backend_can_refcount 1
73 #define elf_backend_rela_normal 1
74
75 #define bfd_elf64_mkobject                    ppc64_elf_mkobject
76 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
77 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
78 #define bfd_elf64_new_section_hook            ppc64_elf_new_section_hook
79 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
80 #define bfd_elf64_bfd_link_hash_table_free    ppc64_elf_link_hash_table_free
81 #define bfd_elf64_get_synthetic_symtab        ppc64_elf_get_synthetic_symtab
82
83 #define elf_backend_object_p                  ppc64_elf_object_p
84 #define elf_backend_grok_prstatus             ppc64_elf_grok_prstatus
85 #define elf_backend_grok_psinfo               ppc64_elf_grok_psinfo
86 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
87 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
88 #define elf_backend_add_symbol_hook           ppc64_elf_add_symbol_hook
89 #define elf_backend_check_directives          ppc64_elf_check_directives
90 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
91 #define elf_backend_check_relocs              ppc64_elf_check_relocs
92 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
93 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
94 #define elf_backend_gc_sweep_hook             ppc64_elf_gc_sweep_hook
95 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
96 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
97 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
98 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
99 #define elf_backend_action_discarded          ppc64_elf_action_discarded
100 #define elf_backend_relocate_section          ppc64_elf_relocate_section
101 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
102 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
103 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
104 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
105 #define elf_backend_special_sections          ppc64_elf_special_sections
106
107 /* The name of the dynamic interpreter.  This is put in the .interp
108    section.  */
109 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
110
111 /* The size in bytes of an entry in the procedure linkage table.  */
112 #define PLT_ENTRY_SIZE 24
113
114 /* The initial size of the plt reserved for the dynamic linker.  */
115 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
116
117 /* TOC base pointers offset from start of TOC.  */
118 #define TOC_BASE_OFF    0x8000
119
120 /* Offset of tp and dtp pointers from start of TLS block.  */
121 #define TP_OFFSET       0x7000
122 #define DTP_OFFSET      0x8000
123
124 /* .plt call stub instructions.  The normal stub is like this, but
125    sometimes the .plt entry crosses a 64k boundary and we need to
126    insert an addis to adjust r12.  */
127 #define PLT_CALL_STUB_SIZE (7*4)
128 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha     */
129 #define STD_R2_40R1     0xf8410028      /* std   %r2,40(%r1)         */
130 #define LD_R11_0R12     0xe96c0000      /* ld    %r11,xxx+0@l(%r12)  */
131 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,xxx+8@l(%r12)   */
132 #define MTCTR_R11       0x7d6903a6      /* mtctr %r11                */
133                                         /* ld    %r11,xxx+16@l(%r12) */
134 #define BCTR            0x4e800420      /* bctr                      */
135
136
137 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
138 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
139
140 #define LD_R2_40R1      0xe8410028      /* ld    %r2,40(%r1)     */
141
142 /* glink call stub instructions.  We enter with the index in R0, and the
143    address of glink entry in CTR.  From that, we can calculate PLT0.  */
144 #define GLINK_CALL_STUB_SIZE (16*4)
145 #define MFCTR_R12       0x7d8902a6      /* mfctr  %r12                  */
146 #define SLDI_R11_R0_3   0x780b1f24      /* sldi   %r11,%r0,3            */
147 #define ADDIC_R2_R0_32K 0x34408000      /* addic. %r2,%r0,-32768        */
148 #define SUB_R12_R12_R11 0x7d8b6050      /* sub    %r12,%r12,%r11        */
149 #define SRADI_R2_R2_63  0x7c42fe76      /* sradi  %r2,%r2,63            */
150 #define SLDI_R11_R0_2   0x780b1764      /* sldi   %r11,%r0,2            */
151 #define AND_R2_R2_R11   0x7c425838      /* and    %r2,%r2,%r11          */
152                                         /* sub    %r12,%r12,%r11        */
153 #define ADD_R12_R12_R2  0x7d8c1214      /* add    %r12,%r12,%r2         */
154 #define ADDIS_R12_R12   0x3d8c0000      /* addis  %r12,%r12,xxx@ha      */
155                                         /* ld     %r11,xxx@l(%r12)      */
156 #define ADDI_R12_R12    0x398c0000      /* addi   %r12,%r12,xxx@l       */
157                                         /* ld     %r2,8(%r12)           */
158                                         /* mtctr  %r11                  */
159                                         /* ld     %r11,16(%r12)         */
160                                         /* bctr                         */
161
162 /* Pad with this.  */
163 #define NOP             0x60000000
164
165 /* Some other nops.  */
166 #define CROR_151515     0x4def7b82
167 #define CROR_313131     0x4ffffb82
168
169 /* .glink entries for the first 32k functions are two instructions.  */
170 #define LI_R0_0         0x38000000      /* li    %r0,0          */
171 #define B_DOT           0x48000000      /* b     .              */
172
173 /* After that, we need two instructions to load the index, followed by
174    a branch.  */
175 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
176 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
177
178 /* Instructions used by the save and restore reg functions.  */
179 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
180 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
181 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
182 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
183 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
184 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
185 #define LI_R12_0        0x39800000      /* li    %r12,0         */
186 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
187 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
188 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
189 #define BLR             0x4e800020      /* blr                  */
190
191 /* Since .opd is an array of descriptors and each entry will end up
192    with identical R_PPC64_RELATIVE relocs, there is really no need to
193    propagate .opd relocs;  The dynamic linker should be taught to
194    relocate .opd without reloc entries.  */
195 #ifndef NO_OPD_RELOCS
196 #define NO_OPD_RELOCS 0
197 #endif
198 \f
199 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
200
201 /* Relocation HOWTO's.  */
202 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
203
204 static reloc_howto_type ppc64_elf_howto_raw[] = {
205   /* This reloc does nothing.  */
206   HOWTO (R_PPC64_NONE,          /* type */
207          0,                     /* rightshift */
208          2,                     /* size (0 = byte, 1 = short, 2 = long) */
209          32,                    /* bitsize */
210          FALSE,                 /* pc_relative */
211          0,                     /* bitpos */
212          complain_overflow_dont, /* complain_on_overflow */
213          bfd_elf_generic_reloc, /* special_function */
214          "R_PPC64_NONE",        /* name */
215          FALSE,                 /* partial_inplace */
216          0,                     /* src_mask */
217          0,                     /* dst_mask */
218          FALSE),                /* pcrel_offset */
219
220   /* A standard 32 bit relocation.  */
221   HOWTO (R_PPC64_ADDR32,        /* type */
222          0,                     /* rightshift */
223          2,                     /* size (0 = byte, 1 = short, 2 = long) */
224          32,                    /* bitsize */
225          FALSE,                 /* pc_relative */
226          0,                     /* bitpos */
227          complain_overflow_bitfield, /* complain_on_overflow */
228          bfd_elf_generic_reloc, /* special_function */
229          "R_PPC64_ADDR32",      /* name */
230          FALSE,                 /* partial_inplace */
231          0,                     /* src_mask */
232          0xffffffff,            /* dst_mask */
233          FALSE),                /* pcrel_offset */
234
235   /* An absolute 26 bit branch; the lower two bits must be zero.
236      FIXME: we don't check that, we just clear them.  */
237   HOWTO (R_PPC64_ADDR24,        /* type */
238          0,                     /* rightshift */
239          2,                     /* size (0 = byte, 1 = short, 2 = long) */
240          26,                    /* bitsize */
241          FALSE,                 /* pc_relative */
242          0,                     /* bitpos */
243          complain_overflow_bitfield, /* complain_on_overflow */
244          bfd_elf_generic_reloc, /* special_function */
245          "R_PPC64_ADDR24",      /* name */
246          FALSE,                 /* partial_inplace */
247          0,                     /* src_mask */
248          0x03fffffc,            /* dst_mask */
249          FALSE),                /* pcrel_offset */
250
251   /* A standard 16 bit relocation.  */
252   HOWTO (R_PPC64_ADDR16,        /* type */
253          0,                     /* rightshift */
254          1,                     /* size (0 = byte, 1 = short, 2 = long) */
255          16,                    /* bitsize */
256          FALSE,                 /* pc_relative */
257          0,                     /* bitpos */
258          complain_overflow_bitfield, /* complain_on_overflow */
259          bfd_elf_generic_reloc, /* special_function */
260          "R_PPC64_ADDR16",      /* name */
261          FALSE,                 /* partial_inplace */
262          0,                     /* src_mask */
263          0xffff,                /* dst_mask */
264          FALSE),                /* pcrel_offset */
265
266   /* A 16 bit relocation without overflow.  */
267   HOWTO (R_PPC64_ADDR16_LO,     /* type */
268          0,                     /* rightshift */
269          1,                     /* size (0 = byte, 1 = short, 2 = long) */
270          16,                    /* bitsize */
271          FALSE,                 /* pc_relative */
272          0,                     /* bitpos */
273          complain_overflow_dont,/* complain_on_overflow */
274          bfd_elf_generic_reloc, /* special_function */
275          "R_PPC64_ADDR16_LO",   /* name */
276          FALSE,                 /* partial_inplace */
277          0,                     /* src_mask */
278          0xffff,                /* dst_mask */
279          FALSE),                /* pcrel_offset */
280
281   /* Bits 16-31 of an address.  */
282   HOWTO (R_PPC64_ADDR16_HI,     /* type */
283          16,                    /* rightshift */
284          1,                     /* size (0 = byte, 1 = short, 2 = long) */
285          16,                    /* bitsize */
286          FALSE,                 /* pc_relative */
287          0,                     /* bitpos */
288          complain_overflow_dont, /* complain_on_overflow */
289          bfd_elf_generic_reloc, /* special_function */
290          "R_PPC64_ADDR16_HI",   /* name */
291          FALSE,                 /* partial_inplace */
292          0,                     /* src_mask */
293          0xffff,                /* dst_mask */
294          FALSE),                /* pcrel_offset */
295
296   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
297      bits, treated as a signed number, is negative.  */
298   HOWTO (R_PPC64_ADDR16_HA,     /* type */
299          16,                    /* rightshift */
300          1,                     /* size (0 = byte, 1 = short, 2 = long) */
301          16,                    /* bitsize */
302          FALSE,                 /* pc_relative */
303          0,                     /* bitpos */
304          complain_overflow_dont, /* complain_on_overflow */
305          ppc64_elf_ha_reloc,    /* special_function */
306          "R_PPC64_ADDR16_HA",   /* name */
307          FALSE,                 /* partial_inplace */
308          0,                     /* src_mask */
309          0xffff,                /* dst_mask */
310          FALSE),                /* pcrel_offset */
311
312   /* An absolute 16 bit branch; the lower two bits must be zero.
313      FIXME: we don't check that, we just clear them.  */
314   HOWTO (R_PPC64_ADDR14,        /* type */
315          0,                     /* rightshift */
316          2,                     /* size (0 = byte, 1 = short, 2 = long) */
317          16,                    /* bitsize */
318          FALSE,                 /* pc_relative */
319          0,                     /* bitpos */
320          complain_overflow_bitfield, /* complain_on_overflow */
321          ppc64_elf_branch_reloc, /* special_function */
322          "R_PPC64_ADDR14",      /* name */
323          FALSE,                 /* partial_inplace */
324          0,                     /* src_mask */
325          0x0000fffc,            /* dst_mask */
326          FALSE),                /* pcrel_offset */
327
328   /* An absolute 16 bit branch, for which bit 10 should be set to
329      indicate that the branch is expected to be taken.  The lower two
330      bits must be zero.  */
331   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
332          0,                     /* rightshift */
333          2,                     /* size (0 = byte, 1 = short, 2 = long) */
334          16,                    /* bitsize */
335          FALSE,                 /* pc_relative */
336          0,                     /* bitpos */
337          complain_overflow_bitfield, /* complain_on_overflow */
338          ppc64_elf_brtaken_reloc, /* special_function */
339          "R_PPC64_ADDR14_BRTAKEN",/* name */
340          FALSE,                 /* partial_inplace */
341          0,                     /* src_mask */
342          0x0000fffc,            /* dst_mask */
343          FALSE),                /* pcrel_offset */
344
345   /* An absolute 16 bit branch, for which bit 10 should be set to
346      indicate that the branch is not expected to be taken.  The lower
347      two bits must be zero.  */
348   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
349          0,                     /* rightshift */
350          2,                     /* size (0 = byte, 1 = short, 2 = long) */
351          16,                    /* bitsize */
352          FALSE,                 /* pc_relative */
353          0,                     /* bitpos */
354          complain_overflow_bitfield, /* complain_on_overflow */
355          ppc64_elf_brtaken_reloc, /* special_function */
356          "R_PPC64_ADDR14_BRNTAKEN",/* name */
357          FALSE,                 /* partial_inplace */
358          0,                     /* src_mask */
359          0x0000fffc,            /* dst_mask */
360          FALSE),                /* pcrel_offset */
361
362   /* A relative 26 bit branch; the lower two bits must be zero.  */
363   HOWTO (R_PPC64_REL24,         /* type */
364          0,                     /* rightshift */
365          2,                     /* size (0 = byte, 1 = short, 2 = long) */
366          26,                    /* bitsize */
367          TRUE,                  /* pc_relative */
368          0,                     /* bitpos */
369          complain_overflow_signed, /* complain_on_overflow */
370          ppc64_elf_branch_reloc, /* special_function */
371          "R_PPC64_REL24",       /* name */
372          FALSE,                 /* partial_inplace */
373          0,                     /* src_mask */
374          0x03fffffc,            /* dst_mask */
375          TRUE),                 /* pcrel_offset */
376
377   /* A relative 16 bit branch; the lower two bits must be zero.  */
378   HOWTO (R_PPC64_REL14,         /* type */
379          0,                     /* rightshift */
380          2,                     /* size (0 = byte, 1 = short, 2 = long) */
381          16,                    /* bitsize */
382          TRUE,                  /* pc_relative */
383          0,                     /* bitpos */
384          complain_overflow_signed, /* complain_on_overflow */
385          ppc64_elf_branch_reloc, /* special_function */
386          "R_PPC64_REL14",       /* name */
387          FALSE,                 /* partial_inplace */
388          0,                     /* src_mask */
389          0x0000fffc,            /* dst_mask */
390          TRUE),                 /* pcrel_offset */
391
392   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
393      the branch is expected to be taken.  The lower two bits must be
394      zero.  */
395   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
396          0,                     /* rightshift */
397          2,                     /* size (0 = byte, 1 = short, 2 = long) */
398          16,                    /* bitsize */
399          TRUE,                  /* pc_relative */
400          0,                     /* bitpos */
401          complain_overflow_signed, /* complain_on_overflow */
402          ppc64_elf_brtaken_reloc, /* special_function */
403          "R_PPC64_REL14_BRTAKEN", /* name */
404          FALSE,                 /* partial_inplace */
405          0,                     /* src_mask */
406          0x0000fffc,            /* dst_mask */
407          TRUE),                 /* pcrel_offset */
408
409   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
410      the branch is not expected to be taken.  The lower two bits must
411      be zero.  */
412   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
413          0,                     /* rightshift */
414          2,                     /* size (0 = byte, 1 = short, 2 = long) */
415          16,                    /* bitsize */
416          TRUE,                  /* pc_relative */
417          0,                     /* bitpos */
418          complain_overflow_signed, /* complain_on_overflow */
419          ppc64_elf_brtaken_reloc, /* special_function */
420          "R_PPC64_REL14_BRNTAKEN",/* name */
421          FALSE,                 /* partial_inplace */
422          0,                     /* src_mask */
423          0x0000fffc,            /* dst_mask */
424          TRUE),                 /* pcrel_offset */
425
426   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
427      symbol.  */
428   HOWTO (R_PPC64_GOT16,         /* type */
429          0,                     /* rightshift */
430          1,                     /* size (0 = byte, 1 = short, 2 = long) */
431          16,                    /* bitsize */
432          FALSE,                 /* pc_relative */
433          0,                     /* bitpos */
434          complain_overflow_signed, /* complain_on_overflow */
435          ppc64_elf_unhandled_reloc, /* special_function */
436          "R_PPC64_GOT16",       /* name */
437          FALSE,                 /* partial_inplace */
438          0,                     /* src_mask */
439          0xffff,                /* dst_mask */
440          FALSE),                /* pcrel_offset */
441
442   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
443      the symbol.  */
444   HOWTO (R_PPC64_GOT16_LO,      /* type */
445          0,                     /* rightshift */
446          1,                     /* size (0 = byte, 1 = short, 2 = long) */
447          16,                    /* bitsize */
448          FALSE,                 /* pc_relative */
449          0,                     /* bitpos */
450          complain_overflow_dont, /* complain_on_overflow */
451          ppc64_elf_unhandled_reloc, /* special_function */
452          "R_PPC64_GOT16_LO",    /* name */
453          FALSE,                 /* partial_inplace */
454          0,                     /* src_mask */
455          0xffff,                /* dst_mask */
456          FALSE),                /* pcrel_offset */
457
458   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
459      the symbol.  */
460   HOWTO (R_PPC64_GOT16_HI,      /* type */
461          16,                    /* rightshift */
462          1,                     /* size (0 = byte, 1 = short, 2 = long) */
463          16,                    /* bitsize */
464          FALSE,                 /* pc_relative */
465          0,                     /* bitpos */
466          complain_overflow_dont,/* complain_on_overflow */
467          ppc64_elf_unhandled_reloc, /* special_function */
468          "R_PPC64_GOT16_HI",    /* name */
469          FALSE,                 /* partial_inplace */
470          0,                     /* src_mask */
471          0xffff,                /* dst_mask */
472          FALSE),                /* pcrel_offset */
473
474   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
475      the symbol.  */
476   HOWTO (R_PPC64_GOT16_HA,      /* type */
477          16,                    /* rightshift */
478          1,                     /* size (0 = byte, 1 = short, 2 = long) */
479          16,                    /* bitsize */
480          FALSE,                 /* pc_relative */
481          0,                     /* bitpos */
482          complain_overflow_dont,/* complain_on_overflow */
483          ppc64_elf_unhandled_reloc, /* special_function */
484          "R_PPC64_GOT16_HA",    /* name */
485          FALSE,                 /* partial_inplace */
486          0,                     /* src_mask */
487          0xffff,                /* dst_mask */
488          FALSE),                /* pcrel_offset */
489
490   /* This is used only by the dynamic linker.  The symbol should exist
491      both in the object being run and in some shared library.  The
492      dynamic linker copies the data addressed by the symbol from the
493      shared library into the object, because the object being
494      run has to have the data at some particular address.  */
495   HOWTO (R_PPC64_COPY,          /* type */
496          0,                     /* rightshift */
497          0,                     /* this one is variable size */
498          0,                     /* bitsize */
499          FALSE,                 /* pc_relative */
500          0,                     /* bitpos */
501          complain_overflow_dont, /* complain_on_overflow */
502          ppc64_elf_unhandled_reloc, /* special_function */
503          "R_PPC64_COPY",        /* name */
504          FALSE,                 /* partial_inplace */
505          0,                     /* src_mask */
506          0,                     /* dst_mask */
507          FALSE),                /* pcrel_offset */
508
509   /* Like R_PPC64_ADDR64, but used when setting global offset table
510      entries.  */
511   HOWTO (R_PPC64_GLOB_DAT,      /* type */
512          0,                     /* rightshift */
513          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
514          64,                    /* bitsize */
515          FALSE,                 /* pc_relative */
516          0,                     /* bitpos */
517          complain_overflow_dont, /* complain_on_overflow */
518          ppc64_elf_unhandled_reloc,  /* special_function */
519          "R_PPC64_GLOB_DAT",    /* name */
520          FALSE,                 /* partial_inplace */
521          0,                     /* src_mask */
522          ONES (64),             /* dst_mask */
523          FALSE),                /* pcrel_offset */
524
525   /* Created by the link editor.  Marks a procedure linkage table
526      entry for a symbol.  */
527   HOWTO (R_PPC64_JMP_SLOT,      /* type */
528          0,                     /* rightshift */
529          0,                     /* size (0 = byte, 1 = short, 2 = long) */
530          0,                     /* bitsize */
531          FALSE,                 /* pc_relative */
532          0,                     /* bitpos */
533          complain_overflow_dont, /* complain_on_overflow */
534          ppc64_elf_unhandled_reloc, /* special_function */
535          "R_PPC64_JMP_SLOT",    /* name */
536          FALSE,                 /* partial_inplace */
537          0,                     /* src_mask */
538          0,                     /* dst_mask */
539          FALSE),                /* pcrel_offset */
540
541   /* Used only by the dynamic linker.  When the object is run, this
542      doubleword64 is set to the load address of the object, plus the
543      addend.  */
544   HOWTO (R_PPC64_RELATIVE,      /* type */
545          0,                     /* rightshift */
546          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
547          64,                    /* bitsize */
548          FALSE,                 /* pc_relative */
549          0,                     /* bitpos */
550          complain_overflow_dont, /* complain_on_overflow */
551          bfd_elf_generic_reloc, /* special_function */
552          "R_PPC64_RELATIVE",    /* name */
553          FALSE,                 /* partial_inplace */
554          0,                     /* src_mask */
555          ONES (64),             /* dst_mask */
556          FALSE),                /* pcrel_offset */
557
558   /* Like R_PPC64_ADDR32, but may be unaligned.  */
559   HOWTO (R_PPC64_UADDR32,       /* type */
560          0,                     /* rightshift */
561          2,                     /* size (0 = byte, 1 = short, 2 = long) */
562          32,                    /* bitsize */
563          FALSE,                 /* pc_relative */
564          0,                     /* bitpos */
565          complain_overflow_bitfield, /* complain_on_overflow */
566          bfd_elf_generic_reloc, /* special_function */
567          "R_PPC64_UADDR32",     /* name */
568          FALSE,                 /* partial_inplace */
569          0,                     /* src_mask */
570          0xffffffff,            /* dst_mask */
571          FALSE),                /* pcrel_offset */
572
573   /* Like R_PPC64_ADDR16, but may be unaligned.  */
574   HOWTO (R_PPC64_UADDR16,       /* type */
575          0,                     /* rightshift */
576          1,                     /* size (0 = byte, 1 = short, 2 = long) */
577          16,                    /* bitsize */
578          FALSE,                 /* pc_relative */
579          0,                     /* bitpos */
580          complain_overflow_bitfield, /* complain_on_overflow */
581          bfd_elf_generic_reloc, /* special_function */
582          "R_PPC64_UADDR16",     /* name */
583          FALSE,                 /* partial_inplace */
584          0,                     /* src_mask */
585          0xffff,                /* dst_mask */
586          FALSE),                /* pcrel_offset */
587
588   /* 32-bit PC relative.  */
589   HOWTO (R_PPC64_REL32,         /* type */
590          0,                     /* rightshift */
591          2,                     /* size (0 = byte, 1 = short, 2 = long) */
592          32,                    /* bitsize */
593          TRUE,                  /* pc_relative */
594          0,                     /* bitpos */
595          /* FIXME: Verify.  Was complain_overflow_bitfield.  */
596          complain_overflow_signed, /* complain_on_overflow */
597          bfd_elf_generic_reloc, /* special_function */
598          "R_PPC64_REL32",       /* name */
599          FALSE,                 /* partial_inplace */
600          0,                     /* src_mask */
601          0xffffffff,            /* dst_mask */
602          TRUE),                 /* pcrel_offset */
603
604   /* 32-bit relocation to the symbol's procedure linkage table.  */
605   HOWTO (R_PPC64_PLT32,         /* type */
606          0,                     /* rightshift */
607          2,                     /* size (0 = byte, 1 = short, 2 = long) */
608          32,                    /* bitsize */
609          FALSE,                 /* pc_relative */
610          0,                     /* bitpos */
611          complain_overflow_bitfield, /* complain_on_overflow */
612          ppc64_elf_unhandled_reloc, /* special_function */
613          "R_PPC64_PLT32",       /* name */
614          FALSE,                 /* partial_inplace */
615          0,                     /* src_mask */
616          0xffffffff,            /* dst_mask */
617          FALSE),                /* pcrel_offset */
618
619   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
620      FIXME: R_PPC64_PLTREL32 not supported.  */
621   HOWTO (R_PPC64_PLTREL32,      /* type */
622          0,                     /* rightshift */
623          2,                     /* size (0 = byte, 1 = short, 2 = long) */
624          32,                    /* bitsize */
625          TRUE,                  /* pc_relative */
626          0,                     /* bitpos */
627          complain_overflow_signed, /* complain_on_overflow */
628          bfd_elf_generic_reloc, /* special_function */
629          "R_PPC64_PLTREL32",    /* name */
630          FALSE,                 /* partial_inplace */
631          0,                     /* src_mask */
632          0xffffffff,            /* dst_mask */
633          TRUE),                 /* pcrel_offset */
634
635   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
636      the symbol.  */
637   HOWTO (R_PPC64_PLT16_LO,      /* type */
638          0,                     /* rightshift */
639          1,                     /* size (0 = byte, 1 = short, 2 = long) */
640          16,                    /* bitsize */
641          FALSE,                 /* pc_relative */
642          0,                     /* bitpos */
643          complain_overflow_dont, /* complain_on_overflow */
644          ppc64_elf_unhandled_reloc, /* special_function */
645          "R_PPC64_PLT16_LO",    /* name */
646          FALSE,                 /* partial_inplace */
647          0,                     /* src_mask */
648          0xffff,                /* dst_mask */
649          FALSE),                /* pcrel_offset */
650
651   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
652      the symbol.  */
653   HOWTO (R_PPC64_PLT16_HI,      /* type */
654          16,                    /* rightshift */
655          1,                     /* size (0 = byte, 1 = short, 2 = long) */
656          16,                    /* bitsize */
657          FALSE,                 /* pc_relative */
658          0,                     /* bitpos */
659          complain_overflow_dont, /* complain_on_overflow */
660          ppc64_elf_unhandled_reloc, /* special_function */
661          "R_PPC64_PLT16_HI",    /* name */
662          FALSE,                 /* partial_inplace */
663          0,                     /* src_mask */
664          0xffff,                /* dst_mask */
665          FALSE),                /* pcrel_offset */
666
667   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
668      the symbol.  */
669   HOWTO (R_PPC64_PLT16_HA,      /* type */
670          16,                    /* rightshift */
671          1,                     /* size (0 = byte, 1 = short, 2 = long) */
672          16,                    /* bitsize */
673          FALSE,                 /* pc_relative */
674          0,                     /* bitpos */
675          complain_overflow_dont, /* complain_on_overflow */
676          ppc64_elf_unhandled_reloc, /* special_function */
677          "R_PPC64_PLT16_HA",    /* name */
678          FALSE,                 /* partial_inplace */
679          0,                     /* src_mask */
680          0xffff,                /* dst_mask */
681          FALSE),                /* pcrel_offset */
682
683   /* 16-bit section relative relocation.  */
684   HOWTO (R_PPC64_SECTOFF,       /* type */
685          0,                     /* rightshift */
686          1,                     /* size (0 = byte, 1 = short, 2 = long) */
687          16,                    /* bitsize */
688          FALSE,                 /* pc_relative */
689          0,                     /* bitpos */
690          complain_overflow_bitfield, /* complain_on_overflow */
691          ppc64_elf_sectoff_reloc, /* special_function */
692          "R_PPC64_SECTOFF",     /* name */
693          FALSE,                 /* partial_inplace */
694          0,                     /* src_mask */
695          0xffff,                /* dst_mask */
696          FALSE),                /* pcrel_offset */
697
698   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
699   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
700          0,                     /* rightshift */
701          1,                     /* size (0 = byte, 1 = short, 2 = long) */
702          16,                    /* bitsize */
703          FALSE,                 /* pc_relative */
704          0,                     /* bitpos */
705          complain_overflow_dont, /* complain_on_overflow */
706          ppc64_elf_sectoff_reloc, /* special_function */
707          "R_PPC64_SECTOFF_LO",  /* name */
708          FALSE,                 /* partial_inplace */
709          0,                     /* src_mask */
710          0xffff,                /* dst_mask */
711          FALSE),                /* pcrel_offset */
712
713   /* 16-bit upper half section relative relocation.  */
714   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
715          16,                    /* rightshift */
716          1,                     /* size (0 = byte, 1 = short, 2 = long) */
717          16,                    /* bitsize */
718          FALSE,                 /* pc_relative */
719          0,                     /* bitpos */
720          complain_overflow_dont, /* complain_on_overflow */
721          ppc64_elf_sectoff_reloc, /* special_function */
722          "R_PPC64_SECTOFF_HI",  /* name */
723          FALSE,                 /* partial_inplace */
724          0,                     /* src_mask */
725          0xffff,                /* dst_mask */
726          FALSE),                /* pcrel_offset */
727
728   /* 16-bit upper half adjusted section relative relocation.  */
729   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
730          16,                    /* rightshift */
731          1,                     /* size (0 = byte, 1 = short, 2 = long) */
732          16,                    /* bitsize */
733          FALSE,                 /* pc_relative */
734          0,                     /* bitpos */
735          complain_overflow_dont, /* complain_on_overflow */
736          ppc64_elf_sectoff_ha_reloc, /* special_function */
737          "R_PPC64_SECTOFF_HA",  /* name */
738          FALSE,                 /* partial_inplace */
739          0,                     /* src_mask */
740          0xffff,                /* dst_mask */
741          FALSE),                /* pcrel_offset */
742
743   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
744   HOWTO (R_PPC64_REL30,         /* type */
745          2,                     /* rightshift */
746          2,                     /* size (0 = byte, 1 = short, 2 = long) */
747          30,                    /* bitsize */
748          TRUE,                  /* pc_relative */
749          0,                     /* bitpos */
750          complain_overflow_dont, /* complain_on_overflow */
751          bfd_elf_generic_reloc, /* special_function */
752          "R_PPC64_REL30",       /* name */
753          FALSE,                 /* partial_inplace */
754          0,                     /* src_mask */
755          0xfffffffc,            /* dst_mask */
756          TRUE),                 /* pcrel_offset */
757
758   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
759
760   /* A standard 64-bit relocation.  */
761   HOWTO (R_PPC64_ADDR64,        /* type */
762          0,                     /* rightshift */
763          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
764          64,                    /* bitsize */
765          FALSE,                 /* pc_relative */
766          0,                     /* bitpos */
767          complain_overflow_dont, /* complain_on_overflow */
768          bfd_elf_generic_reloc, /* special_function */
769          "R_PPC64_ADDR64",      /* name */
770          FALSE,                 /* partial_inplace */
771          0,                     /* src_mask */
772          ONES (64),             /* dst_mask */
773          FALSE),                /* pcrel_offset */
774
775   /* The bits 32-47 of an address.  */
776   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
777          32,                    /* rightshift */
778          1,                     /* size (0 = byte, 1 = short, 2 = long) */
779          16,                    /* bitsize */
780          FALSE,                 /* pc_relative */
781          0,                     /* bitpos */
782          complain_overflow_dont, /* complain_on_overflow */
783          bfd_elf_generic_reloc, /* special_function */
784          "R_PPC64_ADDR16_HIGHER", /* name */
785          FALSE,                 /* partial_inplace */
786          0,                     /* src_mask */
787          0xffff,                /* dst_mask */
788          FALSE),                /* pcrel_offset */
789
790   /* The bits 32-47 of an address, plus 1 if the contents of the low
791      16 bits, treated as a signed number, is negative.  */
792   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
793          32,                    /* rightshift */
794          1,                     /* size (0 = byte, 1 = short, 2 = long) */
795          16,                    /* bitsize */
796          FALSE,                 /* pc_relative */
797          0,                     /* bitpos */
798          complain_overflow_dont, /* complain_on_overflow */
799          ppc64_elf_ha_reloc,    /* special_function */
800          "R_PPC64_ADDR16_HIGHERA", /* name */
801          FALSE,                 /* partial_inplace */
802          0,                     /* src_mask */
803          0xffff,                /* dst_mask */
804          FALSE),                /* pcrel_offset */
805
806   /* The bits 48-63 of an address.  */
807   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
808          48,                    /* rightshift */
809          1,                     /* size (0 = byte, 1 = short, 2 = long) */
810          16,                    /* bitsize */
811          FALSE,                 /* pc_relative */
812          0,                     /* bitpos */
813          complain_overflow_dont, /* complain_on_overflow */
814          bfd_elf_generic_reloc, /* special_function */
815          "R_PPC64_ADDR16_HIGHEST", /* name */
816          FALSE,                 /* partial_inplace */
817          0,                     /* src_mask */
818          0xffff,                /* dst_mask */
819          FALSE),                /* pcrel_offset */
820
821   /* The bits 48-63 of an address, plus 1 if the contents of the low
822      16 bits, treated as a signed number, is negative.  */
823   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
824          48,                    /* rightshift */
825          1,                     /* size (0 = byte, 1 = short, 2 = long) */
826          16,                    /* bitsize */
827          FALSE,                 /* pc_relative */
828          0,                     /* bitpos */
829          complain_overflow_dont, /* complain_on_overflow */
830          ppc64_elf_ha_reloc,    /* special_function */
831          "R_PPC64_ADDR16_HIGHESTA", /* name */
832          FALSE,                 /* partial_inplace */
833          0,                     /* src_mask */
834          0xffff,                /* dst_mask */
835          FALSE),                /* pcrel_offset */
836
837   /* Like ADDR64, but may be unaligned.  */
838   HOWTO (R_PPC64_UADDR64,       /* type */
839          0,                     /* rightshift */
840          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
841          64,                    /* bitsize */
842          FALSE,                 /* pc_relative */
843          0,                     /* bitpos */
844          complain_overflow_dont, /* complain_on_overflow */
845          bfd_elf_generic_reloc, /* special_function */
846          "R_PPC64_UADDR64",     /* name */
847          FALSE,                 /* partial_inplace */
848          0,                     /* src_mask */
849          ONES (64),             /* dst_mask */
850          FALSE),                /* pcrel_offset */
851
852   /* 64-bit relative relocation.  */
853   HOWTO (R_PPC64_REL64,         /* type */
854          0,                     /* rightshift */
855          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
856          64,                    /* bitsize */
857          TRUE,                  /* pc_relative */
858          0,                     /* bitpos */
859          complain_overflow_dont, /* complain_on_overflow */
860          bfd_elf_generic_reloc, /* special_function */
861          "R_PPC64_REL64",       /* name */
862          FALSE,                 /* partial_inplace */
863          0,                     /* src_mask */
864          ONES (64),             /* dst_mask */
865          TRUE),                 /* pcrel_offset */
866
867   /* 64-bit relocation to the symbol's procedure linkage table.  */
868   HOWTO (R_PPC64_PLT64,         /* type */
869          0,                     /* rightshift */
870          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
871          64,                    /* bitsize */
872          FALSE,                 /* pc_relative */
873          0,                     /* bitpos */
874          complain_overflow_dont, /* complain_on_overflow */
875          ppc64_elf_unhandled_reloc, /* special_function */
876          "R_PPC64_PLT64",       /* name */
877          FALSE,                 /* partial_inplace */
878          0,                     /* src_mask */
879          ONES (64),             /* dst_mask */
880          FALSE),                /* pcrel_offset */
881
882   /* 64-bit PC relative relocation to the symbol's procedure linkage
883      table.  */
884   /* FIXME: R_PPC64_PLTREL64 not supported.  */
885   HOWTO (R_PPC64_PLTREL64,      /* type */
886          0,                     /* rightshift */
887          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
888          64,                    /* bitsize */
889          TRUE,                  /* pc_relative */
890          0,                     /* bitpos */
891          complain_overflow_dont, /* complain_on_overflow */
892          ppc64_elf_unhandled_reloc, /* special_function */
893          "R_PPC64_PLTREL64",    /* name */
894          FALSE,                 /* partial_inplace */
895          0,                     /* src_mask */
896          ONES (64),             /* dst_mask */
897          TRUE),                 /* pcrel_offset */
898
899   /* 16 bit TOC-relative relocation.  */
900
901   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
902   HOWTO (R_PPC64_TOC16,         /* type */
903          0,                     /* rightshift */
904          1,                     /* size (0 = byte, 1 = short, 2 = long) */
905          16,                    /* bitsize */
906          FALSE,                 /* pc_relative */
907          0,                     /* bitpos */
908          complain_overflow_signed, /* complain_on_overflow */
909          ppc64_elf_toc_reloc,   /* special_function */
910          "R_PPC64_TOC16",       /* name */
911          FALSE,                 /* partial_inplace */
912          0,                     /* src_mask */
913          0xffff,                /* dst_mask */
914          FALSE),                /* pcrel_offset */
915
916   /* 16 bit TOC-relative relocation without overflow.  */
917
918   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
919   HOWTO (R_PPC64_TOC16_LO,      /* type */
920          0,                     /* rightshift */
921          1,                     /* size (0 = byte, 1 = short, 2 = long) */
922          16,                    /* bitsize */
923          FALSE,                 /* pc_relative */
924          0,                     /* bitpos */
925          complain_overflow_dont, /* complain_on_overflow */
926          ppc64_elf_toc_reloc,   /* special_function */
927          "R_PPC64_TOC16_LO",    /* name */
928          FALSE,                 /* partial_inplace */
929          0,                     /* src_mask */
930          0xffff,                /* dst_mask */
931          FALSE),                /* pcrel_offset */
932
933   /* 16 bit TOC-relative relocation, high 16 bits.  */
934
935   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
936   HOWTO (R_PPC64_TOC16_HI,      /* type */
937          16,                    /* rightshift */
938          1,                     /* size (0 = byte, 1 = short, 2 = long) */
939          16,                    /* bitsize */
940          FALSE,                 /* pc_relative */
941          0,                     /* bitpos */
942          complain_overflow_dont, /* complain_on_overflow */
943          ppc64_elf_toc_reloc,   /* special_function */
944          "R_PPC64_TOC16_HI",    /* name */
945          FALSE,                 /* partial_inplace */
946          0,                     /* src_mask */
947          0xffff,                /* dst_mask */
948          FALSE),                /* pcrel_offset */
949
950   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
951      contents of the low 16 bits, treated as a signed number, is
952      negative.  */
953
954   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
955   HOWTO (R_PPC64_TOC16_HA,      /* type */
956          16,                    /* rightshift */
957          1,                     /* size (0 = byte, 1 = short, 2 = long) */
958          16,                    /* bitsize */
959          FALSE,                 /* pc_relative */
960          0,                     /* bitpos */
961          complain_overflow_dont, /* complain_on_overflow */
962          ppc64_elf_toc_ha_reloc, /* special_function */
963          "R_PPC64_TOC16_HA",    /* name */
964          FALSE,                 /* partial_inplace */
965          0,                     /* src_mask */
966          0xffff,                /* dst_mask */
967          FALSE),                /* pcrel_offset */
968
969   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
970
971   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
972   HOWTO (R_PPC64_TOC,           /* type */
973          0,                     /* rightshift */
974          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
975          64,                    /* bitsize */
976          FALSE,                 /* pc_relative */
977          0,                     /* bitpos */
978          complain_overflow_bitfield, /* complain_on_overflow */
979          ppc64_elf_toc64_reloc, /* special_function */
980          "R_PPC64_TOC",         /* name */
981          FALSE,                 /* partial_inplace */
982          0,                     /* src_mask */
983          ONES (64),             /* dst_mask */
984          FALSE),                /* pcrel_offset */
985
986   /* Like R_PPC64_GOT16, but also informs the link editor that the
987      value to relocate may (!) refer to a PLT entry which the link
988      editor (a) may replace with the symbol value.  If the link editor
989      is unable to fully resolve the symbol, it may (b) create a PLT
990      entry and store the address to the new PLT entry in the GOT.
991      This permits lazy resolution of function symbols at run time.
992      The link editor may also skip all of this and just (c) emit a
993      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
994   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
995     HOWTO (R_PPC64_PLTGOT16,    /* type */
996          0,                     /* rightshift */
997          1,                     /* size (0 = byte, 1 = short, 2 = long) */
998          16,                    /* bitsize */
999          FALSE,                 /* pc_relative */
1000          0,                     /* bitpos */
1001          complain_overflow_signed, /* complain_on_overflow */
1002          ppc64_elf_unhandled_reloc, /* special_function */
1003          "R_PPC64_PLTGOT16",    /* name */
1004          FALSE,                 /* partial_inplace */
1005          0,                     /* src_mask */
1006          0xffff,                /* dst_mask */
1007          FALSE),                /* pcrel_offset */
1008
1009   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1010   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1011   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1012          0,                     /* rightshift */
1013          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1014          16,                    /* bitsize */
1015          FALSE,                 /* pc_relative */
1016          0,                     /* bitpos */
1017          complain_overflow_dont, /* complain_on_overflow */
1018          ppc64_elf_unhandled_reloc, /* special_function */
1019          "R_PPC64_PLTGOT16_LO", /* name */
1020          FALSE,                 /* partial_inplace */
1021          0,                     /* src_mask */
1022          0xffff,                /* dst_mask */
1023          FALSE),                /* pcrel_offset */
1024
1025   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1026   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1027   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1028          16,                    /* rightshift */
1029          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1030          16,                    /* bitsize */
1031          FALSE,                 /* pc_relative */
1032          0,                     /* bitpos */
1033          complain_overflow_dont, /* complain_on_overflow */
1034          ppc64_elf_unhandled_reloc, /* special_function */
1035          "R_PPC64_PLTGOT16_HI", /* name */
1036          FALSE,                 /* partial_inplace */
1037          0,                     /* src_mask */
1038          0xffff,                /* dst_mask */
1039          FALSE),                /* pcrel_offset */
1040
1041   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1042      1 if the contents of the low 16 bits, treated as a signed number,
1043      is negative.  */
1044   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1045   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1046          16,                    /* rightshift */
1047          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1048          16,                    /* bitsize */
1049          FALSE,                 /* pc_relative */
1050          0,                     /* bitpos */
1051          complain_overflow_dont,/* complain_on_overflow */
1052          ppc64_elf_unhandled_reloc, /* special_function */
1053          "R_PPC64_PLTGOT16_HA", /* name */
1054          FALSE,                 /* partial_inplace */
1055          0,                     /* src_mask */
1056          0xffff,                /* dst_mask */
1057          FALSE),                /* pcrel_offset */
1058
1059   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1060   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1061          0,                     /* rightshift */
1062          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1063          16,                    /* bitsize */
1064          FALSE,                 /* pc_relative */
1065          0,                     /* bitpos */
1066          complain_overflow_bitfield, /* complain_on_overflow */
1067          bfd_elf_generic_reloc, /* special_function */
1068          "R_PPC64_ADDR16_DS",   /* name */
1069          FALSE,                 /* partial_inplace */
1070          0,                     /* src_mask */
1071          0xfffc,                /* dst_mask */
1072          FALSE),                /* pcrel_offset */
1073
1074   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1075   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1076          0,                     /* rightshift */
1077          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1078          16,                    /* bitsize */
1079          FALSE,                 /* pc_relative */
1080          0,                     /* bitpos */
1081          complain_overflow_dont,/* complain_on_overflow */
1082          bfd_elf_generic_reloc, /* special_function */
1083          "R_PPC64_ADDR16_LO_DS",/* name */
1084          FALSE,                 /* partial_inplace */
1085          0,                     /* src_mask */
1086          0xfffc,                /* dst_mask */
1087          FALSE),                /* pcrel_offset */
1088
1089   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1090   HOWTO (R_PPC64_GOT16_DS,      /* type */
1091          0,                     /* rightshift */
1092          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1093          16,                    /* bitsize */
1094          FALSE,                 /* pc_relative */
1095          0,                     /* bitpos */
1096          complain_overflow_signed, /* complain_on_overflow */
1097          ppc64_elf_unhandled_reloc, /* special_function */
1098          "R_PPC64_GOT16_DS",    /* name */
1099          FALSE,                 /* partial_inplace */
1100          0,                     /* src_mask */
1101          0xfffc,                /* dst_mask */
1102          FALSE),                /* pcrel_offset */
1103
1104   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1105   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1106          0,                     /* rightshift */
1107          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1108          16,                    /* bitsize */
1109          FALSE,                 /* pc_relative */
1110          0,                     /* bitpos */
1111          complain_overflow_dont, /* complain_on_overflow */
1112          ppc64_elf_unhandled_reloc, /* special_function */
1113          "R_PPC64_GOT16_LO_DS", /* name */
1114          FALSE,                 /* partial_inplace */
1115          0,                     /* src_mask */
1116          0xfffc,                /* dst_mask */
1117          FALSE),                /* pcrel_offset */
1118
1119   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1120   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1121          0,                     /* rightshift */
1122          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1123          16,                    /* bitsize */
1124          FALSE,                 /* pc_relative */
1125          0,                     /* bitpos */
1126          complain_overflow_dont, /* complain_on_overflow */
1127          ppc64_elf_unhandled_reloc, /* special_function */
1128          "R_PPC64_PLT16_LO_DS", /* name */
1129          FALSE,                 /* partial_inplace */
1130          0,                     /* src_mask */
1131          0xfffc,                /* dst_mask */
1132          FALSE),                /* pcrel_offset */
1133
1134   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1135   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1136          0,                     /* rightshift */
1137          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1138          16,                    /* bitsize */
1139          FALSE,                 /* pc_relative */
1140          0,                     /* bitpos */
1141          complain_overflow_bitfield, /* complain_on_overflow */
1142          ppc64_elf_sectoff_reloc, /* special_function */
1143          "R_PPC64_SECTOFF_DS",  /* name */
1144          FALSE,                 /* partial_inplace */
1145          0,                     /* src_mask */
1146          0xfffc,                /* dst_mask */
1147          FALSE),                /* pcrel_offset */
1148
1149   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1150   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1151          0,                     /* rightshift */
1152          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1153          16,                    /* bitsize */
1154          FALSE,                 /* pc_relative */
1155          0,                     /* bitpos */
1156          complain_overflow_dont, /* complain_on_overflow */
1157          ppc64_elf_sectoff_reloc, /* special_function */
1158          "R_PPC64_SECTOFF_LO_DS",/* name */
1159          FALSE,                 /* partial_inplace */
1160          0,                     /* src_mask */
1161          0xfffc,                /* dst_mask */
1162          FALSE),                /* pcrel_offset */
1163
1164   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1165   HOWTO (R_PPC64_TOC16_DS,      /* type */
1166          0,                     /* rightshift */
1167          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1168          16,                    /* bitsize */
1169          FALSE,                 /* pc_relative */
1170          0,                     /* bitpos */
1171          complain_overflow_signed, /* complain_on_overflow */
1172          ppc64_elf_toc_reloc,   /* special_function */
1173          "R_PPC64_TOC16_DS",    /* name */
1174          FALSE,                 /* partial_inplace */
1175          0,                     /* src_mask */
1176          0xfffc,                /* dst_mask */
1177          FALSE),                /* pcrel_offset */
1178
1179   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1180   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1181          0,                     /* rightshift */
1182          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1183          16,                    /* bitsize */
1184          FALSE,                 /* pc_relative */
1185          0,                     /* bitpos */
1186          complain_overflow_dont, /* complain_on_overflow */
1187          ppc64_elf_toc_reloc,   /* special_function */
1188          "R_PPC64_TOC16_LO_DS", /* name */
1189          FALSE,                 /* partial_inplace */
1190          0,                     /* src_mask */
1191          0xfffc,                /* dst_mask */
1192          FALSE),                /* pcrel_offset */
1193
1194   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1195   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1196     HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1197          0,                     /* rightshift */
1198          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1199          16,                    /* bitsize */
1200          FALSE,                 /* pc_relative */
1201          0,                     /* bitpos */
1202          complain_overflow_signed, /* complain_on_overflow */
1203          ppc64_elf_unhandled_reloc, /* special_function */
1204          "R_PPC64_PLTGOT16_DS", /* name */
1205          FALSE,                 /* partial_inplace */
1206          0,                     /* src_mask */
1207          0xfffc,                /* dst_mask */
1208          FALSE),                /* pcrel_offset */
1209
1210   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1211   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1212   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1213          0,                     /* rightshift */
1214          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1215          16,                    /* bitsize */
1216          FALSE,                 /* pc_relative */
1217          0,                     /* bitpos */
1218          complain_overflow_dont, /* complain_on_overflow */
1219          ppc64_elf_unhandled_reloc, /* special_function */
1220          "R_PPC64_PLTGOT16_LO_DS",/* name */
1221          FALSE,                 /* partial_inplace */
1222          0,                     /* src_mask */
1223          0xfffc,                /* dst_mask */
1224          FALSE),                /* pcrel_offset */
1225
1226   /* Marker reloc for TLS.  */
1227   HOWTO (R_PPC64_TLS,
1228          0,                     /* rightshift */
1229          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1230          32,                    /* bitsize */
1231          FALSE,                 /* pc_relative */
1232          0,                     /* bitpos */
1233          complain_overflow_dont, /* complain_on_overflow */
1234          bfd_elf_generic_reloc, /* special_function */
1235          "R_PPC64_TLS",         /* name */
1236          FALSE,                 /* partial_inplace */
1237          0,                     /* src_mask */
1238          0,                     /* dst_mask */
1239          FALSE),                /* pcrel_offset */
1240
1241   /* Computes the load module index of the load module that contains the
1242      definition of its TLS sym.  */
1243   HOWTO (R_PPC64_DTPMOD64,
1244          0,                     /* rightshift */
1245          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1246          64,                    /* bitsize */
1247          FALSE,                 /* pc_relative */
1248          0,                     /* bitpos */
1249          complain_overflow_dont, /* complain_on_overflow */
1250          ppc64_elf_unhandled_reloc, /* special_function */
1251          "R_PPC64_DTPMOD64",    /* name */
1252          FALSE,                 /* partial_inplace */
1253          0,                     /* src_mask */
1254          ONES (64),             /* dst_mask */
1255          FALSE),                /* pcrel_offset */
1256
1257   /* Computes a dtv-relative displacement, the difference between the value
1258      of sym+add and the base address of the thread-local storage block that
1259      contains the definition of sym, minus 0x8000.  */
1260   HOWTO (R_PPC64_DTPREL64,
1261          0,                     /* rightshift */
1262          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1263          64,                    /* bitsize */
1264          FALSE,                 /* pc_relative */
1265          0,                     /* bitpos */
1266          complain_overflow_dont, /* complain_on_overflow */
1267          ppc64_elf_unhandled_reloc, /* special_function */
1268          "R_PPC64_DTPREL64",    /* name */
1269          FALSE,                 /* partial_inplace */
1270          0,                     /* src_mask */
1271          ONES (64),             /* dst_mask */
1272          FALSE),                /* pcrel_offset */
1273
1274   /* A 16 bit dtprel reloc.  */
1275   HOWTO (R_PPC64_DTPREL16,
1276          0,                     /* rightshift */
1277          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1278          16,                    /* bitsize */
1279          FALSE,                 /* pc_relative */
1280          0,                     /* bitpos */
1281          complain_overflow_signed, /* complain_on_overflow */
1282          ppc64_elf_unhandled_reloc, /* special_function */
1283          "R_PPC64_DTPREL16",    /* name */
1284          FALSE,                 /* partial_inplace */
1285          0,                     /* src_mask */
1286          0xffff,                /* dst_mask */
1287          FALSE),                /* pcrel_offset */
1288
1289   /* Like DTPREL16, but no overflow.  */
1290   HOWTO (R_PPC64_DTPREL16_LO,
1291          0,                     /* rightshift */
1292          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1293          16,                    /* bitsize */
1294          FALSE,                 /* pc_relative */
1295          0,                     /* bitpos */
1296          complain_overflow_dont, /* complain_on_overflow */
1297          ppc64_elf_unhandled_reloc, /* special_function */
1298          "R_PPC64_DTPREL16_LO", /* name */
1299          FALSE,                 /* partial_inplace */
1300          0,                     /* src_mask */
1301          0xffff,                /* dst_mask */
1302          FALSE),                /* pcrel_offset */
1303
1304   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1305   HOWTO (R_PPC64_DTPREL16_HI,
1306          16,                    /* rightshift */
1307          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1308          16,                    /* bitsize */
1309          FALSE,                 /* pc_relative */
1310          0,                     /* bitpos */
1311          complain_overflow_dont, /* complain_on_overflow */
1312          ppc64_elf_unhandled_reloc, /* special_function */
1313          "R_PPC64_DTPREL16_HI", /* name */
1314          FALSE,                 /* partial_inplace */
1315          0,                     /* src_mask */
1316          0xffff,                /* dst_mask */
1317          FALSE),                /* pcrel_offset */
1318
1319   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1320   HOWTO (R_PPC64_DTPREL16_HA,
1321          16,                    /* rightshift */
1322          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1323          16,                    /* bitsize */
1324          FALSE,                 /* pc_relative */
1325          0,                     /* bitpos */
1326          complain_overflow_dont, /* complain_on_overflow */
1327          ppc64_elf_unhandled_reloc, /* special_function */
1328          "R_PPC64_DTPREL16_HA", /* name */
1329          FALSE,                 /* partial_inplace */
1330          0,                     /* src_mask */
1331          0xffff,                /* dst_mask */
1332          FALSE),                /* pcrel_offset */
1333
1334   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1335   HOWTO (R_PPC64_DTPREL16_HIGHER,
1336          32,                    /* rightshift */
1337          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1338          16,                    /* bitsize */
1339          FALSE,                 /* pc_relative */
1340          0,                     /* bitpos */
1341          complain_overflow_dont, /* complain_on_overflow */
1342          ppc64_elf_unhandled_reloc, /* special_function */
1343          "R_PPC64_DTPREL16_HIGHER", /* name */
1344          FALSE,                 /* partial_inplace */
1345          0,                     /* src_mask */
1346          0xffff,                /* dst_mask */
1347          FALSE),                /* pcrel_offset */
1348
1349   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1350   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1351          32,                    /* rightshift */
1352          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1353          16,                    /* bitsize */
1354          FALSE,                 /* pc_relative */
1355          0,                     /* bitpos */
1356          complain_overflow_dont, /* complain_on_overflow */
1357          ppc64_elf_unhandled_reloc, /* special_function */
1358          "R_PPC64_DTPREL16_HIGHERA", /* name */
1359          FALSE,                 /* partial_inplace */
1360          0,                     /* src_mask */
1361          0xffff,                /* dst_mask */
1362          FALSE),                /* pcrel_offset */
1363
1364   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1365   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1366          48,                    /* rightshift */
1367          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1368          16,                    /* bitsize */
1369          FALSE,                 /* pc_relative */
1370          0,                     /* bitpos */
1371          complain_overflow_dont, /* complain_on_overflow */
1372          ppc64_elf_unhandled_reloc, /* special_function */
1373          "R_PPC64_DTPREL16_HIGHEST", /* name */
1374          FALSE,                 /* partial_inplace */
1375          0,                     /* src_mask */
1376          0xffff,                /* dst_mask */
1377          FALSE),                /* pcrel_offset */
1378
1379   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1380   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1381          48,                    /* rightshift */
1382          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1383          16,                    /* bitsize */
1384          FALSE,                 /* pc_relative */
1385          0,                     /* bitpos */
1386          complain_overflow_dont, /* complain_on_overflow */
1387          ppc64_elf_unhandled_reloc, /* special_function */
1388          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1389          FALSE,                 /* partial_inplace */
1390          0,                     /* src_mask */
1391          0xffff,                /* dst_mask */
1392          FALSE),                /* pcrel_offset */
1393
1394   /* Like DTPREL16, but for insns with a DS field.  */
1395   HOWTO (R_PPC64_DTPREL16_DS,
1396          0,                     /* rightshift */
1397          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1398          16,                    /* bitsize */
1399          FALSE,                 /* pc_relative */
1400          0,                     /* bitpos */
1401          complain_overflow_signed, /* complain_on_overflow */
1402          ppc64_elf_unhandled_reloc, /* special_function */
1403          "R_PPC64_DTPREL16_DS", /* name */
1404          FALSE,                 /* partial_inplace */
1405          0,                     /* src_mask */
1406          0xfffc,                /* dst_mask */
1407          FALSE),                /* pcrel_offset */
1408
1409   /* Like DTPREL16_DS, but no overflow.  */
1410   HOWTO (R_PPC64_DTPREL16_LO_DS,
1411          0,                     /* rightshift */
1412          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1413          16,                    /* bitsize */
1414          FALSE,                 /* pc_relative */
1415          0,                     /* bitpos */
1416          complain_overflow_dont, /* complain_on_overflow */
1417          ppc64_elf_unhandled_reloc, /* special_function */
1418          "R_PPC64_DTPREL16_LO_DS", /* name */
1419          FALSE,                 /* partial_inplace */
1420          0,                     /* src_mask */
1421          0xfffc,                /* dst_mask */
1422          FALSE),                /* pcrel_offset */
1423
1424   /* Computes a tp-relative displacement, the difference between the value of
1425      sym+add and the value of the thread pointer (r13).  */
1426   HOWTO (R_PPC64_TPREL64,
1427          0,                     /* rightshift */
1428          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1429          64,                    /* bitsize */
1430          FALSE,                 /* pc_relative */
1431          0,                     /* bitpos */
1432          complain_overflow_dont, /* complain_on_overflow */
1433          ppc64_elf_unhandled_reloc, /* special_function */
1434          "R_PPC64_TPREL64",     /* name */
1435          FALSE,                 /* partial_inplace */
1436          0,                     /* src_mask */
1437          ONES (64),             /* dst_mask */
1438          FALSE),                /* pcrel_offset */
1439
1440   /* A 16 bit tprel reloc.  */
1441   HOWTO (R_PPC64_TPREL16,
1442          0,                     /* rightshift */
1443          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1444          16,                    /* bitsize */
1445          FALSE,                 /* pc_relative */
1446          0,                     /* bitpos */
1447          complain_overflow_signed, /* complain_on_overflow */
1448          ppc64_elf_unhandled_reloc, /* special_function */
1449          "R_PPC64_TPREL16",     /* name */
1450          FALSE,                 /* partial_inplace */
1451          0,                     /* src_mask */
1452          0xffff,                /* dst_mask */
1453          FALSE),                /* pcrel_offset */
1454
1455   /* Like TPREL16, but no overflow.  */
1456   HOWTO (R_PPC64_TPREL16_LO,
1457          0,                     /* rightshift */
1458          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1459          16,                    /* bitsize */
1460          FALSE,                 /* pc_relative */
1461          0,                     /* bitpos */
1462          complain_overflow_dont, /* complain_on_overflow */
1463          ppc64_elf_unhandled_reloc, /* special_function */
1464          "R_PPC64_TPREL16_LO",  /* name */
1465          FALSE,                 /* partial_inplace */
1466          0,                     /* src_mask */
1467          0xffff,                /* dst_mask */
1468          FALSE),                /* pcrel_offset */
1469
1470   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1471   HOWTO (R_PPC64_TPREL16_HI,
1472          16,                    /* rightshift */
1473          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1474          16,                    /* bitsize */
1475          FALSE,                 /* pc_relative */
1476          0,                     /* bitpos */
1477          complain_overflow_dont, /* complain_on_overflow */
1478          ppc64_elf_unhandled_reloc, /* special_function */
1479          "R_PPC64_TPREL16_HI",  /* name */
1480          FALSE,                 /* partial_inplace */
1481          0,                     /* src_mask */
1482          0xffff,                /* dst_mask */
1483          FALSE),                /* pcrel_offset */
1484
1485   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1486   HOWTO (R_PPC64_TPREL16_HA,
1487          16,                    /* rightshift */
1488          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1489          16,                    /* bitsize */
1490          FALSE,                 /* pc_relative */
1491          0,                     /* bitpos */
1492          complain_overflow_dont, /* complain_on_overflow */
1493          ppc64_elf_unhandled_reloc, /* special_function */
1494          "R_PPC64_TPREL16_HA",  /* name */
1495          FALSE,                 /* partial_inplace */
1496          0,                     /* src_mask */
1497          0xffff,                /* dst_mask */
1498          FALSE),                /* pcrel_offset */
1499
1500   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1501   HOWTO (R_PPC64_TPREL16_HIGHER,
1502          32,                    /* rightshift */
1503          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1504          16,                    /* bitsize */
1505          FALSE,                 /* pc_relative */
1506          0,                     /* bitpos */
1507          complain_overflow_dont, /* complain_on_overflow */
1508          ppc64_elf_unhandled_reloc, /* special_function */
1509          "R_PPC64_TPREL16_HIGHER",      /* name */
1510          FALSE,                 /* partial_inplace */
1511          0,                     /* src_mask */
1512          0xffff,                /* dst_mask */
1513          FALSE),                /* pcrel_offset */
1514
1515   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1516   HOWTO (R_PPC64_TPREL16_HIGHERA,
1517          32,                    /* rightshift */
1518          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1519          16,                    /* bitsize */
1520          FALSE,                 /* pc_relative */
1521          0,                     /* bitpos */
1522          complain_overflow_dont, /* complain_on_overflow */
1523          ppc64_elf_unhandled_reloc, /* special_function */
1524          "R_PPC64_TPREL16_HIGHERA", /* name */
1525          FALSE,                 /* partial_inplace */
1526          0,                     /* src_mask */
1527          0xffff,                /* dst_mask */
1528          FALSE),                /* pcrel_offset */
1529
1530   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1531   HOWTO (R_PPC64_TPREL16_HIGHEST,
1532          48,                    /* rightshift */
1533          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1534          16,                    /* bitsize */
1535          FALSE,                 /* pc_relative */
1536          0,                     /* bitpos */
1537          complain_overflow_dont, /* complain_on_overflow */
1538          ppc64_elf_unhandled_reloc, /* special_function */
1539          "R_PPC64_TPREL16_HIGHEST", /* name */
1540          FALSE,                 /* partial_inplace */
1541          0,                     /* src_mask */
1542          0xffff,                /* dst_mask */
1543          FALSE),                /* pcrel_offset */
1544
1545   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1546   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1547          48,                    /* rightshift */
1548          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1549          16,                    /* bitsize */
1550          FALSE,                 /* pc_relative */
1551          0,                     /* bitpos */
1552          complain_overflow_dont, /* complain_on_overflow */
1553          ppc64_elf_unhandled_reloc, /* special_function */
1554          "R_PPC64_TPREL16_HIGHESTA", /* name */
1555          FALSE,                 /* partial_inplace */
1556          0,                     /* src_mask */
1557          0xffff,                /* dst_mask */
1558          FALSE),                /* pcrel_offset */
1559
1560   /* Like TPREL16, but for insns with a DS field.  */
1561   HOWTO (R_PPC64_TPREL16_DS,
1562          0,                     /* rightshift */
1563          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1564          16,                    /* bitsize */
1565          FALSE,                 /* pc_relative */
1566          0,                     /* bitpos */
1567          complain_overflow_signed, /* complain_on_overflow */
1568          ppc64_elf_unhandled_reloc, /* special_function */
1569          "R_PPC64_TPREL16_DS",  /* name */
1570          FALSE,                 /* partial_inplace */
1571          0,                     /* src_mask */
1572          0xfffc,                /* dst_mask */
1573          FALSE),                /* pcrel_offset */
1574
1575   /* Like TPREL16_DS, but no overflow.  */
1576   HOWTO (R_PPC64_TPREL16_LO_DS,
1577          0,                     /* rightshift */
1578          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1579          16,                    /* bitsize */
1580          FALSE,                 /* pc_relative */
1581          0,                     /* bitpos */
1582          complain_overflow_dont, /* complain_on_overflow */
1583          ppc64_elf_unhandled_reloc, /* special_function */
1584          "R_PPC64_TPREL16_LO_DS", /* name */
1585          FALSE,                 /* partial_inplace */
1586          0,                     /* src_mask */
1587          0xfffc,                /* dst_mask */
1588          FALSE),                /* pcrel_offset */
1589
1590   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1591      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1592      to the first entry relative to the TOC base (r2).  */
1593   HOWTO (R_PPC64_GOT_TLSGD16,
1594          0,                     /* rightshift */
1595          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1596          16,                    /* bitsize */
1597          FALSE,                 /* pc_relative */
1598          0,                     /* bitpos */
1599          complain_overflow_signed, /* complain_on_overflow */
1600          ppc64_elf_unhandled_reloc, /* special_function */
1601          "R_PPC64_GOT_TLSGD16", /* name */
1602          FALSE,                 /* partial_inplace */
1603          0,                     /* src_mask */
1604          0xffff,                /* dst_mask */
1605          FALSE),                /* pcrel_offset */
1606
1607   /* Like GOT_TLSGD16, but no overflow.  */
1608   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1609          0,                     /* rightshift */
1610          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1611          16,                    /* bitsize */
1612          FALSE,                 /* pc_relative */
1613          0,                     /* bitpos */
1614          complain_overflow_dont, /* complain_on_overflow */
1615          ppc64_elf_unhandled_reloc, /* special_function */
1616          "R_PPC64_GOT_TLSGD16_LO", /* name */
1617          FALSE,                 /* partial_inplace */
1618          0,                     /* src_mask */
1619          0xffff,                /* dst_mask */
1620          FALSE),                /* pcrel_offset */
1621
1622   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1623   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1624          16,                    /* rightshift */
1625          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1626          16,                    /* bitsize */
1627          FALSE,                 /* pc_relative */
1628          0,                     /* bitpos */
1629          complain_overflow_dont, /* complain_on_overflow */
1630          ppc64_elf_unhandled_reloc, /* special_function */
1631          "R_PPC64_GOT_TLSGD16_HI", /* name */
1632          FALSE,                 /* partial_inplace */
1633          0,                     /* src_mask */
1634          0xffff,                /* dst_mask */
1635          FALSE),                /* pcrel_offset */
1636
1637   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1638   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1639          16,                    /* rightshift */
1640          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1641          16,                    /* bitsize */
1642          FALSE,                 /* pc_relative */
1643          0,                     /* bitpos */
1644          complain_overflow_dont, /* complain_on_overflow */
1645          ppc64_elf_unhandled_reloc, /* special_function */
1646          "R_PPC64_GOT_TLSGD16_HA", /* name */
1647          FALSE,                 /* partial_inplace */
1648          0,                     /* src_mask */
1649          0xffff,                /* dst_mask */
1650          FALSE),                /* pcrel_offset */
1651
1652   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1653      with values (sym+add)@dtpmod and zero, and computes the offset to the
1654      first entry relative to the TOC base (r2).  */
1655   HOWTO (R_PPC64_GOT_TLSLD16,
1656          0,                     /* rightshift */
1657          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1658          16,                    /* bitsize */
1659          FALSE,                 /* pc_relative */
1660          0,                     /* bitpos */
1661          complain_overflow_signed, /* complain_on_overflow */
1662          ppc64_elf_unhandled_reloc, /* special_function */
1663          "R_PPC64_GOT_TLSLD16", /* name */
1664          FALSE,                 /* partial_inplace */
1665          0,                     /* src_mask */
1666          0xffff,                /* dst_mask */
1667          FALSE),                /* pcrel_offset */
1668
1669   /* Like GOT_TLSLD16, but no overflow.  */
1670   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1671          0,                     /* rightshift */
1672          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1673          16,                    /* bitsize */
1674          FALSE,                 /* pc_relative */
1675          0,                     /* bitpos */
1676          complain_overflow_dont, /* complain_on_overflow */
1677          ppc64_elf_unhandled_reloc, /* special_function */
1678          "R_PPC64_GOT_TLSLD16_LO", /* name */
1679          FALSE,                 /* partial_inplace */
1680          0,                     /* src_mask */
1681          0xffff,                /* dst_mask */
1682          FALSE),                /* pcrel_offset */
1683
1684   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1685   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1686          16,                    /* rightshift */
1687          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1688          16,                    /* bitsize */
1689          FALSE,                 /* pc_relative */
1690          0,                     /* bitpos */
1691          complain_overflow_dont, /* complain_on_overflow */
1692          ppc64_elf_unhandled_reloc, /* special_function */
1693          "R_PPC64_GOT_TLSLD16_HI", /* name */
1694          FALSE,                 /* partial_inplace */
1695          0,                     /* src_mask */
1696          0xffff,                /* dst_mask */
1697          FALSE),                /* pcrel_offset */
1698
1699   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1700   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1701          16,                    /* rightshift */
1702          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1703          16,                    /* bitsize */
1704          FALSE,                 /* pc_relative */
1705          0,                     /* bitpos */
1706          complain_overflow_dont, /* complain_on_overflow */
1707          ppc64_elf_unhandled_reloc, /* special_function */
1708          "R_PPC64_GOT_TLSLD16_HA", /* name */
1709          FALSE,                 /* partial_inplace */
1710          0,                     /* src_mask */
1711          0xffff,                /* dst_mask */
1712          FALSE),                /* pcrel_offset */
1713
1714   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1715      the offset to the entry relative to the TOC base (r2).  */
1716   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1717          0,                     /* rightshift */
1718          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1719          16,                    /* bitsize */
1720          FALSE,                 /* pc_relative */
1721          0,                     /* bitpos */
1722          complain_overflow_signed, /* complain_on_overflow */
1723          ppc64_elf_unhandled_reloc, /* special_function */
1724          "R_PPC64_GOT_DTPREL16_DS", /* name */
1725          FALSE,                 /* partial_inplace */
1726          0,                     /* src_mask */
1727          0xfffc,                /* dst_mask */
1728          FALSE),                /* pcrel_offset */
1729
1730   /* Like GOT_DTPREL16_DS, but no overflow.  */
1731   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1732          0,                     /* rightshift */
1733          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1734          16,                    /* bitsize */
1735          FALSE,                 /* pc_relative */
1736          0,                     /* bitpos */
1737          complain_overflow_dont, /* complain_on_overflow */
1738          ppc64_elf_unhandled_reloc, /* special_function */
1739          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1740          FALSE,                 /* partial_inplace */
1741          0,                     /* src_mask */
1742          0xfffc,                /* dst_mask */
1743          FALSE),                /* pcrel_offset */
1744
1745   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1746   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1747          16,                    /* rightshift */
1748          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1749          16,                    /* bitsize */
1750          FALSE,                 /* pc_relative */
1751          0,                     /* bitpos */
1752          complain_overflow_dont, /* complain_on_overflow */
1753          ppc64_elf_unhandled_reloc, /* special_function */
1754          "R_PPC64_GOT_DTPREL16_HI", /* name */
1755          FALSE,                 /* partial_inplace */
1756          0,                     /* src_mask */
1757          0xffff,                /* dst_mask */
1758          FALSE),                /* pcrel_offset */
1759
1760   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1761   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1762          16,                    /* rightshift */
1763          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1764          16,                    /* bitsize */
1765          FALSE,                 /* pc_relative */
1766          0,                     /* bitpos */
1767          complain_overflow_dont, /* complain_on_overflow */
1768          ppc64_elf_unhandled_reloc, /* special_function */
1769          "R_PPC64_GOT_DTPREL16_HA", /* name */
1770          FALSE,                 /* partial_inplace */
1771          0,                     /* src_mask */
1772          0xffff,                /* dst_mask */
1773          FALSE),                /* pcrel_offset */
1774
1775   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1776      offset to the entry relative to the TOC base (r2).  */
1777   HOWTO (R_PPC64_GOT_TPREL16_DS,
1778          0,                     /* rightshift */
1779          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1780          16,                    /* bitsize */
1781          FALSE,                 /* pc_relative */
1782          0,                     /* bitpos */
1783          complain_overflow_signed, /* complain_on_overflow */
1784          ppc64_elf_unhandled_reloc, /* special_function */
1785          "R_PPC64_GOT_TPREL16_DS", /* name */
1786          FALSE,                 /* partial_inplace */
1787          0,                     /* src_mask */
1788          0xfffc,                /* dst_mask */
1789          FALSE),                /* pcrel_offset */
1790
1791   /* Like GOT_TPREL16_DS, but no overflow.  */
1792   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1793          0,                     /* rightshift */
1794          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1795          16,                    /* bitsize */
1796          FALSE,                 /* pc_relative */
1797          0,                     /* bitpos */
1798          complain_overflow_dont, /* complain_on_overflow */
1799          ppc64_elf_unhandled_reloc, /* special_function */
1800          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1801          FALSE,                 /* partial_inplace */
1802          0,                     /* src_mask */
1803          0xfffc,                /* dst_mask */
1804          FALSE),                /* pcrel_offset */
1805
1806   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1807   HOWTO (R_PPC64_GOT_TPREL16_HI,
1808          16,                    /* rightshift */
1809          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1810          16,                    /* bitsize */
1811          FALSE,                 /* pc_relative */
1812          0,                     /* bitpos */
1813          complain_overflow_dont, /* complain_on_overflow */
1814          ppc64_elf_unhandled_reloc, /* special_function */
1815          "R_PPC64_GOT_TPREL16_HI", /* name */
1816          FALSE,                 /* partial_inplace */
1817          0,                     /* src_mask */
1818          0xffff,                /* dst_mask */
1819          FALSE),                /* pcrel_offset */
1820
1821   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1822   HOWTO (R_PPC64_GOT_TPREL16_HA,
1823          16,                    /* rightshift */
1824          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1825          16,                    /* bitsize */
1826          FALSE,                 /* pc_relative */
1827          0,                     /* bitpos */
1828          complain_overflow_dont, /* complain_on_overflow */
1829          ppc64_elf_unhandled_reloc, /* special_function */
1830          "R_PPC64_GOT_TPREL16_HA", /* name */
1831          FALSE,                 /* partial_inplace */
1832          0,                     /* src_mask */
1833          0xffff,                /* dst_mask */
1834          FALSE),                /* pcrel_offset */
1835
1836   /* GNU extension to record C++ vtable hierarchy.  */
1837   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1838          0,                     /* rightshift */
1839          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1840          0,                     /* bitsize */
1841          FALSE,                 /* pc_relative */
1842          0,                     /* bitpos */
1843          complain_overflow_dont, /* complain_on_overflow */
1844          NULL,                  /* special_function */
1845          "R_PPC64_GNU_VTINHERIT", /* name */
1846          FALSE,                 /* partial_inplace */
1847          0,                     /* src_mask */
1848          0,                     /* dst_mask */
1849          FALSE),                /* pcrel_offset */
1850
1851   /* GNU extension to record C++ vtable member usage.  */
1852   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
1853          0,                     /* rightshift */
1854          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1855          0,                     /* bitsize */
1856          FALSE,                 /* pc_relative */
1857          0,                     /* bitpos */
1858          complain_overflow_dont, /* complain_on_overflow */
1859          NULL,                  /* special_function */
1860          "R_PPC64_GNU_VTENTRY", /* name */
1861          FALSE,                 /* partial_inplace */
1862          0,                     /* src_mask */
1863          0,                     /* dst_mask */
1864          FALSE),                /* pcrel_offset */
1865 };
1866
1867 \f
1868 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1869    be done.  */
1870
1871 static void
1872 ppc_howto_init (void)
1873 {
1874   unsigned int i, type;
1875
1876   for (i = 0;
1877        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1878        i++)
1879     {
1880       type = ppc64_elf_howto_raw[i].type;
1881       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1882                           / sizeof (ppc64_elf_howto_table[0])));
1883       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1884     }
1885 }
1886
1887 static reloc_howto_type *
1888 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1889                              bfd_reloc_code_real_type code)
1890 {
1891   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1892
1893   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1894     /* Initialize howto table if needed.  */
1895     ppc_howto_init ();
1896
1897   switch (code)
1898     {
1899     default:
1900       return NULL;
1901
1902     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
1903       break;
1904     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
1905       break;
1906     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
1907       break;
1908     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
1909       break;
1910     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
1911       break;
1912     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
1913       break;
1914     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
1915       break;
1916     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
1917       break;
1918     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
1919       break;
1920     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
1921       break;
1922     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
1923       break;
1924     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
1925       break;
1926     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
1927       break;
1928     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
1929       break;
1930     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
1931       break;
1932     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
1933       break;
1934     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
1935       break;
1936     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
1937       break;
1938     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
1939       break;
1940     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
1941       break;
1942     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
1943       break;
1944     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
1945       break;
1946     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
1947       break;
1948     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
1949       break;
1950     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
1951       break;
1952     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
1953       break;
1954     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
1955       break;
1956     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
1957       break;
1958     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
1959       break;
1960     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
1961       break;
1962     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
1963       break;
1964     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
1965       break;
1966     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
1967       break;
1968     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
1969       break;
1970     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
1971       break;
1972     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
1973       break;
1974     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
1975       break;
1976     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
1977       break;
1978     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
1979       break;
1980     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
1981       break;
1982     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
1983       break;
1984     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
1985       break;
1986     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
1987       break;
1988     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
1989       break;
1990     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
1991       break;
1992     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
1993       break;
1994     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
1995       break;
1996     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
1997       break;
1998     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
1999       break;
2000     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2001       break;
2002     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2003       break;
2004     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2005       break;
2006     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2007       break;
2008     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2009       break;
2010     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2011       break;
2012     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2013       break;
2014     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2015       break;
2016     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2017       break;
2018     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2019       break;
2020     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2021       break;
2022     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2023       break;
2024     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2025       break;
2026     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2027       break;
2028     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2029       break;
2030     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2031       break;
2032     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2033       break;
2034     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2035       break;
2036     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2037       break;
2038     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2039       break;
2040     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2041       break;
2042     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2043       break;
2044     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2045       break;
2046     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2047       break;
2048     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2049       break;
2050     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2051       break;
2052     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2053       break;
2054     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2055       break;
2056     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2057       break;
2058     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2059       break;
2060     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2061       break;
2062     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2063       break;
2064     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2065       break;
2066     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2067       break;
2068     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2069       break;
2070     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2071       break;
2072     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2073       break;
2074     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2075       break;
2076     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2077       break;
2078     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2079       break;
2080     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2081       break;
2082     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2083       break;
2084     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2085       break;
2086     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2087       break;
2088     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2089       break;
2090     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2091       break;
2092     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2093       break;
2094     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2095       break;
2096     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2097       break;
2098     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2099       break;
2100     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2101       break;
2102     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2103       break;
2104     }
2105
2106   return ppc64_elf_howto_table[r];
2107 };
2108
2109 /* Set the howto pointer for a PowerPC ELF reloc.  */
2110
2111 static void
2112 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2113                          Elf_Internal_Rela *dst)
2114 {
2115   unsigned int type;
2116
2117   /* Initialize howto table if needed.  */
2118   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2119     ppc_howto_init ();
2120
2121   type = ELF64_R_TYPE (dst->r_info);
2122   if (type >= (sizeof (ppc64_elf_howto_table)
2123                / sizeof (ppc64_elf_howto_table[0])))
2124     {
2125       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2126                              abfd, (int) type);
2127       type = R_PPC64_NONE;
2128     }
2129   cache_ptr->howto = ppc64_elf_howto_table[type];
2130 }
2131
2132 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2133
2134 static bfd_reloc_status_type
2135 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2136                     void *data, asection *input_section,
2137                     bfd *output_bfd, char **error_message)
2138 {
2139   /* If this is a relocatable link (output_bfd test tells us), just
2140      call the generic function.  Any adjustment will be done at final
2141      link time.  */
2142   if (output_bfd != NULL)
2143     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2144                                   input_section, output_bfd, error_message);
2145
2146   /* Adjust the addend for sign extension of the low 16 bits.
2147      We won't actually be using the low 16 bits, so trashing them
2148      doesn't matter.  */
2149   reloc_entry->addend += 0x8000;
2150   return bfd_reloc_continue;
2151 }
2152
2153 static bfd_reloc_status_type
2154 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2155                         void *data, asection *input_section,
2156                         bfd *output_bfd, char **error_message)
2157 {
2158   if (output_bfd != NULL)
2159     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2160                                   input_section, output_bfd, error_message);
2161
2162   if (strcmp (symbol->section->name, ".opd") == 0
2163       && (symbol->section->owner->flags & DYNAMIC) == 0)
2164     {
2165       bfd_vma dest = opd_entry_value (symbol->section,
2166                                       symbol->value + reloc_entry->addend,
2167                                       NULL, NULL);
2168       if (dest != (bfd_vma) -1)
2169         reloc_entry->addend = dest - (symbol->value
2170                                       + symbol->section->output_section->vma
2171                                       + symbol->section->output_offset);
2172     }
2173   return bfd_reloc_continue;
2174 }
2175
2176 static bfd_reloc_status_type
2177 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2178                          void *data, asection *input_section,
2179                          bfd *output_bfd, char **error_message)
2180 {
2181   long insn;
2182   enum elf_ppc64_reloc_type r_type;
2183   bfd_size_type octets;
2184   /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
2185   bfd_boolean is_power4 = FALSE;
2186
2187   /* If this is a relocatable link (output_bfd test tells us), just
2188      call the generic function.  Any adjustment will be done at final
2189      link time.  */
2190   if (output_bfd != NULL)
2191     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2192                                   input_section, output_bfd, error_message);
2193
2194   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2195   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2196   insn &= ~(0x01 << 21);
2197   r_type = reloc_entry->howto->type;
2198   if (r_type == R_PPC64_ADDR14_BRTAKEN
2199       || r_type == R_PPC64_REL14_BRTAKEN)
2200     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2201
2202   if (is_power4)
2203     {
2204       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2205          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2206          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2207       if ((insn & (0x14 << 21)) == (0x04 << 21))
2208         insn |= 0x02 << 21;
2209       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2210         insn |= 0x08 << 21;
2211       else
2212         goto out;
2213     }
2214   else
2215     {
2216       bfd_vma target = 0;
2217       bfd_vma from;
2218
2219       if (!bfd_is_com_section (symbol->section))
2220         target = symbol->value;
2221       target += symbol->section->output_section->vma;
2222       target += symbol->section->output_offset;
2223       target += reloc_entry->addend;
2224
2225       from = (reloc_entry->address
2226               + input_section->output_offset
2227               + input_section->output_section->vma);
2228
2229       /* Invert 'y' bit if not the default.  */
2230       if ((bfd_signed_vma) (target - from) < 0)
2231         insn ^= 0x01 << 21;
2232     }
2233   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2234  out:
2235   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2236                                  input_section, output_bfd, error_message);
2237 }
2238
2239 static bfd_reloc_status_type
2240 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2241                          void *data, asection *input_section,
2242                          bfd *output_bfd, char **error_message)
2243 {
2244   /* If this is a relocatable link (output_bfd test tells us), just
2245      call the generic function.  Any adjustment will be done at final
2246      link time.  */
2247   if (output_bfd != NULL)
2248     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2249                                   input_section, output_bfd, error_message);
2250
2251   /* Subtract the symbol section base address.  */
2252   reloc_entry->addend -= symbol->section->output_section->vma;
2253   return bfd_reloc_continue;
2254 }
2255
2256 static bfd_reloc_status_type
2257 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2258                             void *data, asection *input_section,
2259                             bfd *output_bfd, char **error_message)
2260 {
2261   /* If this is a relocatable link (output_bfd test tells us), just
2262      call the generic function.  Any adjustment will be done at final
2263      link time.  */
2264   if (output_bfd != NULL)
2265     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2266                                   input_section, output_bfd, error_message);
2267
2268   /* Subtract the symbol section base address.  */
2269   reloc_entry->addend -= symbol->section->output_section->vma;
2270
2271   /* Adjust the addend for sign extension of the low 16 bits.  */
2272   reloc_entry->addend += 0x8000;
2273   return bfd_reloc_continue;
2274 }
2275
2276 static bfd_reloc_status_type
2277 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2278                      void *data, asection *input_section,
2279                      bfd *output_bfd, char **error_message)
2280 {
2281   bfd_vma TOCstart;
2282
2283   /* If this is a relocatable link (output_bfd test tells us), just
2284      call the generic function.  Any adjustment will be done at final
2285      link time.  */
2286   if (output_bfd != NULL)
2287     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2288                                   input_section, output_bfd, error_message);
2289
2290   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2291   if (TOCstart == 0)
2292     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2293
2294   /* Subtract the TOC base address.  */
2295   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2296   return bfd_reloc_continue;
2297 }
2298
2299 static bfd_reloc_status_type
2300 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2301                         void *data, asection *input_section,
2302                         bfd *output_bfd, char **error_message)
2303 {
2304   bfd_vma TOCstart;
2305
2306   /* If this is a relocatable link (output_bfd test tells us), just
2307      call the generic function.  Any adjustment will be done at final
2308      link time.  */
2309   if (output_bfd != NULL)
2310     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2311                                   input_section, output_bfd, error_message);
2312
2313   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2314   if (TOCstart == 0)
2315     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2316
2317   /* Subtract the TOC base address.  */
2318   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2319
2320   /* Adjust the addend for sign extension of the low 16 bits.  */
2321   reloc_entry->addend += 0x8000;
2322   return bfd_reloc_continue;
2323 }
2324
2325 static bfd_reloc_status_type
2326 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2327                        void *data, asection *input_section,
2328                        bfd *output_bfd, char **error_message)
2329 {
2330   bfd_vma TOCstart;
2331   bfd_size_type octets;
2332
2333   /* If this is a relocatable link (output_bfd test tells us), just
2334      call the generic function.  Any adjustment will be done at final
2335      link time.  */
2336   if (output_bfd != NULL)
2337     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2338                                   input_section, output_bfd, error_message);
2339
2340   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2341   if (TOCstart == 0)
2342     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2343
2344   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2345   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2346   return bfd_reloc_ok;
2347 }
2348
2349 static bfd_reloc_status_type
2350 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2351                            void *data, asection *input_section,
2352                            bfd *output_bfd, char **error_message)
2353 {
2354   /* If this is a relocatable link (output_bfd test tells us), just
2355      call the generic function.  Any adjustment will be done at final
2356      link time.  */
2357   if (output_bfd != NULL)
2358     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2359                                   input_section, output_bfd, error_message);
2360
2361   if (error_message != NULL)
2362     {
2363       static char buf[60];
2364       sprintf (buf, "generic linker can't handle %s",
2365                reloc_entry->howto->name);
2366       *error_message = buf;
2367     }
2368   return bfd_reloc_dangerous;
2369 }
2370
2371 struct ppc64_elf_obj_tdata
2372 {
2373   struct elf_obj_tdata elf;
2374
2375   /* Shortcuts to dynamic linker sections.  */
2376   asection *got;
2377   asection *relgot;
2378
2379   union {
2380     /* Used during garbage collection.  We attach global symbols defined
2381        on removed .opd entries to this section so that the sym is removed.  */
2382     asection *deleted_section;
2383
2384     /* Used when adding symbols.  */
2385     bfd_boolean has_dotsym;
2386   } u;
2387
2388   /* TLS local dynamic got entry handling.  Suppose for multiple GOT
2389      sections means we potentially need one of these for each input bfd.  */
2390   union {
2391     bfd_signed_vma refcount;
2392     bfd_vma offset;
2393   } tlsld_got;
2394
2395   /* A copy of relocs before they are modified for --emit-relocs.  */
2396   Elf_Internal_Rela *opd_relocs;
2397 };
2398
2399 #define ppc64_elf_tdata(bfd) \
2400   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2401
2402 #define ppc64_tlsld_got(bfd) \
2403   (&ppc64_elf_tdata (bfd)->tlsld_got)
2404
2405 /* Override the generic function because we store some extras.  */
2406
2407 static bfd_boolean
2408 ppc64_elf_mkobject (bfd *abfd)
2409 {
2410   bfd_size_type amt = sizeof (struct ppc64_elf_obj_tdata);
2411   abfd->tdata.any = bfd_zalloc (abfd, amt);
2412   if (abfd->tdata.any == NULL)
2413     return FALSE;
2414   return TRUE;
2415 }
2416
2417 /* Return 1 if target is one of ours.  */
2418
2419 static bfd_boolean
2420 is_ppc64_elf_target (const struct bfd_target *targ)
2421 {
2422   extern const bfd_target bfd_elf64_powerpc_vec;
2423   extern const bfd_target bfd_elf64_powerpcle_vec;
2424
2425   return targ == &bfd_elf64_powerpc_vec || targ == &bfd_elf64_powerpcle_vec;
2426 }
2427
2428 /* Fix bad default arch selected for a 64 bit input bfd when the
2429    default is 32 bit.  */
2430
2431 static bfd_boolean
2432 ppc64_elf_object_p (bfd *abfd)
2433 {
2434   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2435     {
2436       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2437
2438       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2439         {
2440           /* Relies on arch after 32 bit default being 64 bit default.  */
2441           abfd->arch_info = abfd->arch_info->next;
2442           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2443         }
2444     }
2445   return TRUE;
2446 }
2447
2448 /* Support for core dump NOTE sections.  */
2449
2450 static bfd_boolean
2451 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2452 {
2453   size_t offset, size;
2454
2455   if (note->descsz != 504)
2456     return FALSE;
2457
2458   /* pr_cursig */
2459   elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2460
2461   /* pr_pid */
2462   elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 32);
2463
2464   /* pr_reg */
2465   offset = 112;
2466   size = 384;
2467
2468   /* Make a ".reg/999" section.  */
2469   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2470                                           size, note->descpos + offset);
2471 }
2472
2473 static bfd_boolean
2474 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2475 {
2476   if (note->descsz != 136)
2477     return FALSE;
2478
2479   elf_tdata (abfd)->core_program
2480     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2481   elf_tdata (abfd)->core_command
2482     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2483
2484   return TRUE;
2485 }
2486
2487 /* Merge backend specific data from an object file to the output
2488    object file when linking.  */
2489
2490 static bfd_boolean
2491 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2492 {
2493   /* Check if we have the same endianess.  */
2494   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2495       && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
2496       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2497     {
2498       const char *msg;
2499
2500       if (bfd_big_endian (ibfd))
2501         msg = _("%B: compiled for a big endian system "
2502                 "and target is little endian");
2503       else
2504         msg = _("%B: compiled for a little endian system "
2505                 "and target is big endian");
2506
2507       (*_bfd_error_handler) (msg, ibfd);
2508
2509       bfd_set_error (bfd_error_wrong_format);
2510       return FALSE;
2511     }
2512
2513   return TRUE;
2514 }
2515
2516 /* Add extra PPC sections.  */
2517
2518 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2519 {
2520   { ".plt",     4,  0, SHT_NOBITS,   0 },
2521   { ".sbss",    5, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2522   { ".sdata",   6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2523   { ".toc",     4,  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2524   { ".toc1",    5,  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2525   { ".tocbss",  7,  0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2526   { NULL,       0,  0, 0,            0 }
2527 };
2528
2529 struct _ppc64_elf_section_data
2530 {
2531   struct bfd_elf_section_data elf;
2532
2533   /* An array with one entry for each opd function descriptor.  */
2534   union
2535   {
2536     /* Points to the function code section for local opd entries.  */
2537     asection **func_sec;
2538     /* After editing .opd, adjust references to opd local syms.  */
2539     long *adjust;
2540   } opd;
2541
2542   /* An array for toc sections, indexed by offset/8.
2543      Specifies the relocation symbol index used at a given toc offset.  */
2544   unsigned *t_symndx;
2545 };
2546
2547 #define ppc64_elf_section_data(sec) \
2548   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2549
2550 static bfd_boolean
2551 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2552 {
2553   struct _ppc64_elf_section_data *sdata;
2554   bfd_size_type amt = sizeof (*sdata);
2555
2556   sdata = bfd_zalloc (abfd, amt);
2557   if (sdata == NULL)
2558     return FALSE;
2559   sec->used_by_bfd = sdata;
2560
2561   return _bfd_elf_new_section_hook (abfd, sec);
2562 }
2563
2564 static void *
2565 get_opd_info (asection * sec)
2566 {
2567   if (sec != NULL
2568       && ppc64_elf_section_data (sec) != NULL
2569       && ppc64_elf_section_data (sec)->opd.adjust != NULL)
2570     return ppc64_elf_section_data (sec)->opd.adjust;
2571   return NULL;
2572 }
2573 \f
2574 /* Parameters for the qsort hook.  */
2575 static asection *synthetic_opd;
2576 static bfd_boolean synthetic_relocatable;
2577
2578 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
2579
2580 static int
2581 compare_symbols (const void *ap, const void *bp)
2582 {
2583   const asymbol *a = * (const asymbol **) ap;
2584   const asymbol *b = * (const asymbol **) bp;
2585
2586   /* Section symbols first.  */
2587   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2588     return -1;
2589   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2590     return 1;
2591
2592   /* then .opd symbols.  */
2593   if (a->section == synthetic_opd && b->section != synthetic_opd)
2594     return -1;
2595   if (a->section != synthetic_opd && b->section == synthetic_opd)
2596     return 1;
2597
2598   /* then other code symbols.  */
2599   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2600       == (SEC_CODE | SEC_ALLOC)
2601       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2602          != (SEC_CODE | SEC_ALLOC))
2603     return -1;
2604
2605   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2606       != (SEC_CODE | SEC_ALLOC)
2607       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2608          == (SEC_CODE | SEC_ALLOC))
2609     return 1;
2610
2611   if (synthetic_relocatable)
2612     {
2613       if (a->section->id < b->section->id)
2614         return -1;
2615
2616       if (a->section->id > b->section->id)
2617         return 1;
2618     }
2619
2620   if (a->value + a->section->vma < b->value + b->section->vma)
2621     return -1;
2622
2623   if (a->value + a->section->vma > b->value + b->section->vma)
2624     return 1;
2625
2626   /* For syms with the same value, prefer strong dynamic global function
2627      syms over other syms.  */
2628   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2629     return -1;
2630
2631   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2632     return 1;
2633
2634   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2635     return -1;
2636
2637   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2638     return 1;
2639
2640   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2641     return -1;
2642
2643   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2644     return 1;
2645
2646   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2647     return -1;
2648
2649   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2650     return 1;
2651
2652   return 0;
2653 }
2654
2655 /* Search SYMS for a symbol of the given VALUE.  */
2656
2657 static asymbol *
2658 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
2659 {
2660   long mid;
2661
2662   if (id == -1)
2663     {
2664       while (lo < hi)
2665         {
2666           mid = (lo + hi) >> 1;
2667           if (syms[mid]->value + syms[mid]->section->vma < value)
2668             lo = mid + 1;
2669           else if (syms[mid]->value + syms[mid]->section->vma > value)
2670             hi = mid;
2671           else
2672             return syms[mid];
2673         }
2674     }
2675   else
2676     {
2677       while (lo < hi)
2678         {
2679           mid = (lo + hi) >> 1;
2680           if (syms[mid]->section->id < id)
2681             lo = mid + 1;
2682           else if (syms[mid]->section->id > id)
2683             hi = mid;
2684           else if (syms[mid]->value < value)
2685             lo = mid + 1;
2686           else if (syms[mid]->value > value)
2687             hi = mid;
2688           else
2689             return syms[mid];
2690         }
2691     }
2692   return NULL;
2693 }
2694
2695 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2696    entry syms.  */
2697
2698 static long
2699 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2700                                 long static_count, asymbol **static_syms,
2701                                 long dyn_count, asymbol **dyn_syms,
2702                                 asymbol **ret)
2703 {
2704   asymbol *s;
2705   long i;
2706   long count;
2707   char *names;
2708   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2709   asection *opd;
2710   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2711   asymbol **syms;
2712
2713   *ret = NULL;
2714
2715   opd = bfd_get_section_by_name (abfd, ".opd");
2716   if (opd == NULL)
2717     return 0;
2718
2719   symcount = static_count;
2720   if (!relocatable)
2721     symcount += dyn_count;
2722   if (symcount == 0)
2723     return 0;
2724
2725   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2726   if (syms == NULL)
2727     return -1;
2728
2729   if (!relocatable && static_count != 0 && dyn_count != 0)
2730     {
2731       /* Use both symbol tables.  */
2732       memcpy (syms, static_syms, static_count * sizeof (*syms));
2733       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
2734     }
2735   else if (!relocatable && static_count == 0)
2736     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2737   else
2738     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2739
2740   synthetic_opd = opd;
2741   synthetic_relocatable = relocatable;
2742   qsort (syms, symcount, sizeof (*syms), compare_symbols);
2743
2744   if (!relocatable && symcount > 1)
2745     {
2746       long j;
2747       /* Trim duplicate syms, since we may have merged the normal and
2748          dynamic symbols.  Actually, we only care about syms that have
2749          different values, so trim any with the same value.  */
2750       for (i = 1, j = 1; i < symcount; ++i)
2751         if (syms[i - 1]->value + syms[i - 1]->section->vma
2752             != syms[i]->value + syms[i]->section->vma)
2753           syms[j++] = syms[i];
2754       symcount = j;
2755     }
2756
2757   i = 0;
2758   if (syms[i]->section == opd)
2759     ++i;
2760   codesecsym = i;
2761
2762   for (; i < symcount; ++i)
2763     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2764          != (SEC_CODE | SEC_ALLOC))
2765         || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2766       break;
2767   codesecsymend = i;
2768
2769   for (; i < symcount; ++i)
2770     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2771       break;
2772   secsymend = i;
2773
2774   for (; i < symcount; ++i)
2775     if (syms[i]->section != opd)
2776       break;
2777   opdsymend = i;
2778
2779   for (; i < symcount; ++i)
2780     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2781         != (SEC_CODE | SEC_ALLOC))
2782       break;
2783   symcount = i;
2784
2785   count = 0;
2786   if (opdsymend == secsymend)
2787     goto done;
2788
2789   if (relocatable)
2790     {
2791       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2792       arelent *r;
2793       size_t size;
2794       long relcount;
2795
2796       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2797       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2798       if (relcount == 0)
2799         goto done;
2800
2801       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2802         {
2803           count = -1;
2804           goto done;
2805         }
2806
2807       size = 0;
2808       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2809         {
2810           asymbol *sym;
2811
2812           while (r < opd->relocation + relcount
2813                  && r->address < syms[i]->value + opd->vma)
2814             ++r;
2815
2816           if (r == opd->relocation + relcount)
2817             break;
2818
2819           if (r->address != syms[i]->value + opd->vma)
2820             continue;
2821
2822           if (r->howto->type != R_PPC64_ADDR64)
2823             continue;
2824
2825           sym = *r->sym_ptr_ptr;
2826           if (!sym_exists_at (syms, opdsymend, symcount,
2827                               sym->section->id, sym->value + r->addend))
2828             {
2829               ++count;
2830               size += sizeof (asymbol);
2831               size += strlen (syms[i]->name) + 2;
2832             }
2833         }
2834
2835       s = *ret = bfd_malloc (size);
2836       if (s == NULL)
2837         {
2838           count = -1;
2839           goto done;
2840         }
2841
2842       names = (char *) (s + count);
2843
2844       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2845         {
2846           asymbol *sym;
2847
2848           while (r < opd->relocation + relcount
2849                  && r->address < syms[i]->value + opd->vma)
2850             ++r;
2851
2852           if (r == opd->relocation + relcount)
2853             break;
2854
2855           if (r->address != syms[i]->value + opd->vma)
2856             continue;
2857
2858           if (r->howto->type != R_PPC64_ADDR64)
2859             continue;
2860
2861           sym = *r->sym_ptr_ptr;
2862           if (!sym_exists_at (syms, opdsymend, symcount,
2863                               sym->section->id, sym->value + r->addend))
2864             {
2865               size_t len;
2866
2867               *s = *syms[i];
2868               s->section = sym->section;
2869               s->value = sym->value + r->addend;
2870               s->name = names;
2871               *names++ = '.';
2872               len = strlen (syms[i]->name);
2873               memcpy (names, syms[i]->name, len + 1);
2874               names += len + 1;
2875               s++;
2876             }
2877         }
2878     }
2879   else
2880     {
2881       bfd_byte *contents;
2882       size_t size;
2883
2884       if (!bfd_malloc_and_get_section (abfd, opd, &contents))
2885         {
2886           if (contents)
2887             {
2888             free_contents_and_exit:
2889               free (contents);
2890             }
2891           count = -1;
2892           goto done;
2893         }
2894
2895       size = 0;
2896       for (i = secsymend; i < opdsymend; ++i)
2897         {
2898           bfd_vma ent;
2899
2900           ent = bfd_get_64 (abfd, contents + syms[i]->value);
2901           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2902             {
2903               ++count;
2904               size += sizeof (asymbol);
2905               size += strlen (syms[i]->name) + 2;
2906             }
2907         }
2908
2909       s = *ret = bfd_malloc (size);
2910       if (s == NULL)
2911         goto free_contents_and_exit;
2912
2913       names = (char *) (s + count);
2914
2915       for (i = secsymend; i < opdsymend; ++i)
2916         {
2917           bfd_vma ent;
2918
2919           ent = bfd_get_64 (abfd, contents + syms[i]->value);
2920           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2921             {
2922               long lo, hi;
2923               size_t len;
2924               asection *sec = abfd->sections;
2925
2926               *s = *syms[i];
2927               lo = codesecsym;
2928               hi = codesecsymend;
2929               while (lo < hi)
2930                 {
2931                   long mid = (lo + hi) >> 1;
2932                   if (syms[mid]->section->vma < ent)
2933                     lo = mid + 1;
2934                   else if (syms[mid]->section->vma > ent)
2935                     hi = mid;
2936                   else
2937                     {
2938                       sec = syms[mid]->section;
2939                       break;
2940                     }
2941                 }
2942
2943               if (lo >= hi && lo > codesecsym)
2944                 sec = syms[lo - 1]->section;
2945
2946               for (; sec != NULL; sec = sec->next)
2947                 {
2948                   if (sec->vma > ent)
2949                     break;
2950                   if ((sec->flags & SEC_ALLOC) == 0
2951                       || (sec->flags & SEC_LOAD) == 0)
2952                     break;
2953                   if ((sec->flags & SEC_CODE) != 0)
2954                     s->section = sec;
2955                 }
2956               s->value = ent - s->section->vma;
2957               s->name = names;
2958               *names++ = '.';
2959               len = strlen (syms[i]->name);
2960               memcpy (names, syms[i]->name, len + 1);
2961               names += len + 1;
2962               s++;
2963             }
2964         }
2965       free (contents);
2966     }
2967
2968  done:
2969   free (syms);
2970   return count;
2971 }
2972 \f
2973 /* The following functions are specific to the ELF linker, while
2974    functions above are used generally.  Those named ppc64_elf_* are
2975    called by the main ELF linker code.  They appear in this file more
2976    or less in the order in which they are called.  eg.
2977    ppc64_elf_check_relocs is called early in the link process,
2978    ppc64_elf_finish_dynamic_sections is one of the last functions
2979    called.
2980
2981    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2982    functions have both a function code symbol and a function descriptor
2983    symbol.  A call to foo in a relocatable object file looks like:
2984
2985    .            .text
2986    .    x:
2987    .            bl      .foo
2988    .            nop
2989
2990    The function definition in another object file might be:
2991
2992    .            .section .opd
2993    .    foo:    .quad   .foo
2994    .            .quad   .TOC.@tocbase
2995    .            .quad   0
2996    .
2997    .            .text
2998    .    .foo:   blr
2999
3000    When the linker resolves the call during a static link, the branch
3001    unsurprisingly just goes to .foo and the .opd information is unused.
3002    If the function definition is in a shared library, things are a little
3003    different:  The call goes via a plt call stub, the opd information gets
3004    copied to the plt, and the linker patches the nop.
3005
3006    .    x:
3007    .            bl      .foo_stub
3008    .            ld      2,40(1)
3009    .
3010    .
3011    .    .foo_stub:
3012    .            addis   12,2,Lfoo@toc@ha        # in practice, the call stub
3013    .            addi    12,12,Lfoo@toc@l        # is slightly optimized, but
3014    .            std     2,40(1)                 # this is the general idea
3015    .            ld      11,0(12)
3016    .            ld      2,8(12)
3017    .            mtctr   11
3018    .            ld      11,16(12)
3019    .            bctr
3020    .
3021    .            .section .plt
3022    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3023
3024    The "reloc ()" notation is supposed to indicate that the linker emits
3025    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3026    copying.
3027
3028    What are the difficulties here?  Well, firstly, the relocations
3029    examined by the linker in check_relocs are against the function code
3030    sym .foo, while the dynamic relocation in the plt is emitted against
3031    the function descriptor symbol, foo.  Somewhere along the line, we need
3032    to carefully copy dynamic link information from one symbol to the other.
3033    Secondly, the generic part of the elf linker will make .foo a dynamic
3034    symbol as is normal for most other backends.  We need foo dynamic
3035    instead, at least for an application final link.  However, when
3036    creating a shared library containing foo, we need to have both symbols
3037    dynamic so that references to .foo are satisfied during the early
3038    stages of linking.  Otherwise the linker might decide to pull in a
3039    definition from some other object, eg. a static library.
3040
3041    Update: As of August 2004, we support a new convention.  Function
3042    calls may use the function descriptor symbol, ie. "bl foo".  This
3043    behaves exactly as "bl .foo".  */
3044
3045 /* The linker needs to keep track of the number of relocs that it
3046    decides to copy as dynamic relocs in check_relocs for each symbol.
3047    This is so that it can later discard them if they are found to be
3048    unnecessary.  We store the information in a field extending the
3049    regular ELF linker hash table.  */
3050
3051 struct ppc_dyn_relocs
3052 {
3053   struct ppc_dyn_relocs *next;
3054
3055   /* The input section of the reloc.  */
3056   asection *sec;
3057
3058   /* Total number of relocs copied for the input section.  */
3059   bfd_size_type count;
3060
3061   /* Number of pc-relative relocs copied for the input section.  */
3062   bfd_size_type pc_count;
3063 };
3064
3065 /* Track GOT entries needed for a given symbol.  We might need more
3066    than one got entry per symbol.  */
3067 struct got_entry
3068 {
3069   struct got_entry *next;
3070
3071   /* The symbol addend that we'll be placing in the GOT.  */
3072   bfd_vma addend;
3073
3074   /* Unlike other ELF targets, we use separate GOT entries for the same
3075      symbol referenced from different input files.  This is to support
3076      automatic multiple TOC/GOT sections, where the TOC base can vary
3077      from one input file to another.
3078
3079      Point to the BFD owning this GOT entry.  */
3080   bfd *owner;
3081
3082   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
3083      TLS_TPREL or TLS_DTPREL for tls entries.  */
3084   char tls_type;
3085
3086   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
3087   union
3088     {
3089       bfd_signed_vma refcount;
3090       bfd_vma offset;
3091     } got;
3092 };
3093
3094 /* The same for PLT.  */
3095 struct plt_entry
3096 {
3097   struct plt_entry *next;
3098
3099   bfd_vma addend;
3100
3101   union
3102     {
3103       bfd_signed_vma refcount;
3104       bfd_vma offset;
3105     } plt;
3106 };
3107
3108 /* Of those relocs that might be copied as dynamic relocs, this macro
3109    selects those that must be copied when linking a shared library,
3110    even when the symbol is local.  */
3111
3112 #define MUST_BE_DYN_RELOC(RTYPE)                \
3113   ((RTYPE) != R_PPC64_REL32                     \
3114    && (RTYPE) != R_PPC64_REL64                  \
3115    && (RTYPE) != R_PPC64_REL30)
3116
3117 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3118    copying dynamic variables from a shared lib into an app's dynbss
3119    section, and instead use a dynamic relocation to point into the
3120    shared lib.  With code that gcc generates, it's vital that this be
3121    enabled;  In the PowerPC64 ABI, the address of a function is actually
3122    the address of a function descriptor, which resides in the .opd
3123    section.  gcc uses the descriptor directly rather than going via the
3124    GOT as some other ABI's do, which means that initialized function
3125    pointers must reference the descriptor.  Thus, a function pointer
3126    initialized to the address of a function in a shared library will
3127    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3128    redefines the function descriptor symbol to point to the copy.  This
3129    presents a problem as a plt entry for that function is also
3130    initialized from the function descriptor symbol and the copy reloc
3131    may not be initialized first.  */
3132 #define ELIMINATE_COPY_RELOCS 1
3133
3134 /* Section name for stubs is the associated section name plus this
3135    string.  */
3136 #define STUB_SUFFIX ".stub"
3137
3138 /* Linker stubs.
3139    ppc_stub_long_branch:
3140    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3141    destination, but a 24 bit branch in a stub section will reach.
3142    .    b       dest
3143
3144    ppc_stub_plt_branch:
3145    Similar to the above, but a 24 bit branch in the stub section won't
3146    reach its destination.
3147    .    addis   %r12,%r2,xxx@toc@ha
3148    .    ld      %r11,xxx@toc@l(%r12)
3149    .    mtctr   %r11
3150    .    bctr
3151
3152    ppc_stub_plt_call:
3153    Used to call a function in a shared library.  If it so happens that
3154    the plt entry referenced crosses a 64k boundary, then an extra
3155    "addis %r12,%r12,1" will be inserted before the load at xxx+8 or
3156    xxx+16 as appropriate.
3157    .    addis   %r12,%r2,xxx@toc@ha
3158    .    std     %r2,40(%r1)
3159    .    ld      %r11,xxx+0@toc@l(%r12)
3160    .    ld      %r2,xxx+8@toc@l(%r12)
3161    .    mtctr   %r11
3162    .    ld      %r11,xxx+16@toc@l(%r12)
3163    .    bctr
3164
3165    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3166    code to adjust the value and save r2 to support multiple toc sections.
3167    A ppc_stub_long_branch with an r2 offset looks like:
3168    .    std     %r2,40(%r1)
3169    .    addis   %r2,%r2,off@ha
3170    .    addi    %r2,%r2,off@l
3171    .    b       dest
3172
3173    A ppc_stub_plt_branch with an r2 offset looks like:
3174    .    std     %r2,40(%r1)
3175    .    addis   %r12,%r2,xxx@toc@ha
3176    .    ld      %r11,xxx@toc@l(%r12)
3177    .    addis   %r2,%r2,off@ha
3178    .    addi    %r2,%r2,off@l
3179    .    mtctr   %r11
3180    .    bctr
3181 */
3182
3183 enum ppc_stub_type {
3184   ppc_stub_none,
3185   ppc_stub_long_branch,
3186   ppc_stub_long_branch_r2off,
3187   ppc_stub_plt_branch,
3188   ppc_stub_plt_branch_r2off,
3189   ppc_stub_plt_call
3190 };
3191
3192 struct ppc_stub_hash_entry {
3193
3194   /* Base hash table entry structure.  */
3195   struct bfd_hash_entry root;
3196
3197   enum ppc_stub_type stub_type;
3198
3199   /* The stub section.  */
3200   asection *stub_sec;
3201
3202   /* Offset within stub_sec of the beginning of this stub.  */
3203   bfd_vma stub_offset;
3204
3205   /* Given the symbol's value and its section we can determine its final
3206      value when building the stubs (so the stub knows where to jump.  */
3207   bfd_vma target_value;
3208   asection *target_section;
3209
3210   /* The symbol table entry, if any, that this was derived from.  */
3211   struct ppc_link_hash_entry *h;
3212
3213   /* And the reloc addend that this was derived from.  */
3214   bfd_vma addend;
3215
3216   /* Where this stub is being called from, or, in the case of combined
3217      stub sections, the first input section in the group.  */
3218   asection *id_sec;
3219 };
3220
3221 struct ppc_branch_hash_entry {
3222
3223   /* Base hash table entry structure.  */
3224   struct bfd_hash_entry root;
3225
3226   /* Offset within branch lookup table.  */
3227   unsigned int offset;
3228
3229   /* Generation marker.  */
3230   unsigned int iter;
3231 };
3232
3233 struct ppc_link_hash_entry
3234 {
3235   struct elf_link_hash_entry elf;
3236
3237   /* A pointer to the most recently used stub hash entry against this
3238      symbol.  */
3239   struct ppc_stub_hash_entry *stub_cache;
3240
3241   /* Track dynamic relocs copied for this symbol.  */
3242   struct ppc_dyn_relocs *dyn_relocs;
3243
3244   /* Link between function code and descriptor symbols.  */
3245   struct ppc_link_hash_entry *oh;
3246
3247   /* Flag function code and descriptor symbols.  */
3248   unsigned int is_func:1;
3249   unsigned int is_func_descriptor:1;
3250   unsigned int fake:1;
3251
3252   /* Whether global opd/toc sym has been adjusted or not.
3253      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3254      should be set for all globals defined in any opd/toc section.  */
3255   unsigned int adjust_done:1;
3256
3257   /* Set if we twiddled this symbol to weak at some stage.  */
3258   unsigned int was_undefined:1;
3259
3260   /* Contexts in which symbol is used in the GOT (or TOC).
3261      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3262      corresponding relocs are encountered during check_relocs.
3263      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3264      indicate the corresponding GOT entry type is not needed.
3265      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3266      a TPREL one.  We use a separate flag rather than setting TPREL
3267      just for convenience in distinguishing the two cases.  */
3268 #define TLS_GD           1      /* GD reloc. */
3269 #define TLS_LD           2      /* LD reloc. */
3270 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
3271 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
3272 #define TLS_TLS         16      /* Any TLS reloc.  */
3273 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
3274 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
3275   char tls_mask;
3276 };
3277
3278 /* ppc64 ELF linker hash table.  */
3279
3280 struct ppc_link_hash_table
3281 {
3282   struct elf_link_hash_table elf;
3283
3284   /* The stub hash table.  */
3285   struct bfd_hash_table stub_hash_table;
3286
3287   /* Another hash table for plt_branch stubs.  */
3288   struct bfd_hash_table branch_hash_table;
3289
3290   /* Linker stub bfd.  */
3291   bfd *stub_bfd;
3292
3293   /* Linker call-backs.  */
3294   asection * (*add_stub_section) (const char *, asection *);
3295   void (*layout_sections_again) (void);
3296
3297   /* Array to keep track of which stub sections have been created, and
3298      information on stub grouping.  */
3299   struct map_stub {
3300     /* This is the section to which stubs in the group will be attached.  */
3301     asection *link_sec;
3302     /* The stub section.  */
3303     asection *stub_sec;
3304     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
3305     bfd_vma toc_off;
3306   } *stub_group;
3307
3308   /* Temp used when calculating TOC pointers.  */
3309   bfd_vma toc_curr;
3310
3311   /* Highest input section id.  */
3312   int top_id;
3313
3314   /* Highest output section index.  */
3315   int top_index;
3316
3317   /* List of input sections for each output section.  */
3318   asection **input_list;
3319
3320   /* Short-cuts to get to dynamic linker sections.  */
3321   asection *got;
3322   asection *plt;
3323   asection *relplt;
3324   asection *dynbss;
3325   asection *relbss;
3326   asection *glink;
3327   asection *sfpr;
3328   asection *brlt;
3329   asection *relbrlt;
3330
3331   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3332   struct ppc_link_hash_entry *tls_get_addr;
3333   struct ppc_link_hash_entry *tls_get_addr_fd;
3334
3335   /* Statistics.  */
3336   unsigned long stub_count[ppc_stub_plt_call];
3337
3338   /* Number of stubs against global syms.  */
3339   unsigned long stub_globals;
3340
3341   /* Set if we should emit symbols for stubs.  */
3342   unsigned int emit_stub_syms:1;
3343
3344   /* Support for multiple toc sections.  */
3345   unsigned int no_multi_toc:1;
3346   unsigned int multi_toc_needed:1;
3347
3348   /* Set on error.  */
3349   unsigned int stub_error:1;
3350
3351   /* Flag set when small branches are detected.  Used to
3352      select suitable defaults for the stub group size.  */
3353   unsigned int has_14bit_branch:1;
3354
3355   /* Temp used by ppc64_elf_check_directives.  */
3356   unsigned int twiddled_syms:1;
3357
3358   /* Incremented every time we size stubs.  */
3359   unsigned int stub_iteration;
3360
3361   /* Small local sym to section mapping cache.  */
3362   struct sym_sec_cache sym_sec;
3363 };
3364
3365 /* Rename some of the generic section flags to better document how they
3366    are used here.  */
3367 #define has_toc_reloc has_gp_reloc
3368 #define makes_toc_func_call need_finalize_relax
3369 #define call_check_in_progress reloc_done
3370
3371 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
3372
3373 #define ppc_hash_table(p) \
3374   ((struct ppc_link_hash_table *) ((p)->hash))
3375
3376 #define ppc_stub_hash_lookup(table, string, create, copy) \
3377   ((struct ppc_stub_hash_entry *) \
3378    bfd_hash_lookup ((table), (string), (create), (copy)))
3379
3380 #define ppc_branch_hash_lookup(table, string, create, copy) \
3381   ((struct ppc_branch_hash_entry *) \
3382    bfd_hash_lookup ((table), (string), (create), (copy)))
3383
3384 /* Create an entry in the stub hash table.  */
3385
3386 static struct bfd_hash_entry *
3387 stub_hash_newfunc (struct bfd_hash_entry *entry,
3388                    struct bfd_hash_table *table,
3389                    const char *string)
3390 {
3391   /* Allocate the structure if it has not already been allocated by a
3392      subclass.  */
3393   if (entry == NULL)
3394     {
3395       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3396       if (entry == NULL)
3397         return entry;
3398     }
3399
3400   /* Call the allocation method of the superclass.  */
3401   entry = bfd_hash_newfunc (entry, table, string);
3402   if (entry != NULL)
3403     {
3404       struct ppc_stub_hash_entry *eh;
3405
3406       /* Initialize the local fields.  */
3407       eh = (struct ppc_stub_hash_entry *) entry;
3408       eh->stub_type = ppc_stub_none;
3409       eh->stub_sec = NULL;
3410       eh->stub_offset = 0;
3411       eh->target_value = 0;
3412       eh->target_section = NULL;
3413       eh->h = NULL;
3414       eh->id_sec = NULL;
3415     }
3416
3417   return entry;
3418 }
3419
3420 /* Create an entry in the branch hash table.  */
3421
3422 static struct bfd_hash_entry *
3423 branch_hash_newfunc (struct bfd_hash_entry *entry,
3424                      struct bfd_hash_table *table,
3425                      const char *string)
3426 {
3427   /* Allocate the structure if it has not already been allocated by a
3428      subclass.  */
3429   if (entry == NULL)
3430     {
3431       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3432       if (entry == NULL)
3433         return entry;
3434     }
3435
3436   /* Call the allocation method of the superclass.  */
3437   entry = bfd_hash_newfunc (entry, table, string);
3438   if (entry != NULL)
3439     {
3440       struct ppc_branch_hash_entry *eh;
3441
3442       /* Initialize the local fields.  */
3443       eh = (struct ppc_branch_hash_entry *) entry;
3444       eh->offset = 0;
3445       eh->iter = 0;
3446     }
3447
3448   return entry;
3449 }
3450
3451 /* Create an entry in a ppc64 ELF linker hash table.  */
3452
3453 static struct bfd_hash_entry *
3454 link_hash_newfunc (struct bfd_hash_entry *entry,
3455                    struct bfd_hash_table *table,
3456                    const char *string)
3457 {
3458   /* Allocate the structure if it has not already been allocated by a
3459      subclass.  */
3460   if (entry == NULL)
3461     {
3462       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3463       if (entry == NULL)
3464         return entry;
3465     }
3466
3467   /* Call the allocation method of the superclass.  */
3468   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3469   if (entry != NULL)
3470     {
3471       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3472
3473       memset (&eh->stub_cache, 0,
3474               (sizeof (struct ppc_link_hash_entry)
3475                - offsetof (struct ppc_link_hash_entry, stub_cache)));
3476     }
3477
3478   return entry;
3479 }
3480
3481 /* Create a ppc64 ELF linker hash table.  */
3482
3483 static struct bfd_link_hash_table *
3484 ppc64_elf_link_hash_table_create (bfd *abfd)
3485 {
3486   struct ppc_link_hash_table *htab;
3487   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3488
3489   htab = bfd_zmalloc (amt);
3490   if (htab == NULL)
3491     return NULL;
3492
3493   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3494                                       sizeof (struct ppc_link_hash_entry)))
3495     {
3496       free (htab);
3497       return NULL;
3498     }
3499
3500   /* Init the stub hash table too.  */
3501   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3502                             sizeof (struct ppc_stub_hash_entry)))
3503     return NULL;
3504
3505   /* And the branch hash table.  */
3506   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3507                             sizeof (struct ppc_branch_hash_entry)))
3508     return NULL;
3509
3510   /* Initializing two fields of the union is just cosmetic.  We really
3511      only care about glist, but when compiled on a 32-bit host the
3512      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
3513      debugger inspection of these fields look nicer.  */
3514   htab->elf.init_got_refcount.refcount = 0;
3515   htab->elf.init_got_refcount.glist = NULL;
3516   htab->elf.init_plt_refcount.refcount = 0;
3517   htab->elf.init_plt_refcount.glist = NULL;
3518   htab->elf.init_got_offset.offset = 0;
3519   htab->elf.init_got_offset.glist = NULL;
3520   htab->elf.init_plt_offset.offset = 0;
3521   htab->elf.init_plt_offset.glist = NULL;
3522
3523   return &htab->elf.root;
3524 }
3525
3526 /* Free the derived linker hash table.  */
3527
3528 static void
3529 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
3530 {
3531   struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
3532
3533   bfd_hash_table_free (&ret->stub_hash_table);
3534   bfd_hash_table_free (&ret->branch_hash_table);
3535   _bfd_generic_link_hash_table_free (hash);
3536 }
3537
3538 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
3539
3540 void
3541 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
3542 {
3543   struct ppc_link_hash_table *htab;
3544
3545   elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
3546
3547 /* Always hook our dynamic sections into the first bfd, which is the
3548    linker created stub bfd.  This ensures that the GOT header is at
3549    the start of the output TOC section.  */
3550   htab = ppc_hash_table (info);
3551   htab->stub_bfd = abfd;
3552   htab->elf.dynobj = abfd;
3553 }
3554
3555 /* Build a name for an entry in the stub hash table.  */
3556
3557 static char *
3558 ppc_stub_name (const asection *input_section,
3559                const asection *sym_sec,
3560                const struct ppc_link_hash_entry *h,
3561                const Elf_Internal_Rela *rel)
3562 {
3563   char *stub_name;
3564   bfd_size_type len;
3565
3566   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3567      offsets from a sym as a branch target?  In fact, we could
3568      probably assume the addend is always zero.  */
3569   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3570
3571   if (h)
3572     {
3573       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3574       stub_name = bfd_malloc (len);
3575       if (stub_name == NULL)
3576         return stub_name;
3577
3578       sprintf (stub_name, "%08x.%s+%x",
3579                input_section->id & 0xffffffff,
3580                h->elf.root.root.string,
3581                (int) rel->r_addend & 0xffffffff);
3582     }
3583   else
3584     {
3585       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3586       stub_name = bfd_malloc (len);
3587       if (stub_name == NULL)
3588         return stub_name;
3589
3590       sprintf (stub_name, "%08x.%x:%x+%x",
3591                input_section->id & 0xffffffff,
3592                sym_sec->id & 0xffffffff,
3593                (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3594                (int) rel->r_addend & 0xffffffff);
3595     }
3596   if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3597     stub_name[len - 2] = 0;
3598   return stub_name;
3599 }
3600
3601 /* Look up an entry in the stub hash.  Stub entries are cached because
3602    creating the stub name takes a bit of time.  */
3603
3604 static struct ppc_stub_hash_entry *
3605 ppc_get_stub_entry (const asection *input_section,
3606                     const asection *sym_sec,
3607                     struct ppc_link_hash_entry *h,
3608                     const Elf_Internal_Rela *rel,
3609                     struct ppc_link_hash_table *htab)
3610 {
3611   struct ppc_stub_hash_entry *stub_entry;
3612   const asection *id_sec;
3613
3614   /* If this input section is part of a group of sections sharing one
3615      stub section, then use the id of the first section in the group.
3616      Stub names need to include a section id, as there may well be
3617      more than one stub used to reach say, printf, and we need to
3618      distinguish between them.  */
3619   id_sec = htab->stub_group[input_section->id].link_sec;
3620
3621   if (h != NULL && h->stub_cache != NULL
3622       && h->stub_cache->h == h
3623       && h->stub_cache->id_sec == id_sec)
3624     {
3625       stub_entry = h->stub_cache;
3626     }
3627   else
3628     {
3629       char *stub_name;
3630
3631       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
3632       if (stub_name == NULL)
3633         return NULL;
3634
3635       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3636                                          stub_name, FALSE, FALSE);
3637       if (h != NULL)
3638         h->stub_cache = stub_entry;
3639
3640       free (stub_name);
3641     }
3642
3643   return stub_entry;
3644 }
3645
3646 /* Add a new stub entry to the stub hash.  Not all fields of the new
3647    stub entry are initialised.  */
3648
3649 static struct ppc_stub_hash_entry *
3650 ppc_add_stub (const char *stub_name,
3651               asection *section,
3652               struct ppc_link_hash_table *htab)
3653 {
3654   asection *link_sec;
3655   asection *stub_sec;
3656   struct ppc_stub_hash_entry *stub_entry;
3657
3658   link_sec = htab->stub_group[section->id].link_sec;
3659   stub_sec = htab->stub_group[section->id].stub_sec;
3660   if (stub_sec == NULL)
3661     {
3662       stub_sec = htab->stub_group[link_sec->id].stub_sec;
3663       if (stub_sec == NULL)
3664         {
3665           size_t namelen;
3666           bfd_size_type len;
3667           char *s_name;
3668
3669           namelen = strlen (link_sec->name);
3670           len = namelen + sizeof (STUB_SUFFIX);
3671           s_name = bfd_alloc (htab->stub_bfd, len);
3672           if (s_name == NULL)
3673             return NULL;
3674
3675           memcpy (s_name, link_sec->name, namelen);
3676           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3677           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3678           if (stub_sec == NULL)
3679             return NULL;
3680           htab->stub_group[link_sec->id].stub_sec = stub_sec;
3681         }
3682       htab->stub_group[section->id].stub_sec = stub_sec;
3683     }
3684
3685   /* Enter this entry into the linker stub hash table.  */
3686   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3687                                      TRUE, FALSE);
3688   if (stub_entry == NULL)
3689     {
3690       (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
3691                              section->owner, stub_name);
3692       return NULL;
3693     }
3694
3695   stub_entry->stub_sec = stub_sec;
3696   stub_entry->stub_offset = 0;
3697   stub_entry->id_sec = link_sec;
3698   return stub_entry;
3699 }
3700
3701 /* Create sections for linker generated code.  */
3702
3703 static bfd_boolean
3704 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3705 {
3706   struct ppc_link_hash_table *htab;
3707   flagword flags;
3708
3709   htab = ppc_hash_table (info);
3710
3711   /* Create .sfpr for code to save and restore fp regs.  */
3712   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3713            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3714   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3715                                                    flags);
3716   if (htab->sfpr == NULL
3717       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3718     return FALSE;
3719
3720   /* Create .glink for lazy dynamic linking support.  */
3721   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3722                                                     flags);
3723   if (htab->glink == NULL
3724       || ! bfd_set_section_alignment (dynobj, htab->glink, 2))
3725     return FALSE;
3726
3727   /* Create branch lookup table for plt_branch stubs.  */
3728   if (info->shared)
3729     {
3730       flags = (SEC_ALLOC | SEC_LOAD
3731                | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3732       htab->brlt
3733         = bfd_make_section_anyway_with_flags (dynobj, ".data.rel.ro.brlt",
3734                                               flags);
3735     }
3736   else
3737     {
3738       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3739                | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3740       htab->brlt
3741         = bfd_make_section_anyway_with_flags (dynobj, ".rodata.brlt", flags);
3742     }
3743
3744   if (htab->brlt == NULL
3745       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
3746     return FALSE;
3747
3748   if (info->shared)
3749     {
3750       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3751                | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3752       htab->relbrlt
3753         = bfd_make_section_anyway_with_flags (dynobj, ".rela.data.rel.ro.brlt",
3754                                               flags);
3755     }
3756   else if (info->emitrelocations)
3757     {
3758       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3759                | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3760       htab->relbrlt
3761         = bfd_make_section_anyway_with_flags (dynobj, ".rela.rodata.brlt",
3762                                               flags);
3763     }
3764   else
3765     return TRUE;
3766
3767   if (!htab->relbrlt
3768       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3769     return FALSE;
3770
3771   return TRUE;
3772 }
3773
3774 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3775    not already done.  */
3776
3777 static bfd_boolean
3778 create_got_section (bfd *abfd, struct bfd_link_info *info)
3779 {
3780   asection *got, *relgot;
3781   flagword flags;
3782   struct ppc_link_hash_table *htab = ppc_hash_table (info);
3783
3784   if (!htab->got)
3785     {
3786       if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
3787         return FALSE;
3788
3789       htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
3790       if (!htab->got)
3791         abort ();
3792     }
3793
3794   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3795            | SEC_LINKER_CREATED);
3796
3797   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3798   if (!got
3799       || !bfd_set_section_alignment (abfd, got, 3))
3800     return FALSE;
3801
3802   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3803                                                flags | SEC_READONLY);
3804   if (!relgot
3805       || ! bfd_set_section_alignment (abfd, relgot, 3))
3806     return FALSE;
3807
3808   ppc64_elf_tdata (abfd)->got = got;
3809   ppc64_elf_tdata (abfd)->relgot = relgot;
3810   return TRUE;
3811 }
3812
3813 /* Create the dynamic sections, and set up shortcuts.  */
3814
3815 static bfd_boolean
3816 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3817 {
3818   struct ppc_link_hash_table *htab;
3819
3820   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3821     return FALSE;
3822
3823   htab = ppc_hash_table (info);
3824   if (!htab->got)
3825     htab->got = bfd_get_section_by_name (dynobj, ".got");
3826   htab->plt = bfd_get_section_by_name (dynobj, ".plt");
3827   htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
3828   htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
3829   if (!info->shared)
3830     htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
3831
3832   if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
3833       || (!info->shared && !htab->relbss))
3834     abort ();
3835
3836   return TRUE;
3837 }
3838
3839 /* Merge PLT info on FROM with that on TO.  */
3840
3841 static void
3842 move_plt_plist (struct ppc_link_hash_entry *from,
3843                 struct ppc_link_hash_entry *to)
3844 {
3845   if (from->elf.plt.plist != NULL)
3846     {
3847       if (to->elf.plt.plist != NULL)
3848         {
3849           struct plt_entry **entp;
3850           struct plt_entry *ent;
3851
3852           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3853             {
3854               struct plt_entry *dent;
3855
3856               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3857                 if (dent->addend == ent->addend)
3858                   {
3859                     dent->plt.refcount += ent->plt.refcount;
3860                     *entp = ent->next;
3861                     break;
3862                   }
3863               if (dent == NULL)
3864                 entp = &ent->next;
3865             }
3866           *entp = to->elf.plt.plist;
3867         }
3868
3869       to->elf.plt.plist = from->elf.plt.plist;
3870       from->elf.plt.plist = NULL;
3871     }
3872 }
3873
3874 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3875
3876 static void
3877 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3878                                 struct elf_link_hash_entry *dir,
3879                                 struct elf_link_hash_entry *ind)
3880 {
3881   struct ppc_link_hash_entry *edir, *eind;
3882
3883   edir = (struct ppc_link_hash_entry *) dir;
3884   eind = (struct ppc_link_hash_entry *) ind;
3885
3886   /* Copy over any dynamic relocs we may have on the indirect sym.  */
3887   if (eind->dyn_relocs != NULL)
3888     {
3889       if (edir->dyn_relocs != NULL)
3890         {
3891           struct ppc_dyn_relocs **pp;
3892           struct ppc_dyn_relocs *p;
3893
3894           /* Add reloc counts against the indirect sym to the direct sym
3895              list.  Merge any entries against the same section.  */
3896           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3897             {
3898               struct ppc_dyn_relocs *q;
3899
3900               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3901                 if (q->sec == p->sec)
3902                   {
3903                     q->pc_count += p->pc_count;
3904                     q->count += p->count;
3905                     *pp = p->next;
3906                     break;
3907                   }
3908               if (q == NULL)
3909                 pp = &p->next;
3910             }
3911           *pp = edir->dyn_relocs;
3912         }
3913
3914       edir->dyn_relocs = eind->dyn_relocs;
3915       eind->dyn_relocs = NULL;
3916     }
3917
3918   edir->is_func |= eind->is_func;
3919   edir->is_func_descriptor |= eind->is_func_descriptor;
3920   edir->tls_mask |= eind->tls_mask;
3921
3922   /* If called to transfer flags for a weakdef during processing
3923      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
3924      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
3925   if (!(ELIMINATE_COPY_RELOCS
3926         && eind->elf.root.type != bfd_link_hash_indirect
3927         && edir->elf.dynamic_adjusted))
3928     edir->elf.non_got_ref |= eind->elf.non_got_ref;
3929
3930   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3931   edir->elf.ref_regular |= eind->elf.ref_regular;
3932   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3933   edir->elf.needs_plt |= eind->elf.needs_plt;
3934
3935   /* If we were called to copy over info for a weak sym, that's all.  */
3936   if (eind->elf.root.type != bfd_link_hash_indirect)
3937     return;
3938
3939   /* Copy over got entries that we may have already seen to the
3940      symbol which just became indirect.  */
3941   if (eind->elf.got.glist != NULL)
3942     {
3943       if (edir->elf.got.glist != NULL)
3944         {
3945           struct got_entry **entp;
3946           struct got_entry *ent;
3947
3948           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3949             {
3950               struct got_entry *dent;
3951
3952               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3953                 if (dent->addend == ent->addend
3954                     && dent->owner == ent->owner
3955                     && dent->tls_type == ent->tls_type)
3956                   {
3957                     dent->got.refcount += ent->got.refcount;
3958                     *entp = ent->next;
3959                     break;
3960                   }
3961               if (dent == NULL)
3962                 entp = &ent->next;
3963             }
3964           *entp = edir->elf.got.glist;
3965         }
3966
3967       edir->elf.got.glist = eind->elf.got.glist;
3968       eind->elf.got.glist = NULL;
3969     }
3970
3971   /* And plt entries.  */
3972   move_plt_plist (eind, edir);
3973
3974   if (eind->elf.dynindx != -1)
3975     {
3976       if (edir->elf.dynindx != -1)
3977         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3978                                 edir->elf.dynstr_index);
3979       edir->elf.dynindx = eind->elf.dynindx;
3980       edir->elf.dynstr_index = eind->elf.dynstr_index;
3981       eind->elf.dynindx = -1;
3982       eind->elf.dynstr_index = 0;
3983     }
3984 }
3985
3986 /* Find the function descriptor hash entry from the given function code
3987    hash entry FH.  Link the entries via their OH fields.  */
3988
3989 static struct ppc_link_hash_entry *
3990 get_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3991 {
3992   struct ppc_link_hash_entry *fdh = fh->oh;
3993
3994   if (fdh == NULL)
3995     {
3996       const char *fd_name = fh->elf.root.root.string + 1;
3997
3998       fdh = (struct ppc_link_hash_entry *)
3999         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4000       if (fdh != NULL)
4001         {
4002           fdh->is_func_descriptor = 1;
4003           fdh->oh = fh;
4004           fh->is_func = 1;
4005           fh->oh = fdh;
4006         }
4007     }
4008
4009   return fdh;
4010 }
4011
4012 /* Make a fake function descriptor sym for the code sym FH.  */
4013
4014 static struct ppc_link_hash_entry *
4015 make_fdh (struct bfd_link_info *info,
4016           struct ppc_link_hash_entry *fh)
4017 {
4018   bfd *abfd;
4019   asymbol *newsym;
4020   struct bfd_link_hash_entry *bh;
4021   struct ppc_link_hash_entry *fdh;
4022
4023   abfd = fh->elf.root.u.undef.abfd;
4024   newsym = bfd_make_empty_symbol (abfd);
4025   newsym->name = fh->elf.root.root.string + 1;
4026   newsym->section = bfd_und_section_ptr;
4027   newsym->value = 0;
4028   newsym->flags = BSF_WEAK;
4029
4030   bh = NULL;
4031   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4032                                          newsym->flags, newsym->section,
4033                                          newsym->value, NULL, FALSE, FALSE,
4034                                          &bh))
4035     return NULL;
4036
4037   fdh = (struct ppc_link_hash_entry *) bh;
4038   fdh->elf.non_elf = 0;
4039   fdh->fake = 1;
4040   fdh->is_func_descriptor = 1;
4041   fdh->oh = fh;
4042   fh->is_func = 1;
4043   fh->oh = fdh;
4044   return fdh;
4045 }
4046
4047 /* Hacks to support old ABI code.
4048    When making function calls, old ABI code references function entry
4049    points (dot symbols), while new ABI code references the function
4050    descriptor symbol.  We need to make any combination of reference and
4051    definition work together, without breaking archive linking.
4052
4053    For a defined function "foo" and an undefined call to "bar":
4054    An old object defines "foo" and ".foo", references ".bar" (possibly
4055    "bar" too).
4056    A new object defines "foo" and references "bar".
4057
4058    A new object thus has no problem with its undefined symbols being
4059    satisfied by definitions in an old object.  On the other hand, the
4060    old object won't have ".bar" satisfied by a new object.  */
4061
4062 /* Fix function descriptor symbols defined in .opd sections to be
4063    function type.  */
4064
4065 static bfd_boolean
4066 ppc64_elf_add_symbol_hook (bfd *ibfd,
4067                            struct bfd_link_info *info ATTRIBUTE_UNUSED,
4068                            Elf_Internal_Sym *isym,
4069                            const char **name,
4070                            flagword *flags ATTRIBUTE_UNUSED,
4071                            asection **sec,
4072                            bfd_vma *value ATTRIBUTE_UNUSED)
4073 {
4074   if (*sec != NULL
4075       && strcmp (bfd_get_section_name (ibfd, *sec), ".opd") == 0)
4076     isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4077
4078   if ((*name)[0] == '.'
4079       && ELF_ST_BIND (isym->st_info) == STB_GLOBAL
4080       && ELF_ST_TYPE (isym->st_info) < STT_SECTION
4081       && is_ppc64_elf_target (ibfd->xvec))
4082     ppc64_elf_tdata (ibfd)->u.has_dotsym = 1;
4083
4084   return TRUE;
4085 }
4086
4087 /* This function makes an old ABI object reference to ".bar" cause the
4088    inclusion of a new ABI object archive that defines "bar".
4089    NAME is a symbol defined in an archive.  Return a symbol in the hash
4090    table that might be satisfied by the archive symbols.  */
4091
4092 static struct elf_link_hash_entry *
4093 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4094                                  struct bfd_link_info *info,
4095                                  const char *name)
4096 {
4097   struct elf_link_hash_entry *h;
4098   char *dot_name;
4099   size_t len;
4100
4101   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4102   if (h != NULL
4103       /* Don't return this sym if it is a fake function descriptor
4104          created by add_symbol_adjust.  */
4105       && !(h->root.type == bfd_link_hash_undefweak
4106            && ((struct ppc_link_hash_entry *) h)->fake))
4107     return h;
4108
4109   if (name[0] == '.')
4110     return h;
4111
4112   len = strlen (name);
4113   dot_name = bfd_alloc (abfd, len + 2);
4114   if (dot_name == NULL)
4115     return (struct elf_link_hash_entry *) 0 - 1;
4116   dot_name[0] = '.';
4117   memcpy (dot_name + 1, name, len + 1);
4118   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4119   bfd_release (abfd, dot_name);
4120   return h;
4121 }
4122
4123 /* This function satisfies all old ABI object references to ".bar" if a
4124    new ABI object defines "bar".  Well, at least, undefined dot symbols
4125    are made weak.  This stops later archive searches from including an
4126    object if we already have a function descriptor definition.  It also
4127    prevents the linker complaining about undefined symbols.
4128    We also check and correct mismatched symbol visibility here.  The
4129    most restrictive visibility of the function descriptor and the
4130    function entry symbol is used.  */
4131
4132 struct add_symbol_adjust_data
4133 {
4134   struct bfd_link_info *info;
4135   bfd_boolean ok;
4136 };
4137
4138 static bfd_boolean
4139 add_symbol_adjust (struct elf_link_hash_entry *h, void *inf)
4140 {
4141   struct add_symbol_adjust_data *data;
4142   struct ppc_link_hash_table *htab;
4143   struct ppc_link_hash_entry *eh;
4144   struct ppc_link_hash_entry *fdh;
4145
4146   if (h->root.type == bfd_link_hash_indirect)
4147     return TRUE;
4148
4149   if (h->root.type == bfd_link_hash_warning)
4150     h = (struct elf_link_hash_entry *) h->root.u.i.link;
4151
4152   if (h->root.root.string[0] != '.')
4153     return TRUE;
4154
4155   data = inf;
4156   htab = ppc_hash_table (data->info);
4157   eh = (struct ppc_link_hash_entry *) h;
4158   fdh = get_fdh (eh, htab);
4159   if (fdh == NULL
4160       && !data->info->relocatable
4161       && (eh->elf.root.type == bfd_link_hash_undefined
4162           || eh->elf.root.type == bfd_link_hash_undefweak)
4163       && eh->elf.ref_regular)
4164     {
4165       /* Make an undefweak function descriptor sym, which is enough to
4166          pull in an --as-needed shared lib, but won't cause link
4167          errors.  Archives are handled elsewhere.  */
4168       fdh = make_fdh (data->info, eh);
4169       if (fdh == NULL)
4170         data->ok = FALSE;
4171       else
4172         fdh->elf.ref_regular = 1;
4173     }
4174   else if (fdh != NULL)
4175     {
4176       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4177       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4178       if (entry_vis < descr_vis)
4179         fdh->elf.other += entry_vis - descr_vis;
4180       else if (entry_vis > descr_vis)
4181         eh->elf.other += descr_vis - entry_vis;
4182
4183       if ((fdh->elf.root.type == bfd_link_hash_defined
4184            || fdh->elf.root.type == bfd_link_hash_defweak)
4185           && eh->elf.root.type == bfd_link_hash_undefined)
4186         {
4187           eh->elf.root.type = bfd_link_hash_undefweak;
4188           eh->was_undefined = 1;
4189           htab->twiddled_syms = 1;
4190         }
4191     }
4192
4193   return TRUE;
4194 }
4195
4196 static bfd_boolean
4197 ppc64_elf_check_directives (bfd *abfd, struct bfd_link_info *info)
4198 {
4199   struct ppc_link_hash_table *htab;
4200   struct add_symbol_adjust_data data;
4201
4202   if (!is_ppc64_elf_target (abfd->xvec))
4203     return TRUE;
4204
4205   if (!ppc64_elf_tdata (abfd)->u.has_dotsym)
4206     return TRUE;
4207   ppc64_elf_tdata (abfd)->u.deleted_section = NULL;
4208
4209   htab = ppc_hash_table (info);
4210   if (!is_ppc64_elf_target (htab->elf.root.creator))
4211     return TRUE;
4212
4213   data.info = info;
4214   data.ok = TRUE;
4215   elf_link_hash_traverse (&htab->elf, add_symbol_adjust, &data);
4216
4217   /* We need to fix the undefs list for any syms we have twiddled to
4218      undef_weak.  */
4219   if (htab->twiddled_syms)
4220     {
4221       bfd_link_repair_undef_list (&htab->elf.root);
4222       htab->twiddled_syms = 0;
4223     }
4224   return data.ok;
4225 }
4226
4227 static bfd_boolean
4228 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4229                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4230 {
4231   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4232   char *local_got_tls_masks;
4233
4234   if (local_got_ents == NULL)
4235     {
4236       bfd_size_type size = symtab_hdr->sh_info;
4237
4238       size *= sizeof (*local_got_ents) + sizeof (*local_got_tls_masks);
4239       local_got_ents = bfd_zalloc (abfd, size);
4240       if (local_got_ents == NULL)
4241         return FALSE;
4242       elf_local_got_ents (abfd) = local_got_ents;
4243     }
4244
4245   if ((tls_type & TLS_EXPLICIT) == 0)
4246     {
4247       struct got_entry *ent;
4248
4249       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4250         if (ent->addend == r_addend
4251             && ent->owner == abfd
4252             && ent->tls_type == tls_type)
4253           break;
4254       if (ent == NULL)
4255         {
4256           bfd_size_type amt = sizeof (*ent);
4257           ent = bfd_alloc (abfd, amt);
4258           if (ent == NULL)
4259             return FALSE;
4260           ent->next = local_got_ents[r_symndx];
4261           ent->addend = r_addend;
4262           ent->owner = abfd;
4263           ent->tls_type = tls_type;
4264           ent->got.refcount = 0;
4265           local_got_ents[r_symndx] = ent;
4266         }
4267       ent->got.refcount += 1;
4268     }
4269
4270   local_got_tls_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
4271   local_got_tls_masks[r_symndx] |= tls_type;
4272   return TRUE;
4273 }
4274
4275 static bfd_boolean
4276 update_plt_info (bfd *abfd, struct ppc_link_hash_entry *eh, bfd_vma addend)
4277 {
4278   struct plt_entry *ent;
4279
4280   for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
4281     if (ent->addend == addend)
4282       break;
4283   if (ent == NULL)
4284     {
4285       bfd_size_type amt = sizeof (*ent);
4286       ent = bfd_alloc (abfd, amt);
4287       if (ent == NULL)
4288         return FALSE;
4289       ent->next = eh->elf.plt.plist;
4290       ent->addend = addend;
4291       ent->plt.refcount = 0;
4292       eh->elf.plt.plist = ent;
4293     }
4294   ent->plt.refcount += 1;
4295   eh->elf.needs_plt = 1;
4296   if (eh->elf.root.root.string[0] == '.'
4297       && eh->elf.root.root.string[1] != '\0')
4298     eh->is_func = 1;
4299   return TRUE;
4300 }
4301
4302 /* Look through the relocs for a section during the first phase, and
4303    calculate needed space in the global offset table, procedure
4304    linkage table, and dynamic reloc sections.  */
4305
4306 static bfd_boolean
4307 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4308                         asection *sec, const Elf_Internal_Rela *relocs)
4309 {
4310   struct ppc_link_hash_table *htab;
4311   Elf_Internal_Shdr *symtab_hdr;
4312   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4313   const Elf_Internal_Rela *rel;
4314   const Elf_Internal_Rela *rel_end;
4315   asection *sreloc;
4316   asection **opd_sym_map;
4317
4318   if (info->relocatable)
4319     return TRUE;
4320
4321   /* Don't do anything special with non-loaded, non-alloced sections.
4322      In particular, any relocs in such sections should not affect GOT
4323      and PLT reference counting (ie. we don't allow them to create GOT
4324      or PLT entries), there's no possibility or desire to optimize TLS
4325      relocs, and there's not much point in propagating relocs to shared
4326      libs that the dynamic linker won't relocate.  */
4327   if ((sec->flags & SEC_ALLOC) == 0)
4328     return TRUE;
4329
4330   htab = ppc_hash_table (info);
4331   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4332
4333   sym_hashes = elf_sym_hashes (abfd);
4334   sym_hashes_end = (sym_hashes
4335                     + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
4336                     - symtab_hdr->sh_info);
4337
4338   sreloc = NULL;
4339   opd_sym_map = NULL;
4340   if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
4341     {
4342       /* Garbage collection needs some extra help with .opd sections.
4343          We don't want to necessarily keep everything referenced by
4344          relocs in .opd, as that would keep all functions.  Instead,
4345          if we reference an .opd symbol (a function descriptor), we
4346          want to keep the function code symbol's section.  This is
4347          easy for global symbols, but for local syms we need to keep
4348          information about the associated function section.  Later, if
4349          edit_opd deletes entries, we'll use this array to adjust
4350          local syms in .opd.  */
4351       union opd_info {
4352         asection *func_section;
4353         long entry_adjust;
4354       };
4355       bfd_size_type amt;
4356
4357       amt = sec->size * sizeof (union opd_info) / 8;
4358       opd_sym_map = bfd_zalloc (abfd, amt);
4359       if (opd_sym_map == NULL)
4360         return FALSE;
4361       ppc64_elf_section_data (sec)->opd.func_sec = opd_sym_map;
4362     }
4363
4364   if (htab->sfpr == NULL
4365       && !create_linkage_sections (htab->elf.dynobj, info))
4366     return FALSE;
4367
4368   rel_end = relocs + sec->reloc_count;
4369   for (rel = relocs; rel < rel_end; rel++)
4370     {
4371       unsigned long r_symndx;
4372       struct elf_link_hash_entry *h;
4373       enum elf_ppc64_reloc_type r_type;
4374       int tls_type = 0;
4375
4376       r_symndx = ELF64_R_SYM (rel->r_info);
4377       if (r_symndx < symtab_hdr->sh_info)
4378         h = NULL;
4379       else
4380         {
4381           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4382           while (h->root.type == bfd_link_hash_indirect
4383                  || h->root.type == bfd_link_hash_warning)
4384             h = (struct elf_link_hash_entry *) h->root.u.i.link;
4385         }
4386
4387       r_type = ELF64_R_TYPE (rel->r_info);
4388       switch (r_type)
4389         {
4390         case R_PPC64_GOT_TLSLD16:
4391         case R_PPC64_GOT_TLSLD16_LO:
4392         case R_PPC64_GOT_TLSLD16_HI:
4393         case R_PPC64_GOT_TLSLD16_HA:
4394           ppc64_tlsld_got (abfd)->refcount += 1;
4395           tls_type = TLS_TLS | TLS_LD;
4396           goto dogottls;
4397
4398         case R_PPC64_GOT_TLSGD16:
4399         case R_PPC64_GOT_TLSGD16_LO:
4400         case R_PPC64_GOT_TLSGD16_HI:
4401         case R_PPC64_GOT_TLSGD16_HA:
4402           tls_type = TLS_TLS | TLS_GD;
4403           goto dogottls;
4404
4405         case R_PPC64_GOT_TPREL16_DS:
4406         case R_PPC64_GOT_TPREL16_LO_DS:
4407         case R_PPC64_GOT_TPREL16_HI:
4408         case R_PPC64_GOT_TPREL16_HA:
4409           if (info->shared)
4410             info->flags |= DF_STATIC_TLS;
4411           tls_type = TLS_TLS | TLS_TPREL;
4412           goto dogottls;
4413
4414         case R_PPC64_GOT_DTPREL16_DS:
4415         case R_PPC64_GOT_DTPREL16_LO_DS:
4416         case R_PPC64_GOT_DTPREL16_HI:
4417         case R_PPC64_GOT_DTPREL16_HA:
4418           tls_type = TLS_TLS | TLS_DTPREL;
4419         dogottls:
4420           sec->has_tls_reloc = 1;
4421           /* Fall thru */
4422
4423         case R_PPC64_GOT16:
4424         case R_PPC64_GOT16_DS:
4425         case R_PPC64_GOT16_HA:
4426         case R_PPC64_GOT16_HI:
4427         case R_PPC64_GOT16_LO:
4428         case R_PPC64_GOT16_LO_DS:
4429           /* This symbol requires a global offset table entry.  */
4430           sec->has_toc_reloc = 1;
4431           if (ppc64_elf_tdata (abfd)->got == NULL
4432               && !create_got_section (abfd, info))
4433             return FALSE;
4434
4435           if (h != NULL)
4436             {
4437               struct ppc_link_hash_entry *eh;
4438               struct got_entry *ent;
4439
4440               eh = (struct ppc_link_hash_entry *) h;
4441               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4442                 if (ent->addend == rel->r_addend
4443                     && ent->owner == abfd
4444                     && ent->tls_type == tls_type)
4445                   break;
4446               if (ent == NULL)
4447                 {
4448                   bfd_size_type amt = sizeof (*ent);
4449                   ent = bfd_alloc (abfd, amt);
4450                   if (ent == NULL)
4451                     return FALSE;
4452                   ent->next = eh->elf.got.glist;
4453                   ent->addend = rel->r_addend;
4454                   ent->owner = abfd;
4455                   ent->tls_type = tls_type;
4456                   ent->got.refcount = 0;
4457                   eh->elf.got.glist = ent;
4458                 }
4459               ent->got.refcount += 1;
4460               eh->tls_mask |= tls_type;
4461             }
4462           else
4463             /* This is a global offset table entry for a local symbol.  */
4464             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4465                                         rel->r_addend, tls_type))
4466               return FALSE;
4467           break;
4468
4469         case R_PPC64_PLT16_HA:
4470         case R_PPC64_PLT16_HI:
4471         case R_PPC64_PLT16_LO:
4472         case R_PPC64_PLT32:
4473         case R_PPC64_PLT64:
4474           /* This symbol requires a procedure linkage table entry.  We
4475              actually build the entry in adjust_dynamic_symbol,
4476              because this might be a case of linking PIC code without
4477              linking in any dynamic objects, in which case we don't
4478              need to generate a procedure linkage table after all.  */
4479           if (h == NULL)
4480             {
4481               /* It does not make sense to have a procedure linkage
4482                  table entry for a local symbol.  */
4483               bfd_set_error (bfd_error_bad_value);
4484               return FALSE;
4485             }
4486           else
4487             if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4488                                   rel->r_addend))
4489               return FALSE;
4490           break;
4491
4492           /* The following relocations don't need to propagate the
4493              relocation if linking a shared object since they are
4494              section relative.  */
4495         case R_PPC64_SECTOFF:
4496         case R_PPC64_SECTOFF_LO:
4497         case R_PPC64_SECTOFF_HI:
4498         case R_PPC64_SECTOFF_HA:
4499         case R_PPC64_SECTOFF_DS:
4500         case R_PPC64_SECTOFF_LO_DS:
4501         case R_PPC64_DTPREL16:
4502         case R_PPC64_DTPREL16_LO:
4503         case R_PPC64_DTPREL16_HI:
4504         case R_PPC64_DTPREL16_HA:
4505         case R_PPC64_DTPREL16_DS:
4506         case R_PPC64_DTPREL16_LO_DS:
4507         case R_PPC64_DTPREL16_HIGHER:
4508         case R_PPC64_DTPREL16_HIGHERA:
4509         case R_PPC64_DTPREL16_HIGHEST:
4510         case R_PPC64_DTPREL16_HIGHESTA:
4511           break;
4512
4513           /* Nor do these.  */
4514         case R_PPC64_TOC16:
4515         case R_PPC64_TOC16_LO:
4516         case R_PPC64_TOC16_HI:
4517         case R_PPC64_TOC16_HA:
4518         case R_PPC64_TOC16_DS:
4519         case R_PPC64_TOC16_LO_DS:
4520           sec->has_toc_reloc = 1;
4521           break;
4522
4523           /* This relocation describes the C++ object vtable hierarchy.
4524              Reconstruct it for later use during GC.  */
4525         case R_PPC64_GNU_VTINHERIT:
4526           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4527             return FALSE;
4528           break;
4529
4530           /* This relocation describes which C++ vtable entries are actually
4531              used.  Record for later use during GC.  */
4532         case R_PPC64_GNU_VTENTRY:
4533           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4534             return FALSE;
4535           break;
4536
4537         case R_PPC64_REL14:
4538         case R_PPC64_REL14_BRTAKEN:
4539         case R_PPC64_REL14_BRNTAKEN:
4540           {
4541             asection *dest = NULL;
4542
4543             /* Heuristic: If jumping outside our section, chances are
4544                we are going to need a stub.  */
4545             if (h != NULL)
4546               {
4547                 /* If the sym is weak it may be overridden later, so
4548                    don't assume we know where a weak sym lives.  */
4549                 if (h->root.type == bfd_link_hash_defined)
4550                   dest = h->root.u.def.section;
4551               }
4552             else
4553               dest = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4554                                                 sec, r_symndx);
4555             if (dest != sec)
4556               htab->has_14bit_branch = 1;
4557           }
4558           /* Fall through.  */
4559
4560         case R_PPC64_REL24:
4561           if (h != NULL)
4562             {
4563               /* We may need a .plt entry if the function this reloc
4564                  refers to is in a shared lib.  */
4565               if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4566                                     rel->r_addend))
4567                 return FALSE;
4568               if (h == &htab->tls_get_addr->elf
4569                   || h == &htab->tls_get_addr_fd->elf)
4570                 sec->has_tls_reloc = 1;
4571               else if (htab->tls_get_addr == NULL
4572                        && !strncmp (h->root.root.string, ".__tls_get_addr", 15)
4573                        && (h->root.root.string[15] == 0
4574                            || h->root.root.string[15] == '@'))
4575                 {
4576                   htab->tls_get_addr = (struct ppc_link_hash_entry *) h;
4577                   sec->has_tls_reloc = 1;
4578                 }
4579               else if (htab->tls_get_addr_fd == NULL
4580                        && !strncmp (h->root.root.string, "__tls_get_addr", 14)
4581                        && (h->root.root.string[14] == 0
4582                            || h->root.root.string[14] == '@'))
4583                 {
4584                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) h;
4585                   sec->has_tls_reloc = 1;
4586                 }
4587             }
4588           break;
4589
4590         case R_PPC64_TPREL64:
4591           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4592           if (info->shared)
4593             info->flags |= DF_STATIC_TLS;
4594           goto dotlstoc;
4595
4596         case R_PPC64_DTPMOD64:
4597           if (rel + 1 < rel_end
4598               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4599               && rel[1].r_offset == rel->r_offset + 8)
4600             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4601           else
4602             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4603           goto dotlstoc;
4604
4605         case R_PPC64_DTPREL64:
4606           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4607           if (rel != relocs
4608               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4609               && rel[-1].r_offset == rel->r_offset - 8)
4610             /* This is the second reloc of a dtpmod, dtprel pair.
4611                Don't mark with TLS_DTPREL.  */
4612             goto dodyn;
4613
4614         dotlstoc:
4615           sec->has_tls_reloc = 1;
4616           if (h != NULL)
4617             {
4618               struct ppc_link_hash_entry *eh;
4619               eh = (struct ppc_link_hash_entry *) h;
4620               eh->tls_mask |= tls_type;
4621             }
4622           else
4623             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4624                                         rel->r_addend, tls_type))
4625               return FALSE;
4626
4627           if (ppc64_elf_section_data (sec)->t_symndx == NULL)
4628             {
4629               /* One extra to simplify get_tls_mask.  */
4630               bfd_size_type amt = sec->size * sizeof (unsigned) / 8 + 1;
4631               ppc64_elf_section_data (sec)->t_symndx = bfd_zalloc (abfd, amt);
4632               if (ppc64_elf_section_data (sec)->t_symndx == NULL)
4633                 return FALSE;
4634             }
4635           BFD_ASSERT (rel->r_offset % 8 == 0);
4636           ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8] = r_symndx;
4637
4638           /* Mark the second slot of a GD or LD entry.
4639              -1 to indicate GD and -2 to indicate LD.  */
4640           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4641             ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -1;
4642           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4643             ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -2;
4644           goto dodyn;
4645
4646         case R_PPC64_TPREL16:
4647         case R_PPC64_TPREL16_LO:
4648         case R_PPC64_TPREL16_HI:
4649         case R_PPC64_TPREL16_HA:
4650         case R_PPC64_TPREL16_DS:
4651         case R_PPC64_TPREL16_LO_DS:
4652         case R_PPC64_TPREL16_HIGHER:
4653         case R_PPC64_TPREL16_HIGHERA:
4654         case R_PPC64_TPREL16_HIGHEST:
4655         case R_PPC64_TPREL16_HIGHESTA:
4656           if (info->shared)
4657             {
4658               info->flags |= DF_STATIC_TLS;
4659               goto dodyn;
4660             }
4661           break;
4662
4663         case R_PPC64_ADDR64:
4664           if (opd_sym_map != NULL
4665               && rel + 1 < rel_end
4666               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
4667             {
4668               if (h != NULL)
4669                 {
4670                   if (h->root.root.string[0] == '.'
4671                       && h->root.root.string[1] != 0
4672                       && get_fdh ((struct ppc_link_hash_entry *) h, htab))
4673                     ;
4674                   else
4675                     ((struct ppc_link_hash_entry *) h)->is_func = 1;
4676                 }
4677               else
4678                 {
4679                   asection *s;
4680
4681                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
4682                                                  r_symndx);
4683                   if (s == NULL)
4684                     return FALSE;
4685                   else if (s != sec)
4686                     opd_sym_map[rel->r_offset / 8] = s;
4687                 }
4688             }
4689           /* Fall through.  */
4690
4691         case R_PPC64_REL30:
4692         case R_PPC64_REL32:
4693         case R_PPC64_REL64:
4694         case R_PPC64_ADDR14:
4695         case R_PPC64_ADDR14_BRNTAKEN:
4696         case R_PPC64_ADDR14_BRTAKEN:
4697         case R_PPC64_ADDR16:
4698         case R_PPC64_ADDR16_DS:
4699         case R_PPC64_ADDR16_HA:
4700         case R_PPC64_ADDR16_HI:
4701         case R_PPC64_ADDR16_HIGHER:
4702         case R_PPC64_ADDR16_HIGHERA:
4703         case R_PPC64_ADDR16_HIGHEST:
4704         case R_PPC64_ADDR16_HIGHESTA:
4705         case R_PPC64_ADDR16_LO:
4706         case R_PPC64_ADDR16_LO_DS:
4707         case R_PPC64_ADDR24:
4708         case R_PPC64_ADDR32:
4709         case R_PPC64_UADDR16:
4710         case R_PPC64_UADDR32:
4711         case R_PPC64_UADDR64:
4712         case R_PPC64_TOC:
4713           if (h != NULL && !info->shared)
4714             /* We may need a copy reloc.  */
4715             h->non_got_ref = 1;
4716
4717           /* Don't propagate .opd relocs.  */
4718           if (NO_OPD_RELOCS && opd_sym_map != NULL)
4719             break;
4720
4721           /* If we are creating a shared library, and this is a reloc
4722              against a global symbol, or a non PC relative reloc
4723              against a local symbol, then we need to copy the reloc
4724              into the shared library.  However, if we are linking with
4725              -Bsymbolic, we do not need to copy a reloc against a
4726              global symbol which is defined in an object we are
4727              including in the link (i.e., DEF_REGULAR is set).  At
4728              this point we have not seen all the input files, so it is
4729              possible that DEF_REGULAR is not set now but will be set
4730              later (it is never cleared).  In case of a weak definition,
4731              DEF_REGULAR may be cleared later by a strong definition in
4732              a shared library.  We account for that possibility below by
4733              storing information in the dyn_relocs field of the hash
4734              table entry.  A similar situation occurs when creating
4735              shared libraries and symbol visibility changes render the
4736              symbol local.
4737
4738              If on the other hand, we are creating an executable, we
4739              may need to keep relocations for symbols satisfied by a
4740              dynamic library if we manage to avoid copy relocs for the
4741              symbol.  */
4742         dodyn:
4743           if ((info->shared
4744                && (MUST_BE_DYN_RELOC (r_type)
4745                    || (h != NULL
4746                        && (! info->symbolic
4747                            || h->root.type == bfd_link_hash_defweak
4748                            || !h->def_regular))))
4749               || (ELIMINATE_COPY_RELOCS
4750                   && !info->shared
4751                   && h != NULL
4752                   && (h->root.type == bfd_link_hash_defweak
4753                       || !h->def_regular)))
4754             {
4755               struct ppc_dyn_relocs *p;
4756               struct ppc_dyn_relocs **head;
4757
4758               /* We must copy these reloc types into the output file.
4759                  Create a reloc section in dynobj and make room for
4760                  this reloc.  */
4761               if (sreloc == NULL)
4762                 {
4763                   const char *name;
4764                   bfd *dynobj;
4765
4766                   name = (bfd_elf_string_from_elf_section
4767                           (abfd,
4768                            elf_elfheader (abfd)->e_shstrndx,
4769                            elf_section_data (sec)->rel_hdr.sh_name));
4770                   if (name == NULL)
4771                     return FALSE;
4772
4773                   if (strncmp (name, ".rela", 5) != 0
4774                       || strcmp (bfd_get_section_name (abfd, sec),
4775                                  name + 5) != 0)
4776                     {
4777                       (*_bfd_error_handler)
4778                         (_("%B: bad relocation section name `%s\'"),
4779                          abfd, name);
4780                       bfd_set_error (bfd_error_bad_value);
4781                     }
4782
4783                   dynobj = htab->elf.dynobj;
4784                   sreloc = bfd_get_section_by_name (dynobj, name);
4785                   if (sreloc == NULL)
4786                     {
4787                       flagword flags;
4788
4789                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
4790                                | SEC_IN_MEMORY | SEC_LINKER_CREATED
4791                                | SEC_ALLOC | SEC_LOAD);
4792                       sreloc = bfd_make_section_with_flags (dynobj,
4793                                                             name,
4794                                                             flags);
4795                       if (sreloc == NULL
4796                           || ! bfd_set_section_alignment (dynobj, sreloc, 3))
4797                         return FALSE;
4798                     }
4799                   elf_section_data (sec)->sreloc = sreloc;
4800                 }
4801
4802               /* If this is a global symbol, we count the number of
4803                  relocations we need for this symbol.  */
4804               if (h != NULL)
4805                 {
4806                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
4807                 }
4808               else
4809                 {
4810                   /* Track dynamic relocs needed for local syms too.
4811                      We really need local syms available to do this
4812                      easily.  Oh well.  */
4813
4814                   asection *s;
4815                   void *vpp;
4816
4817                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4818                                                  sec, r_symndx);
4819                   if (s == NULL)
4820                     return FALSE;
4821
4822                   vpp = &elf_section_data (s)->local_dynrel;
4823                   head = (struct ppc_dyn_relocs **) vpp;
4824                 }
4825
4826               p = *head;
4827               if (p == NULL || p->sec != sec)
4828                 {
4829                   p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4830                   if (p == NULL)
4831                     return FALSE;
4832                   p->next = *head;
4833                   *head = p;
4834                   p->sec = sec;
4835                   p->count = 0;
4836                   p->pc_count = 0;
4837                 }
4838
4839               p->count += 1;
4840               if (!MUST_BE_DYN_RELOC (r_type))
4841                 p->pc_count += 1;
4842             }
4843           break;
4844
4845         default:
4846           break;
4847         }
4848     }
4849
4850   return TRUE;
4851 }
4852
4853 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
4854    of the code entry point, and its section.  */
4855
4856 static bfd_vma
4857 opd_entry_value (asection *opd_sec,
4858                  bfd_vma offset,
4859                  asection **code_sec,
4860                  bfd_vma *code_off)
4861 {
4862   bfd *opd_bfd = opd_sec->owner;
4863   Elf_Internal_Rela *relocs;
4864   Elf_Internal_Rela *lo, *hi, *look;
4865   bfd_vma val;
4866
4867   /* No relocs implies we are linking a --just-symbols object.  */
4868   if (opd_sec->reloc_count == 0)
4869     {
4870       bfd_vma val;
4871
4872       if (!bfd_get_section_contents (opd_bfd, opd_sec, &val, offset, 8))
4873         return (bfd_vma) -1;
4874
4875       if (code_sec != NULL)
4876         {
4877           asection *sec, *likely = NULL;
4878           for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
4879             if (sec->vma <= val
4880                 && (sec->flags & SEC_LOAD) != 0
4881                 && (sec->flags & SEC_ALLOC) != 0)
4882               likely = sec;
4883           if (likely != NULL)
4884             {
4885               *code_sec = likely;
4886               if (code_off != NULL)
4887                 *code_off = val - likely->vma;
4888             }
4889         }
4890       return val;
4891     }
4892
4893   relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
4894   if (relocs == NULL)
4895     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
4896
4897   /* Go find the opd reloc at the sym address.  */
4898   lo = relocs;
4899   BFD_ASSERT (lo != NULL);
4900   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
4901   val = (bfd_vma) -1;
4902   while (lo < hi)
4903     {
4904       look = lo + (hi - lo) / 2;
4905       if (look->r_offset < offset)
4906         lo = look + 1;
4907       else if (look->r_offset > offset)
4908         hi = look;
4909       else
4910         {
4911           Elf_Internal_Shdr *symtab_hdr = &elf_tdata (opd_bfd)->symtab_hdr;
4912           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
4913               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
4914             {
4915               unsigned long symndx = ELF64_R_SYM (look->r_info);
4916               asection *sec;
4917
4918               if (symndx < symtab_hdr->sh_info)
4919                 {
4920                   Elf_Internal_Sym *sym;
4921
4922                   sym = (Elf_Internal_Sym *) symtab_hdr->contents;
4923                   if (sym == NULL)
4924                     {
4925                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
4926                                                   symtab_hdr->sh_info,
4927                                                   0, NULL, NULL, NULL);
4928                       if (sym == NULL)
4929                         break;
4930                       symtab_hdr->contents = (bfd_byte *) sym;
4931                     }
4932
4933                   sym += symndx;
4934                   val = sym->st_value;
4935                   sec = NULL;
4936                   if ((sym->st_shndx != SHN_UNDEF
4937                        && sym->st_shndx < SHN_LORESERVE)
4938                       || sym->st_shndx > SHN_HIRESERVE)
4939                     sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
4940                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
4941                 }
4942               else
4943                 {
4944                   struct elf_link_hash_entry **sym_hashes;
4945                   struct elf_link_hash_entry *rh;
4946
4947                   sym_hashes = elf_sym_hashes (opd_bfd);
4948                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
4949                   while (rh->root.type == bfd_link_hash_indirect
4950                          || rh->root.type == bfd_link_hash_warning)
4951                     rh = ((struct elf_link_hash_entry *) rh->root.u.i.link);
4952                   BFD_ASSERT (rh->root.type == bfd_link_hash_defined
4953                               || rh->root.type == bfd_link_hash_defweak);
4954                   val = rh->root.u.def.value;
4955                   sec = rh->root.u.def.section;
4956                 }
4957               val += look->r_addend;
4958               if (code_off != NULL)
4959                 *code_off = val;
4960               if (code_sec != NULL)
4961                 *code_sec = sec;
4962               if (sec != NULL && sec->output_section != NULL)
4963                 val += sec->output_section->vma + sec->output_offset;
4964             }
4965           break;
4966         }
4967     }
4968
4969   return val;
4970 }
4971
4972 /* Mark sections containing dynamically referenced symbols.  When
4973    building shared libraries, we must assume that any visible symbol is
4974    referenced.  */
4975
4976 static bfd_boolean
4977 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
4978 {
4979   struct bfd_link_info *info = (struct bfd_link_info *) inf;
4980   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
4981
4982   if (eh->elf.root.type == bfd_link_hash_warning)
4983     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4984
4985   /* Dynamic linking info is on the func descriptor sym.  */
4986   if (eh->oh != NULL
4987       && eh->oh->is_func_descriptor
4988       && (eh->oh->elf.root.type == bfd_link_hash_defined
4989           || eh->oh->elf.root.type == bfd_link_hash_defweak))
4990     eh = eh->oh;
4991
4992   if ((eh->elf.root.type == bfd_link_hash_defined
4993        || eh->elf.root.type == bfd_link_hash_defweak)
4994       && (eh->elf.ref_dynamic
4995           || (!info->executable
4996               && eh->elf.def_regular
4997               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4998               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
4999     {
5000       asection *code_sec;
5001
5002       eh->elf.root.u.def.section->flags |= SEC_KEEP;
5003
5004       /* Function descriptor syms cause the associated
5005          function code sym section to be marked.  */
5006       if (eh->is_func_descriptor
5007           && (eh->oh->elf.root.type == bfd_link_hash_defined
5008               || eh->oh->elf.root.type == bfd_link_hash_defweak))
5009         eh->oh->elf.root.u.def.section->flags |= SEC_KEEP;
5010       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5011                && opd_entry_value (eh->elf.root.u.def.section,
5012                                    eh->elf.root.u.def.value,
5013                                    &code_sec, NULL) != (bfd_vma) -1)
5014         code_sec->flags |= SEC_KEEP;
5015     }
5016
5017   return TRUE;
5018 }
5019
5020 /* Return the section that should be marked against GC for a given
5021    relocation.  */
5022
5023 static asection *
5024 ppc64_elf_gc_mark_hook (asection *sec,
5025                         struct bfd_link_info *info,
5026                         Elf_Internal_Rela *rel,
5027                         struct elf_link_hash_entry *h,
5028                         Elf_Internal_Sym *sym)
5029 {
5030   asection *rsec;
5031
5032   /* First mark all our entry sym sections.  */
5033   if (info->gc_sym_list != NULL)
5034     {
5035       struct ppc_link_hash_table *htab = ppc_hash_table (info);
5036       struct bfd_sym_chain *sym = info->gc_sym_list;
5037
5038       info->gc_sym_list = NULL;
5039       for (; sym != NULL; sym = sym->next)
5040         {
5041           struct ppc_link_hash_entry *eh;
5042
5043           eh = (struct ppc_link_hash_entry *)
5044             elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
5045           if (eh == NULL)
5046             continue;
5047           if (eh->elf.root.type != bfd_link_hash_defined
5048               && eh->elf.root.type != bfd_link_hash_defweak)
5049             continue;
5050
5051           if (eh->is_func_descriptor
5052               && (eh->oh->elf.root.type == bfd_link_hash_defined
5053                   || eh->oh->elf.root.type == bfd_link_hash_defweak))
5054             rsec = eh->oh->elf.root.u.def.section;
5055           else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5056                    && opd_entry_value (eh->elf.root.u.def.section,
5057                                        eh->elf.root.u.def.value,
5058                                        &rsec, NULL) != (bfd_vma) -1)
5059             ;
5060           else
5061             continue;
5062
5063           if (!rsec->gc_mark)
5064             _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5065
5066           rsec = eh->elf.root.u.def.section;
5067           if (!rsec->gc_mark)
5068             _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5069         }
5070     }
5071
5072   /* Syms return NULL if we're marking .opd, so we avoid marking all
5073      function sections, as all functions are referenced in .opd.  */
5074   rsec = NULL;
5075   if (get_opd_info (sec) != NULL)
5076     return rsec;
5077
5078   if (h != NULL)
5079     {
5080       enum elf_ppc64_reloc_type r_type;
5081       struct ppc_link_hash_entry *eh;
5082
5083       r_type = ELF64_R_TYPE (rel->r_info);
5084       switch (r_type)
5085         {
5086         case R_PPC64_GNU_VTINHERIT:
5087         case R_PPC64_GNU_VTENTRY:
5088           break;
5089
5090         default:
5091           switch (h->root.type)
5092             {
5093             case bfd_link_hash_defined:
5094             case bfd_link_hash_defweak:
5095               eh = (struct ppc_link_hash_entry *) h;
5096               if (eh->oh != NULL
5097                   && eh->oh->is_func_descriptor
5098                   && (eh->oh->elf.root.type == bfd_link_hash_defined
5099                       || eh->oh->elf.root.type == bfd_link_hash_defweak))
5100                 eh = eh->oh;
5101
5102               /* Function descriptor syms cause the associated
5103                  function code sym section to be marked.  */
5104               if (eh->is_func_descriptor
5105                   && (eh->oh->elf.root.type == bfd_link_hash_defined
5106                       || eh->oh->elf.root.type == bfd_link_hash_defweak))
5107                 {
5108                   /* They also mark their opd section.  */
5109                   if (!eh->elf.root.u.def.section->gc_mark)
5110                     _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5111                                       ppc64_elf_gc_mark_hook);
5112
5113                   rsec = eh->oh->elf.root.u.def.section;
5114                 }
5115               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5116                        && opd_entry_value (eh->elf.root.u.def.section,
5117                                            eh->elf.root.u.def.value,
5118                                            &rsec, NULL) != (bfd_vma) -1)
5119                 {
5120                   if (!eh->elf.root.u.def.section->gc_mark)
5121                     _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5122                                       ppc64_elf_gc_mark_hook);
5123                 }
5124               else
5125                 rsec = h->root.u.def.section;
5126               break;
5127
5128             case bfd_link_hash_common:
5129               rsec = h->root.u.c.p->section;
5130               break;
5131
5132             default:
5133               break;
5134             }
5135         }
5136     }
5137   else
5138     {
5139       asection **opd_sym_section;
5140
5141       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5142       opd_sym_section = get_opd_info (rsec);
5143       if (opd_sym_section != NULL)
5144         {
5145           if (!rsec->gc_mark)
5146             _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5147
5148           rsec = opd_sym_section[(sym->st_value + rel->r_addend) / 8];
5149         }
5150     }
5151
5152   return rsec;
5153 }
5154
5155 /* Update the .got, .plt. and dynamic reloc reference counts for the
5156    section being removed.  */
5157
5158 static bfd_boolean
5159 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5160                          asection *sec, const Elf_Internal_Rela *relocs)
5161 {
5162   struct ppc_link_hash_table *htab;
5163   Elf_Internal_Shdr *symtab_hdr;
5164   struct elf_link_hash_entry **sym_hashes;
5165   struct got_entry **local_got_ents;
5166   const Elf_Internal_Rela *rel, *relend;
5167
5168   if ((sec->flags & SEC_ALLOC) == 0)
5169     return TRUE;
5170
5171   elf_section_data (sec)->local_dynrel = NULL;
5172
5173   htab = ppc_hash_table (info);
5174   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5175   sym_hashes = elf_sym_hashes (abfd);
5176   local_got_ents = elf_local_got_ents (abfd);
5177
5178   relend = relocs + sec->reloc_count;
5179   for (rel = relocs; rel < relend; rel++)
5180     {
5181       unsigned long r_symndx;
5182       enum elf_ppc64_reloc_type r_type;
5183       struct elf_link_hash_entry *h = NULL;
5184       char tls_type = 0;
5185
5186       r_symndx = ELF64_R_SYM (rel->r_info);
5187       r_type = ELF64_R_TYPE (rel->r_info);
5188       if (r_symndx >= symtab_hdr->sh_info)
5189         {
5190           struct ppc_link_hash_entry *eh;
5191           struct ppc_dyn_relocs **pp;
5192           struct ppc_dyn_relocs *p;
5193
5194           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5195           while (h->root.type == bfd_link_hash_indirect
5196                  || h->root.type == bfd_link_hash_warning)
5197             h = (struct elf_link_hash_entry *) h->root.u.i.link;
5198           eh = (struct ppc_link_hash_entry *) h;
5199
5200           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5201             if (p->sec == sec)
5202               {
5203                 /* Everything must go for SEC.  */
5204                 *pp = p->next;
5205                 break;
5206               }
5207         }
5208
5209       switch (r_type)
5210         {
5211         case R_PPC64_GOT_TLSLD16:
5212         case R_PPC64_GOT_TLSLD16_LO:
5213         case R_PPC64_GOT_TLSLD16_HI:
5214         case R_PPC64_GOT_TLSLD16_HA:
5215           ppc64_tlsld_got (abfd)->refcount -= 1;
5216           tls_type = TLS_TLS | TLS_LD;
5217           goto dogot;
5218
5219         case R_PPC64_GOT_TLSGD16:
5220         case R_PPC64_GOT_TLSGD16_LO:
5221         case R_PPC64_GOT_TLSGD16_HI:
5222         case R_PPC64_GOT_TLSGD16_HA:
5223           tls_type = TLS_TLS | TLS_GD;
5224           goto dogot;
5225
5226         case R_PPC64_GOT_TPREL16_DS:
5227         case R_PPC64_GOT_TPREL16_LO_DS:
5228         case R_PPC64_GOT_TPREL16_HI:
5229         case R_PPC64_GOT_TPREL16_HA:
5230           tls_type = TLS_TLS | TLS_TPREL;
5231           goto dogot;
5232
5233         case R_PPC64_GOT_DTPREL16_DS:
5234         case R_PPC64_GOT_DTPREL16_LO_DS:
5235         case R_PPC64_GOT_DTPREL16_HI:
5236         case R_PPC64_GOT_DTPREL16_HA:
5237           tls_type = TLS_TLS | TLS_DTPREL;
5238           goto dogot;
5239
5240         case R_PPC64_GOT16:
5241         case R_PPC64_GOT16_DS:
5242         case R_PPC64_GOT16_HA:
5243         case R_PPC64_GOT16_HI:
5244         case R_PPC64_GOT16_LO:
5245         case R_PPC64_GOT16_LO_DS:
5246         dogot:
5247           {
5248             struct got_entry *ent;
5249
5250             if (h != NULL)
5251               ent = h->got.glist;
5252             else
5253               ent = local_got_ents[r_symndx];
5254
5255             for (; ent != NULL; ent = ent->next)
5256               if (ent->addend == rel->r_addend
5257                   && ent->owner == abfd
5258                   && ent->tls_type == tls_type)
5259                 break;
5260             if (ent == NULL)
5261               abort ();
5262             if (ent->got.refcount > 0)
5263               ent->got.refcount -= 1;
5264           }
5265           break;
5266
5267         case R_PPC64_PLT16_HA:
5268         case R_PPC64_PLT16_HI:
5269         case R_PPC64_PLT16_LO:
5270         case R_PPC64_PLT32:
5271         case R_PPC64_PLT64:
5272         case R_PPC64_REL14:
5273         case R_PPC64_REL14_BRNTAKEN:
5274         case R_PPC64_REL14_BRTAKEN:
5275         case R_PPC64_REL24:
5276           if (h != NULL)
5277             {
5278               struct plt_entry *ent;
5279
5280               for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5281                 if (ent->addend == rel->r_addend)
5282                   break;
5283               if (ent == NULL)
5284                 abort ();
5285               if (ent->plt.refcount > 0)
5286                 ent->plt.refcount -= 1;
5287             }
5288           break;
5289
5290         default:
5291           break;
5292         }
5293     }
5294   return TRUE;
5295 }
5296
5297 /* The maximum size of .sfpr.  */
5298 #define SFPR_MAX (218*4)
5299
5300 struct sfpr_def_parms
5301 {
5302   const char name[12];
5303   unsigned char lo, hi;
5304   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5305   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5306 };
5307
5308 /* Auto-generate _save*, _rest* functions in .sfpr.  */
5309
5310 static unsigned int
5311 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5312 {
5313   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5314   unsigned int i;
5315   size_t len = strlen (parm->name);
5316   bfd_boolean writing = FALSE;
5317   char sym[16];
5318
5319   memcpy (sym, parm->name, len);
5320   sym[len + 2] = 0;
5321
5322   for (i = parm->lo; i <= parm->hi; i++)
5323     {
5324       struct elf_link_hash_entry *h;
5325
5326       sym[len + 0] = i / 10 + '0';
5327       sym[len + 1] = i % 10 + '0';
5328       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
5329       if (h != NULL
5330           && !h->def_regular)
5331         {
5332           h->root.type = bfd_link_hash_defined;
5333           h->root.u.def.section = htab->sfpr;
5334           h->root.u.def.value = htab->sfpr->size;
5335           h->type = STT_FUNC;
5336           h->def_regular = 1;
5337           _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
5338           writing = TRUE;
5339           if (htab->sfpr->contents == NULL)
5340             {
5341               htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5342               if (htab->sfpr->contents == NULL)
5343                 return FALSE;
5344             }
5345         }
5346       if (writing)
5347         {
5348           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5349           if (i != parm->hi)
5350             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5351           else
5352             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5353           htab->sfpr->size = p - htab->sfpr->contents;
5354         }
5355     }
5356
5357   return TRUE;
5358 }
5359
5360 static bfd_byte *
5361 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5362 {
5363   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5364   return p + 4;
5365 }
5366
5367 static bfd_byte *
5368 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5369 {
5370   p = savegpr0 (abfd, p, r);
5371   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5372   p = p + 4;
5373   bfd_put_32 (abfd, BLR, p);
5374   return p + 4;
5375 }
5376
5377 static bfd_byte *
5378 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5379 {
5380   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5381   return p + 4;
5382 }
5383
5384 static bfd_byte *
5385 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5386 {
5387   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5388   p = p + 4;
5389   p = restgpr0 (abfd, p, r);
5390   bfd_put_32 (abfd, MTLR_R0, p);
5391   p = p + 4;
5392   if (r == 29)
5393     {
5394       p = restgpr0 (abfd, p, 30);
5395       p = restgpr0 (abfd, p, 31);
5396     }
5397   bfd_put_32 (abfd, BLR, p);
5398   return p + 4;
5399 }
5400
5401 static bfd_byte *
5402 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5403 {
5404   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5405   return p + 4;
5406 }
5407
5408 static bfd_byte *
5409 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5410 {
5411   p = savegpr1 (abfd, p, r);
5412   bfd_put_32 (abfd, BLR, p);
5413   return p + 4;
5414 }
5415
5416 static bfd_byte *
5417 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5418 {
5419   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5420   return p + 4;
5421 }
5422
5423 static bfd_byte *
5424 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5425 {
5426   p = restgpr1 (abfd, p, r);
5427   bfd_put_32 (abfd, BLR, p);
5428   return p + 4;
5429 }
5430
5431 static bfd_byte *
5432 savefpr (bfd *abfd, bfd_byte *p, int r)
5433 {
5434   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5435   return p + 4;
5436 }
5437
5438 static bfd_byte *
5439 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5440 {
5441   p = savefpr (abfd, p, r);
5442   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5443   p = p + 4;
5444   bfd_put_32 (abfd, BLR, p);
5445   return p + 4;
5446 }
5447
5448 static bfd_byte *
5449 restfpr (bfd *abfd, bfd_byte *p, int r)
5450 {
5451   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5452   return p + 4;
5453 }
5454
5455 static bfd_byte *
5456 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5457 {
5458   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5459   p = p + 4;
5460   p = restfpr (abfd, p, r);
5461   bfd_put_32 (abfd, MTLR_R0, p);
5462   p = p + 4;
5463   if (r == 29)
5464     {
5465       p = restfpr (abfd, p, 30);
5466       p = restfpr (abfd, p, 31);
5467     }
5468   bfd_put_32 (abfd, BLR, p);
5469   return p + 4;
5470 }
5471
5472 static bfd_byte *
5473 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5474 {
5475   p = savefpr (abfd, p, r);
5476   bfd_put_32 (abfd, BLR, p);
5477   return p + 4;
5478 }
5479
5480 static bfd_byte *
5481 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5482 {
5483   p = restfpr (abfd, p, r);
5484   bfd_put_32 (abfd, BLR, p);
5485   return p + 4;
5486 }
5487
5488 static bfd_byte *
5489 savevr (bfd *abfd, bfd_byte *p, int r)
5490 {
5491   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5492   p = p + 4;
5493   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5494   return p + 4;
5495 }
5496
5497 static bfd_byte *
5498 savevr_tail (bfd *abfd, bfd_byte *p, int r)
5499 {
5500   p = savevr (abfd, p, r);
5501   bfd_put_32 (abfd, BLR, p);
5502   return p + 4;
5503 }
5504
5505 static bfd_byte *
5506 restvr (bfd *abfd, bfd_byte *p, int r)
5507 {
5508   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5509   p = p + 4;
5510   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5511   return p + 4;
5512 }
5513
5514 static bfd_byte *
5515 restvr_tail (bfd *abfd, bfd_byte *p, int r)
5516 {
5517   p = restvr (abfd, p, r);
5518   bfd_put_32 (abfd, BLR, p);
5519   return p + 4;
5520 }
5521
5522 /* Called via elf_link_hash_traverse to transfer dynamic linking
5523    information on function code symbol entries to their corresponding
5524    function descriptor symbol entries.  */
5525
5526 static bfd_boolean
5527 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5528 {
5529   struct bfd_link_info *info;
5530   struct ppc_link_hash_table *htab;
5531   struct plt_entry *ent;
5532   struct ppc_link_hash_entry *fh;
5533   struct ppc_link_hash_entry *fdh;
5534   bfd_boolean force_local;
5535
5536   fh = (struct ppc_link_hash_entry *) h;
5537   if (fh->elf.root.type == bfd_link_hash_indirect)
5538     return TRUE;
5539
5540   if (fh->elf.root.type == bfd_link_hash_warning)
5541     fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
5542
5543   info = inf;
5544   htab = ppc_hash_table (info);
5545
5546   /* Resolve undefined references to dot-symbols as the value
5547      in the function descriptor, if we have one in a regular object.
5548      This is to satisfy cases like ".quad .foo".  Calls to functions
5549      in dynamic objects are handled elsewhere.  */
5550   if (fh->elf.root.type == bfd_link_hash_undefweak
5551       && fh->was_undefined
5552       && (fh->oh->elf.root.type == bfd_link_hash_defined
5553           || fh->oh->elf.root.type == bfd_link_hash_defweak)
5554       && get_opd_info (fh->oh->elf.root.u.def.section) != NULL
5555       && opd_entry_value (fh->oh->elf.root.u.def.section,
5556                           fh->oh->elf.root.u.def.value,
5557                           &fh->elf.root.u.def.section,
5558                           &fh->elf.root.u.def.value) != (bfd_vma) -1)
5559     {
5560       fh->elf.root.type = fh->oh->elf.root.type;
5561       fh->elf.forced_local = 1;
5562     }
5563
5564   /* If this is a function code symbol, transfer dynamic linking
5565      information to the function descriptor symbol.  */
5566   if (!fh->is_func)
5567     return TRUE;
5568
5569   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5570     if (ent->plt.refcount > 0)
5571       break;
5572   if (ent == NULL
5573       || fh->elf.root.root.string[0] != '.'
5574       || fh->elf.root.root.string[1] == '\0')
5575     return TRUE;
5576
5577   /* Find the corresponding function descriptor symbol.  Create it
5578      as undefined if necessary.  */
5579
5580   fdh = get_fdh (fh, htab);
5581   if (fdh != NULL)
5582     while (fdh->elf.root.type == bfd_link_hash_indirect
5583            || fdh->elf.root.type == bfd_link_hash_warning)
5584       fdh = (struct ppc_link_hash_entry *) fdh->elf.root.u.i.link;
5585
5586   if (fdh == NULL
5587       && info->shared
5588       && (fh->elf.root.type == bfd_link_hash_undefined
5589           || fh->elf.root.type == bfd_link_hash_undefweak))
5590     {
5591       fdh = make_fdh (info, fh);
5592       if (fdh == NULL)
5593         return FALSE;
5594     }
5595
5596   /* Fake function descriptors are made undefweak.  If the function
5597      code symbol is strong undefined, make the fake sym the same.
5598      If the function code symbol is defined, then force the fake
5599      descriptor local;  We can't support overriding of symbols in a
5600      shared library on a fake descriptor.  */
5601
5602   if (fdh != NULL
5603       && fdh->fake
5604       && fdh->elf.root.type == bfd_link_hash_undefweak)
5605     {
5606       if (fh->elf.root.type == bfd_link_hash_undefined)
5607         {
5608           fdh->elf.root.type = bfd_link_hash_undefined;
5609           bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
5610         }
5611       else if (fh->elf.root.type == bfd_link_hash_defined
5612                || fh->elf.root.type == bfd_link_hash_defweak)
5613         {
5614           _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
5615         }
5616     }
5617
5618   if (fdh != NULL
5619       && !fdh->elf.forced_local
5620       && (info->shared
5621           || fdh->elf.def_dynamic
5622           || fdh->elf.ref_dynamic
5623           || (fdh->elf.root.type == bfd_link_hash_undefweak
5624               && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
5625     {
5626       if (fdh->elf.dynindx == -1)
5627         if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5628           return FALSE;
5629       fdh->elf.ref_regular |= fh->elf.ref_regular;
5630       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5631       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5632       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
5633       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
5634         {
5635           move_plt_plist (fh, fdh);
5636           fdh->elf.needs_plt = 1;
5637         }
5638       fdh->is_func_descriptor = 1;
5639       fdh->oh = fh;
5640       fh->oh = fdh;
5641     }
5642
5643   /* Now that the info is on the function descriptor, clear the
5644      function code sym info.  Any function code syms for which we
5645      don't have a definition in a regular file, we force local.
5646      This prevents a shared library from exporting syms that have
5647      been imported from another library.  Function code syms that
5648      are really in the library we must leave global to prevent the
5649      linker dragging in a definition from a static library.  */
5650   force_local = (!fh->elf.def_regular
5651                  || fdh == NULL
5652                  || !fdh->elf.def_regular
5653                  || fdh->elf.forced_local);
5654   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5655
5656   return TRUE;
5657 }
5658
5659 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
5660    this hook to a) provide some gcc support functions, and b) transfer
5661    dynamic linking information gathered so far on function code symbol
5662    entries, to their corresponding function descriptor symbol entries.  */
5663
5664 static bfd_boolean
5665 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5666                             struct bfd_link_info *info)
5667 {
5668   struct ppc_link_hash_table *htab;
5669   unsigned int i;
5670   const struct sfpr_def_parms funcs[] =
5671     {
5672       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5673       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5674       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5675       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5676       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5677       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5678       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5679       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5680       { "._savef", 14, 31, savefpr, savefpr1_tail },
5681       { "._restf", 14, 31, restfpr, restfpr1_tail },
5682       { "_savevr_", 20, 31, savevr, savevr_tail },
5683       { "_restvr_", 20, 31, restvr, restvr_tail }
5684     };
5685
5686   htab = ppc_hash_table (info);
5687   if (htab->sfpr == NULL)
5688     /* We don't have any relocs.  */
5689     return TRUE;
5690
5691   /* Provide any missing _save* and _rest* functions.  */
5692   htab->sfpr->size = 0;
5693   for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
5694     if (!sfpr_define (info, &funcs[i]))
5695       return FALSE;
5696
5697   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5698
5699   if (htab->sfpr->size == 0)
5700     htab->sfpr->flags |= SEC_EXCLUDE;
5701
5702   return TRUE;
5703 }
5704
5705 /* Adjust a symbol defined by a dynamic object and referenced by a
5706    regular object.  The current definition is in some section of the
5707    dynamic object, but we're not including those sections.  We have to
5708    change the definition to something the rest of the link can
5709    understand.  */
5710
5711 static bfd_boolean
5712 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
5713                                  struct elf_link_hash_entry *h)
5714 {
5715   struct ppc_link_hash_table *htab;
5716   asection *s;
5717   unsigned int power_of_two;
5718
5719   htab = ppc_hash_table (info);
5720
5721   /* Deal with function syms.  */
5722   if (h->type == STT_FUNC
5723       || h->needs_plt)
5724     {
5725       /* Clear procedure linkage table information for any symbol that
5726          won't need a .plt entry.  */
5727       struct plt_entry *ent;
5728       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5729         if (ent->plt.refcount > 0)
5730           break;
5731       if (ent == NULL
5732           || SYMBOL_CALLS_LOCAL (info, h)
5733           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5734               && h->root.type == bfd_link_hash_undefweak))
5735         {
5736           h->plt.plist = NULL;
5737           h->needs_plt = 0;
5738         }
5739     }
5740   else
5741     h->plt.plist = NULL;
5742
5743   /* If this is a weak symbol, and there is a real definition, the
5744      processor independent code will have arranged for us to see the
5745      real definition first, and we can just use the same value.  */
5746   if (h->u.weakdef != NULL)
5747     {
5748       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
5749                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
5750       h->root.u.def.section = h->u.weakdef->root.u.def.section;
5751       h->root.u.def.value = h->u.weakdef->root.u.def.value;
5752       if (ELIMINATE_COPY_RELOCS)
5753         h->non_got_ref = h->u.weakdef->non_got_ref;
5754       return TRUE;
5755     }
5756
5757   /* If we are creating a shared library, we must presume that the
5758      only references to the symbol are via the global offset table.
5759      For such cases we need not do anything here; the relocations will
5760      be handled correctly by relocate_section.  */
5761   if (info->shared)
5762     return TRUE;
5763
5764   /* If there are no references to this symbol that do not use the
5765      GOT, we don't need to generate a copy reloc.  */
5766   if (!h->non_got_ref)
5767     return TRUE;
5768
5769   if (ELIMINATE_COPY_RELOCS)
5770     {
5771       struct ppc_link_hash_entry * eh;
5772       struct ppc_dyn_relocs *p;
5773
5774       eh = (struct ppc_link_hash_entry *) h;
5775       for (p = eh->dyn_relocs; p != NULL; p = p->next)
5776         {
5777           s = p->sec->output_section;
5778           if (s != NULL && (s->flags & SEC_READONLY) != 0)
5779             break;
5780         }
5781
5782       /* If we didn't find any dynamic relocs in read-only sections, then
5783          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
5784       if (p == NULL)
5785         {
5786           h->non_got_ref = 0;
5787           return TRUE;
5788         }
5789     }
5790
5791   if (h->plt.plist != NULL)
5792     {
5793       /* We should never get here, but unfortunately there are versions
5794          of gcc out there that improperly (for this ABI) put initialized
5795          function pointers, vtable refs and suchlike in read-only
5796          sections.  Allow them to proceed, but warn that this might
5797          break at runtime.  */
5798       (*_bfd_error_handler)
5799         (_("copy reloc against `%s' requires lazy plt linking; "
5800            "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
5801          h->root.root.string);
5802     }
5803
5804   /* This is a reference to a symbol defined by a dynamic object which
5805      is not a function.  */
5806
5807   if (h->size == 0)
5808     {
5809       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
5810                              h->root.root.string);
5811       return TRUE;
5812     }
5813
5814   /* We must allocate the symbol in our .dynbss section, which will
5815      become part of the .bss section of the executable.  There will be
5816      an entry for this symbol in the .dynsym section.  The dynamic
5817      object will contain position independent code, so all references
5818      from the dynamic object to this symbol will go through the global
5819      offset table.  The dynamic linker will use the .dynsym entry to
5820      determine the address it must put in the global offset table, so
5821      both the dynamic object and the regular object will refer to the
5822      same memory location for the variable.  */
5823
5824   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
5825      to copy the initial value out of the dynamic object and into the
5826      runtime process image.  We need to remember the offset into the
5827      .rela.bss section we are going to use.  */
5828   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5829     {
5830       htab->relbss->size += sizeof (Elf64_External_Rela);
5831       h->needs_copy = 1;
5832     }
5833
5834   /* We need to figure out the alignment required for this symbol.  I
5835      have no idea how ELF linkers handle this.  */
5836   power_of_two = bfd_log2 (h->size);
5837   if (power_of_two > 4)
5838     power_of_two = 4;
5839
5840   /* Apply the required alignment.  */
5841   s = htab->dynbss;
5842   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
5843   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
5844     {
5845       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
5846         return FALSE;
5847     }
5848
5849   /* Define the symbol as being at this point in the section.  */
5850   h->root.u.def.section = s;
5851   h->root.u.def.value = s->size;
5852
5853   /* Increment the section size to make room for the symbol.  */
5854   s->size += h->size;
5855
5856   return TRUE;
5857 }
5858
5859 /* If given a function descriptor symbol, hide both the function code
5860    sym and the descriptor.  */
5861 static void
5862 ppc64_elf_hide_symbol (struct bfd_link_info *info,
5863                        struct elf_link_hash_entry *h,
5864                        bfd_boolean force_local)
5865 {
5866   struct ppc_link_hash_entry *eh;
5867   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
5868
5869   eh = (struct ppc_link_hash_entry *) h;
5870   if (eh->is_func_descriptor)
5871     {
5872       struct ppc_link_hash_entry *fh = eh->oh;
5873
5874       if (fh == NULL)
5875         {
5876           const char *p, *q;
5877           struct ppc_link_hash_table *htab;
5878           char save;
5879
5880           /* We aren't supposed to use alloca in BFD because on
5881              systems which do not have alloca the version in libiberty
5882              calls xmalloc, which might cause the program to crash
5883              when it runs out of memory.  This function doesn't have a
5884              return status, so there's no way to gracefully return an
5885              error.  So cheat.  We know that string[-1] can be safely
5886              accessed;  It's either a string in an ELF string table,
5887              or allocated in an objalloc structure.  */
5888
5889           p = eh->elf.root.root.string - 1;
5890           save = *p;
5891           *(char *) p = '.';
5892           htab = ppc_hash_table (info);
5893           fh = (struct ppc_link_hash_entry *)
5894             elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
5895           *(char *) p = save;
5896
5897           /* Unfortunately, if it so happens that the string we were
5898              looking for was allocated immediately before this string,
5899              then we overwrote the string terminator.  That's the only
5900              reason the lookup should fail.  */
5901           if (fh == NULL)
5902             {
5903               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
5904               while (q >= eh->elf.root.root.string && *q == *p)
5905                 --q, --p;
5906               if (q < eh->elf.root.root.string && *p == '.')
5907                 fh = (struct ppc_link_hash_entry *)
5908                   elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
5909             }
5910           if (fh != NULL)
5911             {
5912               eh->oh = fh;
5913               fh->oh = eh;
5914             }
5915         }
5916       if (fh != NULL)
5917         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5918     }
5919 }
5920
5921 static bfd_boolean
5922 get_sym_h (struct elf_link_hash_entry **hp,
5923            Elf_Internal_Sym **symp,
5924            asection **symsecp,
5925            char **tls_maskp,
5926            Elf_Internal_Sym **locsymsp,
5927            unsigned long r_symndx,
5928            bfd *ibfd)
5929 {
5930   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5931
5932   if (r_symndx >= symtab_hdr->sh_info)
5933     {
5934       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
5935       struct elf_link_hash_entry *h;
5936
5937       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5938       while (h->root.type == bfd_link_hash_indirect
5939              || h->root.type == bfd_link_hash_warning)
5940         h = (struct elf_link_hash_entry *) h->root.u.i.link;
5941
5942       if (hp != NULL)
5943         *hp = h;
5944
5945       if (symp != NULL)
5946         *symp = NULL;
5947
5948       if (symsecp != NULL)
5949         {
5950           asection *symsec = NULL;
5951           if (h->root.type == bfd_link_hash_defined
5952               || h->root.type == bfd_link_hash_defweak)
5953             symsec = h->root.u.def.section;
5954           *symsecp = symsec;
5955         }
5956
5957       if (tls_maskp != NULL)
5958         {
5959           struct ppc_link_hash_entry *eh;
5960
5961           eh = (struct ppc_link_hash_entry *) h;
5962           *tls_maskp = &eh->tls_mask;
5963         }
5964     }
5965   else
5966     {
5967       Elf_Internal_Sym *sym;
5968       Elf_Internal_Sym *locsyms = *locsymsp;
5969
5970       if (locsyms == NULL)
5971         {
5972           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
5973           if (locsyms == NULL)
5974             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
5975                                             symtab_hdr->sh_info,
5976                                             0, NULL, NULL, NULL);
5977           if (locsyms == NULL)
5978             return FALSE;
5979           *locsymsp = locsyms;
5980         }
5981       sym = locsyms + r_symndx;
5982
5983       if (hp != NULL)
5984         *hp = NULL;
5985
5986       if (symp != NULL)
5987         *symp = sym;
5988
5989       if (symsecp != NULL)
5990         {
5991           asection *symsec = NULL;
5992           if ((sym->st_shndx != SHN_UNDEF
5993                && sym->st_shndx < SHN_LORESERVE)
5994               || sym->st_shndx > SHN_HIRESERVE)
5995             symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
5996           *symsecp = symsec;
5997         }
5998
5999       if (tls_maskp != NULL)
6000         {
6001           struct got_entry **lgot_ents;
6002           char *tls_mask;
6003
6004           tls_mask = NULL;
6005           lgot_ents = elf_local_got_ents (ibfd);
6006           if (lgot_ents != NULL)
6007             {
6008               char *lgot_masks = (char *) (lgot_ents + symtab_hdr->sh_info);
6009               tls_mask = &lgot_masks[r_symndx];
6010             }
6011           *tls_maskp = tls_mask;
6012         }
6013     }
6014   return TRUE;
6015 }
6016
6017 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
6018    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6019    type suitable for optimization, and 1 otherwise.  */
6020
6021 static int
6022 get_tls_mask (char **tls_maskp, unsigned long *toc_symndx,
6023               Elf_Internal_Sym **locsymsp,
6024               const Elf_Internal_Rela *rel, bfd *ibfd)
6025 {
6026   unsigned long r_symndx;
6027   int next_r;
6028   struct elf_link_hash_entry *h;
6029   Elf_Internal_Sym *sym;
6030   asection *sec;
6031   bfd_vma off;
6032
6033   r_symndx = ELF64_R_SYM (rel->r_info);
6034   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6035     return 0;
6036
6037   if ((*tls_maskp != NULL && **tls_maskp != 0)
6038       || sec == NULL
6039       || ppc64_elf_section_data (sec)->t_symndx == NULL)
6040     return 1;
6041
6042   /* Look inside a TOC section too.  */
6043   if (h != NULL)
6044     {
6045       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6046       off = h->root.u.def.value;
6047     }
6048   else
6049     off = sym->st_value;
6050   off += rel->r_addend;
6051   BFD_ASSERT (off % 8 == 0);
6052   r_symndx = ppc64_elf_section_data (sec)->t_symndx[off / 8];
6053   next_r = ppc64_elf_section_data (sec)->t_symndx[off / 8 + 1];
6054   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6055     return 0;
6056   if (toc_symndx != NULL)
6057     *toc_symndx = r_symndx;
6058   if ((h == NULL
6059        || ((h->root.type == bfd_link_hash_defined
6060             || h->root.type == bfd_link_hash_defweak)
6061            && !h->def_dynamic))
6062       && (next_r == -1 || next_r == -2))
6063     return 1 - next_r;
6064   return 1;
6065 }
6066
6067 /* Adjust all global syms defined in opd sections.  In gcc generated
6068    code for the old ABI, these will already have been done.  */
6069
6070 static bfd_boolean
6071 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6072 {
6073   struct ppc_link_hash_entry *eh;
6074   asection *sym_sec;
6075   long *opd_adjust;
6076
6077   if (h->root.type == bfd_link_hash_indirect)
6078     return TRUE;
6079
6080   if (h->root.type == bfd_link_hash_warning)
6081     h = (struct elf_link_hash_entry *) h->root.u.i.link;
6082
6083   if (h->root.type != bfd_link_hash_defined
6084       && h->root.type != bfd_link_hash_defweak)
6085     return TRUE;
6086
6087   eh = (struct ppc_link_hash_entry *) h;
6088   if (eh->adjust_done)
6089     return TRUE;
6090
6091   sym_sec = eh->elf.root.u.def.section;
6092   opd_adjust = get_opd_info (sym_sec);
6093   if (opd_adjust != NULL)
6094     {
6095       long adjust = opd_adjust[eh->elf.root.u.def.value / 8];
6096       if (adjust == -1)
6097         {
6098           /* This entry has been deleted.  */
6099           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->u.deleted_section;
6100           if (dsec == NULL)
6101             {
6102               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6103                 if (elf_discarded_section (dsec))
6104                   {
6105                     ppc64_elf_tdata (sym_sec->owner)->u.deleted_section = dsec;
6106                     break;
6107                   }
6108             }
6109           eh->elf.root.u.def.value = 0;
6110           eh->elf.root.u.def.section = dsec;
6111         }
6112       else
6113         eh->elf.root.u.def.value += adjust;
6114       eh->adjust_done = 1;
6115     }
6116   return TRUE;
6117 }
6118
6119 /* Handles decrementing dynamic reloc counts for the reloc specified by
6120    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM_SEC
6121    have already been determined.  */
6122
6123 static bfd_boolean
6124 dec_dynrel_count (bfd_vma r_info,
6125                   asection *sec,
6126                   struct bfd_link_info *info,
6127                   Elf_Internal_Sym **local_syms,
6128                   struct elf_link_hash_entry *h,
6129                   asection *sym_sec)
6130 {
6131   enum elf_ppc64_reloc_type r_type;
6132   struct ppc_dyn_relocs *p;
6133   struct ppc_dyn_relocs **pp;
6134
6135   /* Can this reloc be dynamic?  This switch, and later tests here
6136      should be kept in sync with the code in check_relocs.  */
6137   r_type = ELF64_R_TYPE (r_info);
6138   switch (r_type)
6139     {
6140     default:
6141       return TRUE;
6142
6143     case R_PPC64_TPREL16:
6144     case R_PPC64_TPREL16_LO:
6145     case R_PPC64_TPREL16_HI:
6146     case R_PPC64_TPREL16_HA:
6147     case R_PPC64_TPREL16_DS:
6148     case R_PPC64_TPREL16_LO_DS:
6149     case R_PPC64_TPREL16_HIGHER:
6150     case R_PPC64_TPREL16_HIGHERA:
6151     case R_PPC64_TPREL16_HIGHEST:
6152     case R_PPC64_TPREL16_HIGHESTA:
6153       if (!info->shared)
6154         return TRUE;
6155
6156     case R_PPC64_TPREL64:
6157     case R_PPC64_DTPMOD64:
6158     case R_PPC64_DTPREL64:
6159     case R_PPC64_ADDR64:
6160     case R_PPC64_REL30:
6161     case R_PPC64_REL32:
6162     case R_PPC64_REL64:
6163     case R_PPC64_ADDR14:
6164     case R_PPC64_ADDR14_BRNTAKEN:
6165     case R_PPC64_ADDR14_BRTAKEN:
6166     case R_PPC64_ADDR16:
6167     case R_PPC64_ADDR16_DS:
6168     case R_PPC64_ADDR16_HA:
6169     case R_PPC64_ADDR16_HI:
6170     case R_PPC64_ADDR16_HIGHER:
6171     case R_PPC64_ADDR16_HIGHERA:
6172     case R_PPC64_ADDR16_HIGHEST:
6173     case R_PPC64_ADDR16_HIGHESTA:
6174     case R_PPC64_ADDR16_LO:
6175     case R_PPC64_ADDR16_LO_DS:
6176     case R_PPC64_ADDR24:
6177     case R_PPC64_ADDR32:
6178     case R_PPC64_UADDR16:
6179     case R_PPC64_UADDR32:
6180     case R_PPC64_UADDR64:
6181     case R_PPC64_TOC:
6182       break;
6183     }
6184
6185   if (local_syms != NULL)
6186     {
6187       unsigned long r_symndx;
6188       Elf_Internal_Sym *sym;
6189       bfd *ibfd = sec->owner;
6190
6191       r_symndx = ELF64_R_SYM (r_info);
6192       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6193         return FALSE;
6194     }
6195
6196   if ((info->shared
6197        && (MUST_BE_DYN_RELOC (r_type)
6198            || (h != NULL
6199                && (!info->symbolic
6200                    || h->root.type == bfd_link_hash_defweak
6201                    || !h->def_regular))))
6202       || (ELIMINATE_COPY_RELOCS
6203           && !info->shared
6204           && h != NULL
6205           && (h->root.type == bfd_link_hash_defweak
6206               || !h->def_regular)))
6207     ;
6208   else
6209     return TRUE;
6210
6211   if (h != NULL)
6212     pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6213   else
6214     {
6215       if (sym_sec != NULL)
6216         {
6217           void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6218           pp = (struct ppc_dyn_relocs **) vpp;
6219         }
6220       else
6221         {
6222           void *vpp = &elf_section_data (sec)->local_dynrel;
6223           pp = (struct ppc_dyn_relocs **) vpp;
6224         }
6225
6226       /* elf_gc_sweep may have already removed all dyn relocs associated
6227          with local syms for a given section.  Don't report a dynreloc
6228          miscount.  */
6229       if (*pp == NULL)
6230         return TRUE;
6231     }
6232
6233   while ((p = *pp) != NULL)
6234     {
6235       if (p->sec == sec)
6236         {
6237           if (!MUST_BE_DYN_RELOC (r_type))
6238             p->pc_count -= 1;
6239           p->count -= 1;
6240           if (p->count == 0)
6241             *pp = p->next;
6242           return TRUE;
6243         }
6244       pp = &p->next;
6245     }
6246
6247   (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
6248                            sec->owner, sec);
6249   bfd_set_error (bfd_error_bad_value);
6250   return FALSE;
6251 }
6252
6253 /* Remove unused Official Procedure Descriptor entries.  Currently we
6254    only remove those associated with functions in discarded link-once
6255    sections, or weakly defined functions that have been overridden.  It
6256    would be possible to remove many more entries for statically linked
6257    applications.  */
6258
6259 bfd_boolean
6260 ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info,
6261                     bfd_boolean no_opd_opt,
6262                     bfd_boolean non_overlapping)
6263 {
6264   bfd *ibfd;
6265   bfd_boolean some_edited = FALSE;
6266   asection *need_pad = NULL;
6267
6268   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6269     {
6270       asection *sec;
6271       Elf_Internal_Rela *relstart, *rel, *relend;
6272       Elf_Internal_Shdr *symtab_hdr;
6273       Elf_Internal_Sym *local_syms;
6274       struct elf_link_hash_entry **sym_hashes;
6275       bfd_vma offset;
6276       bfd_size_type amt;
6277       long *opd_adjust;
6278       bfd_boolean need_edit, add_aux_fields;
6279       bfd_size_type cnt_16b = 0;
6280
6281       sec = bfd_get_section_by_name (ibfd, ".opd");
6282       if (sec == NULL || sec->size == 0)
6283         continue;
6284
6285       amt = sec->size * sizeof (long) / 8;
6286       opd_adjust = get_opd_info (sec);
6287       if (opd_adjust == NULL)
6288         {
6289           /* check_relocs hasn't been called.  Must be a ld -r link
6290              or --just-symbols object.   */
6291           opd_adjust = bfd_alloc (obfd, amt);
6292           if (opd_adjust == NULL)
6293             return FALSE;
6294           ppc64_elf_section_data (sec)->opd.adjust = opd_adjust;
6295         }
6296       memset (opd_adjust, 0, amt);
6297
6298       if (no_opd_opt)
6299         continue;
6300
6301       if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6302         continue;
6303
6304       if (sec->output_section == bfd_abs_section_ptr)
6305         continue;
6306
6307       /* Look through the section relocs.  */
6308       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6309         continue;
6310
6311       local_syms = NULL;
6312       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
6313       sym_hashes = elf_sym_hashes (ibfd);
6314
6315       /* Read the relocations.  */
6316       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6317                                             info->keep_memory);
6318       if (relstart == NULL)
6319         return FALSE;
6320
6321       /* First run through the relocs to check they are sane, and to
6322          determine whether we need to edit this opd section.  */
6323       need_edit = FALSE;
6324       need_pad = sec;
6325       offset = 0;
6326       relend = relstart + sec->reloc_count;
6327       for (rel = relstart; rel < relend; )
6328         {
6329           enum elf_ppc64_reloc_type r_type;
6330           unsigned long r_symndx;
6331           asection *sym_sec;
6332           struct elf_link_hash_entry *h;
6333           Elf_Internal_Sym *sym;
6334
6335           /* .opd contains a regular array of 16 or 24 byte entries.  We're
6336              only interested in the reloc pointing to a function entry
6337              point.  */
6338           if (rel->r_offset != offset
6339               || rel + 1 >= relend
6340               || (rel + 1)->r_offset != offset + 8)
6341             {
6342               /* If someone messes with .opd alignment then after a
6343                  "ld -r" we might have padding in the middle of .opd.
6344                  Also, there's nothing to prevent someone putting
6345                  something silly in .opd with the assembler.  No .opd
6346                  optimization for them!  */
6347             broken_opd:
6348               (*_bfd_error_handler)
6349                 (_("%B: .opd is not a regular array of opd entries"), ibfd);
6350               need_edit = FALSE;
6351               break;
6352             }
6353
6354           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6355               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6356             {
6357               (*_bfd_error_handler)
6358                 (_("%B: unexpected reloc type %u in .opd section"),
6359                  ibfd, r_type);
6360               need_edit = FALSE;
6361               break;
6362             }
6363
6364           r_symndx = ELF64_R_SYM (rel->r_info);
6365           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6366                           r_symndx, ibfd))
6367             goto error_ret;
6368
6369           if (sym_sec == NULL || sym_sec->owner == NULL)
6370             {
6371               const char *sym_name;
6372               if (h != NULL)
6373                 sym_name = h->root.root.string;
6374               else
6375                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6376                                              sym_sec);
6377
6378               (*_bfd_error_handler)
6379                 (_("%B: undefined sym `%s' in .opd section"),
6380                  ibfd, sym_name);
6381               need_edit = FALSE;
6382               break;
6383             }
6384
6385           /* opd entries are always for functions defined in the
6386              current input bfd.  If the symbol isn't defined in the
6387              input bfd, then we won't be using the function in this
6388              bfd;  It must be defined in a linkonce section in another
6389              bfd, or is weak.  It's also possible that we are
6390              discarding the function due to a linker script /DISCARD/,
6391              which we test for via the output_section.  */
6392           if (sym_sec->owner != ibfd
6393               || sym_sec->output_section == bfd_abs_section_ptr)
6394             need_edit = TRUE;
6395
6396           rel += 2;
6397           if (rel == relend
6398               || (rel + 1 == relend && rel->r_offset == offset + 16))
6399             {
6400               if (sec->size == offset + 24)
6401                 {
6402                   need_pad = NULL;
6403                   break;
6404                 }
6405               if (rel == relend && sec->size == offset + 16)
6406                 {
6407                   cnt_16b++;
6408                   break;
6409                 }
6410               goto broken_opd;
6411             }
6412
6413           if (rel->r_offset == offset + 24)
6414             offset += 24;
6415           else if (rel->r_offset != offset + 16)
6416             goto broken_opd;
6417           else if (rel + 1 < relend
6418                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6419                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6420             {
6421               offset += 16;
6422               cnt_16b++;
6423             }
6424           else if (rel + 2 < relend
6425                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
6426                    && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
6427             {
6428               offset += 24;
6429               rel += 1;
6430             }
6431           else
6432             goto broken_opd;
6433         }
6434
6435       add_aux_fields = non_overlapping && cnt_16b > 0;
6436
6437       if (need_edit || add_aux_fields)
6438         {
6439           Elf_Internal_Rela *write_rel;
6440           bfd_byte *rptr, *wptr;
6441           bfd_byte *new_contents = NULL;
6442           bfd_boolean skip;
6443           long opd_ent_size;
6444
6445           /* This seems a waste of time as input .opd sections are all
6446              zeros as generated by gcc, but I suppose there's no reason
6447              this will always be so.  We might start putting something in
6448              the third word of .opd entries.  */
6449           if ((sec->flags & SEC_IN_MEMORY) == 0)
6450             {
6451               bfd_byte *loc;
6452               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6453                 {
6454                   if (loc != NULL)
6455                     free (loc);
6456                 error_ret:
6457                   if (local_syms != NULL
6458                       && symtab_hdr->contents != (unsigned char *) local_syms)
6459                     free (local_syms);
6460                   if (elf_section_data (sec)->relocs != relstart)
6461                     free (relstart);
6462                   return FALSE;
6463                 }
6464               sec->contents = loc;
6465               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6466             }
6467
6468           elf_section_data (sec)->relocs = relstart;
6469
6470           new_contents = sec->contents;
6471           if (add_aux_fields)
6472             {
6473               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6474               if (new_contents == NULL)
6475                 return FALSE;
6476               need_pad = FALSE;
6477             }
6478           wptr = new_contents;
6479           rptr = sec->contents;
6480
6481           write_rel = relstart;
6482           skip = FALSE;
6483           offset = 0;
6484           opd_ent_size = 0;
6485           for (rel = relstart; rel < relend; rel++)
6486             {
6487               unsigned long r_symndx;
6488               asection *sym_sec;
6489               struct elf_link_hash_entry *h;
6490               Elf_Internal_Sym *sym;
6491
6492               r_symndx = ELF64_R_SYM (rel->r_info);
6493               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6494                               r_symndx, ibfd))
6495                 goto error_ret;
6496
6497               if (rel->r_offset == offset)
6498                 {
6499                   struct ppc_link_hash_entry *fdh = NULL;
6500
6501                   /* See if the .opd entry is full 24 byte or
6502                      16 byte (with fd_aux entry overlapped with next
6503                      fd_func).  */
6504                   opd_ent_size = 24;
6505                   if ((rel + 2 == relend && sec->size == offset + 16)
6506                       || (rel + 3 < relend
6507                           && rel[2].r_offset == offset + 16
6508                           && rel[3].r_offset == offset + 24
6509                           && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
6510                           && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
6511                     opd_ent_size = 16;
6512
6513                   if (h != NULL
6514                       && h->root.root.string[0] == '.')
6515                     {
6516                       fdh = get_fdh ((struct ppc_link_hash_entry *) h,
6517                                      ppc_hash_table (info));
6518                       if (fdh != NULL
6519                           && fdh->elf.root.type != bfd_link_hash_defined
6520                           && fdh->elf.root.type != bfd_link_hash_defweak)
6521                         fdh = NULL;
6522                     }
6523
6524                   skip = (sym_sec->owner != ibfd
6525                           || sym_sec->output_section == bfd_abs_section_ptr);
6526                   if (skip)
6527                     {
6528                       if (fdh != NULL && sym_sec->owner == ibfd)
6529                         {
6530                           /* Arrange for the function descriptor sym
6531                              to be dropped.  */
6532                           fdh->elf.root.u.def.value = 0;
6533                           fdh->elf.root.u.def.section = sym_sec;
6534                         }
6535                       opd_adjust[rel->r_offset / 8] = -1;
6536                     }
6537                   else
6538                     {
6539                       /* We'll be keeping this opd entry.  */
6540
6541                       if (fdh != NULL)
6542                         {
6543                           /* Redefine the function descriptor symbol to
6544                              this location in the opd section.  It is
6545                              necessary to update the value here rather
6546                              than using an array of adjustments as we do
6547                              for local symbols, because various places
6548                              in the generic ELF code use the value
6549                              stored in u.def.value.  */
6550                           fdh->elf.root.u.def.value = wptr - new_contents;
6551                           fdh->adjust_done = 1;
6552                         }
6553
6554                       /* Local syms are a bit tricky.  We could
6555                          tweak them as they can be cached, but
6556                          we'd need to look through the local syms
6557                          for the function descriptor sym which we
6558                          don't have at the moment.  So keep an
6559                          array of adjustments.  */
6560                       opd_adjust[rel->r_offset / 8]
6561                         = (wptr - new_contents) - (rptr - sec->contents);
6562
6563                       if (wptr != rptr)
6564                         memcpy (wptr, rptr, opd_ent_size);
6565                       wptr += opd_ent_size;
6566                       if (add_aux_fields && opd_ent_size == 16)
6567                         {
6568                           memset (wptr, '\0', 8);
6569                           wptr += 8;
6570                         }
6571                     }
6572                   rptr += opd_ent_size;
6573                   offset += opd_ent_size;
6574                 }
6575
6576               if (skip)
6577                 {
6578                   if (!NO_OPD_RELOCS
6579                       && !info->relocatable
6580                       && !dec_dynrel_count (rel->r_info, sec, info,
6581                                             NULL, h, sym_sec))
6582                     goto error_ret;
6583                 }
6584               else
6585                 {
6586                   /* We need to adjust any reloc offsets to point to the
6587                      new opd entries.  While we're at it, we may as well
6588                      remove redundant relocs.  */
6589                   rel->r_offset += opd_adjust[(offset - opd_ent_size) / 8];
6590                   if (write_rel != rel)
6591                     memcpy (write_rel, rel, sizeof (*rel));
6592                   ++write_rel;
6593                 }
6594             }
6595
6596           sec->size = wptr - new_contents;
6597           sec->reloc_count = write_rel - relstart;
6598           if (add_aux_fields)
6599             {
6600               free (sec->contents);
6601               sec->contents = new_contents;
6602             }
6603
6604           /* Fudge the size too, as this is used later in
6605              elf_bfd_final_link if we are emitting relocs.  */
6606           elf_section_data (sec)->rel_hdr.sh_size
6607             = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
6608           BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
6609           some_edited = TRUE;
6610         }
6611       else if (elf_section_data (sec)->relocs != relstart)
6612         free (relstart);
6613
6614       if (local_syms != NULL
6615           && symtab_hdr->contents != (unsigned char *) local_syms)
6616         {
6617           if (!info->keep_memory)
6618             free (local_syms);
6619           else
6620             symtab_hdr->contents = (unsigned char *) local_syms;
6621         }
6622     }
6623
6624   if (some_edited)
6625     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
6626
6627   /* If we are doing a final link and the last .opd entry is just 16 byte
6628      long, add a 8 byte padding after it.  */
6629   if (need_pad != NULL && !info->relocatable)
6630     {
6631       bfd_byte *p;
6632
6633       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
6634         {
6635           BFD_ASSERT (need_pad->size > 0);
6636
6637           p = bfd_malloc (need_pad->size + 8);
6638           if (p == NULL)
6639             return FALSE;
6640
6641           if (! bfd_get_section_contents (need_pad->owner, need_pad,
6642                                           p, 0, need_pad->size))
6643             return FALSE;
6644
6645           need_pad->contents = p;
6646           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6647         }
6648       else
6649         {
6650           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
6651           if (p == NULL)
6652             return FALSE;
6653
6654           need_pad->contents = p;
6655         }
6656
6657       memset (need_pad->contents + need_pad->size, 0, 8);
6658       need_pad->size += 8;
6659     }
6660
6661   return TRUE;
6662 }
6663
6664 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
6665
6666 asection *
6667 ppc64_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
6668 {
6669   struct ppc_link_hash_table *htab;
6670
6671   htab = ppc_hash_table (info);
6672   if (htab->tls_get_addr != NULL)
6673     {
6674       struct ppc_link_hash_entry *h = htab->tls_get_addr;
6675
6676       while (h->elf.root.type == bfd_link_hash_indirect
6677              || h->elf.root.type == bfd_link_hash_warning)
6678         h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
6679
6680       htab->tls_get_addr = h;
6681
6682       if (htab->tls_get_addr_fd == NULL
6683           && h->oh != NULL
6684           && h->oh->is_func_descriptor
6685           && (h->oh->elf.root.type == bfd_link_hash_defined
6686               || h->oh->elf.root.type == bfd_link_hash_defweak))
6687         htab->tls_get_addr_fd = h->oh;
6688     }
6689
6690   if (htab->tls_get_addr_fd != NULL)
6691     {
6692       struct ppc_link_hash_entry *h = htab->tls_get_addr_fd;
6693
6694       while (h->elf.root.type == bfd_link_hash_indirect
6695              || h->elf.root.type == bfd_link_hash_warning)
6696         h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
6697
6698       htab->tls_get_addr_fd = h;
6699     }
6700
6701   return _bfd_elf_tls_setup (obfd, info);
6702 }
6703
6704 /* Run through all the TLS relocs looking for optimization
6705    opportunities.  The linker has been hacked (see ppc64elf.em) to do
6706    a preliminary section layout so that we know the TLS segment
6707    offsets.  We can't optimize earlier because some optimizations need
6708    to know the tp offset, and we need to optimize before allocating
6709    dynamic relocations.  */
6710
6711 bfd_boolean
6712 ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6713 {
6714   bfd *ibfd;
6715   asection *sec;
6716   struct ppc_link_hash_table *htab;
6717
6718   if (info->relocatable || info->shared)
6719     return TRUE;
6720
6721   htab = ppc_hash_table (info);
6722   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6723     {
6724       Elf_Internal_Sym *locsyms = NULL;
6725       asection *toc = bfd_get_section_by_name (ibfd, ".toc");
6726       unsigned char *toc_ref = NULL;
6727
6728       /* Look at all the sections for this file, with TOC last.  */
6729       for (sec = (ibfd->sections == toc && toc && toc->next ? toc->next
6730                   : ibfd->sections);
6731            sec != NULL;
6732            sec = (sec == toc ? NULL
6733                   : sec->next == NULL ? toc
6734                   : sec->next == toc && toc->next ? toc->next
6735                   : sec->next))
6736         if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
6737           {
6738             Elf_Internal_Rela *relstart, *rel, *relend;
6739             int expecting_tls_get_addr;
6740             long toc_ref_index = 0;
6741
6742             /* Read the relocations.  */
6743             relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6744                                                   info->keep_memory);
6745             if (relstart == NULL)
6746               return FALSE;
6747
6748             expecting_tls_get_addr = 0;
6749             relend = relstart + sec->reloc_count;
6750             for (rel = relstart; rel < relend; rel++)
6751               {
6752                 enum elf_ppc64_reloc_type r_type;
6753                 unsigned long r_symndx;
6754                 struct elf_link_hash_entry *h;
6755                 Elf_Internal_Sym *sym;
6756                 asection *sym_sec;
6757                 char *tls_mask;
6758                 char tls_set, tls_clear, tls_type = 0;
6759                 bfd_vma value;
6760                 bfd_boolean ok_tprel, is_local;
6761
6762                 r_symndx = ELF64_R_SYM (rel->r_info);
6763                 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
6764                                 r_symndx, ibfd))
6765                   {
6766                   err_free_rel:
6767                     if (elf_section_data (sec)->relocs != relstart)
6768                       free (relstart);
6769                     if (toc_ref != NULL)
6770                       free (toc_ref);
6771                     if (locsyms != NULL
6772                         && (elf_tdata (ibfd)->symtab_hdr.contents
6773                             != (unsigned char *) locsyms))
6774                       free (locsyms);
6775                     return FALSE;
6776                   }
6777
6778                 if (h != NULL)
6779                   {
6780                     if (h->root.type != bfd_link_hash_defined
6781                         && h->root.type != bfd_link_hash_defweak)
6782                       continue;
6783                     value = h->root.u.def.value;
6784                   }
6785                 else
6786                   /* Symbols referenced by TLS relocs must be of type
6787                      STT_TLS.  So no need for .opd local sym adjust.  */
6788                   value = sym->st_value;
6789
6790                 ok_tprel = FALSE;
6791                 is_local = FALSE;
6792                 if (h == NULL
6793                     || !h->def_dynamic)
6794                   {
6795                     is_local = TRUE;
6796                     value += sym_sec->output_offset;
6797                     value += sym_sec->output_section->vma;
6798                     value -= htab->elf.tls_sec->vma;
6799                     ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
6800                                 < (bfd_vma) 1 << 32);
6801                   }
6802
6803                 r_type = ELF64_R_TYPE (rel->r_info);
6804                 switch (r_type)
6805                   {
6806                   case R_PPC64_GOT_TLSLD16:
6807                   case R_PPC64_GOT_TLSLD16_LO:
6808                   case R_PPC64_GOT_TLSLD16_HI:
6809                   case R_PPC64_GOT_TLSLD16_HA:
6810                     /* These relocs should never be against a symbol
6811                        defined in a shared lib.  Leave them alone if
6812                        that turns out to be the case.  */
6813                     ppc64_tlsld_got (ibfd)->refcount -= 1;
6814                     if (!is_local)
6815                       continue;
6816
6817                     /* LD -> LE */
6818                     tls_set = 0;
6819                     tls_clear = TLS_LD;
6820                     tls_type = TLS_TLS | TLS_LD;
6821                     expecting_tls_get_addr = 1;
6822                     break;
6823
6824                   case R_PPC64_GOT_TLSGD16:
6825                   case R_PPC64_GOT_TLSGD16_LO:
6826                   case R_PPC64_GOT_TLSGD16_HI:
6827                   case R_PPC64_GOT_TLSGD16_HA:
6828                     if (ok_tprel)
6829                       /* GD -> LE */
6830                       tls_set = 0;
6831                     else
6832                       /* GD -> IE */
6833                       tls_set = TLS_TLS | TLS_TPRELGD;
6834                     tls_clear = TLS_GD;
6835                     tls_type = TLS_TLS | TLS_GD;
6836                     expecting_tls_get_addr = 1;
6837                     break;
6838
6839                   case R_PPC64_GOT_TPREL16_DS:
6840                   case R_PPC64_GOT_TPREL16_LO_DS:
6841                   case R_PPC64_GOT_TPREL16_HI:
6842                   case R_PPC64_GOT_TPREL16_HA:
6843                     expecting_tls_get_addr = 0;
6844                     if (ok_tprel)
6845                       {
6846                         /* IE -> LE */
6847                         tls_set = 0;
6848                         tls_clear = TLS_TPREL;
6849                         tls_type = TLS_TLS | TLS_TPREL;
6850                         break;
6851                       }
6852                     else
6853                       continue;
6854
6855                   case R_PPC64_REL14:
6856                   case R_PPC64_REL14_BRTAKEN:
6857                   case R_PPC64_REL14_BRNTAKEN:
6858                   case R_PPC64_REL24:
6859                     if (h != NULL
6860                         && (h == &htab->tls_get_addr->elf
6861                             || h == &htab->tls_get_addr_fd->elf))
6862                       {
6863                         if (!expecting_tls_get_addr
6864                             && rel != relstart
6865                             && ((ELF64_R_TYPE (rel[-1].r_info)
6866                                  == R_PPC64_TOC16)
6867                                 || (ELF64_R_TYPE (rel[-1].r_info)
6868                                     == R_PPC64_TOC16_LO)))
6869                           {
6870                             /* Check for toc tls entries.  */
6871                             char *toc_tls;
6872                             int retval;
6873
6874                             retval = get_tls_mask (&toc_tls, NULL, &locsyms,
6875                                                    rel - 1, ibfd);
6876                             if (retval == 0)
6877                               goto err_free_rel;
6878                             if (retval > 1 && toc_tls != NULL)
6879                               {
6880                                 expecting_tls_get_addr = 1;
6881                                 if (toc_ref != NULL)
6882                                   toc_ref[toc_ref_index] = 1;
6883                               }
6884                           }
6885
6886                         if (expecting_tls_get_addr)
6887                           {
6888                             struct plt_entry *ent;
6889                             for (ent = h->plt.plist; ent; ent = ent->next)
6890                               if (ent->addend == 0)
6891                                 {
6892                                   if (ent->plt.refcount > 0)
6893                                     ent->plt.refcount -= 1;
6894                                   break;
6895                                 }
6896                           }
6897                       }
6898                     expecting_tls_get_addr = 0;
6899                     continue;
6900
6901                   case R_PPC64_TOC16:
6902                   case R_PPC64_TOC16_LO:
6903                   case R_PPC64_TLS:
6904                     expecting_tls_get_addr = 0;
6905                     if (sym_sec == toc && toc != NULL)
6906                       {
6907                         /* Mark this toc entry as referenced by a TLS
6908                            code sequence.  We can do that now in the
6909                            case of R_PPC64_TLS, and after checking for
6910                            tls_get_addr for the TOC16 relocs.  */
6911                         if (toc_ref == NULL)
6912                           {
6913                             toc_ref = bfd_zmalloc (toc->size / 8);
6914                             if (toc_ref == NULL)
6915                               goto err_free_rel;
6916                           }
6917                         if (h != NULL)
6918                           value = h->root.u.def.value;
6919                         else
6920                           value = sym->st_value;
6921                         value += rel->r_addend;
6922                         BFD_ASSERT (value < toc->size && value % 8 == 0);
6923                         toc_ref_index = value / 8;
6924                         if (r_type == R_PPC64_TLS)
6925                           toc_ref[toc_ref_index] = 1;
6926                       }
6927                     continue;
6928
6929                   case R_PPC64_TPREL64:
6930                     expecting_tls_get_addr = 0;
6931                     if (sec != toc
6932                         || toc_ref == NULL
6933                         || !toc_ref[rel->r_offset / 8])
6934                       continue;
6935                     if (ok_tprel)
6936                       {
6937                         /* IE -> LE */
6938                         tls_set = TLS_EXPLICIT;
6939                         tls_clear = TLS_TPREL;
6940                         break;
6941                       }
6942                     else
6943                       continue;
6944
6945                   case R_PPC64_DTPMOD64:
6946                     expecting_tls_get_addr = 0;
6947                     if (sec != toc
6948                         || toc_ref == NULL
6949                         || !toc_ref[rel->r_offset / 8])
6950                       continue;
6951                     if (rel + 1 < relend
6952                         && (rel[1].r_info
6953                             == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
6954                         && rel[1].r_offset == rel->r_offset + 8)
6955                       {
6956                         if (ok_tprel)
6957                           /* GD -> LE */
6958                           tls_set = TLS_EXPLICIT | TLS_GD;
6959                         else
6960                           /* GD -> IE */
6961                           tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
6962                         tls_clear = TLS_GD;
6963                       }
6964                     else
6965                       {
6966                         if (!is_local)
6967                           continue;
6968
6969                         /* LD -> LE */
6970                         tls_set = TLS_EXPLICIT;
6971                         tls_clear = TLS_LD;
6972                       }
6973                     break;
6974
6975                   default:
6976                     expecting_tls_get_addr = 0;
6977                     continue;
6978                   }
6979
6980                 if ((tls_set & TLS_EXPLICIT) == 0)
6981                   {
6982                     struct got_entry *ent;
6983
6984                     /* Adjust got entry for this reloc.  */
6985                     if (h != NULL)
6986                       ent = h->got.glist;
6987                     else
6988                       ent = elf_local_got_ents (ibfd)[r_symndx];
6989
6990                     for (; ent != NULL; ent = ent->next)
6991                       if (ent->addend == rel->r_addend
6992                           && ent->owner == ibfd
6993                           && ent->tls_type == tls_type)
6994                         break;
6995                     if (ent == NULL)
6996                       abort ();
6997
6998                     if (tls_set == 0)
6999                       {
7000                         /* We managed to get rid of a got entry.  */
7001                         if (ent->got.refcount > 0)
7002                           ent->got.refcount -= 1;
7003                       }
7004                   }
7005                 else
7006                   {
7007                     /* If we got rid of a DTPMOD/DTPREL reloc pair then
7008                        we'll lose one or two dyn relocs.  */
7009                     if (!dec_dynrel_count (rel->r_info, sec, info,
7010                                            NULL, h, sym_sec))
7011                       return FALSE;
7012
7013                     if (tls_set == (TLS_EXPLICIT | TLS_GD))
7014                       {
7015                         if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7016                                                NULL, h, sym_sec))
7017                           return FALSE;
7018                       }
7019                   }
7020
7021                 *tls_mask |= tls_set;
7022                 *tls_mask &= ~tls_clear;
7023               }
7024
7025             if (elf_section_data (sec)->relocs != relstart)
7026               free (relstart);
7027           }
7028
7029       if (toc_ref != NULL)
7030         free (toc_ref);
7031
7032       if (locsyms != NULL
7033           && (elf_tdata (ibfd)->symtab_hdr.contents
7034               != (unsigned char *) locsyms))
7035         {
7036           if (!info->keep_memory)
7037             free (locsyms);
7038           else
7039             elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
7040         }
7041     }
7042   return TRUE;
7043 }
7044
7045 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7046    the values of any global symbols in a toc section that has been
7047    edited.  Globals in toc sections should be a rarity, so this function
7048    sets a flag if any are found in toc sections other than the one just
7049    edited, so that futher hash table traversals can be avoided.  */
7050
7051 struct adjust_toc_info
7052 {
7053   asection *toc;
7054   unsigned long *skip;
7055   bfd_boolean global_toc_syms;
7056 };
7057
7058 static bfd_boolean
7059 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7060 {
7061   struct ppc_link_hash_entry *eh;
7062   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7063
7064   if (h->root.type == bfd_link_hash_indirect)
7065     return TRUE;
7066
7067   if (h->root.type == bfd_link_hash_warning)
7068     h = (struct elf_link_hash_entry *) h->root.u.i.link;
7069
7070   if (h->root.type != bfd_link_hash_defined
7071       && h->root.type != bfd_link_hash_defweak)
7072     return TRUE;
7073
7074   eh = (struct ppc_link_hash_entry *) h;
7075   if (eh->adjust_done)
7076     return TRUE;
7077
7078   if (eh->elf.root.u.def.section == toc_inf->toc)
7079     {
7080       unsigned long skip = toc_inf->skip[eh->elf.root.u.def.value >> 3];
7081       if (skip != (unsigned long) -1)
7082         eh->elf.root.u.def.value -= skip;
7083       else
7084         {
7085           (*_bfd_error_handler)
7086             (_("%s defined in removed toc entry"), eh->elf.root.root.string);
7087           eh->elf.root.u.def.section = &bfd_abs_section;
7088           eh->elf.root.u.def.value = 0;
7089         }
7090       eh->adjust_done = 1;
7091     }
7092   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7093     toc_inf->global_toc_syms = TRUE;
7094
7095   return TRUE;
7096 }
7097
7098 /* Examine all relocs referencing .toc sections in order to remove
7099    unused .toc entries.  */
7100
7101 bfd_boolean
7102 ppc64_elf_edit_toc (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
7103 {
7104   bfd *ibfd;
7105   struct adjust_toc_info toc_inf;
7106
7107   toc_inf.global_toc_syms = TRUE;
7108   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7109     {
7110       asection *toc, *sec;
7111       Elf_Internal_Shdr *symtab_hdr;
7112       Elf_Internal_Sym *local_syms;
7113       struct elf_link_hash_entry **sym_hashes;
7114       Elf_Internal_Rela *relstart, *rel;
7115       unsigned long *skip, *drop;
7116       unsigned char *used;
7117       unsigned char *keep, last, some_unused;
7118
7119       toc = bfd_get_section_by_name (ibfd, ".toc");
7120       if (toc == NULL
7121           || toc->size == 0
7122           || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7123           || elf_discarded_section (toc))
7124         continue;
7125
7126       local_syms = NULL;
7127       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7128       sym_hashes = elf_sym_hashes (ibfd);
7129
7130       /* Look at sections dropped from the final link.  */
7131       skip = NULL;
7132       relstart = NULL;
7133       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7134         {
7135           if (sec->reloc_count == 0
7136               || !elf_discarded_section (sec)
7137               || get_opd_info (sec)
7138               || (sec->flags & SEC_ALLOC) == 0
7139               || (sec->flags & SEC_DEBUGGING) != 0)
7140             continue;
7141
7142           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7143           if (relstart == NULL)
7144             goto error_ret;
7145
7146           /* Run through the relocs to see which toc entries might be
7147              unused.  */
7148           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7149             {
7150               enum elf_ppc64_reloc_type r_type;
7151               unsigned long r_symndx;
7152               asection *sym_sec;
7153               struct elf_link_hash_entry *h;
7154               Elf_Internal_Sym *sym;
7155               bfd_vma val;
7156
7157               r_type = ELF64_R_TYPE (rel->r_info);
7158               switch (r_type)
7159                 {
7160                 default:
7161                   continue;
7162
7163                 case R_PPC64_TOC16:
7164                 case R_PPC64_TOC16_LO:
7165                 case R_PPC64_TOC16_HI:
7166                 case R_PPC64_TOC16_HA:
7167                 case R_PPC64_TOC16_DS:
7168                 case R_PPC64_TOC16_LO_DS:
7169                   break;
7170                 }
7171
7172               r_symndx = ELF64_R_SYM (rel->r_info);
7173               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7174                               r_symndx, ibfd))
7175                 goto error_ret;
7176
7177               if (sym_sec != toc)
7178                 continue;
7179
7180               if (h != NULL)
7181                 val = h->root.u.def.value;
7182               else
7183                 val = sym->st_value;
7184               val += rel->r_addend;
7185
7186               if (val >= toc->size)
7187                 continue;
7188
7189               /* Anything in the toc ought to be aligned to 8 bytes.
7190                  If not, don't mark as unused.  */
7191               if (val & 7)
7192                 continue;
7193
7194               if (skip == NULL)
7195                 {
7196                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 7) / 8);
7197                   if (skip == NULL)
7198                     goto error_ret;
7199                 }
7200
7201               skip[val >> 3] = 1;
7202             }
7203
7204           if (elf_section_data (sec)->relocs != relstart)
7205             free (relstart);
7206         }
7207
7208       if (skip == NULL)
7209         continue;
7210
7211       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
7212       if (used == NULL)
7213         {
7214         error_ret:
7215           if (local_syms != NULL
7216               && symtab_hdr->contents != (unsigned char *) local_syms)
7217             free (local_syms);
7218           if (sec != NULL
7219               && relstart != NULL
7220               && elf_section_data (sec)->relocs != relstart)
7221             free (relstart);
7222           if (skip != NULL)
7223             free (skip);
7224           return FALSE;
7225         }
7226
7227       /* Now check all kept sections that might reference the toc.
7228          Check the toc itself last.  */
7229       for (sec = (ibfd->sections == toc && toc->next ? toc->next
7230                   : ibfd->sections);
7231            sec != NULL;
7232            sec = (sec == toc ? NULL
7233                   : sec->next == NULL ? toc
7234                   : sec->next == toc && toc->next ? toc->next
7235                   : sec->next))
7236         {
7237           int repeat;
7238
7239           if (sec->reloc_count == 0
7240               || elf_discarded_section (sec)
7241               || get_opd_info (sec)
7242               || (sec->flags & SEC_ALLOC) == 0
7243               || (sec->flags & SEC_DEBUGGING) != 0)
7244             continue;
7245
7246           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, TRUE);
7247           if (relstart == NULL)
7248             goto error_ret;
7249
7250           /* Mark toc entries referenced as used.  */
7251           repeat = 0;
7252           do
7253             for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7254               {
7255                 enum elf_ppc64_reloc_type r_type;
7256                 unsigned long r_symndx;
7257                 asection *sym_sec;
7258                 struct elf_link_hash_entry *h;
7259                 Elf_Internal_Sym *sym;
7260                 bfd_vma val;
7261
7262                 r_type = ELF64_R_TYPE (rel->r_info);
7263                 switch (r_type)
7264                   {
7265                   case R_PPC64_TOC16:
7266                   case R_PPC64_TOC16_LO:
7267                   case R_PPC64_TOC16_HI:
7268                   case R_PPC64_TOC16_HA:
7269                   case R_PPC64_TOC16_DS:
7270                   case R_PPC64_TOC16_LO_DS:
7271                     /* In case we're taking addresses of toc entries.  */
7272                   case R_PPC64_ADDR64:
7273                     break;
7274
7275                   default:
7276                     continue;
7277                   }
7278
7279                 r_symndx = ELF64_R_SYM (rel->r_info);
7280                 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7281                                 r_symndx, ibfd))
7282                   {
7283                     free (used);
7284                     goto error_ret;
7285                   }
7286
7287                 if (sym_sec != toc)
7288                   continue;
7289
7290                 if (h != NULL)
7291                   val = h->root.u.def.value;
7292                 else
7293                   val = sym->st_value;
7294                 val += rel->r_addend;
7295
7296                 if (val >= toc->size)
7297                   continue;
7298
7299                 /* For the toc section, we only mark as used if
7300                    this entry itself isn't unused.  */
7301                 if (sec == toc
7302                     && !used[val >> 3]
7303                     && (used[rel->r_offset >> 3]
7304                         || !skip[rel->r_offset >> 3]))
7305                   /* Do all the relocs again, to catch reference
7306                      chains.  */
7307                   repeat = 1;
7308
7309                 used[val >> 3] = 1;
7310               }
7311           while (repeat);
7312         }
7313
7314       /* Merge the used and skip arrays.  Assume that TOC
7315          doublewords not appearing as either used or unused belong
7316          to to an entry more than one doubleword in size.  */
7317       for (drop = skip, keep = used, last = 0, some_unused = 0;
7318            drop < skip + (toc->size + 7) / 8;
7319            ++drop, ++keep)
7320         {
7321           if (*keep)
7322             {
7323               *drop = 0;
7324               last = 0;
7325             }
7326           else if (*drop)
7327             {
7328               some_unused = 1;
7329               last = 1;
7330             }
7331           else
7332             *drop = last;
7333         }
7334
7335       free (used);
7336
7337       if (some_unused)
7338         {
7339           bfd_byte *contents, *src;
7340           unsigned long off;
7341
7342           /* Shuffle the toc contents, and at the same time convert the
7343              skip array from booleans into offsets.  */
7344           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
7345             goto error_ret;
7346
7347           elf_section_data (toc)->this_hdr.contents = contents;
7348
7349           for (src = contents, off = 0, drop = skip;
7350                src < contents + toc->size;
7351                src += 8, ++drop)
7352             {
7353               if (*drop)
7354                 {
7355                   *drop = (unsigned long) -1;
7356                   off += 8;
7357                 }
7358               else if (off != 0)
7359                 {
7360                   *drop = off;
7361                   memcpy (src - off, src, 8);
7362                 }
7363             }
7364           toc->rawsize = toc->size;
7365           toc->size = src - contents - off;
7366
7367           if (toc->reloc_count != 0)
7368             {
7369               Elf_Internal_Rela *wrel;
7370               bfd_size_type sz;
7371
7372               /* Read toc relocs.  */
7373               relstart = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
7374                                                     TRUE);
7375               if (relstart == NULL)
7376                 goto error_ret;
7377
7378               /* Remove unused toc relocs, and adjust those we keep.  */
7379               wrel = relstart;
7380               for (rel = relstart; rel < relstart + toc->reloc_count; ++rel)
7381                 if (skip[rel->r_offset >> 3] != (unsigned long) -1)
7382                   {
7383                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
7384                     wrel->r_info = rel->r_info;
7385                     wrel->r_addend = rel->r_addend;
7386                     ++wrel;
7387                   }
7388                 else if (!dec_dynrel_count (rel->r_info, toc, info,
7389                                             &local_syms, NULL, NULL))
7390                   goto error_ret;
7391
7392               toc->reloc_count = wrel - relstart;
7393               sz = elf_section_data (toc)->rel_hdr.sh_entsize;
7394               elf_section_data (toc)->rel_hdr.sh_size = toc->reloc_count * sz;
7395               BFD_ASSERT (elf_section_data (toc)->rel_hdr2 == NULL);
7396             }
7397
7398           /* Adjust addends for relocs against the toc section sym.  */
7399           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7400             {
7401               if (sec->reloc_count == 0
7402                   || elf_discarded_section (sec))
7403                 continue;
7404
7405               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7406                                                     TRUE);
7407               if (relstart == NULL)
7408                 goto error_ret;
7409
7410               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7411                 {
7412                   enum elf_ppc64_reloc_type r_type;
7413                   unsigned long r_symndx;
7414                   asection *sym_sec;
7415                   struct elf_link_hash_entry *h;
7416                   Elf_Internal_Sym *sym;
7417
7418                   r_type = ELF64_R_TYPE (rel->r_info);
7419                   switch (r_type)
7420                     {
7421                     default:
7422                       continue;
7423
7424                     case R_PPC64_TOC16:
7425                     case R_PPC64_TOC16_LO:
7426                     case R_PPC64_TOC16_HI:
7427                     case R_PPC64_TOC16_HA:
7428                     case R_PPC64_TOC16_DS:
7429                     case R_PPC64_TOC16_LO_DS:
7430                     case R_PPC64_ADDR64:
7431                       break;
7432                     }
7433
7434                   r_symndx = ELF64_R_SYM (rel->r_info);
7435                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7436                                   r_symndx, ibfd))
7437                     goto error_ret;
7438
7439                   if (sym_sec != toc || h != NULL || sym->st_value != 0)
7440                     continue;
7441
7442                   rel->r_addend -= skip[rel->r_addend >> 3];
7443                 }
7444             }
7445
7446           /* We shouldn't have local or global symbols defined in the TOC,
7447              but handle them anyway.  */
7448           if (local_syms != NULL)
7449             {
7450               Elf_Internal_Sym *sym;
7451
7452               for (sym = local_syms;
7453                    sym < local_syms + symtab_hdr->sh_info;
7454                    ++sym)
7455                 if (sym->st_shndx != SHN_UNDEF
7456                     && (sym->st_shndx < SHN_LORESERVE
7457                         || sym->st_shndx > SHN_HIRESERVE)
7458                     && sym->st_value != 0
7459                     && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
7460                   {
7461                     if (skip[sym->st_value >> 3] != (unsigned long) -1)
7462                       sym->st_value -= skip[sym->st_value >> 3];
7463                     else
7464                       {
7465                         (*_bfd_error_handler)
7466                           (_("%s defined in removed toc entry"),
7467                            bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7468                                              NULL));
7469                         sym->st_value = 0;
7470                         sym->st_shndx = SHN_ABS;
7471                       }
7472                     symtab_hdr->contents = (unsigned char *) local_syms;
7473                   }
7474             }
7475
7476           /* Finally, adjust any global syms defined in the toc.  */
7477           if (toc_inf.global_toc_syms)
7478             {
7479               toc_inf.toc = toc;
7480               toc_inf.skip = skip;
7481               toc_inf.global_toc_syms = FALSE;
7482               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
7483                                       &toc_inf);
7484             }
7485         }
7486
7487       if (local_syms != NULL
7488           && symtab_hdr->contents != (unsigned char *) local_syms)
7489         {
7490           if (!info->keep_memory)
7491             free (local_syms);
7492           else
7493             symtab_hdr->contents = (unsigned char *) local_syms;
7494         }
7495       free (skip);
7496     }
7497
7498   return TRUE;
7499 }
7500
7501 /* Allocate space in .plt, .got and associated reloc sections for
7502    dynamic relocs.  */
7503
7504 static bfd_boolean
7505 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7506 {
7507   struct bfd_link_info *info;
7508   struct ppc_link_hash_table *htab;
7509   asection *s;
7510   struct ppc_link_hash_entry *eh;
7511   struct ppc_dyn_relocs *p;
7512   struct got_entry *gent;
7513
7514   if (h->root.type == bfd_link_hash_indirect)
7515     return TRUE;
7516
7517   if (h->root.type == bfd_link_hash_warning)
7518     h = (struct elf_link_hash_entry *) h->root.u.i.link;
7519
7520   info = (struct bfd_link_info *) inf;
7521   htab = ppc_hash_table (info);
7522
7523   if (htab->elf.dynamic_sections_created
7524       && h->dynindx != -1
7525       && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
7526     {
7527       struct plt_entry *pent;
7528       bfd_boolean doneone = FALSE;
7529       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7530         if (pent->plt.refcount > 0)
7531           {
7532             /* If this is the first .plt entry, make room for the special
7533                first entry.  */
7534             s = htab->plt;
7535             if (s->size == 0)
7536               s->size += PLT_INITIAL_ENTRY_SIZE;
7537
7538             pent->plt.offset = s->size;
7539
7540             /* Make room for this entry.  */
7541             s->size += PLT_ENTRY_SIZE;
7542
7543             /* Make room for the .glink code.  */
7544             s = htab->glink;
7545             if (s->size == 0)
7546               s->size += GLINK_CALL_STUB_SIZE;
7547             /* We need bigger stubs past index 32767.  */
7548             if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
7549               s->size += 4;
7550             s->size += 2*4;
7551
7552             /* We also need to make an entry in the .rela.plt section.  */
7553             s = htab->relplt;
7554             s->size += sizeof (Elf64_External_Rela);
7555             doneone = TRUE;
7556           }
7557         else
7558           pent->plt.offset = (bfd_vma) -1;
7559       if (!doneone)
7560         {
7561           h->plt.plist = NULL;
7562           h->needs_plt = 0;
7563         }
7564     }
7565   else
7566     {
7567       h->plt.plist = NULL;
7568       h->needs_plt = 0;
7569     }
7570
7571   eh = (struct ppc_link_hash_entry *) h;
7572   /* Run through the TLS GD got entries first if we're changing them
7573      to TPREL.  */
7574   if ((eh->tls_mask & TLS_TPRELGD) != 0)
7575     for (gent = h->got.glist; gent != NULL; gent = gent->next)
7576       if (gent->got.refcount > 0
7577           && (gent->tls_type & TLS_GD) != 0)
7578         {
7579           /* This was a GD entry that has been converted to TPREL.  If
7580              there happens to be a TPREL entry we can use that one.  */
7581           struct got_entry *ent;
7582           for (ent = h->got.glist; ent != NULL; ent = ent->next)
7583             if (ent->got.refcount > 0
7584                 && (ent->tls_type & TLS_TPREL) != 0
7585                 && ent->addend == gent->addend
7586                 && ent->owner == gent->owner)
7587               {
7588                 gent->got.refcount = 0;
7589                 break;
7590               }
7591
7592           /* If not, then we'll be using our own TPREL entry.  */
7593           if (gent->got.refcount != 0)
7594             gent->tls_type = TLS_TLS | TLS_TPREL;
7595         }
7596
7597   for (gent = h->got.glist; gent != NULL; gent = gent->next)
7598     if (gent->got.refcount > 0)
7599       {
7600         bfd_boolean dyn;
7601
7602         /* Make sure this symbol is output as a dynamic symbol.
7603            Undefined weak syms won't yet be marked as dynamic,
7604            nor will all TLS symbols.  */
7605         if (h->dynindx == -1
7606             && !h->forced_local)
7607           {
7608             if (! bfd_elf_link_record_dynamic_symbol (info, h))
7609               return FALSE;
7610           }
7611
7612         if ((gent->tls_type & TLS_LD) != 0
7613             && !h->def_dynamic)
7614           {
7615             gent->got.offset = ppc64_tlsld_got (gent->owner)->offset;
7616             continue;
7617           }
7618
7619         s = ppc64_elf_tdata (gent->owner)->got;
7620         gent->got.offset = s->size;
7621         s->size
7622           += (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)) ? 16 : 8;
7623         dyn = htab->elf.dynamic_sections_created;
7624         if ((info->shared
7625              || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
7626             && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7627                 || h->root.type != bfd_link_hash_undefweak))
7628           ppc64_elf_tdata (gent->owner)->relgot->size
7629             += (gent->tls_type & eh->tls_mask & TLS_GD
7630                 ? 2 * sizeof (Elf64_External_Rela)
7631                 : sizeof (Elf64_External_Rela));
7632       }
7633     else
7634       gent->got.offset = (bfd_vma) -1;
7635
7636   if (eh->dyn_relocs == NULL)
7637     return TRUE;
7638
7639   /* In the shared -Bsymbolic case, discard space allocated for
7640      dynamic pc-relative relocs against symbols which turn out to be
7641      defined in regular objects.  For the normal shared case, discard
7642      space for relocs that have become local due to symbol visibility
7643      changes.  */
7644
7645   if (info->shared)
7646     {
7647       /* Relocs that use pc_count are those that appear on a call insn,
7648          or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
7649          generated via assembly.  We want calls to protected symbols to
7650          resolve directly to the function rather than going via the plt.
7651          If people want function pointer comparisons to work as expected
7652          then they should avoid writing weird assembly.  */
7653       if (SYMBOL_CALLS_LOCAL (info, h))
7654         {
7655           struct ppc_dyn_relocs **pp;
7656
7657           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
7658             {
7659               p->count -= p->pc_count;
7660               p->pc_count = 0;
7661               if (p->count == 0)
7662                 *pp = p->next;
7663               else
7664                 pp = &p->next;
7665             }
7666         }
7667
7668       /* Also discard relocs on undefined weak syms with non-default
7669          visibility.  */
7670       if (eh->dyn_relocs != NULL
7671           && h->root.type == bfd_link_hash_undefweak)
7672         {
7673           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
7674             eh->dyn_relocs = NULL;
7675
7676           /* Make sure this symbol is output as a dynamic symbol.
7677              Undefined weak syms won't yet be marked as dynamic.  */
7678           else if (h->dynindx == -1
7679                    && !h->forced_local)
7680             {
7681               if (! bfd_elf_link_record_dynamic_symbol (info, h))
7682                 return FALSE;
7683             }
7684         }
7685     }
7686   else if (ELIMINATE_COPY_RELOCS)
7687     {
7688       /* For the non-shared case, discard space for relocs against
7689          symbols which turn out to need copy relocs or are not
7690          dynamic.  */
7691
7692       if (!h->non_got_ref
7693           && h->def_dynamic
7694           && !h->def_regular)
7695         {
7696           /* Make sure this symbol is output as a dynamic symbol.
7697              Undefined weak syms won't yet be marked as dynamic.  */
7698           if (h->dynindx == -1
7699               && !h->forced_local)
7700             {
7701               if (! bfd_elf_link_record_dynamic_symbol (info, h))
7702                 return FALSE;
7703             }
7704
7705           /* If that succeeded, we know we'll be keeping all the
7706              relocs.  */
7707           if (h->dynindx != -1)
7708             goto keep;
7709         }
7710
7711       eh->dyn_relocs = NULL;
7712
7713     keep: ;
7714     }
7715
7716   /* Finally, allocate space.  */
7717   for (p = eh->dyn_relocs; p != NULL; p = p->next)
7718     {
7719       asection *sreloc = elf_section_data (p->sec)->sreloc;
7720       sreloc->size += p->count * sizeof (Elf64_External_Rela);
7721     }
7722
7723   return TRUE;
7724 }
7725
7726 /* Find any dynamic relocs that apply to read-only sections.  */
7727
7728 static bfd_boolean
7729 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7730 {
7731   struct ppc_link_hash_entry *eh;
7732   struct ppc_dyn_relocs *p;
7733
7734   if (h->root.type == bfd_link_hash_warning)
7735     h = (struct elf_link_hash_entry *) h->root.u.i.link;
7736
7737   eh = (struct ppc_link_hash_entry *) h;
7738   for (p = eh->dyn_relocs; p != NULL; p = p->next)
7739     {
7740       asection *s = p->sec->output_section;
7741
7742       if (s != NULL && (s->flags & SEC_READONLY) != 0)
7743         {
7744           struct bfd_link_info *info = inf;
7745
7746           info->flags |= DF_TEXTREL;
7747
7748           /* Not an error, just cut short the traversal.  */
7749           return FALSE;
7750         }
7751     }
7752   return TRUE;
7753 }
7754
7755 /* Set the sizes of the dynamic sections.  */
7756
7757 static bfd_boolean
7758 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
7759                                  struct bfd_link_info *info)
7760 {
7761   struct ppc_link_hash_table *htab;
7762   bfd *dynobj;
7763   asection *s;
7764   bfd_boolean relocs;
7765   bfd *ibfd;
7766
7767   htab = ppc_hash_table (info);
7768   dynobj = htab->elf.dynobj;
7769   if (dynobj == NULL)
7770     abort ();
7771
7772   if (htab->elf.dynamic_sections_created)
7773     {
7774       /* Set the contents of the .interp section to the interpreter.  */
7775       if (info->executable)
7776         {
7777           s = bfd_get_section_by_name (dynobj, ".interp");
7778           if (s == NULL)
7779             abort ();
7780           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
7781           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
7782         }
7783     }
7784
7785   /* Set up .got offsets for local syms, and space for local dynamic
7786      relocs.  */
7787   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7788     {
7789       struct got_entry **lgot_ents;
7790       struct got_entry **end_lgot_ents;
7791       char *lgot_masks;
7792       bfd_size_type locsymcount;
7793       Elf_Internal_Shdr *symtab_hdr;
7794       asection *srel;
7795
7796       if (!is_ppc64_elf_target (ibfd->xvec))
7797         continue;
7798
7799       if (ppc64_tlsld_got (ibfd)->refcount > 0)
7800         {
7801           s = ppc64_elf_tdata (ibfd)->got;
7802           ppc64_tlsld_got (ibfd)->offset = s->size;
7803           s->size += 16;
7804           if (info->shared)
7805             {
7806               srel = ppc64_elf_tdata (ibfd)->relgot;
7807               srel->size += sizeof (Elf64_External_Rela);
7808             }
7809         }
7810       else
7811         ppc64_tlsld_got (ibfd)->offset = (bfd_vma) -1;
7812
7813       for (s = ibfd->sections; s != NULL; s = s->next)
7814         {
7815           struct ppc_dyn_relocs *p;
7816
7817           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
7818             {
7819               if (!bfd_is_abs_section (p->sec)
7820                   && bfd_is_abs_section (p->sec->output_section))
7821                 {
7822                   /* Input section has been discarded, either because
7823                      it is a copy of a linkonce section or due to
7824                      linker script /DISCARD/, so we'll be discarding
7825                      the relocs too.  */
7826                 }
7827               else if (p->count != 0)
7828                 {
7829                   srel = elf_section_data (p->sec)->sreloc;
7830                   srel->size += p->count * sizeof (Elf64_External_Rela);
7831                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
7832                     info->flags |= DF_TEXTREL;
7833                 }
7834             }
7835         }
7836
7837       lgot_ents = elf_local_got_ents (ibfd);
7838       if (!lgot_ents)
7839         continue;
7840
7841       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7842       locsymcount = symtab_hdr->sh_info;
7843       end_lgot_ents = lgot_ents + locsymcount;
7844       lgot_masks = (char *) end_lgot_ents;
7845       s = ppc64_elf_tdata (ibfd)->got;
7846       srel = ppc64_elf_tdata (ibfd)->relgot;
7847       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
7848         {
7849           struct got_entry *ent;
7850
7851           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
7852             if (ent->got.refcount > 0)
7853               {
7854                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
7855                   {
7856                     if (ppc64_tlsld_got (ibfd)->offset == (bfd_vma) -1)
7857                       {
7858                         ppc64_tlsld_got (ibfd)->offset = s->size;
7859                         s->size += 16;
7860                         if (info->shared)
7861                           srel->size += sizeof (Elf64_External_Rela);
7862                       }
7863                     ent->got.offset = ppc64_tlsld_got (ibfd)->offset;
7864                   }
7865                 else
7866                   {
7867                     ent->got.offset = s->size;
7868                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
7869                       {
7870                         s->size += 16;
7871                         if (info->shared)
7872                           srel->size += 2 * sizeof (Elf64_External_Rela);
7873                       }
7874                     else
7875                       {
7876                         s->size += 8;
7877                         if (info->shared)
7878                           srel->size += sizeof (Elf64_External_Rela);
7879                       }
7880                   }
7881               }
7882             else
7883               ent->got.offset = (bfd_vma) -1;
7884         }
7885     }
7886
7887   /* Allocate global sym .plt and .got entries, and space for global
7888      sym dynamic relocs.  */
7889   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
7890
7891   /* We now have determined the sizes of the various dynamic sections.
7892      Allocate memory for them.  */
7893   relocs = FALSE;
7894   for (s = dynobj->sections; s != NULL; s = s->next)
7895     {
7896       if ((s->flags & SEC_LINKER_CREATED) == 0)
7897         continue;
7898
7899       if (s == htab->brlt || s == htab->relbrlt)
7900         /* These haven't been allocated yet;  don't strip.  */
7901         continue;
7902       else if (s == htab->got
7903                || s == htab->plt
7904                || s == htab->glink
7905                || s == htab->dynbss)
7906         {
7907           /* Strip this section if we don't need it; see the
7908              comment below.  */
7909         }
7910       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
7911         {
7912           if (s->size != 0)
7913             {
7914               if (s != htab->relplt)
7915                 relocs = TRUE;
7916
7917               /* We use the reloc_count field as a counter if we need
7918                  to copy relocs into the output file.  */
7919               s->reloc_count = 0;
7920             }
7921         }
7922       else
7923         {
7924           /* It's not one of our sections, so don't allocate space.  */
7925           continue;
7926         }
7927
7928       if (s->size == 0)
7929         {
7930           /* If we don't need this section, strip it from the
7931              output file.  This is mostly to handle .rela.bss and
7932              .rela.plt.  We must create both sections in
7933              create_dynamic_sections, because they must be created
7934              before the linker maps input sections to output
7935              sections.  The linker does that before
7936              adjust_dynamic_symbol is called, and it is that
7937              function which decides whether anything needs to go
7938              into these sections.  */
7939           s->flags |= SEC_EXCLUDE;
7940           continue;
7941         }
7942
7943       if ((s->flags & SEC_HAS_CONTENTS) == 0)
7944         continue;
7945
7946       /* Allocate memory for the section contents.  We use bfd_zalloc
7947          here in case unused entries are not reclaimed before the
7948          section's contents are written out.  This should not happen,
7949          but this way if it does we get a R_PPC64_NONE reloc in .rela
7950          sections instead of garbage.
7951          We also rely on the section contents being zero when writing
7952          the GOT.  */
7953       s->contents = bfd_zalloc (dynobj, s->size);
7954       if (s->contents == NULL)
7955         return FALSE;
7956     }
7957
7958   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7959     {
7960       if (!is_ppc64_elf_target (ibfd->xvec))
7961         continue;
7962
7963       s = ppc64_elf_tdata (ibfd)->got;
7964       if (s != NULL && s != htab->got)
7965         {
7966           if (s->size == 0)
7967             s->flags |= SEC_EXCLUDE;
7968           else
7969             {
7970               s->contents = bfd_zalloc (ibfd, s->size);
7971               if (s->contents == NULL)
7972                 return FALSE;
7973             }
7974         }
7975       s = ppc64_elf_tdata (ibfd)->relgot;
7976       if (s != NULL)
7977         {
7978           if (s->size == 0)
7979             s->flags |= SEC_EXCLUDE;
7980           else
7981             {
7982               s->contents = bfd_zalloc (ibfd, s->size);
7983               if (s->contents == NULL)
7984                 return FALSE;
7985               relocs = TRUE;
7986               s->reloc_count = 0;
7987             }
7988         }
7989     }
7990
7991   if (htab->elf.dynamic_sections_created)
7992     {
7993       /* Add some entries to the .dynamic section.  We fill in the
7994          values later, in ppc64_elf_finish_dynamic_sections, but we
7995          must add the entries now so that we get the correct size for
7996          the .dynamic section.  The DT_DEBUG entry is filled in by the
7997          dynamic linker and used by the debugger.  */
7998 #define add_dynamic_entry(TAG, VAL) \
7999   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
8000
8001       if (info->executable)
8002         {
8003           if (!add_dynamic_entry (DT_DEBUG, 0))
8004             return FALSE;
8005         }
8006
8007       if (htab->plt != NULL && htab->plt->size != 0)
8008         {
8009           if (!add_dynamic_entry (DT_PLTGOT, 0)
8010               || !add_dynamic_entry (DT_PLTRELSZ, 0)
8011               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
8012               || !add_dynamic_entry (DT_JMPREL, 0)
8013               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
8014             return FALSE;
8015         }
8016
8017       if (NO_OPD_RELOCS)
8018         {
8019           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
8020               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
8021             return FALSE;
8022         }
8023
8024       if (relocs)
8025         {
8026           if (!add_dynamic_entry (DT_RELA, 0)
8027               || !add_dynamic_entry (DT_RELASZ, 0)
8028               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
8029             return FALSE;
8030
8031           /* If any dynamic relocs apply to a read-only section,
8032              then we need a DT_TEXTREL entry.  */
8033           if ((info->flags & DF_TEXTREL) == 0)
8034             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
8035
8036           if ((info->flags & DF_TEXTREL) != 0)
8037             {
8038               if (!add_dynamic_entry (DT_TEXTREL, 0))
8039                 return FALSE;
8040             }
8041         }
8042     }
8043 #undef add_dynamic_entry
8044
8045   return TRUE;
8046 }
8047
8048 /* Determine the type of stub needed, if any, for a call.  */
8049
8050 static inline enum ppc_stub_type
8051 ppc_type_of_stub (asection *input_sec,
8052                   const Elf_Internal_Rela *rel,
8053                   struct ppc_link_hash_entry **hash,
8054                   bfd_vma destination)
8055 {
8056   struct ppc_link_hash_entry *h = *hash;
8057   bfd_vma location;
8058   bfd_vma branch_offset;
8059   bfd_vma max_branch_offset;
8060   enum elf_ppc64_reloc_type r_type;
8061
8062   if (h != NULL)
8063     {
8064       struct ppc_link_hash_entry *fdh = h;
8065       if (fdh->oh != NULL
8066           && fdh->oh->is_func_descriptor)
8067         fdh = fdh->oh;
8068
8069       if (fdh->elf.dynindx != -1)
8070         {
8071           struct plt_entry *ent;
8072
8073           for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
8074             if (ent->addend == rel->r_addend
8075                 && ent->plt.offset != (bfd_vma) -1)
8076               {
8077                 *hash = fdh;
8078                 return ppc_stub_plt_call;
8079               }
8080         }
8081
8082       /* Here, we know we don't have a plt entry.  If we don't have a
8083          either a defined function descriptor or a defined entry symbol
8084          in a regular object file, then it is pointless trying to make
8085          any other type of stub.  */
8086       if (!((fdh->elf.root.type == bfd_link_hash_defined
8087             || fdh->elf.root.type == bfd_link_hash_defweak)
8088             && fdh->elf.root.u.def.section->output_section != NULL)
8089           && !((h->elf.root.type == bfd_link_hash_defined
8090                 || h->elf.root.type == bfd_link_hash_defweak)
8091                && h->elf.root.u.def.section->output_section != NULL))
8092         return ppc_stub_none;
8093     }
8094
8095   /* Determine where the call point is.  */
8096   location = (input_sec->output_offset
8097               + input_sec->output_section->vma
8098               + rel->r_offset);
8099
8100   branch_offset = destination - location;
8101   r_type = ELF64_R_TYPE (rel->r_info);
8102
8103   /* Determine if a long branch stub is needed.  */
8104   max_branch_offset = 1 << 25;
8105   if (r_type != R_PPC64_REL24)
8106     max_branch_offset = 1 << 15;
8107
8108   if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
8109     /* We need a stub.  Figure out whether a long_branch or plt_branch
8110        is needed later.  */
8111     return ppc_stub_long_branch;
8112
8113   return ppc_stub_none;
8114 }
8115
8116 /* Build a .plt call stub.  */
8117
8118 static inline bfd_byte *
8119 build_plt_stub (bfd *obfd, bfd_byte *p, int offset)
8120 {
8121 #define PPC_LO(v) ((v) & 0xffff)
8122 #define PPC_HI(v) (((v) >> 16) & 0xffff)
8123 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
8124
8125   bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
8126   bfd_put_32 (obfd, STD_R2_40R1, p),                    p += 4;
8127   bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),  p += 4;
8128   if (PPC_HA (offset + 8) != PPC_HA (offset))
8129     bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p),            p += 4;
8130   offset += 8;
8131   bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p),   p += 4;
8132   if (PPC_HA (offset + 8) != PPC_HA (offset))
8133     bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p),            p += 4;
8134   offset += 8;
8135   bfd_put_32 (obfd, MTCTR_R11, p),                      p += 4;
8136   bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),  p += 4;
8137   bfd_put_32 (obfd, BCTR, p),                           p += 4;
8138   return p;
8139 }
8140
8141 static bfd_boolean
8142 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
8143 {
8144   struct ppc_stub_hash_entry *stub_entry;
8145   struct ppc_branch_hash_entry *br_entry;
8146   struct bfd_link_info *info;
8147   struct ppc_link_hash_table *htab;
8148   bfd_byte *loc;
8149   bfd_byte *p;
8150   unsigned int indx;
8151   struct plt_entry *ent;
8152   bfd_vma dest, off;
8153   int size;
8154
8155   /* Massage our args to the form they really have.  */
8156   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
8157   info = in_arg;
8158
8159   htab = ppc_hash_table (info);
8160
8161   /* Make a note of the offset within the stubs for this entry.  */
8162   stub_entry->stub_offset = stub_entry->stub_sec->size;
8163   loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
8164
8165   htab->stub_count[stub_entry->stub_type - 1] += 1;
8166   switch (stub_entry->stub_type)
8167     {
8168     case ppc_stub_long_branch:
8169     case ppc_stub_long_branch_r2off:
8170       /* Branches are relative.  This is where we are going to.  */
8171       off = dest = (stub_entry->target_value
8172                     + stub_entry->target_section->output_offset
8173                     + stub_entry->target_section->output_section->vma);
8174
8175       /* And this is where we are coming from.  */
8176       off -= (stub_entry->stub_offset
8177               + stub_entry->stub_sec->output_offset
8178               + stub_entry->stub_sec->output_section->vma);
8179
8180       if (stub_entry->stub_type != ppc_stub_long_branch_r2off)
8181         size = 4;
8182       else
8183         {
8184           bfd_vma r2off;
8185
8186           r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8187                    - htab->stub_group[stub_entry->id_sec->id].toc_off);
8188           bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
8189           loc += 4;
8190           bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
8191           loc += 4;
8192           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
8193           loc += 4;
8194           off -= 12;
8195           size = 16;
8196         }
8197       bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
8198
8199       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8200         {
8201           (*_bfd_error_handler) (_("long branch stub `%s' offset overflow"),
8202                                  stub_entry->root.string);
8203           htab->stub_error = TRUE;
8204           return FALSE;
8205         }
8206
8207       if (info->emitrelocations)
8208         {
8209           Elf_Internal_Rela *relocs, *r;
8210           struct bfd_elf_section_data *elfsec_data;
8211
8212           elfsec_data = elf_section_data (stub_entry->stub_sec);
8213           relocs = elfsec_data->relocs;
8214           if (relocs == NULL)
8215             {
8216               bfd_size_type relsize;
8217               relsize = stub_entry->stub_sec->reloc_count * sizeof (*relocs);
8218               relocs = bfd_alloc (htab->stub_bfd, relsize);
8219               if (relocs == NULL)
8220                 return FALSE;
8221               elfsec_data->relocs = relocs;
8222               elfsec_data->rel_hdr.sh_size = relsize;
8223               elfsec_data->rel_hdr.sh_entsize = 24;
8224               stub_entry->stub_sec->reloc_count = 0;
8225             }
8226           r = relocs + stub_entry->stub_sec->reloc_count;
8227           stub_entry->stub_sec->reloc_count += 1;
8228           r->r_offset = loc - stub_entry->stub_sec->contents;
8229           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
8230           r->r_addend = dest;
8231           if (stub_entry->h != NULL)
8232             {
8233               struct elf_link_hash_entry **hashes;
8234               unsigned long symndx;
8235               struct ppc_link_hash_entry *h;
8236
8237               hashes = elf_sym_hashes (htab->stub_bfd);
8238               if (hashes == NULL)
8239                 {
8240                   bfd_size_type hsize;
8241
8242                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
8243                   hashes = bfd_zalloc (htab->stub_bfd, hsize);
8244                   if (hashes == NULL)
8245                     return FALSE;
8246                   elf_sym_hashes (htab->stub_bfd) = hashes;
8247                   htab->stub_globals = 1;
8248                 }
8249               symndx = htab->stub_globals++;
8250               h = stub_entry->h;
8251               hashes[symndx] = &h->elf;
8252               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
8253               if (h->oh != NULL && h->oh->is_func)
8254                 h = h->oh;
8255               if (h->elf.root.u.def.section != stub_entry->target_section)
8256                 /* H is an opd symbol.  The addend must be zero.  */
8257                 r->r_addend = 0;
8258               else
8259                 {
8260                   off = (h->elf.root.u.def.value
8261                          + h->elf.root.u.def.section->output_offset
8262                          + h->elf.root.u.def.section->output_section->vma);
8263                   r->r_addend -= off;
8264                 }
8265             }
8266         }
8267       break;
8268
8269     case ppc_stub_plt_branch:
8270     case ppc_stub_plt_branch_r2off:
8271       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8272                                          stub_entry->root.string + 9,
8273                                          FALSE, FALSE);
8274       if (br_entry == NULL)
8275         {
8276           (*_bfd_error_handler) (_("can't find branch stub `%s'"),
8277                                  stub_entry->root.string);
8278           htab->stub_error = TRUE;
8279           return FALSE;
8280         }
8281
8282       off = (stub_entry->target_value
8283              + stub_entry->target_section->output_offset
8284              + stub_entry->target_section->output_section->vma);
8285
8286       bfd_put_64 (htab->brlt->owner, off,
8287                   htab->brlt->contents + br_entry->offset);
8288
8289       if (htab->relbrlt != NULL)
8290         {
8291           /* Create a reloc for the branch lookup table entry.  */
8292           Elf_Internal_Rela rela;
8293           bfd_byte *rl;
8294
8295           rela.r_offset = (br_entry->offset
8296                            + htab->brlt->output_offset
8297                            + htab->brlt->output_section->vma);
8298           rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
8299           rela.r_addend = off;
8300
8301           rl = htab->relbrlt->contents;
8302           rl += htab->relbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
8303           bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
8304         }
8305
8306       off = (br_entry->offset
8307              + htab->brlt->output_offset
8308              + htab->brlt->output_section->vma
8309              - elf_gp (htab->brlt->output_section->owner)
8310              - htab->stub_group[stub_entry->id_sec->id].toc_off);
8311
8312       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
8313         {
8314           (*_bfd_error_handler)
8315             (_("linkage table error against `%s'"),
8316              stub_entry->root.string);
8317           bfd_set_error (bfd_error_bad_value);
8318           htab->stub_error = TRUE;
8319           return FALSE;
8320         }
8321
8322       indx = off;
8323       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
8324         {
8325           bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
8326           loc += 4;
8327           bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
8328           size = 16;
8329         }
8330       else
8331         {
8332           bfd_vma r2off;
8333
8334           r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8335                    - htab->stub_group[stub_entry->id_sec->id].toc_off);
8336           bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
8337           loc += 4;
8338           bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
8339           loc += 4;
8340           bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
8341           loc += 4;
8342           bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
8343           loc += 4;
8344           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
8345           size = 28;
8346         }
8347       loc += 4;
8348       bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
8349       loc += 4;
8350       bfd_put_32 (htab->stub_bfd, BCTR, loc);
8351       break;
8352
8353     case ppc_stub_plt_call:
8354       /* Do the best we can for shared libraries built without
8355          exporting ".foo" for each "foo".  This can happen when symbol
8356          versioning scripts strip all bar a subset of symbols.  */
8357       if (stub_entry->h->oh != NULL
8358           && stub_entry->h->oh->elf.root.type != bfd_link_hash_defined
8359           && stub_entry->h->oh->elf.root.type != bfd_link_hash_defweak)
8360         {
8361           /* Point the symbol at the stub.  There may be multiple stubs,
8362              we don't really care;  The main thing is to make this sym
8363              defined somewhere.  Maybe defining the symbol in the stub
8364              section is a silly idea.  If we didn't do this, htab->top_id
8365              could disappear.  */
8366           stub_entry->h->oh->elf.root.type = bfd_link_hash_defined;
8367           stub_entry->h->oh->elf.root.u.def.section = stub_entry->stub_sec;
8368           stub_entry->h->oh->elf.root.u.def.value = stub_entry->stub_offset;
8369         }
8370
8371       /* Now build the stub.  */
8372       off = (bfd_vma) -1;
8373       for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8374         if (ent->addend == stub_entry->addend)
8375           {
8376             off = ent->plt.offset;
8377             break;
8378           }
8379       if (off >= (bfd_vma) -2)
8380         abort ();
8381
8382       off &= ~ (bfd_vma) 1;
8383       off += (htab->plt->output_offset
8384               + htab->plt->output_section->vma
8385               - elf_gp (htab->plt->output_section->owner)
8386               - htab->stub_group[stub_entry->id_sec->id].toc_off);
8387
8388       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
8389         {
8390           (*_bfd_error_handler)
8391             (_("linkage table error against `%s'"),
8392              stub_entry->h->elf.root.root.string);
8393           bfd_set_error (bfd_error_bad_value);
8394           htab->stub_error = TRUE;
8395           return FALSE;
8396         }
8397
8398       p = build_plt_stub (htab->stub_bfd, loc, off);
8399       size = p - loc;
8400       break;
8401
8402     default:
8403       BFD_FAIL ();
8404       return FALSE;
8405     }
8406
8407   stub_entry->stub_sec->size += size;
8408
8409   if (htab->emit_stub_syms)
8410     {
8411       struct elf_link_hash_entry *h;
8412       size_t len1, len2;
8413       char *name;
8414       const char *const stub_str[] = { "long_branch",
8415                                        "long_branch_r2off",
8416                                        "plt_branch",
8417                                        "plt_branch_r2off",
8418                                        "plt_call" };
8419
8420       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
8421       len2 = strlen (stub_entry->root.string);
8422       name = bfd_malloc (len1 + len2 + 2);
8423       if (name == NULL)
8424         return FALSE;
8425       memcpy (name, stub_entry->root.string, 9);
8426       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
8427       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
8428       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
8429       if (h == NULL)
8430         return FALSE;
8431       if (h->root.type == bfd_link_hash_new)
8432         {
8433           h->root.type = bfd_link_hash_defined;
8434           h->root.u.def.section = stub_entry->stub_sec;
8435           h->root.u.def.value = stub_entry->stub_offset;
8436           h->ref_regular = 1;
8437           h->def_regular = 1;
8438           h->ref_regular_nonweak = 1;
8439           h->forced_local = 1;
8440           h->non_elf = 0;
8441         }
8442     }
8443
8444   return TRUE;
8445 }
8446
8447 /* As above, but don't actually build the stub.  Just bump offset so
8448    we know stub section sizes, and select plt_branch stubs where
8449    long_branch stubs won't do.  */
8450
8451 static bfd_boolean
8452 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
8453 {
8454   struct ppc_stub_hash_entry *stub_entry;
8455   struct bfd_link_info *info;
8456   struct ppc_link_hash_table *htab;
8457   bfd_vma off;
8458   int size;
8459
8460   /* Massage our args to the form they really have.  */
8461   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
8462   info = in_arg;
8463
8464   htab = ppc_hash_table (info);
8465
8466   if (stub_entry->stub_type == ppc_stub_plt_call)
8467     {
8468       struct plt_entry *ent;
8469       off = (bfd_vma) -1;
8470       for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8471         if (ent->addend == stub_entry->addend)
8472           {
8473             off = ent->plt.offset & ~(bfd_vma) 1;
8474             break;
8475           }
8476       if (off >= (bfd_vma) -2)
8477         abort ();
8478       off += (htab->plt->output_offset
8479               + htab->plt->output_section->vma
8480               - elf_gp (htab->plt->output_section->owner)
8481               - htab->stub_group[stub_entry->id_sec->id].toc_off);
8482
8483       size = PLT_CALL_STUB_SIZE;
8484       if (PPC_HA (off + 16) != PPC_HA (off))
8485         size += 4;
8486     }
8487   else
8488     {
8489       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
8490          variants.  */
8491       off = (stub_entry->target_value
8492              + stub_entry->target_section->output_offset
8493              + stub_entry->target_section->output_section->vma);
8494       off -= (stub_entry->stub_sec->size
8495               + stub_entry->stub_sec->output_offset
8496               + stub_entry->stub_sec->output_section->vma);
8497
8498       /* Reset the stub type from the plt variant in case we now
8499          can reach with a shorter stub.  */
8500       if (stub_entry->stub_type >= ppc_stub_plt_branch)
8501         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
8502
8503       size = 4;
8504       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
8505         {
8506           off -= 12;
8507           size = 16;
8508         }
8509
8510       /* If the branch offset if too big, use a ppc_stub_plt_branch.  */
8511       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8512         {
8513           struct ppc_branch_hash_entry *br_entry;
8514
8515           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8516                                              stub_entry->root.string + 9,
8517                                              TRUE, FALSE);
8518           if (br_entry == NULL)
8519             {
8520               (*_bfd_error_handler) (_("can't build branch stub `%s'"),
8521                                      stub_entry->root.string);
8522               htab->stub_error = TRUE;
8523               return FALSE;
8524             }
8525
8526           if (br_entry->iter != htab->stub_iteration)
8527             {
8528               br_entry->iter = htab->stub_iteration;
8529               br_entry->offset = htab->brlt->size;
8530               htab->brlt->size += 8;
8531
8532               if (htab->relbrlt != NULL)
8533                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
8534             }
8535
8536           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
8537           size = 16;
8538           if (stub_entry->stub_type != ppc_stub_plt_branch)
8539             size = 28;
8540         }
8541
8542       if (info->emitrelocations
8543           && (stub_entry->stub_type == ppc_stub_long_branch
8544               || stub_entry->stub_type == ppc_stub_long_branch_r2off))
8545         stub_entry->stub_sec->reloc_count += 1;
8546     }
8547
8548   stub_entry->stub_sec->size += size;
8549   return TRUE;
8550 }
8551
8552 /* Set up various things so that we can make a list of input sections
8553    for each output section included in the link.  Returns -1 on error,
8554    0 when no stubs will be needed, and 1 on success.  */
8555
8556 int
8557 ppc64_elf_setup_section_lists (bfd *output_bfd,
8558                                struct bfd_link_info *info,
8559                                int no_multi_toc)
8560 {
8561   bfd *input_bfd;
8562   int top_id, top_index, id;
8563   asection *section;
8564   asection **input_list;
8565   bfd_size_type amt;
8566   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8567
8568   htab->no_multi_toc = no_multi_toc;
8569
8570   if (htab->brlt == NULL)
8571     return 0;
8572
8573   /* Find the top input section id.  */
8574   for (input_bfd = info->input_bfds, top_id = 3;
8575        input_bfd != NULL;
8576        input_bfd = input_bfd->link_next)
8577     {
8578       for (section = input_bfd->sections;
8579            section != NULL;
8580            section = section->next)
8581         {
8582           if (top_id < section->id)
8583             top_id = section->id;
8584         }
8585     }
8586
8587   htab->top_id = top_id;
8588   amt = sizeof (struct map_stub) * (top_id + 1);
8589   htab->stub_group = bfd_zmalloc (amt);
8590   if (htab->stub_group == NULL)
8591     return -1;
8592
8593   /* Set toc_off for com, und, abs and ind sections.  */
8594   for (id = 0; id < 3; id++)
8595     htab->stub_group[id].toc_off = TOC_BASE_OFF;
8596
8597   elf_gp (output_bfd) = htab->toc_curr = ppc64_elf_toc (output_bfd);
8598
8599   /* We can't use output_bfd->section_count here to find the top output
8600      section index as some sections may have been removed, and
8601      strip_excluded_output_sections doesn't renumber the indices.  */
8602   for (section = output_bfd->sections, top_index = 0;
8603        section != NULL;
8604        section = section->next)
8605     {
8606       if (top_index < section->index)
8607         top_index = section->index;
8608     }
8609
8610   htab->top_index = top_index;
8611   amt = sizeof (asection *) * (top_index + 1);
8612   input_list = bfd_zmalloc (amt);
8613   htab->input_list = input_list;
8614   if (input_list == NULL)
8615     return -1;
8616
8617   return 1;
8618 }
8619
8620 /* The linker repeatedly calls this function for each TOC input section
8621    and linker generated GOT section.  Group input bfds such that the toc
8622    within a group is less than 64k in size.  Will break with cute linker
8623    scripts that play games with dot in the output toc section.  */
8624
8625 void
8626 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
8627 {
8628   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8629
8630   if (!htab->no_multi_toc)
8631     {
8632       bfd_vma addr = isec->output_offset + isec->output_section->vma;
8633       bfd_vma off = addr - htab->toc_curr;
8634
8635       if (off + isec->size > 0x10000)
8636         htab->toc_curr = addr;
8637
8638       elf_gp (isec->owner) = (htab->toc_curr
8639                               - elf_gp (isec->output_section->owner)
8640                               + TOC_BASE_OFF);
8641     }
8642 }
8643
8644 /* Called after the last call to the above function.  */
8645
8646 void
8647 ppc64_elf_reinit_toc (bfd *output_bfd, struct bfd_link_info *info)
8648 {
8649   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8650
8651   htab->multi_toc_needed = htab->toc_curr != elf_gp (output_bfd);
8652
8653   /* toc_curr tracks the TOC offset used for code sections below in
8654      ppc64_elf_next_input_section.  Start off at 0x8000.  */
8655   htab->toc_curr = TOC_BASE_OFF;
8656 }
8657
8658 /* No toc references were found in ISEC.  If the code in ISEC makes no
8659    calls, then there's no need to use toc adjusting stubs when branching
8660    into ISEC.  Actually, indirect calls from ISEC are OK as they will
8661    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
8662    needed, and 2 if a cyclical call-graph was found but no other reason
8663    for a stub was detected.  If called from the top level, a return of
8664    2 means the same as a return of 0.  */
8665
8666 static int
8667 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
8668 {
8669   Elf_Internal_Rela *relstart, *rel;
8670   Elf_Internal_Sym *local_syms;
8671   int ret;
8672   struct ppc_link_hash_table *htab;
8673
8674   /* We know none of our code bearing sections will need toc stubs.  */
8675   if ((isec->flags & SEC_LINKER_CREATED) != 0)
8676     return 0;
8677
8678   if (isec->size == 0)
8679     return 0;
8680
8681   if (isec->output_section == NULL)
8682     return 0;
8683
8684   /* Hack for linux kernel.  .fixup contains branches, but only back to
8685      the function that hit an exception.  */
8686   if (strcmp (isec->name, ".fixup") == 0)
8687     return 0;
8688
8689   if (isec->reloc_count == 0)
8690     return 0;
8691
8692   relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
8693                                         info->keep_memory);
8694   if (relstart == NULL)
8695     return -1;
8696
8697   /* Look for branches to outside of this section.  */
8698   local_syms = NULL;
8699   ret = 0;
8700   htab = ppc_hash_table (info);
8701   for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
8702     {
8703       enum elf_ppc64_reloc_type r_type;
8704       unsigned long r_symndx;
8705       struct elf_link_hash_entry *h;
8706       Elf_Internal_Sym *sym;
8707       asection *sym_sec;
8708       long *opd_adjust;
8709       bfd_vma sym_value;
8710       bfd_vma dest;
8711
8712       r_type = ELF64_R_TYPE (rel->r_info);
8713       if (r_type != R_PPC64_REL24
8714           && r_type != R_PPC64_REL14
8715           && r_type != R_PPC64_REL14_BRTAKEN
8716           && r_type != R_PPC64_REL14_BRNTAKEN)
8717         continue;
8718
8719       r_symndx = ELF64_R_SYM (rel->r_info);
8720       if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
8721                       isec->owner))
8722         {
8723           ret = -1;
8724           break;
8725         }
8726
8727       /* Calls to dynamic lib functions go through a plt call stub
8728          that uses r2.  Branches to undefined symbols might be a call
8729          using old-style dot symbols that can be satisfied by a plt
8730          call into a new-style dynamic library.  */
8731       if (sym_sec == NULL)
8732         {
8733           struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8734           if (eh != NULL
8735               && eh->oh != NULL
8736               && eh->oh->elf.plt.plist != NULL)
8737             {
8738               ret = 1;
8739               break;
8740             }
8741
8742           /* Ignore other undefined symbols.  */
8743           continue;
8744         }
8745
8746       /* Assume branches to other sections not included in the link need
8747          stubs too, to cover -R and absolute syms.  */
8748       if (sym_sec->output_section == NULL)
8749         {
8750           ret = 1;
8751           break;
8752         }
8753
8754       if (h == NULL)
8755         sym_value = sym->st_value;
8756       else
8757         {
8758           if (h->root.type != bfd_link_hash_defined
8759               && h->root.type != bfd_link_hash_defweak)
8760             abort ();
8761           sym_value = h->root.u.def.value;
8762         }
8763       sym_value += rel->r_addend;
8764
8765       /* If this branch reloc uses an opd sym, find the code section.  */
8766       opd_adjust = get_opd_info (sym_sec);
8767       if (opd_adjust != NULL)
8768         {
8769           if (h == NULL)
8770             {
8771               long adjust;
8772
8773               adjust = opd_adjust[sym->st_value / 8];
8774               if (adjust == -1)
8775                 /* Assume deleted functions won't ever be called.  */
8776                 continue;
8777               sym_value += adjust;
8778             }
8779
8780           dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
8781           if (dest == (bfd_vma) -1)
8782             continue;
8783         }
8784       else
8785         dest = (sym_value
8786                 + sym_sec->output_offset
8787                 + sym_sec->output_section->vma);
8788
8789       /* Ignore branch to self.  */
8790       if (sym_sec == isec)
8791         continue;
8792
8793       /* If the called function uses the toc, we need a stub.  */
8794       if (sym_sec->has_toc_reloc
8795           || sym_sec->makes_toc_func_call)
8796         {
8797           ret = 1;
8798           break;
8799         }
8800
8801       /* Assume any branch that needs a long branch stub might in fact
8802          need a plt_branch stub.  A plt_branch stub uses r2.  */
8803       else if (dest - (isec->output_offset
8804                        + isec->output_section->vma
8805                        + rel->r_offset) + (1 << 25) >= (2 << 25))
8806         {
8807           ret = 1;
8808           break;
8809         }
8810
8811       /* If calling back to a section in the process of being tested, we
8812          can't say for sure that no toc adjusting stubs are needed, so
8813          don't return zero.  */
8814       else if (sym_sec->call_check_in_progress)
8815         ret = 2;
8816
8817       /* Branches to another section that itself doesn't have any TOC
8818          references are OK.  Recursively call ourselves to check.  */
8819       else if (sym_sec->id <= htab->top_id
8820                && htab->stub_group[sym_sec->id].toc_off == 0)
8821         {
8822           int recur;
8823
8824           /* Mark current section as indeterminate, so that other
8825              sections that call back to current won't be marked as
8826              known.  */
8827           isec->call_check_in_progress = 1;
8828           recur = toc_adjusting_stub_needed (info, sym_sec);
8829           isec->call_check_in_progress = 0;
8830
8831           if (recur < 0)
8832             {
8833               /* An error.  Exit.  */
8834               ret = -1;
8835               break;
8836             }
8837           else if (recur <= 1)
8838             {
8839               /* Known result.  Mark as checked and set section flag.  */
8840               htab->stub_group[sym_sec->id].toc_off = 1;
8841               if (recur != 0)
8842                 {
8843                   sym_sec->makes_toc_func_call = 1;
8844                   ret = 1;
8845                   break;
8846                 }
8847             }
8848           else
8849             {
8850               /* Unknown result.  Continue checking.  */
8851               ret = 2;
8852             }
8853         }
8854     }
8855
8856   if (local_syms != NULL
8857       && (elf_tdata (isec->owner)->symtab_hdr.contents
8858           != (unsigned char *) local_syms))
8859     free (local_syms);
8860   if (elf_section_data (isec)->relocs != relstart)
8861     free (relstart);
8862
8863   return ret;
8864 }
8865
8866 /* The linker repeatedly calls this function for each input section,
8867    in the order that input sections are linked into output sections.
8868    Build lists of input sections to determine groupings between which
8869    we may insert linker stubs.  */
8870
8871 bfd_boolean
8872 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
8873 {
8874   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8875
8876   if ((isec->output_section->flags & SEC_CODE) != 0
8877       && isec->output_section->index <= htab->top_index)
8878     {
8879       asection **list = htab->input_list + isec->output_section->index;
8880       /* Steal the link_sec pointer for our list.  */
8881 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
8882       /* This happens to make the list in reverse order,
8883          which is what we want.  */
8884       PREV_SEC (isec) = *list;
8885       *list = isec;
8886     }
8887
8888   if (htab->multi_toc_needed)
8889     {
8890       /* If a code section has a function that uses the TOC then we need
8891          to use the right TOC (obviously).  Also, make sure that .opd gets
8892          the correct TOC value for R_PPC64_TOC relocs that don't have or
8893          can't find their function symbol (shouldn't ever happen now).  */
8894       if (isec->has_toc_reloc || (isec->flags & SEC_CODE) == 0)
8895         {
8896           if (elf_gp (isec->owner) != 0)
8897             htab->toc_curr = elf_gp (isec->owner);
8898         }
8899       else if (htab->stub_group[isec->id].toc_off == 0)
8900         {
8901           int ret = toc_adjusting_stub_needed (info, isec);
8902           if (ret < 0)
8903             return FALSE;
8904           else
8905             isec->makes_toc_func_call = ret & 1;
8906         }
8907     }
8908
8909   /* Functions that don't use the TOC can belong in any TOC group.
8910      Use the last TOC base.  This happens to make _init and _fini
8911      pasting work.  */
8912   htab->stub_group[isec->id].toc_off = htab->toc_curr;
8913   return TRUE;
8914 }
8915
8916 /* See whether we can group stub sections together.  Grouping stub
8917    sections may result in fewer stubs.  More importantly, we need to
8918    put all .init* and .fini* stubs at the beginning of the .init or
8919    .fini output sections respectively, because glibc splits the
8920    _init and _fini functions into multiple parts.  Putting a stub in
8921    the middle of a function is not a good idea.  */
8922
8923 static void
8924 group_sections (struct ppc_link_hash_table *htab,
8925                 bfd_size_type stub_group_size,
8926                 bfd_boolean stubs_always_before_branch)
8927 {
8928   asection **list = htab->input_list + htab->top_index;
8929   do
8930     {
8931       asection *tail = *list;
8932       while (tail != NULL)
8933         {
8934           asection *curr;
8935           asection *prev;
8936           bfd_size_type total;
8937           bfd_boolean big_sec;
8938           bfd_vma curr_toc;
8939
8940           curr = tail;
8941           total = tail->size;
8942           big_sec = total > stub_group_size;
8943           if (big_sec)
8944             (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
8945                                      tail->owner, tail);
8946           curr_toc = htab->stub_group[tail->id].toc_off;
8947
8948           while ((prev = PREV_SEC (curr)) != NULL
8949                  && ((total += curr->output_offset - prev->output_offset)
8950                      < stub_group_size)
8951                  && htab->stub_group[prev->id].toc_off == curr_toc)
8952             curr = prev;
8953
8954           /* OK, the size from the start of CURR to the end is less
8955              than stub_group_size and thus can be handled by one stub
8956              section.  (or the tail section is itself larger than
8957              stub_group_size, in which case we may be toast.)  We
8958              should really be keeping track of the total size of stubs
8959              added here, as stubs contribute to the final output
8960              section size.  That's a little tricky, and this way will
8961              only break if stubs added make the total size more than
8962              2^25, ie. for the default stub_group_size, if stubs total
8963              more than 2097152 bytes, or nearly 75000 plt call stubs.  */
8964           do
8965             {
8966               prev = PREV_SEC (tail);
8967               /* Set up this stub group.  */
8968               htab->stub_group[tail->id].link_sec = curr;
8969             }
8970           while (tail != curr && (tail = prev) != NULL);
8971
8972           /* But wait, there's more!  Input sections up to stub_group_size
8973              bytes before the stub section can be handled by it too.
8974              Don't do this if we have a really large section after the
8975              stubs, as adding more stubs increases the chance that
8976              branches may not reach into the stub section.  */
8977           if (!stubs_always_before_branch && !big_sec)
8978             {
8979               total = 0;
8980               while (prev != NULL
8981                      && ((total += tail->output_offset - prev->output_offset)
8982                          < stub_group_size)
8983                      && htab->stub_group[prev->id].toc_off == curr_toc)
8984                 {
8985                   tail = prev;
8986                   prev = PREV_SEC (tail);
8987                   htab->stub_group[tail->id].link_sec = curr;
8988                 }
8989             }
8990           tail = prev;
8991         }
8992     }
8993   while (list-- != htab->input_list);
8994   free (htab->input_list);
8995 #undef PREV_SEC
8996 }
8997
8998 /* Determine and set the size of the stub section for a final link.
8999
9000    The basic idea here is to examine all the relocations looking for
9001    PC-relative calls to a target that is unreachable with a "bl"
9002    instruction.  */
9003
9004 bfd_boolean
9005 ppc64_elf_size_stubs (bfd *output_bfd,
9006                       struct bfd_link_info *info,
9007                       bfd_signed_vma group_size,
9008                       asection *(*add_stub_section) (const char *, asection *),
9009                       void (*layout_sections_again) (void))
9010 {
9011   bfd_size_type stub_group_size;
9012   bfd_boolean stubs_always_before_branch;
9013   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9014
9015   /* Stash our params away.  */
9016   htab->add_stub_section = add_stub_section;
9017   htab->layout_sections_again = layout_sections_again;
9018   stubs_always_before_branch = group_size < 0;
9019   if (group_size < 0)
9020     stub_group_size = -group_size;
9021   else
9022     stub_group_size = group_size;
9023   if (stub_group_size == 1)
9024     {
9025       /* Default values.  */
9026       if (stubs_always_before_branch)
9027         {
9028           stub_group_size = 0x1e00000;
9029           if (htab->has_14bit_branch)
9030             stub_group_size = 0x7800;
9031         }
9032       else
9033         {
9034           stub_group_size = 0x1c00000;
9035           if (htab->has_14bit_branch)
9036             stub_group_size = 0x7000;
9037         }
9038     }
9039
9040   group_sections (htab, stub_group_size, stubs_always_before_branch);
9041
9042   while (1)
9043     {
9044       bfd *input_bfd;
9045       unsigned int bfd_indx;
9046       asection *stub_sec;
9047
9048       htab->stub_iteration += 1;
9049
9050       for (input_bfd = info->input_bfds, bfd_indx = 0;
9051            input_bfd != NULL;
9052            input_bfd = input_bfd->link_next, bfd_indx++)
9053         {
9054           Elf_Internal_Shdr *symtab_hdr;
9055           asection *section;
9056           Elf_Internal_Sym *local_syms = NULL;
9057
9058           if (!is_ppc64_elf_target (input_bfd->xvec))
9059             continue;
9060
9061           /* We'll need the symbol table in a second.  */
9062           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9063           if (symtab_hdr->sh_info == 0)
9064             continue;
9065
9066           /* Walk over each section attached to the input bfd.  */
9067           for (section = input_bfd->sections;
9068                section != NULL;
9069                section = section->next)
9070             {
9071               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
9072
9073               /* If there aren't any relocs, then there's nothing more
9074                  to do.  */
9075               if ((section->flags & SEC_RELOC) == 0
9076                   || section->reloc_count == 0)
9077                 continue;
9078
9079               /* If this section is a link-once section that will be
9080                  discarded, then don't create any stubs.  */
9081               if (section->output_section == NULL
9082                   || section->output_section->owner != output_bfd)
9083                 continue;
9084
9085               /* Get the relocs.  */
9086               internal_relocs
9087                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
9088                                              info->keep_memory);
9089               if (internal_relocs == NULL)
9090                 goto error_ret_free_local;
9091
9092               /* Now examine each relocation.  */
9093               irela = internal_relocs;
9094               irelaend = irela + section->reloc_count;
9095               for (; irela < irelaend; irela++)
9096                 {
9097                   enum elf_ppc64_reloc_type r_type;
9098                   unsigned int r_indx;
9099                   enum ppc_stub_type stub_type;
9100                   struct ppc_stub_hash_entry *stub_entry;
9101                   asection *sym_sec, *code_sec;
9102                   bfd_vma sym_value;
9103                   bfd_vma destination;
9104                   bfd_boolean ok_dest;
9105                   struct ppc_link_hash_entry *hash;
9106                   struct ppc_link_hash_entry *fdh;
9107                   struct elf_link_hash_entry *h;
9108                   Elf_Internal_Sym *sym;
9109                   char *stub_name;
9110                   const asection *id_sec;
9111                   long *opd_adjust;
9112
9113                   r_type = ELF64_R_TYPE (irela->r_info);
9114                   r_indx = ELF64_R_SYM (irela->r_info);
9115
9116                   if (r_type >= R_PPC64_max)
9117                     {
9118                       bfd_set_error (bfd_error_bad_value);
9119                       goto error_ret_free_internal;
9120                     }
9121
9122                   /* Only look for stubs on branch instructions.  */
9123                   if (r_type != R_PPC64_REL24
9124                       && r_type != R_PPC64_REL14
9125                       && r_type != R_PPC64_REL14_BRTAKEN
9126                       && r_type != R_PPC64_REL14_BRNTAKEN)
9127                     continue;
9128
9129                   /* Now determine the call target, its name, value,
9130                      section.  */
9131                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9132                                   r_indx, input_bfd))
9133                     goto error_ret_free_internal;
9134                   hash = (struct ppc_link_hash_entry *) h;
9135
9136                   ok_dest = FALSE;
9137                   fdh = NULL;
9138                   sym_value = 0;
9139                   if (hash == NULL)
9140                     {
9141                       sym_value = sym->st_value;
9142                       ok_dest = TRUE;
9143                     }
9144                   else if (hash->elf.root.type == bfd_link_hash_defined
9145                            || hash->elf.root.type == bfd_link_hash_defweak)
9146                     {
9147                       sym_value = hash->elf.root.u.def.value;
9148                       if (sym_sec->output_section != NULL)
9149                         ok_dest = TRUE;
9150                     }
9151                   else if (hash->elf.root.type == bfd_link_hash_undefweak
9152                            || hash->elf.root.type == bfd_link_hash_undefined)
9153                     {
9154                       /* Recognise an old ABI func code entry sym, and
9155                          use the func descriptor sym instead if it is
9156                          defined.  */
9157                       if (hash->elf.root.root.string[0] == '.'
9158                           && (fdh = get_fdh (hash, htab)) != NULL)
9159                         {
9160                           if (fdh->elf.root.type == bfd_link_hash_defined
9161                               || fdh->elf.root.type == bfd_link_hash_defweak)
9162                             {
9163                               sym_sec = fdh->elf.root.u.def.section;
9164                               sym_value = fdh->elf.root.u.def.value;
9165                               if (sym_sec->output_section != NULL)
9166                                 ok_dest = TRUE;
9167                             }
9168                           else
9169                             fdh = NULL;
9170                         }
9171                     }
9172                   else
9173                     {
9174                       bfd_set_error (bfd_error_bad_value);
9175                       goto error_ret_free_internal;
9176                     }
9177
9178                   destination = 0;
9179                   if (ok_dest)
9180                     {
9181                       sym_value += irela->r_addend;
9182                       destination = (sym_value
9183                                      + sym_sec->output_offset
9184                                      + sym_sec->output_section->vma);
9185                     }
9186
9187                   code_sec = sym_sec;
9188                   opd_adjust = get_opd_info (sym_sec);
9189                   if (opd_adjust != NULL)
9190                     {
9191                       bfd_vma dest;
9192
9193                       if (hash == NULL)
9194                         {
9195                           long adjust = opd_adjust[sym_value / 8];
9196                           if (adjust == -1)
9197                             continue;
9198                           sym_value += adjust;
9199                         }
9200                       dest = opd_entry_value (sym_sec, sym_value,
9201                                               &code_sec, &sym_value);
9202                       if (dest != (bfd_vma) -1)
9203                         {
9204                           destination = dest;
9205                           if (fdh != NULL)
9206                             {
9207                               /* Fixup old ABI sym to point at code
9208                                  entry.  */
9209                               hash->elf.root.type = bfd_link_hash_defweak;
9210                               hash->elf.root.u.def.section = code_sec;
9211                               hash->elf.root.u.def.value = sym_value;
9212                             }
9213                         }
9214                     }
9215
9216                   /* Determine what (if any) linker stub is needed.  */
9217                   stub_type = ppc_type_of_stub (section, irela, &hash,
9218                                                 destination);
9219
9220                   if (stub_type != ppc_stub_plt_call)
9221                     {
9222                       /* Check whether we need a TOC adjusting stub.
9223                          Since the linker pastes together pieces from
9224                          different object files when creating the
9225                          _init and _fini functions, it may be that a
9226                          call to what looks like a local sym is in
9227                          fact a call needing a TOC adjustment.  */
9228                       if (code_sec != NULL
9229                           && code_sec->output_section != NULL
9230                           && (htab->stub_group[code_sec->id].toc_off
9231                               != htab->stub_group[section->id].toc_off)
9232                           && (code_sec->has_toc_reloc
9233                               || code_sec->makes_toc_func_call))
9234                         stub_type = ppc_stub_long_branch_r2off;
9235                     }
9236
9237                   if (stub_type == ppc_stub_none)
9238                     continue;
9239
9240                   /* __tls_get_addr calls might be eliminated.  */
9241                   if (stub_type != ppc_stub_plt_call
9242                       && hash != NULL
9243                       && (hash == htab->tls_get_addr
9244                           || hash == htab->tls_get_addr_fd)
9245                       && section->has_tls_reloc
9246                       && irela != internal_relocs)
9247                     {
9248                       /* Get tls info.  */
9249                       char *tls_mask;
9250
9251                       if (!get_tls_mask (&tls_mask, NULL, &local_syms,
9252                                          irela - 1, input_bfd))
9253                         goto error_ret_free_internal;
9254                       if (*tls_mask != 0)
9255                         continue;
9256                     }
9257
9258                   /* Support for grouping stub sections.  */
9259                   id_sec = htab->stub_group[section->id].link_sec;
9260
9261                   /* Get the name of this stub.  */
9262                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
9263                   if (!stub_name)
9264                     goto error_ret_free_internal;
9265
9266                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
9267                                                      stub_name, FALSE, FALSE);
9268                   if (stub_entry != NULL)
9269                     {
9270                       /* The proper stub has already been created.  */
9271                       free (stub_name);
9272                       continue;
9273                     }
9274
9275                   stub_entry = ppc_add_stub (stub_name, section, htab);
9276                   if (stub_entry == NULL)
9277                     {
9278                       free (stub_name);
9279                     error_ret_free_internal:
9280                       if (elf_section_data (section)->relocs == NULL)
9281                         free (internal_relocs);
9282                     error_ret_free_local:
9283                       if (local_syms != NULL
9284                           && (symtab_hdr->contents
9285                               != (unsigned char *) local_syms))
9286                         free (local_syms);
9287                       return FALSE;
9288                     }
9289
9290                   stub_entry->stub_type = stub_type;
9291                   stub_entry->target_value = sym_value;
9292                   stub_entry->target_section = code_sec;
9293                   stub_entry->h = hash;
9294                   stub_entry->addend = irela->r_addend;
9295
9296                   if (stub_entry->h != NULL)
9297                     htab->stub_globals += 1;
9298                 }
9299
9300               /* We're done with the internal relocs, free them.  */
9301               if (elf_section_data (section)->relocs != internal_relocs)
9302                 free (internal_relocs);
9303             }
9304
9305           if (local_syms != NULL
9306               && symtab_hdr->contents != (unsigned char *) local_syms)
9307             {
9308               if (!info->keep_memory)
9309                 free (local_syms);
9310               else
9311                 symtab_hdr->contents = (unsigned char *) local_syms;
9312             }
9313         }
9314
9315       /* We may have added some stubs.  Find out the new size of the
9316          stub sections.  */
9317       for (stub_sec = htab->stub_bfd->sections;
9318            stub_sec != NULL;
9319            stub_sec = stub_sec->next)
9320         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
9321           {
9322             stub_sec->rawsize = stub_sec->size;
9323             stub_sec->size = 0;
9324             stub_sec->reloc_count = 0;
9325           }
9326
9327       htab->brlt->size = 0;
9328       if (htab->relbrlt != NULL)
9329         htab->relbrlt->size = 0;
9330
9331       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
9332
9333       for (stub_sec = htab->stub_bfd->sections;
9334            stub_sec != NULL;
9335            stub_sec = stub_sec->next)
9336         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9337             && stub_sec->rawsize != stub_sec->size)
9338           break;
9339
9340       /* Exit from this loop when no stubs have been added, and no stubs
9341          have changed size.  */
9342       if (stub_sec == NULL)
9343         break;
9344
9345       /* Ask the linker to do its stuff.  */
9346       (*htab->layout_sections_again) ();
9347     }
9348
9349   /* It would be nice to strip htab->brlt from the output if the
9350      section is empty, but it's too late.  If we strip sections here,
9351      the dynamic symbol table is corrupted since the section symbol
9352      for the stripped section isn't written.  */
9353
9354   return TRUE;
9355 }
9356
9357 /* Called after we have determined section placement.  If sections
9358    move, we'll be called again.  Provide a value for TOCstart.  */
9359
9360 bfd_vma
9361 ppc64_elf_toc (bfd *obfd)
9362 {
9363   asection *s;
9364   bfd_vma TOCstart;
9365
9366   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
9367      order.  The TOC starts where the first of these sections starts.  */
9368   s = bfd_get_section_by_name (obfd, ".got");
9369   if (s == NULL)
9370     s = bfd_get_section_by_name (obfd, ".toc");
9371   if (s == NULL)
9372     s = bfd_get_section_by_name (obfd, ".tocbss");
9373   if (s == NULL)
9374     s = bfd_get_section_by_name (obfd, ".plt");
9375   if (s == NULL)
9376     {
9377       /* This may happen for
9378          o  references to TOC base (SYM@toc / TOC[tc0]) without a
9379          .toc directive
9380          o  bad linker script
9381          o --gc-sections and empty TOC sections
9382
9383          FIXME: Warn user?  */
9384
9385       /* Look for a likely section.  We probably won't even be
9386          using TOCstart.  */
9387       for (s = obfd->sections; s != NULL; s = s->next)
9388         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
9389             == (SEC_ALLOC | SEC_SMALL_DATA))
9390           break;
9391       if (s == NULL)
9392         for (s = obfd->sections; s != NULL; s = s->next)
9393           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
9394               == (SEC_ALLOC | SEC_SMALL_DATA))
9395             break;
9396       if (s == NULL)
9397         for (s = obfd->sections; s != NULL; s = s->next)
9398           if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
9399             break;
9400       if (s == NULL)
9401         for (s = obfd->sections; s != NULL; s = s->next)
9402           if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
9403             break;
9404     }
9405
9406   TOCstart = 0;
9407   if (s != NULL)
9408     TOCstart = s->output_section->vma + s->output_offset;
9409
9410   return TOCstart;
9411 }
9412
9413 /* Build all the stubs associated with the current output file.
9414    The stubs are kept in a hash table attached to the main linker
9415    hash table.  This function is called via gldelf64ppc_finish.  */
9416
9417 bfd_boolean
9418 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
9419                        struct bfd_link_info *info,
9420                        char **stats)
9421 {
9422   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9423   asection *stub_sec;
9424   bfd_byte *p;
9425   int stub_sec_count = 0;
9426
9427   htab->emit_stub_syms = emit_stub_syms;
9428
9429   /* Allocate memory to hold the linker stubs.  */
9430   for (stub_sec = htab->stub_bfd->sections;
9431        stub_sec != NULL;
9432        stub_sec = stub_sec->next)
9433     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9434         && stub_sec->size != 0)
9435       {
9436         stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
9437         if (stub_sec->contents == NULL)
9438           return FALSE;
9439         /* We want to check that built size is the same as calculated
9440            size.  rawsize is a convenient location to use.  */
9441         stub_sec->rawsize = stub_sec->size;
9442         stub_sec->size = 0;
9443       }
9444
9445   if (htab->glink != NULL && htab->glink->contents != NULL)
9446     {
9447       unsigned int indx;
9448       bfd_vma plt0;
9449
9450       /* Build the .glink plt call stub.  */
9451       plt0 = (htab->plt->output_section->vma
9452               + htab->plt->output_offset
9453               - (htab->glink->output_section->vma
9454                  + htab->glink->output_offset
9455                  + GLINK_CALL_STUB_SIZE));
9456       if (plt0 + 0x80008000 > 0xffffffff)
9457         {
9458           (*_bfd_error_handler) (_(".glink and .plt too far apart"));
9459           bfd_set_error (bfd_error_bad_value);
9460           return FALSE;
9461         }
9462
9463       if (htab->emit_stub_syms)
9464         {
9465           struct elf_link_hash_entry *h;
9466           h = elf_link_hash_lookup (&htab->elf, "__glink", TRUE, FALSE, FALSE);
9467           if (h == NULL)
9468             return FALSE;
9469           if (h->root.type == bfd_link_hash_new)
9470             {
9471               h->root.type = bfd_link_hash_defined;
9472               h->root.u.def.section = htab->glink;
9473               h->root.u.def.value = 0;
9474               h->ref_regular = 1;
9475               h->def_regular = 1;
9476               h->ref_regular_nonweak = 1;
9477               h->forced_local = 1;
9478               h->non_elf = 0;
9479             }
9480         }
9481       p = htab->glink->contents;
9482       bfd_put_32 (htab->glink->owner, MFCTR_R12, p);
9483       p += 4;
9484       bfd_put_32 (htab->glink->owner, SLDI_R11_R0_3, p);
9485       p += 4;
9486       bfd_put_32 (htab->glink->owner, ADDIC_R2_R0_32K, p);
9487       p += 4;
9488       bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
9489       p += 4;
9490       bfd_put_32 (htab->glink->owner, SRADI_R2_R2_63, p);
9491       p += 4;
9492       bfd_put_32 (htab->glink->owner, SLDI_R11_R0_2, p);
9493       p += 4;
9494       bfd_put_32 (htab->glink->owner, AND_R2_R2_R11, p);
9495       p += 4;
9496       bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
9497       p += 4;
9498       bfd_put_32 (htab->glink->owner, ADD_R12_R12_R2, p);
9499       p += 4;
9500       bfd_put_32 (htab->glink->owner, ADDIS_R12_R12 | PPC_HA (plt0), p);
9501       p += 4;
9502       bfd_put_32 (htab->glink->owner, LD_R11_0R12 | PPC_LO (plt0), p);
9503       p += 4;
9504       bfd_put_32 (htab->glink->owner, ADDI_R12_R12 | PPC_LO (plt0), p);
9505       p += 4;
9506       bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
9507       p += 4;
9508       bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
9509       p += 4;
9510       bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
9511       p += 4;
9512       bfd_put_32 (htab->glink->owner, BCTR, p);
9513       p += 4;
9514
9515       /* Build the .glink lazy link call stubs.  */
9516       indx = 0;
9517       while (p < htab->glink->contents + htab->glink->size)
9518         {
9519           if (indx < 0x8000)
9520             {
9521               bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9522               p += 4;
9523             }
9524           else
9525             {
9526               bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9527               p += 4;
9528               bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9529               p += 4;
9530             }
9531           bfd_put_32 (htab->glink->owner,
9532                       B_DOT | ((htab->glink->contents - p) & 0x3fffffc), p);
9533           indx++;
9534           p += 4;
9535         }
9536       htab->glink->rawsize = p - htab->glink->contents;
9537     }
9538
9539   if (htab->brlt->size != 0)
9540     {
9541       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
9542                                          htab->brlt->size);
9543       if (htab->brlt->contents == NULL)
9544         return FALSE;
9545     }
9546   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
9547     {
9548       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
9549                                             htab->relbrlt->size);
9550       if (htab->relbrlt->contents == NULL)
9551         return FALSE;
9552     }
9553
9554   /* Build the stubs as directed by the stub hash table.  */
9555   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
9556
9557   for (stub_sec = htab->stub_bfd->sections;
9558        stub_sec != NULL;
9559        stub_sec = stub_sec->next)
9560     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
9561       {
9562         stub_sec_count += 1;
9563         if (stub_sec->rawsize != stub_sec->size)
9564           break;
9565       }
9566
9567   if (stub_sec != NULL
9568       || htab->glink->rawsize != htab->glink->size)
9569     {
9570       htab->stub_error = TRUE;
9571       (*_bfd_error_handler) (_("stubs don't match calculated size"));
9572     }
9573
9574   if (htab->stub_error)
9575     return FALSE;
9576
9577   if (stats != NULL)
9578     {
9579       *stats = bfd_malloc (500);
9580       if (*stats == NULL)
9581         return FALSE;
9582
9583       sprintf (*stats, _("linker stubs in %u group%s\n"
9584                          "  branch       %lu\n"
9585                          "  toc adjust   %lu\n"
9586                          "  long branch  %lu\n"
9587                          "  long toc adj %lu\n"
9588                          "  plt call     %lu"),
9589                stub_sec_count,
9590                stub_sec_count == 1 ? "" : "s",
9591                htab->stub_count[ppc_stub_long_branch - 1],
9592                htab->stub_count[ppc_stub_long_branch_r2off - 1],
9593                htab->stub_count[ppc_stub_plt_branch - 1],
9594                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
9595                htab->stub_count[ppc_stub_plt_call - 1]);
9596     }
9597   return TRUE;
9598 }
9599
9600 /* This function undoes the changes made by add_symbol_adjust.  */
9601
9602 static bfd_boolean
9603 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
9604 {
9605   struct ppc_link_hash_entry *eh;
9606
9607   if (h->root.type == bfd_link_hash_indirect)
9608     return TRUE;
9609
9610   if (h->root.type == bfd_link_hash_warning)
9611     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9612
9613   eh = (struct ppc_link_hash_entry *) h;
9614   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
9615     return TRUE;
9616
9617   eh->elf.root.type = bfd_link_hash_undefined;
9618   return TRUE;
9619 }
9620
9621 void
9622 ppc64_elf_restore_symbols (struct bfd_link_info *info)
9623 {
9624   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9625   elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
9626 }
9627
9628 /* What to do when ld finds relocations against symbols defined in
9629    discarded sections.  */
9630
9631 static unsigned int
9632 ppc64_elf_action_discarded (asection *sec)
9633 {
9634   if (strcmp (".opd", sec->name) == 0)
9635     return 0;
9636
9637   if (strcmp (".toc", sec->name) == 0)
9638     return 0;
9639
9640   if (strcmp (".toc1", sec->name) == 0)
9641     return 0;
9642
9643   return _bfd_elf_default_action_discarded (sec);
9644 }
9645
9646 /* The RELOCATE_SECTION function is called by the ELF backend linker
9647    to handle the relocations for a section.
9648
9649    The relocs are always passed as Rela structures; if the section
9650    actually uses Rel structures, the r_addend field will always be
9651    zero.
9652
9653    This function is responsible for adjust the section contents as
9654    necessary, and (if using Rela relocs and generating a
9655    relocatable output file) adjusting the reloc addend as
9656    necessary.
9657
9658    This function does not have to worry about setting the reloc
9659    address or the reloc symbol index.
9660
9661    LOCAL_SYMS is a pointer to the swapped in local symbols.
9662
9663    LOCAL_SECTIONS is an array giving the section in the input file
9664    corresponding to the st_shndx field of each local symbol.
9665
9666    The global hash table entry for the global symbols can be found
9667    via elf_sym_hashes (input_bfd).
9668
9669    When generating relocatable output, this function must handle
9670    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
9671    going to be the section symbol corresponding to the output
9672    section, which means that the addend must be adjusted
9673    accordingly.  */
9674
9675 static bfd_boolean
9676 ppc64_elf_relocate_section (bfd *output_bfd,
9677                             struct bfd_link_info *info,
9678                             bfd *input_bfd,
9679                             asection *input_section,
9680                             bfd_byte *contents,
9681                             Elf_Internal_Rela *relocs,
9682                             Elf_Internal_Sym *local_syms,
9683                             asection **local_sections)
9684 {
9685   struct ppc_link_hash_table *htab;
9686   Elf_Internal_Shdr *symtab_hdr;
9687   struct elf_link_hash_entry **sym_hashes;
9688   Elf_Internal_Rela *rel;
9689   Elf_Internal_Rela *relend;
9690   Elf_Internal_Rela outrel;
9691   bfd_byte *loc;
9692   struct got_entry **local_got_ents;
9693   bfd_vma TOCstart;
9694   bfd_boolean ret = TRUE;
9695   bfd_boolean is_opd;
9696   /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
9697   bfd_boolean is_power4 = FALSE;
9698
9699   /* Initialize howto table if needed.  */
9700   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9701     ppc_howto_init ();
9702
9703   htab = ppc_hash_table (info);
9704
9705   /* Don't relocate stub sections.  */
9706   if (input_section->owner == htab->stub_bfd)
9707     return TRUE;
9708
9709   local_got_ents = elf_local_got_ents (input_bfd);
9710   TOCstart = elf_gp (output_bfd);
9711   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9712   sym_hashes = elf_sym_hashes (input_bfd);
9713   is_opd = ppc64_elf_section_data (input_section)->opd.adjust != NULL;
9714
9715   rel = relocs;
9716   relend = relocs + input_section->reloc_count;
9717   for (; rel < relend; rel++)
9718     {
9719       enum elf_ppc64_reloc_type r_type;
9720       bfd_vma addend, orig_addend;
9721       bfd_reloc_status_type r;
9722       Elf_Internal_Sym *sym;
9723       asection *sec;
9724       struct elf_link_hash_entry *h_elf;
9725       struct ppc_link_hash_entry *h;
9726       struct ppc_link_hash_entry *fdh;
9727       const char *sym_name;
9728       unsigned long r_symndx, toc_symndx;
9729       char tls_mask, tls_gd, tls_type;
9730       char sym_type;
9731       bfd_vma relocation;
9732       bfd_boolean unresolved_reloc;
9733       bfd_boolean warned;
9734       unsigned long insn, mask;
9735       struct ppc_stub_hash_entry *stub_entry;
9736       bfd_vma max_br_offset;
9737       bfd_vma from;
9738
9739       r_type = ELF64_R_TYPE (rel->r_info);
9740       r_symndx = ELF64_R_SYM (rel->r_info);
9741
9742       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
9743          symbol of the previous ADDR64 reloc.  The symbol gives us the
9744          proper TOC base to use.  */
9745       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
9746           && rel != relocs
9747           && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
9748           && is_opd)
9749         r_symndx = ELF64_R_SYM (rel[-1].r_info);
9750
9751       sym = NULL;
9752       sec = NULL;
9753       h_elf = NULL;
9754       sym_name = NULL;
9755       unresolved_reloc = FALSE;
9756       warned = FALSE;
9757       orig_addend = rel->r_addend;
9758
9759       if (r_symndx < symtab_hdr->sh_info)
9760         {
9761           /* It's a local symbol.  */
9762           long *opd_adjust;
9763
9764           sym = local_syms + r_symndx;
9765           sec = local_sections[r_symndx];
9766           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
9767           sym_type = ELF64_ST_TYPE (sym->st_info);
9768           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9769           opd_adjust = get_opd_info (sec);
9770           if (opd_adjust != NULL)
9771             {
9772               long adjust = opd_adjust[(sym->st_value + rel->r_addend) / 8];
9773               if (adjust == -1)
9774                 relocation = 0;
9775               else
9776                 {
9777                   /* If this is a relocation against the opd section sym
9778                      and we have edited .opd, adjust the reloc addend so
9779                      that ld -r and ld --emit-relocs output is correct.
9780                      If it is a reloc against some other .opd symbol,
9781                      then the symbol value will be adjusted later.  */
9782                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9783                     rel->r_addend += adjust;
9784                   else
9785                     relocation += adjust;
9786                 }
9787             }
9788           if (info->relocatable)
9789             continue;
9790         }
9791       else
9792         {
9793           if (info->relocatable)
9794             continue;
9795           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
9796                                    r_symndx, symtab_hdr, sym_hashes,
9797                                    h_elf, sec, relocation,
9798                                    unresolved_reloc, warned);
9799           sym_name = h_elf->root.root.string;
9800           sym_type = h_elf->type;
9801         }
9802       h = (struct ppc_link_hash_entry *) h_elf;
9803
9804       /* TLS optimizations.  Replace instruction sequences and relocs
9805          based on information we collected in tls_optimize.  We edit
9806          RELOCS so that --emit-relocs will output something sensible
9807          for the final instruction stream.  */
9808       tls_mask = 0;
9809       tls_gd = 0;
9810       toc_symndx = 0;
9811       if (IS_PPC64_TLS_RELOC (r_type))
9812         {
9813           if (h != NULL)
9814             tls_mask = h->tls_mask;
9815           else if (local_got_ents != NULL)
9816             {
9817               char *lgot_masks;
9818               lgot_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
9819               tls_mask = lgot_masks[r_symndx];
9820             }
9821           if (tls_mask == 0 && r_type == R_PPC64_TLS)
9822             {
9823               /* Check for toc tls entries.  */
9824               char *toc_tls;
9825
9826               if (!get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
9827                                  rel, input_bfd))
9828                 return FALSE;
9829
9830               if (toc_tls)
9831                 tls_mask = *toc_tls;
9832             }
9833         }
9834
9835       /* Check that tls relocs are used with tls syms, and non-tls
9836          relocs are used with non-tls syms.  */
9837       if (r_symndx != 0
9838           && r_type != R_PPC64_NONE
9839           && (h == NULL
9840               || h->elf.root.type == bfd_link_hash_defined
9841               || h->elf.root.type == bfd_link_hash_defweak)
9842           && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
9843         {
9844           if (r_type == R_PPC64_TLS && tls_mask != 0)
9845             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
9846             ;
9847           else
9848             (*_bfd_error_handler)
9849               (sym_type == STT_TLS
9850                ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
9851                : _("%B(%A+0x%lx): %s used with non-TLS symbol %s"),
9852                input_bfd,
9853                input_section,
9854                (long) rel->r_offset,
9855                ppc64_elf_howto_table[r_type]->name,
9856                sym_name);
9857         }
9858
9859       /* Ensure reloc mapping code below stays sane.  */
9860       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
9861           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
9862           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
9863           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
9864           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
9865           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
9866           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
9867           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
9868           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
9869           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
9870         abort ();
9871
9872       switch (r_type)
9873         {
9874         default:
9875           break;
9876
9877         case R_PPC64_TOC16:
9878         case R_PPC64_TOC16_LO:
9879         case R_PPC64_TOC16_DS:
9880         case R_PPC64_TOC16_LO_DS:
9881           {
9882             /* Check for toc tls entries.  */
9883             char *toc_tls;
9884             int retval;
9885
9886             retval = get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
9887                                    rel, input_bfd);
9888             if (retval == 0)
9889               return FALSE;
9890
9891             if (toc_tls)
9892               {
9893                 tls_mask = *toc_tls;
9894                 if (r_type == R_PPC64_TOC16_DS
9895                     || r_type == R_PPC64_TOC16_LO_DS)
9896                   {
9897                     if (tls_mask != 0
9898                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
9899                       goto toctprel;
9900                   }
9901                 else
9902                   {
9903                     /* If we found a GD reloc pair, then we might be
9904                        doing a GD->IE transition.  */
9905                     if (retval == 2)
9906                       {
9907                         tls_gd = TLS_TPRELGD;
9908                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
9909                           goto tls_get_addr_check;
9910                       }
9911                     else if (retval == 3)
9912                       {
9913                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
9914                           goto tls_get_addr_check;
9915                       }
9916                   }
9917               }
9918           }
9919           break;
9920
9921         case R_PPC64_GOT_TPREL16_DS:
9922         case R_PPC64_GOT_TPREL16_LO_DS:
9923           if (tls_mask != 0
9924               && (tls_mask & TLS_TPREL) == 0)
9925             {
9926             toctprel:
9927               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
9928               insn &= 31 << 21;
9929               insn |= 0x3c0d0000;       /* addis 0,13,0 */
9930               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
9931               r_type = R_PPC64_TPREL16_HA;
9932               if (toc_symndx != 0)
9933                 {
9934                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
9935                   /* We changed the symbol.  Start over in order to
9936                      get h, sym, sec etc. right.  */
9937                   rel--;
9938                   continue;
9939                 }
9940               else
9941                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
9942             }
9943           break;
9944
9945         case R_PPC64_TLS:
9946           if (tls_mask != 0
9947               && (tls_mask & TLS_TPREL) == 0)
9948             {
9949               bfd_vma rtra;
9950               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
9951               if ((insn & ((0x3f << 26) | (31 << 11)))
9952                   == ((31 << 26) | (13 << 11)))
9953                 rtra = insn & ((1 << 26) - (1 << 16));
9954               else if ((insn & ((0x3f << 26) | (31 << 16)))
9955                        == ((31 << 26) | (13 << 16)))
9956                 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
9957               else
9958                 abort ();
9959               if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
9960                 /* add -> addi.  */
9961                 insn = 14 << 26;
9962               else if ((insn & (31 << 1)) == 23 << 1
9963                        && ((insn & (31 << 6)) < 14 << 6
9964                            || ((insn & (31 << 6)) >= 16 << 6
9965                                && (insn & (31 << 6)) < 24 << 6)))
9966                 /* load and store indexed -> dform.  */
9967                 insn = (32 | ((insn >> 6) & 31)) << 26;
9968               else if ((insn & (31 << 1)) == 21 << 1
9969                        && (insn & (0x1a << 6)) == 0)
9970                 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu.  */
9971                 insn = (((58 | ((insn >> 6) & 4)) << 26)
9972                         | ((insn >> 6) & 1));
9973               else if ((insn & (31 << 1)) == 21 << 1
9974                        && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
9975                 /* lwax -> lwa.  */
9976                 insn = (58 << 26) | 2;
9977               else
9978                 abort ();
9979               insn |= rtra;
9980               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
9981               /* Was PPC64_TLS which sits on insn boundary, now
9982                  PPC64_TPREL16_LO which is at insn+2.  */
9983               rel->r_offset += 2;
9984               r_type = R_PPC64_TPREL16_LO;
9985               if (toc_symndx != 0)
9986                 {
9987                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
9988                   /* We changed the symbol.  Start over in order to
9989                      get h, sym, sec etc. right.  */
9990                   rel--;
9991                   continue;
9992                 }
9993               else
9994                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
9995             }
9996           break;
9997
9998         case R_PPC64_GOT_TLSGD16_HI:
9999         case R_PPC64_GOT_TLSGD16_HA:
10000           tls_gd = TLS_TPRELGD;
10001           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
10002             goto tls_gdld_hi;
10003           break;
10004
10005         case R_PPC64_GOT_TLSLD16_HI:
10006         case R_PPC64_GOT_TLSLD16_HA:
10007           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
10008             {
10009             tls_gdld_hi:
10010               if ((tls_mask & tls_gd) != 0)
10011                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
10012                           + R_PPC64_GOT_TPREL16_DS);
10013               else
10014                 {
10015                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
10016                   rel->r_offset -= 2;
10017                   r_type = R_PPC64_NONE;
10018                 }
10019               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10020             }
10021           break;
10022
10023         case R_PPC64_GOT_TLSGD16:
10024         case R_PPC64_GOT_TLSGD16_LO:
10025           tls_gd = TLS_TPRELGD;
10026           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
10027             goto tls_get_addr_check;
10028           break;
10029
10030         case R_PPC64_GOT_TLSLD16:
10031         case R_PPC64_GOT_TLSLD16_LO:
10032           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
10033             {
10034             tls_get_addr_check:
10035               if (rel + 1 < relend)
10036                 {
10037                   enum elf_ppc64_reloc_type r_type2;
10038                   unsigned long r_symndx2;
10039                   struct elf_link_hash_entry *h2;
10040                   bfd_vma insn1, insn2, insn3;
10041                   bfd_vma offset;
10042
10043                   /* The next instruction should be a call to
10044                      __tls_get_addr.  Peek at the reloc to be sure.  */
10045                   r_type2 = ELF64_R_TYPE (rel[1].r_info);
10046                   r_symndx2 = ELF64_R_SYM (rel[1].r_info);
10047                   if (r_symndx2 < symtab_hdr->sh_info
10048                       || (r_type2 != R_PPC64_REL14
10049                           && r_type2 != R_PPC64_REL14_BRTAKEN
10050                           && r_type2 != R_PPC64_REL14_BRNTAKEN
10051                           && r_type2 != R_PPC64_REL24))
10052                     break;
10053
10054                   h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
10055                   while (h2->root.type == bfd_link_hash_indirect
10056                          || h2->root.type == bfd_link_hash_warning)
10057                     h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
10058                   if (h2 == NULL || (h2 != &htab->tls_get_addr->elf
10059                                      && h2 != &htab->tls_get_addr_fd->elf))
10060                     break;
10061
10062                   /* OK, it checks out.  Replace the call.  */
10063                   offset = rel[1].r_offset;
10064                   insn1 = bfd_get_32 (output_bfd,
10065                                       contents + rel->r_offset - 2);
10066                   insn3 = bfd_get_32 (output_bfd,
10067                                       contents + offset + 4);
10068                   if ((tls_mask & tls_gd) != 0)
10069                     {
10070                       /* IE */
10071                       insn1 &= (1 << 26) - (1 << 2);
10072                       insn1 |= 58 << 26;        /* ld */
10073                       insn2 = 0x7c636a14;       /* add 3,3,13 */
10074                       rel[1].r_info = ELF64_R_INFO (r_symndx2, R_PPC64_NONE);
10075                       if ((tls_mask & TLS_EXPLICIT) == 0)
10076                         r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
10077                                   + R_PPC64_GOT_TPREL16_DS);
10078                       else
10079                         r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
10080                       rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10081                     }
10082                   else
10083                     {
10084                       /* LE */
10085                       insn1 = 0x3c6d0000;       /* addis 3,13,0 */
10086                       insn2 = 0x38630000;       /* addi 3,3,0 */
10087                       if (tls_gd == 0)
10088                         {
10089                           /* Was an LD reloc.  */
10090                           r_symndx = 0;
10091                           rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
10092                           rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
10093                         }
10094                       else if (toc_symndx != 0)
10095                         r_symndx = toc_symndx;
10096                       r_type = R_PPC64_TPREL16_HA;
10097                       rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10098                       rel[1].r_info = ELF64_R_INFO (r_symndx,
10099                                                     R_PPC64_TPREL16_LO);
10100                       rel[1].r_offset += 2;
10101                     }
10102                   if (insn3 == NOP
10103                       || insn3 == CROR_151515 || insn3 == CROR_313131)
10104                     {
10105                       insn3 = insn2;
10106                       insn2 = NOP;
10107                       rel[1].r_offset += 4;
10108                     }
10109                   bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
10110                   bfd_put_32 (output_bfd, insn2, contents + offset);
10111                   bfd_put_32 (output_bfd, insn3, contents + offset + 4);
10112                   if (tls_gd == 0 || toc_symndx != 0)
10113                     {
10114                       /* We changed the symbol.  Start over in order
10115                          to get h, sym, sec etc. right.  */
10116                       rel--;
10117                       continue;
10118                     }
10119                 }
10120             }
10121           break;
10122
10123         case R_PPC64_DTPMOD64:
10124           if (rel + 1 < relend
10125               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
10126               && rel[1].r_offset == rel->r_offset + 8)
10127             {
10128               if ((tls_mask & TLS_GD) == 0)
10129                 {
10130                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
10131                   if ((tls_mask & TLS_TPRELGD) != 0)
10132                     r_type = R_PPC64_TPREL64;
10133                   else
10134                     {
10135                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
10136                       r_type = R_PPC64_NONE;
10137                     }
10138                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10139                 }
10140             }
10141           else
10142             {
10143               if ((tls_mask & TLS_LD) == 0)
10144                 {
10145                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
10146                   r_type = R_PPC64_NONE;
10147                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10148                 }
10149             }
10150           break;
10151
10152         case R_PPC64_TPREL64:
10153           if ((tls_mask & TLS_TPREL) == 0)
10154             {
10155               r_type = R_PPC64_NONE;
10156               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10157             }
10158           break;
10159         }
10160
10161       /* Handle other relocations that tweak non-addend part of insn.  */
10162       insn = 0;
10163       max_br_offset = 1 << 25;
10164       addend = rel->r_addend;
10165       switch (r_type)
10166         {
10167         default:
10168           break;
10169
10170           /* Branch taken prediction relocations.  */
10171         case R_PPC64_ADDR14_BRTAKEN:
10172         case R_PPC64_REL14_BRTAKEN:
10173           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
10174           /* Fall thru.  */
10175
10176           /* Branch not taken prediction relocations.  */
10177         case R_PPC64_ADDR14_BRNTAKEN:
10178         case R_PPC64_REL14_BRNTAKEN:
10179           insn |= bfd_get_32 (output_bfd,
10180                               contents + rel->r_offset) & ~(0x01 << 21);
10181           /* Fall thru.  */
10182
10183         case R_PPC64_REL14:
10184           max_br_offset = 1 << 15;
10185           /* Fall thru.  */
10186
10187         case R_PPC64_REL24:
10188           /* Calls to functions with a different TOC, such as calls to
10189              shared objects, need to alter the TOC pointer.  This is
10190              done using a linkage stub.  A REL24 branching to these
10191              linkage stubs needs to be followed by a nop, as the nop
10192              will be replaced with an instruction to restore the TOC
10193              base pointer.  */
10194           stub_entry = NULL;
10195           fdh = h;
10196           if (((h != NULL
10197                 && (((fdh = h->oh) != NULL
10198                      && fdh->elf.plt.plist != NULL)
10199                     || (fdh = h)->elf.plt.plist != NULL))
10200                || (sec != NULL
10201                    && sec->output_section != NULL
10202                    && sec->id <= htab->top_id
10203                    && (htab->stub_group[sec->id].toc_off
10204                        != htab->stub_group[input_section->id].toc_off)))
10205               && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
10206                                                    rel, htab)) != NULL
10207               && (stub_entry->stub_type == ppc_stub_plt_call
10208                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
10209                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
10210             {
10211               bfd_boolean can_plt_call = FALSE;
10212
10213               if (rel->r_offset + 8 <= input_section->size)
10214                 {
10215                   unsigned long nop;
10216                   nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
10217                   if (nop == NOP
10218                       || nop == CROR_151515 || nop == CROR_313131)
10219                     {
10220                       bfd_put_32 (input_bfd, LD_R2_40R1,
10221                                   contents + rel->r_offset + 4);
10222                       can_plt_call = TRUE;
10223                     }
10224                 }
10225
10226               if (!can_plt_call)
10227                 {
10228                   if (stub_entry->stub_type == ppc_stub_plt_call)
10229                     {
10230                       /* If this is a plain branch rather than a branch
10231                          and link, don't require a nop.  However, don't
10232                          allow tail calls in a shared library as they
10233                          will result in r2 being corrupted.  */
10234                       unsigned long br;
10235                       br = bfd_get_32 (input_bfd, contents + rel->r_offset);
10236                       if (info->executable && (br & 1) == 0)
10237                         can_plt_call = TRUE;
10238                       else
10239                         stub_entry = NULL;
10240                     }
10241                   else if (h != NULL
10242                            && strcmp (h->elf.root.root.string,
10243                                       ".__libc_start_main") == 0)
10244                     {
10245                       /* Allow crt1 branch to go via a toc adjusting stub.  */
10246                       can_plt_call = TRUE;
10247                     }
10248                   else
10249                     {
10250                       if (strcmp (input_section->output_section->name,
10251                                   ".init") == 0
10252                           || strcmp (input_section->output_section->name,
10253                                      ".fini") == 0)
10254                         (*_bfd_error_handler)
10255                           (_("%B(%A+0x%lx): automatic multiple TOCs "
10256                              "not supported using your crt files; "
10257                              "recompile with -mminimal-toc or upgrade gcc"),
10258                            input_bfd,
10259                            input_section,
10260                            (long) rel->r_offset);
10261                       else
10262                         (*_bfd_error_handler)
10263                           (_("%B(%A+0x%lx): sibling call optimization to `%s' "
10264                              "does not allow automatic multiple TOCs; "
10265                              "recompile with -mminimal-toc or "
10266                              "-fno-optimize-sibling-calls, "
10267                              "or make `%s' extern"),
10268                            input_bfd,
10269                            input_section,
10270                            (long) rel->r_offset,
10271                            sym_name,
10272                            sym_name);
10273                       bfd_set_error (bfd_error_bad_value);
10274                       ret = FALSE;
10275                     }
10276                 }
10277
10278               if (can_plt_call
10279                   && stub_entry->stub_type == ppc_stub_plt_call)
10280                 unresolved_reloc = FALSE;
10281             }
10282
10283           if (stub_entry == NULL
10284               && get_opd_info (sec) != NULL)
10285             {
10286               /* The branch destination is the value of the opd entry. */
10287               bfd_vma off = (relocation + addend
10288                              - sec->output_section->vma
10289                              - sec->output_offset);
10290               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
10291               if (dest != (bfd_vma) -1)
10292                 {
10293                   relocation = dest;
10294                   addend = 0;
10295                 }
10296             }
10297
10298           /* If the branch is out of reach we ought to have a long
10299              branch stub.  */
10300           from = (rel->r_offset
10301                   + input_section->output_offset
10302                   + input_section->output_section->vma);
10303
10304           if (stub_entry == NULL
10305               && (relocation + addend - from + max_br_offset
10306                   >= 2 * max_br_offset)
10307               && r_type != R_PPC64_ADDR14_BRTAKEN
10308               && r_type != R_PPC64_ADDR14_BRNTAKEN)
10309             stub_entry = ppc_get_stub_entry (input_section, sec, h, rel,
10310                                              htab);
10311
10312           if (stub_entry != NULL)
10313             {
10314               /* Munge up the value and addend so that we call the stub
10315                  rather than the procedure directly.  */
10316               relocation = (stub_entry->stub_offset
10317                             + stub_entry->stub_sec->output_offset
10318                             + stub_entry->stub_sec->output_section->vma);
10319               addend = 0;
10320             }
10321
10322           if (insn != 0)
10323             {
10324               if (is_power4)
10325                 {
10326                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
10327                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
10328                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
10329                   if ((insn & (0x14 << 21)) == (0x04 << 21))
10330                     insn |= 0x02 << 21;
10331                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
10332                     insn |= 0x08 << 21;
10333                   else
10334                     break;
10335                 }
10336               else
10337                 {
10338                   /* Invert 'y' bit if not the default.  */
10339                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
10340                     insn ^= 0x01 << 21;
10341                 }
10342
10343               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
10344             }
10345
10346           /* NOP out calls to undefined weak functions.
10347              We can thus call a weak function without first
10348              checking whether the function is defined.  */
10349           else if (h != NULL
10350                    && h->elf.root.type == bfd_link_hash_undefweak
10351                    && r_type == R_PPC64_REL24
10352                    && relocation == 0
10353                    && addend == 0)
10354             {
10355               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
10356               continue;
10357             }
10358           break;
10359         }
10360
10361       /* Set `addend'.  */
10362       tls_type = 0;
10363       switch (r_type)
10364         {
10365         default:
10366           (*_bfd_error_handler)
10367             (_("%B: unknown relocation type %d for symbol %s"),
10368              input_bfd, (int) r_type, sym_name);
10369
10370           bfd_set_error (bfd_error_bad_value);
10371           ret = FALSE;
10372           continue;
10373
10374         case R_PPC64_NONE:
10375         case R_PPC64_TLS:
10376         case R_PPC64_GNU_VTINHERIT:
10377         case R_PPC64_GNU_VTENTRY:
10378           continue;
10379
10380           /* GOT16 relocations.  Like an ADDR16 using the symbol's
10381              address in the GOT as relocation value instead of the
10382              symbol's value itself.  Also, create a GOT entry for the
10383              symbol and put the symbol value there.  */
10384         case R_PPC64_GOT_TLSGD16:
10385         case R_PPC64_GOT_TLSGD16_LO:
10386         case R_PPC64_GOT_TLSGD16_HI:
10387         case R_PPC64_GOT_TLSGD16_HA:
10388           tls_type = TLS_TLS | TLS_GD;
10389           goto dogot;
10390
10391         case R_PPC64_GOT_TLSLD16:
10392         case R_PPC64_GOT_TLSLD16_LO:
10393         case R_PPC64_GOT_TLSLD16_HI:
10394         case R_PPC64_GOT_TLSLD16_HA:
10395           tls_type = TLS_TLS | TLS_LD;
10396           goto dogot;
10397
10398         case R_PPC64_GOT_TPREL16_DS:
10399         case R_PPC64_GOT_TPREL16_LO_DS:
10400         case R_PPC64_GOT_TPREL16_HI:
10401         case R_PPC64_GOT_TPREL16_HA:
10402           tls_type = TLS_TLS | TLS_TPREL;
10403           goto dogot;
10404
10405         case R_PPC64_GOT_DTPREL16_DS:
10406         case R_PPC64_GOT_DTPREL16_LO_DS:
10407         case R_PPC64_GOT_DTPREL16_HI:
10408         case R_PPC64_GOT_DTPREL16_HA:
10409           tls_type = TLS_TLS | TLS_DTPREL;
10410           goto dogot;
10411
10412         case R_PPC64_GOT16:
10413         case R_PPC64_GOT16_LO:
10414         case R_PPC64_GOT16_HI:
10415         case R_PPC64_GOT16_HA:
10416         case R_PPC64_GOT16_DS:
10417         case R_PPC64_GOT16_LO_DS:
10418         dogot:
10419           {
10420             /* Relocation is to the entry for this symbol in the global
10421                offset table.  */
10422             asection *got;
10423             bfd_vma *offp;
10424             bfd_vma off;
10425             unsigned long indx = 0;
10426
10427             if (tls_type == (TLS_TLS | TLS_LD)
10428                 && (h == NULL
10429                     || !h->elf.def_dynamic))
10430               offp = &ppc64_tlsld_got (input_bfd)->offset;
10431             else
10432               {
10433                 struct got_entry *ent;
10434
10435                 if (h != NULL)
10436                   {
10437                     bfd_boolean dyn = htab->elf.dynamic_sections_created;
10438                     if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
10439                                                           &h->elf)
10440                         || (info->shared
10441                             && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
10442                       /* This is actually a static link, or it is a
10443                          -Bsymbolic link and the symbol is defined
10444                          locally, or the symbol was forced to be local
10445                          because of a version file.  */
10446                       ;
10447                     else
10448                       {
10449                         indx = h->elf.dynindx;
10450                         unresolved_reloc = FALSE;
10451                       }
10452                     ent = h->elf.got.glist;
10453                   }
10454                 else
10455                   {
10456                     if (local_got_ents == NULL)
10457                       abort ();
10458                     ent = local_got_ents[r_symndx];
10459                   }
10460
10461                 for (; ent != NULL; ent = ent->next)
10462                   if (ent->addend == orig_addend
10463                       && ent->owner == input_bfd
10464                       && ent->tls_type == tls_type)
10465                     break;
10466                 if (ent == NULL)
10467                   abort ();
10468                 offp = &ent->got.offset;
10469               }
10470
10471             got = ppc64_elf_tdata (input_bfd)->got;
10472             if (got == NULL)
10473               abort ();
10474
10475             /* The offset must always be a multiple of 8.  We use the
10476                least significant bit to record whether we have already
10477                processed this entry.  */
10478             off = *offp;
10479             if ((off & 1) != 0)
10480               off &= ~1;
10481             else
10482               {
10483                 /* Generate relocs for the dynamic linker, except in
10484                    the case of TLSLD where we'll use one entry per
10485                    module.  */
10486                 asection *relgot = ppc64_elf_tdata (input_bfd)->relgot;
10487
10488                 *offp = off | 1;
10489                 if ((info->shared || indx != 0)
10490                     && (h == NULL
10491                         || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10492                         || h->elf.root.type != bfd_link_hash_undefweak))
10493                   {
10494                     outrel.r_offset = (got->output_section->vma
10495                                        + got->output_offset
10496                                        + off);
10497                     outrel.r_addend = addend;
10498                     if (tls_type & (TLS_LD | TLS_GD))
10499                       {
10500                         outrel.r_addend = 0;
10501                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
10502                         if (tls_type == (TLS_TLS | TLS_GD))
10503                           {
10504                             loc = relgot->contents;
10505                             loc += (relgot->reloc_count++
10506                                     * sizeof (Elf64_External_Rela));
10507                             bfd_elf64_swap_reloca_out (output_bfd,
10508                                                        &outrel, loc);
10509                             outrel.r_offset += 8;
10510                             outrel.r_addend = addend;
10511                             outrel.r_info
10512                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
10513                           }
10514                       }
10515                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
10516                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
10517                     else if (tls_type == (TLS_TLS | TLS_TPREL))
10518                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
10519                     else if (indx == 0)
10520                       {
10521                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_RELATIVE);
10522
10523                         /* Write the .got section contents for the sake
10524                            of prelink.  */
10525                         loc = got->contents + off;
10526                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
10527                                     loc);
10528                       }
10529                     else
10530                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
10531
10532                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
10533                       {
10534                         outrel.r_addend += relocation;
10535                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
10536                           outrel.r_addend -= htab->elf.tls_sec->vma;
10537                       }
10538                     loc = relgot->contents;
10539                     loc += (relgot->reloc_count++
10540                             * sizeof (Elf64_External_Rela));
10541                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10542                   }
10543
10544                 /* Init the .got section contents here if we're not
10545                    emitting a reloc.  */
10546                 else
10547                   {
10548                     relocation += addend;
10549                     if (tls_type == (TLS_TLS | TLS_LD))
10550                       relocation = 1;
10551                     else if (tls_type != 0)
10552                       {
10553                         relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
10554                         if (tls_type == (TLS_TLS | TLS_TPREL))
10555                           relocation += DTP_OFFSET - TP_OFFSET;
10556
10557                         if (tls_type == (TLS_TLS | TLS_GD))
10558                           {
10559                             bfd_put_64 (output_bfd, relocation,
10560                                         got->contents + off + 8);
10561                             relocation = 1;
10562                           }
10563                       }
10564
10565                     bfd_put_64 (output_bfd, relocation,
10566                                 got->contents + off);
10567                   }
10568               }
10569
10570             if (off >= (bfd_vma) -2)
10571               abort ();
10572
10573             relocation = got->output_offset + off;
10574
10575             /* TOC base (r2) is TOC start plus 0x8000.  */
10576             addend = -TOC_BASE_OFF;
10577           }
10578           break;
10579
10580         case R_PPC64_PLT16_HA:
10581         case R_PPC64_PLT16_HI:
10582         case R_PPC64_PLT16_LO:
10583         case R_PPC64_PLT32:
10584         case R_PPC64_PLT64:
10585           /* Relocation is to the entry for this symbol in the
10586              procedure linkage table.  */
10587
10588           /* Resolve a PLT reloc against a local symbol directly,
10589              without using the procedure linkage table.  */
10590           if (h == NULL)
10591             break;
10592
10593           /* It's possible that we didn't make a PLT entry for this
10594              symbol.  This happens when statically linking PIC code,
10595              or when using -Bsymbolic.  Go find a match if there is a
10596              PLT entry.  */
10597           if (htab->plt != NULL)
10598             {
10599               struct plt_entry *ent;
10600               for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
10601                 if (ent->addend == orig_addend
10602                     && ent->plt.offset != (bfd_vma) -1)
10603                   {
10604                     relocation = (htab->plt->output_section->vma
10605                                   + htab->plt->output_offset
10606                                   + ent->plt.offset);
10607                     unresolved_reloc = FALSE;
10608                   }
10609             }
10610           break;
10611
10612         case R_PPC64_TOC:
10613           /* Relocation value is TOC base.  */
10614           relocation = TOCstart;
10615           if (r_symndx == 0)
10616             relocation += htab->stub_group[input_section->id].toc_off;
10617           else if (unresolved_reloc)
10618             ;
10619           else if (sec != NULL && sec->id <= htab->top_id)
10620             relocation += htab->stub_group[sec->id].toc_off;
10621           else
10622             unresolved_reloc = TRUE;
10623           goto dodyn2;
10624
10625           /* TOC16 relocs.  We want the offset relative to the TOC base,
10626              which is the address of the start of the TOC plus 0x8000.
10627              The TOC consists of sections .got, .toc, .tocbss, and .plt,
10628              in this order.  */
10629         case R_PPC64_TOC16:
10630         case R_PPC64_TOC16_LO:
10631         case R_PPC64_TOC16_HI:
10632         case R_PPC64_TOC16_DS:
10633         case R_PPC64_TOC16_LO_DS:
10634         case R_PPC64_TOC16_HA:
10635           addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
10636           break;
10637
10638           /* Relocate against the beginning of the section.  */
10639         case R_PPC64_SECTOFF:
10640         case R_PPC64_SECTOFF_LO:
10641         case R_PPC64_SECTOFF_HI:
10642         case R_PPC64_SECTOFF_DS:
10643         case R_PPC64_SECTOFF_LO_DS:
10644         case R_PPC64_SECTOFF_HA:
10645           if (sec != NULL)
10646             addend -= sec->output_section->vma;
10647           break;
10648
10649         case R_PPC64_REL14:
10650         case R_PPC64_REL14_BRNTAKEN:
10651         case R_PPC64_REL14_BRTAKEN:
10652         case R_PPC64_REL24:
10653           break;
10654
10655         case R_PPC64_TPREL16:
10656         case R_PPC64_TPREL16_LO:
10657         case R_PPC64_TPREL16_HI:
10658         case R_PPC64_TPREL16_HA:
10659         case R_PPC64_TPREL16_DS:
10660         case R_PPC64_TPREL16_LO_DS:
10661         case R_PPC64_TPREL16_HIGHER:
10662         case R_PPC64_TPREL16_HIGHERA:
10663         case R_PPC64_TPREL16_HIGHEST:
10664         case R_PPC64_TPREL16_HIGHESTA:
10665           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
10666           if (info->shared)
10667             /* The TPREL16 relocs shouldn't really be used in shared
10668                libs as they will result in DT_TEXTREL being set, but
10669                support them anyway.  */
10670             goto dodyn;
10671           break;
10672
10673         case R_PPC64_DTPREL16:
10674         case R_PPC64_DTPREL16_LO:
10675         case R_PPC64_DTPREL16_HI:
10676         case R_PPC64_DTPREL16_HA:
10677         case R_PPC64_DTPREL16_DS:
10678         case R_PPC64_DTPREL16_LO_DS:
10679         case R_PPC64_DTPREL16_HIGHER:
10680         case R_PPC64_DTPREL16_HIGHERA:
10681         case R_PPC64_DTPREL16_HIGHEST:
10682         case R_PPC64_DTPREL16_HIGHESTA:
10683           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
10684           break;
10685
10686         case R_PPC64_DTPMOD64:
10687           relocation = 1;
10688           addend = 0;
10689           goto dodyn;
10690
10691         case R_PPC64_TPREL64:
10692           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
10693           goto dodyn;
10694
10695         case R_PPC64_DTPREL64:
10696           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
10697           /* Fall thru */
10698
10699           /* Relocations that may need to be propagated if this is a
10700              dynamic object.  */
10701         case R_PPC64_REL30:
10702         case R_PPC64_REL32:
10703         case R_PPC64_REL64:
10704         case R_PPC64_ADDR14:
10705         case R_PPC64_ADDR14_BRNTAKEN:
10706         case R_PPC64_ADDR14_BRTAKEN:
10707         case R_PPC64_ADDR16:
10708         case R_PPC64_ADDR16_DS:
10709         case R_PPC64_ADDR16_HA:
10710         case R_PPC64_ADDR16_HI:
10711         case R_PPC64_ADDR16_HIGHER:
10712         case R_PPC64_ADDR16_HIGHERA:
10713         case R_PPC64_ADDR16_HIGHEST:
10714         case R_PPC64_ADDR16_HIGHESTA:
10715         case R_PPC64_ADDR16_LO:
10716         case R_PPC64_ADDR16_LO_DS:
10717         case R_PPC64_ADDR24:
10718         case R_PPC64_ADDR32:
10719         case R_PPC64_ADDR64:
10720         case R_PPC64_UADDR16:
10721         case R_PPC64_UADDR32:
10722         case R_PPC64_UADDR64:
10723           /* r_symndx will be zero only for relocs against symbols
10724              from removed linkonce sections, or sections discarded by
10725              a linker script.  */
10726         dodyn:
10727           if (r_symndx == 0)
10728             break;
10729           /* Fall thru.  */
10730
10731         dodyn2:
10732           if ((input_section->flags & SEC_ALLOC) == 0)
10733             break;
10734
10735           if (NO_OPD_RELOCS && is_opd)
10736             break;
10737
10738           if ((info->shared
10739                && (h == NULL
10740                    || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10741                    || h->elf.root.type != bfd_link_hash_undefweak)
10742                && (MUST_BE_DYN_RELOC (r_type)
10743                    || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
10744               || (ELIMINATE_COPY_RELOCS
10745                   && !info->shared
10746                   && h != NULL
10747                   && h->elf.dynindx != -1
10748                   && !h->elf.non_got_ref
10749                   && h->elf.def_dynamic
10750                   && !h->elf.def_regular))
10751             {
10752               Elf_Internal_Rela outrel;
10753               bfd_boolean skip, relocate;
10754               asection *sreloc;
10755               bfd_byte *loc;
10756               bfd_vma out_off;
10757
10758               /* When generating a dynamic object, these relocations
10759                  are copied into the output file to be resolved at run
10760                  time.  */
10761
10762               skip = FALSE;
10763               relocate = FALSE;
10764
10765               out_off = _bfd_elf_section_offset (output_bfd, info,
10766                                                  input_section, rel->r_offset);
10767               if (out_off == (bfd_vma) -1)
10768                 skip = TRUE;
10769               else if (out_off == (bfd_vma) -2)
10770                 skip = TRUE, relocate = TRUE;
10771               out_off += (input_section->output_section->vma
10772                           + input_section->output_offset);
10773               outrel.r_offset = out_off;
10774               outrel.r_addend = rel->r_addend;
10775
10776               /* Optimize unaligned reloc use.  */
10777               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
10778                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
10779                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
10780               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
10781                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
10782                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
10783               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
10784                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
10785                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
10786
10787               if (skip)
10788                 memset (&outrel, 0, sizeof outrel);
10789               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
10790                        && !is_opd
10791                        && r_type != R_PPC64_TOC)
10792                 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
10793               else
10794                 {
10795                   /* This symbol is local, or marked to become local,
10796                      or this is an opd section reloc which must point
10797                      at a local function.  */
10798                   outrel.r_addend += relocation;
10799                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
10800                     {
10801                       if (is_opd && h != NULL)
10802                         {
10803                           /* Lie about opd entries.  This case occurs
10804                              when building shared libraries and we
10805                              reference a function in another shared
10806                              lib.  The same thing happens for a weak
10807                              definition in an application that's
10808                              overridden by a strong definition in a
10809                              shared lib.  (I believe this is a generic
10810                              bug in binutils handling of weak syms.)
10811                              In these cases we won't use the opd
10812                              entry in this lib.  */
10813                           unresolved_reloc = FALSE;
10814                         }
10815                       outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10816
10817                       /* We need to relocate .opd contents for ld.so.
10818                          Prelink also wants simple and consistent rules
10819                          for relocs.  This make all RELATIVE relocs have
10820                          *r_offset equal to r_addend.  */
10821                       relocate = TRUE;
10822                     }
10823                   else
10824                     {
10825                       long indx = 0;
10826
10827                       if (bfd_is_abs_section (sec))
10828                         ;
10829                       else if (sec == NULL || sec->owner == NULL)
10830                         {
10831                           bfd_set_error (bfd_error_bad_value);
10832                           return FALSE;
10833                         }
10834                       else
10835                         {
10836                           asection *osec;
10837
10838                           osec = sec->output_section;
10839                           indx = elf_section_data (osec)->dynindx;
10840
10841                           /* We are turning this relocation into one
10842                              against a section symbol, so subtract out
10843                              the output section's address but not the
10844                              offset of the input section in the output
10845                              section.  */
10846                           outrel.r_addend -= osec->vma;
10847                         }
10848
10849                       outrel.r_info = ELF64_R_INFO (indx, r_type);
10850                     }
10851                 }
10852
10853               sreloc = elf_section_data (input_section)->sreloc;
10854               if (sreloc == NULL)
10855                 abort ();
10856
10857               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
10858                   >= sreloc->size)
10859                 abort ();
10860               loc = sreloc->contents;
10861               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
10862               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10863
10864               /* If this reloc is against an external symbol, it will
10865                  be computed at runtime, so there's no need to do
10866                  anything now.  However, for the sake of prelink ensure
10867                  that the section contents are a known value.  */
10868               if (! relocate)
10869                 {
10870                   unresolved_reloc = FALSE;
10871                   /* The value chosen here is quite arbitrary as ld.so
10872                      ignores section contents except for the special
10873                      case of .opd where the contents might be accessed
10874                      before relocation.  Choose zero, as that won't
10875                      cause reloc overflow.  */
10876                   relocation = 0;
10877                   addend = 0;
10878                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
10879                      to improve backward compatibility with older
10880                      versions of ld.  */
10881                   if (r_type == R_PPC64_ADDR64)
10882                     addend = outrel.r_addend;
10883                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
10884                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
10885                     addend = (input_section->output_section->vma
10886                               + input_section->output_offset
10887                               + rel->r_offset);
10888                 }
10889             }
10890           break;
10891
10892         case R_PPC64_COPY:
10893         case R_PPC64_GLOB_DAT:
10894         case R_PPC64_JMP_SLOT:
10895         case R_PPC64_RELATIVE:
10896           /* We shouldn't ever see these dynamic relocs in relocatable
10897              files.  */
10898           /* Fall through.  */
10899
10900         case R_PPC64_PLTGOT16:
10901         case R_PPC64_PLTGOT16_DS:
10902         case R_PPC64_PLTGOT16_HA:
10903         case R_PPC64_PLTGOT16_HI:
10904         case R_PPC64_PLTGOT16_LO:
10905         case R_PPC64_PLTGOT16_LO_DS:
10906         case R_PPC64_PLTREL32:
10907         case R_PPC64_PLTREL64:
10908           /* These ones haven't been implemented yet.  */
10909
10910           (*_bfd_error_handler)
10911             (_("%B: relocation %s is not supported for symbol %s."),
10912              input_bfd,
10913              ppc64_elf_howto_table[r_type]->name, sym_name);
10914
10915           bfd_set_error (bfd_error_invalid_operation);
10916           ret = FALSE;
10917           continue;
10918         }
10919
10920       /* Do any further special processing.  */
10921       switch (r_type)
10922         {
10923         default:
10924           break;
10925
10926         case R_PPC64_ADDR16_HA:
10927         case R_PPC64_ADDR16_HIGHERA:
10928         case R_PPC64_ADDR16_HIGHESTA:
10929         case R_PPC64_GOT16_HA:
10930         case R_PPC64_PLTGOT16_HA:
10931         case R_PPC64_PLT16_HA:
10932         case R_PPC64_TOC16_HA:
10933         case R_PPC64_SECTOFF_HA:
10934         case R_PPC64_TPREL16_HA:
10935         case R_PPC64_DTPREL16_HA:
10936         case R_PPC64_GOT_TLSGD16_HA:
10937         case R_PPC64_GOT_TLSLD16_HA:
10938         case R_PPC64_GOT_TPREL16_HA:
10939         case R_PPC64_GOT_DTPREL16_HA:
10940         case R_PPC64_TPREL16_HIGHER:
10941         case R_PPC64_TPREL16_HIGHERA:
10942         case R_PPC64_TPREL16_HIGHEST:
10943         case R_PPC64_TPREL16_HIGHESTA:
10944         case R_PPC64_DTPREL16_HIGHER:
10945         case R_PPC64_DTPREL16_HIGHERA:
10946         case R_PPC64_DTPREL16_HIGHEST:
10947         case R_PPC64_DTPREL16_HIGHESTA:
10948           /* It's just possible that this symbol is a weak symbol
10949              that's not actually defined anywhere. In that case,
10950              'sec' would be NULL, and we should leave the symbol
10951              alone (it will be set to zero elsewhere in the link).  */
10952           if (sec != NULL)
10953             /* Add 0x10000 if sign bit in 0:15 is set.
10954                Bits 0:15 are not used.  */
10955             addend += 0x8000;
10956           break;
10957
10958         case R_PPC64_ADDR16_DS:
10959         case R_PPC64_ADDR16_LO_DS:
10960         case R_PPC64_GOT16_DS:
10961         case R_PPC64_GOT16_LO_DS:
10962         case R_PPC64_PLT16_LO_DS:
10963         case R_PPC64_SECTOFF_DS:
10964         case R_PPC64_SECTOFF_LO_DS:
10965         case R_PPC64_TOC16_DS:
10966         case R_PPC64_TOC16_LO_DS:
10967         case R_PPC64_PLTGOT16_DS:
10968         case R_PPC64_PLTGOT16_LO_DS:
10969         case R_PPC64_GOT_TPREL16_DS:
10970         case R_PPC64_GOT_TPREL16_LO_DS:
10971         case R_PPC64_GOT_DTPREL16_DS:
10972         case R_PPC64_GOT_DTPREL16_LO_DS:
10973         case R_PPC64_TPREL16_DS:
10974         case R_PPC64_TPREL16_LO_DS:
10975         case R_PPC64_DTPREL16_DS:
10976         case R_PPC64_DTPREL16_LO_DS:
10977           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
10978           mask = 3;
10979           /* If this reloc is against an lq insn, then the value must be
10980              a multiple of 16.  This is somewhat of a hack, but the
10981              "correct" way to do this by defining _DQ forms of all the
10982              _DS relocs bloats all reloc switches in this file.  It
10983              doesn't seem to make much sense to use any of these relocs
10984              in data, so testing the insn should be safe.  */
10985           if ((insn & (0x3f << 26)) == (56u << 26))
10986             mask = 15;
10987           if (((relocation + addend) & mask) != 0)
10988             {
10989               (*_bfd_error_handler)
10990                 (_("%B: error: relocation %s not a multiple of %d"),
10991                  input_bfd,
10992                  ppc64_elf_howto_table[r_type]->name,
10993                  mask + 1);
10994               bfd_set_error (bfd_error_bad_value);
10995               ret = FALSE;
10996               continue;
10997             }
10998           break;
10999         }
11000
11001       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
11002          because such sections are not SEC_ALLOC and thus ld.so will
11003          not process them.  */
11004       if (unresolved_reloc
11005           && !((input_section->flags & SEC_DEBUGGING) != 0
11006                && h->elf.def_dynamic))
11007         {
11008           (*_bfd_error_handler)
11009             (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
11010              input_bfd,
11011              input_section,
11012              (long) rel->r_offset,
11013              ppc64_elf_howto_table[(int) r_type]->name,
11014              h->elf.root.root.string);
11015           ret = FALSE;
11016         }
11017
11018       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
11019                                     input_bfd,
11020                                     input_section,
11021                                     contents,
11022                                     rel->r_offset,
11023                                     relocation,
11024                                     addend);
11025
11026       if (r != bfd_reloc_ok)
11027         {
11028           if (sym_name == NULL)
11029             sym_name = "(null)";
11030           if (r == bfd_reloc_overflow)
11031             {
11032               if (warned)
11033                 continue;
11034               if (h != NULL
11035                   && h->elf.root.type == bfd_link_hash_undefweak
11036                   && ppc64_elf_howto_table[r_type]->pc_relative)
11037                 {
11038                   /* Assume this is a call protected by other code that
11039                      detects the symbol is undefined.  If this is the case,
11040                      we can safely ignore the overflow.  If not, the
11041                      program is hosed anyway, and a little warning isn't
11042                      going to help.  */
11043
11044                   continue;
11045                 }
11046
11047               if (!((*info->callbacks->reloc_overflow)
11048                     (info, (h ? &h->elf.root : NULL), sym_name,
11049                      ppc64_elf_howto_table[r_type]->name,
11050                      orig_addend, input_bfd, input_section, rel->r_offset)))
11051                 return FALSE;
11052             }
11053           else
11054             {
11055               (*_bfd_error_handler)
11056                 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
11057                  input_bfd,
11058                  input_section,
11059                  (long) rel->r_offset,
11060                  ppc64_elf_howto_table[r_type]->name,
11061                  sym_name,
11062                  (int) r);
11063               ret = FALSE;
11064             }
11065         }
11066     }
11067
11068   /* If we're emitting relocations, then shortly after this function
11069      returns, reloc offsets and addends for this section will be
11070      adjusted.  Worse, reloc symbol indices will be for the output
11071      file rather than the input.  Save a copy of the relocs for
11072      opd_entry_value.  */
11073   if (is_opd && (info->emitrelocations || info->relocatable))
11074     {
11075       bfd_size_type amt;
11076       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
11077       rel = bfd_alloc (input_bfd, amt);
11078       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
11079       ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
11080       if (rel == NULL)
11081         return FALSE;
11082       memcpy (rel, relocs, amt);
11083     }
11084   return ret;
11085 }
11086
11087 /* Adjust the value of any local symbols in opd sections.  */
11088
11089 static bfd_boolean
11090 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
11091                               const char *name ATTRIBUTE_UNUSED,
11092                               Elf_Internal_Sym *elfsym,
11093                               asection *input_sec,
11094                               struct elf_link_hash_entry *h)
11095 {
11096   long *opd_adjust, adjust;
11097   bfd_vma value;
11098
11099   if (h != NULL)
11100     return TRUE;
11101
11102   opd_adjust = get_opd_info (input_sec);
11103   if (opd_adjust == NULL)
11104     return TRUE;
11105
11106   value = elfsym->st_value - input_sec->output_offset;
11107   if (!info->relocatable)
11108     value -= input_sec->output_section->vma;
11109
11110   adjust = opd_adjust[value / 8];
11111   if (adjust == -1)
11112     elfsym->st_value = 0;
11113   else
11114     elfsym->st_value += adjust;
11115   return TRUE;
11116 }
11117
11118 /* Finish up dynamic symbol handling.  We set the contents of various
11119    dynamic sections here.  */
11120
11121 static bfd_boolean
11122 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
11123                                  struct bfd_link_info *info,
11124                                  struct elf_link_hash_entry *h,
11125                                  Elf_Internal_Sym *sym)
11126 {
11127   struct ppc_link_hash_table *htab;
11128   bfd *dynobj;
11129   struct plt_entry *ent;
11130   Elf_Internal_Rela rela;
11131   bfd_byte *loc;
11132
11133   htab = ppc_hash_table (info);
11134   dynobj = htab->elf.dynobj;
11135
11136   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
11137     if (ent->plt.offset != (bfd_vma) -1)
11138       {
11139         /* This symbol has an entry in the procedure linkage
11140            table.  Set it up.  */
11141
11142         if (htab->plt == NULL
11143             || htab->relplt == NULL
11144             || htab->glink == NULL)
11145           abort ();
11146
11147         /* Create a JMP_SLOT reloc to inform the dynamic linker to
11148            fill in the PLT entry.  */
11149         rela.r_offset = (htab->plt->output_section->vma
11150                          + htab->plt->output_offset
11151                          + ent->plt.offset);
11152         rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
11153         rela.r_addend = ent->addend;
11154
11155         loc = htab->relplt->contents;
11156         loc += ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
11157                 * sizeof (Elf64_External_Rela));
11158         bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
11159       }
11160
11161   if (h->needs_copy)
11162     {
11163       Elf_Internal_Rela rela;
11164       bfd_byte *loc;
11165
11166       /* This symbol needs a copy reloc.  Set it up.  */
11167
11168       if (h->dynindx == -1
11169           || (h->root.type != bfd_link_hash_defined
11170               && h->root.type != bfd_link_hash_defweak)
11171           || htab->relbss == NULL)
11172         abort ();
11173
11174       rela.r_offset = (h->root.u.def.value
11175                        + h->root.u.def.section->output_section->vma
11176                        + h->root.u.def.section->output_offset);
11177       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
11178       rela.r_addend = 0;
11179       loc = htab->relbss->contents;
11180       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
11181       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
11182     }
11183
11184   /* Mark some specially defined symbols as absolute.  */
11185   if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
11186     sym->st_shndx = SHN_ABS;
11187
11188   return TRUE;
11189 }
11190
11191 /* Used to decide how to sort relocs in an optimal manner for the
11192    dynamic linker, before writing them out.  */
11193
11194 static enum elf_reloc_type_class
11195 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
11196 {
11197   enum elf_ppc64_reloc_type r_type;
11198
11199   r_type = ELF64_R_TYPE (rela->r_info);
11200   switch (r_type)
11201     {
11202     case R_PPC64_RELATIVE:
11203       return reloc_class_relative;
11204     case R_PPC64_JMP_SLOT:
11205       return reloc_class_plt;
11206     case R_PPC64_COPY:
11207       return reloc_class_copy;
11208     default:
11209       return reloc_class_normal;
11210     }
11211 }
11212
11213 /* Finish up the dynamic sections.  */
11214
11215 static bfd_boolean
11216 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
11217                                    struct bfd_link_info *info)
11218 {
11219   struct ppc_link_hash_table *htab;
11220   bfd *dynobj;
11221   asection *sdyn;
11222
11223   htab = ppc_hash_table (info);
11224   dynobj = htab->elf.dynobj;
11225   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
11226
11227   if (htab->elf.dynamic_sections_created)
11228     {
11229       Elf64_External_Dyn *dyncon, *dynconend;
11230
11231       if (sdyn == NULL || htab->got == NULL)
11232         abort ();
11233
11234       dyncon = (Elf64_External_Dyn *) sdyn->contents;
11235       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
11236       for (; dyncon < dynconend; dyncon++)
11237         {
11238           Elf_Internal_Dyn dyn;
11239           asection *s;
11240
11241           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
11242
11243           switch (dyn.d_tag)
11244             {
11245             default:
11246               continue;
11247
11248             case DT_PPC64_GLINK:
11249               s = htab->glink;
11250               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11251               /* We stupidly defined DT_PPC64_GLINK to be the start
11252                  of glink rather than the first entry point, which is
11253                  what ld.so needs, and now have a bigger stub to
11254                  support automatic multiple TOCs.  */
11255               dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
11256               break;
11257
11258             case DT_PPC64_OPD:
11259               s = bfd_get_section_by_name (output_bfd, ".opd");
11260               if (s == NULL)
11261                 continue;
11262               dyn.d_un.d_ptr = s->vma;
11263               break;
11264
11265             case DT_PPC64_OPDSZ:
11266               s = bfd_get_section_by_name (output_bfd, ".opd");
11267               if (s == NULL)
11268                 continue;
11269               dyn.d_un.d_val = s->size;
11270               break;
11271
11272             case DT_PLTGOT:
11273               s = htab->plt;
11274               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11275               break;
11276
11277             case DT_JMPREL:
11278               s = htab->relplt;
11279               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11280               break;
11281
11282             case DT_PLTRELSZ:
11283               dyn.d_un.d_val = htab->relplt->size;
11284               break;
11285
11286             case DT_RELASZ:
11287               /* Don't count procedure linkage table relocs in the
11288                  overall reloc count.  */
11289               s = htab->relplt;
11290               if (s == NULL)
11291                 continue;
11292               dyn.d_un.d_val -= s->size;
11293               break;
11294
11295             case DT_RELA:
11296               /* We may not be using the standard ELF linker script.
11297                  If .rela.plt is the first .rela section, we adjust
11298                  DT_RELA to not include it.  */
11299               s = htab->relplt;
11300               if (s == NULL)
11301                 continue;
11302               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
11303                 continue;
11304               dyn.d_un.d_ptr += s->size;
11305               break;
11306             }
11307
11308           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
11309         }
11310     }
11311
11312   if (htab->got != NULL && htab->got->size != 0)
11313     {
11314       /* Fill in the first entry in the global offset table.
11315          We use it to hold the link-time TOCbase.  */
11316       bfd_put_64 (output_bfd,
11317                   elf_gp (output_bfd) + TOC_BASE_OFF,
11318                   htab->got->contents);
11319
11320       /* Set .got entry size.  */
11321       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
11322     }
11323
11324   if (htab->plt != NULL && htab->plt->size != 0)
11325     {
11326       /* Set .plt entry size.  */
11327       elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
11328         = PLT_ENTRY_SIZE;
11329     }
11330
11331   /* We need to handle writing out multiple GOT sections ourselves,
11332      since we didn't add them to DYNOBJ.  We know dynobj is the first
11333      bfd.  */
11334   while ((dynobj = dynobj->link_next) != NULL)
11335     {
11336       asection *s;
11337
11338       if (!is_ppc64_elf_target (dynobj->xvec))
11339         continue;
11340
11341       s = ppc64_elf_tdata (dynobj)->got;
11342       if (s != NULL
11343           && s->size != 0
11344           && s->output_section != bfd_abs_section_ptr
11345           && !bfd_set_section_contents (output_bfd, s->output_section,
11346                                         s->contents, s->output_offset,
11347                                         s->size))
11348         return FALSE;
11349       s = ppc64_elf_tdata (dynobj)->relgot;
11350       if (s != NULL
11351           && s->size != 0
11352           && s->output_section != bfd_abs_section_ptr
11353           && !bfd_set_section_contents (output_bfd, s->output_section,
11354                                         s->contents, s->output_offset,
11355                                         s->size))
11356         return FALSE;
11357     }
11358
11359   return TRUE;
11360 }
11361
11362 #include "elf64-target.h"