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