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