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