include/elf/
[external/binutils.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3    2009, 2010 Free Software Foundation, Inc.
4    Written by Linus Nordberg, Swox AB <info@swox.com>,
5    based on elf32-ppc.c by Ian Lance Taylor.
6    Largely rewritten by Alan Modra.
7
8    This file is part of BFD, the Binary File Descriptor library.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License along
21    with this program; if not, write to the Free Software Foundation, Inc.,
22    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
23
24
25 /* The 64-bit PowerPC ELF ABI may be found at
26    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
28
29 #include "sysdep.h"
30 #include <stdarg.h>
31 #include "bfd.h"
32 #include "bfdlink.h"
33 #include "libbfd.h"
34 #include "elf-bfd.h"
35 #include "elf/ppc64.h"
36 #include "elf64-ppc.h"
37
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 #define elf_backend_post_process_headers      _bfd_elf_set_osabi
116
117 /* The name of the dynamic interpreter.  This is put in the .interp
118    section.  */
119 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
120
121 /* The size in bytes of an entry in the procedure linkage table.  */
122 #define PLT_ENTRY_SIZE 24
123
124 /* The initial size of the plt reserved for the dynamic linker.  */
125 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
126
127 /* TOC base pointers offset from start of TOC.  */
128 #define TOC_BASE_OFF    0x8000
129
130 /* Offset of tp and dtp pointers from start of TLS block.  */
131 #define TP_OFFSET       0x7000
132 #define DTP_OFFSET      0x8000
133
134 /* .plt call stub instructions.  The normal stub is like this, but
135    sometimes the .plt entry crosses a 64k boundary and we need to
136    insert an addi to adjust r12.  */
137 #define PLT_CALL_STUB_SIZE (7*4)
138 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha     */
139 #define STD_R2_40R1     0xf8410028      /* std   %r2,40(%r1)         */
140 #define LD_R11_0R12     0xe96c0000      /* ld    %r11,xxx+0@l(%r12)  */
141 #define MTCTR_R11       0x7d6903a6      /* mtctr %r11                */
142 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,xxx+8@l(%r12)   */
143                                         /* ld    %r11,xxx+16@l(%r12) */
144 #define BCTR            0x4e800420      /* bctr                      */
145
146
147 #define ADDIS_R12_R12   0x3d8c0000      /* addis %r12,%r12,off@ha  */
148 #define ADDI_R12_R12    0x398c0000      /* addi %r12,%r12,off@l  */
149 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
150 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
151
152 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
153 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
154
155 #define LD_R2_40R1      0xe8410028      /* ld    %r2,40(%r1)     */
156
157 /* glink call stub instructions.  We enter with the index in R0.  */
158 #define GLINK_CALL_STUB_SIZE (16*4)
159                                         /* 0:                           */
160                                         /*  .quad plt0-1f               */
161                                         /* __glink:                     */
162 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
163 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
164                                         /* 1:                           */
165 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
166 #define LD_R2_M16R11    0xe84bfff0      /*  ld %2,(0b-1b)(%11)          */
167 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
168 #define ADD_R12_R2_R11  0x7d825a14      /*  add %12,%2,%11              */
169                                         /*  ld %11,0(%12)               */
170                                         /*  ld %2,8(%12)                */
171                                         /*  mtctr %11                   */
172                                         /*  ld %11,16(%12)              */
173                                         /*  bctr                        */
174
175 /* Pad with this.  */
176 #define NOP             0x60000000
177
178 /* Some other nops.  */
179 #define CROR_151515     0x4def7b82
180 #define CROR_313131     0x4ffffb82
181
182 /* .glink entries for the first 32k functions are two instructions.  */
183 #define LI_R0_0         0x38000000      /* li    %r0,0          */
184 #define B_DOT           0x48000000      /* b     .              */
185
186 /* After that, we need two instructions to load the index, followed by
187    a branch.  */
188 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
189 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
190
191 /* Instructions used by the save and restore reg functions.  */
192 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
193 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
194 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
195 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
196 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
197 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
198 #define LI_R12_0        0x39800000      /* li    %r12,0         */
199 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
200 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
201 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
202 #define BLR             0x4e800020      /* blr                  */
203
204 /* Since .opd is an array of descriptors and each entry will end up
205    with identical R_PPC64_RELATIVE relocs, there is really no need to
206    propagate .opd relocs;  The dynamic linker should be taught to
207    relocate .opd without reloc entries.  */
208 #ifndef NO_OPD_RELOCS
209 #define NO_OPD_RELOCS 0
210 #endif
211 \f
212 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
213
214 /* Relocation HOWTO's.  */
215 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
216
217 static reloc_howto_type ppc64_elf_howto_raw[] = {
218   /* This reloc does nothing.  */
219   HOWTO (R_PPC64_NONE,          /* type */
220          0,                     /* rightshift */
221          2,                     /* size (0 = byte, 1 = short, 2 = long) */
222          32,                    /* bitsize */
223          FALSE,                 /* pc_relative */
224          0,                     /* bitpos */
225          complain_overflow_dont, /* complain_on_overflow */
226          bfd_elf_generic_reloc, /* special_function */
227          "R_PPC64_NONE",        /* name */
228          FALSE,                 /* partial_inplace */
229          0,                     /* src_mask */
230          0,                     /* dst_mask */
231          FALSE),                /* pcrel_offset */
232
233   /* A standard 32 bit relocation.  */
234   HOWTO (R_PPC64_ADDR32,        /* type */
235          0,                     /* rightshift */
236          2,                     /* size (0 = byte, 1 = short, 2 = long) */
237          32,                    /* bitsize */
238          FALSE,                 /* pc_relative */
239          0,                     /* bitpos */
240          complain_overflow_bitfield, /* complain_on_overflow */
241          bfd_elf_generic_reloc, /* special_function */
242          "R_PPC64_ADDR32",      /* name */
243          FALSE,                 /* partial_inplace */
244          0,                     /* src_mask */
245          0xffffffff,            /* dst_mask */
246          FALSE),                /* pcrel_offset */
247
248   /* An absolute 26 bit branch; the lower two bits must be zero.
249      FIXME: we don't check that, we just clear them.  */
250   HOWTO (R_PPC64_ADDR24,        /* type */
251          0,                     /* rightshift */
252          2,                     /* size (0 = byte, 1 = short, 2 = long) */
253          26,                    /* bitsize */
254          FALSE,                 /* pc_relative */
255          0,                     /* bitpos */
256          complain_overflow_bitfield, /* complain_on_overflow */
257          bfd_elf_generic_reloc, /* special_function */
258          "R_PPC64_ADDR24",      /* name */
259          FALSE,                 /* partial_inplace */
260          0,                     /* src_mask */
261          0x03fffffc,            /* dst_mask */
262          FALSE),                /* pcrel_offset */
263
264   /* A standard 16 bit relocation.  */
265   HOWTO (R_PPC64_ADDR16,        /* type */
266          0,                     /* rightshift */
267          1,                     /* size (0 = byte, 1 = short, 2 = long) */
268          16,                    /* bitsize */
269          FALSE,                 /* pc_relative */
270          0,                     /* bitpos */
271          complain_overflow_bitfield, /* complain_on_overflow */
272          bfd_elf_generic_reloc, /* special_function */
273          "R_PPC64_ADDR16",      /* name */
274          FALSE,                 /* partial_inplace */
275          0,                     /* src_mask */
276          0xffff,                /* dst_mask */
277          FALSE),                /* pcrel_offset */
278
279   /* A 16 bit relocation without overflow.  */
280   HOWTO (R_PPC64_ADDR16_LO,     /* type */
281          0,                     /* rightshift */
282          1,                     /* size (0 = byte, 1 = short, 2 = long) */
283          16,                    /* bitsize */
284          FALSE,                 /* pc_relative */
285          0,                     /* bitpos */
286          complain_overflow_dont,/* complain_on_overflow */
287          bfd_elf_generic_reloc, /* special_function */
288          "R_PPC64_ADDR16_LO",   /* name */
289          FALSE,                 /* partial_inplace */
290          0,                     /* src_mask */
291          0xffff,                /* dst_mask */
292          FALSE),                /* pcrel_offset */
293
294   /* Bits 16-31 of an address.  */
295   HOWTO (R_PPC64_ADDR16_HI,     /* type */
296          16,                    /* rightshift */
297          1,                     /* size (0 = byte, 1 = short, 2 = long) */
298          16,                    /* bitsize */
299          FALSE,                 /* pc_relative */
300          0,                     /* bitpos */
301          complain_overflow_dont, /* complain_on_overflow */
302          bfd_elf_generic_reloc, /* special_function */
303          "R_PPC64_ADDR16_HI",   /* name */
304          FALSE,                 /* partial_inplace */
305          0,                     /* src_mask */
306          0xffff,                /* dst_mask */
307          FALSE),                /* pcrel_offset */
308
309   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
310      bits, treated as a signed number, is negative.  */
311   HOWTO (R_PPC64_ADDR16_HA,     /* type */
312          16,                    /* rightshift */
313          1,                     /* size (0 = byte, 1 = short, 2 = long) */
314          16,                    /* bitsize */
315          FALSE,                 /* pc_relative */
316          0,                     /* bitpos */
317          complain_overflow_dont, /* complain_on_overflow */
318          ppc64_elf_ha_reloc,    /* special_function */
319          "R_PPC64_ADDR16_HA",   /* name */
320          FALSE,                 /* partial_inplace */
321          0,                     /* src_mask */
322          0xffff,                /* dst_mask */
323          FALSE),                /* pcrel_offset */
324
325   /* An absolute 16 bit branch; the lower two bits must be zero.
326      FIXME: we don't check that, we just clear them.  */
327   HOWTO (R_PPC64_ADDR14,        /* type */
328          0,                     /* rightshift */
329          2,                     /* size (0 = byte, 1 = short, 2 = long) */
330          16,                    /* bitsize */
331          FALSE,                 /* pc_relative */
332          0,                     /* bitpos */
333          complain_overflow_bitfield, /* complain_on_overflow */
334          ppc64_elf_branch_reloc, /* special_function */
335          "R_PPC64_ADDR14",      /* name */
336          FALSE,                 /* partial_inplace */
337          0,                     /* src_mask */
338          0x0000fffc,            /* dst_mask */
339          FALSE),                /* pcrel_offset */
340
341   /* An absolute 16 bit branch, for which bit 10 should be set to
342      indicate that the branch is expected to be taken.  The lower two
343      bits must be zero.  */
344   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
345          0,                     /* rightshift */
346          2,                     /* size (0 = byte, 1 = short, 2 = long) */
347          16,                    /* bitsize */
348          FALSE,                 /* pc_relative */
349          0,                     /* bitpos */
350          complain_overflow_bitfield, /* complain_on_overflow */
351          ppc64_elf_brtaken_reloc, /* special_function */
352          "R_PPC64_ADDR14_BRTAKEN",/* name */
353          FALSE,                 /* partial_inplace */
354          0,                     /* src_mask */
355          0x0000fffc,            /* dst_mask */
356          FALSE),                /* pcrel_offset */
357
358   /* An absolute 16 bit branch, for which bit 10 should be set to
359      indicate that the branch is not expected to be taken.  The lower
360      two bits must be zero.  */
361   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
362          0,                     /* rightshift */
363          2,                     /* size (0 = byte, 1 = short, 2 = long) */
364          16,                    /* bitsize */
365          FALSE,                 /* pc_relative */
366          0,                     /* bitpos */
367          complain_overflow_bitfield, /* complain_on_overflow */
368          ppc64_elf_brtaken_reloc, /* special_function */
369          "R_PPC64_ADDR14_BRNTAKEN",/* name */
370          FALSE,                 /* partial_inplace */
371          0,                     /* src_mask */
372          0x0000fffc,            /* dst_mask */
373          FALSE),                /* pcrel_offset */
374
375   /* A relative 26 bit branch; the lower two bits must be zero.  */
376   HOWTO (R_PPC64_REL24,         /* type */
377          0,                     /* rightshift */
378          2,                     /* size (0 = byte, 1 = short, 2 = long) */
379          26,                    /* bitsize */
380          TRUE,                  /* pc_relative */
381          0,                     /* bitpos */
382          complain_overflow_signed, /* complain_on_overflow */
383          ppc64_elf_branch_reloc, /* special_function */
384          "R_PPC64_REL24",       /* name */
385          FALSE,                 /* partial_inplace */
386          0,                     /* src_mask */
387          0x03fffffc,            /* dst_mask */
388          TRUE),                 /* pcrel_offset */
389
390   /* A relative 16 bit branch; the lower two bits must be zero.  */
391   HOWTO (R_PPC64_REL14,         /* type */
392          0,                     /* rightshift */
393          2,                     /* size (0 = byte, 1 = short, 2 = long) */
394          16,                    /* bitsize */
395          TRUE,                  /* pc_relative */
396          0,                     /* bitpos */
397          complain_overflow_signed, /* complain_on_overflow */
398          ppc64_elf_branch_reloc, /* special_function */
399          "R_PPC64_REL14",       /* name */
400          FALSE,                 /* partial_inplace */
401          0,                     /* src_mask */
402          0x0000fffc,            /* dst_mask */
403          TRUE),                 /* pcrel_offset */
404
405   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
406      the branch is expected to be taken.  The lower two bits must be
407      zero.  */
408   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
409          0,                     /* rightshift */
410          2,                     /* size (0 = byte, 1 = short, 2 = long) */
411          16,                    /* bitsize */
412          TRUE,                  /* pc_relative */
413          0,                     /* bitpos */
414          complain_overflow_signed, /* complain_on_overflow */
415          ppc64_elf_brtaken_reloc, /* special_function */
416          "R_PPC64_REL14_BRTAKEN", /* name */
417          FALSE,                 /* partial_inplace */
418          0,                     /* src_mask */
419          0x0000fffc,            /* dst_mask */
420          TRUE),                 /* pcrel_offset */
421
422   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
423      the branch is not expected to be taken.  The lower two bits must
424      be zero.  */
425   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
426          0,                     /* rightshift */
427          2,                     /* size (0 = byte, 1 = short, 2 = long) */
428          16,                    /* bitsize */
429          TRUE,                  /* pc_relative */
430          0,                     /* bitpos */
431          complain_overflow_signed, /* complain_on_overflow */
432          ppc64_elf_brtaken_reloc, /* special_function */
433          "R_PPC64_REL14_BRNTAKEN",/* name */
434          FALSE,                 /* partial_inplace */
435          0,                     /* src_mask */
436          0x0000fffc,            /* dst_mask */
437          TRUE),                 /* pcrel_offset */
438
439   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
440      symbol.  */
441   HOWTO (R_PPC64_GOT16,         /* type */
442          0,                     /* rightshift */
443          1,                     /* size (0 = byte, 1 = short, 2 = long) */
444          16,                    /* bitsize */
445          FALSE,                 /* pc_relative */
446          0,                     /* bitpos */
447          complain_overflow_signed, /* complain_on_overflow */
448          ppc64_elf_unhandled_reloc, /* special_function */
449          "R_PPC64_GOT16",       /* name */
450          FALSE,                 /* partial_inplace */
451          0,                     /* src_mask */
452          0xffff,                /* dst_mask */
453          FALSE),                /* pcrel_offset */
454
455   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
456      the symbol.  */
457   HOWTO (R_PPC64_GOT16_LO,      /* type */
458          0,                     /* rightshift */
459          1,                     /* size (0 = byte, 1 = short, 2 = long) */
460          16,                    /* bitsize */
461          FALSE,                 /* pc_relative */
462          0,                     /* bitpos */
463          complain_overflow_dont, /* complain_on_overflow */
464          ppc64_elf_unhandled_reloc, /* special_function */
465          "R_PPC64_GOT16_LO",    /* name */
466          FALSE,                 /* partial_inplace */
467          0,                     /* src_mask */
468          0xffff,                /* dst_mask */
469          FALSE),                /* pcrel_offset */
470
471   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
472      the symbol.  */
473   HOWTO (R_PPC64_GOT16_HI,      /* type */
474          16,                    /* rightshift */
475          1,                     /* size (0 = byte, 1 = short, 2 = long) */
476          16,                    /* bitsize */
477          FALSE,                 /* pc_relative */
478          0,                     /* bitpos */
479          complain_overflow_dont,/* complain_on_overflow */
480          ppc64_elf_unhandled_reloc, /* special_function */
481          "R_PPC64_GOT16_HI",    /* name */
482          FALSE,                 /* partial_inplace */
483          0,                     /* src_mask */
484          0xffff,                /* dst_mask */
485          FALSE),                /* pcrel_offset */
486
487   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
488      the symbol.  */
489   HOWTO (R_PPC64_GOT16_HA,      /* type */
490          16,                    /* rightshift */
491          1,                     /* size (0 = byte, 1 = short, 2 = long) */
492          16,                    /* bitsize */
493          FALSE,                 /* pc_relative */
494          0,                     /* bitpos */
495          complain_overflow_dont,/* complain_on_overflow */
496          ppc64_elf_unhandled_reloc, /* special_function */
497          "R_PPC64_GOT16_HA",    /* name */
498          FALSE,                 /* partial_inplace */
499          0,                     /* src_mask */
500          0xffff,                /* dst_mask */
501          FALSE),                /* pcrel_offset */
502
503   /* This is used only by the dynamic linker.  The symbol should exist
504      both in the object being run and in some shared library.  The
505      dynamic linker copies the data addressed by the symbol from the
506      shared library into the object, because the object being
507      run has to have the data at some particular address.  */
508   HOWTO (R_PPC64_COPY,          /* type */
509          0,                     /* rightshift */
510          0,                     /* this one is variable size */
511          0,                     /* bitsize */
512          FALSE,                 /* pc_relative */
513          0,                     /* bitpos */
514          complain_overflow_dont, /* complain_on_overflow */
515          ppc64_elf_unhandled_reloc, /* special_function */
516          "R_PPC64_COPY",        /* name */
517          FALSE,                 /* partial_inplace */
518          0,                     /* src_mask */
519          0,                     /* dst_mask */
520          FALSE),                /* pcrel_offset */
521
522   /* Like R_PPC64_ADDR64, but used when setting global offset table
523      entries.  */
524   HOWTO (R_PPC64_GLOB_DAT,      /* type */
525          0,                     /* rightshift */
526          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
527          64,                    /* bitsize */
528          FALSE,                 /* pc_relative */
529          0,                     /* bitpos */
530          complain_overflow_dont, /* complain_on_overflow */
531          ppc64_elf_unhandled_reloc,  /* special_function */
532          "R_PPC64_GLOB_DAT",    /* name */
533          FALSE,                 /* partial_inplace */
534          0,                     /* src_mask */
535          ONES (64),             /* dst_mask */
536          FALSE),                /* pcrel_offset */
537
538   /* Created by the link editor.  Marks a procedure linkage table
539      entry for a symbol.  */
540   HOWTO (R_PPC64_JMP_SLOT,      /* type */
541          0,                     /* rightshift */
542          0,                     /* size (0 = byte, 1 = short, 2 = long) */
543          0,                     /* bitsize */
544          FALSE,                 /* pc_relative */
545          0,                     /* bitpos */
546          complain_overflow_dont, /* complain_on_overflow */
547          ppc64_elf_unhandled_reloc, /* special_function */
548          "R_PPC64_JMP_SLOT",    /* name */
549          FALSE,                 /* partial_inplace */
550          0,                     /* src_mask */
551          0,                     /* dst_mask */
552          FALSE),                /* pcrel_offset */
553
554   /* Used only by the dynamic linker.  When the object is run, this
555      doubleword64 is set to the load address of the object, plus the
556      addend.  */
557   HOWTO (R_PPC64_RELATIVE,      /* type */
558          0,                     /* rightshift */
559          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
560          64,                    /* bitsize */
561          FALSE,                 /* pc_relative */
562          0,                     /* bitpos */
563          complain_overflow_dont, /* complain_on_overflow */
564          bfd_elf_generic_reloc, /* special_function */
565          "R_PPC64_RELATIVE",    /* name */
566          FALSE,                 /* partial_inplace */
567          0,                     /* src_mask */
568          ONES (64),             /* dst_mask */
569          FALSE),                /* pcrel_offset */
570
571   /* Like R_PPC64_ADDR32, but may be unaligned.  */
572   HOWTO (R_PPC64_UADDR32,       /* type */
573          0,                     /* rightshift */
574          2,                     /* size (0 = byte, 1 = short, 2 = long) */
575          32,                    /* bitsize */
576          FALSE,                 /* pc_relative */
577          0,                     /* bitpos */
578          complain_overflow_bitfield, /* complain_on_overflow */
579          bfd_elf_generic_reloc, /* special_function */
580          "R_PPC64_UADDR32",     /* name */
581          FALSE,                 /* partial_inplace */
582          0,                     /* src_mask */
583          0xffffffff,            /* dst_mask */
584          FALSE),                /* pcrel_offset */
585
586   /* Like R_PPC64_ADDR16, but may be unaligned.  */
587   HOWTO (R_PPC64_UADDR16,       /* type */
588          0,                     /* rightshift */
589          1,                     /* size (0 = byte, 1 = short, 2 = long) */
590          16,                    /* bitsize */
591          FALSE,                 /* pc_relative */
592          0,                     /* bitpos */
593          complain_overflow_bitfield, /* complain_on_overflow */
594          bfd_elf_generic_reloc, /* special_function */
595          "R_PPC64_UADDR16",     /* name */
596          FALSE,                 /* partial_inplace */
597          0,                     /* src_mask */
598          0xffff,                /* dst_mask */
599          FALSE),                /* pcrel_offset */
600
601   /* 32-bit PC relative.  */
602   HOWTO (R_PPC64_REL32,         /* type */
603          0,                     /* rightshift */
604          2,                     /* size (0 = byte, 1 = short, 2 = long) */
605          32,                    /* bitsize */
606          TRUE,                  /* pc_relative */
607          0,                     /* bitpos */
608          /* FIXME: Verify.  Was complain_overflow_bitfield.  */
609          complain_overflow_signed, /* complain_on_overflow */
610          bfd_elf_generic_reloc, /* special_function */
611          "R_PPC64_REL32",       /* name */
612          FALSE,                 /* partial_inplace */
613          0,                     /* src_mask */
614          0xffffffff,            /* dst_mask */
615          TRUE),                 /* pcrel_offset */
616
617   /* 32-bit relocation to the symbol's procedure linkage table.  */
618   HOWTO (R_PPC64_PLT32,         /* type */
619          0,                     /* rightshift */
620          2,                     /* size (0 = byte, 1 = short, 2 = long) */
621          32,                    /* bitsize */
622          FALSE,                 /* pc_relative */
623          0,                     /* bitpos */
624          complain_overflow_bitfield, /* complain_on_overflow */
625          ppc64_elf_unhandled_reloc, /* special_function */
626          "R_PPC64_PLT32",       /* name */
627          FALSE,                 /* partial_inplace */
628          0,                     /* src_mask */
629          0xffffffff,            /* dst_mask */
630          FALSE),                /* pcrel_offset */
631
632   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
633      FIXME: R_PPC64_PLTREL32 not supported.  */
634   HOWTO (R_PPC64_PLTREL32,      /* type */
635          0,                     /* rightshift */
636          2,                     /* size (0 = byte, 1 = short, 2 = long) */
637          32,                    /* bitsize */
638          TRUE,                  /* pc_relative */
639          0,                     /* bitpos */
640          complain_overflow_signed, /* complain_on_overflow */
641          bfd_elf_generic_reloc, /* special_function */
642          "R_PPC64_PLTREL32",    /* name */
643          FALSE,                 /* partial_inplace */
644          0,                     /* src_mask */
645          0xffffffff,            /* dst_mask */
646          TRUE),                 /* pcrel_offset */
647
648   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
649      the symbol.  */
650   HOWTO (R_PPC64_PLT16_LO,      /* type */
651          0,                     /* rightshift */
652          1,                     /* size (0 = byte, 1 = short, 2 = long) */
653          16,                    /* bitsize */
654          FALSE,                 /* pc_relative */
655          0,                     /* bitpos */
656          complain_overflow_dont, /* complain_on_overflow */
657          ppc64_elf_unhandled_reloc, /* special_function */
658          "R_PPC64_PLT16_LO",    /* name */
659          FALSE,                 /* partial_inplace */
660          0,                     /* src_mask */
661          0xffff,                /* dst_mask */
662          FALSE),                /* pcrel_offset */
663
664   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
665      the symbol.  */
666   HOWTO (R_PPC64_PLT16_HI,      /* type */
667          16,                    /* rightshift */
668          1,                     /* size (0 = byte, 1 = short, 2 = long) */
669          16,                    /* bitsize */
670          FALSE,                 /* pc_relative */
671          0,                     /* bitpos */
672          complain_overflow_dont, /* complain_on_overflow */
673          ppc64_elf_unhandled_reloc, /* special_function */
674          "R_PPC64_PLT16_HI",    /* name */
675          FALSE,                 /* partial_inplace */
676          0,                     /* src_mask */
677          0xffff,                /* dst_mask */
678          FALSE),                /* pcrel_offset */
679
680   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
681      the symbol.  */
682   HOWTO (R_PPC64_PLT16_HA,      /* type */
683          16,                    /* rightshift */
684          1,                     /* size (0 = byte, 1 = short, 2 = long) */
685          16,                    /* bitsize */
686          FALSE,                 /* pc_relative */
687          0,                     /* bitpos */
688          complain_overflow_dont, /* complain_on_overflow */
689          ppc64_elf_unhandled_reloc, /* special_function */
690          "R_PPC64_PLT16_HA",    /* name */
691          FALSE,                 /* partial_inplace */
692          0,                     /* src_mask */
693          0xffff,                /* dst_mask */
694          FALSE),                /* pcrel_offset */
695
696   /* 16-bit section relative relocation.  */
697   HOWTO (R_PPC64_SECTOFF,       /* type */
698          0,                     /* rightshift */
699          1,                     /* size (0 = byte, 1 = short, 2 = long) */
700          16,                    /* bitsize */
701          FALSE,                 /* pc_relative */
702          0,                     /* bitpos */
703          complain_overflow_bitfield, /* complain_on_overflow */
704          ppc64_elf_sectoff_reloc, /* special_function */
705          "R_PPC64_SECTOFF",     /* name */
706          FALSE,                 /* partial_inplace */
707          0,                     /* src_mask */
708          0xffff,                /* dst_mask */
709          FALSE),                /* pcrel_offset */
710
711   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
712   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
713          0,                     /* rightshift */
714          1,                     /* size (0 = byte, 1 = short, 2 = long) */
715          16,                    /* bitsize */
716          FALSE,                 /* pc_relative */
717          0,                     /* bitpos */
718          complain_overflow_dont, /* complain_on_overflow */
719          ppc64_elf_sectoff_reloc, /* special_function */
720          "R_PPC64_SECTOFF_LO",  /* name */
721          FALSE,                 /* partial_inplace */
722          0,                     /* src_mask */
723          0xffff,                /* dst_mask */
724          FALSE),                /* pcrel_offset */
725
726   /* 16-bit upper half section relative relocation.  */
727   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
728          16,                    /* rightshift */
729          1,                     /* size (0 = byte, 1 = short, 2 = long) */
730          16,                    /* bitsize */
731          FALSE,                 /* pc_relative */
732          0,                     /* bitpos */
733          complain_overflow_dont, /* complain_on_overflow */
734          ppc64_elf_sectoff_reloc, /* special_function */
735          "R_PPC64_SECTOFF_HI",  /* name */
736          FALSE,                 /* partial_inplace */
737          0,                     /* src_mask */
738          0xffff,                /* dst_mask */
739          FALSE),                /* pcrel_offset */
740
741   /* 16-bit upper half adjusted section relative relocation.  */
742   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
743          16,                    /* rightshift */
744          1,                     /* size (0 = byte, 1 = short, 2 = long) */
745          16,                    /* bitsize */
746          FALSE,                 /* pc_relative */
747          0,                     /* bitpos */
748          complain_overflow_dont, /* complain_on_overflow */
749          ppc64_elf_sectoff_ha_reloc, /* special_function */
750          "R_PPC64_SECTOFF_HA",  /* name */
751          FALSE,                 /* partial_inplace */
752          0,                     /* src_mask */
753          0xffff,                /* dst_mask */
754          FALSE),                /* pcrel_offset */
755
756   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
757   HOWTO (R_PPC64_REL30,         /* type */
758          2,                     /* rightshift */
759          2,                     /* size (0 = byte, 1 = short, 2 = long) */
760          30,                    /* bitsize */
761          TRUE,                  /* pc_relative */
762          0,                     /* bitpos */
763          complain_overflow_dont, /* complain_on_overflow */
764          bfd_elf_generic_reloc, /* special_function */
765          "R_PPC64_REL30",       /* name */
766          FALSE,                 /* partial_inplace */
767          0,                     /* src_mask */
768          0xfffffffc,            /* dst_mask */
769          TRUE),                 /* pcrel_offset */
770
771   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
772
773   /* A standard 64-bit relocation.  */
774   HOWTO (R_PPC64_ADDR64,        /* type */
775          0,                     /* rightshift */
776          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
777          64,                    /* bitsize */
778          FALSE,                 /* pc_relative */
779          0,                     /* bitpos */
780          complain_overflow_dont, /* complain_on_overflow */
781          bfd_elf_generic_reloc, /* special_function */
782          "R_PPC64_ADDR64",      /* name */
783          FALSE,                 /* partial_inplace */
784          0,                     /* src_mask */
785          ONES (64),             /* dst_mask */
786          FALSE),                /* pcrel_offset */
787
788   /* The bits 32-47 of an address.  */
789   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
790          32,                    /* rightshift */
791          1,                     /* size (0 = byte, 1 = short, 2 = long) */
792          16,                    /* bitsize */
793          FALSE,                 /* pc_relative */
794          0,                     /* bitpos */
795          complain_overflow_dont, /* complain_on_overflow */
796          bfd_elf_generic_reloc, /* special_function */
797          "R_PPC64_ADDR16_HIGHER", /* name */
798          FALSE,                 /* partial_inplace */
799          0,                     /* src_mask */
800          0xffff,                /* dst_mask */
801          FALSE),                /* pcrel_offset */
802
803   /* The bits 32-47 of an address, plus 1 if the contents of the low
804      16 bits, treated as a signed number, is negative.  */
805   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
806          32,                    /* rightshift */
807          1,                     /* size (0 = byte, 1 = short, 2 = long) */
808          16,                    /* bitsize */
809          FALSE,                 /* pc_relative */
810          0,                     /* bitpos */
811          complain_overflow_dont, /* complain_on_overflow */
812          ppc64_elf_ha_reloc,    /* special_function */
813          "R_PPC64_ADDR16_HIGHERA", /* name */
814          FALSE,                 /* partial_inplace */
815          0,                     /* src_mask */
816          0xffff,                /* dst_mask */
817          FALSE),                /* pcrel_offset */
818
819   /* The bits 48-63 of an address.  */
820   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
821          48,                    /* rightshift */
822          1,                     /* size (0 = byte, 1 = short, 2 = long) */
823          16,                    /* bitsize */
824          FALSE,                 /* pc_relative */
825          0,                     /* bitpos */
826          complain_overflow_dont, /* complain_on_overflow */
827          bfd_elf_generic_reloc, /* special_function */
828          "R_PPC64_ADDR16_HIGHEST", /* name */
829          FALSE,                 /* partial_inplace */
830          0,                     /* src_mask */
831          0xffff,                /* dst_mask */
832          FALSE),                /* pcrel_offset */
833
834   /* The bits 48-63 of an address, plus 1 if the contents of the low
835      16 bits, treated as a signed number, is negative.  */
836   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
837          48,                    /* rightshift */
838          1,                     /* size (0 = byte, 1 = short, 2 = long) */
839          16,                    /* bitsize */
840          FALSE,                 /* pc_relative */
841          0,                     /* bitpos */
842          complain_overflow_dont, /* complain_on_overflow */
843          ppc64_elf_ha_reloc,    /* special_function */
844          "R_PPC64_ADDR16_HIGHESTA", /* name */
845          FALSE,                 /* partial_inplace */
846          0,                     /* src_mask */
847          0xffff,                /* dst_mask */
848          FALSE),                /* pcrel_offset */
849
850   /* Like ADDR64, but may be unaligned.  */
851   HOWTO (R_PPC64_UADDR64,       /* type */
852          0,                     /* rightshift */
853          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
854          64,                    /* bitsize */
855          FALSE,                 /* pc_relative */
856          0,                     /* bitpos */
857          complain_overflow_dont, /* complain_on_overflow */
858          bfd_elf_generic_reloc, /* special_function */
859          "R_PPC64_UADDR64",     /* name */
860          FALSE,                 /* partial_inplace */
861          0,                     /* src_mask */
862          ONES (64),             /* dst_mask */
863          FALSE),                /* pcrel_offset */
864
865   /* 64-bit relative relocation.  */
866   HOWTO (R_PPC64_REL64,         /* type */
867          0,                     /* rightshift */
868          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
869          64,                    /* bitsize */
870          TRUE,                  /* pc_relative */
871          0,                     /* bitpos */
872          complain_overflow_dont, /* complain_on_overflow */
873          bfd_elf_generic_reloc, /* special_function */
874          "R_PPC64_REL64",       /* name */
875          FALSE,                 /* partial_inplace */
876          0,                     /* src_mask */
877          ONES (64),             /* dst_mask */
878          TRUE),                 /* pcrel_offset */
879
880   /* 64-bit relocation to the symbol's procedure linkage table.  */
881   HOWTO (R_PPC64_PLT64,         /* type */
882          0,                     /* rightshift */
883          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
884          64,                    /* bitsize */
885          FALSE,                 /* pc_relative */
886          0,                     /* bitpos */
887          complain_overflow_dont, /* complain_on_overflow */
888          ppc64_elf_unhandled_reloc, /* special_function */
889          "R_PPC64_PLT64",       /* name */
890          FALSE,                 /* partial_inplace */
891          0,                     /* src_mask */
892          ONES (64),             /* dst_mask */
893          FALSE),                /* pcrel_offset */
894
895   /* 64-bit PC relative relocation to the symbol's procedure linkage
896      table.  */
897   /* FIXME: R_PPC64_PLTREL64 not supported.  */
898   HOWTO (R_PPC64_PLTREL64,      /* type */
899          0,                     /* rightshift */
900          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
901          64,                    /* bitsize */
902          TRUE,                  /* pc_relative */
903          0,                     /* bitpos */
904          complain_overflow_dont, /* complain_on_overflow */
905          ppc64_elf_unhandled_reloc, /* special_function */
906          "R_PPC64_PLTREL64",    /* name */
907          FALSE,                 /* partial_inplace */
908          0,                     /* src_mask */
909          ONES (64),             /* dst_mask */
910          TRUE),                 /* pcrel_offset */
911
912   /* 16 bit TOC-relative relocation.  */
913
914   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
915   HOWTO (R_PPC64_TOC16,         /* type */
916          0,                     /* rightshift */
917          1,                     /* size (0 = byte, 1 = short, 2 = long) */
918          16,                    /* bitsize */
919          FALSE,                 /* pc_relative */
920          0,                     /* bitpos */
921          complain_overflow_signed, /* complain_on_overflow */
922          ppc64_elf_toc_reloc,   /* special_function */
923          "R_PPC64_TOC16",       /* name */
924          FALSE,                 /* partial_inplace */
925          0,                     /* src_mask */
926          0xffff,                /* dst_mask */
927          FALSE),                /* pcrel_offset */
928
929   /* 16 bit TOC-relative relocation without overflow.  */
930
931   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
932   HOWTO (R_PPC64_TOC16_LO,      /* type */
933          0,                     /* rightshift */
934          1,                     /* size (0 = byte, 1 = short, 2 = long) */
935          16,                    /* bitsize */
936          FALSE,                 /* pc_relative */
937          0,                     /* bitpos */
938          complain_overflow_dont, /* complain_on_overflow */
939          ppc64_elf_toc_reloc,   /* special_function */
940          "R_PPC64_TOC16_LO",    /* name */
941          FALSE,                 /* partial_inplace */
942          0,                     /* src_mask */
943          0xffff,                /* dst_mask */
944          FALSE),                /* pcrel_offset */
945
946   /* 16 bit TOC-relative relocation, high 16 bits.  */
947
948   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
949   HOWTO (R_PPC64_TOC16_HI,      /* type */
950          16,                    /* rightshift */
951          1,                     /* size (0 = byte, 1 = short, 2 = long) */
952          16,                    /* bitsize */
953          FALSE,                 /* pc_relative */
954          0,                     /* bitpos */
955          complain_overflow_dont, /* complain_on_overflow */
956          ppc64_elf_toc_reloc,   /* special_function */
957          "R_PPC64_TOC16_HI",    /* name */
958          FALSE,                 /* partial_inplace */
959          0,                     /* src_mask */
960          0xffff,                /* dst_mask */
961          FALSE),                /* pcrel_offset */
962
963   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
964      contents of the low 16 bits, treated as a signed number, is
965      negative.  */
966
967   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
968   HOWTO (R_PPC64_TOC16_HA,      /* type */
969          16,                    /* rightshift */
970          1,                     /* size (0 = byte, 1 = short, 2 = long) */
971          16,                    /* bitsize */
972          FALSE,                 /* pc_relative */
973          0,                     /* bitpos */
974          complain_overflow_dont, /* complain_on_overflow */
975          ppc64_elf_toc_ha_reloc, /* special_function */
976          "R_PPC64_TOC16_HA",    /* name */
977          FALSE,                 /* partial_inplace */
978          0,                     /* src_mask */
979          0xffff,                /* dst_mask */
980          FALSE),                /* pcrel_offset */
981
982   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
983
984   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
985   HOWTO (R_PPC64_TOC,           /* type */
986          0,                     /* rightshift */
987          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
988          64,                    /* bitsize */
989          FALSE,                 /* pc_relative */
990          0,                     /* bitpos */
991          complain_overflow_bitfield, /* complain_on_overflow */
992          ppc64_elf_toc64_reloc, /* special_function */
993          "R_PPC64_TOC",         /* name */
994          FALSE,                 /* partial_inplace */
995          0,                     /* src_mask */
996          ONES (64),             /* dst_mask */
997          FALSE),                /* pcrel_offset */
998
999   /* Like R_PPC64_GOT16, but also informs the link editor that the
1000      value to relocate may (!) refer to a PLT entry which the link
1001      editor (a) may replace with the symbol value.  If the link editor
1002      is unable to fully resolve the symbol, it may (b) create a PLT
1003      entry and store the address to the new PLT entry in the GOT.
1004      This permits lazy resolution of function symbols at run time.
1005      The link editor may also skip all of this and just (c) emit a
1006      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1007   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1008     HOWTO (R_PPC64_PLTGOT16,    /* type */
1009          0,                     /* rightshift */
1010          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1011          16,                    /* bitsize */
1012          FALSE,                 /* pc_relative */
1013          0,                     /* bitpos */
1014          complain_overflow_signed, /* complain_on_overflow */
1015          ppc64_elf_unhandled_reloc, /* special_function */
1016          "R_PPC64_PLTGOT16",    /* name */
1017          FALSE,                 /* partial_inplace */
1018          0,                     /* src_mask */
1019          0xffff,                /* dst_mask */
1020          FALSE),                /* pcrel_offset */
1021
1022   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1023   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1024   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1025          0,                     /* rightshift */
1026          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1027          16,                    /* bitsize */
1028          FALSE,                 /* pc_relative */
1029          0,                     /* bitpos */
1030          complain_overflow_dont, /* complain_on_overflow */
1031          ppc64_elf_unhandled_reloc, /* special_function */
1032          "R_PPC64_PLTGOT16_LO", /* name */
1033          FALSE,                 /* partial_inplace */
1034          0,                     /* src_mask */
1035          0xffff,                /* dst_mask */
1036          FALSE),                /* pcrel_offset */
1037
1038   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1039   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1040   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1041          16,                    /* rightshift */
1042          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1043          16,                    /* bitsize */
1044          FALSE,                 /* pc_relative */
1045          0,                     /* bitpos */
1046          complain_overflow_dont, /* complain_on_overflow */
1047          ppc64_elf_unhandled_reloc, /* special_function */
1048          "R_PPC64_PLTGOT16_HI", /* name */
1049          FALSE,                 /* partial_inplace */
1050          0,                     /* src_mask */
1051          0xffff,                /* dst_mask */
1052          FALSE),                /* pcrel_offset */
1053
1054   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1055      1 if the contents of the low 16 bits, treated as a signed number,
1056      is negative.  */
1057   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1058   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1059          16,                    /* rightshift */
1060          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1061          16,                    /* bitsize */
1062          FALSE,                 /* pc_relative */
1063          0,                     /* bitpos */
1064          complain_overflow_dont,/* complain_on_overflow */
1065          ppc64_elf_unhandled_reloc, /* special_function */
1066          "R_PPC64_PLTGOT16_HA", /* name */
1067          FALSE,                 /* partial_inplace */
1068          0,                     /* src_mask */
1069          0xffff,                /* dst_mask */
1070          FALSE),                /* pcrel_offset */
1071
1072   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1073   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1074          0,                     /* rightshift */
1075          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1076          16,                    /* bitsize */
1077          FALSE,                 /* pc_relative */
1078          0,                     /* bitpos */
1079          complain_overflow_bitfield, /* complain_on_overflow */
1080          bfd_elf_generic_reloc, /* special_function */
1081          "R_PPC64_ADDR16_DS",   /* name */
1082          FALSE,                 /* partial_inplace */
1083          0,                     /* src_mask */
1084          0xfffc,                /* dst_mask */
1085          FALSE),                /* pcrel_offset */
1086
1087   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1088   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1089          0,                     /* rightshift */
1090          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1091          16,                    /* bitsize */
1092          FALSE,                 /* pc_relative */
1093          0,                     /* bitpos */
1094          complain_overflow_dont,/* complain_on_overflow */
1095          bfd_elf_generic_reloc, /* special_function */
1096          "R_PPC64_ADDR16_LO_DS",/* name */
1097          FALSE,                 /* partial_inplace */
1098          0,                     /* src_mask */
1099          0xfffc,                /* dst_mask */
1100          FALSE),                /* pcrel_offset */
1101
1102   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1103   HOWTO (R_PPC64_GOT16_DS,      /* type */
1104          0,                     /* rightshift */
1105          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1106          16,                    /* bitsize */
1107          FALSE,                 /* pc_relative */
1108          0,                     /* bitpos */
1109          complain_overflow_signed, /* complain_on_overflow */
1110          ppc64_elf_unhandled_reloc, /* special_function */
1111          "R_PPC64_GOT16_DS",    /* name */
1112          FALSE,                 /* partial_inplace */
1113          0,                     /* src_mask */
1114          0xfffc,                /* dst_mask */
1115          FALSE),                /* pcrel_offset */
1116
1117   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1118   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1119          0,                     /* rightshift */
1120          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1121          16,                    /* bitsize */
1122          FALSE,                 /* pc_relative */
1123          0,                     /* bitpos */
1124          complain_overflow_dont, /* complain_on_overflow */
1125          ppc64_elf_unhandled_reloc, /* special_function */
1126          "R_PPC64_GOT16_LO_DS", /* name */
1127          FALSE,                 /* partial_inplace */
1128          0,                     /* src_mask */
1129          0xfffc,                /* dst_mask */
1130          FALSE),                /* pcrel_offset */
1131
1132   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1133   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1134          0,                     /* rightshift */
1135          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1136          16,                    /* bitsize */
1137          FALSE,                 /* pc_relative */
1138          0,                     /* bitpos */
1139          complain_overflow_dont, /* complain_on_overflow */
1140          ppc64_elf_unhandled_reloc, /* special_function */
1141          "R_PPC64_PLT16_LO_DS", /* name */
1142          FALSE,                 /* partial_inplace */
1143          0,                     /* src_mask */
1144          0xfffc,                /* dst_mask */
1145          FALSE),                /* pcrel_offset */
1146
1147   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1148   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1149          0,                     /* rightshift */
1150          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1151          16,                    /* bitsize */
1152          FALSE,                 /* pc_relative */
1153          0,                     /* bitpos */
1154          complain_overflow_bitfield, /* complain_on_overflow */
1155          ppc64_elf_sectoff_reloc, /* special_function */
1156          "R_PPC64_SECTOFF_DS",  /* name */
1157          FALSE,                 /* partial_inplace */
1158          0,                     /* src_mask */
1159          0xfffc,                /* dst_mask */
1160          FALSE),                /* pcrel_offset */
1161
1162   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1163   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1164          0,                     /* rightshift */
1165          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1166          16,                    /* bitsize */
1167          FALSE,                 /* pc_relative */
1168          0,                     /* bitpos */
1169          complain_overflow_dont, /* complain_on_overflow */
1170          ppc64_elf_sectoff_reloc, /* special_function */
1171          "R_PPC64_SECTOFF_LO_DS",/* name */
1172          FALSE,                 /* partial_inplace */
1173          0,                     /* src_mask */
1174          0xfffc,                /* dst_mask */
1175          FALSE),                /* pcrel_offset */
1176
1177   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1178   HOWTO (R_PPC64_TOC16_DS,      /* type */
1179          0,                     /* rightshift */
1180          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1181          16,                    /* bitsize */
1182          FALSE,                 /* pc_relative */
1183          0,                     /* bitpos */
1184          complain_overflow_signed, /* complain_on_overflow */
1185          ppc64_elf_toc_reloc,   /* special_function */
1186          "R_PPC64_TOC16_DS",    /* name */
1187          FALSE,                 /* partial_inplace */
1188          0,                     /* src_mask */
1189          0xfffc,                /* dst_mask */
1190          FALSE),                /* pcrel_offset */
1191
1192   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1193   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1194          0,                     /* rightshift */
1195          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1196          16,                    /* bitsize */
1197          FALSE,                 /* pc_relative */
1198          0,                     /* bitpos */
1199          complain_overflow_dont, /* complain_on_overflow */
1200          ppc64_elf_toc_reloc,   /* special_function */
1201          "R_PPC64_TOC16_LO_DS", /* name */
1202          FALSE,                 /* partial_inplace */
1203          0,                     /* src_mask */
1204          0xfffc,                /* dst_mask */
1205          FALSE),                /* pcrel_offset */
1206
1207   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1208   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1209   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1210          0,                     /* rightshift */
1211          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1212          16,                    /* bitsize */
1213          FALSE,                 /* pc_relative */
1214          0,                     /* bitpos */
1215          complain_overflow_signed, /* complain_on_overflow */
1216          ppc64_elf_unhandled_reloc, /* special_function */
1217          "R_PPC64_PLTGOT16_DS", /* name */
1218          FALSE,                 /* partial_inplace */
1219          0,                     /* src_mask */
1220          0xfffc,                /* dst_mask */
1221          FALSE),                /* pcrel_offset */
1222
1223   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1224   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1225   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1226          0,                     /* rightshift */
1227          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1228          16,                    /* bitsize */
1229          FALSE,                 /* pc_relative */
1230          0,                     /* bitpos */
1231          complain_overflow_dont, /* complain_on_overflow */
1232          ppc64_elf_unhandled_reloc, /* special_function */
1233          "R_PPC64_PLTGOT16_LO_DS",/* name */
1234          FALSE,                 /* partial_inplace */
1235          0,                     /* src_mask */
1236          0xfffc,                /* dst_mask */
1237          FALSE),                /* pcrel_offset */
1238
1239   /* Marker relocs for TLS.  */
1240   HOWTO (R_PPC64_TLS,
1241          0,                     /* rightshift */
1242          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1243          32,                    /* bitsize */
1244          FALSE,                 /* pc_relative */
1245          0,                     /* bitpos */
1246          complain_overflow_dont, /* complain_on_overflow */
1247          bfd_elf_generic_reloc, /* special_function */
1248          "R_PPC64_TLS",         /* name */
1249          FALSE,                 /* partial_inplace */
1250          0,                     /* src_mask */
1251          0,                     /* dst_mask */
1252          FALSE),                /* pcrel_offset */
1253
1254   HOWTO (R_PPC64_TLSGD,
1255          0,                     /* rightshift */
1256          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1257          32,                    /* bitsize */
1258          FALSE,                 /* pc_relative */
1259          0,                     /* bitpos */
1260          complain_overflow_dont, /* complain_on_overflow */
1261          bfd_elf_generic_reloc, /* special_function */
1262          "R_PPC64_TLSGD",       /* name */
1263          FALSE,                 /* partial_inplace */
1264          0,                     /* src_mask */
1265          0,                     /* dst_mask */
1266          FALSE),                /* pcrel_offset */
1267
1268   HOWTO (R_PPC64_TLSLD,
1269          0,                     /* rightshift */
1270          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1271          32,                    /* bitsize */
1272          FALSE,                 /* pc_relative */
1273          0,                     /* bitpos */
1274          complain_overflow_dont, /* complain_on_overflow */
1275          bfd_elf_generic_reloc, /* special_function */
1276          "R_PPC64_TLSLD",       /* name */
1277          FALSE,                 /* partial_inplace */
1278          0,                     /* src_mask */
1279          0,                     /* dst_mask */
1280          FALSE),                /* pcrel_offset */
1281
1282   /* Computes the load module index of the load module that contains the
1283      definition of its TLS sym.  */
1284   HOWTO (R_PPC64_DTPMOD64,
1285          0,                     /* rightshift */
1286          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1287          64,                    /* bitsize */
1288          FALSE,                 /* pc_relative */
1289          0,                     /* bitpos */
1290          complain_overflow_dont, /* complain_on_overflow */
1291          ppc64_elf_unhandled_reloc, /* special_function */
1292          "R_PPC64_DTPMOD64",    /* name */
1293          FALSE,                 /* partial_inplace */
1294          0,                     /* src_mask */
1295          ONES (64),             /* dst_mask */
1296          FALSE),                /* pcrel_offset */
1297
1298   /* Computes a dtv-relative displacement, the difference between the value
1299      of sym+add and the base address of the thread-local storage block that
1300      contains the definition of sym, minus 0x8000.  */
1301   HOWTO (R_PPC64_DTPREL64,
1302          0,                     /* rightshift */
1303          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1304          64,                    /* bitsize */
1305          FALSE,                 /* pc_relative */
1306          0,                     /* bitpos */
1307          complain_overflow_dont, /* complain_on_overflow */
1308          ppc64_elf_unhandled_reloc, /* special_function */
1309          "R_PPC64_DTPREL64",    /* name */
1310          FALSE,                 /* partial_inplace */
1311          0,                     /* src_mask */
1312          ONES (64),             /* dst_mask */
1313          FALSE),                /* pcrel_offset */
1314
1315   /* A 16 bit dtprel reloc.  */
1316   HOWTO (R_PPC64_DTPREL16,
1317          0,                     /* rightshift */
1318          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1319          16,                    /* bitsize */
1320          FALSE,                 /* pc_relative */
1321          0,                     /* bitpos */
1322          complain_overflow_signed, /* complain_on_overflow */
1323          ppc64_elf_unhandled_reloc, /* special_function */
1324          "R_PPC64_DTPREL16",    /* name */
1325          FALSE,                 /* partial_inplace */
1326          0,                     /* src_mask */
1327          0xffff,                /* dst_mask */
1328          FALSE),                /* pcrel_offset */
1329
1330   /* Like DTPREL16, but no overflow.  */
1331   HOWTO (R_PPC64_DTPREL16_LO,
1332          0,                     /* rightshift */
1333          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1334          16,                    /* bitsize */
1335          FALSE,                 /* pc_relative */
1336          0,                     /* bitpos */
1337          complain_overflow_dont, /* complain_on_overflow */
1338          ppc64_elf_unhandled_reloc, /* special_function */
1339          "R_PPC64_DTPREL16_LO", /* name */
1340          FALSE,                 /* partial_inplace */
1341          0,                     /* src_mask */
1342          0xffff,                /* dst_mask */
1343          FALSE),                /* pcrel_offset */
1344
1345   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1346   HOWTO (R_PPC64_DTPREL16_HI,
1347          16,                    /* rightshift */
1348          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1349          16,                    /* bitsize */
1350          FALSE,                 /* pc_relative */
1351          0,                     /* bitpos */
1352          complain_overflow_dont, /* complain_on_overflow */
1353          ppc64_elf_unhandled_reloc, /* special_function */
1354          "R_PPC64_DTPREL16_HI", /* name */
1355          FALSE,                 /* partial_inplace */
1356          0,                     /* src_mask */
1357          0xffff,                /* dst_mask */
1358          FALSE),                /* pcrel_offset */
1359
1360   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1361   HOWTO (R_PPC64_DTPREL16_HA,
1362          16,                    /* rightshift */
1363          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1364          16,                    /* bitsize */
1365          FALSE,                 /* pc_relative */
1366          0,                     /* bitpos */
1367          complain_overflow_dont, /* complain_on_overflow */
1368          ppc64_elf_unhandled_reloc, /* special_function */
1369          "R_PPC64_DTPREL16_HA", /* name */
1370          FALSE,                 /* partial_inplace */
1371          0,                     /* src_mask */
1372          0xffff,                /* dst_mask */
1373          FALSE),                /* pcrel_offset */
1374
1375   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1376   HOWTO (R_PPC64_DTPREL16_HIGHER,
1377          32,                    /* rightshift */
1378          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1379          16,                    /* bitsize */
1380          FALSE,                 /* pc_relative */
1381          0,                     /* bitpos */
1382          complain_overflow_dont, /* complain_on_overflow */
1383          ppc64_elf_unhandled_reloc, /* special_function */
1384          "R_PPC64_DTPREL16_HIGHER", /* name */
1385          FALSE,                 /* partial_inplace */
1386          0,                     /* src_mask */
1387          0xffff,                /* dst_mask */
1388          FALSE),                /* pcrel_offset */
1389
1390   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1391   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1392          32,                    /* rightshift */
1393          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1394          16,                    /* bitsize */
1395          FALSE,                 /* pc_relative */
1396          0,                     /* bitpos */
1397          complain_overflow_dont, /* complain_on_overflow */
1398          ppc64_elf_unhandled_reloc, /* special_function */
1399          "R_PPC64_DTPREL16_HIGHERA", /* name */
1400          FALSE,                 /* partial_inplace */
1401          0,                     /* src_mask */
1402          0xffff,                /* dst_mask */
1403          FALSE),                /* pcrel_offset */
1404
1405   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1406   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1407          48,                    /* rightshift */
1408          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1409          16,                    /* bitsize */
1410          FALSE,                 /* pc_relative */
1411          0,                     /* bitpos */
1412          complain_overflow_dont, /* complain_on_overflow */
1413          ppc64_elf_unhandled_reloc, /* special_function */
1414          "R_PPC64_DTPREL16_HIGHEST", /* name */
1415          FALSE,                 /* partial_inplace */
1416          0,                     /* src_mask */
1417          0xffff,                /* dst_mask */
1418          FALSE),                /* pcrel_offset */
1419
1420   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1421   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1422          48,                    /* rightshift */
1423          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1424          16,                    /* bitsize */
1425          FALSE,                 /* pc_relative */
1426          0,                     /* bitpos */
1427          complain_overflow_dont, /* complain_on_overflow */
1428          ppc64_elf_unhandled_reloc, /* special_function */
1429          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1430          FALSE,                 /* partial_inplace */
1431          0,                     /* src_mask */
1432          0xffff,                /* dst_mask */
1433          FALSE),                /* pcrel_offset */
1434
1435   /* Like DTPREL16, but for insns with a DS field.  */
1436   HOWTO (R_PPC64_DTPREL16_DS,
1437          0,                     /* rightshift */
1438          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1439          16,                    /* bitsize */
1440          FALSE,                 /* pc_relative */
1441          0,                     /* bitpos */
1442          complain_overflow_signed, /* complain_on_overflow */
1443          ppc64_elf_unhandled_reloc, /* special_function */
1444          "R_PPC64_DTPREL16_DS", /* name */
1445          FALSE,                 /* partial_inplace */
1446          0,                     /* src_mask */
1447          0xfffc,                /* dst_mask */
1448          FALSE),                /* pcrel_offset */
1449
1450   /* Like DTPREL16_DS, but no overflow.  */
1451   HOWTO (R_PPC64_DTPREL16_LO_DS,
1452          0,                     /* rightshift */
1453          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1454          16,                    /* bitsize */
1455          FALSE,                 /* pc_relative */
1456          0,                     /* bitpos */
1457          complain_overflow_dont, /* complain_on_overflow */
1458          ppc64_elf_unhandled_reloc, /* special_function */
1459          "R_PPC64_DTPREL16_LO_DS", /* name */
1460          FALSE,                 /* partial_inplace */
1461          0,                     /* src_mask */
1462          0xfffc,                /* dst_mask */
1463          FALSE),                /* pcrel_offset */
1464
1465   /* Computes a tp-relative displacement, the difference between the value of
1466      sym+add and the value of the thread pointer (r13).  */
1467   HOWTO (R_PPC64_TPREL64,
1468          0,                     /* rightshift */
1469          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1470          64,                    /* bitsize */
1471          FALSE,                 /* pc_relative */
1472          0,                     /* bitpos */
1473          complain_overflow_dont, /* complain_on_overflow */
1474          ppc64_elf_unhandled_reloc, /* special_function */
1475          "R_PPC64_TPREL64",     /* name */
1476          FALSE,                 /* partial_inplace */
1477          0,                     /* src_mask */
1478          ONES (64),             /* dst_mask */
1479          FALSE),                /* pcrel_offset */
1480
1481   /* A 16 bit tprel reloc.  */
1482   HOWTO (R_PPC64_TPREL16,
1483          0,                     /* rightshift */
1484          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1485          16,                    /* bitsize */
1486          FALSE,                 /* pc_relative */
1487          0,                     /* bitpos */
1488          complain_overflow_signed, /* complain_on_overflow */
1489          ppc64_elf_unhandled_reloc, /* special_function */
1490          "R_PPC64_TPREL16",     /* name */
1491          FALSE,                 /* partial_inplace */
1492          0,                     /* src_mask */
1493          0xffff,                /* dst_mask */
1494          FALSE),                /* pcrel_offset */
1495
1496   /* Like TPREL16, but no overflow.  */
1497   HOWTO (R_PPC64_TPREL16_LO,
1498          0,                     /* rightshift */
1499          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1500          16,                    /* bitsize */
1501          FALSE,                 /* pc_relative */
1502          0,                     /* bitpos */
1503          complain_overflow_dont, /* complain_on_overflow */
1504          ppc64_elf_unhandled_reloc, /* special_function */
1505          "R_PPC64_TPREL16_LO",  /* name */
1506          FALSE,                 /* partial_inplace */
1507          0,                     /* src_mask */
1508          0xffff,                /* dst_mask */
1509          FALSE),                /* pcrel_offset */
1510
1511   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1512   HOWTO (R_PPC64_TPREL16_HI,
1513          16,                    /* rightshift */
1514          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1515          16,                    /* bitsize */
1516          FALSE,                 /* pc_relative */
1517          0,                     /* bitpos */
1518          complain_overflow_dont, /* complain_on_overflow */
1519          ppc64_elf_unhandled_reloc, /* special_function */
1520          "R_PPC64_TPREL16_HI",  /* name */
1521          FALSE,                 /* partial_inplace */
1522          0,                     /* src_mask */
1523          0xffff,                /* dst_mask */
1524          FALSE),                /* pcrel_offset */
1525
1526   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1527   HOWTO (R_PPC64_TPREL16_HA,
1528          16,                    /* rightshift */
1529          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1530          16,                    /* bitsize */
1531          FALSE,                 /* pc_relative */
1532          0,                     /* bitpos */
1533          complain_overflow_dont, /* complain_on_overflow */
1534          ppc64_elf_unhandled_reloc, /* special_function */
1535          "R_PPC64_TPREL16_HA",  /* name */
1536          FALSE,                 /* partial_inplace */
1537          0,                     /* src_mask */
1538          0xffff,                /* dst_mask */
1539          FALSE),                /* pcrel_offset */
1540
1541   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1542   HOWTO (R_PPC64_TPREL16_HIGHER,
1543          32,                    /* rightshift */
1544          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1545          16,                    /* bitsize */
1546          FALSE,                 /* pc_relative */
1547          0,                     /* bitpos */
1548          complain_overflow_dont, /* complain_on_overflow */
1549          ppc64_elf_unhandled_reloc, /* special_function */
1550          "R_PPC64_TPREL16_HIGHER",      /* name */
1551          FALSE,                 /* partial_inplace */
1552          0,                     /* src_mask */
1553          0xffff,                /* dst_mask */
1554          FALSE),                /* pcrel_offset */
1555
1556   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1557   HOWTO (R_PPC64_TPREL16_HIGHERA,
1558          32,                    /* rightshift */
1559          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1560          16,                    /* bitsize */
1561          FALSE,                 /* pc_relative */
1562          0,                     /* bitpos */
1563          complain_overflow_dont, /* complain_on_overflow */
1564          ppc64_elf_unhandled_reloc, /* special_function */
1565          "R_PPC64_TPREL16_HIGHERA", /* name */
1566          FALSE,                 /* partial_inplace */
1567          0,                     /* src_mask */
1568          0xffff,                /* dst_mask */
1569          FALSE),                /* pcrel_offset */
1570
1571   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1572   HOWTO (R_PPC64_TPREL16_HIGHEST,
1573          48,                    /* rightshift */
1574          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1575          16,                    /* bitsize */
1576          FALSE,                 /* pc_relative */
1577          0,                     /* bitpos */
1578          complain_overflow_dont, /* complain_on_overflow */
1579          ppc64_elf_unhandled_reloc, /* special_function */
1580          "R_PPC64_TPREL16_HIGHEST", /* name */
1581          FALSE,                 /* partial_inplace */
1582          0,                     /* src_mask */
1583          0xffff,                /* dst_mask */
1584          FALSE),                /* pcrel_offset */
1585
1586   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1587   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1588          48,                    /* rightshift */
1589          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1590          16,                    /* bitsize */
1591          FALSE,                 /* pc_relative */
1592          0,                     /* bitpos */
1593          complain_overflow_dont, /* complain_on_overflow */
1594          ppc64_elf_unhandled_reloc, /* special_function */
1595          "R_PPC64_TPREL16_HIGHESTA", /* name */
1596          FALSE,                 /* partial_inplace */
1597          0,                     /* src_mask */
1598          0xffff,                /* dst_mask */
1599          FALSE),                /* pcrel_offset */
1600
1601   /* Like TPREL16, but for insns with a DS field.  */
1602   HOWTO (R_PPC64_TPREL16_DS,
1603          0,                     /* rightshift */
1604          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1605          16,                    /* bitsize */
1606          FALSE,                 /* pc_relative */
1607          0,                     /* bitpos */
1608          complain_overflow_signed, /* complain_on_overflow */
1609          ppc64_elf_unhandled_reloc, /* special_function */
1610          "R_PPC64_TPREL16_DS",  /* name */
1611          FALSE,                 /* partial_inplace */
1612          0,                     /* src_mask */
1613          0xfffc,                /* dst_mask */
1614          FALSE),                /* pcrel_offset */
1615
1616   /* Like TPREL16_DS, but no overflow.  */
1617   HOWTO (R_PPC64_TPREL16_LO_DS,
1618          0,                     /* rightshift */
1619          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1620          16,                    /* bitsize */
1621          FALSE,                 /* pc_relative */
1622          0,                     /* bitpos */
1623          complain_overflow_dont, /* complain_on_overflow */
1624          ppc64_elf_unhandled_reloc, /* special_function */
1625          "R_PPC64_TPREL16_LO_DS", /* name */
1626          FALSE,                 /* partial_inplace */
1627          0,                     /* src_mask */
1628          0xfffc,                /* dst_mask */
1629          FALSE),                /* pcrel_offset */
1630
1631   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1632      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1633      to the first entry relative to the TOC base (r2).  */
1634   HOWTO (R_PPC64_GOT_TLSGD16,
1635          0,                     /* rightshift */
1636          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1637          16,                    /* bitsize */
1638          FALSE,                 /* pc_relative */
1639          0,                     /* bitpos */
1640          complain_overflow_signed, /* complain_on_overflow */
1641          ppc64_elf_unhandled_reloc, /* special_function */
1642          "R_PPC64_GOT_TLSGD16", /* name */
1643          FALSE,                 /* partial_inplace */
1644          0,                     /* src_mask */
1645          0xffff,                /* dst_mask */
1646          FALSE),                /* pcrel_offset */
1647
1648   /* Like GOT_TLSGD16, but no overflow.  */
1649   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1650          0,                     /* rightshift */
1651          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1652          16,                    /* bitsize */
1653          FALSE,                 /* pc_relative */
1654          0,                     /* bitpos */
1655          complain_overflow_dont, /* complain_on_overflow */
1656          ppc64_elf_unhandled_reloc, /* special_function */
1657          "R_PPC64_GOT_TLSGD16_LO", /* name */
1658          FALSE,                 /* partial_inplace */
1659          0,                     /* src_mask */
1660          0xffff,                /* dst_mask */
1661          FALSE),                /* pcrel_offset */
1662
1663   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1664   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1665          16,                    /* rightshift */
1666          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1667          16,                    /* bitsize */
1668          FALSE,                 /* pc_relative */
1669          0,                     /* bitpos */
1670          complain_overflow_dont, /* complain_on_overflow */
1671          ppc64_elf_unhandled_reloc, /* special_function */
1672          "R_PPC64_GOT_TLSGD16_HI", /* name */
1673          FALSE,                 /* partial_inplace */
1674          0,                     /* src_mask */
1675          0xffff,                /* dst_mask */
1676          FALSE),                /* pcrel_offset */
1677
1678   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1679   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1680          16,                    /* rightshift */
1681          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1682          16,                    /* bitsize */
1683          FALSE,                 /* pc_relative */
1684          0,                     /* bitpos */
1685          complain_overflow_dont, /* complain_on_overflow */
1686          ppc64_elf_unhandled_reloc, /* special_function */
1687          "R_PPC64_GOT_TLSGD16_HA", /* name */
1688          FALSE,                 /* partial_inplace */
1689          0,                     /* src_mask */
1690          0xffff,                /* dst_mask */
1691          FALSE),                /* pcrel_offset */
1692
1693   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1694      with values (sym+add)@dtpmod and zero, and computes the offset to the
1695      first entry relative to the TOC base (r2).  */
1696   HOWTO (R_PPC64_GOT_TLSLD16,
1697          0,                     /* rightshift */
1698          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1699          16,                    /* bitsize */
1700          FALSE,                 /* pc_relative */
1701          0,                     /* bitpos */
1702          complain_overflow_signed, /* complain_on_overflow */
1703          ppc64_elf_unhandled_reloc, /* special_function */
1704          "R_PPC64_GOT_TLSLD16", /* name */
1705          FALSE,                 /* partial_inplace */
1706          0,                     /* src_mask */
1707          0xffff,                /* dst_mask */
1708          FALSE),                /* pcrel_offset */
1709
1710   /* Like GOT_TLSLD16, but no overflow.  */
1711   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1712          0,                     /* rightshift */
1713          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1714          16,                    /* bitsize */
1715          FALSE,                 /* pc_relative */
1716          0,                     /* bitpos */
1717          complain_overflow_dont, /* complain_on_overflow */
1718          ppc64_elf_unhandled_reloc, /* special_function */
1719          "R_PPC64_GOT_TLSLD16_LO", /* name */
1720          FALSE,                 /* partial_inplace */
1721          0,                     /* src_mask */
1722          0xffff,                /* dst_mask */
1723          FALSE),                /* pcrel_offset */
1724
1725   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1726   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1727          16,                    /* rightshift */
1728          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1729          16,                    /* bitsize */
1730          FALSE,                 /* pc_relative */
1731          0,                     /* bitpos */
1732          complain_overflow_dont, /* complain_on_overflow */
1733          ppc64_elf_unhandled_reloc, /* special_function */
1734          "R_PPC64_GOT_TLSLD16_HI", /* name */
1735          FALSE,                 /* partial_inplace */
1736          0,                     /* src_mask */
1737          0xffff,                /* dst_mask */
1738          FALSE),                /* pcrel_offset */
1739
1740   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1741   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1742          16,                    /* rightshift */
1743          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1744          16,                    /* bitsize */
1745          FALSE,                 /* pc_relative */
1746          0,                     /* bitpos */
1747          complain_overflow_dont, /* complain_on_overflow */
1748          ppc64_elf_unhandled_reloc, /* special_function */
1749          "R_PPC64_GOT_TLSLD16_HA", /* name */
1750          FALSE,                 /* partial_inplace */
1751          0,                     /* src_mask */
1752          0xffff,                /* dst_mask */
1753          FALSE),                /* pcrel_offset */
1754
1755   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1756      the offset to the entry relative to the TOC base (r2).  */
1757   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1758          0,                     /* rightshift */
1759          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1760          16,                    /* bitsize */
1761          FALSE,                 /* pc_relative */
1762          0,                     /* bitpos */
1763          complain_overflow_signed, /* complain_on_overflow */
1764          ppc64_elf_unhandled_reloc, /* special_function */
1765          "R_PPC64_GOT_DTPREL16_DS", /* name */
1766          FALSE,                 /* partial_inplace */
1767          0,                     /* src_mask */
1768          0xfffc,                /* dst_mask */
1769          FALSE),                /* pcrel_offset */
1770
1771   /* Like GOT_DTPREL16_DS, but no overflow.  */
1772   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1773          0,                     /* rightshift */
1774          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1775          16,                    /* bitsize */
1776          FALSE,                 /* pc_relative */
1777          0,                     /* bitpos */
1778          complain_overflow_dont, /* complain_on_overflow */
1779          ppc64_elf_unhandled_reloc, /* special_function */
1780          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1781          FALSE,                 /* partial_inplace */
1782          0,                     /* src_mask */
1783          0xfffc,                /* dst_mask */
1784          FALSE),                /* pcrel_offset */
1785
1786   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1787   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1788          16,                    /* rightshift */
1789          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1790          16,                    /* bitsize */
1791          FALSE,                 /* pc_relative */
1792          0,                     /* bitpos */
1793          complain_overflow_dont, /* complain_on_overflow */
1794          ppc64_elf_unhandled_reloc, /* special_function */
1795          "R_PPC64_GOT_DTPREL16_HI", /* name */
1796          FALSE,                 /* partial_inplace */
1797          0,                     /* src_mask */
1798          0xffff,                /* dst_mask */
1799          FALSE),                /* pcrel_offset */
1800
1801   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1802   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1803          16,                    /* rightshift */
1804          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1805          16,                    /* bitsize */
1806          FALSE,                 /* pc_relative */
1807          0,                     /* bitpos */
1808          complain_overflow_dont, /* complain_on_overflow */
1809          ppc64_elf_unhandled_reloc, /* special_function */
1810          "R_PPC64_GOT_DTPREL16_HA", /* name */
1811          FALSE,                 /* partial_inplace */
1812          0,                     /* src_mask */
1813          0xffff,                /* dst_mask */
1814          FALSE),                /* pcrel_offset */
1815
1816   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1817      offset to the entry relative to the TOC base (r2).  */
1818   HOWTO (R_PPC64_GOT_TPREL16_DS,
1819          0,                     /* rightshift */
1820          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1821          16,                    /* bitsize */
1822          FALSE,                 /* pc_relative */
1823          0,                     /* bitpos */
1824          complain_overflow_signed, /* complain_on_overflow */
1825          ppc64_elf_unhandled_reloc, /* special_function */
1826          "R_PPC64_GOT_TPREL16_DS", /* name */
1827          FALSE,                 /* partial_inplace */
1828          0,                     /* src_mask */
1829          0xfffc,                /* dst_mask */
1830          FALSE),                /* pcrel_offset */
1831
1832   /* Like GOT_TPREL16_DS, but no overflow.  */
1833   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1834          0,                     /* rightshift */
1835          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1836          16,                    /* bitsize */
1837          FALSE,                 /* pc_relative */
1838          0,                     /* bitpos */
1839          complain_overflow_dont, /* complain_on_overflow */
1840          ppc64_elf_unhandled_reloc, /* special_function */
1841          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1842          FALSE,                 /* partial_inplace */
1843          0,                     /* src_mask */
1844          0xfffc,                /* dst_mask */
1845          FALSE),                /* pcrel_offset */
1846
1847   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1848   HOWTO (R_PPC64_GOT_TPREL16_HI,
1849          16,                    /* rightshift */
1850          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1851          16,                    /* bitsize */
1852          FALSE,                 /* pc_relative */
1853          0,                     /* bitpos */
1854          complain_overflow_dont, /* complain_on_overflow */
1855          ppc64_elf_unhandled_reloc, /* special_function */
1856          "R_PPC64_GOT_TPREL16_HI", /* name */
1857          FALSE,                 /* partial_inplace */
1858          0,                     /* src_mask */
1859          0xffff,                /* dst_mask */
1860          FALSE),                /* pcrel_offset */
1861
1862   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1863   HOWTO (R_PPC64_GOT_TPREL16_HA,
1864          16,                    /* rightshift */
1865          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1866          16,                    /* bitsize */
1867          FALSE,                 /* pc_relative */
1868          0,                     /* bitpos */
1869          complain_overflow_dont, /* complain_on_overflow */
1870          ppc64_elf_unhandled_reloc, /* special_function */
1871          "R_PPC64_GOT_TPREL16_HA", /* name */
1872          FALSE,                 /* partial_inplace */
1873          0,                     /* src_mask */
1874          0xffff,                /* dst_mask */
1875          FALSE),                /* pcrel_offset */
1876
1877   HOWTO (R_PPC64_JMP_IREL,      /* type */
1878          0,                     /* rightshift */
1879          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1880          0,                     /* bitsize */
1881          FALSE,                 /* pc_relative */
1882          0,                     /* bitpos */
1883          complain_overflow_dont, /* complain_on_overflow */
1884          ppc64_elf_unhandled_reloc, /* special_function */
1885          "R_PPC64_JMP_IREL",    /* name */
1886          FALSE,                 /* partial_inplace */
1887          0,                     /* src_mask */
1888          0,                     /* dst_mask */
1889          FALSE),                /* pcrel_offset */
1890
1891   HOWTO (R_PPC64_IRELATIVE,     /* type */
1892          0,                     /* rightshift */
1893          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1894          64,                    /* bitsize */
1895          FALSE,                 /* pc_relative */
1896          0,                     /* bitpos */
1897          complain_overflow_dont, /* complain_on_overflow */
1898          bfd_elf_generic_reloc, /* special_function */
1899          "R_PPC64_IRELATIVE",   /* name */
1900          FALSE,                 /* partial_inplace */
1901          0,                     /* src_mask */
1902          ONES (64),             /* dst_mask */
1903          FALSE),                /* pcrel_offset */
1904
1905   /* A 16 bit relative relocation.  */
1906   HOWTO (R_PPC64_REL16,         /* type */
1907          0,                     /* rightshift */
1908          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1909          16,                    /* bitsize */
1910          TRUE,                  /* pc_relative */
1911          0,                     /* bitpos */
1912          complain_overflow_bitfield, /* complain_on_overflow */
1913          bfd_elf_generic_reloc, /* special_function */
1914          "R_PPC64_REL16",       /* name */
1915          FALSE,                 /* partial_inplace */
1916          0,                     /* src_mask */
1917          0xffff,                /* dst_mask */
1918          TRUE),                 /* pcrel_offset */
1919
1920   /* A 16 bit relative relocation without overflow.  */
1921   HOWTO (R_PPC64_REL16_LO,      /* type */
1922          0,                     /* rightshift */
1923          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1924          16,                    /* bitsize */
1925          TRUE,                  /* pc_relative */
1926          0,                     /* bitpos */
1927          complain_overflow_dont,/* complain_on_overflow */
1928          bfd_elf_generic_reloc, /* special_function */
1929          "R_PPC64_REL16_LO",    /* name */
1930          FALSE,                 /* partial_inplace */
1931          0,                     /* src_mask */
1932          0xffff,                /* dst_mask */
1933          TRUE),                 /* pcrel_offset */
1934
1935   /* The high order 16 bits of a relative address.  */
1936   HOWTO (R_PPC64_REL16_HI,      /* type */
1937          16,                    /* rightshift */
1938          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1939          16,                    /* bitsize */
1940          TRUE,                  /* pc_relative */
1941          0,                     /* bitpos */
1942          complain_overflow_dont, /* complain_on_overflow */
1943          bfd_elf_generic_reloc, /* special_function */
1944          "R_PPC64_REL16_HI",    /* name */
1945          FALSE,                 /* partial_inplace */
1946          0,                     /* src_mask */
1947          0xffff,                /* dst_mask */
1948          TRUE),                 /* pcrel_offset */
1949
1950   /* The high order 16 bits of a relative address, plus 1 if the contents of
1951      the low 16 bits, treated as a signed number, is negative.  */
1952   HOWTO (R_PPC64_REL16_HA,      /* type */
1953          16,                    /* rightshift */
1954          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1955          16,                    /* bitsize */
1956          TRUE,                  /* pc_relative */
1957          0,                     /* bitpos */
1958          complain_overflow_dont, /* complain_on_overflow */
1959          ppc64_elf_ha_reloc,    /* special_function */
1960          "R_PPC64_REL16_HA",    /* name */
1961          FALSE,                 /* partial_inplace */
1962          0,                     /* src_mask */
1963          0xffff,                /* dst_mask */
1964          TRUE),                 /* pcrel_offset */
1965
1966   /* GNU extension to record C++ vtable hierarchy.  */
1967   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1968          0,                     /* rightshift */
1969          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1970          0,                     /* bitsize */
1971          FALSE,                 /* pc_relative */
1972          0,                     /* bitpos */
1973          complain_overflow_dont, /* complain_on_overflow */
1974          NULL,                  /* special_function */
1975          "R_PPC64_GNU_VTINHERIT", /* name */
1976          FALSE,                 /* partial_inplace */
1977          0,                     /* src_mask */
1978          0,                     /* dst_mask */
1979          FALSE),                /* pcrel_offset */
1980
1981   /* GNU extension to record C++ vtable member usage.  */
1982   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
1983          0,                     /* rightshift */
1984          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1985          0,                     /* bitsize */
1986          FALSE,                 /* pc_relative */
1987          0,                     /* bitpos */
1988          complain_overflow_dont, /* complain_on_overflow */
1989          NULL,                  /* special_function */
1990          "R_PPC64_GNU_VTENTRY", /* name */
1991          FALSE,                 /* partial_inplace */
1992          0,                     /* src_mask */
1993          0,                     /* dst_mask */
1994          FALSE),                /* pcrel_offset */
1995 };
1996
1997 \f
1998 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1999    be done.  */
2000
2001 static void
2002 ppc_howto_init (void)
2003 {
2004   unsigned int i, type;
2005
2006   for (i = 0;
2007        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2008        i++)
2009     {
2010       type = ppc64_elf_howto_raw[i].type;
2011       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2012                           / sizeof (ppc64_elf_howto_table[0])));
2013       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2014     }
2015 }
2016
2017 static reloc_howto_type *
2018 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2019                              bfd_reloc_code_real_type code)
2020 {
2021   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2022
2023   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2024     /* Initialize howto table if needed.  */
2025     ppc_howto_init ();
2026
2027   switch (code)
2028     {
2029     default:
2030       return NULL;
2031
2032     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2033       break;
2034     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2035       break;
2036     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2037       break;
2038     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2039       break;
2040     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2041       break;
2042     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2043       break;
2044     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2045       break;
2046     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2047       break;
2048     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2049       break;
2050     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2051       break;
2052     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2053       break;
2054     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2055       break;
2056     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2057       break;
2058     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2059       break;
2060     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2061       break;
2062     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2063       break;
2064     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2065       break;
2066     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2067       break;
2068     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2069       break;
2070     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2071       break;
2072     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2073       break;
2074     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2075       break;
2076     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2077       break;
2078     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2079       break;
2080     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2081       break;
2082     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2083       break;
2084     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2085       break;
2086     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2087       break;
2088     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2089       break;
2090     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2091       break;
2092     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2093       break;
2094     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2095       break;
2096     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2097       break;
2098     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2099       break;
2100     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2101       break;
2102     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2103       break;
2104     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2105       break;
2106     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2107       break;
2108     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2109       break;
2110     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2111       break;
2112     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2113       break;
2114     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2115       break;
2116     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2117       break;
2118     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2119       break;
2120     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2121       break;
2122     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2123       break;
2124     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2125       break;
2126     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2127       break;
2128     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2129       break;
2130     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2131       break;
2132     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2133       break;
2134     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2135       break;
2136     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2137       break;
2138     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2139       break;
2140     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2141       break;
2142     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2143       break;
2144     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2145       break;
2146     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2147       break;
2148     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2149       break;
2150     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2151       break;
2152     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2153       break;
2154     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2155       break;
2156     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2157       break;
2158     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2159       break;
2160     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2161       break;
2162     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2163       break;
2164     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2165       break;
2166     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2167       break;
2168     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2169       break;
2170     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2171       break;
2172     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2173       break;
2174     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2175       break;
2176     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2177       break;
2178     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2179       break;
2180     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2181       break;
2182     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2183       break;
2184     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2185       break;
2186     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2187       break;
2188     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2189       break;
2190     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2191       break;
2192     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2193       break;
2194     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2195       break;
2196     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2197       break;
2198     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2199       break;
2200     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2201       break;
2202     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2203       break;
2204     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2205       break;
2206     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2207       break;
2208     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2209       break;
2210     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2211       break;
2212     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2213       break;
2214     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2215       break;
2216     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2217       break;
2218     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2219       break;
2220     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2221       break;
2222     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2223       break;
2224     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2225       break;
2226     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2227       break;
2228     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2229       break;
2230     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2231       break;
2232     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2233       break;
2234     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2235       break;
2236     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2237       break;
2238     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2239       break;
2240     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2241       break;
2242     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2243       break;
2244     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2245       break;
2246     }
2247
2248   return ppc64_elf_howto_table[r];
2249 };
2250
2251 static reloc_howto_type *
2252 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2253                              const char *r_name)
2254 {
2255   unsigned int i;
2256
2257   for (i = 0;
2258        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2259        i++)
2260     if (ppc64_elf_howto_raw[i].name != NULL
2261         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2262       return &ppc64_elf_howto_raw[i];
2263
2264   return NULL;
2265 }
2266
2267 /* Set the howto pointer for a PowerPC ELF reloc.  */
2268
2269 static void
2270 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2271                          Elf_Internal_Rela *dst)
2272 {
2273   unsigned int type;
2274
2275   /* Initialize howto table if needed.  */
2276   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2277     ppc_howto_init ();
2278
2279   type = ELF64_R_TYPE (dst->r_info);
2280   if (type >= (sizeof (ppc64_elf_howto_table)
2281                / sizeof (ppc64_elf_howto_table[0])))
2282     {
2283       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2284                              abfd, (int) type);
2285       type = R_PPC64_NONE;
2286     }
2287   cache_ptr->howto = ppc64_elf_howto_table[type];
2288 }
2289
2290 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2291
2292 static bfd_reloc_status_type
2293 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2294                     void *data, asection *input_section,
2295                     bfd *output_bfd, char **error_message)
2296 {
2297   /* If this is a relocatable link (output_bfd test tells us), just
2298      call the generic function.  Any adjustment will be done at final
2299      link time.  */
2300   if (output_bfd != NULL)
2301     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2302                                   input_section, output_bfd, error_message);
2303
2304   /* Adjust the addend for sign extension of the low 16 bits.
2305      We won't actually be using the low 16 bits, so trashing them
2306      doesn't matter.  */
2307   reloc_entry->addend += 0x8000;
2308   return bfd_reloc_continue;
2309 }
2310
2311 static bfd_reloc_status_type
2312 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2313                         void *data, asection *input_section,
2314                         bfd *output_bfd, char **error_message)
2315 {
2316   if (output_bfd != NULL)
2317     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2318                                   input_section, output_bfd, error_message);
2319
2320   if (strcmp (symbol->section->name, ".opd") == 0
2321       && (symbol->section->owner->flags & DYNAMIC) == 0)
2322     {
2323       bfd_vma dest = opd_entry_value (symbol->section,
2324                                       symbol->value + reloc_entry->addend,
2325                                       NULL, NULL);
2326       if (dest != (bfd_vma) -1)
2327         reloc_entry->addend = dest - (symbol->value
2328                                       + symbol->section->output_section->vma
2329                                       + symbol->section->output_offset);
2330     }
2331   return bfd_reloc_continue;
2332 }
2333
2334 static bfd_reloc_status_type
2335 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2336                          void *data, asection *input_section,
2337                          bfd *output_bfd, char **error_message)
2338 {
2339   long insn;
2340   enum elf_ppc64_reloc_type r_type;
2341   bfd_size_type octets;
2342   /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
2343   bfd_boolean is_power4 = FALSE;
2344
2345   /* If this is a relocatable link (output_bfd test tells us), just
2346      call the generic function.  Any adjustment will be done at final
2347      link time.  */
2348   if (output_bfd != NULL)
2349     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2350                                   input_section, output_bfd, error_message);
2351
2352   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2353   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2354   insn &= ~(0x01 << 21);
2355   r_type = reloc_entry->howto->type;
2356   if (r_type == R_PPC64_ADDR14_BRTAKEN
2357       || r_type == R_PPC64_REL14_BRTAKEN)
2358     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2359
2360   if (is_power4)
2361     {
2362       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2363          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2364          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2365       if ((insn & (0x14 << 21)) == (0x04 << 21))
2366         insn |= 0x02 << 21;
2367       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2368         insn |= 0x08 << 21;
2369       else
2370         goto out;
2371     }
2372   else
2373     {
2374       bfd_vma target = 0;
2375       bfd_vma from;
2376
2377       if (!bfd_is_com_section (symbol->section))
2378         target = symbol->value;
2379       target += symbol->section->output_section->vma;
2380       target += symbol->section->output_offset;
2381       target += reloc_entry->addend;
2382
2383       from = (reloc_entry->address
2384               + input_section->output_offset
2385               + input_section->output_section->vma);
2386
2387       /* Invert 'y' bit if not the default.  */
2388       if ((bfd_signed_vma) (target - from) < 0)
2389         insn ^= 0x01 << 21;
2390     }
2391   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2392  out:
2393   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2394                                  input_section, output_bfd, error_message);
2395 }
2396
2397 static bfd_reloc_status_type
2398 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2399                          void *data, asection *input_section,
2400                          bfd *output_bfd, char **error_message)
2401 {
2402   /* If this is a relocatable link (output_bfd test tells us), just
2403      call the generic function.  Any adjustment will be done at final
2404      link time.  */
2405   if (output_bfd != NULL)
2406     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2407                                   input_section, output_bfd, error_message);
2408
2409   /* Subtract the symbol section base address.  */
2410   reloc_entry->addend -= symbol->section->output_section->vma;
2411   return bfd_reloc_continue;
2412 }
2413
2414 static bfd_reloc_status_type
2415 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2416                             void *data, asection *input_section,
2417                             bfd *output_bfd, char **error_message)
2418 {
2419   /* If this is a relocatable link (output_bfd test tells us), just
2420      call the generic function.  Any adjustment will be done at final
2421      link time.  */
2422   if (output_bfd != NULL)
2423     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2424                                   input_section, output_bfd, error_message);
2425
2426   /* Subtract the symbol section base address.  */
2427   reloc_entry->addend -= symbol->section->output_section->vma;
2428
2429   /* Adjust the addend for sign extension of the low 16 bits.  */
2430   reloc_entry->addend += 0x8000;
2431   return bfd_reloc_continue;
2432 }
2433
2434 static bfd_reloc_status_type
2435 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2436                      void *data, asection *input_section,
2437                      bfd *output_bfd, char **error_message)
2438 {
2439   bfd_vma TOCstart;
2440
2441   /* If this is a relocatable link (output_bfd test tells us), just
2442      call the generic function.  Any adjustment will be done at final
2443      link time.  */
2444   if (output_bfd != NULL)
2445     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2446                                   input_section, output_bfd, error_message);
2447
2448   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2449   if (TOCstart == 0)
2450     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2451
2452   /* Subtract the TOC base address.  */
2453   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2454   return bfd_reloc_continue;
2455 }
2456
2457 static bfd_reloc_status_type
2458 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2459                         void *data, asection *input_section,
2460                         bfd *output_bfd, char **error_message)
2461 {
2462   bfd_vma TOCstart;
2463
2464   /* If this is a relocatable link (output_bfd test tells us), just
2465      call the generic function.  Any adjustment will be done at final
2466      link time.  */
2467   if (output_bfd != NULL)
2468     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2469                                   input_section, output_bfd, error_message);
2470
2471   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2472   if (TOCstart == 0)
2473     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2474
2475   /* Subtract the TOC base address.  */
2476   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2477
2478   /* Adjust the addend for sign extension of the low 16 bits.  */
2479   reloc_entry->addend += 0x8000;
2480   return bfd_reloc_continue;
2481 }
2482
2483 static bfd_reloc_status_type
2484 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2485                        void *data, asection *input_section,
2486                        bfd *output_bfd, char **error_message)
2487 {
2488   bfd_vma TOCstart;
2489   bfd_size_type octets;
2490
2491   /* If this is a relocatable link (output_bfd test tells us), just
2492      call the generic function.  Any adjustment will be done at final
2493      link time.  */
2494   if (output_bfd != NULL)
2495     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2496                                   input_section, output_bfd, error_message);
2497
2498   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2499   if (TOCstart == 0)
2500     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2501
2502   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2503   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2504   return bfd_reloc_ok;
2505 }
2506
2507 static bfd_reloc_status_type
2508 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2509                            void *data, asection *input_section,
2510                            bfd *output_bfd, char **error_message)
2511 {
2512   /* If this is a relocatable link (output_bfd test tells us), just
2513      call the generic function.  Any adjustment will be done at final
2514      link time.  */
2515   if (output_bfd != NULL)
2516     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2517                                   input_section, output_bfd, error_message);
2518
2519   if (error_message != NULL)
2520     {
2521       static char buf[60];
2522       sprintf (buf, "generic linker can't handle %s",
2523                reloc_entry->howto->name);
2524       *error_message = buf;
2525     }
2526   return bfd_reloc_dangerous;
2527 }
2528
2529 /* Track GOT entries needed for a given symbol.  We might need more
2530    than one got entry per symbol.  */
2531 struct got_entry
2532 {
2533   struct got_entry *next;
2534
2535   /* The symbol addend that we'll be placing in the GOT.  */
2536   bfd_vma addend;
2537
2538   /* Unlike other ELF targets, we use separate GOT entries for the same
2539      symbol referenced from different input files.  This is to support
2540      automatic multiple TOC/GOT sections, where the TOC base can vary
2541      from one input file to another.  After partitioning into TOC groups
2542      we merge entries within the group.
2543
2544      Point to the BFD owning this GOT entry.  */
2545   bfd *owner;
2546
2547   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2548      TLS_TPREL or TLS_DTPREL for tls entries.  */
2549   unsigned char tls_type;
2550
2551   /* Non-zero if got.ent points to real entry.  */
2552   unsigned char is_indirect;
2553
2554   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2555   union
2556     {
2557       bfd_signed_vma refcount;
2558       bfd_vma offset;
2559       struct got_entry *ent;
2560     } got;
2561 };
2562
2563 /* The same for PLT.  */
2564 struct plt_entry
2565 {
2566   struct plt_entry *next;
2567
2568   bfd_vma addend;
2569
2570   union
2571     {
2572       bfd_signed_vma refcount;
2573       bfd_vma offset;
2574     } plt;
2575 };
2576
2577 struct ppc64_elf_obj_tdata
2578 {
2579   struct elf_obj_tdata elf;
2580
2581   /* Shortcuts to dynamic linker sections.  */
2582   asection *got;
2583   asection *relgot;
2584
2585   /* Used during garbage collection.  We attach global symbols defined
2586      on removed .opd entries to this section so that the sym is removed.  */
2587   asection *deleted_section;
2588
2589   /* TLS local dynamic got entry handling.  Support for multiple GOT
2590      sections means we potentially need one of these for each input bfd.  */
2591   struct got_entry tlsld_got;
2592
2593   /* A copy of relocs before they are modified for --emit-relocs.  */
2594   Elf_Internal_Rela *opd_relocs;
2595
2596   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2597      the reloc to be in the range -32768 to 32767.  */
2598   unsigned int has_small_toc_reloc;
2599 };
2600
2601 #define ppc64_elf_tdata(bfd) \
2602   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2603
2604 #define ppc64_tlsld_got(bfd) \
2605   (&ppc64_elf_tdata (bfd)->tlsld_got)
2606
2607 #define is_ppc64_elf(bfd) \
2608   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2609    && elf_object_id (bfd) == PPC64_ELF_DATA)
2610
2611 /* Override the generic function because we store some extras.  */
2612
2613 static bfd_boolean
2614 ppc64_elf_mkobject (bfd *abfd)
2615 {
2616   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2617                                   PPC64_ELF_DATA);
2618 }
2619
2620 /* Fix bad default arch selected for a 64 bit input bfd when the
2621    default is 32 bit.  */
2622
2623 static bfd_boolean
2624 ppc64_elf_object_p (bfd *abfd)
2625 {
2626   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2627     {
2628       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2629
2630       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2631         {
2632           /* Relies on arch after 32 bit default being 64 bit default.  */
2633           abfd->arch_info = abfd->arch_info->next;
2634           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2635         }
2636     }
2637   return TRUE;
2638 }
2639
2640 /* Support for core dump NOTE sections.  */
2641
2642 static bfd_boolean
2643 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2644 {
2645   size_t offset, size;
2646
2647   if (note->descsz != 504)
2648     return FALSE;
2649
2650   /* pr_cursig */
2651   elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2652
2653   /* pr_pid */
2654   elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 32);
2655
2656   /* pr_reg */
2657   offset = 112;
2658   size = 384;
2659
2660   /* Make a ".reg/999" section.  */
2661   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2662                                           size, note->descpos + offset);
2663 }
2664
2665 static bfd_boolean
2666 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2667 {
2668   if (note->descsz != 136)
2669     return FALSE;
2670
2671   elf_tdata (abfd)->core_program
2672     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2673   elf_tdata (abfd)->core_command
2674     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2675
2676   return TRUE;
2677 }
2678
2679 static char *
2680 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2681                            ...)
2682 {
2683   switch (note_type)
2684     {
2685     default:
2686       return NULL;
2687
2688     case NT_PRPSINFO:
2689       {
2690         char data[136];
2691         va_list ap;
2692
2693         va_start (ap, note_type);
2694         memset (data, 0, 40);
2695         strncpy (data + 40, va_arg (ap, const char *), 16);
2696         strncpy (data + 56, va_arg (ap, const char *), 80);
2697         va_end (ap);
2698         return elfcore_write_note (abfd, buf, bufsiz,
2699                                    "CORE", note_type, data, sizeof (data));
2700       }
2701
2702     case NT_PRSTATUS:
2703       {
2704         char data[504];
2705         va_list ap;
2706         long pid;
2707         int cursig;
2708         const void *greg;
2709
2710         va_start (ap, note_type);
2711         memset (data, 0, 112);
2712         pid = va_arg (ap, long);
2713         bfd_put_32 (abfd, pid, data + 32);
2714         cursig = va_arg (ap, int);
2715         bfd_put_16 (abfd, cursig, data + 12);
2716         greg = va_arg (ap, const void *);
2717         memcpy (data + 112, greg, 384);
2718         memset (data + 496, 0, 8);
2719         va_end (ap);
2720         return elfcore_write_note (abfd, buf, bufsiz,
2721                                    "CORE", note_type, data, sizeof (data));
2722       }
2723     }
2724 }
2725
2726 /* Merge backend specific data from an object file to the output
2727    object file when linking.  */
2728
2729 static bfd_boolean
2730 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2731 {
2732   /* Check if we have the same endianess.  */
2733   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2734       && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
2735       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2736     {
2737       const char *msg;
2738
2739       if (bfd_big_endian (ibfd))
2740         msg = _("%B: compiled for a big endian system "
2741                 "and target is little endian");
2742       else
2743         msg = _("%B: compiled for a little endian system "
2744                 "and target is big endian");
2745
2746       (*_bfd_error_handler) (msg, ibfd);
2747
2748       bfd_set_error (bfd_error_wrong_format);
2749       return FALSE;
2750     }
2751
2752   return TRUE;
2753 }
2754
2755 /* Add extra PPC sections.  */
2756
2757 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2758 {
2759   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
2760   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2761   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2762   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2763   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2764   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2765   { NULL,                     0,  0, 0,            0 }
2766 };
2767
2768 enum _ppc64_sec_type {
2769   sec_normal = 0,
2770   sec_opd = 1,
2771   sec_toc = 2
2772 };
2773
2774 struct _ppc64_elf_section_data
2775 {
2776   struct bfd_elf_section_data elf;
2777
2778   union
2779   {
2780     /* An array with one entry for each opd function descriptor.  */
2781     struct _opd_sec_data
2782     {
2783       /* Points to the function code section for local opd entries.  */
2784       asection **func_sec;
2785
2786       /* After editing .opd, adjust references to opd local syms.  */
2787       long *adjust;
2788     } opd;
2789
2790     /* An array for toc sections, indexed by offset/8.  */
2791     struct _toc_sec_data
2792     {
2793       /* Specifies the relocation symbol index used at a given toc offset.  */
2794       unsigned *symndx;
2795
2796       /* And the relocation addend.  */
2797       bfd_vma *add;
2798     } toc;
2799   } u;
2800
2801   enum _ppc64_sec_type sec_type:2;
2802
2803   /* Flag set when small branches are detected.  Used to
2804      select suitable defaults for the stub group size.  */
2805   unsigned int has_14bit_branch:1;
2806 };
2807
2808 #define ppc64_elf_section_data(sec) \
2809   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2810
2811 static bfd_boolean
2812 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2813 {
2814   if (!sec->used_by_bfd)
2815     {
2816       struct _ppc64_elf_section_data *sdata;
2817       bfd_size_type amt = sizeof (*sdata);
2818
2819       sdata = bfd_zalloc (abfd, amt);
2820       if (sdata == NULL)
2821         return FALSE;
2822       sec->used_by_bfd = sdata;
2823     }
2824
2825   return _bfd_elf_new_section_hook (abfd, sec);
2826 }
2827
2828 static struct _opd_sec_data *
2829 get_opd_info (asection * sec)
2830 {
2831   if (sec != NULL
2832       && ppc64_elf_section_data (sec) != NULL
2833       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2834     return &ppc64_elf_section_data (sec)->u.opd;
2835   return NULL;
2836 }
2837 \f
2838 /* Parameters for the qsort hook.  */
2839 static bfd_boolean synthetic_relocatable;
2840
2841 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
2842
2843 static int
2844 compare_symbols (const void *ap, const void *bp)
2845 {
2846   const asymbol *a = * (const asymbol **) ap;
2847   const asymbol *b = * (const asymbol **) bp;
2848
2849   /* Section symbols first.  */
2850   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2851     return -1;
2852   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2853     return 1;
2854
2855   /* then .opd symbols.  */
2856   if (strcmp (a->section->name, ".opd") == 0
2857       && strcmp (b->section->name, ".opd") != 0)
2858     return -1;
2859   if (strcmp (a->section->name, ".opd") != 0
2860       && strcmp (b->section->name, ".opd") == 0)
2861     return 1;
2862
2863   /* then other code symbols.  */
2864   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2865       == (SEC_CODE | SEC_ALLOC)
2866       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2867          != (SEC_CODE | SEC_ALLOC))
2868     return -1;
2869
2870   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2871       != (SEC_CODE | SEC_ALLOC)
2872       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2873          == (SEC_CODE | SEC_ALLOC))
2874     return 1;
2875
2876   if (synthetic_relocatable)
2877     {
2878       if (a->section->id < b->section->id)
2879         return -1;
2880
2881       if (a->section->id > b->section->id)
2882         return 1;
2883     }
2884
2885   if (a->value + a->section->vma < b->value + b->section->vma)
2886     return -1;
2887
2888   if (a->value + a->section->vma > b->value + b->section->vma)
2889     return 1;
2890
2891   /* For syms with the same value, prefer strong dynamic global function
2892      syms over other syms.  */
2893   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2894     return -1;
2895
2896   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2897     return 1;
2898
2899   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2900     return -1;
2901
2902   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2903     return 1;
2904
2905   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2906     return -1;
2907
2908   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2909     return 1;
2910
2911   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2912     return -1;
2913
2914   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2915     return 1;
2916
2917   return 0;
2918 }
2919
2920 /* Search SYMS for a symbol of the given VALUE.  */
2921
2922 static asymbol *
2923 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
2924 {
2925   long mid;
2926
2927   if (id == -1)
2928     {
2929       while (lo < hi)
2930         {
2931           mid = (lo + hi) >> 1;
2932           if (syms[mid]->value + syms[mid]->section->vma < value)
2933             lo = mid + 1;
2934           else if (syms[mid]->value + syms[mid]->section->vma > value)
2935             hi = mid;
2936           else
2937             return syms[mid];
2938         }
2939     }
2940   else
2941     {
2942       while (lo < hi)
2943         {
2944           mid = (lo + hi) >> 1;
2945           if (syms[mid]->section->id < id)
2946             lo = mid + 1;
2947           else if (syms[mid]->section->id > id)
2948             hi = mid;
2949           else if (syms[mid]->value < value)
2950             lo = mid + 1;
2951           else if (syms[mid]->value > value)
2952             hi = mid;
2953           else
2954             return syms[mid];
2955         }
2956     }
2957   return NULL;
2958 }
2959
2960 static bfd_boolean
2961 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2962 {
2963   bfd_vma vma = *(bfd_vma *) ptr;
2964   return ((section->flags & SEC_ALLOC) != 0
2965           && section->vma <= vma
2966           && vma < section->vma + section->size);
2967 }
2968
2969 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2970    entry syms.  Also generate @plt symbols for the glink branch table.  */
2971
2972 static long
2973 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2974                                 long static_count, asymbol **static_syms,
2975                                 long dyn_count, asymbol **dyn_syms,
2976                                 asymbol **ret)
2977 {
2978   asymbol *s;
2979   long i;
2980   long count;
2981   char *names;
2982   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2983   asection *opd;
2984   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2985   asymbol **syms;
2986
2987   *ret = NULL;
2988
2989   opd = bfd_get_section_by_name (abfd, ".opd");
2990   if (opd == NULL)
2991     return 0;
2992
2993   symcount = static_count;
2994   if (!relocatable)
2995     symcount += dyn_count;
2996   if (symcount == 0)
2997     return 0;
2998
2999   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3000   if (syms == NULL)
3001     return -1;
3002
3003   if (!relocatable && static_count != 0 && dyn_count != 0)
3004     {
3005       /* Use both symbol tables.  */
3006       memcpy (syms, static_syms, static_count * sizeof (*syms));
3007       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3008     }
3009   else if (!relocatable && static_count == 0)
3010     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3011   else
3012     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3013
3014   synthetic_relocatable = relocatable;
3015   qsort (syms, symcount, sizeof (*syms), compare_symbols);
3016
3017   if (!relocatable && symcount > 1)
3018     {
3019       long j;
3020       /* Trim duplicate syms, since we may have merged the normal and
3021          dynamic symbols.  Actually, we only care about syms that have
3022          different values, so trim any with the same value.  */
3023       for (i = 1, j = 1; i < symcount; ++i)
3024         if (syms[i - 1]->value + syms[i - 1]->section->vma
3025             != syms[i]->value + syms[i]->section->vma)
3026           syms[j++] = syms[i];
3027       symcount = j;
3028     }
3029
3030   i = 0;
3031   if (strcmp (syms[i]->section->name, ".opd") == 0)
3032     ++i;
3033   codesecsym = i;
3034
3035   for (; i < symcount; ++i)
3036     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3037          != (SEC_CODE | SEC_ALLOC))
3038         || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3039       break;
3040   codesecsymend = i;
3041
3042   for (; i < symcount; ++i)
3043     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3044       break;
3045   secsymend = i;
3046
3047   for (; i < symcount; ++i)
3048     if (strcmp (syms[i]->section->name, ".opd") != 0)
3049       break;
3050   opdsymend = i;
3051
3052   for (; i < symcount; ++i)
3053     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3054         != (SEC_CODE | SEC_ALLOC))
3055       break;
3056   symcount = i;
3057
3058   count = 0;
3059
3060   if (relocatable)
3061     {
3062       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3063       arelent *r;
3064       size_t size;
3065       long relcount;
3066
3067       if (opdsymend == secsymend)
3068         goto done;
3069
3070       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3071       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3072       if (relcount == 0)
3073         goto done;
3074
3075       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3076         {
3077           count = -1;
3078           goto done;
3079         }
3080
3081       size = 0;
3082       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3083         {
3084           asymbol *sym;
3085
3086           while (r < opd->relocation + relcount
3087                  && r->address < syms[i]->value + opd->vma)
3088             ++r;
3089
3090           if (r == opd->relocation + relcount)
3091             break;
3092
3093           if (r->address != syms[i]->value + opd->vma)
3094             continue;
3095
3096           if (r->howto->type != R_PPC64_ADDR64)
3097             continue;
3098
3099           sym = *r->sym_ptr_ptr;
3100           if (!sym_exists_at (syms, opdsymend, symcount,
3101                               sym->section->id, sym->value + r->addend))
3102             {
3103               ++count;
3104               size += sizeof (asymbol);
3105               size += strlen (syms[i]->name) + 2;
3106             }
3107         }
3108
3109       s = *ret = bfd_malloc (size);
3110       if (s == NULL)
3111         {
3112           count = -1;
3113           goto done;
3114         }
3115
3116       names = (char *) (s + count);
3117
3118       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3119         {
3120           asymbol *sym;
3121
3122           while (r < opd->relocation + relcount
3123                  && r->address < syms[i]->value + opd->vma)
3124             ++r;
3125
3126           if (r == opd->relocation + relcount)
3127             break;
3128
3129           if (r->address != syms[i]->value + opd->vma)
3130             continue;
3131
3132           if (r->howto->type != R_PPC64_ADDR64)
3133             continue;
3134
3135           sym = *r->sym_ptr_ptr;
3136           if (!sym_exists_at (syms, opdsymend, symcount,
3137                               sym->section->id, sym->value + r->addend))
3138             {
3139               size_t len;
3140
3141               *s = *syms[i];
3142               s->flags |= BSF_SYNTHETIC;
3143               s->section = sym->section;
3144               s->value = sym->value + r->addend;
3145               s->name = names;
3146               *names++ = '.';
3147               len = strlen (syms[i]->name);
3148               memcpy (names, syms[i]->name, len + 1);
3149               names += len + 1;
3150               /* Have udata.p point back to the original symbol this
3151                  synthetic symbol was derived from.  */
3152               s->udata.p = syms[i];
3153               s++;
3154             }
3155         }
3156     }
3157   else
3158     {
3159       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3160       bfd_byte *contents;
3161       size_t size;
3162       long plt_count = 0;
3163       bfd_vma glink_vma = 0, resolv_vma = 0;
3164       asection *dynamic, *glink = NULL, *relplt = NULL;
3165       arelent *p;
3166
3167       if (!bfd_malloc_and_get_section (abfd, opd, &contents))
3168         {
3169           if (contents)
3170             {
3171             free_contents_and_exit:
3172               free (contents);
3173             }
3174           count = -1;
3175           goto done;
3176         }
3177
3178       size = 0;
3179       for (i = secsymend; i < opdsymend; ++i)
3180         {
3181           bfd_vma ent;
3182
3183           /* Ignore bogus symbols.  */
3184           if (syms[i]->value > opd->size - 8)
3185             continue;
3186
3187           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3188           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3189             {
3190               ++count;
3191               size += sizeof (asymbol);
3192               size += strlen (syms[i]->name) + 2;
3193             }
3194         }
3195
3196       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3197       if (dyn_count != 0
3198           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3199         {
3200           bfd_byte *dynbuf, *extdyn, *extdynend;
3201           size_t extdynsize;
3202           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3203
3204           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3205             goto free_contents_and_exit;
3206
3207           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3208           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3209
3210           extdyn = dynbuf;
3211           extdynend = extdyn + dynamic->size;
3212           for (; extdyn < extdynend; extdyn += extdynsize)
3213             {
3214               Elf_Internal_Dyn dyn;
3215               (*swap_dyn_in) (abfd, extdyn, &dyn);
3216
3217               if (dyn.d_tag == DT_NULL)
3218                 break;
3219
3220               if (dyn.d_tag == DT_PPC64_GLINK)
3221                 {
3222                   /* The first glink stub starts at offset 32; see comment in
3223                      ppc64_elf_finish_dynamic_sections. */
3224                   glink_vma = dyn.d_un.d_val + 32;
3225                   /* The .glink section usually does not survive the final
3226                      link; search for the section (usually .text) where the
3227                      glink stubs now reside.  */
3228                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3229                                                 &glink_vma);
3230                   break;
3231                 }
3232             }
3233
3234           free (dynbuf);
3235         }
3236
3237       if (glink != NULL)
3238         {
3239           /* Determine __glink trampoline by reading the relative branch
3240              from the first glink stub.  */
3241           bfd_byte buf[4];
3242           if (bfd_get_section_contents (abfd, glink, buf,
3243                                         glink_vma + 4 - glink->vma, 4))
3244             {
3245               unsigned int insn = bfd_get_32 (abfd, buf);
3246               insn ^= B_DOT;
3247               if ((insn & ~0x3fffffc) == 0)
3248                 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3249             }
3250
3251           if (resolv_vma)
3252             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3253
3254           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3255           if (relplt != NULL)
3256             {
3257               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3258               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3259                 goto free_contents_and_exit;
3260         
3261               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3262               size += plt_count * sizeof (asymbol);
3263
3264               p = relplt->relocation;
3265               for (i = 0; i < plt_count; i++, p++)
3266                 {
3267                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3268                   if (p->addend != 0)
3269                     size += sizeof ("+0x") - 1 + 16;
3270                 }
3271             }
3272         }
3273
3274       s = *ret = bfd_malloc (size);
3275       if (s == NULL)
3276         goto free_contents_and_exit;
3277
3278       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3279
3280       for (i = secsymend; i < opdsymend; ++i)
3281         {
3282           bfd_vma ent;
3283
3284           if (syms[i]->value > opd->size - 8)
3285             continue;
3286
3287           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3288           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3289             {
3290               long lo, hi;
3291               size_t len;
3292               asection *sec = abfd->sections;
3293
3294               *s = *syms[i];
3295               lo = codesecsym;
3296               hi = codesecsymend;
3297               while (lo < hi)
3298                 {
3299                   long mid = (lo + hi) >> 1;
3300                   if (syms[mid]->section->vma < ent)
3301                     lo = mid + 1;
3302                   else if (syms[mid]->section->vma > ent)
3303                     hi = mid;
3304                   else
3305                     {
3306                       sec = syms[mid]->section;
3307                       break;
3308                     }
3309                 }
3310
3311               if (lo >= hi && lo > codesecsym)
3312                 sec = syms[lo - 1]->section;
3313
3314               for (; sec != NULL; sec = sec->next)
3315                 {
3316                   if (sec->vma > ent)
3317                     break;
3318                   if ((sec->flags & SEC_ALLOC) == 0
3319                       || (sec->flags & SEC_LOAD) == 0)
3320                     break;
3321                   if ((sec->flags & SEC_CODE) != 0)
3322                     s->section = sec;
3323                 }
3324               s->flags |= BSF_SYNTHETIC;
3325               s->value = ent - s->section->vma;
3326               s->name = names;
3327               *names++ = '.';
3328               len = strlen (syms[i]->name);
3329               memcpy (names, syms[i]->name, len + 1);
3330               names += len + 1;
3331               /* Have udata.p point back to the original symbol this
3332                  synthetic symbol was derived from.  */
3333               s->udata.p = syms[i];
3334               s++;
3335             }
3336         }
3337       free (contents);
3338
3339       if (glink != NULL && relplt != NULL)
3340         {
3341           if (resolv_vma)
3342             {
3343               /* Add a symbol for the main glink trampoline.  */
3344               memset (s, 0, sizeof *s);
3345               s->the_bfd = abfd;
3346               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3347               s->section = glink;
3348               s->value = resolv_vma - glink->vma;
3349               s->name = names;
3350               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3351               names += sizeof ("__glink_PLTresolve");
3352               s++;
3353               count++;
3354             }
3355
3356           /* FIXME: It would be very much nicer to put sym@plt on the
3357              stub rather than on the glink branch table entry.  The
3358              objdump disassembler would then use a sensible symbol
3359              name on plt calls.  The difficulty in doing so is
3360              a) finding the stubs, and,
3361              b) matching stubs against plt entries, and,
3362              c) there can be multiple stubs for a given plt entry.
3363
3364              Solving (a) could be done by code scanning, but older
3365              ppc64 binaries used different stubs to current code.
3366              (b) is the tricky one since you need to known the toc
3367              pointer for at least one function that uses a pic stub to
3368              be able to calculate the plt address referenced.
3369              (c) means gdb would need to set multiple breakpoints (or
3370              find the glink branch itself) when setting breakpoints
3371              for pending shared library loads.  */
3372           p = relplt->relocation;
3373           for (i = 0; i < plt_count; i++, p++)
3374             {
3375               size_t len;
3376
3377               *s = **p->sym_ptr_ptr;
3378               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3379                  we are defining a symbol, ensure one of them is set.  */
3380               if ((s->flags & BSF_LOCAL) == 0)
3381                 s->flags |= BSF_GLOBAL;
3382               s->flags |= BSF_SYNTHETIC;
3383               s->section = glink;
3384               s->value = glink_vma - glink->vma;
3385               s->name = names;
3386               s->udata.p = NULL;
3387               len = strlen ((*p->sym_ptr_ptr)->name);
3388               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3389               names += len;
3390               if (p->addend != 0)
3391                 {
3392                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3393                   names += sizeof ("+0x") - 1;
3394                   bfd_sprintf_vma (abfd, names, p->addend);
3395                   names += strlen (names);
3396                 }
3397               memcpy (names, "@plt", sizeof ("@plt"));
3398               names += sizeof ("@plt");
3399               s++;
3400               glink_vma += 8;
3401               if (i >= 0x8000)
3402                 glink_vma += 4;
3403             }
3404           count += plt_count;
3405         }
3406     }
3407
3408  done:
3409   free (syms);
3410   return count;
3411 }
3412 \f
3413 /* The following functions are specific to the ELF linker, while
3414    functions above are used generally.  Those named ppc64_elf_* are
3415    called by the main ELF linker code.  They appear in this file more
3416    or less in the order in which they are called.  eg.
3417    ppc64_elf_check_relocs is called early in the link process,
3418    ppc64_elf_finish_dynamic_sections is one of the last functions
3419    called.
3420
3421    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3422    functions have both a function code symbol and a function descriptor
3423    symbol.  A call to foo in a relocatable object file looks like:
3424
3425    .            .text
3426    .    x:
3427    .            bl      .foo
3428    .            nop
3429
3430    The function definition in another object file might be:
3431
3432    .            .section .opd
3433    .    foo:    .quad   .foo
3434    .            .quad   .TOC.@tocbase
3435    .            .quad   0
3436    .
3437    .            .text
3438    .    .foo:   blr
3439
3440    When the linker resolves the call during a static link, the branch
3441    unsurprisingly just goes to .foo and the .opd information is unused.
3442    If the function definition is in a shared library, things are a little
3443    different:  The call goes via a plt call stub, the opd information gets
3444    copied to the plt, and the linker patches the nop.
3445
3446    .    x:
3447    .            bl      .foo_stub
3448    .            ld      2,40(1)
3449    .
3450    .
3451    .    .foo_stub:
3452    .            addis   12,2,Lfoo@toc@ha        # in practice, the call stub
3453    .            addi    12,12,Lfoo@toc@l        # is slightly optimized, but
3454    .            std     2,40(1)                 # this is the general idea
3455    .            ld      11,0(12)
3456    .            ld      2,8(12)
3457    .            mtctr   11
3458    .            ld      11,16(12)
3459    .            bctr
3460    .
3461    .            .section .plt
3462    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3463
3464    The "reloc ()" notation is supposed to indicate that the linker emits
3465    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3466    copying.
3467
3468    What are the difficulties here?  Well, firstly, the relocations
3469    examined by the linker in check_relocs are against the function code
3470    sym .foo, while the dynamic relocation in the plt is emitted against
3471    the function descriptor symbol, foo.  Somewhere along the line, we need
3472    to carefully copy dynamic link information from one symbol to the other.
3473    Secondly, the generic part of the elf linker will make .foo a dynamic
3474    symbol as is normal for most other backends.  We need foo dynamic
3475    instead, at least for an application final link.  However, when
3476    creating a shared library containing foo, we need to have both symbols
3477    dynamic so that references to .foo are satisfied during the early
3478    stages of linking.  Otherwise the linker might decide to pull in a
3479    definition from some other object, eg. a static library.
3480
3481    Update: As of August 2004, we support a new convention.  Function
3482    calls may use the function descriptor symbol, ie. "bl foo".  This
3483    behaves exactly as "bl .foo".  */
3484
3485 /* The linker needs to keep track of the number of relocs that it
3486    decides to copy as dynamic relocs in check_relocs for each symbol.
3487    This is so that it can later discard them if they are found to be
3488    unnecessary.  We store the information in a field extending the
3489    regular ELF linker hash table.  */
3490
3491 struct ppc_dyn_relocs
3492 {
3493   struct ppc_dyn_relocs *next;
3494
3495   /* The input section of the reloc.  */
3496   asection *sec;
3497
3498   /* Total number of relocs copied for the input section.  */
3499   bfd_size_type count;
3500
3501   /* Number of pc-relative relocs copied for the input section.  */
3502   bfd_size_type pc_count;
3503 };
3504
3505 /* Of those relocs that might be copied as dynamic relocs, this function
3506    selects those that must be copied when linking a shared library,
3507    even when the symbol is local.  */
3508
3509 static int
3510 must_be_dyn_reloc (struct bfd_link_info *info,
3511                    enum elf_ppc64_reloc_type r_type)
3512 {
3513   switch (r_type)
3514     {
3515     default:
3516       return 1;
3517
3518     case R_PPC64_REL32:
3519     case R_PPC64_REL64:
3520     case R_PPC64_REL30:
3521       return 0;
3522
3523     case R_PPC64_TPREL16:
3524     case R_PPC64_TPREL16_LO:
3525     case R_PPC64_TPREL16_HI:
3526     case R_PPC64_TPREL16_HA:
3527     case R_PPC64_TPREL16_DS:
3528     case R_PPC64_TPREL16_LO_DS:
3529     case R_PPC64_TPREL16_HIGHER:
3530     case R_PPC64_TPREL16_HIGHERA:
3531     case R_PPC64_TPREL16_HIGHEST:
3532     case R_PPC64_TPREL16_HIGHESTA:
3533     case R_PPC64_TPREL64:
3534       return !info->executable;
3535     }
3536 }
3537
3538 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3539    copying dynamic variables from a shared lib into an app's dynbss
3540    section, and instead use a dynamic relocation to point into the
3541    shared lib.  With code that gcc generates, it's vital that this be
3542    enabled;  In the PowerPC64 ABI, the address of a function is actually
3543    the address of a function descriptor, which resides in the .opd
3544    section.  gcc uses the descriptor directly rather than going via the
3545    GOT as some other ABI's do, which means that initialized function
3546    pointers must reference the descriptor.  Thus, a function pointer
3547    initialized to the address of a function in a shared library will
3548    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3549    redefines the function descriptor symbol to point to the copy.  This
3550    presents a problem as a plt entry for that function is also
3551    initialized from the function descriptor symbol and the copy reloc
3552    may not be initialized first.  */
3553 #define ELIMINATE_COPY_RELOCS 1
3554
3555 /* Section name for stubs is the associated section name plus this
3556    string.  */
3557 #define STUB_SUFFIX ".stub"
3558
3559 /* Linker stubs.
3560    ppc_stub_long_branch:
3561    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3562    destination, but a 24 bit branch in a stub section will reach.
3563    .    b       dest
3564
3565    ppc_stub_plt_branch:
3566    Similar to the above, but a 24 bit branch in the stub section won't
3567    reach its destination.
3568    .    addis   %r12,%r2,xxx@toc@ha
3569    .    ld      %r11,xxx@toc@l(%r12)
3570    .    mtctr   %r11
3571    .    bctr
3572
3573    ppc_stub_plt_call:
3574    Used to call a function in a shared library.  If it so happens that
3575    the plt entry referenced crosses a 64k boundary, then an extra
3576    "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
3577    .    addis   %r12,%r2,xxx@toc@ha
3578    .    std     %r2,40(%r1)
3579    .    ld      %r11,xxx+0@toc@l(%r12)
3580    .    mtctr   %r11
3581    .    ld      %r2,xxx+8@toc@l(%r12)
3582    .    ld      %r11,xxx+16@toc@l(%r12)
3583    .    bctr
3584
3585    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3586    code to adjust the value and save r2 to support multiple toc sections.
3587    A ppc_stub_long_branch with an r2 offset looks like:
3588    .    std     %r2,40(%r1)
3589    .    addis   %r2,%r2,off@ha
3590    .    addi    %r2,%r2,off@l
3591    .    b       dest
3592
3593    A ppc_stub_plt_branch with an r2 offset looks like:
3594    .    std     %r2,40(%r1)
3595    .    addis   %r12,%r2,xxx@toc@ha
3596    .    ld      %r11,xxx@toc@l(%r12)
3597    .    addis   %r2,%r2,off@ha
3598    .    addi    %r2,%r2,off@l
3599    .    mtctr   %r11
3600    .    bctr
3601
3602    In cases where the "addis" instruction would add zero, the "addis" is
3603    omitted and following instructions modified slightly in some cases.
3604 */
3605
3606 enum ppc_stub_type {
3607   ppc_stub_none,
3608   ppc_stub_long_branch,
3609   ppc_stub_long_branch_r2off,
3610   ppc_stub_plt_branch,
3611   ppc_stub_plt_branch_r2off,
3612   ppc_stub_plt_call
3613 };
3614
3615 struct ppc_stub_hash_entry {
3616
3617   /* Base hash table entry structure.  */
3618   struct bfd_hash_entry root;
3619
3620   enum ppc_stub_type stub_type;
3621
3622   /* The stub section.  */
3623   asection *stub_sec;
3624
3625   /* Offset within stub_sec of the beginning of this stub.  */
3626   bfd_vma stub_offset;
3627
3628   /* Given the symbol's value and its section we can determine its final
3629      value when building the stubs (so the stub knows where to jump.  */
3630   bfd_vma target_value;
3631   asection *target_section;
3632
3633   /* The symbol table entry, if any, that this was derived from.  */
3634   struct ppc_link_hash_entry *h;
3635   struct plt_entry *plt_ent;
3636
3637   /* And the reloc addend that this was derived from.  */
3638   bfd_vma addend;
3639
3640   /* Where this stub is being called from, or, in the case of combined
3641      stub sections, the first input section in the group.  */
3642   asection *id_sec;
3643 };
3644
3645 struct ppc_branch_hash_entry {
3646
3647   /* Base hash table entry structure.  */
3648   struct bfd_hash_entry root;
3649
3650   /* Offset within branch lookup table.  */
3651   unsigned int offset;
3652
3653   /* Generation marker.  */
3654   unsigned int iter;
3655 };
3656
3657 struct ppc_link_hash_entry
3658 {
3659   struct elf_link_hash_entry elf;
3660
3661   union {
3662     /* A pointer to the most recently used stub hash entry against this
3663        symbol.  */
3664     struct ppc_stub_hash_entry *stub_cache;
3665
3666     /* A pointer to the next symbol starting with a '.'  */
3667     struct ppc_link_hash_entry *next_dot_sym;
3668   } u;
3669
3670   /* Track dynamic relocs copied for this symbol.  */
3671   struct ppc_dyn_relocs *dyn_relocs;
3672
3673   /* Link between function code and descriptor symbols.  */
3674   struct ppc_link_hash_entry *oh;
3675
3676   /* Flag function code and descriptor symbols.  */
3677   unsigned int is_func:1;
3678   unsigned int is_func_descriptor:1;
3679   unsigned int fake:1;
3680
3681   /* Whether global opd/toc sym has been adjusted or not.
3682      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3683      should be set for all globals defined in any opd/toc section.  */
3684   unsigned int adjust_done:1;
3685
3686   /* Set if we twiddled this symbol to weak at some stage.  */
3687   unsigned int was_undefined:1;
3688
3689   /* Contexts in which symbol is used in the GOT (or TOC).
3690      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3691      corresponding relocs are encountered during check_relocs.
3692      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3693      indicate the corresponding GOT entry type is not needed.
3694      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3695      a TPREL one.  We use a separate flag rather than setting TPREL
3696      just for convenience in distinguishing the two cases.  */
3697 #define TLS_GD           1      /* GD reloc. */
3698 #define TLS_LD           2      /* LD reloc. */
3699 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
3700 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
3701 #define TLS_TLS         16      /* Any TLS reloc.  */
3702 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
3703 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
3704 #define PLT_IFUNC      128      /* STT_GNU_IFUNC.  */
3705   unsigned char tls_mask;
3706 };
3707
3708 /* ppc64 ELF linker hash table.  */
3709
3710 struct ppc_link_hash_table
3711 {
3712   struct elf_link_hash_table elf;
3713
3714   /* The stub hash table.  */
3715   struct bfd_hash_table stub_hash_table;
3716
3717   /* Another hash table for plt_branch stubs.  */
3718   struct bfd_hash_table branch_hash_table;
3719
3720   /* Linker stub bfd.  */
3721   bfd *stub_bfd;
3722
3723   /* Linker call-backs.  */
3724   asection * (*add_stub_section) (const char *, asection *);
3725   void (*layout_sections_again) (void);
3726
3727   /* Array to keep track of which stub sections have been created, and
3728      information on stub grouping.  */
3729   struct map_stub {
3730     /* This is the section to which stubs in the group will be attached.  */
3731     asection *link_sec;
3732     /* The stub section.  */
3733     asection *stub_sec;
3734     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
3735     bfd_vma toc_off;
3736   } *stub_group;
3737
3738   /* Temp used when calculating TOC pointers.  */
3739   bfd_vma toc_curr;
3740   bfd *toc_bfd;
3741   asection *toc_first_sec;
3742
3743   /* Highest input section id.  */
3744   int top_id;
3745
3746   /* Highest output section index.  */
3747   int top_index;
3748
3749   /* Used when adding symbols.  */
3750   struct ppc_link_hash_entry *dot_syms;
3751
3752   /* List of input sections for each output section.  */
3753   asection **input_list;
3754
3755   /* Short-cuts to get to dynamic linker sections.  */
3756   asection *got;
3757   asection *plt;
3758   asection *relplt;
3759   asection *iplt;
3760   asection *reliplt;
3761   asection *dynbss;
3762   asection *relbss;
3763   asection *glink;
3764   asection *sfpr;
3765   asection *brlt;
3766   asection *relbrlt;
3767
3768   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3769   struct ppc_link_hash_entry *tls_get_addr;
3770   struct ppc_link_hash_entry *tls_get_addr_fd;
3771
3772   /* The size of reliplt used by got entry relocs.  */
3773   bfd_size_type got_reli_size;
3774
3775   /* Statistics.  */
3776   unsigned long stub_count[ppc_stub_plt_call];
3777
3778   /* Number of stubs against global syms.  */
3779   unsigned long stub_globals;
3780
3781   /* Set if we should emit symbols for stubs.  */
3782   unsigned int emit_stub_syms:1;
3783
3784   /* Set if __tls_get_addr optimization should not be done.  */
3785   unsigned int no_tls_get_addr_opt:1;
3786
3787   /* Support for multiple toc sections.  */
3788   unsigned int do_multi_toc:1;
3789   unsigned int multi_toc_needed:1;
3790   unsigned int second_toc_pass:1;
3791   unsigned int do_toc_opt:1;
3792
3793   /* Set on error.  */
3794   unsigned int stub_error:1;
3795
3796   /* Temp used by ppc64_elf_process_dot_syms.  */
3797   unsigned int twiddled_syms:1;
3798
3799   /* Incremented every time we size stubs.  */
3800   unsigned int stub_iteration;
3801
3802   /* Small local sym cache.  */
3803   struct sym_cache sym_cache;
3804 };
3805
3806 /* Rename some of the generic section flags to better document how they
3807    are used here.  */
3808
3809 /* Nonzero if this section has TLS related relocations.  */
3810 #define has_tls_reloc sec_flg0
3811
3812 /* Nonzero if this section has a call to __tls_get_addr.  */
3813 #define has_tls_get_addr_call sec_flg1
3814
3815 /* Nonzero if this section has any toc or got relocs.  */
3816 #define has_toc_reloc sec_flg2
3817
3818 /* Nonzero if this section has a call to another section that uses
3819    the toc or got.  */
3820 #define makes_toc_func_call sec_flg3
3821
3822 /* Recursion protection when determining above flag.  */
3823 #define call_check_in_progress sec_flg4
3824 #define call_check_done sec_flg5
3825
3826 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
3827
3828 #define ppc_hash_table(p) \
3829   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3830   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3831
3832 #define ppc_stub_hash_lookup(table, string, create, copy) \
3833   ((struct ppc_stub_hash_entry *) \
3834    bfd_hash_lookup ((table), (string), (create), (copy)))
3835
3836 #define ppc_branch_hash_lookup(table, string, create, copy) \
3837   ((struct ppc_branch_hash_entry *) \
3838    bfd_hash_lookup ((table), (string), (create), (copy)))
3839
3840 /* Create an entry in the stub hash table.  */
3841
3842 static struct bfd_hash_entry *
3843 stub_hash_newfunc (struct bfd_hash_entry *entry,
3844                    struct bfd_hash_table *table,
3845                    const char *string)
3846 {
3847   /* Allocate the structure if it has not already been allocated by a
3848      subclass.  */
3849   if (entry == NULL)
3850     {
3851       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3852       if (entry == NULL)
3853         return entry;
3854     }
3855
3856   /* Call the allocation method of the superclass.  */
3857   entry = bfd_hash_newfunc (entry, table, string);
3858   if (entry != NULL)
3859     {
3860       struct ppc_stub_hash_entry *eh;
3861
3862       /* Initialize the local fields.  */
3863       eh = (struct ppc_stub_hash_entry *) entry;
3864       eh->stub_type = ppc_stub_none;
3865       eh->stub_sec = NULL;
3866       eh->stub_offset = 0;
3867       eh->target_value = 0;
3868       eh->target_section = NULL;
3869       eh->h = NULL;
3870       eh->id_sec = NULL;
3871     }
3872
3873   return entry;
3874 }
3875
3876 /* Create an entry in the branch hash table.  */
3877
3878 static struct bfd_hash_entry *
3879 branch_hash_newfunc (struct bfd_hash_entry *entry,
3880                      struct bfd_hash_table *table,
3881                      const char *string)
3882 {
3883   /* Allocate the structure if it has not already been allocated by a
3884      subclass.  */
3885   if (entry == NULL)
3886     {
3887       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3888       if (entry == NULL)
3889         return entry;
3890     }
3891
3892   /* Call the allocation method of the superclass.  */
3893   entry = bfd_hash_newfunc (entry, table, string);
3894   if (entry != NULL)
3895     {
3896       struct ppc_branch_hash_entry *eh;
3897
3898       /* Initialize the local fields.  */
3899       eh = (struct ppc_branch_hash_entry *) entry;
3900       eh->offset = 0;
3901       eh->iter = 0;
3902     }
3903
3904   return entry;
3905 }
3906
3907 /* Create an entry in a ppc64 ELF linker hash table.  */
3908
3909 static struct bfd_hash_entry *
3910 link_hash_newfunc (struct bfd_hash_entry *entry,
3911                    struct bfd_hash_table *table,
3912                    const char *string)
3913 {
3914   /* Allocate the structure if it has not already been allocated by a
3915      subclass.  */
3916   if (entry == NULL)
3917     {
3918       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3919       if (entry == NULL)
3920         return entry;
3921     }
3922
3923   /* Call the allocation method of the superclass.  */
3924   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3925   if (entry != NULL)
3926     {
3927       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3928
3929       memset (&eh->u.stub_cache, 0,
3930               (sizeof (struct ppc_link_hash_entry)
3931                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3932
3933       /* When making function calls, old ABI code references function entry
3934          points (dot symbols), while new ABI code references the function
3935          descriptor symbol.  We need to make any combination of reference and
3936          definition work together, without breaking archive linking.
3937
3938          For a defined function "foo" and an undefined call to "bar":
3939          An old object defines "foo" and ".foo", references ".bar" (possibly
3940          "bar" too).
3941          A new object defines "foo" and references "bar".
3942
3943          A new object thus has no problem with its undefined symbols being
3944          satisfied by definitions in an old object.  On the other hand, the
3945          old object won't have ".bar" satisfied by a new object.
3946
3947          Keep a list of newly added dot-symbols.  */
3948
3949       if (string[0] == '.')
3950         {
3951           struct ppc_link_hash_table *htab;
3952
3953           htab = (struct ppc_link_hash_table *) table;
3954           eh->u.next_dot_sym = htab->dot_syms;
3955           htab->dot_syms = eh;
3956         }
3957     }
3958
3959   return entry;
3960 }
3961
3962 /* Create a ppc64 ELF linker hash table.  */
3963
3964 static struct bfd_link_hash_table *
3965 ppc64_elf_link_hash_table_create (bfd *abfd)
3966 {
3967   struct ppc_link_hash_table *htab;
3968   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3969
3970   htab = bfd_zmalloc (amt);
3971   if (htab == NULL)
3972     return NULL;
3973
3974   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3975                                       sizeof (struct ppc_link_hash_entry),
3976                                       PPC64_ELF_DATA))
3977     {
3978       free (htab);
3979       return NULL;
3980     }
3981
3982   /* Init the stub hash table too.  */
3983   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3984                             sizeof (struct ppc_stub_hash_entry)))
3985     return NULL;
3986
3987   /* And the branch hash table.  */
3988   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3989                             sizeof (struct ppc_branch_hash_entry)))
3990     return NULL;
3991
3992   /* Initializing two fields of the union is just cosmetic.  We really
3993      only care about glist, but when compiled on a 32-bit host the
3994      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
3995      debugger inspection of these fields look nicer.  */
3996   htab->elf.init_got_refcount.refcount = 0;
3997   htab->elf.init_got_refcount.glist = NULL;
3998   htab->elf.init_plt_refcount.refcount = 0;
3999   htab->elf.init_plt_refcount.glist = NULL;
4000   htab->elf.init_got_offset.offset = 0;
4001   htab->elf.init_got_offset.glist = NULL;
4002   htab->elf.init_plt_offset.offset = 0;
4003   htab->elf.init_plt_offset.glist = NULL;
4004
4005   return &htab->elf.root;
4006 }
4007
4008 /* Free the derived linker hash table.  */
4009
4010 static void
4011 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
4012 {
4013   struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
4014
4015   bfd_hash_table_free (&ret->stub_hash_table);
4016   bfd_hash_table_free (&ret->branch_hash_table);
4017   _bfd_generic_link_hash_table_free (hash);
4018 }
4019
4020 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4021
4022 void
4023 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4024 {
4025   struct ppc_link_hash_table *htab;
4026
4027   elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4028
4029 /* Always hook our dynamic sections into the first bfd, which is the
4030    linker created stub bfd.  This ensures that the GOT header is at
4031    the start of the output TOC section.  */
4032   htab = ppc_hash_table (info);
4033   if (htab == NULL)
4034     return;
4035   htab->stub_bfd = abfd;
4036   htab->elf.dynobj = abfd;
4037 }
4038
4039 /* Build a name for an entry in the stub hash table.  */
4040
4041 static char *
4042 ppc_stub_name (const asection *input_section,
4043                const asection *sym_sec,
4044                const struct ppc_link_hash_entry *h,
4045                const Elf_Internal_Rela *rel)
4046 {
4047   char *stub_name;
4048   bfd_size_type len;
4049
4050   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4051      offsets from a sym as a branch target?  In fact, we could
4052      probably assume the addend is always zero.  */
4053   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4054
4055   if (h)
4056     {
4057       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4058       stub_name = bfd_malloc (len);
4059       if (stub_name == NULL)
4060         return stub_name;
4061
4062       sprintf (stub_name, "%08x.%s+%x",
4063                input_section->id & 0xffffffff,
4064                h->elf.root.root.string,
4065                (int) rel->r_addend & 0xffffffff);
4066     }
4067   else
4068     {
4069       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4070       stub_name = bfd_malloc (len);
4071       if (stub_name == NULL)
4072         return stub_name;
4073
4074       sprintf (stub_name, "%08x.%x:%x+%x",
4075                input_section->id & 0xffffffff,
4076                sym_sec->id & 0xffffffff,
4077                (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4078                (int) rel->r_addend & 0xffffffff);
4079     }
4080   if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4081     stub_name[len - 2] = 0;
4082   return stub_name;
4083 }
4084
4085 /* Look up an entry in the stub hash.  Stub entries are cached because
4086    creating the stub name takes a bit of time.  */
4087
4088 static struct ppc_stub_hash_entry *
4089 ppc_get_stub_entry (const asection *input_section,
4090                     const asection *sym_sec,
4091                     struct ppc_link_hash_entry *h,
4092                     const Elf_Internal_Rela *rel,
4093                     struct ppc_link_hash_table *htab)
4094 {
4095   struct ppc_stub_hash_entry *stub_entry;
4096   const asection *id_sec;
4097
4098   /* If this input section is part of a group of sections sharing one
4099      stub section, then use the id of the first section in the group.
4100      Stub names need to include a section id, as there may well be
4101      more than one stub used to reach say, printf, and we need to
4102      distinguish between them.  */
4103   id_sec = htab->stub_group[input_section->id].link_sec;
4104
4105   if (h != NULL && h->u.stub_cache != NULL
4106       && h->u.stub_cache->h == h
4107       && h->u.stub_cache->id_sec == id_sec)
4108     {
4109       stub_entry = h->u.stub_cache;
4110     }
4111   else
4112     {
4113       char *stub_name;
4114
4115       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4116       if (stub_name == NULL)
4117         return NULL;
4118
4119       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4120                                          stub_name, FALSE, FALSE);
4121       if (h != NULL)
4122         h->u.stub_cache = stub_entry;
4123
4124       free (stub_name);
4125     }
4126
4127   return stub_entry;
4128 }
4129
4130 /* Add a new stub entry to the stub hash.  Not all fields of the new
4131    stub entry are initialised.  */
4132
4133 static struct ppc_stub_hash_entry *
4134 ppc_add_stub (const char *stub_name,
4135               asection *section,
4136               struct ppc_link_hash_table *htab)
4137 {
4138   asection *link_sec;
4139   asection *stub_sec;
4140   struct ppc_stub_hash_entry *stub_entry;
4141
4142   link_sec = htab->stub_group[section->id].link_sec;
4143   stub_sec = htab->stub_group[section->id].stub_sec;
4144   if (stub_sec == NULL)
4145     {
4146       stub_sec = htab->stub_group[link_sec->id].stub_sec;
4147       if (stub_sec == NULL)
4148         {
4149           size_t namelen;
4150           bfd_size_type len;
4151           char *s_name;
4152
4153           namelen = strlen (link_sec->name);
4154           len = namelen + sizeof (STUB_SUFFIX);
4155           s_name = bfd_alloc (htab->stub_bfd, len);
4156           if (s_name == NULL)
4157             return NULL;
4158
4159           memcpy (s_name, link_sec->name, namelen);
4160           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4161           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4162           if (stub_sec == NULL)
4163             return NULL;
4164           htab->stub_group[link_sec->id].stub_sec = stub_sec;
4165         }
4166       htab->stub_group[section->id].stub_sec = stub_sec;
4167     }
4168
4169   /* Enter this entry into the linker stub hash table.  */
4170   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4171                                      TRUE, FALSE);
4172   if (stub_entry == NULL)
4173     {
4174       (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
4175                              section->owner, stub_name);
4176       return NULL;
4177     }
4178
4179   stub_entry->stub_sec = stub_sec;
4180   stub_entry->stub_offset = 0;
4181   stub_entry->id_sec = link_sec;
4182   return stub_entry;
4183 }
4184
4185 /* Create sections for linker generated code.  */
4186
4187 static bfd_boolean
4188 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4189 {
4190   struct ppc_link_hash_table *htab;
4191   flagword flags;
4192
4193   htab = ppc_hash_table (info);
4194   if (htab == NULL)
4195     return FALSE;
4196
4197   /* Create .sfpr for code to save and restore fp regs.  */
4198   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4199            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4200   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4201                                                    flags);
4202   if (htab->sfpr == NULL
4203       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4204     return FALSE;
4205
4206   /* Create .glink for lazy dynamic linking support.  */
4207   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4208                                                     flags);
4209   if (htab->glink == NULL
4210       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4211     return FALSE;
4212
4213   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4214   htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4215   if (htab->iplt == NULL
4216       || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4217     return FALSE;
4218
4219   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4220            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4221   htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4222                                                       ".rela.iplt",
4223                                                       flags);
4224   if (htab->reliplt == NULL
4225       || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4226     return FALSE;
4227
4228   /* Create branch lookup table for plt_branch stubs.  */
4229   flags = (SEC_ALLOC | SEC_LOAD
4230            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4231   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4232                                                    flags);
4233   if (htab->brlt == NULL
4234       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4235     return FALSE;
4236
4237   if (!info->shared)
4238     return TRUE;
4239
4240   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4241            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4242   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4243                                                       ".rela.branch_lt",
4244                                                       flags);
4245   if (htab->relbrlt == NULL
4246       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4247     return FALSE;
4248
4249   return TRUE;
4250 }
4251
4252 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4253    not already done.  */
4254
4255 static bfd_boolean
4256 create_got_section (bfd *abfd, struct bfd_link_info *info)
4257 {
4258   asection *got, *relgot;
4259   flagword flags;
4260   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4261
4262   if (!is_ppc64_elf (abfd))
4263     return FALSE;
4264   if (htab == NULL)
4265     return FALSE;
4266
4267   if (!htab->got)
4268     {
4269       if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4270         return FALSE;
4271
4272       htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4273       if (!htab->got)
4274         abort ();
4275     }
4276
4277   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4278            | SEC_LINKER_CREATED);
4279
4280   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4281   if (!got
4282       || !bfd_set_section_alignment (abfd, got, 3))
4283     return FALSE;
4284
4285   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4286                                                flags | SEC_READONLY);
4287   if (!relgot
4288       || ! bfd_set_section_alignment (abfd, relgot, 3))
4289     return FALSE;
4290
4291   ppc64_elf_tdata (abfd)->got = got;
4292   ppc64_elf_tdata (abfd)->relgot = relgot;
4293   return TRUE;
4294 }
4295
4296 /* Create the dynamic sections, and set up shortcuts.  */
4297
4298 static bfd_boolean
4299 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4300 {
4301   struct ppc_link_hash_table *htab;
4302
4303   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4304     return FALSE;
4305
4306   htab = ppc_hash_table (info);
4307   if (htab == NULL)
4308     return FALSE;
4309
4310   if (!htab->got)
4311     htab->got = bfd_get_section_by_name (dynobj, ".got");
4312   htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4313   htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4314   htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
4315   if (!info->shared)
4316     htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
4317
4318   if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4319       || (!info->shared && !htab->relbss))
4320     abort ();
4321
4322   return TRUE;
4323 }
4324
4325 /* Follow indirect and warning symbol links.  */
4326
4327 static inline struct bfd_link_hash_entry *
4328 follow_link (struct bfd_link_hash_entry *h)
4329 {
4330   while (h->type == bfd_link_hash_indirect
4331          || h->type == bfd_link_hash_warning)
4332     h = h->u.i.link;
4333   return h;
4334 }
4335
4336 static inline struct elf_link_hash_entry *
4337 elf_follow_link (struct elf_link_hash_entry *h)
4338 {
4339   return (struct elf_link_hash_entry *) follow_link (&h->root);
4340 }
4341
4342 static inline struct ppc_link_hash_entry *
4343 ppc_follow_link (struct ppc_link_hash_entry *h)
4344 {
4345   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4346 }
4347
4348 /* Merge PLT info on FROM with that on TO.  */
4349
4350 static void
4351 move_plt_plist (struct ppc_link_hash_entry *from,
4352                 struct ppc_link_hash_entry *to)
4353 {
4354   if (from->elf.plt.plist != NULL)
4355     {
4356       if (to->elf.plt.plist != NULL)
4357         {
4358           struct plt_entry **entp;
4359           struct plt_entry *ent;
4360
4361           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4362             {
4363               struct plt_entry *dent;
4364
4365               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4366                 if (dent->addend == ent->addend)
4367                   {
4368                     dent->plt.refcount += ent->plt.refcount;
4369                     *entp = ent->next;
4370                     break;
4371                   }
4372               if (dent == NULL)
4373                 entp = &ent->next;
4374             }
4375           *entp = to->elf.plt.plist;
4376         }
4377
4378       to->elf.plt.plist = from->elf.plt.plist;
4379       from->elf.plt.plist = NULL;
4380     }
4381 }
4382
4383 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4384
4385 static void
4386 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4387                                 struct elf_link_hash_entry *dir,
4388                                 struct elf_link_hash_entry *ind)
4389 {
4390   struct ppc_link_hash_entry *edir, *eind;
4391
4392   edir = (struct ppc_link_hash_entry *) dir;
4393   eind = (struct ppc_link_hash_entry *) ind;
4394
4395   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4396   if (eind->dyn_relocs != NULL)
4397     {
4398       if (edir->dyn_relocs != NULL)
4399         {
4400           struct ppc_dyn_relocs **pp;
4401           struct ppc_dyn_relocs *p;
4402
4403           /* Add reloc counts against the indirect sym to the direct sym
4404              list.  Merge any entries against the same section.  */
4405           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4406             {
4407               struct ppc_dyn_relocs *q;
4408
4409               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4410                 if (q->sec == p->sec)
4411                   {
4412                     q->pc_count += p->pc_count;
4413                     q->count += p->count;
4414                     *pp = p->next;
4415                     break;
4416                   }
4417               if (q == NULL)
4418                 pp = &p->next;
4419             }
4420           *pp = edir->dyn_relocs;
4421         }
4422
4423       edir->dyn_relocs = eind->dyn_relocs;
4424       eind->dyn_relocs = NULL;
4425     }
4426
4427   edir->is_func |= eind->is_func;
4428   edir->is_func_descriptor |= eind->is_func_descriptor;
4429   edir->tls_mask |= eind->tls_mask;
4430   if (eind->oh != NULL)
4431     edir->oh = ppc_follow_link (eind->oh);
4432
4433   /* If called to transfer flags for a weakdef during processing
4434      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4435      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
4436   if (!(ELIMINATE_COPY_RELOCS
4437         && eind->elf.root.type != bfd_link_hash_indirect
4438         && edir->elf.dynamic_adjusted))
4439     edir->elf.non_got_ref |= eind->elf.non_got_ref;
4440
4441   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4442   edir->elf.ref_regular |= eind->elf.ref_regular;
4443   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4444   edir->elf.needs_plt |= eind->elf.needs_plt;
4445
4446   /* If we were called to copy over info for a weak sym, that's all.  */
4447   if (eind->elf.root.type != bfd_link_hash_indirect)
4448     return;
4449
4450   /* Copy over got entries that we may have already seen to the
4451      symbol which just became indirect.  */
4452   if (eind->elf.got.glist != NULL)
4453     {
4454       if (edir->elf.got.glist != NULL)
4455         {
4456           struct got_entry **entp;
4457           struct got_entry *ent;
4458
4459           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4460             {
4461               struct got_entry *dent;
4462
4463               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4464                 if (dent->addend == ent->addend
4465                     && dent->owner == ent->owner
4466                     && dent->tls_type == ent->tls_type)
4467                   {
4468                     dent->got.refcount += ent->got.refcount;
4469                     *entp = ent->next;
4470                     break;
4471                   }
4472               if (dent == NULL)
4473                 entp = &ent->next;
4474             }
4475           *entp = edir->elf.got.glist;
4476         }
4477
4478       edir->elf.got.glist = eind->elf.got.glist;
4479       eind->elf.got.glist = NULL;
4480     }
4481
4482   /* And plt entries.  */
4483   move_plt_plist (eind, edir);
4484
4485   if (eind->elf.dynindx != -1)
4486     {
4487       if (edir->elf.dynindx != -1)
4488         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4489                                 edir->elf.dynstr_index);
4490       edir->elf.dynindx = eind->elf.dynindx;
4491       edir->elf.dynstr_index = eind->elf.dynstr_index;
4492       eind->elf.dynindx = -1;
4493       eind->elf.dynstr_index = 0;
4494     }
4495 }
4496
4497 /* Find the function descriptor hash entry from the given function code
4498    hash entry FH.  Link the entries via their OH fields.  */
4499
4500 static struct ppc_link_hash_entry *
4501 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4502 {
4503   struct ppc_link_hash_entry *fdh = fh->oh;
4504
4505   if (fdh == NULL)
4506     {
4507       const char *fd_name = fh->elf.root.root.string + 1;
4508
4509       fdh = (struct ppc_link_hash_entry *)
4510         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4511       if (fdh == NULL)
4512         return fdh;
4513
4514       fdh->is_func_descriptor = 1;
4515       fdh->oh = fh;
4516       fh->is_func = 1;
4517       fh->oh = fdh;
4518     }
4519
4520   return ppc_follow_link (fdh);
4521 }
4522
4523 /* Make a fake function descriptor sym for the code sym FH.  */
4524
4525 static struct ppc_link_hash_entry *
4526 make_fdh (struct bfd_link_info *info,
4527           struct ppc_link_hash_entry *fh)
4528 {
4529   bfd *abfd;
4530   asymbol *newsym;
4531   struct bfd_link_hash_entry *bh;
4532   struct ppc_link_hash_entry *fdh;
4533
4534   abfd = fh->elf.root.u.undef.abfd;
4535   newsym = bfd_make_empty_symbol (abfd);
4536   newsym->name = fh->elf.root.root.string + 1;
4537   newsym->section = bfd_und_section_ptr;
4538   newsym->value = 0;
4539   newsym->flags = BSF_WEAK;
4540
4541   bh = NULL;
4542   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4543                                          newsym->flags, newsym->section,
4544                                          newsym->value, NULL, FALSE, FALSE,
4545                                          &bh))
4546     return NULL;
4547
4548   fdh = (struct ppc_link_hash_entry *) bh;
4549   fdh->elf.non_elf = 0;
4550   fdh->fake = 1;
4551   fdh->is_func_descriptor = 1;
4552   fdh->oh = fh;
4553   fh->is_func = 1;
4554   fh->oh = fdh;
4555   return fdh;
4556 }
4557
4558 /* Fix function descriptor symbols defined in .opd sections to be
4559    function type.  */
4560
4561 static bfd_boolean
4562 ppc64_elf_add_symbol_hook (bfd *ibfd,
4563                            struct bfd_link_info *info,
4564                            Elf_Internal_Sym *isym,
4565                            const char **name ATTRIBUTE_UNUSED,
4566                            flagword *flags ATTRIBUTE_UNUSED,
4567                            asection **sec,
4568                            bfd_vma *value ATTRIBUTE_UNUSED)
4569 {
4570   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4571     {
4572       if ((ibfd->flags & DYNAMIC) == 0)
4573         elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4574     }
4575   else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4576     ;
4577   else if (*sec != NULL
4578            && strcmp ((*sec)->name, ".opd") == 0)
4579     isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4580
4581   return TRUE;
4582 }
4583
4584 /* This function makes an old ABI object reference to ".bar" cause the
4585    inclusion of a new ABI object archive that defines "bar".
4586    NAME is a symbol defined in an archive.  Return a symbol in the hash
4587    table that might be satisfied by the archive symbols.  */
4588
4589 static struct elf_link_hash_entry *
4590 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4591                                  struct bfd_link_info *info,
4592                                  const char *name)
4593 {
4594   struct elf_link_hash_entry *h;
4595   char *dot_name;
4596   size_t len;
4597
4598   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4599   if (h != NULL
4600       /* Don't return this sym if it is a fake function descriptor
4601          created by add_symbol_adjust.  */
4602       && !(h->root.type == bfd_link_hash_undefweak
4603            && ((struct ppc_link_hash_entry *) h)->fake))
4604     return h;
4605
4606   if (name[0] == '.')
4607     return h;
4608
4609   len = strlen (name);
4610   dot_name = bfd_alloc (abfd, len + 2);
4611   if (dot_name == NULL)
4612     return (struct elf_link_hash_entry *) 0 - 1;
4613   dot_name[0] = '.';
4614   memcpy (dot_name + 1, name, len + 1);
4615   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4616   bfd_release (abfd, dot_name);
4617   return h;
4618 }
4619
4620 /* This function satisfies all old ABI object references to ".bar" if a
4621    new ABI object defines "bar".  Well, at least, undefined dot symbols
4622    are made weak.  This stops later archive searches from including an
4623    object if we already have a function descriptor definition.  It also
4624    prevents the linker complaining about undefined symbols.
4625    We also check and correct mismatched symbol visibility here.  The
4626    most restrictive visibility of the function descriptor and the
4627    function entry symbol is used.  */
4628
4629 static bfd_boolean
4630 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4631 {
4632   struct ppc_link_hash_table *htab;
4633   struct ppc_link_hash_entry *fdh;
4634
4635   if (eh->elf.root.type == bfd_link_hash_indirect)
4636     return TRUE;
4637
4638   if (eh->elf.root.type == bfd_link_hash_warning)
4639     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4640
4641   if (eh->elf.root.root.string[0] != '.')
4642     abort ();
4643
4644   htab = ppc_hash_table (info);
4645   if (htab == NULL)
4646     return FALSE;
4647
4648   fdh = lookup_fdh (eh, htab);
4649   if (fdh == NULL)
4650     {
4651       if (!info->relocatable
4652           && (eh->elf.root.type == bfd_link_hash_undefined
4653               || eh->elf.root.type == bfd_link_hash_undefweak)
4654           && eh->elf.ref_regular)
4655         {
4656           /* Make an undefweak function descriptor sym, which is enough to
4657              pull in an --as-needed shared lib, but won't cause link
4658              errors.  Archives are handled elsewhere.  */
4659           fdh = make_fdh (info, eh);
4660           if (fdh == NULL)
4661             return FALSE;
4662           fdh->elf.ref_regular = 1;
4663         }
4664     }
4665   else
4666     {
4667       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4668       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4669       if (entry_vis < descr_vis)
4670         fdh->elf.other += entry_vis - descr_vis;
4671       else if (entry_vis > descr_vis)
4672         eh->elf.other += descr_vis - entry_vis;
4673
4674       if ((fdh->elf.root.type == bfd_link_hash_defined
4675            || fdh->elf.root.type == bfd_link_hash_defweak)
4676           && eh->elf.root.type == bfd_link_hash_undefined)
4677         {
4678           eh->elf.root.type = bfd_link_hash_undefweak;
4679           eh->was_undefined = 1;
4680           htab->twiddled_syms = 1;
4681         }
4682     }
4683
4684   return TRUE;
4685 }
4686
4687 /* Process list of dot-symbols we made in link_hash_newfunc.  */
4688
4689 static bfd_boolean
4690 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
4691 {
4692   struct ppc_link_hash_table *htab;
4693   struct ppc_link_hash_entry **p, *eh;
4694
4695   if (!is_ppc64_elf (info->output_bfd))
4696     return TRUE;
4697   htab = ppc_hash_table (info);
4698   if (htab == NULL)
4699     return FALSE;
4700
4701   if (is_ppc64_elf (ibfd))
4702     {
4703       p = &htab->dot_syms;
4704       while ((eh = *p) != NULL)
4705         {
4706           *p = NULL;
4707           if (!add_symbol_adjust (eh, info))
4708             return FALSE;
4709           p = &eh->u.next_dot_sym;
4710         }
4711     }
4712
4713   /* Clear the list for non-ppc64 input files.  */
4714   p = &htab->dot_syms;
4715   while ((eh = *p) != NULL)
4716     {
4717       *p = NULL;
4718       p = &eh->u.next_dot_sym;
4719     }
4720
4721   /* We need to fix the undefs list for any syms we have twiddled to
4722      undef_weak.  */
4723   if (htab->twiddled_syms)
4724     {
4725       bfd_link_repair_undef_list (&htab->elf.root);
4726       htab->twiddled_syms = 0;
4727     }
4728   return TRUE;
4729 }
4730
4731 /* Undo hash table changes when an --as-needed input file is determined
4732    not to be needed.  */
4733
4734 static bfd_boolean
4735 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4736                              struct bfd_link_info *info)
4737 {
4738   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4739
4740   if (htab == NULL)
4741     return FALSE;
4742
4743   htab->dot_syms = NULL;
4744   return TRUE;
4745 }
4746
4747 static struct plt_entry **
4748 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4749                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4750 {
4751   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4752   struct plt_entry **local_plt;
4753   unsigned char *local_got_tls_masks;
4754
4755   if (local_got_ents == NULL)
4756     {
4757       bfd_size_type size = symtab_hdr->sh_info;
4758
4759       size *= (sizeof (*local_got_ents)
4760                + sizeof (*local_plt)
4761                + sizeof (*local_got_tls_masks));
4762       local_got_ents = bfd_zalloc (abfd, size);
4763       if (local_got_ents == NULL)
4764         return NULL;
4765       elf_local_got_ents (abfd) = local_got_ents;
4766     }
4767
4768   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
4769     {
4770       struct got_entry *ent;
4771
4772       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4773         if (ent->addend == r_addend
4774             && ent->owner == abfd
4775             && ent->tls_type == tls_type)
4776           break;
4777       if (ent == NULL)
4778         {
4779           bfd_size_type amt = sizeof (*ent);
4780           ent = bfd_alloc (abfd, amt);
4781           if (ent == NULL)
4782             return FALSE;
4783           ent->next = local_got_ents[r_symndx];
4784           ent->addend = r_addend;
4785           ent->owner = abfd;
4786           ent->tls_type = tls_type;
4787           ent->is_indirect = FALSE;
4788           ent->got.refcount = 0;
4789           local_got_ents[r_symndx] = ent;
4790         }
4791       ent->got.refcount += 1;
4792     }
4793
4794   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4795   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4796   local_got_tls_masks[r_symndx] |= tls_type;
4797
4798   return local_plt + r_symndx;
4799 }
4800
4801 static bfd_boolean
4802 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4803 {
4804   struct plt_entry *ent;
4805
4806   for (ent = *plist; ent != NULL; ent = ent->next)
4807     if (ent->addend == addend)
4808       break;
4809   if (ent == NULL)
4810     {
4811       bfd_size_type amt = sizeof (*ent);
4812       ent = bfd_alloc (abfd, amt);
4813       if (ent == NULL)
4814         return FALSE;
4815       ent->next = *plist;
4816       ent->addend = addend;
4817       ent->plt.refcount = 0;
4818       *plist = ent;
4819     }
4820   ent->plt.refcount += 1;
4821   return TRUE;
4822 }
4823
4824 static bfd_boolean
4825 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4826 {
4827   return (r_type == R_PPC64_REL24
4828           || r_type == R_PPC64_REL14
4829           || r_type == R_PPC64_REL14_BRTAKEN
4830           || r_type == R_PPC64_REL14_BRNTAKEN
4831           || r_type == R_PPC64_ADDR24
4832           || r_type == R_PPC64_ADDR14
4833           || r_type == R_PPC64_ADDR14_BRTAKEN
4834           || r_type == R_PPC64_ADDR14_BRNTAKEN);
4835 }
4836
4837 /* Look through the relocs for a section during the first phase, and
4838    calculate needed space in the global offset table, procedure
4839    linkage table, and dynamic reloc sections.  */
4840
4841 static bfd_boolean
4842 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4843                         asection *sec, const Elf_Internal_Rela *relocs)
4844 {
4845   struct ppc_link_hash_table *htab;
4846   Elf_Internal_Shdr *symtab_hdr;
4847   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4848   const Elf_Internal_Rela *rel;
4849   const Elf_Internal_Rela *rel_end;
4850   asection *sreloc;
4851   asection **opd_sym_map;
4852   struct elf_link_hash_entry *tga, *dottga;
4853
4854   if (info->relocatable)
4855     return TRUE;
4856
4857   /* Don't do anything special with non-loaded, non-alloced sections.
4858      In particular, any relocs in such sections should not affect GOT
4859      and PLT reference counting (ie. we don't allow them to create GOT
4860      or PLT entries), there's no possibility or desire to optimize TLS
4861      relocs, and there's not much point in propagating relocs to shared
4862      libs that the dynamic linker won't relocate.  */
4863   if ((sec->flags & SEC_ALLOC) == 0)
4864     return TRUE;
4865
4866   BFD_ASSERT (is_ppc64_elf (abfd));
4867
4868   htab = ppc_hash_table (info);
4869   if (htab == NULL)
4870     return FALSE;
4871
4872   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4873                               FALSE, FALSE, TRUE);
4874   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4875                                  FALSE, FALSE, TRUE);
4876   symtab_hdr = &elf_symtab_hdr (abfd);
4877
4878   sym_hashes = elf_sym_hashes (abfd);
4879   sym_hashes_end = (sym_hashes
4880                     + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
4881                     - symtab_hdr->sh_info);
4882
4883   sreloc = NULL;
4884   opd_sym_map = NULL;
4885   if (strcmp (sec->name, ".opd") == 0)
4886     {
4887       /* Garbage collection needs some extra help with .opd sections.
4888          We don't want to necessarily keep everything referenced by
4889          relocs in .opd, as that would keep all functions.  Instead,
4890          if we reference an .opd symbol (a function descriptor), we
4891          want to keep the function code symbol's section.  This is
4892          easy for global symbols, but for local syms we need to keep
4893          information about the associated function section.  */
4894       bfd_size_type amt;
4895
4896       amt = sec->size * sizeof (*opd_sym_map) / 8;
4897       opd_sym_map = bfd_zalloc (abfd, amt);
4898       if (opd_sym_map == NULL)
4899         return FALSE;
4900       ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
4901       BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4902       ppc64_elf_section_data (sec)->sec_type = sec_opd;
4903     }
4904
4905   if (htab->sfpr == NULL
4906       && !create_linkage_sections (htab->elf.dynobj, info))
4907     return FALSE;
4908
4909   rel_end = relocs + sec->reloc_count;
4910   for (rel = relocs; rel < rel_end; rel++)
4911     {
4912       unsigned long r_symndx;
4913       struct elf_link_hash_entry *h;
4914       enum elf_ppc64_reloc_type r_type;
4915       int tls_type;
4916       struct _ppc64_elf_section_data *ppc64_sec;
4917       struct plt_entry **ifunc;
4918
4919       r_symndx = ELF64_R_SYM (rel->r_info);
4920       if (r_symndx < symtab_hdr->sh_info)
4921         h = NULL;
4922       else
4923         {
4924           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4925           h = elf_follow_link (h);
4926         }
4927
4928       tls_type = 0;
4929       ifunc = NULL;
4930       if (h != NULL)
4931         {
4932           if (h->type == STT_GNU_IFUNC)
4933             {
4934               h->needs_plt = 1;
4935               ifunc = &h->plt.plist;
4936             }
4937         }
4938       else
4939         {
4940           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4941                                                           abfd, r_symndx);
4942           if (isym == NULL)
4943             return FALSE;
4944
4945           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4946             {
4947               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4948                                              rel->r_addend, PLT_IFUNC);
4949               if (ifunc == NULL)
4950                 return FALSE;
4951             }
4952         }
4953       r_type = ELF64_R_TYPE (rel->r_info);
4954       if (is_branch_reloc (r_type))
4955         {
4956           if (h != NULL && (h == tga || h == dottga))
4957             {
4958               if (rel != relocs
4959                   && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4960                       || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4961                 /* We have a new-style __tls_get_addr call with a marker
4962                    reloc.  */
4963                 ;
4964               else
4965                 /* Mark this section as having an old-style call.  */
4966                 sec->has_tls_get_addr_call = 1;
4967             }
4968
4969           /* STT_GNU_IFUNC symbols must have a PLT entry.  */
4970           if (ifunc != NULL
4971               && !update_plt_info (abfd, ifunc, rel->r_addend))
4972             return FALSE;
4973         }
4974
4975       switch (r_type)
4976         {
4977         case R_PPC64_TLSGD:
4978         case R_PPC64_TLSLD:
4979           /* These special tls relocs tie a call to __tls_get_addr with
4980              its parameter symbol.  */
4981           break;
4982
4983         case R_PPC64_GOT_TLSLD16:
4984         case R_PPC64_GOT_TLSLD16_LO:
4985         case R_PPC64_GOT_TLSLD16_HI:
4986         case R_PPC64_GOT_TLSLD16_HA:
4987           tls_type = TLS_TLS | TLS_LD;
4988           goto dogottls;
4989
4990         case R_PPC64_GOT_TLSGD16:
4991         case R_PPC64_GOT_TLSGD16_LO:
4992         case R_PPC64_GOT_TLSGD16_HI:
4993         case R_PPC64_GOT_TLSGD16_HA:
4994           tls_type = TLS_TLS | TLS_GD;
4995           goto dogottls;
4996
4997         case R_PPC64_GOT_TPREL16_DS:
4998         case R_PPC64_GOT_TPREL16_LO_DS:
4999         case R_PPC64_GOT_TPREL16_HI:
5000         case R_PPC64_GOT_TPREL16_HA:
5001           if (!info->executable)
5002             info->flags |= DF_STATIC_TLS;
5003           tls_type = TLS_TLS | TLS_TPREL;
5004           goto dogottls;
5005
5006         case R_PPC64_GOT_DTPREL16_DS:
5007         case R_PPC64_GOT_DTPREL16_LO_DS:
5008         case R_PPC64_GOT_DTPREL16_HI:
5009         case R_PPC64_GOT_DTPREL16_HA:
5010           tls_type = TLS_TLS | TLS_DTPREL;
5011         dogottls:
5012           sec->has_tls_reloc = 1;
5013           /* Fall thru */
5014
5015         case R_PPC64_GOT16:
5016         case R_PPC64_GOT16_DS:
5017         case R_PPC64_GOT16_HA:
5018         case R_PPC64_GOT16_HI:
5019         case R_PPC64_GOT16_LO:
5020         case R_PPC64_GOT16_LO_DS:
5021           /* This symbol requires a global offset table entry.  */
5022           sec->has_toc_reloc = 1;
5023           if (r_type == R_PPC64_GOT_TLSLD16
5024               || r_type == R_PPC64_GOT_TLSGD16
5025               || r_type == R_PPC64_GOT_TPREL16_DS
5026               || r_type == R_PPC64_GOT_DTPREL16_DS
5027               || r_type == R_PPC64_GOT16
5028               || r_type == R_PPC64_GOT16_DS)
5029             {
5030               htab->do_multi_toc = 1;
5031               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5032             }
5033
5034           if (ppc64_elf_tdata (abfd)->got == NULL
5035               && !create_got_section (abfd, info))
5036             return FALSE;
5037
5038           if (h != NULL)
5039             {
5040               struct ppc_link_hash_entry *eh;
5041               struct got_entry *ent;
5042
5043               eh = (struct ppc_link_hash_entry *) h;
5044               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5045                 if (ent->addend == rel->r_addend
5046                     && ent->owner == abfd
5047                     && ent->tls_type == tls_type)
5048                   break;
5049               if (ent == NULL)
5050                 {
5051                   bfd_size_type amt = sizeof (*ent);
5052                   ent = bfd_alloc (abfd, amt);
5053                   if (ent == NULL)
5054                     return FALSE;
5055                   ent->next = eh->elf.got.glist;
5056                   ent->addend = rel->r_addend;
5057                   ent->owner = abfd;
5058                   ent->tls_type = tls_type;
5059                   ent->is_indirect = FALSE;
5060                   ent->got.refcount = 0;
5061                   eh->elf.got.glist = ent;
5062                 }
5063               ent->got.refcount += 1;
5064               eh->tls_mask |= tls_type;
5065             }
5066           else
5067             /* This is a global offset table entry for a local symbol.  */
5068             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5069                                         rel->r_addend, tls_type))
5070               return FALSE;
5071           break;
5072
5073         case R_PPC64_PLT16_HA:
5074         case R_PPC64_PLT16_HI:
5075         case R_PPC64_PLT16_LO:
5076         case R_PPC64_PLT32:
5077         case R_PPC64_PLT64:
5078           /* This symbol requires a procedure linkage table entry.  We
5079              actually build the entry in adjust_dynamic_symbol,
5080              because this might be a case of linking PIC code without
5081              linking in any dynamic objects, in which case we don't
5082              need to generate a procedure linkage table after all.  */
5083           if (h == NULL)
5084             {
5085               /* It does not make sense to have a procedure linkage
5086                  table entry for a local symbol.  */
5087               bfd_set_error (bfd_error_bad_value);
5088               return FALSE;
5089             }
5090           else
5091             {
5092               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5093                 return FALSE;
5094               h->needs_plt = 1;
5095               if (h->root.root.string[0] == '.'
5096                   && h->root.root.string[1] != '\0')
5097                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5098             }
5099           break;
5100
5101           /* The following relocations don't need to propagate the
5102              relocation if linking a shared object since they are
5103              section relative.  */
5104         case R_PPC64_SECTOFF:
5105         case R_PPC64_SECTOFF_LO:
5106         case R_PPC64_SECTOFF_HI:
5107         case R_PPC64_SECTOFF_HA:
5108         case R_PPC64_SECTOFF_DS:
5109         case R_PPC64_SECTOFF_LO_DS:
5110         case R_PPC64_DTPREL16:
5111         case R_PPC64_DTPREL16_LO:
5112         case R_PPC64_DTPREL16_HI:
5113         case R_PPC64_DTPREL16_HA:
5114         case R_PPC64_DTPREL16_DS:
5115         case R_PPC64_DTPREL16_LO_DS:
5116         case R_PPC64_DTPREL16_HIGHER:
5117         case R_PPC64_DTPREL16_HIGHERA:
5118         case R_PPC64_DTPREL16_HIGHEST:
5119         case R_PPC64_DTPREL16_HIGHESTA:
5120           break;
5121
5122           /* Nor do these.  */
5123         case R_PPC64_REL16:
5124         case R_PPC64_REL16_LO:
5125         case R_PPC64_REL16_HI:
5126         case R_PPC64_REL16_HA:
5127           break;
5128
5129         case R_PPC64_TOC16:
5130         case R_PPC64_TOC16_DS:
5131           htab->do_multi_toc = 1;
5132           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5133         case R_PPC64_TOC16_LO:
5134         case R_PPC64_TOC16_HI:
5135         case R_PPC64_TOC16_HA:
5136         case R_PPC64_TOC16_LO_DS:
5137           sec->has_toc_reloc = 1;
5138           break;
5139
5140           /* This relocation describes the C++ object vtable hierarchy.
5141              Reconstruct it for later use during GC.  */
5142         case R_PPC64_GNU_VTINHERIT:
5143           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5144             return FALSE;
5145           break;
5146
5147           /* This relocation describes which C++ vtable entries are actually
5148              used.  Record for later use during GC.  */
5149         case R_PPC64_GNU_VTENTRY:
5150           BFD_ASSERT (h != NULL);
5151           if (h != NULL
5152               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5153             return FALSE;
5154           break;
5155
5156         case R_PPC64_REL14:
5157         case R_PPC64_REL14_BRTAKEN:
5158         case R_PPC64_REL14_BRNTAKEN:
5159           {
5160             asection *dest = NULL;
5161
5162             /* Heuristic: If jumping outside our section, chances are
5163                we are going to need a stub.  */
5164             if (h != NULL)
5165               {
5166                 /* If the sym is weak it may be overridden later, so
5167                    don't assume we know where a weak sym lives.  */
5168                 if (h->root.type == bfd_link_hash_defined)
5169                   dest = h->root.u.def.section;
5170               }
5171             else
5172               {
5173                 Elf_Internal_Sym *isym;
5174
5175                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5176                                               abfd, r_symndx);
5177                 if (isym == NULL)
5178                   return FALSE;
5179
5180                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5181               }
5182
5183             if (dest != sec)
5184               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5185           }
5186           /* Fall through.  */
5187
5188         case R_PPC64_REL24:
5189           if (h != NULL && ifunc == NULL)
5190             {
5191               /* We may need a .plt entry if the function this reloc
5192                  refers to is in a shared lib.  */
5193               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5194                 return FALSE;
5195               h->needs_plt = 1;
5196               if (h->root.root.string[0] == '.'
5197                   && h->root.root.string[1] != '\0')
5198                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5199               if (h == tga || h == dottga)
5200                 sec->has_tls_reloc = 1;
5201             }
5202           break;
5203
5204         case R_PPC64_TPREL64:
5205           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5206           if (!info->executable)
5207             info->flags |= DF_STATIC_TLS;
5208           goto dotlstoc;
5209
5210         case R_PPC64_DTPMOD64:
5211           if (rel + 1 < rel_end
5212               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5213               && rel[1].r_offset == rel->r_offset + 8)
5214             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5215           else
5216             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5217           goto dotlstoc;
5218
5219         case R_PPC64_DTPREL64:
5220           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5221           if (rel != relocs
5222               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5223               && rel[-1].r_offset == rel->r_offset - 8)
5224             /* This is the second reloc of a dtpmod, dtprel pair.
5225                Don't mark with TLS_DTPREL.  */
5226             goto dodyn;
5227
5228         dotlstoc:
5229           sec->has_tls_reloc = 1;
5230           if (h != NULL)
5231             {
5232               struct ppc_link_hash_entry *eh;
5233               eh = (struct ppc_link_hash_entry *) h;
5234               eh->tls_mask |= tls_type;
5235             }
5236           else
5237             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5238                                         rel->r_addend, tls_type))
5239               return FALSE;
5240
5241           ppc64_sec = ppc64_elf_section_data (sec);
5242           if (ppc64_sec->sec_type != sec_toc)
5243             {
5244               bfd_size_type amt;
5245
5246               /* One extra to simplify get_tls_mask.  */
5247               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5248               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5249               if (ppc64_sec->u.toc.symndx == NULL)
5250                 return FALSE;
5251               amt = sec->size * sizeof (bfd_vma) / 8;
5252               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5253               if (ppc64_sec->u.toc.add == NULL)
5254                 return FALSE;
5255               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5256               ppc64_sec->sec_type = sec_toc;
5257             }
5258           BFD_ASSERT (rel->r_offset % 8 == 0);
5259           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5260           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5261
5262           /* Mark the second slot of a GD or LD entry.
5263              -1 to indicate GD and -2 to indicate LD.  */
5264           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5265             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5266           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5267             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5268           goto dodyn;
5269
5270         case R_PPC64_TPREL16:
5271         case R_PPC64_TPREL16_LO:
5272         case R_PPC64_TPREL16_HI:
5273         case R_PPC64_TPREL16_HA:
5274         case R_PPC64_TPREL16_DS:
5275         case R_PPC64_TPREL16_LO_DS:
5276         case R_PPC64_TPREL16_HIGHER:
5277         case R_PPC64_TPREL16_HIGHERA:
5278         case R_PPC64_TPREL16_HIGHEST:
5279         case R_PPC64_TPREL16_HIGHESTA:
5280           if (info->shared)
5281             {
5282               if (!info->executable)
5283                 info->flags |= DF_STATIC_TLS;
5284               goto dodyn;
5285             }
5286           break;
5287
5288         case R_PPC64_ADDR64:
5289           if (opd_sym_map != NULL
5290               && rel + 1 < rel_end
5291               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5292             {
5293               if (h != NULL)
5294                 {
5295                   if (h->root.root.string[0] == '.'
5296                       && h->root.root.string[1] != 0
5297                       && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5298                     ;
5299                   else
5300                     ((struct ppc_link_hash_entry *) h)->is_func = 1;
5301                 }
5302               else
5303                 {
5304                   asection *s;
5305                   Elf_Internal_Sym *isym;
5306
5307                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5308                                                 abfd, r_symndx);
5309                   if (isym == NULL)
5310                     return FALSE;
5311
5312                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5313                   if (s != NULL && s != sec)
5314                     opd_sym_map[rel->r_offset / 8] = s;
5315                 }
5316             }
5317           /* Fall through.  */
5318
5319         case R_PPC64_REL30:
5320         case R_PPC64_REL32:
5321         case R_PPC64_REL64:
5322         case R_PPC64_ADDR14:
5323         case R_PPC64_ADDR14_BRNTAKEN:
5324         case R_PPC64_ADDR14_BRTAKEN:
5325         case R_PPC64_ADDR16:
5326         case R_PPC64_ADDR16_DS:
5327         case R_PPC64_ADDR16_HA:
5328         case R_PPC64_ADDR16_HI:
5329         case R_PPC64_ADDR16_HIGHER:
5330         case R_PPC64_ADDR16_HIGHERA:
5331         case R_PPC64_ADDR16_HIGHEST:
5332         case R_PPC64_ADDR16_HIGHESTA:
5333         case R_PPC64_ADDR16_LO:
5334         case R_PPC64_ADDR16_LO_DS:
5335         case R_PPC64_ADDR24:
5336         case R_PPC64_ADDR32:
5337         case R_PPC64_UADDR16:
5338         case R_PPC64_UADDR32:
5339         case R_PPC64_UADDR64:
5340         case R_PPC64_TOC:
5341           if (h != NULL && !info->shared)
5342             /* We may need a copy reloc.  */
5343             h->non_got_ref = 1;
5344
5345           /* Don't propagate .opd relocs.  */
5346           if (NO_OPD_RELOCS && opd_sym_map != NULL)
5347             break;
5348
5349           /* If we are creating a shared library, and this is a reloc
5350              against a global symbol, or a non PC relative reloc
5351              against a local symbol, then we need to copy the reloc
5352              into the shared library.  However, if we are linking with
5353              -Bsymbolic, we do not need to copy a reloc against a
5354              global symbol which is defined in an object we are
5355              including in the link (i.e., DEF_REGULAR is set).  At
5356              this point we have not seen all the input files, so it is
5357              possible that DEF_REGULAR is not set now but will be set
5358              later (it is never cleared).  In case of a weak definition,
5359              DEF_REGULAR may be cleared later by a strong definition in
5360              a shared library.  We account for that possibility below by
5361              storing information in the dyn_relocs field of the hash
5362              table entry.  A similar situation occurs when creating
5363              shared libraries and symbol visibility changes render the
5364              symbol local.
5365
5366              If on the other hand, we are creating an executable, we
5367              may need to keep relocations for symbols satisfied by a
5368              dynamic library if we manage to avoid copy relocs for the
5369              symbol.  */
5370         dodyn:
5371           if ((info->shared
5372                && (must_be_dyn_reloc (info, r_type)
5373                    || (h != NULL
5374                        && (! info->symbolic
5375                            || h->root.type == bfd_link_hash_defweak
5376                            || !h->def_regular))))
5377               || (ELIMINATE_COPY_RELOCS
5378                   && !info->shared
5379                   && h != NULL
5380                   && (h->root.type == bfd_link_hash_defweak
5381                       || !h->def_regular))
5382               || (!info->shared
5383                   && ifunc != NULL))
5384             {
5385               struct ppc_dyn_relocs *p;
5386               struct ppc_dyn_relocs **head;
5387
5388               /* We must copy these reloc types into the output file.
5389                  Create a reloc section in dynobj and make room for
5390                  this reloc.  */
5391               if (sreloc == NULL)
5392                 {
5393                   sreloc = _bfd_elf_make_dynamic_reloc_section
5394                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5395
5396                   if (sreloc == NULL)
5397                     return FALSE;
5398                 }
5399
5400               /* If this is a global symbol, we count the number of
5401                  relocations we need for this symbol.  */
5402               if (h != NULL)
5403                 {
5404                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5405                 }
5406               else
5407                 {
5408                   /* Track dynamic relocs needed for local syms too.
5409                      We really need local syms available to do this
5410                      easily.  Oh well.  */
5411                   asection *s;
5412                   void *vpp;
5413                   Elf_Internal_Sym *isym;
5414
5415                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5416                                                 abfd, r_symndx);
5417                   if (isym == NULL)
5418                     return FALSE;
5419
5420                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5421                   if (s == NULL)
5422                     s = sec;
5423
5424                   vpp = &elf_section_data (s)->local_dynrel;
5425                   head = (struct ppc_dyn_relocs **) vpp;
5426                 }
5427
5428               p = *head;
5429               if (p == NULL || p->sec != sec)
5430                 {
5431                   p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5432                   if (p == NULL)
5433                     return FALSE;
5434                   p->next = *head;
5435                   *head = p;
5436                   p->sec = sec;
5437                   p->count = 0;
5438                   p->pc_count = 0;
5439                 }
5440
5441               p->count += 1;
5442               if (!must_be_dyn_reloc (info, r_type))
5443                 p->pc_count += 1;
5444             }
5445           break;
5446
5447         default:
5448           break;
5449         }
5450     }
5451
5452   return TRUE;
5453 }
5454
5455 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
5456    of the code entry point, and its section.  */
5457
5458 static bfd_vma
5459 opd_entry_value (asection *opd_sec,
5460                  bfd_vma offset,
5461                  asection **code_sec,
5462                  bfd_vma *code_off)
5463 {
5464   bfd *opd_bfd = opd_sec->owner;
5465   Elf_Internal_Rela *relocs;
5466   Elf_Internal_Rela *lo, *hi, *look;
5467   bfd_vma val;
5468
5469   /* No relocs implies we are linking a --just-symbols object.  */
5470   if (opd_sec->reloc_count == 0)
5471     {
5472       if (!bfd_get_section_contents (opd_bfd, opd_sec, &val, offset, 8))
5473         return (bfd_vma) -1;
5474
5475       if (code_sec != NULL)
5476         {
5477           asection *sec, *likely = NULL;
5478           for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5479             if (sec->vma <= val
5480                 && (sec->flags & SEC_LOAD) != 0
5481                 && (sec->flags & SEC_ALLOC) != 0)
5482               likely = sec;
5483           if (likely != NULL)
5484             {
5485               *code_sec = likely;
5486               if (code_off != NULL)
5487                 *code_off = val - likely->vma;
5488             }
5489         }
5490       return val;
5491     }
5492
5493   BFD_ASSERT (is_ppc64_elf (opd_bfd));
5494
5495   relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5496   if (relocs == NULL)
5497     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5498
5499   /* Go find the opd reloc at the sym address.  */
5500   lo = relocs;
5501   BFD_ASSERT (lo != NULL);
5502   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5503   val = (bfd_vma) -1;
5504   while (lo < hi)
5505     {
5506       look = lo + (hi - lo) / 2;
5507       if (look->r_offset < offset)
5508         lo = look + 1;
5509       else if (look->r_offset > offset)
5510         hi = look;
5511       else
5512         {
5513           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5514
5515           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5516               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5517             {
5518               unsigned long symndx = ELF64_R_SYM (look->r_info);
5519               asection *sec;
5520
5521               if (symndx < symtab_hdr->sh_info)
5522                 {
5523                   Elf_Internal_Sym *sym;
5524
5525                   sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5526                   if (sym == NULL)
5527                     {
5528                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5529                                                   symtab_hdr->sh_info,
5530                                                   0, NULL, NULL, NULL);
5531                       if (sym == NULL)
5532                         break;
5533                       symtab_hdr->contents = (bfd_byte *) sym;
5534                     }
5535
5536                   sym += symndx;
5537                   val = sym->st_value;
5538                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5539                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5540                 }
5541               else
5542                 {
5543                   struct elf_link_hash_entry **sym_hashes;
5544                   struct elf_link_hash_entry *rh;
5545
5546                   sym_hashes = elf_sym_hashes (opd_bfd);
5547                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
5548                   rh = elf_follow_link (rh);
5549                   BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5550                               || rh->root.type == bfd_link_hash_defweak);
5551                   val = rh->root.u.def.value;
5552                   sec = rh->root.u.def.section;
5553                 }
5554               val += look->r_addend;
5555               if (code_off != NULL)
5556                 *code_off = val;
5557               if (code_sec != NULL)
5558                 *code_sec = sec;
5559               if (sec != NULL && sec->output_section != NULL)
5560                 val += sec->output_section->vma + sec->output_offset;
5561             }
5562           break;
5563         }
5564     }
5565
5566   return val;
5567 }
5568
5569 /* Return true if symbol is defined in a regular object file.  */
5570
5571 static bfd_boolean
5572 is_static_defined (struct elf_link_hash_entry *h)
5573 {
5574   return ((h->root.type == bfd_link_hash_defined
5575            || h->root.type == bfd_link_hash_defweak)
5576           && h->root.u.def.section != NULL
5577           && h->root.u.def.section->output_section != NULL);
5578 }
5579
5580 /* If FDH is a function descriptor symbol, return the associated code
5581    entry symbol if it is defined.  Return NULL otherwise.  */
5582
5583 static struct ppc_link_hash_entry *
5584 defined_code_entry (struct ppc_link_hash_entry *fdh)
5585 {
5586   if (fdh->is_func_descriptor)
5587     {
5588       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5589       if (fh->elf.root.type == bfd_link_hash_defined
5590           || fh->elf.root.type == bfd_link_hash_defweak)
5591         return fh;
5592     }
5593   return NULL;
5594 }
5595
5596 /* If FH is a function code entry symbol, return the associated
5597    function descriptor symbol if it is defined.  Return NULL otherwise.  */
5598
5599 static struct ppc_link_hash_entry *
5600 defined_func_desc (struct ppc_link_hash_entry *fh)
5601 {
5602   if (fh->oh != NULL
5603       && fh->oh->is_func_descriptor)
5604     {
5605       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5606       if (fdh->elf.root.type == bfd_link_hash_defined
5607           || fdh->elf.root.type == bfd_link_hash_defweak)
5608         return fdh;
5609     }
5610   return NULL;
5611 }
5612
5613 /* Mark all our entry sym sections, both opd and code section.  */
5614
5615 static void
5616 ppc64_elf_gc_keep (struct bfd_link_info *info)
5617 {
5618   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5619   struct bfd_sym_chain *sym;
5620
5621   if (htab == NULL)
5622     return;
5623
5624   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5625     {
5626       struct ppc_link_hash_entry *eh, *fh;
5627       asection *sec;
5628
5629       eh = (struct ppc_link_hash_entry *)
5630         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5631       if (eh == NULL)
5632         continue;
5633       if (eh->elf.root.type != bfd_link_hash_defined
5634           && eh->elf.root.type != bfd_link_hash_defweak)
5635         continue;
5636
5637       fh = defined_code_entry (eh);
5638       if (fh != NULL)
5639         {
5640           sec = fh->elf.root.u.def.section;
5641           sec->flags |= SEC_KEEP;
5642         }
5643       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5644                && opd_entry_value (eh->elf.root.u.def.section,
5645                                    eh->elf.root.u.def.value,
5646                                    &sec, NULL) != (bfd_vma) -1)
5647         sec->flags |= SEC_KEEP;
5648
5649       sec = eh->elf.root.u.def.section;
5650       sec->flags |= SEC_KEEP;
5651     }
5652 }
5653
5654 /* Mark sections containing dynamically referenced symbols.  When
5655    building shared libraries, we must assume that any visible symbol is
5656    referenced.  */
5657
5658 static bfd_boolean
5659 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5660 {
5661   struct bfd_link_info *info = (struct bfd_link_info *) inf;
5662   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5663   struct ppc_link_hash_entry *fdh;
5664
5665   if (eh->elf.root.type == bfd_link_hash_warning)
5666     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5667
5668   /* Dynamic linking info is on the func descriptor sym.  */
5669   fdh = defined_func_desc (eh);
5670   if (fdh != NULL)
5671     eh = fdh;
5672
5673   if ((eh->elf.root.type == bfd_link_hash_defined
5674        || eh->elf.root.type == bfd_link_hash_defweak)
5675       && (eh->elf.ref_dynamic
5676           || (!info->executable
5677               && eh->elf.def_regular
5678               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5679               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
5680     {
5681       asection *code_sec;
5682       struct ppc_link_hash_entry *fh;
5683
5684       eh->elf.root.u.def.section->flags |= SEC_KEEP;
5685
5686       /* Function descriptor syms cause the associated
5687          function code sym section to be marked.  */
5688       fh = defined_code_entry (eh);
5689       if (fh != NULL)
5690         {
5691           code_sec = fh->elf.root.u.def.section;
5692           code_sec->flags |= SEC_KEEP;
5693         }
5694       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5695                && opd_entry_value (eh->elf.root.u.def.section,
5696                                    eh->elf.root.u.def.value,
5697                                    &code_sec, NULL) != (bfd_vma) -1)
5698         code_sec->flags |= SEC_KEEP;
5699     }
5700
5701   return TRUE;
5702 }
5703
5704 /* Return the section that should be marked against GC for a given
5705    relocation.  */
5706
5707 static asection *
5708 ppc64_elf_gc_mark_hook (asection *sec,
5709                         struct bfd_link_info *info,
5710                         Elf_Internal_Rela *rel,
5711                         struct elf_link_hash_entry *h,
5712                         Elf_Internal_Sym *sym)
5713 {
5714   asection *rsec;
5715
5716   /* Syms return NULL if we're marking .opd, so we avoid marking all
5717      function sections, as all functions are referenced in .opd.  */
5718   rsec = NULL;
5719   if (get_opd_info (sec) != NULL)
5720     return rsec;
5721
5722   if (h != NULL)
5723     {
5724       enum elf_ppc64_reloc_type r_type;
5725       struct ppc_link_hash_entry *eh, *fh, *fdh;
5726
5727       r_type = ELF64_R_TYPE (rel->r_info);
5728       switch (r_type)
5729         {
5730         case R_PPC64_GNU_VTINHERIT:
5731         case R_PPC64_GNU_VTENTRY:
5732           break;
5733
5734         default:
5735           switch (h->root.type)
5736             {
5737             case bfd_link_hash_defined:
5738             case bfd_link_hash_defweak:
5739               eh = (struct ppc_link_hash_entry *) h;
5740               fdh = defined_func_desc (eh);
5741               if (fdh != NULL)
5742                 eh = fdh;
5743
5744               /* Function descriptor syms cause the associated
5745                  function code sym section to be marked.  */
5746               fh = defined_code_entry (eh);
5747               if (fh != NULL)
5748                 {
5749                   /* They also mark their opd section.  */
5750                   eh->elf.root.u.def.section->gc_mark = 1;
5751
5752                   rsec = fh->elf.root.u.def.section;
5753                 }
5754               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5755                        && opd_entry_value (eh->elf.root.u.def.section,
5756                                            eh->elf.root.u.def.value,
5757                                            &rsec, NULL) != (bfd_vma) -1)
5758                 eh->elf.root.u.def.section->gc_mark = 1;
5759               else
5760                 rsec = h->root.u.def.section;
5761               break;
5762
5763             case bfd_link_hash_common:
5764               rsec = h->root.u.c.p->section;
5765               break;
5766
5767             default:
5768               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5769             }
5770         }
5771     }
5772   else
5773     {
5774       struct _opd_sec_data *opd;
5775
5776       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5777       opd = get_opd_info (rsec);
5778       if (opd != NULL && opd->func_sec != NULL)
5779         {
5780           rsec->gc_mark = 1;
5781
5782           rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
5783         }
5784     }
5785
5786   return rsec;
5787 }
5788
5789 /* Update the .got, .plt. and dynamic reloc reference counts for the
5790    section being removed.  */
5791
5792 static bfd_boolean
5793 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5794                          asection *sec, const Elf_Internal_Rela *relocs)
5795 {
5796   struct ppc_link_hash_table *htab;
5797   Elf_Internal_Shdr *symtab_hdr;
5798   struct elf_link_hash_entry **sym_hashes;
5799   struct got_entry **local_got_ents;
5800   const Elf_Internal_Rela *rel, *relend;
5801
5802   if (info->relocatable)
5803     return TRUE;
5804
5805   if ((sec->flags & SEC_ALLOC) == 0)
5806     return TRUE;
5807
5808   elf_section_data (sec)->local_dynrel = NULL;
5809
5810   htab = ppc_hash_table (info);
5811   if (htab == NULL)
5812     return FALSE;
5813
5814   symtab_hdr = &elf_symtab_hdr (abfd);
5815   sym_hashes = elf_sym_hashes (abfd);
5816   local_got_ents = elf_local_got_ents (abfd);
5817
5818   relend = relocs + sec->reloc_count;
5819   for (rel = relocs; rel < relend; rel++)
5820     {
5821       unsigned long r_symndx;
5822       enum elf_ppc64_reloc_type r_type;
5823       struct elf_link_hash_entry *h = NULL;
5824       unsigned char tls_type = 0;
5825
5826       r_symndx = ELF64_R_SYM (rel->r_info);
5827       r_type = ELF64_R_TYPE (rel->r_info);
5828       if (r_symndx >= symtab_hdr->sh_info)
5829         {
5830           struct ppc_link_hash_entry *eh;
5831           struct ppc_dyn_relocs **pp;
5832           struct ppc_dyn_relocs *p;
5833
5834           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5835           h = elf_follow_link (h);
5836           eh = (struct ppc_link_hash_entry *) h;
5837
5838           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5839             if (p->sec == sec)
5840               {
5841                 /* Everything must go for SEC.  */
5842                 *pp = p->next;
5843                 break;
5844               }
5845         }
5846
5847       if (is_branch_reloc (r_type))
5848         {
5849           struct plt_entry **ifunc = NULL;
5850           if (h != NULL)
5851             {
5852               if (h->type == STT_GNU_IFUNC)
5853                 ifunc = &h->plt.plist;
5854             }
5855           else if (local_got_ents != NULL)
5856             {
5857               struct plt_entry **local_plt = (struct plt_entry **)
5858                 (local_got_ents + symtab_hdr->sh_info);
5859               unsigned char *local_got_tls_masks = (unsigned char *)
5860                 (local_plt + symtab_hdr->sh_info);
5861               if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5862                 ifunc = local_plt + r_symndx;
5863             }
5864           if (ifunc != NULL)
5865             {
5866               struct plt_entry *ent;
5867
5868               for (ent = *ifunc; ent != NULL; ent = ent->next)
5869                 if (ent->addend == rel->r_addend)
5870                   break;
5871               if (ent == NULL)
5872                 abort ();
5873               if (ent->plt.refcount > 0)
5874                 ent->plt.refcount -= 1;
5875               continue;
5876             }
5877         }
5878
5879       switch (r_type)
5880         {
5881         case R_PPC64_GOT_TLSLD16:
5882         case R_PPC64_GOT_TLSLD16_LO:
5883         case R_PPC64_GOT_TLSLD16_HI:
5884         case R_PPC64_GOT_TLSLD16_HA:
5885           tls_type = TLS_TLS | TLS_LD;
5886           goto dogot;
5887
5888         case R_PPC64_GOT_TLSGD16:
5889         case R_PPC64_GOT_TLSGD16_LO:
5890         case R_PPC64_GOT_TLSGD16_HI:
5891         case R_PPC64_GOT_TLSGD16_HA:
5892           tls_type = TLS_TLS | TLS_GD;
5893           goto dogot;
5894
5895         case R_PPC64_GOT_TPREL16_DS:
5896         case R_PPC64_GOT_TPREL16_LO_DS:
5897         case R_PPC64_GOT_TPREL16_HI:
5898         case R_PPC64_GOT_TPREL16_HA:
5899           tls_type = TLS_TLS | TLS_TPREL;
5900           goto dogot;
5901
5902         case R_PPC64_GOT_DTPREL16_DS:
5903         case R_PPC64_GOT_DTPREL16_LO_DS:
5904         case R_PPC64_GOT_DTPREL16_HI:
5905         case R_PPC64_GOT_DTPREL16_HA:
5906           tls_type = TLS_TLS | TLS_DTPREL;
5907           goto dogot;
5908
5909         case R_PPC64_GOT16:
5910         case R_PPC64_GOT16_DS:
5911         case R_PPC64_GOT16_HA:
5912         case R_PPC64_GOT16_HI:
5913         case R_PPC64_GOT16_LO:
5914         case R_PPC64_GOT16_LO_DS:
5915         dogot:
5916           {
5917             struct got_entry *ent;
5918
5919             if (h != NULL)
5920               ent = h->got.glist;
5921             else
5922               ent = local_got_ents[r_symndx];
5923
5924             for (; ent != NULL; ent = ent->next)
5925               if (ent->addend == rel->r_addend
5926                   && ent->owner == abfd
5927                   && ent->tls_type == tls_type)
5928                 break;
5929             if (ent == NULL)
5930               abort ();
5931             if (ent->got.refcount > 0)
5932               ent->got.refcount -= 1;
5933           }
5934           break;
5935
5936         case R_PPC64_PLT16_HA:
5937         case R_PPC64_PLT16_HI:
5938         case R_PPC64_PLT16_LO:
5939         case R_PPC64_PLT32:
5940         case R_PPC64_PLT64:
5941         case R_PPC64_REL14:
5942         case R_PPC64_REL14_BRNTAKEN:
5943         case R_PPC64_REL14_BRTAKEN:
5944         case R_PPC64_REL24:
5945           if (h != NULL)
5946             {
5947               struct plt_entry *ent;
5948
5949               for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5950                 if (ent->addend == rel->r_addend)
5951                   break;
5952               if (ent != NULL && ent->plt.refcount > 0)
5953                 ent->plt.refcount -= 1;
5954             }
5955           break;
5956
5957         default:
5958           break;
5959         }
5960     }
5961   return TRUE;
5962 }
5963
5964 /* The maximum size of .sfpr.  */
5965 #define SFPR_MAX (218*4)
5966
5967 struct sfpr_def_parms
5968 {
5969   const char name[12];
5970   unsigned char lo, hi;
5971   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5972   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5973 };
5974
5975 /* Auto-generate _save*, _rest* functions in .sfpr.  */
5976
5977 static bfd_boolean
5978 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5979 {
5980   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5981   unsigned int i;
5982   size_t len = strlen (parm->name);
5983   bfd_boolean writing = FALSE;
5984   char sym[16];
5985
5986   if (htab == NULL)
5987     return FALSE;
5988
5989   memcpy (sym, parm->name, len);
5990   sym[len + 2] = 0;
5991
5992   for (i = parm->lo; i <= parm->hi; i++)
5993     {
5994       struct elf_link_hash_entry *h;
5995
5996       sym[len + 0] = i / 10 + '0';
5997       sym[len + 1] = i % 10 + '0';
5998       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
5999       if (h != NULL
6000           && !h->def_regular)
6001         {
6002           h->root.type = bfd_link_hash_defined;
6003           h->root.u.def.section = htab->sfpr;
6004           h->root.u.def.value = htab->sfpr->size;
6005           h->type = STT_FUNC;
6006           h->def_regular = 1;
6007           _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6008           writing = TRUE;
6009           if (htab->sfpr->contents == NULL)
6010             {
6011               htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6012               if (htab->sfpr->contents == NULL)
6013                 return FALSE;
6014             }
6015         }
6016       if (writing)
6017         {
6018           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6019           if (i != parm->hi)
6020             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6021           else
6022             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6023           htab->sfpr->size = p - htab->sfpr->contents;
6024         }
6025     }
6026
6027   return TRUE;
6028 }
6029
6030 static bfd_byte *
6031 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6032 {
6033   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6034   return p + 4;
6035 }
6036
6037 static bfd_byte *
6038 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6039 {
6040   p = savegpr0 (abfd, p, r);
6041   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6042   p = p + 4;
6043   bfd_put_32 (abfd, BLR, p);
6044   return p + 4;
6045 }
6046
6047 static bfd_byte *
6048 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6049 {
6050   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6051   return p + 4;
6052 }
6053
6054 static bfd_byte *
6055 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6056 {
6057   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6058   p = p + 4;
6059   p = restgpr0 (abfd, p, r);
6060   bfd_put_32 (abfd, MTLR_R0, p);
6061   p = p + 4;
6062   if (r == 29)
6063     {
6064       p = restgpr0 (abfd, p, 30);
6065       p = restgpr0 (abfd, p, 31);
6066     }
6067   bfd_put_32 (abfd, BLR, p);
6068   return p + 4;
6069 }
6070
6071 static bfd_byte *
6072 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6073 {
6074   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6075   return p + 4;
6076 }
6077
6078 static bfd_byte *
6079 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6080 {
6081   p = savegpr1 (abfd, p, r);
6082   bfd_put_32 (abfd, BLR, p);
6083   return p + 4;
6084 }
6085
6086 static bfd_byte *
6087 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6088 {
6089   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6090   return p + 4;
6091 }
6092
6093 static bfd_byte *
6094 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6095 {
6096   p = restgpr1 (abfd, p, r);
6097   bfd_put_32 (abfd, BLR, p);
6098   return p + 4;
6099 }
6100
6101 static bfd_byte *
6102 savefpr (bfd *abfd, bfd_byte *p, int r)
6103 {
6104   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6105   return p + 4;
6106 }
6107
6108 static bfd_byte *
6109 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6110 {
6111   p = savefpr (abfd, p, r);
6112   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6113   p = p + 4;
6114   bfd_put_32 (abfd, BLR, p);
6115   return p + 4;
6116 }
6117
6118 static bfd_byte *
6119 restfpr (bfd *abfd, bfd_byte *p, int r)
6120 {
6121   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6122   return p + 4;
6123 }
6124
6125 static bfd_byte *
6126 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6127 {
6128   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6129   p = p + 4;
6130   p = restfpr (abfd, p, r);
6131   bfd_put_32 (abfd, MTLR_R0, p);
6132   p = p + 4;
6133   if (r == 29)
6134     {
6135       p = restfpr (abfd, p, 30);
6136       p = restfpr (abfd, p, 31);
6137     }
6138   bfd_put_32 (abfd, BLR, p);
6139   return p + 4;
6140 }
6141
6142 static bfd_byte *
6143 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6144 {
6145   p = savefpr (abfd, p, r);
6146   bfd_put_32 (abfd, BLR, p);
6147   return p + 4;
6148 }
6149
6150 static bfd_byte *
6151 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6152 {
6153   p = restfpr (abfd, p, r);
6154   bfd_put_32 (abfd, BLR, p);
6155   return p + 4;
6156 }
6157
6158 static bfd_byte *
6159 savevr (bfd *abfd, bfd_byte *p, int r)
6160 {
6161   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6162   p = p + 4;
6163   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6164   return p + 4;
6165 }
6166
6167 static bfd_byte *
6168 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6169 {
6170   p = savevr (abfd, p, r);
6171   bfd_put_32 (abfd, BLR, p);
6172   return p + 4;
6173 }
6174
6175 static bfd_byte *
6176 restvr (bfd *abfd, bfd_byte *p, int r)
6177 {
6178   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6179   p = p + 4;
6180   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6181   return p + 4;
6182 }
6183
6184 static bfd_byte *
6185 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6186 {
6187   p = restvr (abfd, p, r);
6188   bfd_put_32 (abfd, BLR, p);
6189   return p + 4;
6190 }
6191
6192 /* Called via elf_link_hash_traverse to transfer dynamic linking
6193    information on function code symbol entries to their corresponding
6194    function descriptor symbol entries.  */
6195
6196 static bfd_boolean
6197 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6198 {
6199   struct bfd_link_info *info;
6200   struct ppc_link_hash_table *htab;
6201   struct plt_entry *ent;
6202   struct ppc_link_hash_entry *fh;
6203   struct ppc_link_hash_entry *fdh;
6204   bfd_boolean force_local;
6205
6206   fh = (struct ppc_link_hash_entry *) h;
6207   if (fh->elf.root.type == bfd_link_hash_indirect)
6208     return TRUE;
6209
6210   if (fh->elf.root.type == bfd_link_hash_warning)
6211     fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
6212
6213   info = inf;
6214   htab = ppc_hash_table (info);
6215   if (htab == NULL)
6216     return FALSE;
6217
6218   /* Resolve undefined references to dot-symbols as the value
6219      in the function descriptor, if we have one in a regular object.
6220      This is to satisfy cases like ".quad .foo".  Calls to functions
6221      in dynamic objects are handled elsewhere.  */
6222   if (fh->elf.root.type == bfd_link_hash_undefweak
6223       && fh->was_undefined
6224       && (fdh = defined_func_desc (fh)) != NULL
6225       && get_opd_info (fdh->elf.root.u.def.section) != NULL
6226       && opd_entry_value (fdh->elf.root.u.def.section,
6227                           fdh->elf.root.u.def.value,
6228                           &fh->elf.root.u.def.section,
6229                           &fh->elf.root.u.def.value) != (bfd_vma) -1)
6230     {
6231       fh->elf.root.type = fdh->elf.root.type;
6232       fh->elf.forced_local = 1;
6233       fh->elf.def_regular = fdh->elf.def_regular;
6234       fh->elf.def_dynamic = fdh->elf.def_dynamic;
6235     }
6236
6237   /* If this is a function code symbol, transfer dynamic linking
6238      information to the function descriptor symbol.  */
6239   if (!fh->is_func)
6240     return TRUE;
6241
6242   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6243     if (ent->plt.refcount > 0)
6244       break;
6245   if (ent == NULL
6246       || fh->elf.root.root.string[0] != '.'
6247       || fh->elf.root.root.string[1] == '\0')
6248     return TRUE;
6249
6250   /* Find the corresponding function descriptor symbol.  Create it
6251      as undefined if necessary.  */
6252
6253   fdh = lookup_fdh (fh, htab);
6254   if (fdh == NULL
6255       && !info->executable
6256       && (fh->elf.root.type == bfd_link_hash_undefined
6257           || fh->elf.root.type == bfd_link_hash_undefweak))
6258     {
6259       fdh = make_fdh (info, fh);
6260       if (fdh == NULL)
6261         return FALSE;
6262     }
6263
6264   /* Fake function descriptors are made undefweak.  If the function
6265      code symbol is strong undefined, make the fake sym the same.
6266      If the function code symbol is defined, then force the fake
6267      descriptor local;  We can't support overriding of symbols in a
6268      shared library on a fake descriptor.  */
6269
6270   if (fdh != NULL
6271       && fdh->fake
6272       && fdh->elf.root.type == bfd_link_hash_undefweak)
6273     {
6274       if (fh->elf.root.type == bfd_link_hash_undefined)
6275         {
6276           fdh->elf.root.type = bfd_link_hash_undefined;
6277           bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6278         }
6279       else if (fh->elf.root.type == bfd_link_hash_defined
6280                || fh->elf.root.type == bfd_link_hash_defweak)
6281         {
6282           _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6283         }
6284     }
6285
6286   if (fdh != NULL
6287       && !fdh->elf.forced_local
6288       && (!info->executable
6289           || fdh->elf.def_dynamic
6290           || fdh->elf.ref_dynamic
6291           || (fdh->elf.root.type == bfd_link_hash_undefweak
6292               && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6293     {
6294       if (fdh->elf.dynindx == -1)
6295         if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6296           return FALSE;
6297       fdh->elf.ref_regular |= fh->elf.ref_regular;
6298       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6299       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6300       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6301       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6302         {
6303           move_plt_plist (fh, fdh);
6304           fdh->elf.needs_plt = 1;
6305         }
6306       fdh->is_func_descriptor = 1;
6307       fdh->oh = fh;
6308       fh->oh = fdh;
6309     }
6310
6311   /* Now that the info is on the function descriptor, clear the
6312      function code sym info.  Any function code syms for which we
6313      don't have a definition in a regular file, we force local.
6314      This prevents a shared library from exporting syms that have
6315      been imported from another library.  Function code syms that
6316      are really in the library we must leave global to prevent the
6317      linker dragging in a definition from a static library.  */
6318   force_local = (!fh->elf.def_regular
6319                  || fdh == NULL
6320                  || !fdh->elf.def_regular
6321                  || fdh->elf.forced_local);
6322   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6323
6324   return TRUE;
6325 }
6326
6327 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
6328    this hook to a) provide some gcc support functions, and b) transfer
6329    dynamic linking information gathered so far on function code symbol
6330    entries, to their corresponding function descriptor symbol entries.  */
6331
6332 static bfd_boolean
6333 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6334                             struct bfd_link_info *info)
6335 {
6336   struct ppc_link_hash_table *htab;
6337   unsigned int i;
6338   const struct sfpr_def_parms funcs[] =
6339     {
6340       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6341       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6342       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6343       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6344       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6345       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6346       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6347       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6348       { "._savef", 14, 31, savefpr, savefpr1_tail },
6349       { "._restf", 14, 31, restfpr, restfpr1_tail },
6350       { "_savevr_", 20, 31, savevr, savevr_tail },
6351       { "_restvr_", 20, 31, restvr, restvr_tail }
6352     };
6353
6354   htab = ppc_hash_table (info);
6355   if (htab == NULL)
6356     return FALSE;
6357
6358   if (htab->sfpr == NULL)
6359     /* We don't have any relocs.  */
6360     return TRUE;
6361
6362   /* Provide any missing _save* and _rest* functions.  */
6363   htab->sfpr->size = 0;
6364   for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6365     if (!sfpr_define (info, &funcs[i]))
6366       return FALSE;
6367
6368   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6369
6370   if (htab->sfpr->size == 0)
6371     htab->sfpr->flags |= SEC_EXCLUDE;
6372
6373   return TRUE;
6374 }
6375
6376 /* Adjust a symbol defined by a dynamic object and referenced by a
6377    regular object.  The current definition is in some section of the
6378    dynamic object, but we're not including those sections.  We have to
6379    change the definition to something the rest of the link can
6380    understand.  */
6381
6382 static bfd_boolean
6383 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6384                                  struct elf_link_hash_entry *h)
6385 {
6386   struct ppc_link_hash_table *htab;
6387   asection *s;
6388
6389   htab = ppc_hash_table (info);
6390   if (htab == NULL)
6391     return FALSE;
6392
6393   /* Deal with function syms.  */
6394   if (h->type == STT_FUNC
6395       || h->type == STT_GNU_IFUNC
6396       || h->needs_plt)
6397     {
6398       /* Clear procedure linkage table information for any symbol that
6399          won't need a .plt entry.  */
6400       struct plt_entry *ent;
6401       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6402         if (ent->plt.refcount > 0)
6403           break;
6404       if (ent == NULL
6405           || (h->type != STT_GNU_IFUNC
6406               && (SYMBOL_CALLS_LOCAL (info, h)
6407                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6408                       && h->root.type == bfd_link_hash_undefweak))))
6409         {
6410           h->plt.plist = NULL;
6411           h->needs_plt = 0;
6412         }
6413     }
6414   else
6415     h->plt.plist = NULL;
6416
6417   /* If this is a weak symbol, and there is a real definition, the
6418      processor independent code will have arranged for us to see the
6419      real definition first, and we can just use the same value.  */
6420   if (h->u.weakdef != NULL)
6421     {
6422       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6423                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
6424       h->root.u.def.section = h->u.weakdef->root.u.def.section;
6425       h->root.u.def.value = h->u.weakdef->root.u.def.value;
6426       if (ELIMINATE_COPY_RELOCS)
6427         h->non_got_ref = h->u.weakdef->non_got_ref;
6428       return TRUE;
6429     }
6430
6431   /* If we are creating a shared library, we must presume that the
6432      only references to the symbol are via the global offset table.
6433      For such cases we need not do anything here; the relocations will
6434      be handled correctly by relocate_section.  */
6435   if (info->shared)
6436     return TRUE;
6437
6438   /* If there are no references to this symbol that do not use the
6439      GOT, we don't need to generate a copy reloc.  */
6440   if (!h->non_got_ref)
6441     return TRUE;
6442
6443   /* Don't generate a copy reloc for symbols defined in the executable.  */
6444   if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6445     return TRUE;
6446
6447   if (ELIMINATE_COPY_RELOCS)
6448     {
6449       struct ppc_link_hash_entry * eh;
6450       struct ppc_dyn_relocs *p;
6451
6452       eh = (struct ppc_link_hash_entry *) h;
6453       for (p = eh->dyn_relocs; p != NULL; p = p->next)
6454         {
6455           s = p->sec->output_section;
6456           if (s != NULL && (s->flags & SEC_READONLY) != 0)
6457             break;
6458         }
6459
6460       /* If we didn't find any dynamic relocs in read-only sections, then
6461          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
6462       if (p == NULL)
6463         {
6464           h->non_got_ref = 0;
6465           return TRUE;
6466         }
6467     }
6468
6469   if (h->plt.plist != NULL)
6470     {
6471       /* We should never get here, but unfortunately there are versions
6472          of gcc out there that improperly (for this ABI) put initialized
6473          function pointers, vtable refs and suchlike in read-only
6474          sections.  Allow them to proceed, but warn that this might
6475          break at runtime.  */
6476       (*_bfd_error_handler)
6477         (_("copy reloc against `%s' requires lazy plt linking; "
6478            "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
6479          h->root.root.string);
6480     }
6481
6482   /* This is a reference to a symbol defined by a dynamic object which
6483      is not a function.  */
6484
6485   if (h->size == 0)
6486     {
6487       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
6488                              h->root.root.string);
6489       return TRUE;
6490     }
6491
6492   /* We must allocate the symbol in our .dynbss section, which will
6493      become part of the .bss section of the executable.  There will be
6494      an entry for this symbol in the .dynsym section.  The dynamic
6495      object will contain position independent code, so all references
6496      from the dynamic object to this symbol will go through the global
6497      offset table.  The dynamic linker will use the .dynsym entry to
6498      determine the address it must put in the global offset table, so
6499      both the dynamic object and the regular object will refer to the
6500      same memory location for the variable.  */
6501
6502   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6503      to copy the initial value out of the dynamic object and into the
6504      runtime process image.  We need to remember the offset into the
6505      .rela.bss section we are going to use.  */
6506   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6507     {
6508       htab->relbss->size += sizeof (Elf64_External_Rela);
6509       h->needs_copy = 1;
6510     }
6511
6512   s = htab->dynbss;
6513
6514   return _bfd_elf_adjust_dynamic_copy (h, s);
6515 }
6516
6517 /* If given a function descriptor symbol, hide both the function code
6518    sym and the descriptor.  */
6519 static void
6520 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6521                        struct elf_link_hash_entry *h,
6522                        bfd_boolean force_local)
6523 {
6524   struct ppc_link_hash_entry *eh;
6525   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6526
6527   eh = (struct ppc_link_hash_entry *) h;
6528   if (eh->is_func_descriptor)
6529     {
6530       struct ppc_link_hash_entry *fh = eh->oh;
6531
6532       if (fh == NULL)
6533         {
6534           const char *p, *q;
6535           struct ppc_link_hash_table *htab;
6536           char save;
6537
6538           /* We aren't supposed to use alloca in BFD because on
6539              systems which do not have alloca the version in libiberty
6540              calls xmalloc, which might cause the program to crash
6541              when it runs out of memory.  This function doesn't have a
6542              return status, so there's no way to gracefully return an
6543              error.  So cheat.  We know that string[-1] can be safely
6544              accessed;  It's either a string in an ELF string table,
6545              or allocated in an objalloc structure.  */
6546
6547           p = eh->elf.root.root.string - 1;
6548           save = *p;
6549           *(char *) p = '.';
6550           htab = ppc_hash_table (info);
6551           if (htab == NULL)
6552             return;
6553
6554           fh = (struct ppc_link_hash_entry *)
6555             elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6556           *(char *) p = save;
6557
6558           /* Unfortunately, if it so happens that the string we were
6559              looking for was allocated immediately before this string,
6560              then we overwrote the string terminator.  That's the only
6561              reason the lookup should fail.  */
6562           if (fh == NULL)
6563             {
6564               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6565               while (q >= eh->elf.root.root.string && *q == *p)
6566                 --q, --p;
6567               if (q < eh->elf.root.root.string && *p == '.')
6568                 fh = (struct ppc_link_hash_entry *)
6569                   elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6570             }
6571           if (fh != NULL)
6572             {
6573               eh->oh = fh;
6574               fh->oh = eh;
6575             }
6576         }
6577       if (fh != NULL)
6578         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6579     }
6580 }
6581
6582 static bfd_boolean
6583 get_sym_h (struct elf_link_hash_entry **hp,
6584            Elf_Internal_Sym **symp,
6585            asection **symsecp,
6586            unsigned char **tls_maskp,
6587            Elf_Internal_Sym **locsymsp,
6588            unsigned long r_symndx,
6589            bfd *ibfd)
6590 {
6591   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6592
6593   if (r_symndx >= symtab_hdr->sh_info)
6594     {
6595       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6596       struct elf_link_hash_entry *h;
6597
6598       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6599       h = elf_follow_link (h);
6600
6601       if (hp != NULL)
6602         *hp = h;
6603
6604       if (symp != NULL)
6605         *symp = NULL;
6606
6607       if (symsecp != NULL)
6608         {
6609           asection *symsec = NULL;
6610           if (h->root.type == bfd_link_hash_defined
6611               || h->root.type == bfd_link_hash_defweak)
6612             symsec = h->root.u.def.section;
6613           *symsecp = symsec;
6614         }
6615
6616       if (tls_maskp != NULL)
6617         {
6618           struct ppc_link_hash_entry *eh;
6619
6620           eh = (struct ppc_link_hash_entry *) h;
6621           *tls_maskp = &eh->tls_mask;
6622         }
6623     }
6624   else
6625     {
6626       Elf_Internal_Sym *sym;
6627       Elf_Internal_Sym *locsyms = *locsymsp;
6628
6629       if (locsyms == NULL)
6630         {
6631           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6632           if (locsyms == NULL)
6633             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6634                                             symtab_hdr->sh_info,
6635                                             0, NULL, NULL, NULL);
6636           if (locsyms == NULL)
6637             return FALSE;
6638           *locsymsp = locsyms;
6639         }
6640       sym = locsyms + r_symndx;
6641
6642       if (hp != NULL)
6643         *hp = NULL;
6644
6645       if (symp != NULL)
6646         *symp = sym;
6647
6648       if (symsecp != NULL)
6649         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6650
6651       if (tls_maskp != NULL)
6652         {
6653           struct got_entry **lgot_ents;
6654           unsigned char *tls_mask;
6655
6656           tls_mask = NULL;
6657           lgot_ents = elf_local_got_ents (ibfd);
6658           if (lgot_ents != NULL)
6659             {
6660               struct plt_entry **local_plt = (struct plt_entry **)
6661                 (lgot_ents + symtab_hdr->sh_info);
6662               unsigned char *lgot_masks = (unsigned char *)
6663                 (local_plt + symtab_hdr->sh_info);
6664               tls_mask = &lgot_masks[r_symndx];
6665             }
6666           *tls_maskp = tls_mask;
6667         }
6668     }
6669   return TRUE;
6670 }
6671
6672 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
6673    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6674    type suitable for optimization, and 1 otherwise.  */
6675
6676 static int
6677 get_tls_mask (unsigned char **tls_maskp,
6678               unsigned long *toc_symndx,
6679               bfd_vma *toc_addend,
6680               Elf_Internal_Sym **locsymsp,
6681               const Elf_Internal_Rela *rel,
6682               bfd *ibfd)
6683 {
6684   unsigned long r_symndx;
6685   int next_r;
6686   struct elf_link_hash_entry *h;
6687   Elf_Internal_Sym *sym;
6688   asection *sec;
6689   bfd_vma off;
6690
6691   r_symndx = ELF64_R_SYM (rel->r_info);
6692   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6693     return 0;
6694
6695   if ((*tls_maskp != NULL && **tls_maskp != 0)
6696       || sec == NULL
6697       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6698     return 1;
6699
6700   /* Look inside a TOC section too.  */
6701   if (h != NULL)
6702     {
6703       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6704       off = h->root.u.def.value;
6705     }
6706   else
6707     off = sym->st_value;
6708   off += rel->r_addend;
6709   BFD_ASSERT (off % 8 == 0);
6710   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6711   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6712   if (toc_symndx != NULL)
6713     *toc_symndx = r_symndx;
6714   if (toc_addend != NULL)
6715     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6716   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6717     return 0;
6718   if ((h == NULL || is_static_defined (h))
6719       && (next_r == -1 || next_r == -2))
6720     return 1 - next_r;
6721   return 1;
6722 }
6723
6724 /* Adjust all global syms defined in opd sections.  In gcc generated
6725    code for the old ABI, these will already have been done.  */
6726
6727 static bfd_boolean
6728 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6729 {
6730   struct ppc_link_hash_entry *eh;
6731   asection *sym_sec;
6732   struct _opd_sec_data *opd;
6733
6734   if (h->root.type == bfd_link_hash_indirect)
6735     return TRUE;
6736
6737   if (h->root.type == bfd_link_hash_warning)
6738     h = (struct elf_link_hash_entry *) h->root.u.i.link;
6739
6740   if (h->root.type != bfd_link_hash_defined
6741       && h->root.type != bfd_link_hash_defweak)
6742     return TRUE;
6743
6744   eh = (struct ppc_link_hash_entry *) h;
6745   if (eh->adjust_done)
6746     return TRUE;
6747
6748   sym_sec = eh->elf.root.u.def.section;
6749   opd = get_opd_info (sym_sec);
6750   if (opd != NULL && opd->adjust != NULL)
6751     {
6752       long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
6753       if (adjust == -1)
6754         {
6755           /* This entry has been deleted.  */
6756           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6757           if (dsec == NULL)
6758             {
6759               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6760                 if (elf_discarded_section (dsec))
6761                   {
6762                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6763                     break;
6764                   }
6765             }
6766           eh->elf.root.u.def.value = 0;
6767           eh->elf.root.u.def.section = dsec;
6768         }
6769       else
6770         eh->elf.root.u.def.value += adjust;
6771       eh->adjust_done = 1;
6772     }
6773   return TRUE;
6774 }
6775
6776 /* Handles decrementing dynamic reloc counts for the reloc specified by
6777    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM_SEC
6778    have already been determined.  */
6779
6780 static bfd_boolean
6781 dec_dynrel_count (bfd_vma r_info,
6782                   asection *sec,
6783                   struct bfd_link_info *info,
6784                   Elf_Internal_Sym **local_syms,
6785                   struct elf_link_hash_entry *h,
6786                   asection *sym_sec)
6787 {
6788   enum elf_ppc64_reloc_type r_type;
6789   struct ppc_dyn_relocs *p;
6790   struct ppc_dyn_relocs **pp;
6791
6792   /* Can this reloc be dynamic?  This switch, and later tests here
6793      should be kept in sync with the code in check_relocs.  */
6794   r_type = ELF64_R_TYPE (r_info);
6795   switch (r_type)
6796     {
6797     default:
6798       return TRUE;
6799
6800     case R_PPC64_TPREL16:
6801     case R_PPC64_TPREL16_LO:
6802     case R_PPC64_TPREL16_HI:
6803     case R_PPC64_TPREL16_HA:
6804     case R_PPC64_TPREL16_DS:
6805     case R_PPC64_TPREL16_LO_DS:
6806     case R_PPC64_TPREL16_HIGHER:
6807     case R_PPC64_TPREL16_HIGHERA:
6808     case R_PPC64_TPREL16_HIGHEST:
6809     case R_PPC64_TPREL16_HIGHESTA:
6810       if (!info->shared)
6811         return TRUE;
6812
6813     case R_PPC64_TPREL64:
6814     case R_PPC64_DTPMOD64:
6815     case R_PPC64_DTPREL64:
6816     case R_PPC64_ADDR64:
6817     case R_PPC64_REL30:
6818     case R_PPC64_REL32:
6819     case R_PPC64_REL64:
6820     case R_PPC64_ADDR14:
6821     case R_PPC64_ADDR14_BRNTAKEN:
6822     case R_PPC64_ADDR14_BRTAKEN:
6823     case R_PPC64_ADDR16:
6824     case R_PPC64_ADDR16_DS:
6825     case R_PPC64_ADDR16_HA:
6826     case R_PPC64_ADDR16_HI:
6827     case R_PPC64_ADDR16_HIGHER:
6828     case R_PPC64_ADDR16_HIGHERA:
6829     case R_PPC64_ADDR16_HIGHEST:
6830     case R_PPC64_ADDR16_HIGHESTA:
6831     case R_PPC64_ADDR16_LO:
6832     case R_PPC64_ADDR16_LO_DS:
6833     case R_PPC64_ADDR24:
6834     case R_PPC64_ADDR32:
6835     case R_PPC64_UADDR16:
6836     case R_PPC64_UADDR32:
6837     case R_PPC64_UADDR64:
6838     case R_PPC64_TOC:
6839       break;
6840     }
6841
6842   if (local_syms != NULL)
6843     {
6844       unsigned long r_symndx;
6845       Elf_Internal_Sym *sym;
6846       bfd *ibfd = sec->owner;
6847
6848       r_symndx = ELF64_R_SYM (r_info);
6849       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6850         return FALSE;
6851     }
6852
6853   if ((info->shared
6854        && (must_be_dyn_reloc (info, r_type)
6855            || (h != NULL
6856                && (!info->symbolic
6857                    || h->root.type == bfd_link_hash_defweak
6858                    || !h->def_regular))))
6859       || (ELIMINATE_COPY_RELOCS
6860           && !info->shared
6861           && h != NULL
6862           && (h->root.type == bfd_link_hash_defweak
6863               || !h->def_regular)))
6864     ;
6865   else
6866     return TRUE;
6867
6868   if (h != NULL)
6869     pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6870   else
6871     {
6872       if (sym_sec != NULL)
6873         {
6874           void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6875           pp = (struct ppc_dyn_relocs **) vpp;
6876         }
6877       else
6878         {
6879           void *vpp = &elf_section_data (sec)->local_dynrel;
6880           pp = (struct ppc_dyn_relocs **) vpp;
6881         }
6882
6883       /* elf_gc_sweep may have already removed all dyn relocs associated
6884          with local syms for a given section.  Don't report a dynreloc
6885          miscount.  */
6886       if (*pp == NULL)
6887         return TRUE;
6888     }
6889
6890   while ((p = *pp) != NULL)
6891     {
6892       if (p->sec == sec)
6893         {
6894           if (!must_be_dyn_reloc (info, r_type))
6895             p->pc_count -= 1;
6896           p->count -= 1;
6897           if (p->count == 0)
6898             *pp = p->next;
6899           return TRUE;
6900         }
6901       pp = &p->next;
6902     }
6903
6904   (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
6905                            sec->owner, sec);
6906   bfd_set_error (bfd_error_bad_value);
6907   return FALSE;
6908 }
6909
6910 /* Remove unused Official Procedure Descriptor entries.  Currently we
6911    only remove those associated with functions in discarded link-once
6912    sections, or weakly defined functions that have been overridden.  It
6913    would be possible to remove many more entries for statically linked
6914    applications.  */
6915
6916 bfd_boolean
6917 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
6918 {
6919   bfd *ibfd;
6920   bfd_boolean some_edited = FALSE;
6921   asection *need_pad = NULL;
6922
6923   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6924     {
6925       asection *sec;
6926       Elf_Internal_Rela *relstart, *rel, *relend;
6927       Elf_Internal_Shdr *symtab_hdr;
6928       Elf_Internal_Sym *local_syms;
6929       struct elf_link_hash_entry **sym_hashes;
6930       bfd_vma offset;
6931       struct _opd_sec_data *opd;
6932       bfd_boolean need_edit, add_aux_fields;
6933       bfd_size_type cnt_16b = 0;
6934
6935       if (!is_ppc64_elf (ibfd))
6936         continue;
6937
6938       sec = bfd_get_section_by_name (ibfd, ".opd");
6939       if (sec == NULL || sec->size == 0)
6940         continue;
6941
6942       if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6943         continue;
6944
6945       if (sec->output_section == bfd_abs_section_ptr)
6946         continue;
6947
6948       /* Look through the section relocs.  */
6949       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6950         continue;
6951
6952       local_syms = NULL;
6953       symtab_hdr = &elf_symtab_hdr (ibfd);
6954       sym_hashes = elf_sym_hashes (ibfd);
6955
6956       /* Read the relocations.  */
6957       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6958                                             info->keep_memory);
6959       if (relstart == NULL)
6960         return FALSE;
6961
6962       /* First run through the relocs to check they are sane, and to
6963          determine whether we need to edit this opd section.  */
6964       need_edit = FALSE;
6965       need_pad = sec;
6966       offset = 0;
6967       relend = relstart + sec->reloc_count;
6968       for (rel = relstart; rel < relend; )
6969         {
6970           enum elf_ppc64_reloc_type r_type;
6971           unsigned long r_symndx;
6972           asection *sym_sec;
6973           struct elf_link_hash_entry *h;
6974           Elf_Internal_Sym *sym;
6975
6976           /* .opd contains a regular array of 16 or 24 byte entries.  We're
6977              only interested in the reloc pointing to a function entry
6978              point.  */
6979           if (rel->r_offset != offset
6980               || rel + 1 >= relend
6981               || (rel + 1)->r_offset != offset + 8)
6982             {
6983               /* If someone messes with .opd alignment then after a
6984                  "ld -r" we might have padding in the middle of .opd.
6985                  Also, there's nothing to prevent someone putting
6986                  something silly in .opd with the assembler.  No .opd
6987                  optimization for them!  */
6988             broken_opd:
6989               (*_bfd_error_handler)
6990                 (_("%B: .opd is not a regular array of opd entries"), ibfd);
6991               need_edit = FALSE;
6992               break;
6993             }
6994
6995           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6996               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6997             {
6998               (*_bfd_error_handler)
6999                 (_("%B: unexpected reloc type %u in .opd section"),
7000                  ibfd, r_type);
7001               need_edit = FALSE;
7002               break;
7003             }
7004
7005           r_symndx = ELF64_R_SYM (rel->r_info);
7006           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7007                           r_symndx, ibfd))
7008             goto error_ret;
7009
7010           if (sym_sec == NULL || sym_sec->owner == NULL)
7011             {
7012               const char *sym_name;
7013               if (h != NULL)
7014                 sym_name = h->root.root.string;
7015               else
7016                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7017                                              sym_sec);
7018
7019               (*_bfd_error_handler)
7020                 (_("%B: undefined sym `%s' in .opd section"),
7021                  ibfd, sym_name);
7022               need_edit = FALSE;
7023               break;
7024             }
7025
7026           /* opd entries are always for functions defined in the
7027              current input bfd.  If the symbol isn't defined in the
7028              input bfd, then we won't be using the function in this
7029              bfd;  It must be defined in a linkonce section in another
7030              bfd, or is weak.  It's also possible that we are
7031              discarding the function due to a linker script /DISCARD/,
7032              which we test for via the output_section.  */
7033           if (sym_sec->owner != ibfd
7034               || sym_sec->output_section == bfd_abs_section_ptr)
7035             need_edit = TRUE;
7036
7037           rel += 2;
7038           if (rel == relend
7039               || (rel + 1 == relend && rel->r_offset == offset + 16))
7040             {
7041               if (sec->size == offset + 24)
7042                 {
7043                   need_pad = NULL;
7044                   break;
7045                 }
7046               if (rel == relend && sec->size == offset + 16)
7047                 {
7048                   cnt_16b++;
7049                   break;
7050                 }
7051               goto broken_opd;
7052             }
7053
7054           if (rel->r_offset == offset + 24)
7055             offset += 24;
7056           else if (rel->r_offset != offset + 16)
7057             goto broken_opd;
7058           else if (rel + 1 < relend
7059                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7060                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7061             {
7062               offset += 16;
7063               cnt_16b++;
7064             }
7065           else if (rel + 2 < relend
7066                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7067                    && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7068             {
7069               offset += 24;
7070               rel += 1;
7071             }
7072           else
7073             goto broken_opd;
7074         }
7075
7076       add_aux_fields = non_overlapping && cnt_16b > 0;
7077
7078       if (need_edit || add_aux_fields)
7079         {
7080           Elf_Internal_Rela *write_rel;
7081           bfd_byte *rptr, *wptr;
7082           bfd_byte *new_contents;
7083           bfd_boolean skip;
7084           long opd_ent_size;
7085           bfd_size_type amt;
7086
7087           new_contents = NULL;
7088           amt = sec->size * sizeof (long) / 8;
7089           opd = &ppc64_elf_section_data (sec)->u.opd;
7090           opd->adjust = bfd_zalloc (sec->owner, amt);
7091           if (opd->adjust == NULL)
7092             return FALSE;
7093           ppc64_elf_section_data (sec)->sec_type = sec_opd;
7094
7095           /* This seems a waste of time as input .opd sections are all
7096              zeros as generated by gcc, but I suppose there's no reason
7097              this will always be so.  We might start putting something in
7098              the third word of .opd entries.  */
7099           if ((sec->flags & SEC_IN_MEMORY) == 0)
7100             {
7101               bfd_byte *loc;
7102               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7103                 {
7104                   if (loc != NULL)
7105                     free (loc);
7106                 error_ret:
7107                   if (local_syms != NULL
7108                       && symtab_hdr->contents != (unsigned char *) local_syms)
7109                     free (local_syms);
7110                   if (elf_section_data (sec)->relocs != relstart)
7111                     free (relstart);
7112                   return FALSE;
7113                 }
7114               sec->contents = loc;
7115               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7116             }
7117
7118           elf_section_data (sec)->relocs = relstart;
7119
7120           new_contents = sec->contents;
7121           if (add_aux_fields)
7122             {
7123               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7124               if (new_contents == NULL)
7125                 return FALSE;
7126               need_pad = FALSE;
7127             }
7128           wptr = new_contents;
7129           rptr = sec->contents;
7130
7131           write_rel = relstart;
7132           skip = FALSE;
7133           offset = 0;
7134           opd_ent_size = 0;
7135           for (rel = relstart; rel < relend; rel++)
7136             {
7137               unsigned long r_symndx;
7138               asection *sym_sec;
7139               struct elf_link_hash_entry *h;
7140               Elf_Internal_Sym *sym;
7141
7142               r_symndx = ELF64_R_SYM (rel->r_info);
7143               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7144                               r_symndx, ibfd))
7145                 goto error_ret;
7146
7147               if (rel->r_offset == offset)
7148                 {
7149                   struct ppc_link_hash_entry *fdh = NULL;
7150
7151                   /* See if the .opd entry is full 24 byte or
7152                      16 byte (with fd_aux entry overlapped with next
7153                      fd_func).  */
7154                   opd_ent_size = 24;
7155                   if ((rel + 2 == relend && sec->size == offset + 16)
7156                       || (rel + 3 < relend
7157                           && rel[2].r_offset == offset + 16
7158                           && rel[3].r_offset == offset + 24
7159                           && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7160                           && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7161                     opd_ent_size = 16;
7162
7163                   if (h != NULL
7164                       && h->root.root.string[0] == '.')
7165                     {
7166                       struct ppc_link_hash_table *htab;
7167
7168                       htab = ppc_hash_table (info);
7169                       if (htab != NULL)
7170                         fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7171                                           htab);
7172                       if (fdh != NULL
7173                           && fdh->elf.root.type != bfd_link_hash_defined
7174                           && fdh->elf.root.type != bfd_link_hash_defweak)
7175                         fdh = NULL;
7176                     }
7177
7178                   skip = (sym_sec->owner != ibfd
7179                           || sym_sec->output_section == bfd_abs_section_ptr);
7180                   if (skip)
7181                     {
7182                       if (fdh != NULL && sym_sec->owner == ibfd)
7183                         {
7184                           /* Arrange for the function descriptor sym
7185                              to be dropped.  */
7186                           fdh->elf.root.u.def.value = 0;
7187                           fdh->elf.root.u.def.section = sym_sec;
7188                         }
7189                       opd->adjust[rel->r_offset / 8] = -1;
7190                     }
7191                   else
7192                     {
7193                       /* We'll be keeping this opd entry.  */
7194
7195                       if (fdh != NULL)
7196                         {
7197                           /* Redefine the function descriptor symbol to
7198                              this location in the opd section.  It is
7199                              necessary to update the value here rather
7200                              than using an array of adjustments as we do
7201                              for local symbols, because various places
7202                              in the generic ELF code use the value
7203                              stored in u.def.value.  */
7204                           fdh->elf.root.u.def.value = wptr - new_contents;
7205                           fdh->adjust_done = 1;
7206                         }
7207
7208                       /* Local syms are a bit tricky.  We could
7209                          tweak them as they can be cached, but
7210                          we'd need to look through the local syms
7211                          for the function descriptor sym which we
7212                          don't have at the moment.  So keep an
7213                          array of adjustments.  */
7214                       opd->adjust[rel->r_offset / 8]
7215                         = (wptr - new_contents) - (rptr - sec->contents);
7216
7217                       if (wptr != rptr)
7218                         memcpy (wptr, rptr, opd_ent_size);
7219                       wptr += opd_ent_size;
7220                       if (add_aux_fields && opd_ent_size == 16)
7221                         {
7222                           memset (wptr, '\0', 8);
7223                           wptr += 8;
7224                         }
7225                     }
7226                   rptr += opd_ent_size;
7227                   offset += opd_ent_size;
7228                 }
7229
7230               if (skip)
7231                 {
7232                   if (!NO_OPD_RELOCS
7233                       && !info->relocatable
7234                       && !dec_dynrel_count (rel->r_info, sec, info,
7235                                             NULL, h, sym_sec))
7236                     goto error_ret;
7237                 }
7238               else
7239                 {
7240                   /* We need to adjust any reloc offsets to point to the
7241                      new opd entries.  While we're at it, we may as well
7242                      remove redundant relocs.  */
7243                   rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
7244                   if (write_rel != rel)
7245                     memcpy (write_rel, rel, sizeof (*rel));
7246                   ++write_rel;
7247                 }
7248             }
7249
7250           sec->size = wptr - new_contents;
7251           sec->reloc_count = write_rel - relstart;
7252           if (add_aux_fields)
7253             {
7254               free (sec->contents);
7255               sec->contents = new_contents;
7256             }
7257
7258           /* Fudge the header size too, as this is used later in
7259              elf_bfd_final_link if we are emitting relocs.  */
7260           elf_section_data (sec)->rel_hdr.sh_size
7261             = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
7262           BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
7263           some_edited = TRUE;
7264         }
7265       else if (elf_section_data (sec)->relocs != relstart)
7266         free (relstart);
7267
7268       if (local_syms != NULL
7269           && symtab_hdr->contents != (unsigned char *) local_syms)
7270         {
7271           if (!info->keep_memory)
7272             free (local_syms);
7273           else
7274             symtab_hdr->contents = (unsigned char *) local_syms;
7275         }
7276     }
7277
7278   if (some_edited)
7279     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7280
7281   /* If we are doing a final link and the last .opd entry is just 16 byte
7282      long, add a 8 byte padding after it.  */
7283   if (need_pad != NULL && !info->relocatable)
7284     {
7285       bfd_byte *p;
7286
7287       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7288         {
7289           BFD_ASSERT (need_pad->size > 0);
7290
7291           p = bfd_malloc (need_pad->size + 8);
7292           if (p == NULL)
7293             return FALSE;
7294
7295           if (! bfd_get_section_contents (need_pad->owner, need_pad,
7296                                           p, 0, need_pad->size))
7297             return FALSE;
7298
7299           need_pad->contents = p;
7300           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7301         }
7302       else
7303         {
7304           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7305           if (p == NULL)
7306             return FALSE;
7307
7308           need_pad->contents = p;
7309         }
7310
7311       memset (need_pad->contents + need_pad->size, 0, 8);
7312       need_pad->size += 8;
7313     }
7314
7315   return TRUE;
7316 }
7317
7318 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
7319
7320 asection *
7321 ppc64_elf_tls_setup (struct bfd_link_info *info,
7322                      int no_tls_get_addr_opt,
7323                      int *no_multi_toc)
7324 {
7325   struct ppc_link_hash_table *htab;
7326
7327   htab = ppc_hash_table (info);
7328   if (htab == NULL)
7329     return NULL;
7330
7331   if (*no_multi_toc)
7332     htab->do_multi_toc = 0;
7333   else if (!htab->do_multi_toc)
7334     *no_multi_toc = 1;
7335
7336   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7337                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7338                                               FALSE, FALSE, TRUE));
7339   /* Move dynamic linking info to the function descriptor sym.  */
7340   if (htab->tls_get_addr != NULL)
7341     func_desc_adjust (&htab->tls_get_addr->elf, info);
7342   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7343                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7344                                                  FALSE, FALSE, TRUE));
7345   if (!no_tls_get_addr_opt)
7346     {
7347       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7348
7349       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7350                                   FALSE, FALSE, TRUE);
7351       if (opt != NULL)
7352         func_desc_adjust (opt, info);
7353       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7354                                      FALSE, FALSE, TRUE);
7355       if (opt_fd != NULL
7356           && (opt_fd->root.type == bfd_link_hash_defined
7357               || opt_fd->root.type == bfd_link_hash_defweak))
7358         {
7359           /* If glibc supports an optimized __tls_get_addr call stub,
7360              signalled by the presence of __tls_get_addr_opt, and we'll
7361              be calling __tls_get_addr via a plt call stub, then
7362              make __tls_get_addr point to __tls_get_addr_opt.  */
7363           tga_fd = &htab->tls_get_addr_fd->elf;
7364           if (htab->elf.dynamic_sections_created
7365               && tga_fd != NULL
7366               && (tga_fd->type == STT_FUNC
7367                   || tga_fd->needs_plt)
7368               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7369                    || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7370                        && tga_fd->root.type == bfd_link_hash_undefweak)))
7371             {
7372               struct plt_entry *ent;
7373
7374               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7375                 if (ent->plt.refcount > 0)
7376                   break;
7377               if (ent != NULL)
7378                 {
7379                   tga_fd->root.type = bfd_link_hash_indirect;
7380                   tga_fd->root.u.i.link = &opt_fd->root;
7381                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7382                   if (opt_fd->dynindx != -1)
7383                     {
7384                       /* Use __tls_get_addr_opt in dynamic relocations.  */
7385                       opt_fd->dynindx = -1;
7386                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7387                                               opt_fd->dynstr_index);
7388                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7389                         return NULL;
7390                     }
7391                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7392                   tga = &htab->tls_get_addr->elf;
7393                   if (opt != NULL && tga != NULL)
7394                     {
7395                       tga->root.type = bfd_link_hash_indirect;
7396                       tga->root.u.i.link = &opt->root;
7397                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
7398                       _bfd_elf_link_hash_hide_symbol (info, opt,
7399                                                       tga->forced_local);
7400                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7401                     }
7402                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7403                   htab->tls_get_addr_fd->is_func_descriptor = 1;
7404                   if (htab->tls_get_addr != NULL)
7405                     {
7406                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7407                       htab->tls_get_addr->is_func = 1;
7408                     }
7409                 }
7410             }
7411         }
7412       else
7413         no_tls_get_addr_opt = TRUE;
7414     }
7415   htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
7416   return _bfd_elf_tls_setup (info->output_bfd, info);
7417 }
7418
7419 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7420    HASH1 or HASH2.  */
7421
7422 static bfd_boolean
7423 branch_reloc_hash_match (const bfd *ibfd,
7424                          const Elf_Internal_Rela *rel,
7425                          const struct ppc_link_hash_entry *hash1,
7426                          const struct ppc_link_hash_entry *hash2)
7427 {
7428   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7429   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7430   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7431
7432   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7433     {
7434       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7435       struct elf_link_hash_entry *h;
7436
7437       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7438       h = elf_follow_link (h);
7439       if (h == &hash1->elf || h == &hash2->elf)
7440         return TRUE;
7441     }
7442   return FALSE;
7443 }
7444
7445 /* Run through all the TLS relocs looking for optimization
7446    opportunities.  The linker has been hacked (see ppc64elf.em) to do
7447    a preliminary section layout so that we know the TLS segment
7448    offsets.  We can't optimize earlier because some optimizations need
7449    to know the tp offset, and we need to optimize before allocating
7450    dynamic relocations.  */
7451
7452 bfd_boolean
7453 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7454 {
7455   bfd *ibfd;
7456   asection *sec;
7457   struct ppc_link_hash_table *htab;
7458   int pass;
7459
7460   if (info->relocatable || !info->executable)
7461     return TRUE;
7462
7463   htab = ppc_hash_table (info);
7464   if (htab == NULL)
7465     return FALSE;
7466
7467   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7468     {
7469       Elf_Internal_Sym *locsyms = NULL;
7470       asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7471       unsigned char *toc_ref = NULL;
7472
7473       /* Look at all the sections for this file.  Make two passes over
7474          the relocs.  On the first pass, mark toc entries involved
7475          with tls relocs, and check that tls relocs involved in
7476          setting up a tls_get_addr call are indeed followed by such a
7477          call.  If they are not, exclude them from the optimizations
7478          done on the second pass.  */
7479       for (pass = 0; pass < 2; ++pass)
7480         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7481           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7482             {
7483               Elf_Internal_Rela *relstart, *rel, *relend;
7484
7485               /* Read the relocations.  */
7486               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7487                                                     info->keep_memory);
7488               if (relstart == NULL)
7489                 return FALSE;
7490
7491               relend = relstart + sec->reloc_count;
7492               for (rel = relstart; rel < relend; rel++)
7493                 {
7494                   enum elf_ppc64_reloc_type r_type;
7495                   unsigned long r_symndx;
7496                   struct elf_link_hash_entry *h;
7497                   Elf_Internal_Sym *sym;
7498                   asection *sym_sec;
7499                   unsigned char *tls_mask;
7500                   unsigned char tls_set, tls_clear, tls_type = 0;
7501                   bfd_vma value;
7502                   bfd_boolean ok_tprel, is_local;
7503                   long toc_ref_index = 0;
7504                   int expecting_tls_get_addr = 0;
7505
7506                   r_symndx = ELF64_R_SYM (rel->r_info);
7507                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7508                                   r_symndx, ibfd))
7509                     {
7510                     err_free_rel:
7511                       if (elf_section_data (sec)->relocs != relstart)
7512                         free (relstart);
7513                       if (toc_ref != NULL)
7514                         free (toc_ref);
7515                       if (locsyms != NULL
7516                           && (elf_symtab_hdr (ibfd).contents
7517                               != (unsigned char *) locsyms))
7518                         free (locsyms);
7519                       return FALSE;
7520                     }
7521
7522                   if (h != NULL)
7523                     {
7524                       if (h->root.type == bfd_link_hash_defined
7525                           || h->root.type == bfd_link_hash_defweak)
7526                         value = h->root.u.def.value;
7527                       else if (h->root.type == bfd_link_hash_undefweak)
7528                         value = 0;
7529                       else
7530                         continue;
7531                     }
7532                   else
7533                     /* Symbols referenced by TLS relocs must be of type
7534                        STT_TLS.  So no need for .opd local sym adjust.  */
7535                     value = sym->st_value;
7536
7537                   ok_tprel = FALSE;
7538                   is_local = FALSE;
7539                   if (h == NULL
7540                       || !h->def_dynamic)
7541                     {
7542                       is_local = TRUE;
7543                       if (h != NULL
7544                           && h->root.type == bfd_link_hash_undefweak)
7545                         ok_tprel = TRUE;
7546                       else
7547                         {
7548                           value += sym_sec->output_offset;
7549                           value += sym_sec->output_section->vma;
7550                           value -= htab->elf.tls_sec->vma;
7551                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7552                                       < (bfd_vma) 1 << 32);
7553                         }
7554                     }
7555
7556                   r_type = ELF64_R_TYPE (rel->r_info);
7557                   switch (r_type)
7558                     {
7559                     case R_PPC64_GOT_TLSLD16:
7560                     case R_PPC64_GOT_TLSLD16_LO:
7561                       expecting_tls_get_addr = 1;
7562                       /* Fall thru */
7563
7564                     case R_PPC64_GOT_TLSLD16_HI:
7565                     case R_PPC64_GOT_TLSLD16_HA:
7566                       /* These relocs should never be against a symbol
7567                          defined in a shared lib.  Leave them alone if
7568                          that turns out to be the case.  */
7569                       if (!is_local)
7570                         continue;
7571
7572                       /* LD -> LE */
7573                       tls_set = 0;
7574                       tls_clear = TLS_LD;
7575                       tls_type = TLS_TLS | TLS_LD;
7576                       break;
7577
7578                     case R_PPC64_GOT_TLSGD16:
7579                     case R_PPC64_GOT_TLSGD16_LO:
7580                       expecting_tls_get_addr = 1;
7581                       /* Fall thru */
7582
7583                     case R_PPC64_GOT_TLSGD16_HI:
7584                     case R_PPC64_GOT_TLSGD16_HA:
7585                       if (ok_tprel)
7586                         /* GD -> LE */
7587                         tls_set = 0;
7588                       else
7589                         /* GD -> IE */
7590                         tls_set = TLS_TLS | TLS_TPRELGD;
7591                       tls_clear = TLS_GD;
7592                       tls_type = TLS_TLS | TLS_GD;
7593                       break;
7594
7595                     case R_PPC64_GOT_TPREL16_DS:
7596                     case R_PPC64_GOT_TPREL16_LO_DS:
7597                     case R_PPC64_GOT_TPREL16_HI:
7598                     case R_PPC64_GOT_TPREL16_HA:
7599                       if (ok_tprel)
7600                         {
7601                           /* IE -> LE */
7602                           tls_set = 0;
7603                           tls_clear = TLS_TPREL;
7604                           tls_type = TLS_TLS | TLS_TPREL;
7605                           break;
7606                         }
7607                       continue;
7608
7609                     case R_PPC64_TOC16:
7610                     case R_PPC64_TOC16_LO:
7611                     case R_PPC64_TLS:
7612                     case R_PPC64_TLSGD:
7613                     case R_PPC64_TLSLD:
7614                       if (sym_sec == NULL || sym_sec != toc)
7615                         continue;
7616
7617                       /* Mark this toc entry as referenced by a TLS
7618                          code sequence.  We can do that now in the
7619                          case of R_PPC64_TLS, and after checking for
7620                          tls_get_addr for the TOC16 relocs.  */
7621                       if (toc_ref == NULL)
7622                         {
7623                           toc_ref = bfd_zmalloc (toc->size / 8);
7624                           if (toc_ref == NULL)
7625                             goto err_free_rel;
7626                         }
7627                       if (h != NULL)
7628                         value = h->root.u.def.value;
7629                       else
7630                         value = sym->st_value;
7631                       value += rel->r_addend;
7632                       BFD_ASSERT (value < toc->size && value % 8 == 0);
7633                       toc_ref_index = value / 8;
7634                       if (r_type == R_PPC64_TLS
7635                           || r_type == R_PPC64_TLSGD
7636                           || r_type == R_PPC64_TLSLD)
7637                         {
7638                           toc_ref[toc_ref_index] = 1;
7639                           continue;
7640                         }
7641
7642                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
7643                         continue;
7644
7645                       tls_set = 0;
7646                       tls_clear = 0;
7647                       expecting_tls_get_addr = 2;
7648                       break;
7649
7650                     case R_PPC64_TPREL64:
7651                       if (pass == 0
7652                           || sec != toc
7653                           || toc_ref == NULL
7654                           || !toc_ref[rel->r_offset / 8])
7655                         continue;
7656                       if (ok_tprel)
7657                         {
7658                           /* IE -> LE */
7659                           tls_set = TLS_EXPLICIT;
7660                           tls_clear = TLS_TPREL;
7661                           break;
7662                         }
7663                       continue;
7664
7665                     case R_PPC64_DTPMOD64:
7666                       if (pass == 0
7667                           || sec != toc
7668                           || toc_ref == NULL
7669                           || !toc_ref[rel->r_offset / 8])
7670                         continue;
7671                       if (rel + 1 < relend
7672                           && (rel[1].r_info
7673                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7674                           && rel[1].r_offset == rel->r_offset + 8)
7675                         {
7676                           if (ok_tprel)
7677                             /* GD -> LE */
7678                             tls_set = TLS_EXPLICIT | TLS_GD;
7679                           else
7680                             /* GD -> IE */
7681                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7682                           tls_clear = TLS_GD;
7683                         }
7684                       else
7685                         {
7686                           if (!is_local)
7687                             continue;
7688
7689                           /* LD -> LE */
7690                           tls_set = TLS_EXPLICIT;
7691                           tls_clear = TLS_LD;
7692                         }
7693                       break;
7694
7695                     default:
7696                       continue;
7697                     }
7698
7699                   if (pass == 0)
7700                     {
7701                       if (!expecting_tls_get_addr
7702                           || !sec->has_tls_get_addr_call)
7703                         continue;
7704
7705                       if (rel + 1 < relend
7706                           && branch_reloc_hash_match (ibfd, rel + 1,
7707                                                       htab->tls_get_addr,
7708                                                       htab->tls_get_addr_fd))
7709                         {
7710                           if (expecting_tls_get_addr == 2)
7711                             {
7712                               /* Check for toc tls entries.  */
7713                               unsigned char *toc_tls;
7714                               int retval;
7715
7716                               retval = get_tls_mask (&toc_tls, NULL, NULL,
7717                                                      &locsyms,
7718                                                      rel, ibfd);
7719                               if (retval == 0)
7720                                 goto err_free_rel;
7721                               if (retval > 1 && toc_tls != NULL)
7722                                 toc_ref[toc_ref_index] = 1;
7723                             }
7724                           continue;
7725                         }
7726
7727                       if (expecting_tls_get_addr != 1)
7728                         continue;
7729
7730                       /* Uh oh, we didn't find the expected call.  We
7731                          could just mark this symbol to exclude it
7732                          from tls optimization but it's safer to skip
7733                          the entire section.  */
7734                       sec->has_tls_reloc = 0;
7735                       break;
7736                     }
7737
7738                   if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
7739                     {
7740                       struct plt_entry *ent;
7741                       for (ent = htab->tls_get_addr->elf.plt.plist;
7742                            ent != NULL;
7743                            ent = ent->next)
7744                         if (ent->addend == 0)
7745                           {
7746                             if (ent->plt.refcount > 0)
7747                               {
7748                                 ent->plt.refcount -= 1;
7749                                 expecting_tls_get_addr = 0;
7750                               }
7751                             break;
7752                           }
7753                     }
7754
7755                   if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
7756                     {
7757                       struct plt_entry *ent;
7758                       for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7759                            ent != NULL;
7760                            ent = ent->next)
7761                         if (ent->addend == 0)
7762                           {
7763                             if (ent->plt.refcount > 0)
7764                               ent->plt.refcount -= 1;
7765                             break;
7766                           }
7767                     }
7768
7769                   if (tls_clear == 0)
7770                     continue;
7771
7772                   if ((tls_set & TLS_EXPLICIT) == 0)
7773                     {
7774                       struct got_entry *ent;
7775
7776                       /* Adjust got entry for this reloc.  */
7777                       if (h != NULL)
7778                         ent = h->got.glist;
7779                       else
7780                         ent = elf_local_got_ents (ibfd)[r_symndx];
7781
7782                       for (; ent != NULL; ent = ent->next)
7783                         if (ent->addend == rel->r_addend
7784                             && ent->owner == ibfd
7785                             && ent->tls_type == tls_type)
7786                           break;
7787                       if (ent == NULL)
7788                         abort ();
7789
7790                       if (tls_set == 0)
7791                         {
7792                           /* We managed to get rid of a got entry.  */
7793                           if (ent->got.refcount > 0)
7794                             ent->got.refcount -= 1;
7795                         }
7796                     }
7797                   else
7798                     {
7799                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
7800                          we'll lose one or two dyn relocs.  */
7801                       if (!dec_dynrel_count (rel->r_info, sec, info,
7802                                              NULL, h, sym_sec))
7803                         return FALSE;
7804
7805                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
7806                         {
7807                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7808                                                  NULL, h, sym_sec))
7809                             return FALSE;
7810                         }
7811                     }
7812
7813                   *tls_mask |= tls_set;
7814                   *tls_mask &= ~tls_clear;
7815                 }
7816
7817               if (elf_section_data (sec)->relocs != relstart)
7818                 free (relstart);
7819             }
7820
7821       if (toc_ref != NULL)
7822         free (toc_ref);
7823
7824       if (locsyms != NULL
7825           && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7826         {
7827           if (!info->keep_memory)
7828             free (locsyms);
7829           else
7830             elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7831         }
7832     }
7833   return TRUE;
7834 }
7835
7836 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7837    the values of any global symbols in a toc section that has been
7838    edited.  Globals in toc sections should be a rarity, so this function
7839    sets a flag if any are found in toc sections other than the one just
7840    edited, so that futher hash table traversals can be avoided.  */
7841
7842 struct adjust_toc_info
7843 {
7844   asection *toc;
7845   unsigned long *skip;
7846   bfd_boolean global_toc_syms;
7847 };
7848
7849 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7850
7851 static bfd_boolean
7852 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7853 {
7854   struct ppc_link_hash_entry *eh;
7855   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7856   unsigned long i;
7857
7858   if (h->root.type == bfd_link_hash_indirect)
7859     return TRUE;
7860
7861   if (h->root.type == bfd_link_hash_warning)
7862     h = (struct elf_link_hash_entry *) h->root.u.i.link;
7863
7864   if (h->root.type != bfd_link_hash_defined
7865       && h->root.type != bfd_link_hash_defweak)
7866     return TRUE;
7867
7868   eh = (struct ppc_link_hash_entry *) h;
7869   if (eh->adjust_done)
7870     return TRUE;
7871
7872   if (eh->elf.root.u.def.section == toc_inf->toc)
7873     {
7874       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7875         i = toc_inf->toc->rawsize >> 3;
7876       else
7877         i = eh->elf.root.u.def.value >> 3;
7878
7879       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
7880         {
7881           (*_bfd_error_handler)
7882             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7883           do
7884             ++i;
7885           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
7886           eh->elf.root.u.def.value = (bfd_vma) i << 3;
7887         }
7888
7889       eh->elf.root.u.def.value -= toc_inf->skip[i];
7890       eh->adjust_done = 1;
7891     }
7892   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7893     toc_inf->global_toc_syms = TRUE;
7894
7895   return TRUE;
7896 }
7897
7898 /* Examine all relocs referencing .toc sections in order to remove
7899    unused .toc entries.  */
7900
7901 bfd_boolean
7902 ppc64_elf_edit_toc (struct bfd_link_info *info)
7903 {
7904   bfd *ibfd;
7905   struct adjust_toc_info toc_inf;
7906   struct ppc_link_hash_table *htab = ppc_hash_table (info);
7907
7908   htab->do_toc_opt = 1;
7909   toc_inf.global_toc_syms = TRUE;
7910   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7911     {
7912       asection *toc, *sec;
7913       Elf_Internal_Shdr *symtab_hdr;
7914       Elf_Internal_Sym *local_syms;
7915       struct elf_link_hash_entry **sym_hashes;
7916       Elf_Internal_Rela *relstart, *rel;
7917       unsigned long *skip, *drop;
7918       unsigned char *used;
7919       unsigned char *keep, last, some_unused;
7920
7921       if (!is_ppc64_elf (ibfd))
7922         continue;
7923
7924       toc = bfd_get_section_by_name (ibfd, ".toc");
7925       if (toc == NULL
7926           || toc->size == 0
7927           || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7928           || elf_discarded_section (toc))
7929         continue;
7930
7931       local_syms = NULL;
7932       symtab_hdr = &elf_symtab_hdr (ibfd);
7933       sym_hashes = elf_sym_hashes (ibfd);
7934
7935       /* Look at sections dropped from the final link.  */
7936       skip = NULL;
7937       relstart = NULL;
7938       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7939         {
7940           if (sec->reloc_count == 0
7941               || !elf_discarded_section (sec)
7942               || get_opd_info (sec)
7943               || (sec->flags & SEC_ALLOC) == 0
7944               || (sec->flags & SEC_DEBUGGING) != 0)
7945             continue;
7946
7947           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7948           if (relstart == NULL)
7949             goto error_ret;
7950
7951           /* Run through the relocs to see which toc entries might be
7952              unused.  */
7953           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7954             {
7955               enum elf_ppc64_reloc_type r_type;
7956               unsigned long r_symndx;
7957               asection *sym_sec;
7958               struct elf_link_hash_entry *h;
7959               Elf_Internal_Sym *sym;
7960               bfd_vma val;
7961
7962               r_type = ELF64_R_TYPE (rel->r_info);
7963               switch (r_type)
7964                 {
7965                 default:
7966                   continue;
7967
7968                 case R_PPC64_TOC16:
7969                 case R_PPC64_TOC16_LO:
7970                 case R_PPC64_TOC16_HI:
7971                 case R_PPC64_TOC16_HA:
7972                 case R_PPC64_TOC16_DS:
7973                 case R_PPC64_TOC16_LO_DS:
7974                   break;
7975                 }
7976
7977               r_symndx = ELF64_R_SYM (rel->r_info);
7978               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7979                               r_symndx, ibfd))
7980                 goto error_ret;
7981
7982               if (sym_sec != toc)
7983                 continue;
7984
7985               if (h != NULL)
7986                 val = h->root.u.def.value;
7987               else
7988                 val = sym->st_value;
7989               val += rel->r_addend;
7990
7991               if (val >= toc->size)
7992                 continue;
7993
7994               /* Anything in the toc ought to be aligned to 8 bytes.
7995                  If not, don't mark as unused.  */
7996               if (val & 7)
7997                 continue;
7998
7999               if (skip == NULL)
8000                 {
8001                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8002                   if (skip == NULL)
8003                     goto error_ret;
8004                 }
8005
8006               skip[val >> 3] = ref_from_discarded;
8007             }
8008
8009           if (elf_section_data (sec)->relocs != relstart)
8010             free (relstart);
8011         }
8012
8013       /* For largetoc loads of address constants, we can convert
8014          .  addis rx,2,addr@got@ha
8015          .  ld ry,addr@got@l(rx)
8016          to
8017          .  addis rx,2,addr@toc@ha
8018          .  addi ry,rx,addr@toc@l
8019          when addr is within 2G of the toc pointer.  This then means
8020          that the word storing "addr" in the toc is no longer needed.  */
8021          
8022       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8023           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8024           && toc->reloc_count != 0)
8025         {
8026           /* Read toc relocs.  */
8027           relstart = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8028                                                 info->keep_memory);
8029           if (relstart == NULL)
8030             goto error_ret;
8031
8032           for (rel = relstart; rel < relstart + toc->reloc_count; ++rel)
8033             {
8034               enum elf_ppc64_reloc_type r_type;
8035               unsigned long r_symndx;
8036               asection *sym_sec;
8037               struct elf_link_hash_entry *h;
8038               Elf_Internal_Sym *sym;
8039               bfd_vma val, addr;
8040
8041               r_type = ELF64_R_TYPE (rel->r_info);
8042               if (r_type != R_PPC64_ADDR64)
8043                 continue;
8044
8045               r_symndx = ELF64_R_SYM (rel->r_info);
8046               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8047                               r_symndx, ibfd))
8048                 goto error_ret;
8049
8050               if (!SYMBOL_REFERENCES_LOCAL (info, h))
8051                 continue;
8052
8053               if (h != NULL)
8054                 val = h->root.u.def.value;
8055               else
8056                 val = sym->st_value;
8057               val += rel->r_addend;
8058               val += sym_sec->output_section->vma + sym_sec->output_offset;
8059
8060               /* We don't yet know the exact toc pointer value, but we
8061                  know it will be somewhere in the toc section.  Don't
8062                  optimize if the difference from any possible toc
8063                  pointer is outside [ff..f80008000, 7fff7fff].  */
8064               addr = toc->output_section->vma + TOC_BASE_OFF;
8065               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8066                 continue;
8067
8068               addr = toc->output_section->vma + toc->output_section->rawsize;
8069               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8070                 continue;
8071
8072               if (skip == NULL)
8073                 {
8074                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8075                   if (skip == NULL)
8076                     goto error_ret;
8077                 }
8078
8079               skip[rel->r_offset >> 3]
8080                 |= can_optimize | ((rel - relstart) << 2);
8081             }
8082
8083           if (elf_section_data (toc)->relocs != relstart)
8084             free (relstart);
8085         }
8086
8087       if (skip == NULL)
8088         continue;
8089
8090       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8091       if (used == NULL)
8092         {
8093         error_ret:
8094           if (local_syms != NULL
8095               && symtab_hdr->contents != (unsigned char *) local_syms)
8096             free (local_syms);
8097           if (sec != NULL
8098               && relstart != NULL
8099               && elf_section_data (sec)->relocs != relstart)
8100             free (relstart);
8101           if (skip != NULL)
8102             free (skip);
8103           return FALSE;
8104         }
8105
8106       /* Now check all kept sections that might reference the toc.
8107          Check the toc itself last.  */
8108       for (sec = (ibfd->sections == toc && toc->next ? toc->next
8109                   : ibfd->sections);
8110            sec != NULL;
8111            sec = (sec == toc ? NULL
8112                   : sec->next == NULL ? toc
8113                   : sec->next == toc && toc->next ? toc->next
8114                   : sec->next))
8115         {
8116           int repeat;
8117
8118           if (sec->reloc_count == 0
8119               || elf_discarded_section (sec)
8120               || get_opd_info (sec)
8121               || (sec->flags & SEC_ALLOC) == 0
8122               || (sec->flags & SEC_DEBUGGING) != 0)
8123             continue;
8124
8125           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8126                                                 info->keep_memory);
8127           if (relstart == NULL)
8128             goto error_ret;
8129
8130           /* Mark toc entries referenced as used.  */
8131           repeat = 0;
8132           do
8133             for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8134               {
8135                 enum elf_ppc64_reloc_type r_type;
8136                 unsigned long r_symndx;
8137                 asection *sym_sec;
8138                 struct elf_link_hash_entry *h;
8139                 Elf_Internal_Sym *sym;
8140                 bfd_vma val;
8141
8142                 r_type = ELF64_R_TYPE (rel->r_info);
8143                 switch (r_type)
8144                   {
8145                   case R_PPC64_TOC16:
8146                   case R_PPC64_TOC16_LO:
8147                   case R_PPC64_TOC16_HI:
8148                   case R_PPC64_TOC16_HA:
8149                   case R_PPC64_TOC16_DS:
8150                   case R_PPC64_TOC16_LO_DS:
8151                     /* In case we're taking addresses of toc entries.  */
8152                   case R_PPC64_ADDR64:
8153                     break;
8154
8155                   default:
8156                     continue;
8157                   }
8158
8159                 r_symndx = ELF64_R_SYM (rel->r_info);
8160                 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8161                                 r_symndx, ibfd))
8162                   {
8163                     free (used);
8164                     goto error_ret;
8165                   }
8166
8167                 if (sym_sec != toc)
8168                   continue;
8169
8170                 if (h != NULL)
8171                   val = h->root.u.def.value;
8172                 else
8173                   val = sym->st_value;
8174                 val += rel->r_addend;
8175
8176                 if (val >= toc->size)
8177                   continue;
8178
8179                 if ((skip[val >> 3] & can_optimize) != 0)
8180                   {
8181                     bfd_vma off;
8182                     unsigned char opc;
8183
8184                     switch (r_type)
8185                       {
8186                       case R_PPC64_TOC16_HA:
8187                         break;
8188
8189                       case R_PPC64_TOC16_LO_DS:
8190                         off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8191                         if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
8192                           return FALSE;
8193                         if ((opc & (0x3f << 2)) == (58u << 2))
8194                           break;
8195                         /* Fall thru */
8196
8197                       default:
8198                         /* Wrong sort of reloc, or not a ld.  We may
8199                            as well clear ref_from_discarded too.  */
8200                         skip[val >> 3] = 0;
8201                       }
8202                   }
8203
8204                 /* For the toc section, we only mark as used if
8205                    this entry itself isn't unused.  */
8206                 if (sec == toc
8207                     && !used[val >> 3]
8208                     && (used[rel->r_offset >> 3]
8209                         || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
8210                   /* Do all the relocs again, to catch reference
8211                      chains.  */
8212                   repeat = 1;
8213
8214                 used[val >> 3] = 1;
8215               }
8216           while (repeat);
8217
8218           if (elf_section_data (sec)->relocs != relstart)
8219             free (relstart);
8220         }
8221
8222       /* Merge the used and skip arrays.  Assume that TOC
8223          doublewords not appearing as either used or unused belong
8224          to to an entry more than one doubleword in size.  */
8225       for (drop = skip, keep = used, last = 0, some_unused = 0;
8226            drop < skip + (toc->size + 7) / 8;
8227            ++drop, ++keep)
8228         {
8229           if (*keep)
8230             {
8231               *drop &= ~ref_from_discarded;
8232               if ((*drop & can_optimize) != 0)
8233                 some_unused = 1;
8234               last = 0;
8235             }
8236           else if (*drop)
8237             {
8238               some_unused = 1;
8239               last = ref_from_discarded;
8240             }
8241           else
8242             *drop = last;
8243         }
8244
8245       free (used);
8246
8247       if (some_unused)
8248         {
8249           bfd_byte *contents, *src;
8250           unsigned long off;
8251           bfd_boolean local_toc_syms = FALSE;
8252
8253           /* Shuffle the toc contents, and at the same time convert the
8254              skip array from booleans into offsets.  */
8255           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8256             goto error_ret;
8257
8258           elf_section_data (toc)->this_hdr.contents = contents;
8259
8260           for (src = contents, off = 0, drop = skip;
8261                src < contents + toc->size;
8262                src += 8, ++drop)
8263             {
8264               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8265                 off += 8;
8266               else if (off != 0)
8267                 {
8268                   *drop = off;
8269                   memcpy (src - off, src, 8);
8270                 }
8271             }
8272           *drop = off;
8273           toc->rawsize = toc->size;
8274           toc->size = src - contents - off;
8275
8276           /* Adjust addends for relocs against the toc section sym,
8277              and optimize any accesses we can.  */
8278           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8279             {
8280               if (sec->reloc_count == 0
8281                   || elf_discarded_section (sec))
8282                 continue;
8283
8284               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8285                                                     info->keep_memory);
8286               if (relstart == NULL)
8287                 goto error_ret;
8288
8289               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8290                 {
8291                   enum elf_ppc64_reloc_type r_type;
8292                   unsigned long r_symndx;
8293                   asection *sym_sec;
8294                   struct elf_link_hash_entry *h;
8295                   Elf_Internal_Sym *sym;
8296                   bfd_vma val;
8297
8298                   r_type = ELF64_R_TYPE (rel->r_info);
8299                   switch (r_type)
8300                     {
8301                     default:
8302                       continue;
8303
8304                     case R_PPC64_TOC16:
8305                     case R_PPC64_TOC16_LO:
8306                     case R_PPC64_TOC16_HI:
8307                     case R_PPC64_TOC16_HA:
8308                     case R_PPC64_TOC16_DS:
8309                     case R_PPC64_TOC16_LO_DS:
8310                     case R_PPC64_ADDR64:
8311                       break;
8312                     }
8313
8314                   r_symndx = ELF64_R_SYM (rel->r_info);
8315                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8316                                   r_symndx, ibfd))
8317                     goto error_ret;
8318
8319                   if (sym_sec != toc)
8320                     continue;
8321
8322                   if (h != NULL)
8323                     val = h->root.u.def.value;
8324                   else
8325                     {
8326                       val = sym->st_value;
8327                       if (val != 0)
8328                         local_toc_syms = TRUE;
8329                     }
8330
8331                   val += rel->r_addend;
8332
8333                   if (val > toc->rawsize)
8334                     val = toc->rawsize;
8335                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
8336                     continue;
8337                   else if ((skip[val >> 3] & can_optimize) != 0)
8338                     {
8339                       Elf_Internal_Rela *tocrel
8340                         = elf_section_data (toc)->relocs + (skip[val >> 3] >> 2);
8341                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8342
8343                       switch (r_type)
8344                         {
8345                         case R_PPC64_TOC16_HA:
8346                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8347                           break;
8348
8349                         case R_PPC64_TOC16_LO_DS:
8350                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8351                           break;
8352
8353                         default:
8354                           abort ();
8355                         }
8356                       rel->r_addend = tocrel->r_addend;
8357                       elf_section_data (sec)->relocs = relstart;
8358                       continue;
8359                     }
8360
8361                   if (h != NULL || sym->st_value != 0)
8362                     continue;
8363
8364                   rel->r_addend -= skip[val >> 3];
8365                   elf_section_data (sec)->relocs = relstart;
8366                 }
8367
8368               if (elf_section_data (sec)->relocs != relstart)
8369                 free (relstart);
8370             }
8371
8372           /* We shouldn't have local or global symbols defined in the TOC,
8373              but handle them anyway.  */
8374           if (local_toc_syms)
8375             {
8376               Elf_Internal_Sym *sym;
8377
8378               for (sym = local_syms;
8379                    sym < local_syms + symtab_hdr->sh_info;
8380                    ++sym)
8381                 if (sym->st_value != 0
8382                     && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8383                   {
8384                     unsigned long i;
8385
8386                     if (sym->st_value > toc->rawsize)
8387                       i = toc->rawsize >> 3;
8388                     else
8389                       i = sym->st_value >> 3;
8390
8391                     if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8392                       {
8393                         (*_bfd_error_handler)
8394                           (_("%s defined on removed toc entry"),
8395                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8396                         do
8397                           ++i;
8398                         while ((skip[i] & (ref_from_discarded | can_optimize)));
8399                         sym->st_value = (bfd_vma) i << 3;
8400                       }
8401
8402                     sym->st_value -= skip[i];
8403                     symtab_hdr->contents = (unsigned char *) local_syms;
8404                   }
8405             }
8406
8407           /* Adjust any global syms defined in this toc input section.  */
8408           if (toc_inf.global_toc_syms)
8409             {
8410               toc_inf.toc = toc;
8411               toc_inf.skip = skip;
8412               toc_inf.global_toc_syms = FALSE;
8413               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8414                                       &toc_inf);
8415             }
8416
8417           if (toc->reloc_count != 0)
8418             {
8419               Elf_Internal_Rela *wrel;
8420               bfd_size_type sz;
8421
8422               /* Read toc relocs.  */
8423               relstart = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8424                                                     TRUE);
8425               if (relstart == NULL)
8426                 goto error_ret;
8427
8428               /* Remove unused toc relocs, and adjust those we keep.  */
8429               wrel = relstart;
8430               for (rel = relstart; rel < relstart + toc->reloc_count; ++rel)
8431                 if ((skip[rel->r_offset >> 3]
8432                      & (ref_from_discarded | can_optimize)) == 0)
8433                   {
8434                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8435                     wrel->r_info = rel->r_info;
8436                     wrel->r_addend = rel->r_addend;
8437                     ++wrel;
8438                   }
8439                 else if (!dec_dynrel_count (rel->r_info, toc, info,
8440                                             &local_syms, NULL, NULL))
8441                   goto error_ret;
8442
8443               toc->reloc_count = wrel - relstart;
8444               sz = elf_section_data (toc)->rel_hdr.sh_entsize;
8445               elf_section_data (toc)->rel_hdr.sh_size = toc->reloc_count * sz;
8446               BFD_ASSERT (elf_section_data (toc)->rel_hdr2 == NULL);
8447             }
8448         }
8449
8450       if (local_syms != NULL
8451           && symtab_hdr->contents != (unsigned char *) local_syms)
8452         {
8453           if (!info->keep_memory)
8454             free (local_syms);
8455           else
8456             symtab_hdr->contents = (unsigned char *) local_syms;
8457         }
8458       free (skip);
8459     }
8460
8461   return TRUE;
8462 }
8463
8464 /* Return true iff input section I references the TOC using
8465    instructions limited to +/-32k offsets.  */
8466
8467 bfd_boolean
8468 ppc64_elf_has_small_toc_reloc (asection *i)
8469 {
8470   return (is_ppc64_elf (i->owner)
8471           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8472 }
8473
8474 /* Allocate space for one GOT entry.  */
8475
8476 static void
8477 allocate_got (struct elf_link_hash_entry *h,
8478               struct bfd_link_info *info,
8479               struct got_entry *gent)
8480 {
8481   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8482   bfd_boolean dyn;
8483   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8484   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8485                  ? 16 : 8);
8486   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8487                   ? 2 : 1) * sizeof (Elf64_External_Rela);
8488   asection *got = ppc64_elf_tdata (gent->owner)->got;
8489
8490   gent->got.offset = got->size;
8491   got->size += entsize;
8492
8493   dyn = htab->elf.dynamic_sections_created;
8494   if ((info->shared
8495        || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8496             && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8497                 || h->root.type != bfd_link_hash_undefweak))
8498     {
8499       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8500       relgot->size += rentsize;
8501     }
8502   else if (h->type == STT_GNU_IFUNC)
8503     {
8504       asection *relgot = htab->reliplt;
8505       relgot->size += rentsize;
8506       htab->got_reli_size += rentsize;
8507     }
8508 }
8509
8510 /* This function merges got entries in the same toc group.  */
8511
8512 static void
8513 merge_got_entries (struct got_entry **pent)
8514 {
8515   struct got_entry *ent, *ent2;
8516
8517   for (ent = *pent; ent != NULL; ent = ent->next)
8518     if (!ent->is_indirect)
8519       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8520         if (!ent2->is_indirect
8521             && ent2->addend == ent->addend
8522             && ent2->tls_type == ent->tls_type
8523             && elf_gp (ent2->owner) == elf_gp (ent->owner))
8524           {
8525             ent2->is_indirect = TRUE;
8526             ent2->got.ent = ent;
8527           }
8528 }
8529
8530 /* Allocate space in .plt, .got and associated reloc sections for
8531    dynamic relocs.  */
8532
8533 static bfd_boolean
8534 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8535 {
8536   struct bfd_link_info *info;
8537   struct ppc_link_hash_table *htab;
8538   asection *s;
8539   struct ppc_link_hash_entry *eh;
8540   struct ppc_dyn_relocs *p;
8541   struct got_entry **pgent, *gent;
8542
8543   if (h->root.type == bfd_link_hash_indirect)
8544     return TRUE;
8545
8546   if (h->root.type == bfd_link_hash_warning)
8547     h = (struct elf_link_hash_entry *) h->root.u.i.link;
8548
8549   info = (struct bfd_link_info *) inf;
8550   htab = ppc_hash_table (info);
8551   if (htab == NULL)
8552     return FALSE;
8553
8554   if ((htab->elf.dynamic_sections_created
8555        && h->dynindx != -1
8556        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8557       || h->type == STT_GNU_IFUNC)
8558     {
8559       struct plt_entry *pent;
8560       bfd_boolean doneone = FALSE;
8561       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8562         if (pent->plt.refcount > 0)
8563           {
8564             if (!htab->elf.dynamic_sections_created
8565                 || h->dynindx == -1)
8566               {
8567                 s = htab->iplt;
8568                 pent->plt.offset = s->size;
8569                 s->size += PLT_ENTRY_SIZE;
8570                 s = htab->reliplt;
8571               }
8572             else
8573               {
8574                 /* If this is the first .plt entry, make room for the special
8575                    first entry.  */
8576                 s = htab->plt;
8577                 if (s->size == 0)
8578                   s->size += PLT_INITIAL_ENTRY_SIZE;
8579
8580                 pent->plt.offset = s->size;
8581
8582                 /* Make room for this entry.  */
8583                 s->size += PLT_ENTRY_SIZE;
8584
8585                 /* Make room for the .glink code.  */
8586                 s = htab->glink;
8587                 if (s->size == 0)
8588                   s->size += GLINK_CALL_STUB_SIZE;
8589                 /* We need bigger stubs past index 32767.  */
8590                 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8591                   s->size += 4;
8592                 s->size += 2*4;
8593
8594                 /* We also need to make an entry in the .rela.plt section.  */
8595                 s = htab->relplt;
8596               }
8597             s->size += sizeof (Elf64_External_Rela);
8598             doneone = TRUE;
8599           }
8600         else
8601           pent->plt.offset = (bfd_vma) -1;
8602       if (!doneone)
8603         {
8604           h->plt.plist = NULL;
8605           h->needs_plt = 0;
8606         }
8607     }
8608   else
8609     {
8610       h->plt.plist = NULL;
8611       h->needs_plt = 0;
8612     }
8613
8614   eh = (struct ppc_link_hash_entry *) h;
8615   /* Run through the TLS GD got entries first if we're changing them
8616      to TPREL.  */
8617   if ((eh->tls_mask & TLS_TPRELGD) != 0)
8618     for (gent = h->got.glist; gent != NULL; gent = gent->next)
8619       if (gent->got.refcount > 0
8620           && (gent->tls_type & TLS_GD) != 0)
8621         {
8622           /* This was a GD entry that has been converted to TPREL.  If
8623              there happens to be a TPREL entry we can use that one.  */
8624           struct got_entry *ent;
8625           for (ent = h->got.glist; ent != NULL; ent = ent->next)
8626             if (ent->got.refcount > 0
8627                 && (ent->tls_type & TLS_TPREL) != 0
8628                 && ent->addend == gent->addend
8629                 && ent->owner == gent->owner)
8630               {
8631                 gent->got.refcount = 0;
8632                 break;
8633               }
8634
8635           /* If not, then we'll be using our own TPREL entry.  */
8636           if (gent->got.refcount != 0)
8637             gent->tls_type = TLS_TLS | TLS_TPREL;
8638         }
8639
8640   /* Remove any list entry that won't generate a word in the GOT before
8641      we call merge_got_entries.  Otherwise we risk merging to empty
8642      entries.  */
8643   pgent = &h->got.glist;
8644   while ((gent = *pgent) != NULL)
8645     if (gent->got.refcount > 0)
8646       {
8647         if ((gent->tls_type & TLS_LD) != 0
8648             && !h->def_dynamic)
8649           {
8650             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8651             *pgent = gent->next;
8652           }
8653         else
8654           pgent = &gent->next;
8655       }
8656     else
8657       *pgent = gent->next;
8658
8659   if (!htab->do_multi_toc)
8660     merge_got_entries (&h->got.glist);
8661
8662   for (gent = h->got.glist; gent != NULL; gent = gent->next)
8663     if (!gent->is_indirect)
8664       {
8665         /* Make sure this symbol is output as a dynamic symbol.
8666            Undefined weak syms won't yet be marked as dynamic,
8667            nor will all TLS symbols.  */
8668         if (h->dynindx == -1
8669             && !h->forced_local
8670             && h->type != STT_GNU_IFUNC
8671             && htab->elf.dynamic_sections_created)
8672           {
8673             if (! bfd_elf_link_record_dynamic_symbol (info, h))
8674               return FALSE;
8675           }
8676
8677         if (!is_ppc64_elf (gent->owner))
8678           abort ();
8679
8680         allocate_got (h, info, gent);
8681       }
8682
8683   if (eh->dyn_relocs == NULL
8684       || (!htab->elf.dynamic_sections_created
8685           && h->type != STT_GNU_IFUNC))
8686     return TRUE;
8687
8688   /* In the shared -Bsymbolic case, discard space allocated for
8689      dynamic pc-relative relocs against symbols which turn out to be
8690      defined in regular objects.  For the normal shared case, discard
8691      space for relocs that have become local due to symbol visibility
8692      changes.  */
8693
8694   if (info->shared)
8695     {
8696       /* Relocs that use pc_count are those that appear on a call insn,
8697          or certain REL relocs (see must_be_dyn_reloc) that can be
8698          generated via assembly.  We want calls to protected symbols to
8699          resolve directly to the function rather than going via the plt.
8700          If people want function pointer comparisons to work as expected
8701          then they should avoid writing weird assembly.  */
8702       if (SYMBOL_CALLS_LOCAL (info, h))
8703         {
8704           struct ppc_dyn_relocs **pp;
8705
8706           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8707             {
8708               p->count -= p->pc_count;
8709               p->pc_count = 0;
8710               if (p->count == 0)
8711                 *pp = p->next;
8712               else
8713                 pp = &p->next;
8714             }
8715         }
8716
8717       /* Also discard relocs on undefined weak syms with non-default
8718          visibility.  */
8719       if (eh->dyn_relocs != NULL
8720           && h->root.type == bfd_link_hash_undefweak)
8721         {
8722           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8723             eh->dyn_relocs = NULL;
8724
8725           /* Make sure this symbol is output as a dynamic symbol.
8726              Undefined weak syms won't yet be marked as dynamic.  */
8727           else if (h->dynindx == -1
8728                    && !h->forced_local)
8729             {
8730               if (! bfd_elf_link_record_dynamic_symbol (info, h))
8731                 return FALSE;
8732             }
8733         }
8734     }
8735   else if (h->type == STT_GNU_IFUNC)
8736     {
8737       if (!h->non_got_ref)
8738         eh->dyn_relocs = NULL;
8739     }
8740   else if (ELIMINATE_COPY_RELOCS)
8741     {
8742       /* For the non-shared case, discard space for relocs against
8743          symbols which turn out to need copy relocs or are not
8744          dynamic.  */
8745
8746       if (!h->non_got_ref
8747           && !h->def_regular)
8748         {
8749           /* Make sure this symbol is output as a dynamic symbol.
8750              Undefined weak syms won't yet be marked as dynamic.  */
8751           if (h->dynindx == -1
8752               && !h->forced_local)
8753             {
8754               if (! bfd_elf_link_record_dynamic_symbol (info, h))
8755                 return FALSE;
8756             }
8757
8758           /* If that succeeded, we know we'll be keeping all the
8759              relocs.  */
8760           if (h->dynindx != -1)
8761             goto keep;
8762         }
8763
8764       eh->dyn_relocs = NULL;
8765
8766     keep: ;
8767     }
8768
8769   /* Finally, allocate space.  */
8770   for (p = eh->dyn_relocs; p != NULL; p = p->next)
8771     {
8772       asection *sreloc = elf_section_data (p->sec)->sreloc;
8773       if (!htab->elf.dynamic_sections_created)
8774         sreloc = htab->reliplt;
8775       sreloc->size += p->count * sizeof (Elf64_External_Rela);
8776     }
8777
8778   return TRUE;
8779 }
8780
8781 /* Find any dynamic relocs that apply to read-only sections.  */
8782
8783 static bfd_boolean
8784 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8785 {
8786   struct ppc_link_hash_entry *eh;
8787   struct ppc_dyn_relocs *p;
8788
8789   if (h->root.type == bfd_link_hash_warning)
8790     h = (struct elf_link_hash_entry *) h->root.u.i.link;
8791
8792   eh = (struct ppc_link_hash_entry *) h;
8793   for (p = eh->dyn_relocs; p != NULL; p = p->next)
8794     {
8795       asection *s = p->sec->output_section;
8796
8797       if (s != NULL && (s->flags & SEC_READONLY) != 0)
8798         {
8799           struct bfd_link_info *info = inf;
8800
8801           info->flags |= DF_TEXTREL;
8802
8803           /* Not an error, just cut short the traversal.  */
8804           return FALSE;
8805         }
8806     }
8807   return TRUE;
8808 }
8809
8810 /* Set the sizes of the dynamic sections.  */
8811
8812 static bfd_boolean
8813 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
8814                                  struct bfd_link_info *info)
8815 {
8816   struct ppc_link_hash_table *htab;
8817   bfd *dynobj;
8818   asection *s;
8819   bfd_boolean relocs;
8820   bfd *ibfd;
8821   struct got_entry *first_tlsld;
8822
8823   htab = ppc_hash_table (info);
8824   if (htab == NULL)
8825     return FALSE;
8826
8827   dynobj = htab->elf.dynobj;
8828   if (dynobj == NULL)
8829     abort ();
8830
8831   if (htab->elf.dynamic_sections_created)
8832     {
8833       /* Set the contents of the .interp section to the interpreter.  */
8834       if (info->executable)
8835         {
8836           s = bfd_get_section_by_name (dynobj, ".interp");
8837           if (s == NULL)
8838             abort ();
8839           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
8840           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
8841         }
8842     }
8843
8844   /* Set up .got offsets for local syms, and space for local dynamic
8845      relocs.  */
8846   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8847     {
8848       struct got_entry **lgot_ents;
8849       struct got_entry **end_lgot_ents;
8850       struct plt_entry **local_plt;
8851       struct plt_entry **end_local_plt;
8852       unsigned char *lgot_masks;
8853       bfd_size_type locsymcount;
8854       Elf_Internal_Shdr *symtab_hdr;
8855       asection *srel;
8856
8857       if (!is_ppc64_elf (ibfd))
8858         continue;
8859
8860       for (s = ibfd->sections; s != NULL; s = s->next)
8861         {
8862           struct ppc_dyn_relocs *p;
8863
8864           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
8865             {
8866               if (!bfd_is_abs_section (p->sec)
8867                   && bfd_is_abs_section (p->sec->output_section))
8868                 {
8869                   /* Input section has been discarded, either because
8870                      it is a copy of a linkonce section or due to
8871                      linker script /DISCARD/, so we'll be discarding
8872                      the relocs too.  */
8873                 }
8874               else if (p->count != 0)
8875                 {
8876                   srel = elf_section_data (p->sec)->sreloc;
8877                   if (!htab->elf.dynamic_sections_created)
8878                     srel = htab->reliplt;
8879                   srel->size += p->count * sizeof (Elf64_External_Rela);
8880                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
8881                     info->flags |= DF_TEXTREL;
8882                 }
8883             }
8884         }
8885
8886       lgot_ents = elf_local_got_ents (ibfd);
8887       if (!lgot_ents)
8888         continue;
8889
8890       symtab_hdr = &elf_symtab_hdr (ibfd);
8891       locsymcount = symtab_hdr->sh_info;
8892       end_lgot_ents = lgot_ents + locsymcount;
8893       local_plt = (struct plt_entry **) end_lgot_ents;
8894       end_local_plt = local_plt + locsymcount;
8895       lgot_masks = (unsigned char *) end_local_plt;
8896       s = ppc64_elf_tdata (ibfd)->got;
8897       srel = ppc64_elf_tdata (ibfd)->relgot;
8898       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
8899         {
8900           struct got_entry **pent, *ent;
8901
8902           pent = lgot_ents;
8903           while ((ent = *pent) != NULL)
8904             if (ent->got.refcount > 0)
8905               {
8906                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
8907                   {
8908                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
8909                     *pent = ent->next;
8910                   }
8911                 else
8912                   {
8913                     unsigned int num = 1;
8914                     ent->got.offset = s->size;
8915                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
8916                       num = 2;
8917                     s->size += num * 8;
8918                     if (info->shared)
8919                       srel->size += num * sizeof (Elf64_External_Rela);
8920                     else if ((*lgot_masks & PLT_IFUNC) != 0)
8921                       {
8922                         htab->reliplt->size
8923                           += num * sizeof (Elf64_External_Rela);
8924                         htab->got_reli_size
8925                           += num * sizeof (Elf64_External_Rela);
8926                       }
8927                     pent = &ent->next;
8928                   }
8929               }
8930             else
8931               *pent = ent->next;
8932         }
8933
8934       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
8935       for (; local_plt < end_local_plt; ++local_plt)
8936         {
8937           struct plt_entry *ent;
8938
8939           for (ent = *local_plt; ent != NULL; ent = ent->next)
8940             if (ent->plt.refcount > 0)
8941               {
8942                 s = htab->iplt;
8943                 ent->plt.offset = s->size;
8944                 s->size += PLT_ENTRY_SIZE;
8945
8946                 htab->reliplt->size += sizeof (Elf64_External_Rela);
8947               }
8948             else
8949               ent->plt.offset = (bfd_vma) -1;
8950         }
8951     }
8952
8953   /* Allocate global sym .plt and .got entries, and space for global
8954      sym dynamic relocs.  */
8955   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
8956
8957   first_tlsld = NULL;
8958   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8959     {
8960       struct got_entry *ent;
8961
8962       if (!is_ppc64_elf (ibfd))
8963         continue;
8964
8965       ent = ppc64_tlsld_got (ibfd);
8966       if (ent->got.refcount > 0)
8967         {
8968           if (!htab->do_multi_toc && first_tlsld != NULL)
8969             {
8970               ent->is_indirect = TRUE;
8971               ent->got.ent = first_tlsld;
8972             }
8973           else
8974             {
8975               if (first_tlsld == NULL)
8976                 first_tlsld = ent;
8977               s = ppc64_elf_tdata (ibfd)->got;
8978               ent->got.offset = s->size;
8979               ent->owner = ibfd;
8980               s->size += 16;
8981               if (info->shared)
8982                 {
8983                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
8984                   srel->size += sizeof (Elf64_External_Rela);
8985                 }
8986             }
8987         }
8988       else
8989         ent->got.offset = (bfd_vma) -1;
8990     }
8991
8992   /* We now have determined the sizes of the various dynamic sections.
8993      Allocate memory for them.  */
8994   relocs = FALSE;
8995   for (s = dynobj->sections; s != NULL; s = s->next)
8996     {
8997       if ((s->flags & SEC_LINKER_CREATED) == 0)
8998         continue;
8999
9000       if (s == htab->brlt || s == htab->relbrlt)
9001         /* These haven't been allocated yet;  don't strip.  */
9002         continue;
9003       else if (s == htab->got
9004                || s == htab->plt
9005                || s == htab->iplt
9006                || s == htab->glink
9007                || s == htab->dynbss)
9008         {
9009           /* Strip this section if we don't need it; see the
9010              comment below.  */
9011         }
9012       else if (CONST_STRNEQ (s->name, ".rela"))
9013         {
9014           if (s->size != 0)
9015             {
9016               if (s != htab->relplt)
9017                 relocs = TRUE;
9018
9019               /* We use the reloc_count field as a counter if we need
9020                  to copy relocs into the output file.  */
9021               s->reloc_count = 0;
9022             }
9023         }
9024       else
9025         {
9026           /* It's not one of our sections, so don't allocate space.  */
9027           continue;
9028         }
9029
9030       if (s->size == 0)
9031         {
9032           /* If we don't need this section, strip it from the
9033              output file.  This is mostly to handle .rela.bss and
9034              .rela.plt.  We must create both sections in
9035              create_dynamic_sections, because they must be created
9036              before the linker maps input sections to output
9037              sections.  The linker does that before
9038              adjust_dynamic_symbol is called, and it is that
9039              function which decides whether anything needs to go
9040              into these sections.  */
9041           s->flags |= SEC_EXCLUDE;
9042           continue;
9043         }
9044
9045       if ((s->flags & SEC_HAS_CONTENTS) == 0)
9046         continue;
9047
9048       /* Allocate memory for the section contents.  We use bfd_zalloc
9049          here in case unused entries are not reclaimed before the
9050          section's contents are written out.  This should not happen,
9051          but this way if it does we get a R_PPC64_NONE reloc in .rela
9052          sections instead of garbage.
9053          We also rely on the section contents being zero when writing
9054          the GOT.  */
9055       s->contents = bfd_zalloc (dynobj, s->size);
9056       if (s->contents == NULL)
9057         return FALSE;
9058     }
9059
9060   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9061     {
9062       if (!is_ppc64_elf (ibfd))
9063         continue;
9064
9065       s = ppc64_elf_tdata (ibfd)->got;
9066       if (s != NULL && s != htab->got)
9067         {
9068           if (s->size == 0)
9069             s->flags |= SEC_EXCLUDE;
9070           else
9071             {
9072               s->contents = bfd_zalloc (ibfd, s->size);
9073               if (s->contents == NULL)
9074                 return FALSE;
9075             }
9076         }
9077       s = ppc64_elf_tdata (ibfd)->relgot;
9078       if (s != NULL)
9079         {
9080           if (s->size == 0)
9081             s->flags |= SEC_EXCLUDE;
9082           else
9083             {
9084               s->contents = bfd_zalloc (ibfd, s->size);
9085               if (s->contents == NULL)
9086                 return FALSE;
9087               relocs = TRUE;
9088               s->reloc_count = 0;
9089             }
9090         }
9091     }
9092
9093   if (htab->elf.dynamic_sections_created)
9094     {
9095       /* Add some entries to the .dynamic section.  We fill in the
9096          values later, in ppc64_elf_finish_dynamic_sections, but we
9097          must add the entries now so that we get the correct size for
9098          the .dynamic section.  The DT_DEBUG entry is filled in by the
9099          dynamic linker and used by the debugger.  */
9100 #define add_dynamic_entry(TAG, VAL) \
9101   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9102
9103       if (info->executable)
9104         {
9105           if (!add_dynamic_entry (DT_DEBUG, 0))
9106             return FALSE;
9107         }
9108
9109       if (htab->plt != NULL && htab->plt->size != 0)
9110         {
9111           if (!add_dynamic_entry (DT_PLTGOT, 0)
9112               || !add_dynamic_entry (DT_PLTRELSZ, 0)
9113               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9114               || !add_dynamic_entry (DT_JMPREL, 0)
9115               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9116             return FALSE;
9117         }
9118
9119       if (NO_OPD_RELOCS)
9120         {
9121           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9122               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9123             return FALSE;
9124         }
9125
9126       if (!htab->no_tls_get_addr_opt
9127           && htab->tls_get_addr_fd != NULL
9128           && htab->tls_get_addr_fd->elf.plt.plist != NULL
9129           && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9130         return FALSE;
9131
9132       if (relocs)
9133         {
9134           if (!add_dynamic_entry (DT_RELA, 0)
9135               || !add_dynamic_entry (DT_RELASZ, 0)
9136               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9137             return FALSE;
9138
9139           /* If any dynamic relocs apply to a read-only section,
9140              then we need a DT_TEXTREL entry.  */
9141           if ((info->flags & DF_TEXTREL) == 0)
9142             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
9143
9144           if ((info->flags & DF_TEXTREL) != 0)
9145             {
9146               if (!add_dynamic_entry (DT_TEXTREL, 0))
9147                 return FALSE;
9148             }
9149         }
9150     }
9151 #undef add_dynamic_entry
9152
9153   return TRUE;
9154 }
9155
9156 /* Determine the type of stub needed, if any, for a call.  */
9157
9158 static inline enum ppc_stub_type
9159 ppc_type_of_stub (asection *input_sec,
9160                   const Elf_Internal_Rela *rel,
9161                   struct ppc_link_hash_entry **hash,
9162                   struct plt_entry **plt_ent,
9163                   bfd_vma destination)
9164 {
9165   struct ppc_link_hash_entry *h = *hash;
9166   bfd_vma location;
9167   bfd_vma branch_offset;
9168   bfd_vma max_branch_offset;
9169   enum elf_ppc64_reloc_type r_type;
9170
9171   if (h != NULL)
9172     {
9173       struct plt_entry *ent;
9174       struct ppc_link_hash_entry *fdh = h;
9175       if (h->oh != NULL
9176           && h->oh->is_func_descriptor)
9177         {
9178           fdh = ppc_follow_link (h->oh);
9179           *hash = fdh;
9180         }
9181
9182       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9183         if (ent->addend == rel->r_addend
9184             && ent->plt.offset != (bfd_vma) -1)
9185           {
9186             *plt_ent = ent;
9187             return ppc_stub_plt_call;
9188           }
9189
9190       /* Here, we know we don't have a plt entry.  If we don't have a
9191          either a defined function descriptor or a defined entry symbol
9192          in a regular object file, then it is pointless trying to make
9193          any other type of stub.  */
9194       if (!is_static_defined (&fdh->elf)
9195           && !is_static_defined (&h->elf))
9196         return ppc_stub_none;
9197     }
9198   else if (elf_local_got_ents (input_sec->owner) != NULL)
9199     {
9200       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9201       struct plt_entry **local_plt = (struct plt_entry **)
9202         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9203       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9204
9205       if (local_plt[r_symndx] != NULL)
9206         {
9207           struct plt_entry *ent;
9208
9209           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9210             if (ent->addend == rel->r_addend
9211                 && ent->plt.offset != (bfd_vma) -1)
9212               {
9213                 *plt_ent = ent;
9214                 return ppc_stub_plt_call;
9215               }
9216         }
9217     }
9218
9219   /* Determine where the call point is.  */
9220   location = (input_sec->output_offset
9221               + input_sec->output_section->vma
9222               + rel->r_offset);
9223
9224   branch_offset = destination - location;
9225   r_type = ELF64_R_TYPE (rel->r_info);
9226
9227   /* Determine if a long branch stub is needed.  */
9228   max_branch_offset = 1 << 25;
9229   if (r_type != R_PPC64_REL24)
9230     max_branch_offset = 1 << 15;
9231
9232   if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9233     /* We need a stub.  Figure out whether a long_branch or plt_branch
9234        is needed later.  */
9235     return ppc_stub_long_branch;
9236
9237   return ppc_stub_none;
9238 }
9239
9240 /* Build a .plt call stub.  */
9241
9242 static inline bfd_byte *
9243 build_plt_stub (bfd *obfd, bfd_byte *p, int offset, Elf_Internal_Rela *r)
9244 {
9245 #define PPC_LO(v) ((v) & 0xffff)
9246 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9247 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9248
9249   if (PPC_HA (offset) != 0)
9250     {
9251       if (r != NULL)
9252         {
9253           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9254           r[1].r_offset = r[0].r_offset + 8;
9255           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9256           r[1].r_addend = r[0].r_addend;
9257           if (PPC_HA (offset + 16) != PPC_HA (offset))
9258             {
9259               r[2].r_offset = r[1].r_offset + 4;
9260               r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9261               r[2].r_addend = r[0].r_addend;
9262             }
9263           else
9264             {
9265               r[2].r_offset = r[1].r_offset + 8;
9266               r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9267               r[2].r_addend = r[0].r_addend + 8;
9268               r[3].r_offset = r[2].r_offset + 4;
9269               r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9270               r[3].r_addend = r[0].r_addend + 16;
9271             }
9272         }
9273       bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p),     p += 4;
9274       bfd_put_32 (obfd, STD_R2_40R1, p),                        p += 4;
9275       bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),      p += 4;
9276       if (PPC_HA (offset + 16) != PPC_HA (offset))
9277         {
9278           bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9279           offset = 0;
9280         }
9281       bfd_put_32 (obfd, MTCTR_R11, p),                          p += 4;
9282       bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p),   p += 4;
9283       bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
9284       bfd_put_32 (obfd, BCTR, p),                               p += 4;
9285     }
9286   else
9287     {
9288       if (r != NULL)
9289         {
9290           r[0].r_offset += 4;
9291           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9292           if (PPC_HA (offset + 16) != PPC_HA (offset))
9293             {
9294               r[1].r_offset = r[0].r_offset + 4;
9295               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9296               r[1].r_addend = r[0].r_addend;
9297             }
9298           else
9299             {
9300               r[1].r_offset = r[0].r_offset + 8;
9301               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9302               r[1].r_addend = r[0].r_addend + 16;
9303               r[2].r_offset = r[1].r_offset + 4;
9304               r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9305               r[2].r_addend = r[0].r_addend + 8;
9306             }
9307         }
9308       bfd_put_32 (obfd, STD_R2_40R1, p),                        p += 4;
9309       bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p),       p += 4;
9310       if (PPC_HA (offset + 16) != PPC_HA (offset))
9311         {
9312           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
9313           offset = 0;
9314         }
9315       bfd_put_32 (obfd, MTCTR_R11, p),                          p += 4;
9316       bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p),  p += 4;
9317       bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p),    p += 4;
9318       bfd_put_32 (obfd, BCTR, p),                               p += 4;
9319     }
9320   return p;
9321 }
9322
9323 /* Build a special .plt call stub for __tls_get_addr.  */
9324
9325 #define LD_R11_0R3      0xe9630000
9326 #define LD_R12_0R3      0xe9830000
9327 #define MR_R0_R3        0x7c601b78
9328 #define CMPDI_R11_0     0x2c2b0000
9329 #define ADD_R3_R12_R13  0x7c6c6a14
9330 #define BEQLR           0x4d820020
9331 #define MR_R3_R0        0x7c030378
9332 #define MFLR_R11        0x7d6802a6
9333 #define STD_R11_0R1     0xf9610000
9334 #define BCTRL           0x4e800421
9335 #define LD_R11_0R1      0xe9610000
9336 #define LD_R2_0R1       0xe8410000
9337 #define MTLR_R11        0x7d6803a6
9338
9339 static inline bfd_byte *
9340 build_tls_get_addr_stub (bfd *obfd, bfd_byte *p, int offset,
9341                          Elf_Internal_Rela *r)
9342 {
9343   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
9344   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
9345   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
9346   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
9347   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
9348   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
9349   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
9350   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
9351   bfd_put_32 (obfd, STD_R11_0R1 + 32, p),       p += 4;
9352
9353   if (r != NULL)
9354     r[0].r_offset += 9 * 4;
9355   p = build_plt_stub (obfd, p, offset, r);
9356   bfd_put_32 (obfd, BCTRL, p - 4);
9357
9358   bfd_put_32 (obfd, LD_R11_0R1 + 32, p),        p += 4;
9359   bfd_put_32 (obfd, LD_R2_0R1 + 40, p),         p += 4;
9360   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
9361   bfd_put_32 (obfd, BLR, p),                    p += 4;
9362
9363   return p;
9364 }
9365
9366 static Elf_Internal_Rela *
9367 get_relocs (asection *sec, int count)
9368 {
9369   Elf_Internal_Rela *relocs;
9370   struct bfd_elf_section_data *elfsec_data;
9371
9372   elfsec_data = elf_section_data (sec);
9373   relocs = elfsec_data->relocs;
9374   if (relocs == NULL)
9375     {
9376       bfd_size_type relsize;
9377       relsize = sec->reloc_count * sizeof (*relocs);
9378       relocs = bfd_alloc (sec->owner, relsize);
9379       if (relocs == NULL)
9380         return NULL;
9381       elfsec_data->relocs = relocs;
9382       elfsec_data->rel_hdr.sh_size = (sec->reloc_count
9383                                       * sizeof (Elf64_External_Rela));
9384       elfsec_data->rel_hdr.sh_entsize = sizeof (Elf64_External_Rela);
9385       sec->reloc_count = 0;
9386     }
9387   relocs += sec->reloc_count;
9388   sec->reloc_count += count;
9389   return relocs;
9390 }
9391
9392 static bfd_boolean
9393 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9394 {
9395   struct ppc_stub_hash_entry *stub_entry;
9396   struct ppc_branch_hash_entry *br_entry;
9397   struct bfd_link_info *info;
9398   struct ppc_link_hash_table *htab;
9399   bfd_byte *loc;
9400   bfd_byte *p;
9401   bfd_vma dest, off;
9402   int size;
9403   Elf_Internal_Rela *r;
9404   asection *plt;
9405
9406   /* Massage our args to the form they really have.  */
9407   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9408   info = in_arg;
9409
9410   htab = ppc_hash_table (info);
9411   if (htab == NULL)
9412     return FALSE;
9413
9414   /* Make a note of the offset within the stubs for this entry.  */
9415   stub_entry->stub_offset = stub_entry->stub_sec->size;
9416   loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
9417
9418   htab->stub_count[stub_entry->stub_type - 1] += 1;
9419   switch (stub_entry->stub_type)
9420     {
9421     case ppc_stub_long_branch:
9422     case ppc_stub_long_branch_r2off:
9423       /* Branches are relative.  This is where we are going to.  */
9424       off = dest = (stub_entry->target_value
9425                     + stub_entry->target_section->output_offset
9426                     + stub_entry->target_section->output_section->vma);
9427
9428       /* And this is where we are coming from.  */
9429       off -= (stub_entry->stub_offset
9430               + stub_entry->stub_sec->output_offset
9431               + stub_entry->stub_sec->output_section->vma);
9432
9433       size = 4;
9434       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9435         {
9436           bfd_vma r2off;
9437
9438           r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
9439                    - htab->stub_group[stub_entry->id_sec->id].toc_off);
9440           bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9441           loc += 4;
9442           size = 12;
9443           if (PPC_HA (r2off) != 0)
9444             {
9445               size = 16;
9446               bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9447               loc += 4;
9448             }
9449           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9450           loc += 4;
9451           off -= size - 4;
9452         }
9453       bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
9454
9455       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9456         {
9457           (*_bfd_error_handler) (_("long branch stub `%s' offset overflow"),
9458                                  stub_entry->root.string);
9459           htab->stub_error = TRUE;
9460           return FALSE;
9461         }
9462
9463       if (info->emitrelocations)
9464         {
9465           r = get_relocs (stub_entry->stub_sec, 1);
9466           if (r == NULL)
9467             return FALSE;
9468           r->r_offset = loc - stub_entry->stub_sec->contents;
9469           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9470           r->r_addend = dest;
9471           if (stub_entry->h != NULL)
9472             {
9473               struct elf_link_hash_entry **hashes;
9474               unsigned long symndx;
9475               struct ppc_link_hash_entry *h;
9476
9477               hashes = elf_sym_hashes (htab->stub_bfd);
9478               if (hashes == NULL)
9479                 {
9480                   bfd_size_type hsize;
9481
9482                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9483                   hashes = bfd_zalloc (htab->stub_bfd, hsize);
9484                   if (hashes == NULL)
9485                     return FALSE;
9486                   elf_sym_hashes (htab->stub_bfd) = hashes;
9487                   htab->stub_globals = 1;
9488                 }
9489               symndx = htab->stub_globals++;
9490               h = stub_entry->h;
9491               hashes[symndx] = &h->elf;
9492               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9493               if (h->oh != NULL && h->oh->is_func)
9494                 h = ppc_follow_link (h->oh);
9495               if (h->elf.root.u.def.section != stub_entry->target_section)
9496                 /* H is an opd symbol.  The addend must be zero.  */
9497                 r->r_addend = 0;
9498               else
9499                 {
9500                   off = (h->elf.root.u.def.value
9501                          + h->elf.root.u.def.section->output_offset
9502                          + h->elf.root.u.def.section->output_section->vma);
9503                   r->r_addend -= off;
9504                 }
9505             }
9506         }
9507       break;
9508
9509     case ppc_stub_plt_branch:
9510     case ppc_stub_plt_branch_r2off:
9511       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9512                                          stub_entry->root.string + 9,
9513                                          FALSE, FALSE);
9514       if (br_entry == NULL)
9515         {
9516           (*_bfd_error_handler) (_("can't find branch stub `%s'"),
9517                                  stub_entry->root.string);
9518           htab->stub_error = TRUE;
9519           return FALSE;
9520         }
9521
9522       dest = (stub_entry->target_value
9523               + stub_entry->target_section->output_offset
9524               + stub_entry->target_section->output_section->vma);
9525
9526       bfd_put_64 (htab->brlt->owner, dest,
9527                   htab->brlt->contents + br_entry->offset);
9528
9529       if (br_entry->iter == htab->stub_iteration)
9530         {
9531           br_entry->iter = 0;
9532
9533           if (htab->relbrlt != NULL)
9534             {
9535               /* Create a reloc for the branch lookup table entry.  */
9536               Elf_Internal_Rela rela;
9537               bfd_byte *rl;
9538
9539               rela.r_offset = (br_entry->offset
9540                                + htab->brlt->output_offset
9541                                + htab->brlt->output_section->vma);
9542               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9543               rela.r_addend = dest;
9544
9545               rl = htab->relbrlt->contents;
9546               rl += (htab->relbrlt->reloc_count++
9547                      * sizeof (Elf64_External_Rela));
9548               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9549             }
9550           else if (info->emitrelocations)
9551             {
9552               r = get_relocs (htab->brlt, 1);
9553               if (r == NULL)
9554                 return FALSE;
9555               /* brlt, being SEC_LINKER_CREATED does not go through the
9556                  normal reloc processing.  Symbols and offsets are not
9557                  translated from input file to output file form, so
9558                  set up the offset per the output file.  */
9559               r->r_offset = (br_entry->offset
9560                              + htab->brlt->output_offset
9561                              + htab->brlt->output_section->vma);
9562               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9563               r->r_addend = dest;
9564             }
9565         }
9566
9567       dest = (br_entry->offset
9568               + htab->brlt->output_offset
9569               + htab->brlt->output_section->vma);
9570
9571       off = (dest
9572              - elf_gp (htab->brlt->output_section->owner)
9573              - htab->stub_group[stub_entry->id_sec->id].toc_off);
9574
9575       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
9576         {
9577           (*_bfd_error_handler)
9578             (_("linkage table error against `%s'"),
9579              stub_entry->root.string);
9580           bfd_set_error (bfd_error_bad_value);
9581           htab->stub_error = TRUE;
9582           return FALSE;
9583         }
9584
9585       if (info->emitrelocations)
9586         {
9587           r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
9588           if (r == NULL)
9589             return FALSE;
9590           r[0].r_offset = loc - stub_entry->stub_sec->contents;
9591           if (bfd_big_endian (info->output_bfd))
9592             r[0].r_offset += 2;
9593           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
9594             r[0].r_offset += 4;
9595           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9596           r[0].r_addend = dest;
9597           if (PPC_HA (off) != 0)
9598             {
9599               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9600               r[1].r_offset = r[0].r_offset + 4;
9601               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9602               r[1].r_addend = r[0].r_addend;
9603             }
9604         }
9605
9606       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9607         {
9608           if (PPC_HA (off) != 0)
9609             {
9610               size = 16;
9611               bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
9612               loc += 4;
9613               bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
9614             }
9615           else
9616             {
9617               size = 12;
9618               bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
9619             }
9620         }
9621       else
9622         {
9623           bfd_vma r2off;
9624
9625           r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
9626                    - htab->stub_group[stub_entry->id_sec->id].toc_off);
9627           bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9628           loc += 4;
9629           size = 20;
9630           if (PPC_HA (off) != 0)
9631             {
9632               size += 4;
9633               bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
9634               loc += 4;
9635               bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
9636               loc += 4;
9637             }
9638           else
9639             {
9640               bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
9641               loc += 4;
9642             }
9643
9644           if (PPC_HA (r2off) != 0)
9645             {
9646               size += 4;
9647               bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9648               loc += 4;
9649             }
9650           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9651         }
9652       loc += 4;
9653       bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
9654       loc += 4;
9655       bfd_put_32 (htab->stub_bfd, BCTR, loc);
9656       break;
9657
9658     case ppc_stub_plt_call:
9659       if (stub_entry->h != NULL
9660           && stub_entry->h->is_func_descriptor
9661           && stub_entry->h->oh != NULL)
9662         {
9663           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
9664
9665           /* If the old-ABI "dot-symbol" is undefined make it weak so
9666              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
9667              FIXME: We used to define the symbol on one of the call
9668              stubs instead, which is why we test symbol section id
9669              against htab->top_id in various places.  Likely all
9670              these checks could now disappear.  */
9671           if (fh->elf.root.type == bfd_link_hash_undefined)
9672             fh->elf.root.type = bfd_link_hash_undefweak;
9673         }
9674
9675       /* Now build the stub.  */
9676       dest = stub_entry->plt_ent->plt.offset & ~1;
9677       if (dest >= (bfd_vma) -2)
9678         abort ();
9679
9680       plt = htab->plt;
9681       if (!htab->elf.dynamic_sections_created
9682           || stub_entry->h == NULL
9683           || stub_entry->h->elf.dynindx == -1)
9684         plt = htab->iplt;
9685
9686       dest += plt->output_offset + plt->output_section->vma;
9687
9688       if (stub_entry->h == NULL
9689           && (stub_entry->plt_ent->plt.offset & 1) == 0)
9690         {
9691           Elf_Internal_Rela rela;
9692           bfd_byte *rl;
9693
9694           rela.r_offset = dest;
9695           rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
9696           rela.r_addend = (stub_entry->target_value
9697                            + stub_entry->target_section->output_offset
9698                            + stub_entry->target_section->output_section->vma);
9699
9700           rl = (htab->reliplt->contents
9701                 + (htab->reliplt->reloc_count++
9702                    * sizeof (Elf64_External_Rela)));
9703           bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
9704           stub_entry->plt_ent->plt.offset |= 1;
9705         }
9706
9707       off = (dest
9708              - elf_gp (plt->output_section->owner)
9709              - htab->stub_group[stub_entry->id_sec->id].toc_off);
9710
9711       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
9712         {
9713           (*_bfd_error_handler)
9714             (_("linkage table error against `%s'"),
9715              stub_entry->h != NULL
9716              ? stub_entry->h->elf.root.root.string
9717              : "<local sym>");
9718           bfd_set_error (bfd_error_bad_value);
9719           htab->stub_error = TRUE;
9720           return FALSE;
9721         }
9722
9723       r = NULL;
9724       if (info->emitrelocations)
9725         {
9726           r = get_relocs (stub_entry->stub_sec,
9727                           (2 + (PPC_HA (off) != 0)
9728                            + (PPC_HA (off + 16) == PPC_HA (off))));
9729           if (r == NULL)
9730             return FALSE;
9731           r[0].r_offset = loc - stub_entry->stub_sec->contents;
9732           if (bfd_big_endian (info->output_bfd))
9733             r[0].r_offset += 2;
9734           r[0].r_addend = dest;
9735         }
9736       if (stub_entry->h != NULL
9737           && (stub_entry->h == htab->tls_get_addr_fd
9738               || stub_entry->h == htab->tls_get_addr)
9739           && !htab->no_tls_get_addr_opt)
9740         p = build_tls_get_addr_stub (htab->stub_bfd, loc, off, r);
9741       else
9742         p = build_plt_stub (htab->stub_bfd, loc, off, r);
9743       size = p - loc;
9744       break;
9745
9746     default:
9747       BFD_FAIL ();
9748       return FALSE;
9749     }
9750
9751   stub_entry->stub_sec->size += size;
9752
9753   if (htab->emit_stub_syms)
9754     {
9755       struct elf_link_hash_entry *h;
9756       size_t len1, len2;
9757       char *name;
9758       const char *const stub_str[] = { "long_branch",
9759                                        "long_branch_r2off",
9760                                        "plt_branch",
9761                                        "plt_branch_r2off",
9762                                        "plt_call" };
9763
9764       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
9765       len2 = strlen (stub_entry->root.string);
9766       name = bfd_malloc (len1 + len2 + 2);
9767       if (name == NULL)
9768         return FALSE;
9769       memcpy (name, stub_entry->root.string, 9);
9770       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
9771       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
9772       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
9773       if (h == NULL)
9774         return FALSE;
9775       if (h->root.type == bfd_link_hash_new)
9776         {
9777           h->root.type = bfd_link_hash_defined;
9778           h->root.u.def.section = stub_entry->stub_sec;
9779           h->root.u.def.value = stub_entry->stub_offset;
9780           h->ref_regular = 1;
9781           h->def_regular = 1;
9782           h->ref_regular_nonweak = 1;
9783           h->forced_local = 1;
9784           h->non_elf = 0;
9785         }
9786     }
9787
9788   return TRUE;
9789 }
9790
9791 /* As above, but don't actually build the stub.  Just bump offset so
9792    we know stub section sizes, and select plt_branch stubs where
9793    long_branch stubs won't do.  */
9794
9795 static bfd_boolean
9796 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9797 {
9798   struct ppc_stub_hash_entry *stub_entry;
9799   struct bfd_link_info *info;
9800   struct ppc_link_hash_table *htab;
9801   bfd_vma off;
9802   int size;
9803
9804   /* Massage our args to the form they really have.  */
9805   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9806   info = in_arg;
9807
9808   htab = ppc_hash_table (info);
9809   if (htab == NULL)
9810     return FALSE;
9811
9812   if (stub_entry->stub_type == ppc_stub_plt_call)
9813     {
9814       asection *plt;
9815       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
9816       if (off >= (bfd_vma) -2)
9817         abort ();
9818       plt = htab->plt;
9819       if (!htab->elf.dynamic_sections_created
9820           || stub_entry->h == NULL
9821           || stub_entry->h->elf.dynindx == -1)
9822         plt = htab->iplt;
9823       off += (plt->output_offset
9824               + plt->output_section->vma
9825               - elf_gp (plt->output_section->owner)
9826               - htab->stub_group[stub_entry->id_sec->id].toc_off);
9827
9828       size = PLT_CALL_STUB_SIZE;
9829       if (PPC_HA (off) == 0)
9830         size -= 4;
9831       if (PPC_HA (off + 16) != PPC_HA (off))
9832         size += 4;
9833       if (stub_entry->h != NULL
9834           && (stub_entry->h == htab->tls_get_addr_fd
9835               || stub_entry->h == htab->tls_get_addr)
9836           && !htab->no_tls_get_addr_opt)
9837         size += 13 * 4;
9838       if (info->emitrelocations)
9839         {
9840           stub_entry->stub_sec->reloc_count
9841             += 2 + (PPC_HA (off) != 0) + (PPC_HA (off + 16) == PPC_HA (off));
9842           stub_entry->stub_sec->flags |= SEC_RELOC;
9843         }
9844     }
9845   else
9846     {
9847       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
9848          variants.  */
9849       bfd_vma r2off = 0;
9850
9851       off = (stub_entry->target_value
9852              + stub_entry->target_section->output_offset
9853              + stub_entry->target_section->output_section->vma);
9854       off -= (stub_entry->stub_sec->size
9855               + stub_entry->stub_sec->output_offset
9856               + stub_entry->stub_sec->output_section->vma);
9857
9858       /* Reset the stub type from the plt variant in case we now
9859          can reach with a shorter stub.  */
9860       if (stub_entry->stub_type >= ppc_stub_plt_branch)
9861         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
9862
9863       size = 4;
9864       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9865         {
9866           r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
9867                    - htab->stub_group[stub_entry->id_sec->id].toc_off);
9868           size = 12;
9869           if (PPC_HA (r2off) != 0)
9870             size = 16;
9871           off -= size - 4;
9872         }
9873
9874       /* If the branch offset if too big, use a ppc_stub_plt_branch.  */
9875       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9876         {
9877           struct ppc_branch_hash_entry *br_entry;
9878
9879           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9880                                              stub_entry->root.string + 9,
9881                                              TRUE, FALSE);
9882           if (br_entry == NULL)
9883             {
9884               (*_bfd_error_handler) (_("can't build branch stub `%s'"),
9885                                      stub_entry->root.string);
9886               htab->stub_error = TRUE;
9887               return FALSE;
9888             }
9889
9890           if (br_entry->iter != htab->stub_iteration)
9891             {
9892               br_entry->iter = htab->stub_iteration;
9893               br_entry->offset = htab->brlt->size;
9894               htab->brlt->size += 8;
9895
9896               if (htab->relbrlt != NULL)
9897                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
9898               else if (info->emitrelocations)
9899                 {
9900                   htab->brlt->reloc_count += 1;
9901                   htab->brlt->flags |= SEC_RELOC;
9902                 }
9903             }
9904
9905           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
9906           off = (br_entry->offset
9907                  + htab->brlt->output_offset
9908                  + htab->brlt->output_section->vma
9909                  - elf_gp (htab->brlt->output_section->owner)
9910                  - htab->stub_group[stub_entry->id_sec->id].toc_off);
9911
9912           if (info->emitrelocations)
9913             {
9914               stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
9915               stub_entry->stub_sec->flags |= SEC_RELOC;
9916             }
9917
9918           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9919             {
9920               size = 12;
9921               if (PPC_HA (off) != 0)
9922                 size = 16;
9923             }
9924           else
9925             {
9926               size = 20;
9927               if (PPC_HA (off) != 0)
9928                 size += 4;
9929
9930               if (PPC_HA (r2off) != 0)
9931                 size += 4;
9932             }
9933         }
9934       else if (info->emitrelocations)
9935         {
9936           stub_entry->stub_sec->reloc_count += 1;
9937           stub_entry->stub_sec->flags |= SEC_RELOC;
9938         }
9939     }
9940
9941   stub_entry->stub_sec->size += size;
9942   return TRUE;
9943 }
9944
9945 /* Set up various things so that we can make a list of input sections
9946    for each output section included in the link.  Returns -1 on error,
9947    0 when no stubs will be needed, and 1 on success.  */
9948
9949 int
9950 ppc64_elf_setup_section_lists
9951   (struct bfd_link_info *info,
9952    asection *(*add_stub_section) (const char *, asection *),
9953    void (*layout_sections_again) (void))
9954 {
9955   bfd *input_bfd;
9956   int top_id, top_index, id;
9957   asection *section;
9958   asection **input_list;
9959   bfd_size_type amt;
9960   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9961
9962   if (htab == NULL)
9963     return -1;
9964   /* Stash our params away.  */
9965   htab->add_stub_section = add_stub_section;
9966   htab->layout_sections_again = layout_sections_again;
9967
9968   if (htab->brlt == NULL)
9969     return 0;
9970
9971   /* Find the top input section id.  */
9972   for (input_bfd = info->input_bfds, top_id = 3;
9973        input_bfd != NULL;
9974        input_bfd = input_bfd->link_next)
9975     {
9976       for (section = input_bfd->sections;
9977            section != NULL;
9978            section = section->next)
9979         {
9980           if (top_id < section->id)
9981             top_id = section->id;
9982         }
9983     }
9984
9985   htab->top_id = top_id;
9986   amt = sizeof (struct map_stub) * (top_id + 1);
9987   htab->stub_group = bfd_zmalloc (amt);
9988   if (htab->stub_group == NULL)
9989     return -1;
9990
9991   /* Set toc_off for com, und, abs and ind sections.  */
9992   for (id = 0; id < 3; id++)
9993     htab->stub_group[id].toc_off = TOC_BASE_OFF;
9994
9995   /* We can't use output_bfd->section_count here to find the top output
9996      section index as some sections may have been removed, and
9997      strip_excluded_output_sections doesn't renumber the indices.  */
9998   for (section = info->output_bfd->sections, top_index = 0;
9999        section != NULL;
10000        section = section->next)
10001     {
10002       if (top_index < section->index)
10003         top_index = section->index;
10004     }
10005
10006   htab->top_index = top_index;
10007   amt = sizeof (asection *) * (top_index + 1);
10008   input_list = bfd_zmalloc (amt);
10009   htab->input_list = input_list;
10010   if (input_list == NULL)
10011     return -1;
10012
10013   return 1;
10014 }
10015
10016 /* Set up for first pass at multitoc partitioning.  */
10017
10018 void
10019 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10020 {
10021   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10022
10023   elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10024   htab->toc_curr = elf_gp (info->output_bfd);
10025   htab->toc_bfd = NULL;
10026   htab->toc_first_sec = NULL;
10027 }
10028
10029 /* The linker repeatedly calls this function for each TOC input section
10030    and linker generated GOT section.  Group input bfds such that the toc
10031    within a group is less than 64k in size.  */
10032
10033 bfd_boolean
10034 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10035 {
10036   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10037   bfd_vma addr, off, limit;
10038
10039   if (htab == NULL)
10040     return FALSE;
10041
10042   if (!htab->second_toc_pass)
10043     {
10044       /* Keep track of the first .toc or .got section for this input bfd.  */
10045       if (htab->toc_bfd != isec->owner)
10046         {
10047           htab->toc_bfd = isec->owner;
10048           htab->toc_first_sec = isec;
10049         }
10050
10051       addr = isec->output_offset + isec->output_section->vma;
10052       off = addr - htab->toc_curr;
10053       limit = 0x80008000;
10054       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10055         limit = 0x10000;
10056       if (off + isec->size > limit)
10057         {
10058           addr = (htab->toc_first_sec->output_offset
10059                   + htab->toc_first_sec->output_section->vma);
10060           htab->toc_curr = addr;
10061         }
10062
10063       /* toc_curr is the base address of this toc group.  Set elf_gp
10064          for the input section to be the offset relative to the
10065          output toc base plus 0x8000.  Making the input elf_gp an
10066          offset allows us to move the toc as a whole without
10067          recalculating input elf_gp.  */
10068       off = htab->toc_curr - elf_gp (isec->output_section->owner);
10069       off += TOC_BASE_OFF;
10070
10071       /* Die if someone uses a linker script that doesn't keep input
10072          file .toc and .got together.  */
10073       if (elf_gp (isec->owner) != 0
10074           && elf_gp (isec->owner) != off)
10075         return FALSE;
10076
10077       elf_gp (isec->owner) = off;
10078       return TRUE;
10079     }
10080
10081   /* During the second pass toc_first_sec points to the start of
10082      a toc group, and toc_curr is used to track the old elf_gp.
10083      We use toc_bfd to ensure we only look at each bfd once.  */
10084   if (htab->toc_bfd == isec->owner)
10085     return TRUE;
10086   htab->toc_bfd = isec->owner;
10087
10088   if (htab->toc_first_sec == NULL
10089       || htab->toc_curr != elf_gp (isec->owner))
10090     {
10091       htab->toc_curr = elf_gp (isec->owner);
10092       htab->toc_first_sec = isec;
10093     }
10094   addr = (htab->toc_first_sec->output_offset
10095           + htab->toc_first_sec->output_section->vma);
10096   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10097   elf_gp (isec->owner) = off;
10098
10099   return TRUE;
10100 }
10101
10102 /* Called via elf_link_hash_traverse to merge GOT entries for global
10103    symbol H.  */
10104
10105 static bfd_boolean
10106 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10107 {
10108   if (h->root.type == bfd_link_hash_indirect)
10109     return TRUE;
10110
10111   if (h->root.type == bfd_link_hash_warning)
10112     h = (struct elf_link_hash_entry *) h->root.u.i.link;
10113
10114   merge_got_entries (&h->got.glist);
10115
10116   return TRUE;
10117 }
10118
10119 /* Called via elf_link_hash_traverse to allocate GOT entries for global
10120    symbol H.  */
10121
10122 static bfd_boolean
10123 reallocate_got (struct elf_link_hash_entry *h, void *inf)
10124 {
10125   struct got_entry *gent;
10126
10127   if (h->root.type == bfd_link_hash_indirect)
10128     return TRUE;
10129
10130   if (h->root.type == bfd_link_hash_warning)
10131     h = (struct elf_link_hash_entry *) h->root.u.i.link;
10132
10133   for (gent = h->got.glist; gent != NULL; gent = gent->next)
10134     if (!gent->is_indirect)
10135       allocate_got (h, (struct bfd_link_info *) inf, gent);
10136   return TRUE;
10137 }
10138
10139 /* Called on the first multitoc pass after the last call to
10140    ppc64_elf_next_toc_section.  This function removes duplicate GOT
10141    entries.  */
10142
10143 bfd_boolean
10144 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
10145 {
10146   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10147   struct bfd *ibfd, *ibfd2;
10148   bfd_boolean done_something;
10149
10150   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
10151
10152   if (!htab->do_multi_toc)
10153     return FALSE;
10154
10155   /* Merge global sym got entries within a toc group.  */
10156   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10157
10158   /* And tlsld_got.  */
10159   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10160     {
10161       struct got_entry *ent, *ent2;
10162
10163       if (!is_ppc64_elf (ibfd))
10164         continue;
10165
10166       ent = ppc64_tlsld_got (ibfd);
10167       if (!ent->is_indirect
10168           && ent->got.offset != (bfd_vma) -1)
10169         {
10170           for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10171             {
10172               if (!is_ppc64_elf (ibfd2))
10173                 continue;
10174
10175               ent2 = ppc64_tlsld_got (ibfd2);
10176               if (!ent2->is_indirect
10177                   && ent2->got.offset != (bfd_vma) -1
10178                   && elf_gp (ibfd2) == elf_gp (ibfd))
10179                 {
10180                   ent2->is_indirect = TRUE;
10181                   ent2->got.ent = ent;
10182                 }
10183             }
10184         }
10185     }
10186
10187   /* Zap sizes of got sections.  */
10188   htab->reliplt->rawsize = htab->reliplt->size;
10189   htab->reliplt->size -= htab->got_reli_size;
10190   htab->got_reli_size = 0;
10191
10192   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10193     {
10194       asection *got, *relgot;
10195
10196       if (!is_ppc64_elf (ibfd))
10197         continue;
10198
10199       got = ppc64_elf_tdata (ibfd)->got;
10200       if (got != NULL)
10201         {
10202           got->rawsize = got->size;
10203           got->size = 0;
10204           relgot = ppc64_elf_tdata (ibfd)->relgot;
10205           relgot->rawsize = relgot->size;
10206           relgot->size = 0;
10207         }
10208     }
10209
10210   /* Now reallocate the got, local syms first.  We don't need to
10211      allocate section contents again since we never increase size.  */
10212   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10213     {
10214       struct got_entry **lgot_ents;
10215       struct got_entry **end_lgot_ents;
10216       struct plt_entry **local_plt;
10217       struct plt_entry **end_local_plt;
10218       unsigned char *lgot_masks;
10219       bfd_size_type locsymcount;
10220       Elf_Internal_Shdr *symtab_hdr;
10221       asection *s, *srel;
10222
10223       if (!is_ppc64_elf (ibfd))
10224         continue;
10225
10226       lgot_ents = elf_local_got_ents (ibfd);
10227       if (!lgot_ents)
10228         continue;
10229
10230       symtab_hdr = &elf_symtab_hdr (ibfd);
10231       locsymcount = symtab_hdr->sh_info;
10232       end_lgot_ents = lgot_ents + locsymcount;
10233       local_plt = (struct plt_entry **) end_lgot_ents;
10234       end_local_plt = local_plt + locsymcount;
10235       lgot_masks = (unsigned char *) end_local_plt;
10236       s = ppc64_elf_tdata (ibfd)->got;
10237       srel = ppc64_elf_tdata (ibfd)->relgot;
10238       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10239         {
10240           struct got_entry *ent;
10241
10242           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
10243             {
10244               unsigned int num = 1;
10245               ent->got.offset = s->size;
10246               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10247                 num = 2;
10248               s->size += num * 8;
10249               if (info->shared)
10250                 srel->size += num * sizeof (Elf64_External_Rela);
10251               else if ((*lgot_masks & PLT_IFUNC) != 0)
10252                 {
10253                   htab->reliplt->size
10254                     += num * sizeof (Elf64_External_Rela);
10255                   htab->got_reli_size
10256                     += num * sizeof (Elf64_External_Rela);
10257                 }
10258             }
10259         }
10260     }
10261
10262   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10263
10264   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10265     {
10266       struct got_entry *ent;
10267
10268       if (!is_ppc64_elf (ibfd))
10269         continue;
10270
10271       ent = ppc64_tlsld_got (ibfd);
10272       if (!ent->is_indirect
10273           && ent->got.offset != (bfd_vma) -1)
10274         {
10275           asection *s = ppc64_elf_tdata (ibfd)->got;
10276           ent->got.offset = s->size;
10277           s->size += 16;
10278           if (info->shared)
10279             {
10280               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10281               srel->size += sizeof (Elf64_External_Rela);
10282             }
10283         }
10284     }
10285
10286   done_something = htab->reliplt->rawsize != htab->reliplt->size;
10287   if (!done_something)
10288     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10289       {
10290         asection *got;
10291
10292         if (!is_ppc64_elf (ibfd))
10293           continue;
10294
10295         got = ppc64_elf_tdata (ibfd)->got;
10296         if (got != NULL)
10297           {
10298             done_something = got->rawsize != got->size;
10299             if (done_something)
10300               break;
10301           }
10302       }
10303
10304   if (done_something)
10305     (*htab->layout_sections_again) ();
10306
10307   /* Set up for second pass over toc sections to recalculate elf_gp
10308      on input sections.  */
10309   htab->toc_bfd = NULL;
10310   htab->toc_first_sec = NULL;
10311   htab->second_toc_pass = TRUE;
10312   return done_something;
10313 }
10314
10315 /* Called after second pass of multitoc partitioning.  */
10316
10317 void
10318 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10319 {
10320   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10321
10322   /* After the second pass, toc_curr tracks the TOC offset used
10323      for code sections below in ppc64_elf_next_input_section.  */
10324   htab->toc_curr = TOC_BASE_OFF;
10325 }
10326
10327 /* No toc references were found in ISEC.  If the code in ISEC makes no
10328    calls, then there's no need to use toc adjusting stubs when branching
10329    into ISEC.  Actually, indirect calls from ISEC are OK as they will
10330    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
10331    needed, and 2 if a cyclical call-graph was found but no other reason
10332    for a stub was detected.  If called from the top level, a return of
10333    2 means the same as a return of 0.  */
10334
10335 static int
10336 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
10337 {
10338   int ret;
10339
10340   /* Mark this section as checked.  */
10341   isec->call_check_done = 1;
10342
10343   /* We know none of our code bearing sections will need toc stubs.  */
10344   if ((isec->flags & SEC_LINKER_CREATED) != 0)
10345     return 0;
10346
10347   if (isec->size == 0)
10348     return 0;
10349
10350   if (isec->output_section == NULL)
10351     return 0;
10352
10353   ret = 0;
10354   if (isec->reloc_count != 0)
10355     {
10356       Elf_Internal_Rela *relstart, *rel;
10357       Elf_Internal_Sym *local_syms;
10358       struct ppc_link_hash_table *htab;
10359
10360       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10361                                             info->keep_memory);
10362       if (relstart == NULL)
10363         return -1;
10364
10365       /* Look for branches to outside of this section.  */
10366       local_syms = NULL;
10367       htab = ppc_hash_table (info);
10368       if (htab == NULL)
10369         return -1;
10370
10371       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
10372         {
10373           enum elf_ppc64_reloc_type r_type;
10374           unsigned long r_symndx;
10375           struct elf_link_hash_entry *h;
10376           struct ppc_link_hash_entry *eh;
10377           Elf_Internal_Sym *sym;
10378           asection *sym_sec;
10379           struct _opd_sec_data *opd;
10380           bfd_vma sym_value;
10381           bfd_vma dest;
10382
10383           r_type = ELF64_R_TYPE (rel->r_info);
10384           if (r_type != R_PPC64_REL24
10385               && r_type != R_PPC64_REL14
10386               && r_type != R_PPC64_REL14_BRTAKEN
10387               && r_type != R_PPC64_REL14_BRNTAKEN)
10388             continue;
10389
10390           r_symndx = ELF64_R_SYM (rel->r_info);
10391           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10392                           isec->owner))
10393             {
10394               ret = -1;
10395               break;
10396             }
10397
10398           /* Calls to dynamic lib functions go through a plt call stub
10399              that uses r2.  */
10400           eh = (struct ppc_link_hash_entry *) h;
10401           if (eh != NULL
10402               && (eh->elf.plt.plist != NULL
10403                   || (eh->oh != NULL
10404                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10405             {
10406               ret = 1;
10407               break;
10408             }
10409
10410           if (sym_sec == NULL)
10411             /* Ignore other undefined symbols.  */
10412             continue;
10413
10414           /* Assume branches to other sections not included in the
10415              link need stubs too, to cover -R and absolute syms.  */
10416           if (sym_sec->output_section == NULL)
10417             {
10418               ret = 1;
10419               break;
10420             }
10421
10422           if (h == NULL)
10423             sym_value = sym->st_value;
10424           else
10425             {
10426               if (h->root.type != bfd_link_hash_defined
10427                   && h->root.type != bfd_link_hash_defweak)
10428                 abort ();
10429               sym_value = h->root.u.def.value;
10430             }
10431           sym_value += rel->r_addend;
10432
10433           /* If this branch reloc uses an opd sym, find the code section.  */
10434           opd = get_opd_info (sym_sec);
10435           if (opd != NULL)
10436             {
10437               if (h == NULL && opd->adjust != NULL)
10438                 {
10439                   long adjust;
10440
10441                   adjust = opd->adjust[sym->st_value / 8];
10442                   if (adjust == -1)
10443                     /* Assume deleted functions won't ever be called.  */
10444                     continue;
10445                   sym_value += adjust;
10446                 }
10447
10448               dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10449               if (dest == (bfd_vma) -1)
10450                 continue;
10451             }
10452           else
10453             dest = (sym_value
10454                     + sym_sec->output_offset
10455                     + sym_sec->output_section->vma);
10456
10457           /* Ignore branch to self.  */
10458           if (sym_sec == isec)
10459             continue;
10460
10461           /* If the called function uses the toc, we need a stub.  */
10462           if (sym_sec->has_toc_reloc
10463               || sym_sec->makes_toc_func_call)
10464             {
10465               ret = 1;
10466               break;
10467             }
10468
10469           /* Assume any branch that needs a long branch stub might in fact
10470              need a plt_branch stub.  A plt_branch stub uses r2.  */
10471           else if (dest - (isec->output_offset
10472                            + isec->output_section->vma
10473                            + rel->r_offset) + (1 << 25) >= (2 << 25))
10474             {
10475               ret = 1;
10476               break;
10477             }
10478
10479           /* If calling back to a section in the process of being
10480              tested, we can't say for sure that no toc adjusting stubs
10481              are needed, so don't return zero.  */
10482           else if (sym_sec->call_check_in_progress)
10483             ret = 2;
10484
10485           /* Branches to another section that itself doesn't have any TOC
10486              references are OK.  Recursively call ourselves to check.  */
10487           else if (!sym_sec->call_check_done)
10488             {
10489               int recur;
10490
10491               /* Mark current section as indeterminate, so that other
10492                  sections that call back to current won't be marked as
10493                  known.  */
10494               isec->call_check_in_progress = 1;
10495               recur = toc_adjusting_stub_needed (info, sym_sec);
10496               isec->call_check_in_progress = 0;
10497
10498               if (recur != 0)
10499                 {
10500                   ret = recur;
10501                   if (recur != 2)
10502                     break;
10503                 }
10504             }
10505         }
10506
10507       if (local_syms != NULL
10508           && (elf_symtab_hdr (isec->owner).contents
10509               != (unsigned char *) local_syms))
10510         free (local_syms);
10511       if (elf_section_data (isec)->relocs != relstart)
10512         free (relstart);
10513     }
10514
10515   if ((ret & 1) == 0
10516       && isec->map_head.s != NULL
10517       && (strcmp (isec->output_section->name, ".init") == 0
10518           || strcmp (isec->output_section->name, ".fini") == 0))
10519     {
10520       if (isec->map_head.s->has_toc_reloc
10521           || isec->map_head.s->makes_toc_func_call)
10522         ret = 1;
10523       else if (!isec->map_head.s->call_check_done)
10524         {
10525           int recur;
10526           isec->call_check_in_progress = 1;
10527           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10528           isec->call_check_in_progress = 0;
10529           if (recur != 0)
10530             ret = recur;
10531         }
10532     }
10533
10534   if (ret == 1)
10535     isec->makes_toc_func_call = 1;
10536
10537   return ret;
10538 }
10539
10540 /* The linker repeatedly calls this function for each input section,
10541    in the order that input sections are linked into output sections.
10542    Build lists of input sections to determine groupings between which
10543    we may insert linker stubs.  */
10544
10545 bfd_boolean
10546 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
10547 {
10548   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10549
10550   if (htab == NULL)
10551     return FALSE;
10552
10553   if ((isec->output_section->flags & SEC_CODE) != 0
10554       && isec->output_section->index <= htab->top_index)
10555     {
10556       asection **list = htab->input_list + isec->output_section->index;
10557       /* Steal the link_sec pointer for our list.  */
10558 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
10559       /* This happens to make the list in reverse order,
10560          which is what we want.  */
10561       PREV_SEC (isec) = *list;
10562       *list = isec;
10563     }
10564
10565   if (htab->multi_toc_needed)
10566     {
10567       /* If a code section has a function that uses the TOC then we need
10568          to use the right TOC (obviously).  Also, make sure that .opd gets
10569          the correct TOC value for R_PPC64_TOC relocs that don't have or
10570          can't find their function symbol (shouldn't ever happen now).
10571          Also specially treat .fixup for the linux kernel.  .fixup
10572          contains branches, but only back to the function that hit an
10573          exception.  */
10574       if (isec->has_toc_reloc
10575           || (isec->flags & SEC_CODE) == 0
10576           || strcmp (isec->name, ".fixup") == 0)
10577         {
10578           if (elf_gp (isec->owner) != 0)
10579             htab->toc_curr = elf_gp (isec->owner);
10580         }
10581       else if (!isec->call_check_done
10582                && toc_adjusting_stub_needed (info, isec) < 0)
10583         return FALSE;
10584     }
10585
10586   /* Functions that don't use the TOC can belong in any TOC group.
10587      Use the last TOC base.  This happens to make _init and _fini
10588      pasting work, because the fragments generally don't use the TOC.  */
10589   htab->stub_group[isec->id].toc_off = htab->toc_curr;
10590   return TRUE;
10591 }
10592
10593 /* Check that all .init and .fini sections use the same toc, if they
10594    have toc relocs.  */
10595
10596 static bfd_boolean
10597 check_pasted_section (struct bfd_link_info *info, const char *name)
10598 {
10599   asection *o = bfd_get_section_by_name (info->output_bfd, name);
10600
10601   if (o != NULL)
10602     {
10603       struct ppc_link_hash_table *htab = ppc_hash_table (info);
10604       bfd_vma toc_off = 0;
10605       asection *i;
10606
10607       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10608         if (i->has_toc_reloc)
10609           {
10610             if (toc_off == 0)
10611               toc_off = htab->stub_group[i->id].toc_off;
10612             else if (toc_off != htab->stub_group[i->id].toc_off)
10613               return FALSE;
10614           }
10615       /* Make sure the whole pasted function uses the same toc offset.  */
10616       if (toc_off != 0)
10617         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10618           htab->stub_group[i->id].toc_off = toc_off;
10619     }
10620   return TRUE;
10621 }
10622
10623 bfd_boolean
10624 ppc64_elf_check_init_fini (struct bfd_link_info *info)
10625 {
10626   return (check_pasted_section (info, ".init")
10627           & check_pasted_section (info, ".fini"));
10628 }
10629
10630 /* See whether we can group stub sections together.  Grouping stub
10631    sections may result in fewer stubs.  More importantly, we need to
10632    put all .init* and .fini* stubs at the beginning of the .init or
10633    .fini output sections respectively, because glibc splits the
10634    _init and _fini functions into multiple parts.  Putting a stub in
10635    the middle of a function is not a good idea.  */
10636
10637 static void
10638 group_sections (struct ppc_link_hash_table *htab,
10639                 bfd_size_type stub_group_size,
10640                 bfd_boolean stubs_always_before_branch)
10641 {
10642   asection **list;
10643   bfd_size_type stub14_group_size;
10644   bfd_boolean suppress_size_errors;
10645
10646   suppress_size_errors = FALSE;
10647   stub14_group_size = stub_group_size;
10648   if (stub_group_size == 1)
10649     {
10650       /* Default values.  */
10651       if (stubs_always_before_branch)
10652         {
10653           stub_group_size = 0x1e00000;
10654           stub14_group_size = 0x7800;
10655         }
10656       else
10657         {
10658           stub_group_size = 0x1c00000;
10659           stub14_group_size = 0x7000;
10660         }
10661       suppress_size_errors = TRUE;
10662     }
10663
10664   list = htab->input_list + htab->top_index;
10665   do
10666     {
10667       asection *tail = *list;
10668       while (tail != NULL)
10669         {
10670           asection *curr;
10671           asection *prev;
10672           bfd_size_type total;
10673           bfd_boolean big_sec;
10674           bfd_vma curr_toc;
10675
10676           curr = tail;
10677           total = tail->size;
10678           big_sec = total > (ppc64_elf_section_data (tail)->has_14bit_branch
10679                              ? stub14_group_size : stub_group_size);
10680           if (big_sec && !suppress_size_errors)
10681             (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
10682                                      tail->owner, tail);
10683           curr_toc = htab->stub_group[tail->id].toc_off;
10684
10685           while ((prev = PREV_SEC (curr)) != NULL
10686                  && ((total += curr->output_offset - prev->output_offset)
10687                      < (ppc64_elf_section_data (prev)->has_14bit_branch
10688                         ? stub14_group_size : stub_group_size))
10689                  && htab->stub_group[prev->id].toc_off == curr_toc)
10690             curr = prev;
10691
10692           /* OK, the size from the start of CURR to the end is less
10693              than stub_group_size and thus can be handled by one stub
10694              section.  (or the tail section is itself larger than
10695              stub_group_size, in which case we may be toast.)  We
10696              should really be keeping track of the total size of stubs
10697              added here, as stubs contribute to the final output
10698              section size.  That's a little tricky, and this way will
10699              only break if stubs added make the total size more than
10700              2^25, ie. for the default stub_group_size, if stubs total
10701              more than 2097152 bytes, or nearly 75000 plt call stubs.  */
10702           do
10703             {
10704               prev = PREV_SEC (tail);
10705               /* Set up this stub group.  */
10706               htab->stub_group[tail->id].link_sec = curr;
10707             }
10708           while (tail != curr && (tail = prev) != NULL);
10709
10710           /* But wait, there's more!  Input sections up to stub_group_size
10711              bytes before the stub section can be handled by it too.
10712              Don't do this if we have a really large section after the
10713              stubs, as adding more stubs increases the chance that
10714              branches may not reach into the stub section.  */
10715           if (!stubs_always_before_branch && !big_sec)
10716             {
10717               total = 0;
10718               while (prev != NULL
10719                      && ((total += tail->output_offset - prev->output_offset)
10720                          < (ppc64_elf_section_data (prev)->has_14bit_branch
10721                             ? stub14_group_size : stub_group_size))
10722                      && htab->stub_group[prev->id].toc_off == curr_toc)
10723                 {
10724                   tail = prev;
10725                   prev = PREV_SEC (tail);
10726                   htab->stub_group[tail->id].link_sec = curr;
10727                 }
10728             }
10729           tail = prev;
10730         }
10731     }
10732   while (list-- != htab->input_list);
10733   free (htab->input_list);
10734 #undef PREV_SEC
10735 }
10736
10737 /* Determine and set the size of the stub section for a final link.
10738
10739    The basic idea here is to examine all the relocations looking for
10740    PC-relative calls to a target that is unreachable with a "bl"
10741    instruction.  */
10742
10743 bfd_boolean
10744 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size)
10745 {
10746   bfd_size_type stub_group_size;
10747   bfd_boolean stubs_always_before_branch;
10748   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10749
10750   if (htab == NULL)
10751     return FALSE;
10752
10753   stubs_always_before_branch = group_size < 0;
10754   if (group_size < 0)
10755     stub_group_size = -group_size;
10756   else
10757     stub_group_size = group_size;
10758
10759   group_sections (htab, stub_group_size, stubs_always_before_branch);
10760
10761   while (1)
10762     {
10763       bfd *input_bfd;
10764       unsigned int bfd_indx;
10765       asection *stub_sec;
10766
10767       htab->stub_iteration += 1;
10768
10769       for (input_bfd = info->input_bfds, bfd_indx = 0;
10770            input_bfd != NULL;
10771            input_bfd = input_bfd->link_next, bfd_indx++)
10772         {
10773           Elf_Internal_Shdr *symtab_hdr;
10774           asection *section;
10775           Elf_Internal_Sym *local_syms = NULL;
10776
10777           if (!is_ppc64_elf (input_bfd))
10778             continue;
10779
10780           /* We'll need the symbol table in a second.  */
10781           symtab_hdr = &elf_symtab_hdr (input_bfd);
10782           if (symtab_hdr->sh_info == 0)
10783             continue;
10784
10785           /* Walk over each section attached to the input bfd.  */
10786           for (section = input_bfd->sections;
10787                section != NULL;
10788                section = section->next)
10789             {
10790               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
10791
10792               /* If there aren't any relocs, then there's nothing more
10793                  to do.  */
10794               if ((section->flags & SEC_RELOC) == 0
10795                   || (section->flags & SEC_ALLOC) == 0
10796                   || (section->flags & SEC_LOAD) == 0
10797                   || (section->flags & SEC_CODE) == 0
10798                   || section->reloc_count == 0)
10799                 continue;
10800
10801               /* If this section is a link-once section that will be
10802                  discarded, then don't create any stubs.  */
10803               if (section->output_section == NULL
10804                   || section->output_section->owner != info->output_bfd)
10805                 continue;
10806
10807               /* Get the relocs.  */
10808               internal_relocs
10809                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
10810                                              info->keep_memory);
10811               if (internal_relocs == NULL)
10812                 goto error_ret_free_local;
10813
10814               /* Now examine each relocation.  */
10815               irela = internal_relocs;
10816               irelaend = irela + section->reloc_count;
10817               for (; irela < irelaend; irela++)
10818                 {
10819                   enum elf_ppc64_reloc_type r_type;
10820                   unsigned int r_indx;
10821                   enum ppc_stub_type stub_type;
10822                   struct ppc_stub_hash_entry *stub_entry;
10823                   asection *sym_sec, *code_sec;
10824                   bfd_vma sym_value, code_value;
10825                   bfd_vma destination;
10826                   bfd_boolean ok_dest;
10827                   struct ppc_link_hash_entry *hash;
10828                   struct ppc_link_hash_entry *fdh;
10829                   struct elf_link_hash_entry *h;
10830                   Elf_Internal_Sym *sym;
10831                   char *stub_name;
10832                   const asection *id_sec;
10833                   struct _opd_sec_data *opd;
10834                   struct plt_entry *plt_ent;
10835
10836                   r_type = ELF64_R_TYPE (irela->r_info);
10837                   r_indx = ELF64_R_SYM (irela->r_info);
10838
10839                   if (r_type >= R_PPC64_max)
10840                     {
10841                       bfd_set_error (bfd_error_bad_value);
10842                       goto error_ret_free_internal;
10843                     }
10844
10845                   /* Only look for stubs on branch instructions.  */
10846                   if (r_type != R_PPC64_REL24
10847                       && r_type != R_PPC64_REL14
10848                       && r_type != R_PPC64_REL14_BRTAKEN
10849                       && r_type != R_PPC64_REL14_BRNTAKEN)
10850                     continue;
10851
10852                   /* Now determine the call target, its name, value,
10853                      section.  */
10854                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
10855                                   r_indx, input_bfd))
10856                     goto error_ret_free_internal;
10857                   hash = (struct ppc_link_hash_entry *) h;
10858
10859                   ok_dest = FALSE;
10860                   fdh = NULL;
10861                   sym_value = 0;
10862                   if (hash == NULL)
10863                     {
10864                       sym_value = sym->st_value;
10865                       ok_dest = TRUE;
10866                     }
10867                   else if (hash->elf.root.type == bfd_link_hash_defined
10868                            || hash->elf.root.type == bfd_link_hash_defweak)
10869                     {
10870                       sym_value = hash->elf.root.u.def.value;
10871                       if (sym_sec->output_section != NULL)
10872                         ok_dest = TRUE;
10873                     }
10874                   else if (hash->elf.root.type == bfd_link_hash_undefweak
10875                            || hash->elf.root.type == bfd_link_hash_undefined)
10876                     {
10877                       /* Recognise an old ABI func code entry sym, and
10878                          use the func descriptor sym instead if it is
10879                          defined.  */
10880                       if (hash->elf.root.root.string[0] == '.'
10881                           && (fdh = lookup_fdh (hash, htab)) != NULL)
10882                         {
10883                           if (fdh->elf.root.type == bfd_link_hash_defined
10884                               || fdh->elf.root.type == bfd_link_hash_defweak)
10885                             {
10886                               sym_sec = fdh->elf.root.u.def.section;
10887                               sym_value = fdh->elf.root.u.def.value;
10888                               if (sym_sec->output_section != NULL)
10889                                 ok_dest = TRUE;
10890                             }
10891                           else
10892                             fdh = NULL;
10893                         }
10894                     }
10895                   else
10896                     {
10897                       bfd_set_error (bfd_error_bad_value);
10898                       goto error_ret_free_internal;
10899                     }
10900
10901                   destination = 0;
10902                   if (ok_dest)
10903                     {
10904                       sym_value += irela->r_addend;
10905                       destination = (sym_value
10906                                      + sym_sec->output_offset
10907                                      + sym_sec->output_section->vma);
10908                     }
10909
10910                   code_sec = sym_sec;
10911                   code_value = sym_value;
10912                   opd = get_opd_info (sym_sec);
10913                   if (opd != NULL)
10914                     {
10915                       bfd_vma dest;
10916
10917                       if (hash == NULL && opd->adjust != NULL)
10918                         {
10919                           long adjust = opd->adjust[sym_value / 8];
10920                           if (adjust == -1)
10921                             continue;
10922                           code_value += adjust;
10923                           sym_value += adjust;
10924                         }
10925                       dest = opd_entry_value (sym_sec, sym_value,
10926                                               &code_sec, &code_value);
10927                       if (dest != (bfd_vma) -1)
10928                         {
10929                           destination = dest;
10930                           if (fdh != NULL)
10931                             {
10932                               /* Fixup old ABI sym to point at code
10933                                  entry.  */
10934                               hash->elf.root.type = bfd_link_hash_defweak;
10935                               hash->elf.root.u.def.section = code_sec;
10936                               hash->elf.root.u.def.value = code_value;
10937                             }
10938                         }
10939                     }
10940
10941                   /* Determine what (if any) linker stub is needed.  */
10942                   plt_ent = NULL;
10943                   stub_type = ppc_type_of_stub (section, irela, &hash,
10944                                                 &plt_ent, destination);
10945
10946                   if (stub_type != ppc_stub_plt_call)
10947                     {
10948                       /* Check whether we need a TOC adjusting stub.
10949                          Since the linker pastes together pieces from
10950                          different object files when creating the
10951                          _init and _fini functions, it may be that a
10952                          call to what looks like a local sym is in
10953                          fact a call needing a TOC adjustment.  */
10954                       if (code_sec != NULL
10955                           && code_sec->output_section != NULL
10956                           && (htab->stub_group[code_sec->id].toc_off
10957                               != htab->stub_group[section->id].toc_off)
10958                           && (code_sec->has_toc_reloc
10959                               || code_sec->makes_toc_func_call))
10960                         stub_type = ppc_stub_long_branch_r2off;
10961                     }
10962
10963                   if (stub_type == ppc_stub_none)
10964                     continue;
10965
10966                   /* __tls_get_addr calls might be eliminated.  */
10967                   if (stub_type != ppc_stub_plt_call
10968                       && hash != NULL
10969                       && (hash == htab->tls_get_addr
10970                           || hash == htab->tls_get_addr_fd)
10971                       && section->has_tls_reloc
10972                       && irela != internal_relocs)
10973                     {
10974                       /* Get tls info.  */
10975                       unsigned char *tls_mask;
10976
10977                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
10978                                          irela - 1, input_bfd))
10979                         goto error_ret_free_internal;
10980                       if (*tls_mask != 0)
10981                         continue;
10982                     }
10983
10984                   /* Support for grouping stub sections.  */
10985                   id_sec = htab->stub_group[section->id].link_sec;
10986
10987                   /* Get the name of this stub.  */
10988                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
10989                   if (!stub_name)
10990                     goto error_ret_free_internal;
10991
10992                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
10993                                                      stub_name, FALSE, FALSE);
10994                   if (stub_entry != NULL)
10995                     {
10996                       /* The proper stub has already been created.  */
10997                       free (stub_name);
10998                       continue;
10999                     }
11000
11001                   stub_entry = ppc_add_stub (stub_name, section, htab);
11002                   if (stub_entry == NULL)
11003                     {
11004                       free (stub_name);
11005                     error_ret_free_internal:
11006                       if (elf_section_data (section)->relocs == NULL)
11007                         free (internal_relocs);
11008                     error_ret_free_local:
11009                       if (local_syms != NULL
11010                           && (symtab_hdr->contents
11011                               != (unsigned char *) local_syms))
11012                         free (local_syms);
11013                       return FALSE;
11014                     }
11015
11016                   stub_entry->stub_type = stub_type;
11017                   if (stub_type != ppc_stub_plt_call)
11018                     {
11019                       stub_entry->target_value = code_value;
11020                       stub_entry->target_section = code_sec;
11021                     }
11022                   else
11023                     {
11024                       stub_entry->target_value = sym_value;
11025                       stub_entry->target_section = sym_sec;
11026                     }
11027                   stub_entry->h = hash;
11028                   stub_entry->plt_ent = plt_ent;
11029                   stub_entry->addend = irela->r_addend;
11030
11031                   if (stub_entry->h != NULL)
11032                     htab->stub_globals += 1;
11033                 }
11034
11035               /* We're done with the internal relocs, free them.  */
11036               if (elf_section_data (section)->relocs != internal_relocs)
11037                 free (internal_relocs);
11038             }
11039
11040           if (local_syms != NULL
11041               && symtab_hdr->contents != (unsigned char *) local_syms)
11042             {
11043               if (!info->keep_memory)
11044                 free (local_syms);
11045               else
11046                 symtab_hdr->contents = (unsigned char *) local_syms;
11047             }
11048         }
11049
11050       /* We may have added some stubs.  Find out the new size of the
11051          stub sections.  */
11052       for (stub_sec = htab->stub_bfd->sections;
11053            stub_sec != NULL;
11054            stub_sec = stub_sec->next)
11055         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11056           {
11057             stub_sec->rawsize = stub_sec->size;
11058             stub_sec->size = 0;
11059             stub_sec->reloc_count = 0;
11060             stub_sec->flags &= ~SEC_RELOC;
11061           }
11062
11063       htab->brlt->size = 0;
11064       htab->brlt->reloc_count = 0;
11065       htab->brlt->flags &= ~SEC_RELOC;
11066       if (htab->relbrlt != NULL)
11067         htab->relbrlt->size = 0;
11068
11069       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
11070
11071       if (info->emitrelocations
11072           && htab->glink != NULL && htab->glink->size != 0)
11073         {
11074           htab->glink->reloc_count = 1;
11075           htab->glink->flags |= SEC_RELOC;
11076         }
11077
11078       for (stub_sec = htab->stub_bfd->sections;
11079            stub_sec != NULL;
11080            stub_sec = stub_sec->next)
11081         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11082             && stub_sec->rawsize != stub_sec->size)
11083           break;
11084
11085       /* Exit from this loop when no stubs have been added, and no stubs
11086          have changed size.  */
11087       if (stub_sec == NULL)
11088         break;
11089
11090       /* Ask the linker to do its stuff.  */
11091       (*htab->layout_sections_again) ();
11092     }
11093
11094   /* It would be nice to strip htab->brlt from the output if the
11095      section is empty, but it's too late.  If we strip sections here,
11096      the dynamic symbol table is corrupted since the section symbol
11097      for the stripped section isn't written.  */
11098
11099   return TRUE;
11100 }
11101
11102 /* Called after we have determined section placement.  If sections
11103    move, we'll be called again.  Provide a value for TOCstart.  */
11104
11105 bfd_vma
11106 ppc64_elf_toc (bfd *obfd)
11107 {
11108   asection *s;
11109   bfd_vma TOCstart;
11110
11111   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11112      order.  The TOC starts where the first of these sections starts.  */
11113   s = bfd_get_section_by_name (obfd, ".got");
11114   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11115     s = bfd_get_section_by_name (obfd, ".toc");
11116   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11117     s = bfd_get_section_by_name (obfd, ".tocbss");
11118   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11119     s = bfd_get_section_by_name (obfd, ".plt");
11120   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11121     {
11122       /* This may happen for
11123          o  references to TOC base (SYM@toc / TOC[tc0]) without a
11124          .toc directive
11125          o  bad linker script
11126          o --gc-sections and empty TOC sections
11127
11128          FIXME: Warn user?  */
11129
11130       /* Look for a likely section.  We probably won't even be
11131          using TOCstart.  */
11132       for (s = obfd->sections; s != NULL; s = s->next)
11133         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11134                          | SEC_EXCLUDE))
11135             == (SEC_ALLOC | SEC_SMALL_DATA))
11136           break;
11137       if (s == NULL)
11138         for (s = obfd->sections; s != NULL; s = s->next)
11139           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
11140               == (SEC_ALLOC | SEC_SMALL_DATA))
11141             break;
11142       if (s == NULL)
11143         for (s = obfd->sections; s != NULL; s = s->next)
11144           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11145               == SEC_ALLOC)
11146             break;
11147       if (s == NULL)
11148         for (s = obfd->sections; s != NULL; s = s->next)
11149           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
11150             break;
11151     }
11152
11153   TOCstart = 0;
11154   if (s != NULL)
11155     TOCstart = s->output_section->vma + s->output_offset;
11156
11157   return TOCstart;
11158 }
11159
11160 /* Build all the stubs associated with the current output file.
11161    The stubs are kept in a hash table attached to the main linker
11162    hash table.  This function is called via gldelf64ppc_finish.  */
11163
11164 bfd_boolean
11165 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11166                        struct bfd_link_info *info,
11167                        char **stats)
11168 {
11169   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11170   asection *stub_sec;
11171   bfd_byte *p;
11172   int stub_sec_count = 0;
11173
11174   if (htab == NULL)
11175     return FALSE;
11176
11177   htab->emit_stub_syms = emit_stub_syms;
11178
11179   /* Allocate memory to hold the linker stubs.  */
11180   for (stub_sec = htab->stub_bfd->sections;
11181        stub_sec != NULL;
11182        stub_sec = stub_sec->next)
11183     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11184         && stub_sec->size != 0)
11185       {
11186         stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11187         if (stub_sec->contents == NULL)
11188           return FALSE;
11189         /* We want to check that built size is the same as calculated
11190            size.  rawsize is a convenient location to use.  */
11191         stub_sec->rawsize = stub_sec->size;
11192         stub_sec->size = 0;
11193       }
11194
11195   if (htab->glink != NULL && htab->glink->size != 0)
11196     {
11197       unsigned int indx;
11198       bfd_vma plt0;
11199
11200       /* Build the .glink plt call stub.  */
11201       if (htab->emit_stub_syms)
11202         {
11203           struct elf_link_hash_entry *h;
11204           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11205                                     TRUE, FALSE, FALSE);
11206           if (h == NULL)
11207             return FALSE;
11208           if (h->root.type == bfd_link_hash_new)
11209             {
11210               h->root.type = bfd_link_hash_defined;
11211               h->root.u.def.section = htab->glink;
11212               h->root.u.def.value = 8;
11213               h->ref_regular = 1;
11214               h->def_regular = 1;
11215               h->ref_regular_nonweak = 1;
11216               h->forced_local = 1;
11217               h->non_elf = 0;
11218             }
11219         }
11220       plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11221       if (info->emitrelocations)
11222         {
11223           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11224           if (r == NULL)
11225             return FALSE;
11226           r->r_offset = (htab->glink->output_offset
11227                          + htab->glink->output_section->vma);
11228           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11229           r->r_addend = plt0;
11230         }
11231       p = htab->glink->contents;
11232       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
11233       bfd_put_64 (htab->glink->owner, plt0, p);
11234       p += 8;
11235       bfd_put_32 (htab->glink->owner, MFLR_R12, p);
11236       p += 4;
11237       bfd_put_32 (htab->glink->owner, BCL_20_31, p);
11238       p += 4;
11239       bfd_put_32 (htab->glink->owner, MFLR_R11, p);
11240       p += 4;
11241       bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
11242       p += 4;
11243       bfd_put_32 (htab->glink->owner, MTLR_R12, p);
11244       p += 4;
11245       bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
11246       p += 4;
11247       bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
11248       p += 4;
11249       bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
11250       p += 4;
11251       bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
11252       p += 4;
11253       bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
11254       p += 4;
11255       bfd_put_32 (htab->glink->owner, BCTR, p);
11256       p += 4;
11257       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11258         {
11259           bfd_put_32 (htab->glink->owner, NOP, p);
11260           p += 4;
11261         }
11262
11263       /* Build the .glink lazy link call stubs.  */
11264       indx = 0;
11265       while (p < htab->glink->contents + htab->glink->size)
11266         {
11267           if (indx < 0x8000)
11268             {
11269               bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
11270               p += 4;
11271             }
11272           else
11273             {
11274               bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
11275               p += 4;
11276               bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
11277               p += 4;
11278             }
11279           bfd_put_32 (htab->glink->owner,
11280                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
11281           indx++;
11282           p += 4;
11283         }
11284       htab->glink->rawsize = p - htab->glink->contents;
11285     }
11286
11287   if (htab->brlt->size != 0)
11288     {
11289       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
11290                                          htab->brlt->size);
11291       if (htab->brlt->contents == NULL)
11292         return FALSE;
11293     }
11294   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
11295     {
11296       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
11297                                             htab->relbrlt->size);
11298       if (htab->relbrlt->contents == NULL)
11299         return FALSE;
11300     }
11301
11302   /* Build the stubs as directed by the stub hash table.  */
11303   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
11304
11305   if (htab->relbrlt != NULL)
11306     htab->relbrlt->reloc_count = 0;
11307
11308   for (stub_sec = htab->stub_bfd->sections;
11309        stub_sec != NULL;
11310        stub_sec = stub_sec->next)
11311     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11312       {
11313         stub_sec_count += 1;
11314         if (stub_sec->rawsize != stub_sec->size)
11315           break;
11316       }
11317
11318   if (stub_sec != NULL
11319       || htab->glink->rawsize != htab->glink->size)
11320     {
11321       htab->stub_error = TRUE;
11322       (*_bfd_error_handler) (_("stubs don't match calculated size"));
11323     }
11324
11325   if (htab->stub_error)
11326     return FALSE;
11327
11328   if (stats != NULL)
11329     {
11330       *stats = bfd_malloc (500);
11331       if (*stats == NULL)
11332         return FALSE;
11333
11334       sprintf (*stats, _("linker stubs in %u group%s\n"
11335                          "  branch       %lu\n"
11336                          "  toc adjust   %lu\n"
11337                          "  long branch  %lu\n"
11338                          "  long toc adj %lu\n"
11339                          "  plt call     %lu"),
11340                stub_sec_count,
11341                stub_sec_count == 1 ? "" : "s",
11342                htab->stub_count[ppc_stub_long_branch - 1],
11343                htab->stub_count[ppc_stub_long_branch_r2off - 1],
11344                htab->stub_count[ppc_stub_plt_branch - 1],
11345                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
11346                htab->stub_count[ppc_stub_plt_call - 1]);
11347     }
11348   return TRUE;
11349 }
11350
11351 /* This function undoes the changes made by add_symbol_adjust.  */
11352
11353 static bfd_boolean
11354 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11355 {
11356   struct ppc_link_hash_entry *eh;
11357
11358   if (h->root.type == bfd_link_hash_indirect)
11359     return TRUE;
11360
11361   if (h->root.type == bfd_link_hash_warning)
11362     h = (struct elf_link_hash_entry *) h->root.u.i.link;
11363
11364   eh = (struct ppc_link_hash_entry *) h;
11365   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
11366     return TRUE;
11367
11368   eh->elf.root.type = bfd_link_hash_undefined;
11369   return TRUE;
11370 }
11371
11372 void
11373 ppc64_elf_restore_symbols (struct bfd_link_info *info)
11374 {
11375   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11376
11377   if (htab != NULL)
11378     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
11379 }
11380
11381 /* What to do when ld finds relocations against symbols defined in
11382    discarded sections.  */
11383
11384 static unsigned int
11385 ppc64_elf_action_discarded (asection *sec)
11386 {
11387   if (strcmp (".opd", sec->name) == 0)
11388     return 0;
11389
11390   if (strcmp (".toc", sec->name) == 0)
11391     return 0;
11392
11393   if (strcmp (".toc1", sec->name) == 0)
11394     return 0;
11395
11396   return _bfd_elf_default_action_discarded (sec);
11397 }
11398
11399 /* REL points to a low-part reloc on a largetoc instruction sequence.
11400    Find the matching high-part reloc instruction and verify that it
11401    is addis REG,r2,x.  If so, return a pointer to the high-part reloc.  */
11402
11403 static const Elf_Internal_Rela *
11404 ha_reloc_match (const Elf_Internal_Rela *relocs,
11405                 const Elf_Internal_Rela *rel,
11406                 unsigned int reg,
11407                 const bfd *input_bfd,
11408                 const bfd_byte *contents)
11409 {
11410   enum elf_ppc64_reloc_type r_type, r_type_ha;
11411   bfd_vma r_info_ha, r_addend;
11412
11413   r_type = ELF64_R_TYPE (rel->r_info);
11414   switch (r_type)
11415     {
11416     case R_PPC64_GOT_TLSLD16_LO:
11417     case R_PPC64_GOT_TLSGD16_LO:
11418     case R_PPC64_GOT_TPREL16_LO_DS:
11419     case R_PPC64_GOT_DTPREL16_LO_DS:
11420     case R_PPC64_GOT16_LO:
11421     case R_PPC64_TOC16_LO:
11422       r_type_ha = r_type + 2;
11423       break;
11424     case R_PPC64_GOT16_LO_DS:
11425       r_type_ha = R_PPC64_GOT16_HA;
11426       break;
11427     case R_PPC64_TOC16_LO_DS:
11428       r_type_ha = R_PPC64_TOC16_HA;
11429       break;
11430     default:
11431       abort ();
11432     }
11433   r_info_ha = ELF64_R_INFO (ELF64_R_SYM (rel->r_info), r_type_ha);
11434   r_addend = rel->r_addend;
11435
11436   while (--rel >= relocs)
11437     if (rel->r_info == r_info_ha
11438         && rel->r_addend == r_addend)
11439       {
11440         const bfd_byte *p = contents + (rel->r_offset & ~3);
11441         unsigned int insn = bfd_get_32 (input_bfd, p);
11442         if ((insn & ((0x3f << 26) | (0x1f << 16)))
11443             == ((15u << 26) | (2 << 16)) /* addis rt,r2,x */
11444             && (insn & (0x1f << 21)) == (reg << 21))
11445           return rel;
11446         break;
11447       }
11448   return NULL;
11449 }
11450
11451 /* The RELOCATE_SECTION function is called by the ELF backend linker
11452    to handle the relocations for a section.
11453
11454    The relocs are always passed as Rela structures; if the section
11455    actually uses Rel structures, the r_addend field will always be
11456    zero.
11457
11458    This function is responsible for adjust the section contents as
11459    necessary, and (if using Rela relocs and generating a
11460    relocatable output file) adjusting the reloc addend as
11461    necessary.
11462
11463    This function does not have to worry about setting the reloc
11464    address or the reloc symbol index.
11465
11466    LOCAL_SYMS is a pointer to the swapped in local symbols.
11467
11468    LOCAL_SECTIONS is an array giving the section in the input file
11469    corresponding to the st_shndx field of each local symbol.
11470
11471    The global hash table entry for the global symbols can be found
11472    via elf_sym_hashes (input_bfd).
11473
11474    When generating relocatable output, this function must handle
11475    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
11476    going to be the section symbol corresponding to the output
11477    section, which means that the addend must be adjusted
11478    accordingly.  */
11479
11480 static bfd_boolean
11481 ppc64_elf_relocate_section (bfd *output_bfd,
11482                             struct bfd_link_info *info,
11483                             bfd *input_bfd,
11484                             asection *input_section,
11485                             bfd_byte *contents,
11486                             Elf_Internal_Rela *relocs,
11487                             Elf_Internal_Sym *local_syms,
11488                             asection **local_sections)
11489 {
11490   struct ppc_link_hash_table *htab;
11491   Elf_Internal_Shdr *symtab_hdr;
11492   struct elf_link_hash_entry **sym_hashes;
11493   Elf_Internal_Rela *rel;
11494   Elf_Internal_Rela *relend;
11495   Elf_Internal_Rela outrel;
11496   bfd_byte *loc;
11497   struct got_entry **local_got_ents;
11498   bfd_vma TOCstart;
11499   bfd_boolean ret = TRUE;
11500   bfd_boolean is_opd;
11501   /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
11502   bfd_boolean is_power4 = FALSE;
11503   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
11504
11505   /* Initialize howto table if needed.  */
11506   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
11507     ppc_howto_init ();
11508
11509   htab = ppc_hash_table (info);
11510   if (htab == NULL)
11511     return FALSE;
11512
11513   /* Don't relocate stub sections.  */
11514   if (input_section->owner == htab->stub_bfd)
11515     return TRUE;
11516
11517   BFD_ASSERT (is_ppc64_elf (input_bfd));
11518
11519   local_got_ents = elf_local_got_ents (input_bfd);
11520   TOCstart = elf_gp (output_bfd);
11521   symtab_hdr = &elf_symtab_hdr (input_bfd);
11522   sym_hashes = elf_sym_hashes (input_bfd);
11523   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
11524
11525   rel = relocs;
11526   relend = relocs + input_section->reloc_count;
11527   for (; rel < relend; rel++)
11528     {
11529       enum elf_ppc64_reloc_type r_type;
11530       bfd_vma addend, orig_addend;
11531       bfd_reloc_status_type r;
11532       Elf_Internal_Sym *sym;
11533       asection *sec;
11534       struct elf_link_hash_entry *h_elf;
11535       struct ppc_link_hash_entry *h;
11536       struct ppc_link_hash_entry *fdh;
11537       const char *sym_name;
11538       unsigned long r_symndx, toc_symndx;
11539       bfd_vma toc_addend;
11540       unsigned char tls_mask, tls_gd, tls_type;
11541       unsigned char sym_type;
11542       bfd_vma relocation;
11543       bfd_boolean unresolved_reloc;
11544       bfd_boolean warned;
11545       unsigned int insn;
11546       bfd_vma mask;
11547       struct ppc_stub_hash_entry *stub_entry;
11548       bfd_vma max_br_offset;
11549       bfd_vma from;
11550
11551       r_type = ELF64_R_TYPE (rel->r_info);
11552       r_symndx = ELF64_R_SYM (rel->r_info);
11553
11554       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
11555          symbol of the previous ADDR64 reloc.  The symbol gives us the
11556          proper TOC base to use.  */
11557       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
11558           && rel != relocs
11559           && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
11560           && is_opd)
11561         r_symndx = ELF64_R_SYM (rel[-1].r_info);
11562
11563       sym = NULL;
11564       sec = NULL;
11565       h_elf = NULL;
11566       sym_name = NULL;
11567       unresolved_reloc = FALSE;
11568       warned = FALSE;
11569       orig_addend = rel->r_addend;
11570
11571       if (r_symndx < symtab_hdr->sh_info)
11572         {
11573           /* It's a local symbol.  */
11574           struct _opd_sec_data *opd;
11575
11576           sym = local_syms + r_symndx;
11577           sec = local_sections[r_symndx];
11578           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
11579           sym_type = ELF64_ST_TYPE (sym->st_info);
11580           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
11581           opd = get_opd_info (sec);
11582           if (opd != NULL && opd->adjust != NULL)
11583             {
11584               long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
11585               if (adjust == -1)
11586                 relocation = 0;
11587               else
11588                 {
11589                   /* If this is a relocation against the opd section sym
11590                      and we have edited .opd, adjust the reloc addend so
11591                      that ld -r and ld --emit-relocs output is correct.
11592                      If it is a reloc against some other .opd symbol,
11593                      then the symbol value will be adjusted later.  */
11594                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11595                     rel->r_addend += adjust;
11596                   else
11597                     relocation += adjust;
11598                 }
11599             }
11600         }
11601       else
11602         {
11603           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
11604                                    r_symndx, symtab_hdr, sym_hashes,
11605                                    h_elf, sec, relocation,
11606                                    unresolved_reloc, warned);
11607           sym_name = h_elf->root.root.string;
11608           sym_type = h_elf->type;
11609         }
11610       h = (struct ppc_link_hash_entry *) h_elf;
11611
11612       if (sec != NULL && elf_discarded_section (sec))
11613         {
11614           /* For relocs against symbols from removed linkonce sections,
11615              or sections discarded by a linker script, we just want the
11616              section contents zeroed.  Avoid any special processing.  */
11617           _bfd_clear_contents (ppc64_elf_howto_table[r_type], input_bfd,
11618                                contents + rel->r_offset);
11619           rel->r_info = 0;
11620           rel->r_addend = 0;
11621           continue;
11622         }
11623
11624       if (info->relocatable)
11625         continue;
11626
11627       /* TLS optimizations.  Replace instruction sequences and relocs
11628          based on information we collected in tls_optimize.  We edit
11629          RELOCS so that --emit-relocs will output something sensible
11630          for the final instruction stream.  */
11631       tls_mask = 0;
11632       tls_gd = 0;
11633       toc_symndx = 0;
11634       if (h != NULL)
11635         tls_mask = h->tls_mask;
11636       else if (local_got_ents != NULL)
11637         {
11638           struct plt_entry **local_plt = (struct plt_entry **)
11639             (local_got_ents + symtab_hdr->sh_info);
11640           unsigned char *lgot_masks = (unsigned char *)
11641             (local_plt + symtab_hdr->sh_info);
11642           tls_mask = lgot_masks[r_symndx];
11643         }
11644       if (tls_mask == 0
11645           && (r_type == R_PPC64_TLS
11646               || r_type == R_PPC64_TLSGD
11647               || r_type == R_PPC64_TLSLD))
11648         {
11649           /* Check for toc tls entries.  */
11650           unsigned char *toc_tls;
11651
11652           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11653                              &local_syms, rel, input_bfd))
11654             return FALSE;
11655
11656           if (toc_tls)
11657             tls_mask = *toc_tls;
11658         }
11659
11660       /* Check that tls relocs are used with tls syms, and non-tls
11661          relocs are used with non-tls syms.  */
11662       if (r_symndx != 0
11663           && r_type != R_PPC64_NONE
11664           && (h == NULL
11665               || h->elf.root.type == bfd_link_hash_defined
11666               || h->elf.root.type == bfd_link_hash_defweak)
11667           && (IS_PPC64_TLS_RELOC (r_type)
11668               != (sym_type == STT_TLS
11669                   || (sym_type == STT_SECTION
11670                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
11671         {
11672           if (tls_mask != 0
11673               && (r_type == R_PPC64_TLS
11674                   || r_type == R_PPC64_TLSGD
11675                   || r_type == R_PPC64_TLSLD))
11676             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
11677             ;
11678           else
11679             (*_bfd_error_handler)
11680               (!IS_PPC64_TLS_RELOC (r_type)
11681                ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
11682                : _("%B(%A+0x%lx): %s used with non-TLS symbol %s"),
11683                input_bfd,
11684                input_section,
11685                (long) rel->r_offset,
11686                ppc64_elf_howto_table[r_type]->name,
11687                sym_name);
11688         }
11689
11690       /* Ensure reloc mapping code below stays sane.  */
11691       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
11692           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
11693           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
11694           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
11695           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
11696           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
11697           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
11698           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
11699           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
11700           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
11701         abort ();
11702
11703       switch (r_type)
11704         {
11705         default:
11706           break;
11707
11708         case R_PPC64_LO_DS_OPT:
11709           insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
11710           if ((insn & (0x3f << 26)) != 58u << 26)
11711             abort ();
11712           insn += (14u << 26) - (58u << 26);
11713           bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
11714           r_type = R_PPC64_TOC16_LO;
11715           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11716           break;
11717
11718         case R_PPC64_TOC16:
11719         case R_PPC64_TOC16_LO:
11720         case R_PPC64_TOC16_DS:
11721         case R_PPC64_TOC16_LO_DS:
11722           {
11723             /* Check for toc tls entries.  */
11724             unsigned char *toc_tls;
11725             int retval;
11726
11727             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11728                                    &local_syms, rel, input_bfd);
11729             if (retval == 0)
11730               return FALSE;
11731
11732             if (toc_tls)
11733               {
11734                 tls_mask = *toc_tls;
11735                 if (r_type == R_PPC64_TOC16_DS
11736                     || r_type == R_PPC64_TOC16_LO_DS)
11737                   {
11738                     if (tls_mask != 0
11739                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
11740                       goto toctprel;
11741                   }
11742                 else
11743                   {
11744                     /* If we found a GD reloc pair, then we might be
11745                        doing a GD->IE transition.  */
11746                     if (retval == 2)
11747                       {
11748                         tls_gd = TLS_TPRELGD;
11749                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11750                           goto tls_ldgd_opt;
11751                       }
11752                     else if (retval == 3)
11753                       {
11754                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11755                           goto tls_ldgd_opt;
11756                       }
11757                   }
11758               }
11759           }
11760           break;
11761
11762         case R_PPC64_GOT_TPREL16_HI:
11763         case R_PPC64_GOT_TPREL16_HA:
11764           if (tls_mask != 0
11765               && (tls_mask & TLS_TPREL) == 0)
11766             {
11767               rel->r_offset -= d_offset;
11768               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
11769               r_type = R_PPC64_NONE;
11770               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11771             }
11772           break;
11773
11774         case R_PPC64_GOT_TPREL16_DS:
11775         case R_PPC64_GOT_TPREL16_LO_DS:
11776           if (tls_mask != 0
11777               && (tls_mask & TLS_TPREL) == 0)
11778             {
11779             toctprel:
11780               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
11781               insn &= 31 << 21;
11782               insn |= 0x3c0d0000;       /* addis 0,13,0 */
11783               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
11784               r_type = R_PPC64_TPREL16_HA;
11785               if (toc_symndx != 0)
11786                 {
11787                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
11788                   rel->r_addend = toc_addend;
11789                   /* We changed the symbol.  Start over in order to
11790                      get h, sym, sec etc. right.  */
11791                   rel--;
11792                   continue;
11793                 }
11794               else
11795                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11796             }
11797           break;
11798
11799         case R_PPC64_TLS:
11800           if (tls_mask != 0
11801               && (tls_mask & TLS_TPREL) == 0)
11802             {
11803               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
11804               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
11805               if (insn == 0)
11806                 abort ();
11807               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
11808               /* Was PPC64_TLS which sits on insn boundary, now
11809                  PPC64_TPREL16_LO which is at low-order half-word.  */
11810               rel->r_offset += d_offset;
11811               r_type = R_PPC64_TPREL16_LO;
11812               if (toc_symndx != 0)
11813                 {
11814                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
11815                   rel->r_addend = toc_addend;
11816                   /* We changed the symbol.  Start over in order to
11817                      get h, sym, sec etc. right.  */
11818                   rel--;
11819                   continue;
11820                 }
11821               else
11822                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11823             }
11824           break;
11825
11826         case R_PPC64_GOT_TLSGD16_HI:
11827         case R_PPC64_GOT_TLSGD16_HA:
11828           tls_gd = TLS_TPRELGD;
11829           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11830             goto tls_gdld_hi;
11831           break;
11832
11833         case R_PPC64_GOT_TLSLD16_HI:
11834         case R_PPC64_GOT_TLSLD16_HA:
11835           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11836             {
11837             tls_gdld_hi:
11838               if ((tls_mask & tls_gd) != 0)
11839                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11840                           + R_PPC64_GOT_TPREL16_DS);
11841               else
11842                 {
11843                   rel->r_offset -= d_offset;
11844                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
11845                   r_type = R_PPC64_NONE;
11846                 }
11847               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11848             }
11849           break;
11850
11851         case R_PPC64_GOT_TLSGD16:
11852         case R_PPC64_GOT_TLSGD16_LO:
11853           tls_gd = TLS_TPRELGD;
11854           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11855             goto tls_ldgd_opt;
11856           break;
11857
11858         case R_PPC64_GOT_TLSLD16:
11859         case R_PPC64_GOT_TLSLD16_LO:
11860           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11861             {
11862               unsigned int insn1, insn2, insn3;
11863               bfd_vma offset;
11864
11865             tls_ldgd_opt:
11866               offset = (bfd_vma) -1;
11867               /* If not using the newer R_PPC64_TLSGD/LD to mark
11868                  __tls_get_addr calls, we must trust that the call
11869                  stays with its arg setup insns, ie. that the next
11870                  reloc is the __tls_get_addr call associated with
11871                  the current reloc.  Edit both insns.  */
11872               if (input_section->has_tls_get_addr_call
11873                   && rel + 1 < relend
11874                   && branch_reloc_hash_match (input_bfd, rel + 1,
11875                                               htab->tls_get_addr,
11876                                               htab->tls_get_addr_fd))
11877                 offset = rel[1].r_offset;
11878               if ((tls_mask & tls_gd) != 0)
11879                 {
11880                   /* IE */
11881                   insn1 = bfd_get_32 (output_bfd,
11882                                       contents + rel->r_offset - d_offset);
11883                   insn1 &= (1 << 26) - (1 << 2);
11884                   insn1 |= 58 << 26;    /* ld */
11885                   insn2 = 0x7c636a14;   /* add 3,3,13 */
11886                   if (offset != (bfd_vma) -1)
11887                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
11888                   if ((tls_mask & TLS_EXPLICIT) == 0)
11889                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11890                               + R_PPC64_GOT_TPREL16_DS);
11891                   else
11892                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
11893                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11894                 }
11895               else
11896                 {
11897                   /* LE */
11898                   insn1 = 0x3c6d0000;   /* addis 3,13,0 */
11899                   insn2 = 0x38630000;   /* addi 3,3,0 */
11900                   if (tls_gd == 0)
11901                     {
11902                       /* Was an LD reloc.  */
11903                       if (toc_symndx)
11904                         sec = local_sections[toc_symndx];
11905                       for (r_symndx = 0;
11906                            r_symndx < symtab_hdr->sh_info;
11907                            r_symndx++)
11908                         if (local_sections[r_symndx] == sec)
11909                           break;
11910                       if (r_symndx >= symtab_hdr->sh_info)
11911                         r_symndx = 0;
11912                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
11913                       if (r_symndx != 0)
11914                         rel->r_addend -= (local_syms[r_symndx].st_value
11915                                           + sec->output_offset
11916                                           + sec->output_section->vma);
11917                     }
11918                   else if (toc_symndx != 0)
11919                     {
11920                       r_symndx = toc_symndx;
11921                       rel->r_addend = toc_addend;
11922                     }
11923                   r_type = R_PPC64_TPREL16_HA;
11924                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11925                   if (offset != (bfd_vma) -1)
11926                     {
11927                       rel[1].r_info = ELF64_R_INFO (r_symndx,
11928                                                     R_PPC64_TPREL16_LO);
11929                       rel[1].r_offset = offset + d_offset;
11930                       rel[1].r_addend = rel->r_addend;
11931                     }
11932                 }
11933               bfd_put_32 (output_bfd, insn1,
11934                           contents + rel->r_offset - d_offset);
11935               if (offset != (bfd_vma) -1)
11936                 {
11937                   insn3 = bfd_get_32 (output_bfd,
11938                                       contents + offset + 4);
11939                   if (insn3 == NOP
11940                       || insn3 == CROR_151515 || insn3 == CROR_313131)
11941                     {
11942                       rel[1].r_offset += 4;
11943                       bfd_put_32 (output_bfd, insn2, contents + offset + 4);
11944                       insn2 = NOP;
11945                     }
11946                   bfd_put_32 (output_bfd, insn2, contents + offset);
11947                 }
11948               if ((tls_mask & tls_gd) == 0
11949                   && (tls_gd == 0 || toc_symndx != 0))
11950                 {
11951                   /* We changed the symbol.  Start over in order
11952                      to get h, sym, sec etc. right.  */
11953                   rel--;
11954                   continue;
11955                 }
11956             }
11957           break;
11958
11959         case R_PPC64_TLSGD:
11960           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11961             {
11962               unsigned int insn2, insn3;
11963               bfd_vma offset = rel->r_offset;
11964
11965               if ((tls_mask & TLS_TPRELGD) != 0)
11966                 {
11967                   /* IE */
11968                   r_type = R_PPC64_NONE;
11969                   insn2 = 0x7c636a14;   /* add 3,3,13 */
11970                 }
11971               else
11972                 {
11973                   /* LE */
11974                   if (toc_symndx != 0)
11975                     {
11976                       r_symndx = toc_symndx;
11977                       rel->r_addend = toc_addend;
11978                     }
11979                   r_type = R_PPC64_TPREL16_LO;
11980                   rel->r_offset = offset + d_offset;
11981                   insn2 = 0x38630000;   /* addi 3,3,0 */
11982                 }
11983               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11984               /* Zap the reloc on the _tls_get_addr call too.  */
11985               BFD_ASSERT (offset == rel[1].r_offset);
11986               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
11987               insn3 = bfd_get_32 (output_bfd,
11988                                   contents + offset + 4);
11989               if (insn3 == NOP
11990                   || insn3 == CROR_151515 || insn3 == CROR_313131)
11991                 {
11992                   rel->r_offset += 4;
11993                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
11994                   insn2 = NOP;
11995                 }
11996               bfd_put_32 (output_bfd, insn2, contents + offset);
11997               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
11998                 {
11999                   rel--;
12000                   continue;
12001                 }
12002             }
12003           break;
12004
12005         case R_PPC64_TLSLD:
12006           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12007             {
12008               unsigned int insn2, insn3;
12009               bfd_vma offset = rel->r_offset;
12010
12011               if (toc_symndx)
12012                 sec = local_sections[toc_symndx];
12013               for (r_symndx = 0;
12014                    r_symndx < symtab_hdr->sh_info;
12015                    r_symndx++)
12016                 if (local_sections[r_symndx] == sec)
12017                   break;
12018               if (r_symndx >= symtab_hdr->sh_info)
12019                 r_symndx = 0;
12020               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12021               if (r_symndx != 0)
12022                 rel->r_addend -= (local_syms[r_symndx].st_value
12023                                   + sec->output_offset
12024                                   + sec->output_section->vma);
12025
12026               r_type = R_PPC64_TPREL16_LO;
12027               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12028               rel->r_offset = offset + d_offset;
12029               /* Zap the reloc on the _tls_get_addr call too.  */
12030               BFD_ASSERT (offset == rel[1].r_offset);
12031               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12032               insn2 = 0x38630000;       /* addi 3,3,0 */
12033               insn3 = bfd_get_32 (output_bfd,
12034                                   contents + offset + 4);
12035               if (insn3 == NOP
12036                   || insn3 == CROR_151515 || insn3 == CROR_313131)
12037                 {
12038                   rel->r_offset += 4;
12039                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12040                   insn2 = NOP;
12041                 }
12042               bfd_put_32 (output_bfd, insn2, contents + offset);
12043               rel--;
12044               continue;
12045             }
12046           break;
12047
12048         case R_PPC64_DTPMOD64:
12049           if (rel + 1 < relend
12050               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12051               && rel[1].r_offset == rel->r_offset + 8)
12052             {
12053               if ((tls_mask & TLS_GD) == 0)
12054                 {
12055                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12056                   if ((tls_mask & TLS_TPRELGD) != 0)
12057                     r_type = R_PPC64_TPREL64;
12058                   else
12059                     {
12060                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12061                       r_type = R_PPC64_NONE;
12062                     }
12063                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12064                 }
12065             }
12066           else
12067             {
12068               if ((tls_mask & TLS_LD) == 0)
12069                 {
12070                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12071                   r_type = R_PPC64_NONE;
12072                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12073                 }
12074             }
12075           break;
12076
12077         case R_PPC64_TPREL64:
12078           if ((tls_mask & TLS_TPREL) == 0)
12079             {
12080               r_type = R_PPC64_NONE;
12081               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12082             }
12083           break;
12084         }
12085
12086       /* Handle other relocations that tweak non-addend part of insn.  */
12087       insn = 0;
12088       max_br_offset = 1 << 25;
12089       addend = rel->r_addend;
12090       switch (r_type)
12091         {
12092         default:
12093           break;
12094
12095           /* Branch taken prediction relocations.  */
12096         case R_PPC64_ADDR14_BRTAKEN:
12097         case R_PPC64_REL14_BRTAKEN:
12098           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
12099           /* Fall thru.  */
12100
12101           /* Branch not taken prediction relocations.  */
12102         case R_PPC64_ADDR14_BRNTAKEN:
12103         case R_PPC64_REL14_BRNTAKEN:
12104           insn |= bfd_get_32 (output_bfd,
12105                               contents + rel->r_offset) & ~(0x01 << 21);
12106           /* Fall thru.  */
12107
12108         case R_PPC64_REL14:
12109           max_br_offset = 1 << 15;
12110           /* Fall thru.  */
12111
12112         case R_PPC64_REL24:
12113           /* Calls to functions with a different TOC, such as calls to
12114              shared objects, need to alter the TOC pointer.  This is
12115              done using a linkage stub.  A REL24 branching to these
12116              linkage stubs needs to be followed by a nop, as the nop
12117              will be replaced with an instruction to restore the TOC
12118              base pointer.  */
12119           fdh = h;
12120           if (h != NULL
12121               && h->oh != NULL
12122               && h->oh->is_func_descriptor)
12123             fdh = ppc_follow_link (h->oh);
12124           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12125           if (stub_entry != NULL
12126               && (stub_entry->stub_type == ppc_stub_plt_call
12127                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12128                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
12129             {
12130               bfd_boolean can_plt_call = FALSE;
12131
12132               if (rel->r_offset + 8 <= input_section->size)
12133                 {
12134                   unsigned long nop;
12135                   nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12136                   if (nop == NOP
12137                       || nop == CROR_151515 || nop == CROR_313131)
12138                     {
12139                       if (h != NULL
12140                           && (h == htab->tls_get_addr_fd
12141                               || h == htab->tls_get_addr)
12142                           && !htab->no_tls_get_addr_opt)
12143                         {
12144                           /* Special stub used, leave nop alone.  */
12145                         }
12146                       else
12147                         bfd_put_32 (input_bfd, LD_R2_40R1,
12148                                     contents + rel->r_offset + 4);
12149                       can_plt_call = TRUE;
12150                     }
12151                 }
12152
12153               if (!can_plt_call)
12154                 {
12155                   if (stub_entry->stub_type == ppc_stub_plt_call)
12156                     {
12157                       /* If this is a plain branch rather than a branch
12158                          and link, don't require a nop.  However, don't
12159                          allow tail calls in a shared library as they
12160                          will result in r2 being corrupted.  */
12161                       unsigned long br;
12162                       br = bfd_get_32 (input_bfd, contents + rel->r_offset);
12163                       if (info->executable && (br & 1) == 0)
12164                         can_plt_call = TRUE;
12165                       else
12166                         stub_entry = NULL;
12167                     }
12168                   else if (h != NULL
12169                            && strcmp (h->elf.root.root.string,
12170                                       ".__libc_start_main") == 0)
12171                     {
12172                       /* Allow crt1 branch to go via a toc adjusting stub.  */
12173                       can_plt_call = TRUE;
12174                     }
12175                   else
12176                     {
12177                       if (strcmp (input_section->output_section->name,
12178                                   ".init") == 0
12179                           || strcmp (input_section->output_section->name,
12180                                      ".fini") == 0)
12181                         (*_bfd_error_handler)
12182                           (_("%B(%A+0x%lx): automatic multiple TOCs "
12183                              "not supported using your crt files; "
12184                              "recompile with -mminimal-toc or upgrade gcc"),
12185                            input_bfd,
12186                            input_section,
12187                            (long) rel->r_offset);
12188                       else
12189                         (*_bfd_error_handler)
12190                           (_("%B(%A+0x%lx): sibling call optimization to `%s' "
12191                              "does not allow automatic multiple TOCs; "
12192                              "recompile with -mminimal-toc or "
12193                              "-fno-optimize-sibling-calls, "
12194                              "or make `%s' extern"),
12195                            input_bfd,
12196                            input_section,
12197                            (long) rel->r_offset,
12198                            sym_name,
12199                            sym_name);
12200                       bfd_set_error (bfd_error_bad_value);
12201                       ret = FALSE;
12202                     }
12203                 }
12204
12205               if (can_plt_call
12206                   && stub_entry->stub_type == ppc_stub_plt_call)
12207                 unresolved_reloc = FALSE;
12208             }
12209
12210           if ((stub_entry == NULL
12211                || stub_entry->stub_type == ppc_stub_long_branch
12212                || stub_entry->stub_type == ppc_stub_plt_branch)
12213               && get_opd_info (sec) != NULL)
12214             {
12215               /* The branch destination is the value of the opd entry. */
12216               bfd_vma off = (relocation + addend
12217                              - sec->output_section->vma
12218                              - sec->output_offset);
12219               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12220               if (dest != (bfd_vma) -1)
12221                 {
12222                   relocation = dest;
12223                   addend = 0;
12224                 }
12225             }
12226
12227           /* If the branch is out of reach we ought to have a long
12228              branch stub.  */
12229           from = (rel->r_offset
12230                   + input_section->output_offset
12231                   + input_section->output_section->vma);
12232
12233           if (stub_entry != NULL
12234               && (stub_entry->stub_type == ppc_stub_long_branch
12235                   || stub_entry->stub_type == ppc_stub_plt_branch)
12236               && (r_type == R_PPC64_ADDR14_BRTAKEN
12237                   || r_type == R_PPC64_ADDR14_BRNTAKEN
12238                   || (relocation + addend - from + max_br_offset
12239                       < 2 * max_br_offset)))
12240             /* Don't use the stub if this branch is in range.  */
12241             stub_entry = NULL;
12242
12243           if (stub_entry != NULL)
12244             {
12245               /* Munge up the value and addend so that we call the stub
12246                  rather than the procedure directly.  */
12247               relocation = (stub_entry->stub_offset
12248                             + stub_entry->stub_sec->output_offset
12249                             + stub_entry->stub_sec->output_section->vma);
12250               addend = 0;
12251             }
12252
12253           if (insn != 0)
12254             {
12255               if (is_power4)
12256                 {
12257                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
12258                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
12259                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
12260                   if ((insn & (0x14 << 21)) == (0x04 << 21))
12261                     insn |= 0x02 << 21;
12262                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
12263                     insn |= 0x08 << 21;
12264                   else
12265                     break;
12266                 }
12267               else
12268                 {
12269                   /* Invert 'y' bit if not the default.  */
12270                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
12271                     insn ^= 0x01 << 21;
12272                 }
12273
12274               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12275             }
12276
12277           /* NOP out calls to undefined weak functions.
12278              We can thus call a weak function without first
12279              checking whether the function is defined.  */
12280           else if (h != NULL
12281                    && h->elf.root.type == bfd_link_hash_undefweak
12282                    && h->elf.dynindx == -1
12283                    && r_type == R_PPC64_REL24
12284                    && relocation == 0
12285                    && addend == 0)
12286             {
12287               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12288               continue;
12289             }
12290           break;
12291         }
12292
12293       /* Set `addend'.  */
12294       tls_type = 0;
12295       switch (r_type)
12296         {
12297         default:
12298           (*_bfd_error_handler)
12299             (_("%B: unknown relocation type %d for symbol %s"),
12300              input_bfd, (int) r_type, sym_name);
12301
12302           bfd_set_error (bfd_error_bad_value);
12303           ret = FALSE;
12304           continue;
12305
12306         case R_PPC64_NONE:
12307         case R_PPC64_TLS:
12308         case R_PPC64_TLSGD:
12309         case R_PPC64_TLSLD:
12310         case R_PPC64_GNU_VTINHERIT:
12311         case R_PPC64_GNU_VTENTRY:
12312           continue;
12313
12314           /* GOT16 relocations.  Like an ADDR16 using the symbol's
12315              address in the GOT as relocation value instead of the
12316              symbol's value itself.  Also, create a GOT entry for the
12317              symbol and put the symbol value there.  */
12318         case R_PPC64_GOT_TLSGD16:
12319         case R_PPC64_GOT_TLSGD16_LO:
12320         case R_PPC64_GOT_TLSGD16_HI:
12321         case R_PPC64_GOT_TLSGD16_HA:
12322           tls_type = TLS_TLS | TLS_GD;
12323           goto dogot;
12324
12325         case R_PPC64_GOT_TLSLD16:
12326         case R_PPC64_GOT_TLSLD16_LO:
12327         case R_PPC64_GOT_TLSLD16_HI:
12328         case R_PPC64_GOT_TLSLD16_HA:
12329           tls_type = TLS_TLS | TLS_LD;
12330           goto dogot;
12331
12332         case R_PPC64_GOT_TPREL16_DS:
12333         case R_PPC64_GOT_TPREL16_LO_DS:
12334         case R_PPC64_GOT_TPREL16_HI:
12335         case R_PPC64_GOT_TPREL16_HA:
12336           tls_type = TLS_TLS | TLS_TPREL;
12337           goto dogot;
12338
12339         case R_PPC64_GOT_DTPREL16_DS:
12340         case R_PPC64_GOT_DTPREL16_LO_DS:
12341         case R_PPC64_GOT_DTPREL16_HI:
12342         case R_PPC64_GOT_DTPREL16_HA:
12343           tls_type = TLS_TLS | TLS_DTPREL;
12344           goto dogot;
12345
12346         case R_PPC64_GOT16:
12347         case R_PPC64_GOT16_LO:
12348         case R_PPC64_GOT16_HI:
12349         case R_PPC64_GOT16_HA:
12350         case R_PPC64_GOT16_DS:
12351         case R_PPC64_GOT16_LO_DS:
12352         dogot:
12353           {
12354             /* Relocation is to the entry for this symbol in the global
12355                offset table.  */
12356             asection *got;
12357             bfd_vma *offp;
12358             bfd_vma off;
12359             unsigned long indx = 0;
12360             struct got_entry *ent;
12361
12362             if (tls_type == (TLS_TLS | TLS_LD)
12363                 && (h == NULL
12364                     || !h->elf.def_dynamic))
12365               ent = ppc64_tlsld_got (input_bfd);
12366             else
12367               {
12368
12369                 if (h != NULL)
12370                   {
12371                     bfd_boolean dyn = htab->elf.dynamic_sections_created;
12372                     if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
12373                                                           &h->elf)
12374                         || (info->shared
12375                             && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
12376                       /* This is actually a static link, or it is a
12377                          -Bsymbolic link and the symbol is defined
12378                          locally, or the symbol was forced to be local
12379                          because of a version file.  */
12380                       ;
12381                     else
12382                       {
12383                         indx = h->elf.dynindx;
12384                         unresolved_reloc = FALSE;
12385                       }
12386                     ent = h->elf.got.glist;
12387                   }
12388                 else
12389                   {
12390                     if (local_got_ents == NULL)
12391                       abort ();
12392                     ent = local_got_ents[r_symndx];
12393                   }
12394
12395                 for (; ent != NULL; ent = ent->next)
12396                   if (ent->addend == orig_addend
12397                       && ent->owner == input_bfd
12398                       && ent->tls_type == tls_type)
12399                     break;
12400               }
12401
12402             if (ent == NULL)
12403               abort ();
12404             if (ent->is_indirect)
12405               ent = ent->got.ent;
12406             offp = &ent->got.offset;
12407             got = ppc64_elf_tdata (ent->owner)->got;
12408             if (got == NULL)
12409               abort ();
12410
12411             /* The offset must always be a multiple of 8.  We use the
12412                least significant bit to record whether we have already
12413                processed this entry.  */
12414             off = *offp;
12415             if ((off & 1) != 0)
12416               off &= ~1;
12417             else
12418               {
12419                 /* Generate relocs for the dynamic linker, except in
12420                    the case of TLSLD where we'll use one entry per
12421                    module.  */
12422                 asection *relgot;
12423                 bfd_boolean ifunc;
12424
12425                 *offp = off | 1;
12426                 relgot = NULL;
12427                 ifunc = (h != NULL
12428                          ? h->elf.type == STT_GNU_IFUNC
12429                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
12430                 if ((info->shared || indx != 0)
12431                     && (h == NULL
12432                         || (tls_type == (TLS_TLS | TLS_LD)
12433                             && !h->elf.def_dynamic)
12434                         || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12435                         || h->elf.root.type != bfd_link_hash_undefweak))
12436                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
12437                 else if (ifunc)
12438                   relgot = htab->reliplt;
12439                 if (relgot != NULL)
12440                   {
12441                     outrel.r_offset = (got->output_section->vma
12442                                        + got->output_offset
12443                                        + off);
12444                     outrel.r_addend = addend;
12445                     if (tls_type & (TLS_LD | TLS_GD))
12446                       {
12447                         outrel.r_addend = 0;
12448                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
12449                         if (tls_type == (TLS_TLS | TLS_GD))
12450                           {
12451                             loc = relgot->contents;
12452                             loc += (relgot->reloc_count++
12453                                     * sizeof (Elf64_External_Rela));
12454                             bfd_elf64_swap_reloca_out (output_bfd,
12455                                                        &outrel, loc);
12456                             outrel.r_offset += 8;
12457                             outrel.r_addend = addend;
12458                             outrel.r_info
12459                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
12460                           }
12461                       }
12462                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
12463                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
12464                     else if (tls_type == (TLS_TLS | TLS_TPREL))
12465                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
12466                     else if (indx != 0)
12467                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
12468                     else
12469                       {
12470                         if (ifunc)
12471                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12472                         else
12473                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12474
12475                         /* Write the .got section contents for the sake
12476                            of prelink.  */
12477                         loc = got->contents + off;
12478                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
12479                                     loc);
12480                       }
12481
12482                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
12483                       {
12484                         outrel.r_addend += relocation;
12485                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
12486                           outrel.r_addend -= htab->elf.tls_sec->vma;
12487                       }
12488                     loc = relgot->contents;
12489                     loc += (relgot->reloc_count++
12490                             * sizeof (Elf64_External_Rela));
12491                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12492                   }
12493
12494                 /* Init the .got section contents here if we're not
12495                    emitting a reloc.  */
12496                 else
12497                   {
12498                     relocation += addend;
12499                     if (tls_type == (TLS_TLS | TLS_LD))
12500                       relocation = 1;
12501                     else if (tls_type != 0)
12502                       {
12503                         relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
12504                         if (tls_type == (TLS_TLS | TLS_TPREL))
12505                           relocation += DTP_OFFSET - TP_OFFSET;
12506
12507                         if (tls_type == (TLS_TLS | TLS_GD))
12508                           {
12509                             bfd_put_64 (output_bfd, relocation,
12510                                         got->contents + off + 8);
12511                             relocation = 1;
12512                           }
12513                       }
12514
12515                     bfd_put_64 (output_bfd, relocation,
12516                                 got->contents + off);
12517                   }
12518               }
12519
12520             if (off >= (bfd_vma) -2)
12521               abort ();
12522
12523             relocation = got->output_section->vma + got->output_offset + off;
12524             addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
12525           }
12526           break;
12527
12528         case R_PPC64_PLT16_HA:
12529         case R_PPC64_PLT16_HI:
12530         case R_PPC64_PLT16_LO:
12531         case R_PPC64_PLT32:
12532         case R_PPC64_PLT64:
12533           /* Relocation is to the entry for this symbol in the
12534              procedure linkage table.  */
12535
12536           /* Resolve a PLT reloc against a local symbol directly,
12537              without using the procedure linkage table.  */
12538           if (h == NULL)
12539             break;
12540
12541           /* It's possible that we didn't make a PLT entry for this
12542              symbol.  This happens when statically linking PIC code,
12543              or when using -Bsymbolic.  Go find a match if there is a
12544              PLT entry.  */
12545           if (htab->plt != NULL)
12546             {
12547               struct plt_entry *ent;
12548               for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
12549                 if (ent->addend == orig_addend
12550                     && ent->plt.offset != (bfd_vma) -1)
12551                   {
12552                     relocation = (htab->plt->output_section->vma
12553                                   + htab->plt->output_offset
12554                                   + ent->plt.offset);
12555                     unresolved_reloc = FALSE;
12556                   }
12557             }
12558           break;
12559
12560         case R_PPC64_TOC:
12561           /* Relocation value is TOC base.  */
12562           relocation = TOCstart;
12563           if (r_symndx == 0)
12564             relocation += htab->stub_group[input_section->id].toc_off;
12565           else if (unresolved_reloc)
12566             ;
12567           else if (sec != NULL && sec->id <= htab->top_id)
12568             relocation += htab->stub_group[sec->id].toc_off;
12569           else
12570             unresolved_reloc = TRUE;
12571           goto dodyn;
12572
12573           /* TOC16 relocs.  We want the offset relative to the TOC base,
12574              which is the address of the start of the TOC plus 0x8000.
12575              The TOC consists of sections .got, .toc, .tocbss, and .plt,
12576              in this order.  */
12577         case R_PPC64_TOC16:
12578         case R_PPC64_TOC16_LO:
12579         case R_PPC64_TOC16_HI:
12580         case R_PPC64_TOC16_DS:
12581         case R_PPC64_TOC16_LO_DS:
12582         case R_PPC64_TOC16_HA:
12583           addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
12584           break;
12585
12586           /* Relocate against the beginning of the section.  */
12587         case R_PPC64_SECTOFF:
12588         case R_PPC64_SECTOFF_LO:
12589         case R_PPC64_SECTOFF_HI:
12590         case R_PPC64_SECTOFF_DS:
12591         case R_PPC64_SECTOFF_LO_DS:
12592         case R_PPC64_SECTOFF_HA:
12593           if (sec != NULL)
12594             addend -= sec->output_section->vma;
12595           break;
12596
12597         case R_PPC64_REL16:
12598         case R_PPC64_REL16_LO:
12599         case R_PPC64_REL16_HI:
12600         case R_PPC64_REL16_HA:
12601           break;
12602
12603         case R_PPC64_REL14:
12604         case R_PPC64_REL14_BRNTAKEN:
12605         case R_PPC64_REL14_BRTAKEN:
12606         case R_PPC64_REL24:
12607           break;
12608
12609         case R_PPC64_TPREL16:
12610         case R_PPC64_TPREL16_LO:
12611         case R_PPC64_TPREL16_HI:
12612         case R_PPC64_TPREL16_HA:
12613         case R_PPC64_TPREL16_DS:
12614         case R_PPC64_TPREL16_LO_DS:
12615         case R_PPC64_TPREL16_HIGHER:
12616         case R_PPC64_TPREL16_HIGHERA:
12617         case R_PPC64_TPREL16_HIGHEST:
12618         case R_PPC64_TPREL16_HIGHESTA:
12619           if (h != NULL
12620               && h->elf.root.type == bfd_link_hash_undefweak
12621               && h->elf.dynindx == -1)
12622             {
12623               /* Make this relocation against an undefined weak symbol
12624                  resolve to zero.  This is really just a tweak, since
12625                  code using weak externs ought to check that they are
12626                  defined before using them.  */
12627               bfd_byte *p = contents + rel->r_offset - d_offset;
12628
12629               insn = bfd_get_32 (output_bfd, p);
12630               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
12631               if (insn != 0)
12632                 bfd_put_32 (output_bfd, insn, p);
12633               break;
12634             }
12635           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
12636           if (info->shared)
12637             /* The TPREL16 relocs shouldn't really be used in shared
12638                libs as they will result in DT_TEXTREL being set, but
12639                support them anyway.  */
12640             goto dodyn;
12641           break;
12642
12643         case R_PPC64_DTPREL16:
12644         case R_PPC64_DTPREL16_LO:
12645         case R_PPC64_DTPREL16_HI:
12646         case R_PPC64_DTPREL16_HA:
12647         case R_PPC64_DTPREL16_DS:
12648         case R_PPC64_DTPREL16_LO_DS:
12649         case R_PPC64_DTPREL16_HIGHER:
12650         case R_PPC64_DTPREL16_HIGHERA:
12651         case R_PPC64_DTPREL16_HIGHEST:
12652         case R_PPC64_DTPREL16_HIGHESTA:
12653           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
12654           break;
12655
12656         case R_PPC64_DTPMOD64:
12657           relocation = 1;
12658           addend = 0;
12659           goto dodyn;
12660
12661         case R_PPC64_TPREL64:
12662           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
12663           goto dodyn;
12664
12665         case R_PPC64_DTPREL64:
12666           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
12667           /* Fall thru */
12668
12669           /* Relocations that may need to be propagated if this is a
12670              dynamic object.  */
12671         case R_PPC64_REL30:
12672         case R_PPC64_REL32:
12673         case R_PPC64_REL64:
12674         case R_PPC64_ADDR14:
12675         case R_PPC64_ADDR14_BRNTAKEN:
12676         case R_PPC64_ADDR14_BRTAKEN:
12677         case R_PPC64_ADDR16:
12678         case R_PPC64_ADDR16_DS:
12679         case R_PPC64_ADDR16_HA:
12680         case R_PPC64_ADDR16_HI:
12681         case R_PPC64_ADDR16_HIGHER:
12682         case R_PPC64_ADDR16_HIGHERA:
12683         case R_PPC64_ADDR16_HIGHEST:
12684         case R_PPC64_ADDR16_HIGHESTA:
12685         case R_PPC64_ADDR16_LO:
12686         case R_PPC64_ADDR16_LO_DS:
12687         case R_PPC64_ADDR24:
12688         case R_PPC64_ADDR32:
12689         case R_PPC64_ADDR64:
12690         case R_PPC64_UADDR16:
12691         case R_PPC64_UADDR32:
12692         case R_PPC64_UADDR64:
12693         dodyn:
12694           if ((input_section->flags & SEC_ALLOC) == 0)
12695             break;
12696
12697           if (NO_OPD_RELOCS && is_opd)
12698             break;
12699
12700           if ((info->shared
12701                && (h == NULL
12702                    || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12703                    || h->elf.root.type != bfd_link_hash_undefweak)
12704                && (must_be_dyn_reloc (info, r_type)
12705                    || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
12706               || (ELIMINATE_COPY_RELOCS
12707                   && !info->shared
12708                   && h != NULL
12709                   && h->elf.dynindx != -1
12710                   && !h->elf.non_got_ref
12711                   && !h->elf.def_regular)
12712               || (!info->shared
12713                   && (h != NULL
12714                       ? h->elf.type == STT_GNU_IFUNC
12715                       : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
12716             {
12717               bfd_boolean skip, relocate;
12718               asection *sreloc;
12719               bfd_vma out_off;
12720
12721               /* When generating a dynamic object, these relocations
12722                  are copied into the output file to be resolved at run
12723                  time.  */
12724
12725               skip = FALSE;
12726               relocate = FALSE;
12727
12728               out_off = _bfd_elf_section_offset (output_bfd, info,
12729                                                  input_section, rel->r_offset);
12730               if (out_off == (bfd_vma) -1)
12731                 skip = TRUE;
12732               else if (out_off == (bfd_vma) -2)
12733                 skip = TRUE, relocate = TRUE;
12734               out_off += (input_section->output_section->vma
12735                           + input_section->output_offset);
12736               outrel.r_offset = out_off;
12737               outrel.r_addend = rel->r_addend;
12738
12739               /* Optimize unaligned reloc use.  */
12740               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
12741                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
12742                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
12743               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
12744                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
12745                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
12746               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
12747                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
12748                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
12749
12750               if (skip)
12751                 memset (&outrel, 0, sizeof outrel);
12752               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
12753                        && !is_opd
12754                        && r_type != R_PPC64_TOC)
12755                 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
12756               else
12757                 {
12758                   /* This symbol is local, or marked to become local,
12759                      or this is an opd section reloc which must point
12760                      at a local function.  */
12761                   outrel.r_addend += relocation;
12762                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
12763                     {
12764                       if (is_opd && h != NULL)
12765                         {
12766                           /* Lie about opd entries.  This case occurs
12767                              when building shared libraries and we
12768                              reference a function in another shared
12769                              lib.  The same thing happens for a weak
12770                              definition in an application that's
12771                              overridden by a strong definition in a
12772                              shared lib.  (I believe this is a generic
12773                              bug in binutils handling of weak syms.)
12774                              In these cases we won't use the opd
12775                              entry in this lib.  */
12776                           unresolved_reloc = FALSE;
12777                         }
12778                       if (!is_opd
12779                           && r_type == R_PPC64_ADDR64
12780                           && (h != NULL
12781                               ? h->elf.type == STT_GNU_IFUNC
12782                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
12783                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12784                       else
12785                         {
12786                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12787
12788                           /* We need to relocate .opd contents for ld.so.
12789                              Prelink also wants simple and consistent rules
12790                              for relocs.  This make all RELATIVE relocs have
12791                              *r_offset equal to r_addend.  */
12792                           relocate = TRUE;
12793                         }
12794                     }
12795                   else
12796                     {
12797                       long indx = 0;
12798
12799                       if (h != NULL
12800                           ? h->elf.type == STT_GNU_IFUNC
12801                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12802                         {
12803                           (*_bfd_error_handler)
12804                             (_("%B(%A+0x%lx): relocation %s for indirect "
12805                                "function %s unsupported"),
12806                              input_bfd,
12807                              input_section,
12808                              (long) rel->r_offset,
12809                              ppc64_elf_howto_table[r_type]->name,
12810                              sym_name);
12811                           ret = FALSE;
12812                         }
12813                       else if (r_symndx == 0 || bfd_is_abs_section (sec))
12814                         ;
12815                       else if (sec == NULL || sec->owner == NULL)
12816                         {
12817                           bfd_set_error (bfd_error_bad_value);
12818                           return FALSE;
12819                         }
12820                       else
12821                         {
12822                           asection *osec;
12823
12824                           osec = sec->output_section;
12825                           indx = elf_section_data (osec)->dynindx;
12826
12827                           if (indx == 0)
12828                             {
12829                               if ((osec->flags & SEC_READONLY) == 0
12830                                   && htab->elf.data_index_section != NULL)
12831                                 osec = htab->elf.data_index_section;
12832                               else
12833                                 osec = htab->elf.text_index_section;
12834                               indx = elf_section_data (osec)->dynindx;
12835                             }
12836                           BFD_ASSERT (indx != 0);
12837
12838                           /* We are turning this relocation into one
12839                              against a section symbol, so subtract out
12840                              the output section's address but not the
12841                              offset of the input section in the output
12842                              section.  */
12843                           outrel.r_addend -= osec->vma;
12844                         }
12845
12846                       outrel.r_info = ELF64_R_INFO (indx, r_type);
12847                     }
12848                 }
12849
12850               sreloc = elf_section_data (input_section)->sreloc;
12851               if (!htab->elf.dynamic_sections_created)
12852                 sreloc = htab->reliplt;
12853               if (sreloc == NULL)
12854                 abort ();
12855
12856               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
12857                   >= sreloc->size)
12858                 abort ();
12859               loc = sreloc->contents;
12860               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
12861               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12862
12863               /* If this reloc is against an external symbol, it will
12864                  be computed at runtime, so there's no need to do
12865                  anything now.  However, for the sake of prelink ensure
12866                  that the section contents are a known value.  */
12867               if (! relocate)
12868                 {
12869                   unresolved_reloc = FALSE;
12870                   /* The value chosen here is quite arbitrary as ld.so
12871                      ignores section contents except for the special
12872                      case of .opd where the contents might be accessed
12873                      before relocation.  Choose zero, as that won't
12874                      cause reloc overflow.  */
12875                   relocation = 0;
12876                   addend = 0;
12877                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
12878                      to improve backward compatibility with older
12879                      versions of ld.  */
12880                   if (r_type == R_PPC64_ADDR64)
12881                     addend = outrel.r_addend;
12882                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
12883                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
12884                     addend = (input_section->output_section->vma
12885                               + input_section->output_offset
12886                               + rel->r_offset);
12887                 }
12888             }
12889           break;
12890
12891         case R_PPC64_COPY:
12892         case R_PPC64_GLOB_DAT:
12893         case R_PPC64_JMP_SLOT:
12894         case R_PPC64_JMP_IREL:
12895         case R_PPC64_RELATIVE:
12896           /* We shouldn't ever see these dynamic relocs in relocatable
12897              files.  */
12898           /* Fall through.  */
12899
12900         case R_PPC64_PLTGOT16:
12901         case R_PPC64_PLTGOT16_DS:
12902         case R_PPC64_PLTGOT16_HA:
12903         case R_PPC64_PLTGOT16_HI:
12904         case R_PPC64_PLTGOT16_LO:
12905         case R_PPC64_PLTGOT16_LO_DS:
12906         case R_PPC64_PLTREL32:
12907         case R_PPC64_PLTREL64:
12908           /* These ones haven't been implemented yet.  */
12909
12910           (*_bfd_error_handler)
12911             (_("%B: relocation %s is not supported for symbol %s."),
12912              input_bfd,
12913              ppc64_elf_howto_table[r_type]->name, sym_name);
12914
12915           bfd_set_error (bfd_error_invalid_operation);
12916           ret = FALSE;
12917           continue;
12918         }
12919
12920       /* Multi-instruction sequences that access the TOC can be
12921          optimized, eg. addis ra,r2,0; addi rb,ra,x;
12922          to             nop;           addi rb,r2,x;  */
12923       switch (r_type)
12924         {
12925         default:
12926           break;
12927
12928         case R_PPC64_GOT_TLSLD16_HI:
12929         case R_PPC64_GOT_TLSGD16_HI:
12930         case R_PPC64_GOT_TPREL16_HI:
12931         case R_PPC64_GOT_DTPREL16_HI:
12932         case R_PPC64_GOT16_HI:
12933         case R_PPC64_TOC16_HI:
12934           /* These relocs would only be useful if building up an
12935              offset to later add to r2, perhaps in an indexed
12936              addressing mode instruction.  Don't try to optimize.
12937              Unfortunately, the possibility of someone building up an
12938              offset like this or even with the HA relocs, means that
12939              we need to check the high insn when optimizing the low
12940              insn.  */
12941           break;
12942
12943         case R_PPC64_GOT_TLSLD16_HA:
12944         case R_PPC64_GOT_TLSGD16_HA:
12945         case R_PPC64_GOT_TPREL16_HA:
12946         case R_PPC64_GOT_DTPREL16_HA:
12947         case R_PPC64_GOT16_HA:
12948         case R_PPC64_TOC16_HA:
12949           /* For now we don't nop out the first instruction.  */
12950           break;
12951
12952         case R_PPC64_GOT_TLSLD16_LO:
12953         case R_PPC64_GOT_TLSGD16_LO:
12954         case R_PPC64_GOT_TPREL16_LO_DS:
12955         case R_PPC64_GOT_DTPREL16_LO_DS:
12956         case R_PPC64_GOT16_LO:
12957         case R_PPC64_GOT16_LO_DS:
12958         case R_PPC64_TOC16_LO:
12959         case R_PPC64_TOC16_LO_DS:
12960           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000)
12961             {
12962               bfd_byte *p = contents + (rel->r_offset & ~3);
12963               insn = bfd_get_32 (input_bfd, p);
12964               if ((insn & (0x3f << 26)) == 14u << 26 /* addi */
12965                   || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
12966                   || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
12967                   || (insn & (0x3f << 26)) == 36u << 26 /* stw */
12968                   || (insn & (0x3f << 26)) == 38u << 26 /* stb */
12969                   || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
12970                   || (insn & (0x3f << 26)) == 42u << 26 /* lha */
12971                   || (insn & (0x3f << 26)) == 44u << 26 /* sth */
12972                   || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
12973                   || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
12974                   || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
12975                   || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
12976                   || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
12977                   || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
12978                   || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
12979                       && (insn & 3) != 1)
12980                   || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
12981                       && ((insn & 3) == 0 || (insn & 3) == 3)))
12982                 {
12983                   unsigned int reg = (insn >> 16) & 0x1f;
12984                   if (ha_reloc_match (relocs, rel, reg, input_bfd, contents))
12985                     {
12986                       insn &= ~(0x1f << 16);
12987                       insn |= 2 << 16;
12988                       bfd_put_32 (input_bfd, insn, p);
12989                     }
12990                 }
12991             }
12992           break;
12993         }
12994
12995       /* Do any further special processing.  */
12996       switch (r_type)
12997         {
12998         default:
12999           break;
13000
13001         case R_PPC64_ADDR16_HA:
13002         case R_PPC64_REL16_HA:
13003         case R_PPC64_ADDR16_HIGHERA:
13004         case R_PPC64_ADDR16_HIGHESTA:
13005         case R_PPC64_TOC16_HA:
13006         case R_PPC64_SECTOFF_HA:
13007         case R_PPC64_TPREL16_HA:
13008         case R_PPC64_DTPREL16_HA:
13009         case R_PPC64_TPREL16_HIGHER:
13010         case R_PPC64_TPREL16_HIGHERA:
13011         case R_PPC64_TPREL16_HIGHEST:
13012         case R_PPC64_TPREL16_HIGHESTA:
13013         case R_PPC64_DTPREL16_HIGHER:
13014         case R_PPC64_DTPREL16_HIGHERA:
13015         case R_PPC64_DTPREL16_HIGHEST:
13016         case R_PPC64_DTPREL16_HIGHESTA:
13017           /* It's just possible that this symbol is a weak symbol
13018              that's not actually defined anywhere. In that case,
13019              'sec' would be NULL, and we should leave the symbol
13020              alone (it will be set to zero elsewhere in the link).  */
13021           if (sec == NULL)
13022             break;
13023           /* Fall thru */
13024
13025         case R_PPC64_GOT16_HA:
13026         case R_PPC64_PLTGOT16_HA:
13027         case R_PPC64_PLT16_HA:
13028         case R_PPC64_GOT_TLSGD16_HA:
13029         case R_PPC64_GOT_TLSLD16_HA:
13030         case R_PPC64_GOT_TPREL16_HA:
13031         case R_PPC64_GOT_DTPREL16_HA:
13032           /* Add 0x10000 if sign bit in 0:15 is set.
13033              Bits 0:15 are not used.  */
13034           addend += 0x8000;
13035           break;
13036
13037         case R_PPC64_ADDR16_DS:
13038         case R_PPC64_ADDR16_LO_DS:
13039         case R_PPC64_GOT16_DS:
13040         case R_PPC64_GOT16_LO_DS:
13041         case R_PPC64_PLT16_LO_DS:
13042         case R_PPC64_SECTOFF_DS:
13043         case R_PPC64_SECTOFF_LO_DS:
13044         case R_PPC64_TOC16_DS:
13045         case R_PPC64_TOC16_LO_DS:
13046         case R_PPC64_PLTGOT16_DS:
13047         case R_PPC64_PLTGOT16_LO_DS:
13048         case R_PPC64_GOT_TPREL16_DS:
13049         case R_PPC64_GOT_TPREL16_LO_DS:
13050         case R_PPC64_GOT_DTPREL16_DS:
13051         case R_PPC64_GOT_DTPREL16_LO_DS:
13052         case R_PPC64_TPREL16_DS:
13053         case R_PPC64_TPREL16_LO_DS:
13054         case R_PPC64_DTPREL16_DS:
13055         case R_PPC64_DTPREL16_LO_DS:
13056           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13057           mask = 3;
13058           /* If this reloc is against an lq insn, then the value must be
13059              a multiple of 16.  This is somewhat of a hack, but the
13060              "correct" way to do this by defining _DQ forms of all the
13061              _DS relocs bloats all reloc switches in this file.  It
13062              doesn't seem to make much sense to use any of these relocs
13063              in data, so testing the insn should be safe.  */
13064           if ((insn & (0x3f << 26)) == (56u << 26))
13065             mask = 15;
13066           if (((relocation + addend) & mask) != 0)
13067             {
13068               (*_bfd_error_handler)
13069                 (_("%B: error: relocation %s not a multiple of %d"),
13070                  input_bfd,
13071                  ppc64_elf_howto_table[r_type]->name,
13072                  mask + 1);
13073               bfd_set_error (bfd_error_bad_value);
13074               ret = FALSE;
13075               continue;
13076             }
13077           break;
13078         }
13079
13080       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13081          because such sections are not SEC_ALLOC and thus ld.so will
13082          not process them.  */
13083       if (unresolved_reloc
13084           && !((input_section->flags & SEC_DEBUGGING) != 0
13085                && h->elf.def_dynamic))
13086         {
13087           (*_bfd_error_handler)
13088             (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
13089              input_bfd,
13090              input_section,
13091              (long) rel->r_offset,
13092              ppc64_elf_howto_table[(int) r_type]->name,
13093              h->elf.root.root.string);
13094           ret = FALSE;
13095         }
13096
13097       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
13098                                     input_bfd,
13099                                     input_section,
13100                                     contents,
13101                                     rel->r_offset,
13102                                     relocation,
13103                                     addend);
13104
13105       if (r != bfd_reloc_ok)
13106         {
13107           if (sym_name == NULL)
13108             sym_name = "(null)";
13109           if (r == bfd_reloc_overflow)
13110             {
13111               if (warned)
13112                 continue;
13113               if (h != NULL
13114                   && h->elf.root.type == bfd_link_hash_undefweak
13115                   && ppc64_elf_howto_table[r_type]->pc_relative)
13116                 {
13117                   /* Assume this is a call protected by other code that
13118                      detects the symbol is undefined.  If this is the case,
13119                      we can safely ignore the overflow.  If not, the
13120                      program is hosed anyway, and a little warning isn't
13121                      going to help.  */
13122
13123                   continue;
13124                 }
13125
13126               if (!((*info->callbacks->reloc_overflow)
13127                     (info, (h ? &h->elf.root : NULL), sym_name,
13128                      ppc64_elf_howto_table[r_type]->name,
13129                      orig_addend, input_bfd, input_section, rel->r_offset)))
13130                 return FALSE;
13131             }
13132           else
13133             {
13134               (*_bfd_error_handler)
13135                 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
13136                  input_bfd,
13137                  input_section,
13138                  (long) rel->r_offset,
13139                  ppc64_elf_howto_table[r_type]->name,
13140                  sym_name,
13141                  (int) r);
13142               ret = FALSE;
13143             }
13144         }
13145     }
13146
13147   /* If we're emitting relocations, then shortly after this function
13148      returns, reloc offsets and addends for this section will be
13149      adjusted.  Worse, reloc symbol indices will be for the output
13150      file rather than the input.  Save a copy of the relocs for
13151      opd_entry_value.  */
13152   if (is_opd && (info->emitrelocations || info->relocatable))
13153     {
13154       bfd_size_type amt;
13155       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13156       rel = bfd_alloc (input_bfd, amt);
13157       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13158       ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13159       if (rel == NULL)
13160         return FALSE;
13161       memcpy (rel, relocs, amt);
13162     }
13163   return ret;
13164 }
13165
13166 /* Adjust the value of any local symbols in opd sections.  */
13167
13168 static int
13169 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13170                               const char *name ATTRIBUTE_UNUSED,
13171                               Elf_Internal_Sym *elfsym,
13172                               asection *input_sec,
13173                               struct elf_link_hash_entry *h)
13174 {
13175   struct _opd_sec_data *opd;
13176   long adjust;
13177   bfd_vma value;
13178
13179   if (h != NULL)
13180     return 1;
13181
13182   opd = get_opd_info (input_sec);
13183   if (opd == NULL || opd->adjust == NULL)
13184     return 1;
13185
13186   value = elfsym->st_value - input_sec->output_offset;
13187   if (!info->relocatable)
13188     value -= input_sec->output_section->vma;
13189
13190   adjust = opd->adjust[value / 8];
13191   if (adjust == -1)
13192     return 2;
13193
13194   elfsym->st_value += adjust;
13195   return 1;
13196 }
13197
13198 /* Finish up dynamic symbol handling.  We set the contents of various
13199    dynamic sections here.  */
13200
13201 static bfd_boolean
13202 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13203                                  struct bfd_link_info *info,
13204                                  struct elf_link_hash_entry *h,
13205                                  Elf_Internal_Sym *sym)
13206 {
13207   struct ppc_link_hash_table *htab;
13208   struct plt_entry *ent;
13209   Elf_Internal_Rela rela;
13210   bfd_byte *loc;
13211
13212   htab = ppc_hash_table (info);
13213   if (htab == NULL)
13214     return FALSE;
13215
13216   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13217     if (ent->plt.offset != (bfd_vma) -1)
13218       {
13219         /* This symbol has an entry in the procedure linkage
13220            table.  Set it up.  */
13221         if (!htab->elf.dynamic_sections_created
13222             || h->dynindx == -1)
13223           {
13224             BFD_ASSERT (h->type == STT_GNU_IFUNC
13225                         && h->def_regular
13226                         && (h->root.type == bfd_link_hash_defined
13227                             || h->root.type == bfd_link_hash_defweak));
13228             rela.r_offset = (htab->iplt->output_section->vma
13229                              + htab->iplt->output_offset
13230                              + ent->plt.offset);
13231             rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13232             rela.r_addend = (h->root.u.def.value
13233                              + h->root.u.def.section->output_offset
13234                              + h->root.u.def.section->output_section->vma
13235                              + ent->addend);
13236             loc = (htab->reliplt->contents
13237                    + (htab->reliplt->reloc_count++
13238                       * sizeof (Elf64_External_Rela)));
13239           }
13240         else
13241           {
13242             rela.r_offset = (htab->plt->output_section->vma
13243                              + htab->plt->output_offset
13244                              + ent->plt.offset);
13245             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13246             rela.r_addend = ent->addend;
13247             loc = (htab->relplt->contents
13248                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13249                       / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
13250           }
13251         bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13252       }
13253
13254   if (h->needs_copy)
13255     {
13256       /* This symbol needs a copy reloc.  Set it up.  */
13257
13258       if (h->dynindx == -1
13259           || (h->root.type != bfd_link_hash_defined
13260               && h->root.type != bfd_link_hash_defweak)
13261           || htab->relbss == NULL)
13262         abort ();
13263
13264       rela.r_offset = (h->root.u.def.value
13265                        + h->root.u.def.section->output_section->vma
13266                        + h->root.u.def.section->output_offset);
13267       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13268       rela.r_addend = 0;
13269       loc = htab->relbss->contents;
13270       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
13271       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13272     }
13273
13274   /* Mark some specially defined symbols as absolute.  */
13275   if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13276     sym->st_shndx = SHN_ABS;
13277
13278   return TRUE;
13279 }
13280
13281 /* Used to decide how to sort relocs in an optimal manner for the
13282    dynamic linker, before writing them out.  */
13283
13284 static enum elf_reloc_type_class
13285 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
13286 {
13287   enum elf_ppc64_reloc_type r_type;
13288
13289   r_type = ELF64_R_TYPE (rela->r_info);
13290   switch (r_type)
13291     {
13292     case R_PPC64_RELATIVE:
13293       return reloc_class_relative;
13294     case R_PPC64_JMP_SLOT:
13295       return reloc_class_plt;
13296     case R_PPC64_COPY:
13297       return reloc_class_copy;
13298     default:
13299       return reloc_class_normal;
13300     }
13301 }
13302
13303 /* Finish up the dynamic sections.  */
13304
13305 static bfd_boolean
13306 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13307                                    struct bfd_link_info *info)
13308 {
13309   struct ppc_link_hash_table *htab;
13310   bfd *dynobj;
13311   asection *sdyn;
13312
13313   htab = ppc_hash_table (info);
13314   if (htab == NULL)
13315     return FALSE;
13316
13317   dynobj = htab->elf.dynobj;
13318   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13319
13320   if (htab->elf.dynamic_sections_created)
13321     {
13322       Elf64_External_Dyn *dyncon, *dynconend;
13323
13324       if (sdyn == NULL || htab->got == NULL)
13325         abort ();
13326
13327       dyncon = (Elf64_External_Dyn *) sdyn->contents;
13328       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
13329       for (; dyncon < dynconend; dyncon++)
13330         {
13331           Elf_Internal_Dyn dyn;
13332           asection *s;
13333
13334           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13335
13336           switch (dyn.d_tag)
13337             {
13338             default:
13339               continue;
13340
13341             case DT_PPC64_GLINK:
13342               s = htab->glink;
13343               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13344               /* We stupidly defined DT_PPC64_GLINK to be the start
13345                  of glink rather than the first entry point, which is
13346                  what ld.so needs, and now have a bigger stub to
13347                  support automatic multiple TOCs.  */
13348               dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
13349               break;
13350
13351             case DT_PPC64_OPD:
13352               s = bfd_get_section_by_name (output_bfd, ".opd");
13353               if (s == NULL)
13354                 continue;
13355               dyn.d_un.d_ptr = s->vma;
13356               break;
13357
13358             case DT_PPC64_OPDSZ:
13359               s = bfd_get_section_by_name (output_bfd, ".opd");
13360               if (s == NULL)
13361                 continue;
13362               dyn.d_un.d_val = s->size;
13363               break;
13364
13365             case DT_PLTGOT:
13366               s = htab->plt;
13367               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13368               break;
13369
13370             case DT_JMPREL:
13371               s = htab->relplt;
13372               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13373               break;
13374
13375             case DT_PLTRELSZ:
13376               dyn.d_un.d_val = htab->relplt->size;
13377               break;
13378
13379             case DT_RELASZ:
13380               /* Don't count procedure linkage table relocs in the
13381                  overall reloc count.  */
13382               s = htab->relplt;
13383               if (s == NULL)
13384                 continue;
13385               dyn.d_un.d_val -= s->size;
13386               break;
13387
13388             case DT_RELA:
13389               /* We may not be using the standard ELF linker script.
13390                  If .rela.plt is the first .rela section, we adjust
13391                  DT_RELA to not include it.  */
13392               s = htab->relplt;
13393               if (s == NULL)
13394                 continue;
13395               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
13396                 continue;
13397               dyn.d_un.d_ptr += s->size;
13398               break;
13399             }
13400
13401           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
13402         }
13403     }
13404
13405   if (htab->got != NULL && htab->got->size != 0)
13406     {
13407       /* Fill in the first entry in the global offset table.
13408          We use it to hold the link-time TOCbase.  */
13409       bfd_put_64 (output_bfd,
13410                   elf_gp (output_bfd) + TOC_BASE_OFF,
13411                   htab->got->contents);
13412
13413       /* Set .got entry size.  */
13414       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
13415     }
13416
13417   if (htab->plt != NULL && htab->plt->size != 0)
13418     {
13419       /* Set .plt entry size.  */
13420       elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
13421         = PLT_ENTRY_SIZE;
13422     }
13423
13424   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
13425      brlt ourselves if emitrelocations.  */
13426   if (htab->brlt != NULL
13427       && htab->brlt->reloc_count != 0
13428       && !_bfd_elf_link_output_relocs (output_bfd,
13429                                        htab->brlt,
13430                                        &elf_section_data (htab->brlt)->rel_hdr,
13431                                        elf_section_data (htab->brlt)->relocs,
13432                                        NULL))
13433     return FALSE;
13434
13435   if (htab->glink != NULL
13436       && htab->glink->reloc_count != 0
13437       && !_bfd_elf_link_output_relocs (output_bfd,
13438                                        htab->glink,
13439                                        &elf_section_data (htab->glink)->rel_hdr,
13440                                        elf_section_data (htab->glink)->relocs,
13441                                        NULL))
13442     return FALSE;
13443
13444   /* We need to handle writing out multiple GOT sections ourselves,
13445      since we didn't add them to DYNOBJ.  We know dynobj is the first
13446      bfd.  */
13447   while ((dynobj = dynobj->link_next) != NULL)
13448     {
13449       asection *s;
13450
13451       if (!is_ppc64_elf (dynobj))
13452         continue;
13453
13454       s = ppc64_elf_tdata (dynobj)->got;
13455       if (s != NULL
13456           && s->size != 0
13457           && s->output_section != bfd_abs_section_ptr
13458           && !bfd_set_section_contents (output_bfd, s->output_section,
13459                                         s->contents, s->output_offset,
13460                                         s->size))
13461         return FALSE;
13462       s = ppc64_elf_tdata (dynobj)->relgot;
13463       if (s != NULL
13464           && s->size != 0
13465           && s->output_section != bfd_abs_section_ptr
13466           && !bfd_set_section_contents (output_bfd, s->output_section,
13467                                         s->contents, s->output_offset,
13468                                         s->size))
13469         return FALSE;
13470     }
13471
13472   return TRUE;
13473 }
13474
13475 #include "elf64-target.h"