* elf64-ppc.c (ppc64_elf_info_to_howto): Formatting.
[external/binutils.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright 1999, 2000, 2001, 2002 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/ppc.h"
31 #include "elf64-ppc.h"
32
33 #define USE_RELA                /* we want RELA relocations, not REL.  */
34
35
36 static void ppc_howto_init
37   PARAMS ((void));
38 static reloc_howto_type *ppc64_elf_reloc_type_lookup
39   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
40 static void ppc64_elf_info_to_howto
41   PARAMS ((bfd *abfd, arelent *cache_ptr, Elf64_Internal_Rela *dst));
42 static bfd_reloc_status_type ppc64_elf_ha_reloc
43   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
44 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
45   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
46 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
47   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
48 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
49   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
50 static bfd_reloc_status_type ppc64_elf_toc_reloc
51   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
52 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
53   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
54 static bfd_reloc_status_type ppc64_elf_toc64_reloc
55   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
56 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
57   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
58 static boolean ppc64_elf_set_private_flags
59   PARAMS ((bfd *, flagword));
60 static boolean ppc64_elf_merge_private_bfd_data
61   PARAMS ((bfd *, bfd *));
62 static boolean ppc64_elf_section_from_shdr
63   PARAMS ((bfd *, Elf64_Internal_Shdr *, char *));
64
65
66 /* The name of the dynamic interpreter.  This is put in the .interp
67    section.  */
68 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
69
70 /* The size in bytes of an entry in the procedure linkage table.  */
71 #define PLT_ENTRY_SIZE 24
72
73 /* The initial size of the plt reserved for the dynamic linker.  */
74 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
75
76 /* TOC base pointers offset from start of TOC.  */
77 #define TOC_BASE_OFF (0x8000)
78
79 /* .plt call stub instructions.  */
80 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha     */
81 #define STD_R2_40R1     0xf8410028      /* std   %r2,40(%r1)         */
82 #define LD_R11_0R12     0xe96c0000      /* ld    %r11,xxx+0@l(%r12)  */
83 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,xxx+8@l(%r12)   */
84 #define MTCTR_R11       0x7d6903a6      /* mtctr %r11                */
85                                         /* ld    %r11,xxx+16@l(%r12) */
86 #define BCTR            0x4e800420      /* bctr                      */
87
88 /* The normal stub is this size.  */
89 #define PLT_CALL_STUB_SIZE (7*4)
90
91 /* But sometimes the .plt entry crosses a 64k boundary, and we need
92    to adjust the high word with this insn.  */
93 #define ADDIS_R12_R12_1 0x3d8c0001      /* addis %r12,%r12,1    */
94
95 /* The .glink fixup call stub is the same as the .plt call stub, but
96    the first instruction restores r2, and the std is omitted.  */
97 #define LD_R2_40R1      0xe8410028      /* ld    %r2,40(%r1)    */
98
99 /* Always allow this much space.  */
100 #define GLINK_CALL_STUB_SIZE (8*4)
101
102 /* Pad with this.  */
103 #define NOP             0x60000000
104
105 /* Some other nops.  */
106 #define CROR_151515     0x4def7b82
107 #define CROR_313131     0x4ffffb82
108
109 /* .glink entries for the first 32k functions are two instructions. */
110 #define LI_R0_0         0x38000000      /* li    %r0,0          */
111 #define B_DOT           0x48000000      /* b     .              */
112
113 /* After that, we need two instructions to load the index, followed by
114    a branch.  */
115 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
116 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
117
118 /* Instructions to save and restore floating point regs.  */
119 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
120 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
121 #define BLR             0x4e800020      /* blr                  */
122
123 /* Since .opd is an array of descriptors and each entry will end up
124    with identical R_PPC64_RELATIVE relocs, there is really no need to
125    propagate .opd relocs;  The dynamic linker should be taught to
126    relocate .opd without reloc entries.  FIXME: .opd should be trimmed
127    of unused values.  */
128 #ifndef NO_OPD_RELOCS
129 #define NO_OPD_RELOCS 0
130 #endif
131 \f
132 /* Relocation HOWTO's.  */
133 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC_max];
134
135 static reloc_howto_type ppc64_elf_howto_raw[] = {
136   /* This reloc does nothing.  */
137   HOWTO (R_PPC64_NONE,          /* type */
138          0,                     /* rightshift */
139          2,                     /* size (0 = byte, 1 = short, 2 = long) */
140          32,                    /* bitsize */
141          false,                 /* pc_relative */
142          0,                     /* bitpos */
143          complain_overflow_bitfield, /* complain_on_overflow */
144          bfd_elf_generic_reloc, /* special_function */
145          "R_PPC64_NONE",        /* name */
146          false,                 /* partial_inplace */
147          0,                     /* src_mask */
148          0,                     /* dst_mask */
149          false),                /* pcrel_offset */
150
151   /* A standard 32 bit relocation.  */
152   HOWTO (R_PPC64_ADDR32,        /* type */
153          0,                     /* rightshift */
154          2,                     /* size (0 = byte, 1 = short, 2 = long) */
155          32,                    /* bitsize */
156          false,                 /* pc_relative */
157          0,                     /* bitpos */
158          complain_overflow_bitfield, /* complain_on_overflow */
159          bfd_elf_generic_reloc, /* special_function */
160          "R_PPC64_ADDR32",      /* name */
161          false,                 /* partial_inplace */
162          0,                     /* src_mask */
163          0xffffffff,            /* dst_mask */
164          false),                /* pcrel_offset */
165
166   /* An absolute 26 bit branch; the lower two bits must be zero.
167      FIXME: we don't check that, we just clear them.  */
168   HOWTO (R_PPC64_ADDR24,        /* type */
169          0,                     /* rightshift */
170          2,                     /* size (0 = byte, 1 = short, 2 = long) */
171          26,                    /* bitsize */
172          false,                 /* pc_relative */
173          0,                     /* bitpos */
174          complain_overflow_bitfield, /* complain_on_overflow */
175          bfd_elf_generic_reloc, /* special_function */
176          "R_PPC64_ADDR24",      /* name */
177          false,                 /* partial_inplace */
178          0,                     /* src_mask */
179          0x3fffffc,             /* dst_mask */
180          false),                /* pcrel_offset */
181
182   /* A standard 16 bit relocation.  */
183   HOWTO (R_PPC64_ADDR16,        /* type */
184          0,                     /* rightshift */
185          1,                     /* size (0 = byte, 1 = short, 2 = long) */
186          16,                    /* bitsize */
187          false,                 /* pc_relative */
188          0,                     /* bitpos */
189          complain_overflow_bitfield, /* complain_on_overflow */
190          bfd_elf_generic_reloc, /* special_function */
191          "R_PPC64_ADDR16",      /* name */
192          false,                 /* partial_inplace */
193          0,                     /* src_mask */
194          0xffff,                /* dst_mask */
195          false),                /* pcrel_offset */
196
197   /* A 16 bit relocation without overflow.  */
198   HOWTO (R_PPC64_ADDR16_LO,     /* type */
199          0,                     /* rightshift */
200          1,                     /* size (0 = byte, 1 = short, 2 = long) */
201          16,                    /* bitsize */
202          false,                 /* pc_relative */
203          0,                     /* bitpos */
204          complain_overflow_dont,/* complain_on_overflow */
205          bfd_elf_generic_reloc, /* special_function */
206          "R_PPC64_ADDR16_LO",   /* name */
207          false,                 /* partial_inplace */
208          0,                     /* src_mask */
209          0xffff,                /* dst_mask */
210          false),                /* pcrel_offset */
211
212   /* Bits 16-31 of an address.  */
213   HOWTO (R_PPC64_ADDR16_HI,     /* type */
214          16,                    /* rightshift */
215          1,                     /* size (0 = byte, 1 = short, 2 = long) */
216          16,                    /* bitsize */
217          false,                 /* pc_relative */
218          0,                     /* bitpos */
219          complain_overflow_dont, /* complain_on_overflow */
220          bfd_elf_generic_reloc, /* special_function */
221          "R_PPC64_ADDR16_HI",   /* name */
222          false,                 /* partial_inplace */
223          0,                     /* src_mask */
224          0xffff,                /* dst_mask */
225          false),                /* pcrel_offset */
226
227   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
228      bits, treated as a signed number, is negative.  */
229   HOWTO (R_PPC64_ADDR16_HA,     /* type */
230          16,                    /* rightshift */
231          1,                     /* size (0 = byte, 1 = short, 2 = long) */
232          16,                    /* bitsize */
233          false,                 /* pc_relative */
234          0,                     /* bitpos */
235          complain_overflow_dont, /* complain_on_overflow */
236          ppc64_elf_ha_reloc,    /* special_function */
237          "R_PPC64_ADDR16_HA",   /* name */
238          false,                 /* partial_inplace */
239          0,                     /* src_mask */
240          0xffff,                /* dst_mask */
241          false),                /* pcrel_offset */
242
243   /* An absolute 16 bit branch; the lower two bits must be zero.
244      FIXME: we don't check that, we just clear them.  */
245   HOWTO (R_PPC64_ADDR14,        /* type */
246          0,                     /* rightshift */
247          2,                     /* size (0 = byte, 1 = short, 2 = long) */
248          16,                    /* bitsize */
249          false,                 /* pc_relative */
250          0,                     /* bitpos */
251          complain_overflow_bitfield, /* complain_on_overflow */
252          bfd_elf_generic_reloc, /* special_function */
253          "R_PPC64_ADDR14",      /* name */
254          false,                 /* partial_inplace */
255          0,                     /* src_mask */
256          0xfffc,                /* dst_mask */
257          false),                /* pcrel_offset */
258
259   /* An absolute 16 bit branch, for which bit 10 should be set to
260      indicate that the branch is expected to be taken.  The lower two
261      bits must be zero.  */
262   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
263          0,                     /* rightshift */
264          2,                     /* size (0 = byte, 1 = short, 2 = long) */
265          16,                    /* bitsize */
266          false,                 /* pc_relative */
267          0,                     /* bitpos */
268          complain_overflow_bitfield, /* complain_on_overflow */
269          ppc64_elf_brtaken_reloc, /* special_function */
270          "R_PPC64_ADDR14_BRTAKEN",/* name */
271          false,                 /* partial_inplace */
272          0,                     /* src_mask */
273          0xfffc,                /* dst_mask */
274          false),                /* pcrel_offset */
275
276   /* An absolute 16 bit branch, for which bit 10 should be set to
277      indicate that the branch is not expected to be taken.  The lower
278      two bits must be zero.  */
279   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
280          0,                     /* rightshift */
281          2,                     /* size (0 = byte, 1 = short, 2 = long) */
282          16,                    /* bitsize */
283          false,                 /* pc_relative */
284          0,                     /* bitpos */
285          complain_overflow_bitfield, /* complain_on_overflow */
286          ppc64_elf_brtaken_reloc, /* special_function */
287          "R_PPC64_ADDR14_BRNTAKEN",/* name */
288          false,                 /* partial_inplace */
289          0,                     /* src_mask */
290          0xfffc,                /* dst_mask */
291          false),                /* pcrel_offset */
292
293   /* A relative 26 bit branch; the lower two bits must be zero.  */
294   HOWTO (R_PPC64_REL24,         /* type */
295          0,                     /* rightshift */
296          2,                     /* size (0 = byte, 1 = short, 2 = long) */
297          26,                    /* bitsize */
298          true,                  /* pc_relative */
299          0,                     /* bitpos */
300          complain_overflow_signed, /* complain_on_overflow */
301          bfd_elf_generic_reloc, /* special_function */
302          "R_PPC64_REL24",       /* name */
303          false,                 /* partial_inplace */
304          0,                     /* src_mask */
305          0x3fffffc,             /* dst_mask */
306          true),                 /* pcrel_offset */
307
308   /* A relative 16 bit branch; the lower two bits must be zero.  */
309   HOWTO (R_PPC64_REL14,         /* type */
310          0,                     /* rightshift */
311          2,                     /* size (0 = byte, 1 = short, 2 = long) */
312          16,                    /* bitsize */
313          true,                  /* pc_relative */
314          0,                     /* bitpos */
315          complain_overflow_signed, /* complain_on_overflow */
316          bfd_elf_generic_reloc, /* special_function */
317          "R_PPC64_REL14",       /* name */
318          false,                 /* partial_inplace */
319          0,                     /* src_mask */
320          0xfffc,                /* dst_mask */
321          true),                 /* pcrel_offset */
322
323   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
324      the branch is expected to be taken.  The lower two bits must be
325      zero.  */
326   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
327          0,                     /* rightshift */
328          2,                     /* size (0 = byte, 1 = short, 2 = long) */
329          16,                    /* bitsize */
330          true,                  /* pc_relative */
331          0,                     /* bitpos */
332          complain_overflow_signed, /* complain_on_overflow */
333          ppc64_elf_brtaken_reloc, /* special_function */
334          "R_PPC64_REL14_BRTAKEN", /* name */
335          false,                 /* partial_inplace */
336          0,                     /* src_mask */
337          0xfffc,                /* dst_mask */
338          true),                 /* pcrel_offset */
339
340   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
341      the branch is not expected to be taken.  The lower two bits must
342      be zero.  */
343   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
344          0,                     /* rightshift */
345          2,                     /* size (0 = byte, 1 = short, 2 = long) */
346          16,                    /* bitsize */
347          true,                  /* pc_relative */
348          0,                     /* bitpos */
349          complain_overflow_signed, /* complain_on_overflow */
350          ppc64_elf_brtaken_reloc, /* special_function */
351          "R_PPC64_REL14_BRNTAKEN",/* name */
352          false,                 /* partial_inplace */
353          0,                     /* src_mask */
354          0xfffc,                /* dst_mask */
355          true),                 /* pcrel_offset */
356
357   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
358      symbol.  */
359   HOWTO (R_PPC64_GOT16,         /* type */
360          0,                     /* rightshift */
361          1,                     /* size (0 = byte, 1 = short, 2 = long) */
362          16,                    /* bitsize */
363          false,                 /* pc_relative */
364          0,                     /* bitpos */
365          complain_overflow_signed, /* complain_on_overflow */
366          ppc64_elf_unhandled_reloc, /* special_function */
367          "R_PPC64_GOT16",       /* name */
368          false,                 /* partial_inplace */
369          0,                     /* src_mask */
370          0xffff,                /* dst_mask */
371          false),                /* pcrel_offset */
372
373   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
374      the symbol.  */
375   HOWTO (R_PPC64_GOT16_LO,      /* type */
376          0,                     /* rightshift */
377          1,                     /* size (0 = byte, 1 = short, 2 = long) */
378          16,                    /* bitsize */
379          false,                 /* pc_relative */
380          0,                     /* bitpos */
381          complain_overflow_dont, /* complain_on_overflow */
382          ppc64_elf_unhandled_reloc, /* special_function */
383          "R_PPC64_GOT16_LO",    /* name */
384          false,                 /* partial_inplace */
385          0,                     /* src_mask */
386          0xffff,                /* dst_mask */
387          false),                /* pcrel_offset */
388
389   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
390      the symbol.  */
391   HOWTO (R_PPC64_GOT16_HI,      /* type */
392          16,                    /* rightshift */
393          1,                     /* size (0 = byte, 1 = short, 2 = long) */
394          16,                    /* bitsize */
395          false,                 /* pc_relative */
396          0,                     /* bitpos */
397          complain_overflow_dont,/* complain_on_overflow */
398          ppc64_elf_unhandled_reloc, /* special_function */
399          "R_PPC64_GOT16_HI",    /* name */
400          false,                 /* partial_inplace */
401          0,                     /* src_mask */
402          0xffff,                /* dst_mask */
403          false),                /* pcrel_offset */
404
405   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
406      the symbol.  */
407   HOWTO (R_PPC64_GOT16_HA,      /* type */
408          16,                    /* rightshift */
409          1,                     /* size (0 = byte, 1 = short, 2 = long) */
410          16,                    /* bitsize */
411          false,                 /* pc_relative */
412          0,                     /* bitpos */
413          complain_overflow_dont,/* complain_on_overflow */
414          ppc64_elf_unhandled_reloc, /* special_function */
415          "R_PPC64_GOT16_HA",    /* name */
416          false,                 /* partial_inplace */
417          0,                     /* src_mask */
418          0xffff,                /* dst_mask */
419          false),                /* pcrel_offset */
420
421   /* This is used only by the dynamic linker.  The symbol should exist
422      both in the object being run and in some shared library.  The
423      dynamic linker copies the data addressed by the symbol from the
424      shared library into the object, because the object being
425      run has to have the data at some particular address.  */
426   HOWTO (R_PPC64_COPY,          /* type */
427          0,                     /* rightshift */
428          2,                     /* size (0 = byte, 1 = short, 2 = long) */
429          32,                    /* bitsize */
430          false,                 /* pc_relative */
431          0,                     /* bitpos */
432          complain_overflow_bitfield, /* complain_on_overflow */
433          ppc64_elf_unhandled_reloc,  /* special_function */
434          "R_PPC64_COPY",        /* name */
435          false,                 /* partial_inplace */
436          0,                     /* src_mask */
437          0,                     /* dst_mask */
438          false),                /* pcrel_offset */
439
440   /* Like R_PPC64_ADDR64, but used when setting global offset table
441      entries.  */
442   HOWTO (R_PPC64_GLOB_DAT,      /* type */
443          0,                     /* rightshift */
444          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
445          64,                    /* bitsize */
446          false,                 /* pc_relative */
447          0,                     /* bitpos */
448          complain_overflow_dont, /* complain_on_overflow */
449          ppc64_elf_unhandled_reloc,  /* special_function */
450          "R_PPC64_GLOB_DAT",    /* name */
451          false,                 /* partial_inplace */
452          0,                     /* src_mask */
453          0xffffffffffffffff,    /* dst_mask */
454          false),                /* pcrel_offset */
455
456   /* Created by the link editor.  Marks a procedure linkage table
457      entry for a symbol.  */
458   HOWTO (R_PPC64_JMP_SLOT,      /* type */
459          0,                     /* rightshift */
460          0,                     /* size (0 = byte, 1 = short, 2 = long) */
461          0,                     /* bitsize */
462          false,                 /* pc_relative */
463          0,                     /* bitpos */
464          complain_overflow_dont, /* complain_on_overflow */
465          ppc64_elf_unhandled_reloc, /* special_function */
466          "R_PPC64_JMP_SLOT",    /* name */
467          false,                 /* partial_inplace */
468          0,                     /* src_mask */
469          0,                     /* dst_mask */
470          false),                /* pcrel_offset */
471
472   /* Used only by the dynamic linker.  When the object is run, this
473      doubleword64 is set to the load address of the object, plus the
474      addend.  */
475   HOWTO (R_PPC64_RELATIVE,      /* type */
476          0,                     /* rightshift */
477          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
478          64,                    /* bitsize */
479          false,                 /* pc_relative */
480          0,                     /* bitpos */
481          complain_overflow_dont, /* complain_on_overflow */
482          bfd_elf_generic_reloc, /* special_function */
483          "R_PPC64_RELATIVE",    /* name */
484          false,                 /* partial_inplace */
485          0,                     /* src_mask */
486          0xffffffffffffffff,    /* dst_mask */
487          false),                /* pcrel_offset */
488
489   /* Like R_PPC64_ADDR32, but may be unaligned.  */
490   HOWTO (R_PPC64_UADDR32,       /* type */
491          0,                     /* rightshift */
492          2,                     /* size (0 = byte, 1 = short, 2 = long) */
493          32,                    /* bitsize */
494          false,                 /* pc_relative */
495          0,                     /* bitpos */
496          complain_overflow_bitfield, /* complain_on_overflow */
497          bfd_elf_generic_reloc, /* special_function */
498          "R_PPC64_UADDR32",     /* name */
499          false,                 /* partial_inplace */
500          0,                     /* src_mask */
501          0xffffffff,            /* dst_mask */
502          false),                /* pcrel_offset */
503
504   /* Like R_PPC64_ADDR16, but may be unaligned.  */
505   HOWTO (R_PPC64_UADDR16,       /* type */
506          0,                     /* rightshift */
507          1,                     /* size (0 = byte, 1 = short, 2 = long) */
508          16,                    /* bitsize */
509          false,                 /* pc_relative */
510          0,                     /* bitpos */
511          complain_overflow_bitfield, /* complain_on_overflow */
512          bfd_elf_generic_reloc, /* special_function */
513          "R_PPC64_UADDR16",     /* name */
514          false,                 /* partial_inplace */
515          0,                     /* src_mask */
516          0xffff,                /* dst_mask */
517          false),                /* pcrel_offset */
518
519   /* 32-bit PC relative.  */
520   HOWTO (R_PPC64_REL32,         /* type */
521          0,                     /* rightshift */
522          2,                     /* size (0 = byte, 1 = short, 2 = long) */
523          32,                    /* bitsize */
524          true,                  /* pc_relative */
525          0,                     /* bitpos */
526          /* FIXME: Verify.  Was complain_overflow_bitfield. */
527          complain_overflow_signed, /* complain_on_overflow */
528          bfd_elf_generic_reloc, /* special_function */
529          "R_PPC64_REL32",       /* name */
530          false,                 /* partial_inplace */
531          0,                     /* src_mask */
532          0xffffffff,            /* dst_mask */
533          true),                 /* pcrel_offset */
534
535   /* 32-bit relocation to the symbol's procedure linkage table.  */
536   HOWTO (R_PPC64_PLT32,         /* type */
537          0,                     /* rightshift */
538          2,                     /* size (0 = byte, 1 = short, 2 = long) */
539          32,                    /* bitsize */
540          false,                 /* pc_relative */
541          0,                     /* bitpos */
542          complain_overflow_bitfield, /* complain_on_overflow */
543          ppc64_elf_unhandled_reloc, /* special_function */
544          "R_PPC64_PLT32",       /* name */
545          false,                 /* partial_inplace */
546          0,                     /* src_mask */
547          0,                     /* dst_mask */
548          false),                /* pcrel_offset */
549
550   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
551      FIXME: R_PPC64_PLTREL32 not supported.  */
552   HOWTO (R_PPC64_PLTREL32,      /* type */
553          0,                     /* rightshift */
554          2,                     /* size (0 = byte, 1 = short, 2 = long) */
555          32,                    /* bitsize */
556          true,                  /* pc_relative */
557          0,                     /* bitpos */
558          complain_overflow_signed, /* complain_on_overflow */
559          bfd_elf_generic_reloc, /* special_function */
560          "R_PPC64_PLTREL32",    /* name */
561          false,                 /* partial_inplace */
562          0,                     /* src_mask */
563          0,                     /* dst_mask */
564          true),                 /* pcrel_offset */
565
566   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
567      the symbol.  */
568   HOWTO (R_PPC64_PLT16_LO,      /* type */
569          0,                     /* rightshift */
570          1,                     /* size (0 = byte, 1 = short, 2 = long) */
571          16,                    /* bitsize */
572          false,                 /* pc_relative */
573          0,                     /* bitpos */
574          complain_overflow_dont, /* complain_on_overflow */
575          ppc64_elf_unhandled_reloc, /* special_function */
576          "R_PPC64_PLT16_LO",    /* name */
577          false,                 /* partial_inplace */
578          0,                     /* src_mask */
579          0xffff,                /* dst_mask */
580          false),                /* pcrel_offset */
581
582   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
583      the symbol.  */
584   HOWTO (R_PPC64_PLT16_HI,      /* type */
585          16,                    /* rightshift */
586          1,                     /* size (0 = byte, 1 = short, 2 = long) */
587          16,                    /* bitsize */
588          false,                 /* pc_relative */
589          0,                     /* bitpos */
590          complain_overflow_dont, /* complain_on_overflow */
591          ppc64_elf_unhandled_reloc, /* special_function */
592          "R_PPC64_PLT16_HI",    /* name */
593          false,                 /* partial_inplace */
594          0,                     /* src_mask */
595          0xffff,                /* dst_mask */
596          false),                /* pcrel_offset */
597
598   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
599      the symbol.  */
600   HOWTO (R_PPC64_PLT16_HA,      /* type */
601          16,                    /* rightshift */
602          1,                     /* size (0 = byte, 1 = short, 2 = long) */
603          16,                    /* bitsize */
604          false,                 /* pc_relative */
605          0,                     /* bitpos */
606          complain_overflow_dont, /* complain_on_overflow */
607          ppc64_elf_unhandled_reloc, /* special_function */
608          "R_PPC64_PLT16_HA",    /* name */
609          false,                 /* partial_inplace */
610          0,                     /* src_mask */
611          0xffff,                /* dst_mask */
612          false),                /* pcrel_offset */
613
614   /* 16-bit section relative relocation.  */
615   HOWTO (R_PPC64_SECTOFF,       /* type */
616          0,                     /* rightshift */
617          1,                     /* size (0 = byte, 1 = short, 2 = long) */
618          16,                    /* bitsize */
619          false,                 /* pc_relative */
620          0,                     /* bitpos */
621          complain_overflow_bitfield, /* complain_on_overflow */
622          ppc64_elf_sectoff_reloc, /* special_function */
623          "R_PPC64_SECTOFF",     /* name */
624          false,                 /* partial_inplace */
625          0,                     /* src_mask */
626          0xffff,                /* dst_mask */
627          false),                /* pcrel_offset */
628
629   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
630   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
631          0,                     /* rightshift */
632          1,                     /* size (0 = byte, 1 = short, 2 = long) */
633          16,                    /* bitsize */
634          false,                 /* pc_relative */
635          0,                     /* bitpos */
636          complain_overflow_dont, /* complain_on_overflow */
637          ppc64_elf_sectoff_reloc, /* special_function */
638          "R_PPC64_SECTOFF_LO",  /* name */
639          false,                 /* partial_inplace */
640          0,                     /* src_mask */
641          0xffff,                /* dst_mask */
642          false),                /* pcrel_offset */
643
644   /* 16-bit upper half section relative relocation.  */
645   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
646          16,                    /* rightshift */
647          1,                     /* size (0 = byte, 1 = short, 2 = long) */
648          16,                    /* bitsize */
649          false,                 /* pc_relative */
650          0,                     /* bitpos */
651          complain_overflow_dont, /* complain_on_overflow */
652          ppc64_elf_sectoff_reloc, /* special_function */
653          "R_PPC64_SECTOFF_HI",  /* name */
654          false,                 /* partial_inplace */
655          0,                     /* src_mask */
656          0xffff,                /* dst_mask */
657          false),                /* pcrel_offset */
658
659   /* 16-bit upper half adjusted section relative relocation.  */
660   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
661          16,                    /* rightshift */
662          1,                     /* size (0 = byte, 1 = short, 2 = long) */
663          16,                    /* bitsize */
664          false,                 /* pc_relative */
665          0,                     /* bitpos */
666          complain_overflow_dont, /* complain_on_overflow */
667          ppc64_elf_sectoff_ha_reloc, /* special_function */
668          "R_PPC64_SECTOFF_HA",  /* name */
669          false,                 /* partial_inplace */
670          0,                     /* src_mask */
671          0xffff,                /* dst_mask */
672          false),                /* pcrel_offset */
673
674   /* Like R_PPC64_REL24 without touching the two least significant
675      bits.  Should have been named R_PPC64_REL30!  */
676   HOWTO (R_PPC64_ADDR30,        /* type */
677          2,                     /* rightshift */
678          2,                     /* size (0 = byte, 1 = short, 2 = long) */
679          30,                    /* bitsize */
680          true,                  /* pc_relative */
681          0,                     /* bitpos */
682          complain_overflow_dont, /* complain_on_overflow */
683          bfd_elf_generic_reloc, /* special_function */
684          "R_PPC64_ADDR30",      /* name */
685          false,                 /* partial_inplace */
686          0,                     /* src_mask */
687          0xfffffffc,            /* dst_mask */
688          true),                 /* pcrel_offset */
689
690   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
691
692   /* A standard 64-bit relocation.  */
693   HOWTO (R_PPC64_ADDR64,        /* type */
694          0,                     /* rightshift */
695          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
696          64,                    /* bitsize */
697          false,                 /* pc_relative */
698          0,                     /* bitpos */
699          complain_overflow_dont, /* complain_on_overflow */
700          bfd_elf_generic_reloc, /* special_function */
701          "R_PPC64_ADDR64",      /* name */
702          false,                 /* partial_inplace */
703          0,                     /* src_mask */
704          0xffffffffffffffff,    /* dst_mask */
705          false),                /* pcrel_offset */
706
707   /* The bits 32-47 of an address.  */
708   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
709          32,                    /* rightshift */
710          1,                     /* size (0 = byte, 1 = short, 2 = long) */
711          16,                    /* bitsize */
712          false,                 /* pc_relative */
713          0,                     /* bitpos */
714          complain_overflow_dont, /* complain_on_overflow */
715          bfd_elf_generic_reloc, /* special_function */
716          "R_PPC64_ADDR16_HIGHER", /* name */
717          false,                 /* partial_inplace */
718          0,                     /* src_mask */
719          0xffff,                /* dst_mask */
720          false),                /* pcrel_offset */
721
722   /* The bits 32-47 of an address, plus 1 if the contents of the low
723      16 bits, treated as a signed number, is negative.  */
724   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
725          32,                    /* rightshift */
726          1,                     /* size (0 = byte, 1 = short, 2 = long) */
727          16,                    /* bitsize */
728          false,                 /* pc_relative */
729          0,                     /* bitpos */
730          complain_overflow_dont, /* complain_on_overflow */
731          ppc64_elf_ha_reloc,    /* special_function */
732          "R_PPC64_ADDR16_HIGHERA", /* name */
733          false,                 /* partial_inplace */
734          0,                     /* src_mask */
735          0xffff,                /* dst_mask */
736          false),                /* pcrel_offset */
737
738   /* The bits 48-63 of an address.  */
739   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
740          48,                    /* rightshift */
741          1,                     /* size (0 = byte, 1 = short, 2 = long) */
742          16,                    /* bitsize */
743          false,                 /* pc_relative */
744          0,                     /* bitpos */
745          complain_overflow_dont, /* complain_on_overflow */
746          bfd_elf_generic_reloc, /* special_function */
747          "R_PPC64_ADDR16_HIGHEST", /* name */
748          false,                 /* partial_inplace */
749          0,                     /* src_mask */
750          0xffff,                /* dst_mask */
751          false),                /* pcrel_offset */
752
753   /* The bits 48-63 of an address, plus 1 if the contents of the low
754      16 bits, treated as a signed number, is negative.  */
755   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
756          48,                    /* rightshift */
757          1,                     /* size (0 = byte, 1 = short, 2 = long) */
758          16,                    /* bitsize */
759          false,                 /* pc_relative */
760          0,                     /* bitpos */
761          complain_overflow_dont, /* complain_on_overflow */
762          ppc64_elf_ha_reloc,    /* special_function */
763          "R_PPC64_ADDR16_HIGHESTA", /* name */
764          false,                 /* partial_inplace */
765          0,                     /* src_mask */
766          0xffff,                /* dst_mask */
767          false),                /* pcrel_offset */
768
769   /* Like ADDR64, but may be unaligned.  */
770   HOWTO (R_PPC64_UADDR64,       /* type */
771          0,                     /* rightshift */
772          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
773          64,                    /* bitsize */
774          false,                 /* pc_relative */
775          0,                     /* bitpos */
776          complain_overflow_dont, /* complain_on_overflow */
777          bfd_elf_generic_reloc, /* special_function */
778          "R_PPC64_UADDR64",     /* name */
779          false,                 /* partial_inplace */
780          0,                     /* src_mask */
781          0xffffffffffffffff,    /* dst_mask */
782          false),                /* pcrel_offset */
783
784   /* 64-bit relative relocation.  */
785   HOWTO (R_PPC64_REL64,         /* type */
786          0,                     /* rightshift */
787          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
788          64,                    /* bitsize */
789          true,                  /* pc_relative */
790          0,                     /* bitpos */
791          complain_overflow_dont, /* complain_on_overflow */
792          bfd_elf_generic_reloc, /* special_function */
793          "R_PPC64_REL64",       /* name */
794          false,                 /* partial_inplace */
795          0,                     /* src_mask */
796          0xffffffffffffffff,    /* dst_mask */
797          true),                 /* pcrel_offset */
798
799   /* 64-bit relocation to the symbol's procedure linkage table. */
800   HOWTO (R_PPC64_PLT64,         /* type */
801          0,                     /* rightshift */
802          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
803          64,                    /* bitsize */
804          false,                 /* pc_relative */
805          0,                     /* bitpos */
806          complain_overflow_dont, /* complain_on_overflow */
807          ppc64_elf_unhandled_reloc, /* special_function */
808          "R_PPC64_PLT64",       /* name */
809          false,                 /* partial_inplace */
810          0,                     /* src_mask */
811          0,                     /* dst_mask */
812          false),                /* pcrel_offset */
813
814   /* 64-bit PC relative relocation to the symbol's procedure linkage
815      table.  */
816   /* FIXME: R_PPC64_PLTREL64 not supported.  */
817   HOWTO (R_PPC64_PLTREL64,      /* type */
818          0,                     /* rightshift */
819          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
820          64,                    /* bitsize */
821          true,                  /* pc_relative */
822          0,                     /* bitpos */
823          complain_overflow_dont, /* complain_on_overflow */
824          ppc64_elf_unhandled_reloc, /* special_function */
825          "R_PPC64_PLTREL64",    /* name */
826          false,                 /* partial_inplace */
827          0,                     /* src_mask */
828          0,                     /* dst_mask */
829          true),                 /* pcrel_offset */
830
831   /* 16 bit TOC-relative relocation.  */
832
833   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
834   HOWTO (R_PPC64_TOC16,         /* type */
835          0,                     /* rightshift */
836          1,                     /* size (0 = byte, 1 = short, 2 = long) */
837          16,                    /* bitsize */
838          false,                 /* pc_relative */
839          0,                     /* bitpos */
840          complain_overflow_signed, /* complain_on_overflow */
841          ppc64_elf_toc_reloc,   /* special_function */
842          "R_PPC64_TOC16",       /* name */
843          false,                 /* partial_inplace */
844          0,                     /* src_mask */
845          0xffff,                /* dst_mask */
846          false),                /* pcrel_offset */
847
848   /* 16 bit TOC-relative relocation without overflow.  */
849
850   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
851   HOWTO (R_PPC64_TOC16_LO,      /* type */
852          0,                     /* rightshift */
853          1,                     /* size (0 = byte, 1 = short, 2 = long) */
854          16,                    /* bitsize */
855          false,                 /* pc_relative */
856          0,                     /* bitpos */
857          complain_overflow_dont, /* complain_on_overflow */
858          ppc64_elf_toc_reloc,   /* special_function */
859          "R_PPC64_TOC16_LO",    /* name */
860          false,                 /* partial_inplace */
861          0,                     /* src_mask */
862          0xffff,                /* dst_mask */
863          false),                /* pcrel_offset */
864
865   /* 16 bit TOC-relative relocation, high 16 bits.  */
866
867   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
868   HOWTO (R_PPC64_TOC16_HI,      /* type */
869          16,                    /* rightshift */
870          1,                     /* size (0 = byte, 1 = short, 2 = long) */
871          16,                    /* bitsize */
872          false,                 /* pc_relative */
873          0,                     /* bitpos */
874          complain_overflow_dont, /* complain_on_overflow */
875          ppc64_elf_toc_reloc,   /* special_function */
876          "R_PPC64_TOC16_HI",    /* name */
877          false,                 /* partial_inplace */
878          0,                     /* src_mask */
879          0xffff,                /* dst_mask */
880          false),                /* pcrel_offset */
881
882   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
883      contents of the low 16 bits, treated as a signed number, is
884      negative.  */
885
886   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
887   HOWTO (R_PPC64_TOC16_HA,      /* type */
888          16,                    /* rightshift */
889          1,                     /* size (0 = byte, 1 = short, 2 = long) */
890          16,                    /* bitsize */
891          false,                 /* pc_relative */
892          0,                     /* bitpos */
893          complain_overflow_dont, /* complain_on_overflow */
894          ppc64_elf_toc_ha_reloc, /* special_function */
895          "R_PPC64_TOC16_HA",    /* name */
896          false,                 /* partial_inplace */
897          0,                     /* src_mask */
898          0xffff,                /* dst_mask */
899          false),                /* pcrel_offset */
900
901   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
902
903   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
904   HOWTO (R_PPC64_TOC,           /* type */
905          0,                     /* rightshift */
906          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
907          64,                    /* bitsize */
908          false,                 /* pc_relative */
909          0,                     /* bitpos */
910          complain_overflow_bitfield, /* complain_on_overflow */
911          ppc64_elf_toc64_reloc, /* special_function */
912          "R_PPC64_TOC",         /* name */
913          false,                 /* partial_inplace */
914          0,                     /* src_mask */
915          0xffffffffffffffff,    /* dst_mask */
916          false),                /* pcrel_offset */
917
918   /* Like R_PPC64_GOT16, but also informs the link editor that the
919      value to relocate may (!) refer to a PLT entry which the link
920      editor (a) may replace with the symbol value.  If the link editor
921      is unable to fully resolve the symbol, it may (b) create a PLT
922      entry and store the address to the new PLT entry in the GOT.
923      This permits lazy resolution of function symbols at run time.
924      The link editor may also skip all of this and just (c) emit a
925      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
926   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
927     HOWTO (R_PPC64_PLTGOT16,    /* type */
928          0,                     /* rightshift */
929          1,                     /* size (0 = byte, 1 = short, 2 = long) */
930          16,                    /* bitsize */
931          false,                 /* pc_relative */
932          0,                     /* bitpos */
933          complain_overflow_signed, /* complain_on_overflow */
934          ppc64_elf_unhandled_reloc, /* special_function */
935          "R_PPC64_PLTGOT16",    /* name */
936          false,                 /* partial_inplace */
937          0,                     /* src_mask */
938          0xffff,                /* dst_mask */
939          false),                /* pcrel_offset */
940
941   /* Like R_PPC64_PLTGOT16, but without overflow.  */
942   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
943   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
944          0,                     /* rightshift */
945          1,                     /* size (0 = byte, 1 = short, 2 = long) */
946          16,                    /* bitsize */
947          false,                 /* pc_relative */
948          0,                     /* bitpos */
949          complain_overflow_dont, /* complain_on_overflow */
950          ppc64_elf_unhandled_reloc, /* special_function */
951          "R_PPC64_PLTGOT16_LO", /* name */
952          false,                 /* partial_inplace */
953          0,                     /* src_mask */
954          0xffff,                /* dst_mask */
955          false),                /* pcrel_offset */
956
957   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
958   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
959   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
960          16,                    /* rightshift */
961          1,                     /* size (0 = byte, 1 = short, 2 = long) */
962          16,                    /* bitsize */
963          false,                 /* pc_relative */
964          0,                     /* bitpos */
965          complain_overflow_dont, /* complain_on_overflow */
966          ppc64_elf_unhandled_reloc, /* special_function */
967          "R_PPC64_PLTGOT16_HI", /* name */
968          false,                 /* partial_inplace */
969          0,                     /* src_mask */
970          0xffff,                /* dst_mask */
971          false),                /* pcrel_offset */
972
973   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
974      1 if the contents of the low 16 bits, treated as a signed number,
975      is negative.  */
976   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
977   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
978          16,                    /* rightshift */
979          1,                     /* size (0 = byte, 1 = short, 2 = long) */
980          16,                    /* bitsize */
981          false,                 /* pc_relative */
982          0,                     /* bitpos */
983          complain_overflow_dont,/* complain_on_overflow */
984          ppc64_elf_unhandled_reloc, /* special_function */
985          "R_PPC64_PLTGOT16_HA", /* name */
986          false,                 /* partial_inplace */
987          0,                     /* src_mask */
988          0xffff,                /* dst_mask */
989          false),                /* pcrel_offset */
990
991   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
992   HOWTO (R_PPC64_ADDR16_DS,     /* type */
993          0,                     /* rightshift */
994          1,                     /* size (0 = byte, 1 = short, 2 = long) */
995          16,                    /* bitsize */
996          false,                 /* pc_relative */
997          0,                     /* bitpos */
998          complain_overflow_bitfield, /* complain_on_overflow */
999          bfd_elf_generic_reloc, /* special_function */
1000          "R_PPC64_ADDR16_DS",   /* name */
1001          false,                 /* partial_inplace */
1002          0,                     /* src_mask */
1003          0xfffc,                /* dst_mask */
1004          false),                /* pcrel_offset */
1005
1006   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1007   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1008          0,                     /* rightshift */
1009          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1010          16,                    /* bitsize */
1011          false,                 /* pc_relative */
1012          0,                     /* bitpos */
1013          complain_overflow_dont,/* complain_on_overflow */
1014          bfd_elf_generic_reloc, /* special_function */
1015          "R_PPC64_ADDR16_LO_DS",/* name */
1016          false,                 /* partial_inplace */
1017          0,                     /* src_mask */
1018          0xfffc,                /* dst_mask */
1019          false),                /* pcrel_offset */
1020
1021   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1022   HOWTO (R_PPC64_GOT16_DS,      /* type */
1023          0,                     /* rightshift */
1024          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1025          16,                    /* bitsize */
1026          false,                 /* pc_relative */
1027          0,                     /* bitpos */
1028          complain_overflow_signed, /* complain_on_overflow */
1029          ppc64_elf_unhandled_reloc, /* special_function */
1030          "R_PPC64_GOT16_DS",    /* name */
1031          false,                 /* partial_inplace */
1032          0,                     /* src_mask */
1033          0xfffc,                /* dst_mask */
1034          false),                /* pcrel_offset */
1035
1036   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1037   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1038          0,                     /* rightshift */
1039          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1040          16,                    /* bitsize */
1041          false,                 /* pc_relative */
1042          0,                     /* bitpos */
1043          complain_overflow_dont, /* complain_on_overflow */
1044          ppc64_elf_unhandled_reloc, /* special_function */
1045          "R_PPC64_GOT16_LO_DS", /* name */
1046          false,                 /* partial_inplace */
1047          0,                     /* src_mask */
1048          0xfffc,                /* dst_mask */
1049          false),                /* pcrel_offset */
1050
1051   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1052   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1053          0,                     /* rightshift */
1054          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1055          16,                    /* bitsize */
1056          false,                 /* pc_relative */
1057          0,                     /* bitpos */
1058          complain_overflow_dont, /* complain_on_overflow */
1059          ppc64_elf_unhandled_reloc, /* special_function */
1060          "R_PPC64_PLT16_LO_DS", /* name */
1061          false,                 /* partial_inplace */
1062          0,                     /* src_mask */
1063          0xfffc,                /* dst_mask */
1064          false),                /* pcrel_offset */
1065
1066   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1067   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1068          0,                     /* rightshift */
1069          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1070          16,                    /* bitsize */
1071          false,                 /* pc_relative */
1072          0,                     /* bitpos */
1073          complain_overflow_bitfield, /* complain_on_overflow */
1074          ppc64_elf_sectoff_reloc, /* special_function */
1075          "R_PPC64_SECTOFF_DS",  /* name */
1076          false,                 /* partial_inplace */
1077          0,                     /* src_mask */
1078          0xfffc,                /* dst_mask */
1079          false),                /* pcrel_offset */
1080
1081   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1082   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1083          0,                     /* rightshift */
1084          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1085          16,                    /* bitsize */
1086          false,                 /* pc_relative */
1087          0,                     /* bitpos */
1088          complain_overflow_dont, /* complain_on_overflow */
1089          ppc64_elf_sectoff_reloc, /* special_function */
1090          "R_PPC64_SECTOFF_LO_DS",/* name */
1091          false,                 /* partial_inplace */
1092          0,                     /* src_mask */
1093          0xfffc,                /* dst_mask */
1094          false),                /* pcrel_offset */
1095
1096   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1097   HOWTO (R_PPC64_TOC16_DS,      /* type */
1098          0,                     /* rightshift */
1099          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1100          16,                    /* bitsize */
1101          false,                 /* pc_relative */
1102          0,                     /* bitpos */
1103          complain_overflow_signed, /* complain_on_overflow */
1104          ppc64_elf_toc_reloc,   /* special_function */
1105          "R_PPC64_TOC16_DS",    /* name */
1106          false,                 /* partial_inplace */
1107          0,                     /* src_mask */
1108          0xfffc,                /* dst_mask */
1109          false),                /* pcrel_offset */
1110
1111   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1112   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1113          0,                     /* rightshift */
1114          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1115          16,                    /* bitsize */
1116          false,                 /* pc_relative */
1117          0,                     /* bitpos */
1118          complain_overflow_dont, /* complain_on_overflow */
1119          ppc64_elf_toc_reloc,   /* special_function */
1120          "R_PPC64_TOC16_LO_DS", /* name */
1121          false,                 /* partial_inplace */
1122          0,                     /* src_mask */
1123          0xfffc,                /* dst_mask */
1124          false),                /* pcrel_offset */
1125
1126   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1127   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1128     HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1129          0,                     /* rightshift */
1130          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1131          16,                    /* bitsize */
1132          false,                 /* pc_relative */
1133          0,                     /* bitpos */
1134          complain_overflow_signed, /* complain_on_overflow */
1135          ppc64_elf_unhandled_reloc, /* special_function */
1136          "R_PPC64_PLTGOT16_DS", /* name */
1137          false,                 /* partial_inplace */
1138          0,                     /* src_mask */
1139          0xfffc,                /* dst_mask */
1140          false),                /* pcrel_offset */
1141
1142   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1143   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1144   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1145          0,                     /* rightshift */
1146          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1147          16,                    /* bitsize */
1148          false,                 /* pc_relative */
1149          0,                     /* bitpos */
1150          complain_overflow_dont, /* complain_on_overflow */
1151          ppc64_elf_unhandled_reloc, /* special_function */
1152          "R_PPC64_PLTGOT16_LO_DS",/* name */
1153          false,                 /* partial_inplace */
1154          0,                     /* src_mask */
1155          0xfffc,                /* dst_mask */
1156          false),                /* pcrel_offset */
1157
1158   /* GNU extension to record C++ vtable hierarchy.  */
1159   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1160          0,                     /* rightshift */
1161          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1162          0,                     /* bitsize */
1163          false,                 /* pc_relative */
1164          0,                     /* bitpos */
1165          complain_overflow_dont, /* complain_on_overflow */
1166          NULL,                  /* special_function */
1167          "R_PPC64_GNU_VTINHERIT", /* name */
1168          false,                 /* partial_inplace */
1169          0,                     /* src_mask */
1170          0,                     /* dst_mask */
1171          false),                /* pcrel_offset */
1172
1173   /* GNU extension to record C++ vtable member usage.  */
1174   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
1175          0,                     /* rightshift */
1176          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1177          0,                     /* bitsize */
1178          false,                 /* pc_relative */
1179          0,                     /* bitpos */
1180          complain_overflow_dont, /* complain_on_overflow */
1181          NULL,                  /* special_function */
1182          "R_PPC64_GNU_VTENTRY", /* name */
1183          false,                 /* partial_inplace */
1184          0,                     /* src_mask */
1185          0,                     /* dst_mask */
1186          false),                /* pcrel_offset */
1187 };
1188
1189 \f
1190 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1191    be done.  */
1192
1193 static void
1194 ppc_howto_init ()
1195 {
1196   unsigned int i, type;
1197
1198   for (i = 0;
1199        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1200        i++)
1201     {
1202       type = ppc64_elf_howto_raw[i].type;
1203       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1204                           / sizeof (ppc64_elf_howto_table[0])));
1205       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1206     }
1207 }
1208
1209 static reloc_howto_type *
1210 ppc64_elf_reloc_type_lookup (abfd, code)
1211      bfd *abfd ATTRIBUTE_UNUSED;
1212      bfd_reloc_code_real_type code;
1213 {
1214   enum elf_ppc_reloc_type ppc_reloc = R_PPC_NONE;
1215
1216   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1217     /* Initialize howto table if needed.  */
1218     ppc_howto_init ();
1219
1220   switch ((int) code)
1221     {
1222     default:
1223       return (reloc_howto_type *) NULL;
1224
1225     case BFD_RELOC_NONE:                 ppc_reloc = R_PPC64_NONE;
1226       break;
1227     case BFD_RELOC_32:                   ppc_reloc = R_PPC64_ADDR32;
1228       break;
1229     case BFD_RELOC_PPC_BA26:             ppc_reloc = R_PPC64_ADDR24;
1230       break;
1231     case BFD_RELOC_16:                   ppc_reloc = R_PPC64_ADDR16;
1232       break;
1233     case BFD_RELOC_LO16:                 ppc_reloc = R_PPC64_ADDR16_LO;
1234       break;
1235     case BFD_RELOC_HI16:                 ppc_reloc = R_PPC64_ADDR16_HI;
1236       break;
1237     case BFD_RELOC_HI16_S:               ppc_reloc = R_PPC64_ADDR16_HA;
1238       break;
1239     case BFD_RELOC_PPC_BA16:             ppc_reloc = R_PPC64_ADDR14;
1240       break;
1241     case BFD_RELOC_PPC_BA16_BRTAKEN:     ppc_reloc = R_PPC64_ADDR14_BRTAKEN;
1242       break;
1243     case BFD_RELOC_PPC_BA16_BRNTAKEN:    ppc_reloc = R_PPC64_ADDR14_BRNTAKEN;
1244       break;
1245     case BFD_RELOC_PPC_B26:              ppc_reloc = R_PPC64_REL24;
1246       break;
1247     case BFD_RELOC_PPC_B16:              ppc_reloc = R_PPC64_REL14;
1248       break;
1249     case BFD_RELOC_PPC_B16_BRTAKEN:      ppc_reloc = R_PPC64_REL14_BRTAKEN;
1250       break;
1251     case BFD_RELOC_PPC_B16_BRNTAKEN:     ppc_reloc = R_PPC64_REL14_BRNTAKEN;
1252       break;
1253     case BFD_RELOC_16_GOTOFF:            ppc_reloc = R_PPC64_GOT16;
1254       break;
1255     case BFD_RELOC_LO16_GOTOFF:          ppc_reloc = R_PPC64_GOT16_LO;
1256       break;
1257     case BFD_RELOC_HI16_GOTOFF:          ppc_reloc = R_PPC64_GOT16_HI;
1258       break;
1259     case BFD_RELOC_HI16_S_GOTOFF:        ppc_reloc = R_PPC64_GOT16_HA;
1260       break;
1261     case BFD_RELOC_PPC_COPY:             ppc_reloc = R_PPC64_COPY;
1262       break;
1263     case BFD_RELOC_PPC_GLOB_DAT:         ppc_reloc = R_PPC64_GLOB_DAT;
1264       break;
1265     case BFD_RELOC_32_PCREL:             ppc_reloc = R_PPC64_REL32;
1266       break;
1267     case BFD_RELOC_32_PLTOFF:            ppc_reloc = R_PPC64_PLT32;
1268       break;
1269     case BFD_RELOC_32_PLT_PCREL:         ppc_reloc = R_PPC64_PLTREL32;
1270       break;
1271     case BFD_RELOC_LO16_PLTOFF:          ppc_reloc = R_PPC64_PLT16_LO;
1272       break;
1273     case BFD_RELOC_HI16_PLTOFF:          ppc_reloc = R_PPC64_PLT16_HI;
1274       break;
1275     case BFD_RELOC_HI16_S_PLTOFF:        ppc_reloc = R_PPC64_PLT16_HA;
1276       break;
1277     case BFD_RELOC_16_BASEREL:           ppc_reloc = R_PPC64_SECTOFF;
1278       break;
1279     case BFD_RELOC_LO16_BASEREL:         ppc_reloc = R_PPC64_SECTOFF_LO;
1280       break;
1281     case BFD_RELOC_HI16_BASEREL:         ppc_reloc = R_PPC64_SECTOFF_HI;
1282       break;
1283     case BFD_RELOC_HI16_S_BASEREL:       ppc_reloc = R_PPC64_SECTOFF_HA;
1284       break;
1285     case BFD_RELOC_CTOR:                 ppc_reloc = R_PPC64_ADDR64;
1286       break;
1287     case BFD_RELOC_64:                   ppc_reloc = R_PPC64_ADDR64;
1288       break;
1289     case BFD_RELOC_PPC64_HIGHER:         ppc_reloc = R_PPC64_ADDR16_HIGHER;
1290       break;
1291     case BFD_RELOC_PPC64_HIGHER_S:       ppc_reloc = R_PPC64_ADDR16_HIGHERA;
1292       break;
1293     case BFD_RELOC_PPC64_HIGHEST:        ppc_reloc = R_PPC64_ADDR16_HIGHEST;
1294       break;
1295     case BFD_RELOC_PPC64_HIGHEST_S:      ppc_reloc = R_PPC64_ADDR16_HIGHESTA;
1296       break;
1297     case BFD_RELOC_64_PCREL:             ppc_reloc = R_PPC64_REL64;
1298       break;
1299     case BFD_RELOC_64_PLTOFF:            ppc_reloc = R_PPC64_PLT64;
1300       break;
1301     case BFD_RELOC_64_PLT_PCREL:         ppc_reloc = R_PPC64_PLTREL64;
1302       break;
1303     case BFD_RELOC_PPC_TOC16:            ppc_reloc = R_PPC64_TOC16;
1304       break;
1305     case BFD_RELOC_PPC64_TOC16_LO:       ppc_reloc = R_PPC64_TOC16_LO;
1306       break;
1307     case BFD_RELOC_PPC64_TOC16_HI:       ppc_reloc = R_PPC64_TOC16_HI;
1308       break;
1309     case BFD_RELOC_PPC64_TOC16_HA:       ppc_reloc = R_PPC64_TOC16_HA;
1310       break;
1311     case BFD_RELOC_PPC64_TOC:            ppc_reloc = R_PPC64_TOC;
1312       break;
1313     case BFD_RELOC_PPC64_PLTGOT16:       ppc_reloc = R_PPC64_PLTGOT16;
1314       break;
1315     case BFD_RELOC_PPC64_PLTGOT16_LO:    ppc_reloc = R_PPC64_PLTGOT16_LO;
1316       break;
1317     case BFD_RELOC_PPC64_PLTGOT16_HI:    ppc_reloc = R_PPC64_PLTGOT16_HI;
1318       break;
1319     case BFD_RELOC_PPC64_PLTGOT16_HA:    ppc_reloc = R_PPC64_PLTGOT16_HA;
1320       break;
1321     case BFD_RELOC_PPC64_ADDR16_DS:      ppc_reloc = R_PPC64_ADDR16_DS;
1322       break;
1323     case BFD_RELOC_PPC64_ADDR16_LO_DS:   ppc_reloc = R_PPC64_ADDR16_LO_DS;
1324       break;
1325     case BFD_RELOC_PPC64_GOT16_DS:       ppc_reloc = R_PPC64_GOT16_DS;
1326       break;
1327     case BFD_RELOC_PPC64_GOT16_LO_DS:    ppc_reloc = R_PPC64_GOT16_LO_DS;
1328       break;
1329     case BFD_RELOC_PPC64_PLT16_LO_DS:    ppc_reloc = R_PPC64_PLT16_LO_DS;
1330       break;
1331     case BFD_RELOC_PPC64_SECTOFF_DS:     ppc_reloc = R_PPC64_SECTOFF_DS;
1332       break;
1333     case BFD_RELOC_PPC64_SECTOFF_LO_DS:  ppc_reloc = R_PPC64_SECTOFF_LO_DS;
1334       break;
1335     case BFD_RELOC_PPC64_TOC16_DS:       ppc_reloc = R_PPC64_TOC16_DS;
1336       break;
1337     case BFD_RELOC_PPC64_TOC16_LO_DS:    ppc_reloc = R_PPC64_TOC16_LO_DS;
1338       break;
1339     case BFD_RELOC_PPC64_PLTGOT16_DS:    ppc_reloc = R_PPC64_PLTGOT16_DS;
1340       break;
1341     case BFD_RELOC_PPC64_PLTGOT16_LO_DS: ppc_reloc = R_PPC64_PLTGOT16_LO_DS;
1342       break;
1343     case BFD_RELOC_VTABLE_INHERIT:       ppc_reloc = R_PPC64_GNU_VTINHERIT;
1344       break;
1345     case BFD_RELOC_VTABLE_ENTRY:         ppc_reloc = R_PPC64_GNU_VTENTRY;
1346       break;
1347     }
1348
1349   return ppc64_elf_howto_table[(int) ppc_reloc];
1350 };
1351
1352 /* Set the howto pointer for a PowerPC ELF reloc.  */
1353
1354 static void
1355 ppc64_elf_info_to_howto (abfd, cache_ptr, dst)
1356      bfd *abfd ATTRIBUTE_UNUSED;
1357      arelent *cache_ptr;
1358      Elf64_Internal_Rela *dst;
1359 {
1360   unsigned int type;
1361
1362   /* Initialize howto table if needed.  */
1363   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1364     ppc_howto_init ();
1365
1366   type = ELF64_R_TYPE (dst->r_info);
1367   BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1368                       / sizeof (ppc64_elf_howto_table[0])));
1369   cache_ptr->howto = ppc64_elf_howto_table[type];
1370 }
1371
1372 /* Handle the R_PPC_ADDR16_HA and similar relocs.  */
1373
1374 static bfd_reloc_status_type
1375 ppc64_elf_ha_reloc (abfd, reloc_entry, symbol, data,
1376                     input_section, output_bfd, error_message)
1377      bfd *abfd;
1378      arelent *reloc_entry;
1379      asymbol *symbol;
1380      PTR data;
1381      asection *input_section;
1382      bfd *output_bfd;
1383      char **error_message;
1384 {
1385   /* If this is a relocatable link (output_bfd test tells us), just
1386      call the generic function.  Any adjustment will be done at final
1387      link time.  */
1388   if (output_bfd != NULL)
1389     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,      
1390                                   input_section, output_bfd, error_message);
1391
1392   /* Adjust the addend for sign extension of the low 16 bits.
1393      We won't actually be using the low 16 bits, so trashing them
1394      doesn't matter.  */
1395   reloc_entry->addend += 0x8000;
1396   return bfd_reloc_continue;
1397 }
1398
1399 static bfd_reloc_status_type
1400 ppc64_elf_brtaken_reloc (abfd, reloc_entry, symbol, data,
1401                          input_section, output_bfd, error_message)
1402      bfd *abfd;
1403      arelent *reloc_entry;
1404      asymbol *symbol;
1405      PTR data;
1406      asection *input_section;
1407      bfd *output_bfd;
1408      char **error_message;
1409 {
1410   long insn;
1411   enum elf_ppc_reloc_type r_type;
1412   bfd_size_type octets;
1413   /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
1414   boolean is_power4 = false;
1415
1416   /* If this is a relocatable link (output_bfd test tells us), just
1417      call the generic function.  Any adjustment will be done at final
1418      link time.  */
1419   if (output_bfd != NULL)
1420     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,      
1421                                   input_section, output_bfd, error_message);
1422
1423   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1424   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1425   insn &= ~(0x01 << 21);
1426   r_type = (enum elf_ppc_reloc_type) reloc_entry->howto->type;
1427   if (r_type == R_PPC64_ADDR14_BRTAKEN
1428       || r_type == R_PPC64_REL14_BRTAKEN)
1429     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1430
1431   if (is_power4)
1432     {
1433       /* Set 'a' bit.  This is 0b00010 in BO field for branch
1434          on CR(BI) insns (BO == 001at or 011at), and 0b01000
1435          for branch on CTR insns (BO == 1a00t or 1a01t).  */
1436       if ((insn & (0x14 << 21)) == (0x04 << 21))
1437         insn |= 0x02 << 21;
1438       else if ((insn & (0x14 << 21)) == (0x10 << 21))
1439         insn |= 0x08 << 21;
1440       else
1441         return bfd_reloc_continue;
1442     }
1443   else
1444     {
1445       bfd_vma target = 0;
1446       bfd_vma from;
1447
1448       if (!bfd_is_com_section (symbol->section))
1449         target = symbol->value;
1450       target += symbol->section->output_section->vma;
1451       target += symbol->section->output_offset;
1452       target += reloc_entry->addend;
1453
1454       from = (reloc_entry->address
1455               + input_section->output_offset
1456               + input_section->output_section->vma);
1457
1458       /* Invert 'y' bit if not the default.  */
1459       if ((bfd_signed_vma) (target - from) < 0)
1460         insn ^= 0x01 << 21;
1461     }
1462   bfd_put_32 (abfd, (bfd_vma) insn, (bfd_byte *) data + octets);
1463   return bfd_reloc_continue;
1464 }
1465
1466 static bfd_reloc_status_type
1467 ppc64_elf_sectoff_reloc (abfd, reloc_entry, symbol, data,
1468                          input_section, output_bfd, error_message)
1469      bfd *abfd;
1470      arelent *reloc_entry;
1471      asymbol *symbol;
1472      PTR data;
1473      asection *input_section;
1474      bfd *output_bfd;
1475      char **error_message;
1476 {
1477   /* If this is a relocatable link (output_bfd test tells us), just
1478      call the generic function.  Any adjustment will be done at final
1479      link time.  */
1480   if (output_bfd != NULL)
1481     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,      
1482                                   input_section, output_bfd, error_message);
1483
1484   /* Subtract the symbol section base address.  */
1485   reloc_entry->addend -= symbol->section->output_section->vma;
1486   return bfd_reloc_continue;
1487 }
1488
1489 static bfd_reloc_status_type
1490 ppc64_elf_sectoff_ha_reloc (abfd, reloc_entry, symbol, data,
1491                             input_section, output_bfd, error_message)
1492      bfd *abfd;
1493      arelent *reloc_entry;
1494      asymbol *symbol;
1495      PTR data;
1496      asection *input_section;
1497      bfd *output_bfd;
1498      char **error_message;
1499 {
1500   /* If this is a relocatable link (output_bfd test tells us), just
1501      call the generic function.  Any adjustment will be done at final
1502      link time.  */
1503   if (output_bfd != NULL)
1504     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,      
1505                                   input_section, output_bfd, error_message);
1506
1507   /* Subtract the symbol section base address.  */
1508   reloc_entry->addend -= symbol->section->output_section->vma;
1509
1510   /* Adjust the addend for sign extension of the low 16 bits.  */
1511   reloc_entry->addend += 0x8000;
1512   return bfd_reloc_continue;
1513 }
1514
1515 static bfd_reloc_status_type
1516 ppc64_elf_toc_reloc (abfd, reloc_entry, symbol, data,
1517                      input_section, output_bfd, error_message)
1518      bfd *abfd;
1519      arelent *reloc_entry;
1520      asymbol *symbol;
1521      PTR data;
1522      asection *input_section;
1523      bfd *output_bfd;
1524      char **error_message;
1525 {
1526   bfd_vma TOCstart;
1527
1528   /* If this is a relocatable link (output_bfd test tells us), just
1529      call the generic function.  Any adjustment will be done at final
1530      link time.  */
1531   if (output_bfd != NULL)
1532     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,      
1533                                   input_section, output_bfd, error_message);
1534
1535   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1536   if (TOCstart == 0)
1537     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
1538
1539   /* Subtract the TOC base address.  */
1540   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1541   return bfd_reloc_continue;
1542 }
1543
1544 static bfd_reloc_status_type
1545 ppc64_elf_toc_ha_reloc (abfd, reloc_entry, symbol, data,
1546                         input_section, output_bfd, error_message)
1547      bfd *abfd;
1548      arelent *reloc_entry;
1549      asymbol *symbol;
1550      PTR data;
1551      asection *input_section;
1552      bfd *output_bfd;
1553      char **error_message;
1554 {
1555   bfd_vma TOCstart;
1556
1557   /* If this is a relocatable link (output_bfd test tells us), just
1558      call the generic function.  Any adjustment will be done at final
1559      link time.  */
1560   if (output_bfd != NULL)
1561     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,      
1562                                   input_section, output_bfd, error_message);
1563
1564   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1565   if (TOCstart == 0)
1566     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
1567
1568   /* Subtract the TOC base address.  */
1569   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1570
1571   /* Adjust the addend for sign extension of the low 16 bits.  */
1572   reloc_entry->addend += 0x8000;
1573   return bfd_reloc_continue;
1574 }
1575
1576 static bfd_reloc_status_type
1577 ppc64_elf_toc64_reloc (abfd, reloc_entry, symbol, data,
1578                        input_section, output_bfd, error_message)
1579      bfd *abfd;
1580      arelent *reloc_entry;
1581      asymbol *symbol;
1582      PTR data;
1583      asection *input_section;
1584      bfd *output_bfd;
1585      char **error_message;
1586 {
1587   bfd_vma TOCstart;
1588   bfd_size_type octets;
1589
1590   /* If this is a relocatable link (output_bfd test tells us), just
1591      call the generic function.  Any adjustment will be done at final
1592      link time.  */
1593   if (output_bfd != NULL)
1594     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,      
1595                                   input_section, output_bfd, error_message);
1596
1597   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1598   if (TOCstart == 0)
1599     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
1600
1601   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1602   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1603   return bfd_reloc_ok;
1604 }
1605
1606 static bfd_reloc_status_type
1607 ppc64_elf_unhandled_reloc (abfd, reloc_entry, symbol, data,
1608                            input_section, output_bfd, error_message)
1609      bfd *abfd;
1610      arelent *reloc_entry;
1611      asymbol *symbol;
1612      PTR data;
1613      asection *input_section;
1614      bfd *output_bfd;
1615      char **error_message;
1616 {
1617   /* If this is a relocatable link (output_bfd test tells us), just
1618      call the generic function.  Any adjustment will be done at final
1619      link time.  */
1620   if (output_bfd != NULL)
1621     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,      
1622                                   input_section, output_bfd, error_message);
1623
1624   if (error_message != NULL)
1625     {
1626       static char buf[60];
1627       sprintf (buf, "generic linker can't handle %s",
1628                reloc_entry->howto->name);
1629       *error_message = buf;
1630     }
1631   return bfd_reloc_dangerous;
1632 }
1633
1634 /* Function to set whether a module needs the -mrelocatable bit set.  */
1635
1636 static boolean
1637 ppc64_elf_set_private_flags (abfd, flags)
1638      bfd *abfd;
1639      flagword flags;
1640 {
1641   BFD_ASSERT (!elf_flags_init (abfd)
1642               || elf_elfheader (abfd)->e_flags == flags);
1643
1644   elf_elfheader (abfd)->e_flags = flags;
1645   elf_flags_init (abfd) = true;
1646   return true;
1647 }
1648
1649 /* Merge backend specific data from an object file to the output
1650    object file when linking.  */
1651 static boolean
1652 ppc64_elf_merge_private_bfd_data (ibfd, obfd)
1653      bfd *ibfd;
1654      bfd *obfd;
1655 {
1656   flagword old_flags;
1657   flagword new_flags;
1658   boolean error;
1659
1660   /* Check if we have the same endianess.  */
1661   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1662       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1663     {
1664       const char *msg;
1665
1666       if (bfd_big_endian (ibfd))
1667         msg = _("%s: compiled for a big endian system and target is little endian");
1668       else
1669         msg = _("%s: compiled for a little endian system and target is big endian");
1670
1671       (*_bfd_error_handler) (msg, bfd_archive_filename (ibfd));
1672
1673       bfd_set_error (bfd_error_wrong_format);
1674       return false;
1675     }
1676
1677   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1678       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1679     return true;
1680
1681   new_flags = elf_elfheader (ibfd)->e_flags;
1682   old_flags = elf_elfheader (obfd)->e_flags;
1683   if (!elf_flags_init (obfd))
1684     {
1685       /* First call, no flags set.  */
1686       elf_flags_init (obfd) = true;
1687       elf_elfheader (obfd)->e_flags = new_flags;
1688     }
1689
1690   else if (new_flags == old_flags)
1691     /* Compatible flags are ok.  */
1692     ;
1693
1694   else
1695     {
1696       /* Incompatible flags.  Warn about -mrelocatable mismatch.
1697          Allow -mrelocatable-lib to be linked with either.  */
1698       error = false;
1699       if ((new_flags & EF_PPC_RELOCATABLE) != 0
1700           && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
1701         {
1702           error = true;
1703           (*_bfd_error_handler)
1704             (_("%s: compiled with -mrelocatable and linked with modules compiled normally"),
1705              bfd_archive_filename (ibfd));
1706         }
1707       else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
1708                && (old_flags & EF_PPC_RELOCATABLE) != 0)
1709         {
1710           error = true;
1711           (*_bfd_error_handler)
1712             (_("%s: compiled normally and linked with modules compiled with -mrelocatable"),
1713              bfd_archive_filename (ibfd));
1714         }
1715
1716       /* The output is -mrelocatable-lib iff both the input files are.  */
1717       if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
1718         elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
1719
1720       /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
1721          but each input file is either -mrelocatable or -mrelocatable-lib.  */
1722       if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
1723           && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
1724           && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
1725         elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
1726
1727       /* Do not warn about eabi vs. V.4 mismatch, just or in the bit
1728          if any module uses it.  */
1729       elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
1730
1731       new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1732       old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1733
1734       /* Warn about any other mismatches.  */
1735       if (new_flags != old_flags)
1736         {
1737           error = true;
1738           (*_bfd_error_handler)
1739             (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1740              bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
1741         }
1742
1743       if (error)
1744         {
1745           bfd_set_error (bfd_error_bad_value);
1746           return false;
1747         }
1748     }
1749
1750   return true;
1751 }
1752
1753 /* Handle a PowerPC specific section when reading an object file.  This
1754    is called when elfcode.h finds a section with an unknown type.  */
1755
1756 static boolean
1757 ppc64_elf_section_from_shdr (abfd, hdr, name)
1758      bfd *abfd;
1759      Elf64_Internal_Shdr *hdr;
1760      char *name;
1761 {
1762   asection *newsect;
1763   flagword flags;
1764
1765   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1766     return false;
1767
1768   newsect = hdr->bfd_section;
1769   flags = bfd_get_section_flags (abfd, newsect);
1770   if (hdr->sh_flags & SHF_EXCLUDE)
1771     flags |= SEC_EXCLUDE;
1772
1773   if (hdr->sh_type == SHT_ORDERED)
1774     flags |= SEC_SORT_ENTRIES;
1775
1776   bfd_set_section_flags (abfd, newsect, flags);
1777   return true;
1778 }
1779 \f
1780 /* The following functions are specific to the ELF linker, while
1781    functions above are used generally.  Those named ppc64_elf_* are
1782    called by the main ELF linker code.  They appear in this file more
1783    or less in the order in which they are called.  eg.
1784    ppc64_elf_check_relocs is called early in the link process,
1785    ppc64_elf_finish_dynamic_sections is one of the last functions
1786    called.
1787
1788    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
1789    functions have both a function code symbol and a function descriptor
1790    symbol.  A call to foo in a relocatable object file looks like:
1791
1792    .            .text
1793    .    x:
1794    .            bl      .foo
1795    .            nop
1796
1797    The function definition in another object file might be:
1798
1799    .            .section .opd
1800    .    foo:    .quad   .foo
1801    .            .quad   .TOC.@tocbase
1802    .            .quad   0
1803    .
1804    .            .text
1805    .    .foo:   blr
1806
1807    When the linker resolves the call during a static link, the branch
1808    unsurprisingly just goes to .foo and the .opd information is unused.
1809    If the function definition is in a shared library, things are a little
1810    different:  The call goes via a plt call stub, the opd information gets
1811    copied to the plt, and the linker patches the nop.
1812
1813    .    x:
1814    .            bl      .foo_stub
1815    .            ld      2,40(1)
1816    .
1817    .
1818    .    .foo_stub:
1819    .            addis   12,2,Lfoo@toc@ha        # in practice, the call stub
1820    .            addi    12,12,Lfoo@toc@l        # is slightly optimised, but
1821    .            std     2,40(1)                 # this is the general idea
1822    .            ld      11,0(12)
1823    .            ld      2,8(12)
1824    .            mtctr   11
1825    .            ld      11,16(12)
1826    .            bctr
1827    .
1828    .            .section .plt
1829    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
1830
1831    The "reloc ()" notation is supposed to indicate that the linker emits
1832    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
1833    copying.
1834
1835    What are the difficulties here?  Well, firstly, the relocations
1836    examined by the linker in check_relocs are against the function code
1837    sym .foo, while the dynamic relocation in the plt is emitted against
1838    the function descriptor symbol, foo.  Somewhere along the line, we need
1839    to carefully copy dynamic link information from one symbol to the other.
1840    Secondly, the generic part of the elf linker will make .foo a dynamic
1841    symbol as is normal for most other backends.  We need foo dynamic
1842    instead, at least for an application final link.  However, when
1843    creating a shared library containing foo, we need to have both symbols
1844    dynamic so that references to .foo are satisfied during the early
1845    stages of linking.  Otherwise the linker might decide to pull in a
1846    definition from some other object, eg. a static library.  */
1847
1848 /* The linker needs to keep track of the number of relocs that it
1849    decides to copy as dynamic relocs in check_relocs for each symbol.
1850    This is so that it can later discard them if they are found to be
1851    unnecessary.  We store the information in a field extending the
1852    regular ELF linker hash table.  */
1853
1854 struct ppc_dyn_relocs
1855 {
1856   struct ppc_dyn_relocs *next;
1857
1858   /* The input section of the reloc.  */
1859   asection *sec;
1860
1861   /* Total number of relocs copied for the input section.  */
1862   bfd_size_type count;
1863
1864   /* Number of pc-relative relocs copied for the input section.  */
1865   bfd_size_type pc_count;
1866 };
1867
1868 /* Of those relocs that might be copied as dynamic relocs, this macro
1869    selects between relative and absolute types.  */
1870
1871 #define IS_ABSOLUTE_RELOC(RTYPE)                \
1872   ((RTYPE) != R_PPC64_REL32                     \
1873    && (RTYPE) != R_PPC64_REL64                  \
1874    && (RTYPE) != R_PPC64_ADDR30)
1875
1876 /* Section name for stubs is the associated section name plus this
1877    string.  */
1878 #define STUB_SUFFIX ".stub"
1879
1880 /* Linker stubs.
1881    ppc_stub_long_branch:
1882    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
1883    destination, but a 24 bit branch in a stub section will reach.
1884    .    b       dest
1885
1886    ppc_stub_plt_branch:
1887    Similar to the above, but a 24 bit branch in the stub section won't
1888    reach its destination.
1889    .    addis   %r12,%r2,xxx@ha
1890    .    ld      %r11,xxx@l(%r12)
1891    .    mtctr   %r11
1892    .    bctr
1893
1894    ppc_stub_plt_call:
1895    Used to call a function in a shared library.
1896    .    addis   %r12,%r2,xxx@ha
1897    .    std     %r2,40(%r1)
1898    .    ld      %r11,xxx+0@l(%r12)
1899    .    ld      %r2,xxx+8@l(%r12)
1900    .    mtctr   %r11
1901    .    ld      %r11,xxx+16@l(%r12)
1902    .    bctr
1903 */
1904
1905 enum ppc_stub_type {
1906   ppc_stub_none,
1907   ppc_stub_long_branch,
1908   ppc_stub_plt_branch,
1909   ppc_stub_plt_call
1910 };
1911
1912 struct ppc_stub_hash_entry {
1913
1914   /* Base hash table entry structure.  */
1915   struct bfd_hash_entry root;
1916
1917   /* The stub section.  */
1918   asection *stub_sec;
1919
1920   /* Offset within stub_sec of the beginning of this stub.  */
1921   bfd_vma stub_offset;
1922
1923   /* Given the symbol's value and its section we can determine its final
1924      value when building the stubs (so the stub knows where to jump.  */
1925   bfd_vma target_value;
1926   asection *target_section;
1927
1928   enum ppc_stub_type stub_type;
1929
1930   /* The symbol table entry, if any, that this was derived from.  */
1931   struct ppc_link_hash_entry *h;
1932
1933   /* Where this stub is being called from, or, in the case of combined
1934      stub sections, the first input section in the group.  */
1935   asection *id_sec;
1936 };
1937
1938 struct ppc_branch_hash_entry {
1939
1940   /* Base hash table entry structure.  */
1941   struct bfd_hash_entry root;
1942
1943   /* Offset within .branch_lt.  */
1944   unsigned int offset;
1945
1946   /* Generation marker.  */
1947   unsigned int iter;
1948 };
1949
1950 struct ppc_link_hash_entry
1951 {
1952   struct elf_link_hash_entry elf;
1953
1954   /* A pointer to the most recently used stub hash entry against this
1955      symbol.  */
1956   struct ppc_stub_hash_entry *stub_cache;
1957
1958   /* Track dynamic relocs copied for this symbol.  */
1959   struct ppc_dyn_relocs *dyn_relocs;
1960
1961   /* Link between function code and descriptor symbols.  */
1962   struct elf_link_hash_entry *oh;
1963
1964   /* Flag function code and descriptor symbols.  */
1965   unsigned int is_func:1;
1966   unsigned int is_func_descriptor:1;
1967 };
1968
1969 /* ppc64 ELF linker hash table.  */
1970
1971 struct ppc_link_hash_table
1972 {
1973   struct elf_link_hash_table elf;
1974
1975   /* The stub hash table.  */
1976   struct bfd_hash_table stub_hash_table;
1977
1978   /* Another hash table for plt_branch stubs.  */
1979   struct bfd_hash_table branch_hash_table;
1980
1981   /* Linker stub bfd.  */
1982   bfd *stub_bfd;
1983
1984   /* Linker call-backs.  */
1985   asection * (*add_stub_section) PARAMS ((const char *, asection *));
1986   void (*layout_sections_again) PARAMS ((void));
1987
1988   /* Array to keep track of which stub sections have been created, and
1989      information on stub grouping.  */
1990   struct map_stub {
1991     /* This is the section to which stubs in the group will be attached.  */
1992     asection *link_sec;
1993     /* The stub section.  */
1994     asection *stub_sec;
1995   } *stub_group;
1996
1997   /* Assorted information used by ppc64_elf_size_stubs.  */
1998   unsigned int bfd_count;
1999   int top_index;
2000   asection **input_list;
2001   Elf_Internal_Sym **all_local_syms;
2002
2003   /* Short-cuts to get to dynamic linker sections.  */
2004   asection *sgot;
2005   asection *srelgot;
2006   asection *splt;
2007   asection *srelplt;
2008   asection *sdynbss;
2009   asection *srelbss;
2010   asection *sglink;
2011   asection *sfpr;
2012   asection *sbrlt;
2013   asection *srelbrlt;
2014
2015   /* Set on error.  */
2016   unsigned int stub_error;
2017
2018   /* Flag set when small branches are detected.  Used to
2019      select suitable defaults for the stub group size.  */
2020   unsigned int has_14bit_branch;
2021
2022   /* Set if we detect a reference undefined weak symbol.  */
2023   unsigned int have_undefweak;
2024
2025   /* Incremented every time we size stubs.  */
2026   unsigned int stub_iteration;
2027
2028   /* Small local sym to section mapping cache.  */
2029   struct sym_sec_cache sym_sec;
2030 };
2031
2032 static struct bfd_hash_entry *stub_hash_newfunc
2033   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2034 static struct bfd_hash_entry *branch_hash_newfunc
2035   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2036 static struct bfd_hash_entry *link_hash_newfunc
2037   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2038 static struct bfd_link_hash_table *ppc64_elf_link_hash_table_create
2039   PARAMS ((bfd *));
2040 static void ppc64_elf_link_hash_table_free
2041   PARAMS ((struct bfd_link_hash_table *));
2042 static char *ppc_stub_name
2043   PARAMS ((const asection *, const asection *,
2044            const struct ppc_link_hash_entry *, const Elf_Internal_Rela *));
2045 static struct ppc_stub_hash_entry *ppc_get_stub_entry
2046   PARAMS ((const asection *, const asection *, struct elf_link_hash_entry *,
2047            const Elf_Internal_Rela *, struct ppc_link_hash_table *));
2048 static struct ppc_stub_hash_entry *ppc_add_stub
2049   PARAMS ((const char *, asection *, struct ppc_link_hash_table *));
2050 static boolean create_linkage_sections
2051   PARAMS ((bfd *, struct bfd_link_info *));
2052 static boolean create_got_section
2053   PARAMS ((bfd *, struct bfd_link_info *));
2054 static boolean ppc64_elf_create_dynamic_sections
2055   PARAMS ((bfd *, struct bfd_link_info *));
2056 static void ppc64_elf_copy_indirect_symbol
2057   PARAMS ((struct elf_link_hash_entry *, struct elf_link_hash_entry *));
2058 static boolean ppc64_elf_check_relocs
2059   PARAMS ((bfd *, struct bfd_link_info *, asection *,
2060            const Elf_Internal_Rela *));
2061 static asection * ppc64_elf_gc_mark_hook
2062   PARAMS ((bfd *abfd, struct bfd_link_info *info, Elf_Internal_Rela *rel,
2063            struct elf_link_hash_entry *h, Elf_Internal_Sym *sym));
2064 static boolean ppc64_elf_gc_sweep_hook
2065   PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
2066            const Elf_Internal_Rela *relocs));
2067 static boolean func_desc_adjust
2068   PARAMS ((struct elf_link_hash_entry *, PTR));
2069 static boolean ppc64_elf_func_desc_adjust
2070   PARAMS ((bfd *, struct bfd_link_info *));
2071 static boolean ppc64_elf_adjust_dynamic_symbol
2072   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
2073 static void ppc64_elf_hide_symbol
2074   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, boolean));
2075 static boolean allocate_dynrelocs
2076   PARAMS ((struct elf_link_hash_entry *, PTR));
2077 static boolean readonly_dynrelocs
2078   PARAMS ((struct elf_link_hash_entry *, PTR));
2079 static enum elf_reloc_type_class ppc64_elf_reloc_type_class
2080   PARAMS ((const Elf_Internal_Rela *));
2081 static boolean ppc64_elf_size_dynamic_sections
2082   PARAMS ((bfd *, struct bfd_link_info *));
2083 static INLINE enum ppc_stub_type ppc_type_of_stub
2084   PARAMS ((asection *, const Elf_Internal_Rela *,
2085            struct ppc_link_hash_entry **, bfd_vma));
2086 static bfd_byte *build_plt_stub
2087   PARAMS ((bfd *, bfd_byte *, int, int));
2088 static boolean ppc_build_one_stub
2089   PARAMS ((struct bfd_hash_entry *, PTR));
2090 static boolean ppc_size_one_stub
2091   PARAMS ((struct bfd_hash_entry *, PTR));
2092 static void group_sections
2093   PARAMS ((struct ppc_link_hash_table *, bfd_size_type, boolean));
2094 static boolean get_local_syms
2095   PARAMS ((bfd *, struct ppc_link_hash_table *));
2096 static boolean ppc64_elf_fake_sections
2097   PARAMS ((bfd *, Elf64_Internal_Shdr *, asection *));
2098 static boolean ppc64_elf_relocate_section
2099   PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
2100            Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
2101            asection **));
2102 static boolean ppc64_elf_finish_dynamic_symbol
2103   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
2104            Elf_Internal_Sym *));
2105 static boolean ppc64_elf_finish_dynamic_sections
2106   PARAMS ((bfd *, struct bfd_link_info *));
2107
2108 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
2109
2110 #define ppc_hash_table(p) \
2111   ((struct ppc_link_hash_table *) ((p)->hash))
2112
2113 #define ppc_stub_hash_lookup(table, string, create, copy) \
2114   ((struct ppc_stub_hash_entry *) \
2115    bfd_hash_lookup ((table), (string), (create), (copy)))
2116
2117 #define ppc_branch_hash_lookup(table, string, create, copy) \
2118   ((struct ppc_branch_hash_entry *) \
2119    bfd_hash_lookup ((table), (string), (create), (copy)))
2120
2121 /* Create an entry in the stub hash table.  */
2122
2123 static struct bfd_hash_entry *
2124 stub_hash_newfunc (entry, table, string)
2125      struct bfd_hash_entry *entry;
2126      struct bfd_hash_table *table;
2127      const char *string;
2128 {
2129   /* Allocate the structure if it has not already been allocated by a
2130      subclass.  */
2131   if (entry == NULL)
2132     {
2133       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
2134       if (entry == NULL)
2135         return entry;
2136     }
2137
2138   /* Call the allocation method of the superclass.  */
2139   entry = bfd_hash_newfunc (entry, table, string);
2140   if (entry != NULL)
2141     {
2142       struct ppc_stub_hash_entry *eh;
2143
2144       /* Initialize the local fields.  */
2145       eh = (struct ppc_stub_hash_entry *) entry;
2146       eh->stub_sec = NULL;
2147       eh->stub_offset = 0;
2148       eh->target_value = 0;
2149       eh->target_section = NULL;
2150       eh->stub_type = ppc_stub_none;
2151       eh->h = NULL;
2152       eh->id_sec = NULL;
2153     }
2154
2155   return entry;
2156 }
2157
2158 /* Create an entry in the branch hash table.  */
2159
2160 static struct bfd_hash_entry *
2161 branch_hash_newfunc (entry, table, string)
2162      struct bfd_hash_entry *entry;
2163      struct bfd_hash_table *table;
2164      const char *string;
2165 {
2166   /* Allocate the structure if it has not already been allocated by a
2167      subclass.  */
2168   if (entry == NULL)
2169     {
2170       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
2171       if (entry == NULL)
2172         return entry;
2173     }
2174
2175   /* Call the allocation method of the superclass.  */
2176   entry = bfd_hash_newfunc (entry, table, string);
2177   if (entry != NULL)
2178     {
2179       struct ppc_branch_hash_entry *eh;
2180
2181       /* Initialize the local fields.  */
2182       eh = (struct ppc_branch_hash_entry *) entry;
2183       eh->offset = 0;
2184       eh->iter = 0;
2185     }
2186
2187   return entry;
2188 }
2189
2190 /* Create an entry in a ppc64 ELF linker hash table.  */
2191
2192 static struct bfd_hash_entry *
2193 link_hash_newfunc (entry, table, string)
2194      struct bfd_hash_entry *entry;
2195      struct bfd_hash_table *table;
2196      const char *string;
2197 {
2198   /* Allocate the structure if it has not already been allocated by a
2199      subclass.  */
2200   if (entry == NULL)
2201     {
2202       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
2203       if (entry == NULL)
2204         return entry;
2205     }
2206
2207   /* Call the allocation method of the superclass.  */
2208   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2209   if (entry != NULL)
2210     {
2211       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
2212
2213       eh->stub_cache = NULL;
2214       eh->dyn_relocs = NULL;
2215       eh->oh = NULL;
2216       eh->is_func = 0;
2217       eh->is_func_descriptor = 0;
2218     }
2219
2220   return entry;
2221 }
2222
2223 /* Create a ppc64 ELF linker hash table.  */
2224
2225 static struct bfd_link_hash_table *
2226 ppc64_elf_link_hash_table_create (abfd)
2227      bfd *abfd;
2228 {
2229   struct ppc_link_hash_table *htab;
2230   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
2231
2232   htab = (struct ppc_link_hash_table *) bfd_malloc (amt);
2233   if (htab == NULL)
2234     return NULL;
2235
2236   if (! _bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc))
2237     {
2238       free (htab);
2239       return NULL;
2240     }
2241
2242   /* Init the stub hash table too.  */
2243   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc))
2244     return NULL;
2245
2246   /* And the branch hash table.  */
2247   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc))
2248     return NULL;
2249
2250   htab->stub_bfd = NULL;
2251   htab->add_stub_section = NULL;
2252   htab->layout_sections_again = NULL;
2253   htab->stub_group = NULL;
2254   htab->sgot = NULL;
2255   htab->srelgot = NULL;
2256   htab->splt = NULL;
2257   htab->srelplt = NULL;
2258   htab->sdynbss = NULL;
2259   htab->srelbss = NULL;
2260   htab->sglink = NULL;
2261   htab->sfpr = NULL;
2262   htab->sbrlt = NULL;
2263   htab->srelbrlt = NULL;
2264   htab->stub_error = 0;
2265   htab->has_14bit_branch = 0;
2266   htab->have_undefweak = 0;
2267   htab->stub_iteration = 0;
2268   htab->sym_sec.abfd = NULL;
2269
2270   return &htab->elf.root;
2271 }
2272
2273 /* Free the derived linker hash table.  */
2274
2275 static void
2276 ppc64_elf_link_hash_table_free (hash)
2277      struct bfd_link_hash_table *hash;
2278 {
2279   struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
2280
2281   bfd_hash_table_free (&ret->stub_hash_table);
2282   bfd_hash_table_free (&ret->branch_hash_table);
2283   _bfd_generic_link_hash_table_free (hash);
2284 }
2285
2286 /* Build a name for an entry in the stub hash table.  */
2287
2288 static char *
2289 ppc_stub_name (input_section, sym_sec, h, rel)
2290      const asection *input_section;
2291      const asection *sym_sec;
2292      const struct ppc_link_hash_entry *h;
2293      const Elf_Internal_Rela *rel;
2294 {
2295   char *stub_name;
2296   bfd_size_type len;
2297
2298   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
2299      offsets from a sym as a branch target?  In fact, we could
2300      probably assume the addend is always zero.  */
2301   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
2302
2303   if (h)
2304     {
2305       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
2306       stub_name = bfd_malloc (len);
2307       if (stub_name != NULL)
2308         {
2309           sprintf (stub_name, "%08x_%s+%x",
2310                    input_section->id & 0xffffffff,
2311                    h->elf.root.root.string,
2312                    (int) rel->r_addend & 0xffffffff);
2313         }
2314     }
2315   else
2316     {
2317       len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
2318       stub_name = bfd_malloc (len);
2319       if (stub_name != NULL)
2320         {
2321           sprintf (stub_name, "%08x_%x:%x+%x",
2322                    input_section->id & 0xffffffff,
2323                    sym_sec->id & 0xffffffff,
2324                    (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
2325                    (int) rel->r_addend & 0xffffffff);
2326         }
2327     }
2328   return stub_name;
2329 }
2330
2331 /* Look up an entry in the stub hash.  Stub entries are cached because
2332    creating the stub name takes a bit of time.  */
2333
2334 static struct ppc_stub_hash_entry *
2335 ppc_get_stub_entry (input_section, sym_sec, hash, rel, htab)
2336      const asection *input_section;
2337      const asection *sym_sec;
2338      struct elf_link_hash_entry *hash;
2339      const Elf_Internal_Rela *rel;
2340      struct ppc_link_hash_table *htab;
2341 {
2342   struct ppc_stub_hash_entry *stub_entry;
2343   struct ppc_link_hash_entry *h = (struct ppc_link_hash_entry *) hash;
2344   const asection *id_sec;
2345
2346   /* If this input section is part of a group of sections sharing one
2347      stub section, then use the id of the first section in the group.
2348      Stub names need to include a section id, as there may well be
2349      more than one stub used to reach say, printf, and we need to
2350      distinguish between them.  */
2351   id_sec = htab->stub_group[input_section->id].link_sec;
2352
2353   if (h != NULL && h->stub_cache != NULL
2354       && h->stub_cache->h == h
2355       && h->stub_cache->id_sec == id_sec)
2356     {
2357       stub_entry = h->stub_cache;
2358     }
2359   else
2360     {
2361       char *stub_name;
2362
2363       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
2364       if (stub_name == NULL)
2365         return NULL;
2366
2367       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
2368                                          stub_name, false, false);
2369       if (h != NULL)
2370         h->stub_cache = stub_entry;
2371
2372       free (stub_name);
2373     }
2374
2375   return stub_entry;
2376 }
2377
2378 /* Add a new stub entry to the stub hash.  Not all fields of the new
2379    stub entry are initialised.  */
2380
2381 static struct ppc_stub_hash_entry *
2382 ppc_add_stub (stub_name, section, htab)
2383      const char *stub_name;
2384      asection *section;
2385      struct ppc_link_hash_table *htab;
2386 {
2387   asection *link_sec;
2388   asection *stub_sec;
2389   struct ppc_stub_hash_entry *stub_entry;
2390
2391   link_sec = htab->stub_group[section->id].link_sec;
2392   stub_sec = htab->stub_group[section->id].stub_sec;
2393   if (stub_sec == NULL)
2394     {
2395       stub_sec = htab->stub_group[link_sec->id].stub_sec;
2396       if (stub_sec == NULL)
2397         {
2398           bfd_size_type len;
2399           char *s_name;
2400
2401           len = strlen (link_sec->name) + sizeof (STUB_SUFFIX);
2402           s_name = bfd_alloc (htab->stub_bfd, len);
2403           if (s_name == NULL)
2404             return NULL;
2405
2406           strcpy (s_name, link_sec->name);
2407           strcpy (s_name + len - sizeof (STUB_SUFFIX), STUB_SUFFIX);
2408           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
2409           if (stub_sec == NULL)
2410             return NULL;
2411           htab->stub_group[link_sec->id].stub_sec = stub_sec;
2412         }
2413       htab->stub_group[section->id].stub_sec = stub_sec;
2414     }
2415
2416   /* Enter this entry into the linker stub hash table.  */
2417   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
2418                                      true, false);
2419   if (stub_entry == NULL)
2420     {
2421       (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
2422                              bfd_archive_filename (section->owner),
2423                              stub_name);
2424       return NULL;
2425     }
2426
2427   stub_entry->stub_sec = stub_sec;
2428   stub_entry->stub_offset = 0;
2429   stub_entry->id_sec = link_sec;
2430   return stub_entry;
2431 }
2432
2433 /* Create sections for linker generated code.  */
2434
2435 static boolean
2436 create_linkage_sections (dynobj, info)
2437      bfd *dynobj;
2438      struct bfd_link_info *info;
2439 {
2440   struct ppc_link_hash_table *htab;
2441   flagword flags;
2442
2443   htab = ppc_hash_table (info);
2444
2445   /* Create .sfpr for code to save and restore fp regs.  */
2446   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2447            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2448   htab->sfpr = bfd_make_section_anyway (dynobj, ".sfpr");
2449   if (htab->sfpr == NULL
2450       || ! bfd_set_section_flags (dynobj, htab->sfpr, flags)
2451       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
2452     return false;
2453
2454   /* Create .glink for lazy dynamic linking support.  */
2455   htab->sglink = bfd_make_section_anyway (dynobj, ".glink");
2456   if (htab->sglink == NULL
2457       || ! bfd_set_section_flags (dynobj, htab->sglink, flags)
2458       || ! bfd_set_section_alignment (dynobj, htab->sglink, 2))
2459     return false;
2460
2461   /* Create .branch_lt for plt_branch stubs.  */
2462   flags = (SEC_ALLOC | SEC_LOAD
2463            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2464   htab->sbrlt = bfd_make_section_anyway (dynobj, ".branch_lt");
2465   if (htab->sbrlt == NULL
2466       || ! bfd_set_section_flags (dynobj, htab->sbrlt, flags)
2467       || ! bfd_set_section_alignment (dynobj, htab->sbrlt, 3))
2468     return false;
2469
2470   if (info->shared)
2471     {
2472       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
2473                | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2474       htab->srelbrlt = bfd_make_section (dynobj, ".rela.branch_lt");
2475       if (!htab->srelbrlt
2476           || ! bfd_set_section_flags (dynobj, htab->srelbrlt, flags)
2477           || ! bfd_set_section_alignment (dynobj, htab->srelbrlt, 3))
2478         return false;
2479     }
2480   return true;
2481 }
2482
2483 /* Create .got and .rela.got sections in DYNOBJ, and set up
2484    shortcuts to them in our hash table.  */
2485
2486 static boolean
2487 create_got_section (dynobj, info)
2488      bfd *dynobj;
2489      struct bfd_link_info *info;
2490 {
2491   struct ppc_link_hash_table *htab;
2492
2493   if (! _bfd_elf_create_got_section (dynobj, info))
2494     return false;
2495
2496   htab = ppc_hash_table (info);
2497   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
2498   if (!htab->sgot)
2499     abort ();
2500
2501   htab->srelgot = bfd_make_section (dynobj, ".rela.got");
2502   if (!htab->srelgot
2503       || ! bfd_set_section_flags (dynobj, htab->srelgot,
2504                                   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2505                                    | SEC_IN_MEMORY | SEC_LINKER_CREATED
2506                                    | SEC_READONLY))
2507       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
2508     return false;
2509   return true;
2510 }
2511
2512 /* Create the dynamic sections, and set up shortcuts.  */
2513
2514 static boolean
2515 ppc64_elf_create_dynamic_sections (dynobj, info)
2516      bfd *dynobj;
2517      struct bfd_link_info *info;
2518 {
2519   struct ppc_link_hash_table *htab;
2520
2521   htab = ppc_hash_table (info);
2522   if (!htab->sgot && !create_got_section (dynobj, info))
2523     return false;
2524
2525   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
2526     return false;
2527
2528   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
2529   htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
2530   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
2531   if (!info->shared)
2532     htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
2533
2534   if (!htab->splt || !htab->srelplt || !htab->sdynbss
2535       || (!info->shared && !htab->srelbss))
2536     abort ();
2537
2538   return true;
2539 }
2540
2541 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
2542
2543 static void
2544 ppc64_elf_copy_indirect_symbol (dir, ind)
2545      struct elf_link_hash_entry *dir, *ind;
2546 {
2547   struct ppc_link_hash_entry *edir, *eind;
2548
2549   edir = (struct ppc_link_hash_entry *) dir;
2550   eind = (struct ppc_link_hash_entry *) ind;
2551
2552   if (eind->dyn_relocs != NULL)
2553     {
2554       if (edir->dyn_relocs != NULL)
2555         {
2556           struct ppc_dyn_relocs **pp;
2557           struct ppc_dyn_relocs *p;
2558
2559           if (ind->root.type == bfd_link_hash_indirect)
2560             abort ();
2561
2562           /* Add reloc counts against the weak sym to the strong sym
2563              list.  Merge any entries against the same section.  */
2564           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2565             {
2566               struct ppc_dyn_relocs *q;
2567
2568               for (q = edir->dyn_relocs; q != NULL; q = q->next)
2569                 if (q->sec == p->sec)
2570                   {
2571                     q->pc_count += p->pc_count;
2572                     q->count += p->count;
2573                     *pp = p->next;
2574                     break;
2575                   }
2576               if (q == NULL)
2577                 pp = &p->next;
2578             }
2579           *pp = edir->dyn_relocs;
2580         }
2581
2582       edir->dyn_relocs = eind->dyn_relocs;
2583       eind->dyn_relocs = NULL;
2584     }
2585
2586   edir->is_func |= eind->is_func;
2587   edir->is_func_descriptor |= eind->is_func_descriptor;
2588
2589   _bfd_elf_link_hash_copy_indirect (dir, ind);
2590 }
2591
2592 /* Look through the relocs for a section during the first phase, and
2593    calculate needed space in the global offset table, procedure
2594    linkage table, and dynamic reloc sections.  */
2595
2596 static boolean
2597 ppc64_elf_check_relocs (abfd, info, sec, relocs)
2598      bfd *abfd;
2599      struct bfd_link_info *info;
2600      asection *sec;
2601      const Elf_Internal_Rela *relocs;
2602 {
2603   struct ppc_link_hash_table *htab;
2604   Elf_Internal_Shdr *symtab_hdr;
2605   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2606   const Elf_Internal_Rela *rel;
2607   const Elf_Internal_Rela *rel_end;
2608   asection *sreloc;
2609   boolean is_opd;
2610
2611   if (info->relocateable)
2612     return true;
2613
2614   htab = ppc_hash_table (info);
2615   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2616
2617   sym_hashes = elf_sym_hashes (abfd);
2618   sym_hashes_end = (sym_hashes
2619                     + symtab_hdr->sh_size / sizeof (Elf64_External_Sym));
2620   if (!elf_bad_symtab (abfd))
2621     sym_hashes_end -= symtab_hdr->sh_info;
2622
2623   sreloc = NULL;
2624   is_opd = strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0;
2625
2626   if (htab->elf.dynobj == NULL)
2627     htab->elf.dynobj = abfd;
2628   if (htab->sfpr == NULL
2629       && !create_linkage_sections (htab->elf.dynobj, info))
2630     return false;
2631
2632   rel_end = relocs + sec->reloc_count;
2633   for (rel = relocs; rel < rel_end; rel++)
2634     {
2635       unsigned long r_symndx;
2636       struct elf_link_hash_entry *h;
2637       enum elf_ppc_reloc_type r_type;
2638
2639       r_symndx = ELF64_R_SYM (rel->r_info);
2640       if (r_symndx < symtab_hdr->sh_info)
2641         h = NULL;
2642       else
2643         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2644
2645       r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
2646       switch (r_type)
2647         {
2648           /* GOT16 relocations */
2649         case R_PPC64_GOT16:
2650         case R_PPC64_GOT16_DS:
2651         case R_PPC64_GOT16_HA:
2652         case R_PPC64_GOT16_HI:
2653         case R_PPC64_GOT16_LO:
2654         case R_PPC64_GOT16_LO_DS:
2655
2656           /* This symbol requires a global offset table entry.  */
2657           if (htab->sgot == NULL
2658               && !create_got_section (htab->elf.dynobj, info))
2659             return false;
2660
2661           if (h != NULL)
2662             {
2663               h->got.refcount += 1;
2664             }
2665           else
2666             {
2667               bfd_signed_vma *local_got_refcounts;
2668
2669               /* This is a global offset table entry for a local symbol.  */
2670               local_got_refcounts = elf_local_got_refcounts (abfd);
2671               if (local_got_refcounts == NULL)
2672                 {
2673                   bfd_size_type size;
2674
2675                   size = symtab_hdr->sh_info;
2676                   size *= sizeof (bfd_signed_vma);
2677                   local_got_refcounts = ((bfd_signed_vma *)
2678                                          bfd_zalloc (abfd, size));
2679                   if (local_got_refcounts == NULL)
2680                     return false;
2681                   elf_local_got_refcounts (abfd) = local_got_refcounts;
2682                 }
2683               local_got_refcounts[r_symndx] += 1;
2684             }
2685           break;
2686
2687         case R_PPC64_PLT16_HA:
2688         case R_PPC64_PLT16_HI:
2689         case R_PPC64_PLT16_LO:
2690         case R_PPC64_PLT32:
2691         case R_PPC64_PLT64:
2692           /* This symbol requires a procedure linkage table entry.  We
2693              actually build the entry in adjust_dynamic_symbol,
2694              because this might be a case of linking PIC code without
2695              linking in any dynamic objects, in which case we don't
2696              need to generate a procedure linkage table after all.  */
2697           if (h == NULL)
2698             {
2699               /* It does not make sense to have a procedure linkage
2700                  table entry for a local symbol.  */
2701               bfd_set_error (bfd_error_bad_value);
2702               return false;
2703             }
2704
2705           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2706           h->plt.refcount += 1;
2707           ((struct ppc_link_hash_entry *) h)->is_func = 1;
2708           break;
2709
2710           /* The following relocations don't need to propagate the
2711              relocation if linking a shared object since they are
2712              section relative.  */
2713         case R_PPC64_SECTOFF:
2714         case R_PPC64_SECTOFF_LO:
2715         case R_PPC64_SECTOFF_HI:
2716         case R_PPC64_SECTOFF_HA:
2717         case R_PPC64_SECTOFF_DS:
2718         case R_PPC64_SECTOFF_LO_DS:
2719         case R_PPC64_TOC16:
2720         case R_PPC64_TOC16_LO:
2721         case R_PPC64_TOC16_HI:
2722         case R_PPC64_TOC16_HA:
2723         case R_PPC64_TOC16_DS:
2724         case R_PPC64_TOC16_LO_DS:
2725           break;
2726
2727           /* This relocation describes the C++ object vtable hierarchy.
2728              Reconstruct it for later use during GC.  */
2729         case R_PPC64_GNU_VTINHERIT:
2730           if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2731             return false;
2732           break;
2733
2734           /* This relocation describes which C++ vtable entries are actually
2735              used.  Record for later use during GC.  */
2736         case R_PPC64_GNU_VTENTRY:
2737           if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2738             return false;
2739           break;
2740
2741         case R_PPC64_REL14:
2742         case R_PPC64_REL14_BRTAKEN:
2743         case R_PPC64_REL14_BRNTAKEN:
2744           htab->has_14bit_branch = 1;
2745           /* Fall through.  */
2746
2747         case R_PPC64_REL24:
2748           if (h != NULL
2749               && h->root.root.string[0] == '.'
2750               && h->root.root.string[1] != 0)
2751             {
2752               /* We may need a .plt entry if the function this reloc
2753                  refers to is in a shared lib.  */
2754               h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2755               h->plt.refcount += 1;
2756               ((struct ppc_link_hash_entry *) h)->is_func = 1;
2757             }
2758           break;
2759
2760         case R_PPC64_ADDR64:
2761           if (is_opd
2762               && h != NULL
2763               && h->root.root.string[0] == '.'
2764               && h->root.root.string[1] != 0)
2765             {
2766               struct elf_link_hash_entry *fdh;
2767
2768               fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
2769                                           false, false, false);
2770               if (fdh != NULL)
2771                 {
2772                   /* Ensure the function descriptor symbol string is
2773                      part of the code symbol string.  We aren't
2774                      changing the name here, just allowing some tricks
2775                      in ppc64_elf_hide_symbol.  */
2776                   fdh->root.root.string = h->root.root.string + 1;
2777                   ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
2778                   ((struct ppc_link_hash_entry *) fdh)->oh = h;
2779                   ((struct ppc_link_hash_entry *) h)->is_func = 1;
2780                   ((struct ppc_link_hash_entry *) h)->oh = fdh;
2781                 }
2782             }
2783           /* Fall through.  */
2784
2785         case R_PPC64_REL64:
2786         case R_PPC64_REL32:
2787         case R_PPC64_ADDR14:
2788         case R_PPC64_ADDR14_BRNTAKEN:
2789         case R_PPC64_ADDR14_BRTAKEN:
2790         case R_PPC64_ADDR16:
2791         case R_PPC64_ADDR16_DS:
2792         case R_PPC64_ADDR16_HA:
2793         case R_PPC64_ADDR16_HI:
2794         case R_PPC64_ADDR16_HIGHER:
2795         case R_PPC64_ADDR16_HIGHERA:
2796         case R_PPC64_ADDR16_HIGHEST:
2797         case R_PPC64_ADDR16_HIGHESTA:
2798         case R_PPC64_ADDR16_LO:
2799         case R_PPC64_ADDR16_LO_DS:
2800         case R_PPC64_ADDR24:
2801         case R_PPC64_ADDR30:
2802         case R_PPC64_ADDR32:
2803         case R_PPC64_UADDR16:
2804         case R_PPC64_UADDR32:
2805         case R_PPC64_UADDR64:
2806         case R_PPC64_TOC:
2807           /* Don't propagate .opd relocs.  */
2808           if (NO_OPD_RELOCS && is_opd)
2809             break;
2810
2811           /* If we are creating a shared library, and this is a reloc
2812              against a global symbol, or a non PC relative reloc
2813              against a local symbol, then we need to copy the reloc
2814              into the shared library.  However, if we are linking with
2815              -Bsymbolic, we do not need to copy a reloc against a
2816              global symbol which is defined in an object we are
2817              including in the link (i.e., DEF_REGULAR is set).  At
2818              this point we have not seen all the input files, so it is
2819              possible that DEF_REGULAR is not set now but will be set
2820              later (it is never cleared).  In case of a weak definition,
2821              DEF_REGULAR may be cleared later by a strong definition in
2822              a shared library.  We account for that possibility below by
2823              storing information in the relocs_copied field of the hash
2824              table entry.  A similar situation occurs when creating
2825              shared libraries and symbol visibility changes render the
2826              symbol local.
2827
2828              If on the other hand, we are creating an executable, we
2829              may need to keep relocations for symbols satisfied by a
2830              dynamic library if we manage to avoid copy relocs for the
2831              symbol.  */
2832           if ((info->shared
2833                && (sec->flags & SEC_ALLOC) != 0
2834                && (IS_ABSOLUTE_RELOC (r_type)
2835                    || (h != NULL
2836                        && (! info->symbolic
2837                            || h->root.type == bfd_link_hash_defweak
2838                            || (h->elf_link_hash_flags
2839                                & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2840               || (!info->shared
2841                   && (sec->flags & SEC_ALLOC) != 0
2842                   && h != NULL
2843                   && (h->root.type == bfd_link_hash_defweak
2844                       || (h->elf_link_hash_flags
2845                           & ELF_LINK_HASH_DEF_REGULAR) == 0)))
2846             {
2847               struct ppc_dyn_relocs *p;
2848               struct ppc_dyn_relocs **head;
2849
2850               /* We must copy these reloc types into the output file.
2851                  Create a reloc section in dynobj and make room for
2852                  this reloc.  */
2853               if (sreloc == NULL)
2854                 {
2855                   const char *name;
2856                   bfd *dynobj;
2857
2858                   name = (bfd_elf_string_from_elf_section
2859                           (abfd,
2860                            elf_elfheader (abfd)->e_shstrndx,
2861                            elf_section_data (sec)->rel_hdr.sh_name));
2862                   if (name == NULL)
2863                     return false;
2864
2865                   if (strncmp (name, ".rela", 5) != 0
2866                       || strcmp (bfd_get_section_name (abfd, sec),
2867                                  name + 5) != 0)
2868                     {
2869                       (*_bfd_error_handler)
2870                         (_("%s: bad relocation section name `%s\'"),
2871                          bfd_archive_filename (abfd), name);
2872                       bfd_set_error (bfd_error_bad_value);
2873                     }
2874
2875                   dynobj = htab->elf.dynobj;
2876                   sreloc = bfd_get_section_by_name (dynobj, name);
2877                   if (sreloc == NULL)
2878                     {
2879                       flagword flags;
2880
2881                       sreloc = bfd_make_section (dynobj, name);
2882                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
2883                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2884                       if ((sec->flags & SEC_ALLOC) != 0)
2885                         flags |= SEC_ALLOC | SEC_LOAD;
2886                       if (sreloc == NULL
2887                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
2888                           || ! bfd_set_section_alignment (dynobj, sreloc, 3))
2889                         return false;
2890                     }
2891                   elf_section_data (sec)->sreloc = sreloc;
2892                 }
2893
2894               /* If this is a global symbol, we count the number of
2895                  relocations we need for this symbol.  */
2896               if (h != NULL)
2897                 {
2898                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
2899                 }
2900               else
2901                 {
2902                   /* Track dynamic relocs needed for local syms too.
2903                      We really need local syms available to do this
2904                      easily.  Oh well.  */
2905
2906                   asection *s;
2907                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
2908                                                  sec, r_symndx);
2909                   if (s == NULL)
2910                     return false;
2911
2912                   head = ((struct ppc_dyn_relocs **)
2913                           &elf_section_data (s)->local_dynrel);
2914                 }
2915
2916               p = *head;
2917               if (p == NULL || p->sec != sec)
2918                 {
2919                   p = ((struct ppc_dyn_relocs *)
2920                        bfd_alloc (htab->elf.dynobj,
2921                                   (bfd_size_type) sizeof *p));
2922                   if (p == NULL)
2923                     return false;
2924                   p->next = *head;
2925                   *head = p;
2926                   p->sec = sec;
2927                   p->count = 0;
2928                   p->pc_count = 0;
2929                 }
2930
2931               p->count += 1;
2932               if (!IS_ABSOLUTE_RELOC (r_type))
2933                 p->pc_count += 1;
2934             }
2935           break;
2936
2937         default:
2938           break;
2939         }
2940     }
2941
2942   return true;
2943 }
2944
2945 /* Return the section that should be marked against GC for a given
2946    relocation.  */
2947
2948 static asection *
2949 ppc64_elf_gc_mark_hook (abfd, info, rel, h, sym)
2950      bfd *abfd;
2951      struct bfd_link_info *info ATTRIBUTE_UNUSED;
2952      Elf_Internal_Rela *rel;
2953      struct elf_link_hash_entry *h;
2954      Elf_Internal_Sym *sym;
2955 {
2956   if (h != NULL)
2957     {
2958       enum elf_ppc_reloc_type r_type;
2959
2960       r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
2961       switch (r_type)
2962         {
2963         case R_PPC64_GNU_VTINHERIT:
2964         case R_PPC64_GNU_VTENTRY:
2965           break;
2966
2967         default:
2968           switch (h->root.type)
2969             {
2970             case bfd_link_hash_defined:
2971             case bfd_link_hash_defweak:
2972               return h->root.u.def.section;
2973
2974             case bfd_link_hash_common:
2975               return h->root.u.c.p->section;
2976
2977             default:
2978               break;
2979             }
2980         }
2981     }
2982   else
2983     {
2984       return bfd_section_from_elf_index (abfd, sym->st_shndx);
2985     }
2986
2987   return NULL;
2988 }
2989
2990 /* Update the .got, .plt. and dynamic reloc reference counts for the
2991    section being removed.  */
2992
2993 static boolean
2994 ppc64_elf_gc_sweep_hook (abfd, info, sec, relocs)
2995      bfd *abfd;
2996      struct bfd_link_info *info ATTRIBUTE_UNUSED;
2997      asection *sec;
2998      const Elf_Internal_Rela *relocs;
2999 {
3000   Elf_Internal_Shdr *symtab_hdr;
3001   struct elf_link_hash_entry **sym_hashes;
3002   bfd_signed_vma *local_got_refcounts;
3003   const Elf_Internal_Rela *rel, *relend;
3004
3005   elf_section_data (sec)->local_dynrel = NULL;
3006
3007   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3008   sym_hashes = elf_sym_hashes (abfd);
3009   local_got_refcounts = elf_local_got_refcounts (abfd);
3010
3011   relend = relocs + sec->reloc_count;
3012   for (rel = relocs; rel < relend; rel++)
3013     {
3014       unsigned long r_symndx;
3015       enum elf_ppc_reloc_type r_type;
3016       struct elf_link_hash_entry *h;
3017
3018       r_symndx = ELF64_R_SYM (rel->r_info);
3019       r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
3020       switch (r_type)
3021         {
3022         case R_PPC64_GOT16:
3023         case R_PPC64_GOT16_DS:
3024         case R_PPC64_GOT16_HA:
3025         case R_PPC64_GOT16_HI:
3026         case R_PPC64_GOT16_LO:
3027         case R_PPC64_GOT16_LO_DS:
3028           if (r_symndx >= symtab_hdr->sh_info)
3029             {
3030               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3031               if (h->got.refcount > 0)
3032                 h->got.refcount--;
3033             }
3034           else
3035             {
3036               if (local_got_refcounts[r_symndx] > 0)
3037                 local_got_refcounts[r_symndx]--;
3038             }
3039           break;
3040
3041         case R_PPC64_PLT16_HA:
3042         case R_PPC64_PLT16_HI:
3043         case R_PPC64_PLT16_LO:
3044         case R_PPC64_PLT32:
3045         case R_PPC64_PLT64:
3046           if (r_symndx >= symtab_hdr->sh_info)
3047             {
3048               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3049               if (h->plt.refcount > 0)
3050                 h->plt.refcount--;
3051             }
3052           break;
3053
3054         case R_PPC64_REL14:
3055         case R_PPC64_REL14_BRNTAKEN:
3056         case R_PPC64_REL14_BRTAKEN:
3057         case R_PPC64_REL24:
3058           if (r_symndx >= symtab_hdr->sh_info)
3059             {
3060               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3061               if (h->plt.refcount > 0)
3062                 h->plt.refcount--;
3063             }
3064           break;
3065
3066         case R_PPC64_REL32:
3067         case R_PPC64_REL64:
3068           if (r_symndx >= symtab_hdr->sh_info)
3069             {
3070               struct ppc_link_hash_entry *eh;
3071               struct ppc_dyn_relocs **pp;
3072               struct ppc_dyn_relocs *p;
3073
3074               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3075               eh = (struct ppc_link_hash_entry *) h;
3076
3077               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3078                 if (p->sec == sec)
3079                   {
3080                     p->pc_count -= 1;
3081                     p->count -= 1;
3082                     if (p->count == 0)
3083                       *pp = p->next;
3084                     break;
3085                   }
3086             }
3087           break;
3088
3089         case R_PPC64_ADDR14:
3090         case R_PPC64_ADDR14_BRNTAKEN:
3091         case R_PPC64_ADDR14_BRTAKEN:
3092         case R_PPC64_ADDR16:
3093         case R_PPC64_ADDR16_DS:
3094         case R_PPC64_ADDR16_HA:
3095         case R_PPC64_ADDR16_HI:
3096         case R_PPC64_ADDR16_HIGHER:
3097         case R_PPC64_ADDR16_HIGHERA:
3098         case R_PPC64_ADDR16_HIGHEST:
3099         case R_PPC64_ADDR16_HIGHESTA:
3100         case R_PPC64_ADDR16_LO:
3101         case R_PPC64_ADDR16_LO_DS:
3102         case R_PPC64_ADDR24:
3103         case R_PPC64_ADDR30:
3104         case R_PPC64_ADDR32:
3105         case R_PPC64_ADDR64:
3106         case R_PPC64_UADDR16:
3107         case R_PPC64_UADDR32:
3108         case R_PPC64_UADDR64:
3109         case R_PPC64_TOC:
3110           if (r_symndx >= symtab_hdr->sh_info)
3111             {
3112               struct ppc_link_hash_entry *eh;
3113               struct ppc_dyn_relocs **pp;
3114               struct ppc_dyn_relocs *p;
3115
3116               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3117               eh = (struct ppc_link_hash_entry *) h;
3118
3119               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3120                 if (p->sec == sec)
3121                   {
3122                     p->count -= 1;
3123                     if (p->count == 0)
3124                       *pp = p->next;
3125                     break;
3126                   }
3127             }
3128           break;
3129
3130         default:
3131           break;
3132         }
3133     }
3134   return true;
3135 }
3136
3137 /* Called via elf_link_hash_traverse to transfer dynamic linking
3138    information on function code symbol entries to their corresponding
3139    function descriptor symbol entries.  */
3140 static boolean
3141 func_desc_adjust (h, inf)
3142      struct elf_link_hash_entry *h;
3143      PTR inf;
3144 {
3145   struct bfd_link_info *info;
3146   struct ppc_link_hash_table *htab;
3147
3148   if (h->root.type == bfd_link_hash_indirect)
3149     return true;
3150
3151   if (h->root.type == bfd_link_hash_warning)
3152     h = (struct elf_link_hash_entry *) h->root.u.i.link;
3153
3154   info = (struct bfd_link_info *) inf;
3155   htab = ppc_hash_table (info);
3156
3157   /* If this is a function code symbol, transfer dynamic linking
3158      information to the function descriptor symbol.  */
3159   if (!((struct ppc_link_hash_entry *) h)->is_func)
3160     return true;
3161
3162   if (h->root.type == bfd_link_hash_undefweak
3163       && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR))
3164     htab->have_undefweak = true;
3165
3166   if (h->plt.refcount > 0
3167       && h->root.root.string[0] == '.'
3168       && h->root.root.string[1] != '\0')
3169     {
3170       struct elf_link_hash_entry *fdh = ((struct ppc_link_hash_entry *) h)->oh;
3171       boolean force_local;
3172
3173       /* Find the corresponding function descriptor symbol.  Create it
3174          as undefined if necessary.  */
3175
3176       if (fdh == NULL)
3177         fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
3178                                     false, false, true);
3179
3180       if (fdh == NULL
3181           && info->shared
3182           && (h->root.type == bfd_link_hash_undefined
3183               || h->root.type == bfd_link_hash_undefweak))
3184         {
3185           bfd *abfd;
3186           asymbol *newsym;
3187
3188           abfd = h->root.u.undef.abfd;
3189           newsym = bfd_make_empty_symbol (abfd);
3190           newsym->name = h->root.root.string + 1;
3191           newsym->section = bfd_und_section_ptr;
3192           newsym->value = 0;
3193           newsym->flags = BSF_OBJECT;
3194           if (h->root.type == bfd_link_hash_undefweak)
3195             newsym->flags |= BSF_WEAK;
3196
3197           if ( !(_bfd_generic_link_add_one_symbol
3198                  (info, abfd, newsym->name, newsym->flags,
3199                   newsym->section, newsym->value, NULL, false, false,
3200                   (struct bfd_link_hash_entry **) &fdh)))
3201             {
3202               return false;
3203             }
3204           fdh->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
3205         }
3206
3207       if (fdh != NULL
3208           && (fdh->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
3209           && (info->shared
3210               || (fdh->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3211               || (fdh->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3212         {
3213           if (fdh->dynindx == -1)
3214             if (! bfd_elf64_link_record_dynamic_symbol (info, fdh))
3215               return false;
3216           fdh->elf_link_hash_flags |= (h->elf_link_hash_flags
3217                                        & (ELF_LINK_HASH_REF_REGULAR
3218                                           | ELF_LINK_HASH_REF_DYNAMIC
3219                                           | ELF_LINK_HASH_REF_REGULAR_NONWEAK
3220                                           | ELF_LINK_NON_GOT_REF));
3221           if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3222             {
3223               fdh->plt.refcount = h->plt.refcount;
3224               fdh->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3225             }
3226           ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
3227           ((struct ppc_link_hash_entry *) fdh)->oh = h;
3228           fdh->root.root.string = h->root.root.string + 1;
3229           ((struct ppc_link_hash_entry *) h)->oh = fdh;
3230         }
3231
3232       /* Now that the info is on the function descriptor, clear the
3233          function code sym info.  Any function code syms for which we
3234          don't have a definition in a regular file, we force local.
3235          This prevents a shared library from exporting syms that have
3236          been imported from another library.  Function code syms that
3237          are really in the library we must leave global to prevent the
3238          linker dragging in a definition from a static library.  */
3239       force_local = ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3240                      && info->shared);
3241       _bfd_elf_link_hash_hide_symbol (info, h, force_local);
3242     }
3243
3244   return true;
3245 }
3246
3247 #define MIN_SAVE_FPR 14
3248 #define MAX_SAVE_FPR 31
3249
3250 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
3251    this hook to a) provide some gcc support functions, and b) transfer
3252    dynamic linking information gathered so far on function code symbol
3253    entries, to their corresponding function descriptor symbol entries.  */
3254 static boolean
3255 ppc64_elf_func_desc_adjust (obfd, info)
3256      bfd *obfd ATTRIBUTE_UNUSED;
3257      struct bfd_link_info *info;
3258 {
3259   struct ppc_link_hash_table *htab;
3260   unsigned int lowest_savef = MAX_SAVE_FPR + 2;
3261   unsigned int lowest_restf = MAX_SAVE_FPR + 2;
3262   unsigned int i;
3263   struct elf_link_hash_entry *h;
3264   bfd_byte *p;
3265   char sym[10];
3266
3267   htab = ppc_hash_table (info);
3268
3269   if (htab->sfpr == NULL)
3270     /* We don't have any relocs.  */
3271     return true;
3272
3273   /* First provide any missing ._savef* and ._restf* functions.  */
3274   memcpy (sym, "._savef14", 10);
3275   for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
3276     {
3277       sym[7] = i / 10 + '0';
3278       sym[8] = i % 10 + '0';
3279       h = elf_link_hash_lookup (&htab->elf, sym, false, false, true);
3280       if (h != NULL
3281           && h->root.type == bfd_link_hash_undefined)
3282         {
3283           if (lowest_savef > i)
3284             lowest_savef = i;
3285           h->root.type = bfd_link_hash_defined;
3286           h->root.u.def.section = htab->sfpr;
3287           h->root.u.def.value = (i - lowest_savef) * 4;
3288           h->type = STT_FUNC;
3289           h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3290           _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
3291         }
3292     }
3293
3294   memcpy (sym, "._restf14", 10);
3295   for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
3296     {
3297       sym[7] = i / 10 + '0';
3298       sym[8] = i % 10 + '0';
3299       h = elf_link_hash_lookup (&htab->elf, sym, false, false, true);
3300       if (h != NULL
3301           && h->root.type == bfd_link_hash_undefined)
3302         {
3303           if (lowest_restf > i)
3304             lowest_restf = i;
3305           h->root.type = bfd_link_hash_defined;
3306           h->root.u.def.section = htab->sfpr;
3307           h->root.u.def.value = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
3308                                  + (i - lowest_restf) * 4);
3309           h->type = STT_FUNC;
3310           h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3311           _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
3312         }
3313     }
3314
3315   elf_link_hash_traverse (&htab->elf, func_desc_adjust, (PTR) info);
3316
3317   htab->sfpr->_raw_size = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
3318                            + (MAX_SAVE_FPR + 2 - lowest_restf) * 4);
3319
3320   if (htab->sfpr->_raw_size == 0)
3321     {
3322       if (!htab->have_undefweak)
3323         {
3324           _bfd_strip_section_from_output (info, htab->sfpr);
3325           return true;
3326         }
3327
3328       htab->sfpr->_raw_size = 4;
3329     }
3330
3331   p = (bfd_byte *) bfd_alloc (htab->elf.dynobj, htab->sfpr->_raw_size);
3332   if (p == NULL)
3333     return false;
3334   htab->sfpr->contents = p;
3335
3336   for (i = lowest_savef; i <= MAX_SAVE_FPR; i++)
3337     {
3338       unsigned int fpr = i << 21;
3339       unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
3340       bfd_put_32 (htab->elf.dynobj, STFD_FR0_0R1 + fpr + stackoff, p);
3341       p += 4;
3342     }
3343   if (lowest_savef <= MAX_SAVE_FPR)
3344     {
3345       bfd_put_32 (htab->elf.dynobj, BLR, p);
3346       p += 4;
3347     }
3348
3349   for (i = lowest_restf; i <= MAX_SAVE_FPR; i++)
3350     {
3351       unsigned int fpr = i << 21;
3352       unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
3353       bfd_put_32 (htab->elf.dynobj, LFD_FR0_0R1 + fpr + stackoff, p);
3354       p += 4;
3355     }
3356   if (lowest_restf <= MAX_SAVE_FPR
3357       || htab->sfpr->_raw_size == 4)
3358     {
3359       bfd_put_32 (htab->elf.dynobj, BLR, p);
3360     }
3361
3362   return true;
3363 }
3364
3365 /* Adjust a symbol defined by a dynamic object and referenced by a
3366    regular object.  The current definition is in some section of the
3367    dynamic object, but we're not including those sections.  We have to
3368    change the definition to something the rest of the link can
3369    understand.  */
3370
3371 static boolean
3372 ppc64_elf_adjust_dynamic_symbol (info, h)
3373      struct bfd_link_info *info;
3374      struct elf_link_hash_entry *h;
3375 {
3376   struct ppc_link_hash_table *htab;
3377   struct ppc_link_hash_entry * eh;
3378   struct ppc_dyn_relocs *p;
3379   asection *s;
3380   unsigned int power_of_two;
3381
3382   htab = ppc_hash_table (info);
3383
3384   /* Deal with function syms.  */
3385   if (h->type == STT_FUNC
3386       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3387     {
3388       /* Clear procedure linkage table information for any symbol that
3389          won't need a .plt entry.  */
3390       if (!((struct ppc_link_hash_entry *) h)->is_func_descriptor
3391           || h->plt.refcount <= 0
3392           || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
3393           || (! info->shared
3394               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3395               && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0))
3396         {
3397           h->plt.offset = (bfd_vma) -1;
3398           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3399         }
3400       return true;
3401     }
3402   else
3403     h->plt.offset = (bfd_vma) -1;
3404
3405   /* If this is a weak symbol, and there is a real definition, the
3406      processor independent code will have arranged for us to see the
3407      real definition first, and we can just use the same value.  */
3408   if (h->weakdef != NULL)
3409     {
3410       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
3411                   || h->weakdef->root.type == bfd_link_hash_defweak);
3412       h->root.u.def.section = h->weakdef->root.u.def.section;
3413       h->root.u.def.value = h->weakdef->root.u.def.value;
3414       return true;
3415     }
3416
3417   /* This is a reference to a symbol defined by a dynamic object which
3418      is not a function.  */
3419
3420   /* If we are creating a shared library, we must presume that the
3421      only references to the symbol are via the global offset table.
3422      For such cases we need not do anything here; the relocations will
3423      be handled correctly by relocate_section.  */
3424   if (info->shared)
3425     return true;
3426
3427   /* If there are no references to this symbol that do not use the
3428      GOT, we don't need to generate a copy reloc.  */
3429   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
3430     return true;
3431
3432   eh = (struct ppc_link_hash_entry *) h;
3433   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3434     {
3435       s = p->sec->output_section;
3436       if (s != NULL && (s->flags & SEC_READONLY) != 0)
3437         break;
3438     }
3439
3440   /* If we didn't find any dynamic relocs in read-only sections, then
3441      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
3442   if (p == NULL)
3443     {
3444       h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
3445       return true;
3446     }
3447
3448   /* We must allocate the symbol in our .dynbss section, which will
3449      become part of the .bss section of the executable.  There will be
3450      an entry for this symbol in the .dynsym section.  The dynamic
3451      object will contain position independent code, so all references
3452      from the dynamic object to this symbol will go through the global
3453      offset table.  The dynamic linker will use the .dynsym entry to
3454      determine the address it must put in the global offset table, so
3455      both the dynamic object and the regular object will refer to the
3456      same memory location for the variable.  */
3457
3458   /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
3459      copy the initial value out of the dynamic object and into the
3460      runtime process image.  We need to remember the offset into the
3461      .rela.bss section we are going to use.  */
3462   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3463     {
3464       htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
3465       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
3466     }
3467
3468   /* We need to figure out the alignment required for this symbol.  I
3469      have no idea how ELF linkers handle this.  */
3470   power_of_two = bfd_log2 (h->size);
3471   if (power_of_two > 4)
3472     power_of_two = 4;
3473
3474   /* Apply the required alignment.  */
3475   s = htab->sdynbss;
3476   s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
3477   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
3478     {
3479       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
3480         return false;
3481     }
3482
3483   /* Define the symbol as being at this point in the section.  */
3484   h->root.u.def.section = s;
3485   h->root.u.def.value = s->_raw_size;
3486
3487   /* Increment the section size to make room for the symbol.  */
3488   s->_raw_size += h->size;
3489
3490   return true;
3491 }
3492
3493 /* If given a function descriptor symbol, hide both the function code
3494    sym and the descriptor.  */
3495 static void
3496 ppc64_elf_hide_symbol (info, h, force_local)
3497      struct bfd_link_info *info;
3498      struct elf_link_hash_entry *h;
3499      boolean force_local;
3500 {
3501   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
3502
3503   if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
3504     {
3505       const char *name;
3506       struct elf_link_hash_entry *fh = ((struct ppc_link_hash_entry *) h)->oh;
3507       struct ppc_link_hash_table *htab;
3508
3509       name = h->root.root.string - 1;
3510       htab = ppc_hash_table (info);
3511       if (fh == NULL)
3512         fh = elf_link_hash_lookup (&htab->elf, name, false, false, false);
3513       if (fh != NULL)
3514         _bfd_elf_link_hash_hide_symbol (info, fh, force_local);
3515     }
3516 }
3517
3518 /* This is the condition under which ppc64_elf_finish_dynamic_symbol
3519    will be called from elflink.h.  If elflink.h doesn't call our
3520    finish_dynamic_symbol routine, we'll need to do something about
3521    initializing any .plt and .got entries in ppc64_elf_relocate_section.  */
3522 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
3523   ((DYN)                                                                \
3524    && ((INFO)->shared                                                   \
3525        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)      \
3526    && ((H)->dynindx != -1                                               \
3527        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
3528
3529 /* Allocate space in .plt, .got and associated reloc sections for
3530    dynamic relocs.  */
3531
3532 static boolean
3533 allocate_dynrelocs (h, inf)
3534      struct elf_link_hash_entry *h;
3535      PTR inf;
3536 {
3537   struct bfd_link_info *info;
3538   struct ppc_link_hash_table *htab;
3539   asection *s;
3540   struct ppc_link_hash_entry *eh;
3541   struct ppc_dyn_relocs *p;
3542
3543   if (h->root.type == bfd_link_hash_indirect)
3544     return true;
3545
3546   if (h->root.type == bfd_link_hash_warning)
3547     h = (struct elf_link_hash_entry *) h->root.u.i.link;
3548
3549   info = (struct bfd_link_info *) inf;
3550   htab = ppc_hash_table (info);
3551
3552   if (htab->elf.dynamic_sections_created
3553       && h->plt.refcount > 0
3554       && h->dynindx != -1)
3555     {
3556       BFD_ASSERT (((struct ppc_link_hash_entry *) h)->is_func_descriptor);
3557
3558       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
3559         {
3560           /* If this is the first .plt entry, make room for the special
3561              first entry.  */
3562           s = htab->splt;
3563           if (s->_raw_size == 0)
3564             s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
3565
3566           h->plt.offset = s->_raw_size;
3567
3568           /* Make room for this entry.  */
3569           s->_raw_size += PLT_ENTRY_SIZE;
3570
3571           /* Make room for the .glink code.  */
3572           s = htab->sglink;
3573           if (s->_raw_size == 0)
3574             s->_raw_size += GLINK_CALL_STUB_SIZE;
3575           /* We need bigger stubs past index 32767.  */
3576           if (s->_raw_size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
3577             s->_raw_size += 4;
3578           s->_raw_size += 2*4;
3579
3580           /* We also need to make an entry in the .rela.plt section.  */
3581           s = htab->srelplt;
3582           s->_raw_size += sizeof (Elf64_External_Rela);
3583         }
3584       else
3585         {
3586           h->plt.offset = (bfd_vma) -1;
3587           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3588         }
3589     }
3590   else
3591     {
3592       h->plt.offset = (bfd_vma) -1;
3593       h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3594     }
3595
3596   if (h->got.refcount > 0)
3597     {
3598       boolean dyn;
3599
3600       /* Make sure this symbol is output as a dynamic symbol.
3601          Undefined weak syms won't yet be marked as dynamic.  */
3602       if (h->dynindx == -1
3603           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3604         {
3605           if (! bfd_elf64_link_record_dynamic_symbol (info, h))
3606             return false;
3607         }
3608
3609       s = htab->sgot;
3610       h->got.offset = s->_raw_size;
3611       s->_raw_size += 8;
3612       dyn = htab->elf.dynamic_sections_created;
3613       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
3614         htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
3615     }
3616   else
3617     h->got.offset = (bfd_vma) -1;
3618
3619   eh = (struct ppc_link_hash_entry *) h;
3620   if (eh->dyn_relocs == NULL)
3621     return true;
3622
3623   /* In the shared -Bsymbolic case, discard space allocated for
3624      dynamic pc-relative relocs against symbols which turn out to be
3625      defined in regular objects.  For the normal shared case, discard
3626      space for relocs that have become local due to symbol visibility
3627      changes.  */
3628
3629   if (info->shared)
3630     {
3631       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3632           && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
3633               || info->symbolic))
3634         {
3635           struct ppc_dyn_relocs **pp;
3636
3637           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3638             {
3639               p->count -= p->pc_count;
3640               p->pc_count = 0;
3641               if (p->count == 0)
3642                 *pp = p->next;
3643               else
3644                 pp = &p->next;
3645             }
3646         }
3647     }
3648   else
3649     {
3650       /* For the non-shared case, discard space for relocs against
3651          symbols which turn out to need copy relocs or are not
3652          dynamic.  */
3653
3654       if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
3655           && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3656                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3657               || (htab->elf.dynamic_sections_created
3658                   && (h->root.type == bfd_link_hash_undefweak
3659                       || h->root.type == bfd_link_hash_undefined))))
3660         {
3661           /* Make sure this symbol is output as a dynamic symbol.
3662              Undefined weak syms won't yet be marked as dynamic.  */
3663           if (h->dynindx == -1
3664               && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3665             {
3666               if (! bfd_elf64_link_record_dynamic_symbol (info, h))
3667                 return false;
3668             }
3669
3670           /* If that succeeded, we know we'll be keeping all the
3671              relocs.  */
3672           if (h->dynindx != -1)
3673             goto keep;
3674         }
3675
3676       eh->dyn_relocs = NULL;
3677
3678     keep: ;
3679     }
3680
3681   /* Finally, allocate space.  */
3682   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3683     {
3684       asection *sreloc = elf_section_data (p->sec)->sreloc;
3685       sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
3686     }
3687
3688   return true;
3689 }
3690
3691 /* Find any dynamic relocs that apply to read-only sections.  */
3692
3693 static boolean
3694 readonly_dynrelocs (h, inf)
3695      struct elf_link_hash_entry *h;
3696      PTR inf;
3697 {
3698   struct ppc_link_hash_entry *eh;
3699   struct ppc_dyn_relocs *p;
3700
3701   if (h->root.type == bfd_link_hash_warning)
3702     h = (struct elf_link_hash_entry *) h->root.u.i.link;
3703
3704   eh = (struct ppc_link_hash_entry *) h;
3705   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3706     {
3707       asection *s = p->sec->output_section;
3708
3709       if (s != NULL && (s->flags & SEC_READONLY) != 0)
3710         {
3711           struct bfd_link_info *info = (struct bfd_link_info *) inf;
3712
3713           info->flags |= DF_TEXTREL;
3714
3715           /* Not an error, just cut short the traversal.  */
3716           return false;
3717         }
3718     }
3719   return true;
3720 }
3721
3722 /* Set the sizes of the dynamic sections.  */
3723
3724 static boolean
3725 ppc64_elf_size_dynamic_sections (output_bfd, info)
3726      bfd *output_bfd ATTRIBUTE_UNUSED;
3727      struct bfd_link_info *info;
3728 {
3729   struct ppc_link_hash_table *htab;
3730   bfd *dynobj;
3731   asection *s;
3732   boolean relocs;
3733   bfd *ibfd;
3734
3735   htab = ppc_hash_table (info);
3736   dynobj = htab->elf.dynobj;
3737   if (dynobj == NULL)
3738     abort ();
3739
3740   if (htab->elf.dynamic_sections_created)
3741     {
3742       /* Set the contents of the .interp section to the interpreter.  */
3743       if (! info->shared)
3744         {
3745           s = bfd_get_section_by_name (dynobj, ".interp");
3746           if (s == NULL)
3747             abort ();
3748           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
3749           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3750         }
3751     }
3752
3753   /* Set up .got offsets for local syms, and space for local dynamic
3754      relocs.  */
3755   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3756     {
3757       bfd_signed_vma *local_got;
3758       bfd_signed_vma *end_local_got;
3759       bfd_size_type locsymcount;
3760       Elf_Internal_Shdr *symtab_hdr;
3761       asection *srel;
3762
3763       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3764         continue;
3765
3766       for (s = ibfd->sections; s != NULL; s = s->next)
3767         {
3768           struct ppc_dyn_relocs *p;
3769
3770           for (p = *((struct ppc_dyn_relocs **)
3771                      &elf_section_data (s)->local_dynrel);
3772                p != NULL;
3773                p = p->next)
3774             {
3775               if (!bfd_is_abs_section (p->sec)
3776                   && bfd_is_abs_section (p->sec->output_section))
3777                 {
3778                   /* Input section has been discarded, either because
3779                      it is a copy of a linkonce section or due to
3780                      linker script /DISCARD/, so we'll be discarding
3781                      the relocs too.  */
3782                 }
3783               else if (p->count != 0)
3784                 {
3785                   srel = elf_section_data (p->sec)->sreloc;
3786                   srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
3787                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3788                     info->flags |= DF_TEXTREL;
3789                 }
3790             }
3791         }
3792
3793       local_got = elf_local_got_refcounts (ibfd);
3794       if (!local_got)
3795         continue;
3796
3797       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3798       locsymcount = symtab_hdr->sh_info;
3799       end_local_got = local_got + locsymcount;
3800       s = htab->sgot;
3801       srel = htab->srelgot;
3802       for (; local_got < end_local_got; ++local_got)
3803         {
3804           if (*local_got > 0)
3805             {
3806               *local_got = s->_raw_size;
3807               s->_raw_size += 8;
3808               if (info->shared)
3809                 srel->_raw_size += sizeof (Elf64_External_Rela);
3810             }
3811           else
3812             *local_got = (bfd_vma) -1;
3813         }
3814     }
3815
3816   /* Allocate global sym .plt and .got entries, and space for global
3817      sym dynamic relocs.  */
3818   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
3819
3820   /* We now have determined the sizes of the various dynamic sections.
3821      Allocate memory for them.  */
3822   relocs = false;
3823   for (s = dynobj->sections; s != NULL; s = s->next)
3824     {
3825       if ((s->flags & SEC_LINKER_CREATED) == 0)
3826         continue;
3827
3828       if (s == htab->sbrlt || s == htab->srelbrlt)
3829         /* These haven't been allocated yet;  don't strip.  */
3830         continue;
3831       else if (s == htab->splt
3832                || s == htab->sgot
3833                || s == htab->sglink)
3834         {
3835           /* Strip this section if we don't need it; see the
3836              comment below.  */
3837         }
3838       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
3839         {
3840           if (s->_raw_size == 0)
3841             {
3842               /* If we don't need this section, strip it from the
3843                  output file.  This is mostly to handle .rela.bss and
3844                  .rela.plt.  We must create both sections in
3845                  create_dynamic_sections, because they must be created
3846                  before the linker maps input sections to output
3847                  sections.  The linker does that before
3848                  adjust_dynamic_symbol is called, and it is that
3849                  function which decides whether anything needs to go
3850                  into these sections.  */
3851             }
3852           else
3853             {
3854               if (s != htab->srelplt)
3855                 relocs = true;
3856
3857               /* We use the reloc_count field as a counter if we need
3858                  to copy relocs into the output file.  */
3859               s->reloc_count = 0;
3860             }
3861         }
3862       else
3863         {
3864           /* It's not one of our sections, so don't allocate space.  */
3865           continue;
3866         }
3867
3868       if (s->_raw_size == 0)
3869         {
3870           _bfd_strip_section_from_output (info, s);
3871           continue;
3872         }
3873
3874       /* Allocate memory for the section contents.  We use bfd_zalloc
3875          here in case unused entries are not reclaimed before the
3876          section's contents are written out.  This should not happen,
3877          but this way if it does, we get a R_PPC64_NONE reloc instead
3878          of garbage.  */
3879       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
3880       if (s->contents == NULL)
3881         return false;
3882     }
3883
3884   if (htab->elf.dynamic_sections_created)
3885     {
3886       /* Add some entries to the .dynamic section.  We fill in the
3887          values later, in ppc64_elf_finish_dynamic_sections, but we
3888          must add the entries now so that we get the correct size for
3889          the .dynamic section.  The DT_DEBUG entry is filled in by the
3890          dynamic linker and used by the debugger.  */
3891 #define add_dynamic_entry(TAG, VAL) \
3892   bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
3893
3894       if (!info->shared)
3895         {
3896           if (!add_dynamic_entry (DT_DEBUG, 0))
3897             return false;
3898         }
3899
3900       if (htab->splt->_raw_size != 0)
3901         {
3902           if (!add_dynamic_entry (DT_PLTGOT, 0)
3903               || !add_dynamic_entry (DT_PLTRELSZ, 0)
3904               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3905               || !add_dynamic_entry (DT_JMPREL, 0)
3906               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
3907             return false;
3908         }
3909
3910       if (NO_OPD_RELOCS)
3911         {
3912           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
3913               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
3914             return false;
3915         }
3916
3917       if (relocs)
3918         {
3919           if (!add_dynamic_entry (DT_RELA, 0)
3920               || !add_dynamic_entry (DT_RELASZ, 0)
3921               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
3922             return false;
3923
3924           /* If any dynamic relocs apply to a read-only section,
3925              then we need a DT_TEXTREL entry.  */
3926           if ((info->flags & DF_TEXTREL) == 0)
3927             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
3928                                     (PTR) info);
3929
3930           if ((info->flags & DF_TEXTREL) != 0)
3931             {
3932               if (!add_dynamic_entry (DT_TEXTREL, 0))
3933                 return false;
3934             }
3935         }
3936     }
3937 #undef add_dynamic_entry
3938
3939   return true;
3940 }
3941
3942 /* Determine the type of stub needed, if any, for a call.  */
3943
3944 static INLINE enum ppc_stub_type
3945 ppc_type_of_stub (input_sec, rel, hash, destination)
3946      asection *input_sec;
3947      const Elf_Internal_Rela *rel;
3948      struct ppc_link_hash_entry **hash;
3949      bfd_vma destination;
3950 {
3951   struct ppc_link_hash_entry *h = *hash;
3952   bfd_vma location;
3953   bfd_vma branch_offset;
3954   bfd_vma max_branch_offset;
3955   unsigned int r_type;
3956
3957   if (h != NULL)
3958     {
3959       if (h->oh != NULL
3960           && h->oh->plt.offset != (bfd_vma) -1
3961           && h->oh->dynindx != -1)
3962         {
3963           *hash = (struct ppc_link_hash_entry *) h->oh;
3964           return ppc_stub_plt_call;
3965         }
3966
3967       if (h->elf.root.type == bfd_link_hash_undefweak
3968           || h->elf.root.type == bfd_link_hash_undefined)
3969         return ppc_stub_none;
3970     }
3971
3972   /* Determine where the call point is.  */
3973   location = (input_sec->output_offset
3974               + input_sec->output_section->vma
3975               + rel->r_offset);
3976
3977   branch_offset = destination - location;
3978   r_type = ELF64_R_TYPE (rel->r_info);
3979
3980   /* Determine if a long branch stub is needed.  */
3981   max_branch_offset = 1 << 25;
3982   if (r_type != (unsigned int) R_PPC64_REL24)
3983     max_branch_offset = 1 << 15;
3984
3985   if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
3986     /* We need a stub.  Figure out whether a long_branch or plt_branch
3987        is needed later.  */
3988     return ppc_stub_long_branch;
3989
3990   return ppc_stub_none;
3991 }
3992
3993 /* Build a .plt call stub.  */
3994
3995 static bfd_byte *
3996 build_plt_stub (obfd, p, offset, glink)
3997      bfd *obfd;
3998      bfd_byte *p;
3999      int offset;
4000      int glink;
4001 {
4002 #define PPC_LO(v) ((v) & 0xffff)
4003 #define PPC_HI(v) (((v) >> 16) & 0xffff)
4004 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
4005
4006   if (glink)
4007     bfd_put_32 (obfd, LD_R2_40R1, p),                   p += 4;
4008   bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
4009   if (!glink)
4010     bfd_put_32 (obfd, STD_R2_40R1, p),                  p += 4;
4011   bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),  p += 4;
4012   if (PPC_HA (offset + 8) != PPC_HA (offset))
4013     bfd_put_32 (obfd, ADDIS_R12_R12_1, p),              p += 4;
4014   offset += 8;
4015   bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p),   p += 4;
4016   if (PPC_HA (offset + 8) != PPC_HA (offset))
4017     bfd_put_32 (obfd, ADDIS_R12_R12_1, p),              p += 4;
4018   offset += 8;
4019   bfd_put_32 (obfd, MTCTR_R11, p),                      p += 4;
4020   bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),  p += 4;
4021   bfd_put_32 (obfd, BCTR, p),                           p += 4;
4022   return p;
4023 }
4024
4025 static boolean
4026 ppc_build_one_stub (gen_entry, in_arg)
4027      struct bfd_hash_entry *gen_entry;
4028      PTR in_arg;
4029 {
4030   struct ppc_stub_hash_entry *stub_entry;
4031   struct ppc_branch_hash_entry *br_entry;
4032   struct bfd_link_info *info;
4033   struct ppc_link_hash_table *htab;
4034   asection *stub_sec;
4035   bfd *stub_bfd;
4036   bfd_byte *loc;
4037   bfd_byte *p;
4038   unsigned int indx;
4039   bfd_vma off;
4040   int size;
4041
4042   /* Massage our args to the form they really have.  */
4043   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4044   info = (struct bfd_link_info *) in_arg;
4045
4046   htab = ppc_hash_table (info);
4047   stub_sec = stub_entry->stub_sec;
4048
4049   /* Make a note of the offset within the stubs for this entry.  */
4050   stub_entry->stub_offset = stub_sec->_cooked_size;
4051   loc = stub_sec->contents + stub_entry->stub_offset;
4052
4053   stub_bfd = stub_sec->owner;
4054
4055   switch (stub_entry->stub_type)
4056     {
4057     case ppc_stub_long_branch:
4058       /* Branches are relative.  This is where we are going to.  */
4059       off = (stub_entry->target_value
4060              + stub_entry->target_section->output_offset
4061              + stub_entry->target_section->output_section->vma);
4062
4063       /* And this is where we are coming from.  */
4064       off -= (stub_entry->stub_offset
4065               + stub_sec->output_offset
4066               + stub_sec->output_section->vma);
4067
4068       BFD_ASSERT (off + (1 << 25) < (bfd_vma) (1 << 26));
4069
4070       bfd_put_32 (stub_bfd, (bfd_vma) B_DOT | (off & 0x3fffffc), loc);
4071       size = 4;
4072       break;
4073
4074     case ppc_stub_plt_branch:
4075       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
4076                                          stub_entry->root.string + 9,
4077                                          false, false);
4078       if (br_entry == NULL)
4079         {
4080           (*_bfd_error_handler) (_("can't find branch stub `%s'"),
4081                                  stub_entry->root.string + 9);
4082           htab->stub_error = true;
4083           return false;
4084         }
4085
4086       off = (stub_entry->target_value
4087              + stub_entry->target_section->output_offset
4088              + stub_entry->target_section->output_section->vma);
4089
4090       bfd_put_64 (htab->sbrlt->owner, off,
4091                   htab->sbrlt->contents + br_entry->offset);
4092
4093       if (info->shared)
4094         {
4095           /* Create a reloc for the branch lookup table entry.  */
4096           Elf_Internal_Rela rela;
4097           Elf64_External_Rela *r;
4098
4099           rela.r_offset = (br_entry->offset
4100                            + htab->sbrlt->output_offset
4101                            + htab->sbrlt->output_section->vma);
4102           rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
4103           rela.r_addend = off;
4104
4105           r = (Elf64_External_Rela *) htab->srelbrlt->contents;
4106           r += htab->srelbrlt->reloc_count++;
4107           bfd_elf64_swap_reloca_out (htab->srelbrlt->owner, &rela, r);
4108         }
4109
4110       off = (br_entry->offset
4111              + htab->sbrlt->output_offset
4112              + htab->sbrlt->output_section->vma
4113              - elf_gp (htab->sbrlt->output_section->owner)
4114              - TOC_BASE_OFF);
4115
4116       if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
4117         {
4118           (*_bfd_error_handler)
4119             (_("linkage table error against `%s'"),
4120              stub_entry->root.string);
4121           bfd_set_error (bfd_error_bad_value);
4122           htab->stub_error = true;
4123           return false;
4124         }
4125
4126       indx = off;
4127       bfd_put_32 (stub_bfd, (bfd_vma) ADDIS_R12_R2 | PPC_HA (indx), loc);
4128       bfd_put_32 (stub_bfd, (bfd_vma) LD_R11_0R12 | PPC_LO (indx), loc + 4);
4129       bfd_put_32 (stub_bfd, (bfd_vma) MTCTR_R11, loc + 8);
4130       bfd_put_32 (stub_bfd, (bfd_vma) BCTR, loc + 12);
4131       size = 16;
4132       break;
4133
4134     case ppc_stub_plt_call:
4135       /* Build the .glink lazy link call stub.  */
4136       p = htab->sglink->contents + htab->sglink->_cooked_size;
4137       indx = htab->sglink->reloc_count;
4138       if (indx < 0x8000)
4139         {
4140           bfd_put_32 (htab->sglink->owner, LI_R0_0 | indx, p);
4141           p += 4;
4142         }
4143       else
4144         {
4145           bfd_put_32 (htab->sglink->owner, LIS_R0_0 | PPC_HI (indx), p);
4146           p += 4;
4147           bfd_put_32 (htab->sglink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
4148           p += 4;
4149         }
4150       bfd_put_32 (htab->sglink->owner,
4151                   B_DOT | ((htab->sglink->contents - p) & 0x3fffffc), p);
4152       p += 4;
4153       htab->sglink->_cooked_size = p - htab->sglink->contents;
4154       htab->sglink->reloc_count += 1;
4155
4156       /* Now build the stub.  */
4157       off = stub_entry->h->elf.plt.offset;
4158       if (off >= (bfd_vma) -2)
4159         abort ();
4160
4161       off &= ~ (bfd_vma) 1;
4162       off += (htab->splt->output_offset
4163               + htab->splt->output_section->vma
4164               - elf_gp (htab->splt->output_section->owner)
4165               - TOC_BASE_OFF);
4166
4167       if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
4168         {
4169           (*_bfd_error_handler)
4170             (_("linkage table error against `%s'"),
4171              stub_entry->h->elf.root.root.string);
4172           bfd_set_error (bfd_error_bad_value);
4173           htab->stub_error = true;
4174           return false;
4175         }
4176
4177       p = build_plt_stub (stub_bfd, loc, (int) off, 0);
4178       size = p - loc;
4179       break;
4180
4181     default:
4182       BFD_FAIL ();
4183       return false;
4184     }
4185
4186   stub_sec->_cooked_size += size;
4187   return true;
4188 }
4189
4190 /* As above, but don't actually build the stub.  Just bump offset so
4191    we know stub section sizes, and select plt_branch stubs where
4192    long_branch stubs won't do.  */
4193
4194 static boolean
4195 ppc_size_one_stub (gen_entry, in_arg)
4196      struct bfd_hash_entry *gen_entry;
4197      PTR in_arg;
4198 {
4199   struct ppc_stub_hash_entry *stub_entry;
4200   struct ppc_link_hash_table *htab;
4201   bfd_vma off;
4202   int size;
4203
4204   /* Massage our args to the form they really have.  */
4205   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4206   htab = (struct ppc_link_hash_table *) in_arg;
4207
4208   if (stub_entry->stub_type == ppc_stub_plt_call)
4209     {
4210       off = stub_entry->h->elf.plt.offset & ~(bfd_vma) 1;
4211       off += (htab->splt->output_offset
4212               + htab->splt->output_section->vma
4213               - elf_gp (htab->splt->output_section->owner)
4214               - TOC_BASE_OFF);
4215
4216       size = 28;
4217       if (PPC_HA ((int) off + 16) != PPC_HA ((int) off))
4218         size += 4;
4219     }
4220   else
4221     {
4222       /* ppc_stub_long_branch or ppc_stub_plt_branch.  */
4223       stub_entry->stub_type = ppc_stub_long_branch;
4224       size = 4;
4225
4226       off = (stub_entry->target_value
4227              + stub_entry->target_section->output_offset
4228              + stub_entry->target_section->output_section->vma);
4229       off -= (stub_entry->stub_sec->_raw_size
4230               + stub_entry->stub_sec->output_offset
4231               + stub_entry->stub_sec->output_section->vma);
4232
4233       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
4234         {
4235           struct ppc_branch_hash_entry *br_entry;
4236
4237           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
4238                                              stub_entry->root.string + 9,
4239                                              true, false);
4240           if (br_entry == NULL)
4241             {
4242               (*_bfd_error_handler) (_("can't build branch stub `%s'"),
4243                                      stub_entry->root.string + 9);
4244               htab->stub_error = true;
4245               return false;
4246             }
4247
4248           if (br_entry->iter != htab->stub_iteration)
4249             {
4250               br_entry->iter = htab->stub_iteration;
4251               br_entry->offset = htab->sbrlt->_raw_size;
4252               htab->sbrlt->_raw_size += 8;
4253             }
4254           stub_entry->stub_type = ppc_stub_plt_branch;
4255           size = 16;
4256         }
4257     }
4258
4259   stub_entry->stub_sec->_raw_size += size;
4260   return true;
4261 }
4262
4263 /* Set up various things so that we can make a list of input sections
4264    for each output section included in the link.  Returns -1 on error,
4265    0 when no stubs will be needed, and 1 on success. */
4266
4267 int
4268 ppc64_elf_setup_section_lists (output_bfd, info)
4269      bfd *output_bfd;
4270      struct bfd_link_info *info;
4271 {
4272   bfd *input_bfd;
4273   unsigned int bfd_count;
4274   int top_id, top_index;
4275   asection *section;
4276   asection **input_list, **list;
4277   bfd_size_type amt;
4278   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4279
4280   if (htab->elf.root.creator->flavour != bfd_target_elf_flavour
4281       || htab->sbrlt == NULL)
4282     return 0;
4283
4284   /* Count the number of input BFDs and find the top input section id.  */
4285   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
4286        input_bfd != NULL;
4287        input_bfd = input_bfd->link_next)
4288     {
4289       bfd_count += 1;
4290       for (section = input_bfd->sections;
4291            section != NULL;
4292            section = section->next)
4293         {
4294           if (top_id < section->id)
4295             top_id = section->id;
4296         }
4297     }
4298   htab->bfd_count = bfd_count;
4299
4300   amt = sizeof (struct map_stub) * (top_id + 1);
4301   htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
4302   if (htab->stub_group == NULL)
4303     return -1;
4304
4305   /* We can't use output_bfd->section_count here to find the top output
4306      section index as some sections may have been removed, and
4307      _bfd_strip_section_from_output doesn't renumber the indices.  */
4308   for (section = output_bfd->sections, top_index = 0;
4309        section != NULL;
4310        section = section->next)
4311     {
4312       if (top_index < section->index)
4313         top_index = section->index;
4314     }
4315
4316   htab->top_index = top_index;
4317   amt = sizeof (asection *) * (top_index + 1);
4318   input_list = (asection **) bfd_malloc (amt);
4319   htab->input_list = input_list;
4320   if (input_list == NULL)
4321     return -1;
4322
4323   /* For sections we aren't interested in, mark their entries with a
4324      value we can check later.  */
4325   list = input_list + top_index;
4326   do
4327     *list = bfd_abs_section_ptr;
4328   while (list-- != input_list);
4329
4330   for (section = output_bfd->sections;
4331        section != NULL;
4332        section = section->next)
4333     {
4334       if ((section->flags & SEC_CODE) != 0)
4335         input_list[section->index] = NULL;
4336     }
4337
4338   return 1;
4339 }
4340
4341 /* The linker repeatedly calls this function for each input section,
4342    in the order that input sections are linked into output sections.
4343    Build lists of input sections to determine groupings between which
4344    we may insert linker stubs.  */
4345
4346 void
4347 ppc64_elf_next_input_section (info, isec)
4348      struct bfd_link_info *info;
4349      asection *isec;
4350 {
4351   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4352
4353   if (isec->output_section->index <= htab->top_index)
4354     {
4355       asection **list = htab->input_list + isec->output_section->index;
4356       if (*list != bfd_abs_section_ptr)
4357         {
4358           /* Steal the link_sec pointer for our list.  */
4359 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
4360           /* This happens to make the list in reverse order,
4361              which is what we want.  */
4362           PREV_SEC (isec) = *list;
4363           *list = isec;
4364         }
4365     }
4366 }
4367
4368 /* See whether we can group stub sections together.  Grouping stub
4369    sections may result in fewer stubs.  More importantly, we need to
4370    put all .init* and .fini* stubs at the beginning of the .init or
4371    .fini output sections respectively, because glibc splits the
4372    _init and _fini functions into multiple parts.  Putting a stub in
4373    the middle of a function is not a good idea.  */
4374
4375 static void
4376 group_sections (htab, stub_group_size, stubs_always_before_branch)
4377      struct ppc_link_hash_table *htab;
4378      bfd_size_type stub_group_size;
4379      boolean stubs_always_before_branch;
4380 {
4381   asection **list = htab->input_list + htab->top_index;
4382   do
4383     {
4384       asection *tail = *list;
4385       if (tail == bfd_abs_section_ptr)
4386         continue;
4387       while (tail != NULL)
4388         {
4389           asection *curr;
4390           asection *prev;
4391           bfd_size_type total;
4392
4393           curr = tail;
4394           if (tail->_cooked_size)
4395             total = tail->_cooked_size;
4396           else
4397             total = tail->_raw_size;
4398           while ((prev = PREV_SEC (curr)) != NULL
4399                  && ((total += curr->output_offset - prev->output_offset)
4400                      < stub_group_size))
4401             curr = prev;
4402
4403           /* OK, the size from the start of CURR to the end is less
4404              than stub_group_size and thus can be handled by one stub
4405              section.  (or the tail section is itself larger than
4406              stub_group_size, in which case we may be toast.)  We
4407              should really be keeping track of the total size of stubs
4408              added here, as stubs contribute to the final output
4409              section size.  That's a little tricky, and this way will
4410              only break if stubs added make the total size more than
4411              2^25, ie. for the default stub_group_size, if stubs total
4412              more than 2834432 bytes, or over 100000 plt call stubs.  */
4413           do
4414             {
4415               prev = PREV_SEC (tail);
4416               /* Set up this stub group.  */
4417               htab->stub_group[tail->id].link_sec = curr;
4418             }
4419           while (tail != curr && (tail = prev) != NULL);
4420
4421           /* But wait, there's more!  Input sections up to stub_group_size
4422              bytes before the stub section can be handled by it too.  */
4423           if (!stubs_always_before_branch)
4424             {
4425               total = 0;
4426               while (prev != NULL
4427                      && ((total += tail->output_offset - prev->output_offset)
4428                          < stub_group_size))
4429                 {
4430                   tail = prev;
4431                   prev = PREV_SEC (tail);
4432                   htab->stub_group[tail->id].link_sec = curr;
4433                 }
4434             }
4435           tail = prev;
4436         }
4437     }
4438   while (list-- != htab->input_list);
4439   free (htab->input_list);
4440 #undef PREV_SEC
4441 }
4442
4443 /* Read in all local syms for all input bfds.  */
4444
4445 static boolean
4446 get_local_syms (input_bfd, htab)
4447      bfd *input_bfd;
4448      struct ppc_link_hash_table *htab;
4449 {
4450   unsigned int bfd_indx;
4451   Elf_Internal_Sym *local_syms, **all_local_syms;
4452
4453   /* We want to read in symbol extension records only once.  To do this
4454      we need to read in the local symbols in parallel and save them for
4455      later use; so hold pointers to the local symbols in an array.  */
4456   bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
4457   all_local_syms = (Elf_Internal_Sym **) bfd_zmalloc (amt);
4458   htab->all_local_syms = all_local_syms;
4459   if (all_local_syms == NULL)
4460     return false;
4461
4462   /* Walk over all the input BFDs, swapping in local symbols.
4463      If we are creating a shared library, create hash entries for the
4464      export stubs.  */
4465   for (bfd_indx = 0;
4466        input_bfd != NULL;
4467        input_bfd = input_bfd->link_next, bfd_indx++)
4468     {
4469       Elf_Internal_Shdr *symtab_hdr;
4470       Elf_Internal_Shdr *shndx_hdr;
4471       Elf_Internal_Sym *isym;
4472       Elf64_External_Sym *ext_syms, *esym, *end_sy;
4473       Elf_External_Sym_Shndx *shndx_buf, *shndx;
4474       bfd_size_type sec_size;
4475
4476       /* We'll need the symbol table in a second.  */
4477       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4478       if (symtab_hdr->sh_info == 0)
4479         continue;
4480
4481       /* We need an array of the local symbols attached to the input bfd.
4482          Unfortunately, we're going to have to read & swap them in.  */
4483       sec_size = symtab_hdr->sh_info;
4484       sec_size *= sizeof (Elf_Internal_Sym);
4485       local_syms = (Elf_Internal_Sym *) bfd_malloc (sec_size);
4486       if (local_syms == NULL)
4487         return false;
4488
4489       all_local_syms[bfd_indx] = local_syms;
4490       sec_size = symtab_hdr->sh_info;
4491       sec_size *= sizeof (Elf64_External_Sym);
4492       ext_syms = (Elf64_External_Sym *) bfd_malloc (sec_size);
4493       if (ext_syms == NULL)
4494         return false;
4495
4496       if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
4497           || bfd_bread ((PTR) ext_syms, sec_size, input_bfd) != sec_size)
4498         {
4499         error_ret_free_ext_syms:
4500           free (ext_syms);
4501           return false;
4502         }
4503
4504       shndx_buf = NULL;
4505       shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
4506       if (shndx_hdr->sh_size != 0)
4507         {
4508           sec_size = symtab_hdr->sh_info;
4509           sec_size *= sizeof (Elf_External_Sym_Shndx);
4510           shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (sec_size);
4511           if (shndx_buf == NULL)
4512             goto error_ret_free_ext_syms;
4513
4514           if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
4515               || bfd_bread ((PTR) shndx_buf, sec_size, input_bfd) != sec_size)
4516             {
4517               free (shndx_buf);
4518               goto error_ret_free_ext_syms;
4519             }
4520         }
4521
4522       /* Swap the local symbols in.  */
4523       for (esym = ext_syms, end_sy = esym + symtab_hdr->sh_info,
4524              isym = local_syms, shndx = shndx_buf;
4525            esym < end_sy;
4526            esym++, isym++, shndx = (shndx ? shndx + 1 : NULL))
4527         bfd_elf64_swap_symbol_in (input_bfd, esym, shndx, isym);
4528
4529       /* Now we can free the external symbols.  */
4530       free (shndx_buf);
4531       free (ext_syms);
4532     }
4533
4534   return true;
4535 }
4536
4537 /* Determine and set the size of the stub section for a final link.
4538
4539    The basic idea here is to examine all the relocations looking for
4540    PC-relative calls to a target that is unreachable with a "bl"
4541    instruction.  */
4542
4543 boolean
4544 ppc64_elf_size_stubs (output_bfd, stub_bfd, info, group_size,
4545                       add_stub_section, layout_sections_again)
4546      bfd *output_bfd;
4547      bfd *stub_bfd;
4548      struct bfd_link_info *info;
4549      bfd_signed_vma group_size;
4550      asection * (*add_stub_section) PARAMS ((const char *, asection *));
4551      void (*layout_sections_again) PARAMS ((void));
4552 {
4553   bfd_size_type stub_group_size;
4554   boolean stubs_always_before_branch;
4555   boolean ret = false;
4556   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4557
4558   /* Stash our params away.  */
4559   htab->stub_bfd = stub_bfd;
4560   htab->add_stub_section = add_stub_section;
4561   htab->layout_sections_again = layout_sections_again;
4562   stubs_always_before_branch = group_size < 0;
4563   if (group_size < 0)
4564     stub_group_size = -group_size;
4565   else
4566     stub_group_size = group_size;
4567   if (stub_group_size == 1)
4568     {
4569       /* Default values.  */
4570       stub_group_size = 30720000;
4571       if (htab->has_14bit_branch)
4572         stub_group_size = 30000;
4573     }
4574
4575   group_sections (htab, stub_group_size, stubs_always_before_branch);
4576
4577   if (! get_local_syms (info->input_bfds, htab))
4578     {
4579       if (htab->all_local_syms)
4580         goto error_ret_free_local;
4581       return false;
4582     }
4583
4584   while (1)
4585     {
4586       bfd *input_bfd;
4587       unsigned int bfd_indx;
4588       asection *stub_sec;
4589       boolean stub_changed;
4590
4591       htab->stub_iteration += 1;
4592       stub_changed = false;
4593
4594       for (input_bfd = info->input_bfds, bfd_indx = 0;
4595            input_bfd != NULL;
4596            input_bfd = input_bfd->link_next, bfd_indx++)
4597         {
4598           Elf_Internal_Shdr *symtab_hdr;
4599           asection *section;
4600           Elf_Internal_Sym *local_syms;
4601
4602           /* We'll need the symbol table in a second.  */
4603           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4604           if (symtab_hdr->sh_info == 0)
4605             continue;
4606
4607           local_syms = htab->all_local_syms[bfd_indx];
4608
4609           /* Walk over each section attached to the input bfd.  */
4610           for (section = input_bfd->sections;
4611                section != NULL;
4612                section = section->next)
4613             {
4614               Elf_Internal_Shdr *input_rel_hdr;
4615               Elf64_External_Rela *external_relocs, *erelaend, *erela;
4616               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
4617               bfd_size_type amt;
4618
4619               /* If there aren't any relocs, then there's nothing more
4620                  to do.  */
4621               if ((section->flags & SEC_RELOC) == 0
4622                   || section->reloc_count == 0)
4623                 continue;
4624
4625               /* If this section is a link-once section that will be
4626                  discarded, then don't create any stubs.  */
4627               if (section->output_section == NULL
4628                   || section->output_section->owner != output_bfd)
4629                 continue;
4630
4631               /* Allocate space for the external relocations.  */
4632               amt = section->reloc_count;
4633               amt *= sizeof (Elf64_External_Rela);
4634               external_relocs = (Elf64_External_Rela *) bfd_malloc (amt);
4635               if (external_relocs == NULL)
4636                 {
4637                   goto error_ret_free_local;
4638                 }
4639
4640               /* Likewise for the internal relocations.  */
4641               amt = section->reloc_count;
4642               amt *= sizeof (Elf_Internal_Rela);
4643               internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
4644               if (internal_relocs == NULL)
4645                 {
4646                   free (external_relocs);
4647                   goto error_ret_free_local;
4648                 }
4649
4650               /* Read in the external relocs.  */
4651               input_rel_hdr = &elf_section_data (section)->rel_hdr;
4652               if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
4653                   || bfd_bread ((PTR) external_relocs,
4654                                 input_rel_hdr->sh_size,
4655                                 input_bfd) != input_rel_hdr->sh_size)
4656                 {
4657                   free (external_relocs);
4658                 error_ret_free_internal:
4659                   free (internal_relocs);
4660                   goto error_ret_free_local;
4661                 }
4662
4663               /* Swap in the relocs.  */
4664               erela = external_relocs;
4665               erelaend = erela + section->reloc_count;
4666               irela = internal_relocs;
4667               for (; erela < erelaend; erela++, irela++)
4668                 bfd_elf64_swap_reloca_in (input_bfd, erela, irela);
4669
4670               /* We're done with the external relocs, free them.  */
4671               free (external_relocs);
4672
4673               /* Now examine each relocation.  */
4674               irela = internal_relocs;
4675               irelaend = irela + section->reloc_count;
4676               for (; irela < irelaend; irela++)
4677                 {
4678                   unsigned int r_type, r_indx;
4679                   enum ppc_stub_type stub_type;
4680                   struct ppc_stub_hash_entry *stub_entry;
4681                   asection *sym_sec;
4682                   bfd_vma sym_value;
4683                   bfd_vma destination;
4684                   struct ppc_link_hash_entry *hash;
4685                   char *stub_name;
4686                   const asection *id_sec;
4687
4688                   r_type = ELF64_R_TYPE (irela->r_info);
4689                   r_indx = ELF64_R_SYM (irela->r_info);
4690
4691                   if (r_type >= (unsigned int) R_PPC_max)
4692                     {
4693                       bfd_set_error (bfd_error_bad_value);
4694                       goto error_ret_free_internal;
4695                     }
4696
4697                   /* Only look for stubs on branch instructions.  */
4698                   if (r_type != (unsigned int) R_PPC64_REL24
4699                       && r_type != (unsigned int) R_PPC64_REL14
4700                       && r_type != (unsigned int) R_PPC64_REL14_BRTAKEN
4701                       && r_type != (unsigned int) R_PPC64_REL14_BRNTAKEN)
4702                     continue;
4703
4704                   /* Now determine the call target, its name, value,
4705                      section.  */
4706                   sym_sec = NULL;
4707                   sym_value = 0;
4708                   destination = 0;
4709                   hash = NULL;
4710                   if (r_indx < symtab_hdr->sh_info)
4711                     {
4712                       /* It's a local symbol.  */
4713                       Elf_Internal_Sym *sym;
4714                       Elf_Internal_Shdr *hdr;
4715
4716                       sym = local_syms + r_indx;
4717                       hdr = elf_elfsections (input_bfd)[sym->st_shndx];
4718                       sym_sec = hdr->bfd_section;
4719                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4720                         sym_value = sym->st_value;
4721                       destination = (sym_value + irela->r_addend
4722                                      + sym_sec->output_offset
4723                                      + sym_sec->output_section->vma);
4724                     }
4725                   else
4726                     {
4727                       /* It's an external symbol.  */
4728                       int e_indx;
4729
4730                       e_indx = r_indx - symtab_hdr->sh_info;
4731                       hash = ((struct ppc_link_hash_entry *)
4732                               elf_sym_hashes (input_bfd)[e_indx]);
4733
4734                       while (hash->elf.root.type == bfd_link_hash_indirect
4735                              || hash->elf.root.type == bfd_link_hash_warning)
4736                         hash = ((struct ppc_link_hash_entry *)
4737                                 hash->elf.root.u.i.link);
4738
4739                       if (hash->elf.root.type == bfd_link_hash_defined
4740                           || hash->elf.root.type == bfd_link_hash_defweak)
4741                         {
4742                           sym_sec = hash->elf.root.u.def.section;
4743                           sym_value = hash->elf.root.u.def.value;
4744                           if (sym_sec->output_section != NULL)
4745                             destination = (sym_value + irela->r_addend
4746                                            + sym_sec->output_offset
4747                                            + sym_sec->output_section->vma);
4748                         }
4749                       else if (hash->elf.root.type == bfd_link_hash_undefweak)
4750                         ;
4751                       else if (hash->elf.root.type == bfd_link_hash_undefined)
4752                         ;
4753                       else
4754                         {
4755                           bfd_set_error (bfd_error_bad_value);
4756                           goto error_ret_free_internal;
4757                         }
4758                     }
4759
4760                   /* Determine what (if any) linker stub is needed.  */
4761                   stub_type = ppc_type_of_stub (section, irela, &hash,
4762                                                 destination);
4763                   if (stub_type == ppc_stub_none)
4764                     continue;
4765
4766                   /* Support for grouping stub sections.  */
4767                   id_sec = htab->stub_group[section->id].link_sec;
4768
4769                   /* Get the name of this stub.  */
4770                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
4771                   if (!stub_name)
4772                     goto error_ret_free_internal;
4773
4774                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4775                                                      stub_name, false, false);
4776                   if (stub_entry != NULL)
4777                     {
4778                       /* The proper stub has already been created.  */
4779                       free (stub_name);
4780                       continue;
4781                     }
4782
4783                   stub_entry = ppc_add_stub (stub_name, section, htab);
4784                   if (stub_entry == NULL)
4785                     {
4786                       free (stub_name);
4787                       goto error_ret_free_local;
4788                     }
4789
4790                   stub_entry->target_value = sym_value;
4791                   stub_entry->target_section = sym_sec;
4792                   stub_entry->stub_type = stub_type;
4793                   stub_entry->h = hash;
4794                   stub_changed = true;
4795                 }
4796
4797               /* We're done with the internal relocs, free them.  */
4798               free (internal_relocs);
4799             }
4800         }
4801
4802       if (!stub_changed)
4803         break;
4804
4805       /* OK, we've added some stubs.  Find out the new size of the
4806          stub sections.  */
4807       for (stub_sec = htab->stub_bfd->sections;
4808            stub_sec != NULL;
4809            stub_sec = stub_sec->next)
4810         {
4811           stub_sec->_raw_size = 0;
4812           stub_sec->_cooked_size = 0;
4813         }
4814       htab->sbrlt->_raw_size = 0;
4815       htab->sbrlt->_cooked_size = 0;
4816
4817       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, htab);
4818
4819       /* Ask the linker to do its stuff.  */
4820       (*htab->layout_sections_again) ();
4821     }
4822
4823   if (htab->sbrlt->_raw_size == 0)
4824     {
4825       _bfd_strip_section_from_output (info, htab->sbrlt);
4826       if (htab->srelbrlt != NULL)
4827         _bfd_strip_section_from_output (info, htab->srelbrlt);
4828     }
4829
4830   ret = true;
4831
4832  error_ret_free_local:
4833   while (htab->bfd_count-- > 0)
4834     if (htab->all_local_syms[htab->bfd_count])
4835       free (htab->all_local_syms[htab->bfd_count]);
4836   free (htab->all_local_syms);
4837
4838   return ret;
4839 }
4840
4841 /* Called after we have determined section placement.  If sections
4842    move, we'll be called again.  Provide a value for TOCstart.  */
4843
4844 bfd_vma
4845 ppc64_elf_toc (obfd)
4846      bfd *obfd;
4847 {
4848   asection *s;
4849   bfd_vma TOCstart;
4850
4851   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
4852      order.  The TOC starts where the first of these sections starts.  */
4853   s = bfd_get_section_by_name (obfd, ".got");
4854   if (s == NULL)
4855     s = bfd_get_section_by_name (obfd, ".toc");
4856   if (s == NULL)
4857     s = bfd_get_section_by_name (obfd, ".tocbss");
4858   if (s == NULL)
4859     s = bfd_get_section_by_name (obfd, ".plt");
4860   if (s == NULL)
4861     {
4862       /* This may happen for
4863          o  references to TOC base (SYM@toc / TOC[tc0]) without a
4864          .toc directive
4865          o  bad linker script
4866          o --gc-sections and empty TOC sections
4867
4868          FIXME: Warn user?  */
4869
4870       /* Look for a likely section.  We probably won't even be
4871          using TOCstart.  */
4872       for (s = obfd->sections; s != NULL; s = s->next)
4873         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
4874             == (SEC_ALLOC | SEC_SMALL_DATA))
4875           break;
4876       if (s == NULL)
4877         for (s = obfd->sections; s != NULL; s = s->next)
4878           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
4879               == (SEC_ALLOC | SEC_SMALL_DATA))
4880             break;
4881       if (s == NULL)
4882         for (s = obfd->sections; s != NULL; s = s->next)
4883           if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
4884             break;
4885       if (s == NULL)
4886         for (s = obfd->sections; s != NULL; s = s->next)
4887           if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
4888             break;
4889     }
4890
4891   TOCstart = 0;
4892   if (s != NULL)
4893     TOCstart = s->output_section->vma + s->output_offset;
4894
4895   return TOCstart;
4896 }
4897
4898 /* Build all the stubs associated with the current output file.
4899    The stubs are kept in a hash table attached to the main linker
4900    hash table.  This function is called via gldelf64ppc_finish.  */
4901
4902 boolean
4903 ppc64_elf_build_stubs (info)
4904      struct bfd_link_info *info;
4905 {
4906   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4907   asection *stub_sec;
4908   bfd_vma plt_r2;
4909   bfd_byte *p;
4910
4911   for (stub_sec = htab->stub_bfd->sections;
4912        stub_sec != NULL;
4913        stub_sec = stub_sec->next)
4914     {
4915       bfd_size_type size;
4916
4917       /* Allocate memory to hold the linker stubs.  */
4918       size = stub_sec->_raw_size;
4919       if (size != 0)
4920         {
4921           stub_sec->contents = (bfd_byte *) bfd_zalloc (htab->stub_bfd, size);
4922           if (stub_sec->contents == NULL)
4923             return false;
4924         }
4925       stub_sec->_cooked_size = 0;
4926     }
4927
4928   if (htab->splt != NULL)
4929     {
4930       /* Build the .glink plt call stub.  */
4931       plt_r2 = (htab->splt->output_offset
4932                 + htab->splt->output_section->vma
4933                 - elf_gp (htab->splt->output_section->owner)
4934                 - TOC_BASE_OFF);
4935       p = htab->sglink->contents;
4936       p = build_plt_stub (htab->sglink->owner, p, (int) plt_r2, 1);
4937       while (p - htab->sglink->contents < GLINK_CALL_STUB_SIZE)
4938         {
4939           bfd_put_32 (htab->sglink->owner, NOP, p);
4940           p += 4;
4941         }
4942       htab->sglink->_cooked_size = p - htab->sglink->contents;
4943
4944       /* Use reloc_count to count entries.  */
4945       htab->sglink->reloc_count = 0;
4946     }
4947
4948   if (htab->sbrlt->_raw_size != 0)
4949     {
4950       htab->sbrlt->contents = (bfd_byte *) bfd_zalloc (htab->sbrlt->owner,
4951                                                        htab->sbrlt->_raw_size);
4952       if (htab->sbrlt->contents == NULL)
4953         return false;
4954     }
4955
4956   /* Build the stubs as directed by the stub hash table.  */
4957   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
4958   htab->sglink->reloc_count = 0;
4959
4960   for (stub_sec = htab->stub_bfd->sections;
4961        stub_sec != NULL;
4962        stub_sec = stub_sec->next)
4963     {
4964       if (stub_sec->_raw_size != stub_sec->_cooked_size)
4965         break;
4966     }
4967
4968   if (stub_sec != NULL
4969       || htab->sglink->_raw_size != htab->sglink->_cooked_size)
4970     {
4971       htab->stub_error = true;
4972       (*_bfd_error_handler) (_("stubs don't match calculated size"));
4973     }
4974
4975   return !htab->stub_error;
4976 }
4977
4978 /* Set up any other section flags and such that may be necessary.  */
4979
4980 static boolean
4981 ppc64_elf_fake_sections (abfd, shdr, asect)
4982      bfd *abfd ATTRIBUTE_UNUSED;
4983      Elf64_Internal_Shdr *shdr;
4984      asection *asect;
4985 {
4986   if ((asect->flags & SEC_EXCLUDE) != 0)
4987     shdr->sh_flags |= SHF_EXCLUDE;
4988
4989   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
4990     shdr->sh_type = SHT_ORDERED;
4991
4992   return true;
4993 }
4994
4995 /* The RELOCATE_SECTION function is called by the ELF backend linker
4996    to handle the relocations for a section.
4997
4998    The relocs are always passed as Rela structures; if the section
4999    actually uses Rel structures, the r_addend field will always be
5000    zero.
5001
5002    This function is responsible for adjust the section contents as
5003    necessary, and (if using Rela relocs and generating a
5004    relocateable output file) adjusting the reloc addend as
5005    necessary.
5006
5007    This function does not have to worry about setting the reloc
5008    address or the reloc symbol index.
5009
5010    LOCAL_SYMS is a pointer to the swapped in local symbols.
5011
5012    LOCAL_SECTIONS is an array giving the section in the input file
5013    corresponding to the st_shndx field of each local symbol.
5014
5015    The global hash table entry for the global symbols can be found
5016    via elf_sym_hashes (input_bfd).
5017
5018    When generating relocateable output, this function must handle
5019    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
5020    going to be the section symbol corresponding to the output
5021    section, which means that the addend must be adjusted
5022    accordingly.  */
5023
5024 static boolean
5025 ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
5026                             contents, relocs, local_syms, local_sections)
5027      bfd *output_bfd;
5028      struct bfd_link_info *info;
5029      bfd *input_bfd;
5030      asection *input_section;
5031      bfd_byte *contents;
5032      Elf_Internal_Rela *relocs;
5033      Elf_Internal_Sym *local_syms;
5034      asection **local_sections;
5035 {
5036   struct ppc_link_hash_table *htab;
5037   Elf_Internal_Shdr *symtab_hdr;
5038   struct elf_link_hash_entry **sym_hashes;
5039   Elf_Internal_Rela *rel;
5040   Elf_Internal_Rela *relend;
5041   bfd_vma *local_got_offsets;
5042   bfd_vma TOCstart;
5043   boolean ret = true;
5044   boolean is_opd;
5045   /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
5046   boolean is_power4 = false;
5047
5048   if (info->relocateable)
5049     return true;
5050
5051   /* Initialize howto table if needed.  */
5052   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5053     ppc_howto_init ();
5054
5055   htab = ppc_hash_table (info);
5056   local_got_offsets = elf_local_got_offsets (input_bfd);
5057   TOCstart = elf_gp (output_bfd);
5058   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5059   sym_hashes = elf_sym_hashes (input_bfd);
5060   is_opd = strcmp (bfd_get_section_name (abfd, input_section), ".opd") == 0;
5061
5062   rel = relocs;
5063   relend = relocs + input_section->reloc_count;
5064   for (; rel < relend; rel++)
5065     {
5066       enum elf_ppc_reloc_type r_type;
5067       bfd_vma offset;
5068       bfd_vma addend;
5069       bfd_reloc_status_type r;
5070       Elf_Internal_Sym *sym;
5071       asection *sec;
5072       struct elf_link_hash_entry *h;
5073       struct elf_link_hash_entry *fdh;
5074       const char *sym_name;
5075       unsigned long r_symndx;
5076       bfd_vma relocation;
5077       boolean unresolved_reloc;
5078       boolean warned;
5079       long insn;
5080       struct ppc_stub_hash_entry *stub_entry;
5081       bfd_vma max_br_offset;
5082       bfd_vma from;
5083
5084       r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
5085       r_symndx = ELF64_R_SYM (rel->r_info);
5086       offset = rel->r_offset;
5087       addend = rel->r_addend;
5088       r = bfd_reloc_other;
5089       sym = (Elf_Internal_Sym *) 0;
5090       sec = (asection *) 0;
5091       h = (struct elf_link_hash_entry *) 0;
5092       sym_name = (const char *) 0;
5093       unresolved_reloc = false;
5094       warned = false;
5095
5096       if (r_type == R_PPC64_TOC)
5097         {
5098           /* Relocation value is TOC base.  Symbol is ignored.  */
5099           relocation = TOCstart + TOC_BASE_OFF;
5100         }
5101       else if (r_symndx < symtab_hdr->sh_info)
5102         {
5103           /* It's a local symbol.  */
5104           sym = local_syms + r_symndx;
5105           sec = local_sections[r_symndx];
5106           sym_name = "<local symbol>";
5107
5108           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
5109           /* rel may have changed, update our copy of addend.  */
5110           addend = rel->r_addend;
5111         }
5112       else
5113         {
5114           /* It's a global symbol.  */
5115           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5116           while (h->root.type == bfd_link_hash_indirect
5117                  || h->root.type == bfd_link_hash_warning)
5118             h = (struct elf_link_hash_entry *) h->root.u.i.link;
5119           sym_name = h->root.root.string;
5120           relocation = 0;
5121           if (h->root.type == bfd_link_hash_defined
5122               || h->root.type == bfd_link_hash_defweak)
5123             {
5124               sec = h->root.u.def.section;
5125               if (sec->output_section == NULL)
5126                 /* Set a flag that will be cleared later if we find a
5127                    relocation value for this symbol.  output_section
5128                    is typically NULL for symbols satisfied by a shared
5129                    library.  */
5130                 unresolved_reloc = true;
5131               else
5132                 relocation = (h->root.u.def.value
5133                               + sec->output_section->vma
5134                               + sec->output_offset);
5135             }
5136           else if (h->root.type == bfd_link_hash_undefweak)
5137             ;
5138           else if (info->shared
5139                    && (!info->symbolic || info->allow_shlib_undefined)
5140                    && !info->no_undefined
5141                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
5142             ;
5143           else
5144             {
5145               if (! ((*info->callbacks->undefined_symbol)
5146                      (info, h->root.root.string, input_bfd, input_section,
5147                       offset, (!info->shared
5148                                || info->no_undefined
5149                                || ELF_ST_VISIBILITY (h->other)))))
5150                 return false;
5151               warned = true;
5152             }
5153         }
5154
5155       /* First handle relocations that tweak non-addend part of insn.  */
5156       insn = 0;
5157       switch (r_type)
5158         {
5159         default:
5160           break;
5161
5162           /* Branch taken prediction relocations.  */
5163         case R_PPC64_ADDR14_BRTAKEN:
5164         case R_PPC64_REL14_BRTAKEN:
5165           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
5166           /* Fall thru. */
5167
5168           /* Branch not taken prediction relocations.  */
5169         case R_PPC64_ADDR14_BRNTAKEN:
5170         case R_PPC64_REL14_BRNTAKEN:
5171           insn |= bfd_get_32 (output_bfd, contents + offset) & ~(0x01 << 21);
5172           if (is_power4)
5173             {
5174               /* Set 'a' bit.  This is 0b00010 in BO field for branch
5175                  on CR(BI) insns (BO == 001at or 011at), and 0b01000
5176                  for branch on CTR insns (BO == 1a00t or 1a01t).  */
5177               if ((insn & (0x14 << 21)) == (0x04 << 21))
5178                 insn |= 0x02 << 21;
5179               else if ((insn & (0x14 << 21)) == (0x10 << 21))
5180                 insn |= 0x08 << 21;
5181               else
5182                 break;
5183             }
5184           else
5185             {
5186               from = (offset
5187                       + input_section->output_offset
5188                       + input_section->output_section->vma);
5189
5190               /* Invert 'y' bit if not the default.  */
5191               if ((bfd_signed_vma) (relocation + addend - from) < 0)
5192                 insn ^= 0x01 << 21;
5193             }
5194
5195           bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
5196           break;
5197
5198         case R_PPC64_REL24:
5199           /* A REL24 branching to a linkage function is followed by a
5200              nop.  We replace the nop with a ld in order to restore
5201              the TOC base pointer.  Only calls to shared objects need
5202              to alter the TOC base.  These are recognized by their
5203              need for a PLT entry.  */
5204           if (h != NULL
5205               && (fdh = ((struct ppc_link_hash_entry *) h)->oh) != NULL
5206               && fdh->plt.offset != (bfd_vma) -1
5207               && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
5208                                                    rel, htab)) != NULL)
5209             {
5210               boolean can_plt_call = 0;
5211
5212               if (offset + 8 <= input_section->_cooked_size)
5213                 {
5214                   insn = bfd_get_32 (input_bfd, contents + offset + 4);
5215                   if (insn == NOP
5216                       || insn == CROR_151515 || insn == CROR_313131)
5217                     {
5218                       bfd_put_32 (input_bfd, (bfd_vma) LD_R2_40R1,
5219                                   contents + offset + 4);
5220                       can_plt_call = 1;
5221                     }
5222                 }
5223
5224               if (!can_plt_call)
5225                 {
5226                   /* If this is a plain branch rather than a branch
5227                      and link, don't require a nop.  */
5228                   insn = bfd_get_32 (input_bfd, contents + offset);
5229                   if ((insn & 1) == 0)
5230                     can_plt_call = 1;
5231                 }
5232
5233               if (can_plt_call)
5234                 {
5235                   relocation = (stub_entry->stub_offset
5236                                 + stub_entry->stub_sec->output_offset
5237                                 + stub_entry->stub_sec->output_section->vma);
5238                   addend = 0;
5239                   unresolved_reloc = false;
5240                 }
5241             }
5242
5243           if (h != NULL
5244               && h->root.type == bfd_link_hash_undefweak
5245               && relocation == 0
5246               && addend == 0)
5247             {
5248               /* Tweak calls to undefined weak functions to point at a
5249                  blr.  We can thus call a weak function without first
5250                  checking whether the function is defined.  We have a
5251                  blr at the end of .sfpr.  */
5252               BFD_ASSERT (htab->sfpr->_raw_size != 0);
5253               relocation = (htab->sfpr->_raw_size - 4
5254                             + htab->sfpr->output_offset
5255                             + htab->sfpr->output_section->vma);
5256               from = (offset
5257                       + input_section->output_offset
5258                       + input_section->output_section->vma);
5259
5260               /* But let's not be silly about it.  If the blr isn't in
5261                  reach, just go to the next instruction.  */
5262               if (relocation - from + (1 << 25) >= (1 << 26)
5263                   || htab->sfpr->_raw_size == 0)
5264                 relocation = from + 4;
5265             }
5266           break;
5267         }
5268
5269       /* Set `addend'.  */
5270       switch (r_type)
5271         {
5272         default:
5273           (*_bfd_error_handler)
5274             (_("%s: unknown relocation type %d for symbol %s"),
5275              bfd_archive_filename (input_bfd), (int) r_type, sym_name);
5276
5277           bfd_set_error (bfd_error_bad_value);
5278           ret = false;
5279           continue;
5280
5281         case R_PPC64_NONE:
5282         case R_PPC_GNU_VTINHERIT:
5283         case R_PPC_GNU_VTENTRY:
5284           continue;
5285
5286           /* GOT16 relocations.  Like an ADDR16 using the symbol's
5287              address in the GOT as relocation value instead of the
5288              symbols value itself.  Also, create a GOT entry for the
5289              symbol and put the symbol value there.  */
5290         case R_PPC64_GOT16:
5291         case R_PPC64_GOT16_LO:
5292         case R_PPC64_GOT16_HI:
5293         case R_PPC64_GOT16_HA:
5294         case R_PPC64_GOT16_DS:
5295         case R_PPC64_GOT16_LO_DS:
5296           {
5297             /* Relocation is to the entry for this symbol in the global
5298                offset table.  */
5299             bfd_vma off;
5300
5301             if (htab->sgot == NULL)
5302               abort ();
5303
5304             if (h != NULL)
5305               {
5306                 boolean dyn;
5307
5308                 off = h->got.offset;
5309                 dyn = htab->elf.dynamic_sections_created;
5310                 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
5311                     || (info->shared
5312                         && (info->symbolic
5313                             || h->dynindx == -1
5314                             || (h->elf_link_hash_flags
5315                                 & ELF_LINK_FORCED_LOCAL))
5316                         && (h->elf_link_hash_flags
5317                             & ELF_LINK_HASH_DEF_REGULAR)))
5318                   {
5319                     /* This is actually a static link, or it is a
5320                        -Bsymbolic link and the symbol is defined
5321                        locally, or the symbol was forced to be local
5322                        because of a version file.  We must initialize
5323                        this entry in the global offset table.  Since the
5324                        offset must always be a multiple of 8, we use the
5325                        least significant bit to record whether we have
5326                        initialized it already.
5327
5328                        When doing a dynamic link, we create a .rel.got
5329                        relocation entry to initialize the value.  This
5330                        is done in the finish_dynamic_symbol routine.  */
5331                     if ((off & 1) != 0)
5332                       off &= ~1;
5333                     else
5334                       {
5335                         bfd_put_64 (output_bfd, relocation,
5336                                     htab->sgot->contents + off);
5337                         h->got.offset |= 1;
5338                       }
5339                   }
5340                 else
5341                   unresolved_reloc = false;
5342               }
5343             else
5344               {
5345                 if (local_got_offsets == NULL)
5346                   abort ();
5347
5348                 off = local_got_offsets[r_symndx];
5349
5350                 /* The offset must always be a multiple of 8.  We use
5351                    the least significant bit to record whether we have
5352                    already processed this entry.  */
5353                 if ((off & 1) != 0)
5354                   off &= ~1;
5355                 else
5356                   {
5357                     bfd_put_64 (output_bfd, relocation,
5358                                 htab->sgot->contents + off);
5359
5360                     if (info->shared)
5361                       {
5362                         Elf_Internal_Rela outrel;
5363                         Elf64_External_Rela *loc;
5364
5365                         /* We need to generate a R_PPC64_RELATIVE reloc
5366                            for the dynamic linker.  */
5367                         outrel.r_offset = (htab->sgot->output_section->vma
5368                                            + htab->sgot->output_offset
5369                                            + off);
5370                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
5371                         outrel.r_addend = relocation;
5372                         loc = (Elf64_External_Rela *) htab->srelgot->contents;
5373                         loc += htab->srelgot->reloc_count++;
5374                         bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
5375                       }
5376
5377                     local_got_offsets[r_symndx] |= 1;
5378                   }
5379               }
5380
5381             if (off >= (bfd_vma) -2)
5382               abort ();
5383
5384             relocation = htab->sgot->output_offset + off;
5385
5386             /* TOC base (r2) is TOC start plus 0x8000.  */
5387             addend -= TOC_BASE_OFF;
5388           }
5389           break;
5390
5391         case R_PPC64_PLT16_HA:
5392         case R_PPC64_PLT16_HI:
5393         case R_PPC64_PLT16_LO:
5394         case R_PPC64_PLT32:
5395         case R_PPC64_PLT64:
5396           /* Relocation is to the entry for this symbol in the
5397              procedure linkage table.  */
5398
5399           /* Resolve a PLT reloc against a local symbol directly,
5400              without using the procedure linkage table.  */
5401           if (h == NULL)
5402             break;
5403
5404           if (h->plt.offset == (bfd_vma) -1
5405               || htab->splt == NULL)
5406             {
5407               /* We didn't make a PLT entry for this symbol.  This
5408                  happens when statically linking PIC code, or when
5409                  using -Bsymbolic.  */
5410               break;
5411             }
5412
5413           relocation = (htab->splt->output_section->vma
5414                         + htab->splt->output_offset
5415                         + h->plt.offset);
5416           unresolved_reloc = false;
5417           break;
5418
5419           /* TOC16 relocs.  We want the offset relative to the TOC base,
5420              which is the address of the start of the TOC plus 0x8000.
5421              The TOC consists of sections .got, .toc, .tocbss, and .plt,
5422              in this order.  */
5423         case R_PPC64_TOC16:
5424         case R_PPC64_TOC16_LO:
5425         case R_PPC64_TOC16_HI:
5426         case R_PPC64_TOC16_DS:
5427         case R_PPC64_TOC16_LO_DS:
5428         case R_PPC64_TOC16_HA:
5429           addend -= TOCstart + TOC_BASE_OFF;
5430           break;
5431
5432           /* Relocate against the beginning of the section.  */
5433         case R_PPC64_SECTOFF:
5434         case R_PPC64_SECTOFF_LO:
5435         case R_PPC64_SECTOFF_HI:
5436         case R_PPC64_SECTOFF_DS:
5437         case R_PPC64_SECTOFF_LO_DS:
5438         case R_PPC64_SECTOFF_HA:
5439           if (sec != (asection *) 0)
5440             addend -= sec->output_section->vma;
5441           break;
5442
5443         case R_PPC64_REL14:
5444         case R_PPC64_REL14_BRNTAKEN:
5445         case R_PPC64_REL14_BRTAKEN:
5446         case R_PPC64_REL24:
5447           break;
5448
5449           /* Relocations that may need to be propagated if this is a
5450              dynamic object.  */
5451         case R_PPC64_REL32:
5452         case R_PPC64_REL64:
5453         case R_PPC64_ADDR14:
5454         case R_PPC64_ADDR14_BRNTAKEN:
5455         case R_PPC64_ADDR14_BRTAKEN:
5456         case R_PPC64_ADDR16:
5457         case R_PPC64_ADDR16_DS:
5458         case R_PPC64_ADDR16_HA:
5459         case R_PPC64_ADDR16_HI:
5460         case R_PPC64_ADDR16_HIGHER:
5461         case R_PPC64_ADDR16_HIGHERA:
5462         case R_PPC64_ADDR16_HIGHEST:
5463         case R_PPC64_ADDR16_HIGHESTA:
5464         case R_PPC64_ADDR16_LO:
5465         case R_PPC64_ADDR16_LO_DS:
5466         case R_PPC64_ADDR24:
5467         case R_PPC64_ADDR30:
5468         case R_PPC64_ADDR32:
5469         case R_PPC64_ADDR64:
5470         case R_PPC64_UADDR16:
5471         case R_PPC64_UADDR32:
5472         case R_PPC64_UADDR64:
5473           /* r_symndx will be zero only for relocs against symbols
5474              from removed linkonce sections, or sections discarded by
5475              a linker script.  */
5476           if (r_symndx == 0)
5477             break;
5478           /* Fall thru.  */
5479
5480         case R_PPC64_TOC:
5481           if ((input_section->flags & SEC_ALLOC) == 0)
5482             break;
5483
5484           if (NO_OPD_RELOCS && is_opd)
5485             break;
5486
5487           if ((info->shared
5488                && (IS_ABSOLUTE_RELOC (r_type)
5489                    || (h != NULL
5490                        && h->dynindx != -1
5491                        && (! info->symbolic
5492                            || (h->elf_link_hash_flags
5493                                & ELF_LINK_HASH_DEF_REGULAR) == 0))))
5494               || (!info->shared
5495                   && h != NULL
5496                   && h->dynindx != -1
5497                   && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5498                   && (((h->elf_link_hash_flags
5499                         & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5500                        && (h->elf_link_hash_flags
5501                            & ELF_LINK_HASH_DEF_REGULAR) == 0)
5502                       || h->root.type == bfd_link_hash_undefweak
5503                       || h->root.type == bfd_link_hash_undefined)))
5504             {
5505               Elf_Internal_Rela outrel;
5506               boolean skip, relocate;
5507               asection *sreloc;
5508               Elf64_External_Rela *loc;
5509
5510               /* When generating a dynamic object, these relocations
5511                  are copied into the output file to be resolved at run
5512                  time.  */
5513
5514               skip = false;
5515               relocate = false;
5516
5517               outrel.r_offset =
5518                 _bfd_elf_section_offset (output_bfd, info, input_section,
5519                                          rel->r_offset);
5520               if (outrel.r_offset == (bfd_vma) -1)
5521                 skip = true;
5522               else if (outrel.r_offset == (bfd_vma) -2)
5523                 skip = true, relocate = true;
5524               outrel.r_offset += (input_section->output_section->vma
5525                                   + input_section->output_offset);
5526               outrel.r_addend = addend;
5527
5528               if (skip)
5529                 memset (&outrel, 0, sizeof outrel);
5530               else if (h != NULL
5531                        && h->dynindx != -1
5532                        && !is_opd
5533                        && (!IS_ABSOLUTE_RELOC (r_type)
5534                            || !info->shared
5535                            || !info->symbolic
5536                            || (h->elf_link_hash_flags
5537                                & ELF_LINK_HASH_DEF_REGULAR) == 0))
5538                 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
5539               else
5540                 {
5541                   /* This symbol is local, or marked to become local,
5542                      or this is an opd section reloc which must point
5543                      at a local function.  */
5544                   outrel.r_addend += relocation;
5545                   relocate = true;
5546                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5547                     {
5548                       outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
5549                     }
5550                   else
5551                     {
5552                       long indx = 0;
5553
5554                       if (bfd_is_abs_section (sec))
5555                         ;
5556                       else if (sec == NULL || sec->owner == NULL)
5557                         {
5558                           bfd_set_error (bfd_error_bad_value);
5559                           return false;
5560                         }
5561                       else
5562                         {
5563                           asection *osec;
5564
5565                           osec = sec->output_section;
5566                           indx = elf_section_data (osec)->dynindx;
5567
5568                           /* We are turning this relocation into one
5569                              against a section symbol, so subtract out
5570                              the output section's address but not the
5571                              offset of the input section in the output
5572                              section.  */
5573                           outrel.r_addend -= osec->vma;
5574                         }
5575
5576                       outrel.r_info = ELF64_R_INFO (indx, r_type);
5577                     }
5578                 }
5579
5580               sreloc = elf_section_data (input_section)->sreloc;
5581               if (sreloc == NULL)
5582                 abort ();
5583
5584               loc = (Elf64_External_Rela *) sreloc->contents;
5585               loc += sreloc->reloc_count++;
5586               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
5587
5588               /* If this reloc is against an external symbol, it will
5589                  be computed at runtime, so there's no need to do
5590                  anything now.  */
5591               if (! relocate)
5592                 continue;
5593             }
5594           break;
5595
5596         case R_PPC64_COPY:
5597         case R_PPC64_GLOB_DAT:
5598         case R_PPC64_JMP_SLOT:
5599         case R_PPC64_RELATIVE:
5600           /* We shouldn't ever see these dynamic relocs in relocatable
5601              files.  */
5602           /* Fall thru */
5603
5604         case R_PPC64_PLTGOT16:
5605         case R_PPC64_PLTGOT16_DS:
5606         case R_PPC64_PLTGOT16_HA:
5607         case R_PPC64_PLTGOT16_HI:
5608         case R_PPC64_PLTGOT16_LO:
5609         case R_PPC64_PLTGOT16_LO_DS:
5610         case R_PPC64_PLTREL32:
5611         case R_PPC64_PLTREL64:
5612           /* These ones haven't been implemented yet.  */
5613
5614           (*_bfd_error_handler)
5615             (_("%s: Relocation %s is not supported for symbol %s."),
5616              bfd_archive_filename (input_bfd),
5617              ppc64_elf_howto_table[(int) r_type]->name, sym_name);
5618
5619           bfd_set_error (bfd_error_invalid_operation);
5620           ret = false;
5621           continue;
5622         }
5623
5624       /* Do any further special processing.  */
5625       switch (r_type)
5626         {
5627         default:
5628           break;
5629
5630         case R_PPC64_ADDR16_HA:
5631         case R_PPC64_ADDR16_HIGHERA:
5632         case R_PPC64_ADDR16_HIGHESTA:
5633         case R_PPC64_PLT16_HA:
5634         case R_PPC64_TOC16_HA:
5635         case R_PPC64_SECTOFF_HA:
5636           /* It's just possible that this symbol is a weak symbol
5637              that's not actually defined anywhere. In that case,
5638              'sec' would be NULL, and we should leave the symbol
5639              alone (it will be set to zero elsewhere in the link).  */
5640           if (sec != NULL)
5641             /* Add 0x10000 if sign bit in 0:15 is set.  */
5642             addend += ((relocation + addend) & 0x8000) << 1;
5643           break;
5644
5645         case R_PPC64_ADDR16_DS:
5646         case R_PPC64_ADDR16_LO_DS:
5647         case R_PPC64_GOT16_DS:
5648         case R_PPC64_GOT16_LO_DS:
5649         case R_PPC64_PLT16_LO_DS:
5650         case R_PPC64_SECTOFF_DS:
5651         case R_PPC64_SECTOFF_LO_DS:
5652         case R_PPC64_TOC16_DS:
5653         case R_PPC64_TOC16_LO_DS:
5654         case R_PPC64_PLTGOT16_DS:
5655         case R_PPC64_PLTGOT16_LO_DS:
5656           if (((relocation + addend) & 3) != 0)
5657             {
5658               (*_bfd_error_handler)
5659                 (_("%s: error: relocation %s not a multiple of 4"),
5660                  bfd_archive_filename (input_bfd),
5661                  ppc64_elf_howto_table[(int) r_type]->name);
5662               bfd_set_error (bfd_error_bad_value);
5663               ret = false;
5664               continue;
5665             }
5666           break;
5667
5668         case R_PPC64_REL14:
5669         case R_PPC64_REL14_BRNTAKEN:
5670         case R_PPC64_REL14_BRTAKEN:
5671           max_br_offset = 1 << 15;
5672           goto branch_check;
5673
5674         case R_PPC64_REL24:
5675           max_br_offset = 1 << 25;
5676
5677         branch_check:
5678           /* If the branch is out of reach, then redirect the
5679              call to the local stub for this function.  */
5680           from = (offset
5681                   + input_section->output_offset
5682                   + input_section->output_section->vma);
5683           if (relocation + addend - from + max_br_offset >= 2 * max_br_offset
5684               && (stub_entry = ppc_get_stub_entry (input_section, sec, h,
5685                                                    rel, htab)) != NULL)
5686             {
5687               /* Munge up the value and addend so that we call the stub
5688                  rather than the procedure directly.  */
5689               relocation = (stub_entry->stub_offset
5690                             + stub_entry->stub_sec->output_offset
5691                             + stub_entry->stub_sec->output_section->vma);
5692               addend = 0;
5693             }
5694           break;
5695         }
5696
5697       /* FIXME: Why do we allow debugging sections to escape this error?
5698          More importantly, why do we not emit dynamic relocs above in
5699          debugging sections (which are ! SEC_ALLOC)?  If we had
5700          emitted the dynamic reloc, we could remove the fudge here.  */
5701       if (unresolved_reloc
5702           && !(info->shared
5703                && (input_section->flags & SEC_DEBUGGING) != 0
5704                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
5705         (*_bfd_error_handler)
5706           (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
5707            bfd_archive_filename (input_bfd),
5708            bfd_get_section_name (input_bfd, input_section),
5709            (long) rel->r_offset,
5710            h->root.root.string);
5711
5712       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5713                                     input_bfd,
5714                                     input_section,
5715                                     contents,
5716                                     offset,
5717                                     relocation,
5718                                     addend);
5719
5720       if (r != bfd_reloc_ok)
5721         {
5722           const char *name;
5723
5724           if (h != NULL)
5725             {
5726               if (h->root.type == bfd_link_hash_undefweak
5727                   && ppc64_elf_howto_table[(int) r_type]->pc_relative)
5728                 {
5729                   /* Assume this is a call protected by other code that
5730                      detects the symbol is undefined.  If this is the case,
5731                      we can safely ignore the overflow.  If not, the
5732                      program is hosed anyway, and a little warning isn't
5733                      going to help.  */
5734
5735                   continue;
5736                 }
5737
5738               name = h->root.root.string;
5739             }
5740           else
5741             {
5742               name = bfd_elf_string_from_elf_section (input_bfd,
5743                                                       symtab_hdr->sh_link,
5744                                                       sym->st_name);
5745               if (name == NULL)
5746                 continue;
5747               if (*name == '\0')
5748                 name = bfd_section_name (input_bfd, sec);
5749             }
5750
5751           if (r == bfd_reloc_overflow)
5752             {
5753               if (warned)
5754                 continue;
5755               if (!((*info->callbacks->reloc_overflow)
5756                     (info, name, ppc64_elf_howto_table[(int) r_type]->name,
5757                      rel->r_addend, input_bfd, input_section, offset)))
5758                 return false;
5759             }
5760           else
5761             {
5762               (*_bfd_error_handler)
5763                 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
5764                  bfd_archive_filename (input_bfd),
5765                  bfd_get_section_name (input_bfd, input_section),
5766                  (long) rel->r_offset, name, (int) r);
5767               ret = false;
5768             }
5769         }
5770     }
5771
5772   return ret;
5773 }
5774
5775 /* Finish up dynamic symbol handling.  We set the contents of various
5776    dynamic sections here.  */
5777
5778 static boolean
5779 ppc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
5780      bfd *output_bfd;
5781      struct bfd_link_info *info;
5782      struct elf_link_hash_entry *h;
5783      Elf_Internal_Sym *sym;
5784 {
5785   struct ppc_link_hash_table *htab;
5786   bfd *dynobj;
5787
5788   htab = ppc_hash_table (info);
5789   dynobj = htab->elf.dynobj;
5790
5791   if (h->plt.offset != (bfd_vma) -1
5792       && ((struct ppc_link_hash_entry *) h)->is_func_descriptor)
5793     {
5794       Elf_Internal_Rela rela;
5795       Elf64_External_Rela *loc;
5796
5797       /* This symbol has an entry in the procedure linkage table.  Set
5798          it up.  */
5799
5800       if (htab->splt == NULL
5801           || htab->srelplt == NULL
5802           || htab->sglink == NULL)
5803         abort ();
5804
5805       /* Create a JMP_SLOT reloc to inform the dynamic linker to
5806          fill in the PLT entry.  */
5807
5808       rela.r_offset = (htab->splt->output_section->vma
5809                        + htab->splt->output_offset
5810                        + h->plt.offset);
5811       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
5812       rela.r_addend = 0;
5813
5814       loc = (Elf64_External_Rela *) htab->srelplt->contents;
5815       loc += (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE;
5816       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5817     }
5818
5819   if (h->got.offset != (bfd_vma) -1)
5820     {
5821       Elf_Internal_Rela rela;
5822       Elf64_External_Rela *loc;
5823
5824       /* This symbol has an entry in the global offset table.  Set it
5825          up.  */
5826
5827       if (htab->sgot == NULL || htab->srelgot == NULL)
5828         abort ();
5829
5830       rela.r_offset = (htab->sgot->output_section->vma
5831                        + htab->sgot->output_offset
5832                        + (h->got.offset &~ (bfd_vma) 1));
5833
5834       /* If this is a static link, or it is a -Bsymbolic link and the
5835          symbol is defined locally or was forced to be local because
5836          of a version file, we just want to emit a RELATIVE reloc.
5837          The entry in the global offset table will already have been
5838          initialized in the relocate_section function.  */
5839       if (info->shared
5840           && (info->symbolic
5841               || h->dynindx == -1
5842               || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
5843           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
5844         {
5845           BFD_ASSERT((h->got.offset & 1) != 0);
5846           rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
5847           rela.r_addend = (h->root.u.def.value
5848                            + h->root.u.def.section->output_section->vma
5849                            + h->root.u.def.section->output_offset);
5850         }
5851       else
5852         {
5853           BFD_ASSERT ((h->got.offset & 1) == 0);
5854           bfd_put_64 (output_bfd, (bfd_vma) 0,
5855                       htab->sgot->contents + h->got.offset);
5856           rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_GLOB_DAT);
5857           rela.r_addend = 0;
5858         }
5859
5860       loc = (Elf64_External_Rela *) htab->srelgot->contents;
5861       loc += htab->srelgot->reloc_count++;
5862       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5863     }
5864
5865   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
5866     {
5867       Elf_Internal_Rela rela;
5868       Elf64_External_Rela *loc;
5869
5870       /* This symbol needs a copy reloc.  Set it up.  */
5871
5872       if (h->dynindx == -1
5873           || (h->root.type != bfd_link_hash_defined
5874               && h->root.type != bfd_link_hash_defweak)
5875           || htab->srelbss == NULL)
5876         abort ();
5877
5878       rela.r_offset = (h->root.u.def.value
5879                        + h->root.u.def.section->output_section->vma
5880                        + h->root.u.def.section->output_offset);
5881       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
5882       rela.r_addend = 0;
5883       loc = (Elf64_External_Rela *) htab->srelbss->contents;
5884       loc += htab->srelbss->reloc_count++;
5885       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5886     }
5887
5888   /* Mark some specially defined symbols as absolute.  */
5889   if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
5890     sym->st_shndx = SHN_ABS;
5891
5892   return true;
5893 }
5894
5895 /* Used to decide how to sort relocs in an optimal manner for the
5896    dynamic linker, before writing them out.  */
5897
5898 static enum elf_reloc_type_class
5899 ppc64_elf_reloc_type_class (rela)
5900      const Elf_Internal_Rela *rela;
5901 {
5902   enum elf_ppc_reloc_type r_type;
5903
5904   r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rela->r_info);
5905   switch (r_type)
5906     {
5907     case R_PPC64_RELATIVE:
5908       return reloc_class_relative;
5909     case R_PPC64_JMP_SLOT:
5910       return reloc_class_plt;
5911     case R_PPC64_COPY:
5912       return reloc_class_copy;
5913     default:
5914       return reloc_class_normal;
5915     }
5916 }
5917
5918 /* Finish up the dynamic sections.  */
5919
5920 static boolean
5921 ppc64_elf_finish_dynamic_sections (output_bfd, info)
5922      bfd *output_bfd;
5923      struct bfd_link_info *info;
5924 {
5925   struct ppc_link_hash_table *htab;
5926   bfd *dynobj;
5927   asection *sdyn;
5928
5929   htab = ppc_hash_table (info);
5930   dynobj = htab->elf.dynobj;
5931   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5932
5933   if (htab->elf.dynamic_sections_created)
5934     {
5935       Elf64_External_Dyn *dyncon, *dynconend;
5936
5937       if (sdyn == NULL || htab->sgot == NULL)
5938         abort ();
5939
5940       dyncon = (Elf64_External_Dyn *) sdyn->contents;
5941       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
5942       for (; dyncon < dynconend; dyncon++)
5943         {
5944           Elf_Internal_Dyn dyn;
5945           asection *s;
5946
5947           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
5948
5949           switch (dyn.d_tag)
5950             {
5951             default:
5952               continue;
5953
5954             case DT_PPC64_GLINK:
5955               dyn.d_un.d_ptr = (htab->sglink->output_section->vma
5956                                 + htab->sglink->output_offset);
5957               break;
5958
5959             case DT_PPC64_OPD:
5960               s = bfd_get_section_by_name (output_bfd, ".opd");
5961               if (s != NULL)
5962                 dyn.d_un.d_ptr = s->vma;
5963               break;
5964
5965             case DT_PPC64_OPDSZ:
5966               s = bfd_get_section_by_name (output_bfd, ".opd");
5967               if (s != NULL)
5968                 dyn.d_un.d_val = s->_raw_size;
5969               break;
5970
5971             case DT_PLTGOT:
5972               dyn.d_un.d_ptr = (htab->splt->output_section->vma
5973                                 + htab->splt->output_offset);
5974               break;
5975
5976             case DT_JMPREL:
5977               dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
5978                                 + htab->srelplt->output_offset);
5979               break;
5980
5981             case DT_PLTRELSZ:
5982               dyn.d_un.d_val = htab->srelplt->_raw_size;
5983               break;
5984
5985             case DT_RELASZ:
5986               /* Don't count procedure linkage table relocs in the
5987                  overall reloc count.  */
5988               if (htab->srelplt != NULL)
5989                 dyn.d_un.d_val -= htab->srelplt->_raw_size;
5990               break;
5991             }
5992
5993           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5994         }
5995     }
5996
5997   if (htab->sgot != NULL && htab->sgot->_raw_size != 0)
5998     {
5999       /* Fill in the first entry in the global offset table.
6000          We use it to hold the link-time TOCbase.  */
6001       bfd_put_64 (output_bfd,
6002                   elf_gp (output_bfd) + TOC_BASE_OFF,
6003                   htab->sgot->contents);
6004
6005       /* Set .got entry size.  */
6006       elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 8;
6007     }
6008
6009   if (htab->splt != NULL && htab->splt->_raw_size != 0)
6010     {
6011       /* Set .plt entry size.  */
6012       elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize
6013         = PLT_ENTRY_SIZE;
6014     }
6015
6016   return true;
6017 }
6018
6019 #define TARGET_LITTLE_SYM       bfd_elf64_powerpcle_vec
6020 #define TARGET_LITTLE_NAME      "elf64-powerpcle"
6021 #define TARGET_BIG_SYM          bfd_elf64_powerpc_vec
6022 #define TARGET_BIG_NAME         "elf64-powerpc"
6023 #define ELF_ARCH                bfd_arch_powerpc
6024 #define ELF_MACHINE_CODE        EM_PPC64
6025 #define ELF_MAXPAGESIZE         0x10000
6026 #define elf_info_to_howto       ppc64_elf_info_to_howto
6027
6028 #ifdef  EM_CYGNUS_POWERPC
6029 #define ELF_MACHINE_ALT1        EM_CYGNUS_POWERPC
6030 #endif
6031
6032 #ifdef EM_PPC_OLD
6033 #define ELF_MACHINE_ALT2        EM_PPC_OLD
6034 #endif
6035
6036 #define elf_backend_want_got_sym 0
6037 #define elf_backend_want_plt_sym 0
6038 #define elf_backend_plt_alignment 3
6039 #define elf_backend_plt_not_loaded 1
6040 #define elf_backend_got_symbol_offset 0
6041 #define elf_backend_got_header_size 8
6042 #define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
6043 #define elf_backend_can_gc_sections 1
6044 #define elf_backend_can_refcount 1
6045 #define elf_backend_rela_normal 1
6046
6047 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
6048 #define bfd_elf64_bfd_set_private_flags       ppc64_elf_set_private_flags
6049 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
6050 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
6051 #define bfd_elf64_bfd_link_hash_table_free    ppc64_elf_link_hash_table_free
6052
6053 #define elf_backend_section_from_shdr         ppc64_elf_section_from_shdr
6054 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
6055 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
6056 #define elf_backend_check_relocs              ppc64_elf_check_relocs
6057 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
6058 #define elf_backend_gc_sweep_hook             ppc64_elf_gc_sweep_hook
6059 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
6060 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
6061 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
6062 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
6063 #define elf_backend_fake_sections             ppc64_elf_fake_sections
6064 #define elf_backend_relocate_section          ppc64_elf_relocate_section
6065 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
6066 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
6067 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
6068
6069 #include "elf64-target.h"