* elf64-ppc.c (ppc64_elf_relocate_section): Correct pcrel section zero.
[external/binutils.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3    Written by Linus Nordberg, Swox AB <info@swox.com>,
4    based on elf32-ppc.c by Ian Lance Taylor.
5
6    This file is part of BFD, the Binary File Descriptor library.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 /* This file is based on the 64-bit PowerPC ELF ABI.  It is also based
23    on the file elf32-ppc.c.  */
24
25 #include "bfd.h"
26 #include "sysdep.h"
27 #include "bfdlink.h"
28 #include "libbfd.h"
29 #include "elf-bfd.h"
30 #include "elf/ppc64.h"
31 #include "elf64-ppc.h"
32
33 static void ppc_howto_init
34   PARAMS ((void));
35 static reloc_howto_type *ppc64_elf_reloc_type_lookup
36   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
37 static void ppc64_elf_info_to_howto
38   PARAMS ((bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst));
39 static bfd_reloc_status_type ppc64_elf_ha_reloc
40   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
41 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
42   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
44   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
45 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
46   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
47 static bfd_reloc_status_type ppc64_elf_toc_reloc
48   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
49 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
50   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
51 static bfd_reloc_status_type ppc64_elf_toc64_reloc
52   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
53 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
54   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
55 static bfd_boolean ppc64_elf_object_p
56   PARAMS ((bfd *));
57 static bfd_boolean ppc64_elf_merge_private_bfd_data
58   PARAMS ((bfd *, bfd *));
59 static bfd_boolean ppc64_elf_new_section_hook
60   PARAMS ((bfd *, asection *));
61
62
63 #define TARGET_LITTLE_SYM       bfd_elf64_powerpcle_vec
64 #define TARGET_LITTLE_NAME      "elf64-powerpcle"
65 #define TARGET_BIG_SYM          bfd_elf64_powerpc_vec
66 #define TARGET_BIG_NAME         "elf64-powerpc"
67 #define ELF_ARCH                bfd_arch_powerpc
68 #define ELF_MACHINE_CODE        EM_PPC64
69 #define ELF_MAXPAGESIZE         0x10000
70 #define elf_info_to_howto       ppc64_elf_info_to_howto
71
72 #define elf_backend_want_got_sym 0
73 #define elf_backend_want_plt_sym 0
74 #define elf_backend_plt_alignment 3
75 #define elf_backend_plt_not_loaded 1
76 #define elf_backend_got_symbol_offset 0
77 #define elf_backend_got_header_size 8
78 #define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
79 #define elf_backend_can_gc_sections 1
80 #define elf_backend_can_refcount 1
81 #define elf_backend_rela_normal 1
82
83 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
84 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
85 #define bfd_elf64_new_section_hook            ppc64_elf_new_section_hook
86 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
87 #define bfd_elf64_bfd_link_hash_table_free    ppc64_elf_link_hash_table_free
88
89 #define elf_backend_object_p                  ppc64_elf_object_p
90 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
91 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
92 #define elf_backend_check_relocs              ppc64_elf_check_relocs
93 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
94 #define elf_backend_gc_sweep_hook             ppc64_elf_gc_sweep_hook
95 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
96 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
97 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
98 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
99 #define elf_backend_relocate_section          ppc64_elf_relocate_section
100 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
101 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
102 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
103
104 /* The name of the dynamic interpreter.  This is put in the .interp
105    section.  */
106 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
107
108 /* The size in bytes of an entry in the procedure linkage table.  */
109 #define PLT_ENTRY_SIZE 24
110
111 /* The initial size of the plt reserved for the dynamic linker.  */
112 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
113
114 /* TOC base pointers offset from start of TOC.  */
115 #define TOC_BASE_OFF    0x8000
116
117 /* Offset of tp and dtp pointers from start of TLS block.  */
118 #define TP_OFFSET       0x7000
119 #define DTP_OFFSET      0x8000
120
121 /* .plt call stub instructions.  The normal stub is like this, but
122    sometimes the .plt entry crosses a 64k boundary and we need to
123    insert an addis to adjust r12.  */
124 #define PLT_CALL_STUB_SIZE (7*4)
125 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha     */
126 #define STD_R2_40R1     0xf8410028      /* std   %r2,40(%r1)         */
127 #define LD_R11_0R12     0xe96c0000      /* ld    %r11,xxx+0@l(%r12)  */
128 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,xxx+8@l(%r12)   */
129 #define MTCTR_R11       0x7d6903a6      /* mtctr %r11                */
130                                         /* ld    %r11,xxx+16@l(%r12) */
131 #define BCTR            0x4e800420      /* bctr                      */
132
133
134 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
135 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
136
137 #define LD_R2_40R1      0xe8410028      /* ld    %r2,40(%r1)     */
138
139 /* glink call stub instructions.  We enter with the index in R0, and the
140    address of glink entry in CTR.  From that, we can calculate PLT0.  */
141 #define GLINK_CALL_STUB_SIZE (16*4)
142 #define MFCTR_R12       0x7d8902a6      /* mfctr  %r12                  */
143 #define SLDI_R11_R0_3   0x780b1f24      /* sldi   %r11,%r0,3            */
144 #define ADDIC_R2_R0_32K 0x34408000      /* addic. %r2,%r0,-32768        */
145 #define SUB_R12_R12_R11 0x7d8b6050      /* sub    %r12,%r12,%r11        */
146 #define SRADI_R2_R2_63  0x7c42fe76      /* sradi  %r2,%r2,63            */
147 #define SLDI_R11_R0_2   0x780b1764      /* sldi   %r11,%r0,2            */
148 #define AND_R2_R2_R11   0x7c425838      /* and    %r2,%r2,%r11          */
149                                         /* sub    %r12,%r12,%r11        */
150 #define ADD_R12_R12_R2  0x7d8c1214      /* add    %r12,%r12,%r2         */
151 #define ADDIS_R12_R12   0x3d8c0000      /* addis  %r12,%r12,xxx@ha      */
152                                         /* ld     %r11,xxx@l(%r12)      */
153 #define ADDI_R12_R12    0x398c0000      /* addi   %r12,%r12,xxx@l       */
154                                         /* ld     %r2,8(%r12)           */
155                                         /* mtctr  %r11                  */
156                                         /* ld     %r11,16(%r12)         */
157                                         /* bctr                         */
158
159 /* Pad with this.  */
160 #define NOP             0x60000000
161
162 /* Some other nops.  */
163 #define CROR_151515     0x4def7b82
164 #define CROR_313131     0x4ffffb82
165
166 /* .glink entries for the first 32k functions are two instructions.  */
167 #define LI_R0_0         0x38000000      /* li    %r0,0          */
168 #define B_DOT           0x48000000      /* b     .              */
169
170 /* After that, we need two instructions to load the index, followed by
171    a branch.  */
172 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
173 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
174
175 /* Instructions to save and restore floating point regs.  */
176 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
177 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
178 #define BLR             0x4e800020      /* blr                  */
179
180 /* Since .opd is an array of descriptors and each entry will end up
181    with identical R_PPC64_RELATIVE relocs, there is really no need to
182    propagate .opd relocs;  The dynamic linker should be taught to
183    relocate .opd without reloc entries.  */
184 #ifndef NO_OPD_RELOCS
185 #define NO_OPD_RELOCS 0
186 #endif
187 \f
188 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
189
190 /* Relocation HOWTO's.  */
191 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
192
193 static reloc_howto_type ppc64_elf_howto_raw[] = {
194   /* This reloc does nothing.  */
195   HOWTO (R_PPC64_NONE,          /* type */
196          0,                     /* rightshift */
197          2,                     /* size (0 = byte, 1 = short, 2 = long) */
198          32,                    /* bitsize */
199          FALSE,                 /* pc_relative */
200          0,                     /* bitpos */
201          complain_overflow_dont, /* complain_on_overflow */
202          bfd_elf_generic_reloc, /* special_function */
203          "R_PPC64_NONE",        /* name */
204          FALSE,                 /* partial_inplace */
205          0,                     /* src_mask */
206          0,                     /* dst_mask */
207          FALSE),                /* pcrel_offset */
208
209   /* A standard 32 bit relocation.  */
210   HOWTO (R_PPC64_ADDR32,        /* type */
211          0,                     /* rightshift */
212          2,                     /* size (0 = byte, 1 = short, 2 = long) */
213          32,                    /* bitsize */
214          FALSE,                 /* pc_relative */
215          0,                     /* bitpos */
216          complain_overflow_bitfield, /* complain_on_overflow */
217          bfd_elf_generic_reloc, /* special_function */
218          "R_PPC64_ADDR32",      /* name */
219          FALSE,                 /* partial_inplace */
220          0,                     /* src_mask */
221          0xffffffff,            /* dst_mask */
222          FALSE),                /* pcrel_offset */
223
224   /* An absolute 26 bit branch; the lower two bits must be zero.
225      FIXME: we don't check that, we just clear them.  */
226   HOWTO (R_PPC64_ADDR24,        /* type */
227          0,                     /* rightshift */
228          2,                     /* size (0 = byte, 1 = short, 2 = long) */
229          26,                    /* bitsize */
230          FALSE,                 /* pc_relative */
231          0,                     /* bitpos */
232          complain_overflow_bitfield, /* complain_on_overflow */
233          bfd_elf_generic_reloc, /* special_function */
234          "R_PPC64_ADDR24",      /* name */
235          FALSE,                 /* partial_inplace */
236          0,                     /* src_mask */
237          0x03fffffc,            /* dst_mask */
238          FALSE),                /* pcrel_offset */
239
240   /* A standard 16 bit relocation.  */
241   HOWTO (R_PPC64_ADDR16,        /* type */
242          0,                     /* rightshift */
243          1,                     /* size (0 = byte, 1 = short, 2 = long) */
244          16,                    /* bitsize */
245          FALSE,                 /* pc_relative */
246          0,                     /* bitpos */
247          complain_overflow_bitfield, /* complain_on_overflow */
248          bfd_elf_generic_reloc, /* special_function */
249          "R_PPC64_ADDR16",      /* name */
250          FALSE,                 /* partial_inplace */
251          0,                     /* src_mask */
252          0xffff,                /* dst_mask */
253          FALSE),                /* pcrel_offset */
254
255   /* A 16 bit relocation without overflow.  */
256   HOWTO (R_PPC64_ADDR16_LO,     /* type */
257          0,                     /* rightshift */
258          1,                     /* size (0 = byte, 1 = short, 2 = long) */
259          16,                    /* bitsize */
260          FALSE,                 /* pc_relative */
261          0,                     /* bitpos */
262          complain_overflow_dont,/* complain_on_overflow */
263          bfd_elf_generic_reloc, /* special_function */
264          "R_PPC64_ADDR16_LO",   /* name */
265          FALSE,                 /* partial_inplace */
266          0,                     /* src_mask */
267          0xffff,                /* dst_mask */
268          FALSE),                /* pcrel_offset */
269
270   /* Bits 16-31 of an address.  */
271   HOWTO (R_PPC64_ADDR16_HI,     /* type */
272          16,                    /* rightshift */
273          1,                     /* size (0 = byte, 1 = short, 2 = long) */
274          16,                    /* bitsize */
275          FALSE,                 /* pc_relative */
276          0,                     /* bitpos */
277          complain_overflow_dont, /* complain_on_overflow */
278          bfd_elf_generic_reloc, /* special_function */
279          "R_PPC64_ADDR16_HI",   /* name */
280          FALSE,                 /* partial_inplace */
281          0,                     /* src_mask */
282          0xffff,                /* dst_mask */
283          FALSE),                /* pcrel_offset */
284
285   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
286      bits, treated as a signed number, is negative.  */
287   HOWTO (R_PPC64_ADDR16_HA,     /* type */
288          16,                    /* rightshift */
289          1,                     /* size (0 = byte, 1 = short, 2 = long) */
290          16,                    /* bitsize */
291          FALSE,                 /* pc_relative */
292          0,                     /* bitpos */
293          complain_overflow_dont, /* complain_on_overflow */
294          ppc64_elf_ha_reloc,    /* special_function */
295          "R_PPC64_ADDR16_HA",   /* name */
296          FALSE,                 /* partial_inplace */
297          0,                     /* src_mask */
298          0xffff,                /* dst_mask */
299          FALSE),                /* pcrel_offset */
300
301   /* An absolute 16 bit branch; the lower two bits must be zero.
302      FIXME: we don't check that, we just clear them.  */
303   HOWTO (R_PPC64_ADDR14,        /* type */
304          0,                     /* rightshift */
305          2,                     /* size (0 = byte, 1 = short, 2 = long) */
306          16,                    /* bitsize */
307          FALSE,                 /* pc_relative */
308          0,                     /* bitpos */
309          complain_overflow_bitfield, /* complain_on_overflow */
310          bfd_elf_generic_reloc, /* special_function */
311          "R_PPC64_ADDR14",      /* name */
312          FALSE,                 /* partial_inplace */
313          0,                     /* src_mask */
314          0x0000fffc,            /* dst_mask */
315          FALSE),                /* pcrel_offset */
316
317   /* An absolute 16 bit branch, for which bit 10 should be set to
318      indicate that the branch is expected to be taken.  The lower two
319      bits must be zero.  */
320   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
321          0,                     /* rightshift */
322          2,                     /* size (0 = byte, 1 = short, 2 = long) */
323          16,                    /* bitsize */
324          FALSE,                 /* pc_relative */
325          0,                     /* bitpos */
326          complain_overflow_bitfield, /* complain_on_overflow */
327          ppc64_elf_brtaken_reloc, /* special_function */
328          "R_PPC64_ADDR14_BRTAKEN",/* name */
329          FALSE,                 /* partial_inplace */
330          0,                     /* src_mask */
331          0x0000fffc,            /* dst_mask */
332          FALSE),                /* pcrel_offset */
333
334   /* An absolute 16 bit branch, for which bit 10 should be set to
335      indicate that the branch is not expected to be taken.  The lower
336      two bits must be zero.  */
337   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
338          0,                     /* rightshift */
339          2,                     /* size (0 = byte, 1 = short, 2 = long) */
340          16,                    /* bitsize */
341          FALSE,                 /* pc_relative */
342          0,                     /* bitpos */
343          complain_overflow_bitfield, /* complain_on_overflow */
344          ppc64_elf_brtaken_reloc, /* special_function */
345          "R_PPC64_ADDR14_BRNTAKEN",/* name */
346          FALSE,                 /* partial_inplace */
347          0,                     /* src_mask */
348          0x0000fffc,            /* dst_mask */
349          FALSE),                /* pcrel_offset */
350
351   /* A relative 26 bit branch; the lower two bits must be zero.  */
352   HOWTO (R_PPC64_REL24,         /* type */
353          0,                     /* rightshift */
354          2,                     /* size (0 = byte, 1 = short, 2 = long) */
355          26,                    /* bitsize */
356          TRUE,                  /* pc_relative */
357          0,                     /* bitpos */
358          complain_overflow_signed, /* complain_on_overflow */
359          bfd_elf_generic_reloc, /* special_function */
360          "R_PPC64_REL24",       /* name */
361          FALSE,                 /* partial_inplace */
362          0,                     /* src_mask */
363          0x03fffffc,            /* dst_mask */
364          TRUE),                 /* pcrel_offset */
365
366   /* A relative 16 bit branch; the lower two bits must be zero.  */
367   HOWTO (R_PPC64_REL14,         /* type */
368          0,                     /* rightshift */
369          2,                     /* size (0 = byte, 1 = short, 2 = long) */
370          16,                    /* bitsize */
371          TRUE,                  /* pc_relative */
372          0,                     /* bitpos */
373          complain_overflow_signed, /* complain_on_overflow */
374          bfd_elf_generic_reloc, /* special_function */
375          "R_PPC64_REL14",       /* name */
376          FALSE,                 /* partial_inplace */
377          0,                     /* src_mask */
378          0x0000fffc,            /* dst_mask */
379          TRUE),                 /* pcrel_offset */
380
381   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
382      the branch is expected to be taken.  The lower two bits must be
383      zero.  */
384   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
385          0,                     /* rightshift */
386          2,                     /* size (0 = byte, 1 = short, 2 = long) */
387          16,                    /* bitsize */
388          TRUE,                  /* pc_relative */
389          0,                     /* bitpos */
390          complain_overflow_signed, /* complain_on_overflow */
391          ppc64_elf_brtaken_reloc, /* special_function */
392          "R_PPC64_REL14_BRTAKEN", /* name */
393          FALSE,                 /* partial_inplace */
394          0,                     /* src_mask */
395          0x0000fffc,            /* dst_mask */
396          TRUE),                 /* pcrel_offset */
397
398   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
399      the branch is not expected to be taken.  The lower two bits must
400      be zero.  */
401   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
402          0,                     /* rightshift */
403          2,                     /* size (0 = byte, 1 = short, 2 = long) */
404          16,                    /* bitsize */
405          TRUE,                  /* pc_relative */
406          0,                     /* bitpos */
407          complain_overflow_signed, /* complain_on_overflow */
408          ppc64_elf_brtaken_reloc, /* special_function */
409          "R_PPC64_REL14_BRNTAKEN",/* name */
410          FALSE,                 /* partial_inplace */
411          0,                     /* src_mask */
412          0x0000fffc,            /* dst_mask */
413          TRUE),                 /* pcrel_offset */
414
415   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
416      symbol.  */
417   HOWTO (R_PPC64_GOT16,         /* type */
418          0,                     /* rightshift */
419          1,                     /* size (0 = byte, 1 = short, 2 = long) */
420          16,                    /* bitsize */
421          FALSE,                 /* pc_relative */
422          0,                     /* bitpos */
423          complain_overflow_signed, /* complain_on_overflow */
424          ppc64_elf_unhandled_reloc, /* special_function */
425          "R_PPC64_GOT16",       /* name */
426          FALSE,                 /* partial_inplace */
427          0,                     /* src_mask */
428          0xffff,                /* dst_mask */
429          FALSE),                /* pcrel_offset */
430
431   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
432      the symbol.  */
433   HOWTO (R_PPC64_GOT16_LO,      /* type */
434          0,                     /* rightshift */
435          1,                     /* size (0 = byte, 1 = short, 2 = long) */
436          16,                    /* bitsize */
437          FALSE,                 /* pc_relative */
438          0,                     /* bitpos */
439          complain_overflow_dont, /* complain_on_overflow */
440          ppc64_elf_unhandled_reloc, /* special_function */
441          "R_PPC64_GOT16_LO",    /* name */
442          FALSE,                 /* partial_inplace */
443          0,                     /* src_mask */
444          0xffff,                /* dst_mask */
445          FALSE),                /* pcrel_offset */
446
447   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
448      the symbol.  */
449   HOWTO (R_PPC64_GOT16_HI,      /* type */
450          16,                    /* rightshift */
451          1,                     /* size (0 = byte, 1 = short, 2 = long) */
452          16,                    /* bitsize */
453          FALSE,                 /* pc_relative */
454          0,                     /* bitpos */
455          complain_overflow_dont,/* complain_on_overflow */
456          ppc64_elf_unhandled_reloc, /* special_function */
457          "R_PPC64_GOT16_HI",    /* name */
458          FALSE,                 /* partial_inplace */
459          0,                     /* src_mask */
460          0xffff,                /* dst_mask */
461          FALSE),                /* pcrel_offset */
462
463   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
464      the symbol.  */
465   HOWTO (R_PPC64_GOT16_HA,      /* type */
466          16,                    /* rightshift */
467          1,                     /* size (0 = byte, 1 = short, 2 = long) */
468          16,                    /* bitsize */
469          FALSE,                 /* pc_relative */
470          0,                     /* bitpos */
471          complain_overflow_dont,/* complain_on_overflow */
472          ppc64_elf_unhandled_reloc, /* special_function */
473          "R_PPC64_GOT16_HA",    /* name */
474          FALSE,                 /* partial_inplace */
475          0,                     /* src_mask */
476          0xffff,                /* dst_mask */
477          FALSE),                /* pcrel_offset */
478
479   /* This is used only by the dynamic linker.  The symbol should exist
480      both in the object being run and in some shared library.  The
481      dynamic linker copies the data addressed by the symbol from the
482      shared library into the object, because the object being
483      run has to have the data at some particular address.  */
484   HOWTO (R_PPC64_COPY,          /* type */
485          0,                     /* rightshift */
486          0,                     /* this one is variable size */
487          0,                     /* bitsize */
488          FALSE,                 /* pc_relative */
489          0,                     /* bitpos */
490          complain_overflow_dont, /* complain_on_overflow */
491          ppc64_elf_unhandled_reloc, /* special_function */
492          "R_PPC64_COPY",        /* name */
493          FALSE,                 /* partial_inplace */
494          0,                     /* src_mask */
495          0,                     /* dst_mask */
496          FALSE),                /* pcrel_offset */
497
498   /* Like R_PPC64_ADDR64, but used when setting global offset table
499      entries.  */
500   HOWTO (R_PPC64_GLOB_DAT,      /* type */
501          0,                     /* rightshift */
502          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
503          64,                    /* bitsize */
504          FALSE,                 /* pc_relative */
505          0,                     /* bitpos */
506          complain_overflow_dont, /* complain_on_overflow */
507          ppc64_elf_unhandled_reloc,  /* special_function */
508          "R_PPC64_GLOB_DAT",    /* name */
509          FALSE,                 /* partial_inplace */
510          0,                     /* src_mask */
511          ONES (64),             /* dst_mask */
512          FALSE),                /* pcrel_offset */
513
514   /* Created by the link editor.  Marks a procedure linkage table
515      entry for a symbol.  */
516   HOWTO (R_PPC64_JMP_SLOT,      /* type */
517          0,                     /* rightshift */
518          0,                     /* size (0 = byte, 1 = short, 2 = long) */
519          0,                     /* bitsize */
520          FALSE,                 /* pc_relative */
521          0,                     /* bitpos */
522          complain_overflow_dont, /* complain_on_overflow */
523          ppc64_elf_unhandled_reloc, /* special_function */
524          "R_PPC64_JMP_SLOT",    /* name */
525          FALSE,                 /* partial_inplace */
526          0,                     /* src_mask */
527          0,                     /* dst_mask */
528          FALSE),                /* pcrel_offset */
529
530   /* Used only by the dynamic linker.  When the object is run, this
531      doubleword64 is set to the load address of the object, plus the
532      addend.  */
533   HOWTO (R_PPC64_RELATIVE,      /* type */
534          0,                     /* rightshift */
535          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
536          64,                    /* bitsize */
537          FALSE,                 /* pc_relative */
538          0,                     /* bitpos */
539          complain_overflow_dont, /* complain_on_overflow */
540          bfd_elf_generic_reloc, /* special_function */
541          "R_PPC64_RELATIVE",    /* name */
542          FALSE,                 /* partial_inplace */
543          0,                     /* src_mask */
544          ONES (64),             /* dst_mask */
545          FALSE),                /* pcrel_offset */
546
547   /* Like R_PPC64_ADDR32, but may be unaligned.  */
548   HOWTO (R_PPC64_UADDR32,       /* type */
549          0,                     /* rightshift */
550          2,                     /* size (0 = byte, 1 = short, 2 = long) */
551          32,                    /* bitsize */
552          FALSE,                 /* pc_relative */
553          0,                     /* bitpos */
554          complain_overflow_bitfield, /* complain_on_overflow */
555          bfd_elf_generic_reloc, /* special_function */
556          "R_PPC64_UADDR32",     /* name */
557          FALSE,                 /* partial_inplace */
558          0,                     /* src_mask */
559          0xffffffff,            /* dst_mask */
560          FALSE),                /* pcrel_offset */
561
562   /* Like R_PPC64_ADDR16, but may be unaligned.  */
563   HOWTO (R_PPC64_UADDR16,       /* type */
564          0,                     /* rightshift */
565          1,                     /* size (0 = byte, 1 = short, 2 = long) */
566          16,                    /* bitsize */
567          FALSE,                 /* pc_relative */
568          0,                     /* bitpos */
569          complain_overflow_bitfield, /* complain_on_overflow */
570          bfd_elf_generic_reloc, /* special_function */
571          "R_PPC64_UADDR16",     /* name */
572          FALSE,                 /* partial_inplace */
573          0,                     /* src_mask */
574          0xffff,                /* dst_mask */
575          FALSE),                /* pcrel_offset */
576
577   /* 32-bit PC relative.  */
578   HOWTO (R_PPC64_REL32,         /* type */
579          0,                     /* rightshift */
580          2,                     /* size (0 = byte, 1 = short, 2 = long) */
581          32,                    /* bitsize */
582          TRUE,                  /* pc_relative */
583          0,                     /* bitpos */
584          /* FIXME: Verify.  Was complain_overflow_bitfield.  */
585          complain_overflow_signed, /* complain_on_overflow */
586          bfd_elf_generic_reloc, /* special_function */
587          "R_PPC64_REL32",       /* name */
588          FALSE,                 /* partial_inplace */
589          0,                     /* src_mask */
590          0xffffffff,            /* dst_mask */
591          TRUE),                 /* pcrel_offset */
592
593   /* 32-bit relocation to the symbol's procedure linkage table.  */
594   HOWTO (R_PPC64_PLT32,         /* type */
595          0,                     /* rightshift */
596          2,                     /* size (0 = byte, 1 = short, 2 = long) */
597          32,                    /* bitsize */
598          FALSE,                 /* pc_relative */
599          0,                     /* bitpos */
600          complain_overflow_bitfield, /* complain_on_overflow */
601          ppc64_elf_unhandled_reloc, /* special_function */
602          "R_PPC64_PLT32",       /* name */
603          FALSE,                 /* partial_inplace */
604          0,                     /* src_mask */
605          0xffffffff,            /* dst_mask */
606          FALSE),                /* pcrel_offset */
607
608   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
609      FIXME: R_PPC64_PLTREL32 not supported.  */
610   HOWTO (R_PPC64_PLTREL32,      /* type */
611          0,                     /* rightshift */
612          2,                     /* size (0 = byte, 1 = short, 2 = long) */
613          32,                    /* bitsize */
614          TRUE,                  /* pc_relative */
615          0,                     /* bitpos */
616          complain_overflow_signed, /* complain_on_overflow */
617          bfd_elf_generic_reloc, /* special_function */
618          "R_PPC64_PLTREL32",    /* name */
619          FALSE,                 /* partial_inplace */
620          0,                     /* src_mask */
621          0xffffffff,            /* dst_mask */
622          TRUE),                 /* pcrel_offset */
623
624   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
625      the symbol.  */
626   HOWTO (R_PPC64_PLT16_LO,      /* type */
627          0,                     /* rightshift */
628          1,                     /* size (0 = byte, 1 = short, 2 = long) */
629          16,                    /* bitsize */
630          FALSE,                 /* pc_relative */
631          0,                     /* bitpos */
632          complain_overflow_dont, /* complain_on_overflow */
633          ppc64_elf_unhandled_reloc, /* special_function */
634          "R_PPC64_PLT16_LO",    /* name */
635          FALSE,                 /* partial_inplace */
636          0,                     /* src_mask */
637          0xffff,                /* dst_mask */
638          FALSE),                /* pcrel_offset */
639
640   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
641      the symbol.  */
642   HOWTO (R_PPC64_PLT16_HI,      /* type */
643          16,                    /* rightshift */
644          1,                     /* size (0 = byte, 1 = short, 2 = long) */
645          16,                    /* bitsize */
646          FALSE,                 /* pc_relative */
647          0,                     /* bitpos */
648          complain_overflow_dont, /* complain_on_overflow */
649          ppc64_elf_unhandled_reloc, /* special_function */
650          "R_PPC64_PLT16_HI",    /* name */
651          FALSE,                 /* partial_inplace */
652          0,                     /* src_mask */
653          0xffff,                /* dst_mask */
654          FALSE),                /* pcrel_offset */
655
656   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
657      the symbol.  */
658   HOWTO (R_PPC64_PLT16_HA,      /* type */
659          16,                    /* rightshift */
660          1,                     /* size (0 = byte, 1 = short, 2 = long) */
661          16,                    /* bitsize */
662          FALSE,                 /* pc_relative */
663          0,                     /* bitpos */
664          complain_overflow_dont, /* complain_on_overflow */
665          ppc64_elf_unhandled_reloc, /* special_function */
666          "R_PPC64_PLT16_HA",    /* name */
667          FALSE,                 /* partial_inplace */
668          0,                     /* src_mask */
669          0xffff,                /* dst_mask */
670          FALSE),                /* pcrel_offset */
671
672   /* 16-bit section relative relocation.  */
673   HOWTO (R_PPC64_SECTOFF,       /* type */
674          0,                     /* rightshift */
675          1,                     /* size (0 = byte, 1 = short, 2 = long) */
676          16,                    /* bitsize */
677          FALSE,                 /* pc_relative */
678          0,                     /* bitpos */
679          complain_overflow_bitfield, /* complain_on_overflow */
680          ppc64_elf_sectoff_reloc, /* special_function */
681          "R_PPC64_SECTOFF",     /* name */
682          FALSE,                 /* partial_inplace */
683          0,                     /* src_mask */
684          0xffff,                /* dst_mask */
685          FALSE),                /* pcrel_offset */
686
687   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
688   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
689          0,                     /* rightshift */
690          1,                     /* size (0 = byte, 1 = short, 2 = long) */
691          16,                    /* bitsize */
692          FALSE,                 /* pc_relative */
693          0,                     /* bitpos */
694          complain_overflow_dont, /* complain_on_overflow */
695          ppc64_elf_sectoff_reloc, /* special_function */
696          "R_PPC64_SECTOFF_LO",  /* name */
697          FALSE,                 /* partial_inplace */
698          0,                     /* src_mask */
699          0xffff,                /* dst_mask */
700          FALSE),                /* pcrel_offset */
701
702   /* 16-bit upper half section relative relocation.  */
703   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
704          16,                    /* rightshift */
705          1,                     /* size (0 = byte, 1 = short, 2 = long) */
706          16,                    /* bitsize */
707          FALSE,                 /* pc_relative */
708          0,                     /* bitpos */
709          complain_overflow_dont, /* complain_on_overflow */
710          ppc64_elf_sectoff_reloc, /* special_function */
711          "R_PPC64_SECTOFF_HI",  /* name */
712          FALSE,                 /* partial_inplace */
713          0,                     /* src_mask */
714          0xffff,                /* dst_mask */
715          FALSE),                /* pcrel_offset */
716
717   /* 16-bit upper half adjusted section relative relocation.  */
718   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
719          16,                    /* rightshift */
720          1,                     /* size (0 = byte, 1 = short, 2 = long) */
721          16,                    /* bitsize */
722          FALSE,                 /* pc_relative */
723          0,                     /* bitpos */
724          complain_overflow_dont, /* complain_on_overflow */
725          ppc64_elf_sectoff_ha_reloc, /* special_function */
726          "R_PPC64_SECTOFF_HA",  /* name */
727          FALSE,                 /* partial_inplace */
728          0,                     /* src_mask */
729          0xffff,                /* dst_mask */
730          FALSE),                /* pcrel_offset */
731
732   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
733   HOWTO (R_PPC64_REL30,         /* type */
734          2,                     /* rightshift */
735          2,                     /* size (0 = byte, 1 = short, 2 = long) */
736          30,                    /* bitsize */
737          TRUE,                  /* pc_relative */
738          0,                     /* bitpos */
739          complain_overflow_dont, /* complain_on_overflow */
740          bfd_elf_generic_reloc, /* special_function */
741          "R_PPC64_REL30",       /* name */
742          FALSE,                 /* partial_inplace */
743          0,                     /* src_mask */
744          0xfffffffc,            /* dst_mask */
745          TRUE),                 /* pcrel_offset */
746
747   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
748
749   /* A standard 64-bit relocation.  */
750   HOWTO (R_PPC64_ADDR64,        /* type */
751          0,                     /* rightshift */
752          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
753          64,                    /* bitsize */
754          FALSE,                 /* pc_relative */
755          0,                     /* bitpos */
756          complain_overflow_dont, /* complain_on_overflow */
757          bfd_elf_generic_reloc, /* special_function */
758          "R_PPC64_ADDR64",      /* name */
759          FALSE,                 /* partial_inplace */
760          0,                     /* src_mask */
761          ONES (64),             /* dst_mask */
762          FALSE),                /* pcrel_offset */
763
764   /* The bits 32-47 of an address.  */
765   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
766          32,                    /* rightshift */
767          1,                     /* size (0 = byte, 1 = short, 2 = long) */
768          16,                    /* bitsize */
769          FALSE,                 /* pc_relative */
770          0,                     /* bitpos */
771          complain_overflow_dont, /* complain_on_overflow */
772          bfd_elf_generic_reloc, /* special_function */
773          "R_PPC64_ADDR16_HIGHER", /* name */
774          FALSE,                 /* partial_inplace */
775          0,                     /* src_mask */
776          0xffff,                /* dst_mask */
777          FALSE),                /* pcrel_offset */
778
779   /* The bits 32-47 of an address, plus 1 if the contents of the low
780      16 bits, treated as a signed number, is negative.  */
781   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
782          32,                    /* rightshift */
783          1,                     /* size (0 = byte, 1 = short, 2 = long) */
784          16,                    /* bitsize */
785          FALSE,                 /* pc_relative */
786          0,                     /* bitpos */
787          complain_overflow_dont, /* complain_on_overflow */
788          ppc64_elf_ha_reloc,    /* special_function */
789          "R_PPC64_ADDR16_HIGHERA", /* name */
790          FALSE,                 /* partial_inplace */
791          0,                     /* src_mask */
792          0xffff,                /* dst_mask */
793          FALSE),                /* pcrel_offset */
794
795   /* The bits 48-63 of an address.  */
796   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
797          48,                    /* rightshift */
798          1,                     /* size (0 = byte, 1 = short, 2 = long) */
799          16,                    /* bitsize */
800          FALSE,                 /* pc_relative */
801          0,                     /* bitpos */
802          complain_overflow_dont, /* complain_on_overflow */
803          bfd_elf_generic_reloc, /* special_function */
804          "R_PPC64_ADDR16_HIGHEST", /* name */
805          FALSE,                 /* partial_inplace */
806          0,                     /* src_mask */
807          0xffff,                /* dst_mask */
808          FALSE),                /* pcrel_offset */
809
810   /* The bits 48-63 of an address, plus 1 if the contents of the low
811      16 bits, treated as a signed number, is negative.  */
812   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
813          48,                    /* rightshift */
814          1,                     /* size (0 = byte, 1 = short, 2 = long) */
815          16,                    /* bitsize */
816          FALSE,                 /* pc_relative */
817          0,                     /* bitpos */
818          complain_overflow_dont, /* complain_on_overflow */
819          ppc64_elf_ha_reloc,    /* special_function */
820          "R_PPC64_ADDR16_HIGHESTA", /* name */
821          FALSE,                 /* partial_inplace */
822          0,                     /* src_mask */
823          0xffff,                /* dst_mask */
824          FALSE),                /* pcrel_offset */
825
826   /* Like ADDR64, but may be unaligned.  */
827   HOWTO (R_PPC64_UADDR64,       /* type */
828          0,                     /* rightshift */
829          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
830          64,                    /* bitsize */
831          FALSE,                 /* pc_relative */
832          0,                     /* bitpos */
833          complain_overflow_dont, /* complain_on_overflow */
834          bfd_elf_generic_reloc, /* special_function */
835          "R_PPC64_UADDR64",     /* name */
836          FALSE,                 /* partial_inplace */
837          0,                     /* src_mask */
838          ONES (64),             /* dst_mask */
839          FALSE),                /* pcrel_offset */
840
841   /* 64-bit relative relocation.  */
842   HOWTO (R_PPC64_REL64,         /* type */
843          0,                     /* rightshift */
844          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
845          64,                    /* bitsize */
846          TRUE,                  /* pc_relative */
847          0,                     /* bitpos */
848          complain_overflow_dont, /* complain_on_overflow */
849          bfd_elf_generic_reloc, /* special_function */
850          "R_PPC64_REL64",       /* name */
851          FALSE,                 /* partial_inplace */
852          0,                     /* src_mask */
853          ONES (64),             /* dst_mask */
854          TRUE),                 /* pcrel_offset */
855
856   /* 64-bit relocation to the symbol's procedure linkage table.  */
857   HOWTO (R_PPC64_PLT64,         /* type */
858          0,                     /* rightshift */
859          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
860          64,                    /* bitsize */
861          FALSE,                 /* pc_relative */
862          0,                     /* bitpos */
863          complain_overflow_dont, /* complain_on_overflow */
864          ppc64_elf_unhandled_reloc, /* special_function */
865          "R_PPC64_PLT64",       /* name */
866          FALSE,                 /* partial_inplace */
867          0,                     /* src_mask */
868          ONES (64),             /* dst_mask */
869          FALSE),                /* pcrel_offset */
870
871   /* 64-bit PC relative relocation to the symbol's procedure linkage
872      table.  */
873   /* FIXME: R_PPC64_PLTREL64 not supported.  */
874   HOWTO (R_PPC64_PLTREL64,      /* type */
875          0,                     /* rightshift */
876          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
877          64,                    /* bitsize */
878          TRUE,                  /* pc_relative */
879          0,                     /* bitpos */
880          complain_overflow_dont, /* complain_on_overflow */
881          ppc64_elf_unhandled_reloc, /* special_function */
882          "R_PPC64_PLTREL64",    /* name */
883          FALSE,                 /* partial_inplace */
884          0,                     /* src_mask */
885          ONES (64),             /* dst_mask */
886          TRUE),                 /* pcrel_offset */
887
888   /* 16 bit TOC-relative relocation.  */
889
890   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
891   HOWTO (R_PPC64_TOC16,         /* type */
892          0,                     /* rightshift */
893          1,                     /* size (0 = byte, 1 = short, 2 = long) */
894          16,                    /* bitsize */
895          FALSE,                 /* pc_relative */
896          0,                     /* bitpos */
897          complain_overflow_signed, /* complain_on_overflow */
898          ppc64_elf_toc_reloc,   /* special_function */
899          "R_PPC64_TOC16",       /* name */
900          FALSE,                 /* partial_inplace */
901          0,                     /* src_mask */
902          0xffff,                /* dst_mask */
903          FALSE),                /* pcrel_offset */
904
905   /* 16 bit TOC-relative relocation without overflow.  */
906
907   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
908   HOWTO (R_PPC64_TOC16_LO,      /* type */
909          0,                     /* rightshift */
910          1,                     /* size (0 = byte, 1 = short, 2 = long) */
911          16,                    /* bitsize */
912          FALSE,                 /* pc_relative */
913          0,                     /* bitpos */
914          complain_overflow_dont, /* complain_on_overflow */
915          ppc64_elf_toc_reloc,   /* special_function */
916          "R_PPC64_TOC16_LO",    /* name */
917          FALSE,                 /* partial_inplace */
918          0,                     /* src_mask */
919          0xffff,                /* dst_mask */
920          FALSE),                /* pcrel_offset */
921
922   /* 16 bit TOC-relative relocation, high 16 bits.  */
923
924   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
925   HOWTO (R_PPC64_TOC16_HI,      /* type */
926          16,                    /* rightshift */
927          1,                     /* size (0 = byte, 1 = short, 2 = long) */
928          16,                    /* bitsize */
929          FALSE,                 /* pc_relative */
930          0,                     /* bitpos */
931          complain_overflow_dont, /* complain_on_overflow */
932          ppc64_elf_toc_reloc,   /* special_function */
933          "R_PPC64_TOC16_HI",    /* name */
934          FALSE,                 /* partial_inplace */
935          0,                     /* src_mask */
936          0xffff,                /* dst_mask */
937          FALSE),                /* pcrel_offset */
938
939   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
940      contents of the low 16 bits, treated as a signed number, is
941      negative.  */
942
943   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
944   HOWTO (R_PPC64_TOC16_HA,      /* type */
945          16,                    /* rightshift */
946          1,                     /* size (0 = byte, 1 = short, 2 = long) */
947          16,                    /* bitsize */
948          FALSE,                 /* pc_relative */
949          0,                     /* bitpos */
950          complain_overflow_dont, /* complain_on_overflow */
951          ppc64_elf_toc_ha_reloc, /* special_function */
952          "R_PPC64_TOC16_HA",    /* name */
953          FALSE,                 /* partial_inplace */
954          0,                     /* src_mask */
955          0xffff,                /* dst_mask */
956          FALSE),                /* pcrel_offset */
957
958   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
959
960   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
961   HOWTO (R_PPC64_TOC,           /* type */
962          0,                     /* rightshift */
963          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
964          64,                    /* bitsize */
965          FALSE,                 /* pc_relative */
966          0,                     /* bitpos */
967          complain_overflow_bitfield, /* complain_on_overflow */
968          ppc64_elf_toc64_reloc, /* special_function */
969          "R_PPC64_TOC",         /* name */
970          FALSE,                 /* partial_inplace */
971          0,                     /* src_mask */
972          ONES (64),             /* dst_mask */
973          FALSE),                /* pcrel_offset */
974
975   /* Like R_PPC64_GOT16, but also informs the link editor that the
976      value to relocate may (!) refer to a PLT entry which the link
977      editor (a) may replace with the symbol value.  If the link editor
978      is unable to fully resolve the symbol, it may (b) create a PLT
979      entry and store the address to the new PLT entry in the GOT.
980      This permits lazy resolution of function symbols at run time.
981      The link editor may also skip all of this and just (c) emit a
982      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
983   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
984     HOWTO (R_PPC64_PLTGOT16,    /* type */
985          0,                     /* rightshift */
986          1,                     /* size (0 = byte, 1 = short, 2 = long) */
987          16,                    /* bitsize */
988          FALSE,                 /* pc_relative */
989          0,                     /* bitpos */
990          complain_overflow_signed, /* complain_on_overflow */
991          ppc64_elf_unhandled_reloc, /* special_function */
992          "R_PPC64_PLTGOT16",    /* name */
993          FALSE,                 /* partial_inplace */
994          0,                     /* src_mask */
995          0xffff,                /* dst_mask */
996          FALSE),                /* pcrel_offset */
997
998   /* Like R_PPC64_PLTGOT16, but without overflow.  */
999   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1000   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1001          0,                     /* rightshift */
1002          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1003          16,                    /* bitsize */
1004          FALSE,                 /* pc_relative */
1005          0,                     /* bitpos */
1006          complain_overflow_dont, /* complain_on_overflow */
1007          ppc64_elf_unhandled_reloc, /* special_function */
1008          "R_PPC64_PLTGOT16_LO", /* name */
1009          FALSE,                 /* partial_inplace */
1010          0,                     /* src_mask */
1011          0xffff,                /* dst_mask */
1012          FALSE),                /* pcrel_offset */
1013
1014   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1015   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1016   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1017          16,                    /* rightshift */
1018          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1019          16,                    /* bitsize */
1020          FALSE,                 /* pc_relative */
1021          0,                     /* bitpos */
1022          complain_overflow_dont, /* complain_on_overflow */
1023          ppc64_elf_unhandled_reloc, /* special_function */
1024          "R_PPC64_PLTGOT16_HI", /* name */
1025          FALSE,                 /* partial_inplace */
1026          0,                     /* src_mask */
1027          0xffff,                /* dst_mask */
1028          FALSE),                /* pcrel_offset */
1029
1030   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1031      1 if the contents of the low 16 bits, treated as a signed number,
1032      is negative.  */
1033   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1034   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1035          16,                    /* rightshift */
1036          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1037          16,                    /* bitsize */
1038          FALSE,                 /* pc_relative */
1039          0,                     /* bitpos */
1040          complain_overflow_dont,/* complain_on_overflow */
1041          ppc64_elf_unhandled_reloc, /* special_function */
1042          "R_PPC64_PLTGOT16_HA", /* name */
1043          FALSE,                 /* partial_inplace */
1044          0,                     /* src_mask */
1045          0xffff,                /* dst_mask */
1046          FALSE),                /* pcrel_offset */
1047
1048   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1049   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1050          0,                     /* rightshift */
1051          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1052          16,                    /* bitsize */
1053          FALSE,                 /* pc_relative */
1054          0,                     /* bitpos */
1055          complain_overflow_bitfield, /* complain_on_overflow */
1056          bfd_elf_generic_reloc, /* special_function */
1057          "R_PPC64_ADDR16_DS",   /* name */
1058          FALSE,                 /* partial_inplace */
1059          0,                     /* src_mask */
1060          0xfffc,                /* dst_mask */
1061          FALSE),                /* pcrel_offset */
1062
1063   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1064   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1065          0,                     /* rightshift */
1066          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1067          16,                    /* bitsize */
1068          FALSE,                 /* pc_relative */
1069          0,                     /* bitpos */
1070          complain_overflow_dont,/* complain_on_overflow */
1071          bfd_elf_generic_reloc, /* special_function */
1072          "R_PPC64_ADDR16_LO_DS",/* name */
1073          FALSE,                 /* partial_inplace */
1074          0,                     /* src_mask */
1075          0xfffc,                /* dst_mask */
1076          FALSE),                /* pcrel_offset */
1077
1078   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1079   HOWTO (R_PPC64_GOT16_DS,      /* type */
1080          0,                     /* rightshift */
1081          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1082          16,                    /* bitsize */
1083          FALSE,                 /* pc_relative */
1084          0,                     /* bitpos */
1085          complain_overflow_signed, /* complain_on_overflow */
1086          ppc64_elf_unhandled_reloc, /* special_function */
1087          "R_PPC64_GOT16_DS",    /* name */
1088          FALSE,                 /* partial_inplace */
1089          0,                     /* src_mask */
1090          0xfffc,                /* dst_mask */
1091          FALSE),                /* pcrel_offset */
1092
1093   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1094   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1095          0,                     /* rightshift */
1096          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1097          16,                    /* bitsize */
1098          FALSE,                 /* pc_relative */
1099          0,                     /* bitpos */
1100          complain_overflow_dont, /* complain_on_overflow */
1101          ppc64_elf_unhandled_reloc, /* special_function */
1102          "R_PPC64_GOT16_LO_DS", /* name */
1103          FALSE,                 /* partial_inplace */
1104          0,                     /* src_mask */
1105          0xfffc,                /* dst_mask */
1106          FALSE),                /* pcrel_offset */
1107
1108   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1109   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1110          0,                     /* rightshift */
1111          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1112          16,                    /* bitsize */
1113          FALSE,                 /* pc_relative */
1114          0,                     /* bitpos */
1115          complain_overflow_dont, /* complain_on_overflow */
1116          ppc64_elf_unhandled_reloc, /* special_function */
1117          "R_PPC64_PLT16_LO_DS", /* name */
1118          FALSE,                 /* partial_inplace */
1119          0,                     /* src_mask */
1120          0xfffc,                /* dst_mask */
1121          FALSE),                /* pcrel_offset */
1122
1123   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1124   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1125          0,                     /* rightshift */
1126          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1127          16,                    /* bitsize */
1128          FALSE,                 /* pc_relative */
1129          0,                     /* bitpos */
1130          complain_overflow_bitfield, /* complain_on_overflow */
1131          ppc64_elf_sectoff_reloc, /* special_function */
1132          "R_PPC64_SECTOFF_DS",  /* name */
1133          FALSE,                 /* partial_inplace */
1134          0,                     /* src_mask */
1135          0xfffc,                /* dst_mask */
1136          FALSE),                /* pcrel_offset */
1137
1138   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1139   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1140          0,                     /* rightshift */
1141          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1142          16,                    /* bitsize */
1143          FALSE,                 /* pc_relative */
1144          0,                     /* bitpos */
1145          complain_overflow_dont, /* complain_on_overflow */
1146          ppc64_elf_sectoff_reloc, /* special_function */
1147          "R_PPC64_SECTOFF_LO_DS",/* name */
1148          FALSE,                 /* partial_inplace */
1149          0,                     /* src_mask */
1150          0xfffc,                /* dst_mask */
1151          FALSE),                /* pcrel_offset */
1152
1153   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1154   HOWTO (R_PPC64_TOC16_DS,      /* type */
1155          0,                     /* rightshift */
1156          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1157          16,                    /* bitsize */
1158          FALSE,                 /* pc_relative */
1159          0,                     /* bitpos */
1160          complain_overflow_signed, /* complain_on_overflow */
1161          ppc64_elf_toc_reloc,   /* special_function */
1162          "R_PPC64_TOC16_DS",    /* name */
1163          FALSE,                 /* partial_inplace */
1164          0,                     /* src_mask */
1165          0xfffc,                /* dst_mask */
1166          FALSE),                /* pcrel_offset */
1167
1168   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1169   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1170          0,                     /* rightshift */
1171          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1172          16,                    /* bitsize */
1173          FALSE,                 /* pc_relative */
1174          0,                     /* bitpos */
1175          complain_overflow_dont, /* complain_on_overflow */
1176          ppc64_elf_toc_reloc,   /* special_function */
1177          "R_PPC64_TOC16_LO_DS", /* name */
1178          FALSE,                 /* partial_inplace */
1179          0,                     /* src_mask */
1180          0xfffc,                /* dst_mask */
1181          FALSE),                /* pcrel_offset */
1182
1183   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1184   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1185     HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1186          0,                     /* rightshift */
1187          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1188          16,                    /* bitsize */
1189          FALSE,                 /* pc_relative */
1190          0,                     /* bitpos */
1191          complain_overflow_signed, /* complain_on_overflow */
1192          ppc64_elf_unhandled_reloc, /* special_function */
1193          "R_PPC64_PLTGOT16_DS", /* name */
1194          FALSE,                 /* partial_inplace */
1195          0,                     /* src_mask */
1196          0xfffc,                /* dst_mask */
1197          FALSE),                /* pcrel_offset */
1198
1199   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1200   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1201   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1202          0,                     /* rightshift */
1203          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1204          16,                    /* bitsize */
1205          FALSE,                 /* pc_relative */
1206          0,                     /* bitpos */
1207          complain_overflow_dont, /* complain_on_overflow */
1208          ppc64_elf_unhandled_reloc, /* special_function */
1209          "R_PPC64_PLTGOT16_LO_DS",/* name */
1210          FALSE,                 /* partial_inplace */
1211          0,                     /* src_mask */
1212          0xfffc,                /* dst_mask */
1213          FALSE),                /* pcrel_offset */
1214
1215   /* Marker reloc for TLS.  */
1216   HOWTO (R_PPC64_TLS,
1217          0,                     /* rightshift */
1218          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1219          32,                    /* bitsize */
1220          FALSE,                 /* pc_relative */
1221          0,                     /* bitpos */
1222          complain_overflow_dont, /* complain_on_overflow */
1223          bfd_elf_generic_reloc, /* special_function */
1224          "R_PPC64_TLS",         /* name */
1225          FALSE,                 /* partial_inplace */
1226          0,                     /* src_mask */
1227          0,                     /* dst_mask */
1228          FALSE),                /* pcrel_offset */
1229
1230   /* Computes the load module index of the load module that contains the
1231      definition of its TLS sym.  */
1232   HOWTO (R_PPC64_DTPMOD64,
1233          0,                     /* rightshift */
1234          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1235          64,                    /* bitsize */
1236          FALSE,                 /* pc_relative */
1237          0,                     /* bitpos */
1238          complain_overflow_dont, /* complain_on_overflow */
1239          ppc64_elf_unhandled_reloc, /* special_function */
1240          "R_PPC64_DTPMOD64",    /* name */
1241          FALSE,                 /* partial_inplace */
1242          0,                     /* src_mask */
1243          ONES (64),             /* dst_mask */
1244          FALSE),                /* pcrel_offset */
1245
1246   /* Computes a dtv-relative displacement, the difference between the value
1247      of sym+add and the base address of the thread-local storage block that
1248      contains the definition of sym, minus 0x8000.  */
1249   HOWTO (R_PPC64_DTPREL64,
1250          0,                     /* rightshift */
1251          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1252          64,                    /* bitsize */
1253          FALSE,                 /* pc_relative */
1254          0,                     /* bitpos */
1255          complain_overflow_dont, /* complain_on_overflow */
1256          ppc64_elf_unhandled_reloc, /* special_function */
1257          "R_PPC64_DTPREL64",    /* name */
1258          FALSE,                 /* partial_inplace */
1259          0,                     /* src_mask */
1260          ONES (64),             /* dst_mask */
1261          FALSE),                /* pcrel_offset */
1262
1263   /* A 16 bit dtprel reloc.  */
1264   HOWTO (R_PPC64_DTPREL16,
1265          0,                     /* rightshift */
1266          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1267          16,                    /* bitsize */
1268          FALSE,                 /* pc_relative */
1269          0,                     /* bitpos */
1270          complain_overflow_signed, /* complain_on_overflow */
1271          ppc64_elf_unhandled_reloc, /* special_function */
1272          "R_PPC64_DTPREL16",    /* name */
1273          FALSE,                 /* partial_inplace */
1274          0,                     /* src_mask */
1275          0xffff,                /* dst_mask */
1276          FALSE),                /* pcrel_offset */
1277
1278   /* Like DTPREL16, but no overflow.  */
1279   HOWTO (R_PPC64_DTPREL16_LO,
1280          0,                     /* rightshift */
1281          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1282          16,                    /* bitsize */
1283          FALSE,                 /* pc_relative */
1284          0,                     /* bitpos */
1285          complain_overflow_dont, /* complain_on_overflow */
1286          ppc64_elf_unhandled_reloc, /* special_function */
1287          "R_PPC64_DTPREL16_LO", /* name */
1288          FALSE,                 /* partial_inplace */
1289          0,                     /* src_mask */
1290          0xffff,                /* dst_mask */
1291          FALSE),                /* pcrel_offset */
1292
1293   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1294   HOWTO (R_PPC64_DTPREL16_HI,
1295          16,                    /* rightshift */
1296          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1297          16,                    /* bitsize */
1298          FALSE,                 /* pc_relative */
1299          0,                     /* bitpos */
1300          complain_overflow_dont, /* complain_on_overflow */
1301          ppc64_elf_unhandled_reloc, /* special_function */
1302          "R_PPC64_DTPREL16_HI", /* name */
1303          FALSE,                 /* partial_inplace */
1304          0,                     /* src_mask */
1305          0xffff,                /* dst_mask */
1306          FALSE),                /* pcrel_offset */
1307
1308   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1309   HOWTO (R_PPC64_DTPREL16_HA,
1310          16,                    /* rightshift */
1311          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1312          16,                    /* bitsize */
1313          FALSE,                 /* pc_relative */
1314          0,                     /* bitpos */
1315          complain_overflow_dont, /* complain_on_overflow */
1316          ppc64_elf_unhandled_reloc, /* special_function */
1317          "R_PPC64_DTPREL16_HA", /* name */
1318          FALSE,                 /* partial_inplace */
1319          0,                     /* src_mask */
1320          0xffff,                /* dst_mask */
1321          FALSE),                /* pcrel_offset */
1322
1323   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1324   HOWTO (R_PPC64_DTPREL16_HIGHER,
1325          32,                    /* rightshift */
1326          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1327          16,                    /* bitsize */
1328          FALSE,                 /* pc_relative */
1329          0,                     /* bitpos */
1330          complain_overflow_dont, /* complain_on_overflow */
1331          ppc64_elf_unhandled_reloc, /* special_function */
1332          "R_PPC64_DTPREL16_HIGHER", /* name */
1333          FALSE,                 /* partial_inplace */
1334          0,                     /* src_mask */
1335          0xffff,                /* dst_mask */
1336          FALSE),                /* pcrel_offset */
1337
1338   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1339   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1340          32,                    /* rightshift */
1341          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1342          16,                    /* bitsize */
1343          FALSE,                 /* pc_relative */
1344          0,                     /* bitpos */
1345          complain_overflow_dont, /* complain_on_overflow */
1346          ppc64_elf_unhandled_reloc, /* special_function */
1347          "R_PPC64_DTPREL16_HIGHERA", /* name */
1348          FALSE,                 /* partial_inplace */
1349          0,                     /* src_mask */
1350          0xffff,                /* dst_mask */
1351          FALSE),                /* pcrel_offset */
1352
1353   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1354   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1355          48,                    /* rightshift */
1356          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1357          16,                    /* bitsize */
1358          FALSE,                 /* pc_relative */
1359          0,                     /* bitpos */
1360          complain_overflow_dont, /* complain_on_overflow */
1361          ppc64_elf_unhandled_reloc, /* special_function */
1362          "R_PPC64_DTPREL16_HIGHEST", /* name */
1363          FALSE,                 /* partial_inplace */
1364          0,                     /* src_mask */
1365          0xffff,                /* dst_mask */
1366          FALSE),                /* pcrel_offset */
1367
1368   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1369   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1370          48,                    /* rightshift */
1371          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1372          16,                    /* bitsize */
1373          FALSE,                 /* pc_relative */
1374          0,                     /* bitpos */
1375          complain_overflow_dont, /* complain_on_overflow */
1376          ppc64_elf_unhandled_reloc, /* special_function */
1377          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1378          FALSE,                 /* partial_inplace */
1379          0,                     /* src_mask */
1380          0xffff,                /* dst_mask */
1381          FALSE),                /* pcrel_offset */
1382
1383   /* Like DTPREL16, but for insns with a DS field.  */
1384   HOWTO (R_PPC64_DTPREL16_DS,
1385          0,                     /* rightshift */
1386          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1387          16,                    /* bitsize */
1388          FALSE,                 /* pc_relative */
1389          0,                     /* bitpos */
1390          complain_overflow_signed, /* complain_on_overflow */
1391          ppc64_elf_unhandled_reloc, /* special_function */
1392          "R_PPC64_DTPREL16_DS", /* name */
1393          FALSE,                 /* partial_inplace */
1394          0,                     /* src_mask */
1395          0xfffc,                /* dst_mask */
1396          FALSE),                /* pcrel_offset */
1397
1398   /* Like DTPREL16_DS, but no overflow.  */
1399   HOWTO (R_PPC64_DTPREL16_LO_DS,
1400          0,                     /* rightshift */
1401          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1402          16,                    /* bitsize */
1403          FALSE,                 /* pc_relative */
1404          0,                     /* bitpos */
1405          complain_overflow_dont, /* complain_on_overflow */
1406          ppc64_elf_unhandled_reloc, /* special_function */
1407          "R_PPC64_DTPREL16_LO_DS", /* name */
1408          FALSE,                 /* partial_inplace */
1409          0,                     /* src_mask */
1410          0xfffc,                /* dst_mask */
1411          FALSE),                /* pcrel_offset */
1412
1413   /* Computes a tp-relative displacement, the difference between the value of
1414      sym+add and the value of the thread pointer (r13).  */
1415   HOWTO (R_PPC64_TPREL64,
1416          0,                     /* rightshift */
1417          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1418          64,                    /* bitsize */
1419          FALSE,                 /* pc_relative */
1420          0,                     /* bitpos */
1421          complain_overflow_dont, /* complain_on_overflow */
1422          ppc64_elf_unhandled_reloc, /* special_function */
1423          "R_PPC64_TPREL64",     /* name */
1424          FALSE,                 /* partial_inplace */
1425          0,                     /* src_mask */
1426          ONES (64),             /* dst_mask */
1427          FALSE),                /* pcrel_offset */
1428
1429   /* A 16 bit tprel reloc.  */
1430   HOWTO (R_PPC64_TPREL16,
1431          0,                     /* rightshift */
1432          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1433          16,                    /* bitsize */
1434          FALSE,                 /* pc_relative */
1435          0,                     /* bitpos */
1436          complain_overflow_signed, /* complain_on_overflow */
1437          ppc64_elf_unhandled_reloc, /* special_function */
1438          "R_PPC64_TPREL16",     /* name */
1439          FALSE,                 /* partial_inplace */
1440          0,                     /* src_mask */
1441          0xffff,                /* dst_mask */
1442          FALSE),                /* pcrel_offset */
1443
1444   /* Like TPREL16, but no overflow.  */
1445   HOWTO (R_PPC64_TPREL16_LO,
1446          0,                     /* rightshift */
1447          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1448          16,                    /* bitsize */
1449          FALSE,                 /* pc_relative */
1450          0,                     /* bitpos */
1451          complain_overflow_dont, /* complain_on_overflow */
1452          ppc64_elf_unhandled_reloc, /* special_function */
1453          "R_PPC64_TPREL16_LO",  /* name */
1454          FALSE,                 /* partial_inplace */
1455          0,                     /* src_mask */
1456          0xffff,                /* dst_mask */
1457          FALSE),                /* pcrel_offset */
1458
1459   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1460   HOWTO (R_PPC64_TPREL16_HI,
1461          16,                    /* rightshift */
1462          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1463          16,                    /* bitsize */
1464          FALSE,                 /* pc_relative */
1465          0,                     /* bitpos */
1466          complain_overflow_dont, /* complain_on_overflow */
1467          ppc64_elf_unhandled_reloc, /* special_function */
1468          "R_PPC64_TPREL16_HI",  /* name */
1469          FALSE,                 /* partial_inplace */
1470          0,                     /* src_mask */
1471          0xffff,                /* dst_mask */
1472          FALSE),                /* pcrel_offset */
1473
1474   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1475   HOWTO (R_PPC64_TPREL16_HA,
1476          16,                    /* rightshift */
1477          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1478          16,                    /* bitsize */
1479          FALSE,                 /* pc_relative */
1480          0,                     /* bitpos */
1481          complain_overflow_dont, /* complain_on_overflow */
1482          ppc64_elf_unhandled_reloc, /* special_function */
1483          "R_PPC64_TPREL16_HA",  /* name */
1484          FALSE,                 /* partial_inplace */
1485          0,                     /* src_mask */
1486          0xffff,                /* dst_mask */
1487          FALSE),                /* pcrel_offset */
1488
1489   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1490   HOWTO (R_PPC64_TPREL16_HIGHER,
1491          32,                    /* rightshift */
1492          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1493          16,                    /* bitsize */
1494          FALSE,                 /* pc_relative */
1495          0,                     /* bitpos */
1496          complain_overflow_dont, /* complain_on_overflow */
1497          ppc64_elf_unhandled_reloc, /* special_function */
1498          "R_PPC64_TPREL16_HIGHER",      /* name */
1499          FALSE,                 /* partial_inplace */
1500          0,                     /* src_mask */
1501          0xffff,                /* dst_mask */
1502          FALSE),                /* pcrel_offset */
1503
1504   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1505   HOWTO (R_PPC64_TPREL16_HIGHERA,
1506          32,                    /* rightshift */
1507          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1508          16,                    /* bitsize */
1509          FALSE,                 /* pc_relative */
1510          0,                     /* bitpos */
1511          complain_overflow_dont, /* complain_on_overflow */
1512          ppc64_elf_unhandled_reloc, /* special_function */
1513          "R_PPC64_TPREL16_HIGHERA", /* name */
1514          FALSE,                 /* partial_inplace */
1515          0,                     /* src_mask */
1516          0xffff,                /* dst_mask */
1517          FALSE),                /* pcrel_offset */
1518
1519   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1520   HOWTO (R_PPC64_TPREL16_HIGHEST,
1521          48,                    /* rightshift */
1522          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1523          16,                    /* bitsize */
1524          FALSE,                 /* pc_relative */
1525          0,                     /* bitpos */
1526          complain_overflow_dont, /* complain_on_overflow */
1527          ppc64_elf_unhandled_reloc, /* special_function */
1528          "R_PPC64_TPREL16_HIGHEST", /* name */
1529          FALSE,                 /* partial_inplace */
1530          0,                     /* src_mask */
1531          0xffff,                /* dst_mask */
1532          FALSE),                /* pcrel_offset */
1533
1534   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1535   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1536          48,                    /* rightshift */
1537          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1538          16,                    /* bitsize */
1539          FALSE,                 /* pc_relative */
1540          0,                     /* bitpos */
1541          complain_overflow_dont, /* complain_on_overflow */
1542          ppc64_elf_unhandled_reloc, /* special_function */
1543          "R_PPC64_TPREL16_HIGHESTA", /* name */
1544          FALSE,                 /* partial_inplace */
1545          0,                     /* src_mask */
1546          0xffff,                /* dst_mask */
1547          FALSE),                /* pcrel_offset */
1548
1549   /* Like TPREL16, but for insns with a DS field.  */
1550   HOWTO (R_PPC64_TPREL16_DS,
1551          0,                     /* rightshift */
1552          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1553          16,                    /* bitsize */
1554          FALSE,                 /* pc_relative */
1555          0,                     /* bitpos */
1556          complain_overflow_signed, /* complain_on_overflow */
1557          ppc64_elf_unhandled_reloc, /* special_function */
1558          "R_PPC64_TPREL16_DS",  /* name */
1559          FALSE,                 /* partial_inplace */
1560          0,                     /* src_mask */
1561          0xfffc,                /* dst_mask */
1562          FALSE),                /* pcrel_offset */
1563
1564   /* Like TPREL16_DS, but no overflow.  */
1565   HOWTO (R_PPC64_TPREL16_LO_DS,
1566          0,                     /* rightshift */
1567          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1568          16,                    /* bitsize */
1569          FALSE,                 /* pc_relative */
1570          0,                     /* bitpos */
1571          complain_overflow_dont, /* complain_on_overflow */
1572          ppc64_elf_unhandled_reloc, /* special_function */
1573          "R_PPC64_TPREL16_LO_DS", /* name */
1574          FALSE,                 /* partial_inplace */
1575          0,                     /* src_mask */
1576          0xfffc,                /* dst_mask */
1577          FALSE),                /* pcrel_offset */
1578
1579   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1580      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1581      to the first entry relative to the TOC base (r2).  */
1582   HOWTO (R_PPC64_GOT_TLSGD16,
1583          0,                     /* rightshift */
1584          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1585          16,                    /* bitsize */
1586          FALSE,                 /* pc_relative */
1587          0,                     /* bitpos */
1588          complain_overflow_signed, /* complain_on_overflow */
1589          ppc64_elf_unhandled_reloc, /* special_function */
1590          "R_PPC64_GOT_TLSGD16", /* name */
1591          FALSE,                 /* partial_inplace */
1592          0,                     /* src_mask */
1593          0xffff,                /* dst_mask */
1594          FALSE),                /* pcrel_offset */
1595
1596   /* Like GOT_TLSGD16, but no overflow.  */
1597   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1598          0,                     /* rightshift */
1599          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1600          16,                    /* bitsize */
1601          FALSE,                 /* pc_relative */
1602          0,                     /* bitpos */
1603          complain_overflow_dont, /* complain_on_overflow */
1604          ppc64_elf_unhandled_reloc, /* special_function */
1605          "R_PPC64_GOT_TLSGD16_LO", /* name */
1606          FALSE,                 /* partial_inplace */
1607          0,                     /* src_mask */
1608          0xffff,                /* dst_mask */
1609          FALSE),                /* pcrel_offset */
1610
1611   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1612   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1613          16,                    /* rightshift */
1614          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1615          16,                    /* bitsize */
1616          FALSE,                 /* pc_relative */
1617          0,                     /* bitpos */
1618          complain_overflow_dont, /* complain_on_overflow */
1619          ppc64_elf_unhandled_reloc, /* special_function */
1620          "R_PPC64_GOT_TLSGD16_HI", /* name */
1621          FALSE,                 /* partial_inplace */
1622          0,                     /* src_mask */
1623          0xffff,                /* dst_mask */
1624          FALSE),                /* pcrel_offset */
1625
1626   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1627   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1628          16,                    /* rightshift */
1629          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1630          16,                    /* bitsize */
1631          FALSE,                 /* pc_relative */
1632          0,                     /* bitpos */
1633          complain_overflow_dont, /* complain_on_overflow */
1634          ppc64_elf_unhandled_reloc, /* special_function */
1635          "R_PPC64_GOT_TLSGD16_HA", /* name */
1636          FALSE,                 /* partial_inplace */
1637          0,                     /* src_mask */
1638          0xffff,                /* dst_mask */
1639          FALSE),                /* pcrel_offset */
1640
1641   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1642      with values (sym+add)@dtpmod and zero, and computes the offset to the
1643      first entry relative to the TOC base (r2).  */
1644   HOWTO (R_PPC64_GOT_TLSLD16,
1645          0,                     /* rightshift */
1646          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1647          16,                    /* bitsize */
1648          FALSE,                 /* pc_relative */
1649          0,                     /* bitpos */
1650          complain_overflow_signed, /* complain_on_overflow */
1651          ppc64_elf_unhandled_reloc, /* special_function */
1652          "R_PPC64_GOT_TLSLD16", /* name */
1653          FALSE,                 /* partial_inplace */
1654          0,                     /* src_mask */
1655          0xffff,                /* dst_mask */
1656          FALSE),                /* pcrel_offset */
1657
1658   /* Like GOT_TLSLD16, but no overflow.  */
1659   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1660          0,                     /* rightshift */
1661          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1662          16,                    /* bitsize */
1663          FALSE,                 /* pc_relative */
1664          0,                     /* bitpos */
1665          complain_overflow_dont, /* complain_on_overflow */
1666          ppc64_elf_unhandled_reloc, /* special_function */
1667          "R_PPC64_GOT_TLSLD16_LO", /* name */
1668          FALSE,                 /* partial_inplace */
1669          0,                     /* src_mask */
1670          0xffff,                /* dst_mask */
1671          FALSE),                /* pcrel_offset */
1672
1673   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1674   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1675          16,                    /* rightshift */
1676          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1677          16,                    /* bitsize */
1678          FALSE,                 /* pc_relative */
1679          0,                     /* bitpos */
1680          complain_overflow_dont, /* complain_on_overflow */
1681          ppc64_elf_unhandled_reloc, /* special_function */
1682          "R_PPC64_GOT_TLSLD16_HI", /* name */
1683          FALSE,                 /* partial_inplace */
1684          0,                     /* src_mask */
1685          0xffff,                /* dst_mask */
1686          FALSE),                /* pcrel_offset */
1687
1688   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1689   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1690          16,                    /* rightshift */
1691          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1692          16,                    /* bitsize */
1693          FALSE,                 /* pc_relative */
1694          0,                     /* bitpos */
1695          complain_overflow_dont, /* complain_on_overflow */
1696          ppc64_elf_unhandled_reloc, /* special_function */
1697          "R_PPC64_GOT_TLSLD16_HA", /* name */
1698          FALSE,                 /* partial_inplace */
1699          0,                     /* src_mask */
1700          0xffff,                /* dst_mask */
1701          FALSE),                /* pcrel_offset */
1702
1703   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1704      the offset to the entry relative to the TOC base (r2).  */
1705   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1706          0,                     /* rightshift */
1707          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1708          16,                    /* bitsize */
1709          FALSE,                 /* pc_relative */
1710          0,                     /* bitpos */
1711          complain_overflow_signed, /* complain_on_overflow */
1712          ppc64_elf_unhandled_reloc, /* special_function */
1713          "R_PPC64_GOT_DTPREL16_DS", /* name */
1714          FALSE,                 /* partial_inplace */
1715          0,                     /* src_mask */
1716          0xfffc,                /* dst_mask */
1717          FALSE),                /* pcrel_offset */
1718
1719   /* Like GOT_DTPREL16_DS, but no overflow.  */
1720   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1721          0,                     /* rightshift */
1722          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1723          16,                    /* bitsize */
1724          FALSE,                 /* pc_relative */
1725          0,                     /* bitpos */
1726          complain_overflow_dont, /* complain_on_overflow */
1727          ppc64_elf_unhandled_reloc, /* special_function */
1728          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1729          FALSE,                 /* partial_inplace */
1730          0,                     /* src_mask */
1731          0xfffc,                /* dst_mask */
1732          FALSE),                /* pcrel_offset */
1733
1734   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1735   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1736          16,                    /* rightshift */
1737          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1738          16,                    /* bitsize */
1739          FALSE,                 /* pc_relative */
1740          0,                     /* bitpos */
1741          complain_overflow_dont, /* complain_on_overflow */
1742          ppc64_elf_unhandled_reloc, /* special_function */
1743          "R_PPC64_GOT_DTPREL16_HI", /* name */
1744          FALSE,                 /* partial_inplace */
1745          0,                     /* src_mask */
1746          0xffff,                /* dst_mask */
1747          FALSE),                /* pcrel_offset */
1748
1749   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1750   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1751          16,                    /* rightshift */
1752          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1753          16,                    /* bitsize */
1754          FALSE,                 /* pc_relative */
1755          0,                     /* bitpos */
1756          complain_overflow_dont, /* complain_on_overflow */
1757          ppc64_elf_unhandled_reloc, /* special_function */
1758          "R_PPC64_GOT_DTPREL16_HA", /* name */
1759          FALSE,                 /* partial_inplace */
1760          0,                     /* src_mask */
1761          0xffff,                /* dst_mask */
1762          FALSE),                /* pcrel_offset */
1763
1764   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1765      offset to the entry relative to the TOC base (r2).  */
1766   HOWTO (R_PPC64_GOT_TPREL16_DS,
1767          0,                     /* rightshift */
1768          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1769          16,                    /* bitsize */
1770          FALSE,                 /* pc_relative */
1771          0,                     /* bitpos */
1772          complain_overflow_signed, /* complain_on_overflow */
1773          ppc64_elf_unhandled_reloc, /* special_function */
1774          "R_PPC64_GOT_TPREL16_DS", /* name */
1775          FALSE,                 /* partial_inplace */
1776          0,                     /* src_mask */
1777          0xfffc,                /* dst_mask */
1778          FALSE),                /* pcrel_offset */
1779
1780   /* Like GOT_TPREL16_DS, but no overflow.  */
1781   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1782          0,                     /* rightshift */
1783          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1784          16,                    /* bitsize */
1785          FALSE,                 /* pc_relative */
1786          0,                     /* bitpos */
1787          complain_overflow_dont, /* complain_on_overflow */
1788          ppc64_elf_unhandled_reloc, /* special_function */
1789          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1790          FALSE,                 /* partial_inplace */
1791          0,                     /* src_mask */
1792          0xfffc,                /* dst_mask */
1793          FALSE),                /* pcrel_offset */
1794
1795   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1796   HOWTO (R_PPC64_GOT_TPREL16_HI,
1797          16,                    /* rightshift */
1798          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1799          16,                    /* bitsize */
1800          FALSE,                 /* pc_relative */
1801          0,                     /* bitpos */
1802          complain_overflow_dont, /* complain_on_overflow */
1803          ppc64_elf_unhandled_reloc, /* special_function */
1804          "R_PPC64_GOT_TPREL16_HI", /* name */
1805          FALSE,                 /* partial_inplace */
1806          0,                     /* src_mask */
1807          0xffff,                /* dst_mask */
1808          FALSE),                /* pcrel_offset */
1809
1810   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1811   HOWTO (R_PPC64_GOT_TPREL16_HA,
1812          16,                    /* rightshift */
1813          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1814          16,                    /* bitsize */
1815          FALSE,                 /* pc_relative */
1816          0,                     /* bitpos */
1817          complain_overflow_dont, /* complain_on_overflow */
1818          ppc64_elf_unhandled_reloc, /* special_function */
1819          "R_PPC64_GOT_TPREL16_HA", /* name */
1820          FALSE,                 /* partial_inplace */
1821          0,                     /* src_mask */
1822          0xffff,                /* dst_mask */
1823          FALSE),                /* pcrel_offset */
1824
1825   /* GNU extension to record C++ vtable hierarchy.  */
1826   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1827          0,                     /* rightshift */
1828          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1829          0,                     /* bitsize */
1830          FALSE,                 /* pc_relative */
1831          0,                     /* bitpos */
1832          complain_overflow_dont, /* complain_on_overflow */
1833          NULL,                  /* special_function */
1834          "R_PPC64_GNU_VTINHERIT", /* name */
1835          FALSE,                 /* partial_inplace */
1836          0,                     /* src_mask */
1837          0,                     /* dst_mask */
1838          FALSE),                /* pcrel_offset */
1839
1840   /* GNU extension to record C++ vtable member usage.  */
1841   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
1842          0,                     /* rightshift */
1843          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1844          0,                     /* bitsize */
1845          FALSE,                 /* pc_relative */
1846          0,                     /* bitpos */
1847          complain_overflow_dont, /* complain_on_overflow */
1848          NULL,                  /* special_function */
1849          "R_PPC64_GNU_VTENTRY", /* name */
1850          FALSE,                 /* partial_inplace */
1851          0,                     /* src_mask */
1852          0,                     /* dst_mask */
1853          FALSE),                /* pcrel_offset */
1854 };
1855
1856 \f
1857 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1858    be done.  */
1859
1860 static void
1861 ppc_howto_init ()
1862 {
1863   unsigned int i, type;
1864
1865   for (i = 0;
1866        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1867        i++)
1868     {
1869       type = ppc64_elf_howto_raw[i].type;
1870       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1871                           / sizeof (ppc64_elf_howto_table[0])));
1872       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1873     }
1874 }
1875
1876 static reloc_howto_type *
1877 ppc64_elf_reloc_type_lookup (abfd, code)
1878      bfd *abfd ATTRIBUTE_UNUSED;
1879      bfd_reloc_code_real_type code;
1880 {
1881   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1882
1883   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1884     /* Initialize howto table if needed.  */
1885     ppc_howto_init ();
1886
1887   switch ((int) code)
1888     {
1889     default:
1890       return (reloc_howto_type *) NULL;
1891
1892     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
1893       break;
1894     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
1895       break;
1896     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
1897       break;
1898     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
1899       break;
1900     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
1901       break;
1902     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
1903       break;
1904     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
1905       break;
1906     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
1907       break;
1908     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
1909       break;
1910     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
1911       break;
1912     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
1913       break;
1914     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
1915       break;
1916     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
1917       break;
1918     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
1919       break;
1920     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
1921       break;
1922     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
1923       break;
1924     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
1925       break;
1926     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
1927       break;
1928     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
1929       break;
1930     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
1931       break;
1932     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
1933       break;
1934     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
1935       break;
1936     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
1937       break;
1938     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
1939       break;
1940     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
1941       break;
1942     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
1943       break;
1944     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
1945       break;
1946     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
1947       break;
1948     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
1949       break;
1950     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
1951       break;
1952     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
1953       break;
1954     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
1955       break;
1956     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
1957       break;
1958     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
1959       break;
1960     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
1961       break;
1962     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
1963       break;
1964     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
1965       break;
1966     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
1967       break;
1968     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
1969       break;
1970     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
1971       break;
1972     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
1973       break;
1974     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
1975       break;
1976     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
1977       break;
1978     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
1979       break;
1980     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
1981       break;
1982     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
1983       break;
1984     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
1985       break;
1986     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
1987       break;
1988     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
1989       break;
1990     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
1991       break;
1992     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
1993       break;
1994     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
1995       break;
1996     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
1997       break;
1998     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
1999       break;
2000     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2001       break;
2002     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2003       break;
2004     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2005       break;
2006     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2007       break;
2008     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2009       break;
2010     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2011       break;
2012     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2013       break;
2014     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2015       break;
2016     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2017       break;
2018     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2019       break;
2020     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2021       break;
2022     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2023       break;
2024     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2025       break;
2026     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2027       break;
2028     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2029       break;
2030     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2031       break;
2032     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2033       break;
2034     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2035       break;
2036     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2037       break;
2038     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2039       break;
2040     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2041       break;
2042     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2043       break;
2044     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2045       break;
2046     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2047       break;
2048     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2049       break;
2050     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2051       break;
2052     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2053       break;
2054     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2055       break;
2056     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2057       break;
2058     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2059       break;
2060     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2061       break;
2062     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2063       break;
2064     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2065       break;
2066     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2067       break;
2068     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2069       break;
2070     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2071       break;
2072     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2073       break;
2074     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2075       break;
2076     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2077       break;
2078     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2079       break;
2080     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2081       break;
2082     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2083       break;
2084     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2085       break;
2086     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2087       break;
2088     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2089       break;
2090     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2091       break;
2092     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2093       break;
2094     }
2095
2096   return ppc64_elf_howto_table[(int) r];
2097 };
2098
2099 /* Set the howto pointer for a PowerPC ELF reloc.  */
2100
2101 static void
2102 ppc64_elf_info_to_howto (abfd, cache_ptr, dst)
2103      bfd *abfd ATTRIBUTE_UNUSED;
2104      arelent *cache_ptr;
2105      Elf_Internal_Rela *dst;
2106 {
2107   unsigned int type;
2108
2109   /* Initialize howto table if needed.  */
2110   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2111     ppc_howto_init ();
2112
2113   type = ELF64_R_TYPE (dst->r_info);
2114   BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2115                       / sizeof (ppc64_elf_howto_table[0])));
2116   cache_ptr->howto = ppc64_elf_howto_table[type];
2117 }
2118
2119 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2120
2121 static bfd_reloc_status_type
2122 ppc64_elf_ha_reloc (abfd, reloc_entry, symbol, data,
2123                     input_section, output_bfd, error_message)
2124      bfd *abfd;
2125      arelent *reloc_entry;
2126      asymbol *symbol;
2127      PTR data;
2128      asection *input_section;
2129      bfd *output_bfd;
2130      char **error_message;
2131 {
2132   /* If this is a relocatable link (output_bfd test tells us), just
2133      call the generic function.  Any adjustment will be done at final
2134      link time.  */
2135   if (output_bfd != NULL)
2136     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2137                                   input_section, output_bfd, error_message);
2138
2139   /* Adjust the addend for sign extension of the low 16 bits.
2140      We won't actually be using the low 16 bits, so trashing them
2141      doesn't matter.  */
2142   reloc_entry->addend += 0x8000;
2143   return bfd_reloc_continue;
2144 }
2145
2146 static bfd_reloc_status_type
2147 ppc64_elf_brtaken_reloc (abfd, reloc_entry, symbol, data,
2148                          input_section, output_bfd, error_message)
2149      bfd *abfd;
2150      arelent *reloc_entry;
2151      asymbol *symbol;
2152      PTR data;
2153      asection *input_section;
2154      bfd *output_bfd;
2155      char **error_message;
2156 {
2157   long insn;
2158   enum elf_ppc64_reloc_type r_type;
2159   bfd_size_type octets;
2160   /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
2161   bfd_boolean is_power4 = FALSE;
2162
2163   /* If this is a relocatable link (output_bfd test tells us), just
2164      call the generic function.  Any adjustment will be done at final
2165      link time.  */
2166   if (output_bfd != NULL)
2167     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2168                                   input_section, output_bfd, error_message);
2169
2170   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2171   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2172   insn &= ~(0x01 << 21);
2173   r_type = (enum elf_ppc64_reloc_type) reloc_entry->howto->type;
2174   if (r_type == R_PPC64_ADDR14_BRTAKEN
2175       || r_type == R_PPC64_REL14_BRTAKEN)
2176     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2177
2178   if (is_power4)
2179     {
2180       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2181          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2182          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2183       if ((insn & (0x14 << 21)) == (0x04 << 21))
2184         insn |= 0x02 << 21;
2185       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2186         insn |= 0x08 << 21;
2187       else
2188         return bfd_reloc_continue;
2189     }
2190   else
2191     {
2192       bfd_vma target = 0;
2193       bfd_vma from;
2194
2195       if (!bfd_is_com_section (symbol->section))
2196         target = symbol->value;
2197       target += symbol->section->output_section->vma;
2198       target += symbol->section->output_offset;
2199       target += reloc_entry->addend;
2200
2201       from = (reloc_entry->address
2202               + input_section->output_offset
2203               + input_section->output_section->vma);
2204
2205       /* Invert 'y' bit if not the default.  */
2206       if ((bfd_signed_vma) (target - from) < 0)
2207         insn ^= 0x01 << 21;
2208     }
2209   bfd_put_32 (abfd, (bfd_vma) insn, (bfd_byte *) data + octets);
2210   return bfd_reloc_continue;
2211 }
2212
2213 static bfd_reloc_status_type
2214 ppc64_elf_sectoff_reloc (abfd, reloc_entry, symbol, data,
2215                          input_section, output_bfd, error_message)
2216      bfd *abfd;
2217      arelent *reloc_entry;
2218      asymbol *symbol;
2219      PTR data;
2220      asection *input_section;
2221      bfd *output_bfd;
2222      char **error_message;
2223 {
2224   /* If this is a relocatable link (output_bfd test tells us), just
2225      call the generic function.  Any adjustment will be done at final
2226      link time.  */
2227   if (output_bfd != NULL)
2228     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2229                                   input_section, output_bfd, error_message);
2230
2231   /* Subtract the symbol section base address.  */
2232   reloc_entry->addend -= symbol->section->output_section->vma;
2233   return bfd_reloc_continue;
2234 }
2235
2236 static bfd_reloc_status_type
2237 ppc64_elf_sectoff_ha_reloc (abfd, reloc_entry, symbol, data,
2238                             input_section, output_bfd, error_message)
2239      bfd *abfd;
2240      arelent *reloc_entry;
2241      asymbol *symbol;
2242      PTR data;
2243      asection *input_section;
2244      bfd *output_bfd;
2245      char **error_message;
2246 {
2247   /* If this is a relocatable link (output_bfd test tells us), just
2248      call the generic function.  Any adjustment will be done at final
2249      link time.  */
2250   if (output_bfd != NULL)
2251     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2252                                   input_section, output_bfd, error_message);
2253
2254   /* Subtract the symbol section base address.  */
2255   reloc_entry->addend -= symbol->section->output_section->vma;
2256
2257   /* Adjust the addend for sign extension of the low 16 bits.  */
2258   reloc_entry->addend += 0x8000;
2259   return bfd_reloc_continue;
2260 }
2261
2262 static bfd_reloc_status_type
2263 ppc64_elf_toc_reloc (abfd, reloc_entry, symbol, data,
2264                      input_section, output_bfd, error_message)
2265      bfd *abfd;
2266      arelent *reloc_entry;
2267      asymbol *symbol;
2268      PTR data;
2269      asection *input_section;
2270      bfd *output_bfd;
2271      char **error_message;
2272 {
2273   bfd_vma TOCstart;
2274
2275   /* If this is a relocatable link (output_bfd test tells us), just
2276      call the generic function.  Any adjustment will be done at final
2277      link time.  */
2278   if (output_bfd != NULL)
2279     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2280                                   input_section, output_bfd, error_message);
2281
2282   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2283   if (TOCstart == 0)
2284     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2285
2286   /* Subtract the TOC base address.  */
2287   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2288   return bfd_reloc_continue;
2289 }
2290
2291 static bfd_reloc_status_type
2292 ppc64_elf_toc_ha_reloc (abfd, reloc_entry, symbol, data,
2293                         input_section, output_bfd, error_message)
2294      bfd *abfd;
2295      arelent *reloc_entry;
2296      asymbol *symbol;
2297      PTR data;
2298      asection *input_section;
2299      bfd *output_bfd;
2300      char **error_message;
2301 {
2302   bfd_vma TOCstart;
2303
2304   /* If this is a relocatable link (output_bfd test tells us), just
2305      call the generic function.  Any adjustment will be done at final
2306      link time.  */
2307   if (output_bfd != NULL)
2308     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2309                                   input_section, output_bfd, error_message);
2310
2311   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2312   if (TOCstart == 0)
2313     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2314
2315   /* Subtract the TOC base address.  */
2316   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2317
2318   /* Adjust the addend for sign extension of the low 16 bits.  */
2319   reloc_entry->addend += 0x8000;
2320   return bfd_reloc_continue;
2321 }
2322
2323 static bfd_reloc_status_type
2324 ppc64_elf_toc64_reloc (abfd, reloc_entry, symbol, data,
2325                        input_section, output_bfd, error_message)
2326      bfd *abfd;
2327      arelent *reloc_entry;
2328      asymbol *symbol;
2329      PTR data;
2330      asection *input_section;
2331      bfd *output_bfd;
2332      char **error_message;
2333 {
2334   bfd_vma TOCstart;
2335   bfd_size_type octets;
2336
2337   /* If this is a relocatable link (output_bfd test tells us), just
2338      call the generic function.  Any adjustment will be done at final
2339      link time.  */
2340   if (output_bfd != NULL)
2341     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2342                                   input_section, output_bfd, error_message);
2343
2344   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2345   if (TOCstart == 0)
2346     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2347
2348   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2349   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2350   return bfd_reloc_ok;
2351 }
2352
2353 static bfd_reloc_status_type
2354 ppc64_elf_unhandled_reloc (abfd, reloc_entry, symbol, data,
2355                            input_section, output_bfd, error_message)
2356      bfd *abfd;
2357      arelent *reloc_entry;
2358      asymbol *symbol;
2359      PTR data;
2360      asection *input_section;
2361      bfd *output_bfd;
2362      char **error_message;
2363 {
2364   /* If this is a relocatable link (output_bfd test tells us), just
2365      call the generic function.  Any adjustment will be done at final
2366      link time.  */
2367   if (output_bfd != NULL)
2368     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2369                                   input_section, output_bfd, error_message);
2370
2371   if (error_message != NULL)
2372     {
2373       static char buf[60];
2374       sprintf (buf, "generic linker can't handle %s",
2375                reloc_entry->howto->name);
2376       *error_message = buf;
2377     }
2378   return bfd_reloc_dangerous;
2379 }
2380
2381 /* Fix bad default arch selected for a 64 bit input bfd when the
2382    default is 32 bit.  */
2383
2384 static bfd_boolean
2385 ppc64_elf_object_p (abfd)
2386      bfd *abfd;
2387 {
2388   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2389     {
2390       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2391
2392       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2393         {
2394           /* Relies on arch after 32 bit default being 64 bit default.  */
2395           abfd->arch_info = abfd->arch_info->next;
2396           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2397         }
2398     }
2399   return TRUE;
2400 }
2401
2402 /* Merge backend specific data from an object file to the output
2403    object file when linking.  */
2404
2405 static bfd_boolean
2406 ppc64_elf_merge_private_bfd_data (ibfd, obfd)
2407      bfd *ibfd;
2408      bfd *obfd;
2409 {
2410   /* Check if we have the same endianess.  */
2411   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2412       && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
2413       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2414     {
2415       const char *msg;
2416
2417       if (bfd_big_endian (ibfd))
2418         msg = _("%s: compiled for a big endian system and target is little endian");
2419       else
2420         msg = _("%s: compiled for a little endian system and target is big endian");
2421
2422       (*_bfd_error_handler) (msg, bfd_archive_filename (ibfd));
2423
2424       bfd_set_error (bfd_error_wrong_format);
2425       return FALSE;
2426     }
2427
2428   return TRUE;
2429 }
2430
2431 struct _ppc64_elf_section_data
2432 {
2433   struct bfd_elf_section_data elf;
2434
2435   /* An array with one entry for each opd function descriptor.  */
2436   union
2437   {
2438     /* Points to the function code section for local opd entries.  */
2439     asection **func_sec;
2440     /* After editing .opd, adjust references to opd local syms.  */
2441     long *adjust;
2442   } opd;
2443
2444   /* An array for toc sections, indexed by offset/8.
2445      Specifies the relocation symbol index used at a given toc offset.  */
2446   unsigned *t_symndx;
2447 };
2448
2449 #define ppc64_elf_section_data(sec) \
2450   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2451
2452 static bfd_boolean
2453 ppc64_elf_new_section_hook (abfd, sec)
2454      bfd *abfd;
2455      asection *sec;
2456 {
2457   struct _ppc64_elf_section_data *sdata;
2458   bfd_size_type amt = sizeof (*sdata);
2459
2460   sdata = (struct _ppc64_elf_section_data *) bfd_zalloc (abfd, amt);
2461   if (sdata == NULL)
2462     return FALSE;
2463   sec->used_by_bfd = (PTR) sdata;
2464
2465   return _bfd_elf_new_section_hook (abfd, sec);
2466 }
2467 \f
2468 /* The following functions are specific to the ELF linker, while
2469    functions above are used generally.  Those named ppc64_elf_* are
2470    called by the main ELF linker code.  They appear in this file more
2471    or less in the order in which they are called.  eg.
2472    ppc64_elf_check_relocs is called early in the link process,
2473    ppc64_elf_finish_dynamic_sections is one of the last functions
2474    called.
2475
2476    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2477    functions have both a function code symbol and a function descriptor
2478    symbol.  A call to foo in a relocatable object file looks like:
2479
2480    .            .text
2481    .    x:
2482    .            bl      .foo
2483    .            nop
2484
2485    The function definition in another object file might be:
2486
2487    .            .section .opd
2488    .    foo:    .quad   .foo
2489    .            .quad   .TOC.@tocbase
2490    .            .quad   0
2491    .
2492    .            .text
2493    .    .foo:   blr
2494
2495    When the linker resolves the call during a static link, the branch
2496    unsurprisingly just goes to .foo and the .opd information is unused.
2497    If the function definition is in a shared library, things are a little
2498    different:  The call goes via a plt call stub, the opd information gets
2499    copied to the plt, and the linker patches the nop.
2500
2501    .    x:
2502    .            bl      .foo_stub
2503    .            ld      2,40(1)
2504    .
2505    .
2506    .    .foo_stub:
2507    .            addis   12,2,Lfoo@toc@ha        # in practice, the call stub
2508    .            addi    12,12,Lfoo@toc@l        # is slightly optimized, but
2509    .            std     2,40(1)                 # this is the general idea
2510    .            ld      11,0(12)
2511    .            ld      2,8(12)
2512    .            mtctr   11
2513    .            ld      11,16(12)
2514    .            bctr
2515    .
2516    .            .section .plt
2517    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
2518
2519    The "reloc ()" notation is supposed to indicate that the linker emits
2520    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
2521    copying.
2522
2523    What are the difficulties here?  Well, firstly, the relocations
2524    examined by the linker in check_relocs are against the function code
2525    sym .foo, while the dynamic relocation in the plt is emitted against
2526    the function descriptor symbol, foo.  Somewhere along the line, we need
2527    to carefully copy dynamic link information from one symbol to the other.
2528    Secondly, the generic part of the elf linker will make .foo a dynamic
2529    symbol as is normal for most other backends.  We need foo dynamic
2530    instead, at least for an application final link.  However, when
2531    creating a shared library containing foo, we need to have both symbols
2532    dynamic so that references to .foo are satisfied during the early
2533    stages of linking.  Otherwise the linker might decide to pull in a
2534    definition from some other object, eg. a static library.  */
2535
2536 /* The linker needs to keep track of the number of relocs that it
2537    decides to copy as dynamic relocs in check_relocs for each symbol.
2538    This is so that it can later discard them if they are found to be
2539    unnecessary.  We store the information in a field extending the
2540    regular ELF linker hash table.  */
2541
2542 struct ppc_dyn_relocs
2543 {
2544   struct ppc_dyn_relocs *next;
2545
2546   /* The input section of the reloc.  */
2547   asection *sec;
2548
2549   /* Total number of relocs copied for the input section.  */
2550   bfd_size_type count;
2551
2552   /* Number of pc-relative relocs copied for the input section.  */
2553   bfd_size_type pc_count;
2554 };
2555
2556 /* Track GOT entries needed for a given symbol.  We might need more
2557    than one got entry per symbol.  */
2558 struct got_entry
2559 {
2560   struct got_entry *next;
2561
2562   /* The symbol addend that we'll be placing in the GOT.  */
2563   bfd_vma addend;
2564
2565   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2566   union
2567     {
2568       bfd_signed_vma refcount;
2569       bfd_vma offset;
2570     } got;
2571
2572   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2573      TLS_TPREL or TLS_DTPREL for tls entries.  */
2574   char tls_type;
2575 };
2576
2577 /* The same for PLT.  */
2578 struct plt_entry
2579 {
2580   struct plt_entry *next;
2581
2582   bfd_vma addend;
2583
2584   union
2585     {
2586       bfd_signed_vma refcount;
2587       bfd_vma offset;
2588     } plt;
2589 };
2590
2591 /* Of those relocs that might be copied as dynamic relocs, this macro
2592    selects those that must be copied when linking a shared library,
2593    even when the symbol is local.  */
2594
2595 #define MUST_BE_DYN_RELOC(RTYPE)                \
2596   ((RTYPE) != R_PPC64_REL32                     \
2597    && (RTYPE) != R_PPC64_REL64                  \
2598    && (RTYPE) != R_PPC64_REL30)
2599
2600 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2601    copying dynamic variables from a shared lib into an app's dynbss
2602    section, and instead use a dynamic relocation to point into the
2603    shared lib.  */
2604 #define ELIMINATE_COPY_RELOCS 1
2605
2606 /* Section name for stubs is the associated section name plus this
2607    string.  */
2608 #define STUB_SUFFIX ".stub"
2609
2610 /* Linker stubs.
2611    ppc_stub_long_branch:
2612    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2613    destination, but a 24 bit branch in a stub section will reach.
2614    .    b       dest
2615
2616    ppc_stub_plt_branch:
2617    Similar to the above, but a 24 bit branch in the stub section won't
2618    reach its destination.
2619    .    addis   %r12,%r2,xxx@toc@ha
2620    .    ld      %r11,xxx@toc@l(%r12)
2621    .    mtctr   %r11
2622    .    bctr
2623
2624    ppc_stub_plt_call:
2625    Used to call a function in a shared library.
2626    .    addis   %r12,%r2,xxx@toc@ha
2627    .    std     %r2,40(%r1)
2628    .    ld      %r11,xxx+0@toc@l(%r12)
2629    .    ld      %r2,xxx+8@toc@l(%r12)
2630    .    mtctr   %r11
2631    .    ld      %r11,xxx+16@toc@l(%r12)
2632    .    bctr
2633
2634    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2635    code to adjust the value and save r2 to support multiple toc sections.
2636    A ppc_stub_long_branch with an r2 offset looks like:
2637    .    std     %r2,40(%r1)
2638    .    addis   %r2,%r2,off@ha
2639    .    addi    %r2,%r2,off@l
2640    .    b       dest
2641
2642    A ppc_stub_plt_branch with an r2 offset looks like:
2643    .    std     %r2,40(%r1)
2644    .    addis   %r12,%r2,xxx@toc@ha
2645    .    ld      %r11,xxx@toc@l(%r12)
2646    .    addis   %r2,%r2,off@ha
2647    .    addi    %r2,%r2,off@l
2648    .    mtctr   %r11
2649    .    bctr
2650 */
2651
2652 enum ppc_stub_type {
2653   ppc_stub_none,
2654   ppc_stub_long_branch,
2655   ppc_stub_long_branch_r2off,
2656   ppc_stub_plt_branch,
2657   ppc_stub_plt_branch_r2off,
2658   ppc_stub_plt_call
2659 };
2660
2661 struct ppc_stub_hash_entry {
2662
2663   /* Base hash table entry structure.  */
2664   struct bfd_hash_entry root;
2665
2666   enum ppc_stub_type stub_type;
2667
2668   /* The stub section.  */
2669   asection *stub_sec;
2670
2671   /* Offset within stub_sec of the beginning of this stub.  */
2672   bfd_vma stub_offset;
2673
2674   /* Given the symbol's value and its section we can determine its final
2675      value when building the stubs (so the stub knows where to jump.  */
2676   bfd_vma target_value;
2677   asection *target_section;
2678
2679   /* The symbol table entry, if any, that this was derived from.  */
2680   struct ppc_link_hash_entry *h;
2681
2682   /* And the reloc addend that this was derived from.  */
2683   bfd_vma addend;
2684
2685   /* Where this stub is being called from, or, in the case of combined
2686      stub sections, the first input section in the group.  */
2687   asection *id_sec;
2688 };
2689
2690 struct ppc_branch_hash_entry {
2691
2692   /* Base hash table entry structure.  */
2693   struct bfd_hash_entry root;
2694
2695   /* Offset within .branch_lt.  */
2696   unsigned int offset;
2697
2698   /* Generation marker.  */
2699   unsigned int iter;
2700 };
2701
2702 struct ppc_link_hash_entry
2703 {
2704   struct elf_link_hash_entry elf;
2705
2706   /* A pointer to the most recently used stub hash entry against this
2707      symbol.  */
2708   struct ppc_stub_hash_entry *stub_cache;
2709
2710   /* Track dynamic relocs copied for this symbol.  */
2711   struct ppc_dyn_relocs *dyn_relocs;
2712
2713   /* Link between function code and descriptor symbols.  */
2714   struct elf_link_hash_entry *oh;
2715
2716   /* Flag function code and descriptor symbols.  */
2717   unsigned int is_func:1;
2718   unsigned int is_func_descriptor:1;
2719   unsigned int is_entry:1;
2720
2721   /* Contexts in which symbol is used in the GOT (or TOC).
2722      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
2723      corresponding relocs are encountered during check_relocs.
2724      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2725      indicate the corresponding GOT entry type is not needed.
2726      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
2727      a TPREL one.  We use a separate flag rather than setting TPREL
2728      just for convenience in distinguishing the two cases.  */
2729 #define TLS_GD           1      /* GD reloc. */
2730 #define TLS_LD           2      /* LD reloc. */
2731 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
2732 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
2733 #define TLS_TLS         16      /* Any TLS reloc.  */
2734 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
2735 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
2736   char tls_mask;
2737 };
2738
2739 /* ppc64 ELF linker hash table.  */
2740
2741 struct ppc_link_hash_table
2742 {
2743   struct elf_link_hash_table elf;
2744
2745   /* The stub hash table.  */
2746   struct bfd_hash_table stub_hash_table;
2747
2748   /* Another hash table for plt_branch stubs.  */
2749   struct bfd_hash_table branch_hash_table;
2750
2751   /* Linker stub bfd.  */
2752   bfd *stub_bfd;
2753
2754   /* Linker call-backs.  */
2755   asection * (*add_stub_section) PARAMS ((const char *, asection *));
2756   void (*layout_sections_again) PARAMS ((void));
2757
2758   /* Array to keep track of which stub sections have been created, and
2759      information on stub grouping.  */
2760   struct map_stub {
2761     /* This is the section to which stubs in the group will be attached.  */
2762     asection *link_sec;
2763     /* The stub section.  */
2764     asection *stub_sec;
2765     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
2766     bfd_vma toc_off;
2767   } *stub_group;
2768
2769   /* Support for multiple toc sections.  */
2770   unsigned int no_multi_toc;
2771   unsigned int multi_toc_needed;
2772
2773   /* Temp used when calculating TOC pointers.  */
2774   bfd_vma toc_curr;
2775
2776   /* Assorted information used by ppc64_elf_size_stubs.  */
2777   int top_index;
2778   asection **input_list;
2779
2780   /* Short-cuts to get to dynamic linker sections.  */
2781   asection *sgot;
2782   asection *srelgot;
2783   asection *splt;
2784   asection *srelplt;
2785   asection *sdynbss;
2786   asection *srelbss;
2787   asection *sglink;
2788   asection *sfpr;
2789   asection *sbrlt;
2790   asection *srelbrlt;
2791
2792   /* Short-cut to first output tls section.  */
2793   asection *tls_sec;
2794
2795   /* Shortcut to .__tls_get_addr.  */
2796   struct elf_link_hash_entry *tls_get_addr;
2797
2798   /* TLS local dynamic got entry handling.  */
2799   union {
2800     bfd_signed_vma refcount;
2801     bfd_vma offset;
2802   } tlsld_got;
2803
2804   /* Set if we should emit symbols for stubs.  */
2805   unsigned int emit_stub_syms;
2806
2807   /* Set on error.  */
2808   unsigned int stub_error;
2809
2810   /* Flag set when small branches are detected.  Used to
2811      select suitable defaults for the stub group size.  */
2812   unsigned int has_14bit_branch;
2813
2814   /* Set if we detect a reference undefined weak symbol.  */
2815   unsigned int have_undefweak;
2816
2817   /* Incremented every time we size stubs.  */
2818   unsigned int stub_iteration;
2819
2820   /* Small local sym to section mapping cache.  */
2821   struct sym_sec_cache sym_sec;
2822 };
2823
2824 static struct bfd_hash_entry *stub_hash_newfunc
2825   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2826 static struct bfd_hash_entry *branch_hash_newfunc
2827   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2828 static struct bfd_hash_entry *link_hash_newfunc
2829   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2830 static struct bfd_link_hash_table *ppc64_elf_link_hash_table_create
2831   PARAMS ((bfd *));
2832 static void ppc64_elf_link_hash_table_free
2833   PARAMS ((struct bfd_link_hash_table *));
2834 static char *ppc_stub_name
2835   PARAMS ((const asection *, const asection *,
2836            const struct ppc_link_hash_entry *, const Elf_Internal_Rela *));
2837 static struct ppc_stub_hash_entry *ppc_get_stub_entry
2838   PARAMS ((const asection *, const asection *, struct elf_link_hash_entry *,
2839            const Elf_Internal_Rela *, struct ppc_link_hash_table *));
2840 static struct ppc_stub_hash_entry *ppc_add_stub
2841   PARAMS ((const char *, asection *, struct ppc_link_hash_table *));
2842 static bfd_boolean create_linkage_sections
2843   PARAMS ((bfd *, struct bfd_link_info *));
2844 static bfd_boolean create_got_section
2845   PARAMS ((bfd *, struct bfd_link_info *));
2846 static bfd_boolean ppc64_elf_create_dynamic_sections
2847   PARAMS ((bfd *, struct bfd_link_info *));
2848 static void ppc64_elf_copy_indirect_symbol
2849   PARAMS ((struct elf_backend_data *, struct elf_link_hash_entry *,
2850            struct elf_link_hash_entry *));
2851 static bfd_boolean update_local_sym_info
2852   PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned long, bfd_vma, int));
2853 static bfd_boolean update_plt_info
2854   PARAMS ((bfd *, struct ppc_link_hash_entry *, bfd_vma));
2855 static bfd_boolean ppc64_elf_check_relocs
2856   PARAMS ((bfd *, struct bfd_link_info *, asection *,
2857            const Elf_Internal_Rela *));
2858 static asection * ppc64_elf_gc_mark_hook
2859   PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
2860            struct elf_link_hash_entry *, Elf_Internal_Sym *));
2861 static bfd_boolean ppc64_elf_gc_sweep_hook
2862   PARAMS ((bfd *, struct bfd_link_info *, asection *,
2863            const Elf_Internal_Rela *));
2864 static bfd_boolean func_desc_adjust
2865   PARAMS ((struct elf_link_hash_entry *, PTR));
2866 static bfd_boolean ppc64_elf_func_desc_adjust
2867   PARAMS ((bfd *, struct bfd_link_info *));
2868 static bfd_boolean ppc64_elf_adjust_dynamic_symbol
2869   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
2870 static void ppc64_elf_hide_symbol
2871   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean));
2872 static bfd_boolean get_sym_h
2873   PARAMS ((struct elf_link_hash_entry **, Elf_Internal_Sym **, asection **,
2874            char **, Elf_Internal_Sym **, unsigned long, bfd *));
2875 static int get_tls_mask
2876   PARAMS ((char **, Elf_Internal_Sym **, const Elf_Internal_Rela *, bfd *));
2877 static bfd_boolean allocate_dynrelocs
2878   PARAMS ((struct elf_link_hash_entry *, PTR));
2879 static bfd_boolean readonly_dynrelocs
2880   PARAMS ((struct elf_link_hash_entry *, PTR));
2881 static enum elf_reloc_type_class ppc64_elf_reloc_type_class
2882   PARAMS ((const Elf_Internal_Rela *));
2883 static bfd_boolean ppc64_elf_size_dynamic_sections
2884   PARAMS ((bfd *, struct bfd_link_info *));
2885 static enum ppc_stub_type ppc_type_of_stub
2886   PARAMS ((asection *, const Elf_Internal_Rela *,
2887            struct ppc_link_hash_entry **, bfd_vma));
2888 static bfd_byte *build_plt_stub
2889   PARAMS ((bfd *, bfd_byte *, int));
2890 static bfd_boolean ppc_build_one_stub
2891   PARAMS ((struct bfd_hash_entry *, PTR));
2892 static bfd_boolean ppc_size_one_stub
2893   PARAMS ((struct bfd_hash_entry *, PTR));
2894 static void group_sections
2895   PARAMS ((struct ppc_link_hash_table *, bfd_size_type, bfd_boolean));
2896 static bfd_boolean ppc64_elf_relocate_section
2897   PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
2898            Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
2899            asection **));
2900 static bfd_boolean ppc64_elf_finish_dynamic_symbol
2901   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
2902            Elf_Internal_Sym *));
2903 static bfd_boolean ppc64_elf_finish_dynamic_sections
2904   PARAMS ((bfd *, struct bfd_link_info *));
2905
2906 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
2907
2908 #define ppc_hash_table(p) \
2909   ((struct ppc_link_hash_table *) ((p)->hash))
2910
2911 #define ppc_stub_hash_lookup(table, string, create, copy) \
2912   ((struct ppc_stub_hash_entry *) \
2913    bfd_hash_lookup ((table), (string), (create), (copy)))
2914
2915 #define ppc_branch_hash_lookup(table, string, create, copy) \
2916   ((struct ppc_branch_hash_entry *) \
2917    bfd_hash_lookup ((table), (string), (create), (copy)))
2918
2919 /* Create an entry in the stub hash table.  */
2920
2921 static struct bfd_hash_entry *
2922 stub_hash_newfunc (entry, table, string)
2923      struct bfd_hash_entry *entry;
2924      struct bfd_hash_table *table;
2925      const char *string;
2926 {
2927   /* Allocate the structure if it has not already been allocated by a
2928      subclass.  */
2929   if (entry == NULL)
2930     {
2931       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
2932       if (entry == NULL)
2933         return entry;
2934     }
2935
2936   /* Call the allocation method of the superclass.  */
2937   entry = bfd_hash_newfunc (entry, table, string);
2938   if (entry != NULL)
2939     {
2940       struct ppc_stub_hash_entry *eh;
2941
2942       /* Initialize the local fields.  */
2943       eh = (struct ppc_stub_hash_entry *) entry;
2944       eh->stub_type = ppc_stub_none;
2945       eh->stub_sec = NULL;
2946       eh->stub_offset = 0;
2947       eh->target_value = 0;
2948       eh->target_section = NULL;
2949       eh->h = NULL;
2950       eh->id_sec = NULL;
2951     }
2952
2953   return entry;
2954 }
2955
2956 /* Create an entry in the branch hash table.  */
2957
2958 static struct bfd_hash_entry *
2959 branch_hash_newfunc (entry, table, string)
2960      struct bfd_hash_entry *entry;
2961      struct bfd_hash_table *table;
2962      const char *string;
2963 {
2964   /* Allocate the structure if it has not already been allocated by a
2965      subclass.  */
2966   if (entry == NULL)
2967     {
2968       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
2969       if (entry == NULL)
2970         return entry;
2971     }
2972
2973   /* Call the allocation method of the superclass.  */
2974   entry = bfd_hash_newfunc (entry, table, string);
2975   if (entry != NULL)
2976     {
2977       struct ppc_branch_hash_entry *eh;
2978
2979       /* Initialize the local fields.  */
2980       eh = (struct ppc_branch_hash_entry *) entry;
2981       eh->offset = 0;
2982       eh->iter = 0;
2983     }
2984
2985   return entry;
2986 }
2987
2988 /* Create an entry in a ppc64 ELF linker hash table.  */
2989
2990 static struct bfd_hash_entry *
2991 link_hash_newfunc (entry, table, string)
2992      struct bfd_hash_entry *entry;
2993      struct bfd_hash_table *table;
2994      const char *string;
2995 {
2996   /* Allocate the structure if it has not already been allocated by a
2997      subclass.  */
2998   if (entry == NULL)
2999     {
3000       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3001       if (entry == NULL)
3002         return entry;
3003     }
3004
3005   /* Call the allocation method of the superclass.  */
3006   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3007   if (entry != NULL)
3008     {
3009       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3010
3011       eh->stub_cache = NULL;
3012       eh->dyn_relocs = NULL;
3013       eh->oh = NULL;
3014       eh->is_func = 0;
3015       eh->is_func_descriptor = 0;
3016       eh->is_entry = 0;
3017       eh->tls_mask = 0;
3018     }
3019
3020   return entry;
3021 }
3022
3023 /* Create a ppc64 ELF linker hash table.  */
3024
3025 static struct bfd_link_hash_table *
3026 ppc64_elf_link_hash_table_create (abfd)
3027      bfd *abfd;
3028 {
3029   struct ppc_link_hash_table *htab;
3030   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3031
3032   htab = (struct ppc_link_hash_table *) bfd_malloc (amt);
3033   if (htab == NULL)
3034     return NULL;
3035
3036   if (! _bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc))
3037     {
3038       free (htab);
3039       return NULL;
3040     }
3041
3042   /* Init the stub hash table too.  */
3043   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc))
3044     return NULL;
3045
3046   /* And the branch hash table.  */
3047   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc))
3048     return NULL;
3049
3050   htab->stub_bfd = NULL;
3051   htab->add_stub_section = NULL;
3052   htab->layout_sections_again = NULL;
3053   htab->stub_group = NULL;
3054   htab->no_multi_toc = 0;
3055   htab->multi_toc_needed = 0;
3056   htab->toc_curr = 0;
3057   htab->sgot = NULL;
3058   htab->srelgot = NULL;
3059   htab->splt = NULL;
3060   htab->srelplt = NULL;
3061   htab->sdynbss = NULL;
3062   htab->srelbss = NULL;
3063   htab->sglink = NULL;
3064   htab->sfpr = NULL;
3065   htab->sbrlt = NULL;
3066   htab->srelbrlt = NULL;
3067   htab->tls_sec = NULL;
3068   htab->tls_get_addr = NULL;
3069   htab->tlsld_got.refcount = 0;
3070   htab->emit_stub_syms = 0;
3071   htab->stub_error = 0;
3072   htab->has_14bit_branch = 0;
3073   htab->have_undefweak = 0;
3074   htab->stub_iteration = 0;
3075   htab->sym_sec.abfd = NULL;
3076   /* Initializing two fields of the union is just cosmetic.  We really
3077      only care about glist, but when compiled on a 32-bit host the
3078      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
3079      debugger inspection of these fields look nicer.  */
3080   htab->elf.init_refcount.refcount = 0;
3081   htab->elf.init_refcount.glist = NULL;
3082   htab->elf.init_offset.offset = 0;
3083   htab->elf.init_offset.glist = NULL;
3084
3085   return &htab->elf.root;
3086 }
3087
3088 /* Free the derived linker hash table.  */
3089
3090 static void
3091 ppc64_elf_link_hash_table_free (hash)
3092      struct bfd_link_hash_table *hash;
3093 {
3094   struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
3095
3096   bfd_hash_table_free (&ret->stub_hash_table);
3097   bfd_hash_table_free (&ret->branch_hash_table);
3098   _bfd_generic_link_hash_table_free (hash);
3099 }
3100
3101 /* Build a name for an entry in the stub hash table.  */
3102
3103 static char *
3104 ppc_stub_name (input_section, sym_sec, h, rel)
3105      const asection *input_section;
3106      const asection *sym_sec;
3107      const struct ppc_link_hash_entry *h;
3108      const Elf_Internal_Rela *rel;
3109 {
3110   char *stub_name;
3111   bfd_size_type len;
3112
3113   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3114      offsets from a sym as a branch target?  In fact, we could
3115      probably assume the addend is always zero.  */
3116   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3117
3118   if (h)
3119     {
3120       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3121       stub_name = bfd_malloc (len);
3122       if (stub_name != NULL)
3123         {
3124           sprintf (stub_name, "%08x_%s+%x",
3125                    input_section->id & 0xffffffff,
3126                    h->elf.root.root.string,
3127                    (int) rel->r_addend & 0xffffffff);
3128         }
3129     }
3130   else
3131     {
3132       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3133       stub_name = bfd_malloc (len);
3134       if (stub_name != NULL)
3135         {
3136           sprintf (stub_name, "%08x_%x:%x+%x",
3137                    input_section->id & 0xffffffff,
3138                    sym_sec->id & 0xffffffff,
3139                    (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3140                    (int) rel->r_addend & 0xffffffff);
3141         }
3142     }
3143   return stub_name;
3144 }
3145
3146 /* Look up an entry in the stub hash.  Stub entries are cached because
3147    creating the stub name takes a bit of time.  */
3148
3149 static struct ppc_stub_hash_entry *
3150 ppc_get_stub_entry (input_section, sym_sec, hash, rel, htab)
3151      const asection *input_section;
3152      const asection *sym_sec;
3153      struct elf_link_hash_entry *hash;
3154      const Elf_Internal_Rela *rel;
3155      struct ppc_link_hash_table *htab;
3156 {
3157   struct ppc_stub_hash_entry *stub_entry;
3158   struct ppc_link_hash_entry *h = (struct ppc_link_hash_entry *) hash;
3159   const asection *id_sec;
3160
3161   /* If this input section is part of a group of sections sharing one
3162      stub section, then use the id of the first section in the group.
3163      Stub names need to include a section id, as there may well be
3164      more than one stub used to reach say, printf, and we need to
3165      distinguish between them.  */
3166   id_sec = htab->stub_group[input_section->id].link_sec;
3167
3168   if (h != NULL && h->stub_cache != NULL
3169       && h->stub_cache->h == h
3170       && h->stub_cache->id_sec == id_sec)
3171     {
3172       stub_entry = h->stub_cache;
3173     }
3174   else
3175     {
3176       char *stub_name;
3177
3178       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
3179       if (stub_name == NULL)
3180         return NULL;
3181
3182       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3183                                          stub_name, FALSE, FALSE);
3184       if (h != NULL)
3185         h->stub_cache = stub_entry;
3186
3187       free (stub_name);
3188     }
3189
3190   return stub_entry;
3191 }
3192
3193 /* Add a new stub entry to the stub hash.  Not all fields of the new
3194    stub entry are initialised.  */
3195
3196 static struct ppc_stub_hash_entry *
3197 ppc_add_stub (stub_name, section, htab)
3198      const char *stub_name;
3199      asection *section;
3200      struct ppc_link_hash_table *htab;
3201 {
3202   asection *link_sec;
3203   asection *stub_sec;
3204   struct ppc_stub_hash_entry *stub_entry;
3205
3206   link_sec = htab->stub_group[section->id].link_sec;
3207   stub_sec = htab->stub_group[section->id].stub_sec;
3208   if (stub_sec == NULL)
3209     {
3210       stub_sec = htab->stub_group[link_sec->id].stub_sec;
3211       if (stub_sec == NULL)
3212         {
3213           size_t namelen;
3214           bfd_size_type len;
3215           char *s_name;
3216
3217           namelen = strlen (link_sec->name);
3218           len = namelen + sizeof (STUB_SUFFIX);
3219           s_name = bfd_alloc (htab->stub_bfd, len);
3220           if (s_name == NULL)
3221             return NULL;
3222
3223           memcpy (s_name, link_sec->name, namelen);
3224           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3225           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3226           if (stub_sec == NULL)
3227             return NULL;
3228           htab->stub_group[link_sec->id].stub_sec = stub_sec;
3229         }
3230       htab->stub_group[section->id].stub_sec = stub_sec;
3231     }
3232
3233   /* Enter this entry into the linker stub hash table.  */
3234   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3235                                      TRUE, FALSE);
3236   if (stub_entry == NULL)
3237     {
3238       (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
3239                              bfd_archive_filename (section->owner),
3240                              stub_name);
3241       return NULL;
3242     }
3243
3244   stub_entry->stub_sec = stub_sec;
3245   stub_entry->stub_offset = 0;
3246   stub_entry->id_sec = link_sec;
3247   return stub_entry;
3248 }
3249
3250 /* Create sections for linker generated code.  */
3251
3252 static bfd_boolean
3253 create_linkage_sections (dynobj, info)
3254      bfd *dynobj;
3255      struct bfd_link_info *info;
3256 {
3257   struct ppc_link_hash_table *htab;
3258   flagword flags;
3259
3260   htab = ppc_hash_table (info);
3261
3262   /* Create .sfpr for code to save and restore fp regs.  */
3263   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3264            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3265   htab->sfpr = bfd_make_section_anyway (dynobj, ".sfpr");
3266   if (htab->sfpr == NULL
3267       || ! bfd_set_section_flags (dynobj, htab->sfpr, flags)
3268       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3269     return FALSE;
3270
3271   /* Create .glink for lazy dynamic linking support.  */
3272   htab->sglink = bfd_make_section_anyway (dynobj, ".glink");
3273   if (htab->sglink == NULL
3274       || ! bfd_set_section_flags (dynobj, htab->sglink, flags)
3275       || ! bfd_set_section_alignment (dynobj, htab->sglink, 2))
3276     return FALSE;
3277
3278   /* Create .branch_lt for plt_branch stubs.  */
3279   flags = (SEC_ALLOC | SEC_LOAD
3280            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3281   htab->sbrlt = bfd_make_section_anyway (dynobj, ".branch_lt");
3282   if (htab->sbrlt == NULL
3283       || ! bfd_set_section_flags (dynobj, htab->sbrlt, flags)
3284       || ! bfd_set_section_alignment (dynobj, htab->sbrlt, 3))
3285     return FALSE;
3286
3287   if (info->shared)
3288     {
3289       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3290                | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3291       htab->srelbrlt = bfd_make_section_anyway (dynobj, ".rela.branch_lt");
3292       if (!htab->srelbrlt
3293           || ! bfd_set_section_flags (dynobj, htab->srelbrlt, flags)
3294           || ! bfd_set_section_alignment (dynobj, htab->srelbrlt, 3))
3295         return FALSE;
3296     }
3297   return TRUE;
3298 }
3299
3300 /* Create .got and .rela.got sections in DYNOBJ, and set up
3301    shortcuts to them in our hash table.  */
3302
3303 static bfd_boolean
3304 create_got_section (dynobj, info)
3305      bfd *dynobj;
3306      struct bfd_link_info *info;
3307 {
3308   struct ppc_link_hash_table *htab;
3309
3310   if (! _bfd_elf_create_got_section (dynobj, info))
3311     return FALSE;
3312
3313   htab = ppc_hash_table (info);
3314   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
3315   if (!htab->sgot)
3316     abort ();
3317
3318   htab->srelgot = bfd_make_section (dynobj, ".rela.got");
3319   if (!htab->srelgot
3320       || ! bfd_set_section_flags (dynobj, htab->srelgot,
3321                                   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3322                                    | SEC_IN_MEMORY | SEC_LINKER_CREATED
3323                                    | SEC_READONLY))
3324       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
3325     return FALSE;
3326   return TRUE;
3327 }
3328
3329 /* Create the dynamic sections, and set up shortcuts.  */
3330
3331 static bfd_boolean
3332 ppc64_elf_create_dynamic_sections (dynobj, info)
3333      bfd *dynobj;
3334      struct bfd_link_info *info;
3335 {
3336   struct ppc_link_hash_table *htab;
3337
3338   htab = ppc_hash_table (info);
3339   if (!htab->sgot && !create_got_section (dynobj, info))
3340     return FALSE;
3341
3342   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3343     return FALSE;
3344
3345   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
3346   htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
3347   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
3348   if (!info->shared)
3349     htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
3350
3351   if (!htab->splt || !htab->srelplt || !htab->sdynbss
3352       || (!info->shared && !htab->srelbss))
3353     abort ();
3354
3355   return TRUE;
3356 }
3357
3358 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3359
3360 static void
3361 ppc64_elf_copy_indirect_symbol (bed, dir, ind)
3362      struct elf_backend_data *bed ATTRIBUTE_UNUSED;
3363      struct elf_link_hash_entry *dir, *ind;
3364 {
3365   struct ppc_link_hash_entry *edir, *eind;
3366   flagword mask;
3367
3368   edir = (struct ppc_link_hash_entry *) dir;
3369   eind = (struct ppc_link_hash_entry *) ind;
3370
3371   /* Copy over any dynamic relocs we may have on the indirect sym.  */
3372   if (eind->dyn_relocs != NULL)
3373     {
3374       if (edir->dyn_relocs != NULL)
3375         {
3376           struct ppc_dyn_relocs **pp;
3377           struct ppc_dyn_relocs *p;
3378
3379           if (eind->elf.root.type == bfd_link_hash_indirect)
3380             abort ();
3381
3382           /* Add reloc counts against the weak sym to the strong sym
3383              list.  Merge any entries against the same section.  */
3384           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3385             {
3386               struct ppc_dyn_relocs *q;
3387
3388               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3389                 if (q->sec == p->sec)
3390                   {
3391                     q->pc_count += p->pc_count;
3392                     q->count += p->count;
3393                     *pp = p->next;
3394                     break;
3395                   }
3396               if (q == NULL)
3397                 pp = &p->next;
3398             }
3399           *pp = edir->dyn_relocs;
3400         }
3401
3402       edir->dyn_relocs = eind->dyn_relocs;
3403       eind->dyn_relocs = NULL;
3404     }
3405
3406   edir->is_func |= eind->is_func;
3407   edir->is_func_descriptor |= eind->is_func_descriptor;
3408   edir->is_entry |= eind->is_entry;
3409   edir->tls_mask |= eind->tls_mask;
3410
3411   mask = (ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR
3412           | ELF_LINK_HASH_REF_REGULAR_NONWEAK | ELF_LINK_NON_GOT_REF);
3413   /* If called to transfer flags for a weakdef during processing
3414      of elf_adjust_dynamic_symbol, don't copy ELF_LINK_NON_GOT_REF.
3415      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
3416   if (ELIMINATE_COPY_RELOCS
3417       && eind->elf.root.type != bfd_link_hash_indirect
3418       && (edir->elf.elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
3419     mask &= ~ELF_LINK_NON_GOT_REF;
3420
3421   edir->elf.elf_link_hash_flags |= eind->elf.elf_link_hash_flags & mask;
3422
3423   /* If we were called to copy over info for a weak sym, that's all.  */
3424   if (eind->elf.root.type != bfd_link_hash_indirect)
3425     return;
3426
3427   /* Copy over got entries that we may have already seen to the
3428      symbol which just became indirect.  */
3429   if (eind->elf.got.glist != NULL)
3430     {
3431       if (edir->elf.got.glist != NULL)
3432         {
3433           struct got_entry **entp;
3434           struct got_entry *ent;
3435
3436           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3437             {
3438               struct got_entry *dent;
3439
3440               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3441                 if (dent->addend == ent->addend
3442                     && dent->tls_type == ent->tls_type)
3443                   {
3444                     dent->got.refcount += ent->got.refcount;
3445                     *entp = ent->next;
3446                     break;
3447                   }
3448               if (dent == NULL)
3449                 entp = &ent->next;
3450             }
3451           *entp = edir->elf.got.glist;
3452         }
3453
3454       edir->elf.got.glist = eind->elf.got.glist;
3455       eind->elf.got.glist = NULL;
3456     }
3457
3458   /* And plt entries.  */
3459   if (eind->elf.plt.plist != NULL)
3460     {
3461       if (edir->elf.plt.plist != NULL)
3462         {
3463           struct plt_entry **entp;
3464           struct plt_entry *ent;
3465
3466           for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
3467             {
3468               struct plt_entry *dent;
3469
3470               for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
3471                 if (dent->addend == ent->addend)
3472                   {
3473                     dent->plt.refcount += ent->plt.refcount;
3474                     *entp = ent->next;
3475                     break;
3476                   }
3477               if (dent == NULL)
3478                 entp = &ent->next;
3479             }
3480           *entp = edir->elf.plt.plist;
3481         }
3482
3483       edir->elf.plt.plist = eind->elf.plt.plist;
3484       eind->elf.plt.plist = NULL;
3485     }
3486
3487   if (edir->elf.dynindx == -1)
3488     {
3489       edir->elf.dynindx = eind->elf.dynindx;
3490       edir->elf.dynstr_index = eind->elf.dynstr_index;
3491       eind->elf.dynindx = -1;
3492       eind->elf.dynstr_index = 0;
3493     }
3494   else
3495     BFD_ASSERT (eind->elf.dynindx == -1);
3496 }
3497
3498 /* Set a flag, used by ppc64_elf_gc_mark_hook, on the entry symbol and
3499    symbols undefined on the command-line.  */
3500
3501 bfd_boolean
3502 ppc64_elf_mark_entry_syms (info)
3503      struct bfd_link_info *info;
3504 {
3505   struct ppc_link_hash_table *htab;
3506   struct bfd_sym_chain *sym;
3507
3508   htab = ppc_hash_table (info);
3509   for (sym = info->gc_sym_list; sym; sym = sym->next)
3510     {
3511       struct elf_link_hash_entry *h;
3512
3513       h = elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
3514       if (h != NULL)
3515         ((struct ppc_link_hash_entry *) h)->is_entry = 1;
3516     }
3517   return TRUE;
3518 }
3519
3520 static bfd_boolean
3521 update_local_sym_info (abfd, symtab_hdr, r_symndx, r_addend, tls_type)
3522      bfd *abfd;
3523      Elf_Internal_Shdr *symtab_hdr;
3524      unsigned long r_symndx;
3525      bfd_vma r_addend;
3526      int tls_type;
3527 {
3528   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
3529   char *local_got_tls_masks;
3530
3531   if (local_got_ents == NULL)
3532     {
3533       bfd_size_type size = symtab_hdr->sh_info;
3534
3535       size *= sizeof (*local_got_ents) + sizeof (*local_got_tls_masks);
3536       local_got_ents = (struct got_entry **) bfd_zalloc (abfd, size);
3537       if (local_got_ents == NULL)
3538         return FALSE;
3539       elf_local_got_ents (abfd) = local_got_ents;
3540     }
3541
3542   if ((tls_type & TLS_EXPLICIT) == 0)
3543     {
3544       struct got_entry *ent;
3545
3546       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
3547         if (ent->addend == r_addend && ent->tls_type == tls_type)
3548           break;
3549       if (ent == NULL)
3550         {
3551           bfd_size_type amt = sizeof (*ent);
3552           ent = (struct got_entry *) bfd_alloc (abfd, amt);
3553           if (ent == NULL)
3554             return FALSE;
3555           ent->next = local_got_ents[r_symndx];
3556           ent->addend = r_addend;
3557           ent->tls_type = tls_type;
3558           ent->got.refcount = 0;
3559           local_got_ents[r_symndx] = ent;
3560         }
3561       ent->got.refcount += 1;
3562     }
3563
3564   local_got_tls_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
3565   local_got_tls_masks[r_symndx] |= tls_type;
3566   return TRUE;
3567 }
3568
3569 static bfd_boolean
3570 update_plt_info (abfd, eh, addend)
3571      bfd *abfd;
3572      struct ppc_link_hash_entry *eh;
3573      bfd_vma addend;
3574 {
3575   struct plt_entry *ent;
3576
3577   for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
3578     if (ent->addend == addend)
3579       break;
3580   if (ent == NULL)
3581     {
3582       bfd_size_type amt = sizeof (*ent);
3583       ent = (struct plt_entry *) bfd_alloc (abfd, amt);
3584       if (ent == NULL)
3585         return FALSE;
3586       ent->next = eh->elf.plt.plist;
3587       ent->addend = addend;
3588       ent->plt.refcount = 0;
3589       eh->elf.plt.plist = ent;
3590     }
3591   ent->plt.refcount += 1;
3592   eh->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3593   eh->is_func = 1;
3594   return TRUE;
3595 }
3596
3597 /* Look through the relocs for a section during the first phase, and
3598    calculate needed space in the global offset table, procedure
3599    linkage table, and dynamic reloc sections.  */
3600
3601 static bfd_boolean
3602 ppc64_elf_check_relocs (abfd, info, sec, relocs)
3603      bfd *abfd;
3604      struct bfd_link_info *info;
3605      asection *sec;
3606      const Elf_Internal_Rela *relocs;
3607 {
3608   struct ppc_link_hash_table *htab;
3609   Elf_Internal_Shdr *symtab_hdr;
3610   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
3611   const Elf_Internal_Rela *rel;
3612   const Elf_Internal_Rela *rel_end;
3613   asection *sreloc;
3614   asection **opd_sym_map;
3615
3616   if (info->relocateable)
3617     return TRUE;
3618
3619   htab = ppc_hash_table (info);
3620   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3621
3622   sym_hashes = elf_sym_hashes (abfd);
3623   sym_hashes_end = (sym_hashes
3624                     + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
3625                     - symtab_hdr->sh_info);
3626
3627   sreloc = NULL;
3628   opd_sym_map = NULL;
3629   if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
3630     {
3631       /* Garbage collection needs some extra help with .opd sections.
3632          We don't want to necessarily keep everything referenced by
3633          relocs in .opd, as that would keep all functions.  Instead,
3634          if we reference an .opd symbol (a function descriptor), we
3635          want to keep the function code symbol's section.  This is
3636          easy for global symbols, but for local syms we need to keep
3637          information about the associated function section.  Later, if
3638          edit_opd deletes entries, we'll use this array to adjust
3639          local syms in .opd.  */
3640       union opd_info {
3641         asection *func_section;
3642         long entry_adjust;
3643       };
3644       bfd_size_type amt;
3645
3646       amt = sec->_raw_size * sizeof (union opd_info) / 24;
3647       opd_sym_map = (asection **) bfd_zalloc (abfd, amt);
3648       if (opd_sym_map == NULL)
3649         return FALSE;
3650       ppc64_elf_section_data (sec)->opd.func_sec = opd_sym_map;
3651     }
3652
3653   if (htab->elf.dynobj == NULL)
3654     htab->elf.dynobj = abfd;
3655   if (htab->sfpr == NULL
3656       && !create_linkage_sections (htab->elf.dynobj, info))
3657     return FALSE;
3658
3659   rel_end = relocs + sec->reloc_count;
3660   for (rel = relocs; rel < rel_end; rel++)
3661     {
3662       unsigned long r_symndx;
3663       struct elf_link_hash_entry *h;
3664       enum elf_ppc64_reloc_type r_type;
3665       int tls_type = 0;
3666
3667       r_symndx = ELF64_R_SYM (rel->r_info);
3668       if (r_symndx < symtab_hdr->sh_info)
3669         h = NULL;
3670       else
3671         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3672
3673       r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
3674       switch (r_type)
3675         {
3676         case R_PPC64_GOT_TLSLD16:
3677         case R_PPC64_GOT_TLSLD16_LO:
3678         case R_PPC64_GOT_TLSLD16_HI:
3679         case R_PPC64_GOT_TLSLD16_HA:
3680           htab->tlsld_got.refcount += 1;
3681           tls_type = TLS_TLS | TLS_LD;
3682           goto dogottls;
3683
3684         case R_PPC64_GOT_TLSGD16:
3685         case R_PPC64_GOT_TLSGD16_LO:
3686         case R_PPC64_GOT_TLSGD16_HI:
3687         case R_PPC64_GOT_TLSGD16_HA:
3688           tls_type = TLS_TLS | TLS_GD;
3689           goto dogottls;
3690
3691         case R_PPC64_GOT_TPREL16_DS:
3692         case R_PPC64_GOT_TPREL16_LO_DS:
3693         case R_PPC64_GOT_TPREL16_HI:
3694         case R_PPC64_GOT_TPREL16_HA:
3695           if (info->shared)
3696             info->flags |= DF_STATIC_TLS;
3697           tls_type = TLS_TLS | TLS_TPREL;
3698           goto dogottls;
3699
3700         case R_PPC64_GOT_DTPREL16_DS:
3701         case R_PPC64_GOT_DTPREL16_LO_DS:
3702         case R_PPC64_GOT_DTPREL16_HI:
3703         case R_PPC64_GOT_DTPREL16_HA:
3704           tls_type = TLS_TLS | TLS_DTPREL;
3705         dogottls:
3706           sec->has_tls_reloc = 1;
3707           /* Fall thru */
3708
3709         case R_PPC64_GOT16:
3710         case R_PPC64_GOT16_DS:
3711         case R_PPC64_GOT16_HA:
3712         case R_PPC64_GOT16_HI:
3713         case R_PPC64_GOT16_LO:
3714         case R_PPC64_GOT16_LO_DS:
3715           /* This symbol requires a global offset table entry.  */
3716           sec->has_gp_reloc = 1;
3717           if (htab->sgot == NULL
3718               && !create_got_section (htab->elf.dynobj, info))
3719             return FALSE;
3720
3721           if (h != NULL)
3722             {
3723               struct ppc_link_hash_entry *eh;
3724               struct got_entry *ent;
3725
3726               eh = (struct ppc_link_hash_entry *) h;
3727               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
3728                 if (ent->addend == rel->r_addend
3729                     && ent->tls_type == tls_type)
3730                   break;
3731               if (ent == NULL)
3732                 {
3733                   bfd_size_type amt = sizeof (*ent);
3734                   ent = (struct got_entry *) bfd_alloc (abfd, amt);
3735                   if (ent == NULL)
3736                     return FALSE;
3737                   ent->next = eh->elf.got.glist;
3738                   ent->addend = rel->r_addend;
3739                   ent->tls_type = tls_type;
3740                   ent->got.refcount = 0;
3741                   eh->elf.got.glist = ent;
3742                 }
3743               ent->got.refcount += 1;
3744               eh->tls_mask |= tls_type;
3745             }
3746           else
3747             /* This is a global offset table entry for a local symbol.  */
3748             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
3749                                         rel->r_addend, tls_type))
3750               return FALSE;
3751           break;
3752
3753         case R_PPC64_PLT16_HA:
3754         case R_PPC64_PLT16_HI:
3755         case R_PPC64_PLT16_LO:
3756         case R_PPC64_PLT32:
3757         case R_PPC64_PLT64:
3758           /* This symbol requires a procedure linkage table entry.  We
3759              actually build the entry in adjust_dynamic_symbol,
3760              because this might be a case of linking PIC code without
3761              linking in any dynamic objects, in which case we don't
3762              need to generate a procedure linkage table after all.  */
3763           if (h == NULL)
3764             {
3765               /* It does not make sense to have a procedure linkage
3766                  table entry for a local symbol.  */
3767               bfd_set_error (bfd_error_bad_value);
3768               return FALSE;
3769             }
3770           else
3771             if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
3772                                   rel->r_addend))
3773               return FALSE;
3774           break;
3775
3776           /* The following relocations don't need to propagate the
3777              relocation if linking a shared object since they are
3778              section relative.  */
3779         case R_PPC64_SECTOFF:
3780         case R_PPC64_SECTOFF_LO:
3781         case R_PPC64_SECTOFF_HI:
3782         case R_PPC64_SECTOFF_HA:
3783         case R_PPC64_SECTOFF_DS:
3784         case R_PPC64_SECTOFF_LO_DS:
3785         case R_PPC64_DTPREL16:
3786         case R_PPC64_DTPREL16_LO:
3787         case R_PPC64_DTPREL16_HI:
3788         case R_PPC64_DTPREL16_HA:
3789         case R_PPC64_DTPREL16_DS:
3790         case R_PPC64_DTPREL16_LO_DS:
3791         case R_PPC64_DTPREL16_HIGHER:
3792         case R_PPC64_DTPREL16_HIGHERA:
3793         case R_PPC64_DTPREL16_HIGHEST:
3794         case R_PPC64_DTPREL16_HIGHESTA:
3795           break;
3796
3797           /* Nor do these.  */
3798         case R_PPC64_TOC16:
3799         case R_PPC64_TOC16_LO:
3800         case R_PPC64_TOC16_HI:
3801         case R_PPC64_TOC16_HA:
3802         case R_PPC64_TOC16_DS:
3803         case R_PPC64_TOC16_LO_DS:
3804           sec->has_gp_reloc = 1;
3805           break;
3806
3807           /* This relocation describes the C++ object vtable hierarchy.
3808              Reconstruct it for later use during GC.  */
3809         case R_PPC64_GNU_VTINHERIT:
3810           if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3811             return FALSE;
3812           break;
3813
3814           /* This relocation describes which C++ vtable entries are actually
3815              used.  Record for later use during GC.  */
3816         case R_PPC64_GNU_VTENTRY:
3817           if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3818             return FALSE;
3819           break;
3820
3821         case R_PPC64_REL14:
3822         case R_PPC64_REL14_BRTAKEN:
3823         case R_PPC64_REL14_BRNTAKEN:
3824           htab->has_14bit_branch = 1;
3825           /* Fall through.  */
3826
3827         case R_PPC64_REL24:
3828           if (h != NULL
3829               && h->root.root.string[0] == '.'
3830               && h->root.root.string[1] != 0)
3831             {
3832               /* We may need a .plt entry if the function this reloc
3833                  refers to is in a shared lib.  */
3834               if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
3835                                     rel->r_addend))
3836                 return FALSE;
3837               if (h == htab->tls_get_addr)
3838                 sec->has_tls_reloc = 1;
3839               else if ((strncmp (h->root.root.string, ".__tls_get_addr", 15)
3840                         == 0)
3841                        && (h->root.root.string[15] == 0
3842                            || h->root.root.string[15] == '@'))
3843                 {
3844                   htab->tls_get_addr = h;
3845                   sec->has_tls_reloc = 1;
3846                 }
3847             }
3848           break;
3849
3850         case R_PPC64_TPREL64:
3851           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
3852           if (info->shared)
3853             info->flags |= DF_STATIC_TLS;
3854           goto dotlstoc;
3855
3856         case R_PPC64_DTPMOD64:
3857           if (rel + 1 < rel_end
3858               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
3859               && rel[1].r_offset == rel->r_offset + 8)
3860             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
3861           else
3862             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
3863           goto dotlstoc;
3864
3865         case R_PPC64_DTPREL64:
3866           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
3867           if (rel != relocs
3868               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
3869               && rel[-1].r_offset == rel->r_offset - 8)
3870             /* This is the second reloc of a dtpmod, dtprel pair.
3871                Don't mark with TLS_DTPREL.  */
3872             goto dodyn;
3873
3874         dotlstoc:
3875           sec->has_tls_reloc = 1;
3876           if (h != NULL)
3877             {
3878               struct ppc_link_hash_entry *eh;
3879               eh = (struct ppc_link_hash_entry *) h;
3880               eh->tls_mask |= tls_type;
3881             }
3882           else
3883             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
3884                                         rel->r_addend, tls_type))
3885               return FALSE;
3886
3887           if (ppc64_elf_section_data (sec)->t_symndx == NULL)
3888             {
3889               /* One extra to simplify get_tls_mask.  */
3890               bfd_size_type amt = sec->_raw_size * sizeof (unsigned) / 8 + 1;
3891               ppc64_elf_section_data (sec)->t_symndx
3892                 = (unsigned *) bfd_zalloc (abfd, amt);
3893               if (ppc64_elf_section_data (sec)->t_symndx == NULL)
3894                 return FALSE;
3895             }
3896           BFD_ASSERT (rel->r_offset % 8 == 0);
3897           ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8] = r_symndx;
3898
3899           /* Mark the second slot of a GD or LD entry.
3900              -1 to indicate GD and -2 to indicate LD.  */
3901           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3902             ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -1;
3903           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3904             ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -2;
3905           goto dodyn;
3906
3907         case R_PPC64_TPREL16:
3908         case R_PPC64_TPREL16_LO:
3909         case R_PPC64_TPREL16_HI:
3910         case R_PPC64_TPREL16_HA:
3911         case R_PPC64_TPREL16_DS:
3912         case R_PPC64_TPREL16_LO_DS:
3913         case R_PPC64_TPREL16_HIGHER:
3914         case R_PPC64_TPREL16_HIGHERA:
3915         case R_PPC64_TPREL16_HIGHEST:
3916         case R_PPC64_TPREL16_HIGHESTA:
3917           if (info->shared)
3918             {
3919               info->flags |= DF_STATIC_TLS;
3920               goto dodyn;
3921             }
3922           break;
3923
3924         case R_PPC64_ADDR64:
3925           if (opd_sym_map != NULL
3926               && h != NULL
3927               && h->root.root.string[0] == '.'
3928               && h->root.root.string[1] != 0)
3929             {
3930               struct elf_link_hash_entry *fdh;
3931
3932               fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
3933                                           FALSE, FALSE, FALSE);
3934               if (fdh != NULL)
3935                 {
3936                   ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
3937                   ((struct ppc_link_hash_entry *) fdh)->oh = h;
3938                   ((struct ppc_link_hash_entry *) h)->is_func = 1;
3939                   ((struct ppc_link_hash_entry *) h)->oh = fdh;
3940                 }
3941             }
3942           if (opd_sym_map != NULL
3943               && h == NULL
3944               && rel + 1 < rel_end
3945               && ((enum elf_ppc64_reloc_type) ELF64_R_TYPE ((rel + 1)->r_info)
3946                   == R_PPC64_TOC))
3947             {
3948               asection *s;
3949
3950               s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
3951                                              r_symndx);
3952               if (s == NULL)
3953                 return FALSE;
3954               else if (s != sec)
3955                 opd_sym_map[rel->r_offset / 24] = s;
3956             }
3957           /* Fall through.  */
3958
3959         case R_PPC64_REL30:
3960         case R_PPC64_REL32:
3961         case R_PPC64_REL64:
3962         case R_PPC64_ADDR14:
3963         case R_PPC64_ADDR14_BRNTAKEN:
3964         case R_PPC64_ADDR14_BRTAKEN:
3965         case R_PPC64_ADDR16:
3966         case R_PPC64_ADDR16_DS:
3967         case R_PPC64_ADDR16_HA:
3968         case R_PPC64_ADDR16_HI:
3969         case R_PPC64_ADDR16_HIGHER:
3970         case R_PPC64_ADDR16_HIGHERA:
3971         case R_PPC64_ADDR16_HIGHEST:
3972         case R_PPC64_ADDR16_HIGHESTA:
3973         case R_PPC64_ADDR16_LO:
3974         case R_PPC64_ADDR16_LO_DS:
3975         case R_PPC64_ADDR24:
3976         case R_PPC64_ADDR32:
3977         case R_PPC64_UADDR16:
3978         case R_PPC64_UADDR32:
3979         case R_PPC64_UADDR64:
3980         case R_PPC64_TOC:
3981           if (h != NULL && !info->shared)
3982             /* We may need a copy reloc.  */
3983             h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
3984
3985           /* Don't propagate .opd relocs.  */
3986           if (NO_OPD_RELOCS && opd_sym_map != NULL)
3987             break;
3988
3989           /* Don't propagate relocs that the dynamic linker won't relocate.  */
3990           if ((sec->flags & SEC_ALLOC) == 0)
3991             break;
3992
3993           /* If we are creating a shared library, and this is a reloc
3994              against a global symbol, or a non PC relative reloc
3995              against a local symbol, then we need to copy the reloc
3996              into the shared library.  However, if we are linking with
3997              -Bsymbolic, we do not need to copy a reloc against a
3998              global symbol which is defined in an object we are
3999              including in the link (i.e., DEF_REGULAR is set).  At
4000              this point we have not seen all the input files, so it is
4001              possible that DEF_REGULAR is not set now but will be set
4002              later (it is never cleared).  In case of a weak definition,
4003              DEF_REGULAR may be cleared later by a strong definition in
4004              a shared library.  We account for that possibility below by
4005              storing information in the dyn_relocs field of the hash
4006              table entry.  A similar situation occurs when creating
4007              shared libraries and symbol visibility changes render the
4008              symbol local.
4009
4010              If on the other hand, we are creating an executable, we
4011              may need to keep relocations for symbols satisfied by a
4012              dynamic library if we manage to avoid copy relocs for the
4013              symbol.  */
4014         dodyn:
4015           if ((info->shared
4016                && (MUST_BE_DYN_RELOC (r_type)
4017                    || (h != NULL
4018                        && (! info->symbolic
4019                            || h->root.type == bfd_link_hash_defweak
4020                            || (h->elf_link_hash_flags
4021                                & ELF_LINK_HASH_DEF_REGULAR) == 0))))
4022               || (ELIMINATE_COPY_RELOCS
4023                   && !info->shared
4024                   && h != NULL
4025                   && (h->root.type == bfd_link_hash_defweak
4026                       || (h->elf_link_hash_flags
4027                           & ELF_LINK_HASH_DEF_REGULAR) == 0)))
4028             {
4029               struct ppc_dyn_relocs *p;
4030               struct ppc_dyn_relocs **head;
4031
4032               /* We must copy these reloc types into the output file.
4033                  Create a reloc section in dynobj and make room for
4034                  this reloc.  */
4035               if (sreloc == NULL)
4036                 {
4037                   const char *name;
4038                   bfd *dynobj;
4039
4040                   name = (bfd_elf_string_from_elf_section
4041                           (abfd,
4042                            elf_elfheader (abfd)->e_shstrndx,
4043                            elf_section_data (sec)->rel_hdr.sh_name));
4044                   if (name == NULL)
4045                     return FALSE;
4046
4047                   if (strncmp (name, ".rela", 5) != 0
4048                       || strcmp (bfd_get_section_name (abfd, sec),
4049                                  name + 5) != 0)
4050                     {
4051                       (*_bfd_error_handler)
4052                         (_("%s: bad relocation section name `%s\'"),
4053                          bfd_archive_filename (abfd), name);
4054                       bfd_set_error (bfd_error_bad_value);
4055                     }
4056
4057                   dynobj = htab->elf.dynobj;
4058                   sreloc = bfd_get_section_by_name (dynobj, name);
4059                   if (sreloc == NULL)
4060                     {
4061                       flagword flags;
4062
4063                       sreloc = bfd_make_section (dynobj, name);
4064                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
4065                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4066                       if ((sec->flags & SEC_ALLOC) != 0)
4067                         flags |= SEC_ALLOC | SEC_LOAD;
4068                       if (sreloc == NULL
4069                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
4070                           || ! bfd_set_section_alignment (dynobj, sreloc, 3))
4071                         return FALSE;
4072                     }
4073                   elf_section_data (sec)->sreloc = sreloc;
4074                 }
4075
4076               /* If this is a global symbol, we count the number of
4077                  relocations we need for this symbol.  */
4078               if (h != NULL)
4079                 {
4080                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
4081                 }
4082               else
4083                 {
4084                   /* Track dynamic relocs needed for local syms too.
4085                      We really need local syms available to do this
4086                      easily.  Oh well.  */
4087
4088                   asection *s;
4089                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4090                                                  sec, r_symndx);
4091                   if (s == NULL)
4092                     return FALSE;
4093
4094                   head = ((struct ppc_dyn_relocs **)
4095                           &elf_section_data (s)->local_dynrel);
4096                 }
4097
4098               p = *head;
4099               if (p == NULL || p->sec != sec)
4100                 {
4101                   p = ((struct ppc_dyn_relocs *)
4102                        bfd_alloc (htab->elf.dynobj,
4103                                   (bfd_size_type) sizeof *p));
4104                   if (p == NULL)
4105                     return FALSE;
4106                   p->next = *head;
4107                   *head = p;
4108                   p->sec = sec;
4109                   p->count = 0;
4110                   p->pc_count = 0;
4111                 }
4112
4113               p->count += 1;
4114               if (!MUST_BE_DYN_RELOC (r_type))
4115                 p->pc_count += 1;
4116             }
4117           break;
4118
4119         default:
4120           break;
4121         }
4122     }
4123
4124   return TRUE;
4125 }
4126
4127 /* Return the section that should be marked against GC for a given
4128    relocation.  */
4129
4130 static asection *
4131 ppc64_elf_gc_mark_hook (sec, info, rel, h, sym)
4132      asection *sec;
4133      struct bfd_link_info *info ATTRIBUTE_UNUSED;
4134      Elf_Internal_Rela *rel;
4135      struct elf_link_hash_entry *h;
4136      Elf_Internal_Sym *sym;
4137 {
4138   asection *rsec = NULL;
4139
4140   if (h != NULL)
4141     {
4142       enum elf_ppc64_reloc_type r_type;
4143       struct ppc_link_hash_entry *fdh;
4144
4145       r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
4146       switch (r_type)
4147         {
4148         case R_PPC64_GNU_VTINHERIT:
4149         case R_PPC64_GNU_VTENTRY:
4150           break;
4151
4152         default:
4153           switch (h->root.type)
4154             {
4155             case bfd_link_hash_defined:
4156             case bfd_link_hash_defweak:
4157               fdh = (struct ppc_link_hash_entry *) h;
4158
4159               /* Function descriptor syms cause the associated
4160                  function code sym section to be marked.  */
4161               if (fdh->is_func_descriptor)
4162                 rsec = fdh->oh->root.u.def.section;
4163
4164               /* Function entry syms return NULL if they are in .opd
4165                  and are not ._start (or others undefined on the ld
4166                  command line).  Thus we avoid marking all function
4167                  sections, as all functions are referenced in .opd.  */
4168               else if ((fdh->oh != NULL
4169                         && ((struct ppc_link_hash_entry *) fdh->oh)->is_entry)
4170                        || ppc64_elf_section_data (sec)->opd.func_sec == NULL)
4171                 rsec = h->root.u.def.section;
4172               break;
4173
4174             case bfd_link_hash_common:
4175               rsec = h->root.u.c.p->section;
4176               break;
4177
4178             default:
4179               break;
4180             }
4181         }
4182     }
4183   else
4184     {
4185       asection **opd_sym_section;
4186
4187       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4188       opd_sym_section = ppc64_elf_section_data (rsec)->opd.func_sec;
4189       if (opd_sym_section != NULL)
4190         rsec = opd_sym_section[sym->st_value / 24];
4191       else if (ppc64_elf_section_data (sec)->opd.func_sec != NULL)
4192         rsec = NULL;
4193     }
4194
4195   return rsec;
4196 }
4197
4198 /* Update the .got, .plt. and dynamic reloc reference counts for the
4199    section being removed.  */
4200
4201 static bfd_boolean
4202 ppc64_elf_gc_sweep_hook (abfd, info, sec, relocs)
4203      bfd *abfd;
4204      struct bfd_link_info *info;
4205      asection *sec;
4206      const Elf_Internal_Rela *relocs;
4207 {
4208   struct ppc_link_hash_table *htab;
4209   Elf_Internal_Shdr *symtab_hdr;
4210   struct elf_link_hash_entry **sym_hashes;
4211   struct got_entry **local_got_ents;
4212   const Elf_Internal_Rela *rel, *relend;
4213
4214   elf_section_data (sec)->local_dynrel = NULL;
4215
4216   htab = ppc_hash_table (info);
4217   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4218   sym_hashes = elf_sym_hashes (abfd);
4219   local_got_ents = elf_local_got_ents (abfd);
4220
4221   relend = relocs + sec->reloc_count;
4222   for (rel = relocs; rel < relend; rel++)
4223     {
4224       unsigned long r_symndx;
4225       enum elf_ppc64_reloc_type r_type;
4226       struct elf_link_hash_entry *h = NULL;
4227       char tls_type = 0;
4228
4229       r_symndx = ELF64_R_SYM (rel->r_info);
4230       r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
4231       if (r_symndx >= symtab_hdr->sh_info)
4232         {
4233           struct ppc_link_hash_entry *eh;
4234           struct ppc_dyn_relocs **pp;
4235           struct ppc_dyn_relocs *p;
4236
4237           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4238           eh = (struct ppc_link_hash_entry *) h;
4239
4240           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4241             if (p->sec == sec)
4242               {
4243                 /* Everything must go for SEC.  */
4244                 *pp = p->next;
4245                 break;
4246               }
4247         }
4248
4249       switch (r_type)
4250         {
4251         case R_PPC64_GOT_TLSLD16:
4252         case R_PPC64_GOT_TLSLD16_LO:
4253         case R_PPC64_GOT_TLSLD16_HI:
4254         case R_PPC64_GOT_TLSLD16_HA:
4255           htab->tlsld_got.refcount -= 1;
4256           tls_type = TLS_TLS | TLS_LD;
4257           goto dogot;
4258
4259         case R_PPC64_GOT_TLSGD16:
4260         case R_PPC64_GOT_TLSGD16_LO:
4261         case R_PPC64_GOT_TLSGD16_HI:
4262         case R_PPC64_GOT_TLSGD16_HA:
4263           tls_type = TLS_TLS | TLS_GD;
4264           goto dogot;
4265
4266         case R_PPC64_GOT_TPREL16_DS:
4267         case R_PPC64_GOT_TPREL16_LO_DS:
4268         case R_PPC64_GOT_TPREL16_HI:
4269         case R_PPC64_GOT_TPREL16_HA:
4270           tls_type = TLS_TLS | TLS_TPREL;
4271           goto dogot;
4272
4273         case R_PPC64_GOT_DTPREL16_DS:
4274         case R_PPC64_GOT_DTPREL16_LO_DS:
4275         case R_PPC64_GOT_DTPREL16_HI:
4276         case R_PPC64_GOT_DTPREL16_HA:
4277           tls_type = TLS_TLS | TLS_DTPREL;
4278           goto dogot;
4279
4280         case R_PPC64_GOT16:
4281         case R_PPC64_GOT16_DS:
4282         case R_PPC64_GOT16_HA:
4283         case R_PPC64_GOT16_HI:
4284         case R_PPC64_GOT16_LO:
4285         case R_PPC64_GOT16_LO_DS:
4286         dogot:
4287           {
4288             struct got_entry *ent;
4289
4290             if (h != NULL)
4291               ent = h->got.glist;
4292             else
4293               ent = local_got_ents[r_symndx];
4294
4295             for (; ent != NULL; ent = ent->next)
4296               if (ent->addend == rel->r_addend
4297                   && ent->tls_type == tls_type)
4298                 break;
4299             if (ent == NULL)
4300               abort ();
4301             if (ent->got.refcount > 0)
4302               ent->got.refcount -= 1;
4303           }
4304           break;
4305
4306         case R_PPC64_PLT16_HA:
4307         case R_PPC64_PLT16_HI:
4308         case R_PPC64_PLT16_LO:
4309         case R_PPC64_PLT32:
4310         case R_PPC64_PLT64:
4311         case R_PPC64_REL14:
4312         case R_PPC64_REL14_BRNTAKEN:
4313         case R_PPC64_REL14_BRTAKEN:
4314         case R_PPC64_REL24:
4315           if (h != NULL)
4316             {
4317               struct plt_entry *ent;
4318
4319               for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4320                 if (ent->addend == rel->r_addend)
4321                   break;
4322               if (ent == NULL)
4323                 abort ();
4324               if (ent->plt.refcount > 0)
4325                 ent->plt.refcount -= 1;
4326             }
4327           break;
4328
4329         default:
4330           break;
4331         }
4332     }
4333   return TRUE;
4334 }
4335
4336 /* Called via elf_link_hash_traverse to transfer dynamic linking
4337    information on function code symbol entries to their corresponding
4338    function descriptor symbol entries.  */
4339 static bfd_boolean
4340 func_desc_adjust (h, inf)
4341      struct elf_link_hash_entry *h;
4342      PTR inf;
4343 {
4344   struct bfd_link_info *info;
4345   struct ppc_link_hash_table *htab;
4346   struct plt_entry *ent;
4347
4348   if (h->root.type == bfd_link_hash_indirect)
4349     return TRUE;
4350
4351   if (h->root.type == bfd_link_hash_warning)
4352     h = (struct elf_link_hash_entry *) h->root.u.i.link;
4353
4354   info = (struct bfd_link_info *) inf;
4355   htab = ppc_hash_table (info);
4356
4357   /* If this is a function code symbol, transfer dynamic linking
4358      information to the function descriptor symbol.  */
4359   if (!((struct ppc_link_hash_entry *) h)->is_func)
4360     return TRUE;
4361
4362   if (h->root.type == bfd_link_hash_undefweak
4363       && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR))
4364     htab->have_undefweak = TRUE;
4365
4366   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4367     if (ent->plt.refcount > 0)
4368       break;
4369   if (ent != NULL
4370       && h->root.root.string[0] == '.'
4371       && h->root.root.string[1] != '\0')
4372     {
4373       struct elf_link_hash_entry *fdh = ((struct ppc_link_hash_entry *) h)->oh;
4374       bfd_boolean force_local;
4375
4376       /* Find the corresponding function descriptor symbol.  Create it
4377          as undefined if necessary.  */
4378
4379       if (fdh == NULL)
4380         fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
4381                                     FALSE, FALSE, TRUE);
4382
4383       if (fdh == NULL
4384           && info->shared
4385           && (h->root.type == bfd_link_hash_undefined
4386               || h->root.type == bfd_link_hash_undefweak))
4387         {
4388           bfd *abfd;
4389           asymbol *newsym;
4390           struct bfd_link_hash_entry *bh;
4391
4392           abfd = h->root.u.undef.abfd;
4393           newsym = bfd_make_empty_symbol (abfd);
4394           newsym->name = h->root.root.string + 1;
4395           newsym->section = bfd_und_section_ptr;
4396           newsym->value = 0;
4397           newsym->flags = BSF_OBJECT;
4398           if (h->root.type == bfd_link_hash_undefweak)
4399             newsym->flags |= BSF_WEAK;
4400
4401           bh = &fdh->root;
4402           if ( !(_bfd_generic_link_add_one_symbol
4403                  (info, abfd, newsym->name, newsym->flags,
4404                   newsym->section, newsym->value, NULL, FALSE, FALSE, &bh)))
4405             {
4406               return FALSE;
4407             }
4408           fdh = (struct elf_link_hash_entry *) bh;
4409           fdh->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
4410         }
4411
4412       if (fdh != NULL
4413           && (fdh->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
4414           && (info->shared
4415               || (fdh->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4416               || (fdh->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
4417         {
4418           if (fdh->dynindx == -1)
4419             if (! bfd_elf64_link_record_dynamic_symbol (info, fdh))
4420               return FALSE;
4421           fdh->elf_link_hash_flags |= (h->elf_link_hash_flags
4422                                        & (ELF_LINK_HASH_REF_REGULAR
4423                                           | ELF_LINK_HASH_REF_DYNAMIC
4424                                           | ELF_LINK_HASH_REF_REGULAR_NONWEAK
4425                                           | ELF_LINK_NON_GOT_REF));
4426           if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4427             {
4428               fdh->plt.plist = h->plt.plist;
4429               fdh->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4430             }
4431           ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
4432           ((struct ppc_link_hash_entry *) fdh)->oh = h;
4433           ((struct ppc_link_hash_entry *) h)->oh = fdh;
4434         }
4435
4436       /* Now that the info is on the function descriptor, clear the
4437          function code sym info.  Any function code syms for which we
4438          don't have a definition in a regular file, we force local.
4439          This prevents a shared library from exporting syms that have
4440          been imported from another library.  Function code syms that
4441          are really in the library we must leave global to prevent the
4442          linker dragging in a definition from a static library.  */
4443       force_local = (info->shared
4444                      && ((h->elf_link_hash_flags
4445                           & ELF_LINK_HASH_DEF_REGULAR) == 0
4446                          || fdh == NULL
4447                          || (fdh->elf_link_hash_flags
4448                              & ELF_LINK_HASH_DEF_REGULAR) == 0
4449                          || (fdh->elf_link_hash_flags
4450                              & ELF_LINK_FORCED_LOCAL) != 0));
4451       _bfd_elf_link_hash_hide_symbol (info, h, force_local);
4452     }
4453
4454   return TRUE;
4455 }
4456
4457 #define MIN_SAVE_FPR 14
4458 #define MAX_SAVE_FPR 31
4459
4460 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
4461    this hook to a) provide some gcc support functions, and b) transfer
4462    dynamic linking information gathered so far on function code symbol
4463    entries, to their corresponding function descriptor symbol entries.  */
4464 static bfd_boolean
4465 ppc64_elf_func_desc_adjust (obfd, info)
4466      bfd *obfd ATTRIBUTE_UNUSED;
4467      struct bfd_link_info *info;
4468 {
4469   struct ppc_link_hash_table *htab;
4470   unsigned int lowest_savef = MAX_SAVE_FPR + 2;
4471   unsigned int lowest_restf = MAX_SAVE_FPR + 2;
4472   unsigned int i;
4473   struct elf_link_hash_entry *h;
4474   bfd_byte *p;
4475   char sym[10];
4476
4477   htab = ppc_hash_table (info);
4478
4479   if (htab->sfpr == NULL)
4480     /* We don't have any relocs.  */
4481     return TRUE;
4482
4483   /* First provide any missing ._savef* and ._restf* functions.  */
4484   memcpy (sym, "._savef14", 10);
4485   for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
4486     {
4487       sym[7] = i / 10 + '0';
4488       sym[8] = i % 10 + '0';
4489       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
4490       if (h != NULL
4491           && h->root.type == bfd_link_hash_undefined)
4492         {
4493           if (lowest_savef > i)
4494             lowest_savef = i;
4495           h->root.type = bfd_link_hash_defined;
4496           h->root.u.def.section = htab->sfpr;
4497           h->root.u.def.value = (i - lowest_savef) * 4;
4498           h->type = STT_FUNC;
4499           h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4500           _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
4501         }
4502     }
4503
4504   memcpy (sym, "._restf14", 10);
4505   for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
4506     {
4507       sym[7] = i / 10 + '0';
4508       sym[8] = i % 10 + '0';
4509       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
4510       if (h != NULL
4511           && h->root.type == bfd_link_hash_undefined)
4512         {
4513           if (lowest_restf > i)
4514             lowest_restf = i;
4515           h->root.type = bfd_link_hash_defined;
4516           h->root.u.def.section = htab->sfpr;
4517           h->root.u.def.value = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
4518                                  + (i - lowest_restf) * 4);
4519           h->type = STT_FUNC;
4520           h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4521           _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
4522         }
4523     }
4524
4525   elf_link_hash_traverse (&htab->elf, func_desc_adjust, (PTR) info);
4526
4527   htab->sfpr->_raw_size = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
4528                            + (MAX_SAVE_FPR + 2 - lowest_restf) * 4);
4529
4530   if (htab->sfpr->_raw_size == 0)
4531     {
4532       if (!htab->have_undefweak)
4533         {
4534           _bfd_strip_section_from_output (info, htab->sfpr);
4535           return TRUE;
4536         }
4537
4538       htab->sfpr->_raw_size = 4;
4539     }
4540
4541   p = (bfd_byte *) bfd_alloc (htab->elf.dynobj, htab->sfpr->_raw_size);
4542   if (p == NULL)
4543     return FALSE;
4544   htab->sfpr->contents = p;
4545
4546   for (i = lowest_savef; i <= MAX_SAVE_FPR; i++)
4547     {
4548       unsigned int fpr = i << 21;
4549       unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
4550       bfd_put_32 (htab->elf.dynobj, STFD_FR0_0R1 + fpr + stackoff, p);
4551       p += 4;
4552     }
4553   if (lowest_savef <= MAX_SAVE_FPR)
4554     {
4555       bfd_put_32 (htab->elf.dynobj, BLR, p);
4556       p += 4;
4557     }
4558
4559   for (i = lowest_restf; i <= MAX_SAVE_FPR; i++)
4560     {
4561       unsigned int fpr = i << 21;
4562       unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
4563       bfd_put_32 (htab->elf.dynobj, LFD_FR0_0R1 + fpr + stackoff, p);
4564       p += 4;
4565     }
4566   if (lowest_restf <= MAX_SAVE_FPR
4567       || htab->sfpr->_raw_size == 4)
4568     {
4569       bfd_put_32 (htab->elf.dynobj, BLR, p);
4570     }
4571
4572   return TRUE;
4573 }
4574
4575 /* Adjust a symbol defined by a dynamic object and referenced by a
4576    regular object.  The current definition is in some section of the
4577    dynamic object, but we're not including those sections.  We have to
4578    change the definition to something the rest of the link can
4579    understand.  */
4580
4581 static bfd_boolean
4582 ppc64_elf_adjust_dynamic_symbol (info, h)
4583      struct bfd_link_info *info;
4584      struct elf_link_hash_entry *h;
4585 {
4586   struct ppc_link_hash_table *htab;
4587   asection *s;
4588   unsigned int power_of_two;
4589
4590   htab = ppc_hash_table (info);
4591
4592   /* Deal with function syms.  */
4593   if (h->type == STT_FUNC
4594       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4595     {
4596       /* Clear procedure linkage table information for any symbol that
4597          won't need a .plt entry.  */
4598       struct plt_entry *ent;
4599       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4600         if (ent->plt.refcount > 0)
4601           break;
4602       if (!((struct ppc_link_hash_entry *) h)->is_func_descriptor
4603           || ent == NULL
4604           || SYMBOL_CALLS_LOCAL (info, h)
4605           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4606               && h->root.type == bfd_link_hash_undefweak))
4607         {
4608           h->plt.plist = NULL;
4609           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4610         }
4611     }
4612   else
4613     h->plt.plist = NULL;
4614
4615   /* If this is a weak symbol, and there is a real definition, the
4616      processor independent code will have arranged for us to see the
4617      real definition first, and we can just use the same value.  */
4618   if (h->weakdef != NULL)
4619     {
4620       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
4621                   || h->weakdef->root.type == bfd_link_hash_defweak);
4622       h->root.u.def.section = h->weakdef->root.u.def.section;
4623       h->root.u.def.value = h->weakdef->root.u.def.value;
4624       if (ELIMINATE_COPY_RELOCS)
4625         h->elf_link_hash_flags
4626           = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
4627              | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
4628       return TRUE;
4629     }
4630
4631   /* This is a reference to a symbol defined by a dynamic object which
4632      is not a function.  */
4633
4634   /* If we are creating a shared library, we must presume that the
4635      only references to the symbol are via the global offset table.
4636      For such cases we need not do anything here; the relocations will
4637      be handled correctly by relocate_section.  */
4638   if (info->shared)
4639     return TRUE;
4640
4641   /* If there are no references to this symbol that do not use the
4642      GOT, we don't need to generate a copy reloc.  */
4643   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
4644     return TRUE;
4645
4646   if (ELIMINATE_COPY_RELOCS)
4647     {
4648       struct ppc_link_hash_entry * eh;
4649       struct ppc_dyn_relocs *p;
4650
4651       eh = (struct ppc_link_hash_entry *) h;
4652       for (p = eh->dyn_relocs; p != NULL; p = p->next)
4653         {
4654           s = p->sec->output_section;
4655           if (s != NULL && (s->flags & SEC_READONLY) != 0)
4656             break;
4657         }
4658
4659       /* If we didn't find any dynamic relocs in read-only sections, then
4660          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
4661       if (p == NULL)
4662         {
4663           h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
4664           return TRUE;
4665         }
4666     }
4667
4668   /* We must allocate the symbol in our .dynbss section, which will
4669      become part of the .bss section of the executable.  There will be
4670      an entry for this symbol in the .dynsym section.  The dynamic
4671      object will contain position independent code, so all references
4672      from the dynamic object to this symbol will go through the global
4673      offset table.  The dynamic linker will use the .dynsym entry to
4674      determine the address it must put in the global offset table, so
4675      both the dynamic object and the regular object will refer to the
4676      same memory location for the variable.  */
4677
4678   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
4679      to copy the initial value out of the dynamic object and into the
4680      runtime process image.  We need to remember the offset into the
4681      .rela.bss section we are going to use.  */
4682   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4683     {
4684       htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
4685       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
4686     }
4687
4688   /* We need to figure out the alignment required for this symbol.  I
4689      have no idea how ELF linkers handle this.  */
4690   power_of_two = bfd_log2 (h->size);
4691   if (power_of_two > 4)
4692     power_of_two = 4;
4693
4694   /* Apply the required alignment.  */
4695   s = htab->sdynbss;
4696   s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
4697   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
4698     {
4699       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
4700         return FALSE;
4701     }
4702
4703   /* Define the symbol as being at this point in the section.  */
4704   h->root.u.def.section = s;
4705   h->root.u.def.value = s->_raw_size;
4706
4707   /* Increment the section size to make room for the symbol.  */
4708   s->_raw_size += h->size;
4709
4710   return TRUE;
4711 }
4712
4713 /* If given a function descriptor symbol, hide both the function code
4714    sym and the descriptor.  */
4715 static void
4716 ppc64_elf_hide_symbol (info, h, force_local)
4717      struct bfd_link_info *info;
4718      struct elf_link_hash_entry *h;
4719      bfd_boolean force_local;
4720 {
4721   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
4722
4723   if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
4724     {
4725       struct elf_link_hash_entry *fh = ((struct ppc_link_hash_entry *) h)->oh;
4726
4727       if (fh == NULL)
4728         {
4729           const char *p, *q;
4730           struct ppc_link_hash_table *htab;
4731           char save;
4732
4733           /* We aren't supposed to use alloca in BFD because on
4734              systems which do not have alloca the version in libiberty
4735              calls xmalloc, which might cause the program to crash
4736              when it runs out of memory.  This function doesn't have a
4737              return status, so there's no way to gracefully return an
4738              error.  So cheat.  We know that string[-1] can be safely
4739              dereferenced;  It's either a string in an ELF string
4740              table, or allocated in an objalloc structure.  */
4741
4742           p = h->root.root.string - 1;
4743           save = *p;
4744           *(char *) p = '.';
4745           htab = ppc_hash_table (info);
4746           fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
4747           *(char *) p = save;
4748
4749           /* Unfortunately, if it so happens that the string we were
4750              looking for was allocated immediately before this string,
4751              then we overwrote the string terminator.  That's the only
4752              reason the lookup should fail.  */
4753           if (fh == NULL)
4754             {
4755               q = h->root.root.string + strlen (h->root.root.string);
4756               while (q >= h->root.root.string && *q == *p)
4757                 --q, --p;
4758               if (q < h->root.root.string && *p == '.')
4759                 fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
4760             }
4761           if (fh != NULL)
4762             {
4763               ((struct ppc_link_hash_entry *) h)->oh = fh;
4764               ((struct ppc_link_hash_entry *) fh)->oh = h;
4765             }
4766         }
4767       if (fh != NULL)
4768         _bfd_elf_link_hash_hide_symbol (info, fh, force_local);
4769     }
4770 }
4771
4772 static bfd_boolean
4773 get_sym_h (hp, symp, symsecp, tls_maskp, locsymsp, r_symndx, ibfd)
4774      struct elf_link_hash_entry **hp;
4775      Elf_Internal_Sym **symp;
4776      asection **symsecp;
4777      char **tls_maskp;
4778      Elf_Internal_Sym **locsymsp;
4779      unsigned long r_symndx;
4780      bfd *ibfd;
4781 {
4782   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4783
4784   if (r_symndx >= symtab_hdr->sh_info)
4785     {
4786       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
4787       struct elf_link_hash_entry *h;
4788
4789       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4790       while (h->root.type == bfd_link_hash_indirect
4791              || h->root.type == bfd_link_hash_warning)
4792         h = (struct elf_link_hash_entry *) h->root.u.i.link;
4793
4794       if (hp != NULL)
4795         *hp = h;
4796
4797       if (symp != NULL)
4798         *symp = NULL;
4799
4800       if (symsecp != NULL)
4801         {
4802           asection *symsec = NULL;
4803           if (h->root.type == bfd_link_hash_defined
4804               || h->root.type == bfd_link_hash_defweak)
4805             symsec = h->root.u.def.section;
4806           *symsecp = symsec;
4807         }
4808
4809       if (tls_maskp != NULL)
4810         {
4811           struct ppc_link_hash_entry *eh;
4812
4813           eh = (struct ppc_link_hash_entry *) h;
4814           *tls_maskp = &eh->tls_mask;
4815         }
4816     }
4817   else
4818     {
4819       Elf_Internal_Sym *sym;
4820       Elf_Internal_Sym *locsyms = *locsymsp;
4821
4822       if (locsyms == NULL)
4823         {
4824           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4825           if (locsyms == NULL)
4826             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4827                                             symtab_hdr->sh_info,
4828                                             0, NULL, NULL, NULL);
4829           if (locsyms == NULL)
4830             return FALSE;
4831           *locsymsp = locsyms;
4832         }
4833       sym = locsyms + r_symndx;
4834
4835       if (hp != NULL)
4836         *hp = NULL;
4837
4838       if (symp != NULL)
4839         *symp = sym;
4840
4841       if (symsecp != NULL)
4842         {
4843           asection *symsec = NULL;
4844           if ((sym->st_shndx != SHN_UNDEF
4845                && sym->st_shndx < SHN_LORESERVE)
4846               || sym->st_shndx > SHN_HIRESERVE)
4847             symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
4848           *symsecp = symsec;
4849         }
4850
4851       if (tls_maskp != NULL)
4852         {
4853           struct got_entry **lgot_ents;
4854           char *tls_mask;
4855
4856           tls_mask = NULL;
4857           lgot_ents = elf_local_got_ents (ibfd);
4858           if (lgot_ents != NULL)
4859             {
4860               char *lgot_masks = (char *) (lgot_ents + symtab_hdr->sh_info);
4861               tls_mask = &lgot_masks[r_symndx];
4862             }
4863           *tls_maskp = tls_mask;
4864         }
4865     }
4866   return TRUE;
4867 }
4868
4869 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
4870    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
4871    type suitable for optimization, and 1 otherwise.  */
4872
4873 static int
4874 get_tls_mask (tls_maskp, locsymsp, rel, ibfd)
4875      char **tls_maskp;
4876      Elf_Internal_Sym **locsymsp;
4877      const Elf_Internal_Rela *rel;
4878      bfd *ibfd;
4879 {
4880   unsigned long r_symndx;
4881   unsigned int next_r;
4882   struct elf_link_hash_entry *h;
4883   Elf_Internal_Sym *sym;
4884   asection *sec;
4885   bfd_vma off;
4886
4887   r_symndx = ELF64_R_SYM (rel->r_info);
4888   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
4889     return 0;
4890
4891   if ((*tls_maskp != NULL && **tls_maskp != 0)
4892       || sec == NULL
4893       || ppc64_elf_section_data (sec)->t_symndx == NULL)
4894     return 1;
4895
4896   /* Look inside a TOC section too.  */
4897   if (h != NULL)
4898     {
4899       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
4900       off = h->root.u.def.value;
4901     }
4902   else
4903     off = sym->st_value;
4904   off += rel->r_addend;
4905   BFD_ASSERT (off % 8 == 0);
4906   r_symndx = ppc64_elf_section_data (sec)->t_symndx[off / 8];
4907   next_r = ppc64_elf_section_data (sec)->t_symndx[off / 8 + 1];
4908   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
4909     return 0;
4910   if (h == NULL
4911       || h->root.type == bfd_link_hash_defined
4912       || h->root.type == bfd_link_hash_defweak)
4913     {
4914       if (next_r == (unsigned) -1)
4915         return 2;
4916       if (next_r == (unsigned) -2
4917           && (h == NULL
4918               || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
4919         return 3;
4920     }
4921   return 1;
4922 }
4923
4924 bfd_boolean
4925 ppc64_elf_edit_opd (obfd, info)
4926      bfd *obfd;
4927      struct bfd_link_info *info;
4928 {
4929   bfd *ibfd;
4930
4931   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4932     {
4933       asection *sec;
4934       Elf_Internal_Rela *relstart, *rel, *relend;
4935       Elf_Internal_Shdr *symtab_hdr;
4936       Elf_Internal_Sym *local_syms;
4937       struct elf_link_hash_entry **sym_hashes;
4938       bfd_vma offset;
4939       bfd_size_type amt;
4940       long *adjust;
4941       bfd_boolean need_edit;
4942
4943       sec = bfd_get_section_by_name (ibfd, ".opd");
4944       if (sec == NULL)
4945         continue;
4946
4947       amt = sec->_raw_size * sizeof (long) / 24;
4948       adjust = ppc64_elf_section_data (sec)->opd.adjust;
4949       if (adjust == NULL)
4950         {
4951           /* Must be a ld -r link.  ie. check_relocs hasn't been
4952              called.  */
4953           adjust = (long *) bfd_zalloc (obfd, amt);
4954           ppc64_elf_section_data (sec)->opd.adjust = adjust;
4955         }
4956       memset (adjust, 0, (size_t) amt);
4957
4958       if (sec->output_section == bfd_abs_section_ptr)
4959         continue;
4960
4961       /* Look through the section relocs.  */
4962       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
4963         continue;
4964
4965       local_syms = NULL;
4966       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4967       sym_hashes = elf_sym_hashes (ibfd);
4968
4969       /* Read the relocations.  */
4970       relstart = _bfd_elf_link_read_relocs (ibfd, sec, (PTR) NULL,
4971                                             (Elf_Internal_Rela *) NULL,
4972                                             info->keep_memory);
4973       if (relstart == NULL)
4974         return FALSE;
4975
4976       /* First run through the relocs to check they are sane, and to
4977          determine whether we need to edit this opd section.  */
4978       need_edit = FALSE;
4979       offset = 0;
4980       relend = relstart + sec->reloc_count;
4981       for (rel = relstart; rel < relend; rel++)
4982         {
4983           enum elf_ppc64_reloc_type r_type;
4984           unsigned long r_symndx;
4985           asection *sym_sec;
4986           struct elf_link_hash_entry *h;
4987           Elf_Internal_Sym *sym;
4988
4989           /* .opd contains a regular array of 24 byte entries.  We're
4990              only interested in the reloc pointing to a function entry
4991              point.  */
4992           r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
4993           if (r_type == R_PPC64_TOC)
4994             continue;
4995
4996           if (r_type != R_PPC64_ADDR64)
4997             {
4998               (*_bfd_error_handler)
4999                 (_("%s: unexpected reloc type %u in .opd section"),
5000                  bfd_archive_filename (ibfd), r_type);
5001               need_edit = FALSE;
5002               break;
5003             }
5004
5005           if (rel + 1 >= relend)
5006             continue;
5007           r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE ((rel + 1)->r_info);
5008           if (r_type != R_PPC64_TOC)
5009             continue;
5010
5011           if (rel->r_offset != offset)
5012             {
5013               /* If someone messes with .opd alignment then after a
5014                  "ld -r" we might have padding in the middle of .opd.
5015                  Also, there's nothing to prevent someone putting
5016                  something silly in .opd with the assembler.  No .opd
5017                  optimization for them!  */
5018               (*_bfd_error_handler)
5019                 (_("%s: .opd is not a regular array of opd entries"),
5020                  bfd_archive_filename (ibfd));
5021               need_edit = FALSE;
5022               break;
5023             }
5024
5025           r_symndx = ELF64_R_SYM (rel->r_info);
5026           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
5027                           r_symndx, ibfd))
5028             goto error_free_rel;
5029
5030           if (sym_sec == NULL || sym_sec->owner == NULL)
5031             {
5032               const char *sym_name;
5033               if (h != NULL)
5034                 sym_name = h->root.root.string;
5035               else
5036                 sym_name = bfd_elf_local_sym_name (ibfd, sym);
5037
5038               (*_bfd_error_handler)
5039                 (_("%s: undefined sym `%s' in .opd section"),
5040                  bfd_archive_filename (ibfd),
5041                  sym_name);
5042               need_edit = FALSE;
5043               break;
5044             }
5045
5046           /* opd entries are always for functions defined in the
5047              current input bfd.  If the symbol isn't defined in the
5048              input bfd, then we won't be using the function in this
5049              bfd;  It must be defined in a linkonce section in another
5050              bfd, or is weak.  It's also possible that we are
5051              discarding the function due to a linker script /DISCARD/,
5052              which we test for via the output_section.  */
5053           if (sym_sec->owner != ibfd
5054               || sym_sec->output_section == bfd_abs_section_ptr)
5055             need_edit = TRUE;
5056
5057           offset += 24;
5058         }
5059
5060       if (need_edit)
5061         {
5062           Elf_Internal_Rela *write_rel;
5063           bfd_byte *rptr, *wptr;
5064           bfd_boolean skip;
5065
5066           /* This seems a waste of time as input .opd sections are all
5067              zeros as generated by gcc, but I suppose there's no reason
5068              this will always be so.  We might start putting something in
5069              the third word of .opd entries.  */
5070           if ((sec->flags & SEC_IN_MEMORY) == 0)
5071             {
5072               bfd_byte *loc = bfd_alloc (ibfd, sec->_raw_size);
5073               if (loc == NULL
5074                   || !bfd_get_section_contents (ibfd, sec, loc, (bfd_vma) 0,
5075                                                 sec->_raw_size))
5076                 {
5077                   if (local_syms != NULL
5078                       && symtab_hdr->contents != (unsigned char *) local_syms)
5079                     free (local_syms);
5080                 error_free_rel:
5081                   if (elf_section_data (sec)->relocs != relstart)
5082                     free (relstart);
5083                   return FALSE;
5084                 }
5085               sec->contents = loc;
5086               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
5087             }
5088
5089           elf_section_data (sec)->relocs = relstart;
5090
5091           wptr = sec->contents;
5092           rptr = sec->contents;
5093           write_rel = relstart;
5094           skip = FALSE;
5095           offset = 0;
5096           for (rel = relstart; rel < relend; rel++)
5097             {
5098               if (rel->r_offset == offset)
5099                 {
5100                   unsigned long r_symndx;
5101                   asection *sym_sec;
5102                   struct elf_link_hash_entry *h;
5103                   Elf_Internal_Sym *sym;
5104
5105                   r_symndx = ELF64_R_SYM (rel->r_info);
5106                   get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
5107                              r_symndx, ibfd);
5108
5109                   skip = (sym_sec->owner != ibfd
5110                           || sym_sec->output_section == bfd_abs_section_ptr);
5111                   if (skip)
5112                     {
5113                       if (h != NULL && sym_sec->owner == ibfd)
5114                         {
5115                           /* Arrange for the function descriptor sym
5116                              to be dropped.  */
5117                           struct ppc_link_hash_entry *fdh;
5118                           struct ppc_link_hash_entry *fh;
5119
5120                           fh = (struct ppc_link_hash_entry *) h;
5121                           fdh = (struct ppc_link_hash_entry *) fh->oh;
5122                           if (fdh == NULL)
5123                             {
5124                               const char *fd_name;
5125                               struct ppc_link_hash_table *htab;
5126
5127                               fd_name = h->root.root.string + 1;
5128                               htab = ppc_hash_table (info);
5129                               fdh = (struct ppc_link_hash_entry *)
5130                                 elf_link_hash_lookup (&htab->elf, fd_name,
5131                                                       FALSE, FALSE, FALSE);
5132                               fdh->is_func_descriptor = 1;
5133                               fdh->oh = &fh->elf;
5134                               fh->is_func = 1;
5135                               fh->oh = &fdh->elf;
5136                             }
5137
5138                           fdh->elf.root.u.def.value = 0;
5139                           fdh->elf.root.u.def.section = sym_sec;
5140                         }
5141                     }
5142                   else
5143                     {
5144                       /* We'll be keeping this opd entry.  */
5145
5146                       if (h != NULL)
5147                         {
5148                           /* Redefine the function descriptor symbol
5149                              to this location in the opd section.
5150                              We've checked above that opd relocs are
5151                              ordered.  */
5152                           struct ppc_link_hash_entry *fdh;
5153                           struct ppc_link_hash_entry *fh;
5154
5155                           fh = (struct ppc_link_hash_entry *) h;
5156                           fdh = (struct ppc_link_hash_entry *) fh->oh;
5157                           if (fdh == NULL)
5158                             {
5159                               const char *fd_name;
5160                               struct ppc_link_hash_table *htab;
5161
5162                               fd_name = h->root.root.string + 1;
5163                               htab = ppc_hash_table (info);
5164                               fdh = (struct ppc_link_hash_entry *)
5165                                 elf_link_hash_lookup (&htab->elf, fd_name,
5166                                                       FALSE, FALSE, FALSE);
5167                               fdh->is_func_descriptor = 1;
5168                               fdh->oh = &fh->elf;
5169                               fh->is_func = 1;
5170                               fh->oh = &fdh->elf;
5171                             }
5172
5173                           fdh->elf.root.u.def.value = wptr - sec->contents;
5174                         }
5175                       else
5176                         {
5177                           /* Local syms are a bit tricky.  We could
5178                              tweak them as they can be cached, but
5179                              we'd need to look through the local syms
5180                              for the function descriptor sym which we
5181                              don't have at the moment.  So keep an
5182                              array of adjustments.  */
5183                           adjust[rel->r_offset / 24] = wptr - rptr;
5184                         }
5185
5186                       if (wptr != rptr)
5187                         memcpy (wptr, rptr, 24);
5188                       wptr += 24;
5189                     }
5190                   rptr += 24;
5191                   offset += 24;
5192                 }
5193
5194               /* We need to adjust any reloc offsets to point to the
5195                  new opd entries.  While we're at it, we may as well
5196                  remove redundant relocs.  */
5197               if (!skip)
5198                 {
5199                   rel->r_offset += wptr - rptr;
5200                   if (write_rel != rel)
5201                     memcpy (write_rel, rel, sizeof (*rel));
5202                   ++write_rel;
5203                 }
5204             }
5205
5206           sec->_cooked_size = wptr - sec->contents;
5207           sec->reloc_count = write_rel - relstart;
5208           /* Fudge the size too, as this is used later in
5209              elf_bfd_final_link if we are emitting relocs.  */
5210           elf_section_data (sec)->rel_hdr.sh_size
5211             = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
5212           BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
5213         }
5214       else if (elf_section_data (sec)->relocs != relstart)
5215         free (relstart);
5216
5217       if (local_syms != NULL
5218           && symtab_hdr->contents != (unsigned char *) local_syms)
5219         {
5220           if (!info->keep_memory)
5221             free (local_syms);
5222           else
5223             symtab_hdr->contents = (unsigned char *) local_syms;
5224         }
5225     }
5226
5227   return TRUE;
5228 }
5229
5230 /* Set htab->tls_sec.  */
5231
5232 bfd_boolean
5233 ppc64_elf_tls_setup (obfd, info)
5234      bfd *obfd;
5235      struct bfd_link_info *info;
5236 {
5237   asection *tls;
5238   struct ppc_link_hash_table *htab;
5239
5240   for (tls = obfd->sections; tls != NULL; tls = tls->next)
5241     if ((tls->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
5242         == (SEC_THREAD_LOCAL | SEC_LOAD))
5243       break;
5244
5245   htab = ppc_hash_table (info);
5246   htab->tls_sec = tls;
5247
5248   if (htab->tls_get_addr != NULL)
5249     {
5250       struct elf_link_hash_entry *h = htab->tls_get_addr;
5251
5252       while (h->root.type == bfd_link_hash_indirect
5253              || h->root.type == bfd_link_hash_warning)
5254         h = (struct elf_link_hash_entry *) h->root.u.i.link;
5255
5256       htab->tls_get_addr = h;
5257     }
5258
5259   return tls != NULL;
5260 }
5261
5262 /* Run through all the TLS relocs looking for optimization
5263    opportunities.  The linker has been hacked (see ppc64elf.em) to do
5264    a preliminary section layout so that we know the TLS segment
5265    offsets.  We can't optimize earlier because some optimizations need
5266    to know the tp offset, and we need to optimize before allocating
5267    dynamic relocations.  */
5268
5269 bfd_boolean
5270 ppc64_elf_tls_optimize (obfd, info)
5271      bfd *obfd ATTRIBUTE_UNUSED;
5272      struct bfd_link_info *info;
5273 {
5274   bfd *ibfd;
5275   asection *sec;
5276   struct ppc_link_hash_table *htab;
5277
5278   if (info->relocateable || info->shared)
5279     return TRUE;
5280
5281   htab = ppc_hash_table (info);
5282   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5283     {
5284       Elf_Internal_Sym *locsyms = NULL;
5285
5286       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
5287         if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
5288           {
5289             Elf_Internal_Rela *relstart, *rel, *relend;
5290             int expecting_tls_get_addr;
5291
5292             /* Read the relocations.  */
5293             relstart = _bfd_elf_link_read_relocs (ibfd, sec, (PTR) NULL,
5294                                                   (Elf_Internal_Rela *) NULL,
5295                                                   info->keep_memory);
5296             if (relstart == NULL)
5297               return FALSE;
5298
5299             expecting_tls_get_addr = 0;
5300             relend = relstart + sec->reloc_count;
5301             for (rel = relstart; rel < relend; rel++)
5302               {
5303                 enum elf_ppc64_reloc_type r_type;
5304                 unsigned long r_symndx;
5305                 struct elf_link_hash_entry *h;
5306                 Elf_Internal_Sym *sym;
5307                 asection *sym_sec;
5308                 char *tls_mask;
5309                 char tls_set, tls_clear, tls_type = 0;
5310                 bfd_vma value;
5311                 bfd_boolean ok_tprel, is_local;
5312
5313                 r_symndx = ELF64_R_SYM (rel->r_info);
5314                 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
5315                                 r_symndx, ibfd))
5316                   {
5317                   err_free_rel:
5318                     if (elf_section_data (sec)->relocs != relstart)
5319                       free (relstart);
5320                     if (locsyms != NULL
5321                         && (elf_tdata (ibfd)->symtab_hdr.contents
5322                             != (unsigned char *) locsyms))
5323                       free (locsyms);
5324                     return FALSE;
5325                   }
5326
5327                 if (h != NULL)
5328                   {
5329                     if (h->root.type != bfd_link_hash_defined
5330                         && h->root.type != bfd_link_hash_defweak)
5331                       continue;
5332                     value = h->root.u.def.value;
5333                   }
5334                 else
5335                   value = sym->st_value;
5336
5337                 ok_tprel = FALSE;
5338                 is_local = FALSE;
5339                 if (h == NULL
5340                     || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
5341                   {
5342                     is_local = TRUE;
5343                     value += sym_sec->output_offset;
5344                     value += sym_sec->output_section->vma;
5345                     value -= htab->tls_sec->vma;
5346                     ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
5347                                 < (bfd_vma) 1 << 32);
5348                   }
5349
5350                 r_type
5351                   = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
5352                 switch (r_type)
5353                   {
5354                   case R_PPC64_GOT_TLSLD16:
5355                   case R_PPC64_GOT_TLSLD16_LO:
5356                   case R_PPC64_GOT_TLSLD16_HI:
5357                   case R_PPC64_GOT_TLSLD16_HA:
5358                     /* These relocs should never be against a symbol
5359                        defined in a shared lib.  Leave them alone if
5360                        that turns out to be the case.  */
5361                     htab->tlsld_got.refcount -= 1;
5362                     if (!is_local)
5363                       continue;
5364
5365                     /* LD -> LE */
5366                     tls_set = 0;
5367                     tls_clear = TLS_LD;
5368                     tls_type = TLS_TLS | TLS_LD;
5369                     expecting_tls_get_addr = 1;
5370                     break;
5371
5372                   case R_PPC64_GOT_TLSGD16:
5373                   case R_PPC64_GOT_TLSGD16_LO:
5374                   case R_PPC64_GOT_TLSGD16_HI:
5375                   case R_PPC64_GOT_TLSGD16_HA:
5376                     if (ok_tprel)
5377                       /* GD -> LE */
5378                       tls_set = 0;
5379                     else
5380                       /* GD -> IE */
5381                       tls_set = TLS_TLS | TLS_TPRELGD;
5382                     tls_clear = TLS_GD;
5383                     tls_type = TLS_TLS | TLS_GD;
5384                     expecting_tls_get_addr = 1;
5385                     break;
5386
5387                   case R_PPC64_GOT_TPREL16_DS:
5388                   case R_PPC64_GOT_TPREL16_LO_DS:
5389                   case R_PPC64_GOT_TPREL16_HI:
5390                   case R_PPC64_GOT_TPREL16_HA:
5391                     expecting_tls_get_addr = 0;
5392                     if (ok_tprel)
5393                       {
5394                         /* IE -> LE */
5395                         tls_set = 0;
5396                         tls_clear = TLS_TPREL;
5397                         tls_type = TLS_TLS | TLS_TPREL;
5398                         break;
5399                       }
5400                     else
5401                       continue;
5402
5403                   case R_PPC64_REL14:
5404                   case R_PPC64_REL14_BRTAKEN:
5405                   case R_PPC64_REL14_BRNTAKEN:
5406                   case R_PPC64_REL24:
5407                     if (h != NULL
5408                         && h == htab->tls_get_addr)
5409                       {
5410                         if (!expecting_tls_get_addr
5411                             && rel != relstart
5412                             && ((ELF64_R_TYPE (rel[-1].r_info)
5413                                  == R_PPC64_TOC16)
5414                                 || (ELF64_R_TYPE (rel[-1].r_info)
5415                                     == R_PPC64_TOC16_LO)))
5416                           {
5417                             /* Check for toc tls entries.  */
5418                             char *toc_tls;
5419                             int retval;
5420
5421                             retval = get_tls_mask (&toc_tls, &locsyms,
5422                                                    rel - 1, ibfd);
5423                             if (retval == 0)
5424                               goto err_free_rel;
5425                             if (toc_tls != NULL)
5426                               expecting_tls_get_addr = retval > 1;
5427                           }
5428
5429                         if (expecting_tls_get_addr)
5430                           {
5431                             struct plt_entry *ent;
5432                             for (ent = h->plt.plist; ent; ent = ent->next)
5433                               if (ent->addend == 0)
5434                                 {
5435                                   if (ent->plt.refcount > 0)
5436                                     ent->plt.refcount -= 1;
5437                                   break;
5438                                 }
5439                           }
5440                       }
5441                     expecting_tls_get_addr = 0;
5442                     continue;
5443
5444                   case R_PPC64_TPREL64:
5445                     expecting_tls_get_addr = 0;
5446                     if (ok_tprel)
5447                       {
5448                         /* IE -> LE */
5449                         tls_set = TLS_EXPLICIT;
5450                         tls_clear = TLS_TPREL;
5451                         break;
5452                       }
5453                     else
5454                       continue;
5455
5456                   case R_PPC64_DTPMOD64:
5457                     expecting_tls_get_addr = 0;
5458                     if (rel + 1 < relend
5459                         && (rel[1].r_info
5460                             == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
5461                         && rel[1].r_offset == rel->r_offset + 8)
5462                       {
5463                         if (ok_tprel)
5464                           /* GD -> LE */
5465                           tls_set = TLS_EXPLICIT | TLS_GD;
5466                         else
5467                           /* GD -> IE */
5468                           tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
5469                         tls_clear = TLS_GD;
5470                       }
5471                     else
5472                       {
5473                         if (!is_local)
5474                           continue;
5475
5476                         /* LD -> LE */
5477                         tls_set = TLS_EXPLICIT;
5478                         tls_clear = TLS_LD;
5479                       }
5480                     break;
5481
5482                   default:
5483                     expecting_tls_get_addr = 0;
5484                     continue;
5485                   }
5486
5487                 if ((tls_set & TLS_EXPLICIT) == 0)
5488                   {
5489                     struct got_entry *ent;
5490
5491                     /* Adjust got entry for this reloc.  */
5492                     if (h != NULL)
5493                       ent = h->got.glist;
5494                     else
5495                       ent = elf_local_got_ents (ibfd)[r_symndx];
5496
5497                     for (; ent != NULL; ent = ent->next)
5498                       if (ent->addend == rel->r_addend
5499                           && ent->tls_type == tls_type)
5500                         break;
5501                     if (ent == NULL)
5502                       abort ();
5503
5504                     if (tls_set == 0)
5505                       {
5506                         /* We managed to get rid of a got entry.  */
5507                         if (ent->got.refcount > 0)
5508                           ent->got.refcount -= 1;
5509                       }
5510                   }
5511                 else if (h != NULL)
5512                   {
5513                     struct ppc_link_hash_entry * eh;
5514                     struct ppc_dyn_relocs **pp;
5515                     struct ppc_dyn_relocs *p;
5516
5517                     /* Adjust dynamic relocs.  */
5518                     eh = (struct ppc_link_hash_entry *) h;
5519                     for (pp = &eh->dyn_relocs;
5520                          (p = *pp) != NULL;
5521                          pp = &p->next)
5522                       if (p->sec == sec)
5523                         {
5524                           /* If we got rid of a DTPMOD/DTPREL reloc
5525                              pair then we'll lose one or two dyn
5526                              relocs.  */
5527                           if (tls_set == (TLS_EXPLICIT | TLS_GD))
5528                             p->count -= 1;
5529                           p->count -= 1;
5530                           if (p->count == 0)
5531                             *pp = p->next;
5532                           break;
5533                         }
5534                   }
5535
5536                 *tls_mask |= tls_set;
5537                 *tls_mask &= ~tls_clear;
5538               }
5539
5540             if (elf_section_data (sec)->relocs != relstart)
5541               free (relstart);
5542           }
5543
5544       if (locsyms != NULL
5545           && (elf_tdata (ibfd)->symtab_hdr.contents
5546               != (unsigned char *) locsyms))
5547         {
5548           if (!info->keep_memory)
5549             free (locsyms);
5550           else
5551             elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
5552         }
5553     }
5554   return TRUE;
5555 }
5556
5557 /* This is the condition under which ppc64_elf_finish_dynamic_symbol
5558    will be called from elflink.h.  If elflink.h doesn't call our
5559    finish_dynamic_symbol routine, we'll need to do something about
5560    initializing any .plt and .got entries in ppc64_elf_relocate_section.  */
5561 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
5562   ((DYN)                                                                \
5563    && ((SHARED)                                                         \
5564        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)      \
5565    && ((H)->dynindx != -1                                               \
5566        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
5567
5568 /* Allocate space in .plt, .got and associated reloc sections for
5569    dynamic relocs.  */
5570
5571 static bfd_boolean
5572 allocate_dynrelocs (h, inf)
5573      struct elf_link_hash_entry *h;
5574      PTR inf;
5575 {
5576   struct bfd_link_info *info;
5577   struct ppc_link_hash_table *htab;
5578   asection *s;
5579   struct ppc_link_hash_entry *eh;
5580   struct ppc_dyn_relocs *p;
5581   struct got_entry *gent;
5582
5583   if (h->root.type == bfd_link_hash_indirect)
5584     return TRUE;
5585
5586   if (h->root.type == bfd_link_hash_warning)
5587     h = (struct elf_link_hash_entry *) h->root.u.i.link;
5588
5589   info = (struct bfd_link_info *) inf;
5590   htab = ppc_hash_table (info);
5591
5592   if (htab->elf.dynamic_sections_created
5593       && h->dynindx != -1
5594       && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
5595     {
5596       struct plt_entry *pent;
5597       bfd_boolean doneone = FALSE;
5598       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
5599         if (pent->plt.refcount > 0)
5600           {
5601             BFD_ASSERT (((struct ppc_link_hash_entry *) h)->is_func_descriptor);
5602
5603             /* If this is the first .plt entry, make room for the special
5604                first entry.  */
5605             s = htab->splt;
5606             if (s->_raw_size == 0)
5607               s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
5608
5609             pent->plt.offset = s->_raw_size;
5610
5611             /* Make room for this entry.  */
5612             s->_raw_size += PLT_ENTRY_SIZE;
5613
5614             /* Make room for the .glink code.  */
5615             s = htab->sglink;
5616             if (s->_raw_size == 0)
5617               s->_raw_size += GLINK_CALL_STUB_SIZE;
5618             /* We need bigger stubs past index 32767.  */
5619             if (s->_raw_size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
5620               s->_raw_size += 4;
5621             s->_raw_size += 2*4;
5622
5623             /* We also need to make an entry in the .rela.plt section.  */
5624             s = htab->srelplt;
5625             s->_raw_size += sizeof (Elf64_External_Rela);
5626             doneone = TRUE;
5627           }
5628         else
5629           pent->plt.offset = (bfd_vma) -1;
5630       if (!doneone)
5631         {
5632           h->plt.plist = NULL;
5633           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
5634         }
5635     }
5636   else
5637     {
5638       h->plt.plist = NULL;
5639       h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
5640     }
5641
5642   eh = (struct ppc_link_hash_entry *) h;
5643   /* Run through the TLS GD got entries first if we're changing them
5644      to TPREL.  */
5645   if ((eh->tls_mask & TLS_TPRELGD) != 0)
5646     for (gent = h->got.glist; gent != NULL; gent = gent->next)
5647       if (gent->got.refcount > 0
5648           && (gent->tls_type & TLS_GD) != 0)
5649         {
5650           /* This was a GD entry that has been converted to TPREL.  If
5651              there happens to be a TPREL entry we can use that one.  */
5652           struct got_entry *ent;
5653           for (ent = h->got.glist; ent != NULL; ent = ent->next)
5654             if (ent->got.refcount > 0
5655                 && (ent->tls_type & TLS_TPREL) != 0
5656                 && ent->addend == gent->addend)
5657               {
5658                 gent->got.refcount = 0;
5659                 break;
5660               }
5661
5662           /* If not, then we'll be using our own TPREL entry.  */
5663           if (gent->got.refcount != 0)
5664             gent->tls_type = TLS_TLS | TLS_TPREL;
5665         }
5666
5667   for (gent = h->got.glist; gent != NULL; gent = gent->next)
5668     if (gent->got.refcount > 0)
5669       {
5670         bfd_boolean dyn;
5671
5672         /* Make sure this symbol is output as a dynamic symbol.
5673            Undefined weak syms won't yet be marked as dynamic,
5674            nor will all TLS symbols.  */
5675         if (h->dynindx == -1
5676             && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5677           {
5678             if (! bfd_elf64_link_record_dynamic_symbol (info, h))
5679               return FALSE;
5680           }
5681
5682         if ((gent->tls_type & TLS_LD) != 0
5683             && !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
5684           {
5685             gent->got.offset = htab->tlsld_got.offset;
5686             continue;
5687           }
5688
5689         s = htab->sgot;
5690         gent->got.offset = s->_raw_size;
5691         s->_raw_size
5692           += (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)) ? 16 : 8;
5693         dyn = htab->elf.dynamic_sections_created;
5694         if ((info->shared
5695              || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
5696             && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5697                 || h->root.type != bfd_link_hash_undefweak))
5698           htab->srelgot->_raw_size
5699             += (gent->tls_type & eh->tls_mask & TLS_GD
5700                 ? 2 * sizeof (Elf64_External_Rela)
5701                 : sizeof (Elf64_External_Rela));
5702       }
5703     else
5704       gent->got.offset = (bfd_vma) -1;
5705
5706   if (eh->dyn_relocs == NULL)
5707     return TRUE;
5708
5709   /* In the shared -Bsymbolic case, discard space allocated for
5710      dynamic pc-relative relocs against symbols which turn out to be
5711      defined in regular objects.  For the normal shared case, discard
5712      space for relocs that have become local due to symbol visibility
5713      changes.  */
5714
5715   if (info->shared)
5716     {
5717       /* Relocs that use pc_count are those that appear on a call insn,
5718          or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
5719          generated via assembly.  We want calls to protected symbols to
5720          resolve directly to the function rather than going via the plt.
5721          If people want function pointer comparisons to work as expected
5722          then they should avoid writing weird assembly.  */
5723       if (SYMBOL_CALLS_LOCAL (info, h))
5724         {
5725           struct ppc_dyn_relocs **pp;
5726
5727           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5728             {
5729               p->count -= p->pc_count;
5730               p->pc_count = 0;
5731               if (p->count == 0)
5732                 *pp = p->next;
5733               else
5734                 pp = &p->next;
5735             }
5736         }
5737
5738       /* Also discard relocs on undefined weak syms with non-default
5739          visibility.  */
5740       if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5741           && h->root.type == bfd_link_hash_undefweak)
5742         eh->dyn_relocs = NULL;
5743     }
5744   else if (ELIMINATE_COPY_RELOCS)
5745     {
5746       /* For the non-shared case, discard space for relocs against
5747          symbols which turn out to need copy relocs or are not
5748          dynamic.  */
5749
5750       if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5751           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5752           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5753         {
5754           /* Make sure this symbol is output as a dynamic symbol.
5755              Undefined weak syms won't yet be marked as dynamic.  */
5756           if (h->dynindx == -1
5757               && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5758             {
5759               if (! bfd_elf64_link_record_dynamic_symbol (info, h))
5760                 return FALSE;
5761             }
5762
5763           /* If that succeeded, we know we'll be keeping all the
5764              relocs.  */
5765           if (h->dynindx != -1)
5766             goto keep;
5767         }
5768
5769       eh->dyn_relocs = NULL;
5770
5771     keep: ;
5772     }
5773
5774   /* Finally, allocate space.  */
5775   for (p = eh->dyn_relocs; p != NULL; p = p->next)
5776     {
5777       asection *sreloc = elf_section_data (p->sec)->sreloc;
5778       sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
5779     }
5780
5781   return TRUE;
5782 }
5783
5784 /* Find any dynamic relocs that apply to read-only sections.  */
5785
5786 static bfd_boolean
5787 readonly_dynrelocs (h, inf)
5788      struct elf_link_hash_entry *h;
5789      PTR inf;
5790 {
5791   struct ppc_link_hash_entry *eh;
5792   struct ppc_dyn_relocs *p;
5793
5794   if (h->root.type == bfd_link_hash_warning)
5795     h = (struct elf_link_hash_entry *) h->root.u.i.link;
5796
5797   eh = (struct ppc_link_hash_entry *) h;
5798   for (p = eh->dyn_relocs; p != NULL; p = p->next)
5799     {
5800       asection *s = p->sec->output_section;
5801
5802       if (s != NULL && (s->flags & SEC_READONLY) != 0)
5803         {
5804           struct bfd_link_info *info = (struct bfd_link_info *) inf;
5805
5806           info->flags |= DF_TEXTREL;
5807
5808           /* Not an error, just cut short the traversal.  */
5809           return FALSE;
5810         }
5811     }
5812   return TRUE;
5813 }
5814
5815 /* Set the sizes of the dynamic sections.  */
5816
5817 static bfd_boolean
5818 ppc64_elf_size_dynamic_sections (output_bfd, info)
5819      bfd *output_bfd ATTRIBUTE_UNUSED;
5820      struct bfd_link_info *info;
5821 {
5822   struct ppc_link_hash_table *htab;
5823   bfd *dynobj;
5824   asection *s;
5825   bfd_boolean relocs;
5826   bfd *ibfd;
5827
5828   htab = ppc_hash_table (info);
5829   dynobj = htab->elf.dynobj;
5830   if (dynobj == NULL)
5831     abort ();
5832
5833   if (htab->elf.dynamic_sections_created)
5834     {
5835       /* Set the contents of the .interp section to the interpreter.  */
5836       if (info->executable)
5837         {
5838           s = bfd_get_section_by_name (dynobj, ".interp");
5839           if (s == NULL)
5840             abort ();
5841           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
5842           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5843         }
5844     }
5845
5846   if (htab->tlsld_got.refcount > 0)
5847     {
5848       htab->tlsld_got.offset = htab->sgot->_raw_size;
5849       htab->sgot->_raw_size += 16;
5850       if (info->shared)
5851         htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
5852     }
5853   else
5854     htab->tlsld_got.offset = (bfd_vma) -1;
5855
5856   /* Set up .got offsets for local syms, and space for local dynamic
5857      relocs.  */
5858   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5859     {
5860       struct got_entry **lgot_ents;
5861       struct got_entry **end_lgot_ents;
5862       char *lgot_masks;
5863       bfd_size_type locsymcount;
5864       Elf_Internal_Shdr *symtab_hdr;
5865       asection *srel;
5866
5867       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
5868         continue;
5869
5870       for (s = ibfd->sections; s != NULL; s = s->next)
5871         {
5872           struct ppc_dyn_relocs *p;
5873
5874           for (p = *((struct ppc_dyn_relocs **)
5875                      &elf_section_data (s)->local_dynrel);
5876                p != NULL;
5877                p = p->next)
5878             {
5879               if (!bfd_is_abs_section (p->sec)
5880                   && bfd_is_abs_section (p->sec->output_section))
5881                 {
5882                   /* Input section has been discarded, either because
5883                      it is a copy of a linkonce section or due to
5884                      linker script /DISCARD/, so we'll be discarding
5885                      the relocs too.  */
5886                 }
5887               else if (p->count != 0)
5888                 {
5889                   srel = elf_section_data (p->sec)->sreloc;
5890                   srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
5891                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
5892                     info->flags |= DF_TEXTREL;
5893                 }
5894             }
5895         }
5896
5897       lgot_ents = elf_local_got_ents (ibfd);
5898       if (!lgot_ents)
5899         continue;
5900
5901       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5902       locsymcount = symtab_hdr->sh_info;
5903       end_lgot_ents = lgot_ents + locsymcount;
5904       lgot_masks = (char *) end_lgot_ents;
5905       s = htab->sgot;
5906       srel = htab->srelgot;
5907       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
5908         {
5909           struct got_entry *ent;
5910
5911           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
5912             if (ent->got.refcount > 0)
5913               {
5914                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
5915                   {
5916                     if (htab->tlsld_got.offset == (bfd_vma) -1)
5917                       {
5918                         htab->tlsld_got.offset = s->_raw_size;
5919                         s->_raw_size += 16;
5920                         if (info->shared)
5921                           srel->_raw_size += sizeof (Elf64_External_Rela);
5922                       }
5923                     ent->got.offset = htab->tlsld_got.offset;
5924                   }
5925                 else
5926                   {
5927                     ent->got.offset = s->_raw_size;
5928                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
5929                       {
5930                         s->_raw_size += 16;
5931                         if (info->shared)
5932                           srel->_raw_size += 2 * sizeof (Elf64_External_Rela);
5933                       }
5934                     else
5935                       {
5936                         s->_raw_size += 8;
5937                         if (info->shared)
5938                           srel->_raw_size += sizeof (Elf64_External_Rela);
5939                       }
5940                   }
5941               }
5942             else
5943               ent->got.offset = (bfd_vma) -1;
5944         }
5945     }
5946
5947   /* Allocate global sym .plt and .got entries, and space for global
5948      sym dynamic relocs.  */
5949   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
5950
5951   /* We now have determined the sizes of the various dynamic sections.
5952      Allocate memory for them.  */
5953   relocs = FALSE;
5954   for (s = dynobj->sections; s != NULL; s = s->next)
5955     {
5956       if ((s->flags & SEC_LINKER_CREATED) == 0)
5957         continue;
5958
5959       /* Reset _cooked_size since prelim layout will set it wrongly,
5960          and a non-zero _cooked_size sticks.  */
5961       s->_cooked_size = 0;
5962
5963       if (s == htab->sbrlt || s == htab->srelbrlt)
5964         /* These haven't been allocated yet;  don't strip.  */
5965         continue;
5966       else if (s == htab->sgot)
5967         {
5968           /* Automatic multiple tocs aren't possible if we are using the
5969              GOT.  The GOT is accessed via r2, so we can't adjust r2.
5970              FIXME: There's no reason why we couldn't lay out multiple
5971              GOTs too.  */
5972           if (s->_raw_size > elf_backend_got_header_size)
5973             htab->no_multi_toc = 1;
5974         }
5975       else if (s == htab->splt
5976                || s == htab->sglink)
5977         {
5978           /* Strip this section if we don't need it; see the
5979              comment below.  */
5980         }
5981       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
5982         {
5983           if (s->_raw_size == 0)
5984             {
5985               /* If we don't need this section, strip it from the
5986                  output file.  This is mostly to handle .rela.bss and
5987                  .rela.plt.  We must create both sections in
5988                  create_dynamic_sections, because they must be created
5989                  before the linker maps input sections to output
5990                  sections.  The linker does that before
5991                  adjust_dynamic_symbol is called, and it is that
5992                  function which decides whether anything needs to go
5993                  into these sections.  */
5994             }
5995           else
5996             {
5997               if (s != htab->srelplt)
5998                 relocs = TRUE;
5999
6000               /* We use the reloc_count field as a counter if we need
6001                  to copy relocs into the output file.  */
6002               s->reloc_count = 0;
6003             }
6004         }
6005       else
6006         {
6007           /* It's not one of our sections, so don't allocate space.  */
6008           continue;
6009         }
6010
6011       if (s->_raw_size == 0)
6012         {
6013           _bfd_strip_section_from_output (info, s);
6014           continue;
6015         }
6016
6017       /* .plt is in the bss section.  We don't initialise it.  */
6018       if ((s->flags & SEC_LOAD) == 0)
6019         continue;
6020
6021       /* Allocate memory for the section contents.  We use bfd_zalloc
6022          here in case unused entries are not reclaimed before the
6023          section's contents are written out.  This should not happen,
6024          but this way if it does we get a R_PPC64_NONE reloc in .rela
6025          sections instead of garbage.
6026          We also rely on the section contents being zero when writing
6027          the GOT.  */
6028       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
6029       if (s->contents == NULL)
6030         return FALSE;
6031     }
6032
6033   if (htab->elf.dynamic_sections_created)
6034     {
6035       /* Add some entries to the .dynamic section.  We fill in the
6036          values later, in ppc64_elf_finish_dynamic_sections, but we
6037          must add the entries now so that we get the correct size for
6038          the .dynamic section.  The DT_DEBUG entry is filled in by the
6039          dynamic linker and used by the debugger.  */
6040 #define add_dynamic_entry(TAG, VAL) \
6041   bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
6042
6043       if (info->executable)
6044         {
6045           if (!add_dynamic_entry (DT_DEBUG, 0))
6046             return FALSE;
6047         }
6048
6049       if (htab->splt != NULL && htab->splt->_raw_size != 0)
6050         {
6051           if (!add_dynamic_entry (DT_PLTGOT, 0)
6052               || !add_dynamic_entry (DT_PLTRELSZ, 0)
6053               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
6054               || !add_dynamic_entry (DT_JMPREL, 0)
6055               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
6056             return FALSE;
6057         }
6058
6059       if (NO_OPD_RELOCS)
6060         {
6061           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
6062               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
6063             return FALSE;
6064         }
6065
6066       if (relocs)
6067         {
6068           if (!add_dynamic_entry (DT_RELA, 0)
6069               || !add_dynamic_entry (DT_RELASZ, 0)
6070               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
6071             return FALSE;
6072
6073           /* If any dynamic relocs apply to a read-only section,
6074              then we need a DT_TEXTREL entry.  */
6075           if ((info->flags & DF_TEXTREL) == 0)
6076             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
6077                                     (PTR) info);
6078
6079           if ((info->flags & DF_TEXTREL) != 0)
6080             {
6081               if (!add_dynamic_entry (DT_TEXTREL, 0))
6082                 return FALSE;
6083             }
6084         }
6085     }
6086 #undef add_dynamic_entry
6087
6088   return TRUE;
6089 }
6090
6091 /* Determine the type of stub needed, if any, for a call.  */
6092
6093 static INLINE enum ppc_stub_type
6094 ppc_type_of_stub (input_sec, rel, hash, destination)
6095      asection *input_sec;
6096      const Elf_Internal_Rela *rel;
6097      struct ppc_link_hash_entry **hash;
6098      bfd_vma destination;
6099 {
6100   struct ppc_link_hash_entry *h = *hash;
6101   bfd_vma location;
6102   bfd_vma branch_offset;
6103   bfd_vma max_branch_offset;
6104   unsigned int r_type;
6105
6106   if (h != NULL)
6107     {
6108       if (h->oh != NULL
6109           && h->oh->dynindx != -1)
6110         {
6111           struct plt_entry *ent;
6112           for (ent = h->oh->plt.plist; ent != NULL; ent = ent->next)
6113             if (ent->addend == rel->r_addend
6114                 && ent->plt.offset != (bfd_vma) -1)
6115               {
6116                 *hash = (struct ppc_link_hash_entry *) h->oh;
6117                 return ppc_stub_plt_call;
6118               }
6119         }
6120
6121       if (h->elf.root.type != bfd_link_hash_defined
6122           && h->elf.root.type != bfd_link_hash_defweak)
6123         return ppc_stub_none;
6124     }
6125
6126   /* Determine where the call point is.  */
6127   location = (input_sec->output_offset
6128               + input_sec->output_section->vma
6129               + rel->r_offset);
6130
6131   branch_offset = destination - location;
6132   r_type = ELF64_R_TYPE (rel->r_info);
6133
6134   /* Determine if a long branch stub is needed.  */
6135   max_branch_offset = 1 << 25;
6136   if (r_type != (unsigned int) R_PPC64_REL24)
6137     max_branch_offset = 1 << 15;
6138
6139   if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
6140     /* We need a stub.  Figure out whether a long_branch or plt_branch
6141        is needed later.  */
6142     return ppc_stub_long_branch;
6143
6144   return ppc_stub_none;
6145 }
6146
6147 /* Build a .plt call stub.  */
6148
6149 static bfd_byte *
6150 build_plt_stub (obfd, p, offset)
6151      bfd *obfd;
6152      bfd_byte *p;
6153      int offset;
6154 {
6155 #define PPC_LO(v) ((v) & 0xffff)
6156 #define PPC_HI(v) (((v) >> 16) & 0xffff)
6157 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
6158
6159   bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
6160   bfd_put_32 (obfd, STD_R2_40R1, p),                    p += 4;
6161   bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),  p += 4;
6162   if (PPC_HA (offset + 8) != PPC_HA (offset))
6163     bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p),            p += 4;
6164   offset += 8;
6165   bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p),   p += 4;
6166   if (PPC_HA (offset + 8) != PPC_HA (offset))
6167     bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p),            p += 4;
6168   offset += 8;
6169   bfd_put_32 (obfd, MTCTR_R11, p),                      p += 4;
6170   bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),  p += 4;
6171   bfd_put_32 (obfd, BCTR, p),                           p += 4;
6172   return p;
6173 }
6174
6175 static bfd_boolean
6176 ppc_build_one_stub (gen_entry, in_arg)
6177      struct bfd_hash_entry *gen_entry;
6178      PTR in_arg;
6179 {
6180   struct ppc_stub_hash_entry *stub_entry;
6181   struct ppc_branch_hash_entry *br_entry;
6182   struct bfd_link_info *info;
6183   struct ppc_link_hash_table *htab;
6184   asection *stub_sec;
6185   bfd *stub_bfd;
6186   bfd_byte *loc;
6187   bfd_byte *p;
6188   unsigned int indx;
6189   struct plt_entry *ent;
6190   bfd_vma off;
6191   int size;
6192
6193   /* Massage our args to the form they really have.  */
6194   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
6195   info = (struct bfd_link_info *) in_arg;
6196
6197   htab = ppc_hash_table (info);
6198   stub_sec = stub_entry->stub_sec;
6199
6200   /* Make a note of the offset within the stubs for this entry.  */
6201   stub_entry->stub_offset = stub_sec->_cooked_size;
6202   loc = stub_sec->contents + stub_entry->stub_offset;
6203
6204   if (htab->emit_stub_syms)
6205     {
6206       struct elf_link_hash_entry *h;
6207       h = elf_link_hash_lookup (&htab->elf, stub_entry->root.string,
6208                                 TRUE, FALSE, FALSE);
6209       if (h == NULL)
6210         return FALSE;
6211       h->root.type = bfd_link_hash_defined;
6212       h->root.u.def.section = stub_entry->stub_sec;
6213       h->root.u.def.value = stub_entry->stub_offset;
6214       h->elf_link_hash_flags = (ELF_LINK_HASH_REF_REGULAR
6215                                 | ELF_LINK_HASH_DEF_REGULAR
6216                                 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
6217                                 | ELF_LINK_FORCED_LOCAL);
6218     }
6219
6220   stub_bfd = stub_sec->owner;
6221
6222   switch (stub_entry->stub_type)
6223     {
6224     case ppc_stub_long_branch:
6225     case ppc_stub_long_branch_r2off:
6226       /* Branches are relative.  This is where we are going to.  */
6227       off = (stub_entry->target_value
6228              + stub_entry->target_section->output_offset
6229              + stub_entry->target_section->output_section->vma);
6230
6231       /* And this is where we are coming from.  */
6232       off -= (stub_entry->stub_offset
6233               + stub_sec->output_offset
6234               + stub_sec->output_section->vma);
6235
6236       if (stub_entry->stub_type != ppc_stub_long_branch_r2off)
6237         size = 4;
6238       else
6239         {
6240           bfd_vma r2off;
6241
6242           r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
6243                    - htab->stub_group[stub_entry->id_sec->id].toc_off);
6244           bfd_put_32 (stub_bfd, (bfd_vma) STD_R2_40R1, loc);
6245           loc += 4;
6246           bfd_put_32 (stub_bfd, (bfd_vma) ADDIS_R2_R2 | PPC_HA (r2off), loc);
6247           loc += 4;
6248           bfd_put_32 (stub_bfd, (bfd_vma) ADDI_R2_R2 | PPC_LO (r2off), loc);
6249           loc += 4;
6250           off -= 12;
6251           size = 16;
6252         }
6253       bfd_put_32 (stub_bfd, (bfd_vma) B_DOT | (off & 0x3fffffc), loc);
6254
6255       BFD_ASSERT (off + (1 << 25) < (bfd_vma) (1 << 26));
6256       break;
6257
6258     case ppc_stub_plt_branch:
6259     case ppc_stub_plt_branch_r2off:
6260       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
6261                                          stub_entry->root.string + 9,
6262                                          FALSE, FALSE);
6263       if (br_entry == NULL)
6264         {
6265           (*_bfd_error_handler) (_("can't find branch stub `%s'"),
6266                                  stub_entry->root.string + 9);
6267           htab->stub_error = TRUE;
6268           return FALSE;
6269         }
6270
6271       off = (stub_entry->target_value
6272              + stub_entry->target_section->output_offset
6273              + stub_entry->target_section->output_section->vma);
6274
6275       bfd_put_64 (htab->sbrlt->owner, off,
6276                   htab->sbrlt->contents + br_entry->offset);
6277
6278       if (info->shared)
6279         {
6280           /* Create a reloc for the branch lookup table entry.  */
6281           Elf_Internal_Rela rela;
6282           bfd_byte *rl;
6283
6284           rela.r_offset = (br_entry->offset
6285                            + htab->sbrlt->output_offset
6286                            + htab->sbrlt->output_section->vma);
6287           rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
6288           rela.r_addend = off;
6289
6290           rl = htab->srelbrlt->contents;
6291           rl += htab->srelbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
6292           bfd_elf64_swap_reloca_out (htab->srelbrlt->owner, &rela, rl);
6293         }
6294
6295       off = (br_entry->offset
6296              + htab->sbrlt->output_offset
6297              + htab->sbrlt->output_section->vma
6298              - elf_gp (htab->sbrlt->output_section->owner)
6299              - htab->stub_group[stub_entry->id_sec->id].toc_off);
6300
6301       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6302         {
6303           (*_bfd_error_handler)
6304             (_("linkage table error against `%s'"),
6305              stub_entry->root.string);
6306           bfd_set_error (bfd_error_bad_value);
6307           htab->stub_error = TRUE;
6308           return FALSE;
6309         }
6310
6311       indx = off;
6312       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
6313         {
6314           bfd_put_32 (stub_bfd, (bfd_vma) ADDIS_R12_R2 | PPC_HA (indx), loc);
6315           loc += 4;
6316           bfd_put_32 (stub_bfd, (bfd_vma) LD_R11_0R12 | PPC_LO (indx), loc);
6317           size = 16;
6318         }
6319       else
6320         {
6321           bfd_vma r2off;
6322
6323           r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
6324                    - htab->stub_group[stub_entry->id_sec->id].toc_off);
6325           bfd_put_32 (stub_bfd, (bfd_vma) STD_R2_40R1, loc);
6326           loc += 4;
6327           bfd_put_32 (stub_bfd, (bfd_vma) ADDIS_R12_R2 | PPC_HA (indx), loc);
6328           loc += 4;
6329           bfd_put_32 (stub_bfd, (bfd_vma) LD_R11_0R12 | PPC_LO (indx), loc);
6330           loc += 4;
6331           bfd_put_32 (stub_bfd, (bfd_vma) ADDIS_R2_R2 | PPC_HA (r2off), loc);
6332           loc += 4;
6333           bfd_put_32 (stub_bfd, (bfd_vma) ADDI_R2_R2 | PPC_LO (r2off), loc);
6334           size = 28;
6335         }
6336       loc += 4;
6337       bfd_put_32 (stub_bfd, (bfd_vma) MTCTR_R11, loc);
6338       loc += 4;
6339       bfd_put_32 (stub_bfd, (bfd_vma) BCTR, loc);
6340       break;
6341
6342     case ppc_stub_plt_call:
6343       /* Do the best we can for shared libraries built without
6344          exporting ".foo" for each "foo".  This can happen when symbol
6345          versioning scripts strip all bar a subset of symbols.  */
6346       if (stub_entry->h->oh->root.type != bfd_link_hash_defined
6347           && stub_entry->h->oh->root.type != bfd_link_hash_defweak)
6348         {
6349           /* Point the symbol at the stub.  There may be multiple stubs,
6350              we don't really care;  The main thing is to make this sym
6351              defined somewhere.  */
6352           stub_entry->h->oh->root.type = bfd_link_hash_defined;
6353           stub_entry->h->oh->root.u.def.section = stub_entry->stub_sec;
6354           stub_entry->h->oh->root.u.def.value = stub_entry->stub_offset;
6355         }
6356
6357       /* Now build the stub.  */
6358       off = (bfd_vma) -1;
6359       for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
6360         if (ent->addend == stub_entry->addend)
6361           {
6362             off = ent->plt.offset;
6363             break;
6364           }
6365       if (off >= (bfd_vma) -2)
6366         abort ();
6367
6368       off &= ~ (bfd_vma) 1;
6369       off += (htab->splt->output_offset
6370               + htab->splt->output_section->vma
6371               - elf_gp (htab->splt->output_section->owner)
6372               - htab->stub_group[stub_entry->id_sec->id].toc_off);
6373
6374       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6375         {
6376           (*_bfd_error_handler)
6377             (_("linkage table error against `%s'"),
6378              stub_entry->h->elf.root.root.string);
6379           bfd_set_error (bfd_error_bad_value);
6380           htab->stub_error = TRUE;
6381           return FALSE;
6382         }
6383
6384       p = build_plt_stub (stub_bfd, loc, (int) off);
6385       size = p - loc;
6386       break;
6387
6388     default:
6389       BFD_FAIL ();
6390       return FALSE;
6391     }
6392
6393   stub_sec->_cooked_size += size;
6394   return TRUE;
6395 }
6396
6397 /* As above, but don't actually build the stub.  Just bump offset so
6398    we know stub section sizes, and select plt_branch stubs where
6399    long_branch stubs won't do.  */
6400
6401 static bfd_boolean
6402 ppc_size_one_stub (gen_entry, in_arg)
6403      struct bfd_hash_entry *gen_entry;
6404      PTR in_arg;
6405 {
6406   struct ppc_stub_hash_entry *stub_entry;
6407   struct ppc_link_hash_table *htab;
6408   bfd_vma off;
6409   int size;
6410
6411   /* Massage our args to the form they really have.  */
6412   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
6413   htab = (struct ppc_link_hash_table *) in_arg;
6414
6415   if (stub_entry->stub_type == ppc_stub_plt_call)
6416     {
6417       struct plt_entry *ent;
6418       off = (bfd_vma) -1;
6419       for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
6420         if (ent->addend == stub_entry->addend)
6421           {
6422             off = ent->plt.offset & ~(bfd_vma) 1;
6423             break;
6424           }
6425       if (off >= (bfd_vma) -2)
6426         abort ();
6427       off += (htab->splt->output_offset
6428               + htab->splt->output_section->vma
6429               - elf_gp (htab->splt->output_section->owner)
6430               - htab->stub_group[stub_entry->id_sec->id].toc_off);
6431
6432       size = PLT_CALL_STUB_SIZE;
6433       if (PPC_HA ((int) off + 16) != PPC_HA ((int) off))
6434         size += 4;
6435     }
6436   else
6437     {
6438       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
6439          variants.  */
6440       off = (stub_entry->target_value
6441              + stub_entry->target_section->output_offset
6442              + stub_entry->target_section->output_section->vma);
6443       off -= (stub_entry->stub_sec->_raw_size
6444               + stub_entry->stub_sec->output_offset
6445               + stub_entry->stub_sec->output_section->vma);
6446
6447       /* Reset the stub type from the plt variant in case we now
6448          can reach with a shorter stub.  */
6449       if (stub_entry->stub_type >= ppc_stub_plt_branch)
6450         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
6451
6452       size = 4;
6453       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
6454         {
6455           off -= 12;
6456           size = 16;
6457         }
6458
6459       /* If the branch offset if too big, use a ppc_stub_plt_branch.  */
6460       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
6461         {
6462           struct ppc_branch_hash_entry *br_entry;
6463
6464           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
6465                                              stub_entry->root.string + 9,
6466                                              TRUE, FALSE);
6467           if (br_entry == NULL)
6468             {
6469               (*_bfd_error_handler) (_("can't build branch stub `%s'"),
6470                                      stub_entry->root.string + 9);
6471               htab->stub_error = TRUE;
6472               return FALSE;
6473             }
6474
6475           if (br_entry->iter != htab->stub_iteration)
6476             {
6477               br_entry->iter = htab->stub_iteration;
6478               br_entry->offset = htab->sbrlt->_raw_size;
6479               htab->sbrlt->_raw_size += 8;
6480             }
6481
6482           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
6483           size = 16;
6484           if (stub_entry->stub_type != ppc_stub_plt_branch)
6485             size = 28;
6486         }
6487     }
6488
6489   stub_entry->stub_sec->_raw_size += size;
6490   return TRUE;
6491 }
6492
6493 /* Set up various things so that we can make a list of input sections
6494    for each output section included in the link.  Returns -1 on error,
6495    0 when no stubs will be needed, and 1 on success.  */
6496
6497 int
6498 ppc64_elf_setup_section_lists (output_bfd, info)
6499      bfd *output_bfd;
6500      struct bfd_link_info *info;
6501 {
6502   bfd *input_bfd;
6503   int top_id, top_index;
6504   asection *section;
6505   asection **input_list, **list;
6506   bfd_size_type amt;
6507   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6508
6509   if (htab->sbrlt == NULL)
6510     return 0;
6511
6512   /* Find the top input section id.  */
6513   for (input_bfd = info->input_bfds, top_id = 0;
6514        input_bfd != NULL;
6515        input_bfd = input_bfd->link_next)
6516     {
6517       for (section = input_bfd->sections;
6518            section != NULL;
6519            section = section->next)
6520         {
6521           if (top_id < section->id)
6522             top_id = section->id;
6523         }
6524     }
6525
6526   amt = sizeof (struct map_stub) * (top_id + 1);
6527   htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
6528   if (htab->stub_group == NULL)
6529     return -1;
6530
6531   elf_gp (output_bfd) = htab->toc_curr = ppc64_elf_toc (output_bfd);
6532
6533   /* We can't use output_bfd->section_count here to find the top output
6534      section index as some sections may have been removed, and
6535      _bfd_strip_section_from_output doesn't renumber the indices.  */
6536   for (section = output_bfd->sections, top_index = 0;
6537        section != NULL;
6538        section = section->next)
6539     {
6540       if (top_index < section->index)
6541         top_index = section->index;
6542     }
6543
6544   htab->top_index = top_index;
6545   amt = sizeof (asection *) * (top_index + 1);
6546   input_list = (asection **) bfd_malloc (amt);
6547   htab->input_list = input_list;
6548   if (input_list == NULL)
6549     return -1;
6550
6551   /* For sections we aren't interested in, mark their entries with a
6552      value we can check later.  */
6553   list = input_list + top_index;
6554   do
6555     *list = bfd_abs_section_ptr;
6556   while (list-- != input_list);
6557
6558   for (section = output_bfd->sections;
6559        section != NULL;
6560        section = section->next)
6561     {
6562       if ((section->flags & SEC_CODE) != 0)
6563         input_list[section->index] = NULL;
6564     }
6565
6566   return 1;
6567 }
6568
6569 /* The linker repeatedly calls this function for each toc input
6570    section.  Group input bfds such that the toc within a group
6571    is less than 64k in size.  Will break with cute linker scripts
6572    that play games with dot in the output toc section.  */
6573
6574 void
6575 ppc64_elf_next_toc_section (info, isec)
6576      struct bfd_link_info *info;
6577      asection *isec;
6578 {
6579   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6580
6581   if (!htab->no_multi_toc)
6582     {
6583       bfd_vma addr = isec->output_offset + isec->output_section->vma;
6584       bfd_vma off = addr - htab->toc_curr;
6585       if (off + isec->_raw_size > 0x10000)
6586         {
6587           htab->toc_curr = addr;
6588           htab->multi_toc_needed = 1;
6589         }
6590       elf_gp (isec->owner) = (htab->toc_curr
6591                               - elf_gp (isec->output_section->owner)
6592                               + TOC_BASE_OFF);
6593     }
6594 }
6595
6596 /* Called after the last call to the above function.  */
6597
6598 void
6599 ppc64_elf_reinit_toc (output_bfd, info)
6600      bfd *output_bfd ATTRIBUTE_UNUSED;
6601      struct bfd_link_info *info;
6602 {
6603   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6604   bfd *ibfd;
6605   bfd_vma curr;
6606
6607   /* toc_curr tracks the TOC offset used for code sections below in
6608      ppc64_elf_next_input_section.  Start off at 0x8000.  */
6609   htab->toc_curr = curr = TOC_BASE_OFF;
6610
6611   /* Set the TOC base in all input bfds.  Some may not have a TOC
6612      section and thus not be set in ppc64_elf_next_toc_section.  */
6613   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6614     {
6615       if (elf_gp (ibfd) == 0)
6616         elf_gp (ibfd) = curr;
6617       else
6618         curr = elf_gp (ibfd);
6619     }
6620 }
6621
6622 /* The linker repeatedly calls this function for each input section,
6623    in the order that input sections are linked into output sections.
6624    Build lists of input sections to determine groupings between which
6625    we may insert linker stubs.  */
6626
6627 void
6628 ppc64_elf_next_input_section (info, isec)
6629      struct bfd_link_info *info;
6630      asection *isec;
6631 {
6632   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6633
6634   if (isec->output_section->index <= htab->top_index)
6635     {
6636       asection **list = htab->input_list + isec->output_section->index;
6637       if (*list != bfd_abs_section_ptr)
6638         {
6639           /* Steal the link_sec pointer for our list.  */
6640 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
6641           /* This happens to make the list in reverse order,
6642              which is what we want.  */
6643           PREV_SEC (isec) = *list;
6644           *list = isec;
6645         }
6646     }
6647
6648   /* If a code section has a function that uses the TOC then we need
6649      to use the right TOC (obviously).  Also, make sure that .opd gets
6650      the correct TOC value.  */
6651   if (isec->has_gp_reloc || (isec->flags & SEC_CODE) == 0)
6652     htab->toc_curr = elf_gp (isec->owner);
6653
6654   /* Functions that don't use the TOC can belong in any TOC group.
6655      Use the last TOC base.  This happens to make _init and _fini
6656      pasting work.  */
6657   htab->stub_group[isec->id].toc_off = htab->toc_curr;
6658 }
6659
6660 /* See whether we can group stub sections together.  Grouping stub
6661    sections may result in fewer stubs.  More importantly, we need to
6662    put all .init* and .fini* stubs at the beginning of the .init or
6663    .fini output sections respectively, because glibc splits the
6664    _init and _fini functions into multiple parts.  Putting a stub in
6665    the middle of a function is not a good idea.  */
6666
6667 static void
6668 group_sections (htab, stub_group_size, stubs_always_before_branch)
6669      struct ppc_link_hash_table *htab;
6670      bfd_size_type stub_group_size;
6671      bfd_boolean stubs_always_before_branch;
6672 {
6673   asection **list = htab->input_list + htab->top_index;
6674   do
6675     {
6676       asection *tail = *list;
6677       if (tail == bfd_abs_section_ptr)
6678         continue;
6679       while (tail != NULL)
6680         {
6681           asection *curr;
6682           asection *prev;
6683           bfd_size_type total;
6684           bfd_boolean big_sec;
6685           bfd_vma curr_toc;
6686
6687           curr = tail;
6688           if (tail->_cooked_size)
6689             total = tail->_cooked_size;
6690           else
6691             total = tail->_raw_size;
6692           big_sec = total >= stub_group_size;
6693           curr_toc = htab->stub_group[tail->id].toc_off;
6694
6695           while ((prev = PREV_SEC (curr)) != NULL
6696                  && ((total += curr->output_offset - prev->output_offset)
6697                      < stub_group_size)
6698                  && htab->stub_group[prev->id].toc_off == curr_toc)
6699             curr = prev;
6700
6701           /* OK, the size from the start of CURR to the end is less
6702              than stub_group_size and thus can be handled by one stub
6703              section.  (or the tail section is itself larger than
6704              stub_group_size, in which case we may be toast.)  We
6705              should really be keeping track of the total size of stubs
6706              added here, as stubs contribute to the final output
6707              section size.  That's a little tricky, and this way will
6708              only break if stubs added make the total size more than
6709              2^25, ie. for the default stub_group_size, if stubs total
6710              more than 2097152 bytes, or nearly 75000 plt call stubs.  */
6711           do
6712             {
6713               prev = PREV_SEC (tail);
6714               /* Set up this stub group.  */
6715               htab->stub_group[tail->id].link_sec = curr;
6716             }
6717           while (tail != curr && (tail = prev) != NULL);
6718
6719           /* But wait, there's more!  Input sections up to stub_group_size
6720              bytes before the stub section can be handled by it too.
6721              Don't do this if we have a really large section after the
6722              stubs, as adding more stubs increases the chance that
6723              branches may not reach into the stub section.  */
6724           if (!stubs_always_before_branch && !big_sec)
6725             {
6726               total = 0;
6727               while (prev != NULL
6728                      && ((total += tail->output_offset - prev->output_offset)
6729                          < stub_group_size)
6730                      && htab->stub_group[prev->id].toc_off == curr_toc)
6731                 {
6732                   tail = prev;
6733                   prev = PREV_SEC (tail);
6734                   htab->stub_group[tail->id].link_sec = curr;
6735                 }
6736             }
6737           tail = prev;
6738         }
6739     }
6740   while (list-- != htab->input_list);
6741   free (htab->input_list);
6742 #undef PREV_SEC
6743 }
6744
6745 /* Determine and set the size of the stub section for a final link.
6746
6747    The basic idea here is to examine all the relocations looking for
6748    PC-relative calls to a target that is unreachable with a "bl"
6749    instruction.  */
6750
6751 bfd_boolean
6752 ppc64_elf_size_stubs (output_bfd, stub_bfd, info, group_size,
6753                       add_stub_section, layout_sections_again)
6754      bfd *output_bfd;
6755      bfd *stub_bfd;
6756      struct bfd_link_info *info;
6757      bfd_signed_vma group_size;
6758      asection * (*add_stub_section) PARAMS ((const char *, asection *));
6759      void (*layout_sections_again) PARAMS ((void));
6760 {
6761   bfd_size_type stub_group_size;
6762   bfd_boolean stubs_always_before_branch;
6763   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6764
6765   /* Stash our params away.  */
6766   htab->stub_bfd = stub_bfd;
6767   htab->add_stub_section = add_stub_section;
6768   htab->layout_sections_again = layout_sections_again;
6769   stubs_always_before_branch = group_size < 0;
6770   if (group_size < 0)
6771     stub_group_size = -group_size;
6772   else
6773     stub_group_size = group_size;
6774   if (stub_group_size == 1)
6775     {
6776       /* Default values.  */
6777       if (stubs_always_before_branch)
6778         {
6779           stub_group_size = 0x1e00000;
6780           if (htab->has_14bit_branch)
6781             stub_group_size = 0x7800;
6782         }
6783       else
6784         {
6785           stub_group_size = 0x1c00000;
6786           if (htab->has_14bit_branch)
6787             stub_group_size = 0x7000;
6788         }
6789     }
6790
6791   group_sections (htab, stub_group_size, stubs_always_before_branch);
6792
6793   while (1)
6794     {
6795       bfd *input_bfd;
6796       unsigned int bfd_indx;
6797       asection *stub_sec;
6798       bfd_boolean stub_changed;
6799
6800       htab->stub_iteration += 1;
6801       stub_changed = FALSE;
6802
6803       for (input_bfd = info->input_bfds, bfd_indx = 0;
6804            input_bfd != NULL;
6805            input_bfd = input_bfd->link_next, bfd_indx++)
6806         {
6807           Elf_Internal_Shdr *symtab_hdr;
6808           asection *section;
6809           Elf_Internal_Sym *local_syms = NULL;
6810
6811           /* We'll need the symbol table in a second.  */
6812           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6813           if (symtab_hdr->sh_info == 0)
6814             continue;
6815
6816           /* Walk over each section attached to the input bfd.  */
6817           for (section = input_bfd->sections;
6818                section != NULL;
6819                section = section->next)
6820             {
6821               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
6822
6823               /* If there aren't any relocs, then there's nothing more
6824                  to do.  */
6825               if ((section->flags & SEC_RELOC) == 0
6826                   || section->reloc_count == 0)
6827                 continue;
6828
6829               /* If this section is a link-once section that will be
6830                  discarded, then don't create any stubs.  */
6831               if (section->output_section == NULL
6832                   || section->output_section->owner != output_bfd)
6833                 continue;
6834
6835               /* Get the relocs.  */
6836               internal_relocs
6837                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
6838                                              (Elf_Internal_Rela *) NULL,
6839                                              info->keep_memory);
6840               if (internal_relocs == NULL)
6841                 goto error_ret_free_local;
6842
6843               /* Now examine each relocation.  */
6844               irela = internal_relocs;
6845               irelaend = irela + section->reloc_count;
6846               for (; irela < irelaend; irela++)
6847                 {
6848                   unsigned int r_type, r_indx;
6849                   enum ppc_stub_type stub_type;
6850                   struct ppc_stub_hash_entry *stub_entry;
6851                   asection *sym_sec;
6852                   bfd_vma sym_value;
6853                   bfd_vma destination;
6854                   struct ppc_link_hash_entry *hash;
6855                   struct elf_link_hash_entry *h;
6856                   Elf_Internal_Sym *sym;
6857                   char *stub_name;
6858                   const asection *id_sec;
6859
6860                   r_type = ELF64_R_TYPE (irela->r_info);
6861                   r_indx = ELF64_R_SYM (irela->r_info);
6862
6863                   if (r_type >= (unsigned int) R_PPC64_max)
6864                     {
6865                       bfd_set_error (bfd_error_bad_value);
6866                       goto error_ret_free_internal;
6867                     }
6868
6869                   /* Only look for stubs on branch instructions.  */
6870                   if (r_type != (unsigned int) R_PPC64_REL24
6871                       && r_type != (unsigned int) R_PPC64_REL14
6872                       && r_type != (unsigned int) R_PPC64_REL14_BRTAKEN
6873                       && r_type != (unsigned int) R_PPC64_REL14_BRNTAKEN)
6874                     continue;
6875
6876                   /* Now determine the call target, its name, value,
6877                      section.  */
6878                   destination = 0;
6879                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6880                                   r_indx, input_bfd))
6881                     goto error_ret_free_internal;
6882                   hash = (struct ppc_link_hash_entry *) h;
6883
6884                   if (hash == NULL)
6885                     {
6886                       /* It's a local symbol.  */
6887                       sym_value = sym->st_value;
6888                       destination = (sym_value + irela->r_addend
6889                                      + sym_sec->output_offset
6890                                      + sym_sec->output_section->vma);
6891                     }
6892                   else
6893                     {
6894                       /* It's an external symbol.  */
6895                       sym_value = 0;
6896                       if (hash->elf.root.type == bfd_link_hash_defined
6897                           || hash->elf.root.type == bfd_link_hash_defweak)
6898                         {
6899                           sym_value = hash->elf.root.u.def.value;
6900                           if (sym_sec->output_section != NULL)
6901                             destination = (sym_value + irela->r_addend
6902                                            + sym_sec->output_offset
6903                                            + sym_sec->output_section->vma);
6904                         }
6905                       else if (hash->elf.root.type == bfd_link_hash_undefweak)
6906                         ;
6907                       else if (hash->elf.root.type == bfd_link_hash_undefined)
6908                         ;
6909                       else
6910                         {
6911                           bfd_set_error (bfd_error_bad_value);
6912                           goto error_ret_free_internal;
6913                         }
6914                     }
6915
6916                   /* Determine what (if any) linker stub is needed.  */
6917                   stub_type = ppc_type_of_stub (section, irela, &hash,
6918                                                 destination);
6919
6920                   if (stub_type != ppc_stub_plt_call)
6921                     {
6922                       /* Check whether we need a TOC adjusting stub.
6923                          Since the linker pastes together pieces from
6924                          different object files when creating the
6925                          _init and _fini functions, it may be that a
6926                          call to what looks like a local sym is in
6927                          fact a call needing a TOC adjustment.  */
6928                       if (sym_sec != NULL
6929                           && sym_sec->output_section != NULL
6930                           && (htab->stub_group[sym_sec->id].toc_off
6931                               != htab->stub_group[section->id].toc_off))
6932                         stub_type = ppc_stub_long_branch_r2off;
6933                     }
6934
6935                   if (stub_type == ppc_stub_none)
6936                     continue;
6937
6938                   /* __tls_get_addr calls might be eliminated.  */
6939                   if (stub_type != ppc_stub_plt_call
6940                       && hash != NULL
6941                       && &hash->elf == htab->tls_get_addr
6942                       && section->has_tls_reloc
6943                       && irela != internal_relocs)
6944                     {
6945                       /* Get tls info.  */
6946                       char *tls_mask;
6947
6948                       if (!get_tls_mask (&tls_mask, &local_syms,
6949                                          irela - 1, input_bfd))
6950                         goto error_ret_free_internal;
6951                       if (*tls_mask != 0)
6952                         continue;
6953                     }
6954
6955                   /* Support for grouping stub sections.  */
6956                   id_sec = htab->stub_group[section->id].link_sec;
6957
6958                   /* Get the name of this stub.  */
6959                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
6960                   if (!stub_name)
6961                     goto error_ret_free_internal;
6962
6963                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
6964                                                      stub_name, FALSE, FALSE);
6965                   if (stub_entry != NULL)
6966                     {
6967                       /* The proper stub has already been created.  */
6968                       free (stub_name);
6969                       continue;
6970                     }
6971
6972                   stub_entry = ppc_add_stub (stub_name, section, htab);
6973                   if (stub_entry == NULL)
6974                     {
6975                       free (stub_name);
6976                     error_ret_free_internal:
6977                       if (elf_section_data (section)->relocs == NULL)
6978                         free (internal_relocs);
6979                     error_ret_free_local:
6980                       if (local_syms != NULL
6981                           && (symtab_hdr->contents
6982                               != (unsigned char *) local_syms))
6983                         free (local_syms);
6984                       return FALSE;
6985                     }
6986
6987                   stub_entry->stub_type = stub_type;
6988                   stub_entry->target_value = sym_value;
6989                   stub_entry->target_section = sym_sec;
6990                   stub_entry->h = hash;
6991                   stub_entry->addend = irela->r_addend;
6992                   stub_changed = TRUE;
6993                 }
6994
6995               /* We're done with the internal relocs, free them.  */
6996               if (elf_section_data (section)->relocs != internal_relocs)
6997                 free (internal_relocs);
6998             }
6999
7000           if (local_syms != NULL
7001               && symtab_hdr->contents != (unsigned char *) local_syms)
7002             {
7003               if (!info->keep_memory)
7004                 free (local_syms);
7005               else
7006                 symtab_hdr->contents = (unsigned char *) local_syms;
7007             }
7008         }
7009
7010       if (!stub_changed)
7011         break;
7012
7013       /* OK, we've added some stubs.  Find out the new size of the
7014          stub sections.  */
7015       for (stub_sec = htab->stub_bfd->sections;
7016            stub_sec != NULL;
7017            stub_sec = stub_sec->next)
7018         {
7019           stub_sec->_raw_size = 0;
7020           stub_sec->_cooked_size = 0;
7021         }
7022       htab->sbrlt->_raw_size = 0;
7023       htab->sbrlt->_cooked_size = 0;
7024
7025       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, htab);
7026
7027       /* Ask the linker to do its stuff.  */
7028       (*htab->layout_sections_again) ();
7029     }
7030
7031   /* It would be nice to strip .branch_lt from the output if the
7032      section is empty, but it's too late.  If we strip sections here,
7033      the dynamic symbol table is corrupted since the section symbol
7034      for the stripped section isn't written.  */
7035
7036   return TRUE;
7037 }
7038
7039 /* Called after we have determined section placement.  If sections
7040    move, we'll be called again.  Provide a value for TOCstart.  */
7041
7042 bfd_vma
7043 ppc64_elf_toc (obfd)
7044      bfd *obfd;
7045 {
7046   asection *s;
7047   bfd_vma TOCstart;
7048
7049   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
7050      order.  The TOC starts where the first of these sections starts.  */
7051   s = bfd_get_section_by_name (obfd, ".got");
7052   if (s == NULL)
7053     s = bfd_get_section_by_name (obfd, ".toc");
7054   if (s == NULL)
7055     s = bfd_get_section_by_name (obfd, ".tocbss");
7056   if (s == NULL)
7057     s = bfd_get_section_by_name (obfd, ".plt");
7058   if (s == NULL)
7059     {
7060       /* This may happen for
7061          o  references to TOC base (SYM@toc / TOC[tc0]) without a
7062          .toc directive
7063          o  bad linker script
7064          o --gc-sections and empty TOC sections
7065
7066          FIXME: Warn user?  */
7067
7068       /* Look for a likely section.  We probably won't even be
7069          using TOCstart.  */
7070       for (s = obfd->sections; s != NULL; s = s->next)
7071         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
7072             == (SEC_ALLOC | SEC_SMALL_DATA))
7073           break;
7074       if (s == NULL)
7075         for (s = obfd->sections; s != NULL; s = s->next)
7076           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
7077               == (SEC_ALLOC | SEC_SMALL_DATA))
7078             break;
7079       if (s == NULL)
7080         for (s = obfd->sections; s != NULL; s = s->next)
7081           if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
7082             break;
7083       if (s == NULL)
7084         for (s = obfd->sections; s != NULL; s = s->next)
7085           if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
7086             break;
7087     }
7088
7089   TOCstart = 0;
7090   if (s != NULL)
7091     TOCstart = s->output_section->vma + s->output_offset;
7092
7093   return TOCstart;
7094 }
7095
7096 /* Build all the stubs associated with the current output file.
7097    The stubs are kept in a hash table attached to the main linker
7098    hash table.  This function is called via gldelf64ppc_finish.  */
7099
7100 bfd_boolean
7101 ppc64_elf_build_stubs (emit_stub_syms, info)
7102      bfd_boolean emit_stub_syms;
7103      struct bfd_link_info *info;
7104 {
7105   struct ppc_link_hash_table *htab = ppc_hash_table (info);
7106   asection *stub_sec;
7107   bfd_byte *p;
7108
7109   htab->emit_stub_syms = emit_stub_syms;
7110   for (stub_sec = htab->stub_bfd->sections;
7111        stub_sec != NULL;
7112        stub_sec = stub_sec->next)
7113     {
7114       bfd_size_type size;
7115
7116       /* Allocate memory to hold the linker stubs.  */
7117       size = stub_sec->_raw_size;
7118       if (size != 0)
7119         {
7120           stub_sec->contents = (bfd_byte *) bfd_zalloc (htab->stub_bfd, size);
7121           if (stub_sec->contents == NULL)
7122             return FALSE;
7123         }
7124       stub_sec->_cooked_size = 0;
7125     }
7126
7127   if (htab->splt != NULL)
7128     {
7129       unsigned int indx;
7130       bfd_vma plt0;
7131
7132       /* Build the .glink plt call stub.  */
7133       plt0 = (htab->splt->output_section->vma
7134               + htab->splt->output_offset
7135               - (htab->sglink->output_section->vma
7136                  + htab->sglink->output_offset
7137                  + GLINK_CALL_STUB_SIZE));
7138       if (plt0 + 0x80008000 > 0xffffffff)
7139         {
7140           (*_bfd_error_handler) (_(".glink and .plt too far apart"));
7141           bfd_set_error (bfd_error_bad_value);
7142           return FALSE;
7143         }
7144
7145       p = htab->sglink->contents;
7146       bfd_put_32 (htab->sglink->owner, MFCTR_R12, p);
7147       p += 4;
7148       bfd_put_32 (htab->sglink->owner, SLDI_R11_R0_3, p);
7149       p += 4;
7150       bfd_put_32 (htab->sglink->owner, ADDIC_R2_R0_32K, p);
7151       p += 4;
7152       bfd_put_32 (htab->sglink->owner, SUB_R12_R12_R11, p);
7153       p += 4;
7154       bfd_put_32 (htab->sglink->owner, SRADI_R2_R2_63, p);
7155       p += 4;
7156       bfd_put_32 (htab->sglink->owner, SLDI_R11_R0_2, p);
7157       p += 4;
7158       bfd_put_32 (htab->sglink->owner, AND_R2_R2_R11, p);
7159       p += 4;
7160       bfd_put_32 (htab->sglink->owner, SUB_R12_R12_R11, p);
7161       p += 4;
7162       bfd_put_32 (htab->sglink->owner, ADD_R12_R12_R2, p);
7163       p += 4;
7164       bfd_put_32 (htab->sglink->owner, ADDIS_R12_R12 | PPC_HA (plt0), p);
7165       p += 4;
7166       bfd_put_32 (htab->sglink->owner, LD_R11_0R12 | PPC_LO (plt0), p);
7167       p += 4;
7168       bfd_put_32 (htab->sglink->owner, ADDI_R12_R12 | PPC_LO (plt0), p);
7169       p += 4;
7170       bfd_put_32 (htab->sglink->owner, LD_R2_0R12 | 8, p);
7171       p += 4;
7172       bfd_put_32 (htab->sglink->owner, MTCTR_R11, p);
7173       p += 4;
7174       bfd_put_32 (htab->sglink->owner, LD_R11_0R12 | 16, p);
7175       p += 4;
7176       bfd_put_32 (htab->sglink->owner, BCTR, p);
7177       p += 4;
7178
7179       /* Build the .glink lazy link call stubs.  */
7180       indx = 0;
7181       while (p < htab->sglink->contents + htab->sglink->_raw_size)
7182         {
7183           if (indx < 0x8000)
7184             {
7185               bfd_put_32 (htab->sglink->owner, LI_R0_0 | indx, p);
7186               p += 4;
7187             }
7188           else
7189             {
7190               bfd_put_32 (htab->sglink->owner, LIS_R0_0 | PPC_HI (indx), p);
7191               p += 4;
7192               bfd_put_32 (htab->sglink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
7193               p += 4;
7194             }
7195           bfd_put_32 (htab->sglink->owner,
7196                       B_DOT | ((htab->sglink->contents - p) & 0x3fffffc), p);
7197           indx++;
7198           p += 4;
7199         }
7200       htab->sglink->_cooked_size = p - htab->sglink->contents;
7201     }
7202
7203   if (htab->sbrlt->_raw_size != 0)
7204     {
7205       htab->sbrlt->contents = (bfd_byte *) bfd_zalloc (htab->sbrlt->owner,
7206                                                        htab->sbrlt->_raw_size);
7207       if (htab->sbrlt->contents == NULL)
7208         return FALSE;
7209     }
7210
7211   /* Build the stubs as directed by the stub hash table.  */
7212   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
7213
7214   for (stub_sec = htab->stub_bfd->sections;
7215        stub_sec != NULL;
7216        stub_sec = stub_sec->next)
7217     {
7218       if (stub_sec->_raw_size != stub_sec->_cooked_size)
7219         break;
7220     }
7221
7222   if (stub_sec != NULL
7223       || htab->sglink->_raw_size != htab->sglink->_cooked_size)
7224     {
7225       htab->stub_error = TRUE;
7226       (*_bfd_error_handler) (_("stubs don't match calculated size"));
7227     }
7228
7229   return !htab->stub_error;
7230 }
7231
7232 /* The RELOCATE_SECTION function is called by the ELF backend linker
7233    to handle the relocations for a section.
7234
7235    The relocs are always passed as Rela structures; if the section
7236    actually uses Rel structures, the r_addend field will always be
7237    zero.
7238
7239    This function is responsible for adjust the section contents as
7240    necessary, and (if using Rela relocs and generating a
7241    relocateable output file) adjusting the reloc addend as
7242    necessary.
7243
7244    This function does not have to worry about setting the reloc
7245    address or the reloc symbol index.
7246
7247    LOCAL_SYMS is a pointer to the swapped in local symbols.
7248
7249    LOCAL_SECTIONS is an array giving the section in the input file
7250    corresponding to the st_shndx field of each local symbol.
7251
7252    The global hash table entry for the global symbols can be found
7253    via elf_sym_hashes (input_bfd).
7254
7255    When generating relocateable output, this function must handle
7256    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
7257    going to be the section symbol corresponding to the output
7258    section, which means that the addend must be adjusted
7259    accordingly.  */
7260
7261 static bfd_boolean
7262 ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
7263                             contents, relocs, local_syms, local_sections)
7264      bfd *output_bfd;
7265      struct bfd_link_info *info;
7266      bfd *input_bfd;
7267      asection *input_section;
7268      bfd_byte *contents;
7269      Elf_Internal_Rela *relocs;
7270      Elf_Internal_Sym *local_syms;
7271      asection **local_sections;
7272 {
7273   struct ppc_link_hash_table *htab;
7274   Elf_Internal_Shdr *symtab_hdr;
7275   struct elf_link_hash_entry **sym_hashes;
7276   Elf_Internal_Rela *rel;
7277   Elf_Internal_Rela *relend;
7278   Elf_Internal_Rela outrel;
7279   bfd_byte *loc;
7280   struct got_entry **local_got_ents;
7281   bfd_vma TOCstart;
7282   bfd_boolean ret = TRUE;
7283   bfd_boolean is_opd;
7284   /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
7285   bfd_boolean is_power4 = FALSE;
7286
7287   if (info->relocateable)
7288     return TRUE;
7289
7290   /* Initialize howto table if needed.  */
7291   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
7292     ppc_howto_init ();
7293
7294   htab = ppc_hash_table (info);
7295   local_got_ents = elf_local_got_ents (input_bfd);
7296   TOCstart = elf_gp (output_bfd);
7297   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7298   sym_hashes = elf_sym_hashes (input_bfd);
7299   is_opd = ppc64_elf_section_data (input_section)->opd.adjust != NULL;
7300
7301   rel = relocs;
7302   relend = relocs + input_section->reloc_count;
7303   for (; rel < relend; rel++)
7304     {
7305       enum elf_ppc64_reloc_type r_type;
7306       bfd_vma addend;
7307       bfd_reloc_status_type r;
7308       Elf_Internal_Sym *sym;
7309       asection *sec;
7310       struct elf_link_hash_entry *h;
7311       struct elf_link_hash_entry *fdh;
7312       const char *sym_name;
7313       unsigned long r_symndx;
7314       char tls_mask, tls_gd, tls_type;
7315       bfd_vma relocation;
7316       bfd_boolean unresolved_reloc;
7317       bfd_boolean warned;
7318       long insn, mask;
7319       struct ppc_stub_hash_entry *stub_entry;
7320       bfd_vma max_br_offset;
7321       bfd_vma from;
7322
7323       r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
7324       r_symndx = ELF64_R_SYM (rel->r_info);
7325       sym = (Elf_Internal_Sym *) 0;
7326       sec = (asection *) 0;
7327       h = (struct elf_link_hash_entry *) 0;
7328       sym_name = (const char *) 0;
7329       unresolved_reloc = FALSE;
7330       warned = FALSE;
7331
7332       if (r_type == R_PPC64_TOC)
7333         {
7334           /* Relocation value is TOC base.  */
7335           relocation = TOCstart + htab->stub_group[input_section->id].toc_off;
7336         }
7337       else if (r_symndx < symtab_hdr->sh_info)
7338         {
7339           /* It's a local symbol.  */
7340           sym = local_syms + r_symndx;
7341           sec = local_sections[r_symndx];
7342           sym_name = bfd_elf_local_sym_name (input_bfd, sym);
7343           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
7344           if (elf_section_data (sec) != NULL)
7345             {
7346               long *opd_sym_adjust;
7347
7348               opd_sym_adjust = ppc64_elf_section_data (sec)->opd.adjust;
7349               if (opd_sym_adjust != NULL && sym->st_value % 24 == 0)
7350                 relocation += opd_sym_adjust[sym->st_value / 24];
7351             }
7352         }
7353       else
7354         {
7355           /* It's a global symbol.  */
7356           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7357           while (h->root.type == bfd_link_hash_indirect
7358                  || h->root.type == bfd_link_hash_warning)
7359             h = (struct elf_link_hash_entry *) h->root.u.i.link;
7360           sym_name = h->root.root.string;
7361           relocation = 0;
7362           if (h->root.type == bfd_link_hash_defined
7363               || h->root.type == bfd_link_hash_defweak)
7364             {
7365               sec = h->root.u.def.section;
7366               if (sec->output_section == NULL)
7367                 /* Set a flag that will be cleared later if we find a
7368                    relocation value for this symbol.  output_section
7369                    is typically NULL for symbols satisfied by a shared
7370                    library.  */
7371                 unresolved_reloc = TRUE;
7372               else
7373                 relocation = (h->root.u.def.value
7374                               + sec->output_section->vma
7375                               + sec->output_offset);
7376             }
7377           else if (h->root.type == bfd_link_hash_undefweak)
7378             ;
7379           else if (info->shared
7380                    && !info->no_undefined
7381                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
7382             ;
7383           else
7384             {
7385               if (! ((*info->callbacks->undefined_symbol)
7386                      (info, h->root.root.string, input_bfd, input_section,
7387                       rel->r_offset, (!info->shared
7388                                       || info->no_undefined
7389                                       || ELF_ST_VISIBILITY (h->other)))))
7390                 return FALSE;
7391               warned = TRUE;
7392             }
7393         }
7394
7395       /* TLS optimizations.  Replace instruction sequences and relocs
7396          based on information we collected in tls_optimize.  We edit
7397          RELOCS so that --emit-relocs will output something sensible
7398          for the final instruction stream.  */
7399       tls_mask = 0;
7400       tls_gd = 0;
7401       if (IS_PPC64_TLS_RELOC (r_type))
7402         {
7403           if (h != NULL)
7404             tls_mask = ((struct ppc_link_hash_entry *) h)->tls_mask;
7405           else if (local_got_ents != NULL)
7406             {
7407               char *lgot_masks;
7408               lgot_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
7409               tls_mask = lgot_masks[r_symndx];
7410             }
7411         }
7412
7413       /* Ensure reloc mapping code below stays sane.  */
7414       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
7415           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
7416           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
7417           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
7418           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
7419           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
7420           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
7421           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
7422           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
7423           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
7424         abort ();
7425       switch (r_type)
7426         {
7427         default:
7428           break;
7429
7430         case R_PPC64_TOC16:
7431         case R_PPC64_TOC16_LO:
7432         case R_PPC64_TOC16_DS:
7433         case R_PPC64_TOC16_LO_DS:
7434           {
7435             /* Check for toc tls entries.  */
7436             char *toc_tls;
7437             int retval;
7438
7439             retval = get_tls_mask (&toc_tls, &local_syms, rel, input_bfd);
7440             if (retval == 0)
7441               return FALSE;
7442
7443             if (toc_tls)
7444               {
7445                 tls_mask = *toc_tls;
7446                 if (r_type == R_PPC64_TOC16_DS
7447                     || r_type == R_PPC64_TOC16_LO_DS)
7448                   {
7449                     if (tls_mask != 0
7450                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
7451                       goto toctprel;
7452                   }
7453                 else
7454                   {
7455                     /* If we found a GD reloc pair, then we might be
7456                        doing a GD->IE transition.  */
7457                     if (retval == 2)
7458                       {
7459                         tls_gd = TLS_TPRELGD;
7460                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7461                           goto tls_get_addr_check;
7462                       }
7463                     else if (retval == 3)
7464                       {
7465                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7466                           goto tls_get_addr_check;
7467                       }
7468                   }
7469               }
7470           }
7471           break;
7472
7473         case R_PPC64_GOT_TPREL16_DS:
7474         case R_PPC64_GOT_TPREL16_LO_DS:
7475           if (tls_mask != 0
7476               && (tls_mask & TLS_TPREL) == 0)
7477             {
7478               bfd_vma insn;
7479             toctprel:
7480               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
7481               insn &= 31 << 21;
7482               insn |= 0x3c0d0000;       /* addis 0,13,0 */
7483               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
7484               r_type = R_PPC64_TPREL16_HA;
7485               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7486             }
7487           break;
7488
7489         case R_PPC64_TLS:
7490           if (tls_mask == 0)
7491             {
7492               /* Check for toc tls entries.  */
7493               char *toc_tls;
7494
7495               if (!get_tls_mask (&toc_tls, &local_syms, rel, input_bfd))
7496                 return FALSE;
7497
7498               if (toc_tls)
7499                 tls_mask = *toc_tls;
7500             }
7501           if (tls_mask != 0
7502               && (tls_mask & TLS_TPREL) == 0)
7503             {
7504               bfd_vma insn, rtra;
7505               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
7506               if ((insn & ((31 << 26) | (31 << 11)))
7507                   == ((31 << 26) | (13 << 11)))
7508                 rtra = insn & ((1 << 26) - (1 << 16));
7509               else if ((insn & ((31 << 26) | (31 << 16)))
7510                        == ((31 << 26) | (13 << 16)))
7511                 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
7512               else
7513                 abort ();
7514               if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
7515                 /* add -> addi.  */
7516                 insn = 14 << 26;
7517               else if ((insn & (31 << 1)) == 23 << 1
7518                        && ((insn & (31 << 6)) < 14 << 6
7519                            || ((insn & (31 << 6)) >= 16 << 6
7520                                && (insn & (31 << 6)) < 24 << 6)))
7521                 /* load and store indexed -> dform.  */
7522                 insn = (32 | ((insn >> 6) & 31)) << 26;
7523               else if ((insn & (31 << 1)) == 21 << 1
7524                        && (insn & (0x1a << 6)) == 0)
7525                 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu.  */
7526                 insn = (((58 | ((insn >> 6) & 4)) << 26)
7527                         | ((insn >> 6) & 1));
7528               else if ((insn & (31 << 1)) == 21 << 1
7529                        && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
7530                 /* lwax -> lwa.  */
7531                 insn = (58 << 26) | 2;
7532               else
7533                 abort ();
7534               insn |= rtra;
7535               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7536               r_type = R_PPC64_TPREL16_LO;
7537               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7538               /* Was PPC64_TLS which sits on insn boundary, now
7539                  PPC64_TPREL16_LO which is at insn+2.  */
7540               rel->r_offset += 2;
7541             }
7542           break;
7543
7544         case R_PPC64_GOT_TLSGD16_HI:
7545         case R_PPC64_GOT_TLSGD16_HA:
7546           tls_gd = TLS_TPRELGD;
7547           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7548             goto tls_gdld_hi;
7549           break;
7550
7551         case R_PPC64_GOT_TLSLD16_HI:
7552         case R_PPC64_GOT_TLSLD16_HA:
7553           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7554             {
7555             tls_gdld_hi:
7556               if ((tls_mask & tls_gd) != 0)
7557                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
7558                           + R_PPC64_GOT_TPREL16_DS);
7559               else
7560                 {
7561                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
7562                   rel->r_offset -= 2;
7563                   r_type = R_PPC64_NONE;
7564                 }
7565               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7566             }
7567           break;
7568
7569         case R_PPC64_GOT_TLSGD16:
7570         case R_PPC64_GOT_TLSGD16_LO:
7571           tls_gd = TLS_TPRELGD;
7572           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7573             goto tls_get_addr_check;
7574           break;
7575
7576         case R_PPC64_GOT_TLSLD16:
7577         case R_PPC64_GOT_TLSLD16_LO:
7578           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7579             {
7580             tls_get_addr_check:
7581               if (rel + 1 < relend)
7582                 {
7583                   enum elf_ppc64_reloc_type r_type2;
7584                   unsigned long r_symndx2;
7585                   struct elf_link_hash_entry *h2;
7586                   bfd_vma insn1, insn2, insn3;
7587                   bfd_vma offset;
7588
7589                   /* The next instruction should be a call to
7590                      __tls_get_addr.  Peek at the reloc to be sure.  */
7591                   r_type2
7592                     = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel[1].r_info);
7593                   r_symndx2 = ELF64_R_SYM (rel[1].r_info);
7594                   if (r_symndx2 < symtab_hdr->sh_info
7595                       || (r_type2 != R_PPC64_REL14
7596                           && r_type2 != R_PPC64_REL14_BRTAKEN
7597                           && r_type2 != R_PPC64_REL14_BRNTAKEN
7598                           && r_type2 != R_PPC64_REL24))
7599                     break;
7600
7601                   h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
7602                   while (h2->root.type == bfd_link_hash_indirect
7603                          || h2->root.type == bfd_link_hash_warning)
7604                     h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
7605                   if (h2 == NULL || h2 != htab->tls_get_addr)
7606                     break;
7607
7608                   /* OK, it checks out.  Replace the call.  */
7609                   offset = rel[1].r_offset;
7610                   insn1 = bfd_get_32 (output_bfd,
7611                                       contents + rel->r_offset - 2);
7612                   insn3 = bfd_get_32 (output_bfd,
7613                                       contents + offset + 4);
7614                   if ((tls_mask & tls_gd) != 0)
7615                     {
7616                       /* IE */
7617                       insn1 &= (1 << 26) - (1 << 2);
7618                       insn1 |= 58 << 26;        /* ld */
7619                       insn2 = 0x7c636a14;       /* add 3,3,13 */
7620                       rel[1].r_info = ELF64_R_INFO (r_symndx2, R_PPC64_NONE);
7621                       if ((tls_mask & TLS_EXPLICIT) == 0)
7622                         r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
7623                                   + R_PPC64_GOT_TPREL16_DS);
7624                       else
7625                         r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
7626                       rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7627                     }
7628                   else
7629                     {
7630                       /* LE */
7631                       insn1 = 0x3c6d0000;       /* addis 3,13,0 */
7632                       insn2 = 0x38630000;       /* addi 3,3,0 */
7633                       if (tls_gd == 0)
7634                         {
7635                           /* Was an LD reloc.  */
7636                           r_symndx = 0;
7637                           rel->r_addend = htab->tls_sec->vma + DTP_OFFSET;
7638                           rel[1].r_addend = htab->tls_sec->vma + DTP_OFFSET;
7639                         }
7640                       r_type = R_PPC64_TPREL16_HA;
7641                       rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7642                       rel[1].r_info = ELF64_R_INFO (r_symndx,
7643                                                     R_PPC64_TPREL16_LO);
7644                       rel[1].r_offset += 2;
7645                     }
7646                   if (insn3 == NOP
7647                       || insn3 == CROR_151515 || insn3 == CROR_313131)
7648                     {
7649                       insn3 = insn2;
7650                       insn2 = NOP;
7651                       rel[1].r_offset += 4;
7652                     }
7653                   bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
7654                   bfd_put_32 (output_bfd, insn2, contents + offset);
7655                   bfd_put_32 (output_bfd, insn3, contents + offset + 4);
7656                   if (tls_gd == 0)
7657                     {
7658                       /* We changed the symbol on an LD reloc.  Start over
7659                          in order to get h, sym, sec etc. right.  */
7660                       rel--;
7661                       continue;
7662                     }
7663                 }
7664             }
7665           break;
7666
7667         case R_PPC64_DTPMOD64:
7668           if (rel + 1 < relend
7669               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
7670               && rel[1].r_offset == rel->r_offset + 8)
7671             {
7672               if ((tls_mask & TLS_GD) == 0)
7673                 {
7674                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
7675                   if ((tls_mask & TLS_TPRELGD) != 0)
7676                     r_type = R_PPC64_TPREL64;
7677                   else
7678                     {
7679                       bfd_put_64 (output_bfd, (bfd_vma) 1,
7680                                   contents + rel->r_offset);
7681                       r_type = R_PPC64_NONE;
7682                     }
7683                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7684                 }
7685             }
7686           else
7687             {
7688               if ((tls_mask & TLS_LD) == 0)
7689                 {
7690                   bfd_put_64 (output_bfd, (bfd_vma) 1,
7691                               contents + rel->r_offset);
7692                   r_type = R_PPC64_NONE;
7693                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7694                 }
7695             }
7696           break;
7697
7698         case R_PPC64_TPREL64:
7699           if ((tls_mask & TLS_TPREL) == 0)
7700             {
7701               r_type = R_PPC64_NONE;
7702               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7703             }
7704           break;
7705         }
7706
7707       /* Handle other relocations that tweak non-addend part of insn.  */
7708       insn = 0;
7709       switch (r_type)
7710         {
7711         default:
7712           break;
7713
7714           /* Branch taken prediction relocations.  */
7715         case R_PPC64_ADDR14_BRTAKEN:
7716         case R_PPC64_REL14_BRTAKEN:
7717           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
7718           /* Fall thru.  */
7719
7720           /* Branch not taken prediction relocations.  */
7721         case R_PPC64_ADDR14_BRNTAKEN:
7722         case R_PPC64_REL14_BRNTAKEN:
7723           insn |= bfd_get_32 (output_bfd,
7724                               contents + rel->r_offset) & ~(0x01 << 21);
7725           if (is_power4)
7726             {
7727               /* Set 'a' bit.  This is 0b00010 in BO field for branch
7728                  on CR(BI) insns (BO == 001at or 011at), and 0b01000
7729                  for branch on CTR insns (BO == 1a00t or 1a01t).  */
7730               if ((insn & (0x14 << 21)) == (0x04 << 21))
7731                 insn |= 0x02 << 21;
7732               else if ((insn & (0x14 << 21)) == (0x10 << 21))
7733                 insn |= 0x08 << 21;
7734               else
7735                 break;
7736             }
7737           else
7738             {
7739               from = (rel->r_offset
7740                       + input_section->output_offset
7741                       + input_section->output_section->vma);
7742
7743               /* Invert 'y' bit if not the default.  */
7744               if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
7745                 insn ^= 0x01 << 21;
7746             }
7747
7748           bfd_put_32 (output_bfd, (bfd_vma) insn, contents + rel->r_offset);
7749           break;
7750
7751         case R_PPC64_REL24:
7752           /* Calls to functions with a different TOC, such as calls to
7753              shared objects, need to alter the TOC pointer.  This is
7754              done using a linkage stub.  A REL24 branching to these
7755              linkage stubs needs to be followed by a nop, as the nop
7756              will be replaced with an instruction to restore the TOC
7757              base pointer.  */
7758           if (((h != NULL
7759                 && (fdh = ((struct ppc_link_hash_entry *) h)->oh) != NULL
7760                 && fdh->plt.plist != NULL)
7761                || ((fdh = h, sec) != NULL
7762                    && sec->output_section != NULL
7763                    && (htab->stub_group[sec->id].toc_off
7764                        != htab->stub_group[input_section->id].toc_off)))
7765               && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
7766                                                    rel, htab)) != NULL
7767               && (stub_entry->stub_type == ppc_stub_plt_call
7768                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
7769                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
7770             {
7771               bfd_boolean can_plt_call = 0;
7772
7773               if (rel->r_offset + 8 <= input_section->_cooked_size)
7774                 {
7775                   insn = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
7776                   if (insn == NOP
7777                       || insn == CROR_151515 || insn == CROR_313131)
7778                     {
7779                       bfd_put_32 (input_bfd, (bfd_vma) LD_R2_40R1,
7780                                   contents + rel->r_offset + 4);
7781                       can_plt_call = 1;
7782                     }
7783                 }
7784
7785               if (!can_plt_call)
7786                 {
7787                   if (stub_entry->stub_type == ppc_stub_plt_call)
7788                     {
7789                       /* If this is a plain branch rather than a branch
7790                          and link, don't require a nop.  */
7791                       insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
7792                       if ((insn & 1) == 0)
7793                         can_plt_call = 1;
7794                     }
7795                   else
7796                     {
7797                       if (strcmp (input_section->output_section->name,
7798                                   ".init") == 0
7799                           || strcmp (input_section->output_section->name,
7800                                      ".fini") == 0)
7801                         (*_bfd_error_handler)
7802                           (_("%s(%s+0x%lx): automatic multiple TOCs "
7803                              "not supported using your crt files; "
7804                              "recompile with -mminimal-toc or upgrade gcc"),
7805                            bfd_archive_filename (input_bfd),
7806                            input_section->name,
7807                            (long) rel->r_offset);
7808                       else
7809                         (*_bfd_error_handler)
7810                           (_("%s(%s+0x%lx): sibling call optimization to `%s' "
7811                              "does not allow automatic multiple TOCs; "
7812                              "recompile with -mminimal-toc or "
7813                              "-fno-optimize-sibling-calls, "
7814                              "or make `%s' extern"),
7815                            bfd_archive_filename (input_bfd),
7816                            input_section->name,
7817                            (long) rel->r_offset,
7818                            sym_name,
7819                            sym_name);
7820                       bfd_set_error (bfd_error_bad_value);
7821                       ret = FALSE;
7822                     }
7823                 }
7824
7825               if (can_plt_call)
7826                 {
7827                   relocation = (stub_entry->stub_offset
7828                                 + stub_entry->stub_sec->output_offset
7829                                 + stub_entry->stub_sec->output_section->vma);
7830                   if (stub_entry->stub_type == ppc_stub_plt_call)
7831                     unresolved_reloc = FALSE;
7832                 }
7833             }
7834
7835           if (h != NULL
7836               && h->root.type == bfd_link_hash_undefweak
7837               && relocation == 0
7838               && rel->r_addend == 0)
7839             {
7840               /* Tweak calls to undefined weak functions to point at a
7841                  blr.  We can thus call a weak function without first
7842                  checking whether the function is defined.  We have a
7843                  blr at the end of .sfpr.  */
7844               BFD_ASSERT (htab->sfpr->_raw_size != 0);
7845               relocation = (htab->sfpr->_raw_size - 4
7846                             + htab->sfpr->output_offset
7847                             + htab->sfpr->output_section->vma);
7848               from = (rel->r_offset
7849                       + input_section->output_offset
7850                       + input_section->output_section->vma);
7851
7852               /* But let's not be silly about it.  If the blr isn't in
7853                  reach, just go to the next instruction.  */
7854               if (relocation - from + (1 << 25) >= (1 << 26)
7855                   || htab->sfpr->_raw_size == 0)
7856                 relocation = from + 4;
7857             }
7858           break;
7859         }
7860
7861       /* Set `addend'.  */
7862       tls_type = 0;
7863       addend = rel->r_addend;
7864       switch (r_type)
7865         {
7866         default:
7867           (*_bfd_error_handler)
7868             (_("%s: unknown relocation type %d for symbol %s"),
7869              bfd_archive_filename (input_bfd), (int) r_type, sym_name);
7870
7871           bfd_set_error (bfd_error_bad_value);
7872           ret = FALSE;
7873           continue;
7874
7875         case R_PPC64_NONE:
7876         case R_PPC64_TLS:
7877         case R_PPC64_GNU_VTINHERIT:
7878         case R_PPC64_GNU_VTENTRY:
7879           continue;
7880
7881           /* GOT16 relocations.  Like an ADDR16 using the symbol's
7882              address in the GOT as relocation value instead of the
7883              symbol's value itself.  Also, create a GOT entry for the
7884              symbol and put the symbol value there.  */
7885         case R_PPC64_GOT_TLSGD16:
7886         case R_PPC64_GOT_TLSGD16_LO:
7887         case R_PPC64_GOT_TLSGD16_HI:
7888         case R_PPC64_GOT_TLSGD16_HA:
7889           tls_type = TLS_TLS | TLS_GD;
7890           goto dogot;
7891
7892         case R_PPC64_GOT_TLSLD16:
7893         case R_PPC64_GOT_TLSLD16_LO:
7894         case R_PPC64_GOT_TLSLD16_HI:
7895         case R_PPC64_GOT_TLSLD16_HA:
7896           tls_type = TLS_TLS | TLS_LD;
7897           goto dogot;
7898
7899         case R_PPC64_GOT_TPREL16_DS:
7900         case R_PPC64_GOT_TPREL16_LO_DS:
7901         case R_PPC64_GOT_TPREL16_HI:
7902         case R_PPC64_GOT_TPREL16_HA:
7903           tls_type = TLS_TLS | TLS_TPREL;
7904           goto dogot;
7905
7906         case R_PPC64_GOT_DTPREL16_DS:
7907         case R_PPC64_GOT_DTPREL16_LO_DS:
7908         case R_PPC64_GOT_DTPREL16_HI:
7909         case R_PPC64_GOT_DTPREL16_HA:
7910           tls_type = TLS_TLS | TLS_DTPREL;
7911           goto dogot;
7912
7913         case R_PPC64_GOT16:
7914         case R_PPC64_GOT16_LO:
7915         case R_PPC64_GOT16_HI:
7916         case R_PPC64_GOT16_HA:
7917         case R_PPC64_GOT16_DS:
7918         case R_PPC64_GOT16_LO_DS:
7919         dogot:
7920           {
7921             /* Relocation is to the entry for this symbol in the global
7922                offset table.  */
7923             bfd_vma *offp;
7924             bfd_vma off;
7925             unsigned long indx = 0;
7926
7927             if (htab->sgot == NULL)
7928               abort ();
7929
7930             if (tls_type == (TLS_TLS | TLS_LD)
7931                 && (h == NULL
7932                     || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
7933               offp = &htab->tlsld_got.offset;
7934             else
7935               {
7936                 struct got_entry *ent;
7937
7938                 if (h != NULL)
7939                   {
7940                     bfd_boolean dyn = htab->elf.dynamic_sections_created;
7941                     if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
7942                         || (info->shared
7943                             && SYMBOL_REFERENCES_LOCAL (info, h)))
7944                       /* This is actually a static link, or it is a
7945                          -Bsymbolic link and the symbol is defined
7946                          locally, or the symbol was forced to be local
7947                          because of a version file.  */
7948                       ;
7949                     else
7950                       {
7951                         indx = h->dynindx;
7952                         unresolved_reloc = FALSE;
7953                       }
7954                     ent = h->got.glist;
7955                   }
7956                 else
7957                   {
7958                     if (local_got_ents == NULL)
7959                       abort ();
7960                     ent = local_got_ents[r_symndx];
7961                   }
7962
7963                 for (; ent != NULL; ent = ent->next)
7964                   if (ent->addend == rel->r_addend
7965                       && ent->tls_type == tls_type)
7966                     break;
7967                 if (ent == NULL)
7968                   abort ();
7969                 offp = &ent->got.offset;
7970               }
7971
7972             /* The offset must always be a multiple of 8.  We use the
7973                least significant bit to record whether we have already
7974                processed this entry.  */
7975             off = *offp;
7976             if ((off & 1) != 0)
7977               off &= ~1;
7978             else
7979               {
7980                 /* Generate relocs for the dynamic linker, except in
7981                    the case of TLSLD where we'll use one entry per
7982                    module.  */
7983                 *offp = off | 1;
7984                 if ((info->shared || indx != 0)
7985                     && (h == NULL
7986                         || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7987                         || h->root.type != bfd_link_hash_undefweak))
7988                   {
7989                     outrel.r_offset = (htab->sgot->output_section->vma
7990                                        + htab->sgot->output_offset
7991                                        + off);
7992                     outrel.r_addend = rel->r_addend;
7993                     if (tls_type & (TLS_LD | TLS_GD))
7994                       {
7995                         outrel.r_addend = 0;
7996                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
7997                         if (tls_type == (TLS_TLS | TLS_GD))
7998                           {
7999                             loc = htab->srelgot->contents;
8000                             loc += (htab->srelgot->reloc_count++
8001                                     * sizeof (Elf64_External_Rela));
8002                             bfd_elf64_swap_reloca_out (output_bfd,
8003                                                        &outrel, loc);
8004                             outrel.r_offset += 8;
8005                             outrel.r_addend = rel->r_addend;
8006                             outrel.r_info
8007                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
8008                           }
8009                       }
8010                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
8011                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
8012                     else if (tls_type == (TLS_TLS | TLS_TPREL))
8013                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
8014                     else if (indx == 0)
8015                       {
8016                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_RELATIVE);
8017
8018                         /* Write the .got section contents for the sake
8019                            of prelink.  */
8020                         loc = htab->sgot->contents + off;
8021                         bfd_put_64 (output_bfd, outrel.r_addend, loc);
8022                       }
8023                     else
8024                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
8025
8026                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
8027                       {
8028                         outrel.r_addend += relocation;
8029                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
8030                           outrel.r_addend -= htab->tls_sec->vma;
8031                       }
8032                     loc = htab->srelgot->contents;
8033                     loc += (htab->srelgot->reloc_count++
8034                             * sizeof (Elf64_External_Rela));
8035                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
8036                   }
8037
8038                 /* Init the .got section contents here if we're not
8039                    emitting a reloc.  */
8040                 else
8041                   {
8042                     relocation += rel->r_addend;
8043                     if (tls_type == (TLS_TLS | TLS_LD))
8044                       relocation = 1;
8045                     else if (tls_type != 0)
8046                       {
8047                         relocation -= htab->tls_sec->vma + DTP_OFFSET;
8048                         if (tls_type == (TLS_TLS | TLS_TPREL))
8049                           relocation += DTP_OFFSET - TP_OFFSET;
8050
8051                         if (tls_type == (TLS_TLS | TLS_GD))
8052                           {
8053                             bfd_put_64 (output_bfd, relocation,
8054                                         htab->sgot->contents + off + 8);
8055                             relocation = 1;
8056                           }
8057                       }
8058
8059                     bfd_put_64 (output_bfd, relocation,
8060                                 htab->sgot->contents + off);
8061                   }
8062               }
8063
8064             if (off >= (bfd_vma) -2)
8065               abort ();
8066
8067             relocation = htab->sgot->output_offset + off;
8068
8069             /* TOC base (r2) is TOC start plus 0x8000.  */
8070             addend = - TOC_BASE_OFF;
8071           }
8072           break;
8073
8074         case R_PPC64_PLT16_HA:
8075         case R_PPC64_PLT16_HI:
8076         case R_PPC64_PLT16_LO:
8077         case R_PPC64_PLT32:
8078         case R_PPC64_PLT64:
8079           /* Relocation is to the entry for this symbol in the
8080              procedure linkage table.  */
8081
8082           /* Resolve a PLT reloc against a local symbol directly,
8083              without using the procedure linkage table.  */
8084           if (h == NULL)
8085             break;
8086
8087           /* It's possible that we didn't make a PLT entry for this
8088              symbol.  This happens when statically linking PIC code,
8089              or when using -Bsymbolic.  Go find a match if there is a
8090              PLT entry.  */
8091           if (htab->splt != NULL)
8092             {
8093               struct plt_entry *ent;
8094               for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8095                 if (ent->addend == rel->r_addend
8096                     && ent->plt.offset != (bfd_vma) -1)
8097                   {
8098                     relocation = (htab->splt->output_section->vma
8099                                   + htab->splt->output_offset
8100                                   + ent->plt.offset);
8101                     unresolved_reloc = FALSE;
8102                   }
8103             }
8104           break;
8105
8106           /* TOC16 relocs.  We want the offset relative to the TOC base,
8107              which is the address of the start of the TOC plus 0x8000.
8108              The TOC consists of sections .got, .toc, .tocbss, and .plt,
8109              in this order.  */
8110         case R_PPC64_TOC16:
8111         case R_PPC64_TOC16_LO:
8112         case R_PPC64_TOC16_HI:
8113         case R_PPC64_TOC16_DS:
8114         case R_PPC64_TOC16_LO_DS:
8115         case R_PPC64_TOC16_HA:
8116           addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
8117           break;
8118
8119           /* Relocate against the beginning of the section.  */
8120         case R_PPC64_SECTOFF:
8121         case R_PPC64_SECTOFF_LO:
8122         case R_PPC64_SECTOFF_HI:
8123         case R_PPC64_SECTOFF_DS:
8124         case R_PPC64_SECTOFF_LO_DS:
8125         case R_PPC64_SECTOFF_HA:
8126           if (sec != (asection *) 0)
8127             addend -= sec->output_section->vma;
8128           break;
8129
8130         case R_PPC64_REL14:
8131         case R_PPC64_REL14_BRNTAKEN:
8132         case R_PPC64_REL14_BRTAKEN:
8133         case R_PPC64_REL24:
8134           break;
8135
8136         case R_PPC64_TPREL16:
8137         case R_PPC64_TPREL16_LO:
8138         case R_PPC64_TPREL16_HI:
8139         case R_PPC64_TPREL16_HA:
8140         case R_PPC64_TPREL16_DS:
8141         case R_PPC64_TPREL16_LO_DS:
8142         case R_PPC64_TPREL16_HIGHER:
8143         case R_PPC64_TPREL16_HIGHERA:
8144         case R_PPC64_TPREL16_HIGHEST:
8145         case R_PPC64_TPREL16_HIGHESTA:
8146           addend -= htab->tls_sec->vma + TP_OFFSET;
8147           if (info->shared)
8148             /* The TPREL16 relocs shouldn't really be used in shared
8149                libs as they will result in DT_TEXTREL being set, but
8150                support them anyway.  */
8151             goto dodyn;
8152           break;
8153
8154         case R_PPC64_DTPREL16:
8155         case R_PPC64_DTPREL16_LO:
8156         case R_PPC64_DTPREL16_HI:
8157         case R_PPC64_DTPREL16_HA:
8158         case R_PPC64_DTPREL16_DS:
8159         case R_PPC64_DTPREL16_LO_DS:
8160         case R_PPC64_DTPREL16_HIGHER:
8161         case R_PPC64_DTPREL16_HIGHERA:
8162         case R_PPC64_DTPREL16_HIGHEST:
8163         case R_PPC64_DTPREL16_HIGHESTA:
8164           addend -= htab->tls_sec->vma + DTP_OFFSET;
8165           break;
8166
8167         case R_PPC64_DTPMOD64:
8168           relocation = 1;
8169           addend = 0;
8170           goto dodyn;
8171
8172         case R_PPC64_TPREL64:
8173           addend -= htab->tls_sec->vma + TP_OFFSET;
8174           goto dodyn;
8175
8176         case R_PPC64_DTPREL64:
8177           addend -= htab->tls_sec->vma + DTP_OFFSET;
8178           /* Fall thru */
8179
8180           /* Relocations that may need to be propagated if this is a
8181              dynamic object.  */
8182         case R_PPC64_REL30:
8183         case R_PPC64_REL32:
8184         case R_PPC64_REL64:
8185         case R_PPC64_ADDR14:
8186         case R_PPC64_ADDR14_BRNTAKEN:
8187         case R_PPC64_ADDR14_BRTAKEN:
8188         case R_PPC64_ADDR16:
8189         case R_PPC64_ADDR16_DS:
8190         case R_PPC64_ADDR16_HA:
8191         case R_PPC64_ADDR16_HI:
8192         case R_PPC64_ADDR16_HIGHER:
8193         case R_PPC64_ADDR16_HIGHERA:
8194         case R_PPC64_ADDR16_HIGHEST:
8195         case R_PPC64_ADDR16_HIGHESTA:
8196         case R_PPC64_ADDR16_LO:
8197         case R_PPC64_ADDR16_LO_DS:
8198         case R_PPC64_ADDR24:
8199         case R_PPC64_ADDR32:
8200         case R_PPC64_ADDR64:
8201         case R_PPC64_UADDR16:
8202         case R_PPC64_UADDR32:
8203         case R_PPC64_UADDR64:
8204           /* r_symndx will be zero only for relocs against symbols
8205              from removed linkonce sections, or sections discarded by
8206              a linker script.  */
8207         dodyn:
8208           if (r_symndx == 0)
8209             break;
8210           /* Fall thru.  */
8211
8212         case R_PPC64_TOC:
8213           if ((input_section->flags & SEC_ALLOC) == 0)
8214             break;
8215
8216           if (NO_OPD_RELOCS && is_opd)
8217             break;
8218
8219           if ((info->shared
8220                && (h == NULL
8221                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8222                    || h->root.type != bfd_link_hash_undefweak)
8223                && (MUST_BE_DYN_RELOC (r_type)
8224                    || (h != NULL
8225                        && !SYMBOL_CALLS_LOCAL (info, h))))
8226               || (ELIMINATE_COPY_RELOCS
8227                   && !info->shared
8228                   && h != NULL
8229                   && h->dynindx != -1
8230                   && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
8231                   && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
8232                   && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
8233             {
8234               Elf_Internal_Rela outrel;
8235               bfd_boolean skip, relocate;
8236               asection *sreloc;
8237               bfd_byte *loc;
8238
8239               /* When generating a dynamic object, these relocations
8240                  are copied into the output file to be resolved at run
8241                  time.  */
8242
8243               skip = FALSE;
8244               relocate = FALSE;
8245
8246               outrel.r_offset =
8247                 _bfd_elf_section_offset (output_bfd, info, input_section,
8248                                          rel->r_offset);
8249               if (outrel.r_offset == (bfd_vma) -1)
8250                 skip = TRUE;
8251               else if (outrel.r_offset == (bfd_vma) -2)
8252                 skip = TRUE, relocate = TRUE;
8253               outrel.r_offset += (input_section->output_section->vma
8254                                   + input_section->output_offset);
8255               outrel.r_addend = rel->r_addend;
8256
8257               if (skip)
8258                 memset (&outrel, 0, sizeof outrel);
8259               else if (h != NULL
8260                        && !SYMBOL_REFERENCES_LOCAL (info, h)
8261                        && !is_opd)
8262                 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
8263               else
8264                 {
8265                   /* This symbol is local, or marked to become local,
8266                      or this is an opd section reloc which must point
8267                      at a local function.  */
8268                   outrel.r_addend += relocation;
8269                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
8270                     {
8271                       if (is_opd && h != NULL)
8272                         {
8273                           /* Lie about opd entries.  This case occurs
8274                              when building shared libraries and we
8275                              reference a function in another shared
8276                              lib.  The same thing happens for a weak
8277                              definition in an application that's
8278                              overridden by a strong definition in a
8279                              shared lib.  (I believe this is a generic
8280                              bug in binutils handling of weak syms.)
8281                              In these cases we won't use the opd
8282                              entry in this lib.  */
8283                           unresolved_reloc = FALSE;
8284                         }
8285                       outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
8286
8287                       /* We need to relocate .opd contents for ld.so.
8288                          Prelink also wants simple and consistent rules
8289                          for relocs.  This make all RELATIVE relocs have
8290                          *r_offset equal to r_addend.  */
8291                       relocate = TRUE;
8292                     }
8293                   else
8294                     {
8295                       long indx = 0;
8296
8297                       if (bfd_is_abs_section (sec))
8298                         ;
8299                       else if (sec == NULL || sec->owner == NULL)
8300                         {
8301                           bfd_set_error (bfd_error_bad_value);
8302                           return FALSE;
8303                         }
8304                       else
8305                         {
8306                           asection *osec;
8307
8308                           osec = sec->output_section;
8309                           indx = elf_section_data (osec)->dynindx;
8310
8311                           /* We are turning this relocation into one
8312                              against a section symbol, so subtract out
8313                              the output section's address but not the
8314                              offset of the input section in the output
8315                              section.  */
8316                           outrel.r_addend -= osec->vma;
8317                         }
8318
8319                       outrel.r_info = ELF64_R_INFO (indx, r_type);
8320                     }
8321                 }
8322
8323               sreloc = elf_section_data (input_section)->sreloc;
8324               if (sreloc == NULL)
8325                 abort ();
8326
8327               loc = sreloc->contents;
8328               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
8329               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
8330
8331               /* If this reloc is against an external symbol, it will
8332                  be computed at runtime, so there's no need to do
8333                  anything now.  However, for the sake of prelink ensure
8334                  that the section contents are a known value.  */
8335               if (! relocate)
8336                 {
8337                   unresolved_reloc = FALSE;
8338                   /* The value chosen here is quite arbitrary as ld.so
8339                      ignores section contents except for the special
8340                      case of .opd where the contents might be accessed
8341                      before relocation.  Choose zero, as that won't
8342                      cause reloc overflow.  */
8343                   relocation = 0;
8344                   addend = 0;
8345                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
8346                      to improve backward compatibility with older
8347                      versions of ld.  */
8348                   if (r_type == R_PPC64_ADDR64)
8349                     addend = outrel.r_addend;
8350                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
8351                   else if (ppc64_elf_howto_table[(int) r_type]->pc_relative)
8352                     addend = (input_section->output_section->vma
8353                               + input_section->output_offset
8354                               + rel->r_offset);
8355                 }
8356             }
8357           break;
8358
8359         case R_PPC64_COPY:
8360         case R_PPC64_GLOB_DAT:
8361         case R_PPC64_JMP_SLOT:
8362         case R_PPC64_RELATIVE:
8363           /* We shouldn't ever see these dynamic relocs in relocatable
8364              files.  */
8365           /* Fall through.  */
8366
8367         case R_PPC64_PLTGOT16:
8368         case R_PPC64_PLTGOT16_DS:
8369         case R_PPC64_PLTGOT16_HA:
8370         case R_PPC64_PLTGOT16_HI:
8371         case R_PPC64_PLTGOT16_LO:
8372         case R_PPC64_PLTGOT16_LO_DS:
8373         case R_PPC64_PLTREL32:
8374         case R_PPC64_PLTREL64:
8375           /* These ones haven't been implemented yet.  */
8376
8377           (*_bfd_error_handler)
8378             (_("%s: relocation %s is not supported for symbol %s."),
8379              bfd_archive_filename (input_bfd),
8380              ppc64_elf_howto_table[(int) r_type]->name, sym_name);
8381
8382           bfd_set_error (bfd_error_invalid_operation);
8383           ret = FALSE;
8384           continue;
8385         }
8386
8387       /* Do any further special processing.  */
8388       switch (r_type)
8389         {
8390         default:
8391           break;
8392
8393         case R_PPC64_ADDR16_HA:
8394         case R_PPC64_ADDR16_HIGHERA:
8395         case R_PPC64_ADDR16_HIGHESTA:
8396         case R_PPC64_GOT16_HA:
8397         case R_PPC64_PLTGOT16_HA:
8398         case R_PPC64_PLT16_HA:
8399         case R_PPC64_TOC16_HA:
8400         case R_PPC64_SECTOFF_HA:
8401         case R_PPC64_TPREL16_HA:
8402         case R_PPC64_DTPREL16_HA:
8403         case R_PPC64_GOT_TLSGD16_HA:
8404         case R_PPC64_GOT_TLSLD16_HA:
8405         case R_PPC64_GOT_TPREL16_HA:
8406         case R_PPC64_GOT_DTPREL16_HA:
8407         case R_PPC64_TPREL16_HIGHER:
8408         case R_PPC64_TPREL16_HIGHERA:
8409         case R_PPC64_TPREL16_HIGHEST:
8410         case R_PPC64_TPREL16_HIGHESTA:
8411         case R_PPC64_DTPREL16_HIGHER:
8412         case R_PPC64_DTPREL16_HIGHERA:
8413         case R_PPC64_DTPREL16_HIGHEST:
8414         case R_PPC64_DTPREL16_HIGHESTA:
8415           /* It's just possible that this symbol is a weak symbol
8416              that's not actually defined anywhere. In that case,
8417              'sec' would be NULL, and we should leave the symbol
8418              alone (it will be set to zero elsewhere in the link).  */
8419           if (sec != NULL)
8420             /* Add 0x10000 if sign bit in 0:15 is set.
8421                Bits 0:15 are not used.  */
8422             addend += 0x8000;
8423           break;
8424
8425         case R_PPC64_ADDR16_DS:
8426         case R_PPC64_ADDR16_LO_DS:
8427         case R_PPC64_GOT16_DS:
8428         case R_PPC64_GOT16_LO_DS:
8429         case R_PPC64_PLT16_LO_DS:
8430         case R_PPC64_SECTOFF_DS:
8431         case R_PPC64_SECTOFF_LO_DS:
8432         case R_PPC64_TOC16_DS:
8433         case R_PPC64_TOC16_LO_DS:
8434         case R_PPC64_PLTGOT16_DS:
8435         case R_PPC64_PLTGOT16_LO_DS:
8436         case R_PPC64_GOT_TPREL16_DS:
8437         case R_PPC64_GOT_TPREL16_LO_DS:
8438         case R_PPC64_GOT_DTPREL16_DS:
8439         case R_PPC64_GOT_DTPREL16_LO_DS:
8440         case R_PPC64_TPREL16_DS:
8441         case R_PPC64_TPREL16_LO_DS:
8442         case R_PPC64_DTPREL16_DS:
8443         case R_PPC64_DTPREL16_LO_DS:
8444           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
8445           mask = 3;
8446           /* If this reloc is against an lq insn, then the value must be
8447              a multiple of 16.  This is somewhat of a hack, but the
8448              "correct" way to do this by defining _DQ forms of all the
8449              _DS relocs bloats all reloc switches in this file.  It
8450              doesn't seem to make much sense to use any of these relocs
8451              in data, so testing the insn should be safe.  */
8452           if ((insn & (0x3f << 26)) == (56 << 26))
8453             mask = 15;
8454           if (((relocation + addend) & mask) != 0)
8455             {
8456               (*_bfd_error_handler)
8457                 (_("%s: error: relocation %s not a multiple of %d"),
8458                  bfd_archive_filename (input_bfd),
8459                  ppc64_elf_howto_table[(int) r_type]->name,
8460                  mask + 1);
8461               bfd_set_error (bfd_error_bad_value);
8462               ret = FALSE;
8463               continue;
8464             }
8465           break;
8466
8467         case R_PPC64_REL14:
8468         case R_PPC64_REL14_BRNTAKEN:
8469         case R_PPC64_REL14_BRTAKEN:
8470           max_br_offset = 1 << 15;
8471           goto branch_check;
8472
8473         case R_PPC64_REL24:
8474           max_br_offset = 1 << 25;
8475
8476         branch_check:
8477           /* If the branch is out of reach or the TOC register needs
8478              adjusting, then redirect the call to the local stub for
8479              this function.  */
8480           from = (rel->r_offset
8481                   + input_section->output_offset
8482                   + input_section->output_section->vma);
8483           if ((relocation + addend - from + max_br_offset >= 2 * max_br_offset
8484                || (sec != NULL
8485                    && sec->output_section != NULL
8486                    && (htab->stub_group[sec->id].toc_off
8487                        != htab->stub_group[input_section->id].toc_off)))
8488               && (stub_entry = ppc_get_stub_entry (input_section, sec, h,
8489                                                    rel, htab)) != NULL)
8490             {
8491               /* Munge up the value and addend so that we call the stub
8492                  rather than the procedure directly.  */
8493               relocation = (stub_entry->stub_offset
8494                             + stub_entry->stub_sec->output_offset
8495                             + stub_entry->stub_sec->output_section->vma);
8496               addend = 0;
8497             }
8498           break;
8499         }
8500
8501       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
8502          because such sections are not SEC_ALLOC and thus ld.so will
8503          not process them.  */
8504       if (unresolved_reloc
8505           && !((input_section->flags & SEC_DEBUGGING) != 0
8506                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
8507         {
8508           (*_bfd_error_handler)
8509             (_("%s(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
8510              bfd_archive_filename (input_bfd),
8511              bfd_get_section_name (input_bfd, input_section),
8512              (long) rel->r_offset,
8513              ppc64_elf_howto_table[(int) r_type]->name,
8514              h->root.root.string);
8515           ret = FALSE;
8516         }
8517
8518       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
8519                                     input_bfd,
8520                                     input_section,
8521                                     contents,
8522                                     rel->r_offset,
8523                                     relocation,
8524                                     addend);
8525
8526       if (r != bfd_reloc_ok)
8527         {
8528           if (sym_name == NULL)
8529             sym_name = "(null)";
8530           if (r == bfd_reloc_overflow)
8531             {
8532               if (warned)
8533                 continue;
8534               if (h != NULL
8535                   && h->root.type == bfd_link_hash_undefweak
8536                   && ppc64_elf_howto_table[(int) r_type]->pc_relative)
8537                 {
8538                   /* Assume this is a call protected by other code that
8539                      detects the symbol is undefined.  If this is the case,
8540                      we can safely ignore the overflow.  If not, the
8541                      program is hosed anyway, and a little warning isn't
8542                      going to help.  */
8543
8544                   continue;
8545                 }
8546
8547               if (!((*info->callbacks->reloc_overflow)
8548                     (info, sym_name, ppc64_elf_howto_table[(int) r_type]->name,
8549                      rel->r_addend, input_bfd, input_section, rel->r_offset)))
8550                 return FALSE;
8551             }
8552           else
8553             {
8554               (*_bfd_error_handler)
8555                 (_("%s(%s+0x%lx): %s reloc against `%s': error %d"),
8556                  bfd_archive_filename (input_bfd),
8557                  bfd_get_section_name (input_bfd, input_section),
8558                  (long) rel->r_offset,
8559                  ppc64_elf_howto_table[(int) r_type]->name,
8560                  sym_name,
8561                  (int) r);
8562               ret = FALSE;
8563             }
8564         }
8565     }
8566
8567   return ret;
8568 }
8569
8570 /* Finish up dynamic symbol handling.  We set the contents of various
8571    dynamic sections here.  */
8572
8573 static bfd_boolean
8574 ppc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
8575      bfd *output_bfd;
8576      struct bfd_link_info *info;
8577      struct elf_link_hash_entry *h;
8578      Elf_Internal_Sym *sym;
8579 {
8580   struct ppc_link_hash_table *htab;
8581   bfd *dynobj;
8582
8583   htab = ppc_hash_table (info);
8584   dynobj = htab->elf.dynobj;
8585
8586   if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
8587     {
8588       struct plt_entry *ent;
8589       Elf_Internal_Rela rela;
8590       bfd_byte *loc;
8591
8592       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8593         if (ent->plt.offset != (bfd_vma) -1)
8594           {
8595             /* This symbol has an entry in the procedure linkage
8596                table.  Set it up.  */
8597
8598             if (htab->splt == NULL
8599                 || htab->srelplt == NULL
8600                 || htab->sglink == NULL)
8601               abort ();
8602
8603             /* Create a JMP_SLOT reloc to inform the dynamic linker to
8604                fill in the PLT entry.  */
8605             rela.r_offset = (htab->splt->output_section->vma
8606                              + htab->splt->output_offset
8607                              + ent->plt.offset);
8608             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
8609             rela.r_addend = ent->addend;
8610
8611             loc = htab->srelplt->contents;
8612             loc += ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
8613                     * sizeof (Elf64_External_Rela));
8614             bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
8615           }
8616     }
8617
8618   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
8619     {
8620       Elf_Internal_Rela rela;
8621       bfd_byte *loc;
8622
8623       /* This symbol needs a copy reloc.  Set it up.  */
8624
8625       if (h->dynindx == -1
8626           || (h->root.type != bfd_link_hash_defined
8627               && h->root.type != bfd_link_hash_defweak)
8628           || htab->srelbss == NULL)
8629         abort ();
8630
8631       rela.r_offset = (h->root.u.def.value
8632                        + h->root.u.def.section->output_section->vma
8633                        + h->root.u.def.section->output_offset);
8634       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
8635       rela.r_addend = 0;
8636       loc = htab->srelbss->contents;
8637       loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
8638       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
8639     }
8640
8641   /* Mark some specially defined symbols as absolute.  */
8642   if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
8643     sym->st_shndx = SHN_ABS;
8644
8645   return TRUE;
8646 }
8647
8648 /* Used to decide how to sort relocs in an optimal manner for the
8649    dynamic linker, before writing them out.  */
8650
8651 static enum elf_reloc_type_class
8652 ppc64_elf_reloc_type_class (rela)
8653      const Elf_Internal_Rela *rela;
8654 {
8655   enum elf_ppc64_reloc_type r_type;
8656
8657   r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rela->r_info);
8658   switch (r_type)
8659     {
8660     case R_PPC64_RELATIVE:
8661       return reloc_class_relative;
8662     case R_PPC64_JMP_SLOT:
8663       return reloc_class_plt;
8664     case R_PPC64_COPY:
8665       return reloc_class_copy;
8666     default:
8667       return reloc_class_normal;
8668     }
8669 }
8670
8671 /* Finish up the dynamic sections.  */
8672
8673 static bfd_boolean
8674 ppc64_elf_finish_dynamic_sections (output_bfd, info)
8675      bfd *output_bfd;
8676      struct bfd_link_info *info;
8677 {
8678   struct ppc_link_hash_table *htab;
8679   bfd *dynobj;
8680   asection *sdyn;
8681
8682   htab = ppc_hash_table (info);
8683   dynobj = htab->elf.dynobj;
8684   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8685
8686   if (htab->elf.dynamic_sections_created)
8687     {
8688       Elf64_External_Dyn *dyncon, *dynconend;
8689
8690       if (sdyn == NULL || htab->sgot == NULL)
8691         abort ();
8692
8693       dyncon = (Elf64_External_Dyn *) sdyn->contents;
8694       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
8695       for (; dyncon < dynconend; dyncon++)
8696         {
8697           Elf_Internal_Dyn dyn;
8698           asection *s;
8699
8700           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
8701
8702           switch (dyn.d_tag)
8703             {
8704             default:
8705               continue;
8706
8707             case DT_PPC64_GLINK:
8708               s = htab->sglink;
8709               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8710               /* We stupidly defined DT_PPC64_GLINK to be the start
8711                  of glink rather than the first entry point, which is
8712                  what ld.so needs, and now have a bigger stub to
8713                  support automatic multiple TOCs.  */
8714               dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
8715               break;
8716
8717             case DT_PPC64_OPD:
8718               s = bfd_get_section_by_name (output_bfd, ".opd");
8719               if (s == NULL)
8720                 continue;
8721               dyn.d_un.d_ptr = s->vma;
8722               break;
8723
8724             case DT_PPC64_OPDSZ:
8725               s = bfd_get_section_by_name (output_bfd, ".opd");
8726               if (s == NULL)
8727                 continue;
8728               dyn.d_un.d_val = s->_raw_size;
8729               break;
8730
8731             case DT_PLTGOT:
8732               s = htab->splt;
8733               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8734               break;
8735
8736             case DT_JMPREL:
8737               s = htab->srelplt;
8738               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8739               break;
8740
8741             case DT_PLTRELSZ:
8742               dyn.d_un.d_val = htab->srelplt->_raw_size;
8743               break;
8744
8745             case DT_RELASZ:
8746               /* Don't count procedure linkage table relocs in the
8747                  overall reloc count.  */
8748               s = htab->srelplt;
8749               if (s == NULL)
8750                 continue;
8751               dyn.d_un.d_val -= s->_raw_size;
8752               break;
8753
8754             case DT_RELA:
8755               /* We may not be using the standard ELF linker script.
8756                  If .rela.plt is the first .rela section, we adjust
8757                  DT_RELA to not include it.  */
8758               s = htab->srelplt;
8759               if (s == NULL)
8760                 continue;
8761               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
8762                 continue;
8763               dyn.d_un.d_ptr += s->_raw_size;
8764               break;
8765             }
8766
8767           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
8768         }
8769     }
8770
8771   if (htab->sgot != NULL && htab->sgot->_raw_size != 0)
8772     {
8773       /* Fill in the first entry in the global offset table.
8774          We use it to hold the link-time TOCbase.  */
8775       bfd_put_64 (output_bfd,
8776                   elf_gp (output_bfd) + TOC_BASE_OFF,
8777                   htab->sgot->contents);
8778
8779       /* Set .got entry size.  */
8780       elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 8;
8781     }
8782
8783   if (htab->splt != NULL && htab->splt->_raw_size != 0)
8784     {
8785       /* Set .plt entry size.  */
8786       elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize
8787         = PLT_ENTRY_SIZE;
8788     }
8789
8790   return TRUE;
8791 }
8792
8793 #include "elf64-target.h"