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