* elf64-ppc.c (get_tls_mask): Add toc_symndx param, save toc
[external/binutils.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright 1999, 2000, 2001, 2002, 2003 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 along
19    with this program; if not, write to the Free Software Foundation, Inc.,
20    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 /* The 64-bit PowerPC ELF ABI may be found at
23    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
24    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
25
26 #include "bfd.h"
27 #include "sysdep.h"
28 #include "bfdlink.h"
29 #include "libbfd.h"
30 #include "elf-bfd.h"
31 #include "elf/ppc64.h"
32 #include "elf64-ppc.h"
33
34 static bfd_reloc_status_type ppc64_elf_ha_reloc
35   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
36 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
37   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
38 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
39   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
41   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_toc_reloc
43   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
45   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_toc64_reloc
47   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
49   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50
51
52 #define TARGET_LITTLE_SYM       bfd_elf64_powerpcle_vec
53 #define TARGET_LITTLE_NAME      "elf64-powerpcle"
54 #define TARGET_BIG_SYM          bfd_elf64_powerpc_vec
55 #define TARGET_BIG_NAME         "elf64-powerpc"
56 #define ELF_ARCH                bfd_arch_powerpc
57 #define ELF_MACHINE_CODE        EM_PPC64
58 #define ELF_MAXPAGESIZE         0x10000
59 #define elf_info_to_howto       ppc64_elf_info_to_howto
60
61 #define elf_backend_want_got_sym 0
62 #define elf_backend_want_plt_sym 0
63 #define elf_backend_plt_alignment 3
64 #define elf_backend_plt_not_loaded 1
65 #define elf_backend_got_symbol_offset 0
66 #define elf_backend_got_header_size 8
67 #define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
68 #define elf_backend_can_gc_sections 1
69 #define elf_backend_can_refcount 1
70 #define elf_backend_rela_normal 1
71
72 #define bfd_elf64_mkobject                    ppc64_elf_mkobject
73 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
74 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
75 #define bfd_elf64_new_section_hook            ppc64_elf_new_section_hook
76 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
77 #define bfd_elf64_bfd_link_hash_table_free    ppc64_elf_link_hash_table_free
78
79 #define elf_backend_object_p                  ppc64_elf_object_p
80 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
81 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
82 #define elf_backend_check_relocs              ppc64_elf_check_relocs
83 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
84 #define elf_backend_gc_sweep_hook             ppc64_elf_gc_sweep_hook
85 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
86 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
87 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
88 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
89 #define elf_backend_relocate_section          ppc64_elf_relocate_section
90 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
91 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
92 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
93 #define elf_backend_special_sections          ppc64_elf_special_sections
94
95 /* The name of the dynamic interpreter.  This is put in the .interp
96    section.  */
97 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
98
99 /* The size in bytes of an entry in the procedure linkage table.  */
100 #define PLT_ENTRY_SIZE 24
101
102 /* The initial size of the plt reserved for the dynamic linker.  */
103 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
104
105 /* TOC base pointers offset from start of TOC.  */
106 #define TOC_BASE_OFF    0x8000
107
108 /* Offset of tp and dtp pointers from start of TLS block.  */
109 #define TP_OFFSET       0x7000
110 #define DTP_OFFSET      0x8000
111
112 /* .plt call stub instructions.  The normal stub is like this, but
113    sometimes the .plt entry crosses a 64k boundary and we need to
114    insert an addis to adjust r12.  */
115 #define PLT_CALL_STUB_SIZE (7*4)
116 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha     */
117 #define STD_R2_40R1     0xf8410028      /* std   %r2,40(%r1)         */
118 #define LD_R11_0R12     0xe96c0000      /* ld    %r11,xxx+0@l(%r12)  */
119 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,xxx+8@l(%r12)   */
120 #define MTCTR_R11       0x7d6903a6      /* mtctr %r11                */
121                                         /* ld    %r11,xxx+16@l(%r12) */
122 #define BCTR            0x4e800420      /* bctr                      */
123
124
125 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
126 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
127
128 #define LD_R2_40R1      0xe8410028      /* ld    %r2,40(%r1)     */
129
130 /* glink call stub instructions.  We enter with the index in R0, and the
131    address of glink entry in CTR.  From that, we can calculate PLT0.  */
132 #define GLINK_CALL_STUB_SIZE (16*4)
133 #define MFCTR_R12       0x7d8902a6      /* mfctr  %r12                  */
134 #define SLDI_R11_R0_3   0x780b1f24      /* sldi   %r11,%r0,3            */
135 #define ADDIC_R2_R0_32K 0x34408000      /* addic. %r2,%r0,-32768        */
136 #define SUB_R12_R12_R11 0x7d8b6050      /* sub    %r12,%r12,%r11        */
137 #define SRADI_R2_R2_63  0x7c42fe76      /* sradi  %r2,%r2,63            */
138 #define SLDI_R11_R0_2   0x780b1764      /* sldi   %r11,%r0,2            */
139 #define AND_R2_R2_R11   0x7c425838      /* and    %r2,%r2,%r11          */
140                                         /* sub    %r12,%r12,%r11        */
141 #define ADD_R12_R12_R2  0x7d8c1214      /* add    %r12,%r12,%r2         */
142 #define ADDIS_R12_R12   0x3d8c0000      /* addis  %r12,%r12,xxx@ha      */
143                                         /* ld     %r11,xxx@l(%r12)      */
144 #define ADDI_R12_R12    0x398c0000      /* addi   %r12,%r12,xxx@l       */
145                                         /* ld     %r2,8(%r12)           */
146                                         /* mtctr  %r11                  */
147                                         /* ld     %r11,16(%r12)         */
148                                         /* bctr                         */
149
150 /* Pad with this.  */
151 #define NOP             0x60000000
152
153 /* Some other nops.  */
154 #define CROR_151515     0x4def7b82
155 #define CROR_313131     0x4ffffb82
156
157 /* .glink entries for the first 32k functions are two instructions.  */
158 #define LI_R0_0         0x38000000      /* li    %r0,0          */
159 #define B_DOT           0x48000000      /* b     .              */
160
161 /* After that, we need two instructions to load the index, followed by
162    a branch.  */
163 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
164 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
165
166 /* Instructions to save and restore floating point regs.  */
167 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
168 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
169 #define BLR             0x4e800020      /* blr                  */
170
171 /* Since .opd is an array of descriptors and each entry will end up
172    with identical R_PPC64_RELATIVE relocs, there is really no need to
173    propagate .opd relocs;  The dynamic linker should be taught to
174    relocate .opd without reloc entries.  */
175 #ifndef NO_OPD_RELOCS
176 #define NO_OPD_RELOCS 0
177 #endif
178 \f
179 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
180
181 /* Relocation HOWTO's.  */
182 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
183
184 static reloc_howto_type ppc64_elf_howto_raw[] = {
185   /* This reloc does nothing.  */
186   HOWTO (R_PPC64_NONE,          /* type */
187          0,                     /* rightshift */
188          2,                     /* size (0 = byte, 1 = short, 2 = long) */
189          32,                    /* bitsize */
190          FALSE,                 /* pc_relative */
191          0,                     /* bitpos */
192          complain_overflow_dont, /* complain_on_overflow */
193          bfd_elf_generic_reloc, /* special_function */
194          "R_PPC64_NONE",        /* name */
195          FALSE,                 /* partial_inplace */
196          0,                     /* src_mask */
197          0,                     /* dst_mask */
198          FALSE),                /* pcrel_offset */
199
200   /* A standard 32 bit relocation.  */
201   HOWTO (R_PPC64_ADDR32,        /* type */
202          0,                     /* rightshift */
203          2,                     /* size (0 = byte, 1 = short, 2 = long) */
204          32,                    /* bitsize */
205          FALSE,                 /* pc_relative */
206          0,                     /* bitpos */
207          complain_overflow_bitfield, /* complain_on_overflow */
208          bfd_elf_generic_reloc, /* special_function */
209          "R_PPC64_ADDR32",      /* name */
210          FALSE,                 /* partial_inplace */
211          0,                     /* src_mask */
212          0xffffffff,            /* dst_mask */
213          FALSE),                /* pcrel_offset */
214
215   /* An absolute 26 bit branch; the lower two bits must be zero.
216      FIXME: we don't check that, we just clear them.  */
217   HOWTO (R_PPC64_ADDR24,        /* type */
218          0,                     /* rightshift */
219          2,                     /* size (0 = byte, 1 = short, 2 = long) */
220          26,                    /* bitsize */
221          FALSE,                 /* pc_relative */
222          0,                     /* bitpos */
223          complain_overflow_bitfield, /* complain_on_overflow */
224          bfd_elf_generic_reloc, /* special_function */
225          "R_PPC64_ADDR24",      /* name */
226          FALSE,                 /* partial_inplace */
227          0,                     /* src_mask */
228          0x03fffffc,            /* dst_mask */
229          FALSE),                /* pcrel_offset */
230
231   /* A standard 16 bit relocation.  */
232   HOWTO (R_PPC64_ADDR16,        /* type */
233          0,                     /* rightshift */
234          1,                     /* size (0 = byte, 1 = short, 2 = long) */
235          16,                    /* bitsize */
236          FALSE,                 /* pc_relative */
237          0,                     /* bitpos */
238          complain_overflow_bitfield, /* complain_on_overflow */
239          bfd_elf_generic_reloc, /* special_function */
240          "R_PPC64_ADDR16",      /* name */
241          FALSE,                 /* partial_inplace */
242          0,                     /* src_mask */
243          0xffff,                /* dst_mask */
244          FALSE),                /* pcrel_offset */
245
246   /* A 16 bit relocation without overflow.  */
247   HOWTO (R_PPC64_ADDR16_LO,     /* type */
248          0,                     /* rightshift */
249          1,                     /* size (0 = byte, 1 = short, 2 = long) */
250          16,                    /* bitsize */
251          FALSE,                 /* pc_relative */
252          0,                     /* bitpos */
253          complain_overflow_dont,/* complain_on_overflow */
254          bfd_elf_generic_reloc, /* special_function */
255          "R_PPC64_ADDR16_LO",   /* name */
256          FALSE,                 /* partial_inplace */
257          0,                     /* src_mask */
258          0xffff,                /* dst_mask */
259          FALSE),                /* pcrel_offset */
260
261   /* Bits 16-31 of an address.  */
262   HOWTO (R_PPC64_ADDR16_HI,     /* type */
263          16,                    /* rightshift */
264          1,                     /* size (0 = byte, 1 = short, 2 = long) */
265          16,                    /* bitsize */
266          FALSE,                 /* pc_relative */
267          0,                     /* bitpos */
268          complain_overflow_dont, /* complain_on_overflow */
269          bfd_elf_generic_reloc, /* special_function */
270          "R_PPC64_ADDR16_HI",   /* name */
271          FALSE,                 /* partial_inplace */
272          0,                     /* src_mask */
273          0xffff,                /* dst_mask */
274          FALSE),                /* pcrel_offset */
275
276   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
277      bits, treated as a signed number, is negative.  */
278   HOWTO (R_PPC64_ADDR16_HA,     /* type */
279          16,                    /* rightshift */
280          1,                     /* size (0 = byte, 1 = short, 2 = long) */
281          16,                    /* bitsize */
282          FALSE,                 /* pc_relative */
283          0,                     /* bitpos */
284          complain_overflow_dont, /* complain_on_overflow */
285          ppc64_elf_ha_reloc,    /* special_function */
286          "R_PPC64_ADDR16_HA",   /* name */
287          FALSE,                 /* partial_inplace */
288          0,                     /* src_mask */
289          0xffff,                /* dst_mask */
290          FALSE),                /* pcrel_offset */
291
292   /* An absolute 16 bit branch; the lower two bits must be zero.
293      FIXME: we don't check that, we just clear them.  */
294   HOWTO (R_PPC64_ADDR14,        /* type */
295          0,                     /* rightshift */
296          2,                     /* size (0 = byte, 1 = short, 2 = long) */
297          16,                    /* bitsize */
298          FALSE,                 /* pc_relative */
299          0,                     /* bitpos */
300          complain_overflow_bitfield, /* complain_on_overflow */
301          bfd_elf_generic_reloc, /* special_function */
302          "R_PPC64_ADDR14",      /* name */
303          FALSE,                 /* partial_inplace */
304          0,                     /* src_mask */
305          0x0000fffc,            /* dst_mask */
306          FALSE),                /* pcrel_offset */
307
308   /* An absolute 16 bit branch, for which bit 10 should be set to
309      indicate that the branch is expected to be taken.  The lower two
310      bits must be zero.  */
311   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
312          0,                     /* rightshift */
313          2,                     /* size (0 = byte, 1 = short, 2 = long) */
314          16,                    /* bitsize */
315          FALSE,                 /* pc_relative */
316          0,                     /* bitpos */
317          complain_overflow_bitfield, /* complain_on_overflow */
318          ppc64_elf_brtaken_reloc, /* special_function */
319          "R_PPC64_ADDR14_BRTAKEN",/* name */
320          FALSE,                 /* partial_inplace */
321          0,                     /* src_mask */
322          0x0000fffc,            /* dst_mask */
323          FALSE),                /* pcrel_offset */
324
325   /* An absolute 16 bit branch, for which bit 10 should be set to
326      indicate that the branch is not expected to be taken.  The lower
327      two bits must be zero.  */
328   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
329          0,                     /* rightshift */
330          2,                     /* size (0 = byte, 1 = short, 2 = long) */
331          16,                    /* bitsize */
332          FALSE,                 /* pc_relative */
333          0,                     /* bitpos */
334          complain_overflow_bitfield, /* complain_on_overflow */
335          ppc64_elf_brtaken_reloc, /* special_function */
336          "R_PPC64_ADDR14_BRNTAKEN",/* name */
337          FALSE,                 /* partial_inplace */
338          0,                     /* src_mask */
339          0x0000fffc,            /* dst_mask */
340          FALSE),                /* pcrel_offset */
341
342   /* A relative 26 bit branch; the lower two bits must be zero.  */
343   HOWTO (R_PPC64_REL24,         /* type */
344          0,                     /* rightshift */
345          2,                     /* size (0 = byte, 1 = short, 2 = long) */
346          26,                    /* bitsize */
347          TRUE,                  /* pc_relative */
348          0,                     /* bitpos */
349          complain_overflow_signed, /* complain_on_overflow */
350          bfd_elf_generic_reloc, /* special_function */
351          "R_PPC64_REL24",       /* name */
352          FALSE,                 /* partial_inplace */
353          0,                     /* src_mask */
354          0x03fffffc,            /* dst_mask */
355          TRUE),                 /* pcrel_offset */
356
357   /* A relative 16 bit branch; the lower two bits must be zero.  */
358   HOWTO (R_PPC64_REL14,         /* type */
359          0,                     /* rightshift */
360          2,                     /* size (0 = byte, 1 = short, 2 = long) */
361          16,                    /* bitsize */
362          TRUE,                  /* pc_relative */
363          0,                     /* bitpos */
364          complain_overflow_signed, /* complain_on_overflow */
365          bfd_elf_generic_reloc, /* special_function */
366          "R_PPC64_REL14",       /* name */
367          FALSE,                 /* partial_inplace */
368          0,                     /* src_mask */
369          0x0000fffc,            /* dst_mask */
370          TRUE),                 /* pcrel_offset */
371
372   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
373      the branch is expected to be taken.  The lower two bits must be
374      zero.  */
375   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
376          0,                     /* rightshift */
377          2,                     /* size (0 = byte, 1 = short, 2 = long) */
378          16,                    /* bitsize */
379          TRUE,                  /* pc_relative */
380          0,                     /* bitpos */
381          complain_overflow_signed, /* complain_on_overflow */
382          ppc64_elf_brtaken_reloc, /* special_function */
383          "R_PPC64_REL14_BRTAKEN", /* name */
384          FALSE,                 /* partial_inplace */
385          0,                     /* src_mask */
386          0x0000fffc,            /* dst_mask */
387          TRUE),                 /* pcrel_offset */
388
389   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
390      the branch is not expected to be taken.  The lower two bits must
391      be zero.  */
392   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
393          0,                     /* rightshift */
394          2,                     /* size (0 = byte, 1 = short, 2 = long) */
395          16,                    /* bitsize */
396          TRUE,                  /* pc_relative */
397          0,                     /* bitpos */
398          complain_overflow_signed, /* complain_on_overflow */
399          ppc64_elf_brtaken_reloc, /* special_function */
400          "R_PPC64_REL14_BRNTAKEN",/* name */
401          FALSE,                 /* partial_inplace */
402          0,                     /* src_mask */
403          0x0000fffc,            /* dst_mask */
404          TRUE),                 /* pcrel_offset */
405
406   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
407      symbol.  */
408   HOWTO (R_PPC64_GOT16,         /* type */
409          0,                     /* rightshift */
410          1,                     /* size (0 = byte, 1 = short, 2 = long) */
411          16,                    /* bitsize */
412          FALSE,                 /* pc_relative */
413          0,                     /* bitpos */
414          complain_overflow_signed, /* complain_on_overflow */
415          ppc64_elf_unhandled_reloc, /* special_function */
416          "R_PPC64_GOT16",       /* name */
417          FALSE,                 /* partial_inplace */
418          0,                     /* src_mask */
419          0xffff,                /* dst_mask */
420          FALSE),                /* pcrel_offset */
421
422   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
423      the symbol.  */
424   HOWTO (R_PPC64_GOT16_LO,      /* type */
425          0,                     /* rightshift */
426          1,                     /* size (0 = byte, 1 = short, 2 = long) */
427          16,                    /* bitsize */
428          FALSE,                 /* pc_relative */
429          0,                     /* bitpos */
430          complain_overflow_dont, /* complain_on_overflow */
431          ppc64_elf_unhandled_reloc, /* special_function */
432          "R_PPC64_GOT16_LO",    /* name */
433          FALSE,                 /* partial_inplace */
434          0,                     /* src_mask */
435          0xffff,                /* dst_mask */
436          FALSE),                /* pcrel_offset */
437
438   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
439      the symbol.  */
440   HOWTO (R_PPC64_GOT16_HI,      /* type */
441          16,                    /* rightshift */
442          1,                     /* size (0 = byte, 1 = short, 2 = long) */
443          16,                    /* bitsize */
444          FALSE,                 /* pc_relative */
445          0,                     /* bitpos */
446          complain_overflow_dont,/* complain_on_overflow */
447          ppc64_elf_unhandled_reloc, /* special_function */
448          "R_PPC64_GOT16_HI",    /* name */
449          FALSE,                 /* partial_inplace */
450          0,                     /* src_mask */
451          0xffff,                /* dst_mask */
452          FALSE),                /* pcrel_offset */
453
454   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
455      the symbol.  */
456   HOWTO (R_PPC64_GOT16_HA,      /* type */
457          16,                    /* rightshift */
458          1,                     /* size (0 = byte, 1 = short, 2 = long) */
459          16,                    /* bitsize */
460          FALSE,                 /* pc_relative */
461          0,                     /* bitpos */
462          complain_overflow_dont,/* complain_on_overflow */
463          ppc64_elf_unhandled_reloc, /* special_function */
464          "R_PPC64_GOT16_HA",    /* name */
465          FALSE,                 /* partial_inplace */
466          0,                     /* src_mask */
467          0xffff,                /* dst_mask */
468          FALSE),                /* pcrel_offset */
469
470   /* This is used only by the dynamic linker.  The symbol should exist
471      both in the object being run and in some shared library.  The
472      dynamic linker copies the data addressed by the symbol from the
473      shared library into the object, because the object being
474      run has to have the data at some particular address.  */
475   HOWTO (R_PPC64_COPY,          /* type */
476          0,                     /* rightshift */
477          0,                     /* this one is variable size */
478          0,                     /* bitsize */
479          FALSE,                 /* pc_relative */
480          0,                     /* bitpos */
481          complain_overflow_dont, /* complain_on_overflow */
482          ppc64_elf_unhandled_reloc, /* special_function */
483          "R_PPC64_COPY",        /* name */
484          FALSE,                 /* partial_inplace */
485          0,                     /* src_mask */
486          0,                     /* dst_mask */
487          FALSE),                /* pcrel_offset */
488
489   /* Like R_PPC64_ADDR64, but used when setting global offset table
490      entries.  */
491   HOWTO (R_PPC64_GLOB_DAT,      /* type */
492          0,                     /* rightshift */
493          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
494          64,                    /* bitsize */
495          FALSE,                 /* pc_relative */
496          0,                     /* bitpos */
497          complain_overflow_dont, /* complain_on_overflow */
498          ppc64_elf_unhandled_reloc,  /* special_function */
499          "R_PPC64_GLOB_DAT",    /* name */
500          FALSE,                 /* partial_inplace */
501          0,                     /* src_mask */
502          ONES (64),             /* dst_mask */
503          FALSE),                /* pcrel_offset */
504
505   /* Created by the link editor.  Marks a procedure linkage table
506      entry for a symbol.  */
507   HOWTO (R_PPC64_JMP_SLOT,      /* type */
508          0,                     /* rightshift */
509          0,                     /* size (0 = byte, 1 = short, 2 = long) */
510          0,                     /* bitsize */
511          FALSE,                 /* pc_relative */
512          0,                     /* bitpos */
513          complain_overflow_dont, /* complain_on_overflow */
514          ppc64_elf_unhandled_reloc, /* special_function */
515          "R_PPC64_JMP_SLOT",    /* name */
516          FALSE,                 /* partial_inplace */
517          0,                     /* src_mask */
518          0,                     /* dst_mask */
519          FALSE),                /* pcrel_offset */
520
521   /* Used only by the dynamic linker.  When the object is run, this
522      doubleword64 is set to the load address of the object, plus the
523      addend.  */
524   HOWTO (R_PPC64_RELATIVE,      /* type */
525          0,                     /* rightshift */
526          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
527          64,                    /* bitsize */
528          FALSE,                 /* pc_relative */
529          0,                     /* bitpos */
530          complain_overflow_dont, /* complain_on_overflow */
531          bfd_elf_generic_reloc, /* special_function */
532          "R_PPC64_RELATIVE",    /* name */
533          FALSE,                 /* partial_inplace */
534          0,                     /* src_mask */
535          ONES (64),             /* dst_mask */
536          FALSE),                /* pcrel_offset */
537
538   /* Like R_PPC64_ADDR32, but may be unaligned.  */
539   HOWTO (R_PPC64_UADDR32,       /* type */
540          0,                     /* rightshift */
541          2,                     /* size (0 = byte, 1 = short, 2 = long) */
542          32,                    /* bitsize */
543          FALSE,                 /* pc_relative */
544          0,                     /* bitpos */
545          complain_overflow_bitfield, /* complain_on_overflow */
546          bfd_elf_generic_reloc, /* special_function */
547          "R_PPC64_UADDR32",     /* name */
548          FALSE,                 /* partial_inplace */
549          0,                     /* src_mask */
550          0xffffffff,            /* dst_mask */
551          FALSE),                /* pcrel_offset */
552
553   /* Like R_PPC64_ADDR16, but may be unaligned.  */
554   HOWTO (R_PPC64_UADDR16,       /* type */
555          0,                     /* rightshift */
556          1,                     /* size (0 = byte, 1 = short, 2 = long) */
557          16,                    /* bitsize */
558          FALSE,                 /* pc_relative */
559          0,                     /* bitpos */
560          complain_overflow_bitfield, /* complain_on_overflow */
561          bfd_elf_generic_reloc, /* special_function */
562          "R_PPC64_UADDR16",     /* name */
563          FALSE,                 /* partial_inplace */
564          0,                     /* src_mask */
565          0xffff,                /* dst_mask */
566          FALSE),                /* pcrel_offset */
567
568   /* 32-bit PC relative.  */
569   HOWTO (R_PPC64_REL32,         /* type */
570          0,                     /* rightshift */
571          2,                     /* size (0 = byte, 1 = short, 2 = long) */
572          32,                    /* bitsize */
573          TRUE,                  /* pc_relative */
574          0,                     /* bitpos */
575          /* FIXME: Verify.  Was complain_overflow_bitfield.  */
576          complain_overflow_signed, /* complain_on_overflow */
577          bfd_elf_generic_reloc, /* special_function */
578          "R_PPC64_REL32",       /* name */
579          FALSE,                 /* partial_inplace */
580          0,                     /* src_mask */
581          0xffffffff,            /* dst_mask */
582          TRUE),                 /* pcrel_offset */
583
584   /* 32-bit relocation to the symbol's procedure linkage table.  */
585   HOWTO (R_PPC64_PLT32,         /* type */
586          0,                     /* rightshift */
587          2,                     /* size (0 = byte, 1 = short, 2 = long) */
588          32,                    /* bitsize */
589          FALSE,                 /* pc_relative */
590          0,                     /* bitpos */
591          complain_overflow_bitfield, /* complain_on_overflow */
592          ppc64_elf_unhandled_reloc, /* special_function */
593          "R_PPC64_PLT32",       /* name */
594          FALSE,                 /* partial_inplace */
595          0,                     /* src_mask */
596          0xffffffff,            /* dst_mask */
597          FALSE),                /* pcrel_offset */
598
599   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
600      FIXME: R_PPC64_PLTREL32 not supported.  */
601   HOWTO (R_PPC64_PLTREL32,      /* type */
602          0,                     /* rightshift */
603          2,                     /* size (0 = byte, 1 = short, 2 = long) */
604          32,                    /* bitsize */
605          TRUE,                  /* pc_relative */
606          0,                     /* bitpos */
607          complain_overflow_signed, /* complain_on_overflow */
608          bfd_elf_generic_reloc, /* special_function */
609          "R_PPC64_PLTREL32",    /* name */
610          FALSE,                 /* partial_inplace */
611          0,                     /* src_mask */
612          0xffffffff,            /* dst_mask */
613          TRUE),                 /* pcrel_offset */
614
615   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
616      the symbol.  */
617   HOWTO (R_PPC64_PLT16_LO,      /* type */
618          0,                     /* rightshift */
619          1,                     /* size (0 = byte, 1 = short, 2 = long) */
620          16,                    /* bitsize */
621          FALSE,                 /* pc_relative */
622          0,                     /* bitpos */
623          complain_overflow_dont, /* complain_on_overflow */
624          ppc64_elf_unhandled_reloc, /* special_function */
625          "R_PPC64_PLT16_LO",    /* name */
626          FALSE,                 /* partial_inplace */
627          0,                     /* src_mask */
628          0xffff,                /* dst_mask */
629          FALSE),                /* pcrel_offset */
630
631   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
632      the symbol.  */
633   HOWTO (R_PPC64_PLT16_HI,      /* type */
634          16,                    /* rightshift */
635          1,                     /* size (0 = byte, 1 = short, 2 = long) */
636          16,                    /* bitsize */
637          FALSE,                 /* pc_relative */
638          0,                     /* bitpos */
639          complain_overflow_dont, /* complain_on_overflow */
640          ppc64_elf_unhandled_reloc, /* special_function */
641          "R_PPC64_PLT16_HI",    /* name */
642          FALSE,                 /* partial_inplace */
643          0,                     /* src_mask */
644          0xffff,                /* dst_mask */
645          FALSE),                /* pcrel_offset */
646
647   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
648      the symbol.  */
649   HOWTO (R_PPC64_PLT16_HA,      /* type */
650          16,                    /* rightshift */
651          1,                     /* size (0 = byte, 1 = short, 2 = long) */
652          16,                    /* bitsize */
653          FALSE,                 /* pc_relative */
654          0,                     /* bitpos */
655          complain_overflow_dont, /* complain_on_overflow */
656          ppc64_elf_unhandled_reloc, /* special_function */
657          "R_PPC64_PLT16_HA",    /* name */
658          FALSE,                 /* partial_inplace */
659          0,                     /* src_mask */
660          0xffff,                /* dst_mask */
661          FALSE),                /* pcrel_offset */
662
663   /* 16-bit section relative relocation.  */
664   HOWTO (R_PPC64_SECTOFF,       /* type */
665          0,                     /* rightshift */
666          1,                     /* size (0 = byte, 1 = short, 2 = long) */
667          16,                    /* bitsize */
668          FALSE,                 /* pc_relative */
669          0,                     /* bitpos */
670          complain_overflow_bitfield, /* complain_on_overflow */
671          ppc64_elf_sectoff_reloc, /* special_function */
672          "R_PPC64_SECTOFF",     /* name */
673          FALSE,                 /* partial_inplace */
674          0,                     /* src_mask */
675          0xffff,                /* dst_mask */
676          FALSE),                /* pcrel_offset */
677
678   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
679   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
680          0,                     /* rightshift */
681          1,                     /* size (0 = byte, 1 = short, 2 = long) */
682          16,                    /* bitsize */
683          FALSE,                 /* pc_relative */
684          0,                     /* bitpos */
685          complain_overflow_dont, /* complain_on_overflow */
686          ppc64_elf_sectoff_reloc, /* special_function */
687          "R_PPC64_SECTOFF_LO",  /* name */
688          FALSE,                 /* partial_inplace */
689          0,                     /* src_mask */
690          0xffff,                /* dst_mask */
691          FALSE),                /* pcrel_offset */
692
693   /* 16-bit upper half section relative relocation.  */
694   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
695          16,                    /* rightshift */
696          1,                     /* size (0 = byte, 1 = short, 2 = long) */
697          16,                    /* bitsize */
698          FALSE,                 /* pc_relative */
699          0,                     /* bitpos */
700          complain_overflow_dont, /* complain_on_overflow */
701          ppc64_elf_sectoff_reloc, /* special_function */
702          "R_PPC64_SECTOFF_HI",  /* name */
703          FALSE,                 /* partial_inplace */
704          0,                     /* src_mask */
705          0xffff,                /* dst_mask */
706          FALSE),                /* pcrel_offset */
707
708   /* 16-bit upper half adjusted section relative relocation.  */
709   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
710          16,                    /* rightshift */
711          1,                     /* size (0 = byte, 1 = short, 2 = long) */
712          16,                    /* bitsize */
713          FALSE,                 /* pc_relative */
714          0,                     /* bitpos */
715          complain_overflow_dont, /* complain_on_overflow */
716          ppc64_elf_sectoff_ha_reloc, /* special_function */
717          "R_PPC64_SECTOFF_HA",  /* name */
718          FALSE,                 /* partial_inplace */
719          0,                     /* src_mask */
720          0xffff,                /* dst_mask */
721          FALSE),                /* pcrel_offset */
722
723   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
724   HOWTO (R_PPC64_REL30,         /* type */
725          2,                     /* rightshift */
726          2,                     /* size (0 = byte, 1 = short, 2 = long) */
727          30,                    /* bitsize */
728          TRUE,                  /* pc_relative */
729          0,                     /* bitpos */
730          complain_overflow_dont, /* complain_on_overflow */
731          bfd_elf_generic_reloc, /* special_function */
732          "R_PPC64_REL30",       /* name */
733          FALSE,                 /* partial_inplace */
734          0,                     /* src_mask */
735          0xfffffffc,            /* dst_mask */
736          TRUE),                 /* pcrel_offset */
737
738   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
739
740   /* A standard 64-bit relocation.  */
741   HOWTO (R_PPC64_ADDR64,        /* type */
742          0,                     /* rightshift */
743          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
744          64,                    /* bitsize */
745          FALSE,                 /* pc_relative */
746          0,                     /* bitpos */
747          complain_overflow_dont, /* complain_on_overflow */
748          bfd_elf_generic_reloc, /* special_function */
749          "R_PPC64_ADDR64",      /* name */
750          FALSE,                 /* partial_inplace */
751          0,                     /* src_mask */
752          ONES (64),             /* dst_mask */
753          FALSE),                /* pcrel_offset */
754
755   /* The bits 32-47 of an address.  */
756   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
757          32,                    /* rightshift */
758          1,                     /* size (0 = byte, 1 = short, 2 = long) */
759          16,                    /* bitsize */
760          FALSE,                 /* pc_relative */
761          0,                     /* bitpos */
762          complain_overflow_dont, /* complain_on_overflow */
763          bfd_elf_generic_reloc, /* special_function */
764          "R_PPC64_ADDR16_HIGHER", /* name */
765          FALSE,                 /* partial_inplace */
766          0,                     /* src_mask */
767          0xffff,                /* dst_mask */
768          FALSE),                /* pcrel_offset */
769
770   /* The bits 32-47 of an address, plus 1 if the contents of the low
771      16 bits, treated as a signed number, is negative.  */
772   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
773          32,                    /* rightshift */
774          1,                     /* size (0 = byte, 1 = short, 2 = long) */
775          16,                    /* bitsize */
776          FALSE,                 /* pc_relative */
777          0,                     /* bitpos */
778          complain_overflow_dont, /* complain_on_overflow */
779          ppc64_elf_ha_reloc,    /* special_function */
780          "R_PPC64_ADDR16_HIGHERA", /* name */
781          FALSE,                 /* partial_inplace */
782          0,                     /* src_mask */
783          0xffff,                /* dst_mask */
784          FALSE),                /* pcrel_offset */
785
786   /* The bits 48-63 of an address.  */
787   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
788          48,                    /* rightshift */
789          1,                     /* size (0 = byte, 1 = short, 2 = long) */
790          16,                    /* bitsize */
791          FALSE,                 /* pc_relative */
792          0,                     /* bitpos */
793          complain_overflow_dont, /* complain_on_overflow */
794          bfd_elf_generic_reloc, /* special_function */
795          "R_PPC64_ADDR16_HIGHEST", /* name */
796          FALSE,                 /* partial_inplace */
797          0,                     /* src_mask */
798          0xffff,                /* dst_mask */
799          FALSE),                /* pcrel_offset */
800
801   /* The bits 48-63 of an address, plus 1 if the contents of the low
802      16 bits, treated as a signed number, is negative.  */
803   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
804          48,                    /* rightshift */
805          1,                     /* size (0 = byte, 1 = short, 2 = long) */
806          16,                    /* bitsize */
807          FALSE,                 /* pc_relative */
808          0,                     /* bitpos */
809          complain_overflow_dont, /* complain_on_overflow */
810          ppc64_elf_ha_reloc,    /* special_function */
811          "R_PPC64_ADDR16_HIGHESTA", /* name */
812          FALSE,                 /* partial_inplace */
813          0,                     /* src_mask */
814          0xffff,                /* dst_mask */
815          FALSE),                /* pcrel_offset */
816
817   /* Like ADDR64, but may be unaligned.  */
818   HOWTO (R_PPC64_UADDR64,       /* type */
819          0,                     /* rightshift */
820          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
821          64,                    /* bitsize */
822          FALSE,                 /* pc_relative */
823          0,                     /* bitpos */
824          complain_overflow_dont, /* complain_on_overflow */
825          bfd_elf_generic_reloc, /* special_function */
826          "R_PPC64_UADDR64",     /* name */
827          FALSE,                 /* partial_inplace */
828          0,                     /* src_mask */
829          ONES (64),             /* dst_mask */
830          FALSE),                /* pcrel_offset */
831
832   /* 64-bit relative relocation.  */
833   HOWTO (R_PPC64_REL64,         /* type */
834          0,                     /* rightshift */
835          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
836          64,                    /* bitsize */
837          TRUE,                  /* pc_relative */
838          0,                     /* bitpos */
839          complain_overflow_dont, /* complain_on_overflow */
840          bfd_elf_generic_reloc, /* special_function */
841          "R_PPC64_REL64",       /* name */
842          FALSE,                 /* partial_inplace */
843          0,                     /* src_mask */
844          ONES (64),             /* dst_mask */
845          TRUE),                 /* pcrel_offset */
846
847   /* 64-bit relocation to the symbol's procedure linkage table.  */
848   HOWTO (R_PPC64_PLT64,         /* type */
849          0,                     /* rightshift */
850          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
851          64,                    /* bitsize */
852          FALSE,                 /* pc_relative */
853          0,                     /* bitpos */
854          complain_overflow_dont, /* complain_on_overflow */
855          ppc64_elf_unhandled_reloc, /* special_function */
856          "R_PPC64_PLT64",       /* name */
857          FALSE,                 /* partial_inplace */
858          0,                     /* src_mask */
859          ONES (64),             /* dst_mask */
860          FALSE),                /* pcrel_offset */
861
862   /* 64-bit PC relative relocation to the symbol's procedure linkage
863      table.  */
864   /* FIXME: R_PPC64_PLTREL64 not supported.  */
865   HOWTO (R_PPC64_PLTREL64,      /* type */
866          0,                     /* rightshift */
867          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
868          64,                    /* bitsize */
869          TRUE,                  /* pc_relative */
870          0,                     /* bitpos */
871          complain_overflow_dont, /* complain_on_overflow */
872          ppc64_elf_unhandled_reloc, /* special_function */
873          "R_PPC64_PLTREL64",    /* name */
874          FALSE,                 /* partial_inplace */
875          0,                     /* src_mask */
876          ONES (64),             /* dst_mask */
877          TRUE),                 /* pcrel_offset */
878
879   /* 16 bit TOC-relative relocation.  */
880
881   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
882   HOWTO (R_PPC64_TOC16,         /* type */
883          0,                     /* rightshift */
884          1,                     /* size (0 = byte, 1 = short, 2 = long) */
885          16,                    /* bitsize */
886          FALSE,                 /* pc_relative */
887          0,                     /* bitpos */
888          complain_overflow_signed, /* complain_on_overflow */
889          ppc64_elf_toc_reloc,   /* special_function */
890          "R_PPC64_TOC16",       /* name */
891          FALSE,                 /* partial_inplace */
892          0,                     /* src_mask */
893          0xffff,                /* dst_mask */
894          FALSE),                /* pcrel_offset */
895
896   /* 16 bit TOC-relative relocation without overflow.  */
897
898   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
899   HOWTO (R_PPC64_TOC16_LO,      /* type */
900          0,                     /* rightshift */
901          1,                     /* size (0 = byte, 1 = short, 2 = long) */
902          16,                    /* bitsize */
903          FALSE,                 /* pc_relative */
904          0,                     /* bitpos */
905          complain_overflow_dont, /* complain_on_overflow */
906          ppc64_elf_toc_reloc,   /* special_function */
907          "R_PPC64_TOC16_LO",    /* name */
908          FALSE,                 /* partial_inplace */
909          0,                     /* src_mask */
910          0xffff,                /* dst_mask */
911          FALSE),                /* pcrel_offset */
912
913   /* 16 bit TOC-relative relocation, high 16 bits.  */
914
915   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
916   HOWTO (R_PPC64_TOC16_HI,      /* type */
917          16,                    /* rightshift */
918          1,                     /* size (0 = byte, 1 = short, 2 = long) */
919          16,                    /* bitsize */
920          FALSE,                 /* pc_relative */
921          0,                     /* bitpos */
922          complain_overflow_dont, /* complain_on_overflow */
923          ppc64_elf_toc_reloc,   /* special_function */
924          "R_PPC64_TOC16_HI",    /* name */
925          FALSE,                 /* partial_inplace */
926          0,                     /* src_mask */
927          0xffff,                /* dst_mask */
928          FALSE),                /* pcrel_offset */
929
930   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
931      contents of the low 16 bits, treated as a signed number, is
932      negative.  */
933
934   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
935   HOWTO (R_PPC64_TOC16_HA,      /* type */
936          16,                    /* rightshift */
937          1,                     /* size (0 = byte, 1 = short, 2 = long) */
938          16,                    /* bitsize */
939          FALSE,                 /* pc_relative */
940          0,                     /* bitpos */
941          complain_overflow_dont, /* complain_on_overflow */
942          ppc64_elf_toc_ha_reloc, /* special_function */
943          "R_PPC64_TOC16_HA",    /* name */
944          FALSE,                 /* partial_inplace */
945          0,                     /* src_mask */
946          0xffff,                /* dst_mask */
947          FALSE),                /* pcrel_offset */
948
949   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
950
951   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
952   HOWTO (R_PPC64_TOC,           /* type */
953          0,                     /* rightshift */
954          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
955          64,                    /* bitsize */
956          FALSE,                 /* pc_relative */
957          0,                     /* bitpos */
958          complain_overflow_bitfield, /* complain_on_overflow */
959          ppc64_elf_toc64_reloc, /* special_function */
960          "R_PPC64_TOC",         /* name */
961          FALSE,                 /* partial_inplace */
962          0,                     /* src_mask */
963          ONES (64),             /* dst_mask */
964          FALSE),                /* pcrel_offset */
965
966   /* Like R_PPC64_GOT16, but also informs the link editor that the
967      value to relocate may (!) refer to a PLT entry which the link
968      editor (a) may replace with the symbol value.  If the link editor
969      is unable to fully resolve the symbol, it may (b) create a PLT
970      entry and store the address to the new PLT entry in the GOT.
971      This permits lazy resolution of function symbols at run time.
972      The link editor may also skip all of this and just (c) emit a
973      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
974   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
975     HOWTO (R_PPC64_PLTGOT16,    /* type */
976          0,                     /* rightshift */
977          1,                     /* size (0 = byte, 1 = short, 2 = long) */
978          16,                    /* bitsize */
979          FALSE,                 /* pc_relative */
980          0,                     /* bitpos */
981          complain_overflow_signed, /* complain_on_overflow */
982          ppc64_elf_unhandled_reloc, /* special_function */
983          "R_PPC64_PLTGOT16",    /* name */
984          FALSE,                 /* partial_inplace */
985          0,                     /* src_mask */
986          0xffff,                /* dst_mask */
987          FALSE),                /* pcrel_offset */
988
989   /* Like R_PPC64_PLTGOT16, but without overflow.  */
990   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
991   HOWTO (R_PPC64_PLTGOT16_LO,   /* 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_dont, /* complain_on_overflow */
998          ppc64_elf_unhandled_reloc, /* special_function */
999          "R_PPC64_PLTGOT16_LO", /* name */
1000          FALSE,                 /* partial_inplace */
1001          0,                     /* src_mask */
1002          0xffff,                /* dst_mask */
1003          FALSE),                /* pcrel_offset */
1004
1005   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1006   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1007   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1008          16,                    /* rightshift */
1009          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1010          16,                    /* bitsize */
1011          FALSE,                 /* pc_relative */
1012          0,                     /* bitpos */
1013          complain_overflow_dont, /* complain_on_overflow */
1014          ppc64_elf_unhandled_reloc, /* special_function */
1015          "R_PPC64_PLTGOT16_HI", /* name */
1016          FALSE,                 /* partial_inplace */
1017          0,                     /* src_mask */
1018          0xffff,                /* dst_mask */
1019          FALSE),                /* pcrel_offset */
1020
1021   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1022      1 if the contents of the low 16 bits, treated as a signed number,
1023      is negative.  */
1024   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1025   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1026          16,                    /* rightshift */
1027          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1028          16,                    /* bitsize */
1029          FALSE,                 /* pc_relative */
1030          0,                     /* bitpos */
1031          complain_overflow_dont,/* complain_on_overflow */
1032          ppc64_elf_unhandled_reloc, /* special_function */
1033          "R_PPC64_PLTGOT16_HA", /* name */
1034          FALSE,                 /* partial_inplace */
1035          0,                     /* src_mask */
1036          0xffff,                /* dst_mask */
1037          FALSE),                /* pcrel_offset */
1038
1039   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1040   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1041          0,                     /* rightshift */
1042          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1043          16,                    /* bitsize */
1044          FALSE,                 /* pc_relative */
1045          0,                     /* bitpos */
1046          complain_overflow_bitfield, /* complain_on_overflow */
1047          bfd_elf_generic_reloc, /* special_function */
1048          "R_PPC64_ADDR16_DS",   /* name */
1049          FALSE,                 /* partial_inplace */
1050          0,                     /* src_mask */
1051          0xfffc,                /* dst_mask */
1052          FALSE),                /* pcrel_offset */
1053
1054   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1055   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1056          0,                     /* rightshift */
1057          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1058          16,                    /* bitsize */
1059          FALSE,                 /* pc_relative */
1060          0,                     /* bitpos */
1061          complain_overflow_dont,/* complain_on_overflow */
1062          bfd_elf_generic_reloc, /* special_function */
1063          "R_PPC64_ADDR16_LO_DS",/* name */
1064          FALSE,                 /* partial_inplace */
1065          0,                     /* src_mask */
1066          0xfffc,                /* dst_mask */
1067          FALSE),                /* pcrel_offset */
1068
1069   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1070   HOWTO (R_PPC64_GOT16_DS,      /* type */
1071          0,                     /* rightshift */
1072          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1073          16,                    /* bitsize */
1074          FALSE,                 /* pc_relative */
1075          0,                     /* bitpos */
1076          complain_overflow_signed, /* complain_on_overflow */
1077          ppc64_elf_unhandled_reloc, /* special_function */
1078          "R_PPC64_GOT16_DS",    /* name */
1079          FALSE,                 /* partial_inplace */
1080          0,                     /* src_mask */
1081          0xfffc,                /* dst_mask */
1082          FALSE),                /* pcrel_offset */
1083
1084   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1085   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1086          0,                     /* rightshift */
1087          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1088          16,                    /* bitsize */
1089          FALSE,                 /* pc_relative */
1090          0,                     /* bitpos */
1091          complain_overflow_dont, /* complain_on_overflow */
1092          ppc64_elf_unhandled_reloc, /* special_function */
1093          "R_PPC64_GOT16_LO_DS", /* name */
1094          FALSE,                 /* partial_inplace */
1095          0,                     /* src_mask */
1096          0xfffc,                /* dst_mask */
1097          FALSE),                /* pcrel_offset */
1098
1099   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1100   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1101          0,                     /* rightshift */
1102          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1103          16,                    /* bitsize */
1104          FALSE,                 /* pc_relative */
1105          0,                     /* bitpos */
1106          complain_overflow_dont, /* complain_on_overflow */
1107          ppc64_elf_unhandled_reloc, /* special_function */
1108          "R_PPC64_PLT16_LO_DS", /* name */
1109          FALSE,                 /* partial_inplace */
1110          0,                     /* src_mask */
1111          0xfffc,                /* dst_mask */
1112          FALSE),                /* pcrel_offset */
1113
1114   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1115   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1116          0,                     /* rightshift */
1117          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1118          16,                    /* bitsize */
1119          FALSE,                 /* pc_relative */
1120          0,                     /* bitpos */
1121          complain_overflow_bitfield, /* complain_on_overflow */
1122          ppc64_elf_sectoff_reloc, /* special_function */
1123          "R_PPC64_SECTOFF_DS",  /* name */
1124          FALSE,                 /* partial_inplace */
1125          0,                     /* src_mask */
1126          0xfffc,                /* dst_mask */
1127          FALSE),                /* pcrel_offset */
1128
1129   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1130   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1131          0,                     /* rightshift */
1132          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1133          16,                    /* bitsize */
1134          FALSE,                 /* pc_relative */
1135          0,                     /* bitpos */
1136          complain_overflow_dont, /* complain_on_overflow */
1137          ppc64_elf_sectoff_reloc, /* special_function */
1138          "R_PPC64_SECTOFF_LO_DS",/* name */
1139          FALSE,                 /* partial_inplace */
1140          0,                     /* src_mask */
1141          0xfffc,                /* dst_mask */
1142          FALSE),                /* pcrel_offset */
1143
1144   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1145   HOWTO (R_PPC64_TOC16_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_signed, /* complain_on_overflow */
1152          ppc64_elf_toc_reloc,   /* special_function */
1153          "R_PPC64_TOC16_DS",    /* name */
1154          FALSE,                 /* partial_inplace */
1155          0,                     /* src_mask */
1156          0xfffc,                /* dst_mask */
1157          FALSE),                /* pcrel_offset */
1158
1159   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1160   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1161          0,                     /* rightshift */
1162          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1163          16,                    /* bitsize */
1164          FALSE,                 /* pc_relative */
1165          0,                     /* bitpos */
1166          complain_overflow_dont, /* complain_on_overflow */
1167          ppc64_elf_toc_reloc,   /* special_function */
1168          "R_PPC64_TOC16_LO_DS", /* name */
1169          FALSE,                 /* partial_inplace */
1170          0,                     /* src_mask */
1171          0xfffc,                /* dst_mask */
1172          FALSE),                /* pcrel_offset */
1173
1174   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1175   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1176     HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1177          0,                     /* rightshift */
1178          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1179          16,                    /* bitsize */
1180          FALSE,                 /* pc_relative */
1181          0,                     /* bitpos */
1182          complain_overflow_signed, /* complain_on_overflow */
1183          ppc64_elf_unhandled_reloc, /* special_function */
1184          "R_PPC64_PLTGOT16_DS", /* name */
1185          FALSE,                 /* partial_inplace */
1186          0,                     /* src_mask */
1187          0xfffc,                /* dst_mask */
1188          FALSE),                /* pcrel_offset */
1189
1190   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1191   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1192   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1193          0,                     /* rightshift */
1194          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1195          16,                    /* bitsize */
1196          FALSE,                 /* pc_relative */
1197          0,                     /* bitpos */
1198          complain_overflow_dont, /* complain_on_overflow */
1199          ppc64_elf_unhandled_reloc, /* special_function */
1200          "R_PPC64_PLTGOT16_LO_DS",/* name */
1201          FALSE,                 /* partial_inplace */
1202          0,                     /* src_mask */
1203          0xfffc,                /* dst_mask */
1204          FALSE),                /* pcrel_offset */
1205
1206   /* Marker reloc for TLS.  */
1207   HOWTO (R_PPC64_TLS,
1208          0,                     /* rightshift */
1209          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1210          32,                    /* bitsize */
1211          FALSE,                 /* pc_relative */
1212          0,                     /* bitpos */
1213          complain_overflow_dont, /* complain_on_overflow */
1214          bfd_elf_generic_reloc, /* special_function */
1215          "R_PPC64_TLS",         /* name */
1216          FALSE,                 /* partial_inplace */
1217          0,                     /* src_mask */
1218          0,                     /* dst_mask */
1219          FALSE),                /* pcrel_offset */
1220
1221   /* Computes the load module index of the load module that contains the
1222      definition of its TLS sym.  */
1223   HOWTO (R_PPC64_DTPMOD64,
1224          0,                     /* rightshift */
1225          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1226          64,                    /* bitsize */
1227          FALSE,                 /* pc_relative */
1228          0,                     /* bitpos */
1229          complain_overflow_dont, /* complain_on_overflow */
1230          ppc64_elf_unhandled_reloc, /* special_function */
1231          "R_PPC64_DTPMOD64",    /* name */
1232          FALSE,                 /* partial_inplace */
1233          0,                     /* src_mask */
1234          ONES (64),             /* dst_mask */
1235          FALSE),                /* pcrel_offset */
1236
1237   /* Computes a dtv-relative displacement, the difference between the value
1238      of sym+add and the base address of the thread-local storage block that
1239      contains the definition of sym, minus 0x8000.  */
1240   HOWTO (R_PPC64_DTPREL64,
1241          0,                     /* rightshift */
1242          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1243          64,                    /* bitsize */
1244          FALSE,                 /* pc_relative */
1245          0,                     /* bitpos */
1246          complain_overflow_dont, /* complain_on_overflow */
1247          ppc64_elf_unhandled_reloc, /* special_function */
1248          "R_PPC64_DTPREL64",    /* name */
1249          FALSE,                 /* partial_inplace */
1250          0,                     /* src_mask */
1251          ONES (64),             /* dst_mask */
1252          FALSE),                /* pcrel_offset */
1253
1254   /* A 16 bit dtprel reloc.  */
1255   HOWTO (R_PPC64_DTPREL16,
1256          0,                     /* rightshift */
1257          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1258          16,                    /* bitsize */
1259          FALSE,                 /* pc_relative */
1260          0,                     /* bitpos */
1261          complain_overflow_signed, /* complain_on_overflow */
1262          ppc64_elf_unhandled_reloc, /* special_function */
1263          "R_PPC64_DTPREL16",    /* name */
1264          FALSE,                 /* partial_inplace */
1265          0,                     /* src_mask */
1266          0xffff,                /* dst_mask */
1267          FALSE),                /* pcrel_offset */
1268
1269   /* Like DTPREL16, but no overflow.  */
1270   HOWTO (R_PPC64_DTPREL16_LO,
1271          0,                     /* rightshift */
1272          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1273          16,                    /* bitsize */
1274          FALSE,                 /* pc_relative */
1275          0,                     /* bitpos */
1276          complain_overflow_dont, /* complain_on_overflow */
1277          ppc64_elf_unhandled_reloc, /* special_function */
1278          "R_PPC64_DTPREL16_LO", /* name */
1279          FALSE,                 /* partial_inplace */
1280          0,                     /* src_mask */
1281          0xffff,                /* dst_mask */
1282          FALSE),                /* pcrel_offset */
1283
1284   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1285   HOWTO (R_PPC64_DTPREL16_HI,
1286          16,                    /* rightshift */
1287          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1288          16,                    /* bitsize */
1289          FALSE,                 /* pc_relative */
1290          0,                     /* bitpos */
1291          complain_overflow_dont, /* complain_on_overflow */
1292          ppc64_elf_unhandled_reloc, /* special_function */
1293          "R_PPC64_DTPREL16_HI", /* name */
1294          FALSE,                 /* partial_inplace */
1295          0,                     /* src_mask */
1296          0xffff,                /* dst_mask */
1297          FALSE),                /* pcrel_offset */
1298
1299   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1300   HOWTO (R_PPC64_DTPREL16_HA,
1301          16,                    /* rightshift */
1302          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1303          16,                    /* bitsize */
1304          FALSE,                 /* pc_relative */
1305          0,                     /* bitpos */
1306          complain_overflow_dont, /* complain_on_overflow */
1307          ppc64_elf_unhandled_reloc, /* special_function */
1308          "R_PPC64_DTPREL16_HA", /* name */
1309          FALSE,                 /* partial_inplace */
1310          0,                     /* src_mask */
1311          0xffff,                /* dst_mask */
1312          FALSE),                /* pcrel_offset */
1313
1314   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1315   HOWTO (R_PPC64_DTPREL16_HIGHER,
1316          32,                    /* rightshift */
1317          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1318          16,                    /* bitsize */
1319          FALSE,                 /* pc_relative */
1320          0,                     /* bitpos */
1321          complain_overflow_dont, /* complain_on_overflow */
1322          ppc64_elf_unhandled_reloc, /* special_function */
1323          "R_PPC64_DTPREL16_HIGHER", /* name */
1324          FALSE,                 /* partial_inplace */
1325          0,                     /* src_mask */
1326          0xffff,                /* dst_mask */
1327          FALSE),                /* pcrel_offset */
1328
1329   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1330   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1331          32,                    /* rightshift */
1332          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1333          16,                    /* bitsize */
1334          FALSE,                 /* pc_relative */
1335          0,                     /* bitpos */
1336          complain_overflow_dont, /* complain_on_overflow */
1337          ppc64_elf_unhandled_reloc, /* special_function */
1338          "R_PPC64_DTPREL16_HIGHERA", /* name */
1339          FALSE,                 /* partial_inplace */
1340          0,                     /* src_mask */
1341          0xffff,                /* dst_mask */
1342          FALSE),                /* pcrel_offset */
1343
1344   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1345   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1346          48,                    /* rightshift */
1347          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1348          16,                    /* bitsize */
1349          FALSE,                 /* pc_relative */
1350          0,                     /* bitpos */
1351          complain_overflow_dont, /* complain_on_overflow */
1352          ppc64_elf_unhandled_reloc, /* special_function */
1353          "R_PPC64_DTPREL16_HIGHEST", /* name */
1354          FALSE,                 /* partial_inplace */
1355          0,                     /* src_mask */
1356          0xffff,                /* dst_mask */
1357          FALSE),                /* pcrel_offset */
1358
1359   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1360   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1361          48,                    /* rightshift */
1362          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1363          16,                    /* bitsize */
1364          FALSE,                 /* pc_relative */
1365          0,                     /* bitpos */
1366          complain_overflow_dont, /* complain_on_overflow */
1367          ppc64_elf_unhandled_reloc, /* special_function */
1368          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1369          FALSE,                 /* partial_inplace */
1370          0,                     /* src_mask */
1371          0xffff,                /* dst_mask */
1372          FALSE),                /* pcrel_offset */
1373
1374   /* Like DTPREL16, but for insns with a DS field.  */
1375   HOWTO (R_PPC64_DTPREL16_DS,
1376          0,                     /* rightshift */
1377          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1378          16,                    /* bitsize */
1379          FALSE,                 /* pc_relative */
1380          0,                     /* bitpos */
1381          complain_overflow_signed, /* complain_on_overflow */
1382          ppc64_elf_unhandled_reloc, /* special_function */
1383          "R_PPC64_DTPREL16_DS", /* name */
1384          FALSE,                 /* partial_inplace */
1385          0,                     /* src_mask */
1386          0xfffc,                /* dst_mask */
1387          FALSE),                /* pcrel_offset */
1388
1389   /* Like DTPREL16_DS, but no overflow.  */
1390   HOWTO (R_PPC64_DTPREL16_LO_DS,
1391          0,                     /* rightshift */
1392          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1393          16,                    /* bitsize */
1394          FALSE,                 /* pc_relative */
1395          0,                     /* bitpos */
1396          complain_overflow_dont, /* complain_on_overflow */
1397          ppc64_elf_unhandled_reloc, /* special_function */
1398          "R_PPC64_DTPREL16_LO_DS", /* name */
1399          FALSE,                 /* partial_inplace */
1400          0,                     /* src_mask */
1401          0xfffc,                /* dst_mask */
1402          FALSE),                /* pcrel_offset */
1403
1404   /* Computes a tp-relative displacement, the difference between the value of
1405      sym+add and the value of the thread pointer (r13).  */
1406   HOWTO (R_PPC64_TPREL64,
1407          0,                     /* rightshift */
1408          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1409          64,                    /* bitsize */
1410          FALSE,                 /* pc_relative */
1411          0,                     /* bitpos */
1412          complain_overflow_dont, /* complain_on_overflow */
1413          ppc64_elf_unhandled_reloc, /* special_function */
1414          "R_PPC64_TPREL64",     /* name */
1415          FALSE,                 /* partial_inplace */
1416          0,                     /* src_mask */
1417          ONES (64),             /* dst_mask */
1418          FALSE),                /* pcrel_offset */
1419
1420   /* A 16 bit tprel reloc.  */
1421   HOWTO (R_PPC64_TPREL16,
1422          0,                     /* rightshift */
1423          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1424          16,                    /* bitsize */
1425          FALSE,                 /* pc_relative */
1426          0,                     /* bitpos */
1427          complain_overflow_signed, /* complain_on_overflow */
1428          ppc64_elf_unhandled_reloc, /* special_function */
1429          "R_PPC64_TPREL16",     /* name */
1430          FALSE,                 /* partial_inplace */
1431          0,                     /* src_mask */
1432          0xffff,                /* dst_mask */
1433          FALSE),                /* pcrel_offset */
1434
1435   /* Like TPREL16, but no overflow.  */
1436   HOWTO (R_PPC64_TPREL16_LO,
1437          0,                     /* rightshift */
1438          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1439          16,                    /* bitsize */
1440          FALSE,                 /* pc_relative */
1441          0,                     /* bitpos */
1442          complain_overflow_dont, /* complain_on_overflow */
1443          ppc64_elf_unhandled_reloc, /* special_function */
1444          "R_PPC64_TPREL16_LO",  /* name */
1445          FALSE,                 /* partial_inplace */
1446          0,                     /* src_mask */
1447          0xffff,                /* dst_mask */
1448          FALSE),                /* pcrel_offset */
1449
1450   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1451   HOWTO (R_PPC64_TPREL16_HI,
1452          16,                    /* rightshift */
1453          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1454          16,                    /* bitsize */
1455          FALSE,                 /* pc_relative */
1456          0,                     /* bitpos */
1457          complain_overflow_dont, /* complain_on_overflow */
1458          ppc64_elf_unhandled_reloc, /* special_function */
1459          "R_PPC64_TPREL16_HI",  /* name */
1460          FALSE,                 /* partial_inplace */
1461          0,                     /* src_mask */
1462          0xffff,                /* dst_mask */
1463          FALSE),                /* pcrel_offset */
1464
1465   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1466   HOWTO (R_PPC64_TPREL16_HA,
1467          16,                    /* rightshift */
1468          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1469          16,                    /* bitsize */
1470          FALSE,                 /* pc_relative */
1471          0,                     /* bitpos */
1472          complain_overflow_dont, /* complain_on_overflow */
1473          ppc64_elf_unhandled_reloc, /* special_function */
1474          "R_PPC64_TPREL16_HA",  /* name */
1475          FALSE,                 /* partial_inplace */
1476          0,                     /* src_mask */
1477          0xffff,                /* dst_mask */
1478          FALSE),                /* pcrel_offset */
1479
1480   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1481   HOWTO (R_PPC64_TPREL16_HIGHER,
1482          32,                    /* rightshift */
1483          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1484          16,                    /* bitsize */
1485          FALSE,                 /* pc_relative */
1486          0,                     /* bitpos */
1487          complain_overflow_dont, /* complain_on_overflow */
1488          ppc64_elf_unhandled_reloc, /* special_function */
1489          "R_PPC64_TPREL16_HIGHER",      /* name */
1490          FALSE,                 /* partial_inplace */
1491          0,                     /* src_mask */
1492          0xffff,                /* dst_mask */
1493          FALSE),                /* pcrel_offset */
1494
1495   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1496   HOWTO (R_PPC64_TPREL16_HIGHERA,
1497          32,                    /* rightshift */
1498          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1499          16,                    /* bitsize */
1500          FALSE,                 /* pc_relative */
1501          0,                     /* bitpos */
1502          complain_overflow_dont, /* complain_on_overflow */
1503          ppc64_elf_unhandled_reloc, /* special_function */
1504          "R_PPC64_TPREL16_HIGHERA", /* name */
1505          FALSE,                 /* partial_inplace */
1506          0,                     /* src_mask */
1507          0xffff,                /* dst_mask */
1508          FALSE),                /* pcrel_offset */
1509
1510   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1511   HOWTO (R_PPC64_TPREL16_HIGHEST,
1512          48,                    /* rightshift */
1513          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1514          16,                    /* bitsize */
1515          FALSE,                 /* pc_relative */
1516          0,                     /* bitpos */
1517          complain_overflow_dont, /* complain_on_overflow */
1518          ppc64_elf_unhandled_reloc, /* special_function */
1519          "R_PPC64_TPREL16_HIGHEST", /* name */
1520          FALSE,                 /* partial_inplace */
1521          0,                     /* src_mask */
1522          0xffff,                /* dst_mask */
1523          FALSE),                /* pcrel_offset */
1524
1525   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1526   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1527          48,                    /* rightshift */
1528          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1529          16,                    /* bitsize */
1530          FALSE,                 /* pc_relative */
1531          0,                     /* bitpos */
1532          complain_overflow_dont, /* complain_on_overflow */
1533          ppc64_elf_unhandled_reloc, /* special_function */
1534          "R_PPC64_TPREL16_HIGHESTA", /* name */
1535          FALSE,                 /* partial_inplace */
1536          0,                     /* src_mask */
1537          0xffff,                /* dst_mask */
1538          FALSE),                /* pcrel_offset */
1539
1540   /* Like TPREL16, but for insns with a DS field.  */
1541   HOWTO (R_PPC64_TPREL16_DS,
1542          0,                     /* rightshift */
1543          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1544          16,                    /* bitsize */
1545          FALSE,                 /* pc_relative */
1546          0,                     /* bitpos */
1547          complain_overflow_signed, /* complain_on_overflow */
1548          ppc64_elf_unhandled_reloc, /* special_function */
1549          "R_PPC64_TPREL16_DS",  /* name */
1550          FALSE,                 /* partial_inplace */
1551          0,                     /* src_mask */
1552          0xfffc,                /* dst_mask */
1553          FALSE),                /* pcrel_offset */
1554
1555   /* Like TPREL16_DS, but no overflow.  */
1556   HOWTO (R_PPC64_TPREL16_LO_DS,
1557          0,                     /* rightshift */
1558          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1559          16,                    /* bitsize */
1560          FALSE,                 /* pc_relative */
1561          0,                     /* bitpos */
1562          complain_overflow_dont, /* complain_on_overflow */
1563          ppc64_elf_unhandled_reloc, /* special_function */
1564          "R_PPC64_TPREL16_LO_DS", /* name */
1565          FALSE,                 /* partial_inplace */
1566          0,                     /* src_mask */
1567          0xfffc,                /* dst_mask */
1568          FALSE),                /* pcrel_offset */
1569
1570   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1571      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1572      to the first entry relative to the TOC base (r2).  */
1573   HOWTO (R_PPC64_GOT_TLSGD16,
1574          0,                     /* rightshift */
1575          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1576          16,                    /* bitsize */
1577          FALSE,                 /* pc_relative */
1578          0,                     /* bitpos */
1579          complain_overflow_signed, /* complain_on_overflow */
1580          ppc64_elf_unhandled_reloc, /* special_function */
1581          "R_PPC64_GOT_TLSGD16", /* name */
1582          FALSE,                 /* partial_inplace */
1583          0,                     /* src_mask */
1584          0xffff,                /* dst_mask */
1585          FALSE),                /* pcrel_offset */
1586
1587   /* Like GOT_TLSGD16, but no overflow.  */
1588   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1589          0,                     /* rightshift */
1590          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1591          16,                    /* bitsize */
1592          FALSE,                 /* pc_relative */
1593          0,                     /* bitpos */
1594          complain_overflow_dont, /* complain_on_overflow */
1595          ppc64_elf_unhandled_reloc, /* special_function */
1596          "R_PPC64_GOT_TLSGD16_LO", /* name */
1597          FALSE,                 /* partial_inplace */
1598          0,                     /* src_mask */
1599          0xffff,                /* dst_mask */
1600          FALSE),                /* pcrel_offset */
1601
1602   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1603   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1604          16,                    /* rightshift */
1605          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1606          16,                    /* bitsize */
1607          FALSE,                 /* pc_relative */
1608          0,                     /* bitpos */
1609          complain_overflow_dont, /* complain_on_overflow */
1610          ppc64_elf_unhandled_reloc, /* special_function */
1611          "R_PPC64_GOT_TLSGD16_HI", /* name */
1612          FALSE,                 /* partial_inplace */
1613          0,                     /* src_mask */
1614          0xffff,                /* dst_mask */
1615          FALSE),                /* pcrel_offset */
1616
1617   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1618   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1619          16,                    /* rightshift */
1620          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1621          16,                    /* bitsize */
1622          FALSE,                 /* pc_relative */
1623          0,                     /* bitpos */
1624          complain_overflow_dont, /* complain_on_overflow */
1625          ppc64_elf_unhandled_reloc, /* special_function */
1626          "R_PPC64_GOT_TLSGD16_HA", /* name */
1627          FALSE,                 /* partial_inplace */
1628          0,                     /* src_mask */
1629          0xffff,                /* dst_mask */
1630          FALSE),                /* pcrel_offset */
1631
1632   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1633      with values (sym+add)@dtpmod and zero, and computes the offset to the
1634      first entry relative to the TOC base (r2).  */
1635   HOWTO (R_PPC64_GOT_TLSLD16,
1636          0,                     /* rightshift */
1637          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1638          16,                    /* bitsize */
1639          FALSE,                 /* pc_relative */
1640          0,                     /* bitpos */
1641          complain_overflow_signed, /* complain_on_overflow */
1642          ppc64_elf_unhandled_reloc, /* special_function */
1643          "R_PPC64_GOT_TLSLD16", /* name */
1644          FALSE,                 /* partial_inplace */
1645          0,                     /* src_mask */
1646          0xffff,                /* dst_mask */
1647          FALSE),                /* pcrel_offset */
1648
1649   /* Like GOT_TLSLD16, but no overflow.  */
1650   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1651          0,                     /* rightshift */
1652          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1653          16,                    /* bitsize */
1654          FALSE,                 /* pc_relative */
1655          0,                     /* bitpos */
1656          complain_overflow_dont, /* complain_on_overflow */
1657          ppc64_elf_unhandled_reloc, /* special_function */
1658          "R_PPC64_GOT_TLSLD16_LO", /* name */
1659          FALSE,                 /* partial_inplace */
1660          0,                     /* src_mask */
1661          0xffff,                /* dst_mask */
1662          FALSE),                /* pcrel_offset */
1663
1664   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1665   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1666          16,                    /* rightshift */
1667          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1668          16,                    /* bitsize */
1669          FALSE,                 /* pc_relative */
1670          0,                     /* bitpos */
1671          complain_overflow_dont, /* complain_on_overflow */
1672          ppc64_elf_unhandled_reloc, /* special_function */
1673          "R_PPC64_GOT_TLSLD16_HI", /* name */
1674          FALSE,                 /* partial_inplace */
1675          0,                     /* src_mask */
1676          0xffff,                /* dst_mask */
1677          FALSE),                /* pcrel_offset */
1678
1679   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1680   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1681          16,                    /* rightshift */
1682          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1683          16,                    /* bitsize */
1684          FALSE,                 /* pc_relative */
1685          0,                     /* bitpos */
1686          complain_overflow_dont, /* complain_on_overflow */
1687          ppc64_elf_unhandled_reloc, /* special_function */
1688          "R_PPC64_GOT_TLSLD16_HA", /* name */
1689          FALSE,                 /* partial_inplace */
1690          0,                     /* src_mask */
1691          0xffff,                /* dst_mask */
1692          FALSE),                /* pcrel_offset */
1693
1694   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1695      the offset to the entry relative to the TOC base (r2).  */
1696   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1697          0,                     /* rightshift */
1698          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1699          16,                    /* bitsize */
1700          FALSE,                 /* pc_relative */
1701          0,                     /* bitpos */
1702          complain_overflow_signed, /* complain_on_overflow */
1703          ppc64_elf_unhandled_reloc, /* special_function */
1704          "R_PPC64_GOT_DTPREL16_DS", /* name */
1705          FALSE,                 /* partial_inplace */
1706          0,                     /* src_mask */
1707          0xfffc,                /* dst_mask */
1708          FALSE),                /* pcrel_offset */
1709
1710   /* Like GOT_DTPREL16_DS, but no overflow.  */
1711   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1712          0,                     /* rightshift */
1713          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1714          16,                    /* bitsize */
1715          FALSE,                 /* pc_relative */
1716          0,                     /* bitpos */
1717          complain_overflow_dont, /* complain_on_overflow */
1718          ppc64_elf_unhandled_reloc, /* special_function */
1719          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1720          FALSE,                 /* partial_inplace */
1721          0,                     /* src_mask */
1722          0xfffc,                /* dst_mask */
1723          FALSE),                /* pcrel_offset */
1724
1725   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1726   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1727          16,                    /* rightshift */
1728          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1729          16,                    /* bitsize */
1730          FALSE,                 /* pc_relative */
1731          0,                     /* bitpos */
1732          complain_overflow_dont, /* complain_on_overflow */
1733          ppc64_elf_unhandled_reloc, /* special_function */
1734          "R_PPC64_GOT_DTPREL16_HI", /* name */
1735          FALSE,                 /* partial_inplace */
1736          0,                     /* src_mask */
1737          0xffff,                /* dst_mask */
1738          FALSE),                /* pcrel_offset */
1739
1740   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1741   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1742          16,                    /* rightshift */
1743          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1744          16,                    /* bitsize */
1745          FALSE,                 /* pc_relative */
1746          0,                     /* bitpos */
1747          complain_overflow_dont, /* complain_on_overflow */
1748          ppc64_elf_unhandled_reloc, /* special_function */
1749          "R_PPC64_GOT_DTPREL16_HA", /* name */
1750          FALSE,                 /* partial_inplace */
1751          0,                     /* src_mask */
1752          0xffff,                /* dst_mask */
1753          FALSE),                /* pcrel_offset */
1754
1755   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1756      offset to the entry relative to the TOC base (r2).  */
1757   HOWTO (R_PPC64_GOT_TPREL16_DS,
1758          0,                     /* rightshift */
1759          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1760          16,                    /* bitsize */
1761          FALSE,                 /* pc_relative */
1762          0,                     /* bitpos */
1763          complain_overflow_signed, /* complain_on_overflow */
1764          ppc64_elf_unhandled_reloc, /* special_function */
1765          "R_PPC64_GOT_TPREL16_DS", /* name */
1766          FALSE,                 /* partial_inplace */
1767          0,                     /* src_mask */
1768          0xfffc,                /* dst_mask */
1769          FALSE),                /* pcrel_offset */
1770
1771   /* Like GOT_TPREL16_DS, but no overflow.  */
1772   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1773          0,                     /* rightshift */
1774          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1775          16,                    /* bitsize */
1776          FALSE,                 /* pc_relative */
1777          0,                     /* bitpos */
1778          complain_overflow_dont, /* complain_on_overflow */
1779          ppc64_elf_unhandled_reloc, /* special_function */
1780          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1781          FALSE,                 /* partial_inplace */
1782          0,                     /* src_mask */
1783          0xfffc,                /* dst_mask */
1784          FALSE),                /* pcrel_offset */
1785
1786   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1787   HOWTO (R_PPC64_GOT_TPREL16_HI,
1788          16,                    /* rightshift */
1789          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1790          16,                    /* bitsize */
1791          FALSE,                 /* pc_relative */
1792          0,                     /* bitpos */
1793          complain_overflow_dont, /* complain_on_overflow */
1794          ppc64_elf_unhandled_reloc, /* special_function */
1795          "R_PPC64_GOT_TPREL16_HI", /* name */
1796          FALSE,                 /* partial_inplace */
1797          0,                     /* src_mask */
1798          0xffff,                /* dst_mask */
1799          FALSE),                /* pcrel_offset */
1800
1801   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1802   HOWTO (R_PPC64_GOT_TPREL16_HA,
1803          16,                    /* rightshift */
1804          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1805          16,                    /* bitsize */
1806          FALSE,                 /* pc_relative */
1807          0,                     /* bitpos */
1808          complain_overflow_dont, /* complain_on_overflow */
1809          ppc64_elf_unhandled_reloc, /* special_function */
1810          "R_PPC64_GOT_TPREL16_HA", /* name */
1811          FALSE,                 /* partial_inplace */
1812          0,                     /* src_mask */
1813          0xffff,                /* dst_mask */
1814          FALSE),                /* pcrel_offset */
1815
1816   /* GNU extension to record C++ vtable hierarchy.  */
1817   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1818          0,                     /* rightshift */
1819          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1820          0,                     /* bitsize */
1821          FALSE,                 /* pc_relative */
1822          0,                     /* bitpos */
1823          complain_overflow_dont, /* complain_on_overflow */
1824          NULL,                  /* special_function */
1825          "R_PPC64_GNU_VTINHERIT", /* name */
1826          FALSE,                 /* partial_inplace */
1827          0,                     /* src_mask */
1828          0,                     /* dst_mask */
1829          FALSE),                /* pcrel_offset */
1830
1831   /* GNU extension to record C++ vtable member usage.  */
1832   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
1833          0,                     /* rightshift */
1834          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1835          0,                     /* bitsize */
1836          FALSE,                 /* pc_relative */
1837          0,                     /* bitpos */
1838          complain_overflow_dont, /* complain_on_overflow */
1839          NULL,                  /* special_function */
1840          "R_PPC64_GNU_VTENTRY", /* name */
1841          FALSE,                 /* partial_inplace */
1842          0,                     /* src_mask */
1843          0,                     /* dst_mask */
1844          FALSE),                /* pcrel_offset */
1845 };
1846
1847 \f
1848 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1849    be done.  */
1850
1851 static void
1852 ppc_howto_init (void)
1853 {
1854   unsigned int i, type;
1855
1856   for (i = 0;
1857        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1858        i++)
1859     {
1860       type = ppc64_elf_howto_raw[i].type;
1861       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1862                           / sizeof (ppc64_elf_howto_table[0])));
1863       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1864     }
1865 }
1866
1867 static reloc_howto_type *
1868 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1869                              bfd_reloc_code_real_type code)
1870 {
1871   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1872
1873   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1874     /* Initialize howto table if needed.  */
1875     ppc_howto_init ();
1876
1877   switch (code)
1878     {
1879     default:
1880       return NULL;
1881
1882     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
1883       break;
1884     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
1885       break;
1886     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
1887       break;
1888     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
1889       break;
1890     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
1891       break;
1892     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
1893       break;
1894     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
1895       break;
1896     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
1897       break;
1898     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
1899       break;
1900     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
1901       break;
1902     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
1903       break;
1904     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
1905       break;
1906     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
1907       break;
1908     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
1909       break;
1910     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
1911       break;
1912     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
1913       break;
1914     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
1915       break;
1916     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
1917       break;
1918     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
1919       break;
1920     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
1921       break;
1922     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
1923       break;
1924     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
1925       break;
1926     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
1927       break;
1928     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
1929       break;
1930     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
1931       break;
1932     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
1933       break;
1934     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
1935       break;
1936     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
1937       break;
1938     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
1939       break;
1940     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
1941       break;
1942     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
1943       break;
1944     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
1945       break;
1946     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
1947       break;
1948     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
1949       break;
1950     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
1951       break;
1952     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
1953       break;
1954     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
1955       break;
1956     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
1957       break;
1958     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
1959       break;
1960     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
1961       break;
1962     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
1963       break;
1964     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
1965       break;
1966     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
1967       break;
1968     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
1969       break;
1970     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
1971       break;
1972     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
1973       break;
1974     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
1975       break;
1976     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
1977       break;
1978     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
1979       break;
1980     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
1981       break;
1982     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
1983       break;
1984     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
1985       break;
1986     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
1987       break;
1988     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
1989       break;
1990     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
1991       break;
1992     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
1993       break;
1994     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
1995       break;
1996     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
1997       break;
1998     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
1999       break;
2000     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2001       break;
2002     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2003       break;
2004     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2005       break;
2006     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2007       break;
2008     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2009       break;
2010     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2011       break;
2012     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2013       break;
2014     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2015       break;
2016     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2017       break;
2018     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2019       break;
2020     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2021       break;
2022     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2023       break;
2024     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2025       break;
2026     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2027       break;
2028     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2029       break;
2030     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2031       break;
2032     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2033       break;
2034     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2035       break;
2036     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2037       break;
2038     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2039       break;
2040     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2041       break;
2042     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2043       break;
2044     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2045       break;
2046     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2047       break;
2048     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2049       break;
2050     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2051       break;
2052     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2053       break;
2054     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2055       break;
2056     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2057       break;
2058     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2059       break;
2060     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2061       break;
2062     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2063       break;
2064     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2065       break;
2066     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2067       break;
2068     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2069       break;
2070     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2071       break;
2072     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2073       break;
2074     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2075       break;
2076     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2077       break;
2078     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2079       break;
2080     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2081       break;
2082     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2083       break;
2084     }
2085
2086   return ppc64_elf_howto_table[r];
2087 };
2088
2089 /* Set the howto pointer for a PowerPC ELF reloc.  */
2090
2091 static void
2092 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2093                          Elf_Internal_Rela *dst)
2094 {
2095   unsigned int type;
2096
2097   /* Initialize howto table if needed.  */
2098   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2099     ppc_howto_init ();
2100
2101   type = ELF64_R_TYPE (dst->r_info);
2102   BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2103                       / sizeof (ppc64_elf_howto_table[0])));
2104   cache_ptr->howto = ppc64_elf_howto_table[type];
2105 }
2106
2107 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2108
2109 static bfd_reloc_status_type
2110 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2111                     void *data, asection *input_section,
2112                     bfd *output_bfd, char **error_message)
2113 {
2114   /* If this is a relocatable link (output_bfd test tells us), just
2115      call the generic function.  Any adjustment will be done at final
2116      link time.  */
2117   if (output_bfd != NULL)
2118     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2119                                   input_section, output_bfd, error_message);
2120
2121   /* Adjust the addend for sign extension of the low 16 bits.
2122      We won't actually be using the low 16 bits, so trashing them
2123      doesn't matter.  */
2124   reloc_entry->addend += 0x8000;
2125   return bfd_reloc_continue;
2126 }
2127
2128 static bfd_reloc_status_type
2129 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2130                          void *data, asection *input_section,
2131                          bfd *output_bfd, char **error_message)
2132 {
2133   long insn;
2134   enum elf_ppc64_reloc_type r_type;
2135   bfd_size_type octets;
2136   /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
2137   bfd_boolean is_power4 = FALSE;
2138
2139   /* If this is a relocatable link (output_bfd test tells us), just
2140      call the generic function.  Any adjustment will be done at final
2141      link time.  */
2142   if (output_bfd != NULL)
2143     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2144                                   input_section, output_bfd, error_message);
2145
2146   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2147   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2148   insn &= ~(0x01 << 21);
2149   r_type = reloc_entry->howto->type;
2150   if (r_type == R_PPC64_ADDR14_BRTAKEN
2151       || r_type == R_PPC64_REL14_BRTAKEN)
2152     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2153
2154   if (is_power4)
2155     {
2156       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2157          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2158          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2159       if ((insn & (0x14 << 21)) == (0x04 << 21))
2160         insn |= 0x02 << 21;
2161       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2162         insn |= 0x08 << 21;
2163       else
2164         return bfd_reloc_continue;
2165     }
2166   else
2167     {
2168       bfd_vma target = 0;
2169       bfd_vma from;
2170
2171       if (!bfd_is_com_section (symbol->section))
2172         target = symbol->value;
2173       target += symbol->section->output_section->vma;
2174       target += symbol->section->output_offset;
2175       target += reloc_entry->addend;
2176
2177       from = (reloc_entry->address
2178               + input_section->output_offset
2179               + input_section->output_section->vma);
2180
2181       /* Invert 'y' bit if not the default.  */
2182       if ((bfd_signed_vma) (target - from) < 0)
2183         insn ^= 0x01 << 21;
2184     }
2185   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2186   return bfd_reloc_continue;
2187 }
2188
2189 static bfd_reloc_status_type
2190 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2191                          void *data, asection *input_section,
2192                          bfd *output_bfd, char **error_message)
2193 {
2194   /* If this is a relocatable link (output_bfd test tells us), just
2195      call the generic function.  Any adjustment will be done at final
2196      link time.  */
2197   if (output_bfd != NULL)
2198     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2199                                   input_section, output_bfd, error_message);
2200
2201   /* Subtract the symbol section base address.  */
2202   reloc_entry->addend -= symbol->section->output_section->vma;
2203   return bfd_reloc_continue;
2204 }
2205
2206 static bfd_reloc_status_type
2207 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2208                             void *data, asection *input_section,
2209                             bfd *output_bfd, char **error_message)
2210 {
2211   /* If this is a relocatable link (output_bfd test tells us), just
2212      call the generic function.  Any adjustment will be done at final
2213      link time.  */
2214   if (output_bfd != NULL)
2215     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2216                                   input_section, output_bfd, error_message);
2217
2218   /* Subtract the symbol section base address.  */
2219   reloc_entry->addend -= symbol->section->output_section->vma;
2220
2221   /* Adjust the addend for sign extension of the low 16 bits.  */
2222   reloc_entry->addend += 0x8000;
2223   return bfd_reloc_continue;
2224 }
2225
2226 static bfd_reloc_status_type
2227 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2228                      void *data, asection *input_section,
2229                      bfd *output_bfd, char **error_message)
2230 {
2231   bfd_vma TOCstart;
2232
2233   /* If this is a relocatable link (output_bfd test tells us), just
2234      call the generic function.  Any adjustment will be done at final
2235      link time.  */
2236   if (output_bfd != NULL)
2237     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2238                                   input_section, output_bfd, error_message);
2239
2240   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2241   if (TOCstart == 0)
2242     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2243
2244   /* Subtract the TOC base address.  */
2245   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2246   return bfd_reloc_continue;
2247 }
2248
2249 static bfd_reloc_status_type
2250 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2251                         void *data, asection *input_section,
2252                         bfd *output_bfd, char **error_message)
2253 {
2254   bfd_vma TOCstart;
2255
2256   /* If this is a relocatable link (output_bfd test tells us), just
2257      call the generic function.  Any adjustment will be done at final
2258      link time.  */
2259   if (output_bfd != NULL)
2260     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2261                                   input_section, output_bfd, error_message);
2262
2263   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2264   if (TOCstart == 0)
2265     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2266
2267   /* Subtract the TOC base address.  */
2268   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2269
2270   /* Adjust the addend for sign extension of the low 16 bits.  */
2271   reloc_entry->addend += 0x8000;
2272   return bfd_reloc_continue;
2273 }
2274
2275 static bfd_reloc_status_type
2276 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2277                        void *data, asection *input_section,
2278                        bfd *output_bfd, char **error_message)
2279 {
2280   bfd_vma TOCstart;
2281   bfd_size_type octets;
2282
2283   /* If this is a relocatable link (output_bfd test tells us), just
2284      call the generic function.  Any adjustment will be done at final
2285      link time.  */
2286   if (output_bfd != NULL)
2287     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2288                                   input_section, output_bfd, error_message);
2289
2290   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2291   if (TOCstart == 0)
2292     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2293
2294   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2295   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2296   return bfd_reloc_ok;
2297 }
2298
2299 static bfd_reloc_status_type
2300 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2301                            void *data, asection *input_section,
2302                            bfd *output_bfd, char **error_message)
2303 {
2304   /* If this is a relocatable link (output_bfd test tells us), just
2305      call the generic function.  Any adjustment will be done at final
2306      link time.  */
2307   if (output_bfd != NULL)
2308     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2309                                   input_section, output_bfd, error_message);
2310
2311   if (error_message != NULL)
2312     {
2313       static char buf[60];
2314       sprintf (buf, "generic linker can't handle %s",
2315                reloc_entry->howto->name);
2316       *error_message = buf;
2317     }
2318   return bfd_reloc_dangerous;
2319 }
2320
2321 struct ppc64_elf_obj_tdata
2322 {
2323   struct elf_obj_tdata elf;
2324
2325   /* Shortcuts to dynamic linker sections.  */
2326   asection *got;
2327   asection *relgot;
2328
2329   /* TLS local dynamic got entry handling.  Suppose for multiple GOT
2330      sections means we potentially need one of these for each input bfd.  */
2331   union {
2332     bfd_signed_vma refcount;
2333     bfd_vma offset;
2334   } tlsld_got;
2335 };
2336
2337 #define ppc64_elf_tdata(bfd) \
2338   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2339
2340 #define ppc64_tlsld_got(bfd) \
2341   (&ppc64_elf_tdata (bfd)->tlsld_got)
2342
2343 /* Override the generic function because we store some extras.  */
2344
2345 static bfd_boolean
2346 ppc64_elf_mkobject (bfd *abfd)
2347 {
2348   bfd_size_type amt = sizeof (struct ppc64_elf_obj_tdata);
2349   abfd->tdata.any = bfd_zalloc (abfd, amt);
2350   if (abfd->tdata.any == NULL)
2351     return FALSE;
2352   return TRUE;
2353 }
2354
2355 /* Fix bad default arch selected for a 64 bit input bfd when the
2356    default is 32 bit.  */
2357
2358 static bfd_boolean
2359 ppc64_elf_object_p (bfd *abfd)
2360 {
2361   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2362     {
2363       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2364
2365       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2366         {
2367           /* Relies on arch after 32 bit default being 64 bit default.  */
2368           abfd->arch_info = abfd->arch_info->next;
2369           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2370         }
2371     }
2372   return TRUE;
2373 }
2374
2375 /* Merge backend specific data from an object file to the output
2376    object file when linking.  */
2377
2378 static bfd_boolean
2379 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2380 {
2381   /* Check if we have the same endianess.  */
2382   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2383       && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
2384       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2385     {
2386       const char *msg;
2387
2388       if (bfd_big_endian (ibfd))
2389         msg = _("%s: compiled for a big endian system "
2390                 "and target is little endian");
2391       else
2392         msg = _("%s: compiled for a little endian system "
2393                 "and target is big endian");
2394
2395       (*_bfd_error_handler) (msg, bfd_archive_filename (ibfd));
2396
2397       bfd_set_error (bfd_error_wrong_format);
2398       return FALSE;
2399     }
2400
2401   return TRUE;
2402 }
2403
2404 /* Add extra PPC sections.  */
2405
2406 static struct bfd_elf_special_section const ppc64_elf_special_sections[]=
2407 {
2408   { ".sdata",   6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2409   { ".sbss",    5, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2410   { ".plt",     4,  0, SHT_NOBITS,   0 },
2411   { ".toc",     4,  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2412   { ".toc1",    5,  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2413   { ".tocbss",  7,  0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2414   { NULL,       0,  0, 0,            0 }
2415 };
2416
2417 struct _ppc64_elf_section_data
2418 {
2419   struct bfd_elf_section_data elf;
2420
2421   /* An array with one entry for each opd function descriptor.  */
2422   union
2423   {
2424     /* Points to the function code section for local opd entries.  */
2425     asection **func_sec;
2426     /* After editing .opd, adjust references to opd local syms.  */
2427     long *adjust;
2428   } opd;
2429
2430   /* An array for toc sections, indexed by offset/8.
2431      Specifies the relocation symbol index used at a given toc offset.  */
2432   unsigned *t_symndx;
2433 };
2434
2435 #define ppc64_elf_section_data(sec) \
2436   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2437
2438 static bfd_boolean
2439 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2440 {
2441   struct _ppc64_elf_section_data *sdata;
2442   bfd_size_type amt = sizeof (*sdata);
2443
2444   sdata = bfd_zalloc (abfd, amt);
2445   if (sdata == NULL)
2446     return FALSE;
2447   sec->used_by_bfd = sdata;
2448
2449   return _bfd_elf_new_section_hook (abfd, sec);
2450 }
2451 \f
2452 /* The following functions are specific to the ELF linker, while
2453    functions above are used generally.  Those named ppc64_elf_* are
2454    called by the main ELF linker code.  They appear in this file more
2455    or less in the order in which they are called.  eg.
2456    ppc64_elf_check_relocs is called early in the link process,
2457    ppc64_elf_finish_dynamic_sections is one of the last functions
2458    called.
2459
2460    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2461    functions have both a function code symbol and a function descriptor
2462    symbol.  A call to foo in a relocatable object file looks like:
2463
2464    .            .text
2465    .    x:
2466    .            bl      .foo
2467    .            nop
2468
2469    The function definition in another object file might be:
2470
2471    .            .section .opd
2472    .    foo:    .quad   .foo
2473    .            .quad   .TOC.@tocbase
2474    .            .quad   0
2475    .
2476    .            .text
2477    .    .foo:   blr
2478
2479    When the linker resolves the call during a static link, the branch
2480    unsurprisingly just goes to .foo and the .opd information is unused.
2481    If the function definition is in a shared library, things are a little
2482    different:  The call goes via a plt call stub, the opd information gets
2483    copied to the plt, and the linker patches the nop.
2484
2485    .    x:
2486    .            bl      .foo_stub
2487    .            ld      2,40(1)
2488    .
2489    .
2490    .    .foo_stub:
2491    .            addis   12,2,Lfoo@toc@ha        # in practice, the call stub
2492    .            addi    12,12,Lfoo@toc@l        # is slightly optimized, but
2493    .            std     2,40(1)                 # this is the general idea
2494    .            ld      11,0(12)
2495    .            ld      2,8(12)
2496    .            mtctr   11
2497    .            ld      11,16(12)
2498    .            bctr
2499    .
2500    .            .section .plt
2501    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
2502
2503    The "reloc ()" notation is supposed to indicate that the linker emits
2504    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
2505    copying.
2506
2507    What are the difficulties here?  Well, firstly, the relocations
2508    examined by the linker in check_relocs are against the function code
2509    sym .foo, while the dynamic relocation in the plt is emitted against
2510    the function descriptor symbol, foo.  Somewhere along the line, we need
2511    to carefully copy dynamic link information from one symbol to the other.
2512    Secondly, the generic part of the elf linker will make .foo a dynamic
2513    symbol as is normal for most other backends.  We need foo dynamic
2514    instead, at least for an application final link.  However, when
2515    creating a shared library containing foo, we need to have both symbols
2516    dynamic so that references to .foo are satisfied during the early
2517    stages of linking.  Otherwise the linker might decide to pull in a
2518    definition from some other object, eg. a static library.  */
2519
2520 /* The linker needs to keep track of the number of relocs that it
2521    decides to copy as dynamic relocs in check_relocs for each symbol.
2522    This is so that it can later discard them if they are found to be
2523    unnecessary.  We store the information in a field extending the
2524    regular ELF linker hash table.  */
2525
2526 struct ppc_dyn_relocs
2527 {
2528   struct ppc_dyn_relocs *next;
2529
2530   /* The input section of the reloc.  */
2531   asection *sec;
2532
2533   /* Total number of relocs copied for the input section.  */
2534   bfd_size_type count;
2535
2536   /* Number of pc-relative relocs copied for the input section.  */
2537   bfd_size_type pc_count;
2538 };
2539
2540 /* Track GOT entries needed for a given symbol.  We might need more
2541    than one got entry per symbol.  */
2542 struct got_entry
2543 {
2544   struct got_entry *next;
2545
2546   /* The symbol addend that we'll be placing in the GOT.  */
2547   bfd_vma addend;
2548
2549   /* Unlike other ELF targets, we use separate GOT entries for the same
2550      symbol referenced from different input files.  This is to support
2551      automatic multiple TOC/GOT sections, where the TOC base can vary
2552      from one input file to another.
2553
2554      Point to the BFD owning this GOT entry.  */
2555   bfd *owner;
2556
2557   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2558      TLS_TPREL or TLS_DTPREL for tls entries.  */
2559   char tls_type;
2560
2561   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2562   union
2563     {
2564       bfd_signed_vma refcount;
2565       bfd_vma offset;
2566     } got;
2567 };
2568
2569 /* The same for PLT.  */
2570 struct plt_entry
2571 {
2572   struct plt_entry *next;
2573
2574   bfd_vma addend;
2575
2576   union
2577     {
2578       bfd_signed_vma refcount;
2579       bfd_vma offset;
2580     } plt;
2581 };
2582
2583 /* Of those relocs that might be copied as dynamic relocs, this macro
2584    selects those that must be copied when linking a shared library,
2585    even when the symbol is local.  */
2586
2587 #define MUST_BE_DYN_RELOC(RTYPE)                \
2588   ((RTYPE) != R_PPC64_REL32                     \
2589    && (RTYPE) != R_PPC64_REL64                  \
2590    && (RTYPE) != R_PPC64_REL30)
2591
2592 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2593    copying dynamic variables from a shared lib into an app's dynbss
2594    section, and instead use a dynamic relocation to point into the
2595    shared lib.  With code that gcc generates, it's vital that this be
2596    enabled;  In the PowerPC64 ABI, the address of a function is actually
2597    the address of a function descriptor, which resides in the .opd
2598    section.  gcc uses the descriptor directly rather than going via the
2599    GOT as some other ABI's do, which means that initialized function
2600    pointers must reference the descriptor.  Thus, a function pointer
2601    initialized to the address of a function in a shared library will
2602    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
2603    redefines the function desctriptor symbol to point to the copy.  This
2604    presents a problem as a plt entry for that function is also
2605    initialized from the function descriptor symbol and the copy reloc
2606    may not be initialized first.  */
2607 #define ELIMINATE_COPY_RELOCS 1
2608
2609 /* Section name for stubs is the associated section name plus this
2610    string.  */
2611 #define STUB_SUFFIX ".stub"
2612
2613 /* Linker stubs.
2614    ppc_stub_long_branch:
2615    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2616    destination, but a 24 bit branch in a stub section will reach.
2617    .    b       dest
2618
2619    ppc_stub_plt_branch:
2620    Similar to the above, but a 24 bit branch in the stub section won't
2621    reach its destination.
2622    .    addis   %r12,%r2,xxx@toc@ha
2623    .    ld      %r11,xxx@toc@l(%r12)
2624    .    mtctr   %r11
2625    .    bctr
2626
2627    ppc_stub_plt_call:
2628    Used to call a function in a shared library.
2629    .    addis   %r12,%r2,xxx@toc@ha
2630    .    std     %r2,40(%r1)
2631    .    ld      %r11,xxx+0@toc@l(%r12)
2632    .    ld      %r2,xxx+8@toc@l(%r12)
2633    .    mtctr   %r11
2634    .    ld      %r11,xxx+16@toc@l(%r12)
2635    .    bctr
2636
2637    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2638    code to adjust the value and save r2 to support multiple toc sections.
2639    A ppc_stub_long_branch with an r2 offset looks like:
2640    .    std     %r2,40(%r1)
2641    .    addis   %r2,%r2,off@ha
2642    .    addi    %r2,%r2,off@l
2643    .    b       dest
2644
2645    A ppc_stub_plt_branch with an r2 offset looks like:
2646    .    std     %r2,40(%r1)
2647    .    addis   %r12,%r2,xxx@toc@ha
2648    .    ld      %r11,xxx@toc@l(%r12)
2649    .    addis   %r2,%r2,off@ha
2650    .    addi    %r2,%r2,off@l
2651    .    mtctr   %r11
2652    .    bctr
2653 */
2654
2655 enum ppc_stub_type {
2656   ppc_stub_none,
2657   ppc_stub_long_branch,
2658   ppc_stub_long_branch_r2off,
2659   ppc_stub_plt_branch,
2660   ppc_stub_plt_branch_r2off,
2661   ppc_stub_plt_call
2662 };
2663
2664 struct ppc_stub_hash_entry {
2665
2666   /* Base hash table entry structure.  */
2667   struct bfd_hash_entry root;
2668
2669   enum ppc_stub_type stub_type;
2670
2671   /* The stub section.  */
2672   asection *stub_sec;
2673
2674   /* Offset within stub_sec of the beginning of this stub.  */
2675   bfd_vma stub_offset;
2676
2677   /* Given the symbol's value and its section we can determine its final
2678      value when building the stubs (so the stub knows where to jump.  */
2679   bfd_vma target_value;
2680   asection *target_section;
2681
2682   /* The symbol table entry, if any, that this was derived from.  */
2683   struct ppc_link_hash_entry *h;
2684
2685   /* And the reloc addend that this was derived from.  */
2686   bfd_vma addend;
2687
2688   /* Where this stub is being called from, or, in the case of combined
2689      stub sections, the first input section in the group.  */
2690   asection *id_sec;
2691 };
2692
2693 struct ppc_branch_hash_entry {
2694
2695   /* Base hash table entry structure.  */
2696   struct bfd_hash_entry root;
2697
2698   /* Offset within .branch_lt.  */
2699   unsigned int offset;
2700
2701   /* Generation marker.  */
2702   unsigned int iter;
2703 };
2704
2705 struct ppc_link_hash_entry
2706 {
2707   struct elf_link_hash_entry elf;
2708
2709   /* A pointer to the most recently used stub hash entry against this
2710      symbol.  */
2711   struct ppc_stub_hash_entry *stub_cache;
2712
2713   /* Track dynamic relocs copied for this symbol.  */
2714   struct ppc_dyn_relocs *dyn_relocs;
2715
2716   /* Link between function code and descriptor symbols.  */
2717   struct elf_link_hash_entry *oh;
2718
2719   /* Flag function code and descriptor symbols.  */
2720   unsigned int is_func:1;
2721   unsigned int is_func_descriptor:1;
2722   unsigned int is_entry:1;
2723
2724   /* Contexts in which symbol is used in the GOT (or TOC).
2725      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
2726      corresponding relocs are encountered during check_relocs.
2727      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2728      indicate the corresponding GOT entry type is not needed.
2729      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
2730      a TPREL one.  We use a separate flag rather than setting TPREL
2731      just for convenience in distinguishing the two cases.  */
2732 #define TLS_GD           1      /* GD reloc. */
2733 #define TLS_LD           2      /* LD reloc. */
2734 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
2735 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
2736 #define TLS_TLS         16      /* Any TLS reloc.  */
2737 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
2738 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
2739   char tls_mask;
2740 };
2741
2742 /* ppc64 ELF linker hash table.  */
2743
2744 struct ppc_link_hash_table
2745 {
2746   struct elf_link_hash_table elf;
2747
2748   /* The stub hash table.  */
2749   struct bfd_hash_table stub_hash_table;
2750
2751   /* Another hash table for plt_branch stubs.  */
2752   struct bfd_hash_table branch_hash_table;
2753
2754   /* Linker stub bfd.  */
2755   bfd *stub_bfd;
2756
2757   /* Linker call-backs.  */
2758   asection * (*add_stub_section) (const char *, asection *);
2759   void (*layout_sections_again) (void);
2760
2761   /* Array to keep track of which stub sections have been created, and
2762      information on stub grouping.  */
2763   struct map_stub {
2764     /* This is the section to which stubs in the group will be attached.  */
2765     asection *link_sec;
2766     /* The stub section.  */
2767     asection *stub_sec;
2768     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
2769     bfd_vma toc_off;
2770   } *stub_group;
2771
2772   /* Support for multiple toc sections.  */
2773   unsigned int no_multi_toc;
2774   unsigned int multi_toc_needed;
2775
2776   /* Temp used when calculating TOC pointers.  */
2777   bfd_vma toc_curr;
2778
2779   /* Highest input section id.  */
2780   int top_id;
2781
2782   /* Highest output section index.  */
2783   int top_index;
2784
2785   /* List of input sections for each output section.  */
2786   asection **input_list;
2787
2788   /* Short-cuts to get to dynamic linker sections.  */
2789   asection *got;
2790   asection *plt;
2791   asection *relplt;
2792   asection *dynbss;
2793   asection *relbss;
2794   asection *glink;
2795   asection *sfpr;
2796   asection *brlt;
2797   asection *relbrlt;
2798
2799   /* Short-cut to first output tls section.  */
2800   asection *tls_sec;
2801
2802   /* Shortcut to .__tls_get_addr.  */
2803   struct elf_link_hash_entry *tls_get_addr;
2804
2805   /* Statistics.  */
2806   unsigned long stub_count[ppc_stub_plt_call];
2807
2808   /* Set if we should emit symbols for stubs.  */
2809   unsigned int emit_stub_syms;
2810
2811   /* Set on error.  */
2812   unsigned int stub_error;
2813
2814   /* Flag set when small branches are detected.  Used to
2815      select suitable defaults for the stub group size.  */
2816   unsigned int has_14bit_branch;
2817
2818   /* Set if we detect a reference undefined weak symbol.  */
2819   unsigned int have_undefweak;
2820
2821   /* Incremented every time we size stubs.  */
2822   unsigned int stub_iteration;
2823
2824   /* Small local sym to section mapping cache.  */
2825   struct sym_sec_cache sym_sec;
2826 };
2827
2828 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
2829
2830 #define ppc_hash_table(p) \
2831   ((struct ppc_link_hash_table *) ((p)->hash))
2832
2833 #define ppc_stub_hash_lookup(table, string, create, copy) \
2834   ((struct ppc_stub_hash_entry *) \
2835    bfd_hash_lookup ((table), (string), (create), (copy)))
2836
2837 #define ppc_branch_hash_lookup(table, string, create, copy) \
2838   ((struct ppc_branch_hash_entry *) \
2839    bfd_hash_lookup ((table), (string), (create), (copy)))
2840
2841 /* Create an entry in the stub hash table.  */
2842
2843 static struct bfd_hash_entry *
2844 stub_hash_newfunc (struct bfd_hash_entry *entry,
2845                    struct bfd_hash_table *table,
2846                    const char *string)
2847 {
2848   /* Allocate the structure if it has not already been allocated by a
2849      subclass.  */
2850   if (entry == NULL)
2851     {
2852       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
2853       if (entry == NULL)
2854         return entry;
2855     }
2856
2857   /* Call the allocation method of the superclass.  */
2858   entry = bfd_hash_newfunc (entry, table, string);
2859   if (entry != NULL)
2860     {
2861       struct ppc_stub_hash_entry *eh;
2862
2863       /* Initialize the local fields.  */
2864       eh = (struct ppc_stub_hash_entry *) entry;
2865       eh->stub_type = ppc_stub_none;
2866       eh->stub_sec = NULL;
2867       eh->stub_offset = 0;
2868       eh->target_value = 0;
2869       eh->target_section = NULL;
2870       eh->h = NULL;
2871       eh->id_sec = NULL;
2872     }
2873
2874   return entry;
2875 }
2876
2877 /* Create an entry in the branch hash table.  */
2878
2879 static struct bfd_hash_entry *
2880 branch_hash_newfunc (struct bfd_hash_entry *entry,
2881                      struct bfd_hash_table *table,
2882                      const char *string)
2883 {
2884   /* Allocate the structure if it has not already been allocated by a
2885      subclass.  */
2886   if (entry == NULL)
2887     {
2888       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
2889       if (entry == NULL)
2890         return entry;
2891     }
2892
2893   /* Call the allocation method of the superclass.  */
2894   entry = bfd_hash_newfunc (entry, table, string);
2895   if (entry != NULL)
2896     {
2897       struct ppc_branch_hash_entry *eh;
2898
2899       /* Initialize the local fields.  */
2900       eh = (struct ppc_branch_hash_entry *) entry;
2901       eh->offset = 0;
2902       eh->iter = 0;
2903     }
2904
2905   return entry;
2906 }
2907
2908 /* Create an entry in a ppc64 ELF linker hash table.  */
2909
2910 static struct bfd_hash_entry *
2911 link_hash_newfunc (struct bfd_hash_entry *entry,
2912                    struct bfd_hash_table *table,
2913                    const char *string)
2914 {
2915   /* Allocate the structure if it has not already been allocated by a
2916      subclass.  */
2917   if (entry == NULL)
2918     {
2919       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
2920       if (entry == NULL)
2921         return entry;
2922     }
2923
2924   /* Call the allocation method of the superclass.  */
2925   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2926   if (entry != NULL)
2927     {
2928       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
2929
2930       eh->stub_cache = NULL;
2931       eh->dyn_relocs = NULL;
2932       eh->oh = NULL;
2933       eh->is_func = 0;
2934       eh->is_func_descriptor = 0;
2935       eh->is_entry = 0;
2936       eh->tls_mask = 0;
2937     }
2938
2939   return entry;
2940 }
2941
2942 /* Create a ppc64 ELF linker hash table.  */
2943
2944 static struct bfd_link_hash_table *
2945 ppc64_elf_link_hash_table_create (bfd *abfd)
2946 {
2947   struct ppc_link_hash_table *htab;
2948   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
2949
2950   htab = bfd_zmalloc (amt);
2951   if (htab == NULL)
2952     return NULL;
2953
2954   if (! _bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc))
2955     {
2956       free (htab);
2957       return NULL;
2958     }
2959
2960   /* Init the stub hash table too.  */
2961   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc))
2962     return NULL;
2963
2964   /* And the branch hash table.  */
2965   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc))
2966     return NULL;
2967
2968   /* Initializing two fields of the union is just cosmetic.  We really
2969      only care about glist, but when compiled on a 32-bit host the
2970      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
2971      debugger inspection of these fields look nicer.  */
2972   htab->elf.init_refcount.refcount = 0;
2973   htab->elf.init_refcount.glist = NULL;
2974   htab->elf.init_offset.offset = 0;
2975   htab->elf.init_offset.glist = NULL;
2976
2977   return &htab->elf.root;
2978 }
2979
2980 /* Free the derived linker hash table.  */
2981
2982 static void
2983 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
2984 {
2985   struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
2986
2987   bfd_hash_table_free (&ret->stub_hash_table);
2988   bfd_hash_table_free (&ret->branch_hash_table);
2989   _bfd_generic_link_hash_table_free (hash);
2990 }
2991
2992 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
2993
2994 void
2995 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
2996 {
2997   struct ppc_link_hash_table *htab;
2998
2999   elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
3000
3001 /* Always hook our dynamic sections into the first bfd, which is the
3002    linker created stub bfd.  This ensures that the GOT header is at
3003    the start of the output TOC section.  */
3004   htab = ppc_hash_table (info);
3005   htab->stub_bfd = abfd;
3006   htab->elf.dynobj = abfd;
3007 }
3008
3009 /* Build a name for an entry in the stub hash table.  */
3010
3011 static char *
3012 ppc_stub_name (const asection *input_section,
3013                const asection *sym_sec,
3014                const struct ppc_link_hash_entry *h,
3015                const Elf_Internal_Rela *rel)
3016 {
3017   char *stub_name;
3018   bfd_size_type len;
3019
3020   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3021      offsets from a sym as a branch target?  In fact, we could
3022      probably assume the addend is always zero.  */
3023   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3024
3025   if (h)
3026     {
3027       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3028       stub_name = bfd_malloc (len);
3029       if (stub_name != NULL)
3030         {
3031           sprintf (stub_name, "%08x.%s+%x",
3032                    input_section->id & 0xffffffff,
3033                    h->elf.root.root.string,
3034                    (int) rel->r_addend & 0xffffffff);
3035         }
3036     }
3037   else
3038     {
3039       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3040       stub_name = bfd_malloc (len);
3041       if (stub_name != NULL)
3042         {
3043           sprintf (stub_name, "%08x.%x:%x+%x",
3044                    input_section->id & 0xffffffff,
3045                    sym_sec->id & 0xffffffff,
3046                    (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3047                    (int) rel->r_addend & 0xffffffff);
3048         }
3049     }
3050   return stub_name;
3051 }
3052
3053 /* Look up an entry in the stub hash.  Stub entries are cached because
3054    creating the stub name takes a bit of time.  */
3055
3056 static struct ppc_stub_hash_entry *
3057 ppc_get_stub_entry (const asection *input_section,
3058                     const asection *sym_sec,
3059                     struct elf_link_hash_entry *hash,
3060                     const Elf_Internal_Rela *rel,
3061                     struct ppc_link_hash_table *htab)
3062 {
3063   struct ppc_stub_hash_entry *stub_entry;
3064   struct ppc_link_hash_entry *h = (struct ppc_link_hash_entry *) hash;
3065   const asection *id_sec;
3066
3067   /* If this input section is part of a group of sections sharing one
3068      stub section, then use the id of the first section in the group.
3069      Stub names need to include a section id, as there may well be
3070      more than one stub used to reach say, printf, and we need to
3071      distinguish between them.  */
3072   id_sec = htab->stub_group[input_section->id].link_sec;
3073
3074   if (h != NULL && h->stub_cache != NULL
3075       && h->stub_cache->h == h
3076       && h->stub_cache->id_sec == id_sec)
3077     {
3078       stub_entry = h->stub_cache;
3079     }
3080   else
3081     {
3082       char *stub_name;
3083
3084       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
3085       if (stub_name == NULL)
3086         return NULL;
3087
3088       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3089                                          stub_name, FALSE, FALSE);
3090       if (h != NULL)
3091         h->stub_cache = stub_entry;
3092
3093       free (stub_name);
3094     }
3095
3096   return stub_entry;
3097 }
3098
3099 /* Add a new stub entry to the stub hash.  Not all fields of the new
3100    stub entry are initialised.  */
3101
3102 static struct ppc_stub_hash_entry *
3103 ppc_add_stub (const char *stub_name,
3104               asection *section,
3105               struct ppc_link_hash_table *htab)
3106 {
3107   asection *link_sec;
3108   asection *stub_sec;
3109   struct ppc_stub_hash_entry *stub_entry;
3110
3111   link_sec = htab->stub_group[section->id].link_sec;
3112   stub_sec = htab->stub_group[section->id].stub_sec;
3113   if (stub_sec == NULL)
3114     {
3115       stub_sec = htab->stub_group[link_sec->id].stub_sec;
3116       if (stub_sec == NULL)
3117         {
3118           size_t namelen;
3119           bfd_size_type len;
3120           char *s_name;
3121
3122           namelen = strlen (link_sec->name);
3123           len = namelen + sizeof (STUB_SUFFIX);
3124           s_name = bfd_alloc (htab->stub_bfd, len);
3125           if (s_name == NULL)
3126             return NULL;
3127
3128           memcpy (s_name, link_sec->name, namelen);
3129           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3130           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3131           if (stub_sec == NULL)
3132             return NULL;
3133           htab->stub_group[link_sec->id].stub_sec = stub_sec;
3134         }
3135       htab->stub_group[section->id].stub_sec = stub_sec;
3136     }
3137
3138   /* Enter this entry into the linker stub hash table.  */
3139   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3140                                      TRUE, FALSE);
3141   if (stub_entry == NULL)
3142     {
3143       (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
3144                              bfd_archive_filename (section->owner),
3145                              stub_name);
3146       return NULL;
3147     }
3148
3149   stub_entry->stub_sec = stub_sec;
3150   stub_entry->stub_offset = 0;
3151   stub_entry->id_sec = link_sec;
3152   return stub_entry;
3153 }
3154
3155 /* Create sections for linker generated code.  */
3156
3157 static bfd_boolean
3158 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3159 {
3160   struct ppc_link_hash_table *htab;
3161   flagword flags;
3162
3163   htab = ppc_hash_table (info);
3164
3165   /* Create .sfpr for code to save and restore fp regs.  */
3166   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3167            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3168   htab->sfpr = bfd_make_section_anyway (dynobj, ".sfpr");
3169   if (htab->sfpr == NULL
3170       || ! bfd_set_section_flags (dynobj, htab->sfpr, flags)
3171       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3172     return FALSE;
3173
3174   /* Create .glink for lazy dynamic linking support.  */
3175   htab->glink = bfd_make_section_anyway (dynobj, ".glink");
3176   if (htab->glink == NULL
3177       || ! bfd_set_section_flags (dynobj, htab->glink, flags)
3178       || ! bfd_set_section_alignment (dynobj, htab->glink, 2))
3179     return FALSE;
3180
3181   /* Create .branch_lt for plt_branch stubs.  */
3182   flags = (SEC_ALLOC | SEC_LOAD
3183            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3184   htab->brlt = bfd_make_section_anyway (dynobj, ".branch_lt");
3185   if (htab->brlt == NULL
3186       || ! bfd_set_section_flags (dynobj, htab->brlt, flags)
3187       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
3188     return FALSE;
3189
3190   if (info->shared)
3191     {
3192       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3193                | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3194       htab->relbrlt = bfd_make_section_anyway (dynobj, ".rela.branch_lt");
3195       if (!htab->relbrlt
3196           || ! bfd_set_section_flags (dynobj, htab->relbrlt, flags)
3197           || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3198         return FALSE;
3199     }
3200   return TRUE;
3201 }
3202
3203 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3204    not already done.  */
3205
3206 static bfd_boolean
3207 create_got_section (bfd *abfd, struct bfd_link_info *info)
3208 {
3209   asection *got, *relgot;
3210   flagword flags;
3211   struct ppc_link_hash_table *htab = ppc_hash_table (info);
3212
3213   if (!htab->got)
3214     {
3215       if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
3216         return FALSE;
3217
3218       htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
3219       if (!htab->got)
3220         abort ();
3221     }
3222
3223   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3224            | SEC_LINKER_CREATED);
3225
3226   got = bfd_make_section (abfd, ".got");
3227   if (!got
3228       || !bfd_set_section_flags (abfd, got, flags)
3229       || !bfd_set_section_alignment (abfd, got, 3))
3230     return FALSE;
3231
3232   relgot = bfd_make_section (abfd, ".rela.got");
3233   if (!relgot
3234       || ! bfd_set_section_flags (abfd, relgot, flags | SEC_READONLY)
3235       || ! bfd_set_section_alignment (abfd, relgot, 3))
3236     return FALSE;
3237
3238   ppc64_elf_tdata (abfd)->got = got;
3239   ppc64_elf_tdata (abfd)->relgot = relgot;
3240   return TRUE;
3241 }
3242
3243 /* Create the dynamic sections, and set up shortcuts.  */
3244
3245 static bfd_boolean
3246 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3247 {
3248   struct ppc_link_hash_table *htab;
3249
3250   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3251     return FALSE;
3252
3253   htab = ppc_hash_table (info);
3254   if (!htab->got)
3255     htab->got = bfd_get_section_by_name (dynobj, ".got");
3256   htab->plt = bfd_get_section_by_name (dynobj, ".plt");
3257   htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
3258   htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
3259   if (!info->shared)
3260     htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
3261
3262   if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
3263       || (!info->shared && !htab->relbss))
3264     abort ();
3265
3266   return TRUE;
3267 }
3268
3269 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3270
3271 static void
3272 ppc64_elf_copy_indirect_symbol
3273   (const struct elf_backend_data *bed ATTRIBUTE_UNUSED,
3274    struct elf_link_hash_entry *dir,
3275    struct elf_link_hash_entry *ind)
3276 {
3277   struct ppc_link_hash_entry *edir, *eind;
3278   flagword mask;
3279
3280   edir = (struct ppc_link_hash_entry *) dir;
3281   eind = (struct ppc_link_hash_entry *) ind;
3282
3283   /* Copy over any dynamic relocs we may have on the indirect sym.  */
3284   if (eind->dyn_relocs != NULL)
3285     {
3286       if (edir->dyn_relocs != NULL)
3287         {
3288           struct ppc_dyn_relocs **pp;
3289           struct ppc_dyn_relocs *p;
3290
3291           if (eind->elf.root.type == bfd_link_hash_indirect)
3292             abort ();
3293
3294           /* Add reloc counts against the weak sym to the strong sym
3295              list.  Merge any entries against the same section.  */
3296           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3297             {
3298               struct ppc_dyn_relocs *q;
3299
3300               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3301                 if (q->sec == p->sec)
3302                   {
3303                     q->pc_count += p->pc_count;
3304                     q->count += p->count;
3305                     *pp = p->next;
3306                     break;
3307                   }
3308               if (q == NULL)
3309                 pp = &p->next;
3310             }
3311           *pp = edir->dyn_relocs;
3312         }
3313
3314       edir->dyn_relocs = eind->dyn_relocs;
3315       eind->dyn_relocs = NULL;
3316     }
3317
3318   edir->is_func |= eind->is_func;
3319   edir->is_func_descriptor |= eind->is_func_descriptor;
3320   edir->is_entry |= eind->is_entry;
3321   edir->tls_mask |= eind->tls_mask;
3322
3323   mask = (ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR
3324           | ELF_LINK_HASH_REF_REGULAR_NONWEAK | ELF_LINK_NON_GOT_REF);
3325   /* If called to transfer flags for a weakdef during processing
3326      of elf_adjust_dynamic_symbol, don't copy ELF_LINK_NON_GOT_REF.
3327      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
3328   if (ELIMINATE_COPY_RELOCS
3329       && eind->elf.root.type != bfd_link_hash_indirect
3330       && (edir->elf.elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
3331     mask &= ~ELF_LINK_NON_GOT_REF;
3332
3333   edir->elf.elf_link_hash_flags |= eind->elf.elf_link_hash_flags & mask;
3334
3335   /* If we were called to copy over info for a weak sym, that's all.  */
3336   if (eind->elf.root.type != bfd_link_hash_indirect)
3337     return;
3338
3339   /* Copy over got entries that we may have already seen to the
3340      symbol which just became indirect.  */
3341   if (eind->elf.got.glist != NULL)
3342     {
3343       if (edir->elf.got.glist != NULL)
3344         {
3345           struct got_entry **entp;
3346           struct got_entry *ent;
3347
3348           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3349             {
3350               struct got_entry *dent;
3351
3352               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3353                 if (dent->addend == ent->addend
3354                     && dent->owner == ent->owner
3355                     && dent->tls_type == ent->tls_type)
3356                   {
3357                     dent->got.refcount += ent->got.refcount;
3358                     *entp = ent->next;
3359                     break;
3360                   }
3361               if (dent == NULL)
3362                 entp = &ent->next;
3363             }
3364           *entp = edir->elf.got.glist;
3365         }
3366
3367       edir->elf.got.glist = eind->elf.got.glist;
3368       eind->elf.got.glist = NULL;
3369     }
3370
3371   /* And plt entries.  */
3372   if (eind->elf.plt.plist != NULL)
3373     {
3374       if (edir->elf.plt.plist != NULL)
3375         {
3376           struct plt_entry **entp;
3377           struct plt_entry *ent;
3378
3379           for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
3380             {
3381               struct plt_entry *dent;
3382
3383               for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
3384                 if (dent->addend == ent->addend)
3385                   {
3386                     dent->plt.refcount += ent->plt.refcount;
3387                     *entp = ent->next;
3388                     break;
3389                   }
3390               if (dent == NULL)
3391                 entp = &ent->next;
3392             }
3393           *entp = edir->elf.plt.plist;
3394         }
3395
3396       edir->elf.plt.plist = eind->elf.plt.plist;
3397       eind->elf.plt.plist = NULL;
3398     }
3399
3400   if (edir->elf.dynindx == -1)
3401     {
3402       edir->elf.dynindx = eind->elf.dynindx;
3403       edir->elf.dynstr_index = eind->elf.dynstr_index;
3404       eind->elf.dynindx = -1;
3405       eind->elf.dynstr_index = 0;
3406     }
3407   else
3408     BFD_ASSERT (eind->elf.dynindx == -1);
3409 }
3410
3411 /* Set a flag, used by ppc64_elf_gc_mark_hook, on the entry symbol and
3412    symbols undefined on the command-line.  */
3413
3414 bfd_boolean
3415 ppc64_elf_mark_entry_syms (struct bfd_link_info *info)
3416 {
3417   struct ppc_link_hash_table *htab;
3418   struct bfd_sym_chain *sym;
3419
3420   htab = ppc_hash_table (info);
3421   for (sym = info->gc_sym_list; sym; sym = sym->next)
3422     {
3423       struct elf_link_hash_entry *h;
3424
3425       h = elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
3426       if (h != NULL)
3427         ((struct ppc_link_hash_entry *) h)->is_entry = 1;
3428     }
3429   return TRUE;
3430 }
3431
3432 static bfd_boolean
3433 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
3434                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
3435 {
3436   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
3437   char *local_got_tls_masks;
3438
3439   if (local_got_ents == NULL)
3440     {
3441       bfd_size_type size = symtab_hdr->sh_info;
3442
3443       size *= sizeof (*local_got_ents) + sizeof (*local_got_tls_masks);
3444       local_got_ents = bfd_zalloc (abfd, size);
3445       if (local_got_ents == NULL)
3446         return FALSE;
3447       elf_local_got_ents (abfd) = local_got_ents;
3448     }
3449
3450   if ((tls_type & TLS_EXPLICIT) == 0)
3451     {
3452       struct got_entry *ent;
3453
3454       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
3455         if (ent->addend == r_addend
3456             && ent->owner == abfd
3457             && ent->tls_type == tls_type)
3458           break;
3459       if (ent == NULL)
3460         {
3461           bfd_size_type amt = sizeof (*ent);
3462           ent = bfd_alloc (abfd, amt);
3463           if (ent == NULL)
3464             return FALSE;
3465           ent->next = local_got_ents[r_symndx];
3466           ent->addend = r_addend;
3467           ent->owner = abfd;
3468           ent->tls_type = tls_type;
3469           ent->got.refcount = 0;
3470           local_got_ents[r_symndx] = ent;
3471         }
3472       ent->got.refcount += 1;
3473     }
3474
3475   local_got_tls_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
3476   local_got_tls_masks[r_symndx] |= tls_type;
3477   return TRUE;
3478 }
3479
3480 static bfd_boolean
3481 update_plt_info (bfd *abfd, struct ppc_link_hash_entry *eh, bfd_vma addend)
3482 {
3483   struct plt_entry *ent;
3484
3485   for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
3486     if (ent->addend == addend)
3487       break;
3488   if (ent == NULL)
3489     {
3490       bfd_size_type amt = sizeof (*ent);
3491       ent = bfd_alloc (abfd, amt);
3492       if (ent == NULL)
3493         return FALSE;
3494       ent->next = eh->elf.plt.plist;
3495       ent->addend = addend;
3496       ent->plt.refcount = 0;
3497       eh->elf.plt.plist = ent;
3498     }
3499   ent->plt.refcount += 1;
3500   eh->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3501   eh->is_func = 1;
3502   return TRUE;
3503 }
3504
3505 /* Find the function descriptor hash entry from the given function code
3506    hash entry FH.  Link the entries via their OH fields.  */
3507 static struct ppc_link_hash_entry *
3508 get_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3509 {
3510   struct ppc_link_hash_entry *fdh = (struct ppc_link_hash_entry *) fh->oh;
3511
3512   if (fdh == NULL)
3513     {
3514       const char *fd_name = fh->elf.root.root.string + 1;
3515
3516       fdh = (struct ppc_link_hash_entry *)
3517         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3518       if (fdh != NULL)
3519         {
3520           fdh->is_func_descriptor = 1;
3521           fdh->oh = &fh->elf;
3522           fh->is_func = 1;
3523           fh->oh = &fdh->elf;
3524         }
3525     }
3526
3527   return fdh;
3528 }
3529
3530 /* Look through the relocs for a section during the first phase, and
3531    calculate needed space in the global offset table, procedure
3532    linkage table, and dynamic reloc sections.  */
3533
3534 static bfd_boolean
3535 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
3536                         asection *sec, const Elf_Internal_Rela *relocs)
3537 {
3538   struct ppc_link_hash_table *htab;
3539   Elf_Internal_Shdr *symtab_hdr;
3540   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
3541   const Elf_Internal_Rela *rel;
3542   const Elf_Internal_Rela *rel_end;
3543   asection *sreloc;
3544   asection **opd_sym_map;
3545
3546   if (info->relocatable)
3547     return TRUE;
3548
3549   htab = ppc_hash_table (info);
3550   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3551
3552   sym_hashes = elf_sym_hashes (abfd);
3553   sym_hashes_end = (sym_hashes
3554                     + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
3555                     - symtab_hdr->sh_info);
3556
3557   sreloc = NULL;
3558   opd_sym_map = NULL;
3559   if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
3560     {
3561       /* Garbage collection needs some extra help with .opd sections.
3562          We don't want to necessarily keep everything referenced by
3563          relocs in .opd, as that would keep all functions.  Instead,
3564          if we reference an .opd symbol (a function descriptor), we
3565          want to keep the function code symbol's section.  This is
3566          easy for global symbols, but for local syms we need to keep
3567          information about the associated function section.  Later, if
3568          edit_opd deletes entries, we'll use this array to adjust
3569          local syms in .opd.  */
3570       union opd_info {
3571         asection *func_section;
3572         long entry_adjust;
3573       };
3574       bfd_size_type amt;
3575
3576       amt = sec->_raw_size * sizeof (union opd_info) / 24;
3577       opd_sym_map = bfd_zalloc (abfd, amt);
3578       if (opd_sym_map == NULL)
3579         return FALSE;
3580       ppc64_elf_section_data (sec)->opd.func_sec = opd_sym_map;
3581     }
3582
3583   if (htab->sfpr == NULL
3584       && !create_linkage_sections (htab->elf.dynobj, info))
3585     return FALSE;
3586
3587   rel_end = relocs + sec->reloc_count;
3588   for (rel = relocs; rel < rel_end; rel++)
3589     {
3590       unsigned long r_symndx;
3591       struct elf_link_hash_entry *h;
3592       enum elf_ppc64_reloc_type r_type;
3593       int tls_type = 0;
3594
3595       r_symndx = ELF64_R_SYM (rel->r_info);
3596       if (r_symndx < symtab_hdr->sh_info)
3597         h = NULL;
3598       else
3599         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3600
3601       r_type = ELF64_R_TYPE (rel->r_info);
3602       switch (r_type)
3603         {
3604         case R_PPC64_GOT_TLSLD16:
3605         case R_PPC64_GOT_TLSLD16_LO:
3606         case R_PPC64_GOT_TLSLD16_HI:
3607         case R_PPC64_GOT_TLSLD16_HA:
3608           ppc64_tlsld_got (abfd)->refcount += 1;
3609           tls_type = TLS_TLS | TLS_LD;
3610           goto dogottls;
3611
3612         case R_PPC64_GOT_TLSGD16:
3613         case R_PPC64_GOT_TLSGD16_LO:
3614         case R_PPC64_GOT_TLSGD16_HI:
3615         case R_PPC64_GOT_TLSGD16_HA:
3616           tls_type = TLS_TLS | TLS_GD;
3617           goto dogottls;
3618
3619         case R_PPC64_GOT_TPREL16_DS:
3620         case R_PPC64_GOT_TPREL16_LO_DS:
3621         case R_PPC64_GOT_TPREL16_HI:
3622         case R_PPC64_GOT_TPREL16_HA:
3623           if (info->shared)
3624             info->flags |= DF_STATIC_TLS;
3625           tls_type = TLS_TLS | TLS_TPREL;
3626           goto dogottls;
3627
3628         case R_PPC64_GOT_DTPREL16_DS:
3629         case R_PPC64_GOT_DTPREL16_LO_DS:
3630         case R_PPC64_GOT_DTPREL16_HI:
3631         case R_PPC64_GOT_DTPREL16_HA:
3632           tls_type = TLS_TLS | TLS_DTPREL;
3633         dogottls:
3634           sec->has_tls_reloc = 1;
3635           /* Fall thru */
3636
3637         case R_PPC64_GOT16:
3638         case R_PPC64_GOT16_DS:
3639         case R_PPC64_GOT16_HA:
3640         case R_PPC64_GOT16_HI:
3641         case R_PPC64_GOT16_LO:
3642         case R_PPC64_GOT16_LO_DS:
3643           /* This symbol requires a global offset table entry.  */
3644           sec->has_gp_reloc = 1;
3645           if (ppc64_elf_tdata (abfd)->got == NULL
3646               && !create_got_section (abfd, info))
3647             return FALSE;
3648
3649           if (h != NULL)
3650             {
3651               struct ppc_link_hash_entry *eh;
3652               struct got_entry *ent;
3653
3654               eh = (struct ppc_link_hash_entry *) h;
3655               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
3656                 if (ent->addend == rel->r_addend
3657                     && ent->owner == abfd
3658                     && ent->tls_type == tls_type)
3659                   break;
3660               if (ent == NULL)
3661                 {
3662                   bfd_size_type amt = sizeof (*ent);
3663                   ent = bfd_alloc (abfd, amt);
3664                   if (ent == NULL)
3665                     return FALSE;
3666                   ent->next = eh->elf.got.glist;
3667                   ent->addend = rel->r_addend;
3668                   ent->owner = abfd;
3669                   ent->tls_type = tls_type;
3670                   ent->got.refcount = 0;
3671                   eh->elf.got.glist = ent;
3672                 }
3673               ent->got.refcount += 1;
3674               eh->tls_mask |= tls_type;
3675             }
3676           else
3677             /* This is a global offset table entry for a local symbol.  */
3678             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
3679                                         rel->r_addend, tls_type))
3680               return FALSE;
3681           break;
3682
3683         case R_PPC64_PLT16_HA:
3684         case R_PPC64_PLT16_HI:
3685         case R_PPC64_PLT16_LO:
3686         case R_PPC64_PLT32:
3687         case R_PPC64_PLT64:
3688           /* This symbol requires a procedure linkage table entry.  We
3689              actually build the entry in adjust_dynamic_symbol,
3690              because this might be a case of linking PIC code without
3691              linking in any dynamic objects, in which case we don't
3692              need to generate a procedure linkage table after all.  */
3693           if (h == NULL)
3694             {
3695               /* It does not make sense to have a procedure linkage
3696                  table entry for a local symbol.  */
3697               bfd_set_error (bfd_error_bad_value);
3698               return FALSE;
3699             }
3700           else
3701             if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
3702                                   rel->r_addend))
3703               return FALSE;
3704           break;
3705
3706           /* The following relocations don't need to propagate the
3707              relocation if linking a shared object since they are
3708              section relative.  */
3709         case R_PPC64_SECTOFF:
3710         case R_PPC64_SECTOFF_LO:
3711         case R_PPC64_SECTOFF_HI:
3712         case R_PPC64_SECTOFF_HA:
3713         case R_PPC64_SECTOFF_DS:
3714         case R_PPC64_SECTOFF_LO_DS:
3715         case R_PPC64_DTPREL16:
3716         case R_PPC64_DTPREL16_LO:
3717         case R_PPC64_DTPREL16_HI:
3718         case R_PPC64_DTPREL16_HA:
3719         case R_PPC64_DTPREL16_DS:
3720         case R_PPC64_DTPREL16_LO_DS:
3721         case R_PPC64_DTPREL16_HIGHER:
3722         case R_PPC64_DTPREL16_HIGHERA:
3723         case R_PPC64_DTPREL16_HIGHEST:
3724         case R_PPC64_DTPREL16_HIGHESTA:
3725           break;
3726
3727           /* Nor do these.  */
3728         case R_PPC64_TOC16:
3729         case R_PPC64_TOC16_LO:
3730         case R_PPC64_TOC16_HI:
3731         case R_PPC64_TOC16_HA:
3732         case R_PPC64_TOC16_DS:
3733         case R_PPC64_TOC16_LO_DS:
3734           sec->has_gp_reloc = 1;
3735           break;
3736
3737           /* This relocation describes the C++ object vtable hierarchy.
3738              Reconstruct it for later use during GC.  */
3739         case R_PPC64_GNU_VTINHERIT:
3740           if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3741             return FALSE;
3742           break;
3743
3744           /* This relocation describes which C++ vtable entries are actually
3745              used.  Record for later use during GC.  */
3746         case R_PPC64_GNU_VTENTRY:
3747           if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3748             return FALSE;
3749           break;
3750
3751         case R_PPC64_REL14:
3752         case R_PPC64_REL14_BRTAKEN:
3753         case R_PPC64_REL14_BRNTAKEN:
3754           htab->has_14bit_branch = 1;
3755           /* Fall through.  */
3756
3757         case R_PPC64_REL24:
3758           if (h != NULL
3759               && h->root.root.string[0] == '.'
3760               && h->root.root.string[1] != 0)
3761             {
3762               /* We may need a .plt entry if the function this reloc
3763                  refers to is in a shared lib.  */
3764               if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
3765                                     rel->r_addend))
3766                 return FALSE;
3767               if (h == htab->tls_get_addr)
3768                 sec->has_tls_reloc = 1;
3769               else if ((strncmp (h->root.root.string, ".__tls_get_addr", 15)
3770                         == 0)
3771                        && (h->root.root.string[15] == 0
3772                            || h->root.root.string[15] == '@'))
3773                 {
3774                   htab->tls_get_addr = h;
3775                   sec->has_tls_reloc = 1;
3776                 }
3777             }
3778           break;
3779
3780         case R_PPC64_TPREL64:
3781           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
3782           if (info->shared)
3783             info->flags |= DF_STATIC_TLS;
3784           goto dotlstoc;
3785
3786         case R_PPC64_DTPMOD64:
3787           if (rel + 1 < rel_end
3788               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
3789               && rel[1].r_offset == rel->r_offset + 8)
3790             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
3791           else
3792             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
3793           goto dotlstoc;
3794
3795         case R_PPC64_DTPREL64:
3796           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
3797           if (rel != relocs
3798               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
3799               && rel[-1].r_offset == rel->r_offset - 8)
3800             /* This is the second reloc of a dtpmod, dtprel pair.
3801                Don't mark with TLS_DTPREL.  */
3802             goto dodyn;
3803
3804         dotlstoc:
3805           sec->has_tls_reloc = 1;
3806           if (h != NULL)
3807             {
3808               struct ppc_link_hash_entry *eh;
3809               eh = (struct ppc_link_hash_entry *) h;
3810               eh->tls_mask |= tls_type;
3811             }
3812           else
3813             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
3814                                         rel->r_addend, tls_type))
3815               return FALSE;
3816
3817           if (ppc64_elf_section_data (sec)->t_symndx == NULL)
3818             {
3819               /* One extra to simplify get_tls_mask.  */
3820               bfd_size_type amt = sec->_raw_size * sizeof (unsigned) / 8 + 1;
3821               ppc64_elf_section_data (sec)->t_symndx = bfd_zalloc (abfd, amt);
3822               if (ppc64_elf_section_data (sec)->t_symndx == NULL)
3823                 return FALSE;
3824             }
3825           BFD_ASSERT (rel->r_offset % 8 == 0);
3826           ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8] = r_symndx;
3827
3828           /* Mark the second slot of a GD or LD entry.
3829              -1 to indicate GD and -2 to indicate LD.  */
3830           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3831             ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -1;
3832           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3833             ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -2;
3834           goto dodyn;
3835
3836         case R_PPC64_TPREL16:
3837         case R_PPC64_TPREL16_LO:
3838         case R_PPC64_TPREL16_HI:
3839         case R_PPC64_TPREL16_HA:
3840         case R_PPC64_TPREL16_DS:
3841         case R_PPC64_TPREL16_LO_DS:
3842         case R_PPC64_TPREL16_HIGHER:
3843         case R_PPC64_TPREL16_HIGHERA:
3844         case R_PPC64_TPREL16_HIGHEST:
3845         case R_PPC64_TPREL16_HIGHESTA:
3846           if (info->shared)
3847             {
3848               info->flags |= DF_STATIC_TLS;
3849               goto dodyn;
3850             }
3851           break;
3852
3853         case R_PPC64_ADDR64:
3854           if (opd_sym_map != NULL
3855               && h != NULL
3856               && h->root.root.string[0] == '.'
3857               && h->root.root.string[1] != 0)
3858             get_fdh ((struct ppc_link_hash_entry *) h, htab);
3859
3860           if (opd_sym_map != NULL
3861               && h == NULL
3862               && rel + 1 < rel_end
3863               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
3864             {
3865               asection *s;
3866
3867               s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
3868                                              r_symndx);
3869               if (s == NULL)
3870                 return FALSE;
3871               else if (s != sec)
3872                 opd_sym_map[rel->r_offset / 24] = s;
3873             }
3874           /* Fall through.  */
3875
3876         case R_PPC64_REL30:
3877         case R_PPC64_REL32:
3878         case R_PPC64_REL64:
3879         case R_PPC64_ADDR14:
3880         case R_PPC64_ADDR14_BRNTAKEN:
3881         case R_PPC64_ADDR14_BRTAKEN:
3882         case R_PPC64_ADDR16:
3883         case R_PPC64_ADDR16_DS:
3884         case R_PPC64_ADDR16_HA:
3885         case R_PPC64_ADDR16_HI:
3886         case R_PPC64_ADDR16_HIGHER:
3887         case R_PPC64_ADDR16_HIGHERA:
3888         case R_PPC64_ADDR16_HIGHEST:
3889         case R_PPC64_ADDR16_HIGHESTA:
3890         case R_PPC64_ADDR16_LO:
3891         case R_PPC64_ADDR16_LO_DS:
3892         case R_PPC64_ADDR24:
3893         case R_PPC64_ADDR32:
3894         case R_PPC64_UADDR16:
3895         case R_PPC64_UADDR32:
3896         case R_PPC64_UADDR64:
3897         case R_PPC64_TOC:
3898           if (h != NULL && !info->shared)
3899             /* We may need a copy reloc.  */
3900             h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
3901
3902           /* Don't propagate .opd relocs.  */
3903           if (NO_OPD_RELOCS && opd_sym_map != NULL)
3904             break;
3905
3906           /* Don't propagate relocs that the dynamic linker won't relocate.  */
3907           if ((sec->flags & SEC_ALLOC) == 0)
3908             break;
3909
3910           /* If we are creating a shared library, and this is a reloc
3911              against a global symbol, or a non PC relative reloc
3912              against a local symbol, then we need to copy the reloc
3913              into the shared library.  However, if we are linking with
3914              -Bsymbolic, we do not need to copy a reloc against a
3915              global symbol which is defined in an object we are
3916              including in the link (i.e., DEF_REGULAR is set).  At
3917              this point we have not seen all the input files, so it is
3918              possible that DEF_REGULAR is not set now but will be set
3919              later (it is never cleared).  In case of a weak definition,
3920              DEF_REGULAR may be cleared later by a strong definition in
3921              a shared library.  We account for that possibility below by
3922              storing information in the dyn_relocs field of the hash
3923              table entry.  A similar situation occurs when creating
3924              shared libraries and symbol visibility changes render the
3925              symbol local.
3926
3927              If on the other hand, we are creating an executable, we
3928              may need to keep relocations for symbols satisfied by a
3929              dynamic library if we manage to avoid copy relocs for the
3930              symbol.  */
3931         dodyn:
3932           if ((info->shared
3933                && (MUST_BE_DYN_RELOC (r_type)
3934                    || (h != NULL
3935                        && (! info->symbolic
3936                            || h->root.type == bfd_link_hash_defweak
3937                            || (h->elf_link_hash_flags
3938                                & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3939               || (ELIMINATE_COPY_RELOCS
3940                   && !info->shared
3941                   && h != NULL
3942                   && (h->root.type == bfd_link_hash_defweak
3943                       || (h->elf_link_hash_flags
3944                           & ELF_LINK_HASH_DEF_REGULAR) == 0)))
3945             {
3946               struct ppc_dyn_relocs *p;
3947               struct ppc_dyn_relocs **head;
3948
3949               /* We must copy these reloc types into the output file.
3950                  Create a reloc section in dynobj and make room for
3951                  this reloc.  */
3952               if (sreloc == NULL)
3953                 {
3954                   const char *name;
3955                   bfd *dynobj;
3956
3957                   name = (bfd_elf_string_from_elf_section
3958                           (abfd,
3959                            elf_elfheader (abfd)->e_shstrndx,
3960                            elf_section_data (sec)->rel_hdr.sh_name));
3961                   if (name == NULL)
3962                     return FALSE;
3963
3964                   if (strncmp (name, ".rela", 5) != 0
3965                       || strcmp (bfd_get_section_name (abfd, sec),
3966                                  name + 5) != 0)
3967                     {
3968                       (*_bfd_error_handler)
3969                         (_("%s: bad relocation section name `%s\'"),
3970                          bfd_archive_filename (abfd), name);
3971                       bfd_set_error (bfd_error_bad_value);
3972                     }
3973
3974                   dynobj = htab->elf.dynobj;
3975                   sreloc = bfd_get_section_by_name (dynobj, name);
3976                   if (sreloc == NULL)
3977                     {
3978                       flagword flags;
3979
3980                       sreloc = bfd_make_section (dynobj, name);
3981                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
3982                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3983                       if ((sec->flags & SEC_ALLOC) != 0)
3984                         flags |= SEC_ALLOC | SEC_LOAD;
3985                       if (sreloc == NULL
3986                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
3987                           || ! bfd_set_section_alignment (dynobj, sreloc, 3))
3988                         return FALSE;
3989                     }
3990                   elf_section_data (sec)->sreloc = sreloc;
3991                 }
3992
3993               /* If this is a global symbol, we count the number of
3994                  relocations we need for this symbol.  */
3995               if (h != NULL)
3996                 {
3997                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
3998                 }
3999               else
4000                 {
4001                   /* Track dynamic relocs needed for local syms too.
4002                      We really need local syms available to do this
4003                      easily.  Oh well.  */
4004
4005                   asection *s;
4006                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4007                                                  sec, r_symndx);
4008                   if (s == NULL)
4009                     return FALSE;
4010
4011                   head = ((struct ppc_dyn_relocs **)
4012                           &elf_section_data (s)->local_dynrel);
4013                 }
4014
4015               p = *head;
4016               if (p == NULL || p->sec != sec)
4017                 {
4018                   p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4019                   if (p == NULL)
4020                     return FALSE;
4021                   p->next = *head;
4022                   *head = p;
4023                   p->sec = sec;
4024                   p->count = 0;
4025                   p->pc_count = 0;
4026                 }
4027
4028               p->count += 1;
4029               if (!MUST_BE_DYN_RELOC (r_type))
4030                 p->pc_count += 1;
4031             }
4032           break;
4033
4034         default:
4035           break;
4036         }
4037     }
4038
4039   return TRUE;
4040 }
4041
4042 /* Return the section that should be marked against GC for a given
4043    relocation.  */
4044
4045 static asection *
4046 ppc64_elf_gc_mark_hook (asection *sec,
4047                         struct bfd_link_info *info ATTRIBUTE_UNUSED,
4048                         Elf_Internal_Rela *rel,
4049                         struct elf_link_hash_entry *h,
4050                         Elf_Internal_Sym *sym)
4051 {
4052   asection *rsec = NULL;
4053
4054   if (h != NULL)
4055     {
4056       enum elf_ppc64_reloc_type r_type;
4057       struct ppc_link_hash_entry *fdh;
4058
4059       r_type = ELF64_R_TYPE (rel->r_info);
4060       switch (r_type)
4061         {
4062         case R_PPC64_GNU_VTINHERIT:
4063         case R_PPC64_GNU_VTENTRY:
4064           break;
4065
4066         default:
4067           switch (h->root.type)
4068             {
4069             case bfd_link_hash_defined:
4070             case bfd_link_hash_defweak:
4071               fdh = (struct ppc_link_hash_entry *) h;
4072
4073               /* Function descriptor syms cause the associated
4074                  function code sym section to be marked.  */
4075               if (fdh->is_func_descriptor)
4076                 rsec = fdh->oh->root.u.def.section;
4077
4078               /* Function entry syms return NULL if they are in .opd
4079                  and are not ._start (or others undefined on the ld
4080                  command line).  Thus we avoid marking all function
4081                  sections, as all functions are referenced in .opd.  */
4082               else if ((fdh->oh != NULL
4083                         && ((struct ppc_link_hash_entry *) fdh->oh)->is_entry)
4084                        || ppc64_elf_section_data (sec)->opd.func_sec == NULL)
4085                 rsec = h->root.u.def.section;
4086               break;
4087
4088             case bfd_link_hash_common:
4089               rsec = h->root.u.c.p->section;
4090               break;
4091
4092             default:
4093               break;
4094             }
4095         }
4096     }
4097   else
4098     {
4099       asection **opd_sym_section;
4100
4101       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4102       opd_sym_section = ppc64_elf_section_data (rsec)->opd.func_sec;
4103       if (opd_sym_section != NULL)
4104         rsec = opd_sym_section[sym->st_value / 24];
4105       else if (ppc64_elf_section_data (sec)->opd.func_sec != NULL)
4106         rsec = NULL;
4107     }
4108
4109   return rsec;
4110 }
4111
4112 /* Update the .got, .plt. and dynamic reloc reference counts for the
4113    section being removed.  */
4114
4115 static bfd_boolean
4116 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
4117                          asection *sec, const Elf_Internal_Rela *relocs)
4118 {
4119   struct ppc_link_hash_table *htab;
4120   Elf_Internal_Shdr *symtab_hdr;
4121   struct elf_link_hash_entry **sym_hashes;
4122   struct got_entry **local_got_ents;
4123   const Elf_Internal_Rela *rel, *relend;
4124
4125   elf_section_data (sec)->local_dynrel = NULL;
4126
4127   htab = ppc_hash_table (info);
4128   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4129   sym_hashes = elf_sym_hashes (abfd);
4130   local_got_ents = elf_local_got_ents (abfd);
4131
4132   relend = relocs + sec->reloc_count;
4133   for (rel = relocs; rel < relend; rel++)
4134     {
4135       unsigned long r_symndx;
4136       enum elf_ppc64_reloc_type r_type;
4137       struct elf_link_hash_entry *h = NULL;
4138       char tls_type = 0;
4139
4140       r_symndx = ELF64_R_SYM (rel->r_info);
4141       r_type = ELF64_R_TYPE (rel->r_info);
4142       if (r_symndx >= symtab_hdr->sh_info)
4143         {
4144           struct ppc_link_hash_entry *eh;
4145           struct ppc_dyn_relocs **pp;
4146           struct ppc_dyn_relocs *p;
4147
4148           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4149           eh = (struct ppc_link_hash_entry *) h;
4150
4151           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4152             if (p->sec == sec)
4153               {
4154                 /* Everything must go for SEC.  */
4155                 *pp = p->next;
4156                 break;
4157               }
4158         }
4159
4160       switch (r_type)
4161         {
4162         case R_PPC64_GOT_TLSLD16:
4163         case R_PPC64_GOT_TLSLD16_LO:
4164         case R_PPC64_GOT_TLSLD16_HI:
4165         case R_PPC64_GOT_TLSLD16_HA:
4166           ppc64_tlsld_got (abfd)->refcount -= 1;
4167           tls_type = TLS_TLS | TLS_LD;
4168           goto dogot;
4169
4170         case R_PPC64_GOT_TLSGD16:
4171         case R_PPC64_GOT_TLSGD16_LO:
4172         case R_PPC64_GOT_TLSGD16_HI:
4173         case R_PPC64_GOT_TLSGD16_HA:
4174           tls_type = TLS_TLS | TLS_GD;
4175           goto dogot;
4176
4177         case R_PPC64_GOT_TPREL16_DS:
4178         case R_PPC64_GOT_TPREL16_LO_DS:
4179         case R_PPC64_GOT_TPREL16_HI:
4180         case R_PPC64_GOT_TPREL16_HA:
4181           tls_type = TLS_TLS | TLS_TPREL;
4182           goto dogot;
4183
4184         case R_PPC64_GOT_DTPREL16_DS:
4185         case R_PPC64_GOT_DTPREL16_LO_DS:
4186         case R_PPC64_GOT_DTPREL16_HI:
4187         case R_PPC64_GOT_DTPREL16_HA:
4188           tls_type = TLS_TLS | TLS_DTPREL;
4189           goto dogot;
4190
4191         case R_PPC64_GOT16:
4192         case R_PPC64_GOT16_DS:
4193         case R_PPC64_GOT16_HA:
4194         case R_PPC64_GOT16_HI:
4195         case R_PPC64_GOT16_LO:
4196         case R_PPC64_GOT16_LO_DS:
4197         dogot:
4198           {
4199             struct got_entry *ent;
4200
4201             if (h != NULL)
4202               ent = h->got.glist;
4203             else
4204               ent = local_got_ents[r_symndx];
4205
4206             for (; ent != NULL; ent = ent->next)
4207               if (ent->addend == rel->r_addend
4208                   && ent->owner == abfd
4209                   && ent->tls_type == tls_type)
4210                 break;
4211             if (ent == NULL)
4212               abort ();
4213             if (ent->got.refcount > 0)
4214               ent->got.refcount -= 1;
4215           }
4216           break;
4217
4218         case R_PPC64_PLT16_HA:
4219         case R_PPC64_PLT16_HI:
4220         case R_PPC64_PLT16_LO:
4221         case R_PPC64_PLT32:
4222         case R_PPC64_PLT64:
4223         case R_PPC64_REL14:
4224         case R_PPC64_REL14_BRNTAKEN:
4225         case R_PPC64_REL14_BRTAKEN:
4226         case R_PPC64_REL24:
4227           if (h != NULL)
4228             {
4229               struct plt_entry *ent;
4230
4231               for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4232                 if (ent->addend == rel->r_addend)
4233                   break;
4234               if (ent == NULL)
4235                 abort ();
4236               if (ent->plt.refcount > 0)
4237                 ent->plt.refcount -= 1;
4238             }
4239           break;
4240
4241         default:
4242           break;
4243         }
4244     }
4245   return TRUE;
4246 }
4247
4248 /* Called via elf_link_hash_traverse to transfer dynamic linking
4249    information on function code symbol entries to their corresponding
4250    function descriptor symbol entries.  */
4251 static bfd_boolean
4252 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
4253 {
4254   struct bfd_link_info *info;
4255   struct ppc_link_hash_table *htab;
4256   struct plt_entry *ent;
4257   struct ppc_link_hash_entry *fh;
4258   struct ppc_link_hash_entry *fdh;
4259   bfd_boolean force_local;
4260
4261   fh = (struct ppc_link_hash_entry *) h;
4262   if (fh->elf.root.type == bfd_link_hash_indirect)
4263     return TRUE;
4264
4265   if (fh->elf.root.type == bfd_link_hash_warning)
4266     fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
4267
4268   info = inf;
4269   htab = ppc_hash_table (info);
4270
4271   /* If this is a function code symbol, transfer dynamic linking
4272      information to the function descriptor symbol.  */
4273   if (!fh->is_func)
4274     return TRUE;
4275
4276   if (fh->elf.root.type == bfd_link_hash_undefweak
4277       && (fh->elf.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR))
4278     htab->have_undefweak = TRUE;
4279
4280   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
4281     if (ent->plt.refcount > 0)
4282       break;
4283   if (ent == NULL
4284       || fh->elf.root.root.string[0] != '.'
4285       || fh->elf.root.root.string[1] == '\0')
4286     return TRUE;
4287
4288   /* Find the corresponding function descriptor symbol.  Create it
4289      as undefined if necessary.  */
4290
4291   fdh = get_fdh (fh, htab);
4292   if (fdh != NULL)
4293     while (fdh->elf.root.type == bfd_link_hash_indirect
4294            || fdh->elf.root.type == bfd_link_hash_warning)
4295       fdh = (struct ppc_link_hash_entry *) fdh->elf.root.u.i.link;
4296
4297   if (fdh == NULL
4298       && info->shared
4299       && (fh->elf.root.type == bfd_link_hash_undefined
4300           || fh->elf.root.type == bfd_link_hash_undefweak))
4301     {
4302       bfd *abfd;
4303       asymbol *newsym;
4304       struct bfd_link_hash_entry *bh;
4305
4306       abfd = fh->elf.root.u.undef.abfd;
4307       newsym = bfd_make_empty_symbol (abfd);
4308       newsym->name = fh->elf.root.root.string + 1;
4309       newsym->section = bfd_und_section_ptr;
4310       newsym->value = 0;
4311       newsym->flags = BSF_OBJECT;
4312       if (fh->elf.root.type == bfd_link_hash_undefweak)
4313         newsym->flags |= BSF_WEAK;
4314
4315       bh = &fdh->elf.root;
4316       if ( !(_bfd_generic_link_add_one_symbol
4317              (info, abfd, newsym->name, newsym->flags,
4318               newsym->section, newsym->value, NULL, FALSE, FALSE, &bh)))
4319         {
4320           return FALSE;
4321         }
4322       fdh = (struct ppc_link_hash_entry *) bh;
4323       fdh->elf.elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
4324       fdh->elf.size = 24;
4325       fdh->elf.type = STT_OBJECT;
4326     }
4327
4328   if (fdh != NULL
4329       && (fdh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
4330       && (info->shared
4331           || (fdh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4332           || (fdh->elf.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
4333           || (fdh->elf.root.type == bfd_link_hash_undefweak
4334               && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
4335     {
4336       if (fdh->elf.dynindx == -1)
4337         if (! bfd_elf64_link_record_dynamic_symbol (info, &fdh->elf))
4338           return FALSE;
4339       fdh->elf.elf_link_hash_flags
4340         |= (fh->elf.elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
4341                                       | ELF_LINK_HASH_REF_DYNAMIC
4342                                       | ELF_LINK_HASH_REF_REGULAR_NONWEAK
4343                                       | ELF_LINK_NON_GOT_REF));
4344       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
4345         {
4346           fdh->elf.plt.plist = fh->elf.plt.plist;
4347           fdh->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4348         }
4349       fdh->is_func_descriptor = 1;
4350       fdh->oh = &fh->elf;
4351       fh->oh = &fdh->elf;
4352     }
4353
4354   /* Now that the info is on the function descriptor, clear the
4355      function code sym info.  Any function code syms for which we
4356      don't have a definition in a regular file, we force local.
4357      This prevents a shared library from exporting syms that have
4358      been imported from another library.  Function code syms that
4359      are really in the library we must leave global to prevent the
4360      linker dragging in a definition from a static library.  */
4361   force_local
4362     = (info->shared
4363        && ((fh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4364            || fdh == NULL
4365            || (fdh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4366            || (fdh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0));
4367   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
4368
4369   return TRUE;
4370 }
4371
4372 #define MIN_SAVE_FPR 14
4373 #define MAX_SAVE_FPR 31
4374
4375 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
4376    this hook to a) provide some gcc support functions, and b) transfer
4377    dynamic linking information gathered so far on function code symbol
4378    entries, to their corresponding function descriptor symbol entries.  */
4379 static bfd_boolean
4380 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
4381                             struct bfd_link_info *info)
4382 {
4383   struct ppc_link_hash_table *htab;
4384   unsigned int lowest_savef = MAX_SAVE_FPR + 2;
4385   unsigned int lowest_restf = MAX_SAVE_FPR + 2;
4386   unsigned int i;
4387   struct elf_link_hash_entry *h;
4388   bfd_byte *p;
4389   char sym[10];
4390
4391   htab = ppc_hash_table (info);
4392
4393   if (htab->sfpr == NULL)
4394     /* We don't have any relocs.  */
4395     return TRUE;
4396
4397   /* First provide any missing ._savef* and ._restf* functions.  */
4398   memcpy (sym, "._savef14", 10);
4399   for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
4400     {
4401       sym[7] = i / 10 + '0';
4402       sym[8] = i % 10 + '0';
4403       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
4404       if (h != NULL
4405           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4406         {
4407           if (lowest_savef > i)
4408             lowest_savef = i;
4409           h->root.type = bfd_link_hash_defined;
4410           h->root.u.def.section = htab->sfpr;
4411           h->root.u.def.value = (i - lowest_savef) * 4;
4412           h->type = STT_FUNC;
4413           h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4414           _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
4415         }
4416     }
4417
4418   memcpy (sym, "._restf14", 10);
4419   for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
4420     {
4421       sym[7] = i / 10 + '0';
4422       sym[8] = i % 10 + '0';
4423       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
4424       if (h != NULL
4425           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4426         {
4427           if (lowest_restf > i)
4428             lowest_restf = i;
4429           h->root.type = bfd_link_hash_defined;
4430           h->root.u.def.section = htab->sfpr;
4431           h->root.u.def.value = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
4432                                  + (i - lowest_restf) * 4);
4433           h->type = STT_FUNC;
4434           h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4435           _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
4436         }
4437     }
4438
4439   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
4440
4441   htab->sfpr->_raw_size = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
4442                            + (MAX_SAVE_FPR + 2 - lowest_restf) * 4);
4443
4444   if (htab->sfpr->_raw_size == 0)
4445     {
4446       if (!htab->have_undefweak)
4447         {
4448           _bfd_strip_section_from_output (info, htab->sfpr);
4449           return TRUE;
4450         }
4451
4452       htab->sfpr->_raw_size = 4;
4453     }
4454
4455   p = bfd_alloc (htab->elf.dynobj, htab->sfpr->_raw_size);
4456   if (p == NULL)
4457     return FALSE;
4458   htab->sfpr->contents = p;
4459
4460   for (i = lowest_savef; i <= MAX_SAVE_FPR; i++)
4461     {
4462       unsigned int fpr = i << 21;
4463       unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
4464       bfd_put_32 (htab->elf.dynobj, STFD_FR0_0R1 + fpr + stackoff, p);
4465       p += 4;
4466     }
4467   if (lowest_savef <= MAX_SAVE_FPR)
4468     {
4469       bfd_put_32 (htab->elf.dynobj, BLR, p);
4470       p += 4;
4471     }
4472
4473   for (i = lowest_restf; i <= MAX_SAVE_FPR; i++)
4474     {
4475       unsigned int fpr = i << 21;
4476       unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
4477       bfd_put_32 (htab->elf.dynobj, LFD_FR0_0R1 + fpr + stackoff, p);
4478       p += 4;
4479     }
4480   if (lowest_restf <= MAX_SAVE_FPR
4481       || htab->sfpr->_raw_size == 4)
4482     {
4483       bfd_put_32 (htab->elf.dynobj, BLR, p);
4484     }
4485
4486   return TRUE;
4487 }
4488
4489 /* Adjust a symbol defined by a dynamic object and referenced by a
4490    regular object.  The current definition is in some section of the
4491    dynamic object, but we're not including those sections.  We have to
4492    change the definition to something the rest of the link can
4493    understand.  */
4494
4495 static bfd_boolean
4496 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
4497                                  struct elf_link_hash_entry *h)
4498 {
4499   struct ppc_link_hash_table *htab;
4500   asection *s;
4501   unsigned int power_of_two;
4502
4503   htab = ppc_hash_table (info);
4504
4505   /* Deal with function syms.  */
4506   if (h->type == STT_FUNC
4507       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4508     {
4509       /* Clear procedure linkage table information for any symbol that
4510          won't need a .plt entry.  */
4511       struct plt_entry *ent;
4512       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4513         if (ent->plt.refcount > 0)
4514           break;
4515       if (!((struct ppc_link_hash_entry *) h)->is_func_descriptor
4516           || ent == NULL
4517           || SYMBOL_CALLS_LOCAL (info, h)
4518           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4519               && h->root.type == bfd_link_hash_undefweak))
4520         {
4521           h->plt.plist = NULL;
4522           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4523         }
4524     }
4525   else
4526     h->plt.plist = NULL;
4527
4528   /* If this is a weak symbol, and there is a real definition, the
4529      processor independent code will have arranged for us to see the
4530      real definition first, and we can just use the same value.  */
4531   if (h->weakdef != NULL)
4532     {
4533       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
4534                   || h->weakdef->root.type == bfd_link_hash_defweak);
4535       h->root.u.def.section = h->weakdef->root.u.def.section;
4536       h->root.u.def.value = h->weakdef->root.u.def.value;
4537       if (ELIMINATE_COPY_RELOCS)
4538         h->elf_link_hash_flags
4539           = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
4540              | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
4541       return TRUE;
4542     }
4543
4544   /* If we are creating a shared library, we must presume that the
4545      only references to the symbol are via the global offset table.
4546      For such cases we need not do anything here; the relocations will
4547      be handled correctly by relocate_section.  */
4548   if (info->shared)
4549     return TRUE;
4550
4551   /* If there are no references to this symbol that do not use the
4552      GOT, we don't need to generate a copy reloc.  */
4553   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
4554     return TRUE;
4555
4556   if (ELIMINATE_COPY_RELOCS)
4557     {
4558       struct ppc_link_hash_entry * eh;
4559       struct ppc_dyn_relocs *p;
4560
4561       eh = (struct ppc_link_hash_entry *) h;
4562       for (p = eh->dyn_relocs; p != NULL; p = p->next)
4563         {
4564           s = p->sec->output_section;
4565           if (s != NULL && (s->flags & SEC_READONLY) != 0)
4566             break;
4567         }
4568
4569       /* If we didn't find any dynamic relocs in read-only sections, then
4570          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
4571       if (p == NULL)
4572         {
4573           h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
4574           return TRUE;
4575         }
4576     }
4577
4578   if (h->plt.plist != NULL)
4579     {
4580       /* We should never get here, but unfortunately there are versions
4581          of gcc out there that improperly (for this ABI) put initialized
4582          function pointers, vtable refs and suchlike in read-only
4583          sections.  Allow them to proceed, but warn that this might
4584          break at runtime.  */
4585       (*_bfd_error_handler)
4586         (_("copy reloc against `%s' requires lazy plt linking; "
4587            "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
4588          h->root.root.string);
4589     }
4590
4591   /* This is a reference to a symbol defined by a dynamic object which
4592      is not a function.  */
4593
4594   /* We must allocate the symbol in our .dynbss section, which will
4595      become part of the .bss section of the executable.  There will be
4596      an entry for this symbol in the .dynsym section.  The dynamic
4597      object will contain position independent code, so all references
4598      from the dynamic object to this symbol will go through the global
4599      offset table.  The dynamic linker will use the .dynsym entry to
4600      determine the address it must put in the global offset table, so
4601      both the dynamic object and the regular object will refer to the
4602      same memory location for the variable.  */
4603
4604   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
4605      to copy the initial value out of the dynamic object and into the
4606      runtime process image.  We need to remember the offset into the
4607      .rela.bss section we are going to use.  */
4608   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4609     {
4610       htab->relbss->_raw_size += sizeof (Elf64_External_Rela);
4611       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
4612     }
4613
4614   /* We need to figure out the alignment required for this symbol.  I
4615      have no idea how ELF linkers handle this.  */
4616   power_of_two = bfd_log2 (h->size);
4617   if (power_of_two > 4)
4618     power_of_two = 4;
4619
4620   /* Apply the required alignment.  */
4621   s = htab->dynbss;
4622   s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
4623   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
4624     {
4625       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
4626         return FALSE;
4627     }
4628
4629   /* Define the symbol as being at this point in the section.  */
4630   h->root.u.def.section = s;
4631   h->root.u.def.value = s->_raw_size;
4632
4633   /* Increment the section size to make room for the symbol.  */
4634   s->_raw_size += h->size;
4635
4636   return TRUE;
4637 }
4638
4639 /* If given a function descriptor symbol, hide both the function code
4640    sym and the descriptor.  */
4641 static void
4642 ppc64_elf_hide_symbol (struct bfd_link_info *info,
4643                        struct elf_link_hash_entry *h,
4644                        bfd_boolean force_local)
4645 {
4646   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
4647
4648   if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
4649     {
4650       struct elf_link_hash_entry *fh = ((struct ppc_link_hash_entry *) h)->oh;
4651
4652       if (fh == NULL)
4653         {
4654           const char *p, *q;
4655           struct ppc_link_hash_table *htab;
4656           char save;
4657
4658           /* We aren't supposed to use alloca in BFD because on
4659              systems which do not have alloca the version in libiberty
4660              calls xmalloc, which might cause the program to crash
4661              when it runs out of memory.  This function doesn't have a
4662              return status, so there's no way to gracefully return an
4663              error.  So cheat.  We know that string[-1] can be safely
4664              dereferenced;  It's either a string in an ELF string
4665              table, or allocated in an objalloc structure.  */
4666
4667           p = h->root.root.string - 1;
4668           save = *p;
4669           *(char *) p = '.';
4670           htab = ppc_hash_table (info);
4671           fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
4672           *(char *) p = save;
4673
4674           /* Unfortunately, if it so happens that the string we were
4675              looking for was allocated immediately before this string,
4676              then we overwrote the string terminator.  That's the only
4677              reason the lookup should fail.  */
4678           if (fh == NULL)
4679             {
4680               q = h->root.root.string + strlen (h->root.root.string);
4681               while (q >= h->root.root.string && *q == *p)
4682                 --q, --p;
4683               if (q < h->root.root.string && *p == '.')
4684                 fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
4685             }
4686           if (fh != NULL)
4687             {
4688               ((struct ppc_link_hash_entry *) h)->oh = fh;
4689               ((struct ppc_link_hash_entry *) fh)->oh = h;
4690             }
4691         }
4692       if (fh != NULL)
4693         _bfd_elf_link_hash_hide_symbol (info, fh, force_local);
4694     }
4695 }
4696
4697 static bfd_boolean
4698 get_sym_h (struct elf_link_hash_entry **hp, Elf_Internal_Sym **symp,
4699            asection **symsecp, char **tls_maskp, Elf_Internal_Sym **locsymsp,
4700            unsigned long r_symndx, bfd *ibfd)
4701 {
4702   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4703
4704   if (r_symndx >= symtab_hdr->sh_info)
4705     {
4706       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
4707       struct elf_link_hash_entry *h;
4708
4709       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4710       while (h->root.type == bfd_link_hash_indirect
4711              || h->root.type == bfd_link_hash_warning)
4712         h = (struct elf_link_hash_entry *) h->root.u.i.link;
4713
4714       if (hp != NULL)
4715         *hp = h;
4716
4717       if (symp != NULL)
4718         *symp = NULL;
4719
4720       if (symsecp != NULL)
4721         {
4722           asection *symsec = NULL;
4723           if (h->root.type == bfd_link_hash_defined
4724               || h->root.type == bfd_link_hash_defweak)
4725             symsec = h->root.u.def.section;
4726           *symsecp = symsec;
4727         }
4728
4729       if (tls_maskp != NULL)
4730         {
4731           struct ppc_link_hash_entry *eh;
4732
4733           eh = (struct ppc_link_hash_entry *) h;
4734           *tls_maskp = &eh->tls_mask;
4735         }
4736     }
4737   else
4738     {
4739       Elf_Internal_Sym *sym;
4740       Elf_Internal_Sym *locsyms = *locsymsp;
4741
4742       if (locsyms == NULL)
4743         {
4744           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4745           if (locsyms == NULL)
4746             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4747                                             symtab_hdr->sh_info,
4748                                             0, NULL, NULL, NULL);
4749           if (locsyms == NULL)
4750             return FALSE;
4751           *locsymsp = locsyms;
4752         }
4753       sym = locsyms + r_symndx;
4754
4755       if (hp != NULL)
4756         *hp = NULL;
4757
4758       if (symp != NULL)
4759         *symp = sym;
4760
4761       if (symsecp != NULL)
4762         {
4763           asection *symsec = NULL;
4764           if ((sym->st_shndx != SHN_UNDEF
4765                && sym->st_shndx < SHN_LORESERVE)
4766               || sym->st_shndx > SHN_HIRESERVE)
4767             symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
4768           *symsecp = symsec;
4769         }
4770
4771       if (tls_maskp != NULL)
4772         {
4773           struct got_entry **lgot_ents;
4774           char *tls_mask;
4775
4776           tls_mask = NULL;
4777           lgot_ents = elf_local_got_ents (ibfd);
4778           if (lgot_ents != NULL)
4779             {
4780               char *lgot_masks = (char *) (lgot_ents + symtab_hdr->sh_info);
4781               tls_mask = &lgot_masks[r_symndx];
4782             }
4783           *tls_maskp = tls_mask;
4784         }
4785     }
4786   return TRUE;
4787 }
4788
4789 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
4790    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
4791    type suitable for optimization, and 1 otherwise.  */
4792
4793 static int
4794 get_tls_mask (char **tls_maskp, unsigned long *toc_symndx,
4795               Elf_Internal_Sym **locsymsp,
4796               const Elf_Internal_Rela *rel, bfd *ibfd)
4797 {
4798   unsigned long r_symndx;
4799   int next_r;
4800   struct elf_link_hash_entry *h;
4801   Elf_Internal_Sym *sym;
4802   asection *sec;
4803   bfd_vma off;
4804
4805   r_symndx = ELF64_R_SYM (rel->r_info);
4806   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
4807     return 0;
4808
4809   if ((*tls_maskp != NULL && **tls_maskp != 0)
4810       || sec == NULL
4811       || ppc64_elf_section_data (sec)->t_symndx == NULL)
4812     return 1;
4813
4814   /* Look inside a TOC section too.  */
4815   if (h != NULL)
4816     {
4817       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
4818       off = h->root.u.def.value;
4819     }
4820   else
4821     off = sym->st_value;
4822   off += rel->r_addend;
4823   BFD_ASSERT (off % 8 == 0);
4824   r_symndx = ppc64_elf_section_data (sec)->t_symndx[off / 8];
4825   next_r = ppc64_elf_section_data (sec)->t_symndx[off / 8 + 1];
4826   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
4827     return 0;
4828   if (toc_symndx != NULL)
4829     *toc_symndx = r_symndx;
4830   if ((h == NULL
4831        || ((h->root.type == bfd_link_hash_defined
4832             || h->root.type == bfd_link_hash_defweak)
4833            && !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
4834       && (next_r == -1 || next_r == -2))
4835     return 1 - next_r;
4836   return 1;
4837 }
4838
4839 bfd_boolean
4840 ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info)
4841 {
4842   bfd *ibfd;
4843
4844   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4845     {
4846       asection *sec;
4847       Elf_Internal_Rela *relstart, *rel, *relend;
4848       Elf_Internal_Shdr *symtab_hdr;
4849       Elf_Internal_Sym *local_syms;
4850       struct elf_link_hash_entry **sym_hashes;
4851       bfd_vma offset;
4852       bfd_size_type amt;
4853       long *adjust;
4854       bfd_boolean need_edit;
4855
4856       sec = bfd_get_section_by_name (ibfd, ".opd");
4857       if (sec == NULL)
4858         continue;
4859
4860       amt = sec->_raw_size * sizeof (long) / 24;
4861       adjust = ppc64_elf_section_data (sec)->opd.adjust;
4862       if (adjust == NULL)
4863         {
4864           /* Must be a ld -r link.  ie. check_relocs hasn't been
4865              called.  */
4866           adjust = bfd_zalloc (obfd, amt);
4867           ppc64_elf_section_data (sec)->opd.adjust = adjust;
4868         }
4869       memset (adjust, 0, amt);
4870
4871       if (sec->output_section == bfd_abs_section_ptr)
4872         continue;
4873
4874       /* Look through the section relocs.  */
4875       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
4876         continue;
4877
4878       local_syms = NULL;
4879       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4880       sym_hashes = elf_sym_hashes (ibfd);
4881
4882       /* Read the relocations.  */
4883       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
4884                                             info->keep_memory);
4885       if (relstart == NULL)
4886         return FALSE;
4887
4888       /* First run through the relocs to check they are sane, and to
4889          determine whether we need to edit this opd section.  */
4890       need_edit = FALSE;
4891       offset = 0;
4892       relend = relstart + sec->reloc_count;
4893       for (rel = relstart; rel < relend; )
4894         {
4895           enum elf_ppc64_reloc_type r_type;
4896           unsigned long r_symndx;
4897           asection *sym_sec;
4898           struct elf_link_hash_entry *h;
4899           Elf_Internal_Sym *sym;
4900
4901           /* .opd contains a regular array of 24 byte entries.  We're
4902              only interested in the reloc pointing to a function entry
4903              point.  */
4904           if (rel->r_offset != offset
4905               || rel + 1 >= relend
4906               || (rel + 1)->r_offset != offset + 8)
4907             {
4908               /* If someone messes with .opd alignment then after a
4909                  "ld -r" we might have padding in the middle of .opd.
4910                  Also, there's nothing to prevent someone putting
4911                  something silly in .opd with the assembler.  No .opd
4912                  optimization for them!  */
4913               (*_bfd_error_handler)
4914                 (_("%s: .opd is not a regular array of opd entries"),
4915                  bfd_archive_filename (ibfd));
4916               need_edit = FALSE;
4917               break;
4918             }
4919
4920           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
4921               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
4922             {
4923               (*_bfd_error_handler)
4924                 (_("%s: unexpected reloc type %u in .opd section"),
4925                  bfd_archive_filename (ibfd), r_type);
4926               need_edit = FALSE;
4927               break;
4928             }
4929
4930           r_symndx = ELF64_R_SYM (rel->r_info);
4931           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
4932                           r_symndx, ibfd))
4933             goto error_ret;
4934
4935           if (sym_sec == NULL || sym_sec->owner == NULL)
4936             {
4937               const char *sym_name;
4938               if (h != NULL)
4939                 sym_name = h->root.root.string;
4940               else
4941                 sym_name = bfd_elf_local_sym_name (ibfd, sym);
4942
4943               (*_bfd_error_handler)
4944                 (_("%s: undefined sym `%s' in .opd section"),
4945                  bfd_archive_filename (ibfd),
4946                  sym_name);
4947               need_edit = FALSE;
4948               break;
4949             }
4950
4951           /* opd entries are always for functions defined in the
4952              current input bfd.  If the symbol isn't defined in the
4953              input bfd, then we won't be using the function in this
4954              bfd;  It must be defined in a linkonce section in another
4955              bfd, or is weak.  It's also possible that we are
4956              discarding the function due to a linker script /DISCARD/,
4957              which we test for via the output_section.  */
4958           if (sym_sec->owner != ibfd
4959               || sym_sec->output_section == bfd_abs_section_ptr)
4960             need_edit = TRUE;
4961
4962           offset += 24;
4963           rel += 2;
4964           /* Allow for the possibility of a reloc on the third word.  */
4965           if (rel < relend
4966               && rel->r_offset == offset - 8)
4967             rel += 1;
4968         }
4969
4970       if (need_edit)
4971         {
4972           Elf_Internal_Rela *write_rel;
4973           bfd_byte *rptr, *wptr;
4974           bfd_boolean skip;
4975
4976           /* This seems a waste of time as input .opd sections are all
4977              zeros as generated by gcc, but I suppose there's no reason
4978              this will always be so.  We might start putting something in
4979              the third word of .opd entries.  */
4980           if ((sec->flags & SEC_IN_MEMORY) == 0)
4981             {
4982               bfd_byte *loc = bfd_alloc (ibfd, sec->_raw_size);
4983               if (loc == NULL
4984                   || !bfd_get_section_contents (ibfd, sec, loc, 0,
4985                                                 sec->_raw_size))
4986                 {
4987                 error_ret:
4988                   if (local_syms != NULL
4989                       && symtab_hdr->contents != (unsigned char *) local_syms)
4990                     free (local_syms);
4991                   if (elf_section_data (sec)->relocs != relstart)
4992                     free (relstart);
4993                   return FALSE;
4994                 }
4995               sec->contents = loc;
4996               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
4997             }
4998
4999           elf_section_data (sec)->relocs = relstart;
5000
5001           wptr = sec->contents;
5002           rptr = sec->contents;
5003           write_rel = relstart;
5004           skip = FALSE;
5005           offset = 0;
5006           for (rel = relstart; rel < relend; rel++)
5007             {
5008               unsigned long r_symndx;
5009               asection *sym_sec;
5010               struct elf_link_hash_entry *h;
5011               Elf_Internal_Sym *sym;
5012
5013               r_symndx = ELF64_R_SYM (rel->r_info);
5014               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
5015                               r_symndx, ibfd))  
5016                 goto error_ret;
5017
5018               if (rel->r_offset == offset)
5019                 {
5020                   struct ppc_link_hash_entry *fdh = NULL;
5021                   if (h != NULL)
5022                     fdh = get_fdh ((struct ppc_link_hash_entry *) h,
5023                                    ppc_hash_table (info));
5024
5025                   skip = (sym_sec->owner != ibfd
5026                           || sym_sec->output_section == bfd_abs_section_ptr);
5027                   if (skip)
5028                     {
5029                       if (h != NULL && sym_sec->owner == ibfd)
5030                         {
5031                           /* Arrange for the function descriptor sym
5032                              to be dropped.  */
5033                           fdh->elf.root.u.def.value = 0;
5034                           fdh->elf.root.u.def.section = sym_sec;
5035                         }
5036                     }
5037                   else
5038                     {
5039                       /* We'll be keeping this opd entry.  */
5040
5041                       if (h != NULL)
5042                         {
5043                           /* Redefine the function descriptor symbol
5044                              to this location in the opd section.
5045                              We've checked above that opd relocs are
5046                              ordered.  */
5047                           fdh->elf.root.u.def.value = wptr - sec->contents;
5048                         }
5049                       else
5050                         {
5051                           /* Local syms are a bit tricky.  We could
5052                              tweak them as they can be cached, but
5053                              we'd need to look through the local syms
5054                              for the function descriptor sym which we
5055                              don't have at the moment.  So keep an
5056                              array of adjustments.  */
5057                           adjust[rel->r_offset / 24] = wptr - rptr;
5058                         }
5059
5060                       if (wptr != rptr)
5061                         memcpy (wptr, rptr, 24);
5062                       wptr += 24;
5063                     }
5064                   rptr += 24;
5065                   offset += 24;
5066                 }
5067
5068               if (skip)
5069                 {
5070                   BFD_ASSERT (MUST_BE_DYN_RELOC (ELF64_R_TYPE (rel->r_info)));
5071                   if (info->shared)
5072                     {
5073                       /* We won't be needing dynamic relocs here.  */
5074                       struct ppc_dyn_relocs **pp;
5075                       struct ppc_dyn_relocs *p;
5076
5077                       if (h != NULL)
5078                         pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5079                       else if (sym_sec != NULL)
5080                         pp = ((struct ppc_dyn_relocs **)
5081                               &elf_section_data (sym_sec)->local_dynrel);
5082                       else
5083                         pp = ((struct ppc_dyn_relocs **)
5084                               &elf_section_data (sec)->local_dynrel);
5085                       while ((p = *pp) != NULL)
5086                         {
5087                           if (p->sec == sec)
5088                             {
5089                               p->count -= 1;
5090                               if (p->count == 0)
5091                                 *pp = p->next;  
5092                               break;
5093                             }
5094                           pp = &p->next;
5095                         }
5096                     }
5097                 }
5098               else
5099                 {
5100                   /* We need to adjust any reloc offsets to point to the
5101                      new opd entries.  While we're at it, we may as well
5102                      remove redundant relocs.  */
5103                   rel->r_offset += wptr - rptr;
5104                   if (write_rel != rel)
5105                     memcpy (write_rel, rel, sizeof (*rel));
5106                   ++write_rel;
5107                 }
5108             }
5109
5110           sec->_cooked_size = wptr - sec->contents;
5111           sec->reloc_count = write_rel - relstart;
5112           /* Fudge the size too, as this is used later in
5113              elf_bfd_final_link if we are emitting relocs.  */
5114           elf_section_data (sec)->rel_hdr.sh_size
5115             = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
5116           BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
5117         }
5118       else if (elf_section_data (sec)->relocs != relstart)
5119         free (relstart);
5120
5121       if (local_syms != NULL
5122           && symtab_hdr->contents != (unsigned char *) local_syms)
5123         {
5124           if (!info->keep_memory)
5125             free (local_syms);
5126           else
5127             symtab_hdr->contents = (unsigned char *) local_syms;
5128         }
5129     }
5130
5131   return TRUE;
5132 }
5133
5134 /* Set htab->tls_sec.  */
5135
5136 bfd_boolean
5137 ppc64_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
5138 {
5139   asection *tls;
5140   struct ppc_link_hash_table *htab;
5141
5142   for (tls = obfd->sections; tls != NULL; tls = tls->next)
5143     if ((tls->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
5144         == (SEC_THREAD_LOCAL | SEC_LOAD))
5145       break;
5146
5147   htab = ppc_hash_table (info);
5148   htab->tls_sec = tls;
5149
5150   if (htab->tls_get_addr != NULL)
5151     {
5152       struct elf_link_hash_entry *h = htab->tls_get_addr;
5153
5154       while (h->root.type == bfd_link_hash_indirect
5155              || h->root.type == bfd_link_hash_warning)
5156         h = (struct elf_link_hash_entry *) h->root.u.i.link;
5157
5158       htab->tls_get_addr = h;
5159     }
5160
5161   return tls != NULL;
5162 }
5163
5164 /* Run through all the TLS relocs looking for optimization
5165    opportunities.  The linker has been hacked (see ppc64elf.em) to do
5166    a preliminary section layout so that we know the TLS segment
5167    offsets.  We can't optimize earlier because some optimizations need
5168    to know the tp offset, and we need to optimize before allocating
5169    dynamic relocations.  */
5170
5171 bfd_boolean
5172 ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
5173 {
5174   bfd *ibfd;
5175   asection *sec;
5176   struct ppc_link_hash_table *htab;
5177
5178   if (info->relocatable || info->shared)
5179     return TRUE;
5180
5181   htab = ppc_hash_table (info);
5182   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5183     {
5184       Elf_Internal_Sym *locsyms = NULL;
5185
5186       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
5187         if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
5188           {
5189             Elf_Internal_Rela *relstart, *rel, *relend;
5190             int expecting_tls_get_addr;
5191
5192             /* Read the relocations.  */
5193             relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
5194                                                   info->keep_memory);
5195             if (relstart == NULL)
5196               return FALSE;
5197
5198             expecting_tls_get_addr = 0;
5199             relend = relstart + sec->reloc_count;
5200             for (rel = relstart; rel < relend; rel++)
5201               {
5202                 enum elf_ppc64_reloc_type r_type;
5203                 unsigned long r_symndx;
5204                 struct elf_link_hash_entry *h;
5205                 Elf_Internal_Sym *sym;
5206                 asection *sym_sec;
5207                 char *tls_mask;
5208                 char tls_set, tls_clear, tls_type = 0;
5209                 bfd_vma value;
5210                 bfd_boolean ok_tprel, is_local;
5211
5212                 r_symndx = ELF64_R_SYM (rel->r_info);
5213                 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
5214                                 r_symndx, ibfd))
5215                   {
5216                   err_free_rel:
5217                     if (elf_section_data (sec)->relocs != relstart)
5218                       free (relstart);
5219                     if (locsyms != NULL
5220                         && (elf_tdata (ibfd)->symtab_hdr.contents
5221                             != (unsigned char *) locsyms))
5222                       free (locsyms);
5223                     return FALSE;
5224                   }
5225
5226                 if (h != NULL)
5227                   {
5228                     if (h->root.type != bfd_link_hash_defined
5229                         && h->root.type != bfd_link_hash_defweak)
5230                       continue;
5231                     value = h->root.u.def.value;
5232                   }
5233                 else
5234                   value = sym->st_value;
5235
5236                 ok_tprel = FALSE;
5237                 is_local = FALSE;
5238                 if (h == NULL
5239                     || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
5240                   {
5241                     is_local = TRUE;
5242                     value += sym_sec->output_offset;
5243                     value += sym_sec->output_section->vma;
5244                     value -= htab->tls_sec->vma;
5245                     ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
5246                                 < (bfd_vma) 1 << 32);
5247                   }
5248
5249                 r_type = ELF64_R_TYPE (rel->r_info);
5250                 switch (r_type)
5251                   {
5252                   case R_PPC64_GOT_TLSLD16:
5253                   case R_PPC64_GOT_TLSLD16_LO:
5254                   case R_PPC64_GOT_TLSLD16_HI:
5255                   case R_PPC64_GOT_TLSLD16_HA:
5256                     /* These relocs should never be against a symbol
5257                        defined in a shared lib.  Leave them alone if
5258                        that turns out to be the case.  */
5259                     ppc64_tlsld_got (ibfd)->refcount -= 1;
5260                     if (!is_local)
5261                       continue;
5262
5263                     /* LD -> LE */
5264                     tls_set = 0;
5265                     tls_clear = TLS_LD;
5266                     tls_type = TLS_TLS | TLS_LD;
5267                     expecting_tls_get_addr = 1;
5268                     break;
5269
5270                   case R_PPC64_GOT_TLSGD16:
5271                   case R_PPC64_GOT_TLSGD16_LO:
5272                   case R_PPC64_GOT_TLSGD16_HI:
5273                   case R_PPC64_GOT_TLSGD16_HA:
5274                     if (ok_tprel)
5275                       /* GD -> LE */
5276                       tls_set = 0;
5277                     else
5278                       /* GD -> IE */
5279                       tls_set = TLS_TLS | TLS_TPRELGD;
5280                     tls_clear = TLS_GD;
5281                     tls_type = TLS_TLS | TLS_GD;
5282                     expecting_tls_get_addr = 1;
5283                     break;
5284
5285                   case R_PPC64_GOT_TPREL16_DS:
5286                   case R_PPC64_GOT_TPREL16_LO_DS:
5287                   case R_PPC64_GOT_TPREL16_HI:
5288                   case R_PPC64_GOT_TPREL16_HA:
5289                     expecting_tls_get_addr = 0;
5290                     if (ok_tprel)
5291                       {
5292                         /* IE -> LE */
5293                         tls_set = 0;
5294                         tls_clear = TLS_TPREL;
5295                         tls_type = TLS_TLS | TLS_TPREL;
5296                         break;
5297                       }
5298                     else
5299                       continue;
5300
5301                   case R_PPC64_REL14:
5302                   case R_PPC64_REL14_BRTAKEN:
5303                   case R_PPC64_REL14_BRNTAKEN:
5304                   case R_PPC64_REL24:
5305                     if (h != NULL
5306                         && h == htab->tls_get_addr)
5307                       {
5308                         if (!expecting_tls_get_addr
5309                             && rel != relstart
5310                             && ((ELF64_R_TYPE (rel[-1].r_info)
5311                                  == R_PPC64_TOC16)
5312                                 || (ELF64_R_TYPE (rel[-1].r_info)
5313                                     == R_PPC64_TOC16_LO)))
5314                           {
5315                             /* Check for toc tls entries.  */
5316                             char *toc_tls;
5317                             int retval;
5318
5319                             retval = get_tls_mask (&toc_tls, NULL, &locsyms,
5320                                                    rel - 1, ibfd);
5321                             if (retval == 0)
5322                               goto err_free_rel;
5323                             if (toc_tls != NULL)
5324                               expecting_tls_get_addr = retval > 1;
5325                           }
5326
5327                         if (expecting_tls_get_addr)
5328                           {
5329                             struct plt_entry *ent;
5330                             for (ent = h->plt.plist; ent; ent = ent->next)
5331                               if (ent->addend == 0)
5332                                 {
5333                                   if (ent->plt.refcount > 0)
5334                                     ent->plt.refcount -= 1;
5335                                   break;
5336                                 }
5337                           }
5338                       }
5339                     expecting_tls_get_addr = 0;
5340                     continue;
5341
5342                   case R_PPC64_TPREL64:
5343                     expecting_tls_get_addr = 0;
5344                     if (ok_tprel)
5345                       {
5346                         /* IE -> LE */
5347                         tls_set = TLS_EXPLICIT;
5348                         tls_clear = TLS_TPREL;
5349                         break;
5350                       }
5351                     else
5352                       continue;
5353
5354                   case R_PPC64_DTPMOD64:
5355                     expecting_tls_get_addr = 0;
5356                     if (rel + 1 < relend
5357                         && (rel[1].r_info
5358                             == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
5359                         && rel[1].r_offset == rel->r_offset + 8)
5360                       {
5361                         if (ok_tprel)
5362                           /* GD -> LE */
5363                           tls_set = TLS_EXPLICIT | TLS_GD;
5364                         else
5365                           /* GD -> IE */
5366                           tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
5367                         tls_clear = TLS_GD;
5368                       }
5369                     else
5370                       {
5371                         if (!is_local)
5372                           continue;
5373
5374                         /* LD -> LE */
5375                         tls_set = TLS_EXPLICIT;
5376                         tls_clear = TLS_LD;
5377                       }
5378                     break;
5379
5380                   default:
5381                     expecting_tls_get_addr = 0;
5382                     continue;
5383                   }
5384
5385                 if ((tls_set & TLS_EXPLICIT) == 0)
5386                   {
5387                     struct got_entry *ent;
5388
5389                     /* Adjust got entry for this reloc.  */
5390                     if (h != NULL)
5391                       ent = h->got.glist;
5392                     else
5393                       ent = elf_local_got_ents (ibfd)[r_symndx];
5394
5395                     for (; ent != NULL; ent = ent->next)
5396                       if (ent->addend == rel->r_addend
5397                           && ent->owner == ibfd
5398                           && ent->tls_type == tls_type)
5399                         break;
5400                     if (ent == NULL)
5401                       abort ();
5402
5403                     if (tls_set == 0)
5404                       {
5405                         /* We managed to get rid of a got entry.  */
5406                         if (ent->got.refcount > 0)
5407                           ent->got.refcount -= 1;
5408                       }
5409                   }
5410                 else if (h != NULL)
5411                   {
5412                     struct ppc_link_hash_entry * eh;
5413                     struct ppc_dyn_relocs **pp;
5414                     struct ppc_dyn_relocs *p;
5415
5416                     /* Adjust dynamic relocs.  */
5417                     eh = (struct ppc_link_hash_entry *) h;
5418                     for (pp = &eh->dyn_relocs;
5419                          (p = *pp) != NULL;
5420                          pp = &p->next)
5421                       if (p->sec == sec)
5422                         {
5423                           /* If we got rid of a DTPMOD/DTPREL reloc
5424                              pair then we'll lose one or two dyn
5425                              relocs.  */
5426                           if (tls_set == (TLS_EXPLICIT | TLS_GD))
5427                             p->count -= 1;
5428                           p->count -= 1;
5429                           if (p->count == 0)
5430                             *pp = p->next;
5431                           break;
5432                         }
5433                   }
5434
5435                 *tls_mask |= tls_set;
5436                 *tls_mask &= ~tls_clear;
5437               }
5438
5439             if (elf_section_data (sec)->relocs != relstart)
5440               free (relstart);
5441           }
5442
5443       if (locsyms != NULL
5444           && (elf_tdata (ibfd)->symtab_hdr.contents
5445               != (unsigned char *) locsyms))
5446         {
5447           if (!info->keep_memory)
5448             free (locsyms);
5449           else
5450             elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
5451         }
5452     }
5453   return TRUE;
5454 }
5455
5456 /* This is the condition under which ppc64_elf_finish_dynamic_symbol
5457    will be called from elflink.h.  If elflink.h doesn't call our
5458    finish_dynamic_symbol routine, we'll need to do something about
5459    initializing any .plt and .got entries in ppc64_elf_relocate_section.  */
5460 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
5461   ((DYN)                                                                \
5462    && ((SHARED)                                                         \
5463        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)      \
5464    && ((H)->dynindx != -1                                               \
5465        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
5466
5467 /* Allocate space in .plt, .got and associated reloc sections for
5468    dynamic relocs.  */
5469
5470 static bfd_boolean
5471 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5472 {
5473   struct bfd_link_info *info;
5474   struct ppc_link_hash_table *htab;
5475   asection *s;
5476   struct ppc_link_hash_entry *eh;
5477   struct ppc_dyn_relocs *p;
5478   struct got_entry *gent;
5479
5480   if (h->root.type == bfd_link_hash_indirect)
5481     return TRUE;
5482
5483   if (h->root.type == bfd_link_hash_warning)
5484     h = (struct elf_link_hash_entry *) h->root.u.i.link;
5485
5486   info = (struct bfd_link_info *) inf;
5487   htab = ppc_hash_table (info);
5488
5489   if (htab->elf.dynamic_sections_created
5490       && h->dynindx != -1
5491       && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
5492     {
5493       struct plt_entry *pent;
5494       bfd_boolean doneone = FALSE;
5495       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
5496         if (pent->plt.refcount > 0)
5497           {
5498             BFD_ASSERT (((struct ppc_link_hash_entry *) h)->is_func_descriptor);
5499
5500             /* If this is the first .plt entry, make room for the special
5501                first entry.  */
5502             s = htab->plt;
5503             if (s->_raw_size == 0)
5504               s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
5505
5506             pent->plt.offset = s->_raw_size;
5507
5508             /* Make room for this entry.  */
5509             s->_raw_size += PLT_ENTRY_SIZE;
5510
5511             /* Make room for the .glink code.  */
5512             s = htab->glink;
5513             if (s->_raw_size == 0)
5514               s->_raw_size += GLINK_CALL_STUB_SIZE;
5515             /* We need bigger stubs past index 32767.  */
5516             if (s->_raw_size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
5517               s->_raw_size += 4;
5518             s->_raw_size += 2*4;
5519
5520             /* We also need to make an entry in the .rela.plt section.  */
5521             s = htab->relplt;
5522             s->_raw_size += sizeof (Elf64_External_Rela);
5523             doneone = TRUE;
5524           }
5525         else
5526           pent->plt.offset = (bfd_vma) -1;
5527       if (!doneone)
5528         {
5529           h->plt.plist = NULL;
5530           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
5531         }
5532     }
5533   else
5534     {
5535       h->plt.plist = NULL;
5536       h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
5537     }
5538
5539   eh = (struct ppc_link_hash_entry *) h;
5540   /* Run through the TLS GD got entries first if we're changing them
5541      to TPREL.  */
5542   if ((eh->tls_mask & TLS_TPRELGD) != 0)
5543     for (gent = h->got.glist; gent != NULL; gent = gent->next)
5544       if (gent->got.refcount > 0
5545           && (gent->tls_type & TLS_GD) != 0)
5546         {
5547           /* This was a GD entry that has been converted to TPREL.  If
5548              there happens to be a TPREL entry we can use that one.  */
5549           struct got_entry *ent;
5550           for (ent = h->got.glist; ent != NULL; ent = ent->next)
5551             if (ent->got.refcount > 0
5552                 && (ent->tls_type & TLS_TPREL) != 0
5553                 && ent->addend == gent->addend
5554                 && ent->owner == gent->owner)
5555               {
5556                 gent->got.refcount = 0;
5557                 break;
5558               }
5559
5560           /* If not, then we'll be using our own TPREL entry.  */
5561           if (gent->got.refcount != 0)
5562             gent->tls_type = TLS_TLS | TLS_TPREL;
5563         }
5564
5565   for (gent = h->got.glist; gent != NULL; gent = gent->next)
5566     if (gent->got.refcount > 0)
5567       {
5568         bfd_boolean dyn;
5569
5570         /* Make sure this symbol is output as a dynamic symbol.
5571            Undefined weak syms won't yet be marked as dynamic,
5572            nor will all TLS symbols.  */
5573         if (h->dynindx == -1
5574             && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5575           {
5576             if (! bfd_elf64_link_record_dynamic_symbol (info, h))
5577               return FALSE;
5578           }
5579
5580         if ((gent->tls_type & TLS_LD) != 0
5581             && !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
5582           {
5583             gent->got.offset = ppc64_tlsld_got (gent->owner)->offset;
5584             continue;
5585           }
5586
5587         s = ppc64_elf_tdata (gent->owner)->got;
5588         gent->got.offset = s->_raw_size;
5589         s->_raw_size
5590           += (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)) ? 16 : 8;
5591         dyn = htab->elf.dynamic_sections_created;
5592         if ((info->shared
5593              || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
5594             && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5595                 || h->root.type != bfd_link_hash_undefweak))
5596           ppc64_elf_tdata (gent->owner)->relgot->_raw_size
5597             += (gent->tls_type & eh->tls_mask & TLS_GD
5598                 ? 2 * sizeof (Elf64_External_Rela)
5599                 : sizeof (Elf64_External_Rela));
5600       }
5601     else
5602       gent->got.offset = (bfd_vma) -1;
5603
5604   if (eh->dyn_relocs == NULL)
5605     return TRUE;
5606
5607   /* In the shared -Bsymbolic case, discard space allocated for
5608      dynamic pc-relative relocs against symbols which turn out to be
5609      defined in regular objects.  For the normal shared case, discard
5610      space for relocs that have become local due to symbol visibility
5611      changes.  */
5612
5613   if (info->shared)
5614     {
5615       /* Relocs that use pc_count are those that appear on a call insn,
5616          or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
5617          generated via assembly.  We want calls to protected symbols to
5618          resolve directly to the function rather than going via the plt.
5619          If people want function pointer comparisons to work as expected
5620          then they should avoid writing weird assembly.  */
5621       if (SYMBOL_CALLS_LOCAL (info, h))
5622         {
5623           struct ppc_dyn_relocs **pp;
5624
5625           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5626             {
5627               p->count -= p->pc_count;
5628               p->pc_count = 0;
5629               if (p->count == 0)
5630                 *pp = p->next;
5631               else
5632                 pp = &p->next;
5633             }
5634         }
5635
5636       /* Also discard relocs on undefined weak syms with non-default
5637          visibility.  */
5638       if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5639           && h->root.type == bfd_link_hash_undefweak)
5640         eh->dyn_relocs = NULL;
5641     }
5642   else if (ELIMINATE_COPY_RELOCS)
5643     {
5644       /* For the non-shared case, discard space for relocs against
5645          symbols which turn out to need copy relocs or are not
5646          dynamic.  */
5647
5648       if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5649           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5650           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5651         {
5652           /* Make sure this symbol is output as a dynamic symbol.
5653              Undefined weak syms won't yet be marked as dynamic.  */
5654           if (h->dynindx == -1
5655               && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5656             {
5657               if (! bfd_elf64_link_record_dynamic_symbol (info, h))
5658                 return FALSE;
5659             }
5660
5661           /* If that succeeded, we know we'll be keeping all the
5662              relocs.  */
5663           if (h->dynindx != -1)
5664             goto keep;
5665         }
5666
5667       eh->dyn_relocs = NULL;
5668
5669     keep: ;
5670     }
5671
5672   /* Finally, allocate space.  */
5673   for (p = eh->dyn_relocs; p != NULL; p = p->next)
5674     {
5675       asection *sreloc = elf_section_data (p->sec)->sreloc;
5676       sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
5677     }
5678
5679   return TRUE;
5680 }
5681
5682 /* Find any dynamic relocs that apply to read-only sections.  */
5683
5684 static bfd_boolean
5685 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5686 {
5687   struct ppc_link_hash_entry *eh;
5688   struct ppc_dyn_relocs *p;
5689
5690   if (h->root.type == bfd_link_hash_warning)
5691     h = (struct elf_link_hash_entry *) h->root.u.i.link;
5692
5693   eh = (struct ppc_link_hash_entry *) h;
5694   for (p = eh->dyn_relocs; p != NULL; p = p->next)
5695     {
5696       asection *s = p->sec->output_section;
5697
5698       if (s != NULL && (s->flags & SEC_READONLY) != 0)
5699         {
5700           struct bfd_link_info *info = inf;
5701
5702           info->flags |= DF_TEXTREL;
5703
5704           /* Not an error, just cut short the traversal.  */
5705           return FALSE;
5706         }
5707     }
5708   return TRUE;
5709 }
5710
5711 /* Set the sizes of the dynamic sections.  */
5712
5713 static bfd_boolean
5714 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5715                                  struct bfd_link_info *info)
5716 {
5717   struct ppc_link_hash_table *htab;
5718   bfd *dynobj;
5719   asection *s;
5720   bfd_boolean relocs;
5721   bfd *ibfd;
5722
5723   htab = ppc_hash_table (info);
5724   dynobj = htab->elf.dynobj;
5725   if (dynobj == NULL)
5726     abort ();
5727
5728   if (htab->elf.dynamic_sections_created)
5729     {
5730       /* Set the contents of the .interp section to the interpreter.  */
5731       if (info->executable)
5732         {
5733           s = bfd_get_section_by_name (dynobj, ".interp");
5734           if (s == NULL)
5735             abort ();
5736           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
5737           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5738         }
5739     }
5740
5741   /* Set up .got offsets for local syms, and space for local dynamic
5742      relocs.  */
5743   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5744     {
5745       struct got_entry **lgot_ents;
5746       struct got_entry **end_lgot_ents;
5747       char *lgot_masks;
5748       bfd_size_type locsymcount;
5749       Elf_Internal_Shdr *symtab_hdr;
5750       asection *srel;
5751
5752       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
5753         continue;
5754
5755       if (ppc64_tlsld_got (ibfd)->refcount > 0)
5756         {
5757           s = ppc64_elf_tdata (ibfd)->got;
5758           ppc64_tlsld_got (ibfd)->offset = s->_raw_size;
5759           s->_raw_size += 16;
5760           if (info->shared)
5761             {
5762               srel = ppc64_elf_tdata (ibfd)->relgot;
5763               srel->_raw_size += sizeof (Elf64_External_Rela);
5764             }
5765         }
5766       else
5767         ppc64_tlsld_got (ibfd)->offset = (bfd_vma) -1;
5768
5769       for (s = ibfd->sections; s != NULL; s = s->next)
5770         {
5771           struct ppc_dyn_relocs *p;
5772
5773           for (p = *((struct ppc_dyn_relocs **)
5774                      &elf_section_data (s)->local_dynrel);
5775                p != NULL;
5776                p = p->next)
5777             {
5778               if (!bfd_is_abs_section (p->sec)
5779                   && bfd_is_abs_section (p->sec->output_section))
5780                 {
5781                   /* Input section has been discarded, either because
5782                      it is a copy of a linkonce section or due to
5783                      linker script /DISCARD/, so we'll be discarding
5784                      the relocs too.  */
5785                 }
5786               else if (p->count != 0)
5787                 {
5788                   srel = elf_section_data (p->sec)->sreloc;
5789                   srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
5790                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
5791                     info->flags |= DF_TEXTREL;
5792                 }
5793             }
5794         }
5795
5796       lgot_ents = elf_local_got_ents (ibfd);
5797       if (!lgot_ents)
5798         continue;
5799
5800       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5801       locsymcount = symtab_hdr->sh_info;
5802       end_lgot_ents = lgot_ents + locsymcount;
5803       lgot_masks = (char *) end_lgot_ents;
5804       s = ppc64_elf_tdata (ibfd)->got;
5805       srel = ppc64_elf_tdata (ibfd)->relgot;
5806       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
5807         {
5808           struct got_entry *ent;
5809
5810           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
5811             if (ent->got.refcount > 0)
5812               {
5813                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
5814                   {
5815                     if (ppc64_tlsld_got (ibfd)->offset == (bfd_vma) -1)
5816                       {
5817                         ppc64_tlsld_got (ibfd)->offset = s->_raw_size;
5818                         s->_raw_size += 16;
5819                         if (info->shared)
5820                           srel->_raw_size += sizeof (Elf64_External_Rela);
5821                       }
5822                     ent->got.offset = ppc64_tlsld_got (ibfd)->offset;
5823                   }
5824                 else
5825                   {
5826                     ent->got.offset = s->_raw_size;
5827                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
5828                       {
5829                         s->_raw_size += 16;
5830                         if (info->shared)
5831                           srel->_raw_size += 2 * sizeof (Elf64_External_Rela);
5832                       }
5833                     else
5834                       {
5835                         s->_raw_size += 8;
5836                         if (info->shared)
5837                           srel->_raw_size += sizeof (Elf64_External_Rela);
5838                       }
5839                   }
5840               }
5841             else
5842               ent->got.offset = (bfd_vma) -1;
5843         }
5844     }
5845
5846   /* Allocate global sym .plt and .got entries, and space for global
5847      sym dynamic relocs.  */
5848   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
5849
5850   /* We now have determined the sizes of the various dynamic sections.
5851      Allocate memory for them.  */
5852   relocs = FALSE;
5853   for (s = dynobj->sections; s != NULL; s = s->next)
5854     {
5855       if ((s->flags & SEC_LINKER_CREATED) == 0)
5856         continue;
5857
5858       /* Reset _cooked_size since prelim layout will set it wrongly,
5859          and a non-zero _cooked_size sticks.  */
5860       s->_cooked_size = 0;
5861
5862       if (s == htab->brlt || s == htab->relbrlt)
5863         /* These haven't been allocated yet;  don't strip.  */
5864         continue;
5865       else if (s == htab->got
5866                || s == htab->plt
5867                || s == htab->glink)
5868         {
5869           /* Strip this section if we don't need it; see the
5870              comment below.  */
5871         }
5872       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
5873         {
5874           if (s->_raw_size == 0)
5875             {
5876               /* If we don't need this section, strip it from the
5877                  output file.  This is mostly to handle .rela.bss and
5878                  .rela.plt.  We must create both sections in
5879                  create_dynamic_sections, because they must be created
5880                  before the linker maps input sections to output
5881                  sections.  The linker does that before
5882                  adjust_dynamic_symbol is called, and it is that
5883                  function which decides whether anything needs to go
5884                  into these sections.  */
5885             }
5886           else
5887             {
5888               if (s != htab->relplt)
5889                 relocs = TRUE;
5890
5891               /* We use the reloc_count field as a counter if we need
5892                  to copy relocs into the output file.  */
5893               s->reloc_count = 0;
5894             }
5895         }
5896       else
5897         {
5898           /* It's not one of our sections, so don't allocate space.  */
5899           continue;
5900         }
5901
5902       if (s->_raw_size == 0)
5903         {
5904           _bfd_strip_section_from_output (info, s);
5905           continue;
5906         }
5907
5908       /* .plt is in the bss section.  We don't initialise it.  */
5909       if ((s->flags & SEC_LOAD) == 0)
5910         continue;
5911
5912       /* Allocate memory for the section contents.  We use bfd_zalloc
5913          here in case unused entries are not reclaimed before the
5914          section's contents are written out.  This should not happen,
5915          but this way if it does we get a R_PPC64_NONE reloc in .rela
5916          sections instead of garbage.
5917          We also rely on the section contents being zero when writing
5918          the GOT.  */
5919       s->contents = bfd_zalloc (dynobj, s->_raw_size);
5920       if (s->contents == NULL)
5921         return FALSE;
5922     }
5923
5924   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5925     {
5926       s = ppc64_elf_tdata (ibfd)->got;
5927       if (s != NULL && s != htab->got)
5928         {
5929           s->_cooked_size = 0;
5930           if (s->_raw_size == 0)
5931             _bfd_strip_section_from_output (info, s);
5932           else
5933             {
5934               s->contents = bfd_zalloc (ibfd, s->_raw_size);
5935               if (s->contents == NULL)
5936                 return FALSE;
5937             }
5938         }
5939       s = ppc64_elf_tdata (ibfd)->relgot;
5940       if (s != NULL)
5941         {
5942           s->_cooked_size = 0;
5943           if (s->_raw_size == 0)
5944             _bfd_strip_section_from_output (info, s);
5945           else
5946             {
5947               s->contents = bfd_zalloc (ibfd, s->_raw_size);
5948               if (s->contents == NULL)
5949                 return FALSE;
5950               relocs = TRUE;
5951               s->reloc_count = 0;
5952             }
5953         }
5954     }
5955
5956   if (htab->elf.dynamic_sections_created)
5957     {
5958       /* Add some entries to the .dynamic section.  We fill in the
5959          values later, in ppc64_elf_finish_dynamic_sections, but we
5960          must add the entries now so that we get the correct size for
5961          the .dynamic section.  The DT_DEBUG entry is filled in by the
5962          dynamic linker and used by the debugger.  */
5963 #define add_dynamic_entry(TAG, VAL) \
5964   bfd_elf64_add_dynamic_entry (info, (TAG), (VAL))
5965
5966       if (info->executable)
5967         {
5968           if (!add_dynamic_entry (DT_DEBUG, 0))
5969             return FALSE;
5970         }
5971
5972       if (htab->plt != NULL && htab->plt->_raw_size != 0)
5973         {
5974           if (!add_dynamic_entry (DT_PLTGOT, 0)
5975               || !add_dynamic_entry (DT_PLTRELSZ, 0)
5976               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5977               || !add_dynamic_entry (DT_JMPREL, 0)
5978               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
5979             return FALSE;
5980         }
5981
5982       if (NO_OPD_RELOCS)
5983         {
5984           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
5985               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
5986             return FALSE;
5987         }
5988
5989       if (relocs)
5990         {
5991           if (!add_dynamic_entry (DT_RELA, 0)
5992               || !add_dynamic_entry (DT_RELASZ, 0)
5993               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
5994             return FALSE;
5995
5996           /* If any dynamic relocs apply to a read-only section,
5997              then we need a DT_TEXTREL entry.  */
5998           if ((info->flags & DF_TEXTREL) == 0)
5999             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
6000
6001           if ((info->flags & DF_TEXTREL) != 0)
6002             {
6003               if (!add_dynamic_entry (DT_TEXTREL, 0))
6004                 return FALSE;
6005             }
6006         }
6007     }
6008 #undef add_dynamic_entry
6009
6010   return TRUE;
6011 }
6012
6013 /* Determine the type of stub needed, if any, for a call.  */
6014
6015 static inline enum ppc_stub_type
6016 ppc_type_of_stub (asection *input_sec,
6017                   const Elf_Internal_Rela *rel,
6018                   struct ppc_link_hash_entry **hash,
6019                   bfd_vma destination)
6020 {
6021   struct ppc_link_hash_entry *h = *hash;
6022   bfd_vma location;
6023   bfd_vma branch_offset;
6024   bfd_vma max_branch_offset;
6025   enum elf_ppc64_reloc_type r_type;
6026
6027   if (h != NULL)
6028     {
6029       if (h->oh != NULL
6030           && h->oh->dynindx != -1)
6031         {
6032           struct plt_entry *ent;
6033           for (ent = h->oh->plt.plist; ent != NULL; ent = ent->next)
6034             if (ent->addend == rel->r_addend
6035                 && ent->plt.offset != (bfd_vma) -1)
6036               {
6037                 *hash = (struct ppc_link_hash_entry *) h->oh;
6038                 return ppc_stub_plt_call;
6039               }
6040         }
6041
6042       if (h->elf.root.type != bfd_link_hash_defined
6043           && h->elf.root.type != bfd_link_hash_defweak)
6044         return ppc_stub_none;
6045     }
6046
6047   /* Determine where the call point is.  */
6048   location = (input_sec->output_offset
6049               + input_sec->output_section->vma
6050               + rel->r_offset);
6051
6052   branch_offset = destination - location;
6053   r_type = ELF64_R_TYPE (rel->r_info);
6054
6055   /* Determine if a long branch stub is needed.  */
6056   max_branch_offset = 1 << 25;
6057   if (r_type != R_PPC64_REL24)
6058     max_branch_offset = 1 << 15;
6059
6060   if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
6061     /* We need a stub.  Figure out whether a long_branch or plt_branch
6062        is needed later.  */
6063     return ppc_stub_long_branch;
6064
6065   return ppc_stub_none;
6066 }
6067
6068 /* Build a .plt call stub.  */
6069
6070 static inline bfd_byte *
6071 build_plt_stub (bfd *obfd, bfd_byte *p, int offset)
6072 {
6073 #define PPC_LO(v) ((v) & 0xffff)
6074 #define PPC_HI(v) (((v) >> 16) & 0xffff)
6075 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
6076
6077   bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
6078   bfd_put_32 (obfd, STD_R2_40R1, p),                    p += 4;
6079   bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),  p += 4;
6080   if (PPC_HA (offset + 8) != PPC_HA (offset))
6081     bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p),            p += 4;
6082   offset += 8;
6083   bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p),   p += 4;
6084   if (PPC_HA (offset + 8) != PPC_HA (offset))
6085     bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p),            p += 4;
6086   offset += 8;
6087   bfd_put_32 (obfd, MTCTR_R11, p),                      p += 4;
6088   bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),  p += 4;
6089   bfd_put_32 (obfd, BCTR, p),                           p += 4;
6090   return p;
6091 }
6092
6093 static bfd_boolean
6094 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
6095 {
6096   struct ppc_stub_hash_entry *stub_entry;
6097   struct ppc_branch_hash_entry *br_entry;
6098   struct bfd_link_info *info;
6099   struct ppc_link_hash_table *htab;
6100   bfd_byte *loc;
6101   bfd_byte *p;
6102   unsigned int indx;
6103   struct plt_entry *ent;
6104   bfd_vma off;
6105   int size;
6106
6107   /* Massage our args to the form they really have.  */
6108   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
6109   info = in_arg;
6110
6111   htab = ppc_hash_table (info);
6112
6113   /* Make a note of the offset within the stubs for this entry.  */
6114   stub_entry->stub_offset = stub_entry->stub_sec->_cooked_size;
6115   loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
6116
6117   htab->stub_count[stub_entry->stub_type - 1] += 1;
6118   switch (stub_entry->stub_type)
6119     {
6120     case ppc_stub_long_branch:
6121     case ppc_stub_long_branch_r2off:
6122       /* Branches are relative.  This is where we are going to.  */
6123       off = (stub_entry->target_value
6124              + stub_entry->target_section->output_offset
6125              + stub_entry->target_section->output_section->vma);
6126
6127       /* And this is where we are coming from.  */
6128       off -= (stub_entry->stub_offset
6129               + stub_entry->stub_sec->output_offset
6130               + stub_entry->stub_sec->output_section->vma);
6131
6132       if (stub_entry->stub_type != ppc_stub_long_branch_r2off)
6133         size = 4;
6134       else
6135         {
6136           bfd_vma r2off;
6137
6138           r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
6139                    - htab->stub_group[stub_entry->id_sec->id].toc_off);
6140           bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
6141           loc += 4;
6142           bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
6143           loc += 4;
6144           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
6145           loc += 4;
6146           off -= 12;
6147           size = 16;
6148         }
6149       bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
6150
6151       BFD_ASSERT (off + (1 << 25) < (bfd_vma) (1 << 26));
6152       break;
6153
6154     case ppc_stub_plt_branch:
6155     case ppc_stub_plt_branch_r2off:
6156       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
6157                                          stub_entry->root.string + 9,
6158                                          FALSE, FALSE);
6159       if (br_entry == NULL)
6160         {
6161           (*_bfd_error_handler) (_("can't find branch stub `%s'"),
6162                                  stub_entry->root.string + 9);
6163           htab->stub_error = TRUE;
6164           return FALSE;
6165         }
6166
6167       off = (stub_entry->target_value
6168              + stub_entry->target_section->output_offset
6169              + stub_entry->target_section->output_section->vma);
6170
6171       bfd_put_64 (htab->brlt->owner, off,
6172                   htab->brlt->contents + br_entry->offset);
6173
6174       if (info->shared)
6175         {
6176           /* Create a reloc for the branch lookup table entry.  */
6177           Elf_Internal_Rela rela;
6178           bfd_byte *rl;
6179
6180           rela.r_offset = (br_entry->offset
6181                            + htab->brlt->output_offset
6182                            + htab->brlt->output_section->vma);
6183           rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
6184           rela.r_addend = off;
6185
6186           rl = htab->relbrlt->contents;
6187           rl += htab->relbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
6188           bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
6189         }
6190
6191       off = (br_entry->offset
6192              + htab->brlt->output_offset
6193              + htab->brlt->output_section->vma
6194              - elf_gp (htab->brlt->output_section->owner)
6195              - htab->stub_group[stub_entry->id_sec->id].toc_off);
6196
6197       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6198         {
6199           (*_bfd_error_handler)
6200             (_("linkage table error against `%s'"),
6201              stub_entry->root.string);
6202           bfd_set_error (bfd_error_bad_value);
6203           htab->stub_error = TRUE;
6204           return FALSE;
6205         }
6206
6207       indx = off;
6208       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
6209         {
6210           bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
6211           loc += 4;
6212           bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
6213           size = 16;
6214         }
6215       else
6216         {
6217           bfd_vma r2off;
6218
6219           r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
6220                    - htab->stub_group[stub_entry->id_sec->id].toc_off);
6221           bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
6222           loc += 4;
6223           bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
6224           loc += 4;
6225           bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
6226           loc += 4;
6227           bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
6228           loc += 4;
6229           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
6230           size = 28;
6231         }
6232       loc += 4;
6233       bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
6234       loc += 4;
6235       bfd_put_32 (htab->stub_bfd, BCTR, loc);
6236       break;
6237
6238     case ppc_stub_plt_call:
6239       /* Do the best we can for shared libraries built without
6240          exporting ".foo" for each "foo".  This can happen when symbol
6241          versioning scripts strip all bar a subset of symbols.  */
6242       if (stub_entry->h->oh->root.type != bfd_link_hash_defined
6243           && stub_entry->h->oh->root.type != bfd_link_hash_defweak)
6244         {
6245           /* Point the symbol at the stub.  There may be multiple stubs,
6246              we don't really care;  The main thing is to make this sym
6247              defined somewhere.  Maybe defining the symbol in the stub
6248              section is a silly idea.  If we didn't do this, htab->top_id
6249              could disappear.  */
6250           stub_entry->h->oh->root.type = bfd_link_hash_defined;
6251           stub_entry->h->oh->root.u.def.section = stub_entry->stub_sec;
6252           stub_entry->h->oh->root.u.def.value = stub_entry->stub_offset;
6253         }
6254
6255       /* Now build the stub.  */
6256       off = (bfd_vma) -1;
6257       for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
6258         if (ent->addend == stub_entry->addend)
6259           {
6260             off = ent->plt.offset;
6261             break;
6262           }
6263       if (off >= (bfd_vma) -2)
6264         abort ();
6265
6266       off &= ~ (bfd_vma) 1;
6267       off += (htab->plt->output_offset
6268               + htab->plt->output_section->vma
6269               - elf_gp (htab->plt->output_section->owner)
6270               - htab->stub_group[stub_entry->id_sec->id].toc_off);
6271
6272       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6273         {
6274           (*_bfd_error_handler)
6275             (_("linkage table error against `%s'"),
6276              stub_entry->h->elf.root.root.string);
6277           bfd_set_error (bfd_error_bad_value);
6278           htab->stub_error = TRUE;
6279           return FALSE;
6280         }
6281
6282       p = build_plt_stub (htab->stub_bfd, loc, off);
6283       size = p - loc;
6284       break;
6285
6286     default:
6287       BFD_FAIL ();
6288       return FALSE;
6289     }
6290
6291   stub_entry->stub_sec->_cooked_size += size;
6292
6293   if (htab->emit_stub_syms
6294       && !(stub_entry->stub_type == ppc_stub_plt_call
6295            && stub_entry->h->oh->root.type == bfd_link_hash_defined
6296            && stub_entry->h->oh->root.u.def.section == stub_entry->stub_sec
6297            && stub_entry->h->oh->root.u.def.value == stub_entry->stub_offset))
6298     {
6299       struct elf_link_hash_entry *h;
6300       h = elf_link_hash_lookup (&htab->elf, stub_entry->root.string,
6301                                 TRUE, FALSE, FALSE);
6302       if (h == NULL)
6303         return FALSE;
6304       if (h->root.type == bfd_link_hash_new)
6305         {
6306           h->root.type = bfd_link_hash_defined;
6307           h->root.u.def.section = stub_entry->stub_sec;
6308           h->root.u.def.value = stub_entry->stub_offset;
6309           h->elf_link_hash_flags = (ELF_LINK_HASH_REF_REGULAR
6310                                     | ELF_LINK_HASH_DEF_REGULAR
6311                                     | ELF_LINK_HASH_REF_REGULAR_NONWEAK
6312                                     | ELF_LINK_FORCED_LOCAL);
6313         }
6314     }
6315
6316   return TRUE;
6317 }
6318
6319 /* As above, but don't actually build the stub.  Just bump offset so
6320    we know stub section sizes, and select plt_branch stubs where
6321    long_branch stubs won't do.  */
6322
6323 static bfd_boolean
6324 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
6325 {
6326   struct ppc_stub_hash_entry *stub_entry;
6327   struct ppc_link_hash_table *htab;
6328   bfd_vma off;
6329   int size;
6330
6331   /* Massage our args to the form they really have.  */
6332   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
6333   htab = in_arg;
6334
6335   if (stub_entry->stub_type == ppc_stub_plt_call)
6336     {
6337       struct plt_entry *ent;
6338       off = (bfd_vma) -1;
6339       for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
6340         if (ent->addend == stub_entry->addend)
6341           {
6342             off = ent->plt.offset & ~(bfd_vma) 1;
6343             break;
6344           }
6345       if (off >= (bfd_vma) -2)
6346         abort ();
6347       off += (htab->plt->output_offset
6348               + htab->plt->output_section->vma
6349               - elf_gp (htab->plt->output_section->owner)
6350               - htab->stub_group[stub_entry->id_sec->id].toc_off);
6351
6352       size = PLT_CALL_STUB_SIZE;
6353       if (PPC_HA (off + 16) != PPC_HA (off))
6354         size += 4;
6355     }
6356   else
6357     {
6358       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
6359          variants.  */
6360       off = (stub_entry->target_value
6361              + stub_entry->target_section->output_offset
6362              + stub_entry->target_section->output_section->vma);
6363       off -= (stub_entry->stub_sec->_raw_size
6364               + stub_entry->stub_sec->output_offset
6365               + stub_entry->stub_sec->output_section->vma);
6366
6367       /* Reset the stub type from the plt variant in case we now
6368          can reach with a shorter stub.  */
6369       if (stub_entry->stub_type >= ppc_stub_plt_branch)
6370         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
6371
6372       size = 4;
6373       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
6374         {
6375           off -= 12;
6376           size = 16;
6377         }
6378
6379       /* If the branch offset if too big, use a ppc_stub_plt_branch.  */
6380       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
6381         {
6382           struct ppc_branch_hash_entry *br_entry;
6383
6384           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
6385                                              stub_entry->root.string + 9,
6386                                              TRUE, FALSE);
6387           if (br_entry == NULL)
6388             {
6389               (*_bfd_error_handler) (_("can't build branch stub `%s'"),
6390                                      stub_entry->root.string + 9);
6391               htab->stub_error = TRUE;
6392               return FALSE;
6393             }
6394
6395           if (br_entry->iter != htab->stub_iteration)
6396             {
6397               br_entry->iter = htab->stub_iteration;
6398               br_entry->offset = htab->brlt->_raw_size;
6399               htab->brlt->_raw_size += 8;
6400             }
6401
6402           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
6403           size = 16;
6404           if (stub_entry->stub_type != ppc_stub_plt_branch)
6405             size = 28;
6406         }
6407     }
6408
6409   stub_entry->stub_sec->_raw_size += size;
6410   return TRUE;
6411 }
6412
6413 /* Set up various things so that we can make a list of input sections
6414    for each output section included in the link.  Returns -1 on error,
6415    0 when no stubs will be needed, and 1 on success.  */
6416
6417 int
6418 ppc64_elf_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
6419 {
6420   bfd *input_bfd;
6421   int top_id, top_index, id;
6422   asection *section;
6423   asection **input_list;
6424   bfd_size_type amt;
6425   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6426
6427   if (htab->brlt == NULL)
6428     return 0;
6429
6430   /* Find the top input section id.  */
6431   for (input_bfd = info->input_bfds, top_id = 3;
6432        input_bfd != NULL;
6433        input_bfd = input_bfd->link_next)
6434     {
6435       for (section = input_bfd->sections;
6436            section != NULL;
6437            section = section->next)
6438         {
6439           if (top_id < section->id)
6440             top_id = section->id;
6441         }
6442     }
6443
6444   htab->top_id = top_id;
6445   amt = sizeof (struct map_stub) * (top_id + 1);
6446   htab->stub_group = bfd_zmalloc (amt);
6447   if (htab->stub_group == NULL)
6448     return -1;
6449
6450   /* Set toc_off for com, und, abs and ind sections.  */
6451   for (id = 0; id < 3; id++)
6452     htab->stub_group[id].toc_off = TOC_BASE_OFF;
6453
6454   elf_gp (output_bfd) = htab->toc_curr = ppc64_elf_toc (output_bfd);
6455
6456   /* We can't use output_bfd->section_count here to find the top output
6457      section index as some sections may have been removed, and
6458      _bfd_strip_section_from_output doesn't renumber the indices.  */
6459   for (section = output_bfd->sections, top_index = 0;
6460        section != NULL;
6461        section = section->next)
6462     {
6463       if (top_index < section->index)
6464         top_index = section->index;
6465     }
6466
6467   htab->top_index = top_index;
6468   amt = sizeof (asection *) * (top_index + 1);
6469   input_list = bfd_zmalloc (amt);
6470   htab->input_list = input_list;
6471   if (input_list == NULL)
6472     return -1;
6473
6474   return 1;
6475 }
6476
6477 /* The linker repeatedly calls this function for each TOC input section
6478    and linker generated GOT section.  Group input bfds such that the toc
6479    within a group is less than 64k in size.  Will break with cute linker
6480    scripts that play games with dot in the output toc section.  */
6481
6482 void
6483 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
6484 {
6485   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6486
6487   if (!htab->no_multi_toc)
6488     {
6489       bfd_vma addr = isec->output_offset + isec->output_section->vma;
6490       bfd_vma off = addr - htab->toc_curr;
6491       if (off + isec->_raw_size > 0x10000)
6492         {
6493           htab->toc_curr = addr;
6494           htab->multi_toc_needed = 1;
6495         }
6496       elf_gp (isec->owner) = (htab->toc_curr
6497                               - elf_gp (isec->output_section->owner)
6498                               + TOC_BASE_OFF);
6499     }
6500 }
6501
6502 /* Called after the last call to the above function.  */
6503
6504 void
6505 ppc64_elf_reinit_toc (bfd *output_bfd ATTRIBUTE_UNUSED,
6506                       struct bfd_link_info *info)
6507 {
6508   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6509
6510   /* toc_curr tracks the TOC offset used for code sections below in
6511      ppc64_elf_next_input_section.  Start off at 0x8000.  */
6512   htab->toc_curr = TOC_BASE_OFF;
6513 }
6514
6515 /* No toc references were found in ISEC.  If the code in ISEC makes no
6516    calls, then there's no need to use toc adjusting stubs when branching
6517    into ISEC.  Actually, indirect calls from ISEC are OK as they will
6518    load r2.  */
6519
6520 static int
6521 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
6522 {
6523   bfd_byte *contents;
6524   bfd_size_type i;
6525   int ret;
6526   int branch_ok;
6527
6528   /* We know none of our code bearing sections will need toc stubs.  */
6529   if ((isec->flags & SEC_LINKER_CREATED) != 0)
6530     return 0;
6531
6532   /* Hack for linux kernel.  .fixup contains branches, but only back to
6533      the function that hit an exception.  */
6534   branch_ok = strcmp (isec->name, ".fixup") == 0;
6535
6536   contents = elf_section_data (isec)->this_hdr.contents;
6537   if (contents == NULL)
6538     {
6539       contents = bfd_malloc (isec->_raw_size);
6540       if (contents == NULL)
6541         return -1;
6542       if (! bfd_get_section_contents (isec->owner, isec, contents,
6543                                       0, isec->_raw_size))
6544         {
6545           free (contents);
6546           return -1;
6547         }
6548       if (info->keep_memory)
6549         elf_section_data (isec)->this_hdr.contents = contents;
6550     }
6551
6552   /* Code scan, because we don't necessarily have relocs on calls to
6553      static functions.  */
6554   ret = 0;
6555   for (i = 0; i < isec->_raw_size; i += 4)
6556     {
6557       unsigned long insn = bfd_get_32 (isec->owner, contents + i);
6558       /* Is this a branch?  */
6559       if ((insn & (0x3f << 26)) == (18 << 26)
6560           /* If branch and link, it's a function call.  */
6561           && ((insn & 1) != 0
6562               /* Sibling calls use a plain branch.  I don't know a way
6563                  of deciding whether a branch is really a sibling call.  */
6564               || !branch_ok))
6565         {
6566           ret = 1;
6567           break;
6568         }
6569     }
6570
6571   if (elf_section_data (isec)->this_hdr.contents != contents)
6572     free (contents);
6573   return ret;
6574 }
6575
6576 /* The linker repeatedly calls this function for each input section,
6577    in the order that input sections are linked into output sections.
6578    Build lists of input sections to determine groupings between which
6579    we may insert linker stubs.  */
6580
6581 bfd_boolean
6582 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
6583 {
6584   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6585   int ret;
6586
6587   if ((isec->output_section->flags & SEC_CODE) != 0
6588       && isec->output_section->index <= htab->top_index)
6589     {
6590       asection **list = htab->input_list + isec->output_section->index;
6591       /* Steal the link_sec pointer for our list.  */
6592 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
6593       /* This happens to make the list in reverse order,
6594          which is what we want.  */
6595       PREV_SEC (isec) = *list;
6596       *list = isec;
6597     }
6598
6599   /* If a code section has a function that uses the TOC then we need
6600      to use the right TOC (obviously).  Also, make sure that .opd gets
6601      the correct TOC value for R_PPC64_TOC relocs that don't have or
6602      can't find their function symbol (shouldn't ever happen now).  */
6603   if (isec->has_gp_reloc || (isec->flags & SEC_CODE) == 0)
6604     {
6605       if (elf_gp (isec->owner) != 0)
6606         htab->toc_curr = elf_gp (isec->owner);
6607     }
6608   else if ((ret = toc_adjusting_stub_needed (info, isec)) < 0)
6609     return FALSE;
6610   else
6611     isec->has_gp_reloc = ret;
6612
6613   /* Functions that don't use the TOC can belong in any TOC group.
6614      Use the last TOC base.  This happens to make _init and _fini
6615      pasting work.  */
6616   htab->stub_group[isec->id].toc_off = htab->toc_curr;
6617   return TRUE;
6618 }
6619
6620 /* See whether we can group stub sections together.  Grouping stub
6621    sections may result in fewer stubs.  More importantly, we need to
6622    put all .init* and .fini* stubs at the beginning of the .init or
6623    .fini output sections respectively, because glibc splits the
6624    _init and _fini functions into multiple parts.  Putting a stub in
6625    the middle of a function is not a good idea.  */
6626
6627 static void
6628 group_sections (struct ppc_link_hash_table *htab,
6629                 bfd_size_type stub_group_size,
6630                 bfd_boolean stubs_always_before_branch)
6631 {
6632   asection **list = htab->input_list + htab->top_index;
6633   do
6634     {
6635       asection *tail = *list;
6636       while (tail != NULL)
6637         {
6638           asection *curr;
6639           asection *prev;
6640           bfd_size_type total;
6641           bfd_boolean big_sec;
6642           bfd_vma curr_toc;
6643
6644           curr = tail;
6645           if (tail->_cooked_size)
6646             total = tail->_cooked_size;
6647           else
6648             total = tail->_raw_size;
6649           big_sec = total >= stub_group_size;
6650           curr_toc = htab->stub_group[tail->id].toc_off;
6651
6652           while ((prev = PREV_SEC (curr)) != NULL
6653                  && ((total += curr->output_offset - prev->output_offset)
6654                      < stub_group_size)
6655                  && htab->stub_group[prev->id].toc_off == curr_toc)
6656             curr = prev;
6657
6658           /* OK, the size from the start of CURR to the end is less
6659              than stub_group_size and thus can be handled by one stub
6660              section.  (or the tail section is itself larger than
6661              stub_group_size, in which case we may be toast.)  We
6662              should really be keeping track of the total size of stubs
6663              added here, as stubs contribute to the final output
6664              section size.  That's a little tricky, and this way will
6665              only break if stubs added make the total size more than
6666              2^25, ie. for the default stub_group_size, if stubs total
6667              more than 2097152 bytes, or nearly 75000 plt call stubs.  */
6668           do
6669             {
6670               prev = PREV_SEC (tail);
6671               /* Set up this stub group.  */
6672               htab->stub_group[tail->id].link_sec = curr;
6673             }
6674           while (tail != curr && (tail = prev) != NULL);
6675
6676           /* But wait, there's more!  Input sections up to stub_group_size
6677              bytes before the stub section can be handled by it too.
6678              Don't do this if we have a really large section after the
6679              stubs, as adding more stubs increases the chance that
6680              branches may not reach into the stub section.  */
6681           if (!stubs_always_before_branch && !big_sec)
6682             {
6683               total = 0;
6684               while (prev != NULL
6685                      && ((total += tail->output_offset - prev->output_offset)
6686                          < stub_group_size)
6687                      && htab->stub_group[prev->id].toc_off == curr_toc)
6688                 {
6689                   tail = prev;
6690                   prev = PREV_SEC (tail);
6691                   htab->stub_group[tail->id].link_sec = curr;
6692                 }
6693             }
6694           tail = prev;
6695         }
6696     }
6697   while (list-- != htab->input_list);
6698   free (htab->input_list);
6699 #undef PREV_SEC
6700 }
6701
6702 /* Determine and set the size of the stub section for a final link.
6703
6704    The basic idea here is to examine all the relocations looking for
6705    PC-relative calls to a target that is unreachable with a "bl"
6706    instruction.  */
6707
6708 bfd_boolean
6709 ppc64_elf_size_stubs (bfd *output_bfd,
6710                       struct bfd_link_info *info,
6711                       bfd_signed_vma group_size,
6712                       asection *(*add_stub_section) (const char *, asection *),
6713                       void (*layout_sections_again) (void))
6714 {
6715   bfd_size_type stub_group_size;
6716   bfd_boolean stubs_always_before_branch;
6717   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6718
6719   /* Stash our params away.  */
6720   htab->add_stub_section = add_stub_section;
6721   htab->layout_sections_again = layout_sections_again;
6722   stubs_always_before_branch = group_size < 0;
6723   if (group_size < 0)
6724     stub_group_size = -group_size;
6725   else
6726     stub_group_size = group_size;
6727   if (stub_group_size == 1)
6728     {
6729       /* Default values.  */
6730       if (stubs_always_before_branch)
6731         {
6732           stub_group_size = 0x1e00000;
6733           if (htab->has_14bit_branch)
6734             stub_group_size = 0x7800;
6735         }
6736       else
6737         {
6738           stub_group_size = 0x1c00000;
6739           if (htab->has_14bit_branch)
6740             stub_group_size = 0x7000;
6741         }
6742     }
6743
6744   group_sections (htab, stub_group_size, stubs_always_before_branch);
6745
6746   while (1)
6747     {
6748       bfd *input_bfd;
6749       unsigned int bfd_indx;
6750       asection *stub_sec;
6751       bfd_boolean stub_changed;
6752
6753       htab->stub_iteration += 1;
6754       stub_changed = FALSE;
6755
6756       for (input_bfd = info->input_bfds, bfd_indx = 0;
6757            input_bfd != NULL;
6758            input_bfd = input_bfd->link_next, bfd_indx++)
6759         {
6760           Elf_Internal_Shdr *symtab_hdr;
6761           asection *section;
6762           Elf_Internal_Sym *local_syms = NULL;
6763
6764           /* We'll need the symbol table in a second.  */
6765           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6766           if (symtab_hdr->sh_info == 0)
6767             continue;
6768
6769           /* Walk over each section attached to the input bfd.  */
6770           for (section = input_bfd->sections;
6771                section != NULL;
6772                section = section->next)
6773             {
6774               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
6775
6776               /* If there aren't any relocs, then there's nothing more
6777                  to do.  */
6778               if ((section->flags & SEC_RELOC) == 0
6779                   || section->reloc_count == 0)
6780                 continue;
6781
6782               /* If this section is a link-once section that will be
6783                  discarded, then don't create any stubs.  */
6784               if (section->output_section == NULL
6785                   || section->output_section->owner != output_bfd)
6786                 continue;
6787
6788               /* Get the relocs.  */
6789               internal_relocs
6790                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
6791                                              info->keep_memory);
6792               if (internal_relocs == NULL)
6793                 goto error_ret_free_local;
6794
6795               /* Now examine each relocation.  */
6796               irela = internal_relocs;
6797               irelaend = irela + section->reloc_count;
6798               for (; irela < irelaend; irela++)
6799                 {
6800                   enum elf_ppc64_reloc_type r_type;
6801                   unsigned int r_indx;
6802                   enum ppc_stub_type stub_type;
6803                   struct ppc_stub_hash_entry *stub_entry;
6804                   asection *sym_sec;
6805                   bfd_vma sym_value;
6806                   bfd_vma destination;
6807                   struct ppc_link_hash_entry *hash;
6808                   struct elf_link_hash_entry *h;
6809                   Elf_Internal_Sym *sym;
6810                   char *stub_name;
6811                   const asection *id_sec;
6812
6813                   r_type = ELF64_R_TYPE (irela->r_info);
6814                   r_indx = ELF64_R_SYM (irela->r_info);
6815
6816                   if (r_type >= R_PPC64_max)
6817                     {
6818                       bfd_set_error (bfd_error_bad_value);
6819                       goto error_ret_free_internal;
6820                     }
6821
6822                   /* Only look for stubs on branch instructions.  */
6823                   if (r_type != R_PPC64_REL24
6824                       && r_type != R_PPC64_REL14
6825                       && r_type != R_PPC64_REL14_BRTAKEN
6826                       && r_type != R_PPC64_REL14_BRNTAKEN)
6827                     continue;
6828
6829                   /* Now determine the call target, its name, value,
6830                      section.  */
6831                   destination = 0;
6832                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6833                                   r_indx, input_bfd))
6834                     goto error_ret_free_internal;
6835                   hash = (struct ppc_link_hash_entry *) h;
6836
6837                   if (hash == NULL)
6838                     {
6839                       /* It's a local symbol.  */
6840                       sym_value = sym->st_value;
6841                       destination = (sym_value + irela->r_addend
6842                                      + sym_sec->output_offset
6843                                      + sym_sec->output_section->vma);
6844                     }
6845                   else
6846                     {
6847                       /* It's an external symbol.  */
6848                       sym_value = 0;
6849                       if (hash->elf.root.type == bfd_link_hash_defined
6850                           || hash->elf.root.type == bfd_link_hash_defweak)
6851                         {
6852                           sym_value = hash->elf.root.u.def.value;
6853                           if (sym_sec->output_section != NULL)
6854                             destination = (sym_value + irela->r_addend
6855                                            + sym_sec->output_offset
6856                                            + sym_sec->output_section->vma);
6857                         }
6858                       else if (hash->elf.root.type == bfd_link_hash_undefweak)
6859                         ;
6860                       else if (hash->elf.root.type == bfd_link_hash_undefined)
6861                         ;
6862                       else
6863                         {
6864                           bfd_set_error (bfd_error_bad_value);
6865                           goto error_ret_free_internal;
6866                         }
6867                     }
6868
6869                   /* Determine what (if any) linker stub is needed.  */
6870                   stub_type = ppc_type_of_stub (section, irela, &hash,
6871                                                 destination);
6872
6873                   if (stub_type != ppc_stub_plt_call)
6874                     {
6875                       /* Check whether we need a TOC adjusting stub.
6876                          Since the linker pastes together pieces from
6877                          different object files when creating the
6878                          _init and _fini functions, it may be that a
6879                          call to what looks like a local sym is in
6880                          fact a call needing a TOC adjustment.  */
6881                       if (sym_sec != NULL
6882                           && sym_sec->output_section != NULL
6883                           && (htab->stub_group[sym_sec->id].toc_off
6884                               != htab->stub_group[section->id].toc_off)
6885                           && sym_sec->has_gp_reloc
6886                           && section->has_gp_reloc)
6887                         stub_type = ppc_stub_long_branch_r2off;
6888                     }
6889
6890                   if (stub_type == ppc_stub_none)
6891                     continue;
6892
6893                   /* __tls_get_addr calls might be eliminated.  */
6894                   if (stub_type != ppc_stub_plt_call
6895                       && hash != NULL
6896                       && &hash->elf == htab->tls_get_addr
6897                       && section->has_tls_reloc
6898                       && irela != internal_relocs)
6899                     {
6900                       /* Get tls info.  */
6901                       char *tls_mask;
6902
6903                       if (!get_tls_mask (&tls_mask, NULL, &local_syms,
6904                                          irela - 1, input_bfd))
6905                         goto error_ret_free_internal;
6906                       if (*tls_mask != 0)
6907                         continue;
6908                     }
6909
6910                   /* Support for grouping stub sections.  */
6911                   id_sec = htab->stub_group[section->id].link_sec;
6912
6913                   /* Get the name of this stub.  */
6914                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
6915                   if (!stub_name)
6916                     goto error_ret_free_internal;
6917
6918                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
6919                                                      stub_name, FALSE, FALSE);
6920                   if (stub_entry != NULL)
6921                     {
6922                       /* The proper stub has already been created.  */
6923                       free (stub_name);
6924                       continue;
6925                     }
6926
6927                   stub_entry = ppc_add_stub (stub_name, section, htab);
6928                   if (stub_entry == NULL)
6929                     {
6930                       free (stub_name);
6931                     error_ret_free_internal:
6932                       if (elf_section_data (section)->relocs == NULL)
6933                         free (internal_relocs);
6934                     error_ret_free_local:
6935                       if (local_syms != NULL
6936                           && (symtab_hdr->contents
6937                               != (unsigned char *) local_syms))
6938                         free (local_syms);
6939                       return FALSE;
6940                     }
6941
6942                   stub_entry->stub_type = stub_type;
6943                   stub_entry->target_value = sym_value;
6944                   stub_entry->target_section = sym_sec;
6945                   stub_entry->h = hash;
6946                   stub_entry->addend = irela->r_addend;
6947                   stub_changed = TRUE;
6948                 }
6949
6950               /* We're done with the internal relocs, free them.  */
6951               if (elf_section_data (section)->relocs != internal_relocs)
6952                 free (internal_relocs);
6953             }
6954
6955           if (local_syms != NULL
6956               && symtab_hdr->contents != (unsigned char *) local_syms)
6957             {
6958               if (!info->keep_memory)
6959                 free (local_syms);
6960               else
6961                 symtab_hdr->contents = (unsigned char *) local_syms;
6962             }
6963         }
6964
6965       if (!stub_changed)
6966         break;
6967
6968       /* OK, we've added some stubs.  Find out the new size of the
6969          stub sections.  */
6970       for (stub_sec = htab->stub_bfd->sections;
6971            stub_sec != NULL;
6972            stub_sec = stub_sec->next)
6973         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
6974           {
6975             stub_sec->_raw_size = 0;
6976             stub_sec->_cooked_size = 0;
6977           }
6978       htab->brlt->_raw_size = 0;
6979       htab->brlt->_cooked_size = 0;
6980
6981       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, htab);
6982
6983       /* Ask the linker to do its stuff.  */
6984       (*htab->layout_sections_again) ();
6985     }
6986
6987   /* It would be nice to strip .branch_lt from the output if the
6988      section is empty, but it's too late.  If we strip sections here,
6989      the dynamic symbol table is corrupted since the section symbol
6990      for the stripped section isn't written.  */
6991
6992   return TRUE;
6993 }
6994
6995 /* Called after we have determined section placement.  If sections
6996    move, we'll be called again.  Provide a value for TOCstart.  */
6997
6998 bfd_vma
6999 ppc64_elf_toc (bfd *obfd)
7000 {
7001   asection *s;
7002   bfd_vma TOCstart;
7003
7004   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
7005      order.  The TOC starts where the first of these sections starts.  */
7006   s = bfd_get_section_by_name (obfd, ".got");
7007   if (s == NULL)
7008     s = bfd_get_section_by_name (obfd, ".toc");
7009   if (s == NULL)
7010     s = bfd_get_section_by_name (obfd, ".tocbss");
7011   if (s == NULL)
7012     s = bfd_get_section_by_name (obfd, ".plt");
7013   if (s == NULL)
7014     {
7015       /* This may happen for
7016          o  references to TOC base (SYM@toc / TOC[tc0]) without a
7017          .toc directive
7018          o  bad linker script
7019          o --gc-sections and empty TOC sections
7020
7021          FIXME: Warn user?  */
7022
7023       /* Look for a likely section.  We probably won't even be
7024          using TOCstart.  */
7025       for (s = obfd->sections; s != NULL; s = s->next)
7026         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
7027             == (SEC_ALLOC | SEC_SMALL_DATA))
7028           break;
7029       if (s == NULL)
7030         for (s = obfd->sections; s != NULL; s = s->next)
7031           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
7032               == (SEC_ALLOC | SEC_SMALL_DATA))
7033             break;
7034       if (s == NULL)
7035         for (s = obfd->sections; s != NULL; s = s->next)
7036           if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
7037             break;
7038       if (s == NULL)
7039         for (s = obfd->sections; s != NULL; s = s->next)
7040           if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
7041             break;
7042     }
7043
7044   TOCstart = 0;
7045   if (s != NULL)
7046     TOCstart = s->output_section->vma + s->output_offset;
7047
7048   return TOCstart;
7049 }
7050
7051 /* Build all the stubs associated with the current output file.
7052    The stubs are kept in a hash table attached to the main linker
7053    hash table.  This function is called via gldelf64ppc_finish.  */
7054
7055 bfd_boolean
7056 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
7057                        struct bfd_link_info *info,
7058                        char **stats)
7059 {
7060   struct ppc_link_hash_table *htab = ppc_hash_table (info);
7061   asection *stub_sec;
7062   bfd_byte *p;
7063   int stub_sec_count = 0;
7064
7065   htab->emit_stub_syms = emit_stub_syms;
7066   for (stub_sec = htab->stub_bfd->sections;
7067        stub_sec != NULL;
7068        stub_sec = stub_sec->next)
7069     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
7070       {
7071         bfd_size_type size;
7072
7073         /* Allocate memory to hold the linker stubs.  */
7074         size = stub_sec->_raw_size;
7075         if (size != 0)
7076           {
7077             stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
7078             if (stub_sec->contents == NULL)
7079               return FALSE;
7080           }
7081         stub_sec->_cooked_size = 0;
7082       }
7083
7084   if (htab->plt != NULL)
7085     {
7086       unsigned int indx;
7087       bfd_vma plt0;
7088
7089       /* Build the .glink plt call stub.  */
7090       plt0 = (htab->plt->output_section->vma
7091               + htab->plt->output_offset
7092               - (htab->glink->output_section->vma
7093                  + htab->glink->output_offset
7094                  + GLINK_CALL_STUB_SIZE));
7095       if (plt0 + 0x80008000 > 0xffffffff)
7096         {
7097           (*_bfd_error_handler) (_(".glink and .plt too far apart"));
7098           bfd_set_error (bfd_error_bad_value);
7099           return FALSE;
7100         }
7101
7102       if (htab->emit_stub_syms)
7103         {
7104           struct elf_link_hash_entry *h;
7105           h = elf_link_hash_lookup (&htab->elf, "__glink", TRUE, FALSE, FALSE);
7106           if (h == NULL)
7107             return FALSE;
7108           if (h->root.type == bfd_link_hash_new)
7109             {
7110               h->root.type = bfd_link_hash_defined;
7111               h->root.u.def.section = htab->glink;
7112               h->root.u.def.value = 0;
7113               h->elf_link_hash_flags = (ELF_LINK_HASH_REF_REGULAR
7114                                         | ELF_LINK_HASH_DEF_REGULAR
7115                                         | ELF_LINK_HASH_REF_REGULAR_NONWEAK
7116                                         | ELF_LINK_FORCED_LOCAL);
7117             }
7118         }
7119       p = htab->glink->contents;
7120       bfd_put_32 (htab->glink->owner, MFCTR_R12, p);
7121       p += 4;
7122       bfd_put_32 (htab->glink->owner, SLDI_R11_R0_3, p);
7123       p += 4;
7124       bfd_put_32 (htab->glink->owner, ADDIC_R2_R0_32K, p);
7125       p += 4;
7126       bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
7127       p += 4;
7128       bfd_put_32 (htab->glink->owner, SRADI_R2_R2_63, p);
7129       p += 4;
7130       bfd_put_32 (htab->glink->owner, SLDI_R11_R0_2, p);
7131       p += 4;
7132       bfd_put_32 (htab->glink->owner, AND_R2_R2_R11, p);
7133       p += 4;
7134       bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
7135       p += 4;
7136       bfd_put_32 (htab->glink->owner, ADD_R12_R12_R2, p);
7137       p += 4;
7138       bfd_put_32 (htab->glink->owner, ADDIS_R12_R12 | PPC_HA (plt0), p);
7139       p += 4;
7140       bfd_put_32 (htab->glink->owner, LD_R11_0R12 | PPC_LO (plt0), p);
7141       p += 4;
7142       bfd_put_32 (htab->glink->owner, ADDI_R12_R12 | PPC_LO (plt0), p);
7143       p += 4;
7144       bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
7145       p += 4;
7146       bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
7147       p += 4;
7148       bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
7149       p += 4;
7150       bfd_put_32 (htab->glink->owner, BCTR, p);
7151       p += 4;
7152
7153       /* Build the .glink lazy link call stubs.  */
7154       indx = 0;
7155       while (p < htab->glink->contents + htab->glink->_raw_size)
7156         {
7157           if (indx < 0x8000)
7158             {
7159               bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
7160               p += 4;
7161             }
7162           else
7163             {
7164               bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
7165               p += 4;
7166               bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
7167               p += 4;
7168             }
7169           bfd_put_32 (htab->glink->owner,
7170                       B_DOT | ((htab->glink->contents - p) & 0x3fffffc), p);
7171           indx++;
7172           p += 4;
7173         }
7174       htab->glink->_cooked_size = p - htab->glink->contents;
7175     }
7176
7177   if (htab->brlt->_raw_size != 0)
7178     {
7179       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
7180                                          htab->brlt->_raw_size);
7181       if (htab->brlt->contents == NULL)
7182         return FALSE;
7183     }
7184
7185   /* Build the stubs as directed by the stub hash table.  */
7186   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
7187
7188   for (stub_sec = htab->stub_bfd->sections;
7189        stub_sec != NULL;
7190        stub_sec = stub_sec->next)
7191     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
7192       {
7193         stub_sec_count += 1;
7194         if (stub_sec->_raw_size != stub_sec->_cooked_size)
7195           break;
7196       }
7197
7198   if (stub_sec != NULL
7199       || htab->glink->_raw_size != htab->glink->_cooked_size)
7200     {
7201       htab->stub_error = TRUE;
7202       (*_bfd_error_handler) (_("stubs don't match calculated size"));
7203     }
7204
7205   if (htab->stub_error)
7206     return FALSE;
7207
7208   if (stats != NULL)
7209     {
7210       *stats = bfd_malloc (500);
7211       if (*stats == NULL)
7212         return FALSE;
7213
7214       sprintf (*stats, _("linker stubs in %u groups\n"
7215                          "  branch       %lu\n"
7216                          "  toc adjust   %lu\n"
7217                          "  long branch  %lu\n"
7218                          "  long toc adj %lu\n"
7219                          "  plt call     %lu"),
7220                stub_sec_count,
7221                htab->stub_count[ppc_stub_long_branch - 1],
7222                htab->stub_count[ppc_stub_long_branch_r2off - 1],
7223                htab->stub_count[ppc_stub_plt_branch - 1],
7224                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
7225                htab->stub_count[ppc_stub_plt_call - 1]);
7226     }
7227   return TRUE;
7228 }
7229
7230 /* The RELOCATE_SECTION function is called by the ELF backend linker
7231    to handle the relocations for a section.
7232
7233    The relocs are always passed as Rela structures; if the section
7234    actually uses Rel structures, the r_addend field will always be
7235    zero.
7236
7237    This function is responsible for adjust the section contents as
7238    necessary, and (if using Rela relocs and generating a
7239    relocatable output file) adjusting the reloc addend as
7240    necessary.
7241
7242    This function does not have to worry about setting the reloc
7243    address or the reloc symbol index.
7244
7245    LOCAL_SYMS is a pointer to the swapped in local symbols.
7246
7247    LOCAL_SECTIONS is an array giving the section in the input file
7248    corresponding to the st_shndx field of each local symbol.
7249
7250    The global hash table entry for the global symbols can be found
7251    via elf_sym_hashes (input_bfd).
7252
7253    When generating relocatable output, this function must handle
7254    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
7255    going to be the section symbol corresponding to the output
7256    section, which means that the addend must be adjusted
7257    accordingly.  */
7258
7259 static bfd_boolean
7260 ppc64_elf_relocate_section (bfd *output_bfd,
7261                             struct bfd_link_info *info,
7262                             bfd *input_bfd,
7263                             asection *input_section,
7264                             bfd_byte *contents,
7265                             Elf_Internal_Rela *relocs,
7266                             Elf_Internal_Sym *local_syms,
7267                             asection **local_sections)
7268 {
7269   struct ppc_link_hash_table *htab;
7270   Elf_Internal_Shdr *symtab_hdr;
7271   struct elf_link_hash_entry **sym_hashes;
7272   Elf_Internal_Rela *rel;
7273   Elf_Internal_Rela *relend;
7274   Elf_Internal_Rela outrel;
7275   bfd_byte *loc;
7276   struct got_entry **local_got_ents;
7277   bfd_vma TOCstart;
7278   bfd_boolean ret = TRUE;
7279   bfd_boolean is_opd;
7280   /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
7281   bfd_boolean is_power4 = FALSE;
7282
7283   if (info->relocatable)
7284     return TRUE;
7285
7286   /* Initialize howto table if needed.  */
7287   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
7288     ppc_howto_init ();
7289
7290   htab = ppc_hash_table (info);
7291   local_got_ents = elf_local_got_ents (input_bfd);
7292   TOCstart = elf_gp (output_bfd);
7293   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7294   sym_hashes = elf_sym_hashes (input_bfd);
7295   is_opd = ppc64_elf_section_data (input_section)->opd.adjust != NULL;
7296
7297   rel = relocs;
7298   relend = relocs + input_section->reloc_count;
7299   for (; rel < relend; rel++)
7300     {
7301       enum elf_ppc64_reloc_type r_type;
7302       bfd_vma addend;
7303       bfd_reloc_status_type r;
7304       Elf_Internal_Sym *sym;
7305       asection *sec;
7306       struct elf_link_hash_entry *h;
7307       struct elf_link_hash_entry *fdh;
7308       const char *sym_name;
7309       unsigned long r_symndx, toc_symndx;
7310       char tls_mask, tls_gd, tls_type;
7311       char sym_type;
7312       bfd_vma relocation;
7313       bfd_boolean unresolved_reloc;
7314       bfd_boolean warned;
7315       unsigned long insn, mask;
7316       struct ppc_stub_hash_entry *stub_entry;
7317       bfd_vma max_br_offset;
7318       bfd_vma from;
7319
7320       r_type = ELF64_R_TYPE (rel->r_info);
7321       r_symndx = ELF64_R_SYM (rel->r_info);
7322
7323       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
7324          symbol of the previous ADDR64 reloc.  The symbol gives us the
7325          proper TOC base to use.  */
7326       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
7327           && rel != relocs
7328           && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
7329           && is_opd)
7330         r_symndx = ELF64_R_SYM (rel[-1].r_info);
7331
7332       sym = NULL;
7333       sec = NULL;
7334       h = NULL;
7335       sym_name = NULL;
7336       unresolved_reloc = FALSE;
7337       warned = FALSE;
7338
7339       if (r_symndx < symtab_hdr->sh_info)
7340         {
7341           /* It's a local symbol.  */
7342           sym = local_syms + r_symndx;
7343           sec = local_sections[r_symndx];
7344           sym_name = bfd_elf_local_sym_name (input_bfd, sym);
7345           sym_type = ELF64_ST_TYPE (sym->st_info);
7346           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
7347           if (elf_section_data (sec) != NULL)
7348             {
7349               long *opd_sym_adjust;
7350
7351               opd_sym_adjust = ppc64_elf_section_data (sec)->opd.adjust;
7352               if (opd_sym_adjust != NULL && sym->st_value % 24 == 0)
7353                 relocation += opd_sym_adjust[sym->st_value / 24];
7354             }
7355         }
7356       else
7357         {
7358           RELOC_FOR_GLOBAL_SYMBOL (h, sym_hashes, r_symndx,
7359                                    symtab_hdr, relocation, sec,
7360                                    unresolved_reloc, info,
7361                                    warned);
7362           sym_name = h->root.root.string;
7363           sym_type = h->type;
7364         }
7365
7366       /* TLS optimizations.  Replace instruction sequences and relocs
7367          based on information we collected in tls_optimize.  We edit
7368          RELOCS so that --emit-relocs will output something sensible
7369          for the final instruction stream.  */
7370       tls_mask = 0;
7371       tls_gd = 0;
7372       toc_symndx = 0;
7373       if (IS_PPC64_TLS_RELOC (r_type))
7374         {
7375           if (h != NULL)
7376             tls_mask = ((struct ppc_link_hash_entry *) h)->tls_mask;
7377           else if (local_got_ents != NULL)
7378             {
7379               char *lgot_masks;
7380               lgot_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
7381               tls_mask = lgot_masks[r_symndx];
7382             }
7383           if (tls_mask == 0 && r_type == R_PPC64_TLS)
7384             {
7385               /* Check for toc tls entries.  */
7386               char *toc_tls;
7387
7388               if (!get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
7389                                  rel, input_bfd))
7390                 return FALSE;
7391
7392               if (toc_tls)
7393                 tls_mask = *toc_tls;
7394             }
7395         }
7396
7397       /* Check that tls relocs are used with tls syms, and non-tls
7398          relocs are used with non-tls syms.  */
7399       if (r_symndx != 0
7400           && r_type != R_PPC64_NONE
7401           && (h == NULL
7402               || h->root.type == bfd_link_hash_defined
7403               || h->root.type == bfd_link_hash_defweak)
7404           && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
7405         {
7406           if (r_type == R_PPC64_TLS && tls_mask != 0)
7407             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
7408             ;
7409           else
7410             (*_bfd_error_handler)
7411               (sym_type == STT_TLS
7412                ? _("%s(%s+0x%lx): %s used with TLS symbol %s")
7413                : _("%s(%s+0x%lx): %s used with non-TLS symbol %s"),
7414                bfd_archive_filename (input_bfd),
7415                input_section->name,
7416                (long) rel->r_offset,
7417                ppc64_elf_howto_table[r_type]->name,
7418                sym_name);
7419         }
7420
7421       /* Ensure reloc mapping code below stays sane.  */
7422       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
7423           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
7424           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
7425           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
7426           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
7427           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
7428           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
7429           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
7430           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
7431           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
7432         abort ();
7433
7434       switch (r_type)
7435         {
7436         default:
7437           break;
7438
7439         case R_PPC64_TOC16:
7440         case R_PPC64_TOC16_LO:
7441         case R_PPC64_TOC16_DS:
7442         case R_PPC64_TOC16_LO_DS:
7443           {
7444             /* Check for toc tls entries.  */
7445             char *toc_tls;
7446             int retval;
7447
7448             retval = get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
7449                                    rel, input_bfd);
7450             if (retval == 0)
7451               return FALSE;
7452
7453             if (toc_tls)
7454               {
7455                 tls_mask = *toc_tls;
7456                 if (r_type == R_PPC64_TOC16_DS
7457                     || r_type == R_PPC64_TOC16_LO_DS)
7458                   {
7459                     if (tls_mask != 0
7460                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
7461                       goto toctprel;
7462                   }
7463                 else
7464                   {
7465                     /* If we found a GD reloc pair, then we might be
7466                        doing a GD->IE transition.  */
7467                     if (retval == 2)
7468                       {
7469                         tls_gd = TLS_TPRELGD;
7470                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7471                           goto tls_get_addr_check;
7472                       }
7473                     else if (retval == 3)
7474                       {
7475                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7476                           goto tls_get_addr_check;
7477                       }
7478                   }
7479               }
7480           }
7481           break;
7482
7483         case R_PPC64_GOT_TPREL16_DS:
7484         case R_PPC64_GOT_TPREL16_LO_DS:
7485           if (tls_mask != 0
7486               && (tls_mask & TLS_TPREL) == 0)
7487             {
7488             toctprel:
7489               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
7490               insn &= 31 << 21;
7491               insn |= 0x3c0d0000;       /* addis 0,13,0 */
7492               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
7493               r_type = R_PPC64_TPREL16_HA;
7494               if (toc_symndx != 0)
7495                 {
7496                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
7497                   /* We changed the symbol.  Start over in order to
7498                      get h, sym, sec etc. right.  */
7499                   rel--;
7500                   continue;
7501                 }
7502               else
7503                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7504             }
7505           break;
7506
7507         case R_PPC64_TLS:
7508           if (tls_mask != 0
7509               && (tls_mask & TLS_TPREL) == 0)
7510             {
7511               bfd_vma rtra;
7512               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
7513               if ((insn & ((0x3f << 26) | (31 << 11)))
7514                   == ((31 << 26) | (13 << 11)))
7515                 rtra = insn & ((1 << 26) - (1 << 16));
7516               else if ((insn & ((0x3f << 26) | (31 << 16)))
7517                        == ((31 << 26) | (13 << 16)))
7518                 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
7519               else
7520                 abort ();
7521               if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
7522                 /* add -> addi.  */
7523                 insn = 14 << 26;
7524               else if ((insn & (31 << 1)) == 23 << 1
7525                        && ((insn & (31 << 6)) < 14 << 6
7526                            || ((insn & (31 << 6)) >= 16 << 6
7527                                && (insn & (31 << 6)) < 24 << 6)))
7528                 /* load and store indexed -> dform.  */
7529                 insn = (32 | ((insn >> 6) & 31)) << 26;
7530               else if ((insn & (31 << 1)) == 21 << 1
7531                        && (insn & (0x1a << 6)) == 0)
7532                 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu.  */
7533                 insn = (((58 | ((insn >> 6) & 4)) << 26)
7534                         | ((insn >> 6) & 1));
7535               else if ((insn & (31 << 1)) == 21 << 1
7536                        && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
7537                 /* lwax -> lwa.  */
7538                 insn = (58 << 26) | 2;
7539               else
7540                 abort ();
7541               insn |= rtra;
7542               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7543               /* Was PPC64_TLS which sits on insn boundary, now
7544                  PPC64_TPREL16_LO which is at insn+2.  */
7545               rel->r_offset += 2;
7546               r_type = R_PPC64_TPREL16_LO;
7547               if (toc_symndx != 0)
7548                 {
7549                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
7550                   /* We changed the symbol.  Start over in order to
7551                      get h, sym, sec etc. right.  */
7552                   rel--;
7553                   continue;
7554                 }
7555               else
7556                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7557             }
7558           break;
7559
7560         case R_PPC64_GOT_TLSGD16_HI:
7561         case R_PPC64_GOT_TLSGD16_HA:
7562           tls_gd = TLS_TPRELGD;
7563           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7564             goto tls_gdld_hi;
7565           break;
7566
7567         case R_PPC64_GOT_TLSLD16_HI:
7568         case R_PPC64_GOT_TLSLD16_HA:
7569           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7570             {
7571             tls_gdld_hi:
7572               if ((tls_mask & tls_gd) != 0)
7573                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
7574                           + R_PPC64_GOT_TPREL16_DS);
7575               else
7576                 {
7577                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
7578                   rel->r_offset -= 2;
7579                   r_type = R_PPC64_NONE;
7580                 }
7581               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7582             }
7583           break;
7584
7585         case R_PPC64_GOT_TLSGD16:
7586         case R_PPC64_GOT_TLSGD16_LO:
7587           tls_gd = TLS_TPRELGD;
7588           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7589             goto tls_get_addr_check;
7590           break;
7591
7592         case R_PPC64_GOT_TLSLD16:
7593         case R_PPC64_GOT_TLSLD16_LO:
7594           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7595             {
7596             tls_get_addr_check:
7597               if (rel + 1 < relend)
7598                 {
7599                   enum elf_ppc64_reloc_type r_type2;
7600                   unsigned long r_symndx2;
7601                   struct elf_link_hash_entry *h2;
7602                   bfd_vma insn1, insn2, insn3;
7603                   bfd_vma offset;
7604
7605                   /* The next instruction should be a call to
7606                      __tls_get_addr.  Peek at the reloc to be sure.  */
7607                   r_type2 = ELF64_R_TYPE (rel[1].r_info);
7608                   r_symndx2 = ELF64_R_SYM (rel[1].r_info);
7609                   if (r_symndx2 < symtab_hdr->sh_info
7610                       || (r_type2 != R_PPC64_REL14
7611                           && r_type2 != R_PPC64_REL14_BRTAKEN
7612                           && r_type2 != R_PPC64_REL14_BRNTAKEN
7613                           && r_type2 != R_PPC64_REL24))
7614                     break;
7615
7616                   h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
7617                   while (h2->root.type == bfd_link_hash_indirect
7618                          || h2->root.type == bfd_link_hash_warning)
7619                     h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
7620                   if (h2 == NULL || h2 != htab->tls_get_addr)
7621                     break;
7622
7623                   /* OK, it checks out.  Replace the call.  */
7624                   offset = rel[1].r_offset;
7625                   insn1 = bfd_get_32 (output_bfd,
7626                                       contents + rel->r_offset - 2);
7627                   insn3 = bfd_get_32 (output_bfd,
7628                                       contents + offset + 4);
7629                   if ((tls_mask & tls_gd) != 0)
7630                     {
7631                       /* IE */
7632                       insn1 &= (1 << 26) - (1 << 2);
7633                       insn1 |= 58 << 26;        /* ld */
7634                       insn2 = 0x7c636a14;       /* add 3,3,13 */
7635                       rel[1].r_info = ELF64_R_INFO (r_symndx2, R_PPC64_NONE);
7636                       if ((tls_mask & TLS_EXPLICIT) == 0)
7637                         r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
7638                                   + R_PPC64_GOT_TPREL16_DS);
7639                       else
7640                         r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
7641                       rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7642                     }
7643                   else
7644                     {
7645                       /* LE */
7646                       insn1 = 0x3c6d0000;       /* addis 3,13,0 */
7647                       insn2 = 0x38630000;       /* addi 3,3,0 */
7648                       if (tls_gd == 0)
7649                         {
7650                           /* Was an LD reloc.  */
7651                           r_symndx = 0;
7652                           rel->r_addend = htab->tls_sec->vma + DTP_OFFSET;
7653                           rel[1].r_addend = htab->tls_sec->vma + DTP_OFFSET;
7654                         }
7655                       else if (toc_symndx != 0)
7656                         r_symndx = toc_symndx;
7657                       r_type = R_PPC64_TPREL16_HA;
7658                       rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7659                       rel[1].r_info = ELF64_R_INFO (r_symndx,
7660                                                     R_PPC64_TPREL16_LO);
7661                       rel[1].r_offset += 2;
7662                     }
7663                   if (insn3 == NOP
7664                       || insn3 == CROR_151515 || insn3 == CROR_313131)
7665                     {
7666                       insn3 = insn2;
7667                       insn2 = NOP;
7668                       rel[1].r_offset += 4;
7669                     }
7670                   bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
7671                   bfd_put_32 (output_bfd, insn2, contents + offset);
7672                   bfd_put_32 (output_bfd, insn3, contents + offset + 4);
7673                   if (tls_gd == 0 || toc_symndx != 0)
7674                     {
7675                       /* We changed the symbol.  Start over in order
7676                          to get h, sym, sec etc. right.  */
7677                       rel--;
7678                       continue;
7679                     }
7680                 }
7681             }
7682           break;
7683
7684         case R_PPC64_DTPMOD64:
7685           if (rel + 1 < relend
7686               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
7687               && rel[1].r_offset == rel->r_offset + 8)
7688             {
7689               if ((tls_mask & TLS_GD) == 0)
7690                 {
7691                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
7692                   if ((tls_mask & TLS_TPRELGD) != 0)
7693                     r_type = R_PPC64_TPREL64;
7694                   else
7695                     {
7696                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
7697                       r_type = R_PPC64_NONE;
7698                     }
7699                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7700                 }
7701             }
7702           else
7703             {
7704               if ((tls_mask & TLS_LD) == 0)
7705                 {
7706                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
7707                   r_type = R_PPC64_NONE;
7708                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7709                 }
7710             }
7711           break;
7712
7713         case R_PPC64_TPREL64:
7714           if ((tls_mask & TLS_TPREL) == 0)
7715             {
7716               r_type = R_PPC64_NONE;
7717               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7718             }
7719           break;
7720         }
7721
7722       /* Handle other relocations that tweak non-addend part of insn.  */
7723       insn = 0;
7724       switch (r_type)
7725         {
7726         default:
7727           break;
7728
7729           /* Branch taken prediction relocations.  */
7730         case R_PPC64_ADDR14_BRTAKEN:
7731         case R_PPC64_REL14_BRTAKEN:
7732           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
7733           /* Fall thru.  */
7734
7735           /* Branch not taken prediction relocations.  */
7736         case R_PPC64_ADDR14_BRNTAKEN:
7737         case R_PPC64_REL14_BRNTAKEN:
7738           insn |= bfd_get_32 (output_bfd,
7739                               contents + rel->r_offset) & ~(0x01 << 21);
7740           if (is_power4)
7741             {
7742               /* Set 'a' bit.  This is 0b00010 in BO field for branch
7743                  on CR(BI) insns (BO == 001at or 011at), and 0b01000
7744                  for branch on CTR insns (BO == 1a00t or 1a01t).  */
7745               if ((insn & (0x14 << 21)) == (0x04 << 21))
7746                 insn |= 0x02 << 21;
7747               else if ((insn & (0x14 << 21)) == (0x10 << 21))
7748                 insn |= 0x08 << 21;
7749               else
7750                 break;
7751             }
7752           else
7753             {
7754               from = (rel->r_offset
7755                       + input_section->output_offset
7756                       + input_section->output_section->vma);
7757
7758               /* Invert 'y' bit if not the default.  */
7759               if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
7760                 insn ^= 0x01 << 21;
7761             }
7762
7763           bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7764           break;
7765
7766         case R_PPC64_REL24:
7767           /* Calls to functions with a different TOC, such as calls to
7768              shared objects, need to alter the TOC pointer.  This is
7769              done using a linkage stub.  A REL24 branching to these
7770              linkage stubs needs to be followed by a nop, as the nop
7771              will be replaced with an instruction to restore the TOC
7772              base pointer.  */
7773           if (((h != NULL
7774                 && (fdh = ((struct ppc_link_hash_entry *) h)->oh) != NULL
7775                 && fdh->plt.plist != NULL)
7776                || ((fdh = h, sec) != NULL
7777                    && sec->output_section != NULL
7778                    && (htab->stub_group[sec->id].toc_off
7779                        != htab->stub_group[input_section->id].toc_off)))
7780               && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
7781                                                    rel, htab)) != NULL
7782               && (stub_entry->stub_type == ppc_stub_plt_call
7783                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
7784                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
7785             {
7786               bfd_boolean can_plt_call = 0;
7787
7788               if (rel->r_offset + 8 <= input_section->_cooked_size)
7789                 {
7790                   insn = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
7791                   if (insn == NOP
7792                       || insn == CROR_151515 || insn == CROR_313131)
7793                     {
7794                       bfd_put_32 (input_bfd, LD_R2_40R1,
7795                                   contents + rel->r_offset + 4);
7796                       can_plt_call = 1;
7797                     }
7798                 }
7799
7800               if (!can_plt_call)
7801                 {
7802                   if (stub_entry->stub_type == ppc_stub_plt_call)
7803                     {
7804                       /* If this is a plain branch rather than a branch
7805                          and link, don't require a nop.  */
7806                       insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
7807                       if ((insn & 1) == 0)
7808                         can_plt_call = 1;
7809                     }
7810                   else if (h != NULL
7811                            && strcmp (h->root.root.string,
7812                                       ".__libc_start_main") == 0)
7813                     {
7814                       /* Allow crt1 branch to go via a toc adjusting stub.  */
7815                       can_plt_call = 1;
7816                     }
7817                   else
7818                     {
7819                       if (strcmp (input_section->output_section->name,
7820                                   ".init") == 0
7821                           || strcmp (input_section->output_section->name,
7822                                      ".fini") == 0)
7823                         (*_bfd_error_handler)
7824                           (_("%s(%s+0x%lx): automatic multiple TOCs "
7825                              "not supported using your crt files; "
7826                              "recompile with -mminimal-toc or upgrade gcc"),
7827                            bfd_archive_filename (input_bfd),
7828                            input_section->name,
7829                            (long) rel->r_offset);
7830                       else
7831                         (*_bfd_error_handler)
7832                           (_("%s(%s+0x%lx): sibling call optimization to `%s' "
7833                              "does not allow automatic multiple TOCs; "
7834                              "recompile with -mminimal-toc or "
7835                              "-fno-optimize-sibling-calls, "
7836                              "or make `%s' extern"),
7837                            bfd_archive_filename (input_bfd),
7838                            input_section->name,
7839                            (long) rel->r_offset,
7840                            sym_name,
7841                            sym_name);
7842                       bfd_set_error (bfd_error_bad_value);
7843                       ret = FALSE;
7844                     }
7845                 }
7846
7847               if (can_plt_call)
7848                 {
7849                   relocation = (stub_entry->stub_offset
7850                                 + stub_entry->stub_sec->output_offset
7851                                 + stub_entry->stub_sec->output_section->vma);
7852                   if (stub_entry->stub_type == ppc_stub_plt_call)
7853                     unresolved_reloc = FALSE;
7854                 }
7855             }
7856
7857           if (h != NULL
7858               && h->root.type == bfd_link_hash_undefweak
7859               && relocation == 0
7860               && rel->r_addend == 0)
7861             {
7862               /* Tweak calls to undefined weak functions to point at a
7863                  blr.  We can thus call a weak function without first
7864                  checking whether the function is defined.  We have a
7865                  blr at the end of .sfpr.  */
7866               BFD_ASSERT (htab->sfpr->_raw_size != 0);
7867               relocation = (htab->sfpr->_raw_size - 4
7868                             + htab->sfpr->output_offset
7869                             + htab->sfpr->output_section->vma);
7870               from = (rel->r_offset
7871                       + input_section->output_offset
7872                       + input_section->output_section->vma);
7873
7874               /* But let's not be silly about it.  If the blr isn't in
7875                  reach, just go to the next instruction.  */
7876               if (relocation - from + (1 << 25) >= (1 << 26)
7877                   || htab->sfpr->_raw_size == 0)
7878                 relocation = from + 4;
7879             }
7880           break;
7881         }
7882
7883       /* Set `addend'.  */
7884       tls_type = 0;
7885       addend = rel->r_addend;
7886       switch (r_type)
7887         {
7888         default:
7889           (*_bfd_error_handler)
7890             (_("%s: unknown relocation type %d for symbol %s"),
7891              bfd_archive_filename (input_bfd), (int) r_type, sym_name);
7892
7893           bfd_set_error (bfd_error_bad_value);
7894           ret = FALSE;
7895           continue;
7896
7897         case R_PPC64_NONE:
7898         case R_PPC64_TLS:
7899         case R_PPC64_GNU_VTINHERIT:
7900         case R_PPC64_GNU_VTENTRY:
7901           continue;
7902
7903           /* GOT16 relocations.  Like an ADDR16 using the symbol's
7904              address in the GOT as relocation value instead of the
7905              symbol's value itself.  Also, create a GOT entry for the
7906              symbol and put the symbol value there.  */
7907         case R_PPC64_GOT_TLSGD16:
7908         case R_PPC64_GOT_TLSGD16_LO:
7909         case R_PPC64_GOT_TLSGD16_HI:
7910         case R_PPC64_GOT_TLSGD16_HA:
7911           tls_type = TLS_TLS | TLS_GD;
7912           goto dogot;
7913
7914         case R_PPC64_GOT_TLSLD16:
7915         case R_PPC64_GOT_TLSLD16_LO:
7916         case R_PPC64_GOT_TLSLD16_HI:
7917         case R_PPC64_GOT_TLSLD16_HA:
7918           tls_type = TLS_TLS | TLS_LD;
7919           goto dogot;
7920
7921         case R_PPC64_GOT_TPREL16_DS:
7922         case R_PPC64_GOT_TPREL16_LO_DS:
7923         case R_PPC64_GOT_TPREL16_HI:
7924         case R_PPC64_GOT_TPREL16_HA:
7925           tls_type = TLS_TLS | TLS_TPREL;
7926           goto dogot;
7927
7928         case R_PPC64_GOT_DTPREL16_DS:
7929         case R_PPC64_GOT_DTPREL16_LO_DS:
7930         case R_PPC64_GOT_DTPREL16_HI:
7931         case R_PPC64_GOT_DTPREL16_HA:
7932           tls_type = TLS_TLS | TLS_DTPREL;
7933           goto dogot;
7934
7935         case R_PPC64_GOT16:
7936         case R_PPC64_GOT16_LO:
7937         case R_PPC64_GOT16_HI:
7938         case R_PPC64_GOT16_HA:
7939         case R_PPC64_GOT16_DS:
7940         case R_PPC64_GOT16_LO_DS:
7941         dogot:
7942           {
7943             /* Relocation is to the entry for this symbol in the global
7944                offset table.  */
7945             asection *got;
7946             bfd_vma *offp;
7947             bfd_vma off;
7948             unsigned long indx = 0;
7949
7950             if (tls_type == (TLS_TLS | TLS_LD)
7951                 && (h == NULL
7952                     || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
7953               offp = &ppc64_tlsld_got (input_bfd)->offset;
7954             else
7955               {
7956                 struct got_entry *ent;
7957
7958                 if (h != NULL)
7959                   {
7960                     bfd_boolean dyn = htab->elf.dynamic_sections_created;
7961                     if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
7962                         || (info->shared
7963                             && SYMBOL_REFERENCES_LOCAL (info, h)))
7964                       /* This is actually a static link, or it is a
7965                          -Bsymbolic link and the symbol is defined
7966                          locally, or the symbol was forced to be local
7967                          because of a version file.  */
7968                       ;
7969                     else
7970                       {
7971                         indx = h->dynindx;
7972                         unresolved_reloc = FALSE;
7973                       }
7974                     ent = h->got.glist;
7975                   }
7976                 else
7977                   {
7978                     if (local_got_ents == NULL)
7979                       abort ();
7980                     ent = local_got_ents[r_symndx];
7981                   }
7982
7983                 for (; ent != NULL; ent = ent->next)
7984                   if (ent->addend == rel->r_addend
7985                       && ent->owner == input_bfd
7986                       && ent->tls_type == tls_type)
7987                     break;
7988                 if (ent == NULL)
7989                   abort ();
7990                 offp = &ent->got.offset;
7991               }
7992
7993             got = ppc64_elf_tdata (input_bfd)->got;
7994             if (got == NULL)
7995               abort ();
7996
7997             /* The offset must always be a multiple of 8.  We use the
7998                least significant bit to record whether we have already
7999                processed this entry.  */
8000             off = *offp;
8001             if ((off & 1) != 0)
8002               off &= ~1;
8003             else
8004               {
8005                 /* Generate relocs for the dynamic linker, except in
8006                    the case of TLSLD where we'll use one entry per
8007                    module.  */
8008                 asection *relgot = ppc64_elf_tdata (input_bfd)->relgot;
8009
8010                 *offp = off | 1;
8011                 if ((info->shared || indx != 0)
8012                     && (h == NULL
8013                         || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8014                         || h->root.type != bfd_link_hash_undefweak))
8015                   {
8016                     outrel.r_offset = (got->output_section->vma
8017                                        + got->output_offset
8018                                        + off);
8019                     outrel.r_addend = rel->r_addend;
8020                     if (tls_type & (TLS_LD | TLS_GD))
8021                       {
8022                         outrel.r_addend = 0;
8023                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
8024                         if (tls_type == (TLS_TLS | TLS_GD))
8025                           {
8026                             loc = relgot->contents;
8027                             loc += (relgot->reloc_count++
8028                                     * sizeof (Elf64_External_Rela));
8029                             bfd_elf64_swap_reloca_out (output_bfd,
8030                                                        &outrel, loc);
8031                             outrel.r_offset += 8;
8032                             outrel.r_addend = rel->r_addend;
8033                             outrel.r_info
8034                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
8035                           }
8036                       }
8037                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
8038                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
8039                     else if (tls_type == (TLS_TLS | TLS_TPREL))
8040                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
8041                     else if (indx == 0)
8042                       {
8043                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_RELATIVE);
8044
8045                         /* Write the .got section contents for the sake
8046                            of prelink.  */
8047                         loc = got->contents + off;
8048                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
8049                                     loc);
8050                       }
8051                     else
8052                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
8053
8054                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
8055                       {
8056                         outrel.r_addend += relocation;
8057                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
8058                           outrel.r_addend -= htab->tls_sec->vma;
8059                       }
8060                     loc = relgot->contents;
8061                     loc += (relgot->reloc_count++
8062                             * sizeof (Elf64_External_Rela));
8063                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
8064                   }
8065
8066                 /* Init the .got section contents here if we're not
8067                    emitting a reloc.  */
8068                 else
8069                   {
8070                     relocation += rel->r_addend;
8071                     if (tls_type == (TLS_TLS | TLS_LD))
8072                       relocation = 1;
8073                     else if (tls_type != 0)
8074                       {
8075                         relocation -= htab->tls_sec->vma + DTP_OFFSET;
8076                         if (tls_type == (TLS_TLS | TLS_TPREL))
8077                           relocation += DTP_OFFSET - TP_OFFSET;
8078
8079                         if (tls_type == (TLS_TLS | TLS_GD))
8080                           {
8081                             bfd_put_64 (output_bfd, relocation,
8082                                         got->contents + off + 8);
8083                             relocation = 1;
8084                           }
8085                       }
8086
8087                     bfd_put_64 (output_bfd, relocation,
8088                                 got->contents + off);
8089                   }
8090               }
8091
8092             if (off >= (bfd_vma) -2)
8093               abort ();
8094
8095             relocation = got->output_offset + off;
8096
8097             /* TOC base (r2) is TOC start plus 0x8000.  */
8098             addend = -TOC_BASE_OFF;
8099           }
8100           break;
8101
8102         case R_PPC64_PLT16_HA:
8103         case R_PPC64_PLT16_HI:
8104         case R_PPC64_PLT16_LO:
8105         case R_PPC64_PLT32:
8106         case R_PPC64_PLT64:
8107           /* Relocation is to the entry for this symbol in the
8108              procedure linkage table.  */
8109
8110           /* Resolve a PLT reloc against a local symbol directly,
8111              without using the procedure linkage table.  */
8112           if (h == NULL)
8113             break;
8114
8115           /* It's possible that we didn't make a PLT entry for this
8116              symbol.  This happens when statically linking PIC code,
8117              or when using -Bsymbolic.  Go find a match if there is a
8118              PLT entry.  */
8119           if (htab->plt != NULL)
8120             {
8121               struct plt_entry *ent;
8122               for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8123                 if (ent->addend == rel->r_addend
8124                     && ent->plt.offset != (bfd_vma) -1)
8125                   {
8126                     relocation = (htab->plt->output_section->vma
8127                                   + htab->plt->output_offset
8128                                   + ent->plt.offset);
8129                     unresolved_reloc = FALSE;
8130                   }
8131             }
8132           break;
8133
8134         case R_PPC64_TOC:
8135           /* Relocation value is TOC base.  */
8136           relocation = TOCstart;
8137           if (r_symndx == 0)
8138             relocation += htab->stub_group[input_section->id].toc_off;
8139           else if (sec != NULL && !unresolved_reloc)
8140             relocation += htab->stub_group[sec->id].toc_off;
8141           else
8142             unresolved_reloc = TRUE;
8143           goto dodyn2;
8144
8145           /* TOC16 relocs.  We want the offset relative to the TOC base,
8146              which is the address of the start of the TOC plus 0x8000.
8147              The TOC consists of sections .got, .toc, .tocbss, and .plt,
8148              in this order.  */
8149         case R_PPC64_TOC16:
8150         case R_PPC64_TOC16_LO:
8151         case R_PPC64_TOC16_HI:
8152         case R_PPC64_TOC16_DS:
8153         case R_PPC64_TOC16_LO_DS:
8154         case R_PPC64_TOC16_HA:
8155           addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
8156           break;
8157
8158           /* Relocate against the beginning of the section.  */
8159         case R_PPC64_SECTOFF:
8160         case R_PPC64_SECTOFF_LO:
8161         case R_PPC64_SECTOFF_HI:
8162         case R_PPC64_SECTOFF_DS:
8163         case R_PPC64_SECTOFF_LO_DS:
8164         case R_PPC64_SECTOFF_HA:
8165           if (sec != NULL)
8166             addend -= sec->output_section->vma;
8167           break;
8168
8169         case R_PPC64_REL14:
8170         case R_PPC64_REL14_BRNTAKEN:
8171         case R_PPC64_REL14_BRTAKEN:
8172         case R_PPC64_REL24:
8173           break;
8174
8175         case R_PPC64_TPREL16:
8176         case R_PPC64_TPREL16_LO:
8177         case R_PPC64_TPREL16_HI:
8178         case R_PPC64_TPREL16_HA:
8179         case R_PPC64_TPREL16_DS:
8180         case R_PPC64_TPREL16_LO_DS:
8181         case R_PPC64_TPREL16_HIGHER:
8182         case R_PPC64_TPREL16_HIGHERA:
8183         case R_PPC64_TPREL16_HIGHEST:
8184         case R_PPC64_TPREL16_HIGHESTA:
8185           addend -= htab->tls_sec->vma + TP_OFFSET;
8186           if (info->shared)
8187             /* The TPREL16 relocs shouldn't really be used in shared
8188                libs as they will result in DT_TEXTREL being set, but
8189                support them anyway.  */
8190             goto dodyn;
8191           break;
8192
8193         case R_PPC64_DTPREL16:
8194         case R_PPC64_DTPREL16_LO:
8195         case R_PPC64_DTPREL16_HI:
8196         case R_PPC64_DTPREL16_HA:
8197         case R_PPC64_DTPREL16_DS:
8198         case R_PPC64_DTPREL16_LO_DS:
8199         case R_PPC64_DTPREL16_HIGHER:
8200         case R_PPC64_DTPREL16_HIGHERA:
8201         case R_PPC64_DTPREL16_HIGHEST:
8202         case R_PPC64_DTPREL16_HIGHESTA:
8203           addend -= htab->tls_sec->vma + DTP_OFFSET;
8204           break;
8205
8206         case R_PPC64_DTPMOD64:
8207           relocation = 1;
8208           addend = 0;
8209           goto dodyn;
8210
8211         case R_PPC64_TPREL64:
8212           addend -= htab->tls_sec->vma + TP_OFFSET;
8213           goto dodyn;
8214
8215         case R_PPC64_DTPREL64:
8216           addend -= htab->tls_sec->vma + DTP_OFFSET;
8217           /* Fall thru */
8218
8219           /* Relocations that may need to be propagated if this is a
8220              dynamic object.  */
8221         case R_PPC64_REL30:
8222         case R_PPC64_REL32:
8223         case R_PPC64_REL64:
8224         case R_PPC64_ADDR14:
8225         case R_PPC64_ADDR14_BRNTAKEN:
8226         case R_PPC64_ADDR14_BRTAKEN:
8227         case R_PPC64_ADDR16:
8228         case R_PPC64_ADDR16_DS:
8229         case R_PPC64_ADDR16_HA:
8230         case R_PPC64_ADDR16_HI:
8231         case R_PPC64_ADDR16_HIGHER:
8232         case R_PPC64_ADDR16_HIGHERA:
8233         case R_PPC64_ADDR16_HIGHEST:
8234         case R_PPC64_ADDR16_HIGHESTA:
8235         case R_PPC64_ADDR16_LO:
8236         case R_PPC64_ADDR16_LO_DS:
8237         case R_PPC64_ADDR24:
8238         case R_PPC64_ADDR32:
8239         case R_PPC64_ADDR64:
8240         case R_PPC64_UADDR16:
8241         case R_PPC64_UADDR32:
8242         case R_PPC64_UADDR64:
8243           /* r_symndx will be zero only for relocs against symbols
8244              from removed linkonce sections, or sections discarded by
8245              a linker script.  */
8246         dodyn:
8247           if (r_symndx == 0)
8248             break;
8249           /* Fall thru.  */
8250
8251         dodyn2:
8252           if ((input_section->flags & SEC_ALLOC) == 0)
8253             break;
8254
8255           if (NO_OPD_RELOCS && is_opd)
8256             break;
8257
8258           if ((info->shared
8259                && (h == NULL
8260                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8261                    || h->root.type != bfd_link_hash_undefweak)
8262                && (MUST_BE_DYN_RELOC (r_type)
8263                    || !SYMBOL_CALLS_LOCAL (info, h)))
8264               || (ELIMINATE_COPY_RELOCS
8265                   && !info->shared
8266                   && h != NULL
8267                   && h->dynindx != -1
8268                   && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
8269                   && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
8270                   && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
8271             {
8272               Elf_Internal_Rela outrel;
8273               bfd_boolean skip, relocate;
8274               asection *sreloc;
8275               bfd_byte *loc;
8276               bfd_vma out_off;
8277
8278               /* When generating a dynamic object, these relocations
8279                  are copied into the output file to be resolved at run
8280                  time.  */
8281
8282               skip = FALSE;
8283               relocate = FALSE;
8284
8285               out_off = _bfd_elf_section_offset (output_bfd, info,
8286                                                  input_section, rel->r_offset);
8287               if (out_off == (bfd_vma) -1)
8288                 skip = TRUE;
8289               else if (out_off == (bfd_vma) -2)
8290                 skip = TRUE, relocate = TRUE;
8291               out_off += (input_section->output_section->vma
8292                           + input_section->output_offset);
8293               outrel.r_offset = out_off;
8294               outrel.r_addend = rel->r_addend;
8295
8296               /* Optimize unaligned reloc use.  */
8297               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
8298                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
8299                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
8300               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
8301                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
8302                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
8303               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
8304                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
8305                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
8306
8307               if (skip)
8308                 memset (&outrel, 0, sizeof outrel);
8309               else if (!SYMBOL_REFERENCES_LOCAL (info, h)
8310                        && !is_opd
8311                        && r_type != R_PPC64_TOC)
8312                 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
8313               else
8314                 {
8315                   /* This symbol is local, or marked to become local,
8316                      or this is an opd section reloc which must point
8317                      at a local function.  */
8318                   outrel.r_addend += relocation;
8319                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
8320                     {
8321                       if (is_opd && h != NULL)
8322                         {
8323                           /* Lie about opd entries.  This case occurs
8324                              when building shared libraries and we
8325                              reference a function in another shared
8326                              lib.  The same thing happens for a weak
8327                              definition in an application that's
8328                              overridden by a strong definition in a
8329                              shared lib.  (I believe this is a generic
8330                              bug in binutils handling of weak syms.)
8331                              In these cases we won't use the opd
8332                              entry in this lib.  */
8333                           unresolved_reloc = FALSE;
8334                         }
8335                       outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
8336
8337                       /* We need to relocate .opd contents for ld.so.
8338                          Prelink also wants simple and consistent rules
8339                          for relocs.  This make all RELATIVE relocs have
8340                          *r_offset equal to r_addend.  */
8341                       relocate = TRUE;
8342                     }
8343                   else
8344                     {
8345                       long indx = 0;
8346
8347                       if (bfd_is_abs_section (sec))
8348                         ;
8349                       else if (sec == NULL || sec->owner == NULL)
8350                         {
8351                           bfd_set_error (bfd_error_bad_value);
8352                           return FALSE;
8353                         }
8354                       else
8355                         {
8356                           asection *osec;
8357
8358                           osec = sec->output_section;
8359                           indx = elf_section_data (osec)->dynindx;
8360
8361                           /* We are turning this relocation into one
8362                              against a section symbol, so subtract out
8363                              the output section's address but not the
8364                              offset of the input section in the output
8365                              section.  */
8366                           outrel.r_addend -= osec->vma;
8367                         }
8368
8369                       outrel.r_info = ELF64_R_INFO (indx, r_type);
8370                     }
8371                 }
8372
8373               sreloc = elf_section_data (input_section)->sreloc;
8374               if (sreloc == NULL)
8375                 abort ();
8376
8377               loc = sreloc->contents;
8378               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
8379               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
8380
8381               /* If this reloc is against an external symbol, it will
8382                  be computed at runtime, so there's no need to do
8383                  anything now.  However, for the sake of prelink ensure
8384                  that the section contents are a known value.  */
8385               if (! relocate)
8386                 {
8387                   unresolved_reloc = FALSE;
8388                   /* The value chosen here is quite arbitrary as ld.so
8389                      ignores section contents except for the special
8390                      case of .opd where the contents might be accessed
8391                      before relocation.  Choose zero, as that won't
8392                      cause reloc overflow.  */
8393                   relocation = 0;
8394                   addend = 0;
8395                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
8396                      to improve backward compatibility with older
8397                      versions of ld.  */
8398                   if (r_type == R_PPC64_ADDR64)
8399                     addend = outrel.r_addend;
8400                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
8401                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
8402                     addend = (input_section->output_section->vma
8403                               + input_section->output_offset
8404                               + rel->r_offset);
8405                 }
8406             }
8407           break;
8408
8409         case R_PPC64_COPY:
8410         case R_PPC64_GLOB_DAT:
8411         case R_PPC64_JMP_SLOT:
8412         case R_PPC64_RELATIVE:
8413           /* We shouldn't ever see these dynamic relocs in relocatable
8414              files.  */
8415           /* Fall through.  */
8416
8417         case R_PPC64_PLTGOT16:
8418         case R_PPC64_PLTGOT16_DS:
8419         case R_PPC64_PLTGOT16_HA:
8420         case R_PPC64_PLTGOT16_HI:
8421         case R_PPC64_PLTGOT16_LO:
8422         case R_PPC64_PLTGOT16_LO_DS:
8423         case R_PPC64_PLTREL32:
8424         case R_PPC64_PLTREL64:
8425           /* These ones haven't been implemented yet.  */
8426
8427           (*_bfd_error_handler)
8428             (_("%s: relocation %s is not supported for symbol %s."),
8429              bfd_archive_filename (input_bfd),
8430              ppc64_elf_howto_table[r_type]->name, sym_name);
8431
8432           bfd_set_error (bfd_error_invalid_operation);
8433           ret = FALSE;
8434           continue;
8435         }
8436
8437       /* Do any further special processing.  */
8438       switch (r_type)
8439         {
8440         default:
8441           break;
8442
8443         case R_PPC64_ADDR16_HA:
8444         case R_PPC64_ADDR16_HIGHERA:
8445         case R_PPC64_ADDR16_HIGHESTA:
8446         case R_PPC64_GOT16_HA:
8447         case R_PPC64_PLTGOT16_HA:
8448         case R_PPC64_PLT16_HA:
8449         case R_PPC64_TOC16_HA:
8450         case R_PPC64_SECTOFF_HA:
8451         case R_PPC64_TPREL16_HA:
8452         case R_PPC64_DTPREL16_HA:
8453         case R_PPC64_GOT_TLSGD16_HA:
8454         case R_PPC64_GOT_TLSLD16_HA:
8455         case R_PPC64_GOT_TPREL16_HA:
8456         case R_PPC64_GOT_DTPREL16_HA:
8457         case R_PPC64_TPREL16_HIGHER:
8458         case R_PPC64_TPREL16_HIGHERA:
8459         case R_PPC64_TPREL16_HIGHEST:
8460         case R_PPC64_TPREL16_HIGHESTA:
8461         case R_PPC64_DTPREL16_HIGHER:
8462         case R_PPC64_DTPREL16_HIGHERA:
8463         case R_PPC64_DTPREL16_HIGHEST:
8464         case R_PPC64_DTPREL16_HIGHESTA:
8465           /* It's just possible that this symbol is a weak symbol
8466              that's not actually defined anywhere. In that case,
8467              'sec' would be NULL, and we should leave the symbol
8468              alone (it will be set to zero elsewhere in the link).  */
8469           if (sec != NULL)
8470             /* Add 0x10000 if sign bit in 0:15 is set.
8471                Bits 0:15 are not used.  */
8472             addend += 0x8000;
8473           break;
8474
8475         case R_PPC64_ADDR16_DS:
8476         case R_PPC64_ADDR16_LO_DS:
8477         case R_PPC64_GOT16_DS:
8478         case R_PPC64_GOT16_LO_DS:
8479         case R_PPC64_PLT16_LO_DS:
8480         case R_PPC64_SECTOFF_DS:
8481         case R_PPC64_SECTOFF_LO_DS:
8482         case R_PPC64_TOC16_DS:
8483         case R_PPC64_TOC16_LO_DS:
8484         case R_PPC64_PLTGOT16_DS:
8485         case R_PPC64_PLTGOT16_LO_DS:
8486         case R_PPC64_GOT_TPREL16_DS:
8487         case R_PPC64_GOT_TPREL16_LO_DS:
8488         case R_PPC64_GOT_DTPREL16_DS:
8489         case R_PPC64_GOT_DTPREL16_LO_DS:
8490         case R_PPC64_TPREL16_DS:
8491         case R_PPC64_TPREL16_LO_DS:
8492         case R_PPC64_DTPREL16_DS:
8493         case R_PPC64_DTPREL16_LO_DS:
8494           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
8495           mask = 3;
8496           /* If this reloc is against an lq insn, then the value must be
8497              a multiple of 16.  This is somewhat of a hack, but the
8498              "correct" way to do this by defining _DQ forms of all the
8499              _DS relocs bloats all reloc switches in this file.  It
8500              doesn't seem to make much sense to use any of these relocs
8501              in data, so testing the insn should be safe.  */
8502           if ((insn & (0x3f << 26)) == (56u << 26))
8503             mask = 15;
8504           if (((relocation + addend) & mask) != 0)
8505             {
8506               (*_bfd_error_handler)
8507                 (_("%s: error: relocation %s not a multiple of %d"),
8508                  bfd_archive_filename (input_bfd),
8509                  ppc64_elf_howto_table[r_type]->name,
8510                  mask + 1);
8511               bfd_set_error (bfd_error_bad_value);
8512               ret = FALSE;
8513               continue;
8514             }
8515           break;
8516
8517         case R_PPC64_REL14:
8518         case R_PPC64_REL14_BRNTAKEN:
8519         case R_PPC64_REL14_BRTAKEN:
8520           max_br_offset = 1 << 15;
8521           goto branch_check;
8522
8523         case R_PPC64_REL24:
8524           max_br_offset = 1 << 25;
8525
8526         branch_check:
8527           /* If the branch is out of reach or the TOC register needs
8528              adjusting, then redirect the call to the local stub for
8529              this function.  */
8530           from = (rel->r_offset
8531                   + input_section->output_offset
8532                   + input_section->output_section->vma);
8533           if ((relocation + addend - from + max_br_offset >= 2 * max_br_offset
8534                || (sec != NULL
8535                    && sec->output_section != NULL
8536                    && sec->id <= htab->top_id
8537                    && (htab->stub_group[sec->id].toc_off
8538                        != htab->stub_group[input_section->id].toc_off)))
8539               && (stub_entry = ppc_get_stub_entry (input_section, sec, h,
8540                                                    rel, htab)) != NULL)
8541             {
8542               /* Munge up the value and addend so that we call the stub
8543                  rather than the procedure directly.  */
8544               relocation = (stub_entry->stub_offset
8545                             + stub_entry->stub_sec->output_offset
8546                             + stub_entry->stub_sec->output_section->vma);
8547               addend = 0;
8548             }
8549           break;
8550         }
8551
8552       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
8553          because such sections are not SEC_ALLOC and thus ld.so will
8554          not process them.  */
8555       if (unresolved_reloc
8556           && !((input_section->flags & SEC_DEBUGGING) != 0
8557                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
8558         {
8559           (*_bfd_error_handler)
8560             (_("%s(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
8561              bfd_archive_filename (input_bfd),
8562              bfd_get_section_name (input_bfd, input_section),
8563              (long) rel->r_offset,
8564              ppc64_elf_howto_table[(int) r_type]->name,
8565              h->root.root.string);
8566           ret = FALSE;
8567         }
8568
8569       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
8570                                     input_bfd,
8571                                     input_section,
8572                                     contents,
8573                                     rel->r_offset,
8574                                     relocation,
8575                                     addend);
8576
8577       if (r != bfd_reloc_ok)
8578         {
8579           if (sym_name == NULL)
8580             sym_name = "(null)";
8581           if (r == bfd_reloc_overflow)
8582             {
8583               if (warned)
8584                 continue;
8585               if (h != NULL
8586                   && h->root.type == bfd_link_hash_undefweak
8587                   && ppc64_elf_howto_table[r_type]->pc_relative)
8588                 {
8589                   /* Assume this is a call protected by other code that
8590                      detects the symbol is undefined.  If this is the case,
8591                      we can safely ignore the overflow.  If not, the
8592                      program is hosed anyway, and a little warning isn't
8593                      going to help.  */
8594
8595                   continue;
8596                 }
8597
8598               if (!((*info->callbacks->reloc_overflow)
8599                     (info, sym_name, ppc64_elf_howto_table[r_type]->name,
8600                      rel->r_addend, input_bfd, input_section, rel->r_offset)))
8601                 return FALSE;
8602             }
8603           else
8604             {
8605               (*_bfd_error_handler)
8606                 (_("%s(%s+0x%lx): %s reloc against `%s': error %d"),
8607                  bfd_archive_filename (input_bfd),
8608                  bfd_get_section_name (input_bfd, input_section),
8609                  (long) rel->r_offset,
8610                  ppc64_elf_howto_table[r_type]->name,
8611                  sym_name,
8612                  (int) r);
8613               ret = FALSE;
8614             }
8615         }
8616     }
8617
8618   return ret;
8619 }
8620
8621 /* Finish up dynamic symbol handling.  We set the contents of various
8622    dynamic sections here.  */
8623
8624 static bfd_boolean
8625 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
8626                                  struct bfd_link_info *info,
8627                                  struct elf_link_hash_entry *h,
8628                                  Elf_Internal_Sym *sym)
8629 {
8630   struct ppc_link_hash_table *htab;
8631   bfd *dynobj;
8632
8633   htab = ppc_hash_table (info);
8634   dynobj = htab->elf.dynobj;
8635
8636   if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
8637     {
8638       struct plt_entry *ent;
8639       Elf_Internal_Rela rela;
8640       bfd_byte *loc;
8641
8642       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8643         if (ent->plt.offset != (bfd_vma) -1)
8644           {
8645             /* This symbol has an entry in the procedure linkage
8646                table.  Set it up.  */
8647
8648             if (htab->plt == NULL
8649                 || htab->relplt == NULL
8650                 || htab->glink == NULL)
8651               abort ();
8652
8653             /* Create a JMP_SLOT reloc to inform the dynamic linker to
8654                fill in the PLT entry.  */
8655             rela.r_offset = (htab->plt->output_section->vma
8656                              + htab->plt->output_offset
8657                              + ent->plt.offset);
8658             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
8659             rela.r_addend = ent->addend;
8660
8661             loc = htab->relplt->contents;
8662             loc += ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
8663                     * sizeof (Elf64_External_Rela));
8664             bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
8665           }
8666     }
8667
8668   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
8669     {
8670       Elf_Internal_Rela rela;
8671       bfd_byte *loc;
8672
8673       /* This symbol needs a copy reloc.  Set it up.  */
8674
8675       if (h->dynindx == -1
8676           || (h->root.type != bfd_link_hash_defined
8677               && h->root.type != bfd_link_hash_defweak)
8678           || htab->relbss == NULL)
8679         abort ();
8680
8681       rela.r_offset = (h->root.u.def.value
8682                        + h->root.u.def.section->output_section->vma
8683                        + h->root.u.def.section->output_offset);
8684       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
8685       rela.r_addend = 0;
8686       loc = htab->relbss->contents;
8687       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
8688       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
8689     }
8690
8691   /* Mark some specially defined symbols as absolute.  */
8692   if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
8693     sym->st_shndx = SHN_ABS;
8694
8695   return TRUE;
8696 }
8697
8698 /* Used to decide how to sort relocs in an optimal manner for the
8699    dynamic linker, before writing them out.  */
8700
8701 static enum elf_reloc_type_class
8702 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
8703 {
8704   enum elf_ppc64_reloc_type r_type;
8705
8706   r_type = ELF64_R_TYPE (rela->r_info);
8707   switch (r_type)
8708     {
8709     case R_PPC64_RELATIVE:
8710       return reloc_class_relative;
8711     case R_PPC64_JMP_SLOT:
8712       return reloc_class_plt;
8713     case R_PPC64_COPY:
8714       return reloc_class_copy;
8715     default:
8716       return reloc_class_normal;
8717     }
8718 }
8719
8720 /* Finish up the dynamic sections.  */
8721
8722 static bfd_boolean
8723 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
8724                                    struct bfd_link_info *info)
8725 {
8726   struct ppc_link_hash_table *htab;
8727   bfd *dynobj;
8728   asection *sdyn;
8729
8730   htab = ppc_hash_table (info);
8731   dynobj = htab->elf.dynobj;
8732   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8733
8734   if (htab->elf.dynamic_sections_created)
8735     {
8736       Elf64_External_Dyn *dyncon, *dynconend;
8737
8738       if (sdyn == NULL || htab->got == NULL)
8739         abort ();
8740
8741       dyncon = (Elf64_External_Dyn *) sdyn->contents;
8742       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
8743       for (; dyncon < dynconend; dyncon++)
8744         {
8745           Elf_Internal_Dyn dyn;
8746           asection *s;
8747
8748           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
8749
8750           switch (dyn.d_tag)
8751             {
8752             default:
8753               continue;
8754
8755             case DT_PPC64_GLINK:
8756               s = htab->glink;
8757               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8758               /* We stupidly defined DT_PPC64_GLINK to be the start
8759                  of glink rather than the first entry point, which is
8760                  what ld.so needs, and now have a bigger stub to
8761                  support automatic multiple TOCs.  */
8762               dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
8763               break;
8764
8765             case DT_PPC64_OPD:
8766               s = bfd_get_section_by_name (output_bfd, ".opd");
8767               if (s == NULL)
8768                 continue;
8769               dyn.d_un.d_ptr = s->vma;
8770               break;
8771
8772             case DT_PPC64_OPDSZ:
8773               s = bfd_get_section_by_name (output_bfd, ".opd");
8774               if (s == NULL)
8775                 continue;
8776               dyn.d_un.d_val = s->_raw_size;
8777               break;
8778
8779             case DT_PLTGOT:
8780               s = htab->plt;
8781               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8782               break;
8783
8784             case DT_JMPREL:
8785               s = htab->relplt;
8786               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8787               break;
8788
8789             case DT_PLTRELSZ:
8790               dyn.d_un.d_val = htab->relplt->_raw_size;
8791               break;
8792
8793             case DT_RELASZ:
8794               /* Don't count procedure linkage table relocs in the
8795                  overall reloc count.  */
8796               s = htab->relplt;
8797               if (s == NULL)
8798                 continue;
8799               dyn.d_un.d_val -= s->_raw_size;
8800               break;
8801
8802             case DT_RELA:
8803               /* We may not be using the standard ELF linker script.
8804                  If .rela.plt is the first .rela section, we adjust
8805                  DT_RELA to not include it.  */
8806               s = htab->relplt;
8807               if (s == NULL)
8808                 continue;
8809               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
8810                 continue;
8811               dyn.d_un.d_ptr += s->_raw_size;
8812               break;
8813             }
8814
8815           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
8816         }
8817     }
8818
8819   if (htab->got != NULL && htab->got->_raw_size != 0)
8820     {
8821       /* Fill in the first entry in the global offset table.
8822          We use it to hold the link-time TOCbase.  */
8823       bfd_put_64 (output_bfd,
8824                   elf_gp (output_bfd) + TOC_BASE_OFF,
8825                   htab->got->contents);
8826
8827       /* Set .got entry size.  */
8828       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
8829     }
8830
8831   if (htab->plt != NULL && htab->plt->_raw_size != 0)
8832     {
8833       /* Set .plt entry size.  */
8834       elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
8835         = PLT_ENTRY_SIZE;
8836     }
8837
8838   /* We need to handle writing out multiple GOT sections ourselves,
8839      since we didn't add them to DYNOBJ.  */
8840   while ((dynobj = dynobj->link_next) != NULL)
8841     {
8842       asection *s;
8843       s = ppc64_elf_tdata (dynobj)->got;
8844       if (s != NULL
8845           && s->_raw_size != 0
8846           && s->output_section != bfd_abs_section_ptr
8847           && !bfd_set_section_contents (output_bfd, s->output_section,
8848                                         s->contents, s->output_offset,
8849                                         s->_raw_size))
8850         return FALSE;
8851       s = ppc64_elf_tdata (dynobj)->relgot;
8852       if (s != NULL
8853           && s->_raw_size != 0
8854           && s->output_section != bfd_abs_section_ptr
8855           && !bfd_set_section_contents (output_bfd, s->output_section,
8856                                         s->contents, s->output_offset,
8857                                         s->_raw_size))
8858         return FALSE;
8859     }
8860
8861   return TRUE;
8862 }
8863
8864 #include "elf64-target.h"