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