* elf64-ppc.c (ppc64_elf_finish_dynamic_sections): Correct sign of
[external/binutils.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3    Written by Linus Nordberg, Swox AB <info@swox.com>,
4    based on elf32-ppc.c by Ian Lance Taylor.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 /* This file is based on the 64-bit PowerPC ELF ABI.  It is also based
23    on the file elf32-ppc.c.  */
24
25 #include "bfd.h"
26 #include "sysdep.h"
27 #include "bfdlink.h"
28 #include "libbfd.h"
29 #include "elf-bfd.h"
30 #include "elf/ppc.h"
31 #include "elf64-ppc.h"
32
33 #define USE_RELA                /* we want RELA relocations, not REL.  */
34
35
36 static void ppc_howto_init
37   PARAMS ((void));
38 static reloc_howto_type *ppc64_elf_reloc_type_lookup
39   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
40 static void ppc64_elf_info_to_howto
41   PARAMS ((bfd *abfd, arelent *cache_ptr, Elf64_Internal_Rela *dst));
42 static bfd_reloc_status_type ppc64_elf_addr16_ha_reloc
43   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
44 static boolean ppc64_elf_set_private_flags
45   PARAMS ((bfd *, flagword));
46 static boolean ppc64_elf_merge_private_bfd_data
47   PARAMS ((bfd *, bfd *));
48 static boolean ppc64_elf_section_from_shdr
49   PARAMS ((bfd *, Elf64_Internal_Shdr *, char *));
50 static struct bfd_hash_entry *link_hash_newfunc
51   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
52 static struct bfd_link_hash_table *ppc64_elf_link_hash_table_create
53   PARAMS ((bfd *));
54 static boolean create_got_section
55   PARAMS ((bfd *, struct bfd_link_info *));
56 static boolean ppc64_elf_create_dynamic_sections
57   PARAMS ((bfd *, struct bfd_link_info *));
58 static void ppc64_elf_copy_indirect_symbol
59   PARAMS ((struct elf_link_hash_entry *, struct elf_link_hash_entry *));
60 static boolean ppc64_elf_check_relocs
61   PARAMS ((bfd *, struct bfd_link_info *, asection *,
62            const Elf_Internal_Rela *));
63 static asection * ppc64_elf_gc_mark_hook
64   PARAMS ((bfd *abfd, struct bfd_link_info *info, Elf_Internal_Rela *rel,
65            struct elf_link_hash_entry *h, Elf_Internal_Sym *sym));
66 static boolean ppc64_elf_gc_sweep_hook
67   PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
68            const Elf_Internal_Rela *relocs));
69 static boolean func_desc_adjust
70   PARAMS ((struct elf_link_hash_entry *, PTR));
71 static boolean ppc64_elf_func_desc_adjust
72   PARAMS ((bfd *, struct bfd_link_info *));
73 static boolean ppc64_elf_adjust_dynamic_symbol
74   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
75 static void ppc64_elf_hide_symbol
76   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, boolean));
77 static boolean allocate_dynrelocs
78   PARAMS ((struct elf_link_hash_entry *, PTR));
79 static boolean readonly_dynrelocs
80   PARAMS ((struct elf_link_hash_entry *, PTR));
81 static enum elf_reloc_type_class ppc64_elf_reloc_type_class
82   PARAMS ((const Elf_Internal_Rela *));
83 static boolean ppc64_elf_size_dynamic_sections
84   PARAMS ((bfd *, struct bfd_link_info *));
85 static bfd_byte *build_plt_stub
86   PARAMS ((bfd *, bfd_byte *, int, int));
87 static boolean build_one_stub
88   PARAMS ((struct elf_link_hash_entry *, PTR));
89 static boolean ppc64_elf_fake_sections
90   PARAMS ((bfd *, Elf64_Internal_Shdr *, asection *));
91 static boolean ppc64_elf_relocate_section
92   PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
93            Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
94            asection **));
95 static boolean ppc64_elf_finish_dynamic_symbol
96   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
97            Elf_Internal_Sym *));
98 static boolean ppc64_elf_finish_dynamic_sections
99   PARAMS ((bfd *, struct bfd_link_info *));
100
101
102 /* Mask to set RA in memory instructions.  */
103 #define RA_REGISTER_MASK 0x001f0000
104
105 /* Value to shift register by to insert RA.  */
106 #define RA_REGISTER_SHIFT 16
107
108 /* The name of the dynamic interpreter.  This is put in the .interp
109    section.  */
110 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
111
112 /* The size in bytes of an entry in the procedure linkage table.  */
113 #define PLT_ENTRY_SIZE 24
114
115 /* The initial size of the plt reserved for the dynamic linker.  */
116 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
117
118 /* TOC base pointers offset from start of TOC.  */
119 #define TOC_BASE_OFF (0x8000)
120
121 /* .plt call stub instructions.  */
122 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha     */
123 #define STD_R2_40R1     0xf8410028      /* std   %r2,40(%r1)         */
124 #define LD_R11_0R12     0xe96c0000      /* ld    %r11,xxx+0@l(%r12)  */
125 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,xxx+8@l(%r12)   */
126 #define MTCTR_R11       0x7d6903a6      /* mtctr %r11                */
127                                         /* ld    %r11,xxx+16@l(%r12) */
128 #define BCTR            0x4e800420      /* bctr                      */
129
130 /* The normal stub is this size.  */
131 #define PLT_CALL_STUB_SIZE (7*4)
132
133 /* But sometimes the .plt entry crosses a 64k boundary, and we need
134    to adjust the high word with this insn.  */
135 #define ADDIS_R12_R12_1 0x3d8c0001      /* addis %r12,%r12,1    */
136
137 /* The .glink fixup call stub is the same as the .plt call stub, but
138    the first instruction restores r2, and the std is omitted.  */
139 #define LD_R2_40R1      0xe8410028      /* ld    %r2,40(%r1)    */
140
141 /* Always allow this much space.  */
142 #define GLINK_CALL_STUB_SIZE (8*4)
143
144 /* Pad with this.  */
145 #define NOP             0x60000000
146
147 /* .glink entries for the first 32k functions are two instructions. */
148 #define LI_R0_0         0x38000000      /* li    %r0,0          */
149 #define B_DOT           0x48000000      /* b     .              */
150
151 /* After that, we need two instructions to load the index, followed by
152    a branch.  */
153 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
154 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
155 \f
156 /* Relocation HOWTO's.  */
157 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC_max];
158
159 static reloc_howto_type ppc64_elf_howto_raw[] = {
160   /* This reloc does nothing.  */
161   HOWTO (R_PPC64_NONE,          /* type */
162          0,                     /* rightshift */
163          2,                     /* size (0 = byte, 1 = short, 2 = long) */
164          32,                    /* bitsize */
165          false,                 /* pc_relative */
166          0,                     /* bitpos */
167          complain_overflow_bitfield, /* complain_on_overflow */
168          bfd_elf_generic_reloc, /* special_function */
169          "R_PPC64_NONE",        /* name */
170          false,                 /* partial_inplace */
171          0,                     /* src_mask */
172          0,                     /* dst_mask */
173          false),                /* pcrel_offset */
174
175   /* A standard 32 bit relocation.  */
176   HOWTO (R_PPC64_ADDR32,        /* type */
177          0,                     /* rightshift */
178          2,                     /* size (0 = byte, 1 = short, 2 = long) */
179          32,                    /* bitsize */
180          false,                 /* pc_relative */
181          0,                     /* bitpos */
182          complain_overflow_bitfield, /* complain_on_overflow */
183          bfd_elf_generic_reloc, /* special_function */
184          "R_PPC64_ADDR32",      /* name */
185          false,                 /* partial_inplace */
186          0,                     /* src_mask */
187          0xffffffff,            /* dst_mask */
188          false),                /* pcrel_offset */
189
190   /* An absolute 26 bit branch; the lower two bits must be zero.
191      FIXME: we don't check that, we just clear them.  */
192   HOWTO (R_PPC64_ADDR24,        /* type */
193          0,                     /* rightshift */
194          2,                     /* size (0 = byte, 1 = short, 2 = long) */
195          26,                    /* bitsize */
196          false,                 /* pc_relative */
197          0,                     /* bitpos */
198          complain_overflow_bitfield, /* complain_on_overflow */
199          bfd_elf_generic_reloc, /* special_function */
200          "R_PPC64_ADDR24",      /* name */
201          false,                 /* partial_inplace */
202          0,                     /* src_mask */
203          0x3fffffc,             /* dst_mask */
204          false),                /* pcrel_offset */
205
206   /* A standard 16 bit relocation.  */
207   HOWTO (R_PPC64_ADDR16,        /* type */
208          0,                     /* rightshift */
209          1,                     /* size (0 = byte, 1 = short, 2 = long) */
210          16,                    /* bitsize */
211          false,                 /* pc_relative */
212          0,                     /* bitpos */
213          complain_overflow_bitfield, /* complain_on_overflow */
214          bfd_elf_generic_reloc, /* special_function */
215          "R_PPC64_ADDR16",      /* name */
216          false,                 /* partial_inplace */
217          0,                     /* src_mask */
218          0xffff,                /* dst_mask */
219          false),                /* pcrel_offset */
220
221   /* A 16 bit relocation without overflow.  */
222   HOWTO (R_PPC64_ADDR16_LO,     /* type */
223          0,                     /* rightshift */
224          1,                     /* size (0 = byte, 1 = short, 2 = long) */
225          16,                    /* bitsize */
226          false,                 /* pc_relative */
227          0,                     /* bitpos */
228          complain_overflow_dont,/* complain_on_overflow */
229          bfd_elf_generic_reloc, /* special_function */
230          "R_PPC64_ADDR16_LO",   /* name */
231          false,                 /* partial_inplace */
232          0,                     /* src_mask */
233          0xffff,                /* dst_mask */
234          false),                /* pcrel_offset */
235
236   /* Bits 16-31 of an address.  */
237   HOWTO (R_PPC64_ADDR16_HI,     /* type */
238          16,                    /* rightshift */
239          1,                     /* size (0 = byte, 1 = short, 2 = long) */
240          16,                    /* bitsize */
241          false,                 /* pc_relative */
242          0,                     /* bitpos */
243          complain_overflow_dont, /* complain_on_overflow */
244          bfd_elf_generic_reloc, /* special_function */
245          "R_PPC64_ADDR16_HI",   /* name */
246          false,                 /* partial_inplace */
247          0,                     /* src_mask */
248          0xffff,                /* dst_mask */
249          false),                /* pcrel_offset */
250
251   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
252      bits, treated as a signed number, is negative.  */
253   HOWTO (R_PPC64_ADDR16_HA,     /* type */
254          16,                    /* rightshift */
255          1,                     /* size (0 = byte, 1 = short, 2 = long) */
256          16,                    /* bitsize */
257          false,                 /* pc_relative */
258          0,                     /* bitpos */
259          complain_overflow_dont, /* complain_on_overflow */
260          ppc64_elf_addr16_ha_reloc, /* special_function */
261          "R_PPC64_ADDR16_HA",   /* name */
262          false,                 /* partial_inplace */
263          0,                     /* src_mask */
264          0xffff,                /* dst_mask */
265          false),                /* pcrel_offset */
266
267   /* An absolute 16 bit branch; the lower two bits must be zero.
268      FIXME: we don't check that, we just clear them.  */
269   HOWTO (R_PPC64_ADDR14,        /* type */
270          0,                     /* rightshift */
271          2,                     /* size (0 = byte, 1 = short, 2 = long) */
272          16,                    /* bitsize */
273          false,                 /* pc_relative */
274          0,                     /* bitpos */
275          complain_overflow_bitfield, /* complain_on_overflow */
276          bfd_elf_generic_reloc, /* special_function */
277          "R_PPC64_ADDR14",      /* name */
278          false,                 /* partial_inplace */
279          0,                     /* src_mask */
280          0xfffc,                /* dst_mask */
281          false),                /* pcrel_offset */
282
283   /* An absolute 16 bit branch, for which bit 10 should be set to
284      indicate that the branch is expected to be taken.  The lower two
285      bits must be zero.  */
286   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
287          0,                     /* rightshift */
288          2,                     /* size (0 = byte, 1 = short, 2 = long) */
289          16,                    /* bitsize */
290          false,                 /* pc_relative */
291          0,                     /* bitpos */
292          complain_overflow_bitfield, /* complain_on_overflow */
293          bfd_elf_generic_reloc, /* special_function */
294          "R_PPC64_ADDR14_BRTAKEN",/* name */
295          false,                 /* partial_inplace */
296          0,                     /* src_mask */
297          0xfffc,                /* dst_mask */
298          false),                /* pcrel_offset */
299
300   /* An absolute 16 bit branch, for which bit 10 should be set to
301      indicate that the branch is not expected to be taken.  The lower
302      two bits must be zero.  */
303   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
304          0,                     /* rightshift */
305          2,                     /* size (0 = byte, 1 = short, 2 = long) */
306          16,                    /* bitsize */
307          false,                 /* pc_relative */
308          0,                     /* bitpos */
309          complain_overflow_bitfield, /* complain_on_overflow */
310          bfd_elf_generic_reloc, /* special_function */
311          "R_PPC64_ADDR14_BRNTAKEN",/* name */
312          false,                 /* partial_inplace */
313          0,                     /* src_mask */
314          0xfffc,                /* dst_mask */
315          false),                /* pcrel_offset */
316
317   /* A relative 26 bit branch; the lower two bits must be zero.  */
318   HOWTO (R_PPC64_REL24,         /* type */
319          0,                     /* rightshift */
320          2,                     /* size (0 = byte, 1 = short, 2 = long) */
321          26,                    /* bitsize */
322          true,                  /* pc_relative */
323          0,                     /* bitpos */
324          complain_overflow_signed, /* complain_on_overflow */
325          bfd_elf_generic_reloc, /* special_function */
326          "R_PPC64_REL24",       /* name */
327          false,                 /* partial_inplace */
328          0,                     /* src_mask */
329          0x3fffffc,             /* dst_mask */
330          true),                 /* pcrel_offset */
331
332   /* A relative 16 bit branch; the lower two bits must be zero.  */
333   HOWTO (R_PPC64_REL14,         /* type */
334          0,                     /* rightshift */
335          2,                     /* size (0 = byte, 1 = short, 2 = long) */
336          16,                    /* bitsize */
337          true,                  /* pc_relative */
338          0,                     /* bitpos */
339          complain_overflow_signed, /* complain_on_overflow */
340          bfd_elf_generic_reloc, /* special_function */
341          "R_PPC64_REL14",       /* name */
342          false,                 /* partial_inplace */
343          0,                     /* src_mask */
344          0xfffc,                /* dst_mask */
345          true),                 /* pcrel_offset */
346
347   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
348      the branch is expected to be taken.  The lower two bits must be
349      zero.  */
350   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
351          0,                     /* rightshift */
352          2,                     /* size (0 = byte, 1 = short, 2 = long) */
353          16,                    /* bitsize */
354          true,                  /* pc_relative */
355          0,                     /* bitpos */
356          complain_overflow_signed, /* complain_on_overflow */
357          bfd_elf_generic_reloc, /* special_function */
358          "R_PPC64_REL14_BRTAKEN", /* name */
359          false,                 /* partial_inplace */
360          0,                     /* src_mask */
361          0xfffc,                /* dst_mask */
362          true),                 /* pcrel_offset */
363
364   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
365      the branch is not expected to be taken.  The lower two bits must
366      be zero.  */
367   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
368          0,                     /* rightshift */
369          2,                     /* size (0 = byte, 1 = short, 2 = long) */
370          16,                    /* bitsize */
371          true,                  /* pc_relative */
372          0,                     /* bitpos */
373          complain_overflow_signed, /* complain_on_overflow */
374          bfd_elf_generic_reloc, /* special_function */
375          "R_PPC64_REL14_BRNTAKEN",/* name */
376          false,                 /* partial_inplace */
377          0,                     /* src_mask */
378          0xfffc,                /* dst_mask */
379          true),                 /* pcrel_offset */
380
381   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
382      symbol.  */
383   HOWTO (R_PPC64_GOT16,         /* type */
384          0,                     /* rightshift */
385          1,                     /* size (0 = byte, 1 = short, 2 = long) */
386          16,                    /* bitsize */
387          false,                 /* pc_relative */
388          0,                     /* bitpos */
389          complain_overflow_signed, /* complain_on_overflow */
390          bfd_elf_generic_reloc, /* special_function */
391          "R_PPC64_GOT16",       /* name */
392          false,                 /* partial_inplace */
393          0,                     /* src_mask */
394          0xffff,                /* dst_mask */
395          false),                /* pcrel_offset */
396
397   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
398      the symbol.  */
399   HOWTO (R_PPC64_GOT16_LO,      /* type */
400          0,                     /* rightshift */
401          1,                     /* size (0 = byte, 1 = short, 2 = long) */
402          16,                    /* bitsize */
403          false,                 /* pc_relative */
404          0,                     /* bitpos */
405          complain_overflow_dont, /* complain_on_overflow */
406          bfd_elf_generic_reloc, /* special_function */
407          "R_PPC64_GOT16_LO",    /* name */
408          false,                 /* partial_inplace */
409          0,                     /* src_mask */
410          0xffff,                /* dst_mask */
411          false),                /* pcrel_offset */
412
413   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
414      the symbol.  */
415   HOWTO (R_PPC64_GOT16_HI,      /* type */
416          16,                    /* rightshift */
417          1,                     /* size (0 = byte, 1 = short, 2 = long) */
418          16,                    /* bitsize */
419          false,                 /* pc_relative */
420          0,                     /* bitpos */
421          complain_overflow_dont,/* complain_on_overflow */
422          bfd_elf_generic_reloc, /* special_function */
423          "R_PPC64_GOT16_HI",    /* name */
424          false,                 /* partial_inplace */
425          0,                     /* src_mask */
426          0xffff,                /* dst_mask */
427          false),                /* pcrel_offset */
428
429   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
430      the symbol.  */
431   HOWTO (R_PPC64_GOT16_HA,      /* type */
432          16,                    /* rightshift */
433          1,                     /* size (0 = byte, 1 = short, 2 = long) */
434          16,                    /* bitsize */
435          false,                 /* pc_relative */
436          0,                     /* bitpos */
437          complain_overflow_dont,/* complain_on_overflow */
438          ppc64_elf_addr16_ha_reloc, /* special_function */
439          "R_PPC64_GOT16_HA",    /* name */
440          false,                 /* partial_inplace */
441          0,                     /* src_mask */
442          0xffff,                /* dst_mask */
443          false),                /* pcrel_offset */
444
445   /* This is used only by the dynamic linker.  The symbol should exist
446      both in the object being run and in some shared library.  The
447      dynamic linker copies the data addressed by the symbol from the
448      shared library into the object, because the object being
449      run has to have the data at some particular address.  */
450   HOWTO (R_PPC64_COPY,          /* type */
451          0,                     /* rightshift */
452          2,                     /* size (0 = byte, 1 = short, 2 = long) */
453          32,                    /* bitsize */
454          false,                 /* pc_relative */
455          0,                     /* bitpos */
456          complain_overflow_bitfield, /* complain_on_overflow */
457          bfd_elf_generic_reloc,  /* special_function */
458          "R_PPC64_COPY",        /* name */
459          false,                 /* partial_inplace */
460          0,                     /* src_mask */
461          0,                     /* dst_mask */
462          false),                /* pcrel_offset */
463
464   /* Like R_PPC64_ADDR64, but used when setting global offset table
465      entries.  */
466   HOWTO (R_PPC64_GLOB_DAT,      /* type */
467          0,                     /* rightshift */
468          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
469          64,                    /* bitsize */
470          false,                 /* pc_relative */
471          0,                     /* bitpos */
472          complain_overflow_dont, /* complain_on_overflow */
473          bfd_elf_generic_reloc,  /* special_function */
474          "R_PPC64_GLOB_DAT",    /* name */
475          false,                 /* partial_inplace */
476          0,                     /* src_mask */
477          0xffffffffffffffff,    /* dst_mask */
478          false),                /* pcrel_offset */
479
480   /* Created by the link editor.  Marks a procedure linkage table
481      entry for a symbol.  */
482   HOWTO (R_PPC64_JMP_SLOT,      /* type */
483          0,                     /* rightshift */
484          0,                     /* size (0 = byte, 1 = short, 2 = long) */
485          0,                     /* bitsize */
486          false,                 /* pc_relative */
487          0,                     /* bitpos */
488          complain_overflow_dont, /* complain_on_overflow */
489          bfd_elf_generic_reloc, /* special_function */
490          "R_PPC64_JMP_SLOT",    /* name */
491          false,                 /* partial_inplace */
492          0,                     /* src_mask */
493          0,                     /* dst_mask */
494          false),                /* pcrel_offset */
495
496   /* Used only by the dynamic linker.  When the object is run, this
497      doubleword64 is set to the load address of the object, plus the
498      addend.  */
499   HOWTO (R_PPC64_RELATIVE,      /* type */
500          0,                     /* rightshift */
501          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
502          64,                    /* bitsize */
503          false,                 /* pc_relative */
504          0,                     /* bitpos */
505          complain_overflow_dont, /* complain_on_overflow */
506          bfd_elf_generic_reloc, /* special_function */
507          "R_PPC64_RELATIVE",    /* name */
508          false,                 /* partial_inplace */
509          0,                     /* src_mask */
510          0xffffffffffffffff,    /* dst_mask */
511          false),                /* pcrel_offset */
512
513   /* Like R_PPC64_ADDR32, but may be unaligned.  */
514   HOWTO (R_PPC64_UADDR32,       /* type */
515          0,                     /* rightshift */
516          2,                     /* size (0 = byte, 1 = short, 2 = long) */
517          32,                    /* bitsize */
518          false,                 /* pc_relative */
519          0,                     /* bitpos */
520          complain_overflow_bitfield, /* complain_on_overflow */
521          bfd_elf_generic_reloc, /* special_function */
522          "R_PPC64_UADDR32",     /* name */
523          false,                 /* partial_inplace */
524          0,                     /* src_mask */
525          0xffffffff,            /* dst_mask */
526          false),                /* pcrel_offset */
527
528   /* Like R_PPC64_ADDR16, but may be unaligned.  */
529   HOWTO (R_PPC64_UADDR16,       /* type */
530          0,                     /* rightshift */
531          1,                     /* size (0 = byte, 1 = short, 2 = long) */
532          16,                    /* bitsize */
533          false,                 /* pc_relative */
534          0,                     /* bitpos */
535          complain_overflow_bitfield, /* complain_on_overflow */
536          bfd_elf_generic_reloc, /* special_function */
537          "R_PPC64_UADDR16",     /* name */
538          false,                 /* partial_inplace */
539          0,                     /* src_mask */
540          0xffff,                /* dst_mask */
541          false),                /* pcrel_offset */
542
543   /* 32-bit PC relative.  */
544   HOWTO (R_PPC64_REL32,         /* type */
545          0,                     /* rightshift */
546          2,                     /* size (0 = byte, 1 = short, 2 = long) */
547          32,                    /* bitsize */
548          true,                  /* pc_relative */
549          0,                     /* bitpos */
550          /* FIXME: Verify.  Was complain_overflow_bitfield. */
551          complain_overflow_signed, /* complain_on_overflow */
552          bfd_elf_generic_reloc, /* special_function */
553          "R_PPC64_REL32",       /* name */
554          false,                 /* partial_inplace */
555          0,                     /* src_mask */
556          0xffffffff,            /* dst_mask */
557          true),                 /* pcrel_offset */
558
559   /* 32-bit relocation to the symbol's procedure linkage table.  */
560   HOWTO (R_PPC64_PLT32,         /* type */
561          0,                     /* rightshift */
562          2,                     /* size (0 = byte, 1 = short, 2 = long) */
563          32,                    /* bitsize */
564          false,                 /* pc_relative */
565          0,                     /* bitpos */
566          complain_overflow_bitfield, /* complain_on_overflow */
567          bfd_elf_generic_reloc, /* special_function */
568          "R_PPC64_PLT32",       /* name */
569          false,                 /* partial_inplace */
570          0,                     /* src_mask */
571          0,                     /* dst_mask */
572          false),                /* pcrel_offset */
573
574   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
575      FIXME: R_PPC64_PLTREL32 not supported.  */
576   HOWTO (R_PPC64_PLTREL32,      /* type */
577          0,                     /* rightshift */
578          2,                     /* size (0 = byte, 1 = short, 2 = long) */
579          32,                    /* bitsize */
580          true,                  /* pc_relative */
581          0,                     /* bitpos */
582          complain_overflow_signed, /* complain_on_overflow */
583          bfd_elf_generic_reloc, /* special_function */
584          "R_PPC64_PLTREL32",    /* name */
585          false,                 /* partial_inplace */
586          0,                     /* src_mask */
587          0,                     /* dst_mask */
588          true),                 /* pcrel_offset */
589
590   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
591      the symbol.  */
592   HOWTO (R_PPC64_PLT16_LO,      /* type */
593          0,                     /* rightshift */
594          1,                     /* size (0 = byte, 1 = short, 2 = long) */
595          16,                    /* bitsize */
596          false,                 /* pc_relative */
597          0,                     /* bitpos */
598          complain_overflow_dont, /* complain_on_overflow */
599          bfd_elf_generic_reloc, /* special_function */
600          "R_PPC64_PLT16_LO",    /* name */
601          false,                 /* partial_inplace */
602          0,                     /* src_mask */
603          0xffff,                /* dst_mask */
604          false),                /* pcrel_offset */
605
606   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
607      the symbol.  */
608   HOWTO (R_PPC64_PLT16_HI,      /* type */
609          16,                    /* rightshift */
610          1,                     /* size (0 = byte, 1 = short, 2 = long) */
611          16,                    /* bitsize */
612          false,                 /* pc_relative */
613          0,                     /* bitpos */
614          complain_overflow_dont, /* complain_on_overflow */
615          bfd_elf_generic_reloc, /* special_function */
616          "R_PPC64_PLT16_HI",    /* name */
617          false,                 /* partial_inplace */
618          0,                     /* src_mask */
619          0xffff,                /* dst_mask */
620          false),                /* pcrel_offset */
621
622   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
623      the symbol.  */
624   HOWTO (R_PPC64_PLT16_HA,      /* type */
625          16,                    /* rightshift */
626          1,                     /* size (0 = byte, 1 = short, 2 = long) */
627          16,                    /* bitsize */
628          false,                 /* pc_relative */
629          0,                     /* bitpos */
630          complain_overflow_dont, /* complain_on_overflow */
631          ppc64_elf_addr16_ha_reloc, /* special_function */
632          "R_PPC64_PLT16_HA",    /* name */
633          false,                 /* partial_inplace */
634          0,                     /* src_mask */
635          0xffff,                /* dst_mask */
636          false),                /* pcrel_offset */
637
638   /* 32-bit section relative relocation.  */
639   /* FIXME: Verify R_PPC64_SECTOFF.  Seems strange with size=2 and
640      dst_mask=0.  */
641   HOWTO (R_PPC64_SECTOFF,       /* type */
642          0,                     /* rightshift */
643          2,                     /* size (0 = byte, 1 = short, 2 = long) */
644          32,                    /* bitsize */
645          true,                  /* pc_relative */
646          0,                     /* bitpos */
647          complain_overflow_bitfield, /* complain_on_overflow */
648          bfd_elf_generic_reloc, /* special_function */
649          "R_PPC64_SECTOFF",     /* name */
650          false,                 /* partial_inplace */
651          0,                     /* src_mask */
652          0,                     /* dst_mask */
653          true),                 /* pcrel_offset */
654
655   /* 16-bit lower half section relative relocation.  */
656   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
657          0,                     /* rightshift */
658          1,                     /* size (0 = byte, 1 = short, 2 = long) */
659          16,                    /* bitsize */
660          false,                 /* pc_relative */
661          0,                     /* bitpos */
662          complain_overflow_dont, /* complain_on_overflow */
663          bfd_elf_generic_reloc, /* special_function */
664          "R_PPC64_SECTOFF_LO",  /* name */
665          false,                 /* partial_inplace */
666          0,                     /* src_mask */
667          0xffff,                /* dst_mask */
668          false),                /* pcrel_offset */
669
670   /* 16-bit upper half section relative relocation.  */
671   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
672          16,                    /* rightshift */
673          1,                     /* size (0 = byte, 1 = short, 2 = long) */
674          16,                    /* bitsize */
675          false,                 /* pc_relative */
676          0,                     /* bitpos */
677          complain_overflow_dont, /* complain_on_overflow */
678          bfd_elf_generic_reloc, /* special_function */
679          "R_PPC64_SECTOFF_HI",  /* name */
680          false,                 /* partial_inplace */
681          0,                     /* src_mask */
682          0xffff,                /* dst_mask */
683          false),                /* pcrel_offset */
684
685   /* 16-bit upper half adjusted section relative relocation.  */
686   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
687          16,                    /* rightshift */
688          1,                     /* size (0 = byte, 1 = short, 2 = long) */
689          16,                    /* bitsize */
690          false,                 /* pc_relative */
691          0,                     /* bitpos */
692          complain_overflow_dont, /* complain_on_overflow */
693          ppc64_elf_addr16_ha_reloc, /* special_function */
694          "R_PPC64_SECTOFF_HA",  /* name */
695          false,                 /* partial_inplace */
696          0,                     /* src_mask */
697          0xffff,                /* dst_mask */
698          false),                /* pcrel_offset */
699
700   /* Like R_PPC64_REL24 without touching the two least significant
701      bits.  */
702   /* FIXME: Verify R_PPC64_ADDR30.  */
703   HOWTO (R_PPC64_ADDR30,        /* type */
704          2,                     /* rightshift */
705          2,                     /* size (0 = byte, 1 = short, 2 = long) */
706          30,                    /* bitsize */
707          true,                  /* pc_relative */
708          0,                     /* bitpos */
709          complain_overflow_dont, /* complain_on_overflow */
710          bfd_elf_generic_reloc, /* special_function */
711          "R_PPC64_ADDR30",      /* name */
712          false,                 /* partial_inplace */
713          0,                     /* src_mask */
714          0xfffffffc,            /* dst_mask */
715          true),                 /* pcrel_offset */
716
717   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
718
719   /* A standard 64-bit relocation.  */
720   HOWTO (R_PPC64_ADDR64,        /* type */
721          0,                     /* rightshift */
722          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
723          64,                    /* bitsize */
724          false,                 /* pc_relative */
725          0,                     /* bitpos */
726          complain_overflow_dont, /* complain_on_overflow */
727          bfd_elf_generic_reloc, /* special_function */
728          "R_PPC64_ADDR64",      /* name */
729          false,                 /* partial_inplace */
730          0,                     /* src_mask */
731          0xffffffffffffffff,    /* dst_mask */
732          false),                /* pcrel_offset */
733
734   /* The bits 32-47 of an address.  */
735   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
736          32,                    /* rightshift */
737          1,                     /* size (0 = byte, 1 = short, 2 = long) */
738          16,                    /* bitsize */
739          false,                 /* pc_relative */
740          0,                     /* bitpos */
741          complain_overflow_dont, /* complain_on_overflow */
742          bfd_elf_generic_reloc, /* special_function */
743          "R_PPC64_ADDR16_HIGHER", /* name */
744          false,                 /* partial_inplace */
745          0,                     /* src_mask */
746          0xffff,                /* dst_mask */
747          false),                /* pcrel_offset */
748
749   /* The bits 32-47 of an address, plus 1 if the contents of the low
750      16 bits, treated as a signed number, is negative.  */
751   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
752          32,                    /* rightshift */
753          1,                     /* size (0 = byte, 1 = short, 2 = long) */
754          16,                    /* bitsize */
755          false,                 /* pc_relative */
756          0,                     /* bitpos */
757          complain_overflow_dont, /* complain_on_overflow */
758          ppc64_elf_addr16_ha_reloc, /* special_function */
759          "R_PPC64_ADDR16_HIGHERA", /* name */
760          false,                 /* partial_inplace */
761          0,                     /* src_mask */
762          0xffff,                /* dst_mask */
763          false),                /* pcrel_offset */
764
765   /* The bits 48-63 of an address.  */
766   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
767          48,                    /* rightshift */
768          1,                     /* size (0 = byte, 1 = short, 2 = long) */
769          16,                    /* bitsize */
770          false,                 /* pc_relative */
771          0,                     /* bitpos */
772          complain_overflow_dont, /* complain_on_overflow */
773          bfd_elf_generic_reloc, /* special_function */
774          "R_PPC64_ADDR16_HIGHEST", /* name */
775          false,                 /* partial_inplace */
776          0,                     /* src_mask */
777          0xffff,                /* dst_mask */
778          false),                /* pcrel_offset */
779
780   /* The bits 48-63 of an address, plus 1 if the contents of the low
781      16 bits, treated as a signed number, is negative.  */
782   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
783          48,                    /* rightshift */
784          1,                     /* size (0 = byte, 1 = short, 2 = long) */
785          16,                    /* bitsize */
786          false,                 /* pc_relative */
787          0,                     /* bitpos */
788          complain_overflow_dont, /* complain_on_overflow */
789          ppc64_elf_addr16_ha_reloc, /* special_function */
790          "R_PPC64_ADDR16_HIGHESTA", /* name */
791          false,                 /* partial_inplace */
792          0,                     /* src_mask */
793          0xffff,                /* dst_mask */
794          false),                /* pcrel_offset */
795
796   /* Like ADDR64, but may be unaligned.  */
797   HOWTO (R_PPC64_UADDR64,       /* type */
798          0,                     /* rightshift */
799          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
800          64,                    /* bitsize */
801          false,                 /* pc_relative */
802          0,                     /* bitpos */
803          complain_overflow_dont, /* complain_on_overflow */
804          bfd_elf_generic_reloc, /* special_function */
805          "R_PPC64_UADDR64",     /* name */
806          false,                 /* partial_inplace */
807          0,                     /* src_mask */
808          0xffffffffffffffff,    /* dst_mask */
809          false),                /* pcrel_offset */
810
811   /* 64-bit relative relocation.  */
812   HOWTO (R_PPC64_REL64,         /* type */
813          0,                     /* rightshift */
814          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
815          64,                    /* bitsize */
816          true,                  /* pc_relative */
817          0,                     /* bitpos */
818          complain_overflow_dont, /* complain_on_overflow */
819          bfd_elf_generic_reloc, /* special_function */
820          "R_PPC64_REL64",       /* name */
821          false,                 /* partial_inplace */
822          0,                     /* src_mask */
823          0xffffffffffffffff,    /* dst_mask */
824          true),                 /* pcrel_offset */
825
826   /* 64-bit relocation to the symbol's procedure linkage table. */
827   HOWTO (R_PPC64_PLT64,         /* type */
828          0,                     /* rightshift */
829          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
830          64,                    /* bitsize */
831          false,                 /* pc_relative */
832          0,                     /* bitpos */
833          complain_overflow_dont, /* complain_on_overflow */
834          bfd_elf_generic_reloc, /* special_function */
835          "R_PPC64_PLT64",       /* name */
836          false,                 /* partial_inplace */
837          0,                     /* src_mask */
838          0,                     /* dst_mask */
839          false),                /* pcrel_offset */
840
841   /* 64-bit PC relative relocation to the symbol's procedure linkage
842      table.  */
843   /* FIXME: R_PPC64_PLTREL64 not supported.  */
844   HOWTO (R_PPC64_PLTREL64,      /* type */
845          0,                     /* rightshift */
846          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
847          64,                    /* bitsize */
848          true,                  /* pc_relative */
849          0,                     /* bitpos */
850          complain_overflow_dont, /* complain_on_overflow */
851          bfd_elf_generic_reloc, /* special_function */
852          "R_PPC64_PLTREL64",    /* name */
853          false,                 /* partial_inplace */
854          0,                     /* src_mask */
855          0,                     /* dst_mask */
856          true),                 /* pcrel_offset */
857
858   /* 16 bit TOC-relative relocation.  */
859
860   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
861   HOWTO (R_PPC64_TOC16,         /* type */
862          0,                     /* rightshift */
863          1,                     /* size (0 = byte, 1 = short, 2 = long) */
864          16,                    /* bitsize */
865          false,                 /* pc_relative */
866          0,                     /* bitpos */
867          complain_overflow_signed, /* complain_on_overflow */
868          bfd_elf_generic_reloc, /* special_function */
869          "R_PPC64_TOC16",       /* name */
870          false,                 /* partial_inplace */
871          0,                     /* src_mask */
872          0xffff,                /* dst_mask */
873          false),                /* pcrel_offset */
874
875   /* 16 bit TOC-relative relocation without overflow.  */
876
877   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
878   HOWTO (R_PPC64_TOC16_LO,      /* type */
879          0,                     /* rightshift */
880          1,                     /* size (0 = byte, 1 = short, 2 = long) */
881          16,                    /* bitsize */
882          false,                 /* pc_relative */
883          0,                     /* bitpos */
884          complain_overflow_dont, /* complain_on_overflow */
885          bfd_elf_generic_reloc, /* special_function */
886          "R_PPC64_TOC16_LO",    /* name */
887          false,                 /* partial_inplace */
888          0,                     /* src_mask */
889          0xffff,                /* dst_mask */
890          false),                /* pcrel_offset */
891
892   /* 16 bit TOC-relative relocation, high 16 bits.  */
893
894   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
895   HOWTO (R_PPC64_TOC16_HI,      /* type */
896          16,                    /* rightshift */
897          1,                     /* size (0 = byte, 1 = short, 2 = long) */
898          16,                    /* bitsize */
899          false,                 /* pc_relative */
900          0,                     /* bitpos */
901          complain_overflow_dont, /* complain_on_overflow */
902          bfd_elf_generic_reloc, /* special_function */
903          "R_PPC64_TOC16_HI",    /* name */
904          false,                 /* partial_inplace */
905          0,                     /* src_mask */
906          0xffff,                /* dst_mask */
907          false),                /* pcrel_offset */
908
909   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
910      contents of the low 16 bits, treated as a signed number, is
911      negative.  */
912
913   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
914   HOWTO (R_PPC64_TOC16_HA,      /* type */
915          16,                    /* rightshift */
916          1,                     /* size (0 = byte, 1 = short, 2 = long) */
917          16,                    /* bitsize */
918          false,                 /* pc_relative */
919          0,                     /* bitpos */
920          complain_overflow_dont, /* complain_on_overflow */
921          ppc64_elf_addr16_ha_reloc, /* special_function */
922          "R_PPC64_TOC16_HA",    /* name */
923          false,                 /* partial_inplace */
924          0,                     /* src_mask */
925          0xffff,                /* dst_mask */
926          false),                /* pcrel_offset */
927
928   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
929
930   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
931   HOWTO (R_PPC64_TOC,           /* type */
932          0,                     /* rightshift */
933          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
934          64,                    /* bitsize */
935          false,                 /* pc_relative */
936          0,                     /* bitpos */
937          complain_overflow_bitfield, /* complain_on_overflow */
938          bfd_elf_generic_reloc, /* special_function */
939          "R_PPC64_TOC",         /* name */
940          false,                 /* partial_inplace */
941          0,                     /* src_mask */
942          0xffffffffffffffff,    /* dst_mask */
943          false),                /* pcrel_offset */
944
945   /* Like R_PPC64_GOT16, but also informs the link editor that the
946      value to relocate may (!) refer to a PLT entry which the link
947      editor (a) may replace with the symbol value.  If the link editor
948      is unable to fully resolve the symbol, it may (b) create a PLT
949      entry and store the address to the new PLT entry in the GOT.
950      This permits lazy resolution of function symbols at run time.
951      The link editor may also skip all of this and just (c) emit a
952      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
953   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
954     HOWTO (R_PPC64_PLTGOT16,    /* type */
955          0,                     /* rightshift */
956          1,                     /* size (0 = byte, 1 = short, 2 = long) */
957          16,                    /* bitsize */
958          false,                 /* pc_relative */
959          0,                     /* bitpos */
960          complain_overflow_signed, /* complain_on_overflow */
961          bfd_elf_generic_reloc, /* special_function */
962          "R_PPC64_PLTGOT16",    /* name */
963          false,                 /* partial_inplace */
964          0,                     /* src_mask */
965          0xffff,                /* dst_mask */
966          false),                /* pcrel_offset */
967
968   /* Like R_PPC64_PLTGOT16, but without overflow.  */
969   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
970   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
971          0,                     /* rightshift */
972          1,                     /* size (0 = byte, 1 = short, 2 = long) */
973          16,                    /* bitsize */
974          false,                 /* pc_relative */
975          0,                     /* bitpos */
976          complain_overflow_dont, /* complain_on_overflow */
977          bfd_elf_generic_reloc, /* special_function */
978          "R_PPC64_PLTGOT16_LO", /* name */
979          false,                 /* partial_inplace */
980          0,                     /* src_mask */
981          0xffff,                /* dst_mask */
982          false),                /* pcrel_offset */
983
984   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
985   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
986   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
987          16,                    /* rightshift */
988          1,                     /* size (0 = byte, 1 = short, 2 = long) */
989          16,                    /* bitsize */
990          false,                 /* pc_relative */
991          0,                     /* bitpos */
992          complain_overflow_dont, /* complain_on_overflow */
993          bfd_elf_generic_reloc, /* special_function */
994          "R_PPC64_PLTGOT16_HI", /* name */
995          false,                 /* partial_inplace */
996          0,                     /* src_mask */
997          0xffff,                /* dst_mask */
998          false),                /* pcrel_offset */
999
1000   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1001      1 if the contents of the low 16 bits, treated as a signed number,
1002      is negative.  */
1003   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1004   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1005          16,                    /* rightshift */
1006          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1007          16,                    /* bitsize */
1008          false,                 /* pc_relative */
1009          0,                     /* bitpos */
1010          complain_overflow_dont,/* complain_on_overflow */
1011          ppc64_elf_addr16_ha_reloc, /* special_function */
1012          "R_PPC64_PLTGOT16_HA", /* name */
1013          false,                 /* partial_inplace */
1014          0,                     /* src_mask */
1015          0xffff,                /* dst_mask */
1016          false),                /* pcrel_offset */
1017
1018   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1019   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1020          0,                     /* rightshift */
1021          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1022          16,                    /* bitsize */
1023          false,                 /* pc_relative */
1024          0,                     /* bitpos */
1025          complain_overflow_bitfield, /* complain_on_overflow */
1026          bfd_elf_generic_reloc, /* special_function */
1027          "R_PPC64_ADDR16_DS",   /* name */
1028          false,                 /* partial_inplace */
1029          0,                     /* src_mask */
1030          0xfffc,                /* dst_mask */
1031          false),                /* pcrel_offset */
1032
1033   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1034   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1035          0,                     /* rightshift */
1036          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1037          16,                    /* bitsize */
1038          false,                 /* pc_relative */
1039          0,                     /* bitpos */
1040          complain_overflow_dont,/* complain_on_overflow */
1041          bfd_elf_generic_reloc, /* special_function */
1042          "R_PPC64_ADDR16_LO_DS",/* name */
1043          false,                 /* partial_inplace */
1044          0,                     /* src_mask */
1045          0xfffc,                /* dst_mask */
1046          false),                /* pcrel_offset */
1047
1048   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1049   HOWTO (R_PPC64_GOT16_DS,      /* type */
1050          0,                     /* rightshift */
1051          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1052          16,                    /* bitsize */
1053          false,                 /* pc_relative */
1054          0,                     /* bitpos */
1055          complain_overflow_signed, /* complain_on_overflow */
1056          bfd_elf_generic_reloc, /* special_function */
1057          "R_PPC64_GOT16_DS",    /* name */
1058          false,                 /* partial_inplace */
1059          0,                     /* src_mask */
1060          0xfffc,                /* dst_mask */
1061          false),                /* pcrel_offset */
1062
1063   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1064   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1065          0,                     /* rightshift */
1066          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1067          16,                    /* bitsize */
1068          false,                 /* pc_relative */
1069          0,                     /* bitpos */
1070          complain_overflow_dont, /* complain_on_overflow */
1071          bfd_elf_generic_reloc, /* special_function */
1072          "R_PPC64_GOT16_LO_DS", /* name */
1073          false,                 /* partial_inplace */
1074          0,                     /* src_mask */
1075          0xfffc,                /* dst_mask */
1076          false),                /* pcrel_offset */
1077
1078   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1079   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1080          0,                     /* rightshift */
1081          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1082          16,                    /* bitsize */
1083          false,                 /* pc_relative */
1084          0,                     /* bitpos */
1085          complain_overflow_dont, /* complain_on_overflow */
1086          bfd_elf_generic_reloc, /* special_function */
1087          "R_PPC64_PLT16_LO_DS", /* name */
1088          false,                 /* partial_inplace */
1089          0,                     /* src_mask */
1090          0xfffc,                /* dst_mask */
1091          false),                /* pcrel_offset */
1092
1093   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1094   /* FIXME: Verify R_PPC64_SECTOFF.  Seems strange with size=2 and
1095      dst_mask=0.  */
1096   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1097          0,                     /* rightshift */
1098          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1099          32,                    /* bitsize */
1100          true,                  /* pc_relative */
1101          0,                     /* bitpos */
1102          complain_overflow_bitfield, /* complain_on_overflow */
1103          bfd_elf_generic_reloc, /* special_function */
1104          "R_PPC64_SECTOFF_DS",  /* name */
1105          false,                 /* partial_inplace */
1106          0,                     /* src_mask */
1107          0,                     /* dst_mask */
1108          true),                 /* pcrel_offset */
1109
1110   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1111   HOWTO (R_PPC64_SECTOFF_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          bfd_elf_generic_reloc, /* special_function */
1119          "R_PPC64_SECTOFF_LO_DS",/* name */
1120          false,                 /* partial_inplace */
1121          0,                     /* src_mask */
1122          0xfffc,                /* dst_mask */
1123          false),                /* pcrel_offset */
1124
1125   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1126   HOWTO (R_PPC64_TOC16_DS,      /* type */
1127          0,                     /* rightshift */
1128          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1129          16,                    /* bitsize */
1130          false,                 /* pc_relative */
1131          0,                     /* bitpos */
1132          complain_overflow_signed, /* complain_on_overflow */
1133          bfd_elf_generic_reloc, /* special_function */
1134          "R_PPC64_TOC16_DS",    /* name */
1135          false,                 /* partial_inplace */
1136          0,                     /* src_mask */
1137          0xfffc,                /* dst_mask */
1138          false),                /* pcrel_offset */
1139
1140   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1141   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1142          0,                     /* rightshift */
1143          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1144          16,                    /* bitsize */
1145          false,                 /* pc_relative */
1146          0,                     /* bitpos */
1147          complain_overflow_dont, /* complain_on_overflow */
1148          bfd_elf_generic_reloc, /* special_function */
1149          "R_PPC64_TOC16_LO_DS", /* name */
1150          false,                 /* partial_inplace */
1151          0,                     /* src_mask */
1152          0xfffc,                /* dst_mask */
1153          false),                /* pcrel_offset */
1154
1155   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1156   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1157     HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1158          0,                     /* rightshift */
1159          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1160          16,                    /* bitsize */
1161          false,                 /* pc_relative */
1162          0,                     /* bitpos */
1163          complain_overflow_signed, /* complain_on_overflow */
1164          bfd_elf_generic_reloc, /* special_function */
1165          "R_PPC64_PLTGOT16_DS", /* name */
1166          false,                 /* partial_inplace */
1167          0,                     /* src_mask */
1168          0xfffc,                /* dst_mask */
1169          false),                /* pcrel_offset */
1170
1171   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1172   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1173   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1174          0,                     /* rightshift */
1175          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1176          16,                    /* bitsize */
1177          false,                 /* pc_relative */
1178          0,                     /* bitpos */
1179          complain_overflow_dont, /* complain_on_overflow */
1180          bfd_elf_generic_reloc, /* special_function */
1181          "R_PPC64_PLTGOT16_LO_DS",/* name */
1182          false,                 /* partial_inplace */
1183          0,                     /* src_mask */
1184          0xfffc,                /* dst_mask */
1185          false),                /* pcrel_offset */
1186
1187   /* GNU extension to record C++ vtable hierarchy.  */
1188   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1189          0,                     /* rightshift */
1190          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1191          0,                     /* bitsize */
1192          false,                 /* pc_relative */
1193          0,                     /* bitpos */
1194          complain_overflow_dont, /* complain_on_overflow */
1195          NULL,                  /* special_function */
1196          "R_PPC64_GNU_VTINHERIT", /* name */
1197          false,                 /* partial_inplace */
1198          0,                     /* src_mask */
1199          0,                     /* dst_mask */
1200          false),                /* pcrel_offset */
1201
1202   /* GNU extension to record C++ vtable member usage.  */
1203   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
1204          0,                     /* rightshift */
1205          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1206          0,                     /* bitsize */
1207          false,                 /* pc_relative */
1208          0,                     /* bitpos */
1209          complain_overflow_dont, /* complain_on_overflow */
1210          NULL,                  /* special_function */
1211          "R_PPC64_GNU_VTENTRY", /* name */
1212          false,                 /* partial_inplace */
1213          0,                     /* src_mask */
1214          0,                     /* dst_mask */
1215          false),                /* pcrel_offset */
1216 };
1217
1218 \f
1219 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1220    be done.  */
1221
1222 static void
1223 ppc_howto_init ()
1224 {
1225   unsigned int i, type;
1226
1227   for (i = 0;
1228        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1229        i++)
1230     {
1231       type = ppc64_elf_howto_raw[i].type;
1232       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1233                           / sizeof (ppc64_elf_howto_table[0])));
1234       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1235     }
1236 }
1237
1238 static reloc_howto_type *
1239 ppc64_elf_reloc_type_lookup (abfd, code)
1240      bfd *abfd ATTRIBUTE_UNUSED;
1241      bfd_reloc_code_real_type code;
1242 {
1243   enum elf_ppc_reloc_type ppc_reloc = R_PPC_NONE;
1244
1245   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1246     /* Initialize howto table if needed.  */
1247     ppc_howto_init ();
1248
1249   switch ((int) code)
1250     {
1251     default:
1252       return (reloc_howto_type *) NULL;
1253
1254     case BFD_RELOC_NONE:                 ppc_reloc = R_PPC64_NONE;
1255       break;
1256     case BFD_RELOC_32:                   ppc_reloc = R_PPC64_ADDR32;
1257       break;
1258     case BFD_RELOC_PPC_BA26:             ppc_reloc = R_PPC64_ADDR24;
1259       break;
1260     case BFD_RELOC_16:                   ppc_reloc = R_PPC64_ADDR16;
1261       break;
1262     case BFD_RELOC_LO16:                 ppc_reloc = R_PPC64_ADDR16_LO;
1263       break;
1264     case BFD_RELOC_HI16:                 ppc_reloc = R_PPC64_ADDR16_HI;
1265       break;
1266     case BFD_RELOC_HI16_S:               ppc_reloc = R_PPC64_ADDR16_HA;
1267       break;
1268     case BFD_RELOC_PPC_BA16:             ppc_reloc = R_PPC64_ADDR14;
1269       break;
1270     case BFD_RELOC_PPC_BA16_BRTAKEN:     ppc_reloc = R_PPC64_ADDR14_BRTAKEN;
1271       break;
1272     case BFD_RELOC_PPC_BA16_BRNTAKEN:    ppc_reloc = R_PPC64_ADDR14_BRNTAKEN;
1273       break;
1274     case BFD_RELOC_PPC_B26:              ppc_reloc = R_PPC64_REL24;
1275       break;
1276     case BFD_RELOC_PPC_B16:              ppc_reloc = R_PPC64_REL14;
1277       break;
1278     case BFD_RELOC_PPC_B16_BRTAKEN:      ppc_reloc = R_PPC64_REL14_BRTAKEN;
1279       break;
1280     case BFD_RELOC_PPC_B16_BRNTAKEN:     ppc_reloc = R_PPC64_REL14_BRNTAKEN;
1281       break;
1282     case BFD_RELOC_16_GOTOFF:            ppc_reloc = R_PPC64_GOT16;
1283       break;
1284     case BFD_RELOC_LO16_GOTOFF:          ppc_reloc = R_PPC64_GOT16_LO;
1285       break;
1286     case BFD_RELOC_HI16_GOTOFF:          ppc_reloc = R_PPC64_GOT16_HI;
1287       break;
1288     case BFD_RELOC_HI16_S_GOTOFF:        ppc_reloc = R_PPC64_GOT16_HA;
1289       break;
1290     case BFD_RELOC_PPC_COPY:             ppc_reloc = R_PPC64_COPY;
1291       break;
1292     case BFD_RELOC_PPC_GLOB_DAT:         ppc_reloc = R_PPC64_GLOB_DAT;
1293       break;
1294     case BFD_RELOC_32_PCREL:             ppc_reloc = R_PPC64_REL32;
1295       break;
1296     case BFD_RELOC_32_PLTOFF:            ppc_reloc = R_PPC64_PLT32;
1297       break;
1298     case BFD_RELOC_32_PLT_PCREL:         ppc_reloc = R_PPC64_PLTREL32;
1299       break;
1300     case BFD_RELOC_LO16_PLTOFF:          ppc_reloc = R_PPC64_PLT16_LO;
1301       break;
1302     case BFD_RELOC_HI16_PLTOFF:          ppc_reloc = R_PPC64_PLT16_HI;
1303       break;
1304     case BFD_RELOC_HI16_S_PLTOFF:        ppc_reloc = R_PPC64_PLT16_HA;
1305       break;
1306     case BFD_RELOC_32_BASEREL:           ppc_reloc = R_PPC64_SECTOFF;
1307       break;
1308     case BFD_RELOC_LO16_BASEREL:         ppc_reloc = R_PPC64_SECTOFF_LO;
1309       break;
1310     case BFD_RELOC_HI16_BASEREL:         ppc_reloc = R_PPC64_SECTOFF_HI;
1311       break;
1312     case BFD_RELOC_HI16_S_BASEREL:       ppc_reloc = R_PPC64_SECTOFF_HA;
1313       break;
1314     case BFD_RELOC_CTOR:                 ppc_reloc = R_PPC64_ADDR64;
1315       break;
1316     case BFD_RELOC_64:                   ppc_reloc = R_PPC64_ADDR64;
1317       break;
1318     case BFD_RELOC_PPC64_HIGHER:         ppc_reloc = R_PPC64_ADDR16_HIGHER;
1319       break;
1320     case BFD_RELOC_PPC64_HIGHER_S:       ppc_reloc = R_PPC64_ADDR16_HIGHERA;
1321       break;
1322     case BFD_RELOC_PPC64_HIGHEST:        ppc_reloc = R_PPC64_ADDR16_HIGHEST;
1323       break;
1324     case BFD_RELOC_PPC64_HIGHEST_S:      ppc_reloc = R_PPC64_ADDR16_HIGHESTA;
1325       break;
1326     case BFD_RELOC_64_PCREL:             ppc_reloc = R_PPC64_REL64;
1327       break;
1328     case BFD_RELOC_64_PLTOFF:            ppc_reloc = R_PPC64_PLT64;
1329       break;
1330     case BFD_RELOC_64_PLT_PCREL:         ppc_reloc = R_PPC64_PLTREL64;
1331       break;
1332     case BFD_RELOC_PPC_TOC16:            ppc_reloc = R_PPC64_TOC16;
1333       break;
1334     case BFD_RELOC_PPC64_TOC16_LO:       ppc_reloc = R_PPC64_TOC16_LO;
1335       break;
1336     case BFD_RELOC_PPC64_TOC16_HI:       ppc_reloc = R_PPC64_TOC16_HI;
1337       break;
1338     case BFD_RELOC_PPC64_TOC16_HA:       ppc_reloc = R_PPC64_TOC16_HA;
1339       break;
1340     case BFD_RELOC_PPC64_TOC:            ppc_reloc = R_PPC64_TOC;
1341       break;
1342     case BFD_RELOC_PPC64_PLTGOT16:       ppc_reloc = R_PPC64_PLTGOT16;
1343       break;
1344     case BFD_RELOC_PPC64_PLTGOT16_LO:    ppc_reloc = R_PPC64_PLTGOT16_LO;
1345       break;
1346     case BFD_RELOC_PPC64_PLTGOT16_HI:    ppc_reloc = R_PPC64_PLTGOT16_HI;
1347       break;
1348     case BFD_RELOC_PPC64_PLTGOT16_HA:    ppc_reloc = R_PPC64_PLTGOT16_HA;
1349       break;
1350     case BFD_RELOC_PPC64_ADDR16_DS:      ppc_reloc = R_PPC64_ADDR16_DS;
1351       break;
1352     case BFD_RELOC_PPC64_ADDR16_LO_DS:   ppc_reloc = R_PPC64_ADDR16_LO_DS;
1353       break;
1354     case BFD_RELOC_PPC64_GOT16_DS:       ppc_reloc = R_PPC64_GOT16_DS;
1355       break;
1356     case BFD_RELOC_PPC64_GOT16_LO_DS:    ppc_reloc = R_PPC64_GOT16_LO_DS;
1357       break;
1358     case BFD_RELOC_PPC64_PLT16_LO_DS:    ppc_reloc = R_PPC64_PLT16_LO_DS;
1359       break;
1360     case BFD_RELOC_PPC64_SECTOFF_DS:     ppc_reloc = R_PPC64_SECTOFF_DS;
1361       break;
1362     case BFD_RELOC_PPC64_SECTOFF_LO_DS:  ppc_reloc = R_PPC64_SECTOFF_LO_DS;
1363       break;
1364     case BFD_RELOC_PPC64_TOC16_DS:       ppc_reloc = R_PPC64_TOC16_DS;
1365       break;
1366     case BFD_RELOC_PPC64_TOC16_LO_DS:    ppc_reloc = R_PPC64_TOC16_LO_DS;
1367       break;
1368     case BFD_RELOC_PPC64_PLTGOT16_DS:    ppc_reloc = R_PPC64_PLTGOT16_DS;
1369       break;
1370     case BFD_RELOC_PPC64_PLTGOT16_LO_DS: ppc_reloc = R_PPC64_PLTGOT16_LO_DS;
1371       break;
1372     case BFD_RELOC_VTABLE_INHERIT:       ppc_reloc = R_PPC64_GNU_VTINHERIT;
1373       break;
1374     case BFD_RELOC_VTABLE_ENTRY:         ppc_reloc = R_PPC64_GNU_VTENTRY;
1375       break;
1376     }
1377
1378   return ppc64_elf_howto_table[(int) ppc_reloc];
1379 };
1380
1381 /* Set the howto pointer for a PowerPC ELF reloc.  */
1382
1383 static void
1384 ppc64_elf_info_to_howto (abfd, cache_ptr, dst)
1385      bfd *abfd ATTRIBUTE_UNUSED;
1386      arelent *cache_ptr;
1387      Elf64_Internal_Rela *dst;
1388 {
1389   unsigned int type;
1390
1391   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1392     /* Initialize howto table if needed.  */
1393     ppc_howto_init ();
1394
1395   type = ELF64_R_TYPE (dst->r_info);
1396   BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1397                       / sizeof (ppc64_elf_howto_table[0])));
1398   cache_ptr->howto = ppc64_elf_howto_table[type];
1399 }
1400
1401 /* Handle the R_PPC_ADDR16_HA and similar relocs.  */
1402
1403 static bfd_reloc_status_type
1404 ppc64_elf_addr16_ha_reloc (abfd, reloc_entry, symbol, data, input_section,
1405                            output_bfd, error_message)
1406      bfd *abfd ATTRIBUTE_UNUSED;
1407      arelent *reloc_entry;
1408      asymbol *symbol;
1409      PTR data ATTRIBUTE_UNUSED;
1410      asection *input_section;
1411      bfd *output_bfd;
1412      char **error_message ATTRIBUTE_UNUSED;
1413 {
1414   bfd_vma relocation;
1415
1416   if (output_bfd != NULL)
1417     {
1418       reloc_entry->address += input_section->output_offset;
1419       return bfd_reloc_ok;
1420     }
1421
1422   if (reloc_entry->address > input_section->_cooked_size)
1423     return bfd_reloc_outofrange;
1424
1425   if (bfd_is_com_section (symbol->section))
1426     relocation = 0;
1427   else
1428     relocation = symbol->value;
1429
1430   relocation += symbol->section->output_section->vma;
1431   relocation += symbol->section->output_offset;
1432   relocation += reloc_entry->addend;
1433
1434   reloc_entry->addend += (relocation & 0x8000) << 1;
1435
1436   return bfd_reloc_continue;
1437 }
1438
1439 /* Function to set whether a module needs the -mrelocatable bit set.  */
1440
1441 static boolean
1442 ppc64_elf_set_private_flags (abfd, flags)
1443      bfd *abfd;
1444      flagword flags;
1445 {
1446   BFD_ASSERT (!elf_flags_init (abfd)
1447               || elf_elfheader (abfd)->e_flags == flags);
1448
1449   elf_elfheader (abfd)->e_flags = flags;
1450   elf_flags_init (abfd) = true;
1451   return true;
1452 }
1453
1454 /* Merge backend specific data from an object file to the output
1455    object file when linking.  */
1456 static boolean
1457 ppc64_elf_merge_private_bfd_data (ibfd, obfd)
1458      bfd *ibfd;
1459      bfd *obfd;
1460 {
1461   flagword old_flags;
1462   flagword new_flags;
1463   boolean error;
1464
1465   /* Check if we have the same endianess.  */
1466   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1467       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1468     {
1469       const char *msg;
1470
1471       if (bfd_big_endian (ibfd))
1472         msg = _("%s: compiled for a big endian system and target is little endian");
1473       else
1474         msg = _("%s: compiled for a little endian system and target is big endian");
1475
1476       (*_bfd_error_handler) (msg, bfd_archive_filename (ibfd));
1477
1478       bfd_set_error (bfd_error_wrong_format);
1479       return false;
1480     }
1481
1482   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1483       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1484     return true;
1485
1486   new_flags = elf_elfheader (ibfd)->e_flags;
1487   old_flags = elf_elfheader (obfd)->e_flags;
1488   if (!elf_flags_init (obfd))
1489     {
1490       /* First call, no flags set.  */
1491       elf_flags_init (obfd) = true;
1492       elf_elfheader (obfd)->e_flags = new_flags;
1493     }
1494
1495   else if (new_flags == old_flags)
1496     /* Compatible flags are ok.  */
1497     ;
1498
1499   else
1500     {
1501       /* Incompatible flags.  Warn about -mrelocatable mismatch.
1502          Allow -mrelocatable-lib to be linked with either.  */
1503       error = false;
1504       if ((new_flags & EF_PPC_RELOCATABLE) != 0
1505           && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
1506         {
1507           error = true;
1508           (*_bfd_error_handler)
1509             (_("%s: compiled with -mrelocatable and linked with modules compiled normally"),
1510              bfd_archive_filename (ibfd));
1511         }
1512       else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
1513                && (old_flags & EF_PPC_RELOCATABLE) != 0)
1514         {
1515           error = true;
1516           (*_bfd_error_handler)
1517             (_("%s: compiled normally and linked with modules compiled with -mrelocatable"),
1518              bfd_archive_filename (ibfd));
1519         }
1520
1521       /* The output is -mrelocatable-lib iff both the input files are.  */
1522       if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
1523         elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
1524
1525       /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
1526          but each input file is either -mrelocatable or -mrelocatable-lib.  */
1527       if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
1528           && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
1529           && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
1530         elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
1531
1532       /* Do not warn about eabi vs. V.4 mismatch, just or in the bit
1533          if any module uses it.  */
1534       elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
1535
1536       new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1537       old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1538
1539       /* Warn about any other mismatches.  */
1540       if (new_flags != old_flags)
1541         {
1542           error = true;
1543           (*_bfd_error_handler)
1544             (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1545              bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
1546         }
1547
1548       if (error)
1549         {
1550           bfd_set_error (bfd_error_bad_value);
1551           return false;
1552         }
1553     }
1554
1555   return true;
1556 }
1557
1558 /* Handle a PowerPC specific section when reading an object file.  This
1559    is called when elfcode.h finds a section with an unknown type.  */
1560
1561 static boolean
1562 ppc64_elf_section_from_shdr (abfd, hdr, name)
1563      bfd *abfd;
1564      Elf64_Internal_Shdr *hdr;
1565      char *name;
1566 {
1567   asection *newsect;
1568   flagword flags;
1569
1570   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1571     return false;
1572
1573   newsect = hdr->bfd_section;
1574   flags = bfd_get_section_flags (abfd, newsect);
1575   if (hdr->sh_flags & SHF_EXCLUDE)
1576     flags |= SEC_EXCLUDE;
1577
1578   if (hdr->sh_type == SHT_ORDERED)
1579     flags |= SEC_SORT_ENTRIES;
1580
1581   bfd_set_section_flags (abfd, newsect, flags);
1582   return true;
1583 }
1584 \f
1585 /* The following functions are specific to the ELF linker, while
1586    functions above are used generally.  Those named ppc64_elf_* are
1587    called by the main ELF linker code.  They appear in this file more
1588    or less in the order in which they are called.  eg.
1589    ppc64_elf_check_relocs is called early in the link process,
1590    ppc64_elf_finish_dynamic_sections is one of the last functions
1591    called.
1592
1593    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
1594    functions have both a function code symbol and a function descriptor
1595    symbol.  A call to foo in a relocatable object file looks like:
1596
1597    .            .text
1598    .    x:
1599    .            bl      .foo
1600    .            nop
1601
1602    The function definition in another object file might be:
1603
1604    .            .section .opd
1605    .    foo:    .quad   .foo
1606    .            .quad   .TOC.@tocbase
1607    .            .quad   0
1608    .
1609    .            .text
1610    .    .foo:   blr
1611
1612    When the linker resolves the call during a static link, the branch
1613    unsurprisingly just goes to .foo and the .opd information is unused.
1614    If the function definition is in a shared library, things are a little
1615    different:  The call goes via a plt call stub, the opd information gets
1616    copied to the plt, and the linker patches the nop.
1617
1618    .    x:
1619    .            bl      .foo_stub
1620    .            ld      2,40(1)
1621    .
1622    .
1623    .    .foo_stub:
1624    .            addis   12,2,Lfoo@toc@ha        # in practice, the call stub
1625    .            addi    12,12,Lfoo@toc@l        # is slightly optimised, but
1626    .            std     2,40(1)                 # this is the general idea
1627    .            ld      11,0(12)
1628    .            ld      2,8(12)
1629    .            mtctr   11
1630    .            ld      11,16(12)
1631    .            bctr
1632    .
1633    .            .section .plt
1634    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
1635
1636    The "reloc ()" notation is supposed to indicate that the linker emits
1637    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
1638    copying.
1639
1640    What are the difficulties here?  Well, firstly, the relocations
1641    examined by the linker in check_relocs are against the function code
1642    sym .foo, while the dynamic relocation in the plt is emitted against
1643    the function descriptor symbol, foo.  Somewhere along the line, we need
1644    to carefully copy dynamic link information from one symbol to the other.
1645    Secondly, the generic part of the elf linker will make .foo a dynamic
1646    symbol as is normal for most other backends.  We need foo dynamic
1647    instead, at least for an application final link.  However, when
1648    creating a shared library containing foo, we need to have both symbols
1649    dynamic so that references to .foo are satisfied during the early
1650    stages of linking.  Otherwise the linker might decide to pull in a
1651    definition from some other object, eg. a static library.  Thirdly, we'd
1652    like to use .foo as the stub symbol to avoid creating another symbol.
1653    We need to make sure that when .foo labels a stub in a shared library,
1654    it isn't exported.  */
1655
1656 /* The linker needs to keep track of the number of relocs that it
1657    decides to copy as dynamic relocs in check_relocs for each symbol.
1658    This is so that it can later discard them if they are found to be
1659    unnecessary.  We store the information in a field extending the
1660    regular ELF linker hash table.  */
1661
1662 struct ppc_dyn_relocs
1663 {
1664   struct ppc_dyn_relocs *next;
1665
1666   /* The input section of the reloc.  */
1667   asection *sec;
1668
1669   /* Total number of relocs copied for the input section.  */
1670   bfd_size_type count;
1671
1672   /* Number of pc-relative relocs copied for the input section.  */
1673   bfd_size_type pc_count;
1674 };
1675
1676 /* Of those relocs that might be copied as dynamic relocs, this macro
1677    selects between relative and absolute types.  */
1678
1679 #define IS_ABSOLUTE_RELOC(RTYPE)                \
1680   ((RTYPE) != R_PPC64_REL14                     \
1681    && (RTYPE) != R_PPC64_REL14_BRNTAKEN         \
1682    && (RTYPE) != R_PPC64_REL14_BRTAKEN          \
1683    && (RTYPE) != R_PPC64_REL24                  \
1684    && (RTYPE) != R_PPC64_REL32                  \
1685    && (RTYPE) != R_PPC64_REL64)
1686
1687 /* ppc64 ELF linker hash entry.  */
1688
1689 struct ppc_link_hash_entry
1690 {
1691   struct elf_link_hash_entry elf;
1692
1693   /* Track dynamic relocs copied for this symbol.  */
1694   struct ppc_dyn_relocs *dyn_relocs;
1695
1696   /* Flag function code and descriptor symbols.  */
1697   unsigned int is_func:1;
1698   unsigned int is_func_descriptor:1;
1699 };
1700
1701 /* ppc64 ELF linker hash table.  */
1702
1703 struct ppc_link_hash_table
1704 {
1705   struct elf_link_hash_table elf;
1706
1707   /* Short-cuts to get to dynamic linker sections.  */
1708   asection *sgot;
1709   asection *srelgot;
1710   asection *splt;
1711   asection *srelplt;
1712   asection *sdynbss;
1713   asection *srelbss;
1714   asection *sstub;
1715   asection *sglink;
1716
1717   /* Set on error.  */
1718   int plt_overflow;
1719
1720   /* Small local sym to section mapping cache.  */
1721   struct sym_sec_cache sym_sec;
1722 };
1723
1724 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
1725
1726 #define ppc_hash_table(p) \
1727   ((struct ppc_link_hash_table *) ((p)->hash))
1728
1729 /* Create an entry in a ppc64 ELF linker hash table.  */
1730
1731 static struct bfd_hash_entry *
1732 link_hash_newfunc (entry, table, string)
1733      struct bfd_hash_entry *entry;
1734      struct bfd_hash_table *table;
1735      const char *string;
1736 {
1737   /* Allocate the structure if it has not already been allocated by a
1738      subclass.  */
1739   if (entry == NULL)
1740     {
1741       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
1742       if (entry == NULL)
1743         return entry;
1744     }
1745
1746   /* Call the allocation method of the superclass.  */
1747   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
1748   if (entry != NULL)
1749     {
1750       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
1751
1752       eh->dyn_relocs = NULL;
1753       eh->is_func = 0;
1754       eh->is_func_descriptor = 0;
1755     }
1756
1757   return entry;
1758 }
1759
1760 /* Create a ppc64 ELF linker hash table.  */
1761
1762 static struct bfd_link_hash_table *
1763 ppc64_elf_link_hash_table_create (abfd)
1764      bfd *abfd;
1765 {
1766   struct ppc_link_hash_table *htab;
1767   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
1768
1769   htab = (struct ppc_link_hash_table *) bfd_alloc (abfd, amt);
1770   if (htab == NULL)
1771     return NULL;
1772
1773   if (! _bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc))
1774     {
1775       bfd_release (abfd, htab);
1776       return NULL;
1777     }
1778
1779   htab->sgot = NULL;
1780   htab->srelgot = NULL;
1781   htab->splt = NULL;
1782   htab->srelplt = NULL;
1783   htab->sdynbss = NULL;
1784   htab->srelbss = NULL;
1785   htab->sstub = NULL;
1786   htab->sglink = NULL;
1787   htab->plt_overflow = 0;
1788   htab->sym_sec.abfd = NULL;
1789
1790   return &htab->elf.root;
1791 }
1792
1793 /* Create .got and .rela.got sections in DYNOBJ, and set up
1794    shortcuts to them in our hash table.  */
1795
1796 static boolean
1797 create_got_section (dynobj, info)
1798      bfd *dynobj;
1799      struct bfd_link_info *info;
1800 {
1801   struct ppc_link_hash_table *htab;
1802
1803   if (! _bfd_elf_create_got_section (dynobj, info))
1804     return false;
1805
1806   htab = ppc_hash_table (info);
1807   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
1808   if (!htab->sgot)
1809     abort ();
1810
1811   htab->srelgot = bfd_make_section (dynobj, ".rela.got");
1812   if (!htab->srelgot
1813       || ! bfd_set_section_flags (dynobj, htab->srelgot,
1814                                   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1815                                    | SEC_IN_MEMORY | SEC_LINKER_CREATED
1816                                    | SEC_READONLY))
1817       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
1818     return false;
1819   return true;
1820 }
1821
1822 /* Create the .stub and .glink sections as well as the ordinary
1823    dynamic sections.  */
1824
1825 static boolean
1826 ppc64_elf_create_dynamic_sections (dynobj, info)
1827      bfd *dynobj;
1828      struct bfd_link_info *info;
1829 {
1830   struct ppc_link_hash_table *htab;
1831   flagword flags;
1832
1833   htab = ppc_hash_table (info);
1834   if (!htab->sgot && !create_got_section (dynobj, info))
1835     return false;
1836
1837   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
1838     return false;
1839
1840   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
1841   htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
1842   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
1843   if (!info->shared)
1844     htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
1845
1846   if (!htab->splt || !htab->srelplt || !htab->sdynbss
1847       || (!info->shared && !htab->srelbss))
1848     abort ();
1849
1850   /* Create .stub and .glink for global linkage functions.  */
1851   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
1852            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1853   htab->sstub = bfd_make_section (dynobj, ".stub");
1854   if (htab->sstub == NULL
1855       || ! bfd_set_section_flags (dynobj, htab->sstub, flags)
1856       || ! bfd_set_section_alignment (dynobj, htab->sstub, 2))
1857     return false;
1858   htab->sglink = bfd_make_section (dynobj, ".glink");
1859   if (htab->sglink == NULL
1860       || ! bfd_set_section_flags (dynobj, htab->sglink, flags)
1861       || ! bfd_set_section_alignment (dynobj, htab->sglink, 3))
1862     return false;
1863
1864   return true;
1865 }
1866
1867 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
1868
1869 static void
1870 ppc64_elf_copy_indirect_symbol (dir, ind)
1871      struct elf_link_hash_entry *dir, *ind;
1872 {
1873   struct ppc_link_hash_entry *edir, *eind;
1874
1875   edir = (struct ppc_link_hash_entry *) dir;
1876   eind = (struct ppc_link_hash_entry *) ind;
1877
1878   if (eind->dyn_relocs != NULL)
1879     {
1880       if (edir->dyn_relocs != NULL)
1881         {
1882           struct ppc_dyn_relocs **pp;
1883           struct ppc_dyn_relocs *p;
1884
1885           if (ind->root.type == bfd_link_hash_indirect)
1886             abort ();
1887
1888           /* Add reloc counts against the weak sym to the strong sym
1889              list.  Merge any entries against the same section.  */
1890           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1891             {
1892               struct ppc_dyn_relocs *q;
1893
1894               for (q = edir->dyn_relocs; q != NULL; q = q->next)
1895                 if (q->sec == p->sec)
1896                   {
1897                     q->pc_count += p->pc_count;
1898                     q->count += p->count;
1899                     *pp = p->next;
1900                     break;
1901                   }
1902               if (q == NULL)
1903                 pp = &p->next;
1904             }
1905           *pp = edir->dyn_relocs;
1906         }
1907
1908       edir->dyn_relocs = eind->dyn_relocs;
1909       eind->dyn_relocs = NULL;
1910     }
1911
1912   edir->is_func |= eind->is_func;
1913   edir->is_func_descriptor |= eind->is_func_descriptor;
1914
1915   _bfd_elf_link_hash_copy_indirect (dir, ind);
1916 }
1917
1918 /* Look through the relocs for a section during the first phase, and
1919    calculate needed space in the global offset table, procedure
1920    linkage table, and dynamic reloc sections.  */
1921
1922 static boolean
1923 ppc64_elf_check_relocs (abfd, info, sec, relocs)
1924      bfd *abfd;
1925      struct bfd_link_info *info;
1926      asection *sec;
1927      const Elf_Internal_Rela *relocs;
1928 {
1929   struct ppc_link_hash_table *htab;
1930   Elf_Internal_Shdr *symtab_hdr;
1931   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
1932   const Elf_Internal_Rela *rel;
1933   const Elf_Internal_Rela *rel_end;
1934   asection *sreloc;
1935   boolean is_opd;
1936
1937   if (info->relocateable)
1938     return true;
1939
1940   htab = ppc_hash_table (info);
1941   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1942
1943   sym_hashes = elf_sym_hashes (abfd);
1944   sym_hashes_end = (sym_hashes
1945                     + symtab_hdr->sh_size / sizeof (Elf64_External_Sym));
1946   if (!elf_bad_symtab (abfd))
1947     sym_hashes_end -= symtab_hdr->sh_info;
1948
1949   sreloc = NULL;
1950   is_opd = strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0;
1951
1952   rel_end = relocs + sec->reloc_count;
1953   for (rel = relocs; rel < rel_end; rel++)
1954     {
1955       unsigned long r_symndx;
1956       struct elf_link_hash_entry *h;
1957       enum elf_ppc_reloc_type r_type;
1958
1959       r_symndx = ELF64_R_SYM (rel->r_info);
1960       if (r_symndx < symtab_hdr->sh_info)
1961         h = NULL;
1962       else
1963         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1964
1965       r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
1966       switch (r_type)
1967         {
1968           /* GOT16 relocations */
1969         case R_PPC64_GOT16:
1970         case R_PPC64_GOT16_DS:
1971         case R_PPC64_GOT16_HA:
1972         case R_PPC64_GOT16_HI:
1973         case R_PPC64_GOT16_LO:
1974         case R_PPC64_GOT16_LO_DS:
1975
1976           /* This symbol requires a global offset table entry.  */
1977           if (htab->sgot == NULL)
1978             {
1979               if (htab->elf.dynobj == NULL)
1980                 htab->elf.dynobj = abfd;
1981               if (!create_got_section (htab->elf.dynobj, info))
1982                 return false;
1983             }
1984
1985           if (h != NULL)
1986             {
1987               h->got.refcount += 1;
1988             }
1989           else
1990             {
1991               bfd_signed_vma *local_got_refcounts;
1992
1993               /* This is a global offset table entry for a local symbol.  */
1994               local_got_refcounts = elf_local_got_refcounts (abfd);
1995               if (local_got_refcounts == NULL)
1996                 {
1997                   bfd_size_type size;
1998
1999                   size = symtab_hdr->sh_info;
2000                   size *= sizeof (bfd_signed_vma);
2001                   local_got_refcounts = ((bfd_signed_vma *)
2002                                          bfd_zalloc (abfd, size));
2003                   if (local_got_refcounts == NULL)
2004                     return false;
2005                   elf_local_got_refcounts (abfd) = local_got_refcounts;
2006                 }
2007               local_got_refcounts[r_symndx] += 1;
2008             }
2009           break;
2010
2011         case R_PPC64_PLT16_HA:
2012         case R_PPC64_PLT16_HI:
2013         case R_PPC64_PLT16_LO:
2014         case R_PPC64_PLT32:
2015         case R_PPC64_PLT64:
2016           /* This symbol requires a procedure linkage table entry.  We
2017              actually build the entry in adjust_dynamic_symbol,
2018              because this might be a case of linking PIC code without
2019              linking in any dynamic objects, in which case we don't
2020              need to generate a procedure linkage table after all.  */
2021           if (h == NULL)
2022             {
2023               /* It does not make sense to have a procedure linkage
2024                  table entry for a local symbol.  */
2025               bfd_set_error (bfd_error_bad_value);
2026               return false;
2027             }
2028
2029           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2030           h->plt.refcount += 1;
2031           ((struct ppc_link_hash_entry *) h)->is_func = 1;
2032           break;
2033
2034           /* The following relocations don't need to propagate the
2035              relocation if linking a shared object since they are
2036              section relative.  */
2037         case R_PPC64_SECTOFF:
2038         case R_PPC64_SECTOFF_LO:
2039         case R_PPC64_SECTOFF_HI:
2040         case R_PPC64_SECTOFF_HA:
2041         case R_PPC64_SECTOFF_DS:
2042         case R_PPC64_SECTOFF_LO_DS:
2043         case R_PPC64_TOC16:
2044         case R_PPC64_TOC16_LO:
2045         case R_PPC64_TOC16_HI:
2046         case R_PPC64_TOC16_HA:
2047         case R_PPC64_TOC16_DS:
2048         case R_PPC64_TOC16_LO_DS:
2049           break;
2050
2051           /* This relocation describes the C++ object vtable hierarchy.
2052              Reconstruct it for later use during GC.  */
2053         case R_PPC64_GNU_VTINHERIT:
2054           if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2055             return false;
2056           break;
2057
2058           /* This relocation describes which C++ vtable entries are actually
2059              used.  Record for later use during GC.  */
2060         case R_PPC64_GNU_VTENTRY:
2061           if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2062             return false;
2063           break;
2064
2065         case R_PPC64_REL24:
2066           if (h != NULL
2067               && h->root.root.string[0] == '.'
2068               && h->root.root.string[1] != 0)
2069             {
2070               /* We may need a .plt entry if the function this reloc
2071                  refers to is in a shared lib.  */
2072               h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2073               h->plt.refcount += 1;
2074               ((struct ppc_link_hash_entry *) h)->is_func = 1;
2075             }
2076           break;
2077
2078         case R_PPC64_ADDR64:
2079           if (is_opd
2080               && h != NULL
2081               && h->root.root.string[0] == '.'
2082               && h->root.root.string[1] != 0)
2083             {
2084               struct elf_link_hash_entry *fdh;
2085
2086               fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
2087                                           false, false, false);
2088               if (fdh != NULL)
2089                 {
2090                   /* Ensure the function descriptor symbol string is
2091                      part of the code symbol string.  We aren't
2092                      changing the name here, just allowing some tricks
2093                      in ppc64_elf_hide_symbol.  */
2094                   fdh->root.root.string = h->root.root.string + 1;
2095                   ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
2096                   ((struct ppc_link_hash_entry *) h)->is_func = 1;
2097                 }
2098             }
2099           /* Fall through.  */
2100
2101         case R_PPC64_REL64:
2102         case R_PPC64_REL32:
2103         case R_PPC64_REL14:
2104         case R_PPC64_REL14_BRTAKEN:
2105         case R_PPC64_REL14_BRNTAKEN:
2106         case R_PPC64_ADDR14:
2107         case R_PPC64_ADDR14_BRNTAKEN:
2108         case R_PPC64_ADDR14_BRTAKEN:
2109         case R_PPC64_ADDR16:
2110         case R_PPC64_ADDR16_DS:
2111         case R_PPC64_ADDR16_HA:
2112         case R_PPC64_ADDR16_HI:
2113         case R_PPC64_ADDR16_HIGHER:
2114         case R_PPC64_ADDR16_HIGHERA:
2115         case R_PPC64_ADDR16_HIGHEST:
2116         case R_PPC64_ADDR16_HIGHESTA:
2117         case R_PPC64_ADDR16_LO:
2118         case R_PPC64_ADDR16_LO_DS:
2119         case R_PPC64_ADDR24:
2120         case R_PPC64_ADDR30:
2121         case R_PPC64_ADDR32:
2122         case R_PPC64_UADDR16:
2123         case R_PPC64_UADDR32:
2124         case R_PPC64_UADDR64:
2125         case R_PPC64_TOC:
2126 #if 0
2127               /* Don't propagate .opd relocs.  */
2128           if (is_opd)
2129             break;
2130 #endif
2131
2132           /* If we are creating a shared library, and this is a reloc
2133              against a global symbol, or a non PC relative reloc
2134              against a local symbol, then we need to copy the reloc
2135              into the shared library.  However, if we are linking with
2136              -Bsymbolic, we do not need to copy a reloc against a
2137              global symbol which is defined in an object we are
2138              including in the link (i.e., DEF_REGULAR is set).  At
2139              this point we have not seen all the input files, so it is
2140              possible that DEF_REGULAR is not set now but will be set
2141              later (it is never cleared).  In case of a weak definition,
2142              DEF_REGULAR may be cleared later by a strong definition in
2143              a shared library.  We account for that possibility below by
2144              storing information in the relocs_copied field of the hash
2145              table entry.  A similar situation occurs when creating
2146              shared libraries and symbol visibility changes render the
2147              symbol local.
2148
2149              If on the other hand, we are creating an executable, we
2150              may need to keep relocations for symbols satisfied by a
2151              dynamic library if we manage to avoid copy relocs for the
2152              symbol.  */
2153           if ((info->shared
2154                && (sec->flags & SEC_ALLOC) != 0
2155                && (IS_ABSOLUTE_RELOC (r_type)
2156                    || (h != NULL
2157                        && (! info->symbolic
2158                            || h->root.type == bfd_link_hash_defweak
2159                            || (h->elf_link_hash_flags
2160                                & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2161               || (!info->shared
2162                   && (sec->flags & SEC_ALLOC) != 0
2163                   && h != NULL
2164                   && (h->root.type == bfd_link_hash_defweak
2165                       || (h->elf_link_hash_flags
2166                           & ELF_LINK_HASH_DEF_REGULAR) == 0)))
2167             {
2168               struct ppc_dyn_relocs *p;
2169               struct ppc_dyn_relocs **head;
2170
2171               /* We must copy these reloc types into the output file.
2172                  Create a reloc section in dynobj and make room for
2173                  this reloc.  */
2174               if (sreloc == NULL)
2175                 {
2176                   const char *name;
2177                   bfd *dynobj;
2178
2179                   name = (bfd_elf_string_from_elf_section
2180                           (abfd,
2181                            elf_elfheader (abfd)->e_shstrndx,
2182                            elf_section_data (sec)->rel_hdr.sh_name));
2183                   if (name == NULL)
2184                     return false;
2185
2186                   if (strncmp (name, ".rela", 5) != 0
2187                       || strcmp (bfd_get_section_name (abfd, sec),
2188                                  name + 5) != 0)
2189                     {
2190                       (*_bfd_error_handler)
2191                         (_("%s: bad relocation section name `%s\'"),
2192                          bfd_archive_filename (abfd), name);
2193                       bfd_set_error (bfd_error_bad_value);
2194                     }
2195
2196                   if (htab->elf.dynobj == NULL)
2197                     htab->elf.dynobj = abfd;
2198
2199                   dynobj = htab->elf.dynobj;
2200                   sreloc = bfd_get_section_by_name (dynobj, name);
2201                   if (sreloc == NULL)
2202                     {
2203                       flagword flags;
2204
2205                       sreloc = bfd_make_section (dynobj, name);
2206                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
2207                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2208                       if ((sec->flags & SEC_ALLOC) != 0)
2209                         flags |= SEC_ALLOC | SEC_LOAD;
2210                       if (sreloc == NULL
2211                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
2212                           || ! bfd_set_section_alignment (dynobj, sreloc, 3))
2213                         return false;
2214                     }
2215                   elf_section_data (sec)->sreloc = sreloc;
2216                 }
2217
2218               /* If this is a global symbol, we count the number of
2219                  relocations we need for this symbol.  */
2220               if (h != NULL)
2221                 {
2222                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
2223                 }
2224               else
2225                 {
2226                   /* Track dynamic relocs needed for local syms too.
2227                      We really need local syms available to do this
2228                      easily.  Oh well.  */
2229
2230                   asection *s;
2231                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
2232                                                  sec, r_symndx);
2233                   if (s == NULL)
2234                     return false;
2235
2236                   head = ((struct ppc_dyn_relocs **)
2237                           &elf_section_data (s)->local_dynrel);
2238                 }
2239
2240               p = *head;
2241               if (p == NULL || p->sec != sec)
2242                 {
2243                   p = ((struct ppc_dyn_relocs *)
2244                        bfd_alloc (htab->elf.dynobj,
2245                                   (bfd_size_type) sizeof *p));
2246                   if (p == NULL)
2247                     return false;
2248                   p->next = *head;
2249                   *head = p;
2250                   p->sec = sec;
2251                   p->count = 0;
2252                   p->pc_count = 0;
2253                 }
2254
2255               p->count += 1;
2256               if (!IS_ABSOLUTE_RELOC (r_type))
2257                 p->pc_count += 1;
2258             }
2259           break;
2260
2261         default:
2262         }
2263     }
2264
2265   return true;
2266 }
2267
2268 /* Return the section that should be marked against GC for a given
2269    relocation.  */
2270
2271 static asection *
2272 ppc64_elf_gc_mark_hook (abfd, info, rel, h, sym)
2273      bfd *abfd;
2274      struct bfd_link_info *info ATTRIBUTE_UNUSED;
2275      Elf_Internal_Rela *rel;
2276      struct elf_link_hash_entry *h;
2277      Elf_Internal_Sym *sym;
2278 {
2279   if (h != NULL)
2280     {
2281       enum elf_ppc_reloc_type r_type;
2282
2283       r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
2284       switch (r_type)
2285         {
2286         case R_PPC64_GNU_VTINHERIT:
2287         case R_PPC64_GNU_VTENTRY:
2288           break;
2289
2290         default:
2291           switch (h->root.type)
2292             {
2293             case bfd_link_hash_defined:
2294             case bfd_link_hash_defweak:
2295               return h->root.u.def.section;
2296
2297             case bfd_link_hash_common:
2298               return h->root.u.c.p->section;
2299
2300             default:
2301               break;
2302             }
2303         }
2304     }
2305   else
2306     {
2307       return bfd_section_from_elf_index (abfd, sym->st_shndx);
2308     }
2309
2310   return NULL;
2311 }
2312
2313 /* Update the .got, .plt. and dynamic reloc reference counts for the
2314    section being removed.  */
2315
2316 static boolean
2317 ppc64_elf_gc_sweep_hook (abfd, info, sec, relocs)
2318      bfd *abfd;
2319      struct bfd_link_info *info ATTRIBUTE_UNUSED;
2320      asection *sec;
2321      const Elf_Internal_Rela *relocs;
2322 {
2323   Elf_Internal_Shdr *symtab_hdr;
2324   struct elf_link_hash_entry **sym_hashes;
2325   bfd_signed_vma *local_got_refcounts;
2326   const Elf_Internal_Rela *rel, *relend;
2327
2328   elf_section_data (sec)->local_dynrel = NULL;
2329
2330   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2331   sym_hashes = elf_sym_hashes (abfd);
2332   local_got_refcounts = elf_local_got_refcounts (abfd);
2333
2334   relend = relocs + sec->reloc_count;
2335   for (rel = relocs; rel < relend; rel++)
2336     {
2337       unsigned long r_symndx;
2338       enum elf_ppc_reloc_type r_type;
2339       struct elf_link_hash_entry *h;
2340
2341       r_symndx = ELF64_R_SYM (rel->r_info);
2342       r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
2343       switch (r_type)
2344         {
2345         case R_PPC64_GOT16:
2346         case R_PPC64_GOT16_DS:
2347         case R_PPC64_GOT16_HA:
2348         case R_PPC64_GOT16_HI:
2349         case R_PPC64_GOT16_LO:
2350         case R_PPC64_GOT16_LO_DS:
2351           if (r_symndx >= symtab_hdr->sh_info)
2352             {
2353               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2354               if (h->got.refcount > 0)
2355                 h->got.refcount--;
2356             }
2357           else
2358             {
2359               if (local_got_refcounts[r_symndx] > 0)
2360                 local_got_refcounts[r_symndx]--;
2361             }
2362           break;
2363
2364         case R_PPC64_PLT16_HA:
2365         case R_PPC64_PLT16_HI:
2366         case R_PPC64_PLT16_LO:
2367         case R_PPC64_PLT32:
2368         case R_PPC64_PLT64:
2369           if (r_symndx >= symtab_hdr->sh_info)
2370             {
2371               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2372               if (h->plt.refcount > 0)
2373                 h->plt.refcount--;
2374             }
2375           break;
2376
2377         case R_PPC64_REL24:
2378           if (r_symndx >= symtab_hdr->sh_info)
2379             {
2380               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2381               if (h->plt.refcount > 0)
2382                 h->plt.refcount--;
2383             }
2384           break;
2385
2386         case R_PPC64_REL14:
2387         case R_PPC64_REL14_BRNTAKEN:
2388         case R_PPC64_REL14_BRTAKEN:
2389         case R_PPC64_REL32:
2390         case R_PPC64_REL64:
2391           if (r_symndx >= symtab_hdr->sh_info)
2392             {
2393               struct ppc_link_hash_entry *eh;
2394               struct ppc_dyn_relocs **pp;
2395               struct ppc_dyn_relocs *p;
2396
2397               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2398               eh = (struct ppc_link_hash_entry *) h;
2399
2400               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
2401                 if (p->sec == sec)
2402                   {
2403                     p->pc_count -= 1;
2404                     p->count -= 1;
2405                     if (p->count == 0)
2406                       *pp = p->next;
2407                     break;
2408                   }
2409             }
2410           break;
2411
2412         case R_PPC64_ADDR14:
2413         case R_PPC64_ADDR14_BRNTAKEN:
2414         case R_PPC64_ADDR14_BRTAKEN:
2415         case R_PPC64_ADDR16:
2416         case R_PPC64_ADDR16_DS:
2417         case R_PPC64_ADDR16_HA:
2418         case R_PPC64_ADDR16_HI:
2419         case R_PPC64_ADDR16_HIGHER:
2420         case R_PPC64_ADDR16_HIGHERA:
2421         case R_PPC64_ADDR16_HIGHEST:
2422         case R_PPC64_ADDR16_HIGHESTA:
2423         case R_PPC64_ADDR16_LO:
2424         case R_PPC64_ADDR16_LO_DS:
2425         case R_PPC64_ADDR24:
2426         case R_PPC64_ADDR30:
2427         case R_PPC64_ADDR32:
2428         case R_PPC64_ADDR64:
2429         case R_PPC64_UADDR16:
2430         case R_PPC64_UADDR32:
2431         case R_PPC64_UADDR64:
2432         case R_PPC64_TOC:
2433           if (r_symndx >= symtab_hdr->sh_info)
2434             {
2435               struct ppc_link_hash_entry *eh;
2436               struct ppc_dyn_relocs **pp;
2437               struct ppc_dyn_relocs *p;
2438
2439               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2440               eh = (struct ppc_link_hash_entry *) h;
2441
2442               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
2443                 if (p->sec == sec)
2444                   {
2445                     p->count -= 1;
2446                     if (p->count == 0)
2447                       *pp = p->next;
2448                     break;
2449                   }
2450             }
2451           break;
2452
2453         default:
2454           break;
2455         }
2456     }
2457   return true;
2458 }
2459
2460 /* Called via elf_link_hash_traverse to transfer dynamic linking
2461    information on function code symbol entries to their corresponding
2462    function descriptor symbol entries.  */
2463 static boolean
2464 func_desc_adjust (h, inf)
2465      struct elf_link_hash_entry *h;
2466      PTR inf;
2467 {
2468   struct bfd_link_info *info;
2469   struct ppc_link_hash_table *htab;
2470
2471   if (h->root.type == bfd_link_hash_indirect
2472       || h->root.type == bfd_link_hash_warning)
2473     return true;
2474
2475   info = (struct bfd_link_info *) inf;
2476   htab = ppc_hash_table (info);
2477
2478   /* If this is a function code symbol, transfer dynamic linking
2479      information to the function descriptor symbol.  */
2480   if (!((struct ppc_link_hash_entry *) h)->is_func)
2481     return true;
2482
2483   if (h->plt.refcount > 0
2484       && h->root.root.string[0] == '.'
2485       && h->root.root.string[1] != '\0')
2486     {
2487       struct elf_link_hash_entry *fdh;
2488       boolean force_local;
2489
2490       /* Find the corresponding function descriptor symbol.  Create it
2491          as undefined if necessary.  */
2492
2493       fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
2494                                   false, false, true);
2495
2496       if (fdh == NULL && info->shared)
2497         {
2498           bfd *abfd;
2499           asymbol *newsym;
2500
2501           /* Create it as undefined.  */
2502           if (h->root.type == bfd_link_hash_undefined
2503               || h->root.type == bfd_link_hash_undefweak)
2504             abfd = h->root.u.undef.abfd;
2505           else if (h->root.type == bfd_link_hash_defined
2506                    || h->root.type == bfd_link_hash_defweak)
2507             abfd = h->root.u.def.section->owner;
2508           else
2509             abort ();
2510           newsym = bfd_make_empty_symbol (abfd);
2511           newsym->name = h->root.root.string + 1;
2512           newsym->section = bfd_und_section_ptr;
2513           newsym->value = 0;
2514           newsym->flags = BSF_OBJECT;
2515           if (h->root.type == bfd_link_hash_undefweak)
2516             newsym->flags |= BSF_WEAK;
2517
2518           if ( !(_bfd_generic_link_add_one_symbol
2519                  (info, abfd, newsym->name, newsym->flags,
2520                   newsym->section, newsym->value, NULL, false, false,
2521                   (struct bfd_link_hash_entry **) &fdh)))
2522             {
2523               return false;
2524             }
2525         }
2526
2527       if (fdh != NULL
2528           && (fdh->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
2529           && (info->shared
2530               || (fdh->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2531               || (fdh->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
2532         {
2533           if (fdh->dynindx == -1)
2534             if (! bfd_elf64_link_record_dynamic_symbol (info, fdh))
2535               return false;
2536           fdh->plt.refcount = h->plt.refcount;
2537           fdh->elf_link_hash_flags |= (h->elf_link_hash_flags
2538                                        & (ELF_LINK_HASH_REF_REGULAR
2539                                           | ELF_LINK_HASH_REF_DYNAMIC
2540                                           | ELF_LINK_HASH_REF_REGULAR_NONWEAK
2541                                           | ELF_LINK_NON_GOT_REF));
2542           fdh->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2543           ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
2544           fdh->root.root.string = h->root.root.string + 1;
2545         }
2546
2547       /* Now that the info is on the function descriptor, clear the
2548          function code sym info.  Any function code syms for which we
2549          don't have a definition in a regular file, we force local.
2550          This prevents a shared library from exporting syms that have
2551          been imported from another library.  Function code syms that
2552          are really in the library we must leave global to prevent the
2553          linker dragging a definition in from a static library.  */
2554       force_local = (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0;
2555       _bfd_elf_link_hash_hide_symbol (info, h, force_local);
2556     }
2557
2558   return true;
2559 }
2560
2561 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
2562    this hook to transfer dynamic linking information gathered so far
2563    on function code symbol entries, to their corresponding function
2564    descriptor symbol entries.  */
2565 static boolean
2566 ppc64_elf_func_desc_adjust (obfd, info)
2567      bfd *obfd ATTRIBUTE_UNUSED;
2568      struct bfd_link_info *info;
2569 {
2570   struct ppc_link_hash_table *htab;
2571
2572   htab = ppc_hash_table (info);
2573   elf_link_hash_traverse (&htab->elf, func_desc_adjust, (PTR) info);
2574   return true;
2575 }
2576
2577 /* Adjust a symbol defined by a dynamic object and referenced by a
2578    regular object.  The current definition is in some section of the
2579    dynamic object, but we're not including those sections.  We have to
2580    change the definition to something the rest of the link can
2581    understand.  */
2582
2583 static boolean
2584 ppc64_elf_adjust_dynamic_symbol (info, h)
2585      struct bfd_link_info *info;
2586      struct elf_link_hash_entry *h;
2587 {
2588   struct ppc_link_hash_table *htab;
2589   struct ppc_link_hash_entry * eh;
2590   struct ppc_dyn_relocs *p;
2591   asection *s;
2592   unsigned int power_of_two;
2593
2594   htab = ppc_hash_table (info);
2595
2596   /* Deal with function syms.  */
2597   if (h->type == STT_FUNC
2598       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2599     {
2600       /* Clear procedure linkage table information for any symbol that
2601          won't need a .plt entry.  */
2602       if (!((struct ppc_link_hash_entry *) h)->is_func_descriptor
2603           || h->plt.refcount <= 0
2604           || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2605           || (! info->shared
2606               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2607               && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0))
2608         {
2609           h->plt.offset = (bfd_vma) -1;
2610           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2611         }
2612       return true;
2613     }
2614   else
2615     h->plt.offset = (bfd_vma) -1;
2616
2617   /* If this is a weak symbol, and there is a real definition, the
2618      processor independent code will have arranged for us to see the
2619      real definition first, and we can just use the same value.  */
2620   if (h->weakdef != NULL)
2621     {
2622       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2623                   || h->weakdef->root.type == bfd_link_hash_defweak);
2624       h->root.u.def.section = h->weakdef->root.u.def.section;
2625       h->root.u.def.value = h->weakdef->root.u.def.value;
2626       return true;
2627     }
2628
2629   /* This is a reference to a symbol defined by a dynamic object which
2630      is not a function.  */
2631
2632   /* If we are creating a shared library, we must presume that the
2633      only references to the symbol are via the global offset table.
2634      For such cases we need not do anything here; the relocations will
2635      be handled correctly by relocate_section.  */
2636   if (info->shared)
2637     return true;
2638
2639   /* If there are no references to this symbol that do not use the
2640      GOT, we don't need to generate a copy reloc.  */
2641   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2642     return true;
2643
2644   eh = (struct ppc_link_hash_entry *) h;
2645   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2646     {
2647       s = p->sec->output_section;
2648       if (s != NULL && (s->flags & SEC_READONLY) != 0)
2649         break;
2650     }
2651
2652   /* If we didn't find any dynamic relocs in read-only sections, then
2653      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
2654   if (p == NULL)
2655     {
2656       h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
2657       return true;
2658     }
2659
2660   /* We must allocate the symbol in our .dynbss section, which will
2661      become part of the .bss section of the executable.  There will be
2662      an entry for this symbol in the .dynsym section.  The dynamic
2663      object will contain position independent code, so all references
2664      from the dynamic object to this symbol will go through the global
2665      offset table.  The dynamic linker will use the .dynsym entry to
2666      determine the address it must put in the global offset table, so
2667      both the dynamic object and the regular object will refer to the
2668      same memory location for the variable.  */
2669
2670   /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
2671      copy the initial value out of the dynamic object and into the
2672      runtime process image.  We need to remember the offset into the
2673      .rela.bss section we are going to use.  */
2674   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2675     {
2676       htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
2677       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2678     }
2679
2680   /* We need to figure out the alignment required for this symbol.  I
2681      have no idea how ELF linkers handle this.  */
2682   power_of_two = bfd_log2 (h->size);
2683   if (power_of_two > 4)
2684     power_of_two = 4;
2685
2686   /* Apply the required alignment.  */
2687   s = htab->sdynbss;
2688   s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
2689   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
2690     {
2691       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
2692         return false;
2693     }
2694
2695   /* Define the symbol as being at this point in the section.  */
2696   h->root.u.def.section = s;
2697   h->root.u.def.value = s->_raw_size;
2698
2699   /* Increment the section size to make room for the symbol.  */
2700   s->_raw_size += h->size;
2701
2702   return true;
2703 }
2704
2705 /* If given a function descriptor symbol, hide both the function code
2706    sym and the descriptor.  */
2707 static void
2708 ppc64_elf_hide_symbol (info, h, force_local)
2709      struct bfd_link_info *info;
2710      struct elf_link_hash_entry *h;
2711      boolean force_local;
2712 {
2713   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
2714
2715   if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
2716     {
2717       const char *name;
2718       struct elf_link_hash_entry *fh;
2719       struct ppc_link_hash_table *htab;
2720
2721       name = h->root.root.string - 1;
2722       htab = ppc_hash_table (info);
2723       fh = elf_link_hash_lookup (&htab->elf, name, false, false, false);
2724       if (fh != NULL)
2725         _bfd_elf_link_hash_hide_symbol (info, fh, force_local);
2726     }
2727 }
2728
2729 /* This is the condition under which ppc64_elf_finish_dynamic_symbol
2730    will be called from elflink.h.  If elflink.h doesn't call our
2731    finish_dynamic_symbol routine, we'll need to do something about
2732    initializing any .plt and .got entries in ppc64_elf_relocate_section.  */
2733 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
2734   ((DYN)                                                                \
2735    && ((INFO)->shared                                                   \
2736        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)      \
2737    && ((H)->dynindx != -1                                               \
2738        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
2739
2740 /* Allocate space in .plt, .got and associated reloc sections for
2741    dynamic relocs.  */
2742
2743 static boolean
2744 allocate_dynrelocs (h, inf)
2745      struct elf_link_hash_entry *h;
2746      PTR inf;
2747 {
2748   struct bfd_link_info *info;
2749   struct ppc_link_hash_table *htab;
2750   asection *s;
2751   struct ppc_link_hash_entry *eh;
2752   struct ppc_dyn_relocs *p;
2753
2754   if (h->root.type == bfd_link_hash_indirect
2755       || h->root.type == bfd_link_hash_warning)
2756     return true;
2757
2758   info = (struct bfd_link_info *) inf;
2759   htab = ppc_hash_table (info);
2760
2761   if (htab->elf.dynamic_sections_created
2762       && h->plt.refcount > 0
2763       && h->dynindx != -1)
2764     {
2765       BFD_ASSERT (((struct ppc_link_hash_entry *) h)->is_func_descriptor);
2766
2767       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
2768         {
2769           /* If this is the first .plt entry, make room for the special
2770              first entry.  */
2771           s = htab->splt;
2772           if (s->_raw_size == 0)
2773             s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
2774
2775           h->plt.offset = s->_raw_size;
2776
2777           /* Make room for this entry.  */
2778           s->_raw_size += PLT_ENTRY_SIZE;
2779
2780           /* Make room for the .stub and .glink code.  */
2781           s = htab->sstub;
2782           s->_raw_size += PLT_CALL_STUB_SIZE;
2783
2784           s = htab->sglink;
2785           if (s->_raw_size == 0)
2786             s->_raw_size += GLINK_CALL_STUB_SIZE;
2787           /* We need bigger stubs past index 32767.  */
2788           if (s->_raw_size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
2789             s->_raw_size += 4;
2790           s->_raw_size += 2*4;
2791
2792           /* We also need to make an entry in the .rela.plt section.  */
2793           s = htab->srelplt;
2794           s->_raw_size += sizeof (Elf64_External_Rela);
2795         }
2796       else
2797         {
2798           h->plt.offset = (bfd_vma) -1;
2799           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2800         }
2801     }
2802   else
2803     {
2804       h->plt.offset = (bfd_vma) -1;
2805       h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2806     }
2807
2808   if (h->got.refcount > 0)
2809     {
2810       boolean dyn;
2811
2812       /* Make sure this symbol is output as a dynamic symbol.
2813          Undefined weak syms won't yet be marked as dynamic.  */
2814       if (h->dynindx == -1
2815           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2816         {
2817           if (! bfd_elf64_link_record_dynamic_symbol (info, h))
2818             return false;
2819         }
2820
2821       s = htab->sgot;
2822       h->got.offset = s->_raw_size;
2823       s->_raw_size += 8;
2824       dyn = htab->elf.dynamic_sections_created;
2825       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
2826         htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
2827     }
2828   else
2829     h->got.offset = (bfd_vma) -1;
2830
2831   eh = (struct ppc_link_hash_entry *) h;
2832   if (eh->dyn_relocs == NULL)
2833     return true;
2834
2835   /* In the shared -Bsymbolic case, discard space allocated for
2836      dynamic pc-relative relocs against symbols which turn out to be
2837      defined in regular objects.  For the normal shared case, discard
2838      space for relocs that have become local due to symbol visibility
2839      changes.  */
2840
2841   if (info->shared)
2842     {
2843       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2844           && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2845               || info->symbolic))
2846         {
2847           struct ppc_dyn_relocs **pp;
2848
2849           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2850             {
2851               p->count -= p->pc_count;
2852               p->pc_count = 0;
2853               if (p->count == 0)
2854                 *pp = p->next;
2855               else
2856                 pp = &p->next;
2857             }
2858         }
2859     }
2860   else
2861     {
2862       /* For the non-shared case, discard space for relocs against
2863          symbols which turn out to need copy relocs or are not
2864          dynamic.  */
2865
2866       if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2867           && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2868                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2869               || (htab->elf.dynamic_sections_created
2870                   && (h->root.type == bfd_link_hash_undefweak
2871                       || h->root.type == bfd_link_hash_undefined))))
2872         {
2873           /* Make sure this symbol is output as a dynamic symbol.
2874              Undefined weak syms won't yet be marked as dynamic.  */
2875           if (h->dynindx == -1
2876               && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2877             {
2878               if (! bfd_elf64_link_record_dynamic_symbol (info, h))
2879                 return false;
2880             }
2881
2882           /* If that succeeded, we know we'll be keeping all the
2883              relocs.  */
2884           if (h->dynindx != -1)
2885             goto keep;
2886         }
2887
2888       eh->dyn_relocs = NULL;
2889
2890     keep: ;
2891     }
2892
2893   /* Finally, allocate space.  */
2894   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2895     {
2896       asection *sreloc = elf_section_data (p->sec)->sreloc;
2897       sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
2898     }
2899
2900   return true;
2901 }
2902
2903 /* Find any dynamic relocs that apply to read-only sections.  */
2904
2905 static boolean
2906 readonly_dynrelocs (h, inf)
2907      struct elf_link_hash_entry *h;
2908      PTR inf;
2909 {
2910   struct ppc_link_hash_entry *eh;
2911   struct ppc_dyn_relocs *p;
2912
2913   eh = (struct ppc_link_hash_entry *) h;
2914   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2915     {
2916       asection *s = p->sec->output_section;
2917
2918       if (s != NULL && (s->flags & SEC_READONLY) != 0)
2919         {
2920           struct bfd_link_info *info = (struct bfd_link_info *) inf;
2921
2922           info->flags |= DF_TEXTREL;
2923
2924           /* Not an error, just cut short the traversal.  */
2925           return false;
2926         }
2927     }
2928   return true;
2929 }
2930
2931 /* Set the sizes of the dynamic sections.  */
2932
2933 static boolean
2934 ppc64_elf_size_dynamic_sections (output_bfd, info)
2935      bfd *output_bfd ATTRIBUTE_UNUSED;
2936      struct bfd_link_info *info;
2937 {
2938   struct ppc_link_hash_table *htab;
2939   bfd *dynobj;
2940   asection *s;
2941   boolean relocs;
2942   bfd *ibfd;
2943
2944   htab = ppc_hash_table (info);
2945   dynobj = htab->elf.dynobj;
2946   if (dynobj == NULL)
2947     abort ();
2948
2949   if (htab->elf.dynamic_sections_created)
2950     {
2951       /* Set the contents of the .interp section to the interpreter.  */
2952       if (! info->shared)
2953         {
2954           s = bfd_get_section_by_name (dynobj, ".interp");
2955           if (s == NULL)
2956             abort ();
2957           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2958           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2959         }
2960     }
2961
2962   /* Set up .got offsets for local syms, and space for local dynamic
2963      relocs.  */
2964   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2965     {
2966       bfd_signed_vma *local_got;
2967       bfd_signed_vma *end_local_got;
2968       bfd_size_type locsymcount;
2969       Elf_Internal_Shdr *symtab_hdr;
2970       asection *srel;
2971
2972       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2973         continue;
2974
2975       for (s = ibfd->sections; s != NULL; s = s->next)
2976         {
2977           struct ppc_dyn_relocs *p;
2978
2979           for (p = *((struct ppc_dyn_relocs **)
2980                      &elf_section_data (s)->local_dynrel);
2981                p != NULL;
2982                p = p->next)
2983             {
2984               if (!bfd_is_abs_section (p->sec)
2985                   && bfd_is_abs_section (p->sec->output_section))
2986                 {
2987                   /* Input section has been discarded, either because
2988                      it is a copy of a linkonce section or due to
2989                      linker script /DISCARD/, so we'll be discarding
2990                      the relocs too.  */
2991                 }
2992               else
2993                 {
2994                   srel = elf_section_data (p->sec)->sreloc;
2995                   srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
2996                 }
2997             }
2998         }
2999
3000       local_got = elf_local_got_refcounts (ibfd);
3001       if (!local_got)
3002         continue;
3003
3004       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3005       locsymcount = symtab_hdr->sh_info;
3006       end_local_got = local_got + locsymcount;
3007       s = htab->sgot;
3008       srel = htab->srelgot;
3009       for (; local_got < end_local_got; ++local_got)
3010         {
3011           if (*local_got > 0)
3012             {
3013               *local_got = s->_raw_size;
3014               s->_raw_size += 8;
3015               if (info->shared)
3016                 srel->_raw_size += sizeof (Elf64_External_Rela);
3017             }
3018           else
3019             *local_got = (bfd_vma) -1;
3020         }
3021     }
3022
3023   /* Allocate global sym .plt and .got entries, and space for global
3024      sym dynamic relocs.  */
3025   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
3026
3027   /* We now have determined the sizes of the various dynamic sections.
3028      Allocate memory for them.  */
3029   relocs = false;
3030   for (s = dynobj->sections; s != NULL; s = s->next)
3031     {
3032       bfd_vma size;
3033
3034       if ((s->flags & SEC_LINKER_CREATED) == 0)
3035         continue;
3036
3037       if (s == htab->splt
3038           || s == htab->sgot
3039           || s == htab->sstub
3040           || s == htab->sglink)
3041         {
3042           /* Strip this section if we don't need it; see the
3043              comment below.  */
3044         }
3045       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
3046         {
3047           if (s->_raw_size == 0)
3048             {
3049               /* If we don't need this section, strip it from the
3050                  output file.  This is mostly to handle .rela.bss and
3051                  .rela.plt.  We must create both sections in
3052                  create_dynamic_sections, because they must be created
3053                  before the linker maps input sections to output
3054                  sections.  The linker does that before
3055                  adjust_dynamic_symbol is called, and it is that
3056                  function which decides whether anything needs to go
3057                  into these sections.  */
3058             }
3059           else
3060             {
3061               if (s != htab->srelplt)
3062                 relocs = true;
3063
3064               /* We use the reloc_count field as a counter if we need
3065                  to copy relocs into the output file.  */
3066               s->reloc_count = 0;
3067             }
3068         }
3069       else
3070         {
3071           /* It's not one of our sections, so don't allocate space.  */
3072           continue;
3073         }
3074
3075       if (s->_raw_size == 0)
3076         {
3077           _bfd_strip_section_from_output (info, s);
3078           continue;
3079         }
3080
3081       /* Allocate memory for the section contents.  We use bfd_zalloc
3082          here in case unused entries are not reclaimed before the
3083          section's contents are written out.  This should not happen,
3084          but this way if it does, we get a R_PPC64_NONE reloc instead
3085          of garbage.  */
3086       size = s->_raw_size;
3087       if (s == htab->sstub)
3088         {
3089           /* .stub may grow.  Allocate enough for the maximum growth.  */
3090           size += (size + 65536 + 65535) / 65536 * 4;
3091         }
3092       s->contents = (bfd_byte *) bfd_zalloc (dynobj, size);
3093       if (s->contents == NULL)
3094         return false;
3095     }
3096
3097   if (htab->elf.dynamic_sections_created)
3098     {
3099       /* Add some entries to the .dynamic section.  We fill in the
3100          values later, in ppc64_elf_finish_dynamic_sections, but we
3101          must add the entries now so that we get the correct size for
3102          the .dynamic section.  The DT_DEBUG entry is filled in by the
3103          dynamic linker and used by the debugger.  */
3104 #define add_dynamic_entry(TAG, VAL) \
3105   bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
3106
3107       if (!info->shared)
3108         {
3109           if (!add_dynamic_entry (DT_DEBUG, 0))
3110             return false;
3111         }
3112
3113       if (htab->splt->_raw_size != 0)
3114         {
3115           if (!add_dynamic_entry (DT_PLTGOT, 0)
3116               || !add_dynamic_entry (DT_PLTRELSZ, 0)
3117               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3118               || !add_dynamic_entry (DT_JMPREL, 0)
3119               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
3120             return false;
3121         }
3122
3123       if (relocs)
3124         {
3125           if (!add_dynamic_entry (DT_RELA, 0)
3126               || !add_dynamic_entry (DT_RELASZ, 0)
3127               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
3128             return false;
3129
3130           /* If any dynamic relocs apply to a read-only section,
3131              then we need a DT_TEXTREL entry.  */
3132           elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, (PTR) info);
3133
3134           if ((info->flags & DF_TEXTREL) != 0)
3135             {
3136               if (!add_dynamic_entry (DT_TEXTREL, 0))
3137                 return false;
3138             }
3139         }
3140     }
3141 #undef add_dynamic_entry
3142
3143   return true;
3144 }
3145
3146 /* Called after we have seen all the input files/sections, but before
3147    final symbol resolution and section placement has been determined.
3148
3149    We use this hook to provide a value for TOCstart, which we store in
3150    the output bfd elf_gp.  */
3151
3152 boolean
3153 ppc64_elf_set_toc (obfd, info)
3154      bfd *obfd;
3155      struct bfd_link_info *info;
3156 {
3157   if (!info->relocateable)
3158     {
3159       asection *s;
3160       bfd_vma TOCstart;
3161
3162       /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
3163          order.  The TOC starts where the first of these sections starts.  */
3164       s = bfd_get_section_by_name (obfd, ".got");
3165       if (s == NULL)
3166         s = bfd_get_section_by_name (obfd, ".toc");
3167       if (s == NULL)
3168         s = bfd_get_section_by_name (obfd, ".tocbss");
3169       if (s == NULL)
3170         s = bfd_get_section_by_name (obfd, ".plt");
3171       if (s == NULL)
3172         {
3173           /* This may happen for
3174              o  references to TOC base (SYM@toc / TOC[tc0]) without a
3175              .toc directive
3176              o  bad linker script
3177              o --gc-sections and empty TOC sections
3178
3179              FIXME: Warn user?  */
3180
3181           /* Look for a likely section.  We probably won't even be
3182              using TOCstart.  */
3183           for (s = obfd->sections; s != NULL; s = s->next)
3184             if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
3185                 == (SEC_ALLOC | SEC_SMALL_DATA))
3186               break;
3187           if (s == NULL)
3188             for (s = obfd->sections; s != NULL; s = s->next)
3189               if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
3190                   == (SEC_ALLOC | SEC_SMALL_DATA))
3191                 break;
3192           if (s == NULL)
3193             for (s = obfd->sections; s != NULL; s = s->next)
3194               if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
3195                 break;
3196           if (s == NULL)
3197             for (s = obfd->sections; s != NULL; s = s->next)
3198               if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
3199                 break;
3200         }
3201
3202       TOCstart = 0;
3203       if (s != NULL)
3204         TOCstart = s->output_section->vma + s->output_offset;
3205
3206       elf_gp (obfd) = TOCstart;
3207     }
3208   return true;
3209 }
3210
3211 /* PowerPC64 .plt entries are 24 bytes long, which doesn't divide
3212    evenly into 64k.  Sometimes with a large enough .plt, we'll need to
3213    use offsets differing in the high 16 bits when accessing a .plt
3214    entry from a .plt call stub.  This function adjusts the size of
3215    .stub to accommodate the extra stub instruction needed in such
3216    cases.  */
3217
3218 boolean
3219 ppc64_elf_size_stubs (obfd, info, changed)
3220      bfd *obfd;
3221      struct bfd_link_info *info;
3222      int *changed;
3223 {
3224   struct ppc_link_hash_table *htab = ppc_hash_table (info);
3225   bfd_vma plt_offset, next_64k;
3226   long base, num, extra;
3227
3228   /* .plt and .stub should be both present, or both absent.  */
3229   if ((htab->splt == NULL || htab->splt->_raw_size == 0)
3230       != (htab->sstub == NULL || htab->sstub->_raw_size == 0))
3231     abort ();
3232
3233   /* If no .plt, then nothing to do.  */
3234   if (htab->splt == NULL || htab->splt->_raw_size == 0)
3235     return true;
3236
3237   plt_offset = (htab->splt->output_section->vma
3238                 + htab->splt->output_offset
3239                 - elf_gp (obfd));
3240   next_64k = (plt_offset + 65535) & -65536;
3241
3242   /* If the .plt doesn't have any entries crossing a 64k boundary,
3243      then there is no need for bigger stubs.  */
3244   if (next_64k <= plt_offset + htab->splt->_raw_size)
3245     return true;
3246
3247   /* OK, so we have at least one transition.  Since .plt entries are
3248      24 bytes long, we'll strike it lucky every 3*64k, with the 64k
3249      boundary between .plt entries.  */
3250   base = next_64k / 65536;
3251   num = (plt_offset + htab->splt->_raw_size - next_64k) / 65536;
3252   extra = (base % 3 + num + 1) * 2 / 3;
3253
3254   /* Allow one extra instruction for each EXTRA.  The change in .stub
3255      may change the location of .toc and .plt.  .toc and .plt ought to
3256      move as a group, but someone might be playing with eg. .plt
3257      alignment, so don't allow .stub size to decrease.  */
3258   if (htab->sstub->_cooked_size < htab->sstub->_raw_size + extra * 4)
3259     {
3260       htab->sstub->_cooked_size = htab->sstub->_raw_size + extra * 4;
3261       *changed = true;
3262     }
3263   return true;
3264 }
3265
3266 /* Build a .plt call stub.  */
3267
3268 static bfd_byte *
3269 build_plt_stub (obfd, p, offset, glink)
3270      bfd *obfd;
3271      bfd_byte *p;
3272      int offset;
3273      int glink;
3274 {
3275 #define PPC_LO(v) ((v) & 0xffff)
3276 #define PPC_HI(v) (((v) >> 16) & 0xffff)
3277 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
3278
3279   if (glink)
3280     bfd_put_32 (obfd, LD_R2_40R1, p),                   p += 4;
3281   bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
3282   if (!glink)
3283     bfd_put_32 (obfd, STD_R2_40R1, p),                  p += 4;
3284   bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),  p += 4;
3285   if (PPC_HA (offset + 8) != PPC_HA (offset))
3286     bfd_put_32 (obfd, ADDIS_R12_R12_1, p),              p += 4;
3287   offset += 8;
3288   bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p),   p += 4;
3289   if (PPC_HA (offset + 8) != PPC_HA (offset))
3290     bfd_put_32 (obfd, ADDIS_R12_R12_1, p),              p += 4;
3291   offset += 8;
3292   bfd_put_32 (obfd, MTCTR_R11, p),                      p += 4;
3293   bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),  p += 4;
3294   bfd_put_32 (obfd, BCTR, p),                           p += 4;
3295   return p;
3296 }
3297
3298 /* Build the stubs for one function call.  */
3299
3300 static boolean
3301 build_one_stub (h, inf)
3302      struct elf_link_hash_entry *h;
3303      PTR inf;
3304 {
3305   struct bfd_link_info *info;
3306   struct ppc_link_hash_table *htab;
3307
3308   if (h->root.type == bfd_link_hash_indirect
3309       || h->root.type == bfd_link_hash_warning)
3310     return true;
3311
3312   info = (struct bfd_link_info *) inf;
3313   htab = ppc_hash_table (info);
3314
3315   if (htab->elf.dynamic_sections_created
3316       && h->plt.offset != (bfd_vma) -1)
3317     {
3318       struct elf_link_hash_entry *fh;
3319       asection *s;
3320       bfd_vma plt_r2;
3321       bfd_byte *p;
3322       unsigned int indx;
3323
3324       BFD_ASSERT (((struct ppc_link_hash_entry *) h)->is_func_descriptor);
3325
3326       fh = elf_link_hash_lookup (&htab->elf, h->root.root.string - 1,
3327                                  false, false, true);
3328
3329       if (fh == NULL)
3330         abort ();
3331
3332       BFD_ASSERT (((struct ppc_link_hash_entry *) fh)->is_func);
3333
3334       /* Point the function at the linkage stub.  This works because
3335          the only references to the function code sym are calls.
3336          Function pointer comparisons use the function descriptor.  */
3337       s = htab->sstub;
3338       fh->root.type = bfd_link_hash_defined;
3339       fh->root.u.def.section = s;
3340       fh->root.u.def.value = s->_cooked_size;
3341
3342       /* Build the .plt call stub.  */
3343       plt_r2 = (htab->splt->output_section->vma
3344                 + htab->splt->output_offset
3345                 + h->plt.offset
3346                 - elf_gp (htab->splt->output_section->owner)
3347                 - TOC_BASE_OFF);
3348
3349       if (plt_r2 + 0x80000000 > 0xffffffff
3350           || (plt_r2 & 3) != 0)
3351         {
3352           (*_bfd_error_handler)
3353             (_("linkage table error against `%s'"),
3354              h->root.root.string);
3355           bfd_set_error (bfd_error_bad_value);
3356           htab->plt_overflow = true;
3357           return false;
3358         }
3359       p = s->contents + s->_cooked_size;
3360       p = build_plt_stub (s->owner, p, (int) plt_r2, 0);
3361       s->_cooked_size = p - s->contents;
3362
3363       /* Build the .glink lazy link call stub.  */
3364       s = htab->sglink;
3365       p = s->contents + s->_cooked_size;
3366       indx = s->reloc_count;
3367       if (indx < 0x8000)
3368         {
3369           bfd_put_32 (s->owner, LI_R0_0 | indx, p);
3370           p += 4;
3371         }
3372       else
3373         {
3374           bfd_put_32 (s->owner, LIS_R0_0 | PPC_HI (indx), p);
3375           p += 4;
3376           bfd_put_32 (s->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
3377           p += 4;
3378         }
3379       bfd_put_32 (s->owner, B_DOT | ((s->contents - p) & 0x3fffffc), p);
3380       p += 4;
3381       s->_cooked_size = p - s->contents;
3382       s->reloc_count += 1;
3383     }
3384   return true;
3385 }
3386
3387 boolean
3388 ppc64_elf_build_stubs (obfd, info)
3389      bfd *obfd;
3390      struct bfd_link_info *info;
3391 {
3392   struct ppc_link_hash_table *htab = ppc_hash_table (info);
3393   bfd_vma old_stub_size;
3394   bfd_vma plt_r2;
3395   bfd_byte *p;
3396
3397   /* If no .plt stubs, then nothing to do.  */
3398   if (htab->sstub == NULL || htab->sstub->_raw_size == 0)
3399     return true;
3400
3401   old_stub_size = htab->sstub->_cooked_size;
3402   htab->sstub->_cooked_size = 0;
3403
3404   /* Build the .glink plt call stub.  */
3405   plt_r2 = (htab->splt->output_section->vma
3406             + htab->splt->output_offset
3407             - elf_gp (obfd)
3408             - TOC_BASE_OFF);
3409   p = htab->sglink->contents;
3410   p = build_plt_stub (htab->sglink->owner, p, (int) plt_r2, 1);
3411   while (p - htab->sglink->contents < GLINK_CALL_STUB_SIZE)
3412     {
3413       bfd_put_32 (htab->sglink->owner, NOP, p);
3414       p += 4;
3415     }
3416   htab->sglink->_cooked_size = p - htab->sglink->contents;
3417
3418   /* Use reloc_count to count entries.  */
3419   htab->sglink->reloc_count = 0;
3420
3421   elf_link_hash_traverse (&htab->elf, build_one_stub, (PTR) info);
3422   htab->sglink->reloc_count = 0;
3423
3424   if (htab->plt_overflow)
3425     return false;
3426
3427   if (old_stub_size != htab->sstub->_cooked_size
3428       || htab->sglink->_raw_size != htab->sglink->_cooked_size)
3429     {
3430       (*_bfd_error_handler)
3431         (_("stub section size doesn't match calculated size"));
3432       bfd_set_error (bfd_error_bad_value);
3433       return false;
3434     }
3435   return true;
3436 }
3437
3438 /* Set up any other section flags and such that may be necessary.  */
3439
3440 static boolean
3441 ppc64_elf_fake_sections (abfd, shdr, asect)
3442      bfd *abfd ATTRIBUTE_UNUSED;
3443      Elf64_Internal_Shdr *shdr;
3444      asection *asect;
3445 {
3446   if ((asect->flags & SEC_EXCLUDE) != 0)
3447     shdr->sh_flags |= SHF_EXCLUDE;
3448
3449   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
3450     shdr->sh_type = SHT_ORDERED;
3451
3452   return true;
3453 }
3454
3455 /* The RELOCATE_SECTION function is called by the ELF backend linker
3456    to handle the relocations for a section.
3457
3458    The relocs are always passed as Rela structures; if the section
3459    actually uses Rel structures, the r_addend field will always be
3460    zero.
3461
3462    This function is responsible for adjust the section contents as
3463    necessary, and (if using Rela relocs and generating a
3464    relocateable output file) adjusting the reloc addend as
3465    necessary.
3466
3467    This function does not have to worry about setting the reloc
3468    address or the reloc symbol index.
3469
3470    LOCAL_SYMS is a pointer to the swapped in local symbols.
3471
3472    LOCAL_SECTIONS is an array giving the section in the input file
3473    corresponding to the st_shndx field of each local symbol.
3474
3475    The global hash table entry for the global symbols can be found
3476    via elf_sym_hashes (input_bfd).
3477
3478    When generating relocateable output, this function must handle
3479    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
3480    going to be the section symbol corresponding to the output
3481    section, which means that the addend must be adjusted
3482    accordingly.  */
3483
3484 static boolean
3485 ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
3486                             contents, relocs, local_syms, local_sections)
3487      bfd *output_bfd;
3488      struct bfd_link_info *info;
3489      bfd *input_bfd;
3490      asection *input_section;
3491      bfd_byte *contents;
3492      Elf_Internal_Rela *relocs;
3493      Elf_Internal_Sym *local_syms;
3494      asection **local_sections;
3495 {
3496   struct ppc_link_hash_table *htab;
3497   Elf_Internal_Shdr *symtab_hdr;
3498   struct elf_link_hash_entry **sym_hashes;
3499   Elf_Internal_Rela *rel;
3500   Elf_Internal_Rela *relend;
3501   bfd_vma *local_got_offsets;
3502   bfd_vma TOCstart;
3503   boolean ret = true;
3504
3505   /* Initialize howto table if needed.  */
3506   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
3507     ppc_howto_init ();
3508
3509   htab = ppc_hash_table (info);
3510   local_got_offsets = elf_local_got_offsets (input_bfd);
3511   TOCstart = elf_gp (output_bfd);
3512   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3513   sym_hashes = elf_sym_hashes (input_bfd);
3514
3515   rel = relocs;
3516   relend = relocs + input_section->reloc_count;
3517   for (; rel < relend; rel++)
3518     {
3519       enum elf_ppc_reloc_type r_type;
3520       bfd_vma offset;
3521       bfd_vma addend;
3522       bfd_reloc_status_type r;
3523       Elf_Internal_Sym *sym;
3524       asection *sec;
3525       struct elf_link_hash_entry *h;
3526       const char *sym_name;
3527       unsigned long r_symndx;
3528       bfd_vma relocation;
3529       boolean unresolved_reloc;
3530       boolean has_nop;
3531       long insn;
3532
3533       r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
3534       r_symndx = ELF64_R_SYM (rel->r_info);
3535
3536       if (info->relocateable)
3537         {
3538           /* This is a relocatable link.  We don't have to change
3539              anything, unless the reloc is against a section symbol,
3540              in which case we have to adjust according to where the
3541              section symbol winds up in the output section.  */
3542           if (r_symndx < symtab_hdr->sh_info)
3543             {
3544               sym = local_syms + r_symndx;
3545               if ((unsigned) ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3546                 {
3547                   sec = local_sections[r_symndx];
3548                   rel->r_addend += sec->output_offset + sym->st_value;
3549                 }
3550             }
3551           continue;
3552         }
3553
3554       /* This is a final link.  */
3555
3556       offset = rel->r_offset;
3557       addend = rel->r_addend;
3558       r = bfd_reloc_other;
3559       sym = (Elf_Internal_Sym *) 0;
3560       sec = (asection *) 0;
3561       h = (struct elf_link_hash_entry *) 0;
3562       sym_name = (const char *) 0;
3563       unresolved_reloc = false;
3564
3565       if (r_type == R_PPC64_TOC)
3566         {
3567           /* Relocation value is TOC base.  Symbol is ignored.  */
3568           relocation = TOCstart + TOC_BASE_OFF;
3569         }
3570       else if (r_symndx < symtab_hdr->sh_info)
3571         {
3572           /* It's a local symbol.  */
3573           sym = local_syms + r_symndx;
3574           sec = local_sections[r_symndx];
3575           sym_name = "<local symbol>";
3576
3577           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
3578           addend = rel->r_addend;
3579         }
3580       else
3581         {
3582           /* It's a global symbol.  */
3583           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3584           while (h->root.type == bfd_link_hash_indirect
3585                  || h->root.type == bfd_link_hash_warning)
3586             h = (struct elf_link_hash_entry *) h->root.u.i.link;
3587           sym_name = h->root.root.string;
3588           relocation = 0;
3589           if (h->root.type == bfd_link_hash_defined
3590               || h->root.type == bfd_link_hash_defweak)
3591             {
3592               sec = h->root.u.def.section;
3593               if (sec->output_section == NULL)
3594                 /* Set a flag that will be cleared later if we find a
3595                    relocation value for this symbol.  output_section
3596                    is typically NULL for symbols satisfied by a shared
3597                    library.  */
3598                 unresolved_reloc = true;
3599               else
3600                 relocation = (h->root.u.def.value
3601                               + sec->output_section->vma
3602                               + sec->output_offset);
3603             }
3604           else if (h->root.type == bfd_link_hash_undefweak)
3605             ;
3606           else if (info->shared
3607                    && (!info->symbolic || info->allow_shlib_undefined)
3608                    && !info->no_undefined
3609                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3610             ;
3611           else
3612             {
3613               if (! ((*info->callbacks->undefined_symbol)
3614                      (info, h->root.root.string, input_bfd, input_section,
3615                       offset, (!info->shared
3616                                || info->no_undefined
3617                                || ELF_ST_VISIBILITY (h->other)))))
3618                 return false;
3619               relocation = 0;
3620             }
3621         }
3622
3623       /* First handle relocations that tweak non-addend part of insn.  */
3624       insn = 0;
3625       switch (r_type)
3626         {
3627         default:
3628           break;
3629
3630           /* Branch taken prediction relocations.  */
3631         case R_PPC64_ADDR14_BRTAKEN:
3632         case R_PPC64_REL14_BRTAKEN:
3633           insn = 0x01 << 21; /* Set 't' bit, lowest bit of BO field. */
3634           /* Fall thru. */
3635
3636           /* Branch not taken prediction relocations.  */
3637         case R_PPC64_ADDR14_BRNTAKEN:
3638         case R_PPC64_REL14_BRNTAKEN:
3639           insn |= bfd_get_32 (output_bfd, contents + offset) & ~(0x01 << 21);
3640           /* Set 'a' bit.  This is 0b00010 in BO field for branch on CR(BI)
3641              insns (BO == 001at or 011at), and 0b01000 for branch on CTR
3642              insns (BO == 1a00t or 1a01t).  */
3643           if ((insn & (0x14 << 21)) == (0x04 << 21))
3644             insn |= 0x02 << 21;
3645           else if ((insn & (0x14 << 21)) == (0x10 << 21))
3646             insn |= 0x08 << 21;
3647           else
3648             break;
3649
3650           bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
3651           break;
3652
3653         case R_PPC64_REL24:
3654         case R_PPC64_ADDR24:
3655           /* An ADDR24 or REL24 branching to a linkage function may be
3656              followed by a nop that we have to replace with a ld in
3657              order to restore the TOC base pointer.  Only calls to
3658              shared objects need to alter the TOC base.  These are
3659              recognized by their need for a PLT entry.  */
3660           has_nop = 0;
3661           if (h != NULL
3662               && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3663               /* Make sure that there really is an instruction after
3664                  the branch that we can decode.  */
3665               && offset + 8 <= input_section->_cooked_size)
3666             {
3667               bfd_byte *pnext;
3668
3669               pnext = contents + offset + 4;
3670               insn = bfd_get_32 (input_bfd, pnext);
3671
3672               if (insn == 0x60000000     /* nop (ori  r0,r0,0) */
3673                   || insn == 0x4def7b82  /* cror 15,15,15 */
3674                   || insn == 0x4ffffb82) /* cror 31,31,31 */
3675                 {
3676                   bfd_put_32 (input_bfd,
3677                               (bfd_vma) 0xe8410028, /* ld r2,40(r1) */
3678                               pnext);
3679                   has_nop = 1;
3680                 }
3681             }
3682
3683           if (h != NULL
3684               && h->root.type == bfd_link_hash_undefweak
3685               && r_type == R_PPC64_REL24
3686               && addend == 0
3687               && relocation == 0)
3688             {
3689               /* Tweak calls to undefined weak functions to behave as
3690                  if the "called" function immediately returns.  We can
3691                  thus call to a weak function without first checking
3692                  whether the function is defined.  */
3693               relocation = 4;
3694               if (has_nop)
3695                 relocation = 8;
3696             }
3697           break;
3698         }
3699
3700       /* Set `addend'.  */
3701       switch (r_type)
3702         {
3703         default:
3704           (*_bfd_error_handler)
3705             (_("%s: unknown relocation type %d for symbol %s"),
3706              bfd_archive_filename (input_bfd), (int) r_type, sym_name);
3707
3708           bfd_set_error (bfd_error_bad_value);
3709           ret = false;
3710           continue;
3711
3712         case R_PPC64_NONE:
3713         case R_PPC_GNU_VTINHERIT:
3714         case R_PPC_GNU_VTENTRY:
3715           continue;
3716
3717           /* GOT16 relocations.  Like an ADDR16 using the symbol's
3718              address in the GOT as relocation value instead of the
3719              symbols value itself.  Also, create a GOT entry for the
3720              symbol and put the symbol value there.  */
3721         case R_PPC64_GOT16:
3722         case R_PPC64_GOT16_LO:
3723         case R_PPC64_GOT16_HI:
3724         case R_PPC64_GOT16_HA:
3725         case R_PPC64_GOT16_DS:
3726         case R_PPC64_GOT16_LO_DS:
3727           {
3728             /* Relocation is to the entry for this symbol in the global
3729                offset table.  */
3730             bfd_vma off;
3731
3732             if (htab->sgot == NULL)
3733               abort ();
3734
3735             if (h != NULL)
3736               {
3737                 boolean dyn;
3738
3739                 off = h->got.offset;
3740                 dyn = htab->elf.dynamic_sections_created;
3741                 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
3742                     || (info->shared
3743                         && (info->symbolic
3744                             || h->dynindx == -1
3745                             || (h->elf_link_hash_flags
3746                                 & ELF_LINK_FORCED_LOCAL))
3747                         && (h->elf_link_hash_flags
3748                             & ELF_LINK_HASH_DEF_REGULAR)))
3749                   {
3750                     /* This is actually a static link, or it is a
3751                        -Bsymbolic link and the symbol is defined
3752                        locally, or the symbol was forced to be local
3753                        because of a version file.  We must initialize
3754                        this entry in the global offset table.  Since the
3755                        offset must always be a multiple of 8, we use the
3756                        least significant bit to record whether we have
3757                        initialized it already.
3758
3759                        When doing a dynamic link, we create a .rel.got
3760                        relocation entry to initialize the value.  This
3761                        is done in the finish_dynamic_symbol routine.  */
3762                     if ((off & 1) != 0)
3763                       off &= ~1;
3764                     else
3765                       {
3766                         bfd_put_64 (output_bfd, relocation,
3767                                     htab->sgot->contents + off);
3768                         h->got.offset |= 1;
3769                       }
3770                   }
3771                 else
3772                   unresolved_reloc = false;
3773               }
3774             else
3775               {
3776                 if (local_got_offsets == NULL)
3777                   abort ();
3778
3779                 off = local_got_offsets[r_symndx];
3780
3781                 /* The offset must always be a multiple of 8.  We use
3782                    the least significant bit to record whether we have
3783                    already processed this entry.  */
3784                 if ((off & 1) != 0)
3785                   off &= ~1;
3786                 else
3787                   {
3788                     bfd_put_64 (output_bfd, relocation,
3789                                 htab->sgot->contents + off);
3790
3791                     if (info->shared)
3792                       {
3793                         Elf_Internal_Rela outrel;
3794                         Elf64_External_Rela *loc;
3795
3796                         /* We need to generate a R_PPC64_RELATIVE reloc
3797                            for the dynamic linker.  */
3798                         outrel.r_offset = (htab->sgot->output_section->vma
3799                                            + htab->sgot->output_offset
3800                                            + off);
3801                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
3802                         outrel.r_addend = relocation;
3803                         loc = (Elf64_External_Rela *) htab->srelgot->contents;
3804                         loc += htab->srelgot->reloc_count++;
3805                         bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3806                       }
3807
3808                     local_got_offsets[r_symndx] |= 1;
3809                   }
3810               }
3811
3812             if (off >= (bfd_vma) -2)
3813               abort ();
3814
3815             relocation = htab->sgot->output_offset + off;
3816
3817             /* TOC base (r2) is TOC start plus 0x8000.  */
3818             addend -= TOC_BASE_OFF;
3819           }
3820           break;
3821
3822         case R_PPC64_PLT16_HA:
3823         case R_PPC64_PLT16_HI:
3824         case R_PPC64_PLT16_LO:
3825         case R_PPC64_PLT32:
3826         case R_PPC64_PLT64:
3827           /* Relocation is to the entry for this symbol in the
3828              procedure linkage table.  */
3829
3830           /* Resolve a PLT reloc against a local symbol directly,
3831              without using the procedure linkage table.  */
3832           if (h == NULL)
3833             break;
3834
3835           if (h->plt.offset == (bfd_vma) -1
3836               || htab->splt == NULL)
3837             {
3838               /* We didn't make a PLT entry for this symbol.  This
3839                  happens when statically linking PIC code, or when
3840                  using -Bsymbolic.  */
3841               break;
3842             }
3843
3844           relocation = (htab->splt->output_section->vma
3845                         + htab->splt->output_offset
3846                         + h->plt.offset);
3847           unresolved_reloc = false;
3848           break;
3849
3850           /* TOC16 relocs.  We want the offset relative to the TOC base,
3851              which is the address of the start of the TOC plus 0x8000.
3852              The TOC consists of sections .got, .toc, .tocbss, and .plt,
3853              in this order.  */
3854         case R_PPC64_TOC16:
3855         case R_PPC64_TOC16_LO:
3856         case R_PPC64_TOC16_HI:
3857         case R_PPC64_TOC16_DS:
3858         case R_PPC64_TOC16_LO_DS:
3859         case R_PPC64_TOC16_HA:
3860           addend -= TOCstart + TOC_BASE_OFF;
3861           break;
3862
3863           /* Relocate against the beginning of the section.  */
3864         case R_PPC64_SECTOFF:
3865         case R_PPC64_SECTOFF_LO:
3866         case R_PPC64_SECTOFF_HI:
3867         case R_PPC64_SECTOFF_DS:
3868         case R_PPC64_SECTOFF_LO_DS:
3869         case R_PPC64_SECTOFF_HA:
3870           if (sec != (asection *) 0)
3871             addend -= sec->output_section->vma;
3872           break;
3873
3874         case R_PPC64_REL24:
3875           break;
3876
3877           /* Relocations that may need to be propagated if this is a
3878              dynamic object.  */
3879         case R_PPC64_REL14:
3880         case R_PPC64_REL14_BRNTAKEN:
3881         case R_PPC64_REL14_BRTAKEN:
3882         case R_PPC64_REL32:
3883         case R_PPC64_REL64:
3884         case R_PPC64_ADDR14:
3885         case R_PPC64_ADDR14_BRNTAKEN:
3886         case R_PPC64_ADDR14_BRTAKEN:
3887         case R_PPC64_ADDR16:
3888         case R_PPC64_ADDR16_DS:
3889         case R_PPC64_ADDR16_HA:
3890         case R_PPC64_ADDR16_HI:
3891         case R_PPC64_ADDR16_HIGHER:
3892         case R_PPC64_ADDR16_HIGHERA:
3893         case R_PPC64_ADDR16_HIGHEST:
3894         case R_PPC64_ADDR16_HIGHESTA:
3895         case R_PPC64_ADDR16_LO:
3896         case R_PPC64_ADDR16_LO_DS:
3897         case R_PPC64_ADDR24:
3898         case R_PPC64_ADDR30:
3899         case R_PPC64_ADDR32:
3900         case R_PPC64_ADDR64:
3901         case R_PPC64_UADDR16:
3902         case R_PPC64_UADDR32:
3903         case R_PPC64_UADDR64:
3904           /* r_symndx will be zero only for relocs against symbols
3905              from removed linkonce sections, or sections discarded by
3906              a linker script.  */
3907           if (r_symndx == 0)
3908             break;
3909           /* Fall thru.  */
3910
3911         case R_PPC64_TOC:
3912           if ((input_section->flags & SEC_ALLOC) == 0)
3913             break;
3914
3915           if ((info->shared
3916                && (IS_ABSOLUTE_RELOC (r_type)
3917                    || (h != NULL
3918                        && h->dynindx != -1
3919                        && (! info->symbolic
3920                            || (h->elf_link_hash_flags
3921                                & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3922               || (!info->shared
3923                   && h != NULL
3924                   && h->dynindx != -1
3925                   && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
3926                   && (((h->elf_link_hash_flags
3927                         & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3928                        && (h->elf_link_hash_flags
3929                            & ELF_LINK_HASH_DEF_REGULAR) == 0)
3930                       || h->root.type == bfd_link_hash_undefweak
3931                       || h->root.type == bfd_link_hash_undefined)))
3932             {
3933               Elf_Internal_Rela outrel;
3934               boolean skip, relocate;
3935               asection *sreloc;
3936               Elf64_External_Rela *loc;
3937
3938 #if 0
3939               if (strcmp (bfd_get_section_name (output_bfd, input_section),
3940                           ".opd") == 0)
3941                 break;
3942 #endif
3943
3944               /* When generating a dynamic object, these relocations
3945                  are copied into the output file to be resolved at run
3946                  time.  */
3947
3948               skip = false;
3949
3950               outrel.r_offset =
3951                 _bfd_elf_section_offset (output_bfd, info, input_section,
3952                                          rel->r_offset);
3953               if (outrel.r_offset == (bfd_vma) -1)
3954                 skip = true;
3955
3956               outrel.r_offset += (input_section->output_section->vma
3957                                   + input_section->output_offset);
3958               outrel.r_addend = addend;
3959
3960               if (skip)
3961                 {
3962                   relocate = false;
3963                   memset (&outrel, 0, sizeof outrel);
3964                 }
3965               else if (h != NULL
3966                        && h->dynindx != -1
3967                        && (!IS_ABSOLUTE_RELOC (r_type)
3968                            || !info->shared
3969                            || !info->symbolic
3970                            || (h->elf_link_hash_flags
3971                                & ELF_LINK_HASH_DEF_REGULAR) == 0))
3972                 {
3973                   relocate = false;
3974                   outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
3975                 }
3976               else
3977                 {
3978                   /* This symbol is local, or marked to become local.  */
3979                   outrel.r_addend += relocation;
3980                   relocate = true;
3981                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
3982                     {
3983                       outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
3984                     }
3985                   else
3986                     {
3987                       long indx = 0;
3988
3989                       if (r_type == R_PPC64_TOC || bfd_is_abs_section (sec))
3990                         ;
3991                       else if (sec == NULL || sec->owner == NULL)
3992                         {
3993                           bfd_set_error (bfd_error_bad_value);
3994                           return false;
3995                         }
3996                       else
3997                         {
3998                           asection *osec;
3999
4000                           osec = sec->output_section;
4001                           indx = elf_section_data (osec)->dynindx;
4002
4003                           /* We are turning this relocation into one
4004                              against a section symbol, so subtract out
4005                              the output section's address but not the
4006                              offset of the input section in the output
4007                              section.  */
4008                           outrel.r_addend -= osec->vma;
4009                         }
4010
4011                       outrel.r_info = ELF64_R_INFO (indx, r_type);
4012                     }
4013                 }
4014
4015               sreloc = elf_section_data (input_section)->sreloc;
4016               if (sreloc == NULL)
4017                 abort ();
4018
4019               loc = (Elf64_External_Rela *) sreloc->contents;
4020               loc += sreloc->reloc_count++;
4021               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
4022
4023               /* If this reloc is against an external symbol, it will
4024                  be computed at runtime, so there's no need to do
4025                  anything now.  */
4026               if (! relocate)
4027                 continue;
4028             }
4029           break;
4030
4031         case R_PPC64_COPY:
4032         case R_PPC64_GLOB_DAT:
4033         case R_PPC64_JMP_SLOT:
4034         case R_PPC64_RELATIVE:
4035           /* We shouldn't ever see these dynamic relocs in relocatable
4036              files.  */
4037           /* Fall thru */
4038
4039         case R_PPC64_PLTGOT16:
4040         case R_PPC64_PLTGOT16_DS:
4041         case R_PPC64_PLTGOT16_HA:
4042         case R_PPC64_PLTGOT16_HI:
4043         case R_PPC64_PLTGOT16_LO:
4044         case R_PPC64_PLTGOT16_LO_DS:
4045         case R_PPC64_PLTREL32:
4046         case R_PPC64_PLTREL64:
4047           /* These ones haven't been implemented yet.  */
4048
4049           (*_bfd_error_handler)
4050             (_("%s: Relocation %s is not supported for symbol %s."),
4051              bfd_archive_filename (input_bfd),
4052              ppc64_elf_howto_table[(int) r_type]->name, sym_name);
4053
4054           bfd_set_error (bfd_error_invalid_operation);
4055           ret = false;
4056           continue;
4057         }
4058
4059       /* Do any further special processing.  */
4060       switch (r_type)
4061         {
4062         default:
4063           break;
4064
4065         case R_PPC64_ADDR16_HA:
4066         case R_PPC64_ADDR16_HIGHERA:
4067         case R_PPC64_ADDR16_HIGHESTA:
4068         case R_PPC64_PLT16_HA:
4069         case R_PPC64_TOC16_HA:
4070         case R_PPC64_SECTOFF_HA:
4071           /* It's just possible that this symbol is a weak symbol
4072              that's not actually defined anywhere. In that case,
4073              'sec' would be NULL, and we should leave the symbol
4074              alone (it will be set to zero elsewhere in the link).  */
4075           if (sec != NULL)
4076             /* Add 0x10000 if sign bit in 0:15 is set.  */
4077             addend += ((relocation + addend) & 0x8000) << 1;
4078           break;
4079
4080         case R_PPC64_ADDR16_DS:
4081         case R_PPC64_ADDR16_LO_DS:
4082         case R_PPC64_GOT16_DS:
4083         case R_PPC64_GOT16_LO_DS:
4084         case R_PPC64_PLT16_LO_DS:
4085         case R_PPC64_SECTOFF_DS:
4086         case R_PPC64_SECTOFF_LO_DS:
4087         case R_PPC64_TOC16_DS:
4088         case R_PPC64_TOC16_LO_DS:
4089         case R_PPC64_PLTGOT16_DS:
4090         case R_PPC64_PLTGOT16_LO_DS:
4091           if (((relocation + addend) & 3) != 0)
4092             {
4093               (*_bfd_error_handler)
4094                 (_("%s: error: relocation %s not a multiple of 4"),
4095                  bfd_archive_filename (input_bfd),
4096                  ppc64_elf_howto_table[(int) r_type]->name);
4097               bfd_set_error (bfd_error_bad_value);
4098               ret = false;
4099               continue;
4100             }
4101           break;
4102         }
4103
4104       /* FIXME: Why do we allow debugging sections to escape this error?
4105          More importantly, why do we not emit dynamic relocs above in
4106          debugging sections (which are ! SEC_ALLOC)?  If we had
4107          emitted the dynamic reloc, we could remove the fudge here.  */
4108       if (unresolved_reloc
4109           && !(info->shared
4110                && (input_section->flags & SEC_DEBUGGING) != 0
4111                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
4112         (*_bfd_error_handler)
4113           (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
4114            bfd_archive_filename (input_bfd),
4115            bfd_get_section_name (input_bfd, input_section),
4116            (long) rel->r_offset,
4117            h->root.root.string);
4118
4119       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
4120                                     input_bfd,
4121                                     input_section,
4122                                     contents,
4123                                     offset,
4124                                     relocation,
4125                                     addend);
4126
4127       if (r == bfd_reloc_ok)
4128         ;
4129       else if (r == bfd_reloc_overflow)
4130         {
4131           const char *name;
4132
4133           if (h != NULL)
4134             {
4135               if (h->root.type == bfd_link_hash_undefweak
4136                   && ppc64_elf_howto_table[(int) r_type]->pc_relative)
4137                 {
4138                   /* Assume this is a call protected by other code that
4139                      detects the symbol is undefined.  If this is the case,
4140                      we can safely ignore the overflow.  If not, the
4141                      program is hosed anyway, and a little warning isn't
4142                      going to help.  */
4143
4144                   continue;
4145                 }
4146
4147               name = h->root.root.string;
4148             }
4149           else
4150             {
4151               name = bfd_elf_string_from_elf_section (input_bfd,
4152                                                       symtab_hdr->sh_link,
4153                                                       sym->st_name);
4154               if (name == NULL)
4155                 continue;
4156               if (*name == '\0')
4157                 name = bfd_section_name (input_bfd, sec);
4158             }
4159
4160           if (! ((*info->callbacks->reloc_overflow)
4161                  (info, name, ppc64_elf_howto_table[(int) r_type]->name,
4162                   (bfd_vma) 0, input_bfd, input_section, offset)))
4163             return false;
4164         }
4165       else
4166         ret = false;
4167     }
4168
4169   return ret;
4170 }
4171
4172 /* Finish up dynamic symbol handling.  We set the contents of various
4173    dynamic sections here.  */
4174
4175 static boolean
4176 ppc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
4177      bfd *output_bfd;
4178      struct bfd_link_info *info;
4179      struct elf_link_hash_entry *h;
4180      Elf_Internal_Sym *sym;
4181 {
4182   struct ppc_link_hash_table *htab;
4183   bfd *dynobj;
4184
4185   htab = ppc_hash_table (info);
4186   dynobj = htab->elf.dynobj;
4187
4188   if (h->plt.offset != (bfd_vma) -1)
4189     {
4190       Elf_Internal_Rela rela;
4191       Elf64_External_Rela *loc;
4192
4193       /* This symbol has an entry in the procedure linkage table.  Set
4194          it up.  */
4195
4196       if (htab->splt == NULL
4197           || htab->srelplt == NULL
4198           || htab->sglink == NULL)
4199         abort ();
4200
4201       BFD_ASSERT (((struct ppc_link_hash_entry *) h)->is_func_descriptor);
4202
4203       /* Create a JMP_SLOT reloc to inform the dynamic linker to
4204          fill in the PLT entry.  */
4205
4206       rela.r_offset = (htab->splt->output_section->vma
4207                        + htab->splt->output_offset
4208                        + h->plt.offset);
4209       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
4210       rela.r_addend = 0;
4211
4212       loc = (Elf64_External_Rela *) htab->srelplt->contents;
4213       loc += (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE;
4214       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
4215     }
4216
4217   if (h->got.offset != (bfd_vma) -1)
4218     {
4219       Elf_Internal_Rela rela;
4220       Elf64_External_Rela *loc;
4221
4222       /* This symbol has an entry in the global offset table.  Set it
4223          up.  */
4224
4225       if (htab->sgot == NULL || htab->srelgot == NULL)
4226         abort ();
4227
4228       rela.r_offset = (htab->sgot->output_section->vma
4229                        + htab->sgot->output_offset
4230                        + (h->got.offset &~ (bfd_vma) 1));
4231
4232       /* If this is a static link, or it is a -Bsymbolic link and the
4233          symbol is defined locally or was forced to be local because
4234          of a version file, we just want to emit a RELATIVE reloc.
4235          The entry in the global offset table will already have been
4236          initialized in the relocate_section function.  */
4237       if (info->shared
4238           && (info->symbolic
4239               || h->dynindx == -1
4240               || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
4241           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
4242         {
4243           BFD_ASSERT((h->got.offset & 1) != 0);
4244           rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
4245           rela.r_addend = (h->root.u.def.value
4246                            + h->root.u.def.section->output_section->vma
4247                            + h->root.u.def.section->output_offset);
4248         }
4249       else
4250         {
4251           BFD_ASSERT ((h->got.offset & 1) == 0);
4252           bfd_put_64 (output_bfd, (bfd_vma) 0,
4253                       htab->sgot->contents + h->got.offset);
4254           rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_GLOB_DAT);
4255           rela.r_addend = 0;
4256         }
4257
4258       loc = (Elf64_External_Rela *) htab->srelgot->contents;
4259       loc += htab->srelgot->reloc_count++;
4260       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
4261     }
4262
4263   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4264     {
4265       Elf_Internal_Rela rela;
4266       Elf64_External_Rela *loc;
4267
4268       /* This symbol needs a copy reloc.  Set it up.  */
4269
4270       if (h->dynindx == -1
4271           || (h->root.type != bfd_link_hash_defined
4272               && h->root.type != bfd_link_hash_defweak)
4273           || htab->srelbss == NULL)
4274         abort ();
4275
4276       rela.r_offset = (h->root.u.def.value
4277                        + h->root.u.def.section->output_section->vma
4278                        + h->root.u.def.section->output_offset);
4279       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
4280       rela.r_addend = 0;
4281       loc = (Elf64_External_Rela *) htab->srelbss->contents;
4282       loc += htab->srelbss->reloc_count++;
4283       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
4284     }
4285
4286   /* Mark some specially defined symbols as absolute.  */
4287   if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
4288     sym->st_shndx = SHN_ABS;
4289
4290   return true;
4291 }
4292
4293 /* Used to decide how to sort relocs in an optimal manner for the
4294    dynamic linker, before writing them out.  */
4295
4296 static enum elf_reloc_type_class
4297 ppc64_elf_reloc_type_class (rela)
4298      const Elf_Internal_Rela *rela;
4299 {
4300   enum elf_ppc_reloc_type r_type;
4301
4302   r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rela->r_info);
4303   switch (r_type)
4304     {
4305     case R_PPC64_RELATIVE:
4306       return reloc_class_relative;
4307     case R_PPC64_JMP_SLOT:
4308       return reloc_class_plt;
4309     case R_PPC64_COPY:
4310       return reloc_class_copy;
4311     default:
4312       return reloc_class_normal;
4313     }
4314 }
4315
4316 /* Finish up the dynamic sections.  */
4317
4318 static boolean
4319 ppc64_elf_finish_dynamic_sections (output_bfd, info)
4320      bfd *output_bfd;
4321      struct bfd_link_info *info;
4322 {
4323   struct ppc_link_hash_table *htab;
4324   bfd *dynobj;
4325   asection *sdyn;
4326
4327   htab = ppc_hash_table (info);
4328   dynobj = htab->elf.dynobj;
4329   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4330
4331   if (htab->elf.dynamic_sections_created)
4332     {
4333       Elf64_External_Dyn *dyncon, *dynconend;
4334
4335       if (sdyn == NULL || htab->sgot == NULL)
4336         abort ();
4337
4338       dyncon = (Elf64_External_Dyn *) sdyn->contents;
4339       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4340       for (; dyncon < dynconend; dyncon++)
4341         {
4342           Elf_Internal_Dyn dyn;
4343
4344           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
4345
4346           switch (dyn.d_tag)
4347             {
4348             default:
4349               continue;
4350
4351             case DT_PPC64_GLINK:
4352               dyn.d_un.d_ptr = (htab->sglink->output_section->vma
4353                                 + htab->sglink->output_offset);
4354               break;
4355
4356             case DT_PLTGOT:
4357               dyn.d_un.d_ptr = (htab->splt->output_section->vma
4358                                 + htab->splt->output_offset);
4359               break;
4360
4361             case DT_JMPREL:
4362               dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
4363                                 + htab->srelplt->output_offset);
4364               break;
4365
4366             case DT_PLTRELSZ:
4367               dyn.d_un.d_val = htab->srelplt->_raw_size;
4368               break;
4369
4370             case DT_RELASZ:
4371               /* Don't count procedure linkage table relocs in the
4372                  overall reloc count.  */
4373               if (htab->srelplt != NULL)
4374                 dyn.d_un.d_val -= htab->srelplt->_raw_size;
4375               break;
4376             }
4377
4378           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4379         }
4380     }
4381
4382   if (htab->sgot != NULL && htab->sgot->_raw_size != 0)
4383     {
4384       /* Fill in the first entry in the global offset table.
4385          We use it to hold the link-time TOCbase.  */
4386       bfd_put_64 (output_bfd,
4387                   elf_gp (output_bfd) + TOC_BASE_OFF,
4388                   htab->sgot->contents);
4389
4390       /* Set .got entry size.  */
4391       elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 8;
4392     }
4393
4394   if (htab->splt != NULL && htab->splt->_raw_size != 0)
4395     {
4396       /* Set .plt entry size.  */
4397       elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize
4398         = PLT_ENTRY_SIZE;
4399     }
4400
4401   return true;
4402 }
4403
4404 #define TARGET_LITTLE_SYM       bfd_elf64_powerpcle_vec
4405 #define TARGET_LITTLE_NAME      "elf64-powerpcle"
4406 #define TARGET_BIG_SYM          bfd_elf64_powerpc_vec
4407 #define TARGET_BIG_NAME         "elf64-powerpc"
4408 #define ELF_ARCH                bfd_arch_powerpc
4409 #define ELF_MACHINE_CODE        EM_PPC64
4410 #define ELF_MAXPAGESIZE         0x10000
4411 #define elf_info_to_howto       ppc64_elf_info_to_howto
4412
4413 #ifdef  EM_CYGNUS_POWERPC
4414 #define ELF_MACHINE_ALT1        EM_CYGNUS_POWERPC
4415 #endif
4416
4417 #ifdef EM_PPC_OLD
4418 #define ELF_MACHINE_ALT2        EM_PPC_OLD
4419 #endif
4420
4421 #define elf_backend_want_got_sym 0
4422 #define elf_backend_want_plt_sym 0
4423 #define elf_backend_plt_alignment 3
4424 #define elf_backend_plt_not_loaded 1
4425 #define elf_backend_got_symbol_offset 0
4426 #define elf_backend_got_header_size 8
4427 #define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
4428 #define elf_backend_can_gc_sections 1
4429 #define elf_backend_can_refcount 1
4430
4431 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
4432 #define bfd_elf64_bfd_set_private_flags       ppc64_elf_set_private_flags
4433 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
4434 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
4435
4436 #define elf_backend_section_from_shdr         ppc64_elf_section_from_shdr
4437 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
4438 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
4439 #define elf_backend_check_relocs              ppc64_elf_check_relocs
4440 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
4441 #define elf_backend_gc_sweep_hook             ppc64_elf_gc_sweep_hook
4442 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
4443 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
4444 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
4445 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
4446 #define elf_backend_fake_sections             ppc64_elf_fake_sections
4447 #define elf_backend_relocate_section          ppc64_elf_relocate_section
4448 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
4449 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
4450 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
4451
4452 #include "elf64-target.h"