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