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