Coverage Report

Created: 2026-09-14 08:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf64-ppc.c
Line
Count
Source
1
/* PowerPC64-specific support for 64-bit ELF.
2
   Copyright (C) 1999-2026 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.
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 3 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
   51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
22
23
24
/* The 64-bit PowerPC ELF ABI may be found at
25
   http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26
   http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
27
28
/* The assembler should generate a full set of section symbols even
29
   when they appear unused.  The linux kernel build tool recordmcount
30
   needs them.  */
31
#define TARGET_KEEP_UNUSED_SECTION_SYMBOLS true
32
33
#include "sysdep.h"
34
#include <stdarg.h>
35
#include "bfd.h"
36
#include "bfdlink.h"
37
#include "libbfd.h"
38
#include "elf-bfd.h"
39
#include "elf/ppc64.h"
40
#include "elf64-ppc.h"
41
#include "dwarf2.h"
42
43
/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
44
55
#define OCTETS_PER_BYTE(ABFD, SEC) 1
45
46
static bfd_reloc_status_type ppc64_elf_ha_reloc
47
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48
static bfd_reloc_status_type ppc64_elf_branch_reloc
49
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50
static bfd_reloc_status_type ppc64_elf_brtaken_reloc
51
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52
static bfd_reloc_status_type ppc64_elf_sectoff_reloc
53
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54
static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
55
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56
static bfd_reloc_status_type ppc64_elf_toc_reloc
57
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58
static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
59
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
60
static bfd_reloc_status_type ppc64_elf_toc64_reloc
61
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62
static bfd_reloc_status_type ppc64_elf_prefix_reloc
63
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64
static bfd_reloc_status_type ppc64_elf_unhandled_reloc
65
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
66
static bfd_vma opd_entry_value
67
  (asection *, bfd_vma, asection **, bfd_vma *, bool);
68
69
#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
70
#define TARGET_LITTLE_NAME  "elf64-powerpcle"
71
#define TARGET_BIG_SYM    powerpc_elf64_vec
72
#define TARGET_BIG_NAME   "elf64-powerpc"
73
#define ELF_ARCH    bfd_arch_powerpc
74
#define ELF_TARGET_ID   PPC64_ELF_DATA
75
#define ELF_MACHINE_CODE  EM_PPC64
76
#define ELF_MAXPAGESIZE   0x10000
77
#define ELF_COMMONPAGESIZE  0x1000
78
#define elf_info_to_howto ppc64_elf_info_to_howto
79
80
#define elf_backend_want_got_sym 0
81
#define elf_backend_want_plt_sym 0
82
#define elf_backend_plt_alignment 3
83
#define elf_backend_plt_not_loaded 1
84
#define elf_backend_got_header_size 8
85
#define elf_backend_want_dynrelro 1
86
#define elf_backend_want_stub_bfd 1
87
#define elf_backend_can_gc_sections 1
88
#define elf_backend_can_refcount 1
89
#define elf_backend_rela_normal 1
90
#define elf_backend_dtrel_excludes_plt 1
91
#define elf_backend_default_execstack 0
92
93
#define bfd_elf64_mkobject          ppc64_elf_mkobject
94
#define bfd_elf64_bfd_free_cached_info        ppc64_elf_free_cached_info
95
#define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
96
#define bfd_elf64_bfd_reloc_name_lookup       ppc64_elf_reloc_name_lookup
97
#define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
98
#define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
99
#define bfd_elf64_new_section_hook        ppc64_elf_new_section_hook
100
#define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
101
#define bfd_elf64_get_synthetic_symtab        ppc64_elf_get_synthetic_symtab
102
#define bfd_elf64_bfd_link_just_syms        ppc64_elf_link_just_syms
103
#define bfd_elf64_bfd_gc_sections       ppc64_elf_gc_sections
104
105
#define elf_backend_object_p          ppc64_elf_object_p
106
#define elf_backend_grok_prstatus       ppc64_elf_grok_prstatus
107
#define elf_backend_grok_psinfo         ppc64_elf_grok_psinfo
108
#define elf_backend_write_core_note       ppc64_elf_write_core_note
109
#define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
110
#define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
111
#define elf_backend_add_symbol_hook       ppc64_elf_add_symbol_hook
112
#define elf_backend_check_directives        ppc64_elf_before_check_relocs
113
#define elf_backend_notice_as_needed        ppc64_elf_notice_as_needed
114
#define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
115
#define elf_backend_check_relocs        ppc64_elf_check_relocs
116
#define elf_backend_relocs_compatible       _bfd_elf_relocs_compatible
117
#define elf_backend_gc_keep         ppc64_elf_gc_keep
118
#define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
119
#define elf_backend_gc_mark_hook        ppc64_elf_gc_mark_hook
120
#define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
121
#define elf_backend_hide_symbol         ppc64_elf_hide_symbol
122
#define elf_backend_maybe_function_sym        ppc64_elf_maybe_function_sym
123
#define elf_backend_early_size_sections       ppc64_elf_edit
124
#define elf_backend_late_size_sections        ppc64_elf_late_size_sections
125
#define elf_backend_hash_symbol         ppc64_elf_hash_symbol
126
#define elf_backend_init_index_section        _bfd_elf_init_2_index_sections
127
#define elf_backend_action_discarded        ppc64_elf_action_discarded
128
#define elf_backend_relocate_section        ppc64_elf_relocate_section
129
#define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
130
#define elf_backend_reloc_type_class        ppc64_elf_reloc_type_class
131
#define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
132
#define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
133
#define elf_backend_special_sections        ppc64_elf_special_sections
134
#define elf_backend_section_flags       ppc64_elf_section_flags
135
#define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
136
#define elf_backend_merge_symbol        ppc64_elf_merge_symbol
137
#define elf_backend_get_reloc_section       bfd_get_section_by_name
138
139
/* The name of the dynamic interpreter.  This is put in the .interp
140
   section.  */
141
0
#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
142
143
/* The size in bytes of an entry in the procedure linkage table.  */
144
0
#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
145
0
#define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
146
147
/* The initial size of the plt reserved for the dynamic linker.  */
148
0
#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
149
150
/* Offsets to some stack save slots.  */
151
#define STK_LR 16
152
0
#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
153
/* This one is dodgy.  ELFv2 does not have a linker word, so use the
154
   CR save slot.  Used only by optimised __tls_get_addr call stub,
155
   relying on __tls_get_addr_opt not saving CR..  */
156
0
#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
157
158
/* TOC base pointers offset from start of TOC.  */
159
27
#define TOC_BASE_OFF  0x8000
160
/* TOC base alignment.  */
161
28
#define TOC_BASE_ALIGN  256
162
163
/* Offset of tp and dtp pointers from start of TLS block.  */
164
0
#define TP_OFFSET 0x7000
165
0
#define DTP_OFFSET  0x8000
166
167
/* .plt call stub instructions.  The normal stub is like this, but
168
   sometimes the .plt entry crosses a 64k boundary and we need to
169
   insert an addi to adjust r11.  */
170
#define STD_R2_0R1  0xf8410000  /* std   %r2,0+40(%r1)       */
171
#define ADDIS_R11_R2  0x3d620000  /* addis %r11,%r2,xxx@ha     */
172
#define LD_R12_0R11 0xe98b0000  /* ld  %r12,xxx+0@l(%r11)  */
173
#define MTCTR_R12 0x7d8903a6  /* mtctr %r12        */
174
#define LD_R2_0R11  0xe84b0000  /* ld  %r2,xxx+8@l(%r11)   */
175
#define LD_R11_0R11 0xe96b0000  /* ld  %r11,xxx+16@l(%r11) */
176
#define BCTR    0x4e800420  /* bctr          */
177
178
#define ADDI_R11_R11  0x396b0000  /* addi %r11,%r11,off@l  */
179
#define ADDI_R12_R11  0x398b0000  /* addi %r12,%r11,off@l  */
180
#define ADDI_R12_R12  0x398c0000  /* addi %r12,%r12,off@l  */
181
#define ADDIS_R2_R2 0x3c420000  /* addis %r2,%r2,off@ha  */
182
0
#define ADDI_R2_R2  0x38420000  /* addi  %r2,%r2,off@l   */
183
184
#define XOR_R2_R12_R12  0x7d826278  /* xor   %r2,%r12,%r12   */
185
#define ADD_R11_R11_R2  0x7d6b1214  /* add   %r11,%r11,%r2   */
186
#define XOR_R11_R12_R12 0x7d8b6278  /* xor   %r11,%r12,%r12  */
187
#define ADD_R2_R2_R11 0x7c425a14  /* add   %r2,%r2,%r11  */
188
#define CMPLDI_R2_0 0x28220000  /* cmpldi %r2,0    */
189
#define BNECTR    0x4ca20420  /* bnectr+     */
190
#define BNECTR_P4 0x4ce20420  /* bnectr+     */
191
192
#define LD_R12_0R2  0xe9820000  /* ld  %r12,xxx+0(%r2) */
193
#define LD_R11_0R2  0xe9620000  /* ld  %r11,xxx+0(%r2) */
194
#define LD_R2_0R2 0xe8420000  /* ld  %r2,xxx+0(%r2)  */
195
196
0
#define LD_R2_0R1 0xe8410000  /* ld  %r2,0(%r1)  */
197
0
#define LD_R2_0R12  0xe84c0000  /* ld  %r2,0(%r12)   */
198
0
#define ADD_R2_R2_R12 0x7c426214  /* add   %r2,%r2,%r12  */
199
200
#define LI_R11_0  0x39600000  /* li    %r11,0   */
201
#define LIS_R2    0x3c400000  /* lis %r2,xxx@ha   */
202
#define LIS_R11   0x3d600000  /* lis %r11,xxx@ha    */
203
#define LIS_R12   0x3d800000  /* lis %r12,xxx@ha    */
204
0
#define ADDIS_R2_R12  0x3c4c0000  /* addis %r2,%r12,xxx@ha  */
205
#define ADDIS_R12_R2  0x3d820000  /* addis %r12,%r2,xxx@ha  */
206
#define ADDIS_R12_R11 0x3d8b0000  /* addis %r12,%r11,xxx@ha */
207
#define ADDIS_R12_R12 0x3d8c0000  /* addis %r12,%r12,xxx@ha */
208
#define ORIS_R12_R12_0  0x658c0000  /* oris  %r12,%r12,xxx@hi */
209
#define ORI_R11_R11_0 0x616b0000  /* ori   %r11,%r11,xxx@l  */
210
#define ORI_R12_R12_0 0x618c0000  /* ori   %r12,%r12,xxx@l  */
211
#define LD_R12_0R12 0xe98c0000  /* ld  %r12,xxx@l(%r12) */
212
#define SLDI_R11_R11_34 0x796b1746  /* sldi  %r11,%r11,34     */
213
#define SLDI_R12_R12_32 0x799c07c6  /* sldi  %r12,%r12,32     */
214
#define LDX_R12_R11_R12 0x7d8b602a  /* ldx   %r12,%r11,%r12   */
215
#define ADD_R12_R11_R12 0x7d8b6214  /* add   %r12,%r11,%r12   */
216
0
#define PADDI_R12_PC  0x0610000039800000ULL
217
0
#define PLD_R12_PC  0x04100000e5800000ULL
218
0
#define PNOP    0x0700000000000000ULL
219
220
/* __glink_PLTresolve stub instructions.  We enter with the index in
221
   R0 for ELFv1, and the address of a glink branch in R12 for ELFv2.  */
222
#define GLINK_PLTRESOLVE_SIZE(htab)     \
223
0
  (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
224
          /* 0:       */
225
          /*  .quad plt0-1f   */
226
          /* __glink:     */
227
#define MFLR_R12  0x7d8802a6  /*  mflr %12      */
228
#define BCL_20_31 0x429f0005  /*  bcl 20,31,1f    */
229
          /* 1:       */
230
#define MFLR_R11  0x7d6802a6  /*  mflr %11      */
231
          /*  ld %2,(0b-1b)(%11)    */
232
#define MTLR_R12  0x7d8803a6  /*  mtlr %12      */
233
#define ADD_R11_R2_R11  0x7d625a14  /*  add %11,%2,%11    */
234
          /*  ld %12,0(%11)   */
235
          /*  ld %2,8(%11)    */
236
          /*  mtctr %12     */
237
          /*  ld %11,16(%11)    */
238
          /*  bctr      */
239
240
#define MFLR_R0   0x7c0802a6  /* mflr %r0     */
241
#define MTLR_R0   0x7c0803a6  /* mtlr %r0     */
242
#define SUB_R12_R12_R11 0x7d8b6050  /* subf %r12,%r11,%r12    */
243
#define ADDI_R0_R12 0x380c0000  /* addi %r0,%r12,0    */
244
#define SRDI_R0_R0_2  0x7800f082  /* rldicl %r0,%r0,62,2    */
245
0
#define LD_R0_0R11  0xe80b0000  /* ld %r0,0(%r11)   */
246
#define ADD_R11_R0_R11  0x7d605a14  /* add %r11,%r0,%r11    */
247
248
/* Pad with this.  */
249
0
#define NOP   0x60000000
250
251
/* Some other nops.  */
252
0
#define CROR_151515 0x4def7b82
253
0
#define CROR_313131 0x4ffffb82
254
255
/* .glink entries for the first 32k functions are two instructions.  */
256
#define LI_R0_0   0x38000000  /* li    %r0,0    */
257
0
#define B_DOT   0x48000000  /* b     .    */
258
259
/* After that, we need two instructions to load the index, followed by
260
   a branch.  */
261
#define LIS_R0_0  0x3c000000  /* lis   %r0,0    */
262
#define ORI_R0_R0_0 0x60000000  /* ori   %r0,%r0,0  */
263
264
/* Instructions used by the save and restore reg functions.  */
265
#define STD_R0_0R1  0xf8010000  /* std   %r0,0(%r1) */
266
#define STD_R0_0R12 0xf80c0000  /* std   %r0,0(%r12)  */
267
#define LD_R0_0R1 0xe8010000  /* ld    %r0,0(%r1) */
268
#define LD_R0_0R12  0xe80c0000  /* ld    %r0,0(%r12)  */
269
#define STFD_FR0_0R1  0xd8010000  /* stfd  %fr0,0(%r1)  */
270
#define LFD_FR0_0R1 0xc8010000  /* lfd   %fr0,0(%r1)  */
271
#define LI_R12_0  0x39800000  /* li    %r12,0   */
272
#define STVX_VR0_R12_R0 0x7c0c01ce  /* stvx  %v0,%r12,%r0 */
273
#define LVX_VR0_R12_R0  0x7c0c00ce  /* lvx   %v0,%r12,%r0 */
274
#define MTLR_R0   0x7c0803a6  /* mtlr  %r0    */
275
#define BLR   0x4e800020  /* blr      */
276
277
/* Since .opd is an array of descriptors and each entry will end up
278
   with identical R_PPC64_RELATIVE relocs, there is really no need to
279
   propagate .opd relocs;  The dynamic linker should be taught to
280
   relocate .opd without reloc entries.  */
281
#ifndef NO_OPD_RELOCS
282
0
#define NO_OPD_RELOCS 0
283
#endif
284
285
#ifndef ARRAY_SIZE
286
5.19k
#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
287
#endif
288
289
static inline int
290
abiversion (bfd *abfd)
291
26
{
292
26
  return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
293
26
}
294
295
static inline void
296
set_abiversion (bfd *abfd, int ver)
297
0
{
298
0
  elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
299
0
  elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
300
0
}
301
302
#define is_ppc64_elf(bfd) \
303
181
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
304
181
   && elf_object_id (bfd) == PPC64_ELF_DATA)
305

306
/* Relocation HOWTO's.  */
307
/* Like other ELF RELA targets that don't apply multiple
308
   field-altering relocations to the same localation, src_mask is
309
   always zero and pcrel_offset is the same as pc_relative.
310
   PowerPC can always use a zero bitpos, even when the field is not at
311
   the LSB.  For example, a REL24 could use rightshift=2, bisize=24
312
   and bitpos=2 which matches the ABI description, or as we do here,
313
   rightshift=0, bitsize=26 and bitpos=0.  */
314
#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
315
      complain, special_func)       \
316
  HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
317
   complain_overflow_ ## complain, special_func,    \
318
   #type, false, 0, mask, pc_relative)
319
320
static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
321
322
static reloc_howto_type ppc64_elf_howto_raw[] =
323
{
324
  /* This reloc does nothing.  */
325
  HOW (R_PPC64_NONE, 0, 0, 0, 0, false, dont,
326
       bfd_elf_generic_reloc),
327
328
  /* A standard 32 bit relocation.  */
329
  HOW (R_PPC64_ADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
330
       bfd_elf_generic_reloc),
331
332
  /* An absolute 26 bit branch; the lower two bits must be zero.
333
     FIXME: we don't check that, we just clear them.  */
334
  HOW (R_PPC64_ADDR24, 4, 26, 0x03fffffc, 0, false, bitfield,
335
       bfd_elf_generic_reloc),
336
337
  /* A standard 16 bit relocation.  */
338
  HOW (R_PPC64_ADDR16, 2, 16, 0xffff, 0, false, bitfield,
339
       bfd_elf_generic_reloc),
340
341
  /* A 16 bit relocation without overflow.  */
342
  HOW (R_PPC64_ADDR16_LO, 2, 16, 0xffff, 0, false, dont,
343
       bfd_elf_generic_reloc),
344
345
  /* Bits 16-31 of an address.  */
346
  HOW (R_PPC64_ADDR16_HI, 2, 16, 0xffff, 16, false, signed,
347
       bfd_elf_generic_reloc),
348
349
  /* Bits 16-31 of an address, plus 1 if the contents of the low 16
350
     bits, treated as a signed number, is negative.  */
351
  HOW (R_PPC64_ADDR16_HA, 2, 16, 0xffff, 16, false, signed,
352
       ppc64_elf_ha_reloc),
353
354
  /* An absolute 16 bit branch; the lower two bits must be zero.
355
     FIXME: we don't check that, we just clear them.  */
356
  HOW (R_PPC64_ADDR14, 4, 16, 0x0000fffc, 0, false, signed,
357
       ppc64_elf_branch_reloc),
358
359
  /* An absolute 16 bit branch, for which bit 10 should be set to
360
     indicate that the branch is expected to be taken.  The lower two
361
     bits must be zero.  */
362
  HOW (R_PPC64_ADDR14_BRTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
363
       ppc64_elf_brtaken_reloc),
364
365
  /* An absolute 16 bit branch, for which bit 10 should be set to
366
     indicate that the branch is not expected to be taken.  The lower
367
     two bits must be zero.  */
368
  HOW (R_PPC64_ADDR14_BRNTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
369
       ppc64_elf_brtaken_reloc),
370
371
  /* A relative 26 bit branch; the lower two bits must be zero.  */
372
  HOW (R_PPC64_REL24, 4, 26, 0x03fffffc, 0, true, signed,
373
       ppc64_elf_branch_reloc),
374
375
  /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer.  */
376
  HOW (R_PPC64_REL24_NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
377
       ppc64_elf_branch_reloc),
378
379
  /* Another variant, when p10 insns can't be used on stubs.  */
380
  HOW (R_PPC64_REL24_P9NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
381
       ppc64_elf_branch_reloc),
382
383
  /* A relative 16 bit branch; the lower two bits must be zero.  */
384
  HOW (R_PPC64_REL14, 4, 16, 0x0000fffc, 0, true, signed,
385
       ppc64_elf_branch_reloc),
386
387
  /* A relative 16 bit branch.  Bit 10 should be set to indicate that
388
     the branch is expected to be taken.  The lower two bits must be
389
     zero.  */
390
  HOW (R_PPC64_REL14_BRTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
391
       ppc64_elf_brtaken_reloc),
392
393
  /* A relative 16 bit branch.  Bit 10 should be set to indicate that
394
     the branch is not expected to be taken.  The lower two bits must
395
     be zero.  */
396
  HOW (R_PPC64_REL14_BRNTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
397
       ppc64_elf_brtaken_reloc),
398
399
  /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
400
     symbol.  */
401
  HOW (R_PPC64_GOT16, 2, 16, 0xffff, 0, false, signed,
402
       ppc64_elf_unhandled_reloc),
403
404
  /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
405
     the symbol.  */
406
  HOW (R_PPC64_GOT16_LO, 2, 16, 0xffff, 0, false, dont,
407
       ppc64_elf_unhandled_reloc),
408
409
  /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
410
     the symbol.  */
411
  HOW (R_PPC64_GOT16_HI, 2, 16, 0xffff, 16, false, signed,
412
       ppc64_elf_unhandled_reloc),
413
414
  /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
415
     the symbol.  */
416
  HOW (R_PPC64_GOT16_HA, 2, 16, 0xffff, 16, false, signed,
417
       ppc64_elf_unhandled_reloc),
418
419
  /* This is used only by the dynamic linker.  The symbol should exist
420
     both in the object being run and in some shared library.  The
421
     dynamic linker copies the data addressed by the symbol from the
422
     shared library into the object, because the object being
423
     run has to have the data at some particular address.  */
424
  HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
425
       ppc64_elf_unhandled_reloc),
426
427
  /* Like R_PPC64_ADDR64, but used when setting global offset table
428
     entries.  */
429
  HOW (R_PPC64_GLOB_DAT, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
430
       ppc64_elf_unhandled_reloc),
431
432
  /* Created by the link editor.  Marks a procedure linkage table
433
     entry for a symbol.  */
434
  HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
435
       ppc64_elf_unhandled_reloc),
436
437
  /* Used only by the dynamic linker.  When the object is run, this
438
     doubleword64 is set to the load address of the object, plus the
439
     addend.  */
440
  HOW (R_PPC64_RELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
441
       bfd_elf_generic_reloc),
442
443
  /* Like R_PPC64_ADDR32, but may be unaligned.  */
444
  HOW (R_PPC64_UADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
445
       bfd_elf_generic_reloc),
446
447
  /* Like R_PPC64_ADDR16, but may be unaligned.  */
448
  HOW (R_PPC64_UADDR16, 2, 16, 0xffff, 0, false, bitfield,
449
       bfd_elf_generic_reloc),
450
451
  /* 32-bit PC relative.  */
452
  HOW (R_PPC64_REL32, 4, 32, 0xffffffff, 0, true, signed,
453
       bfd_elf_generic_reloc),
454
455
  /* 32-bit relocation to the symbol's procedure linkage table.  */
456
  HOW (R_PPC64_PLT32, 4, 32, 0xffffffff, 0, false, bitfield,
457
       ppc64_elf_unhandled_reloc),
458
459
  /* 32-bit PC relative relocation to the symbol's procedure linkage table.
460
     FIXME: R_PPC64_PLTREL32 not supported.  */
461
  HOW (R_PPC64_PLTREL32, 4, 32, 0xffffffff, 0, true, signed,
462
       ppc64_elf_unhandled_reloc),
463
464
  /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
465
     the symbol.  */
466
  HOW (R_PPC64_PLT16_LO, 2, 16, 0xffff, 0, false, dont,
467
       ppc64_elf_unhandled_reloc),
468
469
  /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
470
     the symbol.  */
471
  HOW (R_PPC64_PLT16_HI, 2, 16, 0xffff, 16, false, signed,
472
       ppc64_elf_unhandled_reloc),
473
474
  /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
475
     the symbol.  */
476
  HOW (R_PPC64_PLT16_HA, 2, 16, 0xffff, 16, false, signed,
477
       ppc64_elf_unhandled_reloc),
478
479
  /* 16-bit section relative relocation.  */
480
  HOW (R_PPC64_SECTOFF, 2, 16, 0xffff, 0, false, signed,
481
       ppc64_elf_sectoff_reloc),
482
483
  /* Like R_PPC64_SECTOFF, but no overflow warning.  */
484
  HOW (R_PPC64_SECTOFF_LO, 2, 16, 0xffff, 0, false, dont,
485
       ppc64_elf_sectoff_reloc),
486
487
  /* 16-bit upper half section relative relocation.  */
488
  HOW (R_PPC64_SECTOFF_HI, 2, 16, 0xffff, 16, false, signed,
489
       ppc64_elf_sectoff_reloc),
490
491
  /* 16-bit upper half adjusted section relative relocation.  */
492
  HOW (R_PPC64_SECTOFF_HA, 2, 16, 0xffff, 16, false, signed,
493
       ppc64_elf_sectoff_ha_reloc),
494
495
  /* Like R_PPC64_REL24 without touching the two least significant bits.  */
496
  HOW (R_PPC64_REL30, 4, 30, 0xfffffffc, 2, true, dont,
497
       bfd_elf_generic_reloc),
498
499
  /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
500
501
  /* A standard 64-bit relocation.  */
502
  HOW (R_PPC64_ADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
503
       bfd_elf_generic_reloc),
504
505
  /* The bits 32-47 of an address.  */
506
  HOW (R_PPC64_ADDR16_HIGHER, 2, 16, 0xffff, 32, false, dont,
507
       bfd_elf_generic_reloc),
508
509
  /* The bits 32-47 of an address, plus 1 if the contents of the low
510
     16 bits, treated as a signed number, is negative.  */
511
  HOW (R_PPC64_ADDR16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
512
       ppc64_elf_ha_reloc),
513
514
  /* The bits 48-63 of an address.  */
515
  HOW (R_PPC64_ADDR16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
516
       bfd_elf_generic_reloc),
517
518
  /* The bits 48-63 of an address, plus 1 if the contents of the low
519
     16 bits, treated as a signed number, is negative.  */
520
  HOW (R_PPC64_ADDR16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
521
       ppc64_elf_ha_reloc),
522
523
  /* Like ADDR64, but may be unaligned.  */
524
  HOW (R_PPC64_UADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
525
       bfd_elf_generic_reloc),
526
527
  /* 64-bit relative relocation.  */
528
  HOW (R_PPC64_REL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
529
       bfd_elf_generic_reloc),
530
531
  /* 64-bit relocation to the symbol's procedure linkage table.  */
532
  HOW (R_PPC64_PLT64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
533
       ppc64_elf_unhandled_reloc),
534
535
  /* 64-bit PC relative relocation to the symbol's procedure linkage
536
     table.  */
537
  /* FIXME: R_PPC64_PLTREL64 not supported.  */
538
  HOW (R_PPC64_PLTREL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
539
       ppc64_elf_unhandled_reloc),
540
541
  /* 16 bit TOC-relative relocation.  */
542
  /* R_PPC64_TOC16    47     half16*  S + A - .TOC.  */
543
  HOW (R_PPC64_TOC16, 2, 16, 0xffff, 0, false, signed,
544
       ppc64_elf_toc_reloc),
545
546
  /* 16 bit TOC-relative relocation without overflow.  */
547
  /* R_PPC64_TOC16_LO   48     half16  #lo (S + A - .TOC.)  */
548
  HOW (R_PPC64_TOC16_LO, 2, 16, 0xffff, 0, false, dont,
549
       ppc64_elf_toc_reloc),
550
551
  /* 16 bit TOC-relative relocation, high 16 bits.  */
552
  /* R_PPC64_TOC16_HI   49     half16  #hi (S + A - .TOC.)  */
553
  HOW (R_PPC64_TOC16_HI, 2, 16, 0xffff, 16, false, signed,
554
       ppc64_elf_toc_reloc),
555
556
  /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
557
     contents of the low 16 bits, treated as a signed number, is
558
     negative.  */
559
  /* R_PPC64_TOC16_HA   50     half16  #ha (S + A - .TOC.)  */
560
  HOW (R_PPC64_TOC16_HA, 2, 16, 0xffff, 16, false, signed,
561
       ppc64_elf_toc_ha_reloc),
562
563
  /* 64-bit relocation; insert value of TOC base (.TOC.).  */
564
  /* R_PPC64_TOC      51     doubleword64  .TOC.  */
565
  HOW (R_PPC64_TOC, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
566
       ppc64_elf_toc64_reloc),
567
568
  /* Like R_PPC64_GOT16, but also informs the link editor that the
569
     value to relocate may (!) refer to a PLT entry which the link
570
     editor (a) may replace with the symbol value.  If the link editor
571
     is unable to fully resolve the symbol, it may (b) create a PLT
572
     entry and store the address to the new PLT entry in the GOT.
573
     This permits lazy resolution of function symbols at run time.
574
     The link editor may also skip all of this and just (c) emit a
575
     R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
576
  /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
577
    HOW (R_PPC64_PLTGOT16, 2, 16, 0xffff, 0, false,signed,
578
    ppc64_elf_unhandled_reloc),
579
580
  /* Like R_PPC64_PLTGOT16, but without overflow.  */
581
  /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
582
  HOW (R_PPC64_PLTGOT16_LO, 2, 16, 0xffff, 0, false, dont,
583
       ppc64_elf_unhandled_reloc),
584
585
  /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
586
  /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
587
  HOW (R_PPC64_PLTGOT16_HI, 2, 16, 0xffff, 16, false, signed,
588
       ppc64_elf_unhandled_reloc),
589
590
  /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
591
     1 if the contents of the low 16 bits, treated as a signed number,
592
     is negative.  */
593
  /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
594
  HOW (R_PPC64_PLTGOT16_HA, 2, 16, 0xffff, 16, false, signed,
595
       ppc64_elf_unhandled_reloc),
596
597
  /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
598
  HOW (R_PPC64_ADDR16_DS, 2, 16, 0xfffc, 0, false, signed,
599
       bfd_elf_generic_reloc),
600
601
  /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
602
  HOW (R_PPC64_ADDR16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
603
       bfd_elf_generic_reloc),
604
605
  /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
606
  HOW (R_PPC64_GOT16_DS, 2, 16, 0xfffc, 0, false, signed,
607
       ppc64_elf_unhandled_reloc),
608
609
  /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
610
  HOW (R_PPC64_GOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
611
       ppc64_elf_unhandled_reloc),
612
613
  /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
614
  HOW (R_PPC64_PLT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
615
       ppc64_elf_unhandled_reloc),
616
617
  /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
618
  HOW (R_PPC64_SECTOFF_DS, 2, 16, 0xfffc, 0, false, signed,
619
       ppc64_elf_sectoff_reloc),
620
621
  /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
622
  HOW (R_PPC64_SECTOFF_LO_DS, 2, 16, 0xfffc, 0, false, dont,
623
       ppc64_elf_sectoff_reloc),
624
625
  /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
626
  HOW (R_PPC64_TOC16_DS, 2, 16, 0xfffc, 0, false, signed,
627
       ppc64_elf_toc_reloc),
628
629
  /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
630
  HOW (R_PPC64_TOC16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
631
       ppc64_elf_toc_reloc),
632
633
  /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
634
  /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
635
  HOW (R_PPC64_PLTGOT16_DS, 2, 16, 0xfffc, 0, false, signed,
636
       ppc64_elf_unhandled_reloc),
637
638
  /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
639
  /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
640
  HOW (R_PPC64_PLTGOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
641
       ppc64_elf_unhandled_reloc),
642
643
  /* Marker relocs for TLS.  */
644
  HOW (R_PPC64_TLS, 4, 32, 0, 0, false, dont,
645
       bfd_elf_generic_reloc),
646
647
  HOW (R_PPC64_TLSGD, 4, 32, 0, 0, false, dont,
648
       bfd_elf_generic_reloc),
649
650
  HOW (R_PPC64_TLSLD, 4, 32, 0, 0, false, dont,
651
       bfd_elf_generic_reloc),
652
653
  /* Marker reloc for optimizing r2 save in prologue rather than on
654
     each plt call stub.  */
655
  HOW (R_PPC64_TOCSAVE, 4, 32, 0, 0, false, dont,
656
       bfd_elf_generic_reloc),
657
658
  /* Marker relocs on inline plt call instructions.  */
659
  HOW (R_PPC64_PLTSEQ, 4, 32, 0, 0, false, dont,
660
       bfd_elf_generic_reloc),
661
662
  HOW (R_PPC64_PLTCALL, 4, 32, 0, 0, false, dont,
663
       bfd_elf_generic_reloc),
664
665
  /* Computes the load module index of the load module that contains the
666
     definition of its TLS sym.  */
667
  HOW (R_PPC64_DTPMOD64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
668
       ppc64_elf_unhandled_reloc),
669
670
  /* Computes a dtv-relative displacement, the difference between the value
671
     of sym+add and the base address of the thread-local storage block that
672
     contains the definition of sym, minus 0x8000.  */
673
  HOW (R_PPC64_DTPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
674
       ppc64_elf_unhandled_reloc),
675
676
  /* A 16 bit dtprel reloc.  */
677
  HOW (R_PPC64_DTPREL16, 2, 16, 0xffff, 0, false, signed,
678
       ppc64_elf_unhandled_reloc),
679
680
  /* Like DTPREL16, but no overflow.  */
681
  HOW (R_PPC64_DTPREL16_LO, 2, 16, 0xffff, 0, false, dont,
682
       ppc64_elf_unhandled_reloc),
683
684
  /* Like DTPREL16_LO, but next higher group of 16 bits.  */
685
  HOW (R_PPC64_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
686
       ppc64_elf_unhandled_reloc),
687
688
  /* Like DTPREL16_HI, but adjust for low 16 bits.  */
689
  HOW (R_PPC64_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
690
       ppc64_elf_unhandled_reloc),
691
692
  /* Like DTPREL16_HI, but next higher group of 16 bits.  */
693
  HOW (R_PPC64_DTPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
694
       ppc64_elf_unhandled_reloc),
695
696
  /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
697
  HOW (R_PPC64_DTPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
698
       ppc64_elf_unhandled_reloc),
699
700
  /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
701
  HOW (R_PPC64_DTPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
702
       ppc64_elf_unhandled_reloc),
703
704
  /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
705
  HOW (R_PPC64_DTPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
706
       ppc64_elf_unhandled_reloc),
707
708
  /* Like DTPREL16, but for insns with a DS field.  */
709
  HOW (R_PPC64_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
710
       ppc64_elf_unhandled_reloc),
711
712
  /* Like DTPREL16_DS, but no overflow.  */
713
  HOW (R_PPC64_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
714
       ppc64_elf_unhandled_reloc),
715
716
  /* Computes a tp-relative displacement, the difference between the value of
717
     sym+add and the value of the thread pointer (r13).  */
718
  HOW (R_PPC64_TPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
719
       ppc64_elf_unhandled_reloc),
720
721
  /* A 16 bit tprel reloc.  */
722
  HOW (R_PPC64_TPREL16, 2, 16, 0xffff, 0, false, signed,
723
       ppc64_elf_unhandled_reloc),
724
725
  /* Like TPREL16, but no overflow.  */
726
  HOW (R_PPC64_TPREL16_LO, 2, 16, 0xffff, 0, false, dont,
727
       ppc64_elf_unhandled_reloc),
728
729
  /* Like TPREL16_LO, but next higher group of 16 bits.  */
730
  HOW (R_PPC64_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
731
       ppc64_elf_unhandled_reloc),
732
733
  /* Like TPREL16_HI, but adjust for low 16 bits.  */
734
  HOW (R_PPC64_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
735
       ppc64_elf_unhandled_reloc),
736
737
  /* Like TPREL16_HI, but next higher group of 16 bits.  */
738
  HOW (R_PPC64_TPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
739
       ppc64_elf_unhandled_reloc),
740
741
  /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
742
  HOW (R_PPC64_TPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
743
       ppc64_elf_unhandled_reloc),
744
745
  /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
746
  HOW (R_PPC64_TPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
747
       ppc64_elf_unhandled_reloc),
748
749
  /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
750
  HOW (R_PPC64_TPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
751
       ppc64_elf_unhandled_reloc),
752
753
  /* Like TPREL16, but for insns with a DS field.  */
754
  HOW (R_PPC64_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
755
       ppc64_elf_unhandled_reloc),
756
757
  /* Like TPREL16_DS, but no overflow.  */
758
  HOW (R_PPC64_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
759
       ppc64_elf_unhandled_reloc),
760
761
  /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
762
     with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
763
     to the first entry relative to the TOC base (r2).  */
764
  HOW (R_PPC64_GOT_TLSGD16, 2, 16, 0xffff, 0, false, signed,
765
       ppc64_elf_unhandled_reloc),
766
767
  /* Like GOT_TLSGD16, but no overflow.  */
768
  HOW (R_PPC64_GOT_TLSGD16_LO, 2, 16, 0xffff, 0, false, dont,
769
       ppc64_elf_unhandled_reloc),
770
771
  /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
772
  HOW (R_PPC64_GOT_TLSGD16_HI, 2, 16, 0xffff, 16, false, signed,
773
       ppc64_elf_unhandled_reloc),
774
775
  /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
776
  HOW (R_PPC64_GOT_TLSGD16_HA, 2, 16, 0xffff, 16, false, signed,
777
       ppc64_elf_unhandled_reloc),
778
779
  /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
780
     with values (sym+add)@dtpmod and zero, and computes the offset to the
781
     first entry relative to the TOC base (r2).  */
782
  HOW (R_PPC64_GOT_TLSLD16, 2, 16, 0xffff, 0, false, signed,
783
       ppc64_elf_unhandled_reloc),
784
785
  /* Like GOT_TLSLD16, but no overflow.  */
786
  HOW (R_PPC64_GOT_TLSLD16_LO, 2, 16, 0xffff, 0, false, dont,
787
       ppc64_elf_unhandled_reloc),
788
789
  /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
790
  HOW (R_PPC64_GOT_TLSLD16_HI, 2, 16, 0xffff, 16, false, signed,
791
       ppc64_elf_unhandled_reloc),
792
793
  /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
794
  HOW (R_PPC64_GOT_TLSLD16_HA, 2, 16, 0xffff, 16, false, signed,
795
       ppc64_elf_unhandled_reloc),
796
797
  /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
798
     the offset to the entry relative to the TOC base (r2).  */
799
  HOW (R_PPC64_GOT_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
800
       ppc64_elf_unhandled_reloc),
801
802
  /* Like GOT_DTPREL16_DS, but no overflow.  */
803
  HOW (R_PPC64_GOT_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
804
       ppc64_elf_unhandled_reloc),
805
806
  /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
807
  HOW (R_PPC64_GOT_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
808
       ppc64_elf_unhandled_reloc),
809
810
  /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
811
  HOW (R_PPC64_GOT_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
812
       ppc64_elf_unhandled_reloc),
813
814
  /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
815
     offset to the entry relative to the TOC base (r2).  */
816
  HOW (R_PPC64_GOT_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
817
       ppc64_elf_unhandled_reloc),
818
819
  /* Like GOT_TPREL16_DS, but no overflow.  */
820
  HOW (R_PPC64_GOT_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
821
       ppc64_elf_unhandled_reloc),
822
823
  /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
824
  HOW (R_PPC64_GOT_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
825
       ppc64_elf_unhandled_reloc),
826
827
  /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
828
  HOW (R_PPC64_GOT_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
829
       ppc64_elf_unhandled_reloc),
830
831
  HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
832
       ppc64_elf_unhandled_reloc),
833
834
  HOW (R_PPC64_IRELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
835
       bfd_elf_generic_reloc),
836
837
  /* A 16 bit relative relocation.  */
838
  HOW (R_PPC64_REL16, 2, 16, 0xffff, 0, true, signed,
839
       bfd_elf_generic_reloc),
840
841
  /* A 16 bit relative relocation without overflow.  */
842
  HOW (R_PPC64_REL16_LO, 2, 16, 0xffff, 0, true, dont,
843
       bfd_elf_generic_reloc),
844
845
  /* The high order 16 bits of a relative address.  */
846
  HOW (R_PPC64_REL16_HI, 2, 16, 0xffff, 16, true, signed,
847
       bfd_elf_generic_reloc),
848
849
  /* The high order 16 bits of a relative address, plus 1 if the contents of
850
     the low 16 bits, treated as a signed number, is negative.  */
851
  HOW (R_PPC64_REL16_HA, 2, 16, 0xffff, 16, true, signed,
852
       ppc64_elf_ha_reloc),
853
854
  HOW (R_PPC64_REL16_HIGH, 2, 16, 0xffff, 16, true, dont,
855
       bfd_elf_generic_reloc),
856
857
  HOW (R_PPC64_REL16_HIGHA, 2, 16, 0xffff, 16, true, dont,
858
       ppc64_elf_ha_reloc),
859
860
  HOW (R_PPC64_REL16_HIGHER, 2, 16, 0xffff, 32, true, dont,
861
       bfd_elf_generic_reloc),
862
863
  HOW (R_PPC64_REL16_HIGHERA, 2, 16, 0xffff, 32, true, dont,
864
       ppc64_elf_ha_reloc),
865
866
  HOW (R_PPC64_REL16_HIGHEST, 2, 16, 0xffff, 48, true, dont,
867
       bfd_elf_generic_reloc),
868
869
  HOW (R_PPC64_REL16_HIGHESTA, 2, 16, 0xffff, 48, true, dont,
870
       ppc64_elf_ha_reloc),
871
872
  /* Like R_PPC64_REL16_HA but for split field in addpcis.  */
873
  HOW (R_PPC64_REL16DX_HA, 4, 16, 0x1fffc1, 16, true, signed,
874
       ppc64_elf_ha_reloc),
875
876
  /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
877
  HOW (R_PPC64_16DX_HA, 4, 16, 0x1fffc1, 16, false, signed,
878
       ppc64_elf_ha_reloc),
879
880
  /* Like R_PPC64_ADDR16_HI, but no overflow.  */
881
  HOW (R_PPC64_ADDR16_HIGH, 2, 16, 0xffff, 16, false, dont,
882
       bfd_elf_generic_reloc),
883
884
  /* Like R_PPC64_ADDR16_HA, but no overflow.  */
885
  HOW (R_PPC64_ADDR16_HIGHA, 2, 16, 0xffff, 16, false, dont,
886
       ppc64_elf_ha_reloc),
887
888
  /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
889
  HOW (R_PPC64_DTPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
890
       ppc64_elf_unhandled_reloc),
891
892
  /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
893
  HOW (R_PPC64_DTPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
894
       ppc64_elf_unhandled_reloc),
895
896
  /* Like R_PPC64_TPREL16_HI, but no overflow.  */
897
  HOW (R_PPC64_TPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
898
       ppc64_elf_unhandled_reloc),
899
900
  /* Like R_PPC64_TPREL16_HA, but no overflow.  */
901
  HOW (R_PPC64_TPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
902
       ppc64_elf_unhandled_reloc),
903
904
  /* Marker reloc on ELFv2 large-model function entry.  */
905
  HOW (R_PPC64_ENTRY, 4, 32, 0, 0, false, dont,
906
       bfd_elf_generic_reloc),
907
908
  /* Like ADDR64, but use local entry point of function.  */
909
  HOW (R_PPC64_ADDR64_LOCAL, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
910
       bfd_elf_generic_reloc),
911
912
  HOW (R_PPC64_PLTSEQ_NOTOC, 4, 32, 0, 0, false, dont,
913
       bfd_elf_generic_reloc),
914
915
  HOW (R_PPC64_PLTCALL_NOTOC, 4, 32, 0, 0, false, dont,
916
       bfd_elf_generic_reloc),
917
918
  HOW (R_PPC64_PCREL_OPT, 4, 32, 0, 0, false, dont,
919
       bfd_elf_generic_reloc),
920
921
  HOW (R_PPC64_D34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
922
       ppc64_elf_prefix_reloc),
923
924
  HOW (R_PPC64_D34_LO, 8, 34, 0x3ffff0000ffffULL, 0, false, dont,
925
       ppc64_elf_prefix_reloc),
926
927
  HOW (R_PPC64_D34_HI30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
928
       ppc64_elf_prefix_reloc),
929
930
  HOW (R_PPC64_D34_HA30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
931
       ppc64_elf_prefix_reloc),
932
933
  HOW (R_PPC64_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
934
       ppc64_elf_prefix_reloc),
935
936
  HOW (R_PPC64_GOT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
937
       ppc64_elf_unhandled_reloc),
938
939
  HOW (R_PPC64_PLT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
940
       ppc64_elf_unhandled_reloc),
941
942
  HOW (R_PPC64_PLT_PCREL34_NOTOC, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
943
       ppc64_elf_unhandled_reloc),
944
945
  HOW (R_PPC64_TPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
946
       ppc64_elf_unhandled_reloc),
947
948
  HOW (R_PPC64_DTPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
949
       ppc64_elf_unhandled_reloc),
950
951
  HOW (R_PPC64_GOT_TLSGD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
952
       ppc64_elf_unhandled_reloc),
953
954
  HOW (R_PPC64_GOT_TLSLD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
955
       ppc64_elf_unhandled_reloc),
956
957
  HOW (R_PPC64_GOT_TPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
958
       ppc64_elf_unhandled_reloc),
959
960
  HOW (R_PPC64_GOT_DTPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
961
       ppc64_elf_unhandled_reloc),
962
963
  HOW (R_PPC64_ADDR16_HIGHER34, 2, 16, 0xffff, 34, false, dont,
964
       bfd_elf_generic_reloc),
965
966
  HOW (R_PPC64_ADDR16_HIGHERA34, 2, 16, 0xffff, 34, false, dont,
967
       ppc64_elf_ha_reloc),
968
969
  HOW (R_PPC64_ADDR16_HIGHEST34, 2, 16, 0xffff, 50, false, dont,
970
       bfd_elf_generic_reloc),
971
972
  HOW (R_PPC64_ADDR16_HIGHESTA34, 2, 16, 0xffff, 50, false, dont,
973
       ppc64_elf_ha_reloc),
974
975
  HOW (R_PPC64_REL16_HIGHER34, 2, 16, 0xffff, 34, true, dont,
976
       bfd_elf_generic_reloc),
977
978
  HOW (R_PPC64_REL16_HIGHERA34, 2, 16, 0xffff, 34, true, dont,
979
       ppc64_elf_ha_reloc),
980
981
  HOW (R_PPC64_REL16_HIGHEST34, 2, 16, 0xffff, 50, true, dont,
982
       bfd_elf_generic_reloc),
983
984
  HOW (R_PPC64_REL16_HIGHESTA34, 2, 16, 0xffff, 50, true, dont,
985
       ppc64_elf_ha_reloc),
986
987
  HOW (R_PPC64_D28, 8, 28, 0xfff0000ffffULL, 0, false, signed,
988
       ppc64_elf_prefix_reloc),
989
990
  HOW (R_PPC64_PCREL28, 8, 28, 0xfff0000ffffULL, 0, true, signed,
991
       ppc64_elf_prefix_reloc),
992
993
  /* GNU extension to record C++ vtable hierarchy.  */
994
  HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
995
       NULL),
996
997
  /* GNU extension to record C++ vtable member usage.  */
998
  HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
999
       NULL),
1000
};
1001
1002

1003
/* Initialize the ppc64_elf_howto_table, so that linear accesses can
1004
   be done.  */
1005
1006
static void
1007
ppc_howto_init (void)
1008
5
{
1009
5
  unsigned int i, type;
1010
1011
815
  for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1012
810
    {
1013
810
      type = ppc64_elf_howto_raw[i].type;
1014
810
      BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
1015
810
      ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1016
810
    }
1017
5
}
1018
1019
static reloc_howto_type *
1020
ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
1021
0
{
1022
0
  enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1023
1024
0
  if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1025
    /* Initialize howto table if needed.  */
1026
0
    ppc_howto_init ();
1027
1028
0
  switch (code)
1029
0
    {
1030
0
    default:
1031
      /* xgettext:c-format */
1032
0
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1033
0
        (int) code);
1034
0
      bfd_set_error (bfd_error_bad_value);
1035
0
      return NULL;
1036
1037
0
    case BFD_RELOC_NONE:      r = R_PPC64_NONE;
1038
0
      break;
1039
0
    case BFD_RELOC_32:        r = R_PPC64_ADDR32;
1040
0
      break;
1041
0
    case BFD_RELOC_PPC_BA26:      r = R_PPC64_ADDR24;
1042
0
      break;
1043
0
    case BFD_RELOC_16:        r = R_PPC64_ADDR16;
1044
0
      break;
1045
0
    case BFD_RELOC_LO16:      r = R_PPC64_ADDR16_LO;
1046
0
      break;
1047
0
    case BFD_RELOC_HI16:      r = R_PPC64_ADDR16_HI;
1048
0
      break;
1049
0
    case BFD_RELOC_PPC64_ADDR16_HIGH:   r = R_PPC64_ADDR16_HIGH;
1050
0
      break;
1051
0
    case BFD_RELOC_HI16_S:      r = R_PPC64_ADDR16_HA;
1052
0
      break;
1053
0
    case BFD_RELOC_PPC64_ADDR16_HIGHA:    r = R_PPC64_ADDR16_HIGHA;
1054
0
      break;
1055
0
    case BFD_RELOC_PPC_BA16:      r = R_PPC64_ADDR14;
1056
0
      break;
1057
0
    case BFD_RELOC_PPC_BA16_BRTAKEN:    r = R_PPC64_ADDR14_BRTAKEN;
1058
0
      break;
1059
0
    case BFD_RELOC_PPC_BA16_BRNTAKEN:   r = R_PPC64_ADDR14_BRNTAKEN;
1060
0
      break;
1061
0
    case BFD_RELOC_PPC_B26:     r = R_PPC64_REL24;
1062
0
      break;
1063
0
    case BFD_RELOC_PPC64_REL24_NOTOC:   r = R_PPC64_REL24_NOTOC;
1064
0
      break;
1065
0
    case BFD_RELOC_PPC64_REL24_P9NOTOC:   r = R_PPC64_REL24_P9NOTOC;
1066
0
      break;
1067
0
    case BFD_RELOC_PPC_B16:     r = R_PPC64_REL14;
1068
0
      break;
1069
0
    case BFD_RELOC_PPC_B16_BRTAKEN:   r = R_PPC64_REL14_BRTAKEN;
1070
0
      break;
1071
0
    case BFD_RELOC_PPC_B16_BRNTAKEN:    r = R_PPC64_REL14_BRNTAKEN;
1072
0
      break;
1073
0
    case BFD_RELOC_16_GOTOFF:     r = R_PPC64_GOT16;
1074
0
      break;
1075
0
    case BFD_RELOC_LO16_GOTOFF:     r = R_PPC64_GOT16_LO;
1076
0
      break;
1077
0
    case BFD_RELOC_HI16_GOTOFF:     r = R_PPC64_GOT16_HI;
1078
0
      break;
1079
0
    case BFD_RELOC_HI16_S_GOTOFF:   r = R_PPC64_GOT16_HA;
1080
0
      break;
1081
0
    case BFD_RELOC_COPY:      r = R_PPC64_COPY;
1082
0
      break;
1083
0
    case BFD_RELOC_GLOB_DAT:      r = R_PPC64_GLOB_DAT;
1084
0
      break;
1085
0
    case BFD_RELOC_32_PCREL:      r = R_PPC64_REL32;
1086
0
      break;
1087
0
    case BFD_RELOC_32_PLTOFF:     r = R_PPC64_PLT32;
1088
0
      break;
1089
0
    case BFD_RELOC_32_PLT_PCREL:    r = R_PPC64_PLTREL32;
1090
0
      break;
1091
0
    case BFD_RELOC_LO16_PLTOFF:     r = R_PPC64_PLT16_LO;
1092
0
      break;
1093
0
    case BFD_RELOC_HI16_PLTOFF:     r = R_PPC64_PLT16_HI;
1094
0
      break;
1095
0
    case BFD_RELOC_HI16_S_PLTOFF:   r = R_PPC64_PLT16_HA;
1096
0
      break;
1097
0
    case BFD_RELOC_16_BASEREL:      r = R_PPC64_SECTOFF;
1098
0
      break;
1099
0
    case BFD_RELOC_LO16_BASEREL:    r = R_PPC64_SECTOFF_LO;
1100
0
      break;
1101
0
    case BFD_RELOC_HI16_BASEREL:    r = R_PPC64_SECTOFF_HI;
1102
0
      break;
1103
0
    case BFD_RELOC_HI16_S_BASEREL:    r = R_PPC64_SECTOFF_HA;
1104
0
      break;
1105
0
    case BFD_RELOC_CTOR:      r = R_PPC64_ADDR64;
1106
0
      break;
1107
0
    case BFD_RELOC_64:        r = R_PPC64_ADDR64;
1108
0
      break;
1109
0
    case BFD_RELOC_PPC64_HIGHER:    r = R_PPC64_ADDR16_HIGHER;
1110
0
      break;
1111
0
    case BFD_RELOC_PPC64_HIGHER_S:    r = R_PPC64_ADDR16_HIGHERA;
1112
0
      break;
1113
0
    case BFD_RELOC_PPC64_HIGHEST:   r = R_PPC64_ADDR16_HIGHEST;
1114
0
      break;
1115
0
    case BFD_RELOC_PPC64_HIGHEST_S:   r = R_PPC64_ADDR16_HIGHESTA;
1116
0
      break;
1117
0
    case BFD_RELOC_64_PCREL:      r = R_PPC64_REL64;
1118
0
      break;
1119
0
    case BFD_RELOC_64_PLTOFF:     r = R_PPC64_PLT64;
1120
0
      break;
1121
0
    case BFD_RELOC_64_PLT_PCREL:    r = R_PPC64_PLTREL64;
1122
0
      break;
1123
0
    case BFD_RELOC_PPC_TOC16:     r = R_PPC64_TOC16;
1124
0
      break;
1125
0
    case BFD_RELOC_PPC64_TOC16_LO:    r = R_PPC64_TOC16_LO;
1126
0
      break;
1127
0
    case BFD_RELOC_PPC64_TOC16_HI:    r = R_PPC64_TOC16_HI;
1128
0
      break;
1129
0
    case BFD_RELOC_PPC64_TOC16_HA:    r = R_PPC64_TOC16_HA;
1130
0
      break;
1131
0
    case BFD_RELOC_PPC64_TOC:     r = R_PPC64_TOC;
1132
0
      break;
1133
0
    case BFD_RELOC_PPC64_PLTGOT16:    r = R_PPC64_PLTGOT16;
1134
0
      break;
1135
0
    case BFD_RELOC_PPC64_PLTGOT16_LO:   r = R_PPC64_PLTGOT16_LO;
1136
0
      break;
1137
0
    case BFD_RELOC_PPC64_PLTGOT16_HI:   r = R_PPC64_PLTGOT16_HI;
1138
0
      break;
1139
0
    case BFD_RELOC_PPC64_PLTGOT16_HA:   r = R_PPC64_PLTGOT16_HA;
1140
0
      break;
1141
0
    case BFD_RELOC_PPC64_ADDR16_DS:   r = R_PPC64_ADDR16_DS;
1142
0
      break;
1143
0
    case BFD_RELOC_PPC64_ADDR16_LO_DS:    r = R_PPC64_ADDR16_LO_DS;
1144
0
      break;
1145
0
    case BFD_RELOC_PPC64_GOT16_DS:    r = R_PPC64_GOT16_DS;
1146
0
      break;
1147
0
    case BFD_RELOC_PPC64_GOT16_LO_DS:   r = R_PPC64_GOT16_LO_DS;
1148
0
      break;
1149
0
    case BFD_RELOC_PPC64_PLT16_LO_DS:   r = R_PPC64_PLT16_LO_DS;
1150
0
      break;
1151
0
    case BFD_RELOC_PPC64_SECTOFF_DS:    r = R_PPC64_SECTOFF_DS;
1152
0
      break;
1153
0
    case BFD_RELOC_PPC64_SECTOFF_LO_DS:   r = R_PPC64_SECTOFF_LO_DS;
1154
0
      break;
1155
0
    case BFD_RELOC_PPC64_TOC16_DS:    r = R_PPC64_TOC16_DS;
1156
0
      break;
1157
0
    case BFD_RELOC_PPC64_TOC16_LO_DS:   r = R_PPC64_TOC16_LO_DS;
1158
0
      break;
1159
0
    case BFD_RELOC_PPC64_PLTGOT16_DS:   r = R_PPC64_PLTGOT16_DS;
1160
0
      break;
1161
0
    case BFD_RELOC_PPC64_PLTGOT16_LO_DS:  r = R_PPC64_PLTGOT16_LO_DS;
1162
0
      break;
1163
0
    case BFD_RELOC_PPC64_TLS_PCREL:
1164
0
    case BFD_RELOC_PPC_TLS:     r = R_PPC64_TLS;
1165
0
      break;
1166
0
    case BFD_RELOC_PPC_TLSGD:     r = R_PPC64_TLSGD;
1167
0
      break;
1168
0
    case BFD_RELOC_PPC_TLSLD:     r = R_PPC64_TLSLD;
1169
0
      break;
1170
0
    case BFD_RELOC_PPC_DTPMOD:      r = R_PPC64_DTPMOD64;
1171
0
      break;
1172
0
    case BFD_RELOC_PPC_TPREL16:     r = R_PPC64_TPREL16;
1173
0
      break;
1174
0
    case BFD_RELOC_PPC_TPREL16_LO:    r = R_PPC64_TPREL16_LO;
1175
0
      break;
1176
0
    case BFD_RELOC_PPC_TPREL16_HI:    r = R_PPC64_TPREL16_HI;
1177
0
      break;
1178
0
    case BFD_RELOC_PPC64_TPREL16_HIGH:    r = R_PPC64_TPREL16_HIGH;
1179
0
      break;
1180
0
    case BFD_RELOC_PPC_TPREL16_HA:    r = R_PPC64_TPREL16_HA;
1181
0
      break;
1182
0
    case BFD_RELOC_PPC64_TPREL16_HIGHA:   r = R_PPC64_TPREL16_HIGHA;
1183
0
      break;
1184
0
    case BFD_RELOC_PPC_TPREL:     r = R_PPC64_TPREL64;
1185
0
      break;
1186
0
    case BFD_RELOC_PPC_DTPREL16:    r = R_PPC64_DTPREL16;
1187
0
      break;
1188
0
    case BFD_RELOC_PPC_DTPREL16_LO:   r = R_PPC64_DTPREL16_LO;
1189
0
      break;
1190
0
    case BFD_RELOC_PPC_DTPREL16_HI:   r = R_PPC64_DTPREL16_HI;
1191
0
      break;
1192
0
    case BFD_RELOC_PPC64_DTPREL16_HIGH:   r = R_PPC64_DTPREL16_HIGH;
1193
0
      break;
1194
0
    case BFD_RELOC_PPC_DTPREL16_HA:   r = R_PPC64_DTPREL16_HA;
1195
0
      break;
1196
0
    case BFD_RELOC_PPC64_DTPREL16_HIGHA:  r = R_PPC64_DTPREL16_HIGHA;
1197
0
      break;
1198
0
    case BFD_RELOC_PPC_DTPREL:      r = R_PPC64_DTPREL64;
1199
0
      break;
1200
0
    case BFD_RELOC_PPC_GOT_TLSGD16:   r = R_PPC64_GOT_TLSGD16;
1201
0
      break;
1202
0
    case BFD_RELOC_PPC_GOT_TLSGD16_LO:    r = R_PPC64_GOT_TLSGD16_LO;
1203
0
      break;
1204
0
    case BFD_RELOC_PPC_GOT_TLSGD16_HI:    r = R_PPC64_GOT_TLSGD16_HI;
1205
0
      break;
1206
0
    case BFD_RELOC_PPC_GOT_TLSGD16_HA:    r = R_PPC64_GOT_TLSGD16_HA;
1207
0
      break;
1208
0
    case BFD_RELOC_PPC_GOT_TLSLD16:   r = R_PPC64_GOT_TLSLD16;
1209
0
      break;
1210
0
    case BFD_RELOC_PPC_GOT_TLSLD16_LO:    r = R_PPC64_GOT_TLSLD16_LO;
1211
0
      break;
1212
0
    case BFD_RELOC_PPC_GOT_TLSLD16_HI:    r = R_PPC64_GOT_TLSLD16_HI;
1213
0
      break;
1214
0
    case BFD_RELOC_PPC_GOT_TLSLD16_HA:    r = R_PPC64_GOT_TLSLD16_HA;
1215
0
      break;
1216
0
    case BFD_RELOC_PPC_GOT_TPREL16:   r = R_PPC64_GOT_TPREL16_DS;
1217
0
      break;
1218
0
    case BFD_RELOC_PPC_GOT_TPREL16_LO:    r = R_PPC64_GOT_TPREL16_LO_DS;
1219
0
      break;
1220
0
    case BFD_RELOC_PPC_GOT_TPREL16_HI:    r = R_PPC64_GOT_TPREL16_HI;
1221
0
      break;
1222
0
    case BFD_RELOC_PPC_GOT_TPREL16_HA:    r = R_PPC64_GOT_TPREL16_HA;
1223
0
      break;
1224
0
    case BFD_RELOC_PPC_GOT_DTPREL16:    r = R_PPC64_GOT_DTPREL16_DS;
1225
0
      break;
1226
0
    case BFD_RELOC_PPC_GOT_DTPREL16_LO:   r = R_PPC64_GOT_DTPREL16_LO_DS;
1227
0
      break;
1228
0
    case BFD_RELOC_PPC_GOT_DTPREL16_HI:   r = R_PPC64_GOT_DTPREL16_HI;
1229
0
      break;
1230
0
    case BFD_RELOC_PPC_GOT_DTPREL16_HA:   r = R_PPC64_GOT_DTPREL16_HA;
1231
0
      break;
1232
0
    case BFD_RELOC_PPC64_TPREL16_DS:    r = R_PPC64_TPREL16_DS;
1233
0
      break;
1234
0
    case BFD_RELOC_PPC64_TPREL16_LO_DS:   r = R_PPC64_TPREL16_LO_DS;
1235
0
      break;
1236
0
    case BFD_RELOC_PPC64_TPREL16_HIGHER:  r = R_PPC64_TPREL16_HIGHER;
1237
0
      break;
1238
0
    case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1239
0
      break;
1240
0
    case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1241
0
      break;
1242
0
    case BFD_RELOC_PPC64_TPREL16_HIGHESTA:  r = R_PPC64_TPREL16_HIGHESTA;
1243
0
      break;
1244
0
    case BFD_RELOC_PPC64_DTPREL16_DS:   r = R_PPC64_DTPREL16_DS;
1245
0
      break;
1246
0
    case BFD_RELOC_PPC64_DTPREL16_LO_DS:  r = R_PPC64_DTPREL16_LO_DS;
1247
0
      break;
1248
0
    case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1249
0
      break;
1250
0
    case BFD_RELOC_PPC64_DTPREL16_HIGHERA:  r = R_PPC64_DTPREL16_HIGHERA;
1251
0
      break;
1252
0
    case BFD_RELOC_PPC64_DTPREL16_HIGHEST:  r = R_PPC64_DTPREL16_HIGHEST;
1253
0
      break;
1254
0
    case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1255
0
      break;
1256
0
    case BFD_RELOC_16_PCREL:      r = R_PPC64_REL16;
1257
0
      break;
1258
0
    case BFD_RELOC_LO16_PCREL:      r = R_PPC64_REL16_LO;
1259
0
      break;
1260
0
    case BFD_RELOC_HI16_PCREL:      r = R_PPC64_REL16_HI;
1261
0
      break;
1262
0
    case BFD_RELOC_HI16_S_PCREL:    r = R_PPC64_REL16_HA;
1263
0
      break;
1264
0
    case BFD_RELOC_PPC64_REL16_HIGH:    r = R_PPC64_REL16_HIGH;
1265
0
      break;
1266
0
    case BFD_RELOC_PPC64_REL16_HIGHA:   r = R_PPC64_REL16_HIGHA;
1267
0
      break;
1268
0
    case BFD_RELOC_PPC64_REL16_HIGHER:    r = R_PPC64_REL16_HIGHER;
1269
0
      break;
1270
0
    case BFD_RELOC_PPC64_REL16_HIGHERA:   r = R_PPC64_REL16_HIGHERA;
1271
0
      break;
1272
0
    case BFD_RELOC_PPC64_REL16_HIGHEST:   r = R_PPC64_REL16_HIGHEST;
1273
0
      break;
1274
0
    case BFD_RELOC_PPC64_REL16_HIGHESTA:  r = R_PPC64_REL16_HIGHESTA;
1275
0
      break;
1276
0
    case BFD_RELOC_PPC_16DX_HA:     r = R_PPC64_16DX_HA;
1277
0
      break;
1278
0
    case BFD_RELOC_PPC_REL16DX_HA:    r = R_PPC64_REL16DX_HA;
1279
0
      break;
1280
0
    case BFD_RELOC_PPC64_ENTRY:     r = R_PPC64_ENTRY;
1281
0
      break;
1282
0
    case BFD_RELOC_PPC64_ADDR64_LOCAL:    r = R_PPC64_ADDR64_LOCAL;
1283
0
      break;
1284
0
    case BFD_RELOC_PPC64_D34:     r = R_PPC64_D34;
1285
0
      break;
1286
0
    case BFD_RELOC_PPC64_D34_LO:    r = R_PPC64_D34_LO;
1287
0
      break;
1288
0
    case BFD_RELOC_PPC64_D34_HI30:    r = R_PPC64_D34_HI30;
1289
0
      break;
1290
0
    case BFD_RELOC_PPC64_D34_HA30:    r = R_PPC64_D34_HA30;
1291
0
      break;
1292
0
    case BFD_RELOC_PPC64_PCREL34:   r = R_PPC64_PCREL34;
1293
0
      break;
1294
0
    case BFD_RELOC_PPC64_GOT_PCREL34:   r = R_PPC64_GOT_PCREL34;
1295
0
      break;
1296
0
    case BFD_RELOC_PPC64_PLT_PCREL34:   r = R_PPC64_PLT_PCREL34;
1297
0
      break;
1298
0
    case BFD_RELOC_PPC64_TPREL34:   r = R_PPC64_TPREL34;
1299
0
      break;
1300
0
    case BFD_RELOC_PPC64_DTPREL34:    r = R_PPC64_DTPREL34;
1301
0
      break;
1302
0
    case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
1303
0
      break;
1304
0
    case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
1305
0
      break;
1306
0
    case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
1307
0
      break;
1308
0
    case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34:  r = R_PPC64_GOT_DTPREL_PCREL34;
1309
0
      break;
1310
0
    case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1311
0
      break;
1312
0
    case BFD_RELOC_PPC64_ADDR16_HIGHERA34:  r = R_PPC64_ADDR16_HIGHERA34;
1313
0
      break;
1314
0
    case BFD_RELOC_PPC64_ADDR16_HIGHEST34:  r = R_PPC64_ADDR16_HIGHEST34;
1315
0
      break;
1316
0
    case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1317
0
      break;
1318
0
    case BFD_RELOC_PPC64_REL16_HIGHER34:  r = R_PPC64_REL16_HIGHER34;
1319
0
      break;
1320
0
    case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1321
0
      break;
1322
0
    case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1323
0
      break;
1324
0
    case BFD_RELOC_PPC64_REL16_HIGHESTA34:  r = R_PPC64_REL16_HIGHESTA34;
1325
0
      break;
1326
0
    case BFD_RELOC_PPC64_D28:     r = R_PPC64_D28;
1327
0
      break;
1328
0
    case BFD_RELOC_PPC64_PCREL28:   r = R_PPC64_PCREL28;
1329
0
      break;
1330
0
    case BFD_RELOC_VTABLE_INHERIT:    r = R_PPC64_GNU_VTINHERIT;
1331
0
      break;
1332
0
    case BFD_RELOC_VTABLE_ENTRY:    r = R_PPC64_GNU_VTENTRY;
1333
0
      break;
1334
0
    }
1335
1336
0
  return ppc64_elf_howto_table[r];
1337
0
};
1338
1339
static reloc_howto_type *
1340
ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
1341
0
{
1342
0
  unsigned int i;
1343
0
  static char *compat_map[][2] = {
1344
0
    { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1345
0
    { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1346
0
    { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1347
0
    { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1348
0
  };
1349
1350
0
  for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1351
0
    if (ppc64_elf_howto_raw[i].name != NULL
1352
0
  && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1353
0
      return &ppc64_elf_howto_raw[i];
1354
1355
  /* Handle old names of relocations in case they were used by
1356
     .reloc directives.
1357
     FIXME: Remove this soon.  Mapping the reloc names is very likely
1358
     completely unnecessary.  */
1359
0
  for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1360
0
    if (strcasecmp (compat_map[i][0], r_name) == 0)
1361
0
      {
1362
0
  _bfd_error_handler (_("warning: %s should be used rather than %s"),
1363
0
          compat_map[i][1], compat_map[i][0]);
1364
0
  return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1365
0
      }
1366
1367
0
  return NULL;
1368
0
}
1369
1370
/* Set the howto pointer for a PowerPC ELF reloc.  */
1371
1372
static bool
1373
ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1374
       Elf_Internal_Rela *dst)
1375
2.18k
{
1376
2.18k
  unsigned int type;
1377
1378
  /* Initialize howto table if needed.  */
1379
2.18k
  if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1380
5
    ppc_howto_init ();
1381
1382
2.18k
  type = ELF64_R_TYPE (dst->r_info);
1383
2.18k
  if (type >= ARRAY_SIZE (ppc64_elf_howto_table)
1384
2.04k
      || ppc64_elf_howto_table[type] == NULL)
1385
183
    {
1386
      /* xgettext:c-format */
1387
183
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1388
183
        abfd, type);
1389
183
      bfd_set_error (bfd_error_bad_value);
1390
183
      return false;
1391
183
    }
1392
2.00k
  cache_ptr->howto = ppc64_elf_howto_table[type];
1393
2.00k
  return true;
1394
2.18k
}
1395
1396
/* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
1397
1398
static bfd_reloc_status_type
1399
ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1400
        void *data, asection *input_section,
1401
        bfd *output_bfd, char **error_message)
1402
15
{
1403
15
  enum elf_ppc64_reloc_type r_type;
1404
15
  long insn;
1405
15
  bfd_size_type octets;
1406
15
  bfd_vma value;
1407
1408
  /* If this is a relocatable link (output_bfd test tells us), just
1409
     call the generic function.  Any adjustment will be done at final
1410
     link time.  */
1411
15
  if (output_bfd != NULL)
1412
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1413
0
          input_section, output_bfd, error_message);
1414
1415
  /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1416
     We won't actually be using the low bits, so trashing them
1417
     doesn't matter.  */
1418
15
  r_type = reloc_entry->howto->type;
1419
15
  if (r_type == R_PPC64_ADDR16_HIGHERA34
1420
2
      || r_type == R_PPC64_ADDR16_HIGHESTA34
1421
2
      || r_type == R_PPC64_REL16_HIGHERA34
1422
1
      || r_type == R_PPC64_REL16_HIGHESTA34)
1423
14
    reloc_entry->addend += 1ULL << 33;
1424
1
  else
1425
1
    reloc_entry->addend += 1U << 15;
1426
15
  if (r_type != R_PPC64_REL16DX_HA)
1427
15
    return bfd_reloc_continue;
1428
1429
0
  value = 0;
1430
0
  if (!bfd_is_com_section (symbol->section))
1431
0
    value = symbol->value;
1432
0
  value += (reloc_entry->addend
1433
0
      + symbol->section->output_offset
1434
0
      + symbol->section->output_section->vma);
1435
0
  value -= (reloc_entry->address
1436
0
      + input_section->output_offset
1437
0
      + input_section->output_section->vma);
1438
0
  value = (bfd_signed_vma) value >> 16;
1439
1440
0
  octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1441
0
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1442
0
          input_section, octets))
1443
0
    return bfd_reloc_outofrange;
1444
1445
0
  insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1446
0
  insn &= ~0x1fffc1;
1447
0
  insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1448
0
  bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1449
0
  if (value + 0x8000 > 0xffff)
1450
0
    return bfd_reloc_overflow;
1451
0
  return bfd_reloc_ok;
1452
0
}
1453
1454
static bfd_reloc_status_type
1455
ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1456
      void *data, asection *input_section,
1457
      bfd *output_bfd, char **error_message)
1458
153
{
1459
153
  if (output_bfd != NULL)
1460
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1461
0
          input_section, output_bfd, error_message);
1462
1463
153
  if (symbol->section->owner == NULL
1464
56
      || !is_ppc64_elf (symbol->section->owner))
1465
97
    return bfd_reloc_continue;
1466
1467
56
  if (strcmp (symbol->section->name, ".opd") == 0
1468
0
      && (symbol->section->owner->flags & DYNAMIC) == 0)
1469
0
    {
1470
0
      bfd_vma dest = opd_entry_value (symbol->section,
1471
0
              symbol->value + reloc_entry->addend,
1472
0
              NULL, NULL, false);
1473
0
      if (dest != (bfd_vma) -1)
1474
0
  reloc_entry->addend = dest - (symbol->value
1475
0
              + symbol->section->output_section->vma
1476
0
              + symbol->section->output_offset);
1477
0
    }
1478
56
  else
1479
56
    {
1480
56
      elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1481
1482
56
      if (symbol->section->owner != abfd
1483
0
    && abiversion (symbol->section->owner) >= 2)
1484
0
  {
1485
0
    unsigned int i;
1486
1487
0
    for (i = 0; i < symbol->section->owner->symcount; ++i)
1488
0
      {
1489
0
        asymbol *symdef = symbol->section->owner->outsymbols[i];
1490
1491
0
        if (strcmp (symdef->name, symbol->name) == 0)
1492
0
    {
1493
0
      elfsym = (elf_symbol_type *) symdef;
1494
0
      break;
1495
0
    }
1496
0
      }
1497
0
  }
1498
56
      reloc_entry->addend
1499
56
  += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1500
56
    }
1501
56
  return bfd_reloc_continue;
1502
153
}
1503
1504
static bfd_reloc_status_type
1505
ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1506
       void *data, asection *input_section,
1507
       bfd *output_bfd, char **error_message)
1508
49
{
1509
49
  long insn;
1510
49
  enum elf_ppc64_reloc_type r_type;
1511
49
  bfd_size_type octets;
1512
  /* Assume 'at' branch hints.  */
1513
49
  bool is_isa_v2 = true;
1514
1515
  /* If this is a relocatable link (output_bfd test tells us), just
1516
     call the generic function.  Any adjustment will be done at final
1517
     link time.  */
1518
49
  if (output_bfd != NULL)
1519
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1520
0
          input_section, output_bfd, error_message);
1521
1522
49
  octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1523
49
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1524
49
          input_section, octets))
1525
2
    return bfd_reloc_outofrange;
1526
1527
47
  insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1528
47
  insn &= ~(0x01 << 21);
1529
47
  r_type = reloc_entry->howto->type;
1530
47
  if (r_type == R_PPC64_ADDR14_BRTAKEN
1531
28
      || r_type == R_PPC64_REL14_BRTAKEN)
1532
37
    insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
1533
1534
47
  if (is_isa_v2)
1535
47
    {
1536
      /* Set 'a' bit.  This is 0b00010 in BO field for branch
1537
   on CR(BI) insns (BO == 001at or 011at), and 0b01000
1538
   for branch on CTR insns (BO == 1a00t or 1a01t).  */
1539
47
      if ((insn & (0x14 << 21)) == (0x04 << 21))
1540
11
  insn |= 0x02 << 21;
1541
36
      else if ((insn & (0x14 << 21)) == (0x10 << 21))
1542
15
  insn |= 0x08 << 21;
1543
21
      else
1544
21
  goto out;
1545
47
    }
1546
0
  else
1547
0
    {
1548
0
      bfd_vma target = 0;
1549
0
      bfd_vma from;
1550
1551
0
      if (!bfd_is_com_section (symbol->section))
1552
0
  target = symbol->value;
1553
0
      target += symbol->section->output_section->vma;
1554
0
      target += symbol->section->output_offset;
1555
0
      target += reloc_entry->addend;
1556
1557
0
      from = (reloc_entry->address
1558
0
        + input_section->output_offset
1559
0
        + input_section->output_section->vma);
1560
1561
      /* Invert 'y' bit if not the default.  */
1562
0
      if ((bfd_signed_vma) (target - from) < 0)
1563
0
  insn ^= 0x01 << 21;
1564
0
    }
1565
26
  bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1566
47
 out:
1567
47
  return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1568
47
         input_section, output_bfd, error_message);
1569
26
}
1570
1571
static bfd_reloc_status_type
1572
ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1573
       void *data, asection *input_section,
1574
       bfd *output_bfd, char **error_message)
1575
11
{
1576
  /* If this is a relocatable link (output_bfd test tells us), just
1577
     call the generic function.  Any adjustment will be done at final
1578
     link time.  */
1579
11
  if (output_bfd != NULL)
1580
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1581
0
          input_section, output_bfd, error_message);
1582
1583
  /* Subtract the symbol section base address.  */
1584
11
  reloc_entry->addend -= symbol->section->output_section->vma;
1585
11
  return bfd_reloc_continue;
1586
11
}
1587
1588
static bfd_reloc_status_type
1589
ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1590
          void *data, asection *input_section,
1591
          bfd *output_bfd, char **error_message)
1592
21
{
1593
  /* If this is a relocatable link (output_bfd test tells us), just
1594
     call the generic function.  Any adjustment will be done at final
1595
     link time.  */
1596
21
  if (output_bfd != NULL)
1597
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1598
0
          input_section, output_bfd, error_message);
1599
1600
  /* Subtract the symbol section base address.  */
1601
21
  reloc_entry->addend -= symbol->section->output_section->vma;
1602
1603
  /* Adjust the addend for sign extension of the low 16 bits.  */
1604
21
  reloc_entry->addend += 0x8000;
1605
21
  return bfd_reloc_continue;
1606
21
}
1607
1608
static bfd_reloc_status_type
1609
ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1610
         void *data, asection *input_section,
1611
         bfd *output_bfd, char **error_message)
1612
22
{
1613
22
  bfd_vma TOCstart;
1614
1615
  /* If this is a relocatable link (output_bfd test tells us), just
1616
     call the generic function.  Any adjustment will be done at final
1617
     link time.  */
1618
22
  if (output_bfd != NULL)
1619
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1620
0
          input_section, output_bfd, error_message);
1621
1622
22
  TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1623
22
  if (TOCstart == 0)
1624
21
    TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1625
1626
  /* Subtract the TOC base address.  */
1627
22
  reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1628
22
  return bfd_reloc_continue;
1629
22
}
1630
1631
static bfd_reloc_status_type
1632
ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1633
      void *data, asection *input_section,
1634
      bfd *output_bfd, char **error_message)
1635
5
{
1636
5
  bfd_vma TOCstart;
1637
1638
  /* If this is a relocatable link (output_bfd test tells us), just
1639
     call the generic function.  Any adjustment will be done at final
1640
     link time.  */
1641
5
  if (output_bfd != NULL)
1642
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1643
0
          input_section, output_bfd, error_message);
1644
1645
5
  TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1646
5
  if (TOCstart == 0)
1647
4
    TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1648
1649
  /* Subtract the TOC base address.  */
1650
5
  reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1651
1652
  /* Adjust the addend for sign extension of the low 16 bits.  */
1653
5
  reloc_entry->addend += 0x8000;
1654
5
  return bfd_reloc_continue;
1655
5
}
1656
1657
static bfd_reloc_status_type
1658
ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1659
           void *data, asection *input_section,
1660
           bfd *output_bfd, char **error_message)
1661
3
{
1662
3
  bfd_vma TOCstart;
1663
3
  bfd_size_type octets;
1664
1665
  /* If this is a relocatable link (output_bfd test tells us), just
1666
     call the generic function.  Any adjustment will be done at final
1667
     link time.  */
1668
3
  if (output_bfd != NULL)
1669
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1670
0
          input_section, output_bfd, error_message);
1671
1672
3
  octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1673
3
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1674
3
          input_section, octets))
1675
0
    return bfd_reloc_outofrange;
1676
1677
3
  TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1678
3
  if (TOCstart == 0)
1679
3
    TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1680
1681
3
  bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1682
3
  return bfd_reloc_ok;
1683
3
}
1684
1685
static bfd_reloc_status_type
1686
ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1687
      void *data, asection *input_section,
1688
      bfd *output_bfd, char **error_message)
1689
3
{
1690
3
  uint64_t insn;
1691
3
  bfd_vma targ;
1692
3
  bfd_size_type octets;
1693
1694
3
  if (output_bfd != NULL)
1695
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1696
0
          input_section, output_bfd, error_message);
1697
1698
3
  octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1699
3
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1700
3
          input_section, octets))
1701
1
    return bfd_reloc_outofrange;
1702
1703
2
  insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1704
2
  insn <<= 32;
1705
2
  insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
1706
1707
2
  targ = (symbol->section->output_section->vma
1708
2
    + symbol->section->output_offset
1709
2
    + reloc_entry->addend);
1710
2
  if (!bfd_is_com_section (symbol->section))
1711
2
    targ += symbol->value;
1712
2
  if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1713
0
    targ += 1ULL << 33;
1714
2
  if (reloc_entry->howto->pc_relative)
1715
0
    {
1716
0
      bfd_vma from = (reloc_entry->address
1717
0
          + input_section->output_offset
1718
0
          + input_section->output_section->vma);
1719
0
      targ -=from;
1720
0
    }
1721
2
  targ >>= reloc_entry->howto->rightshift;
1722
2
  insn &= ~reloc_entry->howto->dst_mask;
1723
2
  insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1724
2
  bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
1725
2
  bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
1726
2
  if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1727
2
      && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1728
2
    >= 1ULL << reloc_entry->howto->bitsize))
1729
1
    return bfd_reloc_overflow;
1730
1
  return bfd_reloc_ok;
1731
2
}
1732
1733
static bfd_reloc_status_type
1734
ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1735
         void *data, asection *input_section,
1736
         bfd *output_bfd, char **error_message)
1737
27
{
1738
  /* If this is a relocatable link (output_bfd test tells us), just
1739
     call the generic function.  Any adjustment will be done at final
1740
     link time.  */
1741
27
  if (output_bfd != NULL)
1742
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1743
0
          input_section, output_bfd, error_message);
1744
1745
27
  if (error_message != NULL)
1746
27
    *error_message = bfd_asprintf (_("generic linker can't handle %s"),
1747
27
           reloc_entry->howto->name);
1748
27
  return bfd_reloc_dangerous;
1749
27
}
1750
1751
/* Track GOT entries needed for a given symbol.  We might need more
1752
   than one got entry per symbol.  */
1753
struct got_entry
1754
{
1755
  struct got_entry *next;
1756
1757
  /* The symbol addend that we'll be placing in the GOT.  */
1758
  bfd_vma addend;
1759
1760
  /* Unlike other ELF targets, we use separate GOT entries for the same
1761
     symbol referenced from different input files.  This is to support
1762
     automatic multiple TOC/GOT sections, where the TOC base can vary
1763
     from one input file to another.  After partitioning into TOC groups
1764
     we merge entries within the group.
1765
1766
     Point to the BFD owning this GOT entry.  */
1767
  bfd *owner;
1768
1769
  /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1770
     TLS_TPREL or TLS_DTPREL for tls entries.  */
1771
  unsigned char tls_type;
1772
1773
  /* Non-zero if got.ent points to real entry.  */
1774
  unsigned char is_indirect;
1775
1776
  /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
1777
  union
1778
  {
1779
    bfd_signed_vma refcount;
1780
    bfd_vma offset;
1781
    struct got_entry *ent;
1782
  } got;
1783
};
1784
1785
/* The same for PLT.  */
1786
struct plt_entry
1787
{
1788
  struct plt_entry *next;
1789
1790
  bfd_vma addend;
1791
1792
  union
1793
  {
1794
    bfd_signed_vma refcount;
1795
    bfd_vma offset;
1796
  } plt;
1797
};
1798
1799
struct ppc64_elf_obj_tdata
1800
{
1801
  struct elf_obj_tdata elf;
1802
1803
  /* Shortcuts to dynamic linker sections.  */
1804
  asection *got;
1805
  asection *relgot;
1806
1807
  /* Used during garbage collection.  We attach global symbols defined
1808
     on removed .opd entries to this section so that the sym is removed.  */
1809
  asection *deleted_section;
1810
1811
  /* TLS local dynamic got entry handling.  Support for multiple GOT
1812
     sections means we potentially need one of these for each input bfd.  */
1813
  struct got_entry tlsld_got;
1814
1815
  /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1816
     the reloc to be in the range -32768 to 32767.  */
1817
  unsigned int has_small_toc_reloc : 1;
1818
1819
  /* Set if toc/got ha relocs detected not using r2, or lo reloc
1820
     instruction not one we handle.  */
1821
  unsigned int unexpected_toc_insn : 1;
1822
1823
  /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1824
     this file.  */
1825
  unsigned int has_optrel : 1;
1826
};
1827
1828
#define ppc64_elf_tdata(bfd) \
1829
0
  ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1830
1831
#define ppc64_tlsld_got(bfd) \
1832
0
  (&ppc64_elf_tdata (bfd)->tlsld_got)
1833
1834
/* Override the generic function because we store some extras.  */
1835
1836
static bool
1837
ppc64_elf_mkobject (bfd *abfd)
1838
36.8k
{
1839
36.8k
  return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata));
1840
36.8k
}
1841
1842
/* Fix bad default arch selected for a 64 bit input bfd when the
1843
   default is 32 bit.  Also select arch based on apuinfo.  */
1844
1845
static bool
1846
ppc64_elf_object_p (bfd *abfd)
1847
646
{
1848
646
  if (!abfd->arch_info->the_default)
1849
0
    return true;
1850
1851
646
  if (abfd->arch_info->bits_per_word == 32)
1852
0
    {
1853
0
      Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1854
1855
0
      if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1856
0
  {
1857
    /* Relies on arch after 32 bit default being 64 bit default.  */
1858
0
    abfd->arch_info = abfd->arch_info->next;
1859
0
    BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1860
0
  }
1861
0
    }
1862
646
  return _bfd_elf_ppc_set_arch (abfd);
1863
646
}
1864
1865
/* Support for core dump NOTE sections.  */
1866
1867
static bool
1868
ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1869
0
{
1870
0
  size_t offset, size;
1871
1872
0
  if (note->descsz != 504)
1873
0
    return false;
1874
1875
  /* pr_cursig */
1876
0
  elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1877
1878
  /* pr_pid */
1879
0
  elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1880
1881
  /* pr_reg */
1882
0
  offset = 112;
1883
0
  size = 384;
1884
1885
  /* Make a ".reg/999" section.  */
1886
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1887
0
            size, note->descpos + offset);
1888
0
}
1889
1890
static bool
1891
ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1892
0
{
1893
0
  if (note->descsz != 136)
1894
0
    return false;
1895
1896
0
  elf_tdata (abfd)->core->pid
1897
0
    = bfd_get_32 (abfd, note->descdata + 24);
1898
0
  elf_tdata (abfd)->core->program
1899
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1900
0
  elf_tdata (abfd)->core->command
1901
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1902
1903
0
  return true;
1904
0
}
1905
1906
static char *
1907
ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1908
         ...)
1909
0
{
1910
0
  switch (note_type)
1911
0
    {
1912
0
    default:
1913
0
      return NULL;
1914
1915
0
    case NT_PRPSINFO:
1916
0
      {
1917
0
  char data[136] ATTRIBUTE_NONSTRING;
1918
0
  va_list ap;
1919
1920
0
  va_start (ap, note_type);
1921
0
  memset (data, 0, sizeof (data));
1922
0
  strncpy (data + 40, va_arg (ap, const char *), 16);
1923
#if GCC_VERSION == 8000 || GCC_VERSION == 8001
1924
  DIAGNOSTIC_PUSH;
1925
  /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1926
     -Wstringop-truncation:
1927
     https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1928
   */
1929
  DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1930
#endif
1931
0
  strncpy (data + 56, va_arg (ap, const char *), 80);
1932
#if GCC_VERSION == 8000 || GCC_VERSION == 8001
1933
  DIAGNOSTIC_POP;
1934
#endif
1935
0
  va_end (ap);
1936
0
  return elfcore_write_note (abfd, buf, bufsiz,
1937
0
           "CORE", note_type, data, sizeof (data));
1938
0
      }
1939
1940
0
    case NT_PRSTATUS:
1941
0
      {
1942
0
  char data[504];
1943
0
  va_list ap;
1944
0
  long pid;
1945
0
  int cursig;
1946
0
  const void *greg;
1947
1948
0
  va_start (ap, note_type);
1949
0
  memset (data, 0, 112);
1950
0
  pid = va_arg (ap, long);
1951
0
  bfd_put_32 (abfd, pid, data + 32);
1952
0
  cursig = va_arg (ap, int);
1953
0
  bfd_put_16 (abfd, cursig, data + 12);
1954
0
  greg = va_arg (ap, const void *);
1955
0
  memcpy (data + 112, greg, 384);
1956
0
  memset (data + 496, 0, 8);
1957
0
  va_end (ap);
1958
0
  return elfcore_write_note (abfd, buf, bufsiz,
1959
0
           "CORE", note_type, data, sizeof (data));
1960
0
      }
1961
0
    }
1962
0
}
1963
1964
/* Add extra PPC sections.  */
1965
1966
static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1967
{
1968
  { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
1969
  { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1970
  { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1971
  { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1972
  { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1973
  { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1974
  { NULL,         0,  0, 0,      0 }
1975
};
1976
1977
enum _ppc64_sec_type {
1978
  sec_normal = 0,
1979
  sec_opd = 1,
1980
  sec_toc = 2,
1981
  sec_stub = 3
1982
};
1983
1984
struct _ppc64_elf_section_data
1985
{
1986
  struct bfd_elf_section_data elf;
1987
1988
  union
1989
  {
1990
    /* An array with one entry for each opd function descriptor,
1991
       and some spares since opd entries may be either 16 or 24 bytes.  */
1992
0
#define OPD_NDX(OFF) ((OFF) >> 4)
1993
    struct _opd_sec_data
1994
    {
1995
      /* Points to the function code section for local opd entries.  */
1996
      asection **func_sec;
1997
1998
      /* After editing .opd, adjust references to opd local syms.  */
1999
      long *adjust;
2000
2001
      union
2002
      {
2003
  /* A copy of relocs before they are modified for --emit-relocs.  */
2004
  Elf_Internal_Rela *relocs;
2005
2006
  /* Section contents.  */
2007
  bfd_byte *contents;
2008
      } u;
2009
    } opd;
2010
2011
    /* An array for toc sections, indexed by offset/8.  */
2012
    struct _toc_sec_data
2013
    {
2014
      /* Specifies the relocation symbol index used at a given toc offset.  */
2015
      unsigned *symndx;
2016
2017
      /* And the relocation addend.  */
2018
      bfd_vma *add;
2019
    } toc;
2020
2021
    /* Stub debugging.  */
2022
    struct ppc_stub_hash_entry *last_ent;
2023
  } u;
2024
2025
  enum _ppc64_sec_type sec_type:2;
2026
2027
  /* Flag set when small branches are detected.  Used to
2028
     select suitable defaults for the stub group size.  */
2029
  unsigned int has_14bit_branch:1;
2030
2031
  /* Flag set when PLTCALL relocs are detected.  */
2032
  unsigned int has_pltcall:1;
2033
2034
  /* Flag set when section has PLT/GOT/TOC relocations that can be
2035
     optimised.  */
2036
  unsigned int has_optrel:1;
2037
};
2038
2039
#define ppc64_elf_section_data(sec) \
2040
755
  ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2041
2042
static bool
2043
ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2044
9.10k
{
2045
9.10k
  struct _ppc64_elf_section_data *sdata;
2046
2047
9.10k
  sdata = bfd_zalloc (abfd, sizeof (*sdata));
2048
9.10k
  if (sdata == NULL)
2049
0
    return false;
2050
9.10k
  sec->used_by_bfd = sdata;
2051
2052
9.10k
  return _bfd_elf_new_section_hook (abfd, sec);
2053
9.10k
}
2054
2055
static bool
2056
ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2057
8.46k
{
2058
8.46k
  const char *name = hdr->bfd_section->name;
2059
2060
8.46k
  if (startswith (name, ".sbss")
2061
8.37k
      || startswith (name, ".sdata"))
2062
172
    hdr->bfd_section->flags |= SEC_SMALL_DATA;
2063
2064
8.46k
  return true;
2065
8.46k
}
2066
2067
static struct _opd_sec_data *
2068
get_opd_info (asection * sec)
2069
125
{
2070
125
  if (sec != NULL
2071
125
      && ppc64_elf_section_data (sec) != NULL
2072
125
      && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2073
119
    return &ppc64_elf_section_data (sec)->u.opd;
2074
6
  return NULL;
2075
125
}
2076

2077
/* Parameters for the qsort hook.  */
2078
static bool synthetic_relocatable;
2079
static const asection *synthetic_opd;
2080
2081
/* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
2082
2083
static int
2084
compare_symbols (const void *ap, const void *bp)
2085
0
{
2086
0
  const asymbol *a = *(const asymbol **) ap;
2087
0
  const asymbol *b = *(const asymbol **) bp;
2088
2089
  /* Section symbols first.  */
2090
0
  if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2091
0
    return -1;
2092
0
  if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2093
0
    return 1;
2094
2095
  /* then .opd symbols.  */
2096
0
  if (synthetic_opd != NULL)
2097
0
    {
2098
0
      if (strcmp (a->section->name, ".opd") == 0
2099
0
    && strcmp (b->section->name, ".opd") != 0)
2100
0
  return -1;
2101
0
      if (strcmp (a->section->name, ".opd") != 0
2102
0
    && strcmp (b->section->name, ".opd") == 0)
2103
0
  return 1;
2104
0
    }
2105
2106
  /* then other code symbols.  */
2107
0
  if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2108
0
       == (SEC_CODE | SEC_ALLOC))
2109
0
      && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2110
0
    != (SEC_CODE | SEC_ALLOC)))
2111
0
    return -1;
2112
2113
0
  if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2114
0
       != (SEC_CODE | SEC_ALLOC))
2115
0
      && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2116
0
    == (SEC_CODE | SEC_ALLOC)))
2117
0
    return 1;
2118
2119
0
  if (synthetic_relocatable)
2120
0
    {
2121
0
      if (a->section->id < b->section->id)
2122
0
  return -1;
2123
2124
0
      if (a->section->id > b->section->id)
2125
0
  return 1;
2126
0
    }
2127
2128
0
  if (a->value + a->section->vma < b->value + b->section->vma)
2129
0
    return -1;
2130
2131
0
  if (a->value + a->section->vma > b->value + b->section->vma)
2132
0
    return 1;
2133
2134
  /* For syms with the same value, prefer strong dynamic global function
2135
     syms over other syms.  */
2136
0
  if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2137
0
    return -1;
2138
2139
0
  if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2140
0
    return 1;
2141
2142
0
  if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2143
0
    return -1;
2144
2145
0
  if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2146
0
    return 1;
2147
2148
0
  if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2149
0
    return -1;
2150
2151
0
  if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2152
0
    return 1;
2153
2154
0
  if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2155
0
    return -1;
2156
2157
0
  if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2158
0
    return 1;
2159
2160
  /* Finally, sort on where the symbol is in memory.  The symbols will
2161
     be in at most two malloc'd blocks, one for static syms, one for
2162
     dynamic syms, and we distinguish the two blocks above by testing
2163
     BSF_DYNAMIC.  Since we are sorting the symbol pointers which were
2164
     originally in the same order as the symbols (and we're not
2165
     sorting the symbols themselves), this ensures a stable sort.  */
2166
0
  if (a < b)
2167
0
    return -1;
2168
0
  if (a > b)
2169
0
    return 1;
2170
0
  return 0;
2171
0
}
2172
2173
/* Search SYMS for a symbol of the given VALUE.  */
2174
2175
static asymbol *
2176
sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2177
         bfd_vma value)
2178
0
{
2179
0
  size_t mid;
2180
2181
0
  if (id == (unsigned) -1)
2182
0
    {
2183
0
      while (lo < hi)
2184
0
  {
2185
0
    mid = (lo + hi) >> 1;
2186
0
    if (syms[mid]->value + syms[mid]->section->vma < value)
2187
0
      lo = mid + 1;
2188
0
    else if (syms[mid]->value + syms[mid]->section->vma > value)
2189
0
      hi = mid;
2190
0
    else
2191
0
      return syms[mid];
2192
0
  }
2193
0
    }
2194
0
  else
2195
0
    {
2196
0
      while (lo < hi)
2197
0
  {
2198
0
    mid = (lo + hi) >> 1;
2199
0
    if (syms[mid]->section->id < id)
2200
0
      lo = mid + 1;
2201
0
    else if (syms[mid]->section->id > id)
2202
0
      hi = mid;
2203
0
    else if (syms[mid]->value < value)
2204
0
      lo = mid + 1;
2205
0
    else if (syms[mid]->value > value)
2206
0
      hi = mid;
2207
0
    else
2208
0
      return syms[mid];
2209
0
  }
2210
0
    }
2211
0
  return NULL;
2212
0
}
2213
2214
static bool
2215
section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2216
0
{
2217
0
  bfd_vma vma = *(bfd_vma *) ptr;
2218
0
  return ((section->flags & SEC_ALLOC) != 0
2219
0
    && section->vma <= vma
2220
0
    && vma < section->vma + section->size);
2221
0
}
2222
2223
/* Create synthetic symbols, effectively restoring "dot-symbol" function
2224
   entry syms.  Also generate @plt symbols for the glink branch table.
2225
   Returns count of synthetic symbols in RET or -1 on error.  */
2226
2227
static long
2228
ppc64_elf_get_synthetic_symtab (bfd *abfd,
2229
        long static_count, asymbol **static_syms,
2230
        long dyn_count, asymbol **dyn_syms,
2231
        asymbol **ret)
2232
26
{
2233
26
  asymbol *s;
2234
26
  size_t i, j, count;
2235
26
  char *names;
2236
26
  size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2237
26
  asection *opd = NULL;
2238
26
  bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2239
26
  asymbol **syms;
2240
26
  int abi = abiversion (abfd);
2241
2242
26
  *ret = NULL;
2243
2244
26
  if (abi < 2)
2245
24
    {
2246
24
      opd = bfd_get_section_by_name (abfd, ".opd");
2247
24
      if (opd == NULL && abi == 1)
2248
0
  return 0;
2249
24
    }
2250
2251
26
  syms = NULL;
2252
26
  codesecsym = 0;
2253
26
  codesecsymend = 0;
2254
26
  secsymend = 0;
2255
26
  opdsymend = 0;
2256
26
  symcount = 0;
2257
26
  if (opd != NULL)
2258
0
    {
2259
0
      symcount = static_count;
2260
0
      if (!relocatable)
2261
0
  symcount += dyn_count;
2262
0
      if (symcount == 0)
2263
0
  return 0;
2264
2265
0
      syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2266
0
      if (syms == NULL)
2267
0
  return -1;
2268
2269
0
      if (!relocatable && static_count != 0 && dyn_count != 0)
2270
0
  {
2271
    /* Use both symbol tables.  */
2272
0
    memcpy (syms, static_syms, static_count * sizeof (*syms));
2273
0
    memcpy (syms + static_count, dyn_syms,
2274
0
      (dyn_count + 1) * sizeof (*syms));
2275
0
  }
2276
0
      else if (!relocatable && static_count == 0)
2277
0
  memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2278
0
      else
2279
0
  memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2280
2281
      /* Trim uninteresting symbols.  Interesting symbols are section,
2282
   function, and notype symbols.  */
2283
0
      for (i = 0, j = 0; i < symcount; ++i)
2284
0
  if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2285
0
             | BSF_RELC | BSF_SRELC)) == 0)
2286
0
    syms[j++] = syms[i];
2287
0
      symcount = j;
2288
2289
0
      synthetic_relocatable = relocatable;
2290
0
      synthetic_opd = opd;
2291
0
      qsort (syms, symcount, sizeof (*syms), compare_symbols);
2292
2293
0
      if (!relocatable && symcount > 1)
2294
0
  {
2295
    /* Trim duplicate syms, since we may have merged the normal
2296
       and dynamic symbols.  Actually, we only care about syms
2297
       that have different values, so trim any with the same
2298
       value.  Don't consider ifunc and ifunc resolver symbols
2299
       duplicates however, because GDB wants to know whether a
2300
       text symbol is an ifunc resolver.  */
2301
0
    for (i = 1, j = 1; i < symcount; ++i)
2302
0
      {
2303
0
        const asymbol *s0 = syms[i - 1];
2304
0
        const asymbol *s1 = syms[i];
2305
2306
0
        if ((s0->value + s0->section->vma
2307
0
       != s1->value + s1->section->vma)
2308
0
      || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2309
0
          != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2310
0
    syms[j++] = syms[i];
2311
0
      }
2312
0
    symcount = j;
2313
0
  }
2314
2315
0
      i = 0;
2316
      /* Note that here and in compare_symbols we can't compare opd and
2317
   sym->section directly.  With separate debug info files, the
2318
   symbols will be extracted from the debug file while abfd passed
2319
   to this function is the real binary.  */
2320
0
      if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2321
0
    && strcmp (syms[i]->section->name, ".opd") == 0)
2322
0
  ++i;
2323
0
      codesecsym = i;
2324
2325
0
      for (; i < symcount; ++i)
2326
0
  if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2327
0
           | SEC_THREAD_LOCAL))
2328
0
       != (SEC_CODE | SEC_ALLOC))
2329
0
      || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2330
0
    break;
2331
0
      codesecsymend = i;
2332
2333
0
      for (; i < symcount; ++i)
2334
0
  if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2335
0
    break;
2336
0
      secsymend = i;
2337
2338
0
      for (; i < symcount; ++i)
2339
0
  if (strcmp (syms[i]->section->name, ".opd") != 0)
2340
0
    break;
2341
0
      opdsymend = i;
2342
2343
0
      for (; i < symcount; ++i)
2344
0
  if (((syms[i]->section->flags
2345
0
        & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2346
0
      != (SEC_CODE | SEC_ALLOC))
2347
0
    break;
2348
0
      symcount = i;
2349
0
    }
2350
26
  count = 0;
2351
2352
26
  if (relocatable)
2353
16
    {
2354
16
      bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2355
16
      arelent *r;
2356
16
      size_t size;
2357
16
      size_t relcount;
2358
2359
16
      if (opdsymend == secsymend)
2360
16
  goto done;
2361
2362
0
      slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2363
0
      relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2364
0
      if (relcount == 0)
2365
0
  goto done;
2366
2367
0
      if (!(*slurp_relocs) (abfd, opd, static_syms, false))
2368
0
  {
2369
0
    count = -1;
2370
0
    goto done;
2371
0
  }
2372
2373
0
      size = 0;
2374
0
      for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2375
0
  {
2376
0
    asymbol *sym;
2377
2378
0
    while (r < opd->relocation + relcount
2379
0
     && r->address < syms[i]->value + opd->vma)
2380
0
      ++r;
2381
2382
0
    if (r == opd->relocation + relcount)
2383
0
      break;
2384
2385
0
    if (r->address != syms[i]->value + opd->vma)
2386
0
      continue;
2387
2388
0
    if (r->howto->type != R_PPC64_ADDR64)
2389
0
      continue;
2390
2391
0
    sym = *r->sym_ptr_ptr;
2392
0
    if (!sym_exists_at (syms, opdsymend, symcount,
2393
0
            sym->section->id, sym->value + r->addend))
2394
0
      {
2395
0
        ++count;
2396
0
        size += sizeof (asymbol);
2397
0
        size += strlen (syms[i]->name) + 2;
2398
0
      }
2399
0
  }
2400
2401
0
      if (size == 0)
2402
0
  goto done;
2403
0
      s = *ret = bfd_malloc (size);
2404
0
      if (s == NULL)
2405
0
  {
2406
0
    count = -1;
2407
0
    goto done;
2408
0
  }
2409
2410
0
      names = (char *) (s + count);
2411
2412
0
      for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2413
0
  {
2414
0
    asymbol *sym;
2415
2416
0
    while (r < opd->relocation + relcount
2417
0
     && r->address < syms[i]->value + opd->vma)
2418
0
      ++r;
2419
2420
0
    if (r == opd->relocation + relcount)
2421
0
      break;
2422
2423
0
    if (r->address != syms[i]->value + opd->vma)
2424
0
      continue;
2425
2426
0
    if (r->howto->type != R_PPC64_ADDR64)
2427
0
      continue;
2428
2429
0
    sym = *r->sym_ptr_ptr;
2430
0
    if (!sym_exists_at (syms, opdsymend, symcount,
2431
0
            sym->section->id, sym->value + r->addend))
2432
0
      {
2433
0
        size_t len;
2434
2435
0
        *s = *syms[i];
2436
0
        s->flags |= BSF_SYNTHETIC;
2437
0
        s->section = sym->section;
2438
0
        s->value = sym->value + r->addend;
2439
0
        s->name = names;
2440
0
        *names++ = '.';
2441
0
        len = strlen (syms[i]->name);
2442
0
        memcpy (names, syms[i]->name, len + 1);
2443
0
        names += len + 1;
2444
        /* Have udata.p point back to the original symbol this
2445
     synthetic symbol was derived from.  */
2446
0
        s->udata.p = syms[i];
2447
0
        s++;
2448
0
      }
2449
0
  }
2450
0
    }
2451
10
  else
2452
10
    {
2453
10
      bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2454
10
      bfd_byte *contents = NULL;
2455
10
      size_t size;
2456
10
      size_t plt_count = 0;
2457
10
      bfd_vma glink_vma = 0, resolv_vma = 0;
2458
10
      asection *dynamic, *glink = NULL, *relplt = NULL;
2459
10
      arelent *p;
2460
2461
10
      if (opd != NULL
2462
0
    && ((opd->flags & SEC_HAS_CONTENTS) == 0
2463
0
        || !bfd_malloc_and_get_section (abfd, opd, &contents)))
2464
0
  {
2465
0
  free_contents_and_exit_err:
2466
0
    count = -1;
2467
10
  free_contents_and_exit:
2468
10
    free (contents);
2469
10
    goto done;
2470
0
  }
2471
2472
10
      size = 0;
2473
10
      for (i = secsymend; i < opdsymend; ++i)
2474
0
  {
2475
0
    bfd_vma ent;
2476
2477
    /* Ignore bogus symbols.  */
2478
0
    if (syms[i]->value > opd->size - 8)
2479
0
      continue;
2480
2481
0
    ent = bfd_get_64 (abfd, contents + syms[i]->value);
2482
0
    if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2483
0
      {
2484
0
        ++count;
2485
0
        size += sizeof (asymbol);
2486
0
        size += strlen (syms[i]->name) + 2;
2487
0
      }
2488
0
  }
2489
2490
      /* Get start of .glink stubs from DT_PPC64_GLINK.  */
2491
10
      if (dyn_count != 0
2492
0
    && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2493
0
  {
2494
0
    bfd_byte *dynbuf, *extdyn, *extdynend;
2495
0
    size_t extdynsize;
2496
0
    void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2497
2498
0
    if ((dynamic->flags & SEC_HAS_CONTENTS) == 0
2499
0
        || !bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2500
0
      goto free_contents_and_exit_err;
2501
2502
0
    extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2503
0
    swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2504
2505
0
    for (extdyn = dynbuf, extdynend = dynbuf + dynamic->size;
2506
0
         (size_t) (extdynend - extdyn) >= extdynsize;
2507
0
         extdyn += extdynsize)
2508
0
      {
2509
0
        Elf_Internal_Dyn dyn;
2510
0
        (*swap_dyn_in) (abfd, extdyn, &dyn);
2511
2512
0
        if (dyn.d_tag == DT_NULL)
2513
0
    break;
2514
2515
0
        if (dyn.d_tag == DT_PPC64_GLINK)
2516
0
    {
2517
      /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2518
         See comment in ppc64_elf_finish_dynamic_sections. */
2519
0
      glink_vma = dyn.d_un.d_val + 8 * 4;
2520
      /* The .glink section usually does not survive the final
2521
         link; search for the section (usually .text) where the
2522
         glink stubs now reside.  */
2523
0
      glink = bfd_sections_find_if (abfd, section_covers_vma,
2524
0
            &glink_vma);
2525
0
      break;
2526
0
    }
2527
0
      }
2528
2529
0
    free (dynbuf);
2530
0
  }
2531
2532
10
      if (glink != NULL)
2533
0
  {
2534
    /* Determine __glink trampoline by reading the relative branch
2535
       from the first glink stub.  */
2536
0
    bfd_byte buf[4];
2537
0
    unsigned int off = 0;
2538
2539
0
    while (bfd_get_section_contents (abfd, glink, buf,
2540
0
             glink_vma + off - glink->vma, 4))
2541
0
      {
2542
0
        unsigned int insn = bfd_get_32 (abfd, buf);
2543
0
        insn ^= B_DOT;
2544
0
        if ((insn & ~0x3fffffc) == 0)
2545
0
    {
2546
0
      resolv_vma
2547
0
        = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2548
0
      break;
2549
0
    }
2550
0
        off += 4;
2551
0
        if (off > 4)
2552
0
    break;
2553
0
      }
2554
2555
0
    if (resolv_vma)
2556
0
      size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2557
2558
0
    relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2559
0
    if (relplt != NULL)
2560
0
      {
2561
0
        slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2562
0
        if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
2563
0
    goto free_contents_and_exit_err;
2564
2565
0
        plt_count = NUM_SHDR_ENTRIES (&elf_section_data (relplt)->this_hdr);
2566
0
        size += plt_count * sizeof (asymbol);
2567
2568
0
        p = relplt->relocation;
2569
0
        for (i = 0; i < plt_count; i++, p++)
2570
0
    {
2571
0
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2572
0
      if (p->addend != 0)
2573
0
        size += sizeof ("+0x") - 1 + 16;
2574
0
    }
2575
0
      }
2576
0
  }
2577
2578
10
      if (size == 0)
2579
10
  goto free_contents_and_exit;
2580
0
      s = *ret = bfd_malloc (size);
2581
0
      if (s == NULL)
2582
0
  goto free_contents_and_exit_err;
2583
2584
0
      names = (char *) (s + count + plt_count + (resolv_vma != 0));
2585
2586
0
      for (i = secsymend; i < opdsymend; ++i)
2587
0
  {
2588
0
    bfd_vma ent;
2589
2590
0
    if (syms[i]->value > opd->size - 8)
2591
0
      continue;
2592
2593
0
    ent = bfd_get_64 (abfd, contents + syms[i]->value);
2594
0
    if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2595
0
      {
2596
0
        size_t lo, hi;
2597
0
        size_t len;
2598
0
        asection *sec = abfd->sections;
2599
2600
0
        *s = *syms[i];
2601
0
        lo = codesecsym;
2602
0
        hi = codesecsymend;
2603
0
        while (lo < hi)
2604
0
    {
2605
0
      size_t mid = (lo + hi) >> 1;
2606
0
      if (syms[mid]->section->vma < ent)
2607
0
        lo = mid + 1;
2608
0
      else if (syms[mid]->section->vma > ent)
2609
0
        hi = mid;
2610
0
      else
2611
0
        {
2612
0
          sec = syms[mid]->section;
2613
0
          break;
2614
0
        }
2615
0
    }
2616
2617
0
        if (lo >= hi && lo > codesecsym)
2618
0
    sec = syms[lo - 1]->section;
2619
2620
0
        for (; sec != NULL; sec = sec->next)
2621
0
    {
2622
0
      if (sec->vma > ent)
2623
0
        break;
2624
      /* SEC_LOAD may not be set if SEC is from a separate debug
2625
         info file.  */
2626
0
      if ((sec->flags & SEC_ALLOC) == 0)
2627
0
        break;
2628
0
      if ((sec->flags & SEC_CODE) != 0)
2629
0
        s->section = sec;
2630
0
    }
2631
0
        s->flags |= BSF_SYNTHETIC;
2632
0
        s->value = ent - s->section->vma;
2633
0
        s->name = names;
2634
0
        *names++ = '.';
2635
0
        len = strlen (syms[i]->name);
2636
0
        memcpy (names, syms[i]->name, len + 1);
2637
0
        names += len + 1;
2638
        /* Have udata.p point back to the original symbol this
2639
     synthetic symbol was derived from.  */
2640
0
        s->udata.p = syms[i];
2641
0
        s++;
2642
0
      }
2643
0
  }
2644
0
      free (contents);
2645
2646
0
      if (glink != NULL && relplt != NULL)
2647
0
  {
2648
0
    if (resolv_vma)
2649
0
      {
2650
        /* Add a symbol for the main glink trampoline.  */
2651
0
        memset (s, 0, sizeof *s);
2652
0
        s->the_bfd = abfd;
2653
0
        s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2654
0
        s->section = glink;
2655
0
        s->value = resolv_vma - glink->vma;
2656
0
        s->name = names;
2657
0
        memcpy (names, "__glink_PLTresolve",
2658
0
          sizeof ("__glink_PLTresolve"));
2659
0
        names += sizeof ("__glink_PLTresolve");
2660
0
        s++;
2661
0
        count++;
2662
0
      }
2663
2664
    /* FIXME: It would be very much nicer to put sym@plt on the
2665
       stub rather than on the glink branch table entry.  The
2666
       objdump disassembler would then use a sensible symbol
2667
       name on plt calls.  The difficulty in doing so is
2668
       a) finding the stubs, and,
2669
       b) matching stubs against plt entries, and,
2670
       c) there can be multiple stubs for a given plt entry.
2671
2672
       Solving (a) could be done by code scanning, but older
2673
       ppc64 binaries used different stubs to current code.
2674
       (b) is the tricky one since you need to known the toc
2675
       pointer for at least one function that uses a pic stub to
2676
       be able to calculate the plt address referenced.
2677
       (c) means gdb would need to set multiple breakpoints (or
2678
       find the glink branch itself) when setting breakpoints
2679
       for pending shared library loads.  */
2680
0
    p = relplt->relocation;
2681
0
    for (i = 0; i < plt_count; i++, p++)
2682
0
      {
2683
0
        size_t len;
2684
2685
0
        *s = **p->sym_ptr_ptr;
2686
        /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
2687
     we are defining a symbol, ensure one of them is set.  */
2688
0
        if ((s->flags & BSF_LOCAL) == 0)
2689
0
    s->flags |= BSF_GLOBAL;
2690
0
        s->flags |= BSF_SYNTHETIC;
2691
0
        s->section = glink;
2692
0
        s->value = glink_vma - glink->vma;
2693
0
        s->name = names;
2694
0
        s->udata.p = NULL;
2695
0
        len = strlen ((*p->sym_ptr_ptr)->name);
2696
0
        memcpy (names, (*p->sym_ptr_ptr)->name, len);
2697
0
        names += len;
2698
0
        if (p->addend != 0)
2699
0
    {
2700
0
      memcpy (names, "+0x", sizeof ("+0x") - 1);
2701
0
      names += sizeof ("+0x") - 1;
2702
0
      bfd_sprintf_vma (abfd, names, p->addend);
2703
0
      names += strlen (names);
2704
0
    }
2705
0
        memcpy (names, "@plt", sizeof ("@plt"));
2706
0
        names += sizeof ("@plt");
2707
0
        s++;
2708
0
        if (abi < 2)
2709
0
    {
2710
0
      glink_vma += 8;
2711
0
      if (i >= 0x8000)
2712
0
        glink_vma += 4;
2713
0
    }
2714
0
        else
2715
0
    glink_vma += 4;
2716
0
      }
2717
0
    count += plt_count;
2718
0
  }
2719
0
    }
2720
2721
26
 done:
2722
26
  free (syms);
2723
26
  return count;
2724
26
}
2725

2726
/* The following functions are specific to the ELF linker, while
2727
   functions above are used generally.  Those named ppc64_elf_* are
2728
   called by the main ELF linker code.  They appear in this file more
2729
   or less in the order in which they are called.  eg.
2730
   ppc64_elf_check_relocs is called early in the link process,
2731
   ppc64_elf_finish_dynamic_sections is one of the last functions
2732
   called.
2733
2734
   PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2735
   functions have both a function code symbol and a function descriptor
2736
   symbol.  A call to foo in a relocatable object file looks like:
2737
2738
   .    .text
2739
   .  x:
2740
   .    bl  .foo
2741
   .    nop
2742
2743
   The function definition in another object file might be:
2744
2745
   .    .section .opd
2746
   .  foo:  .quad .foo
2747
   .    .quad .TOC.@tocbase
2748
   .    .quad 0
2749
   .
2750
   .    .text
2751
   .  .foo: blr
2752
2753
   When the linker resolves the call during a static link, the branch
2754
   unsurprisingly just goes to .foo and the .opd information is unused.
2755
   If the function definition is in a shared library, things are a little
2756
   different:  The call goes via a plt call stub, the opd information gets
2757
   copied to the plt, and the linker patches the nop.
2758
2759
   .  x:
2760
   .    bl  .foo_stub
2761
   .    ld  2,40(1)
2762
   .
2763
   .
2764
   .  .foo_stub:
2765
   .    std 2,40(1)     # in practice, the call stub
2766
   .    addis 11,2,Lfoo@toc@ha  # is slightly optimized, but
2767
   .    addi  11,11,Lfoo@toc@l  # this is the general idea
2768
   .    ld  12,0(11)
2769
   .    ld  2,8(11)
2770
   .    mtctr 12
2771
   .    ld  11,16(11)
2772
   .    bctr
2773
   .
2774
   .    .section .plt
2775
   .  Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2776
2777
   The "reloc ()" notation is supposed to indicate that the linker emits
2778
   an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
2779
   copying.
2780
2781
   What are the difficulties here?  Well, firstly, the relocations
2782
   examined by the linker in check_relocs are against the function code
2783
   sym .foo, while the dynamic relocation in the plt is emitted against
2784
   the function descriptor symbol, foo.  Somewhere along the line, we need
2785
   to carefully copy dynamic link information from one symbol to the other.
2786
   Secondly, the generic part of the elf linker will make .foo a dynamic
2787
   symbol as is normal for most other backends.  We need foo dynamic
2788
   instead, at least for an application final link.  However, when
2789
   creating a shared library containing foo, we need to have both symbols
2790
   dynamic so that references to .foo are satisfied during the early
2791
   stages of linking.  Otherwise the linker might decide to pull in a
2792
   definition from some other object, eg. a static library.
2793
2794
   Update: As of August 2004, we support a new convention.  Function
2795
   calls may use the function descriptor symbol, ie. "bl foo".  This
2796
   behaves exactly as "bl .foo".  */
2797
2798
/* Of those relocs that might be copied as dynamic relocs, this
2799
   function selects those that must be copied when linking a shared
2800
   library or PIE, even when the symbol is local.  */
2801
2802
static int
2803
must_be_dyn_reloc (struct bfd_link_info *info,
2804
       enum elf_ppc64_reloc_type r_type)
2805
0
{
2806
0
  switch (r_type)
2807
0
    {
2808
0
    default:
2809
      /* Only relative relocs can be resolved when the object load
2810
   address isn't fixed.  DTPREL64 is excluded because the
2811
   dynamic linker needs to differentiate global dynamic from
2812
   local dynamic __tls_index pairs when PPC64_OPT_TLS is set.  */
2813
0
      return 1;
2814
2815
0
    case R_PPC64_REL32:
2816
0
    case R_PPC64_REL64:
2817
0
    case R_PPC64_REL30:
2818
0
    case R_PPC64_TOC16:
2819
0
    case R_PPC64_TOC16_DS:
2820
0
    case R_PPC64_TOC16_LO:
2821
0
    case R_PPC64_TOC16_HI:
2822
0
    case R_PPC64_TOC16_HA:
2823
0
    case R_PPC64_TOC16_LO_DS:
2824
0
      return 0;
2825
2826
0
    case R_PPC64_TPREL16:
2827
0
    case R_PPC64_TPREL16_LO:
2828
0
    case R_PPC64_TPREL16_HI:
2829
0
    case R_PPC64_TPREL16_HA:
2830
0
    case R_PPC64_TPREL16_DS:
2831
0
    case R_PPC64_TPREL16_LO_DS:
2832
0
    case R_PPC64_TPREL16_HIGH:
2833
0
    case R_PPC64_TPREL16_HIGHA:
2834
0
    case R_PPC64_TPREL16_HIGHER:
2835
0
    case R_PPC64_TPREL16_HIGHERA:
2836
0
    case R_PPC64_TPREL16_HIGHEST:
2837
0
    case R_PPC64_TPREL16_HIGHESTA:
2838
0
    case R_PPC64_TPREL64:
2839
0
    case R_PPC64_TPREL34:
2840
      /* These relocations are relative but in a shared library the
2841
   linker doesn't know the thread pointer base.  */
2842
0
      return bfd_link_dll (info);
2843
0
    }
2844
0
}
2845
2846
/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2847
   copying dynamic variables from a shared lib into an app's .dynbss
2848
   section, and instead use a dynamic relocation to point into the
2849
   shared lib.  With code that gcc generates it is vital that this be
2850
   enabled;  In the PowerPC64 ELFv1 ABI the address of a function is
2851
   actually the address of a function descriptor which resides in the
2852
   .opd section.  gcc uses the descriptor directly rather than going
2853
   via the GOT as some other ABIs do, which means that initialized
2854
   function pointers reference the descriptor.  Thus, a function
2855
   pointer initialized to the address of a function in a shared
2856
   library will either require a .dynbss copy and a copy reloc, or a
2857
   dynamic reloc.  Using a .dynbss copy redefines the function
2858
   descriptor symbol to point to the copy.  This presents a problem as
2859
   a PLT entry for that function is also initialized from the function
2860
   descriptor symbol and the copy may not be initialized first.  */
2861
0
#define ELIMINATE_COPY_RELOCS 1
2862
2863
/* Section name for stubs is the associated section name plus this
2864
   string.  */
2865
0
#define STUB_SUFFIX ".stub"
2866
2867
/* Linker stubs.
2868
   ppc_stub_long_branch:
2869
   Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2870
   destination, but a 24 bit branch in a stub section will reach.
2871
   .  b dest
2872
2873
   ppc_stub_plt_branch:
2874
   Similar to the above, but a 24 bit branch in the stub section won't
2875
   reach its destination.
2876
   .  addis %r12,%r2,xxx@toc@ha
2877
   .  ld  %r12,xxx@toc@l(%r12)
2878
   .  mtctr %r12
2879
   .  bctr
2880
2881
   ppc_stub_plt_call:
2882
   Used to call a function in a shared library.  If it so happens that
2883
   the plt entry referenced crosses a 64k boundary, then an extra
2884
   "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2885
   An r2save variant starts with "std %r2,40(%r1)".
2886
   .  addis %r11,%r2,xxx@toc@ha
2887
   .  ld  %r12,xxx+0@toc@l(%r11)
2888
   .  mtctr %r12
2889
   .  ld  %r2,xxx+8@toc@l(%r11)
2890
   .  ld  %r11,xxx+16@toc@l(%r11)
2891
   .  bctr
2892
2893
   ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2894
   code to adjust the value and save r2 to support multiple toc sections.
2895
   A ppc_stub_long_branch with an r2 offset looks like:
2896
   .  std %r2,40(%r1)
2897
   .  addis %r2,%r2,off@ha
2898
   .  addi  %r2,%r2,off@l
2899
   .  b dest
2900
2901
   A ppc_stub_plt_branch with an r2 offset looks like:
2902
   .  std %r2,40(%r1)
2903
   .  addis %r12,%r2,xxx@toc@ha
2904
   .  ld  %r12,xxx@toc@l(%r12)
2905
   .  addis %r2,%r2,off@ha
2906
   .  addi  %r2,%r2,off@l
2907
   .  mtctr %r12
2908
   .  bctr
2909
2910
   All of the above stubs are shown as their ELFv1 variants.  ELFv2
2911
   variants exist too, simpler for plt calls since a new toc pointer
2912
   and static chain are not loaded by the stub.  In addition, ELFv2
2913
   has some more complex stubs to handle calls marked with NOTOC
2914
   relocs from functions where r2 is not a valid toc pointer.
2915
   ppc_stub_long_branch_p9notoc:
2916
   .  mflr  %r12
2917
   .  bcl 20,31,1f
2918
   .  1:
2919
   .  mflr  %r11
2920
   .  mtlr  %r12
2921
   .  addis %r12,%r11,dest-1b@ha
2922
   .  addi  %r12,%r12,dest-1b@l
2923
   .  b dest
2924
2925
   ppc_stub_plt_branch_p9notoc:
2926
   .  mflr  %r12
2927
   .  bcl 20,31,1f
2928
   .  1:
2929
   .  mflr  %r11
2930
   .  mtlr  %r12
2931
   .  lis %r12,xxx-1b@highest
2932
   .  ori %r12,%r12,xxx-1b@higher
2933
   .  sldi  %r12,%r12,32
2934
   .  oris  %r12,%r12,xxx-1b@high
2935
   .  ori %r12,%r12,xxx-1b@l
2936
   .  add %r12,%r11,%r12
2937
   .  mtctr %r12
2938
   .  bctr
2939
2940
   ppc_stub_plt_call_p9notoc:
2941
   .  mflr  %r12
2942
   .  bcl 20,31,1f
2943
   .  1:
2944
   .  mflr  %r11
2945
   .  mtlr  %r12
2946
   .  lis %r12,xxx-1b@highest
2947
   .  ori %r12,%r12,xxx-1b@higher
2948
   .  sldi  %r12,%r12,32
2949
   .  oris  %r12,%r12,xxx-1b@high
2950
   .  ori %r12,%r12,xxx-1b@l
2951
   .  ldx %r12,%r11,%r12
2952
   .  mtctr %r12
2953
   .  bctr
2954
2955
   There are also ELFv1 power10 variants of these stubs.
2956
   ppc_stub_long_branch_notoc:
2957
   .  pla %r12,dest@pcrel
2958
   .  b dest
2959
   ppc_stub_plt_branch_notoc:
2960
   .  lis %r11,(dest-1f)@highesta34
2961
   .  ori %r11,%r11,(dest-1f)@highera34
2962
   .  sldi  %r11,%r11,34
2963
   . 1: pla %r12,dest@pcrel
2964
   .  add %r12,%r11,%r12
2965
   .  mtctr %r12
2966
   .  bctr
2967
   ppc_stub_plt_call_notoc:
2968
   .  lis %r11,(xxx-1f)@highesta34
2969
   .  ori %r11,%r11,(xxx-1f)@highera34
2970
   .  sldi  %r11,%r11,34
2971
   . 1: pla %r12,xxx@pcrel
2972
   .  ldx %r12,%r11,%r12
2973
   .  mtctr %r12
2974
   .  bctr
2975
2976
   In cases where the high instructions would add zero, they are
2977
   omitted and following instructions modified in some cases.
2978
   For example, a power10 ppc_stub_plt_call_notoc might simplify down
2979
   to
2980
   .  pld %r12,xxx@pcrel
2981
   .  mtctr %r12
2982
   .  bctr
2983
2984
   Stub variants may be merged.  For example, if printf is called from
2985
   code with the tocsave optimization (ie. r2 saved in function
2986
   prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2987
   and from other code without the tocsave optimization requiring a
2988
   ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2989
   type will be created.  Calls with the tocsave optimization will
2990
   enter this stub after the instruction saving r2.  A similar
2991
   situation exists when calls are marked with R_PPC64_REL24_NOTOC
2992
   relocations.  These require a ppc_stub_plt_call_notoc linkage stub
2993
   to call an external function like printf.  If other calls to printf
2994
   require a ppc_stub_plt_call linkage stub then a single
2995
   ppc_stub_plt_call_notoc linkage stub may be used for both types of
2996
   call.  */
2997
2998
enum ppc_stub_main_type
2999
{
3000
  ppc_stub_none,
3001
  ppc_stub_long_branch,
3002
  ppc_stub_plt_branch,
3003
  ppc_stub_plt_call,
3004
  ppc_stub_global_entry,
3005
  ppc_stub_save_res
3006
};
3007
3008
/* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
3009
   these variations.  */
3010
3011
enum ppc_stub_sub_type
3012
{
3013
  ppc_stub_toc,
3014
  ppc_stub_notoc,
3015
  ppc_stub_p9notoc
3016
};
3017
3018
struct ppc_stub_type
3019
{
3020
  ENUM_BITFIELD (ppc_stub_main_type) main : 3;
3021
  ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
3022
  unsigned int r2save : 1;
3023
};
3024
3025
/* Information on stub grouping.  */
3026
struct map_stub
3027
{
3028
  /* The stub section.  */
3029
  asection *stub_sec;
3030
  /* This is the section to which stubs in the group will be attached.  */
3031
  asection *link_sec;
3032
  /* Next group.  */
3033
  struct map_stub *next;
3034
  /* Whether to emit a copy of register save/restore functions in this
3035
     group.  */
3036
  int needs_save_res;
3037
  /* Current offset within stubs after the insn restoring lr in a
3038
     _notoc or _both stub using bcl for pc-relative addressing, or
3039
     after the insn restoring lr in a __tls_get_addr_opt plt stub.  */
3040
  unsigned int lr_restore;
3041
  /* Accumulated size of EH info emitted to describe return address
3042
     if stubs modify lr.  Does not include 17 byte FDE header.  */
3043
  unsigned int eh_size;
3044
  /* Offset in glink_eh_frame to the start of EH info for this group.  */
3045
  unsigned int eh_base;
3046
};
3047
3048
struct ppc_stub_hash_entry
3049
{
3050
  /* Base hash table entry structure.  */
3051
  struct bfd_hash_entry root;
3052
3053
  struct ppc_stub_type type;
3054
3055
  /* Group information.  */
3056
  struct map_stub *group;
3057
3058
  /* Offset within stub_sec of the beginning of this stub.  */
3059
  bfd_vma stub_offset;
3060
3061
  /* Given the symbol's value and its section we can determine its final
3062
     value when building the stubs (so the stub knows where to jump.  */
3063
  bfd_vma target_value;
3064
  asection *target_section;
3065
3066
  /* The symbol table entry, if any, that this was derived from.  */
3067
  struct ppc_link_hash_entry *h;
3068
  struct plt_entry *plt_ent;
3069
3070
  /* Symbol type.  */
3071
  unsigned char symtype;
3072
3073
  /* Symbol st_other.  */
3074
  unsigned char other;
3075
3076
  /* Debug: Track hash table traversal.  */
3077
  unsigned int id;
3078
};
3079
3080
struct ppc_branch_hash_entry
3081
{
3082
  /* Base hash table entry structure.  */
3083
  struct bfd_hash_entry root;
3084
3085
  /* Offset within branch lookup table.  */
3086
  unsigned int offset;
3087
3088
  /* Generation marker.  */
3089
  unsigned int iter;
3090
};
3091
3092
/* Used to track dynamic relocations.  */
3093
struct ppc_dyn_relocs
3094
{
3095
  struct ppc_dyn_relocs *next;
3096
3097
  /* The input section of the reloc.  */
3098
  asection *sec;
3099
3100
  /* Total number of relocs copied for the input section.  */
3101
  unsigned int count;
3102
3103
  /* Number of pc-relative relocs copied for the input section.  */
3104
  unsigned int pc_count;
3105
3106
  /* Number of relocs that might become R_PPC64_RELATIVE.  */
3107
  unsigned int rel_count;
3108
};
3109
3110
struct ppc_local_dyn_relocs
3111
{
3112
  struct ppc_local_dyn_relocs *next;
3113
3114
  /* The input section of the reloc.  */
3115
  asection *sec;
3116
3117
  /* Total number of relocs copied for the input section.  */
3118
  unsigned int count;
3119
3120
  /* Number of relocs that might become R_PPC64_RELATIVE.  */
3121
  unsigned int rel_count : 31;
3122
3123
  /* Whether this entry is for STT_GNU_IFUNC symbols.  */
3124
  unsigned int ifunc : 1;
3125
};
3126
3127
struct ppc_link_hash_entry
3128
{
3129
  struct elf_link_hash_entry elf;
3130
3131
  union
3132
  {
3133
    /* A pointer to the most recently used stub hash entry against this
3134
       symbol.  */
3135
    struct ppc_stub_hash_entry *stub_cache;
3136
3137
    /* A pointer to the next symbol starting with a '.'  */
3138
    struct ppc_link_hash_entry *next_dot_sym;
3139
  } u;
3140
3141
  /* Link between function code and descriptor symbols.  */
3142
  struct ppc_link_hash_entry *oh;
3143
3144
  /* Flag function code and descriptor symbols.  */
3145
  unsigned int is_func:1;
3146
  unsigned int is_func_descriptor:1;
3147
  unsigned int fake:1;
3148
3149
  /* Whether global opd/toc sym has been adjusted or not.
3150
     After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3151
     should be set for all globals defined in any opd/toc section.  */
3152
  unsigned int adjust_done:1;
3153
3154
  /* Set if this is an out-of-line register save/restore function,
3155
     with non-standard calling convention.  */
3156
  unsigned int save_res:1;
3157
3158
  /* Set if a duplicate symbol with non-zero localentry is detected,
3159
     even when the duplicate symbol does not provide a definition.  */
3160
  unsigned int non_zero_localentry:1;
3161
3162
  /* Contexts in which symbol is used in the GOT (or TOC).
3163
     Bits are or'd into the mask as the corresponding relocs are
3164
     encountered during check_relocs, with TLS_TLS being set when any
3165
     of the other TLS bits are set.  tls_optimize clears bits when
3166
     optimizing to indicate the corresponding GOT entry type is not
3167
     needed.  If set, TLS_TLS is never cleared.  tls_optimize may also
3168
     set TLS_GDIE when a GD reloc turns into an IE one.
3169
     These flags are also kept for local symbols.  */
3170
0
#define TLS_TLS    1  /* Any TLS reloc.  */
3171
0
#define TLS_GD     2  /* GD reloc. */
3172
0
#define TLS_LD     4  /* LD reloc. */
3173
0
#define TLS_TPREL  8  /* TPREL reloc, => IE. */
3174
0
#define TLS_DTPREL  16  /* DTPREL reloc, => LD. */
3175
0
#define TLS_MARK  32  /* __tls_get_addr call marked. */
3176
0
#define TLS_GDIE  64  /* GOT TPREL reloc resulting from GD->IE. */
3177
0
#define TLS_EXPLICIT   256  /* TOC section TLS reloc, not stored. */
3178
  unsigned char tls_mask;
3179
3180
  /* The above field is also used to mark function symbols.  In which
3181
     case TLS_TLS will be 0.  */
3182
0
#define PLT_IFUNC  2  /* STT_GNU_IFUNC.  */
3183
0
#define PLT_KEEP   4  /* inline plt call requires plt entry.  */
3184
0
#define NON_GOT        256  /* local symbol plt, not stored.  */
3185
};
3186
3187
static inline struct ppc_link_hash_entry *
3188
ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3189
0
{
3190
0
  return (struct ppc_link_hash_entry *) ent;
3191
0
}
3192
3193
static inline struct elf_link_hash_entry *
3194
elf_hash_entry (struct ppc_link_hash_entry *ent)
3195
0
{
3196
0
  return (struct elf_link_hash_entry *) ent;
3197
0
}
3198
3199
/* ppc64 ELF linker hash table.  */
3200
3201
struct ppc_link_hash_table
3202
{
3203
  struct elf_link_hash_table elf;
3204
3205
  /* The stub hash table.  */
3206
  struct bfd_hash_table stub_hash_table;
3207
3208
  /* Another hash table for plt_branch stubs.  */
3209
  struct bfd_hash_table branch_hash_table;
3210
3211
  /* Hash table for function prologue tocsave.  */
3212
  htab_t tocsave_htab;
3213
3214
  /* Various options and other info passed from the linker.  */
3215
  struct ppc64_elf_params *params;
3216
3217
  /* The size of sec_info below.  */
3218
  unsigned int sec_info_arr_size;
3219
3220
  /* Per-section array of extra section info.  Done this way rather
3221
     than as part of ppc64_elf_section_data so we have the info for
3222
     non-ppc64 sections.  */
3223
  struct
3224
  {
3225
    /* Along with elf_gp, specifies the TOC pointer used by this section.  */
3226
    bfd_vma toc_off;
3227
3228
    union
3229
    {
3230
      /* The section group that this section belongs to.  */
3231
      struct map_stub *group;
3232
      /* A temp section list pointer.  */
3233
      asection *list;
3234
    } u;
3235
  } *sec_info;
3236
3237
  /* Linked list of groups.  */
3238
  struct map_stub *group;
3239
3240
  /* Temp used when calculating TOC pointers.  */
3241
  bfd_vma toc_curr;
3242
  bfd *toc_bfd;
3243
  asection *toc_first_sec;
3244
3245
  /* Used when adding symbols.  */
3246
  struct ppc_link_hash_entry *dot_syms;
3247
3248
  /* Shortcuts to get to dynamic linker sections.  */
3249
  asection *glink;
3250
  asection *global_entry;
3251
  asection *sfpr;
3252
  asection *pltlocal;
3253
  asection *relpltlocal;
3254
  asection *brlt;
3255
  asection *relbrlt;
3256
  asection *glink_eh_frame;
3257
3258
  /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3259
  struct ppc_link_hash_entry *tls_get_addr;
3260
  struct ppc_link_hash_entry *tls_get_addr_fd;
3261
  struct ppc_link_hash_entry *tga_desc;
3262
  struct ppc_link_hash_entry *tga_desc_fd;
3263
  struct map_stub *tga_group;
3264
3265
  /* The size of reliplt used by got entry relocs.  */
3266
  bfd_size_type got_reli_size;
3267
3268
  /* DT_RELR array of section/r_offset.  */
3269
  size_t relr_alloc;
3270
  size_t relr_count;
3271
  struct
3272
  {
3273
    asection *sec;
3274
    bfd_vma off;
3275
  } *relr;
3276
3277
  /* Statistics.  */
3278
  unsigned long stub_count[ppc_stub_save_res];
3279
3280
  /* Number of stubs against global syms.  */
3281
  unsigned long stub_globals;
3282
3283
  /* Set if we're linking code with function descriptors.  */
3284
  unsigned int opd_abi:1;
3285
3286
  /* Support for multiple toc sections.  */
3287
  unsigned int do_multi_toc:1;
3288
  unsigned int multi_toc_needed:1;
3289
  unsigned int second_toc_pass:1;
3290
  unsigned int do_toc_opt:1;
3291
3292
  /* Set if tls optimization is enabled.  */
3293
  unsigned int do_tls_opt:1;
3294
3295
  /* Set if inline plt calls should be converted to direct calls.  */
3296
  unsigned int can_convert_all_inline_plt:1;
3297
3298
  /* Set if a stub_offset changed.  */
3299
  unsigned int stub_changed:1;
3300
3301
  /* Set on error.  */
3302
  unsigned int stub_error:1;
3303
3304
  /* Whether func_desc_adjust needs to be run over symbols.  */
3305
  unsigned int need_func_desc_adj:1;
3306
3307
  /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized.  */
3308
  unsigned int has_plt_localentry0:1;
3309
3310
  /* Whether calls are made via the PLT from NOTOC functions.  */
3311
  unsigned int notoc_plt:1;
3312
3313
  /* Whether any code linked seems to be Power10.  */
3314
  unsigned int has_power10_relocs:1;
3315
3316
  /* Incremented once for each stub sized.  */
3317
  unsigned int stub_id;
3318
3319
  /* Incremented every time we size stubs.  */
3320
  unsigned int stub_iteration;
3321
3322
/* After 20 iterations of stub sizing we no longer allow stubs to
3323
   shrink.  This is to break out of a pathological case where adding
3324
   stubs or increasing their size on one iteration decreases section
3325
   gaps (perhaps due to alignment), which then results in smaller
3326
   stubs on the next iteration.  */
3327
0
#define STUB_SHRINK_ITER 20
3328
};
3329
3330
/* Rename some of the generic section flags to better document how they
3331
   are used here.  */
3332
3333
/* Nonzero if this section has TLS related relocations.  */
3334
0
#define has_tls_reloc sec_flg0
3335
3336
/* Nonzero if this section has a call to __tls_get_addr lacking marker
3337
   relocations.  */
3338
0
#define nomark_tls_get_addr sec_flg1
3339
3340
/* Nonzero if this section has any toc or got relocs.  */
3341
0
#define has_toc_reloc sec_flg2
3342
3343
/* Nonzero if this section has a call to another section that uses
3344
   the toc or got.  */
3345
0
#define makes_toc_func_call sec_flg3
3346
3347
/* Recursion protection when determining above flag.  */
3348
0
#define call_check_in_progress sec_flg4
3349
0
#define call_check_done sec_flg5
3350
3351
/* Get the ppc64 ELF linker hash table from a link_info structure.  */
3352
3353
#define ppc_hash_table(p) \
3354
0
  ((is_elf_hash_table ((p)->hash)          \
3355
0
    && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3356
0
   ? (struct ppc_link_hash_table *) (p)->hash : NULL)
3357
3358
#define ppc_stub_hash_lookup(table, string, create, copy) \
3359
0
  ((struct ppc_stub_hash_entry *) \
3360
0
   bfd_hash_lookup ((table), (string), (create), (copy)))
3361
3362
#define ppc_branch_hash_lookup(table, string, create, copy) \
3363
0
  ((struct ppc_branch_hash_entry *) \
3364
0
   bfd_hash_lookup ((table), (string), (create), (copy)))
3365
3366
/* Create an entry in the stub hash table.  */
3367
3368
static struct bfd_hash_entry *
3369
stub_hash_newfunc (struct bfd_hash_entry *entry,
3370
       struct bfd_hash_table *table,
3371
       const char *string)
3372
0
{
3373
  /* Allocate the structure if it has not already been allocated by a
3374
     subclass.  */
3375
0
  if (entry == NULL)
3376
0
    {
3377
0
      entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3378
0
      if (entry == NULL)
3379
0
  return entry;
3380
0
    }
3381
3382
  /* Call the allocation method of the superclass.  */
3383
0
  entry = bfd_hash_newfunc (entry, table, string);
3384
0
  if (entry != NULL)
3385
0
    {
3386
0
      struct ppc_stub_hash_entry *eh;
3387
3388
      /* Initialize the local fields.  */
3389
0
      eh = (struct ppc_stub_hash_entry *) entry;
3390
0
      eh->type.main = ppc_stub_none;
3391
0
      eh->type.sub = ppc_stub_toc;
3392
0
      eh->type.r2save = 0;
3393
0
      eh->group = NULL;
3394
0
      eh->stub_offset = 0;
3395
0
      eh->target_value = 0;
3396
0
      eh->target_section = NULL;
3397
0
      eh->h = NULL;
3398
0
      eh->plt_ent = NULL;
3399
0
      eh->symtype = 0;
3400
0
      eh->other = 0;
3401
0
      eh->id = 0;
3402
0
    }
3403
3404
0
  return entry;
3405
0
}
3406
3407
/* Create an entry in the branch hash table.  */
3408
3409
static struct bfd_hash_entry *
3410
branch_hash_newfunc (struct bfd_hash_entry *entry,
3411
         struct bfd_hash_table *table,
3412
         const char *string)
3413
0
{
3414
  /* Allocate the structure if it has not already been allocated by a
3415
     subclass.  */
3416
0
  if (entry == NULL)
3417
0
    {
3418
0
      entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3419
0
      if (entry == NULL)
3420
0
  return entry;
3421
0
    }
3422
3423
  /* Call the allocation method of the superclass.  */
3424
0
  entry = bfd_hash_newfunc (entry, table, string);
3425
0
  if (entry != NULL)
3426
0
    {
3427
0
      struct ppc_branch_hash_entry *eh;
3428
3429
      /* Initialize the local fields.  */
3430
0
      eh = (struct ppc_branch_hash_entry *) entry;
3431
0
      eh->offset = 0;
3432
0
      eh->iter = 0;
3433
0
    }
3434
3435
0
  return entry;
3436
0
}
3437
3438
/* Create an entry in a ppc64 ELF linker hash table.  */
3439
3440
static struct bfd_hash_entry *
3441
link_hash_newfunc (struct bfd_hash_entry *entry,
3442
       struct bfd_hash_table *table,
3443
       const char *string)
3444
0
{
3445
  /* Allocate the structure if it has not already been allocated by a
3446
     subclass.  */
3447
0
  if (entry == NULL)
3448
0
    {
3449
0
      entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3450
0
      if (entry == NULL)
3451
0
  return entry;
3452
0
    }
3453
3454
  /* Call the allocation method of the superclass.  */
3455
0
  entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3456
0
  if (entry != NULL)
3457
0
    {
3458
0
      struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3459
3460
0
      memset (&eh->u.stub_cache, 0,
3461
0
        (sizeof (struct ppc_link_hash_entry)
3462
0
         - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3463
3464
      /* When making function calls, old ABI code references function entry
3465
   points (dot symbols), while new ABI code references the function
3466
   descriptor symbol.  We need to make any combination of reference and
3467
   definition work together, without breaking archive linking.
3468
3469
   For a defined function "foo" and an undefined call to "bar":
3470
   An old object defines "foo" and ".foo", references ".bar" (possibly
3471
   "bar" too).
3472
   A new object defines "foo" and references "bar".
3473
3474
   A new object thus has no problem with its undefined symbols being
3475
   satisfied by definitions in an old object.  On the other hand, the
3476
   old object won't have ".bar" satisfied by a new object.
3477
3478
   Keep a list of newly added dot-symbols.  */
3479
3480
0
      if (string[0] == '.')
3481
0
  {
3482
0
    struct ppc_link_hash_table *htab;
3483
3484
0
    htab = (struct ppc_link_hash_table *) table;
3485
0
    eh->u.next_dot_sym = htab->dot_syms;
3486
0
    htab->dot_syms = eh;
3487
0
  }
3488
0
    }
3489
3490
0
  return entry;
3491
0
}
3492
3493
struct tocsave_entry
3494
{
3495
  asection *sec;
3496
  bfd_vma offset;
3497
};
3498
3499
static hashval_t
3500
tocsave_htab_hash (const void *p)
3501
0
{
3502
0
  const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3503
0
  return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3504
0
}
3505
3506
static int
3507
tocsave_htab_eq (const void *p1, const void *p2)
3508
0
{
3509
0
  const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3510
0
  const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3511
0
  return e1->sec == e2->sec && e1->offset == e2->offset;
3512
0
}
3513
3514
/* Destroy a ppc64 ELF linker hash table.  */
3515
3516
static void
3517
ppc64_elf_link_hash_table_free (bfd *obfd)
3518
0
{
3519
0
  struct ppc_link_hash_table *htab;
3520
3521
0
  htab = (struct ppc_link_hash_table *) obfd->link.hash;
3522
0
  free (htab->relr);
3523
0
  if (htab->tocsave_htab)
3524
0
    htab_delete (htab->tocsave_htab);
3525
0
  bfd_hash_table_free (&htab->branch_hash_table);
3526
0
  bfd_hash_table_free (&htab->stub_hash_table);
3527
0
  _bfd_elf_link_hash_table_free (obfd);
3528
0
}
3529
3530
/* Create a ppc64 ELF linker hash table.  */
3531
3532
static struct bfd_link_hash_table *
3533
ppc64_elf_link_hash_table_create (bfd *abfd)
3534
0
{
3535
0
  struct ppc_link_hash_table *htab;
3536
0
  size_t amt = sizeof (struct ppc_link_hash_table);
3537
3538
0
  htab = bfd_zmalloc (amt);
3539
0
  if (htab == NULL)
3540
0
    return NULL;
3541
3542
0
  if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3543
0
              sizeof (struct ppc_link_hash_entry)))
3544
0
    {
3545
0
      free (htab);
3546
0
      return NULL;
3547
0
    }
3548
3549
  /* Init the stub hash table too.  */
3550
0
  if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3551
0
          sizeof (struct ppc_stub_hash_entry)))
3552
0
    {
3553
0
      _bfd_elf_link_hash_table_free (abfd);
3554
0
      return NULL;
3555
0
    }
3556
3557
  /* And the branch hash table.  */
3558
0
  if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3559
0
          sizeof (struct ppc_branch_hash_entry)))
3560
0
    {
3561
0
      bfd_hash_table_free (&htab->stub_hash_table);
3562
0
      _bfd_elf_link_hash_table_free (abfd);
3563
0
      return NULL;
3564
0
    }
3565
3566
0
  htab->tocsave_htab = htab_try_create (1024,
3567
0
          tocsave_htab_hash,
3568
0
          tocsave_htab_eq,
3569
0
          NULL);
3570
0
  if (htab->tocsave_htab == NULL)
3571
0
    {
3572
0
      ppc64_elf_link_hash_table_free (abfd);
3573
0
      return NULL;
3574
0
    }
3575
0
  htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3576
3577
  /* Initializing two fields of the union is just cosmetic.  We really
3578
     only care about glist, but when compiled on a 32-bit host the
3579
     bfd_vma fields are larger.  Setting the bfd_vma to zero makes
3580
     debugger inspection of these fields look nicer.  */
3581
0
  htab->elf.init_got_refcount.refcount = 0;
3582
0
  htab->elf.init_got_refcount.glist = NULL;
3583
0
  htab->elf.init_plt_refcount.refcount = 0;
3584
0
  htab->elf.init_plt_refcount.glist = NULL;
3585
0
  htab->elf.init_got_offset.offset = 0;
3586
0
  htab->elf.init_got_offset.glist = NULL;
3587
0
  htab->elf.init_plt_offset.offset = 0;
3588
0
  htab->elf.init_plt_offset.glist = NULL;
3589
3590
0
  return &htab->elf.root;
3591
0
}
3592
3593
/* Create sections for linker generated code.  */
3594
3595
static bool
3596
create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3597
0
{
3598
0
  struct ppc_link_hash_table *htab;
3599
0
  flagword flags;
3600
3601
0
  htab = ppc_hash_table (info);
3602
3603
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3604
0
     | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3605
0
  if (htab->params->save_restore_funcs)
3606
0
    {
3607
      /* Create .sfpr for code to save and restore fp regs.  */
3608
0
      htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3609
0
                   flags);
3610
0
      if (htab->sfpr == NULL
3611
0
    || !bfd_set_section_alignment (htab->sfpr, 2))
3612
0
  return false;
3613
0
    }
3614
3615
0
  if (bfd_link_relocatable (info))
3616
0
    return true;
3617
3618
  /* Create .glink for lazy dynamic linking support.  */
3619
0
  htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3620
0
                flags);
3621
0
  if (htab->glink == NULL
3622
0
      || !bfd_set_section_alignment (htab->glink, 3))
3623
0
    return false;
3624
3625
  /* The part of .glink used by global entry stubs, separate so that
3626
     it can be aligned appropriately without affecting htab->glink.  */
3627
0
  htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3628
0
                 flags);
3629
0
  if (htab->global_entry == NULL
3630
0
      || !bfd_set_section_alignment (htab->global_entry, 2))
3631
0
    return false;
3632
3633
0
  if (!info->no_ld_generated_unwind_info)
3634
0
    {
3635
0
      flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3636
0
         | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3637
0
      htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3638
0
                 ".eh_frame",
3639
0
                 flags);
3640
0
      if (htab->glink_eh_frame == NULL
3641
0
    || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
3642
0
  return false;
3643
0
    }
3644
3645
0
  flags = SEC_ALLOC | SEC_LINKER_CREATED;
3646
0
  htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3647
0
  if (htab->elf.iplt == NULL
3648
0
      || !bfd_set_section_alignment (htab->elf.iplt, 3))
3649
0
    return false;
3650
3651
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3652
0
     | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3653
0
  htab->elf.irelplt
3654
0
    = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3655
0
  if (htab->elf.irelplt == NULL
3656
0
      || !bfd_set_section_alignment (htab->elf.irelplt, 3))
3657
0
    return false;
3658
3659
  /* Create branch lookup table for plt_branch stubs.  */
3660
0
  flags = (SEC_ALLOC | SEC_LOAD
3661
0
     | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3662
0
  htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3663
0
               flags);
3664
0
  if (htab->brlt == NULL
3665
0
      || !bfd_set_section_alignment (htab->brlt, 3))
3666
0
    return false;
3667
3668
  /* Local plt entries, put in .branch_lt but a separate section for
3669
     convenience.  */
3670
0
  htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3671
0
                   flags);
3672
0
  if (htab->pltlocal == NULL
3673
0
      || !bfd_set_section_alignment (htab->pltlocal, 3))
3674
0
    return false;
3675
3676
0
  if (!bfd_link_pic (info))
3677
0
    return true;
3678
3679
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3680
0
     | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3681
0
  htab->relbrlt
3682
0
    = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3683
0
  if (htab->relbrlt == NULL
3684
0
      || !bfd_set_section_alignment (htab->relbrlt, 3))
3685
0
    return false;
3686
3687
0
  htab->relpltlocal
3688
0
    = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3689
0
  if (htab->relpltlocal == NULL
3690
0
      || !bfd_set_section_alignment (htab->relpltlocal, 3))
3691
0
    return false;
3692
3693
0
  return true;
3694
0
}
3695
3696
bool
3697
ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3698
       struct ppc64_elf_params *params)
3699
0
{
3700
0
  struct ppc_link_hash_table *htab;
3701
3702
/* Always hook our dynamic sections into the first bfd, which is the
3703
   linker created stub bfd.  This ensures that the GOT header is at
3704
   the start of the output TOC section.  */
3705
0
  htab = ppc_hash_table (info);
3706
0
  htab->elf.dynobj = params->stub_bfd;
3707
0
  htab->params = params;
3708
3709
0
  return create_linkage_sections (htab->elf.dynobj, info);
3710
0
}
3711
3712
/* Build a name for an entry in the stub hash table.  */
3713
3714
static char *
3715
ppc_stub_name (const asection *input_section,
3716
         const asection *sym_sec,
3717
         const struct ppc_link_hash_entry *h,
3718
         const Elf_Internal_Rela *rel)
3719
0
{
3720
0
  char *stub_name;
3721
0
  ssize_t len;
3722
3723
  /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3724
     offsets from a sym as a branch target?  In fact, we could
3725
     probably assume the addend is always zero.  */
3726
0
  BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3727
3728
0
  if (h)
3729
0
    {
3730
0
      len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3731
0
      stub_name = bfd_malloc (len);
3732
0
      if (stub_name == NULL)
3733
0
  return stub_name;
3734
3735
0
      len = sprintf (stub_name, "%08x.%s+%x",
3736
0
         input_section->id & 0xffffffff,
3737
0
         h->elf.root.root.string,
3738
0
         (int) rel->r_addend & 0xffffffff);
3739
0
    }
3740
0
  else
3741
0
    {
3742
0
      len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3743
0
      stub_name = bfd_malloc (len);
3744
0
      if (stub_name == NULL)
3745
0
  return stub_name;
3746
3747
0
      len = sprintf (stub_name, "%08x.%x:%x+%x",
3748
0
         input_section->id & 0xffffffff,
3749
0
         sym_sec->id & 0xffffffff,
3750
0
         (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3751
0
         (int) rel->r_addend & 0xffffffff);
3752
0
    }
3753
0
  if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3754
0
    stub_name[len - 2] = 0;
3755
0
  return stub_name;
3756
0
}
3757
3758
/* If mixing power10 with non-power10 code and --power10-stubs is not
3759
   specified (or is auto) then there may be multiple stub types for any
3760
   given symbol.  Up to three classes of stubs are stored in separate
3761
   stub_hash_table entries having the same key string.  The entries
3762
   will always be adjacent on entry->root.next chain, even if hash
3763
   table resizing occurs.  This function selects the correct entry to
3764
   use.  */
3765
3766
static struct ppc_stub_hash_entry *
3767
select_alt_stub (struct ppc_stub_hash_entry *entry,
3768
     enum elf_ppc64_reloc_type r_type)
3769
0
{
3770
0
  enum ppc_stub_sub_type subt;
3771
3772
0
  switch (r_type)
3773
0
    {
3774
0
    case R_PPC64_REL24_NOTOC:
3775
0
      subt = ppc_stub_notoc;
3776
0
      break;
3777
0
    case R_PPC64_REL24_P9NOTOC:
3778
0
      subt = ppc_stub_p9notoc;
3779
0
      break;
3780
0
    default:
3781
0
      subt = ppc_stub_toc;
3782
0
      break;
3783
0
    }
3784
3785
0
  while (entry != NULL && entry->type.sub != subt)
3786
0
    {
3787
0
      const char *stub_name = entry->root.string;
3788
3789
0
      entry = (struct ppc_stub_hash_entry *) entry->root.next;
3790
0
      if (entry != NULL
3791
0
    && entry->root.string != stub_name)
3792
0
  entry = NULL;
3793
0
    }
3794
3795
0
  return entry;
3796
0
}
3797
3798
/* Look up an entry in the stub hash.  Stub entries are cached because
3799
   creating the stub name takes a bit of time.  */
3800
3801
static struct ppc_stub_hash_entry *
3802
ppc_get_stub_entry (const asection *input_section,
3803
        const asection *sym_sec,
3804
        struct ppc_link_hash_entry *h,
3805
        const Elf_Internal_Rela *rel,
3806
        struct ppc_link_hash_table *htab)
3807
0
{
3808
0
  struct ppc_stub_hash_entry *stub_entry;
3809
0
  struct map_stub *group;
3810
3811
  /* If this input section is part of a group of sections sharing one
3812
     stub section, then use the id of the first section in the group.
3813
     Stub names need to include a section id, as there may well be
3814
     more than one stub used to reach say, printf, and we need to
3815
     distinguish between them.  */
3816
0
  group = htab->sec_info[input_section->id].u.group;
3817
0
  if (group == NULL)
3818
0
    return NULL;
3819
3820
0
  if (h != NULL && h->u.stub_cache != NULL
3821
0
      && h->u.stub_cache->h == h
3822
0
      && h->u.stub_cache->group == group)
3823
0
    {
3824
0
      stub_entry = h->u.stub_cache;
3825
0
    }
3826
0
  else
3827
0
    {
3828
0
      char *stub_name;
3829
3830
0
      stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3831
0
      if (stub_name == NULL)
3832
0
  return NULL;
3833
3834
0
      stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3835
0
           stub_name, false, false);
3836
0
      if (h != NULL)
3837
0
  h->u.stub_cache = stub_entry;
3838
3839
0
      free (stub_name);
3840
0
    }
3841
3842
0
  if (stub_entry != NULL && htab->params->power10_stubs == -1)
3843
0
    stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
3844
3845
0
  return stub_entry;
3846
0
}
3847
3848
/* Add a new stub entry to the stub hash.  Not all fields of the new
3849
   stub entry are initialised.  */
3850
3851
static struct ppc_stub_hash_entry *
3852
ppc_add_stub (const char *stub_name,
3853
        asection *section,
3854
        struct bfd_link_info *info)
3855
0
{
3856
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
3857
0
  struct map_stub *group;
3858
0
  asection *link_sec;
3859
0
  asection *stub_sec;
3860
0
  struct ppc_stub_hash_entry *stub_entry;
3861
3862
0
  group = htab->sec_info[section->id].u.group;
3863
0
  link_sec = group->link_sec;
3864
0
  stub_sec = group->stub_sec;
3865
0
  if (stub_sec == NULL)
3866
0
    {
3867
0
      size_t namelen;
3868
0
      bfd_size_type len;
3869
0
      char *s_name;
3870
3871
0
      namelen = strlen (link_sec->name);
3872
0
      len = namelen + sizeof (STUB_SUFFIX);
3873
0
      s_name = bfd_alloc (htab->params->stub_bfd, len);
3874
0
      if (s_name == NULL)
3875
0
  return NULL;
3876
3877
0
      memcpy (s_name, link_sec->name, namelen);
3878
0
      memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3879
0
      stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3880
0
      if (stub_sec == NULL)
3881
0
  return NULL;
3882
0
      group->stub_sec = stub_sec;
3883
0
    }
3884
3885
  /* Enter this entry into the linker stub hash table.  */
3886
0
  stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3887
0
             true, true);
3888
0
  if (stub_entry == NULL)
3889
0
    {
3890
      /* xgettext:c-format */
3891
0
      _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3892
0
        section->owner, stub_name);
3893
0
      return NULL;
3894
0
    }
3895
3896
0
  stub_entry->group = group;
3897
0
  stub_entry->stub_offset = 0;
3898
0
  return stub_entry;
3899
0
}
3900
3901
/* A stub has already been created, but it may not be the required
3902
   type.  We shouldn't be transitioning from plt_call to long_branch
3903
   stubs or vice versa, but we might be upgrading from plt_call to
3904
   plt_call with r2save for example.  */
3905
3906
static bool
3907
ppc_merge_stub (struct ppc_link_hash_table *htab,
3908
    struct ppc_stub_hash_entry *stub_entry,
3909
    struct ppc_stub_type stub_type,
3910
    enum elf_ppc64_reloc_type r_type)
3911
0
{
3912
0
  struct ppc_stub_type old_type = stub_entry->type;
3913
3914
0
  if (old_type.main == ppc_stub_save_res)
3915
0
    return true;
3916
3917
0
  if (htab->params->power10_stubs == -1)
3918
0
    {
3919
      /* For --power10-stubs=auto, don't merge _notoc and other
3920
   varieties of stubs.  */
3921
0
      struct ppc_stub_hash_entry *alt_stub;
3922
3923
0
      alt_stub = select_alt_stub (stub_entry, r_type);
3924
0
      if (alt_stub == NULL)
3925
0
  {
3926
0
    alt_stub = ((struct ppc_stub_hash_entry *)
3927
0
          stub_hash_newfunc (NULL,
3928
0
           &htab->stub_hash_table,
3929
0
           stub_entry->root.string));
3930
0
    if (alt_stub == NULL)
3931
0
      return false;
3932
3933
0
    *alt_stub = *stub_entry;
3934
0
    stub_entry->root.next = &alt_stub->root;
3935
3936
    /* Sort notoc stubs first, then toc stubs, then p9notoc.
3937
       Not that it matters, this just puts smaller stubs first.  */
3938
0
    if (stub_type.sub == ppc_stub_notoc)
3939
0
      alt_stub = stub_entry;
3940
0
    else if (stub_type.sub == ppc_stub_p9notoc
3941
0
       && alt_stub->root.next
3942
0
       && alt_stub->root.next->string == alt_stub->root.string)
3943
0
      {
3944
0
        struct ppc_stub_hash_entry *next
3945
0
    = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3946
0
        alt_stub->type = next->type;
3947
0
        alt_stub = next;
3948
0
      }
3949
0
    alt_stub->type = stub_type;
3950
0
    return true;
3951
0
  }
3952
0
      stub_entry = alt_stub;
3953
0
    }
3954
3955
0
  old_type = stub_entry->type;
3956
0
  if (old_type.main == ppc_stub_plt_branch)
3957
0
    old_type.main = ppc_stub_long_branch;
3958
3959
0
  if (old_type.main != stub_type.main
3960
0
      || (old_type.sub != stub_type.sub
3961
0
    && old_type.sub != ppc_stub_toc
3962
0
    && stub_type.sub != ppc_stub_toc))
3963
0
    abort ();
3964
3965
0
  stub_entry->type.sub |= stub_type.sub;
3966
0
  stub_entry->type.r2save |= stub_type.r2save;
3967
0
  return true;
3968
0
}
3969
3970
/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3971
   not already done.  */
3972
3973
static bool
3974
create_got_section (bfd *abfd, struct bfd_link_info *info)
3975
0
{
3976
0
  asection *got, *relgot;
3977
0
  flagword flags;
3978
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
3979
3980
0
  if (!is_ppc64_elf (abfd))
3981
0
    return false;
3982
0
  if (htab == NULL)
3983
0
    return false;
3984
3985
0
  if (!htab->elf.sgot
3986
0
      && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3987
0
    return false;
3988
3989
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3990
0
     | SEC_LINKER_CREATED);
3991
3992
0
  got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3993
0
  if (!got
3994
0
      || !bfd_set_section_alignment (got, 3))
3995
0
    return false;
3996
3997
0
  relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3998
0
                 flags | SEC_READONLY);
3999
0
  if (!relgot
4000
0
      || !bfd_set_section_alignment (relgot, 3))
4001
0
    return false;
4002
4003
0
  ppc64_elf_tdata (abfd)->got = got;
4004
0
  ppc64_elf_tdata (abfd)->relgot = relgot;
4005
0
  return true;
4006
0
}
4007
4008
/* Follow indirect and warning symbol links.  */
4009
4010
static inline struct bfd_link_hash_entry *
4011
follow_link (struct bfd_link_hash_entry *h)
4012
0
{
4013
0
  while (h->type == bfd_link_hash_indirect
4014
0
   || h->type == bfd_link_hash_warning)
4015
0
    h = h->u.i.link;
4016
0
  return h;
4017
0
}
4018
4019
static inline struct elf_link_hash_entry *
4020
elf_follow_link (struct elf_link_hash_entry *h)
4021
0
{
4022
0
  return (struct elf_link_hash_entry *) follow_link (&h->root);
4023
0
}
4024
4025
static inline struct ppc_link_hash_entry *
4026
ppc_follow_link (struct ppc_link_hash_entry *h)
4027
0
{
4028
0
  return ppc_elf_hash_entry (elf_follow_link (&h->elf));
4029
0
}
4030
4031
/* Merge PLT info on FROM with that on TO.  */
4032
4033
static void
4034
move_plt_plist (struct ppc_link_hash_entry *from,
4035
    struct ppc_link_hash_entry *to)
4036
0
{
4037
0
  if (from->elf.plt.plist != NULL)
4038
0
    {
4039
0
      if (to->elf.plt.plist != NULL)
4040
0
  {
4041
0
    struct plt_entry **entp;
4042
0
    struct plt_entry *ent;
4043
4044
0
    for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4045
0
      {
4046
0
        struct plt_entry *dent;
4047
4048
0
        for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4049
0
    if (dent->addend == ent->addend)
4050
0
      {
4051
0
        dent->plt.refcount += ent->plt.refcount;
4052
0
        *entp = ent->next;
4053
0
        break;
4054
0
      }
4055
0
        if (dent == NULL)
4056
0
    entp = &ent->next;
4057
0
      }
4058
0
    *entp = to->elf.plt.plist;
4059
0
  }
4060
4061
0
      to->elf.plt.plist = from->elf.plt.plist;
4062
0
      from->elf.plt.plist = NULL;
4063
0
    }
4064
0
}
4065
4066
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
4067
4068
static void
4069
ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4070
        struct elf_link_hash_entry *dir,
4071
        struct elf_link_hash_entry *ind)
4072
0
{
4073
0
  struct ppc_link_hash_entry *edir, *eind;
4074
4075
0
  edir = ppc_elf_hash_entry (dir);
4076
0
  eind = ppc_elf_hash_entry (ind);
4077
4078
0
  edir->is_func |= eind->is_func;
4079
0
  edir->is_func_descriptor |= eind->is_func_descriptor;
4080
0
  edir->tls_mask |= eind->tls_mask;
4081
0
  if (eind->oh != NULL)
4082
0
    edir->oh = ppc_follow_link (eind->oh);
4083
4084
0
  if (edir->elf.versioned != versioned_hidden)
4085
0
    edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4086
0
  edir->elf.ref_regular |= eind->elf.ref_regular;
4087
0
  edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4088
0
  edir->elf.non_got_ref |= eind->elf.non_got_ref;
4089
0
  edir->elf.needs_plt |= eind->elf.needs_plt;
4090
0
  edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4091
4092
  /* If we were called to copy over info for a weak sym, don't copy
4093
     dyn_relocs, plt/got info, or dynindx.  We used to copy dyn_relocs
4094
     in order to simplify readonly_dynrelocs and save a field in the
4095
     symbol hash entry, but that means dyn_relocs can't be used in any
4096
     tests about a specific symbol, or affect other symbol flags which
4097
     are then tested.  */
4098
0
  if (eind->elf.root.type != bfd_link_hash_indirect)
4099
0
    return;
4100
4101
  /* Copy over any dynamic relocs we may have on the indirect sym.  */
4102
0
  if (ind->dyn_relocs != NULL)
4103
0
    {
4104
0
      if (dir->dyn_relocs != NULL)
4105
0
  {
4106
0
    struct ppc_dyn_relocs **pp;
4107
0
    struct ppc_dyn_relocs *p;
4108
4109
    /* Add reloc counts against the indirect sym to the direct sym
4110
       list.  Merge any entries against the same section.  */
4111
0
    for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
4112
0
         (p = *pp) != NULL;
4113
0
         )
4114
0
      {
4115
0
        struct ppc_dyn_relocs *q;
4116
4117
0
        for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4118
0
       q != NULL;
4119
0
       q = q->next)
4120
0
    if (q->sec == p->sec)
4121
0
      {
4122
0
        q->count += p->count;
4123
0
        q->pc_count += p->pc_count;
4124
0
        q->rel_count += p->rel_count;
4125
0
        *pp = p->next;
4126
0
        break;
4127
0
      }
4128
0
        if (q == NULL)
4129
0
    pp = &p->next;
4130
0
      }
4131
0
    *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4132
0
  }
4133
4134
0
      dir->dyn_relocs = ind->dyn_relocs;
4135
0
      ind->dyn_relocs = NULL;
4136
0
    }
4137
4138
  /* Copy over got entries that we may have already seen to the
4139
     symbol which just became indirect.  */
4140
0
  if (eind->elf.got.glist != NULL)
4141
0
    {
4142
0
      if (edir->elf.got.glist != NULL)
4143
0
  {
4144
0
    struct got_entry **entp;
4145
0
    struct got_entry *ent;
4146
4147
0
    for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4148
0
      {
4149
0
        struct got_entry *dent;
4150
4151
0
        for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4152
0
    if (dent->addend == ent->addend
4153
0
        && dent->owner == ent->owner
4154
0
        && dent->tls_type == ent->tls_type)
4155
0
      {
4156
0
        dent->got.refcount += ent->got.refcount;
4157
0
        *entp = ent->next;
4158
0
        break;
4159
0
      }
4160
0
        if (dent == NULL)
4161
0
    entp = &ent->next;
4162
0
      }
4163
0
    *entp = edir->elf.got.glist;
4164
0
  }
4165
4166
0
      edir->elf.got.glist = eind->elf.got.glist;
4167
0
      eind->elf.got.glist = NULL;
4168
0
    }
4169
4170
  /* And plt entries.  */
4171
0
  move_plt_plist (eind, edir);
4172
4173
0
  if (eind->elf.dynindx != -1)
4174
0
    {
4175
0
      if (edir->elf.dynindx != -1)
4176
0
  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4177
0
        edir->elf.dynstr_index);
4178
0
      edir->elf.dynindx = eind->elf.dynindx;
4179
0
      edir->elf.dynstr_index = eind->elf.dynstr_index;
4180
0
      eind->elf.dynindx = -1;
4181
0
      eind->elf.dynstr_index = 0;
4182
0
    }
4183
0
}
4184
4185
/* Find the function descriptor hash entry from the given function code
4186
   hash entry FH.  Link the entries via their OH fields.  */
4187
4188
static struct ppc_link_hash_entry *
4189
lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4190
0
{
4191
0
  struct ppc_link_hash_entry *fdh = fh->oh;
4192
4193
0
  if (fdh == NULL)
4194
0
    {
4195
0
      const char *fd_name = fh->elf.root.root.string + 1;
4196
4197
0
      fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4198
0
                  false, false, false));
4199
0
      if (fdh == NULL)
4200
0
  return fdh;
4201
4202
0
      fdh->is_func_descriptor = 1;
4203
0
      fdh->oh = fh;
4204
0
      fh->is_func = 1;
4205
0
      fh->oh = fdh;
4206
0
    }
4207
4208
0
  fdh = ppc_follow_link (fdh);
4209
0
  fdh->is_func_descriptor = 1;
4210
0
  fdh->oh = fh;
4211
0
  return fdh;
4212
0
}
4213
4214
/* Make a fake function descriptor sym for the undefined code sym FH.  */
4215
4216
static struct ppc_link_hash_entry *
4217
make_fdh (struct bfd_link_info *info,
4218
    struct ppc_link_hash_entry *fh)
4219
0
{
4220
0
  bfd *abfd = fh->elf.root.u.undef.abfd;
4221
0
  struct bfd_link_hash_entry *bh = NULL;
4222
0
  struct ppc_link_hash_entry *fdh;
4223
0
  flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4224
0
        ? BSF_WEAK
4225
0
        : BSF_GLOBAL);
4226
4227
0
  if (!_bfd_generic_link_add_one_symbol (info, abfd,
4228
0
           fh->elf.root.root.string + 1,
4229
0
           flags, bfd_und_section_ptr, 0,
4230
0
           NULL, false, false, &bh))
4231
0
    return NULL;
4232
4233
0
  fdh = (struct ppc_link_hash_entry *) bh;
4234
0
  fdh->elf.non_elf = 0;
4235
0
  fdh->fake = 1;
4236
0
  fdh->is_func_descriptor = 1;
4237
0
  fdh->oh = fh;
4238
0
  fh->is_func = 1;
4239
0
  fh->oh = fdh;
4240
0
  return fdh;
4241
0
}
4242
4243
/* Fix function descriptor symbols defined in .opd sections to be
4244
   function type.  */
4245
4246
static bool
4247
ppc64_elf_add_symbol_hook (bfd *ibfd,
4248
         struct bfd_link_info *info,
4249
         Elf_Internal_Sym *isym,
4250
         const char **name,
4251
         flagword *flags ATTRIBUTE_UNUSED,
4252
         asection **sec,
4253
         bfd_vma *value)
4254
0
{
4255
0
  if (*sec != NULL
4256
0
      && strcmp ((*sec)->name, ".opd") == 0)
4257
0
    {
4258
0
      asection *code_sec;
4259
4260
0
      if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4261
0
      || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4262
0
  isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4263
4264
      /* If the symbol is a function defined in .opd, and the function
4265
   code is in a discarded group, let it appear to be undefined.  */
4266
0
      if (!bfd_link_relocatable (info)
4267
0
    && (*sec)->reloc_count != 0
4268
0
    && opd_entry_value (*sec, *value, &code_sec, NULL,
4269
0
            false) != (bfd_vma) -1
4270
0
    && discarded_section (code_sec))
4271
0
  {
4272
0
    *sec = bfd_und_section_ptr;
4273
0
    isym->st_shndx = SHN_UNDEF;
4274
0
  }
4275
0
    }
4276
0
  else if (*sec != NULL
4277
0
     && strcmp ((*sec)->name, ".toc") == 0
4278
0
     && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4279
0
    {
4280
0
      struct ppc_link_hash_table *htab = ppc_hash_table (info);
4281
0
      if (htab != NULL)
4282
0
  htab->params->object_in_toc = 1;
4283
0
    }
4284
4285
0
  if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4286
0
    {
4287
0
      if (abiversion (ibfd) == 0)
4288
0
  set_abiversion (ibfd, 2);
4289
0
      else if (abiversion (ibfd) == 1)
4290
0
  {
4291
0
    _bfd_error_handler (_("symbol '%s' has invalid st_other"
4292
0
        " for ABI version 1"), *name);
4293
0
    bfd_set_error (bfd_error_bad_value);
4294
0
    return false;
4295
0
  }
4296
0
    }
4297
4298
0
  return true;
4299
0
}
4300
4301
/* Merge non-visibility st_other attributes: local entry point.  */
4302
4303
static void
4304
ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4305
          unsigned int st_other,
4306
          bool definition,
4307
          bool dynamic)
4308
0
{
4309
0
  if (definition && (!dynamic || !h->def_regular))
4310
0
    h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
4311
0
    | ELF_ST_VISIBILITY (h->other));
4312
0
}
4313
4314
/* Hook called on merging a symbol.  We use this to clear "fake" since
4315
   we now have a real symbol.  */
4316
4317
static bool
4318
ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4319
      const Elf_Internal_Sym *isym,
4320
      asection **psec ATTRIBUTE_UNUSED,
4321
      bool newdef ATTRIBUTE_UNUSED,
4322
      bool olddef ATTRIBUTE_UNUSED,
4323
      bfd *oldbfd ATTRIBUTE_UNUSED,
4324
      const asection *oldsec ATTRIBUTE_UNUSED)
4325
0
{
4326
0
  ppc_elf_hash_entry (h)->fake = 0;
4327
0
  if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4328
0
    ppc_elf_hash_entry (h)->non_zero_localentry = 1;
4329
0
  return true;
4330
0
}
4331
4332
/* This function makes an old ABI object reference to ".bar" cause the
4333
   inclusion of a new ABI object archive that defines "bar".
4334
   NAME is a symbol defined in an archive.  Return a symbol in the hash
4335
   table that might be satisfied by the archive symbols.  */
4336
4337
static struct bfd_link_hash_entry *
4338
ppc64_elf_archive_symbol_lookup (bfd *abfd,
4339
         struct bfd_link_info *info,
4340
         const char *name)
4341
0
{
4342
0
  struct bfd_link_hash_entry *h;
4343
0
  char *dot_name;
4344
0
  size_t len;
4345
4346
0
  h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4347
0
  if (h != NULL
4348
0
      && ppc_hash_table (info) != NULL
4349
      /* Don't return this sym if it is a fake function descriptor
4350
   created by add_symbol_adjust.  */
4351
0
      && !((struct ppc_link_hash_entry *) h)->fake)
4352
0
    return h;
4353
4354
0
  if (name[0] == '.')
4355
0
    return h;
4356
4357
0
  len = strlen (name);
4358
0
  dot_name = bfd_alloc (abfd, len + 2);
4359
0
  if (dot_name == NULL)
4360
0
    return (struct bfd_link_hash_entry *) -1;
4361
0
  dot_name[0] = '.';
4362
0
  memcpy (dot_name + 1, name, len + 1);
4363
0
  h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4364
0
  bfd_release (abfd, dot_name);
4365
0
  if (h != NULL)
4366
0
    return h;
4367
4368
0
  if (strcmp (name, "__tls_get_addr_opt") == 0)
4369
0
    h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
4370
0
  return h;
4371
0
}
4372
4373
/* This function satisfies all old ABI object references to ".bar" if a
4374
   new ABI object defines "bar".  Well, at least, undefined dot symbols
4375
   are made weak.  This stops later archive searches from including an
4376
   object if we already have a function descriptor definition.  It also
4377
   prevents the linker complaining about undefined symbols.
4378
   We also check and correct mismatched symbol visibility here.  The
4379
   most restrictive visibility of the function descriptor and the
4380
   function entry symbol is used.  */
4381
4382
static bool
4383
add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4384
0
{
4385
0
  struct ppc_link_hash_table *htab;
4386
0
  struct ppc_link_hash_entry *fdh;
4387
4388
0
  if (eh->elf.root.type == bfd_link_hash_warning)
4389
0
    eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4390
4391
0
  if (eh->elf.root.type == bfd_link_hash_indirect)
4392
0
    return true;
4393
4394
0
  if (eh->elf.root.root.string[0] != '.')
4395
0
    abort ();
4396
4397
0
  htab = ppc_hash_table (info);
4398
0
  if (htab == NULL)
4399
0
    return false;
4400
4401
0
  fdh = lookup_fdh (eh, htab);
4402
0
  if (fdh == NULL
4403
0
      && !bfd_link_relocatable (info)
4404
0
      && (eh->elf.root.type == bfd_link_hash_undefined
4405
0
    || eh->elf.root.type == bfd_link_hash_undefweak)
4406
0
      && eh->elf.ref_regular)
4407
0
    {
4408
      /* Make an undefined function descriptor sym, in order to
4409
   pull in an --as-needed shared lib.  Archives are handled
4410
   elsewhere.  */
4411
0
      fdh = make_fdh (info, eh);
4412
0
      if (fdh == NULL)
4413
0
  return false;
4414
0
    }
4415
4416
0
  if (fdh != NULL)
4417
0
    {
4418
0
      unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4419
0
      unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4420
4421
      /* Make both descriptor and entry symbol have the most
4422
   constraining visibility of either symbol.  */
4423
0
      if (entry_vis < descr_vis)
4424
0
  fdh->elf.other += entry_vis - descr_vis;
4425
0
      else if (entry_vis > descr_vis)
4426
0
  eh->elf.other += descr_vis - entry_vis;
4427
4428
      /* Propagate reference flags from entry symbol to function
4429
   descriptor symbol.  */
4430
0
      fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4431
0
      fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4432
0
      fdh->elf.ref_regular |= eh->elf.ref_regular;
4433
0
      fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4434
4435
0
      if (!fdh->elf.forced_local
4436
0
    && fdh->elf.dynindx == -1
4437
0
    && fdh->elf.versioned != versioned_hidden
4438
0
    && (bfd_link_dll (info)
4439
0
        || fdh->elf.def_dynamic
4440
0
        || fdh->elf.ref_dynamic)
4441
0
    && (eh->elf.ref_regular
4442
0
        || eh->elf.def_regular))
4443
0
  {
4444
0
    if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4445
0
      return false;
4446
0
  }
4447
0
    }
4448
4449
0
  return true;
4450
0
}
4451
4452
/* Set up opd section info and abiversion for IBFD, and process list
4453
   of dot-symbols we made in link_hash_newfunc.  */
4454
4455
static bool
4456
ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4457
0
{
4458
0
  struct ppc_link_hash_table *htab;
4459
0
  struct ppc_link_hash_entry **p, *eh;
4460
0
  asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4461
4462
0
  if (opd != NULL && opd->size != 0)
4463
0
    {
4464
0
      if (ppc64_elf_section_data (opd)->sec_type == sec_normal)
4465
0
  ppc64_elf_section_data (opd)->sec_type = sec_opd;
4466
0
      else if (ppc64_elf_section_data (opd)->sec_type != sec_opd)
4467
0
  BFD_FAIL ();
4468
4469
0
      if (abiversion (ibfd) == 0)
4470
0
  set_abiversion (ibfd, 1);
4471
0
      else if (abiversion (ibfd) >= 2)
4472
0
  {
4473
    /* xgettext:c-format */
4474
0
    _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4475
0
            ibfd, abiversion (ibfd));
4476
0
    bfd_set_error (bfd_error_bad_value);
4477
0
    return false;
4478
0
  }
4479
0
    }
4480
4481
0
  if (is_ppc64_elf (info->output_bfd))
4482
0
    {
4483
      /* For input files without an explicit abiversion in e_flags
4484
   we should have flagged any with symbol st_other bits set
4485
   as ELFv2 and above flagged those with .opd as ELFv1.
4486
   Set the output abiversion if not yet set, and for any input
4487
   still ambiguous, take its abiversion from the output.
4488
   Differences in ABI are reported later.  */
4489
0
      if (abiversion (info->output_bfd) == 0)
4490
0
  set_abiversion (info->output_bfd, abiversion (ibfd));
4491
0
      else if (abiversion (ibfd) == 0)
4492
0
  set_abiversion (ibfd, abiversion (info->output_bfd));
4493
0
    }
4494
4495
0
  htab = ppc_hash_table (info);
4496
0
  if (htab == NULL)
4497
0
    return true;
4498
4499
0
  if (opd != NULL && opd->size != 0
4500
0
      && (ibfd->flags & DYNAMIC) == 0
4501
0
      && (opd->flags & SEC_RELOC) != 0
4502
0
      && opd->reloc_count != 0
4503
0
      && !bfd_is_abs_section (opd->output_section)
4504
0
      && info->gc_sections)
4505
0
    {
4506
      /* Garbage collection needs some extra help with .opd sections.
4507
   We don't want to necessarily keep everything referenced by
4508
   relocs in .opd, as that would keep all functions.  Instead,
4509
   if we reference an .opd symbol (a function descriptor), we
4510
   want to keep the function code symbol's section.  This is
4511
   easy for global symbols, but for local syms we need to keep
4512
   information about the associated function section.  */
4513
0
      bfd_size_type amt;
4514
0
      asection **opd_sym_map;
4515
0
      Elf_Internal_Shdr *symtab_hdr;
4516
0
      Elf_Internal_Rela *relocs, *rel_end, *rel;
4517
4518
0
      amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4519
0
      opd_sym_map = bfd_zalloc (ibfd, amt);
4520
0
      if (opd_sym_map == NULL)
4521
0
  return false;
4522
0
      ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4523
0
      relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4524
0
            info->keep_memory);
4525
0
      if (relocs == NULL)
4526
0
  return false;
4527
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
4528
0
      rel_end = relocs + opd->reloc_count - 1;
4529
0
      for (rel = relocs; rel < rel_end; rel++)
4530
0
  {
4531
0
    enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4532
0
    unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4533
4534
0
    if (r_type == R_PPC64_ADDR64
4535
0
        && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4536
0
        && r_symndx < symtab_hdr->sh_info)
4537
0
      {
4538
0
        Elf_Internal_Sym *isym;
4539
0
        asection *s;
4540
4541
0
        isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4542
0
              r_symndx);
4543
0
        if (isym == NULL)
4544
0
    {
4545
0
      if (elf_section_data (opd)->relocs != relocs)
4546
0
        free (relocs);
4547
0
      return false;
4548
0
    }
4549
4550
0
        s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4551
0
        if (s != NULL && s != opd)
4552
0
    opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4553
0
      }
4554
0
  }
4555
0
      if (elf_section_data (opd)->relocs != relocs)
4556
0
  free (relocs);
4557
0
    }
4558
4559
0
  p = &htab->dot_syms;
4560
0
  while ((eh = *p) != NULL)
4561
0
    {
4562
0
      *p = NULL;
4563
0
      if (&eh->elf == htab->elf.hgot)
4564
0
  ;
4565
0
      else if (htab->elf.hgot == NULL
4566
0
         && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4567
0
  htab->elf.hgot = &eh->elf;
4568
0
      else if (abiversion (ibfd) <= 1)
4569
0
  {
4570
0
    htab->need_func_desc_adj = 1;
4571
0
    if (!add_symbol_adjust (eh, info))
4572
0
      return false;
4573
0
  }
4574
0
      p = &eh->u.next_dot_sym;
4575
0
    }
4576
0
  return true;
4577
0
}
4578
4579
/* Undo hash table changes when an --as-needed input file is determined
4580
   not to be needed.  */
4581
4582
static bool
4583
ppc64_elf_notice_as_needed (bfd *ibfd,
4584
          struct bfd_link_info *info,
4585
          enum notice_asneeded_action act)
4586
0
{
4587
0
  if (act == notice_not_needed)
4588
0
    {
4589
0
      struct ppc_link_hash_table *htab = ppc_hash_table (info);
4590
4591
0
      if (htab == NULL)
4592
0
  return false;
4593
4594
0
      htab->dot_syms = NULL;
4595
0
    }
4596
0
  return _bfd_elf_notice_as_needed (ibfd, info, act);
4597
0
}
4598
4599
/* If --just-symbols against a final linked binary, then assume we need
4600
   toc adjusting stubs when calling functions defined there.  */
4601
4602
static void
4603
ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4604
0
{
4605
0
  if ((sec->flags & SEC_CODE) != 0
4606
0
      && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4607
0
      && is_ppc64_elf (sec->owner))
4608
0
    {
4609
0
      if (abiversion (sec->owner) >= 2
4610
0
    || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4611
0
  sec->has_toc_reloc = 1;
4612
0
    }
4613
0
  _bfd_elf_link_just_syms (sec, info);
4614
0
}
4615
4616
static struct plt_entry **
4617
update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4618
           unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4619
0
{
4620
0
  struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4621
0
  struct plt_entry **local_plt;
4622
0
  unsigned char *local_got_tls_masks;
4623
4624
0
  if (local_got_ents == NULL)
4625
0
    {
4626
0
      bfd_size_type size = symtab_hdr->sh_info;
4627
4628
0
      size *= (sizeof (*local_got_ents)
4629
0
         + sizeof (*local_plt)
4630
0
         + sizeof (*local_got_tls_masks));
4631
0
      local_got_ents = bfd_zalloc (abfd, size);
4632
0
      if (local_got_ents == NULL)
4633
0
  return NULL;
4634
0
      elf_local_got_ents (abfd) = local_got_ents;
4635
0
    }
4636
4637
0
  if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4638
0
    {
4639
0
      struct got_entry *ent;
4640
4641
0
      for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4642
0
  if (ent->addend == r_addend
4643
0
      && ent->owner == abfd
4644
0
      && ent->tls_type == tls_type)
4645
0
    break;
4646
0
      if (ent == NULL)
4647
0
  {
4648
0
    size_t amt = sizeof (*ent);
4649
0
    ent = bfd_alloc (abfd, amt);
4650
0
    if (ent == NULL)
4651
0
      return NULL;
4652
0
    ent->next = local_got_ents[r_symndx];
4653
0
    ent->addend = r_addend;
4654
0
    ent->owner = abfd;
4655
0
    ent->tls_type = tls_type;
4656
0
    ent->is_indirect = false;
4657
0
    ent->got.refcount = 0;
4658
0
    local_got_ents[r_symndx] = ent;
4659
0
  }
4660
0
      ent->got.refcount += 1;
4661
0
    }
4662
4663
0
  local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4664
0
  local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4665
0
  local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4666
4667
0
  return local_plt + r_symndx;
4668
0
}
4669
4670
static bool
4671
update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4672
0
{
4673
0
  struct plt_entry *ent;
4674
4675
0
  for (ent = *plist; ent != NULL; ent = ent->next)
4676
0
    if (ent->addend == addend)
4677
0
      break;
4678
0
  if (ent == NULL)
4679
0
    {
4680
0
      size_t amt = sizeof (*ent);
4681
0
      ent = bfd_alloc (abfd, amt);
4682
0
      if (ent == NULL)
4683
0
  return false;
4684
0
      ent->next = *plist;
4685
0
      ent->addend = addend;
4686
0
      ent->plt.refcount = 0;
4687
0
      *plist = ent;
4688
0
    }
4689
0
  ent->plt.refcount += 1;
4690
0
  return true;
4691
0
}
4692
4693
static bool
4694
is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4695
0
{
4696
0
  return (r_type == R_PPC64_REL24
4697
0
    || r_type == R_PPC64_REL24_NOTOC
4698
0
    || r_type == R_PPC64_REL24_P9NOTOC
4699
0
    || r_type == R_PPC64_REL14
4700
0
    || r_type == R_PPC64_REL14_BRTAKEN
4701
0
    || r_type == R_PPC64_REL14_BRNTAKEN
4702
0
    || r_type == R_PPC64_ADDR24
4703
0
    || r_type == R_PPC64_ADDR14
4704
0
    || r_type == R_PPC64_ADDR14_BRTAKEN
4705
0
    || r_type == R_PPC64_ADDR14_BRNTAKEN
4706
0
    || r_type == R_PPC64_PLTCALL
4707
0
    || r_type == R_PPC64_PLTCALL_NOTOC);
4708
0
}
4709
4710
/* Relocs on inline plt call sequence insns prior to the call.  */
4711
4712
static bool
4713
is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4714
0
{
4715
0
  return (r_type == R_PPC64_PLT16_HA
4716
0
    || r_type == R_PPC64_PLT16_HI
4717
0
    || r_type == R_PPC64_PLT16_LO
4718
0
    || r_type == R_PPC64_PLT16_LO_DS
4719
0
    || r_type == R_PPC64_PLT_PCREL34
4720
0
    || r_type == R_PPC64_PLT_PCREL34_NOTOC
4721
0
    || r_type == R_PPC64_PLTSEQ
4722
0
    || r_type == R_PPC64_PLTSEQ_NOTOC);
4723
0
}
4724
4725
/* Of relocs which might appear paired with TLSGD and TLSLD marker
4726
   relocs, return true for those that operate on a dword.  */
4727
4728
static bool
4729
is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4730
0
{
4731
0
  return (r_type == R_PPC64_PLT_PCREL34
4732
0
    || r_type == R_PPC64_PLT_PCREL34_NOTOC
4733
0
    || r_type == R_PPC64_PLTCALL);
4734
0
}
4735
4736
/* The RELR encoding doesn't allow odd addresses, so RELR_ALIGN must
4737
   be at least 1.  R_PPC64_RELATIVE relocs require alignment of 2**3.
4738
   We use 3 here to avoid complexity in relocate_section, where for a
4739
   value of 1 we'd need to test for not just an output RELATIVE reloc
4740
   near the call to maybe_relr but also UADDR64 and some conditions on
4741
   the symbol.  See PR30824.  */
4742
0
#define RELR_ALIGN 3
4743
4744
static bool
4745
maybe_relr (enum elf_ppc64_reloc_type r_type,
4746
      const Elf_Internal_Rela *rel,
4747
      const asection *sec)
4748
0
{
4749
0
  return ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
4750
0
    && (rel->r_offset & ((1 << RELR_ALIGN) - 1)) == 0
4751
0
    && sec->alignment_power >= RELR_ALIGN);
4752
0
}
4753
4754
/* Like bfd_reloc_offset_in_range but without a howto.  Return true
4755
   iff a field of SIZE bytes at OFFSET is within SEC limits.  */
4756
4757
static bool
4758
offset_in_range (asection *sec, bfd_vma offset, size_t size)
4759
0
{
4760
0
  return offset <= sec->size && size <= sec->size - offset;
4761
0
}
4762
4763
/* Look through the relocs for a section during the first phase, and
4764
   calculate needed space in the global offset table, procedure
4765
   linkage table, and dynamic reloc sections.  */
4766
4767
static bool
4768
ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4769
      asection *sec, const Elf_Internal_Rela *relocs)
4770
0
{
4771
0
  struct ppc_link_hash_table *htab;
4772
0
  Elf_Internal_Shdr *symtab_hdr;
4773
0
  struct elf_link_hash_entry **sym_hashes;
4774
0
  const Elf_Internal_Rela *rel;
4775
0
  const Elf_Internal_Rela *rel_end;
4776
0
  asection *sreloc;
4777
0
  struct elf_link_hash_entry *tga, *dottga;
4778
0
  bool is_opd;
4779
4780
0
  if (bfd_link_relocatable (info))
4781
0
    return true;
4782
4783
0
  BFD_ASSERT (is_ppc64_elf (abfd));
4784
4785
0
  htab = ppc_hash_table (info);
4786
0
  if (htab == NULL)
4787
0
    return false;
4788
4789
0
  tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4790
0
            false, false, true);
4791
0
  dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4792
0
         false, false, true);
4793
0
  symtab_hdr = &elf_symtab_hdr (abfd);
4794
0
  sym_hashes = elf_sym_hashes (abfd);
4795
0
  sreloc = NULL;
4796
0
  is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4797
0
  rel_end = relocs + sec->reloc_count;
4798
0
  for (rel = relocs; rel < rel_end; rel++)
4799
0
    {
4800
0
      unsigned long r_symndx;
4801
0
      struct elf_link_hash_entry *h;
4802
0
      Elf_Internal_Sym *isym;
4803
0
      enum elf_ppc64_reloc_type r_type;
4804
0
      int tls_type;
4805
0
      struct _ppc64_elf_section_data *ppc64_sec;
4806
0
      struct plt_entry **ifunc, **plt_list;
4807
4808
0
      r_symndx = ELF64_R_SYM (rel->r_info);
4809
0
      if (r_symndx < symtab_hdr->sh_info)
4810
0
  {
4811
0
    h = NULL;
4812
0
    isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
4813
0
    if (isym == NULL)
4814
0
      return false;
4815
0
  }
4816
0
      else
4817
0
  {
4818
0
    isym = NULL;
4819
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4820
0
    h = elf_follow_link (h);
4821
4822
0
    if (h == htab->elf.hgot)
4823
0
      sec->has_toc_reloc = 1;
4824
0
  }
4825
4826
0
      r_type = ELF64_R_TYPE (rel->r_info);
4827
0
      switch (r_type)
4828
0
  {
4829
0
  case R_PPC64_D34:
4830
0
  case R_PPC64_D34_LO:
4831
0
  case R_PPC64_D34_HI30:
4832
0
  case R_PPC64_D34_HA30:
4833
0
  case R_PPC64_D28:
4834
0
  case R_PPC64_TPREL34:
4835
0
  case R_PPC64_DTPREL34:
4836
0
  case R_PPC64_PCREL34:
4837
0
  case R_PPC64_GOT_PCREL34:
4838
0
  case R_PPC64_GOT_TLSGD_PCREL34:
4839
0
  case R_PPC64_GOT_TLSLD_PCREL34:
4840
0
  case R_PPC64_GOT_TPREL_PCREL34:
4841
0
  case R_PPC64_GOT_DTPREL_PCREL34:
4842
0
  case R_PPC64_PLT_PCREL34:
4843
0
  case R_PPC64_PLT_PCREL34_NOTOC:
4844
0
  case R_PPC64_PCREL28:
4845
0
    htab->has_power10_relocs = 1;
4846
0
    break;
4847
0
  default:
4848
0
    break;
4849
0
  }
4850
4851
0
      switch (r_type)
4852
0
  {
4853
0
  case R_PPC64_PLT16_HA:
4854
0
  case R_PPC64_GOT_TLSLD16_HA:
4855
0
  case R_PPC64_GOT_TLSGD16_HA:
4856
0
  case R_PPC64_GOT_TPREL16_HA:
4857
0
  case R_PPC64_GOT_DTPREL16_HA:
4858
0
  case R_PPC64_GOT16_HA:
4859
0
  case R_PPC64_TOC16_HA:
4860
0
  case R_PPC64_PLT16_LO:
4861
0
  case R_PPC64_PLT16_LO_DS:
4862
0
  case R_PPC64_GOT_TLSLD16_LO:
4863
0
  case R_PPC64_GOT_TLSGD16_LO:
4864
0
  case R_PPC64_GOT_TPREL16_LO_DS:
4865
0
  case R_PPC64_GOT_DTPREL16_LO_DS:
4866
0
  case R_PPC64_GOT16_LO:
4867
0
  case R_PPC64_GOT16_LO_DS:
4868
0
  case R_PPC64_TOC16_LO:
4869
0
  case R_PPC64_TOC16_LO_DS:
4870
0
  case R_PPC64_GOT_PCREL34:
4871
0
    ppc64_elf_tdata (abfd)->has_optrel = 1;
4872
0
    ppc64_elf_section_data (sec)->has_optrel = 1;
4873
0
    break;
4874
0
  default:
4875
0
    break;
4876
0
  }
4877
4878
0
      ifunc = NULL;
4879
0
      if (h != NULL)
4880
0
  {
4881
0
    if (h->type == STT_GNU_IFUNC)
4882
0
      {
4883
0
        h->needs_plt = 1;
4884
0
        ifunc = &h->plt.plist;
4885
0
      }
4886
0
  }
4887
0
      else
4888
0
  {
4889
0
    if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4890
0
      {
4891
0
        ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4892
0
               rel->r_addend,
4893
0
               NON_GOT | PLT_IFUNC);
4894
0
        if (ifunc == NULL)
4895
0
    return false;
4896
0
      }
4897
0
  }
4898
4899
0
      tls_type = 0;
4900
0
      switch (r_type)
4901
0
  {
4902
0
  case R_PPC64_PLTSEQ:
4903
0
  case R_PPC64_PLTSEQ_NOTOC:
4904
    /* Inline plt call code emitted by gcc doesn't support
4905
       modifying the tls_index words to short-circuit
4906
       __tls_get_addr calls.  See PR32387.  */
4907
0
    if (h != NULL && (h == tga || h == dottga))
4908
0
      htab->params->tls_get_addr_opt = 0;
4909
0
    break;
4910
4911
0
  case R_PPC64_TLSGD:
4912
0
  case R_PPC64_TLSLD:
4913
    /* These special tls relocs tie a call to __tls_get_addr with
4914
       its parameter symbol.  */
4915
0
    if (h != NULL)
4916
0
      ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
4917
0
    else
4918
0
      if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4919
0
          rel->r_addend,
4920
0
          NON_GOT | TLS_TLS | TLS_MARK))
4921
0
        return false;
4922
0
    sec->has_tls_reloc = 1;
4923
0
    break;
4924
4925
0
  case R_PPC64_GOT_TLSLD16:
4926
0
  case R_PPC64_GOT_TLSLD16_LO:
4927
0
  case R_PPC64_GOT_TLSLD16_HI:
4928
0
  case R_PPC64_GOT_TLSLD16_HA:
4929
0
  case R_PPC64_GOT_TLSLD_PCREL34:
4930
0
    tls_type = TLS_TLS | TLS_LD;
4931
0
    goto dogottls;
4932
4933
0
  case R_PPC64_GOT_TLSGD16:
4934
0
  case R_PPC64_GOT_TLSGD16_LO:
4935
0
  case R_PPC64_GOT_TLSGD16_HI:
4936
0
  case R_PPC64_GOT_TLSGD16_HA:
4937
0
  case R_PPC64_GOT_TLSGD_PCREL34:
4938
0
    tls_type = TLS_TLS | TLS_GD;
4939
0
    goto dogottls;
4940
4941
0
  case R_PPC64_GOT_TPREL16_DS:
4942
0
  case R_PPC64_GOT_TPREL16_LO_DS:
4943
0
  case R_PPC64_GOT_TPREL16_HI:
4944
0
  case R_PPC64_GOT_TPREL16_HA:
4945
0
  case R_PPC64_GOT_TPREL_PCREL34:
4946
0
    if (bfd_link_dll (info))
4947
0
      info->flags |= DF_STATIC_TLS;
4948
0
    tls_type = TLS_TLS | TLS_TPREL;
4949
0
    goto dogottls;
4950
4951
0
  case R_PPC64_GOT_DTPREL16_DS:
4952
0
  case R_PPC64_GOT_DTPREL16_LO_DS:
4953
0
  case R_PPC64_GOT_DTPREL16_HI:
4954
0
  case R_PPC64_GOT_DTPREL16_HA:
4955
0
  case R_PPC64_GOT_DTPREL_PCREL34:
4956
0
    tls_type = TLS_TLS | TLS_DTPREL;
4957
0
  dogottls:
4958
0
    sec->has_tls_reloc = 1;
4959
0
    goto dogot;
4960
4961
0
  case R_PPC64_GOT16:
4962
0
  case R_PPC64_GOT16_LO:
4963
0
  case R_PPC64_GOT16_HI:
4964
0
  case R_PPC64_GOT16_HA:
4965
0
  case R_PPC64_GOT16_DS:
4966
0
  case R_PPC64_GOT16_LO_DS:
4967
0
  case R_PPC64_GOT_PCREL34:
4968
0
  dogot:
4969
    /* This symbol requires a global offset table entry.  */
4970
0
    sec->has_toc_reloc = 1;
4971
0
    if (r_type == R_PPC64_GOT_TLSLD16
4972
0
        || r_type == R_PPC64_GOT_TLSGD16
4973
0
        || r_type == R_PPC64_GOT_TPREL16_DS
4974
0
        || r_type == R_PPC64_GOT_DTPREL16_DS
4975
0
        || r_type == R_PPC64_GOT16
4976
0
        || r_type == R_PPC64_GOT16_DS)
4977
0
      {
4978
0
        htab->do_multi_toc = 1;
4979
0
        ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4980
0
      }
4981
4982
0
    if (ppc64_elf_tdata (abfd)->got == NULL
4983
0
        && !create_got_section (abfd, info))
4984
0
      return false;
4985
4986
0
    if (h != NULL)
4987
0
      {
4988
0
        struct ppc_link_hash_entry *eh;
4989
0
        struct got_entry *ent;
4990
4991
0
        eh = ppc_elf_hash_entry (h);
4992
0
        for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4993
0
    if (ent->addend == rel->r_addend
4994
0
        && ent->owner == abfd
4995
0
        && ent->tls_type == tls_type)
4996
0
      break;
4997
0
        if (ent == NULL)
4998
0
    {
4999
0
      size_t amt = sizeof (*ent);
5000
0
      ent = bfd_alloc (abfd, amt);
5001
0
      if (ent == NULL)
5002
0
        return false;
5003
0
      ent->next = eh->elf.got.glist;
5004
0
      ent->addend = rel->r_addend;
5005
0
      ent->owner = abfd;
5006
0
      ent->tls_type = tls_type;
5007
0
      ent->is_indirect = false;
5008
0
      ent->got.refcount = 0;
5009
0
      eh->elf.got.glist = ent;
5010
0
    }
5011
0
        ent->got.refcount += 1;
5012
0
        eh->tls_mask |= tls_type;
5013
0
      }
5014
0
    else
5015
      /* This is a global offset table entry for a local symbol.  */
5016
0
      if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5017
0
          rel->r_addend, tls_type))
5018
0
        return false;
5019
0
    break;
5020
5021
0
  case R_PPC64_PLT16_HA:
5022
0
  case R_PPC64_PLT16_HI:
5023
0
  case R_PPC64_PLT16_LO:
5024
0
  case R_PPC64_PLT16_LO_DS:
5025
0
  case R_PPC64_PLT_PCREL34:
5026
0
  case R_PPC64_PLT_PCREL34_NOTOC:
5027
0
  case R_PPC64_PLT32:
5028
0
  case R_PPC64_PLT64:
5029
    /* This symbol requires a procedure linkage table entry.  */
5030
0
    plt_list = ifunc;
5031
0
    if (h != NULL)
5032
0
      {
5033
0
        h->needs_plt = 1;
5034
0
        if (h->root.root.string[0] == '.'
5035
0
      && h->root.root.string[1] != '\0')
5036
0
    ppc_elf_hash_entry (h)->is_func = 1;
5037
0
        ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
5038
0
        plt_list = &h->plt.plist;
5039
0
      }
5040
0
    if (plt_list == NULL)
5041
0
      plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5042
0
                rel->r_addend,
5043
0
                NON_GOT | PLT_KEEP);
5044
0
    if (!update_plt_info (abfd, plt_list, rel->r_addend))
5045
0
      return false;
5046
0
    break;
5047
5048
    /* The following relocations don't need to propagate the
5049
       relocation if linking a shared object since they are
5050
       section relative.  */
5051
0
  case R_PPC64_SECTOFF:
5052
0
  case R_PPC64_SECTOFF_LO:
5053
0
  case R_PPC64_SECTOFF_HI:
5054
0
  case R_PPC64_SECTOFF_HA:
5055
0
  case R_PPC64_SECTOFF_DS:
5056
0
  case R_PPC64_SECTOFF_LO_DS:
5057
0
  case R_PPC64_DTPREL16:
5058
0
  case R_PPC64_DTPREL16_LO:
5059
0
  case R_PPC64_DTPREL16_HI:
5060
0
  case R_PPC64_DTPREL16_HA:
5061
0
  case R_PPC64_DTPREL16_DS:
5062
0
  case R_PPC64_DTPREL16_LO_DS:
5063
0
  case R_PPC64_DTPREL16_HIGH:
5064
0
  case R_PPC64_DTPREL16_HIGHA:
5065
0
  case R_PPC64_DTPREL16_HIGHER:
5066
0
  case R_PPC64_DTPREL16_HIGHERA:
5067
0
  case R_PPC64_DTPREL16_HIGHEST:
5068
0
  case R_PPC64_DTPREL16_HIGHESTA:
5069
0
    break;
5070
5071
    /* Nor do these.  */
5072
0
  case R_PPC64_REL16:
5073
0
  case R_PPC64_REL16_LO:
5074
0
  case R_PPC64_REL16_HI:
5075
0
  case R_PPC64_REL16_HA:
5076
0
  case R_PPC64_REL16_HIGH:
5077
0
  case R_PPC64_REL16_HIGHA:
5078
0
  case R_PPC64_REL16_HIGHER:
5079
0
  case R_PPC64_REL16_HIGHERA:
5080
0
  case R_PPC64_REL16_HIGHEST:
5081
0
  case R_PPC64_REL16_HIGHESTA:
5082
0
  case R_PPC64_REL16_HIGHER34:
5083
0
  case R_PPC64_REL16_HIGHERA34:
5084
0
  case R_PPC64_REL16_HIGHEST34:
5085
0
  case R_PPC64_REL16_HIGHESTA34:
5086
0
  case R_PPC64_REL16DX_HA:
5087
0
    break;
5088
5089
    /* Not supported as a dynamic relocation.  */
5090
0
  case R_PPC64_ADDR64_LOCAL:
5091
0
    if (bfd_link_pic (info))
5092
0
      {
5093
0
        if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5094
0
    ppc_howto_init ();
5095
        /* xgettext:c-format */
5096
0
        info->callbacks->einfo (_("%H: %s unsupported "
5097
0
          "in shared libraries and PIEs\n"),
5098
0
              abfd, sec, rel->r_offset,
5099
0
              ppc64_elf_howto_table[r_type]->name);
5100
0
        bfd_set_error (bfd_error_bad_value);
5101
0
        return false;
5102
0
      }
5103
0
    break;
5104
5105
0
  case R_PPC64_TOC16:
5106
0
  case R_PPC64_TOC16_DS:
5107
0
    htab->do_multi_toc = 1;
5108
0
    ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5109
    /* Fall through.  */
5110
0
  case R_PPC64_TOC16_LO:
5111
0
  case R_PPC64_TOC16_HI:
5112
0
  case R_PPC64_TOC16_HA:
5113
0
  case R_PPC64_TOC16_LO_DS:
5114
0
    sec->has_toc_reloc = 1;
5115
0
    if (h != NULL && bfd_link_executable (info))
5116
0
      {
5117
        /* We may need a copy reloc.  */
5118
0
        h->non_got_ref = 1;
5119
        /* Strongly prefer a copy reloc over a dynamic reloc.
5120
     glibc ld.so as of 2019-08 will error out if one of
5121
     these relocations is emitted.  */
5122
0
        h->needs_copy = 1;
5123
0
        goto dodyn;
5124
0
      }
5125
0
    break;
5126
5127
    /* Marker reloc.  */
5128
0
  case R_PPC64_ENTRY:
5129
0
    break;
5130
5131
    /* This relocation describes the C++ object vtable hierarchy.
5132
       Reconstruct it for later use during GC.  */
5133
0
  case R_PPC64_GNU_VTINHERIT:
5134
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5135
0
      return false;
5136
0
    break;
5137
5138
    /* This relocation describes which C++ vtable entries are actually
5139
       used.  Record for later use during GC.  */
5140
0
  case R_PPC64_GNU_VTENTRY:
5141
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5142
0
      return false;
5143
0
    break;
5144
5145
0
  case R_PPC64_REL14:
5146
0
  case R_PPC64_REL14_BRTAKEN:
5147
0
  case R_PPC64_REL14_BRNTAKEN:
5148
0
    {
5149
0
      asection *dest = NULL;
5150
5151
      /* Heuristic: If jumping outside our section, chances are
5152
         we are going to need a stub.  */
5153
0
      if (h != NULL)
5154
0
        {
5155
    /* If the sym is weak it may be overridden later, so
5156
       don't assume we know where a weak sym lives.  */
5157
0
    if (h->root.type == bfd_link_hash_defined)
5158
0
      dest = h->root.u.def.section;
5159
0
        }
5160
0
      else
5161
0
        dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5162
5163
0
      if (dest != sec)
5164
0
        ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5165
0
    }
5166
0
    goto rel24;
5167
5168
0
  case R_PPC64_PLTCALL:
5169
0
  case R_PPC64_PLTCALL_NOTOC:
5170
0
    ppc64_elf_section_data (sec)->has_pltcall = 1;
5171
    /* Fall through.  */
5172
5173
0
  case R_PPC64_REL24:
5174
0
  case R_PPC64_REL24_NOTOC:
5175
0
  case R_PPC64_REL24_P9NOTOC:
5176
0
  rel24:
5177
0
    plt_list = ifunc;
5178
0
    if (h != NULL)
5179
0
      {
5180
0
        h->needs_plt = 1;
5181
0
        if (h->root.root.string[0] == '.'
5182
0
      && h->root.root.string[1] != '\0')
5183
0
    ppc_elf_hash_entry (h)->is_func = 1;
5184
5185
0
        if (h == tga || h == dottga)
5186
0
    {
5187
0
      sec->has_tls_reloc = 1;
5188
0
      if (rel != relocs
5189
0
          && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5190
0
        || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5191
        /* We have a new-style __tls_get_addr call with
5192
           a marker reloc.  */
5193
0
        ;
5194
0
      else
5195
        /* Mark this section as having an old-style call.  */
5196
0
        sec->nomark_tls_get_addr = 1;
5197
0
    }
5198
0
        plt_list = &h->plt.plist;
5199
0
      }
5200
5201
    /* We may need a .plt entry if the function this reloc
5202
       refers to is in a shared lib.  */
5203
0
    if (plt_list
5204
0
        && !update_plt_info (abfd, plt_list, rel->r_addend))
5205
0
      return false;
5206
0
    break;
5207
5208
0
  case R_PPC64_ADDR14:
5209
0
  case R_PPC64_ADDR14_BRNTAKEN:
5210
0
  case R_PPC64_ADDR14_BRTAKEN:
5211
0
  case R_PPC64_ADDR24:
5212
0
    goto dodyn;
5213
5214
0
  case R_PPC64_TPREL64:
5215
0
    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5216
0
    if (bfd_link_dll (info))
5217
0
      info->flags |= DF_STATIC_TLS;
5218
0
    goto dotlstoc;
5219
5220
0
  case R_PPC64_DTPMOD64:
5221
0
    if (rel + 1 < rel_end
5222
0
        && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5223
0
        && rel[1].r_offset == rel->r_offset + 8)
5224
0
      tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5225
0
    else
5226
0
      tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5227
0
    goto dotlstoc;
5228
5229
0
  case R_PPC64_DTPREL64:
5230
0
    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5231
0
    if (rel != relocs
5232
0
        && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5233
0
        && rel[-1].r_offset == rel->r_offset - 8)
5234
      /* This is the second reloc of a dtpmod, dtprel pair.
5235
         Don't mark with TLS_DTPREL.  */
5236
0
      goto dodyn;
5237
5238
0
  dotlstoc:
5239
0
    sec->has_tls_reloc = 1;
5240
0
    if (h != NULL)
5241
0
      ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
5242
0
    else
5243
0
      if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5244
0
          rel->r_addend, tls_type))
5245
0
        return false;
5246
5247
0
    ppc64_sec = ppc64_elf_section_data (sec);
5248
0
    if (ppc64_sec->sec_type == sec_normal)
5249
0
      {
5250
0
        bfd_size_type amt;
5251
5252
        /* One extra to simplify get_tls_mask.  */
5253
0
        amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5254
0
        ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5255
0
        if (ppc64_sec->u.toc.symndx == NULL)
5256
0
    return false;
5257
0
        amt = sec->size * sizeof (bfd_vma) / 8;
5258
0
        ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5259
0
        if (ppc64_sec->u.toc.add == NULL)
5260
0
    return false;
5261
0
        ppc64_sec->sec_type = sec_toc;
5262
0
      }
5263
0
    if (ppc64_sec->sec_type != sec_toc
5264
0
        || rel->r_offset % 8 != 0)
5265
0
      {
5266
0
        info->callbacks->einfo (_("%H: %s unsupported here\n"),
5267
0
              abfd, sec, rel->r_offset,
5268
0
              ppc64_elf_howto_table[r_type]->name);
5269
0
        bfd_set_error (bfd_error_bad_value);
5270
0
        return false;
5271
0
      }
5272
0
    ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5273
0
    ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5274
5275
    /* Mark the second slot of a GD or LD entry.
5276
       -1 to indicate GD and -2 to indicate LD.  */
5277
0
    if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5278
0
      ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5279
0
    else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5280
0
      ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5281
0
    goto dodyn;
5282
5283
0
  case R_PPC64_TPREL16_HI:
5284
0
  case R_PPC64_TPREL16_HA:
5285
0
  case R_PPC64_TPREL16_HIGH:
5286
0
  case R_PPC64_TPREL16_HIGHA:
5287
0
  case R_PPC64_TPREL16_HIGHER:
5288
0
  case R_PPC64_TPREL16_HIGHERA:
5289
0
  case R_PPC64_TPREL16_HIGHEST:
5290
0
  case R_PPC64_TPREL16_HIGHESTA:
5291
0
    sec->has_tls_reloc = 1;
5292
    /* Fall through.  */
5293
0
  case R_PPC64_TPREL34:
5294
0
  case R_PPC64_TPREL16:
5295
0
  case R_PPC64_TPREL16_DS:
5296
0
  case R_PPC64_TPREL16_LO:
5297
0
  case R_PPC64_TPREL16_LO_DS:
5298
0
    if (bfd_link_dll (info))
5299
0
      info->flags |= DF_STATIC_TLS;
5300
0
    goto dodyn;
5301
5302
0
  case R_PPC64_ADDR64:
5303
0
    if (is_opd
5304
0
        && rel + 1 < rel_end
5305
0
        && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5306
0
      {
5307
0
        if (h != NULL)
5308
0
    ppc_elf_hash_entry (h)->is_func = 1;
5309
0
      }
5310
    /* Fall through.  */
5311
5312
0
  case R_PPC64_ADDR16:
5313
0
  case R_PPC64_ADDR16_DS:
5314
0
  case R_PPC64_ADDR16_HA:
5315
0
  case R_PPC64_ADDR16_HI:
5316
0
  case R_PPC64_ADDR16_HIGH:
5317
0
  case R_PPC64_ADDR16_HIGHA:
5318
0
  case R_PPC64_ADDR16_HIGHER:
5319
0
  case R_PPC64_ADDR16_HIGHERA:
5320
0
  case R_PPC64_ADDR16_HIGHEST:
5321
0
  case R_PPC64_ADDR16_HIGHESTA:
5322
0
  case R_PPC64_ADDR16_LO:
5323
0
  case R_PPC64_ADDR16_LO_DS:
5324
0
  case R_PPC64_D34:
5325
0
  case R_PPC64_D34_LO:
5326
0
  case R_PPC64_D34_HI30:
5327
0
  case R_PPC64_D34_HA30:
5328
0
  case R_PPC64_ADDR16_HIGHER34:
5329
0
  case R_PPC64_ADDR16_HIGHERA34:
5330
0
  case R_PPC64_ADDR16_HIGHEST34:
5331
0
  case R_PPC64_ADDR16_HIGHESTA34:
5332
0
  case R_PPC64_D28:
5333
0
    if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5334
0
        && rel->r_addend == 0)
5335
0
      {
5336
        /* We may need a .plt entry if this reloc refers to a
5337
     function in a shared lib.  */
5338
0
        if (!update_plt_info (abfd, &h->plt.plist, 0))
5339
0
    return false;
5340
0
        h->pointer_equality_needed = 1;
5341
0
      }
5342
    /* Fall through.  */
5343
5344
0
  case R_PPC64_REL30:
5345
0
  case R_PPC64_REL32:
5346
0
  case R_PPC64_REL64:
5347
0
  case R_PPC64_ADDR32:
5348
0
  case R_PPC64_UADDR16:
5349
0
  case R_PPC64_UADDR32:
5350
0
  case R_PPC64_UADDR64:
5351
0
  case R_PPC64_TOC:
5352
0
    if (h != NULL && bfd_link_executable (info))
5353
      /* We may need a copy reloc.  */
5354
0
      h->non_got_ref = 1;
5355
5356
    /* Don't propagate .opd relocs.  */
5357
0
    if (NO_OPD_RELOCS && is_opd)
5358
0
      break;
5359
5360
    /* Set up information for symbols that might need dynamic
5361
       relocations.  At this point in linking we have read all
5362
       the input files and resolved most symbols, but have not
5363
       yet decided whether symbols are dynamic or finalized
5364
       symbol flags.  In some cases we might be setting dynamic
5365
       reloc info for symbols that do not end up needing such.
5366
       That's OK, adjust_dynamic_symbol and allocate_dynrelocs
5367
       work together with this code.  */
5368
0
  dodyn:
5369
0
    if ((h != NULL
5370
0
         && !SYMBOL_REFERENCES_LOCAL (info, h))
5371
0
        || (bfd_link_pic (info)
5372
0
      && (h != NULL
5373
0
          ? !bfd_is_abs_symbol (&h->root)
5374
0
          : isym->st_shndx != SHN_ABS)
5375
0
      && must_be_dyn_reloc (info, r_type))
5376
0
        || (!bfd_link_pic (info)
5377
0
      && ifunc != NULL))
5378
0
      {
5379
        /* We must copy these reloc types into the output file.
5380
     Create a reloc section in dynobj and make room for
5381
     this reloc.  */
5382
0
        if (sreloc == NULL)
5383
0
    {
5384
0
      sreloc = _bfd_elf_make_dynamic_reloc_section
5385
0
        (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
5386
5387
0
      if (sreloc == NULL)
5388
0
        return false;
5389
0
    }
5390
5391
        /* If this is a global symbol, we count the number of
5392
     relocations we need for this symbol.  */
5393
0
        if (h != NULL)
5394
0
    {
5395
0
      struct ppc_dyn_relocs *p;
5396
0
      struct ppc_dyn_relocs **head;
5397
5398
0
      head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
5399
0
      p = *head;
5400
0
      if (p == NULL || p->sec != sec)
5401
0
        {
5402
0
          p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5403
0
          if (p == NULL)
5404
0
      return false;
5405
0
          p->next = *head;
5406
0
          *head = p;
5407
0
          p->sec = sec;
5408
0
          p->count = 0;
5409
0
          p->pc_count = 0;
5410
0
          p->rel_count = 0;
5411
0
        }
5412
0
      p->count += 1;
5413
0
      if (!must_be_dyn_reloc (info, r_type))
5414
0
        p->pc_count += 1;
5415
0
      if (maybe_relr (r_type, rel, sec))
5416
0
        p->rel_count += 1;
5417
0
    }
5418
0
        else
5419
0
    {
5420
      /* Track dynamic relocs needed for local syms too.  */
5421
0
      struct ppc_local_dyn_relocs *p;
5422
0
      struct ppc_local_dyn_relocs **head;
5423
0
      bool is_ifunc;
5424
0
      asection *s;
5425
0
      void *vpp;
5426
5427
0
      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5428
0
      if (s == NULL)
5429
0
        s = sec;
5430
5431
0
      vpp = &elf_section_data (s)->local_dynrel;
5432
0
      head = (struct ppc_local_dyn_relocs **) vpp;
5433
0
      is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5434
0
      p = *head;
5435
0
      if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5436
0
        p = p->next;
5437
0
      if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5438
0
        {
5439
0
          p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5440
0
          if (p == NULL)
5441
0
      return false;
5442
0
          p->next = *head;
5443
0
          *head = p;
5444
0
          p->sec = sec;
5445
0
          p->count = 0;
5446
0
          p->rel_count = 0;
5447
0
          p->ifunc = is_ifunc;
5448
0
        }
5449
0
      p->count += 1;
5450
0
      if (maybe_relr (r_type, rel, sec))
5451
0
        p->rel_count += 1;
5452
0
    }
5453
0
      }
5454
0
    break;
5455
5456
0
  default:
5457
0
    break;
5458
0
  }
5459
0
    }
5460
5461
0
  return true;
5462
0
}
5463
5464
/* Merge backend specific data from an object file to the output
5465
   object file when linking.  */
5466
5467
static bool
5468
ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5469
0
{
5470
0
  bfd *obfd = info->output_bfd;
5471
0
  unsigned long iflags, oflags;
5472
5473
0
  if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5474
0
    return true;
5475
5476
0
  if (!is_ppc64_elf (ibfd))
5477
0
    return true;
5478
5479
0
  if (!_bfd_generic_verify_endian_match (ibfd, info))
5480
0
    return false;
5481
5482
0
  iflags = elf_elfheader (ibfd)->e_flags;
5483
0
  oflags = elf_elfheader (obfd)->e_flags;
5484
5485
0
  if (iflags & ~EF_PPC64_ABI)
5486
0
    {
5487
0
      _bfd_error_handler
5488
  /* xgettext:c-format */
5489
0
  (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5490
0
      bfd_set_error (bfd_error_bad_value);
5491
0
      return false;
5492
0
    }
5493
0
  else if (iflags != oflags && iflags != 0)
5494
0
    {
5495
0
      _bfd_error_handler
5496
  /* xgettext:c-format */
5497
0
  (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5498
0
   ibfd, iflags, oflags);
5499
0
      bfd_set_error (bfd_error_bad_value);
5500
0
      return false;
5501
0
    }
5502
5503
0
  if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5504
0
    return false;
5505
5506
  /* Merge Tag_compatibility attributes and any common GNU ones.  */
5507
0
  return _bfd_elf_merge_object_attributes (ibfd, info);
5508
0
}
5509
5510
static bool
5511
ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5512
54
{
5513
  /* Print normal ELF private data.  */
5514
54
  _bfd_elf_print_private_bfd_data (abfd, ptr);
5515
5516
54
  if (elf_elfheader (abfd)->e_flags != 0)
5517
3
    {
5518
3
      FILE *file = ptr;
5519
5520
3
      fprintf (file, _("private flags = 0x%lx:"),
5521
3
         elf_elfheader (abfd)->e_flags);
5522
5523
3
      if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5524
2
  fprintf (file, _(" [abiv%ld]"),
5525
2
     elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5526
3
      fputc ('\n', file);
5527
3
    }
5528
5529
54
  return true;
5530
54
}
5531
5532
/* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
5533
   of the code entry point, and its section, which must be in the same
5534
   object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
5535
5536
static bfd_vma
5537
opd_entry_value (asection *opd_sec,
5538
     bfd_vma offset,
5539
     asection **code_sec,
5540
     bfd_vma *code_off,
5541
     bool in_code_sec)
5542
125
{
5543
125
  bfd *opd_bfd = opd_sec->owner;
5544
125
  Elf_Internal_Rela *relocs;
5545
125
  Elf_Internal_Rela *lo, *hi, *look;
5546
125
  bfd_vma val;
5547
5548
125
  if (!is_ppc64_elf (opd_bfd))
5549
0
    return (bfd_vma) -1;
5550
5551
125
  if (ppc64_elf_section_data (opd_sec)->sec_type == sec_normal)
5552
6
    ppc64_elf_section_data (opd_sec)->sec_type = sec_opd;
5553
119
  else if (ppc64_elf_section_data (opd_sec)->sec_type != sec_opd)
5554
0
    return (bfd_vma) -1;
5555
5556
  /* No relocs implies we are linking a --just-symbols object, or looking
5557
     at a final linked executable with addr2line or somesuch.  */
5558
125
  if (opd_sec->reloc_count == 0)
5559
125
    {
5560
125
      bfd_byte *contents = ppc64_elf_section_data (opd_sec)->u.opd.u.contents;
5561
5562
125
      if (contents == NULL)
5563
125
  {
5564
125
    if ((opd_sec->flags & SEC_HAS_CONTENTS) == 0
5565
41
        || !bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5566
125
      return (bfd_vma) -1;
5567
0
    ppc64_elf_section_data (opd_sec)->u.opd.u.contents = contents;
5568
0
  }
5569
5570
      /* PR 17512: file: 64b9dfbb.  */
5571
0
      if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5572
0
  return (bfd_vma) -1;
5573
5574
0
      val = bfd_get_64 (opd_bfd, contents + offset);
5575
0
      if (code_sec != NULL)
5576
0
  {
5577
0
    asection *sec, *likely = NULL;
5578
5579
0
    if (in_code_sec)
5580
0
      {
5581
0
        sec = *code_sec;
5582
0
        if (sec->vma <= val
5583
0
      && val < sec->vma + sec->size)
5584
0
    likely = sec;
5585
0
        else
5586
0
    val = -1;
5587
0
      }
5588
0
    else
5589
0
      for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5590
0
        if (sec->vma <= val
5591
0
      && (sec->flags & SEC_LOAD) != 0
5592
0
      && (sec->flags & SEC_ALLOC) != 0)
5593
0
    likely = sec;
5594
0
    if (likely != NULL)
5595
0
      {
5596
0
        *code_sec = likely;
5597
0
        if (code_off != NULL)
5598
0
    *code_off = val - likely->vma;
5599
0
      }
5600
0
  }
5601
0
      return val;
5602
0
    }
5603
5604
0
  relocs = ppc64_elf_section_data (opd_sec)->u.opd.u.relocs;
5605
0
  if (relocs == NULL)
5606
0
    relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
5607
  /* PR 17512: file: df8e1fd6.  */
5608
0
  if (relocs == NULL)
5609
0
    return (bfd_vma) -1;
5610
5611
  /* Go find the opd reloc at the sym address.  */
5612
0
  lo = relocs;
5613
0
  hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5614
0
  val = (bfd_vma) -1;
5615
0
  while (lo < hi)
5616
0
    {
5617
0
      look = lo + (hi - lo) / 2;
5618
0
      if (look->r_offset < offset)
5619
0
  lo = look + 1;
5620
0
      else if (look->r_offset > offset)
5621
0
  hi = look;
5622
0
      else
5623
0
  {
5624
0
    Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5625
5626
0
    if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5627
0
        && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5628
0
      {
5629
0
        unsigned long symndx = ELF64_R_SYM (look->r_info);
5630
0
        asection *sec = NULL;
5631
5632
0
        if (symndx >= symtab_hdr->sh_info
5633
0
      && elf_sym_hashes (opd_bfd) != NULL)
5634
0
    {
5635
0
      struct elf_link_hash_entry **sym_hashes;
5636
0
      struct elf_link_hash_entry *rh;
5637
5638
0
      sym_hashes = elf_sym_hashes (opd_bfd);
5639
0
      rh = sym_hashes[symndx - symtab_hdr->sh_info];
5640
0
      if (rh != NULL)
5641
0
        {
5642
0
          rh = elf_follow_link (rh);
5643
0
          if (rh->root.type != bfd_link_hash_defined
5644
0
        && rh->root.type != bfd_link_hash_defweak)
5645
0
      break;
5646
0
          if (rh->root.u.def.section->owner == opd_bfd)
5647
0
      {
5648
0
        val = rh->root.u.def.value;
5649
0
        sec = rh->root.u.def.section;
5650
0
      }
5651
0
        }
5652
0
    }
5653
5654
0
        if (sec == NULL)
5655
0
    {
5656
0
      Elf_Internal_Sym *sym;
5657
5658
0
      if (symndx < symtab_hdr->sh_info)
5659
0
        {
5660
0
          sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5661
0
          if (sym == NULL)
5662
0
      {
5663
0
        size_t symcnt = symtab_hdr->sh_info;
5664
0
        sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5665
0
                  symcnt, 0,
5666
0
                  NULL, NULL, NULL);
5667
0
        if (sym == NULL)
5668
0
          break;
5669
0
        symtab_hdr->contents = (bfd_byte *) sym;
5670
0
      }
5671
0
          sym += symndx;
5672
0
        }
5673
0
      else
5674
0
        {
5675
0
          sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5676
0
              1, symndx,
5677
0
              NULL, NULL, NULL);
5678
0
          if (sym == NULL)
5679
0
      break;
5680
0
        }
5681
0
      sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5682
0
      if (sec != NULL)
5683
0
        {
5684
0
          BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5685
0
          val = sym->st_value;
5686
0
        }
5687
0
      if (symndx >= symtab_hdr->sh_info)
5688
0
        free (sym);
5689
0
      if (sec == NULL)
5690
0
        break;
5691
0
    }
5692
5693
0
        val += look->r_addend;
5694
0
        if (code_off != NULL)
5695
0
    *code_off = val;
5696
0
        if (code_sec != NULL)
5697
0
    {
5698
0
      if (in_code_sec && *code_sec != sec)
5699
0
        return -1;
5700
0
      else
5701
0
        *code_sec = sec;
5702
0
    }
5703
0
        if (sec->output_section != NULL)
5704
0
    val += sec->output_section->vma + sec->output_offset;
5705
0
      }
5706
0
    break;
5707
0
  }
5708
0
    }
5709
5710
0
  return val;
5711
0
}
5712
5713
/* If the ELF symbol SYM might be a function in SEC, return the
5714
   function size and set *CODE_OFF to the function's entry point,
5715
   otherwise return zero.  */
5716
5717
static bfd_size_type
5718
ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5719
            bfd_vma *code_off)
5720
25.9k
{
5721
25.9k
  bfd_size_type size;
5722
25.9k
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
5723
5724
25.9k
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5725
25.9k
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5726
3.44k
    return 0;
5727
5728
22.4k
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5729
5730
  /* In theory we should check that the symbol's type satisfies
5731
     _bfd_elf_is_function_type(), but there are some function-like
5732
     symbols which would fail this test.  (eg _start).  Instead
5733
     we check for hidden, local, notype symbols with zero size.
5734
     This type of symbol is generated by the annobin plugin for gcc
5735
     and clang, and should not be considered to be a function symbol.  */
5736
22.4k
  if (size == 0
5737
7.15k
      && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5738
5.18k
      && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5739
4.94k
      && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5740
193
    return 0;
5741
5742
22.2k
  if (strcmp (sym->section->name, ".opd") == 0)
5743
125
    {
5744
125
      struct _opd_sec_data *opd = get_opd_info (sym->section);
5745
125
      bfd_vma symval = sym->value;
5746
5747
125
      if (opd != NULL
5748
119
    && opd->adjust != NULL
5749
0
    && elf_section_data (sym->section)->relocs != NULL)
5750
0
  {
5751
    /* opd_entry_value will use cached relocs that have been
5752
       adjusted, but with raw symbols.  That means both local
5753
       and global symbols need adjusting.  */
5754
0
    long adjust = opd->adjust[OPD_NDX (symval)];
5755
0
    if (adjust == -1)
5756
0
      return 0;
5757
0
    symval += adjust;
5758
0
  }
5759
5760
125
      if (opd_entry_value (sym->section, symval,
5761
125
         &sec, code_off, true) == (bfd_vma) -1)
5762
125
  return 0;
5763
      /* An old ABI binary with dot-syms has a size of 24 on the .opd
5764
   symbol.  This size has nothing to do with the code size of the
5765
   function, which is what we're supposed to return, but the
5766
   code size isn't available without looking up the dot-sym.
5767
   However, doing that would be a waste of time particularly
5768
   since elf_find_function will look at the dot-sym anyway.
5769
   Now, elf_find_function will keep the largest size of any
5770
   function sym found at the code address of interest, so return
5771
   1 here to avoid it incorrectly caching a larger function size
5772
   for a small function.  This does mean we return the wrong
5773
   size for a new-ABI function of size 24, but all that does is
5774
   disable caching for such functions.  */
5775
0
      if (size == 24)
5776
0
  size = 1;
5777
0
    }
5778
22.1k
  else
5779
22.1k
    {
5780
22.1k
      if (sym->section != sec)
5781
21.8k
  return 0;
5782
368
      *code_off = sym->value;
5783
368
    }
5784
5785
  /* Do not return 0 for the function's size.  */
5786
368
  return size ? size : 1;
5787
22.2k
}
5788
5789
/* Return true if symbol is a strong function defined in an ELFv2
5790
   object with st_other localentry bits of zero, ie. its local entry
5791
   point coincides with its global entry point.  */
5792
5793
static bool
5794
is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5795
0
{
5796
0
  return (h != NULL
5797
0
    && h->type == STT_FUNC
5798
0
    && h->root.type == bfd_link_hash_defined
5799
0
    && (STO_PPC64_LOCAL_MASK & h->other) == 0
5800
0
    && !ppc_elf_hash_entry (h)->non_zero_localentry
5801
0
    && is_ppc64_elf (h->root.u.def.section->owner)
5802
0
    && abiversion (h->root.u.def.section->owner) >= 2);
5803
0
}
5804
5805
/* Return true if symbol is defined in a regular object file.  */
5806
5807
static bool
5808
is_static_defined (struct elf_link_hash_entry *h)
5809
0
{
5810
0
  return ((h->root.type == bfd_link_hash_defined
5811
0
     || h->root.type == bfd_link_hash_defweak)
5812
0
    && h->root.u.def.section != NULL
5813
0
    && h->root.u.def.section->output_section != NULL);
5814
0
}
5815
5816
/* If FDH is a function descriptor symbol, return the associated code
5817
   entry symbol if it is defined.  Return NULL otherwise.  */
5818
5819
static struct ppc_link_hash_entry *
5820
defined_code_entry (struct ppc_link_hash_entry *fdh)
5821
0
{
5822
0
  if (fdh->is_func_descriptor)
5823
0
    {
5824
0
      struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5825
0
      if (fh->elf.root.type == bfd_link_hash_defined
5826
0
    || fh->elf.root.type == bfd_link_hash_defweak)
5827
0
  return fh;
5828
0
    }
5829
0
  return NULL;
5830
0
}
5831
5832
/* If FH is a function code entry symbol, return the associated
5833
   function descriptor symbol if it is defined.  Return NULL otherwise.  */
5834
5835
static struct ppc_link_hash_entry *
5836
defined_func_desc (struct ppc_link_hash_entry *fh)
5837
0
{
5838
0
  if (fh->oh != NULL
5839
0
      && fh->oh->is_func_descriptor)
5840
0
    {
5841
0
      struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5842
0
      if (fdh->elf.root.type == bfd_link_hash_defined
5843
0
    || fdh->elf.root.type == bfd_link_hash_defweak)
5844
0
  return fdh;
5845
0
    }
5846
0
  return NULL;
5847
0
}
5848
5849
/* Given H is a symbol that satisfies is_static_defined, return the
5850
   value in the output file.  */
5851
5852
static bfd_vma
5853
defined_sym_val (struct elf_link_hash_entry *h)
5854
0
{
5855
0
  return (h->root.u.def.section->output_section->vma
5856
0
    + h->root.u.def.section->output_offset
5857
0
    + h->root.u.def.value);
5858
0
}
5859
5860
/* Return true if H matches __tls_get_addr or one of its variants.  */
5861
5862
static bool
5863
is_tls_get_addr (struct elf_link_hash_entry *h,
5864
     struct ppc_link_hash_table *htab)
5865
0
{
5866
0
  return (h == elf_hash_entry (htab->tls_get_addr_fd)
5867
0
    || h == elf_hash_entry (htab->tga_desc_fd)
5868
0
    || h == elf_hash_entry (htab->tls_get_addr)
5869
0
    || h == elf_hash_entry (htab->tga_desc));
5870
0
}
5871
5872
static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
5873
5874
/* Garbage collect sections, after first dealing with dot-symbols.  */
5875
5876
static bool
5877
ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5878
0
{
5879
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
5880
5881
0
  if (htab != NULL && htab->need_func_desc_adj)
5882
0
    {
5883
0
      elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5884
0
      htab->need_func_desc_adj = 0;
5885
0
    }
5886
0
  return bfd_elf_gc_sections (abfd, info);
5887
0
}
5888
5889
/* Mark all our entry sym sections, both opd and code section.  */
5890
5891
static void
5892
ppc64_elf_gc_keep (struct bfd_link_info *info)
5893
0
{
5894
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
5895
0
  struct bfd_sym_chain *sym;
5896
5897
0
  if (htab == NULL)
5898
0
    return;
5899
5900
0
  for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5901
0
    {
5902
0
      struct ppc_link_hash_entry *eh, *fh;
5903
0
      asection *sec;
5904
5905
0
      eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5906
0
                 false, false, true));
5907
0
      if (eh == NULL)
5908
0
  continue;
5909
0
      if (eh->elf.root.type != bfd_link_hash_defined
5910
0
    && eh->elf.root.type != bfd_link_hash_defweak)
5911
0
  continue;
5912
5913
0
      fh = defined_code_entry (eh);
5914
0
      if (fh != NULL)
5915
0
  {
5916
0
    sec = fh->elf.root.u.def.section;
5917
0
    sec->flags |= SEC_KEEP;
5918
0
  }
5919
0
      else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5920
0
         && opd_entry_value (eh->elf.root.u.def.section,
5921
0
           eh->elf.root.u.def.value,
5922
0
           &sec, NULL, false) != (bfd_vma) -1)
5923
0
  sec->flags |= SEC_KEEP;
5924
5925
0
      sec = eh->elf.root.u.def.section;
5926
0
      sec->flags |= SEC_KEEP;
5927
0
    }
5928
0
}
5929
5930
/* Mark sections containing dynamically referenced symbols.  When
5931
   building shared libraries, we must assume that any visible symbol is
5932
   referenced.  */
5933
5934
static bool
5935
ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5936
0
{
5937
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
5938
0
  struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
5939
0
  struct ppc_link_hash_entry *fdh;
5940
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
5941
5942
  /* Dynamic linking info is on the func descriptor sym.  */
5943
0
  fdh = defined_func_desc (eh);
5944
0
  if (fdh != NULL)
5945
0
    eh = fdh;
5946
5947
0
  if ((eh->elf.root.type == bfd_link_hash_defined
5948
0
       || eh->elf.root.type == bfd_link_hash_defweak)
5949
0
      && (!eh->elf.start_stop
5950
0
    || eh->elf.root.ldscript_def
5951
0
    || !info->start_stop_gc)
5952
0
      && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5953
0
    || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5954
0
        && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5955
0
        && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5956
0
        && (!bfd_link_executable (info)
5957
0
      || info->gc_keep_exported
5958
0
      || info->export_dynamic
5959
0
      || (eh->elf.dynamic
5960
0
          && d != NULL
5961
0
          && (*d->match) (&d->head, NULL,
5962
0
              eh->elf.root.root.string)))
5963
0
        && (eh->elf.versioned >= versioned
5964
0
      || !bfd_hide_sym_by_version (info->version_info,
5965
0
                 eh->elf.root.root.string)))))
5966
0
    {
5967
0
      asection *code_sec;
5968
0
      struct ppc_link_hash_entry *fh;
5969
5970
0
      eh->elf.root.u.def.section->flags |= SEC_KEEP;
5971
5972
      /* Function descriptor syms cause the associated
5973
   function code sym section to be marked.  */
5974
0
      fh = defined_code_entry (eh);
5975
0
      if (fh != NULL)
5976
0
  {
5977
0
    code_sec = fh->elf.root.u.def.section;
5978
0
    code_sec->flags |= SEC_KEEP;
5979
0
  }
5980
0
      else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5981
0
         && opd_entry_value (eh->elf.root.u.def.section,
5982
0
           eh->elf.root.u.def.value,
5983
0
           &code_sec, NULL, false) != (bfd_vma) -1)
5984
0
  code_sec->flags |= SEC_KEEP;
5985
0
    }
5986
5987
0
  return true;
5988
0
}
5989
5990
/* Return the section that should be marked against GC for a given
5991
   relocation.  */
5992
5993
static asection *
5994
ppc64_elf_gc_mark_hook (asection *sec,
5995
      struct bfd_link_info *info,
5996
      struct elf_reloc_cookie *cookie,
5997
      struct elf_link_hash_entry *h,
5998
      unsigned int symndx)
5999
0
{
6000
0
  asection *rsec;
6001
6002
  /* Syms return NULL if we're marking .opd, so we avoid marking all
6003
     function sections, as all functions are referenced in .opd.  */
6004
0
  rsec = NULL;
6005
0
  if (get_opd_info (sec) != NULL)
6006
0
    return rsec;
6007
6008
0
  if (h != NULL)
6009
0
    {
6010
0
      enum elf_ppc64_reloc_type r_type;
6011
0
      struct ppc_link_hash_entry *eh, *fh, *fdh;
6012
6013
0
      r_type = ELF64_R_TYPE (cookie->rel->r_info);
6014
0
      switch (r_type)
6015
0
  {
6016
0
  case R_PPC64_GNU_VTINHERIT:
6017
0
  case R_PPC64_GNU_VTENTRY:
6018
0
    break;
6019
6020
0
  default:
6021
0
    switch (h->root.type)
6022
0
      {
6023
0
      case bfd_link_hash_defined:
6024
0
      case bfd_link_hash_defweak:
6025
0
        eh = ppc_elf_hash_entry (h);
6026
0
        fdh = defined_func_desc (eh);
6027
0
        if (fdh != NULL)
6028
0
    {
6029
      /* -mcall-aixdesc code references the dot-symbol on
6030
         a call reloc.  Mark the function descriptor too
6031
         against garbage collection.  */
6032
0
      fdh->elf.mark = 1;
6033
0
      if (fdh->elf.is_weakalias)
6034
0
        weakdef (&fdh->elf)->mark = 1;
6035
0
      eh = fdh;
6036
0
    }
6037
6038
        /* Function descriptor syms cause the associated
6039
     function code sym section to be marked.  */
6040
0
        fh = defined_code_entry (eh);
6041
0
        if (fh != NULL)
6042
0
    {
6043
      /* They also mark their opd section.  */
6044
0
      eh->elf.root.u.def.section->gc_mark = 1;
6045
6046
0
      rsec = fh->elf.root.u.def.section;
6047
0
    }
6048
0
        else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6049
0
           && opd_entry_value (eh->elf.root.u.def.section,
6050
0
             eh->elf.root.u.def.value,
6051
0
             &rsec, NULL, false) != (bfd_vma) -1)
6052
0
    eh->elf.root.u.def.section->gc_mark = 1;
6053
0
        else
6054
0
    rsec = h->root.u.def.section;
6055
0
        break;
6056
6057
0
      case bfd_link_hash_common:
6058
0
        rsec = h->root.u.c.p->section;
6059
0
        break;
6060
6061
0
      default:
6062
0
        return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
6063
0
      }
6064
0
  }
6065
0
    }
6066
0
  else
6067
0
    {
6068
0
      struct _opd_sec_data *opd;
6069
6070
0
      rsec = _bfd_get_local_sym_section (cookie, symndx);
6071
0
      opd = get_opd_info (rsec);
6072
0
      if (opd != NULL && opd->func_sec != NULL)
6073
0
  {
6074
0
    rsec->gc_mark = 1;
6075
6076
0
    struct ppc_link_hash_table *htab = ppc_hash_table (info);
6077
0
    Elf_Internal_Sym *sym
6078
0
      = bfd_sym_from_r_symndx (&htab->elf.sym_cache, cookie->abfd,
6079
0
             symndx);
6080
0
    if (sym)
6081
0
      {
6082
0
        bfd_vma addr = sym->st_value + cookie->rel->r_addend;
6083
0
        rsec = opd->func_sec[OPD_NDX (addr)];
6084
0
      }
6085
0
  }
6086
0
    }
6087
6088
0
  return rsec;
6089
0
}
6090
6091
/* The maximum size of .sfpr.  */
6092
0
#define SFPR_MAX (218*4)
6093
6094
struct sfpr_def_parms
6095
{
6096
  const char name[12];
6097
  unsigned char lo, hi;
6098
  bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6099
  bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
6100
};
6101
6102
/* Auto-generate _save*, _rest* functions in .sfpr.
6103
   If STUB_SEC is non-null, define alias symbols in STUB_SEC
6104
   instead.  */
6105
6106
static bool
6107
sfpr_define (struct bfd_link_info *info,
6108
       const struct sfpr_def_parms *parm,
6109
       asection *stub_sec)
6110
0
{
6111
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
6112
0
  unsigned int i;
6113
0
  size_t len = strlen (parm->name);
6114
0
  bool writing = false;
6115
0
  char sym[16];
6116
6117
0
  if (htab == NULL)
6118
0
    return false;
6119
6120
0
  memcpy (sym, parm->name, len);
6121
0
  sym[len + 2] = 0;
6122
6123
0
  for (i = parm->lo; i <= parm->hi; i++)
6124
0
    {
6125
0
      struct ppc_link_hash_entry *h;
6126
6127
0
      sym[len + 0] = i / 10 + '0';
6128
0
      sym[len + 1] = i % 10 + '0';
6129
0
      h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
6130
0
                writing, true, true));
6131
0
      if (stub_sec != NULL)
6132
0
  {
6133
0
    if (h != NULL
6134
0
        && h->elf.root.type == bfd_link_hash_defined
6135
0
        && h->elf.root.u.def.section == htab->sfpr)
6136
0
      {
6137
0
        struct elf_link_hash_entry *s;
6138
0
        char buf[32];
6139
0
        sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6140
0
        s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
6141
0
        if (s == NULL)
6142
0
    return false;
6143
0
        if (s->root.type == bfd_link_hash_new)
6144
0
    {
6145
0
      s->root.type = bfd_link_hash_defined;
6146
0
      s->root.u.def.section = stub_sec;
6147
0
      s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6148
0
           + h->elf.root.u.def.value);
6149
0
      s->ref_regular = 1;
6150
0
      s->def_regular = 1;
6151
0
      s->ref_regular_nonweak = 1;
6152
0
      s->forced_local = 1;
6153
0
      s->non_elf = 0;
6154
0
      s->root.linker_def = 1;
6155
0
    }
6156
0
      }
6157
0
    continue;
6158
0
  }
6159
0
      if (h != NULL)
6160
0
  {
6161
0
    h->save_res = 1;
6162
0
    if (!h->elf.def_regular)
6163
0
      {
6164
0
        h->elf.root.type = bfd_link_hash_defined;
6165
0
        h->elf.root.u.def.section = htab->sfpr;
6166
0
        h->elf.root.u.def.value = htab->sfpr->size;
6167
0
        h->elf.type = STT_FUNC;
6168
0
        h->elf.def_regular = 1;
6169
0
        h->elf.non_elf = 0;
6170
0
        _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6171
0
        writing = true;
6172
0
        if (htab->sfpr->contents == NULL)
6173
0
    {
6174
0
      htab->sfpr->contents
6175
0
        = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6176
0
      if (htab->sfpr->contents == NULL)
6177
0
        return false;
6178
0
      htab->sfpr->alloced = 1;
6179
0
    }
6180
0
      }
6181
0
  }
6182
0
      if (writing)
6183
0
  {
6184
0
    bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6185
0
    if (i != parm->hi)
6186
0
      p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6187
0
    else
6188
0
      p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6189
0
    htab->sfpr->size = p - htab->sfpr->contents;
6190
0
  }
6191
0
    }
6192
6193
0
  return true;
6194
0
}
6195
6196
static bfd_byte *
6197
savegpr0 (bfd *abfd, bfd_byte *p, int r)
6198
0
{
6199
0
  bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6200
0
  return p + 4;
6201
0
}
6202
6203
static bfd_byte *
6204
savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6205
0
{
6206
0
  p = savegpr0 (abfd, p, r);
6207
0
  bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6208
0
  p = p + 4;
6209
0
  bfd_put_32 (abfd, BLR, p);
6210
0
  return p + 4;
6211
0
}
6212
6213
static bfd_byte *
6214
restgpr0 (bfd *abfd, bfd_byte *p, int r)
6215
0
{
6216
0
  bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6217
0
  return p + 4;
6218
0
}
6219
6220
static bfd_byte *
6221
restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6222
0
{
6223
0
  bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6224
0
  p = p + 4;
6225
0
  p = restgpr0 (abfd, p, r);
6226
0
  bfd_put_32 (abfd, MTLR_R0, p);
6227
0
  p = p + 4;
6228
0
  if (r == 29)
6229
0
    {
6230
0
      p = restgpr0 (abfd, p, 30);
6231
0
      p = restgpr0 (abfd, p, 31);
6232
0
    }
6233
0
  bfd_put_32 (abfd, BLR, p);
6234
0
  return p + 4;
6235
0
}
6236
6237
static bfd_byte *
6238
savegpr1 (bfd *abfd, bfd_byte *p, int r)
6239
0
{
6240
0
  bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6241
0
  return p + 4;
6242
0
}
6243
6244
static bfd_byte *
6245
savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6246
0
{
6247
0
  p = savegpr1 (abfd, p, r);
6248
0
  bfd_put_32 (abfd, BLR, p);
6249
0
  return p + 4;
6250
0
}
6251
6252
static bfd_byte *
6253
restgpr1 (bfd *abfd, bfd_byte *p, int r)
6254
0
{
6255
0
  bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6256
0
  return p + 4;
6257
0
}
6258
6259
static bfd_byte *
6260
restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6261
0
{
6262
0
  p = restgpr1 (abfd, p, r);
6263
0
  bfd_put_32 (abfd, BLR, p);
6264
0
  return p + 4;
6265
0
}
6266
6267
static bfd_byte *
6268
savefpr (bfd *abfd, bfd_byte *p, int r)
6269
0
{
6270
0
  bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6271
0
  return p + 4;
6272
0
}
6273
6274
static bfd_byte *
6275
savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6276
0
{
6277
0
  p = savefpr (abfd, p, r);
6278
0
  bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6279
0
  p = p + 4;
6280
0
  bfd_put_32 (abfd, BLR, p);
6281
0
  return p + 4;
6282
0
}
6283
6284
static bfd_byte *
6285
restfpr (bfd *abfd, bfd_byte *p, int r)
6286
0
{
6287
0
  bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6288
0
  return p + 4;
6289
0
}
6290
6291
static bfd_byte *
6292
restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6293
0
{
6294
0
  bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6295
0
  p = p + 4;
6296
0
  p = restfpr (abfd, p, r);
6297
0
  bfd_put_32 (abfd, MTLR_R0, p);
6298
0
  p = p + 4;
6299
0
  if (r == 29)
6300
0
    {
6301
0
      p = restfpr (abfd, p, 30);
6302
0
      p = restfpr (abfd, p, 31);
6303
0
    }
6304
0
  bfd_put_32 (abfd, BLR, p);
6305
0
  return p + 4;
6306
0
}
6307
6308
static bfd_byte *
6309
savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6310
0
{
6311
0
  p = savefpr (abfd, p, r);
6312
0
  bfd_put_32 (abfd, BLR, p);
6313
0
  return p + 4;
6314
0
}
6315
6316
static bfd_byte *
6317
restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6318
0
{
6319
0
  p = restfpr (abfd, p, r);
6320
0
  bfd_put_32 (abfd, BLR, p);
6321
0
  return p + 4;
6322
0
}
6323
6324
static bfd_byte *
6325
savevr (bfd *abfd, bfd_byte *p, int r)
6326
0
{
6327
0
  bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6328
0
  p = p + 4;
6329
0
  bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6330
0
  return p + 4;
6331
0
}
6332
6333
static bfd_byte *
6334
savevr_tail (bfd *abfd, bfd_byte *p, int r)
6335
0
{
6336
0
  p = savevr (abfd, p, r);
6337
0
  bfd_put_32 (abfd, BLR, p);
6338
0
  return p + 4;
6339
0
}
6340
6341
static bfd_byte *
6342
restvr (bfd *abfd, bfd_byte *p, int r)
6343
0
{
6344
0
  bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6345
0
  p = p + 4;
6346
0
  bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6347
0
  return p + 4;
6348
0
}
6349
6350
static bfd_byte *
6351
restvr_tail (bfd *abfd, bfd_byte *p, int r)
6352
0
{
6353
0
  p = restvr (abfd, p, r);
6354
0
  bfd_put_32 (abfd, BLR, p);
6355
0
  return p + 4;
6356
0
}
6357
6358
#define STDU_R1_0R1 0xf8210001
6359
#define ADDI_R1_R1  0x38210000
6360
6361
/* Emit prologue of wrapper preserving regs around a call to
6362
   __tls_get_addr_opt.  */
6363
6364
static bfd_byte *
6365
tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6366
0
{
6367
0
  unsigned int i;
6368
6369
0
  bfd_put_32 (obfd, MFLR_R0, p);
6370
0
  p += 4;
6371
0
  bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6372
0
  p += 4;
6373
6374
0
  if (htab->opd_abi)
6375
0
    {
6376
0
      for (i = 4; i < 12; i++)
6377
0
  {
6378
0
    bfd_put_32 (obfd,
6379
0
          STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6380
0
    p += 4;
6381
0
  }
6382
0
      bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6383
0
      p += 4;
6384
0
    }
6385
0
  else
6386
0
    {
6387
0
      for (i = 4; i < 12; i++)
6388
0
  {
6389
0
    bfd_put_32 (obfd,
6390
0
          STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6391
0
    p += 4;
6392
0
  }
6393
0
      bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6394
0
      p += 4;
6395
0
    }
6396
0
  return p;
6397
0
}
6398
6399
/* Emit epilogue of wrapper preserving regs around a call to
6400
   __tls_get_addr_opt.  */
6401
6402
static bfd_byte *
6403
tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6404
0
{
6405
0
  unsigned int i;
6406
6407
0
  if (htab->opd_abi)
6408
0
    {
6409
0
      for (i = 4; i < 12; i++)
6410
0
  {
6411
0
    bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6412
0
    p += 4;
6413
0
  }
6414
0
      bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6415
0
      p += 4;
6416
0
    }
6417
0
  else
6418
0
    {
6419
0
      for (i = 4; i < 12; i++)
6420
0
  {
6421
0
    bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6422
0
    p += 4;
6423
0
  }
6424
0
      bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6425
0
      p += 4;
6426
0
    }
6427
0
  bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6428
0
  p += 4;
6429
0
  bfd_put_32 (obfd, MTLR_R0, p);
6430
0
  p += 4;
6431
0
  bfd_put_32 (obfd, BLR, p);
6432
0
  p += 4;
6433
0
  return p;
6434
0
}
6435
6436
/* Called via elf_link_hash_traverse to transfer dynamic linking
6437
   information on function code symbol entries to their corresponding
6438
   function descriptor symbol entries.  Must not be called twice for
6439
   any given code symbol.  */
6440
6441
static bool
6442
func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6443
0
{
6444
0
  struct bfd_link_info *info;
6445
0
  struct ppc_link_hash_table *htab;
6446
0
  struct ppc_link_hash_entry *fh;
6447
0
  struct ppc_link_hash_entry *fdh;
6448
0
  bool force_local;
6449
6450
0
  fh = ppc_elf_hash_entry (h);
6451
0
  if (fh->elf.root.type == bfd_link_hash_indirect)
6452
0
    return true;
6453
6454
0
  if (!fh->is_func)
6455
0
    return true;
6456
6457
0
  if (fh->elf.root.root.string[0] != '.'
6458
0
      || fh->elf.root.root.string[1] == '\0')
6459
0
    return true;
6460
6461
0
  info = inf;
6462
0
  htab = ppc_hash_table (info);
6463
0
  if (htab == NULL)
6464
0
    return false;
6465
6466
  /* Find the corresponding function descriptor symbol.  */
6467
0
  fdh = lookup_fdh (fh, htab);
6468
6469
  /* Resolve undefined references to dot-symbols as the value
6470
     in the function descriptor, if we have one in a regular object.
6471
     This is to satisfy cases like ".quad .foo".  Calls to functions
6472
     in dynamic objects are handled elsewhere.  */
6473
0
  if ((fh->elf.root.type == bfd_link_hash_undefined
6474
0
       || fh->elf.root.type == bfd_link_hash_undefweak)
6475
0
      && (fdh->elf.root.type == bfd_link_hash_defined
6476
0
    || fdh->elf.root.type == bfd_link_hash_defweak)
6477
0
      && get_opd_info (fdh->elf.root.u.def.section) != NULL
6478
0
      && opd_entry_value (fdh->elf.root.u.def.section,
6479
0
        fdh->elf.root.u.def.value,
6480
0
        &fh->elf.root.u.def.section,
6481
0
        &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
6482
0
    {
6483
0
      fh->elf.root.type = fdh->elf.root.type;
6484
0
      fh->elf.forced_local = 1;
6485
0
      fh->elf.def_regular = fdh->elf.def_regular;
6486
0
      fh->elf.def_dynamic = fdh->elf.def_dynamic;
6487
0
    }
6488
6489
0
  if (!fh->elf.dynamic)
6490
0
    {
6491
0
      struct plt_entry *ent;
6492
6493
0
      for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6494
0
  if (ent->plt.refcount > 0)
6495
0
    break;
6496
0
      if (ent == NULL)
6497
0
  {
6498
0
    if (fdh != NULL && fdh->fake)
6499
0
      _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6500
0
    return true;
6501
0
  }
6502
0
    }
6503
6504
  /* Create a descriptor as undefined if necessary.  */
6505
0
  if (fdh == NULL
6506
0
      && !bfd_link_executable (info)
6507
0
      && (fh->elf.root.type == bfd_link_hash_undefined
6508
0
    || fh->elf.root.type == bfd_link_hash_undefweak))
6509
0
    {
6510
0
      fdh = make_fdh (info, fh);
6511
0
      if (fdh == NULL)
6512
0
  return false;
6513
0
    }
6514
6515
  /* We can't support overriding of symbols on a fake descriptor.  */
6516
0
  if (fdh != NULL
6517
0
      && fdh->fake
6518
0
      && (fh->elf.root.type == bfd_link_hash_defined
6519
0
    || fh->elf.root.type == bfd_link_hash_defweak))
6520
0
    _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6521
6522
  /* Transfer dynamic linking information to the function descriptor.  */
6523
0
  if (fdh != NULL)
6524
0
    {
6525
0
      fdh->elf.ref_regular |= fh->elf.ref_regular;
6526
0
      fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6527
0
      fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6528
0
      fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6529
0
      fdh->elf.dynamic |= fh->elf.dynamic;
6530
0
      fdh->elf.needs_plt |= (fh->elf.needs_plt
6531
0
           || fh->elf.type == STT_FUNC
6532
0
           || fh->elf.type == STT_GNU_IFUNC);
6533
0
      move_plt_plist (fh, fdh);
6534
6535
0
      if (!fdh->elf.forced_local
6536
0
    && fh->elf.dynindx != -1)
6537
0
  if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6538
0
    return false;
6539
0
    }
6540
6541
  /* Now that the info is on the function descriptor, clear the
6542
     function code sym info.  Any function code syms for which we
6543
     don't have a definition in a regular file, we force local.
6544
     This prevents a shared library from exporting syms that have
6545
     been imported from another library.  Function code syms that
6546
     are really in the library we must leave global to prevent the
6547
     linker dragging in a definition from a static library.  */
6548
0
  force_local = (!fh->elf.def_regular
6549
0
     || fdh == NULL
6550
0
     || !fdh->elf.def_regular
6551
0
     || fdh->elf.forced_local);
6552
0
  _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6553
6554
0
  return true;
6555
0
}
6556
6557
static const struct sfpr_def_parms save_res_funcs[] =
6558
  {
6559
    { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6560
    { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6561
    { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6562
    { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6563
    { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6564
    { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6565
    { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6566
    { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6567
    { "._savef", 14, 31, savefpr, savefpr1_tail },
6568
    { "._restf", 14, 31, restfpr, restfpr1_tail },
6569
    { "_savevr_", 20, 31, savevr, savevr_tail },
6570
    { "_restvr_", 20, 31, restvr, restvr_tail }
6571
  };
6572
6573
/* Called near the start of bfd_elf_size_dynamic_sections.  We use
6574
   this hook to a) run the edit functions in this file, b) provide
6575
   some gcc support functions, and c) transfer dynamic linking
6576
   information gathered so far on function code symbol entries, to
6577
   their corresponding function descriptor symbol entries.  */
6578
6579
static bool
6580
ppc64_elf_edit (struct bfd_link_info *info)
6581
0
{
6582
0
  struct ppc_link_hash_table *htab;
6583
6584
0
  htab = ppc_hash_table (info);
6585
0
  if (htab == NULL)
6586
0
    return false;
6587
6588
  /* Call back into the linker, which then runs the edit functions.  */
6589
0
  htab->params->edit ();
6590
6591
  /* Provide any missing _save* and _rest* functions.  */
6592
0
  if (htab->sfpr != NULL)
6593
0
    {
6594
0
      unsigned int i;
6595
6596
0
      htab->sfpr->size = 0;
6597
0
      for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6598
0
  if (!sfpr_define (info, &save_res_funcs[i], NULL))
6599
0
    return false;
6600
0
      if (htab->sfpr->size == 0)
6601
0
  htab->sfpr->flags |= SEC_EXCLUDE;
6602
0
    }
6603
6604
0
  if (bfd_link_relocatable (info))
6605
0
    return true;
6606
6607
0
  if (htab->elf.hgot != NULL)
6608
0
    {
6609
0
      _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
6610
      /* Make .TOC. defined so as to prevent it being made dynamic.
6611
   The wrong value here is fixed later in ppc64_elf_set_toc.  */
6612
0
      if (!htab->elf.hgot->def_regular
6613
0
    || htab->elf.hgot->root.type != bfd_link_hash_defined)
6614
0
  {
6615
0
    htab->elf.hgot->root.type = bfd_link_hash_defined;
6616
0
    htab->elf.hgot->root.u.def.value = 0;
6617
0
    htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6618
0
    htab->elf.hgot->def_regular = 1;
6619
0
    htab->elf.hgot->root.linker_def = 1;
6620
0
  }
6621
0
      htab->elf.hgot->type = STT_OBJECT;
6622
0
      htab->elf.hgot->other
6623
0
  = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6624
0
    }
6625
6626
0
  return true;
6627
0
}
6628
6629
/* Return true if we have dynamic relocs against H or any of its weak
6630
   aliases, that apply to read-only sections.  Cannot be used after
6631
   size_dynamic_sections.  */
6632
6633
static bool
6634
alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6635
0
{
6636
0
  struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6637
0
  do
6638
0
    {
6639
0
      if (_bfd_elf_readonly_dynrelocs (&eh->elf))
6640
0
  return true;
6641
0
      eh = ppc_elf_hash_entry (eh->elf.u.alias);
6642
0
    }
6643
0
  while (eh != NULL && &eh->elf != h);
6644
6645
0
  return false;
6646
0
}
6647
6648
/* Return whether EH has pc-relative dynamic relocs.  */
6649
6650
static bool
6651
pc_dynrelocs (struct ppc_link_hash_entry *eh)
6652
0
{
6653
0
  struct ppc_dyn_relocs *p;
6654
6655
0
  for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
6656
0
    if (p->pc_count != 0)
6657
0
      return true;
6658
0
  return false;
6659
0
}
6660
6661
/* Return true if a global entry stub will be created for H.  Valid
6662
   for ELFv2 before plt entries have been allocated.  */
6663
6664
static bool
6665
global_entry_stub (struct elf_link_hash_entry *h)
6666
0
{
6667
0
  struct plt_entry *pent;
6668
6669
0
  if (!h->pointer_equality_needed
6670
0
      || h->def_regular)
6671
0
    return false;
6672
6673
0
  for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6674
0
    if (pent->plt.refcount > 0
6675
0
  && pent->addend == 0)
6676
0
      return true;
6677
6678
0
  return false;
6679
0
}
6680
6681
/* Adjust a symbol defined by a dynamic object and referenced by a
6682
   regular object.  The current definition is in some section of the
6683
   dynamic object, but we're not including those sections.  We have to
6684
   change the definition to something the rest of the link can
6685
   understand.  */
6686
6687
static bool
6688
ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6689
         struct elf_link_hash_entry *h)
6690
0
{
6691
0
  struct ppc_link_hash_table *htab;
6692
0
  asection *s, *srel;
6693
6694
0
  htab = ppc_hash_table (info);
6695
0
  if (htab == NULL)
6696
0
    return false;
6697
6698
  /* Deal with function syms.  */
6699
0
  if (h->type == STT_FUNC
6700
0
      || h->type == STT_GNU_IFUNC
6701
0
      || h->needs_plt)
6702
0
    {
6703
0
      bool local = (ppc_elf_hash_entry (h)->save_res
6704
0
        || SYMBOL_CALLS_LOCAL (info, h)
6705
0
        || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6706
      /* Discard dyn_relocs when non-pic if we've decided that a
6707
   function symbol is local and not an ifunc.  We keep dynamic
6708
   relocs for ifuncs when local rather than always emitting a
6709
   plt call stub for them and defining the symbol on the call
6710
   stub.  We can't do that for ELFv1 anyway (a function symbol
6711
   is defined on a descriptor, not code) and it can be faster at
6712
   run-time due to not needing to bounce through a stub.  The
6713
   dyn_relocs for ifuncs will be applied even in a static
6714
   executable.  */
6715
0
      if (!bfd_link_pic (info)
6716
0
    && h->type != STT_GNU_IFUNC
6717
0
    && local)
6718
0
  h->dyn_relocs = NULL;
6719
6720
      /* Clear procedure linkage table information for any symbol that
6721
   won't need a .plt entry.  */
6722
0
      struct plt_entry *ent;
6723
0
      for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6724
0
  if (ent->plt.refcount > 0)
6725
0
    break;
6726
0
      if (ent == NULL
6727
0
    || (h->type != STT_GNU_IFUNC
6728
0
        && local
6729
0
        && (htab->can_convert_all_inline_plt
6730
0
      || (ppc_elf_hash_entry (h)->tls_mask
6731
0
          & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6732
0
  {
6733
0
    h->plt.plist = NULL;
6734
0
    h->needs_plt = 0;
6735
0
    h->pointer_equality_needed = 0;
6736
0
  }
6737
0
      else if (abiversion (info->output_bfd) >= 2)
6738
0
  {
6739
    /* Taking a function's address in a read/write section
6740
       doesn't require us to define the function symbol in the
6741
       executable on a global entry stub.  A dynamic reloc can
6742
       be used instead.  The reason we prefer a few more dynamic
6743
       relocs is that calling via a global entry stub costs a
6744
       few more instructions, and pointer_equality_needed causes
6745
       extra work in ld.so when resolving these symbols.  */
6746
0
    if (global_entry_stub (h))
6747
0
      {
6748
0
        if (!_bfd_elf_readonly_dynrelocs (h))
6749
0
    {
6750
0
      h->pointer_equality_needed = 0;
6751
      /* If we haven't seen a branch reloc and the symbol
6752
         isn't an ifunc then we don't need a plt entry.  */
6753
0
      if (!h->needs_plt)
6754
0
        h->plt.plist = NULL;
6755
0
    }
6756
0
        else if (!bfd_link_pic (info))
6757
    /* We are going to be defining the function symbol on the
6758
       plt stub, so no dyn_relocs needed when non-pic.  */
6759
0
    h->dyn_relocs = NULL;
6760
0
      }
6761
6762
    /* ELFv2 function symbols can't have copy relocs.  */
6763
0
    return true;
6764
0
  }
6765
0
      else if (!h->needs_plt
6766
0
         && !_bfd_elf_readonly_dynrelocs (h))
6767
0
  {
6768
    /* If we haven't seen a branch reloc and the symbol isn't an
6769
       ifunc then we don't need a plt entry.  */
6770
0
    h->plt.plist = NULL;
6771
0
    h->pointer_equality_needed = 0;
6772
0
    return true;
6773
0
  }
6774
0
    }
6775
0
  else
6776
0
    h->plt.plist = NULL;
6777
6778
  /* If this is a weak symbol, and there is a real definition, the
6779
     processor independent code will have arranged for us to see the
6780
     real definition first, and we can just use the same value.  */
6781
0
  if (h->is_weakalias)
6782
0
    {
6783
0
      struct elf_link_hash_entry *def = weakdef (h);
6784
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6785
0
      h->root.u.def.section = def->root.u.def.section;
6786
0
      h->root.u.def.value = def->root.u.def.value;
6787
0
      if (def->root.u.def.section == htab->elf.sdynbss
6788
0
    || def->root.u.def.section == htab->elf.sdynrelro)
6789
0
  h->dyn_relocs = NULL;
6790
0
      return true;
6791
0
    }
6792
6793
  /* If we are creating a shared library, we must presume that the
6794
     only references to the symbol are via the global offset table.
6795
     For such cases we need not do anything here; the relocations will
6796
     be handled correctly by relocate_section.  */
6797
0
  if (!bfd_link_executable (info))
6798
0
    return true;
6799
6800
  /* If there are no references to this symbol that do not use the
6801
     GOT, we don't need to generate a copy reloc.  */
6802
0
  if (!h->non_got_ref)
6803
0
    return true;
6804
6805
  /* Don't generate a copy reloc for symbols defined in the executable.  */
6806
0
  if (!h->def_dynamic || !h->ref_regular || h->def_regular
6807
6808
      /* If -z nocopyreloc was given, don't generate them either.  */
6809
0
      || info->nocopyreloc
6810
6811
      /* If we don't find any dynamic relocs in read-only sections, then
6812
   we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
6813
0
      || (ELIMINATE_COPY_RELOCS
6814
0
    && !h->needs_copy
6815
0
    && !alias_readonly_dynrelocs (h))
6816
6817
      /* Protected variables do not work with .dynbss.  The copy in
6818
   .dynbss won't be used by the shared library with the protected
6819
   definition for the variable.  Text relocations are preferable
6820
   to an incorrect program.  */
6821
0
      || h->protected_def)
6822
0
    return true;
6823
6824
0
  if (h->type == STT_FUNC
6825
0
      || h->type == STT_GNU_IFUNC)
6826
0
    {
6827
      /* .dynbss copies of function symbols only work if we have
6828
   ELFv1 dot-symbols.  ELFv1 compilers since 2004 default to not
6829
   use dot-symbols and set the function symbol size to the text
6830
   size of the function rather than the size of the descriptor.
6831
   That's wrong for copying a descriptor.  */
6832
0
      if (ppc_elf_hash_entry (h)->oh == NULL
6833
0
    || !(h->size == 24 || h->size == 16))
6834
0
  return true;
6835
6836
      /* We should never get here, but unfortunately there are old
6837
   versions of gcc (circa gcc-3.2) that improperly for the
6838
   ELFv1 ABI put initialized function pointers, vtable refs and
6839
   suchlike in read-only sections.  Allow them to proceed, but
6840
   warn that this might break at runtime.  */
6841
0
      info->callbacks->einfo
6842
0
  (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6843
0
     "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6844
0
   h->root.root.string);
6845
0
    }
6846
6847
  /* This is a reference to a symbol defined by a dynamic object which
6848
     is not a function.  */
6849
6850
  /* We must allocate the symbol in our .dynbss section, which will
6851
     become part of the .bss section of the executable.  There will be
6852
     an entry for this symbol in the .dynsym section.  The dynamic
6853
     object will contain position independent code, so all references
6854
     from the dynamic object to this symbol will go through the global
6855
     offset table.  The dynamic linker will use the .dynsym entry to
6856
     determine the address it must put in the global offset table, so
6857
     both the dynamic object and the regular object will refer to the
6858
     same memory location for the variable.  */
6859
0
  if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6860
0
    {
6861
0
      s = htab->elf.sdynrelro;
6862
0
      srel = htab->elf.sreldynrelro;
6863
0
    }
6864
0
  else
6865
0
    {
6866
0
      s = htab->elf.sdynbss;
6867
0
      srel = htab->elf.srelbss;
6868
0
    }
6869
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6870
0
    {
6871
      /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6872
   linker to copy the initial value out of the dynamic object
6873
   and into the runtime process image.  */
6874
0
      srel->size += sizeof (Elf64_External_Rela);
6875
0
      h->needs_copy = 1;
6876
0
    }
6877
6878
  /* We no longer want dyn_relocs.  */
6879
0
  h->dyn_relocs = NULL;
6880
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
6881
0
}
6882
6883
/* If given a function descriptor symbol, hide both the function code
6884
   sym and the descriptor.  */
6885
static void
6886
ppc64_elf_hide_symbol (struct bfd_link_info *info,
6887
           struct elf_link_hash_entry *h,
6888
           bool force_local)
6889
0
{
6890
0
  struct ppc_link_hash_entry *eh;
6891
0
  _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6892
6893
0
  if (ppc_hash_table (info) == NULL)
6894
0
    return;
6895
6896
0
  eh = ppc_elf_hash_entry (h);
6897
0
  if (eh->is_func_descriptor)
6898
0
    {
6899
0
      struct ppc_link_hash_entry *fh = eh->oh;
6900
6901
0
      if (fh == NULL)
6902
0
  {
6903
0
    const char *p, *q;
6904
0
    struct elf_link_hash_table *htab = elf_hash_table (info);
6905
0
    char save;
6906
6907
    /* We aren't supposed to use alloca in BFD because on
6908
       systems which do not have alloca the version in libiberty
6909
       calls xmalloc, which might cause the program to crash
6910
       when it runs out of memory.  This function doesn't have a
6911
       return status, so there's no way to gracefully return an
6912
       error.  So cheat.  We know that string[-1] can be safely
6913
       accessed;  It's either a string in an ELF string table,
6914
       or allocated in an objalloc structure.  */
6915
6916
0
    p = eh->elf.root.root.string - 1;
6917
0
    save = *p;
6918
0
    *(char *) p = '.';
6919
0
    fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6920
0
               false, false));
6921
0
    *(char *) p = save;
6922
6923
    /* Unfortunately, if it so happens that the string we were
6924
       looking for was allocated immediately before this string,
6925
       then we overwrote the string terminator.  That's the only
6926
       reason the lookup should fail.  */
6927
0
    if (fh == NULL)
6928
0
      {
6929
0
        q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6930
0
        while (q >= eh->elf.root.root.string && *q == *p)
6931
0
    --q, --p;
6932
0
        if (q < eh->elf.root.root.string && *p == '.')
6933
0
    fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6934
0
                     false, false));
6935
0
      }
6936
0
    if (fh != NULL)
6937
0
      {
6938
0
        eh->oh = fh;
6939
0
        fh->oh = eh;
6940
0
      }
6941
0
  }
6942
0
      if (fh != NULL)
6943
0
  _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6944
0
    }
6945
0
}
6946
6947
static bool
6948
get_sym_h (struct elf_link_hash_entry **hp,
6949
     Elf_Internal_Sym **symp,
6950
     asection **symsecp,
6951
     unsigned char **tls_maskp,
6952
     Elf_Internal_Sym **locsymsp,
6953
     unsigned long r_symndx,
6954
     bfd *ibfd)
6955
0
{
6956
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6957
6958
0
  if (r_symndx >= symtab_hdr->sh_info)
6959
0
    {
6960
0
      struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6961
0
      struct elf_link_hash_entry *h;
6962
6963
0
      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6964
0
      h = elf_follow_link (h);
6965
6966
0
      if (hp != NULL)
6967
0
  *hp = h;
6968
6969
0
      if (symp != NULL)
6970
0
  *symp = NULL;
6971
6972
0
      if (symsecp != NULL)
6973
0
  {
6974
0
    asection *symsec = NULL;
6975
0
    if (h->root.type == bfd_link_hash_defined
6976
0
        || h->root.type == bfd_link_hash_defweak)
6977
0
      symsec = h->root.u.def.section;
6978
0
    *symsecp = symsec;
6979
0
  }
6980
6981
0
      if (tls_maskp != NULL)
6982
0
  *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
6983
0
    }
6984
0
  else
6985
0
    {
6986
0
      Elf_Internal_Sym *sym;
6987
0
      Elf_Internal_Sym *locsyms = *locsymsp;
6988
6989
0
      if (locsyms == NULL)
6990
0
  {
6991
0
    locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6992
0
    if (locsyms == NULL)
6993
0
      locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6994
0
              symtab_hdr->sh_info,
6995
0
              0, NULL, NULL, NULL);
6996
0
    if (locsyms == NULL)
6997
0
      return false;
6998
0
    *locsymsp = locsyms;
6999
0
  }
7000
0
      sym = locsyms + r_symndx;
7001
7002
0
      if (hp != NULL)
7003
0
  *hp = NULL;
7004
7005
0
      if (symp != NULL)
7006
0
  *symp = sym;
7007
7008
0
      if (symsecp != NULL)
7009
0
  *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7010
7011
0
      if (tls_maskp != NULL)
7012
0
  {
7013
0
    struct got_entry **lgot_ents;
7014
0
    unsigned char *tls_mask;
7015
7016
0
    tls_mask = NULL;
7017
0
    lgot_ents = elf_local_got_ents (ibfd);
7018
0
    if (lgot_ents != NULL)
7019
0
      {
7020
0
        struct plt_entry **local_plt = (struct plt_entry **)
7021
0
    (lgot_ents + symtab_hdr->sh_info);
7022
0
        unsigned char *lgot_masks = (unsigned char *)
7023
0
    (local_plt + symtab_hdr->sh_info);
7024
0
        tls_mask = &lgot_masks[r_symndx];
7025
0
      }
7026
0
    *tls_maskp = tls_mask;
7027
0
  }
7028
0
    }
7029
0
  return true;
7030
0
}
7031
7032
/* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
7033
   error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7034
   type suitable for optimization, and 1 otherwise.  */
7035
7036
static int
7037
get_tls_mask (unsigned char **tls_maskp,
7038
        unsigned long *toc_symndx,
7039
        bfd_vma *toc_addend,
7040
        Elf_Internal_Sym **locsymsp,
7041
        const Elf_Internal_Rela *rel,
7042
        bfd *ibfd)
7043
0
{
7044
0
  unsigned long r_symndx;
7045
0
  int next_r;
7046
0
  struct elf_link_hash_entry *h;
7047
0
  Elf_Internal_Sym *sym;
7048
0
  asection *sec;
7049
0
  bfd_vma off;
7050
7051
0
  r_symndx = ELF64_R_SYM (rel->r_info);
7052
0
  if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7053
0
    return 0;
7054
7055
0
  if ((*tls_maskp != NULL
7056
0
       && (**tls_maskp & TLS_TLS) != 0
7057
0
       && **tls_maskp != (TLS_TLS | TLS_MARK))
7058
0
      || sec == NULL
7059
0
      || ppc64_elf_section_data (sec) == NULL
7060
0
      || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7061
0
    return 1;
7062
7063
  /* Look inside a TOC section too.  */
7064
0
  if (h != NULL)
7065
0
    {
7066
0
      BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7067
0
      off = h->root.u.def.value;
7068
0
    }
7069
0
  else
7070
0
    off = sym->st_value;
7071
0
  off += rel->r_addend;
7072
0
  BFD_ASSERT (off % 8 == 0);
7073
0
  r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7074
0
  next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7075
0
  if (toc_symndx != NULL)
7076
0
    *toc_symndx = r_symndx;
7077
0
  if (toc_addend != NULL)
7078
0
    *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7079
0
  if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7080
0
    return 0;
7081
0
  if ((h == NULL || is_static_defined (h))
7082
0
      && (next_r == -1 || next_r == -2))
7083
0
    return 1 - next_r;
7084
0
  return 1;
7085
0
}
7086
7087
/* Find (or create) an entry in the tocsave hash table.  */
7088
7089
static struct tocsave_entry *
7090
tocsave_find (struct ppc_link_hash_table *htab,
7091
        enum insert_option insert,
7092
        Elf_Internal_Sym **local_syms,
7093
        const Elf_Internal_Rela *irela,
7094
        bfd *ibfd)
7095
0
{
7096
0
  unsigned long r_indx;
7097
0
  struct elf_link_hash_entry *h;
7098
0
  Elf_Internal_Sym *sym;
7099
0
  struct tocsave_entry ent, *p;
7100
0
  hashval_t hash;
7101
0
  struct tocsave_entry **slot;
7102
7103
0
  r_indx = ELF64_R_SYM (irela->r_info);
7104
0
  if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7105
0
    return NULL;
7106
0
  if (ent.sec == NULL || ent.sec->output_section == NULL)
7107
0
    {
7108
0
      _bfd_error_handler
7109
0
  (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7110
0
      return NULL;
7111
0
    }
7112
7113
0
  if (h != NULL)
7114
0
    ent.offset = h->root.u.def.value;
7115
0
  else
7116
0
    ent.offset = sym->st_value;
7117
0
  ent.offset += irela->r_addend;
7118
7119
0
  hash = tocsave_htab_hash (&ent);
7120
0
  slot = ((struct tocsave_entry **)
7121
0
    htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7122
0
  if (slot == NULL)
7123
0
    return NULL;
7124
7125
0
  if (*slot == NULL)
7126
0
    {
7127
0
      p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7128
0
      if (p == NULL)
7129
0
  return NULL;
7130
0
      *p = ent;
7131
0
      *slot = p;
7132
0
    }
7133
0
  return *slot;
7134
0
}
7135
7136
/* Adjust all global syms defined in opd sections.  In gcc generated
7137
   code for the old ABI, these will already have been done.  */
7138
7139
static bool
7140
adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7141
0
{
7142
0
  struct ppc_link_hash_entry *eh;
7143
0
  asection *sym_sec;
7144
0
  struct _opd_sec_data *opd;
7145
7146
0
  if (h->root.type == bfd_link_hash_indirect)
7147
0
    return true;
7148
7149
0
  if (h->root.type != bfd_link_hash_defined
7150
0
      && h->root.type != bfd_link_hash_defweak)
7151
0
    return true;
7152
7153
0
  eh = ppc_elf_hash_entry (h);
7154
0
  if (eh->adjust_done)
7155
0
    return true;
7156
7157
0
  sym_sec = eh->elf.root.u.def.section;
7158
0
  opd = get_opd_info (sym_sec);
7159
0
  if (opd != NULL && opd->adjust != NULL)
7160
0
    {
7161
0
      long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7162
0
      if (adjust == -1)
7163
0
  {
7164
    /* This entry has been deleted.  */
7165
0
    asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7166
0
    if (dsec == NULL)
7167
0
      {
7168
0
        for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7169
0
    if (discarded_section (dsec))
7170
0
      {
7171
0
        ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7172
0
        break;
7173
0
      }
7174
0
      }
7175
0
    eh->elf.root.u.def.value = 0;
7176
0
    eh->elf.root.u.def.section = dsec;
7177
0
  }
7178
0
      else
7179
0
  eh->elf.root.u.def.value += adjust;
7180
0
      eh->adjust_done = 1;
7181
0
    }
7182
0
  return true;
7183
0
}
7184
7185
/* Handles decrementing dynamic reloc counts for the reloc specified by
7186
   R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7187
   have already been determined.  */
7188
7189
static bool
7190
dec_dynrel_count (const Elf_Internal_Rela *rel,
7191
      asection *sec,
7192
      struct bfd_link_info *info,
7193
      Elf_Internal_Sym **local_syms,
7194
      struct elf_link_hash_entry *h,
7195
      Elf_Internal_Sym *sym)
7196
0
{
7197
0
  enum elf_ppc64_reloc_type r_type;
7198
0
  asection *sym_sec = NULL;
7199
7200
  /* Can this reloc be dynamic?  This switch, and later tests here
7201
     should be kept in sync with the code in check_relocs.  */
7202
0
  r_type = ELF64_R_TYPE (rel->r_info);
7203
0
  switch (r_type)
7204
0
    {
7205
0
    default:
7206
0
      return true;
7207
7208
0
    case R_PPC64_TOC16:
7209
0
    case R_PPC64_TOC16_DS:
7210
0
    case R_PPC64_TOC16_LO:
7211
0
    case R_PPC64_TOC16_HI:
7212
0
    case R_PPC64_TOC16_HA:
7213
0
    case R_PPC64_TOC16_LO_DS:
7214
0
      if (h == NULL)
7215
0
  return true;
7216
0
      break;
7217
7218
0
    case R_PPC64_TPREL16:
7219
0
    case R_PPC64_TPREL16_LO:
7220
0
    case R_PPC64_TPREL16_HI:
7221
0
    case R_PPC64_TPREL16_HA:
7222
0
    case R_PPC64_TPREL16_DS:
7223
0
    case R_PPC64_TPREL16_LO_DS:
7224
0
    case R_PPC64_TPREL16_HIGH:
7225
0
    case R_PPC64_TPREL16_HIGHA:
7226
0
    case R_PPC64_TPREL16_HIGHER:
7227
0
    case R_PPC64_TPREL16_HIGHERA:
7228
0
    case R_PPC64_TPREL16_HIGHEST:
7229
0
    case R_PPC64_TPREL16_HIGHESTA:
7230
0
    case R_PPC64_TPREL64:
7231
0
    case R_PPC64_TPREL34:
7232
0
    case R_PPC64_DTPMOD64:
7233
0
    case R_PPC64_DTPREL64:
7234
0
    case R_PPC64_ADDR64:
7235
0
    case R_PPC64_REL30:
7236
0
    case R_PPC64_REL32:
7237
0
    case R_PPC64_REL64:
7238
0
    case R_PPC64_ADDR14:
7239
0
    case R_PPC64_ADDR14_BRNTAKEN:
7240
0
    case R_PPC64_ADDR14_BRTAKEN:
7241
0
    case R_PPC64_ADDR16:
7242
0
    case R_PPC64_ADDR16_DS:
7243
0
    case R_PPC64_ADDR16_HA:
7244
0
    case R_PPC64_ADDR16_HI:
7245
0
    case R_PPC64_ADDR16_HIGH:
7246
0
    case R_PPC64_ADDR16_HIGHA:
7247
0
    case R_PPC64_ADDR16_HIGHER:
7248
0
    case R_PPC64_ADDR16_HIGHERA:
7249
0
    case R_PPC64_ADDR16_HIGHEST:
7250
0
    case R_PPC64_ADDR16_HIGHESTA:
7251
0
    case R_PPC64_ADDR16_LO:
7252
0
    case R_PPC64_ADDR16_LO_DS:
7253
0
    case R_PPC64_ADDR24:
7254
0
    case R_PPC64_ADDR32:
7255
0
    case R_PPC64_UADDR16:
7256
0
    case R_PPC64_UADDR32:
7257
0
    case R_PPC64_UADDR64:
7258
0
    case R_PPC64_TOC:
7259
0
    case R_PPC64_D34:
7260
0
    case R_PPC64_D34_LO:
7261
0
    case R_PPC64_D34_HI30:
7262
0
    case R_PPC64_D34_HA30:
7263
0
    case R_PPC64_ADDR16_HIGHER34:
7264
0
    case R_PPC64_ADDR16_HIGHERA34:
7265
0
    case R_PPC64_ADDR16_HIGHEST34:
7266
0
    case R_PPC64_ADDR16_HIGHESTA34:
7267
0
    case R_PPC64_D28:
7268
0
      break;
7269
0
    }
7270
7271
0
  if (local_syms != NULL)
7272
0
    {
7273
0
      unsigned long r_symndx;
7274
0
      bfd *ibfd = sec->owner;
7275
7276
0
      r_symndx = ELF64_R_SYM (rel->r_info);
7277
0
      if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7278
0
  return false;
7279
0
    }
7280
7281
0
  if ((h != NULL
7282
0
       && !SYMBOL_REFERENCES_LOCAL (info, h))
7283
0
      || (bfd_link_pic (info)
7284
0
    && (h != NULL
7285
0
        ? !bfd_is_abs_symbol (&h->root)
7286
0
        : sym_sec != bfd_abs_section_ptr)
7287
0
    && must_be_dyn_reloc (info, r_type))
7288
0
      || (!bfd_link_pic (info)
7289
0
    && (h != NULL
7290
0
        ? h->type == STT_GNU_IFUNC
7291
0
        : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
7292
0
    ;
7293
0
  else
7294
0
    return true;
7295
7296
0
  if (h != NULL)
7297
0
    {
7298
0
      struct ppc_dyn_relocs *p;
7299
0
      struct ppc_dyn_relocs **pp;
7300
0
      pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
7301
7302
      /* elf_gc_sweep may have already removed all dyn relocs associated
7303
   with local syms for a given section.  Also, symbol flags are
7304
   changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7305
   report a dynreloc miscount.  */
7306
0
      if (*pp == NULL && info->gc_sections)
7307
0
  return true;
7308
7309
0
      while ((p = *pp) != NULL)
7310
0
  {
7311
0
    if (p->sec == sec)
7312
0
      {
7313
0
        if (!must_be_dyn_reloc (info, r_type))
7314
0
    p->pc_count -= 1;
7315
0
        if (maybe_relr (r_type, rel, sec))
7316
0
    p->rel_count -= 1;
7317
0
        p->count -= 1;
7318
0
        if (p->count == 0)
7319
0
    *pp = p->next;
7320
0
        return true;
7321
0
      }
7322
0
    pp = &p->next;
7323
0
  }
7324
0
    }
7325
0
  else
7326
0
    {
7327
0
      struct ppc_local_dyn_relocs *p;
7328
0
      struct ppc_local_dyn_relocs **pp;
7329
0
      void *vpp;
7330
0
      bool is_ifunc;
7331
7332
0
      if (local_syms == NULL)
7333
0
  sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7334
0
      if (sym_sec == NULL)
7335
0
  sym_sec = sec;
7336
7337
0
      vpp = &elf_section_data (sym_sec)->local_dynrel;
7338
0
      pp = (struct ppc_local_dyn_relocs **) vpp;
7339
7340
0
      if (*pp == NULL && info->gc_sections)
7341
0
  return true;
7342
7343
0
      is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7344
0
      while ((p = *pp) != NULL)
7345
0
  {
7346
0
    if (p->sec == sec && p->ifunc == is_ifunc)
7347
0
      {
7348
0
        if (maybe_relr (r_type, rel, sec))
7349
0
    p->rel_count -= 1;
7350
0
        p->count -= 1;
7351
0
        if (p->count == 0)
7352
0
    *pp = p->next;
7353
0
        return true;
7354
0
      }
7355
0
    pp = &p->next;
7356
0
  }
7357
0
    }
7358
7359
  /* xgettext:c-format */
7360
0
  _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7361
0
          sec->owner, sec);
7362
0
  bfd_set_error (bfd_error_bad_value);
7363
0
  return false;
7364
0
}
7365
7366
/* Remove unused Official Procedure Descriptor entries.  Currently we
7367
   only remove those associated with functions in discarded link-once
7368
   sections, or weakly defined functions that have been overridden.  It
7369
   would be possible to remove many more entries for statically linked
7370
   applications.  */
7371
7372
bool
7373
ppc64_elf_edit_opd (struct bfd_link_info *info)
7374
0
{
7375
0
  bfd *ibfd;
7376
0
  bool some_edited = false;
7377
0
  asection *need_pad = NULL;
7378
0
  struct ppc_link_hash_table *htab;
7379
7380
0
  htab = ppc_hash_table (info);
7381
0
  if (htab == NULL)
7382
0
    return false;
7383
7384
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7385
0
    {
7386
0
      asection *sec;
7387
0
      Elf_Internal_Rela *relstart, *rel, *relend;
7388
0
      Elf_Internal_Shdr *symtab_hdr;
7389
0
      Elf_Internal_Sym *local_syms;
7390
0
      struct _opd_sec_data *opd;
7391
0
      bool need_edit, add_aux_fields, broken;
7392
0
      bfd_size_type cnt_16b = 0;
7393
7394
0
      if (!is_ppc64_elf (ibfd))
7395
0
  continue;
7396
7397
0
      sec = bfd_get_section_by_name (ibfd, ".opd");
7398
0
      if (sec == NULL
7399
0
    || sec->size == 0
7400
0
    || (sec->flags & SEC_HAS_CONTENTS) == 0)
7401
0
  continue;
7402
7403
0
      if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7404
0
  continue;
7405
7406
0
      if (sec->output_section == bfd_abs_section_ptr)
7407
0
  continue;
7408
7409
      /* Look through the section relocs.  */
7410
0
      if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7411
0
  continue;
7412
7413
0
      local_syms = NULL;
7414
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
7415
7416
      /* Read the relocations.  */
7417
0
      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7418
0
              info->keep_memory);
7419
0
      if (relstart == NULL)
7420
0
  return false;
7421
7422
      /* First run through the relocs to check they are sane, and to
7423
   determine whether we need to edit this opd section.  */
7424
0
      need_edit = false;
7425
0
      broken = false;
7426
0
      need_pad = sec;
7427
0
      relend = relstart + sec->reloc_count;
7428
0
      for (rel = relstart; rel < relend; )
7429
0
  {
7430
0
    enum elf_ppc64_reloc_type r_type;
7431
0
    unsigned long r_symndx;
7432
0
    asection *sym_sec;
7433
0
    struct elf_link_hash_entry *h;
7434
0
    Elf_Internal_Sym *sym;
7435
0
    bfd_vma offset;
7436
7437
    /* .opd contains an array of 16 or 24 byte entries.  We're
7438
       only interested in the reloc pointing to a function entry
7439
       point.  */
7440
0
    offset = rel->r_offset;
7441
0
    if (rel + 1 == relend
7442
0
        || rel[1].r_offset != offset + 8)
7443
0
      {
7444
        /* If someone messes with .opd alignment then after a
7445
     "ld -r" we might have padding in the middle of .opd.
7446
     Also, there's nothing to prevent someone putting
7447
     something silly in .opd with the assembler.  No .opd
7448
     optimization for them!  */
7449
0
      broken_opd:
7450
0
        _bfd_error_handler
7451
0
    (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7452
0
        broken = true;
7453
0
        break;
7454
0
      }
7455
7456
0
    if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7457
0
        || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7458
0
      {
7459
0
        _bfd_error_handler
7460
    /* xgettext:c-format */
7461
0
    (_("%pB: unexpected reloc type %u in .opd section"),
7462
0
     ibfd, r_type);
7463
0
        broken = true;
7464
0
        break;
7465
0
      }
7466
7467
0
    r_symndx = ELF64_R_SYM (rel->r_info);
7468
0
    if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7469
0
        r_symndx, ibfd))
7470
0
      goto error_ret;
7471
7472
0
    if (sym_sec == NULL || sym_sec->owner == NULL)
7473
0
      {
7474
0
        const char *sym_name;
7475
0
        if (h != NULL)
7476
0
    sym_name = h->root.root.string;
7477
0
        else
7478
0
    sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7479
0
               sym_sec);
7480
7481
0
        _bfd_error_handler
7482
    /* xgettext:c-format */
7483
0
    (_("%pB: undefined sym `%s' in .opd section"),
7484
0
     ibfd, sym_name);
7485
0
        broken = true;
7486
0
        break;
7487
0
      }
7488
7489
    /* opd entries are always for functions defined in the
7490
       current input bfd.  If the symbol isn't defined in the
7491
       input bfd, then we won't be using the function in this
7492
       bfd;  It must be defined in a linkonce section in another
7493
       bfd, or is weak.  It's also possible that we are
7494
       discarding the function due to a linker script /DISCARD/,
7495
       which we test for via the output_section.  */
7496
0
    if (sym_sec->owner != ibfd
7497
0
        || sym_sec->output_section == bfd_abs_section_ptr)
7498
0
      need_edit = true;
7499
7500
0
    rel += 2;
7501
0
    if (rel + 1 == relend
7502
0
        || (rel + 2 < relend
7503
0
      && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7504
0
      ++rel;
7505
7506
0
    if (rel == relend)
7507
0
      {
7508
0
        if (sec->size == offset + 24)
7509
0
    {
7510
0
      need_pad = NULL;
7511
0
      break;
7512
0
    }
7513
0
        if (sec->size == offset + 16)
7514
0
    {
7515
0
      cnt_16b++;
7516
0
      break;
7517
0
    }
7518
0
        goto broken_opd;
7519
0
      }
7520
0
    else if (rel + 1 < relend
7521
0
       && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7522
0
       && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7523
0
      {
7524
0
        if (rel[0].r_offset == offset + 16)
7525
0
    cnt_16b++;
7526
0
        else if (rel[0].r_offset != offset + 24)
7527
0
    goto broken_opd;
7528
0
      }
7529
0
    else
7530
0
      goto broken_opd;
7531
0
  }
7532
7533
0
      add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7534
7535
0
      if (!broken && (need_edit || add_aux_fields))
7536
0
  {
7537
0
    Elf_Internal_Rela *write_rel;
7538
0
    Elf_Internal_Shdr *rel_hdr;
7539
0
    bfd_byte *rptr, *wptr;
7540
0
    bfd_byte *new_contents;
7541
0
    bfd_size_type amt;
7542
7543
0
    new_contents = NULL;
7544
0
    amt = OPD_NDX (sec->size) * sizeof (long);
7545
0
    opd = &ppc64_elf_section_data (sec)->u.opd;
7546
0
    opd->adjust = bfd_zalloc (sec->owner, amt);
7547
0
    if (opd->adjust == NULL)
7548
0
      return false;
7549
7550
    /* This seems a waste of time as input .opd sections are all
7551
       zeros as generated by gcc, but I suppose there's no reason
7552
       this will always be so.  We might start putting something in
7553
       the third word of .opd entries.  */
7554
0
    if ((sec->flags & SEC_IN_MEMORY) == 0)
7555
0
      {
7556
0
        bfd_byte *loc;
7557
0
        if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7558
0
    {
7559
0
      free (loc);
7560
0
    error_ret:
7561
0
      if (symtab_hdr->contents != (unsigned char *) local_syms)
7562
0
        free (local_syms);
7563
0
      if (elf_section_data (sec)->relocs != relstart)
7564
0
        free (relstart);
7565
0
      return false;
7566
0
    }
7567
0
        sec->contents = loc;
7568
0
        sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7569
0
      }
7570
7571
0
    elf_section_data (sec)->relocs = relstart;
7572
7573
0
    new_contents = sec->contents;
7574
0
    if (add_aux_fields)
7575
0
      {
7576
0
        new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7577
0
        if (new_contents == NULL)
7578
0
    return false;
7579
0
        need_pad = NULL;
7580
0
      }
7581
0
    wptr = new_contents;
7582
0
    rptr = sec->contents;
7583
0
    write_rel = relstart;
7584
0
    for (rel = relstart; rel < relend; )
7585
0
      {
7586
0
        unsigned long r_symndx;
7587
0
        asection *sym_sec;
7588
0
        struct elf_link_hash_entry *h;
7589
0
        struct ppc_link_hash_entry *fdh = NULL;
7590
0
        Elf_Internal_Sym *sym;
7591
0
        long opd_ent_size;
7592
0
        Elf_Internal_Rela *next_rel;
7593
0
        bool skip;
7594
7595
0
        r_symndx = ELF64_R_SYM (rel->r_info);
7596
0
        if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7597
0
            r_symndx, ibfd))
7598
0
    goto error_ret;
7599
7600
0
        next_rel = rel + 2;
7601
0
        if (next_rel + 1 == relend
7602
0
      || (next_rel + 2 < relend
7603
0
          && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7604
0
    ++next_rel;
7605
7606
        /* See if the .opd entry is full 24 byte or
7607
     16 byte (with fd_aux entry overlapped with next
7608
     fd_func).  */
7609
0
        opd_ent_size = 24;
7610
0
        if (next_rel == relend)
7611
0
    {
7612
0
      if (sec->size == rel->r_offset + 16)
7613
0
        opd_ent_size = 16;
7614
0
    }
7615
0
        else if (next_rel->r_offset == rel->r_offset + 16)
7616
0
    opd_ent_size = 16;
7617
7618
0
        if (h != NULL
7619
0
      && h->root.root.string[0] == '.')
7620
0
    {
7621
0
      fdh = ppc_elf_hash_entry (h)->oh;
7622
0
      if (fdh != NULL)
7623
0
        {
7624
0
          fdh = ppc_follow_link (fdh);
7625
0
          if (fdh->elf.root.type != bfd_link_hash_defined
7626
0
        && fdh->elf.root.type != bfd_link_hash_defweak)
7627
0
      fdh = NULL;
7628
0
        }
7629
0
    }
7630
7631
0
        skip = (sym_sec->owner != ibfd
7632
0
          || sym_sec->output_section == bfd_abs_section_ptr);
7633
0
        if (skip)
7634
0
    {
7635
0
      if (fdh != NULL && sym_sec->owner == ibfd)
7636
0
        {
7637
          /* Arrange for the function descriptor sym
7638
       to be dropped.  */
7639
0
          fdh->elf.root.u.def.value = 0;
7640
0
          fdh->elf.root.u.def.section = sym_sec;
7641
0
        }
7642
0
      opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7643
7644
0
      if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7645
0
        rel = next_rel;
7646
0
      else
7647
0
        while (1)
7648
0
          {
7649
0
      if (!dec_dynrel_count (rel, sec, info,
7650
0
                 NULL, h, sym))
7651
0
        goto error_ret;
7652
7653
0
      if (++rel == next_rel)
7654
0
        break;
7655
7656
0
      r_symndx = ELF64_R_SYM (rel->r_info);
7657
0
      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7658
0
          r_symndx, ibfd))
7659
0
        goto error_ret;
7660
0
          }
7661
0
    }
7662
0
        else
7663
0
    {
7664
      /* We'll be keeping this opd entry.  */
7665
0
      long adjust;
7666
7667
0
      if (fdh != NULL)
7668
0
        {
7669
          /* Redefine the function descriptor symbol to
7670
       this location in the opd section.  It is
7671
       necessary to update the value here rather
7672
       than using an array of adjustments as we do
7673
       for local symbols, because various places
7674
       in the generic ELF code use the value
7675
       stored in u.def.value.  */
7676
0
          fdh->elf.root.u.def.value = wptr - new_contents;
7677
0
          fdh->adjust_done = 1;
7678
0
        }
7679
7680
      /* Local syms are a bit tricky.  We could
7681
         tweak them as they can be cached, but
7682
         we'd need to look through the local syms
7683
         for the function descriptor sym which we
7684
         don't have at the moment.  So keep an
7685
         array of adjustments.  */
7686
0
      adjust = (wptr - new_contents) - (rptr - sec->contents);
7687
0
      opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7688
7689
0
      if (wptr != rptr)
7690
0
        memcpy (wptr, rptr, opd_ent_size);
7691
0
      wptr += opd_ent_size;
7692
0
      if (add_aux_fields && opd_ent_size == 16)
7693
0
        {
7694
0
          memset (wptr, '\0', 8);
7695
0
          wptr += 8;
7696
0
        }
7697
7698
      /* We need to adjust any reloc offsets to point to the
7699
         new opd entries.  */
7700
0
      for ( ; rel != next_rel; ++rel)
7701
0
        {
7702
0
          rel->r_offset += adjust;
7703
0
          if (write_rel != rel)
7704
0
      memcpy (write_rel, rel, sizeof (*rel));
7705
0
          ++write_rel;
7706
0
        }
7707
0
    }
7708
7709
0
        rptr += opd_ent_size;
7710
0
      }
7711
7712
0
    sec->size = wptr - new_contents;
7713
0
    sec->reloc_count = write_rel - relstart;
7714
0
    if (add_aux_fields)
7715
0
      {
7716
0
        free (sec->contents);
7717
0
        sec->contents = new_contents;
7718
0
      }
7719
7720
    /* Fudge the header size too, as this is used later in
7721
       elf_bfd_final_link if we are emitting relocs.  */
7722
0
    rel_hdr = _bfd_elf_single_rel_hdr (sec);
7723
0
    rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7724
0
    some_edited = true;
7725
0
  }
7726
0
      else if (elf_section_data (sec)->relocs != relstart)
7727
0
  free (relstart);
7728
7729
0
      if (local_syms != NULL
7730
0
    && symtab_hdr->contents != (unsigned char *) local_syms)
7731
0
  {
7732
0
    if (!info->keep_memory)
7733
0
      free (local_syms);
7734
0
    else
7735
0
      symtab_hdr->contents = (unsigned char *) local_syms;
7736
0
  }
7737
0
    }
7738
7739
0
  if (some_edited)
7740
0
    elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7741
7742
  /* If we are doing a final link and the last .opd entry is just 16 byte
7743
     long, add a 8 byte padding after it.  */
7744
0
  if (need_pad != NULL && !bfd_link_relocatable (info))
7745
0
    {
7746
0
      bfd_byte *p;
7747
7748
0
      if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7749
0
  {
7750
0
    BFD_ASSERT (need_pad->size > 0);
7751
7752
0
    p = bfd_malloc (need_pad->size + 8);
7753
0
    if (p == NULL)
7754
0
      return false;
7755
7756
0
    if (!bfd_get_section_contents (need_pad->owner, need_pad,
7757
0
           p, 0, need_pad->size))
7758
0
      return false;
7759
7760
0
    need_pad->contents = p;
7761
0
    need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7762
0
  }
7763
0
      else
7764
0
  {
7765
0
    p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7766
0
    if (p == NULL)
7767
0
      return false;
7768
7769
0
    need_pad->contents = p;
7770
0
  }
7771
7772
0
      memset (need_pad->contents + need_pad->size, 0, 8);
7773
0
      need_pad->size += 8;
7774
0
    }
7775
7776
0
  return true;
7777
0
}
7778
7779
/* Analyze inline PLT call relocations to see whether calls to locally
7780
   defined functions can be converted to direct calls.  */
7781
7782
bool
7783
ppc64_elf_inline_plt (struct bfd_link_info *info)
7784
0
{
7785
0
  struct ppc_link_hash_table *htab;
7786
0
  bfd *ibfd;
7787
0
  asection *sec;
7788
0
  bfd_vma low_vma, high_vma, limit;
7789
7790
0
  htab = ppc_hash_table (info);
7791
0
  if (htab == NULL)
7792
0
    return false;
7793
7794
  /* A bl insn can reach -0x2000000 to 0x1fffffc.  The limit is
7795
     reduced somewhat to cater for possible stubs that might be added
7796
     between the call and its destination.  */
7797
0
  if (htab->params->group_size < 0)
7798
0
    {
7799
0
      limit = -htab->params->group_size;
7800
0
      if (limit == 1)
7801
0
  limit = 0x1e00000;
7802
0
    }
7803
0
  else
7804
0
    {
7805
0
      limit = htab->params->group_size;
7806
0
      if (limit == 1)
7807
0
  limit = 0x1c00000;
7808
0
    }
7809
7810
0
  low_vma = -1;
7811
0
  high_vma = 0;
7812
0
  for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7813
0
    if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7814
0
      {
7815
0
  if (low_vma > sec->vma)
7816
0
    low_vma = sec->vma;
7817
0
  if (high_vma < sec->vma + sec->size)
7818
0
    high_vma = sec->vma + sec->size;
7819
0
      }
7820
7821
  /* If a "bl" can reach anywhere in local code sections, then we can
7822
     convert all inline PLT sequences to direct calls when the symbol
7823
     is local.  */
7824
0
  if (high_vma - low_vma < limit)
7825
0
    {
7826
0
      htab->can_convert_all_inline_plt = 1;
7827
0
      return true;
7828
0
    }
7829
7830
  /* Otherwise, go looking through relocs for cases where a direct
7831
     call won't reach.  Mark the symbol on any such reloc to disable
7832
     the optimization and keep the PLT entry as it seems likely that
7833
     this will be better than creating trampolines.  Note that this
7834
     will disable the optimization for all inline PLT calls to a
7835
     particular symbol, not just those that won't reach.  The
7836
     difficulty in doing a more precise optimization is that the
7837
     linker needs to make a decision depending on whether a
7838
     particular R_PPC64_PLTCALL insn can be turned into a direct
7839
     call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7840
     the sequence, and there is nothing that ties those relocs
7841
     together except their symbol.  */
7842
7843
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7844
0
    {
7845
0
      Elf_Internal_Shdr *symtab_hdr;
7846
0
      Elf_Internal_Sym *local_syms;
7847
7848
0
      if (!is_ppc64_elf (ibfd))
7849
0
  continue;
7850
7851
0
      local_syms = NULL;
7852
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
7853
7854
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7855
0
  if (ppc64_elf_section_data (sec)->has_pltcall
7856
0
      && !bfd_is_abs_section (sec->output_section))
7857
0
    {
7858
0
      Elf_Internal_Rela *relstart, *rel, *relend;
7859
7860
      /* Read the relocations.  */
7861
0
      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7862
0
              info->keep_memory);
7863
0
      if (relstart == NULL)
7864
0
        return false;
7865
7866
0
      relend = relstart + sec->reloc_count;
7867
0
      for (rel = relstart; rel < relend; rel++)
7868
0
        {
7869
0
    enum elf_ppc64_reloc_type r_type;
7870
0
    unsigned long r_symndx;
7871
0
    asection *sym_sec;
7872
0
    struct elf_link_hash_entry *h;
7873
0
    Elf_Internal_Sym *sym;
7874
0
    unsigned char *tls_maskp;
7875
7876
0
    r_type = ELF64_R_TYPE (rel->r_info);
7877
0
    if (r_type != R_PPC64_PLTCALL
7878
0
        && r_type != R_PPC64_PLTCALL_NOTOC)
7879
0
      continue;
7880
7881
0
    r_symndx = ELF64_R_SYM (rel->r_info);
7882
0
    if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7883
0
        r_symndx, ibfd))
7884
0
      {
7885
0
        if (elf_section_data (sec)->relocs != relstart)
7886
0
          free (relstart);
7887
0
        if (symtab_hdr->contents != (bfd_byte *) local_syms)
7888
0
          free (local_syms);
7889
0
        return false;
7890
0
      }
7891
7892
0
    if (sym_sec != NULL && sym_sec->output_section != NULL)
7893
0
      {
7894
0
        bfd_vma from, to;
7895
0
        if (h != NULL)
7896
0
          to = h->root.u.def.value;
7897
0
        else
7898
0
          to = sym->st_value;
7899
0
        to += (rel->r_addend
7900
0
         + sym_sec->output_offset
7901
0
         + sym_sec->output_section->vma);
7902
0
        from = (rel->r_offset
7903
0
          + sec->output_offset
7904
0
          + sec->output_section->vma);
7905
0
        if (to - from + limit < 2 * limit
7906
0
      && !(r_type == R_PPC64_PLTCALL_NOTOC
7907
0
           && (((h ? h->other : sym->st_other)
7908
0
          & STO_PPC64_LOCAL_MASK)
7909
0
         > 1 << STO_PPC64_LOCAL_BIT)))
7910
0
          *tls_maskp &= ~PLT_KEEP;
7911
0
      }
7912
0
        }
7913
0
      if (elf_section_data (sec)->relocs != relstart)
7914
0
        free (relstart);
7915
0
    }
7916
7917
0
      if (local_syms != NULL
7918
0
    && symtab_hdr->contents != (unsigned char *) local_syms)
7919
0
  {
7920
0
    if (!info->keep_memory)
7921
0
      free (local_syms);
7922
0
    else
7923
0
      symtab_hdr->contents = (unsigned char *) local_syms;
7924
0
  }
7925
0
    }
7926
7927
0
  return true;
7928
0
}
7929
7930
/* Set htab->tls_get_addr and various other info specific to TLS.
7931
   This needs to run before dynamic symbols are processed in
7932
   bfd_elf_size_dynamic_sections.  */
7933
7934
bool
7935
ppc64_elf_tls_setup (struct bfd_link_info *info)
7936
0
{
7937
0
  struct ppc_link_hash_table *htab;
7938
0
  struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
7939
7940
0
  htab = ppc_hash_table (info);
7941
0
  if (htab == NULL)
7942
0
    return false;
7943
7944
  /* Move dynamic linking info to the function descriptor sym.  */
7945
0
  if (htab->need_func_desc_adj)
7946
0
    {
7947
0
      elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7948
0
      htab->need_func_desc_adj = 0;
7949
0
    }
7950
7951
0
  if (abiversion (info->output_bfd) == 1)
7952
0
    htab->opd_abi = 1;
7953
7954
0
  if (htab->params->no_multi_toc)
7955
0
    htab->do_multi_toc = 0;
7956
0
  else if (!htab->do_multi_toc)
7957
0
    htab->params->no_multi_toc = 1;
7958
7959
  /* Default to --no-plt-localentry, as this option can cause problems
7960
     with symbol interposition.  For example, glibc libpthread.so and
7961
     libc.so duplicate many pthread symbols, with a fallback
7962
     implementation in libc.so.  In some cases the fallback does more
7963
     work than the pthread implementation.  __pthread_condattr_destroy
7964
     is one such symbol: the libpthread.so implementation is
7965
     localentry:0 while the libc.so implementation is localentry:8.
7966
     An app that "cleverly" uses dlopen to only load necessary
7967
     libraries at runtime may omit loading libpthread.so when not
7968
     running multi-threaded, which then results in the libc.so
7969
     fallback symbols being used and ld.so complaining.  Now there
7970
     are workarounds in ld (see non_zero_localentry) to detect the
7971
     pthread situation, but that may not be the only case where
7972
     --plt-localentry can cause trouble.  */
7973
0
  if (htab->params->plt_localentry0 < 0)
7974
0
    htab->params->plt_localentry0 = 0;
7975
0
  if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7976
0
    {
7977
      /* The issue is that __glink_PLTresolve saves r2, which is done
7978
   because glibc ld.so _dl_runtime_resolve restores r2 to support
7979
   a glibc plt call optimisation where global entry code is
7980
   skipped on calls that resolve to the same binary.  The
7981
   __glink_PLTresolve save of r2 is incompatible with code
7982
   making tail calls, because the tail call might go via the
7983
   resolver and thus overwrite the proper saved r2.  */
7984
0
      _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7985
0
          "power10 pc-relative code"));
7986
0
      htab->params->plt_localentry0 = 0;
7987
0
    }
7988
0
  if (htab->params->plt_localentry0
7989
0
      && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7990
0
             false, false, false) == NULL)
7991
0
    _bfd_error_handler
7992
0
      (_("warning: --plt-localentry is especially dangerous without "
7993
0
   "ld.so support to detect ABI violations"));
7994
7995
0
  tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7996
0
            false, false, true);
7997
0
  htab->tls_get_addr = ppc_elf_hash_entry (tga);
7998
0
  tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7999
0
         false, false, true);
8000
0
  htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
8001
8002
0
  desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
8003
0
             false, false, true);
8004
0
  htab->tga_desc = ppc_elf_hash_entry (desc);
8005
0
  desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
8006
0
          false, false, true);
8007
0
  htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
8008
8009
0
  if (htab->params->tls_get_addr_opt)
8010
0
    {
8011
0
      struct elf_link_hash_entry *opt, *opt_fd;
8012
8013
0
      opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8014
0
          false, false, true);
8015
0
      opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8016
0
             false, false, true);
8017
0
      if (opt_fd != NULL
8018
0
    && (opt_fd->root.type == bfd_link_hash_defined
8019
0
        || opt_fd->root.type == bfd_link_hash_defweak))
8020
0
  {
8021
    /* If glibc supports an optimized __tls_get_addr call stub,
8022
       signalled by the presence of __tls_get_addr_opt, and we'll
8023
       be calling __tls_get_addr via a plt call stub, then
8024
       make __tls_get_addr point to __tls_get_addr_opt.  */
8025
0
    if (!(htab->elf.dynamic_sections_created
8026
0
    && tga_fd != NULL
8027
0
    && (tga_fd->type == STT_FUNC
8028
0
        || tga_fd->needs_plt)
8029
0
    && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8030
0
         || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
8031
0
      tga_fd = NULL;
8032
0
    if (!(htab->elf.dynamic_sections_created
8033
0
    && desc_fd != NULL
8034
0
    && (desc_fd->type == STT_FUNC
8035
0
        || desc_fd->needs_plt)
8036
0
    && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
8037
0
         || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
8038
0
      desc_fd = NULL;
8039
8040
0
    if (tga_fd != NULL || desc_fd != NULL)
8041
0
      {
8042
0
        struct plt_entry *ent = NULL;
8043
8044
0
        if (tga_fd != NULL)
8045
0
    for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8046
0
      if (ent->plt.refcount > 0)
8047
0
        break;
8048
0
        if (ent == NULL && desc_fd != NULL)
8049
0
    for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8050
0
      if (ent->plt.refcount > 0)
8051
0
        break;
8052
0
        if (ent != NULL)
8053
0
    {
8054
0
      if (tga_fd != NULL)
8055
0
        {
8056
0
          tga_fd->root.type = bfd_link_hash_indirect;
8057
0
          tga_fd->root.u.i.link = &opt_fd->root;
8058
0
          tga_fd->root.u.i.warning = NULL;
8059
0
          ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8060
0
        }
8061
0
      if (desc_fd != NULL)
8062
0
        {
8063
0
          desc_fd->root.type = bfd_link_hash_indirect;
8064
0
          desc_fd->root.u.i.link = &opt_fd->root;
8065
0
          desc_fd->root.u.i.warning = NULL;
8066
0
          ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8067
0
        }
8068
0
      opt_fd->mark = 1;
8069
0
      if (opt_fd->dynindx != -1)
8070
0
        {
8071
          /* Use __tls_get_addr_opt in dynamic relocations.  */
8072
0
          opt_fd->dynindx = -1;
8073
0
          _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8074
0
                opt_fd->dynstr_index);
8075
0
          if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8076
0
      return false;
8077
0
        }
8078
0
      if (tga_fd != NULL)
8079
0
        {
8080
0
          htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
8081
0
          tga = elf_hash_entry (htab->tls_get_addr);
8082
0
          if (opt != NULL && tga != NULL)
8083
0
      {
8084
0
        tga->root.type = bfd_link_hash_indirect;
8085
0
        tga->root.u.i.link = &opt->root;
8086
0
        tga->root.u.i.warning = NULL;
8087
0
        ppc64_elf_copy_indirect_symbol (info, opt, tga);
8088
0
        opt->mark = 1;
8089
0
        _bfd_elf_link_hash_hide_symbol (info, opt,
8090
0
                tga->forced_local);
8091
0
        htab->tls_get_addr = ppc_elf_hash_entry (opt);
8092
0
      }
8093
0
          htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8094
0
          htab->tls_get_addr_fd->is_func_descriptor = 1;
8095
0
          if (htab->tls_get_addr != NULL)
8096
0
      {
8097
0
        htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8098
0
        htab->tls_get_addr->is_func = 1;
8099
0
      }
8100
0
        }
8101
0
      if (desc_fd != NULL)
8102
0
        {
8103
0
          htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8104
0
          if (opt != NULL && desc != NULL)
8105
0
      {
8106
0
        desc->root.type = bfd_link_hash_indirect;
8107
0
        desc->root.u.i.link = &opt->root;
8108
0
        desc->root.u.i.warning = NULL;
8109
0
        ppc64_elf_copy_indirect_symbol (info, opt, desc);
8110
0
        opt->mark = 1;
8111
0
        _bfd_elf_link_hash_hide_symbol (info, opt,
8112
0
                desc->forced_local);
8113
0
        htab->tga_desc = ppc_elf_hash_entry (opt);
8114
0
      }
8115
0
          htab->tga_desc_fd->oh = htab->tga_desc;
8116
0
          htab->tga_desc_fd->is_func_descriptor = 1;
8117
0
          if (htab->tga_desc != NULL)
8118
0
      {
8119
0
        htab->tga_desc->oh = htab->tga_desc_fd;
8120
0
        htab->tga_desc->is_func = 1;
8121
0
      }
8122
0
        }
8123
0
    }
8124
0
      }
8125
0
  }
8126
0
      else if (htab->params->tls_get_addr_opt < 0)
8127
0
  htab->params->tls_get_addr_opt = 0;
8128
0
    }
8129
8130
0
  if (htab->tga_desc_fd != NULL
8131
0
      && htab->params->tls_get_addr_opt
8132
0
      && htab->params->no_tls_get_addr_regsave == -1)
8133
0
    htab->params->no_tls_get_addr_regsave = 0;
8134
8135
0
  return true;
8136
0
}
8137
8138
/* Return TRUE iff REL is a branch reloc with a global symbol matching
8139
   any of HASH1, HASH2, HASH3, or HASH4.  */
8140
8141
static bool
8142
branch_reloc_hash_match (bfd *ibfd,
8143
       Elf_Internal_Rela *rel,
8144
       struct ppc_link_hash_entry *hash1,
8145
       struct ppc_link_hash_entry *hash2,
8146
       struct ppc_link_hash_entry *hash3,
8147
       struct ppc_link_hash_entry *hash4)
8148
0
{
8149
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8150
0
  enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8151
0
  unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8152
8153
0
  if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8154
0
    {
8155
0
      struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8156
0
      struct elf_link_hash_entry *h;
8157
8158
0
      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8159
0
      h = elf_follow_link (h);
8160
0
      if (h == elf_hash_entry (hash1)
8161
0
    || h == elf_hash_entry (hash2)
8162
0
    || h == elf_hash_entry (hash3)
8163
0
    || h == elf_hash_entry (hash4))
8164
0
  return true;
8165
0
    }
8166
0
  return false;
8167
0
}
8168
8169
/* Run through all the TLS relocs looking for optimization
8170
   opportunities.  The linker has been hacked (see ppc64elf.em) to do
8171
   a preliminary section layout so that we know the TLS segment
8172
   offsets.  We can't optimize earlier because some optimizations need
8173
   to know the tp offset, and we need to optimize before allocating
8174
   dynamic relocations.  */
8175
8176
bool
8177
ppc64_elf_tls_optimize (struct bfd_link_info *info)
8178
0
{
8179
0
  bfd *ibfd;
8180
0
  asection *sec;
8181
0
  struct ppc_link_hash_table *htab;
8182
0
  unsigned char *toc_ref;
8183
0
  int pass;
8184
8185
0
  if (!bfd_link_executable (info))
8186
0
    return true;
8187
8188
0
  htab = ppc_hash_table (info);
8189
0
  if (htab == NULL)
8190
0
    return false;
8191
8192
0
  htab->do_tls_opt = 1;
8193
8194
  /* Make two passes over the relocs.  On the first pass, mark toc
8195
     entries involved with tls relocs, and check that tls relocs
8196
     involved in setting up a tls_get_addr call are indeed followed by
8197
     such a call.  If they are not, we can't do any tls optimization.
8198
     On the second pass twiddle tls_mask flags to notify
8199
     relocate_section that optimization can be done, and adjust got
8200
     and plt refcounts.  */
8201
0
  toc_ref = NULL;
8202
0
  for (pass = 0; pass < 2; ++pass)
8203
0
    for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8204
0
      {
8205
0
  Elf_Internal_Sym *locsyms = NULL;
8206
0
  asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8207
8208
0
  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8209
0
    if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8210
0
      {
8211
0
        Elf_Internal_Rela *relstart, *rel, *relend;
8212
0
        bool found_tls_get_addr_arg = 0;
8213
8214
        /* Read the relocations.  */
8215
0
        relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8216
0
                info->keep_memory);
8217
0
        if (relstart == NULL)
8218
0
    {
8219
0
      free (toc_ref);
8220
0
      return false;
8221
0
    }
8222
8223
0
        relend = relstart + sec->reloc_count;
8224
0
        for (rel = relstart; rel < relend; rel++)
8225
0
    {
8226
0
      enum elf_ppc64_reloc_type r_type;
8227
0
      unsigned long r_symndx;
8228
0
      struct elf_link_hash_entry *h;
8229
0
      Elf_Internal_Sym *sym;
8230
0
      asection *sym_sec;
8231
0
      unsigned char *tls_mask;
8232
0
      unsigned int tls_set, tls_clear, tls_type = 0;
8233
0
      bfd_vma value;
8234
0
      bool ok_tprel, is_local;
8235
0
      long toc_ref_index = 0;
8236
0
      int expecting_tls_get_addr = 0;
8237
0
      bool ret = false;
8238
8239
0
      r_symndx = ELF64_R_SYM (rel->r_info);
8240
0
      if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8241
0
          r_symndx, ibfd))
8242
0
        {
8243
0
        err_free_rel:
8244
0
          if (elf_section_data (sec)->relocs != relstart)
8245
0
      free (relstart);
8246
0
          free (toc_ref);
8247
0
          if (elf_symtab_hdr (ibfd).contents
8248
0
        != (unsigned char *) locsyms)
8249
0
      free (locsyms);
8250
0
          return ret;
8251
0
        }
8252
8253
0
      if (h != NULL)
8254
0
        {
8255
0
          if (h->root.type == bfd_link_hash_defined
8256
0
        || h->root.type == bfd_link_hash_defweak)
8257
0
      value = h->root.u.def.value;
8258
0
          else if (h->root.type == bfd_link_hash_undefweak)
8259
0
      value = 0;
8260
0
          else
8261
0
      {
8262
0
        found_tls_get_addr_arg = 0;
8263
0
        continue;
8264
0
      }
8265
0
        }
8266
0
      else
8267
        /* Symbols referenced by TLS relocs must be of type
8268
           STT_TLS.  So no need for .opd local sym adjust.  */
8269
0
        value = sym->st_value;
8270
8271
0
      ok_tprel = false;
8272
0
      is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8273
0
      if (is_local)
8274
0
        {
8275
0
          if (h != NULL
8276
0
        && h->root.type == bfd_link_hash_undefweak)
8277
0
      ok_tprel = true;
8278
0
          else if (sym_sec != NULL
8279
0
             && sym_sec->output_section != NULL)
8280
0
      {
8281
0
        value += sym_sec->output_offset;
8282
0
        value += sym_sec->output_section->vma;
8283
0
        value -= htab->elf.tls_sec->vma + TP_OFFSET;
8284
        /* Note that even though the prefix insns
8285
           allow a 1<<33 offset we use the same test
8286
           as for addis;addi.  There may be a mix of
8287
           pcrel and non-pcrel code and the decision
8288
           to optimise is per symbol, not per TLS
8289
           sequence.  */
8290
0
        ok_tprel = value + 0x80008000ULL < 1ULL << 32;
8291
0
      }
8292
0
        }
8293
8294
0
      r_type = ELF64_R_TYPE (rel->r_info);
8295
      /* If this section has old-style __tls_get_addr calls
8296
         without marker relocs, then check that each
8297
         __tls_get_addr call reloc is preceded by a reloc
8298
         that conceivably belongs to the __tls_get_addr arg
8299
         setup insn.  If we don't find matching arg setup
8300
         relocs, don't do any tls optimization.  */
8301
0
      if (pass == 0
8302
0
          && sec->nomark_tls_get_addr
8303
0
          && h != NULL
8304
0
          && is_tls_get_addr (h, htab)
8305
0
          && !found_tls_get_addr_arg
8306
0
          && is_branch_reloc (r_type))
8307
0
        {
8308
0
          info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8309
0
            "TLS optimization disabled\n"),
8310
0
                ibfd, sec, rel->r_offset);
8311
0
          ret = true;
8312
0
          goto err_free_rel;
8313
0
        }
8314
8315
0
      found_tls_get_addr_arg = 0;
8316
0
      switch (r_type)
8317
0
        {
8318
0
        case R_PPC64_GOT_TLSLD16:
8319
0
        case R_PPC64_GOT_TLSLD16_LO:
8320
0
        case R_PPC64_GOT_TLSLD_PCREL34:
8321
0
          expecting_tls_get_addr = 1;
8322
0
          found_tls_get_addr_arg = 1;
8323
          /* Fall through.  */
8324
8325
0
        case R_PPC64_GOT_TLSLD16_HI:
8326
0
        case R_PPC64_GOT_TLSLD16_HA:
8327
          /* These relocs should never be against a symbol
8328
       defined in a shared lib.  Leave them alone if
8329
       that turns out to be the case.  */
8330
0
          if (!is_local)
8331
0
      continue;
8332
8333
          /* LD -> LE */
8334
0
          tls_set = 0;
8335
0
          tls_clear = TLS_LD;
8336
0
          tls_type = TLS_TLS | TLS_LD;
8337
0
          break;
8338
8339
0
        case R_PPC64_GOT_TLSGD16:
8340
0
        case R_PPC64_GOT_TLSGD16_LO:
8341
0
        case R_PPC64_GOT_TLSGD_PCREL34:
8342
0
          expecting_tls_get_addr = 1;
8343
0
          found_tls_get_addr_arg = 1;
8344
          /* Fall through. */
8345
8346
0
        case R_PPC64_GOT_TLSGD16_HI:
8347
0
        case R_PPC64_GOT_TLSGD16_HA:
8348
0
          if (ok_tprel)
8349
      /* GD -> LE */
8350
0
      tls_set = 0;
8351
0
          else
8352
      /* GD -> IE */
8353
0
      tls_set = TLS_TLS | TLS_GDIE;
8354
0
          tls_clear = TLS_GD;
8355
0
          tls_type = TLS_TLS | TLS_GD;
8356
0
          break;
8357
8358
0
        case R_PPC64_GOT_TPREL_PCREL34:
8359
0
        case R_PPC64_GOT_TPREL16_DS:
8360
0
        case R_PPC64_GOT_TPREL16_LO_DS:
8361
0
        case R_PPC64_GOT_TPREL16_HI:
8362
0
        case R_PPC64_GOT_TPREL16_HA:
8363
0
          if (ok_tprel)
8364
0
      {
8365
        /* IE -> LE */
8366
0
        tls_set = 0;
8367
0
        tls_clear = TLS_TPREL;
8368
0
        tls_type = TLS_TLS | TLS_TPREL;
8369
0
        break;
8370
0
      }
8371
0
          continue;
8372
8373
0
        case R_PPC64_TLSLD:
8374
0
          if (!is_local)
8375
0
      continue;
8376
          /* Fall through.  */
8377
0
        case R_PPC64_TLSGD:
8378
0
          if (rel + 1 < relend
8379
0
        && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8380
0
      {
8381
0
        if (pass != 0
8382
0
            && (ELF64_R_TYPE (rel[1].r_info)
8383
0
          != R_PPC64_PLTSEQ)
8384
0
            && (ELF64_R_TYPE (rel[1].r_info)
8385
0
          != R_PPC64_PLTSEQ_NOTOC))
8386
0
          {
8387
0
            r_symndx = ELF64_R_SYM (rel[1].r_info);
8388
0
            if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8389
0
                r_symndx, ibfd))
8390
0
        goto err_free_rel;
8391
0
            if (h != NULL)
8392
0
        {
8393
0
          struct plt_entry *ent = NULL;
8394
8395
0
          for (ent = h->plt.plist;
8396
0
               ent != NULL;
8397
0
               ent = ent->next)
8398
0
            if (ent->addend == rel[1].r_addend)
8399
0
              break;
8400
8401
0
          if (ent != NULL
8402
0
              && ent->plt.refcount > 0)
8403
0
            ent->plt.refcount -= 1;
8404
0
        }
8405
0
          }
8406
0
        continue;
8407
0
      }
8408
0
          found_tls_get_addr_arg = 1;
8409
          /* Fall through.  */
8410
8411
0
        case R_PPC64_TLS:
8412
0
        case R_PPC64_TOC16:
8413
0
        case R_PPC64_TOC16_LO:
8414
0
          if (sym_sec == NULL || sym_sec != toc)
8415
0
      continue;
8416
8417
          /* Mark this toc entry as referenced by a TLS
8418
       code sequence.  We can do that now in the
8419
       case of R_PPC64_TLS, and after checking for
8420
       tls_get_addr for the TOC16 relocs.  */
8421
0
          if (toc_ref == NULL)
8422
0
      toc_ref
8423
0
        = bfd_zmalloc (toc->output_section->rawsize / 8);
8424
0
          if (toc_ref == NULL)
8425
0
      goto err_free_rel;
8426
8427
0
          if (h != NULL)
8428
0
      value = h->root.u.def.value;
8429
0
          else
8430
0
      value = sym->st_value;
8431
0
          value += rel->r_addend;
8432
0
          if (value % 8 != 0)
8433
0
      continue;
8434
0
          BFD_ASSERT (value < toc->size
8435
0
          && toc->output_offset % 8 == 0);
8436
0
          toc_ref_index = (value + toc->output_offset) / 8;
8437
0
          if (r_type == R_PPC64_TLS
8438
0
        || r_type == R_PPC64_TLSGD
8439
0
        || r_type == R_PPC64_TLSLD)
8440
0
      {
8441
0
        toc_ref[toc_ref_index] = 1;
8442
0
        continue;
8443
0
      }
8444
8445
0
          if (pass != 0 && toc_ref[toc_ref_index] == 0)
8446
0
      continue;
8447
8448
0
          tls_set = 0;
8449
0
          tls_clear = 0;
8450
0
          expecting_tls_get_addr = 2;
8451
0
          break;
8452
8453
0
        case R_PPC64_TPREL64:
8454
0
          if (pass == 0
8455
0
        || sec != toc
8456
0
        || toc_ref == NULL
8457
0
        || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8458
0
      continue;
8459
0
          if (ok_tprel)
8460
0
      {
8461
        /* IE -> LE */
8462
0
        tls_set = TLS_EXPLICIT;
8463
0
        tls_clear = TLS_TPREL;
8464
0
        break;
8465
0
      }
8466
0
          continue;
8467
8468
0
        case R_PPC64_DTPMOD64:
8469
0
          if (pass == 0
8470
0
        || sec != toc
8471
0
        || toc_ref == NULL
8472
0
        || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8473
0
      continue;
8474
0
          if (rel + 1 < relend
8475
0
        && (rel[1].r_info
8476
0
            == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8477
0
        && rel[1].r_offset == rel->r_offset + 8)
8478
0
      {
8479
0
        if (ok_tprel)
8480
          /* GD -> LE */
8481
0
          tls_set = TLS_EXPLICIT | TLS_GD;
8482
0
        else
8483
          /* GD -> IE */
8484
0
          tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8485
0
        tls_clear = TLS_GD;
8486
0
      }
8487
0
          else
8488
0
      {
8489
0
        if (!is_local)
8490
0
          continue;
8491
8492
        /* LD -> LE */
8493
0
        tls_set = TLS_EXPLICIT;
8494
0
        tls_clear = TLS_LD;
8495
0
      }
8496
0
          break;
8497
8498
0
        case R_PPC64_TPREL16_HA:
8499
0
          if (pass == 0)
8500
0
      {
8501
0
        unsigned char buf[4];
8502
0
        unsigned int insn;
8503
0
        bfd_vma off = rel->r_offset & ~3;
8504
0
        if (!bfd_get_section_contents (ibfd, sec, buf,
8505
0
               off, 4))
8506
0
          goto err_free_rel;
8507
0
        insn = bfd_get_32 (ibfd, buf);
8508
        /* addis rt,13,imm */
8509
0
        if ((insn & ((0x3fu << 26) | 0x1f << 16))
8510
0
            != ((15u << 26) | (13 << 16)))
8511
0
          {
8512
            /* xgettext:c-format */
8513
0
            info->callbacks->minfo
8514
0
        (_("%H: warning: %s unexpected insn %#x.\n"),
8515
0
         ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8516
0
            htab->do_tls_opt = 0;
8517
0
          }
8518
0
      }
8519
0
          continue;
8520
8521
0
        case R_PPC64_TPREL16_HI:
8522
0
        case R_PPC64_TPREL16_HIGH:
8523
0
        case R_PPC64_TPREL16_HIGHA:
8524
0
        case R_PPC64_TPREL16_HIGHER:
8525
0
        case R_PPC64_TPREL16_HIGHERA:
8526
0
        case R_PPC64_TPREL16_HIGHEST:
8527
0
        case R_PPC64_TPREL16_HIGHESTA:
8528
          /* These can all be used in sequences along with
8529
       TPREL16_LO or TPREL16_LO_DS in ways we aren't
8530
       able to verify easily.  */
8531
0
          htab->do_tls_opt = 0;
8532
0
          continue;
8533
8534
0
        default:
8535
0
          continue;
8536
0
        }
8537
8538
0
      if (pass == 0)
8539
0
        {
8540
0
          if (!expecting_tls_get_addr
8541
0
        || !sec->nomark_tls_get_addr)
8542
0
      continue;
8543
8544
0
          if (rel + 1 < relend
8545
0
        && branch_reloc_hash_match (ibfd, rel + 1,
8546
0
                  htab->tls_get_addr_fd,
8547
0
                  htab->tga_desc_fd,
8548
0
                  htab->tls_get_addr,
8549
0
                  htab->tga_desc))
8550
0
      {
8551
0
        if (expecting_tls_get_addr == 2)
8552
0
          {
8553
            /* Check for toc tls entries.  */
8554
0
            unsigned char *toc_tls;
8555
0
            int retval;
8556
8557
0
            retval = get_tls_mask (&toc_tls, NULL, NULL,
8558
0
                 &locsyms,
8559
0
                 rel, ibfd);
8560
0
            if (retval == 0)
8561
0
        goto err_free_rel;
8562
0
            if (toc_tls != NULL)
8563
0
        {
8564
0
          if ((*toc_tls & TLS_TLS) != 0
8565
0
              && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8566
0
            found_tls_get_addr_arg = 1;
8567
0
          if (retval > 1)
8568
0
            toc_ref[toc_ref_index] = 1;
8569
0
        }
8570
0
          }
8571
0
        continue;
8572
0
      }
8573
8574
          /* Uh oh, we didn't find the expected call.  We
8575
       could just mark this symbol to exclude it
8576
       from tls optimization but it's safer to skip
8577
       the entire optimization.  */
8578
          /* xgettext:c-format */
8579
0
          info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8580
0
            "TLS optimization disabled\n"),
8581
0
                ibfd, sec, rel->r_offset);
8582
0
          ret = true;
8583
0
          goto err_free_rel;
8584
0
        }
8585
8586
      /* If we don't have old-style __tls_get_addr calls
8587
         without TLSGD/TLSLD marker relocs, and we haven't
8588
         found a new-style __tls_get_addr call with a
8589
         marker for this symbol, then we either have a
8590
         broken object file or an -mlongcall style
8591
         indirect call to __tls_get_addr without a marker.
8592
         Disable optimization in this case.  */
8593
0
      if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8594
0
          && (tls_set & TLS_EXPLICIT) == 0
8595
0
          && !sec->nomark_tls_get_addr
8596
0
          && ((*tls_mask & (TLS_TLS | TLS_MARK))
8597
0
        != (TLS_TLS | TLS_MARK)))
8598
0
        continue;
8599
8600
0
      if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8601
0
        {
8602
0
          struct plt_entry *ent = NULL;
8603
8604
0
          if (htab->tls_get_addr_fd != NULL)
8605
0
      for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8606
0
           ent != NULL;
8607
0
           ent = ent->next)
8608
0
        if (ent->addend == 0)
8609
0
          break;
8610
8611
0
          if (ent == NULL && htab->tga_desc_fd != NULL)
8612
0
      for (ent = htab->tga_desc_fd->elf.plt.plist;
8613
0
           ent != NULL;
8614
0
           ent = ent->next)
8615
0
        if (ent->addend == 0)
8616
0
          break;
8617
8618
0
          if (ent == NULL && htab->tls_get_addr != NULL)
8619
0
      for (ent = htab->tls_get_addr->elf.plt.plist;
8620
0
           ent != NULL;
8621
0
           ent = ent->next)
8622
0
        if (ent->addend == 0)
8623
0
          break;
8624
8625
0
          if (ent == NULL && htab->tga_desc != NULL)
8626
0
      for (ent = htab->tga_desc->elf.plt.plist;
8627
0
           ent != NULL;
8628
0
           ent = ent->next)
8629
0
        if (ent->addend == 0)
8630
0
          break;
8631
8632
0
          if (ent != NULL
8633
0
        && ent->plt.refcount > 0)
8634
0
      ent->plt.refcount -= 1;
8635
0
        }
8636
8637
0
      if (tls_clear == 0)
8638
0
        continue;
8639
8640
0
      if ((tls_set & TLS_EXPLICIT) == 0)
8641
0
        {
8642
0
          struct got_entry *ent;
8643
8644
          /* Adjust got entry for this reloc.  */
8645
0
          if (h != NULL)
8646
0
      ent = h->got.glist;
8647
0
          else
8648
0
      ent = elf_local_got_ents (ibfd)[r_symndx];
8649
8650
0
          for (; ent != NULL; ent = ent->next)
8651
0
      if (ent->addend == rel->r_addend
8652
0
          && ent->owner == ibfd
8653
0
          && ent->tls_type == tls_type)
8654
0
        break;
8655
0
          if (ent == NULL)
8656
0
      abort ();
8657
8658
0
          if (tls_set == 0)
8659
0
      {
8660
        /* We managed to get rid of a got entry.  */
8661
0
        if (ent->got.refcount > 0)
8662
0
          ent->got.refcount -= 1;
8663
0
      }
8664
0
        }
8665
0
      else
8666
0
        {
8667
          /* If we got rid of a DTPMOD/DTPREL reloc pair then
8668
       we'll lose one or two dyn relocs.  */
8669
0
          if (!dec_dynrel_count (rel, sec, info,
8670
0
               NULL, h, sym))
8671
0
      return false;
8672
8673
0
          if (tls_set == (TLS_EXPLICIT | TLS_GD))
8674
0
      {
8675
0
        if (!dec_dynrel_count (rel + 1, sec, info,
8676
0
             NULL, h, sym))
8677
0
          return false;
8678
0
      }
8679
0
        }
8680
8681
0
      *tls_mask |= tls_set & 0xff;
8682
0
      *tls_mask &= ~tls_clear;
8683
0
    }
8684
8685
0
        if (elf_section_data (sec)->relocs != relstart)
8686
0
    free (relstart);
8687
0
      }
8688
8689
0
  if (locsyms != NULL
8690
0
      && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8691
0
    {
8692
0
      if (!info->keep_memory)
8693
0
        free (locsyms);
8694
0
      else
8695
0
        elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8696
0
    }
8697
0
      }
8698
8699
0
  free (toc_ref);
8700
0
  return true;
8701
0
}
8702
8703
/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8704
   the values of any global symbols in a toc section that has been
8705
   edited.  Globals in toc sections should be a rarity, so this function
8706
   sets a flag if any are found in toc sections other than the one just
8707
   edited, so that further hash table traversals can be avoided.  */
8708
8709
struct adjust_toc_info
8710
{
8711
  asection *toc;
8712
  unsigned long *skip;
8713
  bool global_toc_syms;
8714
};
8715
8716
enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8717
8718
static bool
8719
adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8720
0
{
8721
0
  struct ppc_link_hash_entry *eh;
8722
0
  struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8723
0
  unsigned long i;
8724
8725
0
  if (h->root.type != bfd_link_hash_defined
8726
0
      && h->root.type != bfd_link_hash_defweak)
8727
0
    return true;
8728
8729
0
  eh = ppc_elf_hash_entry (h);
8730
0
  if (eh->adjust_done)
8731
0
    return true;
8732
8733
0
  if (eh->elf.root.u.def.section == toc_inf->toc)
8734
0
    {
8735
0
      if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8736
0
  i = toc_inf->toc->rawsize >> 3;
8737
0
      else
8738
0
  i = eh->elf.root.u.def.value >> 3;
8739
8740
0
      if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8741
0
  {
8742
0
    _bfd_error_handler
8743
0
      (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8744
0
    do
8745
0
      ++i;
8746
0
    while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8747
0
    eh->elf.root.u.def.value = (bfd_vma) i << 3;
8748
0
  }
8749
8750
0
      eh->elf.root.u.def.value -= toc_inf->skip[i];
8751
0
      eh->adjust_done = 1;
8752
0
    }
8753
0
  else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8754
0
    toc_inf->global_toc_syms = true;
8755
8756
0
  return true;
8757
0
}
8758
8759
/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8760
   on a _LO variety toc/got reloc.  */
8761
8762
static bool
8763
ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8764
0
{
8765
0
  return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8766
0
    || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8767
0
    || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8768
0
    || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8769
0
    || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8770
0
    || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8771
0
    || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8772
0
    || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8773
0
    || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8774
0
    || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8775
0
    || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8776
0
    || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8777
0
    || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8778
0
    || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8779
0
    || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8780
0
    || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8781
0
    || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8782
        /* Exclude lfqu by testing reloc.  If relocs are ever
8783
     defined for the reduced D field in psq_lu then those
8784
     will need testing too.  */
8785
0
        && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8786
0
    || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
8787
0
        && (insn & 1) == 0)
8788
0
    || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8789
0
    || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8790
        /* Exclude stfqu.  psq_stu as above for psq_lu.  */
8791
0
        && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8792
0
    || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
8793
0
        && (insn & 1) == 0));
8794
0
}
8795
8796
/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8797
     pld ra,symbol@got@pcrel
8798
     load/store rt,off(ra)
8799
   or
8800
     pla ra,symbol@pcrel
8801
     load/store rt,off(ra)
8802
   may be translated to
8803
     pload/pstore rt,symbol+off@pcrel
8804
     nop.
8805
   This function returns true if the optimization is possible, placing
8806
   the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8807
8808
   On entry to this function, the linker has already determined that
8809
   the pld can be replaced with pla: *PINSN1 is that pla insn,
8810
   while *PINSN2 is the second instruction.  */
8811
8812
static bool
8813
xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8814
0
{
8815
0
  uint64_t insn1 = *pinsn1;
8816
0
  uint64_t insn2 = *pinsn2;
8817
0
  bfd_signed_vma off;
8818
8819
0
  if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8820
0
    {
8821
      /* Check that regs match.  */
8822
0
      if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8823
0
  return false;
8824
8825
      /* P8LS or PMLS form, non-pcrel.  */
8826
0
      if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8827
0
  return false;
8828
8829
0
      *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8830
0
      *pinsn2 = PNOP;
8831
0
      off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8832
0
      *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8833
0
      return true;
8834
0
    }
8835
8836
0
  insn2 >>= 32;
8837
8838
  /* Check that regs match.  */
8839
0
  if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8840
0
    return false;
8841
8842
0
  switch ((insn2 >> 26) & 63)
8843
0
    {
8844
0
    default:
8845
0
      return false;
8846
8847
0
    case 32: /* lwz */
8848
0
    case 34: /* lbz */
8849
0
    case 36: /* stw */
8850
0
    case 38: /* stb */
8851
0
    case 40: /* lhz */
8852
0
    case 42: /* lha */
8853
0
    case 44: /* sth */
8854
0
    case 48: /* lfs */
8855
0
    case 50: /* lfd */
8856
0
    case 52: /* stfs */
8857
0
    case 54: /* stfd */
8858
      /* These are the PMLS cases, where we just need to tack a prefix
8859
   on the insn.  */
8860
0
      insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8861
0
         | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8862
0
      off = insn2 & 0xffff;
8863
0
      break;
8864
8865
0
    case 58: /* lwa, ld */
8866
0
      if ((insn2 & 1) != 0)
8867
0
  return false;
8868
0
      insn1 = ((1ULL << 58) | (1ULL << 52)
8869
0
         | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8870
0
         | (insn2 & (31ULL << 21)));
8871
0
      off = insn2 & 0xfffc;
8872
0
      break;
8873
8874
0
    case 57: /* lxsd, lxssp */
8875
0
      if ((insn2 & 3) < 2)
8876
0
  return false;
8877
0
      insn1 = ((1ULL << 58) | (1ULL << 52)
8878
0
         | ((40ULL | (insn2 & 3)) << 26)
8879
0
         | (insn2 & (31ULL << 21)));
8880
0
      off = insn2 & 0xfffc;
8881
0
      break;
8882
8883
0
    case 61: /* stxsd, stxssp, lxv, stxv  */
8884
0
      if ((insn2 & 3) == 0)
8885
0
  return false;
8886
0
      else if ((insn2 & 3) >= 2)
8887
0
  {
8888
0
    insn1 = ((1ULL << 58) | (1ULL << 52)
8889
0
       | ((44ULL | (insn2 & 3)) << 26)
8890
0
       | (insn2 & (31ULL << 21)));
8891
0
    off = insn2 & 0xfffc;
8892
0
  }
8893
0
      else
8894
0
  {
8895
0
    insn1 = ((1ULL << 58) | (1ULL << 52)
8896
0
       | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8897
0
       | (insn2 & (31ULL << 21)));
8898
0
    off = insn2 & 0xfff0;
8899
0
  }
8900
0
      break;
8901
8902
0
    case 56: /* lq */
8903
0
      insn1 = ((1ULL << 58) | (1ULL << 52)
8904
0
         | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8905
0
      off = insn2 & 0xffff;
8906
0
      break;
8907
8908
0
    case 6: /* lxvp, stxvp */
8909
0
      if ((insn2 & 0xe) != 0)
8910
0
  return false;
8911
0
      insn1 = ((1ULL << 58) | (1ULL << 52)
8912
0
         | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8913
0
         | (insn2 & (31ULL << 21)));
8914
0
      off = insn2 & 0xfff0;
8915
0
      break;
8916
8917
0
    case 62: /* std, stq */
8918
0
      if ((insn2 & 1) != 0)
8919
0
  return false;
8920
0
      insn1 = ((1ULL << 58) | (1ULL << 52)
8921
0
         | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8922
0
         | (insn2 & (31ULL << 21)));
8923
0
      off = insn2 & 0xfffc;
8924
0
      break;
8925
0
    }
8926
8927
0
  *pinsn1 = insn1;
8928
0
  *pinsn2 = (uint64_t) NOP << 32;
8929
0
  *poff = (off ^ 0x8000) - 0x8000;
8930
0
  return true;
8931
0
}
8932
8933
/* Examine all relocs referencing .toc sections in order to remove
8934
   unused .toc entries.  */
8935
8936
bool
8937
ppc64_elf_edit_toc (struct bfd_link_info *info)
8938
0
{
8939
0
  bfd *ibfd;
8940
0
  struct adjust_toc_info toc_inf;
8941
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
8942
8943
0
  htab->do_toc_opt = 1;
8944
0
  toc_inf.global_toc_syms = true;
8945
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8946
0
    {
8947
0
      asection *toc, *sec;
8948
0
      Elf_Internal_Shdr *symtab_hdr;
8949
0
      Elf_Internal_Sym *local_syms;
8950
0
      Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8951
0
      unsigned long *skip, *drop;
8952
0
      unsigned char *used;
8953
0
      unsigned char *keep, last, some_unused;
8954
8955
0
      if (!is_ppc64_elf (ibfd))
8956
0
  continue;
8957
8958
0
      toc = bfd_get_section_by_name (ibfd, ".toc");
8959
0
      if (toc == NULL
8960
0
    || toc->size == 0
8961
0
    || (toc->flags & SEC_HAS_CONTENTS) == 0
8962
0
    || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8963
0
    || discarded_section (toc))
8964
0
  continue;
8965
8966
0
      toc_relocs = NULL;
8967
0
      local_syms = NULL;
8968
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
8969
8970
      /* Look at sections dropped from the final link.  */
8971
0
      skip = NULL;
8972
0
      relstart = NULL;
8973
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8974
0
  {
8975
0
    if (sec->reloc_count == 0
8976
0
        || !discarded_section (sec)
8977
0
        || get_opd_info (sec)
8978
0
        || (sec->flags & SEC_ALLOC) == 0
8979
0
        || (sec->flags & SEC_DEBUGGING) != 0)
8980
0
      continue;
8981
8982
0
    relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
8983
0
    if (relstart == NULL)
8984
0
      goto error_ret;
8985
8986
    /* Run through the relocs to see which toc entries might be
8987
       unused.  */
8988
0
    for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8989
0
      {
8990
0
        enum elf_ppc64_reloc_type r_type;
8991
0
        unsigned long r_symndx;
8992
0
        asection *sym_sec;
8993
0
        struct elf_link_hash_entry *h;
8994
0
        Elf_Internal_Sym *sym;
8995
0
        bfd_vma val;
8996
8997
0
        r_type = ELF64_R_TYPE (rel->r_info);
8998
0
        switch (r_type)
8999
0
    {
9000
0
    default:
9001
0
      continue;
9002
9003
0
    case R_PPC64_TOC16:
9004
0
    case R_PPC64_TOC16_LO:
9005
0
    case R_PPC64_TOC16_HI:
9006
0
    case R_PPC64_TOC16_HA:
9007
0
    case R_PPC64_TOC16_DS:
9008
0
    case R_PPC64_TOC16_LO_DS:
9009
0
      break;
9010
0
    }
9011
9012
0
        r_symndx = ELF64_R_SYM (rel->r_info);
9013
0
        if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9014
0
            r_symndx, ibfd))
9015
0
    goto error_ret;
9016
9017
0
        if (sym_sec != toc)
9018
0
    continue;
9019
9020
0
        if (h != NULL)
9021
0
    val = h->root.u.def.value;
9022
0
        else
9023
0
    val = sym->st_value;
9024
0
        val += rel->r_addend;
9025
9026
0
        if (val >= toc->size)
9027
0
    continue;
9028
9029
        /* Anything in the toc ought to be aligned to 8 bytes.
9030
     If not, don't mark as unused.  */
9031
0
        if (val & 7)
9032
0
    continue;
9033
9034
0
        if (skip == NULL)
9035
0
    {
9036
0
      skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9037
0
      if (skip == NULL)
9038
0
        goto error_ret;
9039
0
    }
9040
9041
0
        skip[val >> 3] = ref_from_discarded;
9042
0
      }
9043
9044
0
    if (elf_section_data (sec)->relocs != relstart)
9045
0
      free (relstart);
9046
0
  }
9047
9048
      /* For largetoc loads of address constants, we can convert
9049
   .  addis rx,2,addr@got@ha
9050
   .  ld ry,addr@got@l(rx)
9051
   to
9052
   .  addis rx,2,addr@toc@ha
9053
   .  addi ry,rx,addr@toc@l
9054
   when addr is within 2G of the toc pointer.  This then means
9055
   that the word storing "addr" in the toc is no longer needed.  */
9056
9057
0
      if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9058
0
    && toc->output_section->rawsize < (bfd_vma) 1 << 31
9059
0
    && toc->reloc_count != 0)
9060
0
  {
9061
    /* Read toc relocs.  */
9062
0
    toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9063
0
              info->keep_memory);
9064
0
    if (toc_relocs == NULL)
9065
0
      goto error_ret;
9066
9067
0
    for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9068
0
      {
9069
0
        enum elf_ppc64_reloc_type r_type;
9070
0
        unsigned long r_symndx;
9071
0
        asection *sym_sec;
9072
0
        struct elf_link_hash_entry *h;
9073
0
        Elf_Internal_Sym *sym;
9074
0
        bfd_vma val, addr;
9075
9076
0
        r_type = ELF64_R_TYPE (rel->r_info);
9077
0
        if (r_type != R_PPC64_ADDR64)
9078
0
    continue;
9079
9080
0
        r_symndx = ELF64_R_SYM (rel->r_info);
9081
0
        if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9082
0
            r_symndx, ibfd))
9083
0
    goto error_ret;
9084
9085
0
        if (sym_sec == NULL
9086
0
      || sym_sec->output_section == NULL
9087
0
      || discarded_section (sym_sec))
9088
0
    continue;
9089
9090
0
        if (!SYMBOL_REFERENCES_LOCAL (info, h)
9091
0
      || (bfd_link_pic (info)
9092
0
          && sym_sec == bfd_abs_section_ptr))
9093
0
    continue;
9094
9095
0
        if (h != NULL)
9096
0
    {
9097
0
      if (h->type == STT_GNU_IFUNC)
9098
0
        continue;
9099
0
      val = h->root.u.def.value;
9100
0
    }
9101
0
        else
9102
0
    {
9103
0
      if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9104
0
        continue;
9105
0
      val = sym->st_value;
9106
0
    }
9107
0
        val += rel->r_addend;
9108
0
        val += sym_sec->output_section->vma + sym_sec->output_offset;
9109
9110
        /* We don't yet know the exact toc pointer value, but we
9111
     know it will be somewhere in the toc section.  Don't
9112
     optimize if the difference from any possible toc
9113
     pointer is outside [ff..f80008000, 7fff7fff].  */
9114
0
        addr = toc->output_section->vma + TOC_BASE_OFF;
9115
0
        if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9116
0
    continue;
9117
9118
0
        addr = toc->output_section->vma + toc->output_section->rawsize;
9119
0
        if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9120
0
    continue;
9121
9122
0
        if (skip == NULL)
9123
0
    {
9124
0
      skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9125
0
      if (skip == NULL)
9126
0
        goto error_ret;
9127
0
    }
9128
9129
0
        skip[rel->r_offset >> 3]
9130
0
    |= can_optimize | ((rel - toc_relocs) << 2);
9131
0
      }
9132
0
  }
9133
9134
0
      if (skip == NULL)
9135
0
  continue;
9136
9137
0
      used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9138
0
      if (used == NULL)
9139
0
  {
9140
0
  error_ret:
9141
0
    if (symtab_hdr->contents != (unsigned char *) local_syms)
9142
0
      free (local_syms);
9143
0
    if (sec != NULL
9144
0
        && elf_section_data (sec)->relocs != relstart)
9145
0
      free (relstart);
9146
0
    if (elf_section_data (toc)->relocs != toc_relocs)
9147
0
      free (toc_relocs);
9148
0
    free (skip);
9149
0
    return false;
9150
0
  }
9151
9152
      /* Now check all kept sections that might reference the toc.
9153
   Check the toc itself last.  */
9154
0
      for (sec = (ibfd->sections == toc && toc->next ? toc->next
9155
0
      : ibfd->sections);
9156
0
     sec != NULL;
9157
0
     sec = (sec == toc ? NULL
9158
0
      : sec->next == NULL ? toc
9159
0
      : sec->next == toc && toc->next ? toc->next
9160
0
      : sec->next))
9161
0
  {
9162
0
    int repeat;
9163
9164
0
    if (sec->reloc_count == 0
9165
0
        || discarded_section (sec)
9166
0
        || get_opd_info (sec)
9167
0
        || (sec->flags & SEC_ALLOC) == 0
9168
0
        || (sec->flags & SEC_DEBUGGING) != 0)
9169
0
      continue;
9170
9171
0
    relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9172
0
            info->keep_memory);
9173
0
    if (relstart == NULL)
9174
0
      {
9175
0
        free (used);
9176
0
        goto error_ret;
9177
0
      }
9178
9179
    /* Mark toc entries referenced as used.  */
9180
0
    do
9181
0
      {
9182
0
        repeat = 0;
9183
0
        for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9184
0
    {
9185
0
      enum elf_ppc64_reloc_type r_type;
9186
0
      unsigned long r_symndx;
9187
0
      asection *sym_sec;
9188
0
      struct elf_link_hash_entry *h;
9189
0
      Elf_Internal_Sym *sym;
9190
0
      bfd_vma val;
9191
9192
0
      r_type = ELF64_R_TYPE (rel->r_info);
9193
0
      switch (r_type)
9194
0
        {
9195
0
        case R_PPC64_TOC16:
9196
0
        case R_PPC64_TOC16_LO:
9197
0
        case R_PPC64_TOC16_HI:
9198
0
        case R_PPC64_TOC16_HA:
9199
0
        case R_PPC64_TOC16_DS:
9200
0
        case R_PPC64_TOC16_LO_DS:
9201
          /* In case we're taking addresses of toc entries.  */
9202
0
        case R_PPC64_ADDR64:
9203
0
          break;
9204
9205
0
        default:
9206
0
          continue;
9207
0
        }
9208
9209
0
      r_symndx = ELF64_R_SYM (rel->r_info);
9210
0
      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9211
0
          r_symndx, ibfd))
9212
0
        {
9213
0
          free (used);
9214
0
          goto error_ret;
9215
0
        }
9216
9217
0
      if (sym_sec != toc)
9218
0
        continue;
9219
9220
0
      if (h != NULL)
9221
0
        val = h->root.u.def.value;
9222
0
      else
9223
0
        val = sym->st_value;
9224
0
      val += rel->r_addend;
9225
9226
0
      if (val >= toc->size)
9227
0
        continue;
9228
9229
0
      if ((skip[val >> 3] & can_optimize) != 0)
9230
0
        {
9231
0
          bfd_vma off;
9232
0
          unsigned char opc;
9233
9234
0
          switch (r_type)
9235
0
      {
9236
0
      case R_PPC64_TOC16_HA:
9237
0
        break;
9238
9239
0
      case R_PPC64_TOC16_LO_DS:
9240
0
        off = rel->r_offset;
9241
0
        off += (bfd_big_endian (ibfd) ? -2 : 3);
9242
0
        if (!bfd_get_section_contents (ibfd, sec, &opc,
9243
0
               off, 1))
9244
0
          {
9245
0
            free (used);
9246
0
            goto error_ret;
9247
0
          }
9248
0
        if ((opc & (0x3f << 2)) == (58u << 2))
9249
0
          break;
9250
        /* Fall through.  */
9251
9252
0
      default:
9253
        /* Wrong sort of reloc, or not a ld.  We may
9254
           as well clear ref_from_discarded too.  */
9255
0
        skip[val >> 3] = 0;
9256
0
      }
9257
0
        }
9258
9259
0
      if (sec != toc)
9260
0
        used[val >> 3] = 1;
9261
      /* For the toc section, we only mark as used if this
9262
         entry itself isn't unused.  */
9263
0
      else if ((used[rel->r_offset >> 3]
9264
0
          || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9265
0
         && !used[val >> 3])
9266
0
        {
9267
          /* Do all the relocs again, to catch reference
9268
       chains.  */
9269
0
          repeat = 1;
9270
0
          used[val >> 3] = 1;
9271
0
        }
9272
0
    }
9273
0
      }
9274
0
    while (repeat);
9275
9276
0
    if (elf_section_data (sec)->relocs != relstart)
9277
0
      free (relstart);
9278
0
  }
9279
9280
      /* Merge the used and skip arrays.  Assume that TOC
9281
   doublewords not appearing as either used or unused belong
9282
   to an entry more than one doubleword in size.  */
9283
0
      for (drop = skip, keep = used, last = 0, some_unused = 0;
9284
0
     drop < skip + (toc->size + 7) / 8;
9285
0
     ++drop, ++keep)
9286
0
  {
9287
0
    if (*keep)
9288
0
      {
9289
0
        *drop &= ~ref_from_discarded;
9290
0
        if ((*drop & can_optimize) != 0)
9291
0
    some_unused = 1;
9292
0
        last = 0;
9293
0
      }
9294
0
    else if ((*drop & ref_from_discarded) != 0)
9295
0
      {
9296
0
        some_unused = 1;
9297
0
        last = ref_from_discarded;
9298
0
      }
9299
0
    else
9300
0
      *drop = last;
9301
0
  }
9302
9303
0
      free (used);
9304
9305
0
      if (some_unused)
9306
0
  {
9307
0
    bfd_byte *contents, *src;
9308
0
    unsigned long off;
9309
0
    Elf_Internal_Sym *sym;
9310
0
    bool local_toc_syms = false;
9311
9312
    /* Shuffle the toc contents, and at the same time convert the
9313
       skip array from booleans into offsets.  */
9314
0
    if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9315
0
      goto error_ret;
9316
9317
0
    elf_section_data (toc)->this_hdr.contents = contents;
9318
9319
0
    for (src = contents, off = 0, drop = skip;
9320
0
         src < contents + toc->size;
9321
0
         src += 8, ++drop)
9322
0
      {
9323
0
        if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9324
0
    off += 8;
9325
0
        else if (off != 0)
9326
0
    {
9327
0
      *drop = off;
9328
0
      memcpy (src - off, src, 8);
9329
0
    }
9330
0
      }
9331
0
    *drop = off;
9332
0
    toc->rawsize = toc->size;
9333
0
    toc->size = src - contents - off;
9334
9335
    /* Adjust addends for relocs against the toc section sym,
9336
       and optimize any accesses we can.  */
9337
0
    for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9338
0
      {
9339
0
        if (sec->reloc_count == 0
9340
0
      || discarded_section (sec))
9341
0
    continue;
9342
9343
0
        relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9344
0
                info->keep_memory);
9345
0
        if (relstart == NULL)
9346
0
    goto error_ret;
9347
9348
0
        for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9349
0
    {
9350
0
      enum elf_ppc64_reloc_type r_type;
9351
0
      unsigned long r_symndx;
9352
0
      asection *sym_sec;
9353
0
      struct elf_link_hash_entry *h;
9354
0
      bfd_vma val;
9355
9356
0
      r_type = ELF64_R_TYPE (rel->r_info);
9357
0
      switch (r_type)
9358
0
        {
9359
0
        default:
9360
0
          continue;
9361
9362
0
        case R_PPC64_TOC16:
9363
0
        case R_PPC64_TOC16_LO:
9364
0
        case R_PPC64_TOC16_HI:
9365
0
        case R_PPC64_TOC16_HA:
9366
0
        case R_PPC64_TOC16_DS:
9367
0
        case R_PPC64_TOC16_LO_DS:
9368
0
        case R_PPC64_ADDR64:
9369
0
          break;
9370
0
        }
9371
9372
0
      r_symndx = ELF64_R_SYM (rel->r_info);
9373
0
      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9374
0
          r_symndx, ibfd))
9375
0
        goto error_ret;
9376
9377
0
      if (sym_sec != toc)
9378
0
        continue;
9379
9380
0
      if (h != NULL)
9381
0
        val = h->root.u.def.value;
9382
0
      else
9383
0
        {
9384
0
          val = sym->st_value;
9385
0
          if (val != 0)
9386
0
      local_toc_syms = true;
9387
0
        }
9388
9389
0
      val += rel->r_addend;
9390
9391
0
      if (val > toc->rawsize)
9392
0
        val = toc->rawsize;
9393
0
      else if ((skip[val >> 3] & ref_from_discarded) != 0)
9394
0
        continue;
9395
0
      else if ((skip[val >> 3] & can_optimize) != 0)
9396
0
        {
9397
0
          Elf_Internal_Rela *tocrel
9398
0
      = toc_relocs + (skip[val >> 3] >> 2);
9399
0
          unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9400
9401
0
          switch (r_type)
9402
0
      {
9403
0
      case R_PPC64_TOC16_HA:
9404
0
        rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9405
0
        break;
9406
9407
0
      case R_PPC64_TOC16_LO_DS:
9408
0
        rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9409
0
        break;
9410
9411
0
      default:
9412
0
        if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9413
0
          ppc_howto_init ();
9414
0
        info->callbacks->einfo
9415
          /* xgettext:c-format */
9416
0
          (_("%H: %s references "
9417
0
             "optimized away TOC entry\n"),
9418
0
           ibfd, sec, rel->r_offset,
9419
0
           ppc64_elf_howto_table[r_type]->name);
9420
0
        bfd_set_error (bfd_error_bad_value);
9421
0
        goto error_ret;
9422
0
      }
9423
0
          rel->r_addend = tocrel->r_addend;
9424
0
          elf_section_data (sec)->relocs = relstart;
9425
0
          continue;
9426
0
        }
9427
9428
0
      if (h != NULL || sym->st_value != 0)
9429
0
        continue;
9430
9431
0
      rel->r_addend -= skip[val >> 3];
9432
0
      elf_section_data (sec)->relocs = relstart;
9433
0
    }
9434
9435
0
        if (elf_section_data (sec)->relocs != relstart)
9436
0
    free (relstart);
9437
0
      }
9438
9439
    /* We shouldn't have local or global symbols defined in the TOC,
9440
       but handle them anyway.  */
9441
0
    if (local_syms != NULL)
9442
0
      for (sym = local_syms;
9443
0
     sym < local_syms + symtab_hdr->sh_info;
9444
0
     ++sym)
9445
0
        if (sym->st_value != 0
9446
0
      && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9447
0
    {
9448
0
      unsigned long i;
9449
9450
0
      if (sym->st_value > toc->rawsize)
9451
0
        i = toc->rawsize >> 3;
9452
0
      else
9453
0
        i = sym->st_value >> 3;
9454
9455
0
      if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9456
0
        {
9457
0
          if (local_toc_syms)
9458
0
      _bfd_error_handler
9459
0
        (_("%s defined on removed toc entry"),
9460
0
         bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9461
0
          do
9462
0
      ++i;
9463
0
          while ((skip[i] & (ref_from_discarded | can_optimize)));
9464
0
          sym->st_value = (bfd_vma) i << 3;
9465
0
        }
9466
9467
0
      sym->st_value -= skip[i];
9468
0
      symtab_hdr->contents = (unsigned char *) local_syms;
9469
0
    }
9470
9471
    /* Adjust any global syms defined in this toc input section.  */
9472
0
    if (toc_inf.global_toc_syms)
9473
0
      {
9474
0
        toc_inf.toc = toc;
9475
0
        toc_inf.skip = skip;
9476
0
        toc_inf.global_toc_syms = false;
9477
0
        elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9478
0
              &toc_inf);
9479
0
      }
9480
9481
0
    if (toc->reloc_count != 0)
9482
0
      {
9483
0
        Elf_Internal_Shdr *rel_hdr;
9484
0
        Elf_Internal_Rela *wrel;
9485
0
        bfd_size_type sz;
9486
9487
        /* Remove unused toc relocs, and adjust those we keep.  */
9488
0
        if (toc_relocs == NULL)
9489
0
    toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9490
0
              info->keep_memory);
9491
0
        if (toc_relocs == NULL)
9492
0
    goto error_ret;
9493
9494
0
        wrel = toc_relocs;
9495
0
        for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9496
0
    if ((skip[rel->r_offset >> 3]
9497
0
         & (ref_from_discarded | can_optimize)) == 0)
9498
0
      {
9499
0
        wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9500
0
        wrel->r_info = rel->r_info;
9501
0
        wrel->r_addend = rel->r_addend;
9502
0
        ++wrel;
9503
0
      }
9504
0
    else if (!dec_dynrel_count (rel, toc, info,
9505
0
              &local_syms, NULL, NULL))
9506
0
      goto error_ret;
9507
9508
0
        elf_section_data (toc)->relocs = toc_relocs;
9509
0
        toc->reloc_count = wrel - toc_relocs;
9510
0
        rel_hdr = _bfd_elf_single_rel_hdr (toc);
9511
0
        sz = rel_hdr->sh_entsize;
9512
0
        rel_hdr->sh_size = toc->reloc_count * sz;
9513
0
      }
9514
0
  }
9515
0
      else if (elf_section_data (toc)->relocs != toc_relocs)
9516
0
  free (toc_relocs);
9517
9518
0
      if (local_syms != NULL
9519
0
    && symtab_hdr->contents != (unsigned char *) local_syms)
9520
0
  {
9521
0
    if (!info->keep_memory)
9522
0
      free (local_syms);
9523
0
    else
9524
0
      symtab_hdr->contents = (unsigned char *) local_syms;
9525
0
  }
9526
0
      free (skip);
9527
0
    }
9528
9529
  /* Look for cases where we can change an indirect GOT access to
9530
     a GOT relative or PC relative access, possibly reducing the
9531
     number of GOT entries.  */
9532
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9533
0
    {
9534
0
      asection *sec;
9535
0
      Elf_Internal_Shdr *symtab_hdr;
9536
0
      Elf_Internal_Sym *local_syms;
9537
0
      Elf_Internal_Rela *relstart, *rel;
9538
0
      bfd_vma got;
9539
9540
0
      if (!is_ppc64_elf (ibfd))
9541
0
  continue;
9542
9543
0
      if (!ppc64_elf_tdata (ibfd)->has_optrel)
9544
0
  continue;
9545
9546
0
      sec = ppc64_elf_tdata (ibfd)->got;
9547
0
      got = 0;
9548
0
      if (sec != NULL)
9549
0
  got = sec->output_section->vma + sec->output_offset + 0x8000;
9550
9551
0
      local_syms = NULL;
9552
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
9553
9554
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9555
0
  {
9556
0
    if (sec->reloc_count == 0
9557
0
        || !ppc64_elf_section_data (sec)->has_optrel
9558
0
        || discarded_section (sec))
9559
0
      continue;
9560
9561
0
    relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9562
0
            info->keep_memory);
9563
0
    if (relstart == NULL)
9564
0
      {
9565
0
      got_error_ret:
9566
0
        if (symtab_hdr->contents != (unsigned char *) local_syms)
9567
0
    free (local_syms);
9568
0
        if (sec != NULL
9569
0
      && elf_section_data (sec)->relocs != relstart)
9570
0
    free (relstart);
9571
0
        return false;
9572
0
      }
9573
9574
0
    for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9575
0
      {
9576
0
        enum elf_ppc64_reloc_type r_type;
9577
0
        unsigned long r_symndx;
9578
0
        Elf_Internal_Sym *sym;
9579
0
        asection *sym_sec;
9580
0
        struct elf_link_hash_entry *h;
9581
0
        struct got_entry *ent;
9582
0
        bfd_vma val, pc;
9583
0
        unsigned char buf[8];
9584
0
        unsigned int insn;
9585
0
        enum {no_check, check_lo, check_ha} insn_check;
9586
9587
0
        r_type = ELF64_R_TYPE (rel->r_info);
9588
0
        switch (r_type)
9589
0
    {
9590
0
    default:
9591
0
      insn_check = no_check;
9592
0
      break;
9593
9594
0
    case R_PPC64_PLT16_HA:
9595
0
    case R_PPC64_GOT_TLSLD16_HA:
9596
0
    case R_PPC64_GOT_TLSGD16_HA:
9597
0
    case R_PPC64_GOT_TPREL16_HA:
9598
0
    case R_PPC64_GOT_DTPREL16_HA:
9599
0
    case R_PPC64_GOT16_HA:
9600
0
    case R_PPC64_TOC16_HA:
9601
0
      insn_check = check_ha;
9602
0
      break;
9603
9604
0
    case R_PPC64_PLT16_LO:
9605
0
    case R_PPC64_PLT16_LO_DS:
9606
0
    case R_PPC64_GOT_TLSLD16_LO:
9607
0
    case R_PPC64_GOT_TLSGD16_LO:
9608
0
    case R_PPC64_GOT_TPREL16_LO_DS:
9609
0
    case R_PPC64_GOT_DTPREL16_LO_DS:
9610
0
    case R_PPC64_GOT16_LO:
9611
0
    case R_PPC64_GOT16_LO_DS:
9612
0
    case R_PPC64_TOC16_LO:
9613
0
    case R_PPC64_TOC16_LO_DS:
9614
0
      insn_check = check_lo;
9615
0
      break;
9616
0
    }
9617
9618
0
        if (insn_check != no_check)
9619
0
    {
9620
0
      bfd_vma off = rel->r_offset & ~3;
9621
9622
0
      if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9623
0
        goto got_error_ret;
9624
9625
0
      insn = bfd_get_32 (ibfd, buf);
9626
0
      if (insn_check == check_lo
9627
0
          ? !ok_lo_toc_insn (insn, r_type)
9628
0
          : ((insn & ((0x3fu << 26) | 0x1f << 16))
9629
0
       != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9630
0
        {
9631
0
          char str[12];
9632
9633
0
          ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9634
0
          sprintf (str, "%#08x", insn);
9635
0
          info->callbacks->einfo
9636
      /* xgettext:c-format */
9637
0
      (_("%H: got/toc optimization is not supported for"
9638
0
         " %s instruction\n"),
9639
0
       ibfd, sec, rel->r_offset & ~3, str);
9640
0
          continue;
9641
0
        }
9642
0
    }
9643
9644
0
        switch (r_type)
9645
0
    {
9646
    /* Note that we don't delete GOT entries for
9647
       R_PPC64_GOT16_DS since we'd need a lot more
9648
       analysis.  For starters, the preliminary layout is
9649
       before the GOT, PLT, dynamic sections and stubs are
9650
       laid out.  Then we'd need to allow for changes in
9651
       distance between sections caused by alignment.  */
9652
0
    default:
9653
0
      continue;
9654
9655
0
    case R_PPC64_GOT16_HA:
9656
0
    case R_PPC64_GOT16_LO_DS:
9657
0
    case R_PPC64_GOT_PCREL34:
9658
0
      break;
9659
0
    }
9660
9661
0
        r_symndx = ELF64_R_SYM (rel->r_info);
9662
0
        if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9663
0
            r_symndx, ibfd))
9664
0
    goto got_error_ret;
9665
9666
0
        if (sym_sec == NULL
9667
0
      || sym_sec->output_section == NULL
9668
0
      || discarded_section (sym_sec))
9669
0
    continue;
9670
9671
0
        if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9672
0
    continue;
9673
9674
0
        if (!SYMBOL_REFERENCES_LOCAL (info, h)
9675
0
      || (bfd_link_pic (info)
9676
0
          && sym_sec == bfd_abs_section_ptr))
9677
0
    continue;
9678
9679
0
        if (h != NULL)
9680
0
    val = h->root.u.def.value;
9681
0
        else
9682
0
    val = sym->st_value;
9683
0
        val += rel->r_addend;
9684
0
        val += sym_sec->output_section->vma + sym_sec->output_offset;
9685
9686
/* Fudge factor to allow for the fact that the preliminary layout
9687
   isn't exact.  Reduce limits by this factor.  */
9688
0
#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9689
9690
0
        switch (r_type)
9691
0
    {
9692
0
    default:
9693
0
      continue;
9694
9695
0
    case R_PPC64_GOT16_HA:
9696
0
      if (val - got + LIMIT_ADJUST (0x80008000ULL)
9697
0
          >= LIMIT_ADJUST (0x100000000ULL))
9698
0
        continue;
9699
9700
0
      if (!bfd_get_section_contents (ibfd, sec, buf,
9701
0
             rel->r_offset & ~3, 4))
9702
0
        goto got_error_ret;
9703
0
      insn = bfd_get_32 (ibfd, buf);
9704
0
      if (((insn & ((0x3fu << 26) | 0x1f << 16))
9705
0
           != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9706
0
        continue;
9707
0
      break;
9708
9709
0
    case R_PPC64_GOT16_LO_DS:
9710
0
      if (val - got + LIMIT_ADJUST (0x80008000ULL)
9711
0
          >= LIMIT_ADJUST (0x100000000ULL))
9712
0
        continue;
9713
0
      if (!bfd_get_section_contents (ibfd, sec, buf,
9714
0
             rel->r_offset & ~3, 4))
9715
0
        goto got_error_ret;
9716
0
      insn = bfd_get_32 (ibfd, buf);
9717
0
      if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
9718
0
        continue;
9719
0
      break;
9720
9721
0
    case R_PPC64_GOT_PCREL34:
9722
0
      pc = rel->r_offset;
9723
0
      pc += sec->output_section->vma + sec->output_offset;
9724
0
      if (val - pc + LIMIT_ADJUST (1ULL << 33)
9725
0
          >= LIMIT_ADJUST (1ULL << 34))
9726
0
        continue;
9727
0
      if (!bfd_get_section_contents (ibfd, sec, buf,
9728
0
             rel->r_offset & ~3, 8))
9729
0
        goto got_error_ret;
9730
0
      insn = bfd_get_32 (ibfd, buf);
9731
0
      if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9732
0
        continue;
9733
0
      insn = bfd_get_32 (ibfd, buf + 4);
9734
0
      if ((insn & (0x3fu << 26)) != 57u << 26)
9735
0
        continue;
9736
0
      break;
9737
0
    }
9738
0
#undef LIMIT_ADJUST
9739
9740
0
        if (h != NULL)
9741
0
    ent = h->got.glist;
9742
0
        else
9743
0
    {
9744
0
      struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9745
0
      ent = local_got_ents[r_symndx];
9746
0
    }
9747
0
        for (; ent != NULL; ent = ent->next)
9748
0
    if (ent->addend == rel->r_addend
9749
0
        && ent->owner == ibfd
9750
0
        && ent->tls_type == 0)
9751
0
      break;
9752
0
        BFD_ASSERT (ent && ent->got.refcount > 0);
9753
0
        ent->got.refcount -= 1;
9754
0
      }
9755
9756
0
    if (elf_section_data (sec)->relocs != relstart)
9757
0
      free (relstart);
9758
0
  }
9759
9760
0
      if (local_syms != NULL
9761
0
    && symtab_hdr->contents != (unsigned char *) local_syms)
9762
0
  {
9763
0
    if (!info->keep_memory)
9764
0
      free (local_syms);
9765
0
    else
9766
0
      symtab_hdr->contents = (unsigned char *) local_syms;
9767
0
  }
9768
0
    }
9769
9770
0
  return true;
9771
0
}
9772
9773
/* Return true iff input section I references the TOC using
9774
   instructions limited to +/-32k offsets.  */
9775
9776
bool
9777
ppc64_elf_has_small_toc_reloc (asection *i)
9778
0
{
9779
0
  return (is_ppc64_elf (i->owner)
9780
0
    && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9781
0
}
9782
9783
/* Allocate space for one GOT entry.  */
9784
9785
static void
9786
allocate_got (struct elf_link_hash_entry *h,
9787
        struct bfd_link_info *info,
9788
        struct got_entry *gent)
9789
0
{
9790
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
9791
0
  struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
9792
0
  int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9793
0
     ? 16 : 8);
9794
0
  int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9795
0
      ? 2 : 1) * sizeof (Elf64_External_Rela);
9796
0
  asection *got = ppc64_elf_tdata (gent->owner)->got;
9797
9798
0
  gent->got.offset = got->size;
9799
0
  got->size += entsize;
9800
9801
0
  if (h->type == STT_GNU_IFUNC)
9802
0
    {
9803
0
      htab->elf.irelplt->size += rentsize;
9804
0
      htab->got_reli_size += rentsize;
9805
0
    }
9806
0
  else if (((bfd_link_pic (info)
9807
0
       && (gent->tls_type == 0
9808
0
     ? !info->enable_dt_relr
9809
0
     : !(bfd_link_executable (info)
9810
0
         && SYMBOL_REFERENCES_LOCAL (info, h)))
9811
0
       && !bfd_is_abs_symbol (&h->root))
9812
0
      || (htab->elf.dynamic_sections_created
9813
0
    && h->dynindx != -1
9814
0
    && !SYMBOL_REFERENCES_LOCAL (info, h)))
9815
0
     && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9816
0
    {
9817
0
      asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9818
0
      relgot->size += rentsize;
9819
0
    }
9820
0
}
9821
9822
/* This function merges got entries in the same toc group.  */
9823
9824
static void
9825
merge_got_entries (struct got_entry **pent)
9826
0
{
9827
0
  struct got_entry *ent, *ent2;
9828
9829
0
  for (ent = *pent; ent != NULL; ent = ent->next)
9830
0
    if (!ent->is_indirect)
9831
0
      for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9832
0
  if (!ent2->is_indirect
9833
0
      && ent2->addend == ent->addend
9834
0
      && ent2->tls_type == ent->tls_type
9835
0
      && elf_gp (ent2->owner) == elf_gp (ent->owner))
9836
0
    {
9837
0
      ent2->is_indirect = true;
9838
0
      ent2->got.ent = ent;
9839
0
    }
9840
0
}
9841
9842
/* If H is undefined, make it dynamic if that makes sense.  */
9843
9844
static bool
9845
ensure_undef_dynamic (struct bfd_link_info *info,
9846
          struct elf_link_hash_entry *h)
9847
0
{
9848
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
9849
9850
0
  if (htab->dynamic_sections_created
9851
0
      && ((info->dynamic_undefined_weak != 0
9852
0
     && h->root.type == bfd_link_hash_undefweak)
9853
0
    || h->root.type == bfd_link_hash_undefined)
9854
0
      && h->dynindx == -1
9855
0
      && !h->forced_local
9856
0
      && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9857
0
    return bfd_elf_link_record_dynamic_symbol (info, h);
9858
0
  return true;
9859
0
}
9860
9861
/* Choose whether to use htab->iplt or htab->pltlocal rather than the
9862
   usual htab->elf.splt section for a PLT entry.  */
9863
9864
static inline
9865
bool use_local_plt (struct bfd_link_info *info,
9866
         struct elf_link_hash_entry *h)
9867
0
{
9868
0
  return (h == NULL
9869
0
    || h->dynindx == -1
9870
0
    || !elf_hash_table (info)->dynamic_sections_created);
9871
0
}
9872
9873
/* Allocate space in .plt, .got and associated reloc sections for
9874
   dynamic relocs.  */
9875
9876
static bool
9877
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9878
0
{
9879
0
  struct bfd_link_info *info;
9880
0
  struct ppc_link_hash_table *htab;
9881
0
  asection *s;
9882
0
  struct ppc_link_hash_entry *eh;
9883
0
  struct got_entry **pgent, *gent;
9884
9885
0
  if (h->root.type == bfd_link_hash_indirect)
9886
0
    return true;
9887
9888
0
  info = (struct bfd_link_info *) inf;
9889
0
  htab = ppc_hash_table (info);
9890
0
  if (htab == NULL)
9891
0
    return false;
9892
9893
0
  eh = ppc_elf_hash_entry (h);
9894
  /* Run through the TLS GD got entries first if we're changing them
9895
     to TPREL.  */
9896
0
  if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9897
0
    for (gent = h->got.glist; gent != NULL; gent = gent->next)
9898
0
      if (gent->got.refcount > 0
9899
0
    && (gent->tls_type & TLS_GD) != 0)
9900
0
  {
9901
    /* This was a GD entry that has been converted to TPREL.  If
9902
       there happens to be a TPREL entry we can use that one.  */
9903
0
    struct got_entry *ent;
9904
0
    for (ent = h->got.glist; ent != NULL; ent = ent->next)
9905
0
      if (ent->got.refcount > 0
9906
0
    && (ent->tls_type & TLS_TPREL) != 0
9907
0
    && ent->addend == gent->addend
9908
0
    && ent->owner == gent->owner)
9909
0
        {
9910
0
    gent->got.refcount = 0;
9911
0
    break;
9912
0
        }
9913
9914
    /* If not, then we'll be using our own TPREL entry.  */
9915
0
    if (gent->got.refcount != 0)
9916
0
      gent->tls_type = TLS_TLS | TLS_TPREL;
9917
0
  }
9918
9919
  /* Remove any list entry that won't generate a word in the GOT before
9920
     we call merge_got_entries.  Otherwise we risk merging to empty
9921
     entries.  */
9922
0
  pgent = &h->got.glist;
9923
0
  while ((gent = *pgent) != NULL)
9924
0
    if (gent->got.refcount > 0)
9925
0
      {
9926
0
  if ((gent->tls_type & TLS_LD) != 0
9927
0
      && SYMBOL_REFERENCES_LOCAL (info, h))
9928
0
    {
9929
0
      ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9930
0
      *pgent = gent->next;
9931
0
    }
9932
0
  else
9933
0
    pgent = &gent->next;
9934
0
      }
9935
0
    else
9936
0
      *pgent = gent->next;
9937
9938
0
  if (!htab->do_multi_toc)
9939
0
    merge_got_entries (&h->got.glist);
9940
9941
0
  for (gent = h->got.glist; gent != NULL; gent = gent->next)
9942
0
    if (!gent->is_indirect)
9943
0
      {
9944
  /* Ensure we catch all the cases where this symbol should
9945
     be made dynamic.  */
9946
0
  if (!ensure_undef_dynamic (info, h))
9947
0
    return false;
9948
9949
0
  if (!is_ppc64_elf (gent->owner))
9950
0
    abort ();
9951
9952
0
  allocate_got (h, info, gent);
9953
0
      }
9954
9955
  /* If no dynamic sections we can't have dynamic relocs, except for
9956
     IFUNCs which are handled even in static executables.  */
9957
0
  if (!htab->elf.dynamic_sections_created
9958
0
      && h->type != STT_GNU_IFUNC)
9959
0
    h->dyn_relocs = NULL;
9960
9961
  /* Discard relocs on undefined symbols that must be local.  */
9962
0
  else if (h->root.type == bfd_link_hash_undefined
9963
0
     && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9964
0
    h->dyn_relocs = NULL;
9965
9966
  /* Also discard relocs on undefined weak syms with non-default
9967
     visibility, or when dynamic_undefined_weak says so.  */
9968
0
  else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9969
0
    h->dyn_relocs = NULL;
9970
9971
0
  if (h->dyn_relocs != NULL)
9972
0
    {
9973
0
      struct ppc_dyn_relocs *p, **pp;
9974
9975
      /* In the shared -Bsymbolic case, discard space allocated for
9976
   dynamic pc-relative relocs against symbols which turn out to
9977
   be defined in regular objects.  For the normal shared case,
9978
   discard space for relocs that have become local due to symbol
9979
   visibility changes.  */
9980
0
      if (bfd_link_pic (info))
9981
0
  {
9982
    /* Relocs that use pc_count are those that appear on a call
9983
       insn, or certain REL relocs (see must_be_dyn_reloc) that
9984
       can be generated via assembly.  We want calls to
9985
       protected symbols to resolve directly to the function
9986
       rather than going via the plt.  If people want function
9987
       pointer comparisons to work as expected then they should
9988
       avoid writing weird assembly.  */
9989
0
    if (SYMBOL_CALLS_LOCAL (info, h))
9990
0
      {
9991
0
        for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
9992
0
       (p = *pp) != NULL;
9993
0
       )
9994
0
    {
9995
0
      p->count -= p->pc_count;
9996
0
      p->pc_count = 0;
9997
0
      if (p->count == 0)
9998
0
        *pp = p->next;
9999
0
      else
10000
0
        pp = &p->next;
10001
0
    }
10002
0
      }
10003
10004
0
    if (h->dyn_relocs != NULL)
10005
0
      {
10006
        /* Ensure we catch all the cases where this symbol
10007
     should be made dynamic.  */
10008
0
        if (!ensure_undef_dynamic (info, h))
10009
0
    return false;
10010
0
      }
10011
0
  }
10012
10013
      /* For a fixed position executable, discard space for
10014
   relocs against symbols which are not dynamic.  */
10015
0
      else if (h->type != STT_GNU_IFUNC)
10016
0
  {
10017
0
    if ((h->dynamic_adjusted
10018
0
         || (h->ref_regular
10019
0
       && h->root.type == bfd_link_hash_undefweak
10020
0
       && (info->dynamic_undefined_weak > 0
10021
0
           || !_bfd_elf_readonly_dynrelocs (h))))
10022
0
        && !h->def_regular
10023
0
        && !ELF_COMMON_DEF_P (h))
10024
0
      {
10025
        /* Ensure we catch all the cases where this symbol
10026
     should be made dynamic.  */
10027
0
        if (!ensure_undef_dynamic (info, h))
10028
0
    return false;
10029
10030
        /* But if that didn't work out, discard dynamic relocs.  */
10031
0
        if (h->dynindx == -1)
10032
0
    h->dyn_relocs = NULL;
10033
0
      }
10034
0
    else
10035
0
      h->dyn_relocs = NULL;
10036
0
  }
10037
10038
      /* Finally, allocate space.  */
10039
0
      for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
10040
0
  if (!discarded_section (p->sec))
10041
0
    {
10042
0
      unsigned int count;
10043
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
10044
0
      if (eh->elf.type == STT_GNU_IFUNC)
10045
0
        sreloc = htab->elf.irelplt;
10046
0
      count = p->count;
10047
0
      if (info->enable_dt_relr
10048
0
    && ((!NO_OPD_RELOCS
10049
0
         && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
10050
0
        || (eh->elf.type != STT_GNU_IFUNC
10051
0
      && SYMBOL_REFERENCES_LOCAL (info, h))))
10052
0
        count -= p->rel_count;
10053
0
      sreloc->size += count * sizeof (Elf64_External_Rela);
10054
0
    }
10055
0
    }
10056
10057
  /* We might need a PLT entry when the symbol
10058
     a) is dynamic, or
10059
     b) is an ifunc, or
10060
     c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10061
     d) has plt16 relocs and we are linking statically.  */
10062
0
  if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10063
0
      || h->type == STT_GNU_IFUNC
10064
0
      || (h->needs_plt && h->dynamic_adjusted)
10065
0
      || (h->needs_plt
10066
0
    && h->def_regular
10067
0
    && !htab->elf.dynamic_sections_created
10068
0
    && !htab->can_convert_all_inline_plt
10069
0
    && (ppc_elf_hash_entry (h)->tls_mask
10070
0
        & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10071
0
    {
10072
0
      struct plt_entry *pent;
10073
0
      bool doneone = false;
10074
0
      for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10075
0
  if (pent->plt.refcount > 0)
10076
0
    {
10077
0
      if (!ensure_undef_dynamic (info, h))
10078
0
        return false;
10079
10080
0
      if (use_local_plt (info, h))
10081
0
        {
10082
0
    if (h->type == STT_GNU_IFUNC)
10083
0
      {
10084
0
        s = htab->elf.iplt;
10085
0
        pent->plt.offset = s->size;
10086
0
        s->size += PLT_ENTRY_SIZE (htab);
10087
0
        s = htab->elf.irelplt;
10088
0
      }
10089
0
    else
10090
0
      {
10091
0
        s = htab->pltlocal;
10092
0
        pent->plt.offset = s->size;
10093
0
        s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10094
0
        s = NULL;
10095
0
        if (bfd_link_pic (info)
10096
0
      && !(info->enable_dt_relr && !htab->opd_abi))
10097
0
          s = htab->relpltlocal;
10098
0
      }
10099
0
        }
10100
0
      else
10101
0
        {
10102
    /* If this is the first .plt entry, make room for the special
10103
       first entry.  */
10104
0
    s = htab->elf.splt;
10105
0
    if (s->size == 0)
10106
0
      s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10107
10108
0
    pent->plt.offset = s->size;
10109
10110
    /* Make room for this entry.  */
10111
0
    s->size += PLT_ENTRY_SIZE (htab);
10112
10113
    /* Make room for the .glink code.  */
10114
0
    s = htab->glink;
10115
0
    if (s->size == 0)
10116
0
      s->size += GLINK_PLTRESOLVE_SIZE (htab);
10117
0
    if (htab->opd_abi)
10118
0
      {
10119
        /* We need bigger stubs past index 32767.  */
10120
0
        if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10121
0
          s->size += 4;
10122
0
        s->size += 2*4;
10123
0
      }
10124
0
    else
10125
0
      s->size += 4;
10126
10127
    /* We also need to make an entry in the .rela.plt section.  */
10128
0
    s = htab->elf.srelplt;
10129
0
        }
10130
0
      if (s != NULL)
10131
0
        s->size += sizeof (Elf64_External_Rela);
10132
0
      doneone = true;
10133
0
    }
10134
0
  else
10135
0
    pent->plt.offset = (bfd_vma) -1;
10136
0
      if (!doneone)
10137
0
  {
10138
0
    h->plt.plist = NULL;
10139
0
    h->needs_plt = 0;
10140
0
  }
10141
0
    }
10142
0
  else
10143
0
    {
10144
0
      h->plt.plist = NULL;
10145
0
      h->needs_plt = 0;
10146
0
    }
10147
10148
0
  return true;
10149
0
}
10150
10151
0
#define PPC_LO(v) ((v) & 0xffff)
10152
0
#define PPC_HI(v) (((v) >> 16) & 0xffff)
10153
0
#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10154
#define D34(v) \
10155
0
  ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10156
#define HA34(v) ((v + (1ULL << 33)) >> 34)
10157
10158
/* Called via elf_link_hash_traverse from ppc64_elf_late_size_sections
10159
   to set up space for global entry stubs.  These are put in glink,
10160
   after the branch table.  */
10161
10162
static bool
10163
size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10164
0
{
10165
0
  struct bfd_link_info *info;
10166
0
  struct ppc_link_hash_table *htab;
10167
0
  struct plt_entry *pent;
10168
0
  asection *s, *plt;
10169
10170
0
  if (h->root.type == bfd_link_hash_indirect)
10171
0
    return true;
10172
10173
0
  if (!h->pointer_equality_needed)
10174
0
    return true;
10175
10176
0
  if (h->def_regular)
10177
0
    return true;
10178
10179
0
  info = inf;
10180
0
  htab = ppc_hash_table (info);
10181
0
  if (htab == NULL)
10182
0
    return false;
10183
10184
0
  s = htab->global_entry;
10185
0
  plt = htab->elf.splt;
10186
0
  for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10187
0
    if (pent->plt.offset != (bfd_vma) -1
10188
0
  && pent->addend == 0)
10189
0
      {
10190
  /* For ELFv2, if this symbol is not defined in a regular file
10191
     and we are not generating a shared library or pie, then we
10192
     need to define the symbol in the executable on a call stub.
10193
     This is to avoid text relocations.  */
10194
0
  bfd_vma off, stub_align, stub_off, stub_size;
10195
0
  unsigned int align_power;
10196
10197
0
  stub_size = 16;
10198
0
  stub_off = s->size;
10199
0
  if (htab->params->plt_stub_align >= 0)
10200
0
    align_power = htab->params->plt_stub_align;
10201
0
  else
10202
0
    align_power = -htab->params->plt_stub_align;
10203
  /* Setting section alignment is delayed until we know it is
10204
     non-empty.  Otherwise the .text output section will be
10205
     aligned at least to plt_stub_align even when no global
10206
     entry stubs are needed.  */
10207
0
  if (!bfd_link_align_section (s, align_power))
10208
0
    return false;
10209
0
  stub_align = (bfd_vma) 1 << align_power;
10210
0
  if (htab->params->plt_stub_align >= 0
10211
0
      || ((((stub_off + stub_size - 1) & -stub_align)
10212
0
     - (stub_off & -stub_align))
10213
0
    > ((stub_size - 1) & -stub_align)))
10214
0
    stub_off = (stub_off + stub_align - 1) & -stub_align;
10215
0
  off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10216
0
  off -= stub_off + s->output_offset + s->output_section->vma;
10217
  /* Note that for --plt-stub-align negative we have a possible
10218
     dependency between stub offset and size.  Break that
10219
     dependency by assuming the max stub size when calculating
10220
     the stub offset.  */
10221
0
  if (PPC_HA (off) == 0)
10222
0
    stub_size -= 4;
10223
0
  h->root.type = bfd_link_hash_defined;
10224
0
  h->root.u.def.section = s;
10225
0
  h->root.u.def.value = stub_off;
10226
0
  s->size = stub_off + stub_size;
10227
0
  break;
10228
0
      }
10229
0
  return true;
10230
0
}
10231
10232
/* Set the sizes of the dynamic sections.  */
10233
10234
static bool
10235
ppc64_elf_late_size_sections (struct bfd_link_info *info)
10236
0
{
10237
0
  struct ppc_link_hash_table *htab;
10238
0
  bfd *dynobj;
10239
0
  asection *s;
10240
0
  bool relocs;
10241
0
  bfd *ibfd;
10242
0
  struct got_entry *first_tlsld;
10243
10244
0
  htab = ppc_hash_table (info);
10245
0
  if (htab == NULL)
10246
0
    return false;
10247
10248
0
  dynobj = htab->elf.dynobj;
10249
0
  if (dynobj == NULL)
10250
0
    return true;
10251
10252
0
  if (htab->elf.dynamic_sections_created)
10253
0
    {
10254
      /* Set the contents of the .interp section to the interpreter.  */
10255
0
      if (bfd_link_executable (info) && !info->nointerp)
10256
0
  {
10257
0
    s = htab->elf.interp;
10258
0
    if (s == NULL)
10259
0
      abort ();
10260
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10261
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10262
0
    s->alloced = 1;
10263
0
  }
10264
0
    }
10265
10266
  /* Set up .got offsets for local syms, and space for local dynamic
10267
     relocs.  */
10268
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10269
0
    {
10270
0
      struct got_entry **lgot_ents;
10271
0
      struct got_entry **end_lgot_ents;
10272
0
      struct plt_entry **local_plt;
10273
0
      struct plt_entry **end_local_plt;
10274
0
      unsigned char *lgot_masks;
10275
0
      bfd_size_type locsymcount;
10276
0
      Elf_Internal_Shdr *symtab_hdr;
10277
0
      Elf_Internal_Sym *local_syms;
10278
0
      Elf_Internal_Sym *isym;
10279
10280
0
      if (!is_ppc64_elf (ibfd))
10281
0
  continue;
10282
10283
0
      for (s = ibfd->sections; s != NULL; s = s->next)
10284
0
  {
10285
0
    struct ppc_local_dyn_relocs *p;
10286
10287
0
    for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10288
0
      {
10289
0
        if (discarded_section (p->sec))
10290
0
    {
10291
      /* Input section has been discarded, either because
10292
         it is a copy of a linkonce section or due to
10293
         linker script /DISCARD/, so we'll be discarding
10294
         the relocs too.  */
10295
0
    }
10296
0
        else if (p->count != 0)
10297
0
    {
10298
0
      unsigned int count;
10299
0
      asection *srel;
10300
10301
0
      count = p->count;
10302
0
      if (info->enable_dt_relr
10303
0
          && ((!NO_OPD_RELOCS
10304
0
         && (ppc64_elf_section_data (p->sec)->sec_type
10305
0
             == sec_opd))
10306
0
        || !p->ifunc))
10307
0
        count -= p->rel_count;
10308
0
      srel = elf_section_data (p->sec)->sreloc;
10309
0
      if (p->ifunc)
10310
0
        srel = htab->elf.irelplt;
10311
0
      srel->size += count * sizeof (Elf64_External_Rela);
10312
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10313
0
        info->flags |= DF_TEXTREL;
10314
0
    }
10315
0
      }
10316
0
  }
10317
10318
0
      lgot_ents = elf_local_got_ents (ibfd);
10319
0
      if (!lgot_ents)
10320
0
  continue;
10321
10322
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
10323
0
      locsymcount = symtab_hdr->sh_info;
10324
0
      end_lgot_ents = lgot_ents + locsymcount;
10325
0
      local_plt = (struct plt_entry **) end_lgot_ents;
10326
0
      end_local_plt = local_plt + locsymcount;
10327
0
      lgot_masks = (unsigned char *) end_local_plt;
10328
0
      local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
10329
0
      if (local_syms == NULL && locsymcount != 0)
10330
0
  {
10331
0
    local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
10332
0
               0, NULL, NULL, NULL);
10333
0
    if (local_syms == NULL)
10334
0
      return false;
10335
0
  }
10336
0
      s = ppc64_elf_tdata (ibfd)->got;
10337
0
      for (isym = local_syms;
10338
0
     lgot_ents < end_lgot_ents;
10339
0
     ++lgot_ents, ++lgot_masks, isym++)
10340
0
  {
10341
0
    struct got_entry **pent, *ent;
10342
10343
0
    pent = lgot_ents;
10344
0
    while ((ent = *pent) != NULL)
10345
0
      if (ent->got.refcount > 0)
10346
0
        {
10347
0
    if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10348
0
      {
10349
0
        ppc64_tlsld_got (ibfd)->got.refcount += 1;
10350
0
        *pent = ent->next;
10351
0
      }
10352
0
    else
10353
0
      {
10354
0
        unsigned int ent_size = 8;
10355
0
        unsigned int rel_size = sizeof (Elf64_External_Rela);
10356
10357
0
        ent->got.offset = s->size;
10358
0
        if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10359
0
          {
10360
0
      ent_size *= 2;
10361
0
      rel_size *= 2;
10362
0
          }
10363
0
        s->size += ent_size;
10364
0
        if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10365
0
          {
10366
0
      htab->elf.irelplt->size += rel_size;
10367
0
      htab->got_reli_size += rel_size;
10368
0
          }
10369
0
        else if (bfd_link_pic (info)
10370
0
           && (ent->tls_type == 0
10371
0
         ? !info->enable_dt_relr
10372
0
         : !bfd_link_executable (info))
10373
0
           && isym->st_shndx != SHN_ABS)
10374
0
          {
10375
0
      asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10376
0
      srel->size += rel_size;
10377
0
          }
10378
0
        pent = &ent->next;
10379
0
      }
10380
0
        }
10381
0
      else
10382
0
        *pent = ent->next;
10383
0
  }
10384
0
      if (local_syms != NULL
10385
0
    && symtab_hdr->contents != (unsigned char *) local_syms)
10386
0
  {
10387
0
    if (!info->keep_memory)
10388
0
      free (local_syms);
10389
0
    else
10390
0
      symtab_hdr->contents = (unsigned char *) local_syms;
10391
0
  }
10392
10393
      /* Allocate space for plt calls to local syms.  */
10394
0
      lgot_masks = (unsigned char *) end_local_plt;
10395
0
      for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10396
0
  {
10397
0
    struct plt_entry *ent;
10398
10399
0
    for (ent = *local_plt; ent != NULL; ent = ent->next)
10400
0
      if (ent->plt.refcount > 0)
10401
0
        {
10402
0
    if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10403
0
      {
10404
0
        s = htab->elf.iplt;
10405
0
        ent->plt.offset = s->size;
10406
0
        s->size += PLT_ENTRY_SIZE (htab);
10407
0
        htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10408
0
      }
10409
0
    else if (htab->can_convert_all_inline_plt
10410
0
       || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10411
0
      ent->plt.offset = (bfd_vma) -1;
10412
0
    else
10413
0
      {
10414
0
        s = htab->pltlocal;
10415
0
        ent->plt.offset = s->size;
10416
0
        s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10417
0
        if (bfd_link_pic (info)
10418
0
      && !(info->enable_dt_relr && !htab->opd_abi))
10419
0
          htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10420
0
      }
10421
0
        }
10422
0
      else
10423
0
        ent->plt.offset = (bfd_vma) -1;
10424
0
  }
10425
0
    }
10426
10427
  /* Allocate global sym .plt and .got entries, and space for global
10428
     sym dynamic relocs.  */
10429
0
  elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10430
10431
0
  if (!htab->opd_abi && !bfd_link_pic (info))
10432
0
    elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10433
10434
0
  first_tlsld = NULL;
10435
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10436
0
    {
10437
0
      struct got_entry *ent;
10438
10439
0
      if (!is_ppc64_elf (ibfd))
10440
0
  continue;
10441
10442
0
      ent = ppc64_tlsld_got (ibfd);
10443
0
      if (ent->got.refcount > 0)
10444
0
  {
10445
0
    if (!htab->do_multi_toc && first_tlsld != NULL)
10446
0
      {
10447
0
        ent->is_indirect = true;
10448
0
        ent->got.ent = first_tlsld;
10449
0
      }
10450
0
    else
10451
0
      {
10452
0
        if (first_tlsld == NULL)
10453
0
    first_tlsld = ent;
10454
0
        s = ppc64_elf_tdata (ibfd)->got;
10455
0
        ent->got.offset = s->size;
10456
0
        ent->owner = ibfd;
10457
0
        s->size += 16;
10458
0
        if (bfd_link_dll (info))
10459
0
    {
10460
0
      asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10461
0
      srel->size += sizeof (Elf64_External_Rela);
10462
0
    }
10463
0
      }
10464
0
  }
10465
0
      else
10466
0
  ent->got.offset = (bfd_vma) -1;
10467
0
    }
10468
10469
  /* We now have determined the sizes of the various dynamic sections.
10470
     Allocate memory for them.  */
10471
0
  relocs = false;
10472
0
  for (s = dynobj->sections; s != NULL; s = s->next)
10473
0
    {
10474
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
10475
0
  continue;
10476
10477
0
      if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
10478
  /* These haven't been allocated yet;  don't strip.  */
10479
0
  continue;
10480
0
      else if (s == htab->elf.sgot
10481
0
         || s == htab->elf.splt
10482
0
         || s == htab->elf.iplt
10483
0
         || s == htab->pltlocal
10484
0
         || s == htab->glink
10485
0
         || s == htab->global_entry
10486
0
         || s == htab->elf.sdynbss
10487
0
         || s == htab->elf.sdynrelro)
10488
0
  {
10489
    /* Strip this section if we don't need it; see the
10490
       comment below.  */
10491
0
  }
10492
0
      else if (s == htab->glink_eh_frame)
10493
0
  {
10494
0
    if (!bfd_is_abs_section (s->output_section))
10495
      /* Not sized yet.  */
10496
0
      continue;
10497
0
  }
10498
0
      else if (startswith (s->name, ".rela"))
10499
0
  {
10500
0
    if (s->size != 0)
10501
0
      {
10502
0
        if (s != htab->elf.srelplt)
10503
0
    relocs = true;
10504
10505
        /* We use the reloc_count field as a counter if we need
10506
     to copy relocs into the output file.  */
10507
0
        s->reloc_count = 0;
10508
0
      }
10509
0
  }
10510
0
      else
10511
0
  {
10512
    /* It's not one of our sections, so don't allocate space.  */
10513
0
    continue;
10514
0
  }
10515
10516
0
      if (s->size == 0)
10517
0
  {
10518
    /* If we don't need this section, strip it from the
10519
       output file.  This is mostly to handle .rela.bss and
10520
       .rela.plt.  We must create both sections in
10521
       create_dynamic_sections, because they must be created
10522
       before the linker maps input sections to output
10523
       sections.  The linker does that before
10524
       adjust_dynamic_symbol is called, and it is that
10525
       function which decides whether anything needs to go
10526
       into these sections.  */
10527
0
    s->flags |= SEC_EXCLUDE;
10528
0
    continue;
10529
0
  }
10530
10531
0
      if (bfd_is_abs_section (s->output_section))
10532
0
  _bfd_error_handler (_("warning: discarding dynamic section %s"),
10533
0
          s->name);
10534
10535
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
10536
0
  continue;
10537
10538
      /* Allocate memory for the section contents.  We use bfd_zalloc
10539
   here in case unused entries are not reclaimed before the
10540
   section's contents are written out.  This should not happen,
10541
   but this way if it does we get a R_PPC64_NONE reloc in .rela
10542
   sections instead of garbage.
10543
   We also rely on the section contents being zero when writing
10544
   the GOT and .dynrelro.  */
10545
0
      s->contents = bfd_zalloc (dynobj, s->size);
10546
0
      if (s->contents == NULL)
10547
0
  return false;
10548
0
      s->alloced = 1;
10549
0
    }
10550
10551
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10552
0
    {
10553
0
      if (!is_ppc64_elf (ibfd))
10554
0
  continue;
10555
10556
0
      s = ppc64_elf_tdata (ibfd)->got;
10557
0
      if (s != NULL && s != htab->elf.sgot)
10558
0
  {
10559
0
    if (s->size == 0)
10560
0
      s->flags |= SEC_EXCLUDE;
10561
0
    else
10562
0
      {
10563
0
        s->contents = bfd_zalloc (ibfd, s->size);
10564
0
        if (s->contents == NULL)
10565
0
    return false;
10566
0
        s->alloced = 1;
10567
0
      }
10568
0
  }
10569
0
      s = ppc64_elf_tdata (ibfd)->relgot;
10570
0
      if (s != NULL)
10571
0
  {
10572
0
    if (s->size == 0)
10573
0
      s->flags |= SEC_EXCLUDE;
10574
0
    else
10575
0
      {
10576
0
        s->contents = bfd_zalloc (ibfd, s->size);
10577
0
        if (s->contents == NULL)
10578
0
    return false;
10579
0
        s->alloced = 1;
10580
0
        relocs = true;
10581
0
        s->reloc_count = 0;
10582
0
      }
10583
0
  }
10584
0
    }
10585
10586
0
  if (htab->elf.dynamic_sections_created)
10587
0
    {
10588
0
      bool tls_opt;
10589
10590
      /* Add some entries to the .dynamic section.  We fill in the
10591
   values later, in ppc64_elf_finish_dynamic_sections, but we
10592
   must add the entries now so that we get the correct size for
10593
   the .dynamic section.  The DT_DEBUG entry is filled in by the
10594
   dynamic linker and used by the debugger.  */
10595
0
#define add_dynamic_entry(TAG, VAL) \
10596
0
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10597
10598
0
      if (bfd_link_executable (info))
10599
0
  {
10600
0
    if (!add_dynamic_entry (DT_DEBUG, 0))
10601
0
      return false;
10602
0
  }
10603
10604
0
      if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10605
0
  {
10606
0
    if (!add_dynamic_entry (DT_PLTGOT, 0)
10607
0
        || !add_dynamic_entry (DT_PLTRELSZ, 0)
10608
0
        || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10609
0
        || !add_dynamic_entry (DT_JMPREL, 0)
10610
0
        || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10611
0
      return false;
10612
0
  }
10613
10614
0
      if (NO_OPD_RELOCS && abiversion (info->output_bfd) <= 1)
10615
0
  {
10616
0
    if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10617
0
        || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10618
0
      return false;
10619
0
  }
10620
10621
0
      tls_opt = (htab->params->tls_get_addr_opt
10622
0
     && ((htab->tls_get_addr_fd != NULL
10623
0
          && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10624
0
         || (htab->tga_desc_fd != NULL
10625
0
       && htab->tga_desc_fd->elf.plt.plist != NULL)));
10626
0
      if (tls_opt || !htab->opd_abi)
10627
0
  {
10628
0
    if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10629
0
      return false;
10630
0
  }
10631
10632
0
      if (relocs)
10633
0
  {
10634
0
    if (!add_dynamic_entry (DT_RELA, 0)
10635
0
        || !add_dynamic_entry (DT_RELASZ, 0)
10636
0
        || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10637
0
      return false;
10638
10639
    /* If any dynamic relocs apply to a read-only section,
10640
       then we need a DT_TEXTREL entry.  */
10641
0
    if ((info->flags & DF_TEXTREL) == 0)
10642
0
      elf_link_hash_traverse (&htab->elf,
10643
0
            _bfd_elf_maybe_set_textrel, info);
10644
10645
0
    if ((info->flags & DF_TEXTREL) != 0)
10646
0
      {
10647
0
        if (!add_dynamic_entry (DT_TEXTREL, 0))
10648
0
    return false;
10649
0
      }
10650
0
  }
10651
0
    }
10652
0
#undef add_dynamic_entry
10653
10654
0
  return true;
10655
0
}
10656
10657
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
10658
10659
static bool
10660
ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10661
0
{
10662
0
  if (h->plt.plist != NULL
10663
0
      && !h->def_regular
10664
0
      && !h->pointer_equality_needed)
10665
0
    return false;
10666
10667
0
  return _bfd_elf_hash_symbol (h);
10668
0
}
10669
10670
/* Determine the type of stub needed, if any, for a call.  */
10671
10672
static inline enum ppc_stub_main_type
10673
ppc_type_of_stub (asection *input_sec,
10674
      const Elf_Internal_Rela *rel,
10675
      struct ppc_link_hash_entry **hash,
10676
      struct plt_entry **plt_ent,
10677
      bfd_vma destination,
10678
      unsigned long local_off)
10679
0
{
10680
0
  struct ppc_link_hash_entry *h = *hash;
10681
0
  bfd_vma location;
10682
0
  bfd_vma branch_offset;
10683
0
  bfd_vma max_branch_offset;
10684
0
  enum elf_ppc64_reloc_type r_type;
10685
10686
0
  if (h != NULL)
10687
0
    {
10688
0
      struct plt_entry *ent;
10689
0
      struct ppc_link_hash_entry *fdh = h;
10690
0
      if (h->oh != NULL
10691
0
    && h->oh->is_func_descriptor)
10692
0
  {
10693
0
    fdh = ppc_follow_link (h->oh);
10694
0
    *hash = fdh;
10695
0
  }
10696
10697
0
      for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10698
0
  if (ent->addend == rel->r_addend
10699
0
      && ent->plt.offset != (bfd_vma) -1)
10700
0
    {
10701
0
      *plt_ent = ent;
10702
0
      return ppc_stub_plt_call;
10703
0
    }
10704
10705
      /* Here, we know we don't have a plt entry.  If we don't have a
10706
   either a defined function descriptor or a defined entry symbol
10707
   in a regular object file, then it is pointless trying to make
10708
   any other type of stub.  */
10709
0
      if (!is_static_defined (&fdh->elf)
10710
0
    && !is_static_defined (&h->elf))
10711
0
  return ppc_stub_none;
10712
0
    }
10713
0
  else if (elf_local_got_ents (input_sec->owner) != NULL)
10714
0
    {
10715
0
      Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10716
0
      struct plt_entry **local_plt = (struct plt_entry **)
10717
0
  elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10718
0
      unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10719
10720
0
      if (local_plt[r_symndx] != NULL)
10721
0
  {
10722
0
    struct plt_entry *ent;
10723
10724
0
    for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10725
0
      if (ent->addend == rel->r_addend
10726
0
    && ent->plt.offset != (bfd_vma) -1)
10727
0
        {
10728
0
    *plt_ent = ent;
10729
0
    return ppc_stub_plt_call;
10730
0
        }
10731
0
  }
10732
0
    }
10733
10734
  /* Determine where the call point is.  */
10735
0
  location = (input_sec->output_offset
10736
0
        + input_sec->output_section->vma
10737
0
        + rel->r_offset);
10738
10739
0
  branch_offset = destination - location;
10740
0
  r_type = ELF64_R_TYPE (rel->r_info);
10741
10742
  /* Determine if a long branch stub is needed.  */
10743
0
  max_branch_offset = 1 << 25;
10744
0
  if (r_type == R_PPC64_REL14
10745
0
      || r_type == R_PPC64_REL14_BRTAKEN
10746
0
      || r_type == R_PPC64_REL14_BRNTAKEN)
10747
0
    max_branch_offset = 1 << 15;
10748
10749
0
  if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10750
    /* We need a stub.  Figure out whether a long_branch or plt_branch
10751
       is needed later.  */
10752
0
    return ppc_stub_long_branch;
10753
10754
0
  return ppc_stub_none;
10755
0
}
10756
10757
/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10758
   then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10759
   .  mflr  %r12
10760
   .  bcl 20,31,1f
10761
   .1:  mflr  %r11
10762
   .  mtlr  %r12
10763
   .  lis %r12,xxx-1b@highest
10764
   .  ori %r12,%r12,xxx-1b@higher
10765
   .  sldi  %r12,%r12,32
10766
   .  oris  %r12,%r12,xxx-1b@high
10767
   .  ori %r12,%r12,xxx-1b@l
10768
   .  add/ldx %r12,%r11,%r12  */
10769
10770
static bfd_byte *
10771
build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
10772
0
{
10773
0
  bfd_put_32 (abfd, MFLR_R12, p);
10774
0
  p += 4;
10775
0
  bfd_put_32 (abfd, BCL_20_31, p);
10776
0
  p += 4;
10777
0
  bfd_put_32 (abfd, MFLR_R11, p);
10778
0
  p += 4;
10779
0
  bfd_put_32 (abfd, MTLR_R12, p);
10780
0
  p += 4;
10781
0
  if (off + 0x8000 < 0x10000)
10782
0
    {
10783
0
      if (load)
10784
0
  bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10785
0
      else
10786
0
  bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10787
0
      p += 4;
10788
0
    }
10789
0
  else if (off + 0x80008000ULL < 0x100000000ULL)
10790
0
    {
10791
0
      bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10792
0
      p += 4;
10793
0
      if (load)
10794
0
  bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10795
0
      else
10796
0
  bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10797
0
      p += 4;
10798
0
    }
10799
0
  else
10800
0
    {
10801
0
      if (off + 0x800000000000ULL < 0x1000000000000ULL)
10802
0
  {
10803
0
    bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10804
0
    p += 4;
10805
0
  }
10806
0
      else
10807
0
  {
10808
0
    bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10809
0
    p += 4;
10810
0
    if (((off >> 32) & 0xffff) != 0)
10811
0
      {
10812
0
        bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10813
0
        p += 4;
10814
0
      }
10815
0
  }
10816
0
      if (((off >> 32) & 0xffffffffULL) != 0)
10817
0
  {
10818
0
    bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10819
0
    p += 4;
10820
0
  }
10821
0
      if (PPC_HI (off) != 0)
10822
0
  {
10823
0
    bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10824
0
    p += 4;
10825
0
  }
10826
0
      if (PPC_LO (off) != 0)
10827
0
  {
10828
0
    bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10829
0
    p += 4;
10830
0
  }
10831
0
      if (load)
10832
0
  bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10833
0
      else
10834
0
  bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10835
0
      p += 4;
10836
0
    }
10837
0
  return p;
10838
0
}
10839
10840
static unsigned int
10841
size_offset (bfd_vma off)
10842
0
{
10843
0
  unsigned int size;
10844
0
  if (off + 0x8000 < 0x10000)
10845
0
    size = 4;
10846
0
  else if (off + 0x80008000ULL < 0x100000000ULL)
10847
0
    size = 8;
10848
0
  else
10849
0
    {
10850
0
      if (off + 0x800000000000ULL < 0x1000000000000ULL)
10851
0
  size = 4;
10852
0
      else
10853
0
  {
10854
0
    size = 4;
10855
0
    if (((off >> 32) & 0xffff) != 0)
10856
0
      size += 4;
10857
0
  }
10858
0
      if (((off >> 32) & 0xffffffffULL) != 0)
10859
0
  size += 4;
10860
0
      if (PPC_HI (off) != 0)
10861
0
  size += 4;
10862
0
      if (PPC_LO (off) != 0)
10863
0
  size += 4;
10864
0
      size += 4;
10865
0
    }
10866
0
  return size + 16;
10867
0
}
10868
10869
static unsigned int
10870
num_relocs_for_offset (bfd_vma off)
10871
0
{
10872
0
  unsigned int num_rel;
10873
0
  if (off + 0x8000 < 0x10000)
10874
0
    num_rel = 1;
10875
0
  else if (off + 0x80008000ULL < 0x100000000ULL)
10876
0
    num_rel = 2;
10877
0
  else
10878
0
    {
10879
0
      num_rel = 1;
10880
0
      if (off + 0x800000000000ULL >= 0x1000000000000ULL
10881
0
    && ((off >> 32) & 0xffff) != 0)
10882
0
  num_rel += 1;
10883
0
      if (PPC_HI (off) != 0)
10884
0
  num_rel += 1;
10885
0
      if (PPC_LO (off) != 0)
10886
0
  num_rel += 1;
10887
0
    }
10888
0
  return num_rel;
10889
0
}
10890
10891
static Elf_Internal_Rela *
10892
emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10893
      bfd_vma roff, bfd_vma targ, bfd_vma off)
10894
0
{
10895
0
  bfd_vma relative_targ = targ - (roff - 8);
10896
0
  if (bfd_big_endian (info->output_bfd))
10897
0
    roff += 2;
10898
0
  r->r_offset = roff;
10899
0
  r->r_addend = relative_targ + roff;
10900
0
  if (off + 0x8000 < 0x10000)
10901
0
    r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10902
0
  else if (off + 0x80008000ULL < 0x100000000ULL)
10903
0
    {
10904
0
      r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10905
0
      ++r;
10906
0
      roff += 4;
10907
0
      r->r_offset = roff;
10908
0
      r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10909
0
      r->r_addend = relative_targ + roff;
10910
0
    }
10911
0
  else
10912
0
    {
10913
0
      if (off + 0x800000000000ULL < 0x1000000000000ULL)
10914
0
  r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10915
0
      else
10916
0
  {
10917
0
    r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10918
0
    if (((off >> 32) & 0xffff) != 0)
10919
0
      {
10920
0
        ++r;
10921
0
        roff += 4;
10922
0
        r->r_offset = roff;
10923
0
        r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10924
0
        r->r_addend = relative_targ + roff;
10925
0
      }
10926
0
  }
10927
0
      if (((off >> 32) & 0xffffffffULL) != 0)
10928
0
  roff += 4;
10929
0
      if (PPC_HI (off) != 0)
10930
0
  {
10931
0
    ++r;
10932
0
    roff += 4;
10933
0
    r->r_offset = roff;
10934
0
    r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10935
0
    r->r_addend = relative_targ + roff;
10936
0
  }
10937
0
      if (PPC_LO (off) != 0)
10938
0
  {
10939
0
    ++r;
10940
0
    roff += 4;
10941
0
    r->r_offset = roff;
10942
0
    r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10943
0
    r->r_addend = relative_targ + roff;
10944
0
  }
10945
0
    }
10946
0
  return r;
10947
0
}
10948
10949
static bfd_byte *
10950
build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10951
          bool load)
10952
0
{
10953
0
  uint64_t insn;
10954
0
  if (off - odd + (1ULL << 33) < 1ULL << 34)
10955
0
    {
10956
0
      off -= odd;
10957
0
      if (odd)
10958
0
  {
10959
0
    bfd_put_32 (abfd, NOP, p);
10960
0
    p += 4;
10961
0
  }
10962
0
      if (load)
10963
0
  insn = PLD_R12_PC;
10964
0
      else
10965
0
  insn = PADDI_R12_PC;
10966
0
      insn |= D34 (off);
10967
0
      bfd_put_32 (abfd, insn >> 32, p);
10968
0
      p += 4;
10969
0
      bfd_put_32 (abfd, insn, p);
10970
0
    }
10971
  /* The minimum value for paddi is -0x200000000.  The minimum value
10972
     for li is -0x8000, which when shifted by 34 and added gives a
10973
     minimum value of -0x2000200000000.  The maximum value is
10974
     0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1.  */
10975
0
  else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10976
0
    {
10977
0
      off -= 8 - odd;
10978
0
      bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10979
0
      p += 4;
10980
0
      if (!odd)
10981
0
  {
10982
0
    bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10983
0
    p += 4;
10984
0
  }
10985
0
      insn = PADDI_R12_PC | D34 (off);
10986
0
      bfd_put_32 (abfd, insn >> 32, p);
10987
0
      p += 4;
10988
0
      bfd_put_32 (abfd, insn, p);
10989
0
      p += 4;
10990
0
      if (odd)
10991
0
  {
10992
0
    bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10993
0
    p += 4;
10994
0
  }
10995
0
      if (load)
10996
0
  bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10997
0
      else
10998
0
  bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10999
0
    }
11000
0
  else
11001
0
    {
11002
0
      off -= odd + 8;
11003
0
      bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
11004
0
      p += 4;
11005
0
      bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
11006
0
      p += 4;
11007
0
      if (odd)
11008
0
  {
11009
0
    bfd_put_32 (abfd, SLDI_R11_R11_34, p);
11010
0
    p += 4;
11011
0
  }
11012
0
      insn = PADDI_R12_PC | D34 (off);
11013
0
      bfd_put_32 (abfd, insn >> 32, p);
11014
0
      p += 4;
11015
0
      bfd_put_32 (abfd, insn, p);
11016
0
      p += 4;
11017
0
      if (!odd)
11018
0
  {
11019
0
    bfd_put_32 (abfd, SLDI_R11_R11_34, p);
11020
0
    p += 4;
11021
0
  }
11022
0
      if (load)
11023
0
  bfd_put_32 (abfd, LDX_R12_R11_R12, p);
11024
0
      else
11025
0
  bfd_put_32 (abfd, ADD_R12_R11_R12, p);
11026
0
    }
11027
0
  p += 4;
11028
0
  return p;
11029
0
}
11030
11031
static unsigned int
11032
size_power10_offset (bfd_vma off, int odd)
11033
0
{
11034
0
  if (off - odd + (1ULL << 33) < 1ULL << 34)
11035
0
    return odd + 8;
11036
0
  else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11037
0
    return 20;
11038
0
  else
11039
0
    return 24;
11040
0
}
11041
11042
static unsigned int
11043
num_relocs_for_power10_offset (bfd_vma off, int odd)
11044
0
{
11045
0
  if (off - odd + (1ULL << 33) < 1ULL << 34)
11046
0
    return 1;
11047
0
  else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11048
0
    return 2;
11049
0
  else
11050
0
    return 3;
11051
0
}
11052
11053
static Elf_Internal_Rela *
11054
emit_relocs_for_power10_offset (struct bfd_link_info *info,
11055
        Elf_Internal_Rela *r, bfd_vma roff,
11056
        bfd_vma targ, bfd_vma off, int odd)
11057
0
{
11058
0
  if (off - odd + (1ULL << 33) < 1ULL << 34)
11059
0
    roff += odd;
11060
0
  else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11061
0
    {
11062
0
      int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11063
0
      r->r_offset = roff + d_offset;
11064
0
      r->r_addend = targ + 8 - odd - d_offset;
11065
0
      r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11066
0
      ++r;
11067
0
      roff += 8 - odd;
11068
0
    }
11069
0
  else
11070
0
    {
11071
0
      int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11072
0
      r->r_offset = roff + d_offset;
11073
0
      r->r_addend = targ + 8 + odd - d_offset;
11074
0
      r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
11075
0
      ++r;
11076
0
      roff += 4;
11077
0
      r->r_offset = roff + d_offset;
11078
0
      r->r_addend = targ + 4 + odd - d_offset;
11079
0
      r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11080
0
      ++r;
11081
0
      roff += 4 + odd;
11082
0
    }
11083
0
  r->r_offset = roff;
11084
0
  r->r_addend = targ;
11085
0
  r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
11086
0
  return r;
11087
0
}
11088
11089
/* Emit .eh_frame opcode to advance pc by DELTA.  */
11090
11091
static bfd_byte *
11092
eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
11093
0
{
11094
0
  delta /= 4;
11095
0
  if (delta < 64)
11096
0
    *eh++ = DW_CFA_advance_loc + delta;
11097
0
  else if (delta < 256)
11098
0
    {
11099
0
      *eh++ = DW_CFA_advance_loc1;
11100
0
      *eh++ = delta;
11101
0
    }
11102
0
  else if (delta < 65536)
11103
0
    {
11104
0
      *eh++ = DW_CFA_advance_loc2;
11105
0
      bfd_put_16 (abfd, delta, eh);
11106
0
      eh += 2;
11107
0
    }
11108
0
  else
11109
0
    {
11110
0
      *eh++ = DW_CFA_advance_loc4;
11111
0
      bfd_put_32 (abfd, delta, eh);
11112
0
      eh += 4;
11113
0
    }
11114
0
  return eh;
11115
0
}
11116
11117
/* Size of required .eh_frame opcode to advance pc by DELTA.  */
11118
11119
static unsigned int
11120
eh_advance_size (unsigned int delta)
11121
0
{
11122
0
  if (delta < 64 * 4)
11123
    /* DW_CFA_advance_loc+[1..63].  */
11124
0
    return 1;
11125
0
  if (delta < 256 * 4)
11126
    /* DW_CFA_advance_loc1, byte.  */
11127
0
    return 2;
11128
0
  if (delta < 65536 * 4)
11129
    /* DW_CFA_advance_loc2, 2 bytes.  */
11130
0
    return 3;
11131
  /* DW_CFA_advance_loc4, 4 bytes.  */
11132
0
  return 5;
11133
0
}
11134
11135
/* With power7 weakly ordered memory model, it is possible for ld.so
11136
   to update a plt entry in one thread and have another thread see a
11137
   stale zero toc entry.  To avoid this we need some sort of acquire
11138
   barrier in the call stub.  One solution is to make the load of the
11139
   toc word seem to appear to depend on the load of the function entry
11140
   word.  Another solution is to test for r2 being zero, and branch to
11141
   the appropriate glink entry if so.
11142
11143
   .  fake dep barrier  compare
11144
   .  ld 12,xxx(2)    ld 12,xxx(2)
11145
   .  mtctr 12    mtctr 12
11146
   .  xor 11,12,12    ld 2,xxx+8(2)
11147
   .  add 2,2,11    cmpldi 2,0
11148
   .  ld 2,xxx+8(2)   bnectr+
11149
   .  bctr      b <glink_entry>
11150
11151
   The solution involving the compare turns out to be faster, so
11152
   that's what we use unless the branch won't reach.  */
11153
11154
0
#define ALWAYS_USE_FAKE_DEP 0
11155
0
#define ALWAYS_EMIT_R2SAVE 0
11156
11157
static inline unsigned int
11158
plt_stub_size (struct ppc_link_hash_table *htab,
11159
         struct ppc_stub_hash_entry *stub_entry,
11160
         bfd_vma off,
11161
         unsigned int odd)
11162
0
{
11163
0
  unsigned size;
11164
11165
0
  if (stub_entry->type.sub == ppc_stub_notoc)
11166
0
    {
11167
0
      size = 8 + size_power10_offset (off, odd);
11168
0
      if (stub_entry->type.r2save)
11169
0
  size += 4;
11170
0
    }
11171
0
  else if (stub_entry->type.sub == ppc_stub_p9notoc)
11172
0
    {
11173
0
      size = 8 + size_offset (off - 8);
11174
0
      if (stub_entry->type.r2save)
11175
0
  size += 4;
11176
0
    }
11177
0
  else
11178
0
    {
11179
0
      size = 12;
11180
0
      if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11181
0
  size += 4;
11182
0
      if (PPC_HA (off) != 0)
11183
0
  size += 4;
11184
0
      if (htab->opd_abi)
11185
0
  {
11186
0
    size += 4;
11187
0
    if (htab->params->plt_static_chain)
11188
0
      size += 4;
11189
0
    if (htab->params->plt_thread_safe
11190
0
        && htab->elf.dynamic_sections_created
11191
0
        && stub_entry->h != NULL
11192
0
        && stub_entry->h->elf.dynindx != -1)
11193
0
      size += 8;
11194
0
    if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11195
0
        != PPC_HA (off))
11196
0
      size += 4;
11197
0
  }
11198
0
    }
11199
0
  if (stub_entry->h != NULL
11200
0
      && is_tls_get_addr (&stub_entry->h->elf, htab)
11201
0
      && htab->params->tls_get_addr_opt)
11202
0
    {
11203
0
      if (!htab->params->no_tls_get_addr_regsave)
11204
0
  {
11205
0
    size += 30 * 4;
11206
0
    if (stub_entry->type.r2save)
11207
0
      size += 4;
11208
0
  }
11209
0
      else
11210
0
  {
11211
0
    size += 7 * 4;
11212
0
    if (stub_entry->type.r2save)
11213
0
      size += 6 * 4;
11214
0
  }
11215
0
    }
11216
0
  return size;
11217
0
}
11218
11219
/* Depending on the sign of plt_stub_align:
11220
   If positive, return the padding to align to a 2**plt_stub_align
11221
   boundary.
11222
   If negative, if this stub would cross fewer 2**plt_stub_align
11223
   boundaries if we align, then return the padding needed to do so.  */
11224
11225
static inline unsigned int
11226
plt_stub_pad (int plt_stub_align,
11227
        bfd_vma stub_off,
11228
        unsigned int stub_size)
11229
0
{
11230
0
  unsigned int stub_align;
11231
11232
0
  if (plt_stub_align >= 0)
11233
0
    stub_align = 1u << plt_stub_align;
11234
0
  else
11235
0
    {
11236
0
      stub_align = 1u << -plt_stub_align;
11237
0
      if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11238
0
    <= ((stub_size - 1) & -stub_align))
11239
0
  return 0;
11240
0
    }
11241
0
  return stub_align - 1 - ((stub_off - 1) & (stub_align - 1));
11242
0
}
11243
11244
/* Build a toc using .plt call stub.  */
11245
11246
static inline bfd_byte *
11247
build_plt_stub (struct ppc_link_hash_table *htab,
11248
    struct ppc_stub_hash_entry *stub_entry,
11249
    bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11250
0
{
11251
0
  bfd *obfd = htab->params->stub_bfd;
11252
0
  bool plt_load_toc = htab->opd_abi;
11253
0
  bool plt_static_chain = htab->params->plt_static_chain;
11254
0
  bool plt_thread_safe = (htab->params->plt_thread_safe
11255
0
        && htab->elf.dynamic_sections_created
11256
0
        && stub_entry->h != NULL
11257
0
        && stub_entry->h->elf.dynindx != -1);
11258
0
  bool use_fake_dep = plt_thread_safe;
11259
0
  bfd_vma cmp_branch_off = 0;
11260
11261
0
  if (!ALWAYS_USE_FAKE_DEP
11262
0
      && plt_load_toc
11263
0
      && plt_thread_safe
11264
0
      && !(stub_entry->h != NULL
11265
0
     && is_tls_get_addr (&stub_entry->h->elf, htab)
11266
0
     && htab->params->tls_get_addr_opt))
11267
0
    {
11268
0
      bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11269
0
      bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11270
0
        / PLT_ENTRY_SIZE (htab));
11271
0
      bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11272
0
      bfd_vma to, from;
11273
11274
0
      if (pltindex > 32768)
11275
0
  glinkoff += (pltindex - 32768) * 4;
11276
0
      to = (glinkoff
11277
0
      + htab->glink->output_offset
11278
0
      + htab->glink->output_section->vma);
11279
0
      from = (p - stub_entry->group->stub_sec->contents
11280
0
        + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11281
0
        + 4 * (PPC_HA (offset) != 0)
11282
0
        + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11283
0
         != PPC_HA (offset))
11284
0
        + 4 * (plt_static_chain != 0)
11285
0
        + 20
11286
0
        + stub_entry->group->stub_sec->output_offset
11287
0
        + stub_entry->group->stub_sec->output_section->vma);
11288
0
      cmp_branch_off = to - from;
11289
0
      use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11290
0
    }
11291
11292
0
  if (PPC_HA (offset) != 0)
11293
0
    {
11294
0
      if (r != NULL)
11295
0
  {
11296
0
    if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11297
0
      r[0].r_offset += 4;
11298
0
    r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11299
0
    r[1].r_offset = r[0].r_offset + 4;
11300
0
    r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11301
0
    r[1].r_addend = r[0].r_addend;
11302
0
    if (plt_load_toc)
11303
0
      {
11304
0
        if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11305
0
    {
11306
0
      r[2].r_offset = r[1].r_offset + 4;
11307
0
      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11308
0
      r[2].r_addend = r[0].r_addend;
11309
0
    }
11310
0
        else
11311
0
    {
11312
0
      r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11313
0
      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11314
0
      r[2].r_addend = r[0].r_addend + 8;
11315
0
      if (plt_static_chain)
11316
0
        {
11317
0
          r[3].r_offset = r[2].r_offset + 4;
11318
0
          r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11319
0
          r[3].r_addend = r[0].r_addend + 16;
11320
0
        }
11321
0
    }
11322
0
      }
11323
0
  }
11324
0
      if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11325
0
  bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),  p += 4;
11326
0
      if (plt_load_toc)
11327
0
  {
11328
0
    bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11329
0
    bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
11330
0
  }
11331
0
      else
11332
0
  {
11333
0
    bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11334
0
    bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
11335
0
  }
11336
0
      if (plt_load_toc
11337
0
    && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11338
0
  {
11339
0
    bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11340
0
    offset = 0;
11341
0
  }
11342
0
      bfd_put_32 (obfd, MTCTR_R12, p),        p += 4;
11343
0
      if (plt_load_toc)
11344
0
  {
11345
0
    if (use_fake_dep)
11346
0
      {
11347
0
        bfd_put_32 (obfd, XOR_R2_R12_R12, p),   p += 4;
11348
0
        bfd_put_32 (obfd, ADD_R11_R11_R2, p),   p += 4;
11349
0
      }
11350
0
    bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11351
0
    if (plt_static_chain)
11352
0
      bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11353
0
  }
11354
0
    }
11355
0
  else
11356
0
    {
11357
0
      if (r != NULL)
11358
0
  {
11359
0
    if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11360
0
      r[0].r_offset += 4;
11361
0
    r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11362
0
    if (plt_load_toc)
11363
0
      {
11364
0
        if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11365
0
    {
11366
0
      r[1].r_offset = r[0].r_offset + 4;
11367
0
      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11368
0
      r[1].r_addend = r[0].r_addend;
11369
0
    }
11370
0
        else
11371
0
    {
11372
0
      r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11373
0
      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11374
0
      r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11375
0
      if (plt_static_chain)
11376
0
        {
11377
0
          r[2].r_offset = r[1].r_offset + 4;
11378
0
          r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11379
0
          r[2].r_addend = r[0].r_addend + 8;
11380
0
        }
11381
0
    }
11382
0
      }
11383
0
  }
11384
0
      if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11385
0
  bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),  p += 4;
11386
0
      bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11387
0
      if (plt_load_toc
11388
0
    && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11389
0
  {
11390
0
    bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11391
0
    offset = 0;
11392
0
  }
11393
0
      bfd_put_32 (obfd, MTCTR_R12, p),        p += 4;
11394
0
      if (plt_load_toc)
11395
0
  {
11396
0
    if (use_fake_dep)
11397
0
      {
11398
0
        bfd_put_32 (obfd, XOR_R11_R12_R12, p),    p += 4;
11399
0
        bfd_put_32 (obfd, ADD_R2_R2_R11, p),    p += 4;
11400
0
      }
11401
0
    if (plt_static_chain)
11402
0
      bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11403
0
    bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11404
0
  }
11405
0
    }
11406
0
  if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11407
0
    {
11408
0
      bfd_put_32 (obfd, CMPLDI_R2_0, p),      p += 4;
11409
0
      bfd_put_32 (obfd, BNECTR_P4, p),        p += 4;
11410
0
      bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11411
0
    }
11412
0
  else
11413
0
    bfd_put_32 (obfd, BCTR, p),         p += 4;
11414
0
  return p;
11415
0
}
11416
11417
/* Build a special .plt call stub for __tls_get_addr.  */
11418
11419
#define LD_R0_0R3 0xe8030000
11420
#define LD_R12_0R3  0xe9830000
11421
#define MR_R0_R3  0x7c601b78
11422
#define CMPDI_R0_0  0x2c200000
11423
#define ADD_R3_R12_R13  0x7c6c6a14
11424
#define BEQLR   0x4d820020
11425
#define MR_R3_R0  0x7c030378
11426
#define BCTRL   0x4e800421
11427
11428
static bfd_byte *
11429
build_tls_get_addr_head (struct ppc_link_hash_table *htab,
11430
       struct ppc_stub_hash_entry *stub_entry,
11431
       bfd_byte *p)
11432
0
{
11433
0
  bfd *obfd = htab->params->stub_bfd;
11434
11435
0
  bfd_put_32 (obfd, LD_R0_0R3 + 0, p),    p += 4;
11436
0
  bfd_put_32 (obfd, LD_R12_0R3 + 8, p),   p += 4;
11437
0
  bfd_put_32 (obfd, CMPDI_R0_0, p),   p += 4;
11438
0
  bfd_put_32 (obfd, MR_R0_R3, p),   p += 4;
11439
0
  bfd_put_32 (obfd, ADD_R3_R12_R13, p),   p += 4;
11440
0
  bfd_put_32 (obfd, BEQLR, p),      p += 4;
11441
0
  bfd_put_32 (obfd, MR_R3_R0, p),   p += 4;
11442
11443
0
  if (!htab->params->no_tls_get_addr_regsave)
11444
0
    p = tls_get_addr_prologue (obfd, p, htab);
11445
0
  else if (stub_entry->type.r2save)
11446
0
    {
11447
0
      bfd_put_32 (obfd, MFLR_R0, p);
11448
0
      p += 4;
11449
0
      bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11450
0
      p += 4;
11451
0
    }
11452
0
  return p;
11453
0
}
11454
11455
static bfd_byte *
11456
build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11457
       struct ppc_stub_hash_entry *stub_entry,
11458
       bfd_byte *p,
11459
       bfd_byte *loc)
11460
0
{
11461
0
  bfd *obfd = htab->params->stub_bfd;
11462
11463
0
  if (!htab->params->no_tls_get_addr_regsave)
11464
0
    {
11465
0
      bfd_put_32 (obfd, BCTRL, p - 4);
11466
11467
0
      if (stub_entry->type.r2save)
11468
0
  {
11469
0
    bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11470
0
    p += 4;
11471
0
  }
11472
0
      p = tls_get_addr_epilogue (obfd, p, htab);
11473
0
    }
11474
0
  else if (stub_entry->type.r2save)
11475
0
    {
11476
0
      bfd_put_32 (obfd, BCTRL, p - 4);
11477
11478
0
      bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11479
0
      p += 4;
11480
0
      bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11481
0
      p += 4;
11482
0
      bfd_put_32 (obfd, MTLR_R0, p);
11483
0
      p += 4;
11484
0
      bfd_put_32 (obfd, BLR, p);
11485
0
      p += 4;
11486
0
    }
11487
11488
0
  if (htab->glink_eh_frame != NULL
11489
0
      && htab->glink_eh_frame->size != 0)
11490
0
    {
11491
0
      bfd_byte *base, *eh;
11492
11493
0
      base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11494
0
      eh = base + stub_entry->group->eh_size;
11495
11496
0
      if (!htab->params->no_tls_get_addr_regsave)
11497
0
  {
11498
0
    unsigned int cfa_updt, delta, i;
11499
11500
    /* After the bctrl, lr has been modified so we need to emit
11501
       .eh_frame info saying the return address is on the stack.  In
11502
       fact we must put the EH info at or before the call rather
11503
       than after it, because the EH info for a call needs to be
11504
       specified by that point.
11505
       See libgcc/unwind-dw2.c execute_cfa_program.
11506
       Any stack pointer update must be described immediately after
11507
       the instruction making the change, and since the stdu occurs
11508
       after saving regs we put all the reg saves and the cfa
11509
       change there.  */
11510
0
    cfa_updt = stub_entry->stub_offset + 18 * 4;
11511
0
    delta = cfa_updt - stub_entry->group->lr_restore;
11512
0
    stub_entry->group->lr_restore
11513
0
      = stub_entry->stub_offset + (p - loc) - 4;
11514
0
    eh = eh_advance (htab->elf.dynobj, eh, delta);
11515
0
    *eh++ = DW_CFA_def_cfa_offset;
11516
0
    if (htab->opd_abi)
11517
0
      {
11518
0
        *eh++ = 128;
11519
0
        *eh++ = 1;
11520
0
      }
11521
0
    else
11522
0
      *eh++ = 96;
11523
0
    *eh++ = DW_CFA_offset_extended_sf;
11524
0
    *eh++ = 65;
11525
0
    *eh++ = (-16 / 8) & 0x7f;
11526
0
    for (i = 4; i < 12; i++)
11527
0
      {
11528
0
        *eh++ = DW_CFA_offset + i;
11529
0
        *eh++ = (htab->opd_abi ? 13 : 12) - i;
11530
0
      }
11531
0
    *eh++ = (DW_CFA_advance_loc
11532
0
       + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11533
0
    *eh++ = DW_CFA_def_cfa_offset;
11534
0
    *eh++ = 0;
11535
0
    for (i = 4; i < 12; i++)
11536
0
      *eh++ = DW_CFA_restore + i;
11537
0
    *eh++ = DW_CFA_advance_loc + 2;
11538
0
    *eh++ = DW_CFA_restore_extended;
11539
0
    *eh++ = 65;
11540
0
    stub_entry->group->eh_size = eh - base;
11541
0
  }
11542
0
      else if (stub_entry->type.r2save)
11543
0
  {
11544
0
    unsigned int lr_used, delta;
11545
11546
0
    lr_used = stub_entry->stub_offset + (p - 20 - loc);
11547
0
    delta = lr_used - stub_entry->group->lr_restore;
11548
0
    stub_entry->group->lr_restore = lr_used + 16;
11549
0
    eh = eh_advance (htab->elf.dynobj, eh, delta);
11550
0
    *eh++ = DW_CFA_offset_extended_sf;
11551
0
    *eh++ = 65;
11552
0
    *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11553
0
    *eh++ = DW_CFA_advance_loc + 4;
11554
0
    *eh++ = DW_CFA_restore_extended;
11555
0
    *eh++ = 65;
11556
0
    stub_entry->group->eh_size = eh - base;
11557
0
  }
11558
0
    }
11559
0
  return p;
11560
0
}
11561
11562
static Elf_Internal_Rela *
11563
get_relocs (asection *sec, int count)
11564
0
{
11565
0
  Elf_Internal_Rela *relocs;
11566
0
  struct bfd_elf_section_data *elfsec_data;
11567
11568
0
  elfsec_data = elf_section_data (sec);
11569
0
  relocs = elfsec_data->relocs;
11570
0
  if (relocs == NULL)
11571
0
    {
11572
0
      bfd_size_type relsize;
11573
0
      relsize = sec->reloc_count * sizeof (*relocs);
11574
0
      relocs = bfd_malloc (relsize);
11575
0
      if (relocs == NULL)
11576
0
  return NULL;
11577
0
      elfsec_data->relocs = relocs;
11578
0
      elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11579
0
            sizeof (Elf_Internal_Shdr));
11580
0
      if (elfsec_data->rela.hdr == NULL)
11581
0
  return NULL;
11582
0
      elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11583
0
          * sizeof (Elf64_External_Rela));
11584
0
      elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11585
0
      sec->reloc_count = 0;
11586
0
    }
11587
0
  relocs += sec->reloc_count;
11588
0
  sec->reloc_count += count;
11589
0
  return relocs;
11590
0
}
11591
11592
static bool
11593
swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, bfd_byte *loc, asection *s)
11594
0
{
11595
0
  if ((size_t) (loc - s->contents) >= s->size)
11596
0
    return false;
11597
0
  bfd_elf64_swap_reloca_out (obfd, rel, loc);
11598
0
  return true;
11599
0
}
11600
11601
static bool
11602
count_and_swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, asection *s)
11603
0
{
11604
0
  bfd_byte *loc = s->contents;
11605
0
  loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
11606
0
  return swap_reloc_out (obfd, rel, loc, s);
11607
0
}
11608
11609
11610
/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11611
   forms, to the equivalent relocs against the global symbol given by
11612
   STUB_ENTRY->H.  */
11613
11614
static bool
11615
use_global_in_relocs (struct ppc_link_hash_table *htab,
11616
          struct ppc_stub_hash_entry *stub_entry,
11617
          Elf_Internal_Rela *r, unsigned int num_rel)
11618
0
{
11619
0
  struct elf_link_hash_entry **hashes;
11620
0
  unsigned long symndx;
11621
0
  struct ppc_link_hash_entry *h;
11622
0
  bfd_vma symval;
11623
11624
  /* Relocs are always against symbols in their own object file.  Fake
11625
     up global sym hashes for the stub bfd (which has no symbols).  */
11626
0
  hashes = elf_sym_hashes (htab->params->stub_bfd);
11627
0
  if (hashes == NULL)
11628
0
    {
11629
0
      bfd_size_type hsize;
11630
11631
      /* When called the first time, stub_globals will contain the
11632
   total number of symbols seen during stub sizing.  After
11633
   allocating, stub_globals is used as an index to fill the
11634
   hashes array.  */
11635
0
      hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11636
0
      hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11637
0
      if (hashes == NULL)
11638
0
  return false;
11639
0
      elf_sym_hashes (htab->params->stub_bfd) = hashes;
11640
0
      Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (htab->params->stub_bfd);
11641
0
      symtab_hdr->sh_entsize = sizeof (Elf64_External_Sym);
11642
0
      symtab_hdr->sh_size = (htab->stub_globals + 1) * symtab_hdr->sh_entsize;
11643
0
      htab->stub_globals = 1;
11644
0
    }
11645
0
  symndx = htab->stub_globals++;
11646
0
  h = stub_entry->h;
11647
0
  hashes[symndx] = &h->elf;
11648
0
  if (h->oh != NULL && h->oh->is_func)
11649
0
    h = ppc_follow_link (h->oh);
11650
0
  BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11651
0
        || h->elf.root.type == bfd_link_hash_defweak);
11652
0
  symval = defined_sym_val (&h->elf);
11653
0
  while (num_rel-- != 0)
11654
0
    {
11655
0
      r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11656
0
      if (h->elf.root.u.def.section != stub_entry->target_section)
11657
0
  {
11658
    /* H is an opd symbol.  The addend must be zero, and the
11659
       branch reloc is the only one we can convert.  */
11660
0
    r->r_addend = 0;
11661
0
    break;
11662
0
  }
11663
0
      else
11664
0
  r->r_addend -= symval;
11665
0
      --r;
11666
0
    }
11667
0
  return true;
11668
0
}
11669
11670
static bfd_vma
11671
get_r2off (struct bfd_link_info *info,
11672
     struct ppc_stub_hash_entry *stub_entry)
11673
0
{
11674
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
11675
0
  bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11676
11677
0
  if (r2off == 0)
11678
0
    {
11679
      /* Support linking -R objects.  Get the toc pointer from the
11680
   opd entry.  */
11681
0
      char buf[8];
11682
0
      if (!htab->opd_abi)
11683
0
  return r2off;
11684
0
      asection *opd = stub_entry->h->elf.root.u.def.section;
11685
0
      bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11686
11687
0
      if (strcmp (opd->name, ".opd") != 0
11688
0
    || opd->reloc_count != 0)
11689
0
  {
11690
0
    info->callbacks->einfo
11691
0
      (_("%P: cannot find opd entry toc for `%pT'\n"),
11692
0
       stub_entry->h->elf.root.root.string);
11693
0
    bfd_set_error (bfd_error_bad_value);
11694
0
    return (bfd_vma) -1;
11695
0
  }
11696
0
      if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11697
0
  return (bfd_vma) -1;
11698
0
      r2off = bfd_get_64 (opd->owner, buf);
11699
0
      r2off -= elf_gp (info->output_bfd);
11700
0
    }
11701
0
  r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11702
0
  return r2off;
11703
0
}
11704
11705
/* Debug dump.  */
11706
11707
static void
11708
dump_stub (const char *header,
11709
     struct ppc_stub_hash_entry *stub_entry,
11710
     size_t end_offset)
11711
0
{
11712
0
  const char *t1, *t2, *t3;
11713
0
  switch (stub_entry->type.main)
11714
0
    {
11715
0
    case ppc_stub_none:   t1 = "none";    break;
11716
0
    case ppc_stub_long_branch:  t1 = "long_branch"; break;
11717
0
    case ppc_stub_plt_branch: t1 = "plt_branch";  break;
11718
0
    case ppc_stub_plt_call: t1 = "plt_call";  break;
11719
0
    case ppc_stub_global_entry: t1 = "global_entry";  break;
11720
0
    case ppc_stub_save_res: t1 = "save_res";  break;
11721
0
    default:      t1 = "???";   break;
11722
0
    }
11723
0
  switch (stub_entry->type.sub)
11724
0
    {
11725
0
    case ppc_stub_toc:    t2 = "toc";   break;
11726
0
    case ppc_stub_notoc:  t2 = "notoc";   break;
11727
0
    case ppc_stub_p9notoc:  t2 = "p9notoc";   break;
11728
0
    default:      t2 = "???";   break;
11729
0
    }
11730
0
  t3 = stub_entry->type.r2save ? "r2save" : "";
11731
0
  fprintf (stderr, "%s id = %u type = %s:%s:%s\n",
11732
0
     header, stub_entry->id, t1, t2, t3);
11733
0
  fprintf (stderr, "name = %s\n", stub_entry->root.string);
11734
0
  fprintf (stderr, "offset = 0x%" PRIx64 ":", stub_entry->stub_offset);
11735
0
  for (size_t i = stub_entry->stub_offset; i < end_offset; i += 4)
11736
0
    {
11737
0
      asection *stub_sec = stub_entry->group->stub_sec;
11738
0
      uint32_t *p = (uint32_t *) (stub_sec->contents + i);
11739
0
      fprintf (stderr, " %08x", (uint32_t) bfd_get_32 (stub_sec->owner, p));
11740
0
    }
11741
0
  fprintf (stderr, "\n");
11742
0
}
11743
11744
static bool
11745
ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11746
0
{
11747
0
  struct ppc_stub_hash_entry *stub_entry;
11748
0
  struct ppc_branch_hash_entry *br_entry;
11749
0
  struct bfd_link_info *info;
11750
0
  struct ppc_link_hash_table *htab;
11751
0
  bfd *obfd;
11752
0
  bfd_byte *loc;
11753
0
  bfd_byte *p, *relp;
11754
0
  bfd_vma targ, off;
11755
0
  Elf_Internal_Rela *r;
11756
0
  asection *plt;
11757
0
  int num_rel;
11758
0
  int odd;
11759
0
  bool is_tga;
11760
11761
  /* Massage our args to the form they really have.  */
11762
0
  stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11763
0
  info = in_arg;
11764
11765
0
  htab = ppc_hash_table (info);
11766
0
  if (htab == NULL)
11767
0
    return false;
11768
11769
0
  struct _ppc64_elf_section_data *esd
11770
0
    = ppc64_elf_section_data (stub_entry->group->stub_sec);
11771
0
  ++htab->stub_id;
11772
0
  if (stub_entry->id != htab->stub_id
11773
0
      || (stub_entry->type.main != ppc_stub_save_res
11774
0
    && stub_entry->stub_offset < stub_entry->group->stub_sec->size))
11775
0
    {
11776
0
      BFD_ASSERT (0);
11777
0
      if (stub_entry->id != htab->stub_id)
11778
0
  fprintf (stderr, "Expected id %u, got %u\n",
11779
0
     htab->stub_id, stub_entry->id);
11780
0
      if (stub_entry->stub_offset < stub_entry->group->stub_sec->size)
11781
0
  fprintf (stderr, "Expected offset >= %" PRIx64 ", got %"
11782
0
     PRIx64 "\n", stub_entry->group->stub_sec->size,
11783
0
     stub_entry->stub_offset);
11784
0
      if (esd->sec_type == sec_stub)
11785
0
  dump_stub ("Previous:", esd->u.last_ent, stub_entry->stub_offset);
11786
0
      dump_stub ("Current:", stub_entry, 0);
11787
0
    }
11788
0
  if (esd->sec_type == sec_normal)
11789
0
    esd->sec_type = sec_stub;
11790
0
  if (esd->sec_type == sec_stub)
11791
0
    esd->u.last_ent = stub_entry;
11792
0
  loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11793
11794
0
  htab->stub_count[stub_entry->type.main - 1] += 1;
11795
0
  if (stub_entry->type.main == ppc_stub_long_branch
11796
0
      && stub_entry->type.sub == ppc_stub_toc)
11797
0
    {
11798
      /* Branches are relative.  This is where we are going to.  */
11799
0
      targ = (stub_entry->target_value
11800
0
        + stub_entry->target_section->output_offset
11801
0
        + stub_entry->target_section->output_section->vma);
11802
0
      targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11803
11804
      /* And this is where we are coming from.  */
11805
0
      off = (stub_entry->stub_offset
11806
0
       + stub_entry->group->stub_sec->output_offset
11807
0
       + stub_entry->group->stub_sec->output_section->vma);
11808
0
      off = targ - off;
11809
11810
0
      p = loc;
11811
0
      obfd = htab->params->stub_bfd;
11812
0
      if (stub_entry->type.r2save)
11813
0
  {
11814
0
    bfd_vma r2off = get_r2off (info, stub_entry);
11815
11816
0
    if (r2off == (bfd_vma) -1)
11817
0
      {
11818
0
        htab->stub_error = true;
11819
0
        return false;
11820
0
      }
11821
0
    bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11822
0
    p += 4;
11823
0
    if (PPC_HA (r2off) != 0)
11824
0
      {
11825
0
        bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11826
0
        p += 4;
11827
0
      }
11828
0
    if (PPC_LO (r2off) != 0)
11829
0
      {
11830
0
        bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11831
0
        p += 4;
11832
0
      }
11833
0
    off -= p - loc;
11834
0
  }
11835
0
      bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
11836
0
      p += 4;
11837
11838
0
      if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11839
0
  {
11840
0
    _bfd_error_handler
11841
0
      (_("long branch stub `%s' offset overflow"),
11842
0
       stub_entry->root.string);
11843
0
    htab->stub_error = true;
11844
0
    return false;
11845
0
  }
11846
11847
0
      if (info->emitrelocations)
11848
0
  {
11849
0
    r = get_relocs (stub_entry->group->stub_sec, 1);
11850
0
    if (r == NULL)
11851
0
      return false;
11852
0
    r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11853
0
    r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11854
0
    r->r_addend = targ;
11855
0
    if (stub_entry->h != NULL
11856
0
        && !use_global_in_relocs (htab, stub_entry, r, 1))
11857
0
      return false;
11858
0
  }
11859
0
    }
11860
0
  else if (stub_entry->type.main == ppc_stub_plt_branch
11861
0
     && stub_entry->type.sub == ppc_stub_toc)
11862
0
    {
11863
0
      br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11864
0
           stub_entry->root.string + 9,
11865
0
           false, false);
11866
0
      if (br_entry == NULL)
11867
0
  {
11868
0
    _bfd_error_handler (_("can't find branch stub `%s'"),
11869
0
            stub_entry->root.string);
11870
0
    htab->stub_error = true;
11871
0
    return false;
11872
0
  }
11873
11874
0
      targ = (stub_entry->target_value
11875
0
        + stub_entry->target_section->output_offset
11876
0
        + stub_entry->target_section->output_section->vma);
11877
0
      if (!stub_entry->type.r2save)
11878
0
  targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11879
11880
0
      bfd_put_64 (htab->brlt->owner, targ,
11881
0
      htab->brlt->contents + br_entry->offset);
11882
11883
0
      if (br_entry->iter == htab->stub_iteration)
11884
0
  {
11885
0
    br_entry->iter = 0;
11886
11887
0
    if (htab->relbrlt != NULL && !info->enable_dt_relr)
11888
0
      {
11889
        /* Create a reloc for the branch lookup table entry.  */
11890
0
        Elf_Internal_Rela rela;
11891
11892
0
        rela.r_offset = (br_entry->offset
11893
0
             + htab->brlt->output_offset
11894
0
             + htab->brlt->output_section->vma);
11895
0
        rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11896
0
        rela.r_addend = targ;
11897
11898
0
        BFD_ASSERT (count_and_swap_reloc_out (htab->relbrlt->owner, &rela,
11899
0
                htab->relbrlt));
11900
0
      }
11901
0
    else if (info->emitrelocations)
11902
0
      {
11903
0
        r = get_relocs (htab->brlt, 1);
11904
0
        if (r == NULL)
11905
0
    return false;
11906
        /* brlt, being SEC_LINKER_CREATED does not go through the
11907
     normal reloc processing.  Symbols and offsets are not
11908
     translated from input file to output file form, so
11909
     set up the offset per the output file.  */
11910
0
        r->r_offset = (br_entry->offset
11911
0
           + htab->brlt->output_offset
11912
0
           + htab->brlt->output_section->vma);
11913
0
        r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11914
0
        r->r_addend = targ;
11915
0
      }
11916
0
  }
11917
11918
0
      targ = (br_entry->offset
11919
0
        + htab->brlt->output_offset
11920
0
        + htab->brlt->output_section->vma);
11921
11922
0
      off = (elf_gp (info->output_bfd)
11923
0
       + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11924
0
      off = targ - off;
11925
11926
0
      if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11927
0
  {
11928
0
    info->callbacks->einfo
11929
0
      (_("%P: linkage table error against `%pT'\n"),
11930
0
       stub_entry->root.string);
11931
0
    bfd_set_error (bfd_error_bad_value);
11932
0
    htab->stub_error = true;
11933
0
    return false;
11934
0
  }
11935
11936
0
      if (info->emitrelocations)
11937
0
  {
11938
0
    r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11939
0
    if (r == NULL)
11940
0
      return false;
11941
0
    r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11942
0
    if (bfd_big_endian (info->output_bfd))
11943
0
      r[0].r_offset += 2;
11944
0
    if (stub_entry->type.r2save)
11945
0
      r[0].r_offset += 4;
11946
0
    r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11947
0
    r[0].r_addend = targ;
11948
0
    if (PPC_HA (off) != 0)
11949
0
      {
11950
0
        r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11951
0
        r[1].r_offset = r[0].r_offset + 4;
11952
0
        r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11953
0
        r[1].r_addend = r[0].r_addend;
11954
0
      }
11955
0
  }
11956
11957
0
      p = loc;
11958
0
      obfd = htab->params->stub_bfd;
11959
0
      if (!stub_entry->type.r2save)
11960
0
  {
11961
0
    if (PPC_HA (off) != 0)
11962
0
      {
11963
0
        bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11964
0
        p += 4;
11965
0
        bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11966
0
      }
11967
0
    else
11968
0
      bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11969
0
  }
11970
0
      else
11971
0
  {
11972
0
    bfd_vma r2off = get_r2off (info, stub_entry);
11973
11974
0
    if (r2off == (bfd_vma) -1)
11975
0
      {
11976
0
        htab->stub_error = true;
11977
0
        return false;
11978
0
      }
11979
11980
0
    bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11981
0
    p += 4;
11982
0
    if (PPC_HA (off) != 0)
11983
0
      {
11984
0
        bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11985
0
        p += 4;
11986
0
        bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11987
0
      }
11988
0
    else
11989
0
      bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11990
11991
0
    if (PPC_HA (r2off) != 0)
11992
0
      {
11993
0
        p += 4;
11994
0
        bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11995
0
      }
11996
0
    if (PPC_LO (r2off) != 0)
11997
0
      {
11998
0
        p += 4;
11999
0
        bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
12000
0
      }
12001
0
  }
12002
0
      p += 4;
12003
0
      bfd_put_32 (obfd, MTCTR_R12, p);
12004
0
      p += 4;
12005
0
      bfd_put_32 (obfd, BCTR, p);
12006
0
      p += 4;
12007
0
    }
12008
0
  else if (stub_entry->type.sub >= ppc_stub_notoc)
12009
0
    {
12010
0
      bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
12011
0
      p = loc;
12012
0
      off = (stub_entry->stub_offset
12013
0
       + stub_entry->group->stub_sec->output_offset
12014
0
       + stub_entry->group->stub_sec->output_section->vma);
12015
0
      obfd = htab->params->stub_bfd;
12016
0
      is_tga = (is_plt
12017
0
    && stub_entry->h != NULL
12018
0
    && is_tls_get_addr (&stub_entry->h->elf, htab)
12019
0
    && htab->params->tls_get_addr_opt);
12020
0
      if (is_tga)
12021
0
  {
12022
0
    p = build_tls_get_addr_head (htab, stub_entry, p);
12023
0
    off += p - loc;
12024
0
  }
12025
0
      if (stub_entry->type.r2save)
12026
0
  {
12027
0
    off += 4;
12028
0
    bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
12029
0
    p += 4;
12030
0
  }
12031
0
      if (is_plt)
12032
0
  {
12033
0
    targ = stub_entry->plt_ent->plt.offset & ~1;
12034
0
    if (targ >= (bfd_vma) -2)
12035
0
      abort ();
12036
12037
0
    plt = htab->elf.splt;
12038
0
    if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12039
0
      {
12040
0
        if (stub_entry->symtype == STT_GNU_IFUNC)
12041
0
    plt = htab->elf.iplt;
12042
0
        else
12043
0
    plt = htab->pltlocal;
12044
0
      }
12045
0
    targ += plt->output_offset + plt->output_section->vma;
12046
0
  }
12047
0
      else
12048
0
  targ = (stub_entry->target_value
12049
0
    + stub_entry->target_section->output_offset
12050
0
    + stub_entry->target_section->output_section->vma);
12051
0
      odd = off & 4;
12052
0
      off = targ - off;
12053
12054
0
      relp = p;
12055
0
      num_rel = 0;
12056
0
      if (stub_entry->type.sub == ppc_stub_notoc)
12057
0
  p = build_power10_offset (obfd, p, off, odd, is_plt);
12058
0
      else
12059
0
  {
12060
0
    if (htab->glink_eh_frame != NULL
12061
0
        && htab->glink_eh_frame->size != 0)
12062
0
      {
12063
0
        bfd_byte *base, *eh;
12064
0
        unsigned int lr_used, delta;
12065
12066
0
        base = (htab->glink_eh_frame->contents
12067
0
          + stub_entry->group->eh_base + 17);
12068
0
        eh = base + stub_entry->group->eh_size;
12069
0
        lr_used = stub_entry->stub_offset + (p - loc) + 8;
12070
0
        delta = lr_used - stub_entry->group->lr_restore;
12071
0
        stub_entry->group->lr_restore = lr_used + 8;
12072
0
        eh = eh_advance (htab->elf.dynobj, eh, delta);
12073
0
        *eh++ = DW_CFA_register;
12074
0
        *eh++ = 65;
12075
0
        *eh++ = 12;
12076
0
        *eh++ = DW_CFA_advance_loc + 2;
12077
0
        *eh++ = DW_CFA_restore_extended;
12078
0
        *eh++ = 65;
12079
0
        stub_entry->group->eh_size = eh - base;
12080
0
      }
12081
12082
    /* The notoc stubs calculate their target (either a PLT entry or
12083
       the global entry point of a function) relative to the PC
12084
       returned by the "bcl" two instructions past the start of the
12085
       sequence emitted by build_offset.  The offset is therefore 8
12086
       less than calculated from the start of the sequence.  */
12087
0
    off -= 8;
12088
0
    p = build_offset (obfd, p, off, is_plt);
12089
0
  }
12090
12091
0
      if (stub_entry->type.main == ppc_stub_long_branch)
12092
0
  {
12093
0
    bfd_vma from;
12094
0
    num_rel = 1;
12095
0
    from = (stub_entry->stub_offset
12096
0
      + stub_entry->group->stub_sec->output_offset
12097
0
      + stub_entry->group->stub_sec->output_section->vma
12098
0
      + (p - loc));
12099
0
    bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
12100
0
  }
12101
0
      else
12102
0
  {
12103
0
    bfd_put_32 (obfd, MTCTR_R12, p);
12104
0
    p += 4;
12105
0
    bfd_put_32 (obfd, BCTR, p);
12106
0
  }
12107
0
      p += 4;
12108
12109
0
      if (is_tga)
12110
0
  p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12111
12112
0
      if (info->emitrelocations)
12113
0
  {
12114
0
    bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
12115
0
    if (stub_entry->type.sub == ppc_stub_notoc)
12116
0
      num_rel += num_relocs_for_power10_offset (off, odd);
12117
0
    else
12118
0
      {
12119
0
        num_rel += num_relocs_for_offset (off);
12120
0
        roff += 16;
12121
0
      }
12122
0
    r = get_relocs (stub_entry->group->stub_sec, num_rel);
12123
0
    if (r == NULL)
12124
0
      return false;
12125
0
    if (stub_entry->type.sub == ppc_stub_notoc)
12126
0
      r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
12127
0
    else
12128
0
      r = emit_relocs_for_offset (info, r, roff, targ, off);
12129
0
    if (stub_entry->type.main == ppc_stub_long_branch)
12130
0
      {
12131
0
        ++r;
12132
0
        roff = p - 4 - stub_entry->group->stub_sec->contents;
12133
0
        r->r_offset = roff;
12134
0
        r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
12135
0
        r->r_addend = targ;
12136
0
        if (stub_entry->h != NULL
12137
0
      && !use_global_in_relocs (htab, stub_entry, r, num_rel))
12138
0
    return false;
12139
0
      }
12140
0
  }
12141
0
    }
12142
0
  else if (stub_entry->type.main == ppc_stub_plt_call)
12143
0
    {
12144
0
      if (stub_entry->h != NULL
12145
0
    && stub_entry->h->is_func_descriptor
12146
0
    && stub_entry->h->oh != NULL)
12147
0
  {
12148
0
    struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
12149
12150
    /* If the old-ABI "dot-symbol" is undefined make it weak so
12151
       we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
12152
0
    if (fh->elf.root.type == bfd_link_hash_undefined
12153
0
        && (stub_entry->h->elf.root.type == bfd_link_hash_defined
12154
0
      || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
12155
0
      fh->elf.root.type = bfd_link_hash_undefweak;
12156
0
  }
12157
12158
      /* Now build the stub.  */
12159
0
      targ = stub_entry->plt_ent->plt.offset & ~1;
12160
0
      if (targ >= (bfd_vma) -2)
12161
0
  abort ();
12162
12163
0
      plt = htab->elf.splt;
12164
0
      if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12165
0
  {
12166
0
    if (stub_entry->symtype == STT_GNU_IFUNC)
12167
0
      plt = htab->elf.iplt;
12168
0
    else
12169
0
      plt = htab->pltlocal;
12170
0
  }
12171
0
      targ += plt->output_offset + plt->output_section->vma;
12172
12173
0
      off = (elf_gp (info->output_bfd)
12174
0
       + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12175
0
      off = targ - off;
12176
12177
0
      if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
12178
0
  {
12179
0
    info->callbacks->einfo
12180
      /* xgettext:c-format */
12181
0
      (_("%P: linkage table error against `%pT'\n"),
12182
0
       stub_entry->h != NULL
12183
0
       ? stub_entry->h->elf.root.root.string
12184
0
       : "<local sym>");
12185
0
    bfd_set_error (bfd_error_bad_value);
12186
0
    htab->stub_error = true;
12187
0
    return false;
12188
0
  }
12189
12190
0
      r = NULL;
12191
0
      if (info->emitrelocations)
12192
0
  {
12193
0
    r = get_relocs (stub_entry->group->stub_sec,
12194
0
        ((PPC_HA (off) != 0)
12195
0
         + (htab->opd_abi
12196
0
            ? 2 + (htab->params->plt_static_chain
12197
0
             && PPC_HA (off + 16) == PPC_HA (off))
12198
0
            : 1)));
12199
0
    if (r == NULL)
12200
0
      return false;
12201
0
    r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
12202
0
    if (bfd_big_endian (info->output_bfd))
12203
0
      r[0].r_offset += 2;
12204
0
    r[0].r_addend = targ;
12205
0
  }
12206
0
      p = loc;
12207
0
      obfd = htab->params->stub_bfd;
12208
0
      is_tga = (stub_entry->h != NULL
12209
0
    && is_tls_get_addr (&stub_entry->h->elf, htab)
12210
0
    && htab->params->tls_get_addr_opt);
12211
0
      if (is_tga)
12212
0
  {
12213
0
    p = build_tls_get_addr_head (htab, stub_entry, p);
12214
0
    if (r != NULL)
12215
0
      r[0].r_offset += p - loc;
12216
0
  }
12217
0
      p = build_plt_stub (htab, stub_entry, p, off, r);
12218
0
      if (is_tga)
12219
0
  p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12220
0
    }
12221
0
  else if (stub_entry->type.main == ppc_stub_save_res)
12222
0
    return true;
12223
0
  else
12224
0
    {
12225
0
      BFD_FAIL ();
12226
0
      return false;
12227
0
    }
12228
12229
0
  stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
12230
12231
0
  if (htab->params->emit_stub_syms)
12232
0
    {
12233
0
      struct elf_link_hash_entry *h;
12234
0
      size_t len1, len2;
12235
0
      char *name;
12236
0
      static const char stub_str[][16] = { "long_branch",
12237
0
             "plt_branch",
12238
0
             "plt_call" };
12239
12240
0
      len1 = strlen (stub_str[stub_entry->type.main - 1]);
12241
0
      len2 = strlen (stub_entry->root.string);
12242
0
      name = bfd_alloc (info->output_bfd, len1 + len2 + 2);
12243
0
      if (name == NULL)
12244
0
  return false;
12245
0
      memcpy (name, stub_entry->root.string, 9);
12246
0
      memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
12247
0
      memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
12248
0
      h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
12249
0
      if (h == NULL)
12250
0
  return false;
12251
0
      if (h->root.type == bfd_link_hash_new)
12252
0
  {
12253
0
    h->root.type = bfd_link_hash_defined;
12254
0
    h->root.u.def.section = stub_entry->group->stub_sec;
12255
0
    h->root.u.def.value = stub_entry->stub_offset;
12256
0
    h->ref_regular = 1;
12257
0
    h->def_regular = 1;
12258
0
    h->ref_regular_nonweak = 1;
12259
0
    h->forced_local = 1;
12260
0
    h->non_elf = 0;
12261
0
    h->root.linker_def = 1;
12262
0
  }
12263
0
    }
12264
12265
0
  return true;
12266
0
}
12267
12268
/* As above, but don't actually build the stub.  Just bump offset so
12269
   we know stub section sizes, and select plt_branch stubs where
12270
   long_branch stubs won't do.  */
12271
12272
static bool
12273
ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
12274
0
{
12275
0
  struct ppc_stub_hash_entry *stub_entry;
12276
0
  struct bfd_link_info *info;
12277
0
  struct ppc_link_hash_table *htab;
12278
0
  asection *plt;
12279
0
  bfd_vma targ, off, r2off;
12280
0
  unsigned int size, pad, extra, lr_used, delta, odd;
12281
0
  bfd_vma stub_offset;
12282
12283
  /* Massage our args to the form they really have.  */
12284
0
  stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
12285
0
  info = in_arg;
12286
12287
0
  htab = ppc_hash_table (info);
12288
0
  if (htab == NULL)
12289
0
    return false;
12290
12291
  /* Fail if the target section could not be assigned to an output
12292
     section.  The user should fix his linker script.  */
12293
0
  if (stub_entry->target_section != NULL
12294
0
      && stub_entry->target_section->output_section == NULL
12295
0
      && info->non_contiguous_regions)
12296
0
    info->callbacks->fatal (_("%P: Could not assign `%pA' to an output section. "
12297
0
            "Retry without --enable-non-contiguous-regions.\n"),
12298
0
          stub_entry->target_section);
12299
12300
  /* Same for the group.  */
12301
0
  if (stub_entry->group->stub_sec != NULL
12302
0
      && stub_entry->group->stub_sec->output_section == NULL
12303
0
      && info->non_contiguous_regions)
12304
0
    info->callbacks->fatal (_("%P: Could not assign `%pA' to an output section. "
12305
0
            "Retry without --enable-non-contiguous-regions.\n"),
12306
0
          stub_entry->group->stub_sec);
12307
12308
  /* Make a note of the offset within the stubs for this entry.  */
12309
0
  stub_offset = stub_entry->group->stub_sec->size;
12310
0
  if (htab->stub_iteration > STUB_SHRINK_ITER
12311
0
      && stub_entry->stub_offset > stub_offset)
12312
0
    stub_offset = stub_entry->stub_offset;
12313
0
  stub_entry->id = ++htab->stub_id;
12314
12315
0
  if (stub_entry->h != NULL
12316
0
      && stub_entry->h->save_res
12317
0
      && stub_entry->h->elf.root.type == bfd_link_hash_defined
12318
0
      && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12319
0
    {
12320
      /* Don't make stubs to out-of-line register save/restore
12321
   functions.  Instead, emit copies of the functions.  */
12322
0
      stub_entry->group->needs_save_res = 1;
12323
0
      stub_entry->type.main = ppc_stub_save_res;
12324
0
      stub_entry->type.sub = ppc_stub_toc;
12325
0
      stub_entry->type.r2save = 0;
12326
0
      return true;
12327
0
    }
12328
12329
0
  if (stub_entry->type.main == ppc_stub_plt_branch)
12330
0
    {
12331
      /* Reset the stub type from the plt branch variant in case we now
12332
   can reach with a shorter stub.  */
12333
0
      stub_entry->type.main = ppc_stub_long_branch;
12334
0
    }
12335
12336
0
  if (stub_entry->type.main == ppc_stub_long_branch
12337
0
      && stub_entry->type.sub == ppc_stub_toc)
12338
0
    {
12339
0
      targ = (stub_entry->target_value
12340
0
        + stub_entry->target_section->output_offset
12341
0
        + stub_entry->target_section->output_section->vma);
12342
0
      targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12343
0
      off = (stub_offset
12344
0
       + stub_entry->group->stub_sec->output_offset
12345
0
       + stub_entry->group->stub_sec->output_section->vma);
12346
12347
0
      size = 4;
12348
0
      r2off = 0;
12349
0
      if (stub_entry->type.r2save)
12350
0
  {
12351
0
    r2off = get_r2off (info, stub_entry);
12352
0
    if (r2off == (bfd_vma) -1)
12353
0
      {
12354
0
        htab->stub_error = true;
12355
0
        return false;
12356
0
      }
12357
0
    size = 8;
12358
0
    if (PPC_HA (r2off) != 0)
12359
0
      size += 4;
12360
0
    if (PPC_LO (r2off) != 0)
12361
0
      size += 4;
12362
0
    off += size - 4;
12363
0
  }
12364
0
      off = targ - off;
12365
12366
      /* If the branch offset is too big, use a ppc_stub_plt_branch.
12367
   Do the same for -R objects without function descriptors.  */
12368
0
      if ((stub_entry->type.r2save
12369
0
     && r2off == 0
12370
0
     && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12371
0
    || off + (1 << 25) >= (bfd_vma) (1 << 26))
12372
0
  {
12373
0
    struct ppc_branch_hash_entry *br_entry;
12374
12375
0
    br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12376
0
               stub_entry->root.string + 9,
12377
0
               true, false);
12378
0
    if (br_entry == NULL)
12379
0
      {
12380
0
        _bfd_error_handler (_("can't build branch stub `%s'"),
12381
0
          stub_entry->root.string);
12382
0
        htab->stub_error = true;
12383
0
        return false;
12384
0
      }
12385
12386
0
    if (br_entry->iter != htab->stub_iteration)
12387
0
      {
12388
0
        br_entry->iter = htab->stub_iteration;
12389
0
        br_entry->offset = htab->brlt->size;
12390
0
        htab->brlt->size += 8;
12391
12392
0
        if (htab->relbrlt != NULL && !info->enable_dt_relr)
12393
0
    htab->relbrlt->size += sizeof (Elf64_External_Rela);
12394
0
        else if (info->emitrelocations)
12395
0
    {
12396
0
      htab->brlt->reloc_count += 1;
12397
0
      htab->brlt->flags |= SEC_RELOC;
12398
0
    }
12399
0
      }
12400
12401
0
    targ = (br_entry->offset
12402
0
      + htab->brlt->output_offset
12403
0
      + htab->brlt->output_section->vma);
12404
0
    off = (elf_gp (info->output_bfd)
12405
0
     + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12406
0
    off = targ - off;
12407
12408
0
    if (info->emitrelocations)
12409
0
      {
12410
0
        stub_entry->group->stub_sec->reloc_count
12411
0
    += 1 + (PPC_HA (off) != 0);
12412
0
        stub_entry->group->stub_sec->flags |= SEC_RELOC;
12413
0
      }
12414
12415
0
    stub_entry->type.main = ppc_stub_plt_branch;
12416
0
    if (!stub_entry->type.r2save)
12417
0
      {
12418
0
        size = 12;
12419
0
        if (PPC_HA (off) != 0)
12420
0
    size = 16;
12421
0
      }
12422
0
    else
12423
0
      {
12424
0
        size = 16;
12425
0
        if (PPC_HA (off) != 0)
12426
0
    size += 4;
12427
12428
0
        if (PPC_HA (r2off) != 0)
12429
0
    size += 4;
12430
0
        if (PPC_LO (r2off) != 0)
12431
0
    size += 4;
12432
0
      }
12433
0
    pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12434
0
    stub_offset += pad;
12435
0
  }
12436
0
      else if (info->emitrelocations)
12437
0
  {
12438
0
    stub_entry->group->stub_sec->reloc_count += 1;
12439
0
    stub_entry->group->stub_sec->flags |= SEC_RELOC;
12440
0
  }
12441
0
    }
12442
0
  else if (stub_entry->type.main == ppc_stub_long_branch)
12443
0
    {
12444
0
      off = (stub_offset
12445
0
       + stub_entry->group->stub_sec->output_offset
12446
0
       + stub_entry->group->stub_sec->output_section->vma);
12447
0
      size = 0;
12448
0
      if (stub_entry->type.r2save)
12449
0
  size = 4;
12450
0
      off += size;
12451
0
      targ = (stub_entry->target_value
12452
0
        + stub_entry->target_section->output_offset
12453
0
        + stub_entry->target_section->output_section->vma);
12454
0
      odd = off & 4;
12455
0
      off = targ - off;
12456
12457
0
      if (stub_entry->type.sub == ppc_stub_notoc)
12458
0
  extra = size_power10_offset (off, odd);
12459
0
      else
12460
0
  extra = size_offset (off - 8);
12461
      /* Include branch insn plus those in the offset sequence.  */
12462
0
      size += 4 + extra;
12463
12464
      /* If the branch can't reach, use a plt_branch.
12465
   The branch insn is at the end, or "extra" bytes along.  So
12466
   its offset will be "extra" bytes less that that already
12467
   calculated.  */
12468
0
      if (off - extra + (1 << 25) >= (bfd_vma) (1 << 26))
12469
0
  {
12470
0
    stub_entry->type.main = ppc_stub_plt_branch;
12471
0
    size += 4;
12472
0
    pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12473
0
    if (pad != 0)
12474
0
      {
12475
0
        stub_offset += pad;
12476
0
        off -= pad;
12477
0
        odd ^= pad & 4;
12478
0
        size -= extra;
12479
0
        if (stub_entry->type.sub == ppc_stub_notoc)
12480
0
    extra = size_power10_offset (off, odd);
12481
0
        else
12482
0
    extra = size_offset (off - 8);
12483
0
        size += extra;
12484
0
      }
12485
0
  }
12486
0
      else if (info->emitrelocations)
12487
0
  stub_entry->group->stub_sec->reloc_count +=1;
12488
12489
0
      if (info->emitrelocations)
12490
0
  {
12491
0
    unsigned int num_rel;
12492
0
    if (stub_entry->type.sub == ppc_stub_notoc)
12493
0
      num_rel = num_relocs_for_power10_offset (off, odd);
12494
0
    else
12495
0
      num_rel = num_relocs_for_offset (off - 8);
12496
0
    stub_entry->group->stub_sec->reloc_count += num_rel;
12497
0
    stub_entry->group->stub_sec->flags |= SEC_RELOC;
12498
0
  }
12499
12500
0
      if (stub_entry->type.sub != ppc_stub_notoc)
12501
0
  {
12502
    /* After the bcl, lr has been modified so we need to emit
12503
       .eh_frame info saying the return address is in r12.  */
12504
0
    lr_used = stub_offset + 8;
12505
0
    if (stub_entry->type.r2save)
12506
0
      lr_used += 4;
12507
    /* The eh_frame info will consist of a DW_CFA_advance_loc or
12508
       variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12509
       DW_CFA_restore_extended 65.  */
12510
0
    delta = lr_used - stub_entry->group->lr_restore;
12511
0
    stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12512
0
    stub_entry->group->lr_restore = lr_used + 8;
12513
0
  }
12514
0
    }
12515
0
  else if (stub_entry->type.sub >= ppc_stub_notoc)
12516
0
    {
12517
0
      BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
12518
0
      lr_used = 0;
12519
0
      if (stub_entry->h != NULL
12520
0
    && is_tls_get_addr (&stub_entry->h->elf, htab)
12521
0
    && htab->params->tls_get_addr_opt)
12522
0
  {
12523
0
    lr_used += 7 * 4;
12524
0
    if (!htab->params->no_tls_get_addr_regsave)
12525
0
      lr_used += 11 * 4;
12526
0
    else if (stub_entry->type.r2save)
12527
0
      lr_used += 2 * 4;
12528
0
  }
12529
0
      if (stub_entry->type.r2save)
12530
0
  lr_used += 4;
12531
0
      targ = stub_entry->plt_ent->plt.offset & ~1;
12532
0
      if (targ >= (bfd_vma) -2)
12533
0
  abort ();
12534
12535
0
      plt = htab->elf.splt;
12536
0
      if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12537
0
  {
12538
0
    if (stub_entry->symtype == STT_GNU_IFUNC)
12539
0
      plt = htab->elf.iplt;
12540
0
    else
12541
0
      plt = htab->pltlocal;
12542
0
  }
12543
0
      targ += plt->output_offset + plt->output_section->vma;
12544
0
      off = (stub_offset
12545
0
       + stub_entry->group->stub_sec->output_offset
12546
0
       + stub_entry->group->stub_sec->output_section->vma
12547
0
       + lr_used);
12548
0
      odd = off & 4;
12549
0
      off = targ - off;
12550
12551
0
      size = plt_stub_size (htab, stub_entry, off, odd);
12552
0
      pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12553
0
      if (pad != 0)
12554
0
  {
12555
0
    stub_offset += pad;
12556
0
    off -= pad;
12557
0
    odd ^= pad & 4;
12558
0
    size = plt_stub_size (htab, stub_entry, off, odd);
12559
0
  }
12560
12561
0
      if (info->emitrelocations)
12562
0
  {
12563
0
    unsigned int num_rel;
12564
0
    if (stub_entry->type.sub == ppc_stub_notoc)
12565
0
      num_rel = num_relocs_for_power10_offset (off, odd);
12566
0
    else
12567
0
      num_rel = num_relocs_for_offset (off - 8);
12568
0
    stub_entry->group->stub_sec->reloc_count += num_rel;
12569
0
    stub_entry->group->stub_sec->flags |= SEC_RELOC;
12570
0
  }
12571
12572
0
      if (stub_entry->type.sub != ppc_stub_notoc)
12573
0
  {
12574
    /* After the bcl, lr has been modified so we need to emit
12575
       .eh_frame info saying the return address is in r12.  */
12576
0
    lr_used += stub_offset + 8;
12577
    /* The eh_frame info will consist of a DW_CFA_advance_loc or
12578
       variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12579
       DW_CFA_restore_extended 65.  */
12580
0
    delta = lr_used - stub_entry->group->lr_restore;
12581
0
    stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12582
0
    stub_entry->group->lr_restore = lr_used + 8;
12583
0
  }
12584
0
      if (stub_entry->h != NULL
12585
0
    && is_tls_get_addr (&stub_entry->h->elf, htab)
12586
0
    && htab->params->tls_get_addr_opt)
12587
0
  {
12588
0
    if (!htab->params->no_tls_get_addr_regsave)
12589
0
      {
12590
0
        unsigned int cfa_updt = stub_offset + 18 * 4;
12591
0
        delta = cfa_updt - stub_entry->group->lr_restore;
12592
0
        stub_entry->group->eh_size += eh_advance_size (delta);
12593
0
        stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12594
0
        stub_entry->group->lr_restore = stub_offset + size - 4;
12595
0
      }
12596
0
    else if (stub_entry->type.r2save)
12597
0
      {
12598
0
        lr_used = stub_offset + size - 20;
12599
0
        delta = lr_used - stub_entry->group->lr_restore;
12600
0
        stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12601
0
        stub_entry->group->lr_restore = stub_offset + size - 4;
12602
0
      }
12603
0
  }
12604
0
    }
12605
0
  else if (stub_entry->type.main == ppc_stub_plt_call)
12606
0
    {
12607
0
      targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12608
0
      if (targ >= (bfd_vma) -2)
12609
0
  abort ();
12610
0
      plt = htab->elf.splt;
12611
0
      if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12612
0
  {
12613
0
    if (stub_entry->symtype == STT_GNU_IFUNC)
12614
0
      plt = htab->elf.iplt;
12615
0
    else
12616
0
      plt = htab->pltlocal;
12617
0
  }
12618
0
      targ += plt->output_offset + plt->output_section->vma;
12619
12620
0
      off = (elf_gp (info->output_bfd)
12621
0
       + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12622
0
      off = targ - off;
12623
12624
0
      size = plt_stub_size (htab, stub_entry, off, 0);
12625
0
      pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12626
0
      stub_offset += pad;
12627
12628
0
      if (info->emitrelocations)
12629
0
  {
12630
0
    stub_entry->group->stub_sec->reloc_count
12631
0
      += ((PPC_HA (off) != 0)
12632
0
    + (htab->opd_abi
12633
0
       ? 2 + (htab->params->plt_static_chain
12634
0
        && PPC_HA (off + 16) == PPC_HA (off))
12635
0
       : 1));
12636
0
    stub_entry->group->stub_sec->flags |= SEC_RELOC;
12637
0
  }
12638
12639
0
      if (stub_entry->h != NULL
12640
0
    && is_tls_get_addr (&stub_entry->h->elf, htab)
12641
0
    && htab->params->tls_get_addr_opt
12642
0
    && stub_entry->type.r2save)
12643
0
  {
12644
0
    if (!htab->params->no_tls_get_addr_regsave)
12645
0
      {
12646
        /* Adjustments to r1 need to be described.  */
12647
0
        unsigned int cfa_updt = stub_offset + 18 * 4;
12648
0
        delta = cfa_updt - stub_entry->group->lr_restore;
12649
0
        stub_entry->group->eh_size += eh_advance_size (delta);
12650
0
        stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12651
0
      }
12652
0
    else
12653
0
      {
12654
0
        lr_used = stub_offset + size - 20;
12655
        /* The eh_frame info will consist of a DW_CFA_advance_loc
12656
     or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12657
     DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65.  */
12658
0
        delta = lr_used - stub_entry->group->lr_restore;
12659
0
        stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12660
0
      }
12661
0
    stub_entry->group->lr_restore = stub_offset + size - 4;
12662
0
  }
12663
0
    }
12664
0
  else
12665
0
    {
12666
0
      BFD_FAIL ();
12667
0
      return false;
12668
0
    }
12669
12670
0
  if (stub_entry->stub_offset != stub_offset)
12671
0
    htab->stub_changed = true;
12672
0
  stub_entry->stub_offset = stub_offset;
12673
0
  stub_entry->group->stub_sec->size = stub_offset + size;
12674
0
  return true;
12675
0
}
12676
12677
/* Set up various things so that we can make a list of input sections
12678
   for each output section included in the link.  Returns -1 on error,
12679
   0 when no stubs will be needed, and 1 on success.  */
12680
12681
int
12682
ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12683
0
{
12684
0
  unsigned int id;
12685
0
  size_t amt;
12686
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
12687
12688
0
  if (htab == NULL)
12689
0
    return -1;
12690
12691
  /* The access to _bfd_section_id here is unlocked, so for the time
12692
     being this function cannot be called in multi-threaded mode.  */
12693
0
  BFD_ASSERT (!_bfd_threading_enabled ());
12694
12695
0
  htab->sec_info_arr_size = _bfd_section_id;
12696
0
  amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12697
0
  htab->sec_info = bfd_zalloc (info->output_bfd, amt);
12698
0
  if (htab->sec_info == NULL)
12699
0
    return -1;
12700
12701
  /* Set toc_off for com, und, abs and ind sections.  */
12702
0
  for (id = 0; id < 3; id++)
12703
0
    htab->sec_info[id].toc_off = TOC_BASE_OFF;
12704
12705
0
  return 1;
12706
0
}
12707
12708
/* Set up for first pass at multitoc partitioning.  */
12709
12710
void
12711
ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12712
0
{
12713
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
12714
12715
0
  htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12716
0
  htab->toc_bfd = NULL;
12717
0
  htab->toc_first_sec = NULL;
12718
0
}
12719
12720
/* The linker repeatedly calls this function for each TOC input section
12721
   and linker generated GOT section.  Group input bfds such that the toc
12722
   within a group is less than 64k in size.  */
12723
12724
bool
12725
ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12726
0
{
12727
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
12728
0
  bfd_vma addr, off, limit;
12729
12730
0
  if (htab == NULL)
12731
0
    return false;
12732
12733
0
  if (!htab->second_toc_pass)
12734
0
    {
12735
      /* Keep track of the first .toc or .got section for this input bfd.  */
12736
0
      bool new_bfd = htab->toc_bfd != isec->owner;
12737
12738
0
      if (new_bfd)
12739
0
  {
12740
0
    htab->toc_bfd = isec->owner;
12741
0
    htab->toc_first_sec = isec;
12742
0
  }
12743
12744
0
      addr = isec->output_offset + isec->output_section->vma;
12745
0
      off = addr - htab->toc_curr;
12746
0
      limit = 0x80008000;
12747
0
      if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12748
0
  limit = 0x10000;
12749
0
      if (off + isec->size > limit)
12750
0
  {
12751
0
    addr = (htab->toc_first_sec->output_offset
12752
0
      + htab->toc_first_sec->output_section->vma);
12753
0
    htab->toc_curr = addr;
12754
0
    htab->toc_curr &= -TOC_BASE_ALIGN;
12755
0
  }
12756
12757
      /* toc_curr is the base address of this toc group.  Set elf_gp
12758
   for the input section to be the offset relative to the
12759
   output toc base plus 0x8000.  Making the input elf_gp an
12760
   offset allows us to move the toc as a whole without
12761
   recalculating input elf_gp.  */
12762
0
      off = htab->toc_curr - elf_gp (info->output_bfd);
12763
0
      off += TOC_BASE_OFF;
12764
12765
      /* Die if someone uses a linker script that doesn't keep input
12766
   file .toc and .got together.  */
12767
0
      if (new_bfd
12768
0
    && elf_gp (isec->owner) != 0
12769
0
    && elf_gp (isec->owner) != off)
12770
0
  return false;
12771
12772
0
      elf_gp (isec->owner) = off;
12773
0
      return true;
12774
0
    }
12775
12776
  /* During the second pass toc_first_sec points to the start of
12777
     a toc group, and toc_curr is used to track the old elf_gp.
12778
     We use toc_bfd to ensure we only look at each bfd once.  */
12779
0
  if (htab->toc_bfd == isec->owner)
12780
0
    return true;
12781
0
  htab->toc_bfd = isec->owner;
12782
12783
0
  if (htab->toc_first_sec == NULL
12784
0
      || htab->toc_curr != elf_gp (isec->owner))
12785
0
    {
12786
0
      htab->toc_curr = elf_gp (isec->owner);
12787
0
      htab->toc_first_sec = isec;
12788
0
    }
12789
0
  addr = (htab->toc_first_sec->output_offset
12790
0
    + htab->toc_first_sec->output_section->vma);
12791
0
  off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12792
0
  elf_gp (isec->owner) = off;
12793
12794
0
  return true;
12795
0
}
12796
12797
/* Called via elf_link_hash_traverse to merge GOT entries for global
12798
   symbol H.  */
12799
12800
static bool
12801
merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12802
0
{
12803
0
  if (h->root.type == bfd_link_hash_indirect)
12804
0
    return true;
12805
12806
0
  merge_got_entries (&h->got.glist);
12807
12808
0
  return true;
12809
0
}
12810
12811
/* Called via elf_link_hash_traverse to allocate GOT entries for global
12812
   symbol H.  */
12813
12814
static bool
12815
reallocate_got (struct elf_link_hash_entry *h, void *inf)
12816
0
{
12817
0
  struct got_entry *gent;
12818
12819
0
  if (h->root.type == bfd_link_hash_indirect)
12820
0
    return true;
12821
12822
0
  for (gent = h->got.glist; gent != NULL; gent = gent->next)
12823
0
    if (!gent->is_indirect)
12824
0
      allocate_got (h, (struct bfd_link_info *) inf, gent);
12825
0
  return true;
12826
0
}
12827
12828
/* Called on the first multitoc pass after the last call to
12829
   ppc64_elf_next_toc_section.  This function removes duplicate GOT
12830
   entries.  */
12831
12832
bool
12833
ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12834
0
{
12835
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
12836
0
  struct bfd *ibfd, *ibfd2;
12837
0
  bool done_something;
12838
12839
0
  htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12840
12841
0
  if (!htab->do_multi_toc)
12842
0
    return false;
12843
12844
  /* Merge global sym got entries within a toc group.  */
12845
0
  elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12846
12847
  /* And tlsld_got.  */
12848
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12849
0
    {
12850
0
      struct got_entry *ent, *ent2;
12851
12852
0
      if (!is_ppc64_elf (ibfd))
12853
0
  continue;
12854
12855
0
      ent = ppc64_tlsld_got (ibfd);
12856
0
      if (!ent->is_indirect
12857
0
    && ent->got.offset != (bfd_vma) -1)
12858
0
  {
12859
0
    for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12860
0
      {
12861
0
        if (!is_ppc64_elf (ibfd2))
12862
0
    continue;
12863
12864
0
        ent2 = ppc64_tlsld_got (ibfd2);
12865
0
        if (!ent2->is_indirect
12866
0
      && ent2->got.offset != (bfd_vma) -1
12867
0
      && elf_gp (ibfd2) == elf_gp (ibfd))
12868
0
    {
12869
0
      ent2->is_indirect = true;
12870
0
      ent2->got.ent = ent;
12871
0
    }
12872
0
      }
12873
0
  }
12874
0
    }
12875
12876
  /* Zap sizes of got sections.  */
12877
0
  htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12878
0
  htab->elf.irelplt->size -= htab->got_reli_size;
12879
0
  htab->got_reli_size = 0;
12880
12881
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12882
0
    {
12883
0
      asection *got, *relgot;
12884
12885
0
      if (!is_ppc64_elf (ibfd))
12886
0
  continue;
12887
12888
0
      got = ppc64_elf_tdata (ibfd)->got;
12889
0
      if (got != NULL)
12890
0
  {
12891
0
    got->rawsize = got->size;
12892
0
    got->size = 0;
12893
0
    relgot = ppc64_elf_tdata (ibfd)->relgot;
12894
0
    relgot->rawsize = relgot->size;
12895
0
    relgot->size = 0;
12896
0
  }
12897
0
    }
12898
12899
  /* Now reallocate the got, local syms first.  We don't need to
12900
     allocate section contents again since we never increase size.  */
12901
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12902
0
    {
12903
0
      struct got_entry **lgot_ents;
12904
0
      struct got_entry **end_lgot_ents;
12905
0
      struct plt_entry **local_plt;
12906
0
      struct plt_entry **end_local_plt;
12907
0
      unsigned char *lgot_masks;
12908
0
      bfd_size_type locsymcount;
12909
0
      Elf_Internal_Shdr *symtab_hdr;
12910
0
      asection *s;
12911
0
      Elf_Internal_Sym *local_syms;
12912
0
      Elf_Internal_Sym *isym;
12913
12914
0
      if (!is_ppc64_elf (ibfd))
12915
0
  continue;
12916
12917
0
      lgot_ents = elf_local_got_ents (ibfd);
12918
0
      if (!lgot_ents)
12919
0
  continue;
12920
12921
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
12922
0
      locsymcount = symtab_hdr->sh_info;
12923
0
      end_lgot_ents = lgot_ents + locsymcount;
12924
0
      local_plt = (struct plt_entry **) end_lgot_ents;
12925
0
      end_local_plt = local_plt + locsymcount;
12926
0
      lgot_masks = (unsigned char *) end_local_plt;
12927
0
      local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
12928
0
      if (local_syms == NULL && locsymcount != 0)
12929
0
  {
12930
0
    local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
12931
0
               0, NULL, NULL, NULL);
12932
0
    if (local_syms == NULL)
12933
0
      return false;
12934
0
  }
12935
0
      s = ppc64_elf_tdata (ibfd)->got;
12936
0
      for (isym = local_syms;
12937
0
     lgot_ents < end_lgot_ents;
12938
0
     ++lgot_ents, ++lgot_masks, isym++)
12939
0
  {
12940
0
    struct got_entry *ent;
12941
12942
0
    for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12943
0
      {
12944
0
        unsigned int ent_size = 8;
12945
0
        unsigned int rel_size = sizeof (Elf64_External_Rela);
12946
12947
0
        ent->got.offset = s->size;
12948
0
        if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12949
0
    {
12950
0
      ent_size *= 2;
12951
0
      rel_size *= 2;
12952
0
    }
12953
0
        s->size += ent_size;
12954
0
        if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12955
0
    {
12956
0
      htab->elf.irelplt->size += rel_size;
12957
0
      htab->got_reli_size += rel_size;
12958
0
    }
12959
0
        else if (bfd_link_pic (info)
12960
0
           && (ent->tls_type == 0
12961
0
         ? !info->enable_dt_relr
12962
0
         : !bfd_link_executable (info))
12963
0
           && isym->st_shndx != SHN_ABS)
12964
0
    {
12965
0
      asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12966
0
      srel->size += rel_size;
12967
0
    }
12968
0
      }
12969
0
  }
12970
0
    }
12971
12972
0
  elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12973
12974
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12975
0
    {
12976
0
      struct got_entry *ent;
12977
12978
0
      if (!is_ppc64_elf (ibfd))
12979
0
  continue;
12980
12981
0
      ent = ppc64_tlsld_got (ibfd);
12982
0
      if (!ent->is_indirect
12983
0
    && ent->got.offset != (bfd_vma) -1)
12984
0
  {
12985
0
    asection *s = ppc64_elf_tdata (ibfd)->got;
12986
0
    ent->got.offset = s->size;
12987
0
    s->size += 16;
12988
0
    if (bfd_link_dll (info))
12989
0
      {
12990
0
        asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12991
0
        srel->size += sizeof (Elf64_External_Rela);
12992
0
      }
12993
0
  }
12994
0
    }
12995
12996
0
  done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12997
0
  if (!done_something)
12998
0
    for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12999
0
      {
13000
0
  asection *got;
13001
13002
0
  if (!is_ppc64_elf (ibfd))
13003
0
    continue;
13004
13005
0
  got = ppc64_elf_tdata (ibfd)->got;
13006
0
  if (got != NULL)
13007
0
    {
13008
0
      done_something = got->rawsize != got->size;
13009
0
      if (done_something)
13010
0
        break;
13011
0
    }
13012
0
      }
13013
13014
0
  if (done_something)
13015
0
    (*htab->params->layout_sections_again) ();
13016
13017
  /* Set up for second pass over toc sections to recalculate elf_gp
13018
     on input sections.  */
13019
0
  htab->toc_bfd = NULL;
13020
0
  htab->toc_first_sec = NULL;
13021
0
  htab->second_toc_pass = true;
13022
0
  return done_something;
13023
0
}
13024
13025
/* Called after second pass of multitoc partitioning.  */
13026
13027
void
13028
ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
13029
0
{
13030
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
13031
13032
  /* After the second pass, toc_curr tracks the TOC offset used
13033
     for code sections below in ppc64_elf_next_input_section.  */
13034
0
  htab->toc_curr = TOC_BASE_OFF;
13035
0
}
13036
13037
/* No toc references were found in ISEC.  If the code in ISEC makes no
13038
   calls, then there's no need to use toc adjusting stubs when branching
13039
   into ISEC.  Actually, indirect calls from ISEC are OK as they will
13040
   load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
13041
   needed, and 2 if a cyclical call-graph was found but no other reason
13042
   for a stub was detected.  If called from the top level, a return of
13043
   2 means the same as a return of 0.  */
13044
13045
static int
13046
toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
13047
0
{
13048
0
  int ret;
13049
13050
  /* Mark this section as checked.  */
13051
0
  isec->call_check_done = 1;
13052
13053
  /* We know none of our code bearing sections will need toc stubs.  */
13054
0
  if ((isec->flags & SEC_LINKER_CREATED) != 0)
13055
0
    return 0;
13056
13057
0
  if (isec->size == 0)
13058
0
    return 0;
13059
13060
0
  if (isec->output_section == NULL)
13061
0
    return 0;
13062
13063
0
  ret = 0;
13064
0
  if (isec->reloc_count != 0)
13065
0
    {
13066
0
      Elf_Internal_Rela *relstart, *rel;
13067
0
      Elf_Internal_Sym *local_syms;
13068
0
      struct ppc_link_hash_table *htab;
13069
13070
0
      relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
13071
0
              info->keep_memory);
13072
0
      if (relstart == NULL)
13073
0
  return -1;
13074
13075
      /* Look for branches to outside of this section.  */
13076
0
      local_syms = NULL;
13077
0
      htab = ppc_hash_table (info);
13078
0
      if (htab == NULL)
13079
0
  return -1;
13080
13081
0
      for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
13082
0
  {
13083
0
    enum elf_ppc64_reloc_type r_type;
13084
0
    unsigned long r_symndx;
13085
0
    struct elf_link_hash_entry *h;
13086
0
    struct ppc_link_hash_entry *eh;
13087
0
    Elf_Internal_Sym *sym;
13088
0
    asection *sym_sec;
13089
0
    struct _opd_sec_data *opd;
13090
0
    bfd_vma sym_value;
13091
0
    bfd_vma dest;
13092
13093
0
    r_type = ELF64_R_TYPE (rel->r_info);
13094
0
    if (r_type != R_PPC64_REL24
13095
0
        && r_type != R_PPC64_REL24_NOTOC
13096
0
        && r_type != R_PPC64_REL24_P9NOTOC
13097
0
        && r_type != R_PPC64_REL14
13098
0
        && r_type != R_PPC64_REL14_BRTAKEN
13099
0
        && r_type != R_PPC64_REL14_BRNTAKEN
13100
0
        && r_type != R_PPC64_PLTCALL
13101
0
        && r_type != R_PPC64_PLTCALL_NOTOC)
13102
0
      continue;
13103
13104
0
    r_symndx = ELF64_R_SYM (rel->r_info);
13105
0
    if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
13106
0
        isec->owner))
13107
0
      {
13108
0
        ret = -1;
13109
0
        break;
13110
0
      }
13111
13112
    /* Calls to dynamic lib functions go through a plt call stub
13113
       that uses r2.  */
13114
0
    eh = ppc_elf_hash_entry (h);
13115
0
    if (eh != NULL
13116
0
        && (eh->elf.plt.plist != NULL
13117
0
      || (eh->oh != NULL
13118
0
          && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
13119
0
      {
13120
0
        ret = 1;
13121
0
        break;
13122
0
      }
13123
13124
0
    if (sym_sec == NULL)
13125
      /* Ignore other undefined symbols.  */
13126
0
      continue;
13127
13128
    /* Assume branches to other sections not included in the
13129
       link need stubs too, to cover -R and absolute syms.  */
13130
0
    if (sym_sec->output_section == NULL)
13131
0
      {
13132
0
        ret = 1;
13133
0
        break;
13134
0
      }
13135
13136
0
    if (h == NULL)
13137
0
      sym_value = sym->st_value;
13138
0
    else
13139
0
      {
13140
0
        if (h->root.type != bfd_link_hash_defined
13141
0
      && h->root.type != bfd_link_hash_defweak)
13142
0
    abort ();
13143
0
        sym_value = h->root.u.def.value;
13144
0
      }
13145
0
    sym_value += rel->r_addend;
13146
13147
    /* If this branch reloc uses an opd sym, find the code section.  */
13148
0
    opd = get_opd_info (sym_sec);
13149
0
    if (opd != NULL)
13150
0
      {
13151
0
        if (h == NULL && opd->adjust != NULL)
13152
0
    {
13153
0
      long adjust;
13154
13155
0
      adjust = opd->adjust[OPD_NDX (sym_value)];
13156
0
      if (adjust == -1)
13157
        /* Assume deleted functions won't ever be called.  */
13158
0
        continue;
13159
0
      sym_value += adjust;
13160
0
    }
13161
13162
0
        dest = opd_entry_value (sym_sec, sym_value,
13163
0
              &sym_sec, NULL, false);
13164
0
        if (dest == (bfd_vma) -1)
13165
0
    continue;
13166
0
      }
13167
0
    else
13168
0
      dest = (sym_value
13169
0
        + sym_sec->output_offset
13170
0
        + sym_sec->output_section->vma);
13171
13172
    /* Ignore branch to self.  */
13173
0
    if (sym_sec == isec)
13174
0
      continue;
13175
13176
    /* If the called function uses the toc, we need a stub.  */
13177
0
    if (sym_sec->has_toc_reloc
13178
0
        || sym_sec->makes_toc_func_call)
13179
0
      {
13180
0
        ret = 1;
13181
0
        break;
13182
0
      }
13183
13184
    /* Assume any branch that needs a long branch stub might in fact
13185
       need a plt_branch stub.  A plt_branch stub uses r2.  */
13186
0
    else if (dest - (isec->output_offset
13187
0
         + isec->output_section->vma
13188
0
         + rel->r_offset) + (1 << 25)
13189
0
       >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13190
0
                   ? h->other
13191
0
                   : sym->st_other))
13192
0
      {
13193
0
        ret = 1;
13194
0
        break;
13195
0
      }
13196
13197
    /* If calling back to a section in the process of being
13198
       tested, we can't say for sure that no toc adjusting stubs
13199
       are needed, so don't return zero.  */
13200
0
    else if (sym_sec->call_check_in_progress)
13201
0
      ret = 2;
13202
13203
    /* Branches to another section that itself doesn't have any TOC
13204
       references are OK.  Recursively call ourselves to check.  */
13205
0
    else if (!sym_sec->call_check_done)
13206
0
      {
13207
0
        int recur;
13208
13209
        /* Mark current section as indeterminate, so that other
13210
     sections that call back to current won't be marked as
13211
     known.  */
13212
0
        isec->call_check_in_progress = 1;
13213
0
        recur = toc_adjusting_stub_needed (info, sym_sec);
13214
0
        isec->call_check_in_progress = 0;
13215
13216
0
        if (recur != 0)
13217
0
    {
13218
0
      ret = recur;
13219
0
      if (recur != 2)
13220
0
        break;
13221
0
    }
13222
0
      }
13223
0
  }
13224
13225
0
      if (elf_symtab_hdr (isec->owner).contents
13226
0
    != (unsigned char *) local_syms)
13227
0
  free (local_syms);
13228
0
      if (elf_section_data (isec)->relocs != relstart)
13229
0
  free (relstart);
13230
0
    }
13231
13232
0
  if ((ret & 1) == 0
13233
0
      && isec->map_head.s != NULL
13234
0
      && (strcmp (isec->output_section->name, ".init") == 0
13235
0
    || strcmp (isec->output_section->name, ".fini") == 0))
13236
0
    {
13237
0
      if (isec->map_head.s->has_toc_reloc
13238
0
    || isec->map_head.s->makes_toc_func_call)
13239
0
  ret = 1;
13240
0
      else if (!isec->map_head.s->call_check_done)
13241
0
  {
13242
0
    int recur;
13243
0
    isec->call_check_in_progress = 1;
13244
0
    recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13245
0
    isec->call_check_in_progress = 0;
13246
0
    if (recur != 0)
13247
0
      ret = recur;
13248
0
  }
13249
0
    }
13250
13251
0
  if (ret == 1)
13252
0
    isec->makes_toc_func_call = 1;
13253
13254
0
  return ret;
13255
0
}
13256
13257
/* The linker repeatedly calls this function for each input section,
13258
   in the order that input sections are linked into output sections.
13259
   Build lists of input sections to determine groupings between which
13260
   we may insert linker stubs.  */
13261
13262
bool
13263
ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
13264
0
{
13265
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
13266
13267
0
  if (htab == NULL)
13268
0
    return false;
13269
13270
0
  if ((isec->output_section->flags & SEC_CODE) != 0
13271
0
      && isec->output_section->id < htab->sec_info_arr_size)
13272
0
    {
13273
      /* This happens to make the list in reverse order,
13274
   which is what we want.  */
13275
0
      htab->sec_info[isec->id].u.list
13276
0
  = htab->sec_info[isec->output_section->id].u.list;
13277
0
      htab->sec_info[isec->output_section->id].u.list = isec;
13278
0
    }
13279
13280
0
  if (htab->multi_toc_needed)
13281
0
    {
13282
      /* Analyse sections that aren't already flagged as needing a
13283
   valid toc pointer.  Exclude .fixup for the linux kernel.
13284
   .fixup contains branches, but only back to the function that
13285
   hit an exception.  */
13286
0
      if (!(isec->has_toc_reloc
13287
0
      || (isec->flags & SEC_CODE) == 0
13288
0
      || strcmp (isec->name, ".fixup") == 0
13289
0
      || isec->call_check_done))
13290
0
  {
13291
0
    if (toc_adjusting_stub_needed (info, isec) < 0)
13292
0
      return false;
13293
0
  }
13294
      /* Make all sections use the TOC assigned for this object file.
13295
   This will be wrong for pasted sections;  We fix that in
13296
   check_pasted_section().  */
13297
0
      if (elf_gp (isec->owner) != 0)
13298
0
  htab->toc_curr = elf_gp (isec->owner);
13299
0
    }
13300
13301
0
  htab->sec_info[isec->id].toc_off = htab->toc_curr;
13302
0
  return true;
13303
0
}
13304
13305
/* Check that all .init and .fini sections use the same toc, if they
13306
   have toc relocs.  */
13307
13308
static bool
13309
check_pasted_section (struct bfd_link_info *info, const char *name)
13310
0
{
13311
0
  asection *o = bfd_get_section_by_name (info->output_bfd, name);
13312
13313
0
  if (o != NULL)
13314
0
    {
13315
0
      struct ppc_link_hash_table *htab = ppc_hash_table (info);
13316
0
      bfd_vma toc_off = 0;
13317
0
      asection *i;
13318
13319
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13320
0
  if (i->has_toc_reloc)
13321
0
    {
13322
0
      if (toc_off == 0)
13323
0
        toc_off = htab->sec_info[i->id].toc_off;
13324
0
      else if (toc_off != htab->sec_info[i->id].toc_off)
13325
0
        return false;
13326
0
    }
13327
13328
0
      if (toc_off == 0)
13329
0
  for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13330
0
    if (i->makes_toc_func_call)
13331
0
      {
13332
0
        toc_off = htab->sec_info[i->id].toc_off;
13333
0
        break;
13334
0
      }
13335
13336
      /* Make sure the whole pasted function uses the same toc offset.  */
13337
0
      if (toc_off != 0)
13338
0
  for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13339
0
    htab->sec_info[i->id].toc_off = toc_off;
13340
0
    }
13341
0
  return true;
13342
0
}
13343
13344
bool
13345
ppc64_elf_check_init_fini (struct bfd_link_info *info)
13346
0
{
13347
0
  bool ret1 = check_pasted_section (info, ".init");
13348
0
  bool ret2 = check_pasted_section (info, ".fini");
13349
13350
0
  return ret1 && ret2;
13351
0
}
13352
13353
/* See whether we can group stub sections together.  Grouping stub
13354
   sections may result in fewer stubs.  More importantly, we need to
13355
   put all .init* and .fini* stubs at the beginning of the .init or
13356
   .fini output sections respectively, because glibc splits the
13357
   _init and _fini functions into multiple parts.  Putting a stub in
13358
   the middle of a function is not a good idea.  */
13359
13360
static bool
13361
group_sections (struct bfd_link_info *info,
13362
    bfd_size_type stub_group_size,
13363
    bool stubs_always_before_branch)
13364
0
{
13365
0
  struct ppc_link_hash_table *htab;
13366
0
  asection *osec;
13367
0
  bool suppress_size_errors;
13368
13369
0
  htab = ppc_hash_table (info);
13370
0
  if (htab == NULL)
13371
0
    return false;
13372
13373
0
  suppress_size_errors = false;
13374
0
  if (stub_group_size == 1)
13375
0
    {
13376
      /* Default values.  */
13377
0
      if (stubs_always_before_branch)
13378
0
  stub_group_size = 0x1e00000;
13379
0
      else
13380
0
  stub_group_size = 0x1c00000;
13381
0
      suppress_size_errors = true;
13382
0
    }
13383
13384
0
  for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
13385
0
    {
13386
0
      asection *tail;
13387
13388
0
      if (osec->id >= htab->sec_info_arr_size)
13389
0
  continue;
13390
13391
0
      tail = htab->sec_info[osec->id].u.list;
13392
0
      while (tail != NULL)
13393
0
  {
13394
0
    asection *curr;
13395
0
    asection *prev;
13396
0
    bfd_size_type total;
13397
0
    bool big_sec;
13398
0
    bfd_vma curr_toc;
13399
0
    struct map_stub *group;
13400
0
    bfd_size_type group_size;
13401
13402
0
    curr = tail;
13403
0
    total = tail->size;
13404
0
    group_size = (ppc64_elf_section_data (tail) != NULL
13405
0
      && ppc64_elf_section_data (tail)->has_14bit_branch
13406
0
      ? stub_group_size >> 10 : stub_group_size);
13407
13408
0
    big_sec = total > group_size;
13409
0
    if (big_sec && !suppress_size_errors)
13410
      /* xgettext:c-format */
13411
0
      _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
13412
0
        tail->owner, tail);
13413
0
    curr_toc = htab->sec_info[tail->id].toc_off;
13414
13415
0
    while ((prev = htab->sec_info[curr->id].u.list) != NULL
13416
0
     && ((total += curr->output_offset - prev->output_offset)
13417
0
         < (ppc64_elf_section_data (prev) != NULL
13418
0
      && ppc64_elf_section_data (prev)->has_14bit_branch
13419
0
      ? (group_size = stub_group_size >> 10) : group_size))
13420
0
     && htab->sec_info[prev->id].toc_off == curr_toc)
13421
0
      curr = prev;
13422
13423
    /* OK, the size from the start of CURR to the end is less
13424
       than group_size and thus can be handled by one stub
13425
       section.  (or the tail section is itself larger than
13426
       group_size, in which case we may be toast.)  We should
13427
       really be keeping track of the total size of stubs added
13428
       here, as stubs contribute to the final output section
13429
       size.  That's a little tricky, and this way will only
13430
       break if stubs added make the total size more than 2^25,
13431
       ie. for the default stub_group_size, if stubs total more
13432
       than 2097152 bytes, or nearly 75000 plt call stubs.  */
13433
0
    group = bfd_alloc (curr->owner, sizeof (*group));
13434
0
    if (group == NULL)
13435
0
      return false;
13436
0
    group->link_sec = curr;
13437
0
    group->stub_sec = NULL;
13438
0
    group->needs_save_res = 0;
13439
0
    group->lr_restore = 0;
13440
0
    group->eh_size = 0;
13441
0
    group->eh_base = 0;
13442
0
    group->next = htab->group;
13443
0
    htab->group = group;
13444
0
    do
13445
0
      {
13446
0
        prev = htab->sec_info[tail->id].u.list;
13447
        /* Set up this stub group.  */
13448
0
        htab->sec_info[tail->id].u.group = group;
13449
0
      }
13450
0
    while (tail != curr && (tail = prev) != NULL);
13451
13452
    /* But wait, there's more!  Input sections up to group_size
13453
       bytes before the stub section can be handled by it too.
13454
       Don't do this if we have a really large section after the
13455
       stubs, as adding more stubs increases the chance that
13456
       branches may not reach into the stub section.  */
13457
0
    if (!stubs_always_before_branch && !big_sec)
13458
0
      {
13459
0
        total = 0;
13460
0
        while (prev != NULL
13461
0
         && ((total += tail->output_offset - prev->output_offset)
13462
0
       < (ppc64_elf_section_data (prev) != NULL
13463
0
          && ppc64_elf_section_data (prev)->has_14bit_branch
13464
0
          ? (group_size = stub_group_size >> 10)
13465
0
          : group_size))
13466
0
         && htab->sec_info[prev->id].toc_off == curr_toc)
13467
0
    {
13468
0
      tail = prev;
13469
0
      prev = htab->sec_info[tail->id].u.list;
13470
0
      htab->sec_info[tail->id].u.group = group;
13471
0
    }
13472
0
      }
13473
0
    tail = prev;
13474
0
  }
13475
0
    }
13476
0
  return true;
13477
0
}
13478
13479
static const unsigned char glink_eh_frame_cie[] =
13480
{
13481
  0, 0, 0, 16,        /* length.  */
13482
  0, 0, 0, 0,       /* id.  */
13483
  1,          /* CIE version.  */
13484
  'z', 'R', 0,        /* Augmentation string.  */
13485
  4,          /* Code alignment.  */
13486
  0x78,         /* Data alignment.  */
13487
  65,         /* RA reg.  */
13488
  1,          /* Augmentation size.  */
13489
  DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding.  */
13490
  DW_CFA_def_cfa, 1, 0      /* def_cfa: r1 offset 0.  */
13491
};
13492
13493
/* Stripping output sections is normally done before dynamic section
13494
   symbols have been allocated.  This function is called later, and
13495
   handles cases like htab->brlt which is mapped to its own output
13496
   section.  */
13497
13498
static void
13499
maybe_strip_output (struct bfd_link_info *info, asection *isec)
13500
0
{
13501
0
  if (isec->size == 0
13502
0
      && isec->output_section->size == 0
13503
0
      && !(isec->output_section->flags & SEC_KEEP)
13504
0
      && !bfd_section_removed_from_list (info->output_bfd,
13505
0
           isec->output_section)
13506
0
      && elf_section_data (isec->output_section)->dynindx == 0)
13507
0
    {
13508
0
      isec->output_section->flags |= SEC_EXCLUDE;
13509
0
      bfd_section_list_remove (info->output_bfd, isec->output_section);
13510
0
      info->output_bfd->section_count--;
13511
0
    }
13512
0
}
13513
13514
/* Stash R_PPC64_RELATIVE reloc at input section SEC, r_offset OFF to
13515
   the array of such relocs.  */
13516
13517
static bool
13518
append_relr_off (struct ppc_link_hash_table *htab, asection *sec, bfd_vma off)
13519
0
{
13520
0
  if (htab->relr_count >= htab->relr_alloc)
13521
0
    {
13522
0
      if (htab->relr_alloc == 0)
13523
0
  htab->relr_alloc = 4096;
13524
0
      else
13525
0
  htab->relr_alloc *= 2;
13526
0
      htab->relr = bfd_realloc (htab->relr,
13527
0
        htab->relr_alloc * sizeof (*htab->relr));
13528
0
      if (htab->relr == NULL)
13529
0
  return false;
13530
0
    }
13531
0
  htab->relr[htab->relr_count].sec = sec;
13532
0
  htab->relr[htab->relr_count].off = off;
13533
0
  htab->relr_count++;
13534
0
  return true;
13535
0
}
13536
13537
/* qsort comparator for bfd_vma args.  */
13538
13539
static int
13540
compare_relr_address (const void *arg1, const void *arg2)
13541
0
{
13542
0
  bfd_vma a = *(bfd_vma *) arg1;
13543
0
  bfd_vma b = *(bfd_vma *) arg2;
13544
0
  return a < b ? -1 : a > b ? 1 : 0;
13545
0
}
13546
13547
/* Produce a malloc'd sorted array of reloc addresses from the info
13548
   stored by append_relr_off.  */
13549
13550
static bfd_vma *
13551
sort_relr (struct ppc_link_hash_table *htab)
13552
0
{
13553
0
  bfd_vma *addr = bfd_malloc (htab->relr_count * sizeof (*addr));
13554
0
  if (addr == NULL)
13555
0
    return NULL;
13556
13557
0
  for (size_t i = 0; i < htab->relr_count; i++)
13558
0
    addr[i] = (htab->relr[i].sec->output_section->vma
13559
0
         + htab->relr[i].sec->output_offset
13560
0
         + htab->relr[i].off);
13561
13562
0
  if (htab->relr_count > 1)
13563
0
    qsort (addr, htab->relr_count, sizeof (*addr), compare_relr_address);
13564
13565
0
  return addr;
13566
0
}
13567
13568
/* Look over GOT and PLT entries saved on elf_local_got_ents for all
13569
   input files, stashing info about needed relative relocs.  */
13570
13571
static bool
13572
got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13573
0
{
13574
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
13575
0
  bfd *ibfd;
13576
13577
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13578
0
    {
13579
0
      struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13580
0
      struct plt_entry **local_plt, **lplt, **end_local_plt;
13581
0
      Elf_Internal_Shdr *symtab_hdr;
13582
0
      bfd_size_type locsymcount;
13583
0
      Elf_Internal_Sym *local_syms;
13584
0
      Elf_Internal_Sym *isym;
13585
0
      struct plt_entry *pent;
13586
0
      struct got_entry *gent;
13587
13588
0
      if (!is_ppc64_elf (ibfd))
13589
0
  continue;
13590
13591
0
      lgot_ents = elf_local_got_ents (ibfd);
13592
0
      if (!lgot_ents)
13593
0
  continue;
13594
13595
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
13596
0
      locsymcount = symtab_hdr->sh_info;
13597
0
      local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
13598
0
      if (local_syms == NULL && locsymcount != 0)
13599
0
  {
13600
0
    local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
13601
0
               0, NULL, NULL, NULL);
13602
0
    if (local_syms == NULL)
13603
0
      return false;
13604
0
  }
13605
0
      end_lgot_ents = lgot_ents + locsymcount;
13606
0
      local_plt = (struct plt_entry **) end_lgot_ents;
13607
0
      end_local_plt = local_plt + locsymcount;
13608
0
      for (lgot = lgot_ents, isym = local_syms;
13609
0
     lgot < end_lgot_ents;
13610
0
     ++lgot, ++isym)
13611
0
  for (gent = *lgot; gent != NULL; gent = gent->next)
13612
0
    if (!gent->is_indirect
13613
0
        && gent->tls_type == 0
13614
0
        && gent->got.offset != (bfd_vma) -1
13615
0
        && isym->st_shndx != SHN_ABS)
13616
0
      {
13617
0
        asection *got = ppc64_elf_tdata (gent->owner)->got;
13618
0
        if (!append_relr_off (htab, got, gent->got.offset))
13619
0
    {
13620
0
      htab->stub_error = true;
13621
0
      return false;
13622
0
    }
13623
0
      }
13624
13625
0
      if (!htab->opd_abi)
13626
0
  for (lplt = local_plt, isym = local_syms;
13627
0
       lplt < end_local_plt;
13628
0
       ++lplt, ++isym)
13629
0
    for (pent = *lplt; pent != NULL; pent = pent->next)
13630
0
      if (pent->plt.offset != (bfd_vma) -1
13631
0
    && ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC)
13632
0
        {
13633
0
    if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13634
0
      {
13635
0
        if (symtab_hdr->contents != (unsigned char *) local_syms)
13636
0
          free (local_syms);
13637
0
        return false;
13638
0
      }
13639
0
        }
13640
13641
0
      if (local_syms != NULL
13642
0
    && symtab_hdr->contents != (unsigned char *) local_syms)
13643
0
  {
13644
0
    if (!info->keep_memory)
13645
0
      free (local_syms);
13646
0
    else
13647
0
      symtab_hdr->contents = (unsigned char *) local_syms;
13648
0
  }
13649
0
    }
13650
0
  return true;
13651
0
}
13652
13653
/* Stash info about needed GOT and PLT entry relative relocs for
13654
   global symbol H.  */
13655
13656
static bool
13657
got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13658
0
{
13659
0
  struct bfd_link_info *info;
13660
0
  struct ppc_link_hash_table *htab;
13661
0
  struct plt_entry *pent;
13662
0
  struct got_entry *gent;
13663
13664
0
  if (h->root.type == bfd_link_hash_indirect)
13665
0
    return true;
13666
13667
0
  info = (struct bfd_link_info *) inf;
13668
0
  htab = ppc_hash_table (info);
13669
0
  if (htab == NULL)
13670
0
    return false;
13671
13672
0
  if (h->type != STT_GNU_IFUNC
13673
0
      && h->def_regular
13674
0
      && (h->root.type == bfd_link_hash_defined
13675
0
    || h->root.type == bfd_link_hash_defweak))
13676
0
    {
13677
0
      if ((!htab->elf.dynamic_sections_created
13678
0
     || h->dynindx == -1
13679
0
     || SYMBOL_REFERENCES_LOCAL (info, h))
13680
0
    && !bfd_is_abs_symbol (&h->root))
13681
0
  for (gent = h->got.glist; gent != NULL; gent = gent->next)
13682
0
    if (!gent->is_indirect
13683
0
        && gent->tls_type == 0
13684
0
        && gent->got.offset != (bfd_vma) -1)
13685
0
      {
13686
0
        asection *got = ppc64_elf_tdata (gent->owner)->got;
13687
0
        if (!append_relr_off (htab, got, gent->got.offset))
13688
0
    {
13689
0
      htab->stub_error = true;
13690
0
      return false;
13691
0
    }
13692
0
      }
13693
13694
0
      if (!htab->opd_abi
13695
0
    && use_local_plt (info, h))
13696
0
  for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13697
0
    if (pent->plt.offset != (bfd_vma) -1)
13698
0
      {
13699
0
        if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13700
0
    {
13701
0
      htab->stub_error = true;
13702
0
      return false;
13703
0
    }
13704
0
      }
13705
0
    }
13706
0
  return true;
13707
0
}
13708
13709
/* Determine and set the size of the stub section for a final link.
13710
13711
   The basic idea here is to examine all the relocations looking for
13712
   PC-relative calls to a target that is unreachable with a "bl"
13713
   instruction.  */
13714
13715
bool
13716
ppc64_elf_size_stubs (struct bfd_link_info *info)
13717
0
{
13718
0
  bfd_size_type stub_group_size;
13719
0
  bool stubs_always_before_branch;
13720
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
13721
13722
0
  if (htab == NULL)
13723
0
    return false;
13724
13725
0
  if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13726
0
    htab->params->power10_stubs = 0;
13727
13728
0
  if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13729
0
    htab->params->plt_thread_safe = 1;
13730
0
  if (!htab->opd_abi)
13731
0
    htab->params->plt_thread_safe = 0;
13732
0
  else if (htab->params->plt_thread_safe == -1)
13733
0
    {
13734
0
      static const char *const thread_starter[] =
13735
0
  {
13736
0
    "pthread_create",
13737
    /* libstdc++ */
13738
0
    "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13739
    /* librt */
13740
0
    "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13741
0
    "mq_notify", "create_timer",
13742
    /* libanl */
13743
0
    "getaddrinfo_a",
13744
    /* libgomp */
13745
0
    "GOMP_parallel",
13746
0
    "GOMP_parallel_start",
13747
0
    "GOMP_parallel_loop_static",
13748
0
    "GOMP_parallel_loop_static_start",
13749
0
    "GOMP_parallel_loop_dynamic",
13750
0
    "GOMP_parallel_loop_dynamic_start",
13751
0
    "GOMP_parallel_loop_guided",
13752
0
    "GOMP_parallel_loop_guided_start",
13753
0
    "GOMP_parallel_loop_runtime",
13754
0
    "GOMP_parallel_loop_runtime_start",
13755
0
    "GOMP_parallel_sections",
13756
0
    "GOMP_parallel_sections_start",
13757
    /* libgo */
13758
0
    "__go_go",
13759
0
  };
13760
0
      unsigned i;
13761
13762
0
      for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13763
0
  {
13764
0
    struct elf_link_hash_entry *h;
13765
0
    h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13766
0
            false, false, true);
13767
0
    htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13768
0
    if (htab->params->plt_thread_safe)
13769
0
      break;
13770
0
  }
13771
0
    }
13772
0
  stubs_always_before_branch = htab->params->group_size < 0;
13773
0
  if (htab->params->group_size < 0)
13774
0
    stub_group_size = -htab->params->group_size;
13775
0
  else
13776
0
    stub_group_size = htab->params->group_size;
13777
13778
0
  if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13779
0
    return false;
13780
13781
0
  htab->tga_group = NULL;
13782
0
  if (!htab->params->no_tls_get_addr_regsave
13783
0
      && htab->tga_desc_fd != NULL
13784
0
      && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13785
0
    || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13786
0
      && htab->tls_get_addr_fd != NULL
13787
0
      && is_static_defined (&htab->tls_get_addr_fd->elf))
13788
0
    {
13789
0
      asection *sym_sec, *code_sec, *stub_sec;
13790
0
      bfd_vma sym_value;
13791
0
      struct _opd_sec_data *opd;
13792
13793
0
      sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13794
0
      sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13795
0
      code_sec = sym_sec;
13796
0
      opd = get_opd_info (sym_sec);
13797
0
      if (opd != NULL)
13798
0
  opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
13799
0
      htab->tga_group = htab->sec_info[code_sec->id].u.group;
13800
0
      stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13801
0
                htab->tga_group->link_sec);
13802
0
      if (stub_sec == NULL)
13803
0
  return false;
13804
0
      htab->tga_group->stub_sec = stub_sec;
13805
13806
0
      htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13807
0
      htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13808
0
      htab->tga_desc_fd->elf.root.u.def.value = 0;
13809
0
      htab->tga_desc_fd->elf.type = STT_FUNC;
13810
0
      htab->tga_desc_fd->elf.def_regular = 1;
13811
0
      htab->tga_desc_fd->elf.non_elf = 0;
13812
0
      _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
13813
0
    }
13814
13815
  /* Loop until no stubs added.  After iteration 20 of this loop we may
13816
     exit on a stub section shrinking.  */
13817
13818
0
  while (1)
13819
0
    {
13820
0
      bfd *input_bfd;
13821
0
      struct map_stub *group;
13822
13823
0
      htab->stub_iteration += 1;
13824
0
      htab->relr_count = 0;
13825
13826
0
      for (input_bfd = info->input_bfds;
13827
0
     input_bfd != NULL;
13828
0
     input_bfd = input_bfd->link.next)
13829
0
  {
13830
0
    Elf_Internal_Shdr *symtab_hdr;
13831
0
    asection *section;
13832
0
    Elf_Internal_Sym *local_syms = NULL;
13833
13834
0
    if (!is_ppc64_elf (input_bfd))
13835
0
      continue;
13836
13837
    /* We'll need the symbol table in a second.  */
13838
0
    symtab_hdr = &elf_symtab_hdr (input_bfd);
13839
0
    if (symtab_hdr->sh_info == 0)
13840
0
      continue;
13841
13842
    /* Walk over each section attached to the input bfd.  */
13843
0
    for (section = input_bfd->sections;
13844
0
         section != NULL;
13845
0
         section = section->next)
13846
0
      {
13847
0
        Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13848
0
        bool is_opd;
13849
13850
        /* If there aren't any relocs, then there's nothing more
13851
     to do.  */
13852
0
        if ((section->flags & SEC_RELOC) == 0
13853
0
      || (section->flags & SEC_ALLOC) == 0
13854
0
      || (section->flags & SEC_LOAD) == 0
13855
0
      || section->reloc_count == 0)
13856
0
    continue;
13857
13858
0
        if (!info->enable_dt_relr
13859
0
      && (section->flags & SEC_CODE) == 0)
13860
0
    continue;
13861
13862
        /* If this section is a link-once section that will be
13863
     discarded, then don't create any stubs.  */
13864
0
        if (section->output_section == NULL
13865
0
      || section->output_section->owner != info->output_bfd)
13866
0
    continue;
13867
13868
        /* Get the relocs.  */
13869
0
        internal_relocs
13870
0
    = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13871
0
               info->keep_memory);
13872
0
        if (internal_relocs == NULL)
13873
0
    goto error_ret_free_local;
13874
13875
0
        is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13876
13877
        /* Now examine each relocation.  */
13878
0
        irela = internal_relocs;
13879
0
        irelaend = irela + section->reloc_count;
13880
0
        for (; irela < irelaend; irela++)
13881
0
    {
13882
0
      enum elf_ppc64_reloc_type r_type;
13883
0
      unsigned int r_indx;
13884
0
      struct ppc_stub_type stub_type;
13885
0
      struct ppc_stub_hash_entry *stub_entry;
13886
0
      asection *sym_sec, *code_sec;
13887
0
      bfd_vma sym_value, code_value;
13888
0
      bfd_vma destination;
13889
0
      unsigned long local_off;
13890
0
      bool ok_dest;
13891
0
      struct ppc_link_hash_entry *hash;
13892
0
      struct ppc_link_hash_entry *fdh;
13893
0
      struct elf_link_hash_entry *h;
13894
0
      Elf_Internal_Sym *sym;
13895
0
      char *stub_name;
13896
0
      const asection *id_sec;
13897
0
      struct _opd_sec_data *opd;
13898
0
      struct plt_entry *plt_ent;
13899
13900
0
      r_type = ELF64_R_TYPE (irela->r_info);
13901
0
      r_indx = ELF64_R_SYM (irela->r_info);
13902
13903
0
      if (r_type >= R_PPC64_max)
13904
0
        {
13905
0
          bfd_set_error (bfd_error_bad_value);
13906
0
          goto error_ret_free_internal;
13907
0
        }
13908
13909
      /* Only look for stubs on branch instructions.  */
13910
0
      switch (r_type)
13911
0
        {
13912
0
        default:
13913
0
          if (info->enable_dt_relr
13914
0
        && maybe_relr (r_type, irela, section))
13915
0
      break;
13916
0
          continue;
13917
13918
0
        case R_PPC64_REL24:
13919
0
        case R_PPC64_REL24_NOTOC:
13920
0
        case R_PPC64_REL24_P9NOTOC:
13921
0
        case R_PPC64_REL14:
13922
0
        case R_PPC64_REL14_BRTAKEN:
13923
0
        case R_PPC64_REL14_BRNTAKEN:
13924
0
          if ((section->flags & SEC_CODE) != 0)
13925
0
      break;
13926
0
          continue;
13927
0
        }
13928
13929
      /* Now determine the call target, its name, value,
13930
         section.  */
13931
0
      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13932
0
          r_indx, input_bfd))
13933
0
        goto error_ret_free_internal;
13934
13935
0
      if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13936
0
        {
13937
          /* Only locally defined symbols can possibly use
13938
       relative relocations.  */
13939
0
          bfd_vma r_offset;
13940
0
          if ((sym_sec == NULL
13941
0
         || sym_sec->output_section == NULL)
13942
        /* No symbol is OK too.  */
13943
0
        && !(sym != NULL && sym->st_shndx == 0)
13944
        /* Hack for __ehdr_start, which is undefined
13945
           at this point.  */
13946
0
        && !(h != NULL && h->root.linker_def))
13947
0
      continue;
13948
0
          if (NO_OPD_RELOCS && is_opd)
13949
0
      continue;
13950
0
          if (!is_opd
13951
0
        && r_type == R_PPC64_ADDR64)
13952
0
      {
13953
0
        if (h != NULL
13954
0
            ? h->type == STT_GNU_IFUNC
13955
0
            : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13956
0
          continue;
13957
0
        if (h != NULL
13958
0
            ? bfd_is_abs_symbol (&h->root)
13959
0
            : sym->st_shndx == SHN_ABS)
13960
0
          continue;
13961
0
        if (h != NULL
13962
0
            && !SYMBOL_REFERENCES_LOCAL (info, h))
13963
0
          continue;
13964
0
      }
13965
0
          r_offset = _bfd_elf_section_offset (info->output_bfd,
13966
0
                info,
13967
0
                section,
13968
0
                irela->r_offset);
13969
0
          if (r_offset >= (bfd_vma) -2)
13970
0
      continue;
13971
0
          if (!append_relr_off (htab, section, r_offset))
13972
0
      goto error_ret_free_internal;
13973
0
          continue;
13974
0
        }
13975
13976
0
      hash = ppc_elf_hash_entry (h);
13977
0
      ok_dest = false;
13978
0
      fdh = NULL;
13979
0
      sym_value = 0;
13980
0
      if (hash == NULL)
13981
0
        {
13982
0
          sym_value = sym->st_value;
13983
0
          if (sym_sec != NULL
13984
0
        && sym_sec->output_section != NULL)
13985
0
      ok_dest = true;
13986
0
        }
13987
0
      else if (hash->elf.root.type == bfd_link_hash_defined
13988
0
         || hash->elf.root.type == bfd_link_hash_defweak)
13989
0
        {
13990
0
          sym_value = hash->elf.root.u.def.value;
13991
0
          if (sym_sec->output_section != NULL)
13992
0
      ok_dest = true;
13993
0
        }
13994
0
      else if (hash->elf.root.type == bfd_link_hash_undefweak
13995
0
         || hash->elf.root.type == bfd_link_hash_undefined)
13996
0
        {
13997
          /* Recognise an old ABI func code entry sym, and
13998
       use the func descriptor sym instead if it is
13999
       defined.  */
14000
0
          if (hash->elf.root.root.string[0] == '.'
14001
0
        && hash->oh != NULL)
14002
0
      {
14003
0
        fdh = ppc_follow_link (hash->oh);
14004
0
        if (fdh->elf.root.type == bfd_link_hash_defined
14005
0
            || fdh->elf.root.type == bfd_link_hash_defweak)
14006
0
          {
14007
0
            sym_sec = fdh->elf.root.u.def.section;
14008
0
            sym_value = fdh->elf.root.u.def.value;
14009
0
            if (sym_sec->output_section != NULL)
14010
0
        ok_dest = true;
14011
0
          }
14012
0
        else
14013
0
          fdh = NULL;
14014
0
      }
14015
0
        }
14016
0
      else
14017
0
        {
14018
0
          bfd_set_error (bfd_error_bad_value);
14019
0
          goto error_ret_free_internal;
14020
0
        }
14021
14022
0
      destination = 0;
14023
0
      local_off = 0;
14024
0
      if (ok_dest)
14025
0
        {
14026
0
          sym_value += irela->r_addend;
14027
0
          destination = (sym_value
14028
0
             + sym_sec->output_offset
14029
0
             + sym_sec->output_section->vma);
14030
0
          local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
14031
0
                  ? hash->elf.other
14032
0
                  : sym->st_other);
14033
0
        }
14034
14035
0
      code_sec = sym_sec;
14036
0
      code_value = sym_value;
14037
0
      opd = get_opd_info (sym_sec);
14038
0
      if (opd != NULL)
14039
0
        {
14040
0
          bfd_vma dest;
14041
14042
0
          if (hash == NULL && opd->adjust != NULL)
14043
0
      {
14044
0
        long adjust = opd->adjust[OPD_NDX (sym_value)];
14045
0
        if (adjust == -1)
14046
0
          continue;
14047
0
        code_value += adjust;
14048
0
        sym_value += adjust;
14049
0
      }
14050
0
          dest = opd_entry_value (sym_sec, sym_value,
14051
0
                &code_sec, &code_value, false);
14052
0
          if (dest != (bfd_vma) -1)
14053
0
      {
14054
0
        destination = dest;
14055
0
        if (fdh != NULL)
14056
0
          {
14057
            /* Fixup old ABI sym to point at code
14058
         entry.  */
14059
0
            hash->elf.root.type = bfd_link_hash_defweak;
14060
0
            hash->elf.root.u.def.section = code_sec;
14061
0
            hash->elf.root.u.def.value = code_value;
14062
0
          }
14063
0
      }
14064
0
        }
14065
14066
      /* Determine what (if any) linker stub is needed.  */
14067
0
      plt_ent = NULL;
14068
0
      stub_type.main = ppc_type_of_stub (section, irela, &hash,
14069
0
                 &plt_ent, destination,
14070
0
                 local_off);
14071
0
      stub_type.sub = ppc_stub_toc;
14072
0
      stub_type.r2save = 0;
14073
14074
0
      if (r_type == R_PPC64_REL24_NOTOC
14075
0
          || r_type == R_PPC64_REL24_P9NOTOC)
14076
0
        {
14077
0
          enum ppc_stub_sub_type notoc = ppc_stub_notoc;
14078
0
          if (htab->params->power10_stubs == 0
14079
0
        || (r_type == R_PPC64_REL24_P9NOTOC
14080
0
            && htab->params->power10_stubs != 1))
14081
0
      notoc = ppc_stub_p9notoc;
14082
0
          if (stub_type.main == ppc_stub_plt_call)
14083
0
      stub_type.sub = notoc;
14084
0
          else if (stub_type.main == ppc_stub_long_branch
14085
0
             || (code_sec != NULL
14086
0
           && code_sec->output_section != NULL
14087
0
           && (((hash ? hash->elf.other : sym->st_other)
14088
0
          & STO_PPC64_LOCAL_MASK)
14089
0
               > 1 << STO_PPC64_LOCAL_BIT)))
14090
0
      {
14091
0
        stub_type.main = ppc_stub_long_branch;
14092
0
        stub_type.sub = notoc;
14093
0
        stub_type.r2save = 0;
14094
0
      }
14095
0
        }
14096
0
      else if (stub_type.main != ppc_stub_plt_call)
14097
0
        {
14098
          /* Check whether we need a TOC adjusting stub.
14099
       Since the linker pastes together pieces from
14100
       different object files when creating the
14101
       _init and _fini functions, it may be that a
14102
       call to what looks like a local sym is in
14103
       fact a call needing a TOC adjustment.  */
14104
0
          if ((code_sec != NULL
14105
0
         && code_sec->output_section != NULL
14106
0
         && (code_sec->has_toc_reloc
14107
0
             || code_sec->makes_toc_func_call)
14108
0
         && (htab->sec_info[code_sec->id].toc_off
14109
0
             != htab->sec_info[section->id].toc_off))
14110
0
        || (((hash ? hash->elf.other : sym->st_other)
14111
0
             & STO_PPC64_LOCAL_MASK)
14112
0
            == 1 << STO_PPC64_LOCAL_BIT))
14113
0
      {
14114
0
        stub_type.main = ppc_stub_long_branch;
14115
0
        stub_type.sub = ppc_stub_toc;
14116
0
        stub_type.r2save = 1;
14117
0
      }
14118
0
        }
14119
14120
0
      if (stub_type.main == ppc_stub_none)
14121
0
        continue;
14122
14123
      /* __tls_get_addr calls might be eliminated.  */
14124
0
      if (stub_type.main != ppc_stub_plt_call
14125
0
          && hash != NULL
14126
0
          && is_tls_get_addr (&hash->elf, htab)
14127
0
          && section->has_tls_reloc
14128
0
          && irela != internal_relocs)
14129
0
        {
14130
          /* Get tls info.  */
14131
0
          unsigned char *tls_mask;
14132
14133
0
          if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
14134
0
           irela - 1, input_bfd))
14135
0
      goto error_ret_free_internal;
14136
0
          if ((*tls_mask & TLS_TLS) != 0
14137
0
        && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
14138
0
      continue;
14139
0
        }
14140
14141
0
      if (stub_type.main == ppc_stub_plt_call
14142
0
          && stub_type.sub == ppc_stub_toc)
14143
0
        {
14144
0
          if (!htab->opd_abi
14145
0
        && htab->params->plt_localentry0 != 0
14146
0
        && is_elfv2_localentry0 (&hash->elf))
14147
0
      htab->has_plt_localentry0 = 1;
14148
0
          else if (irela + 1 < irelaend
14149
0
             && irela[1].r_offset == irela->r_offset + 4
14150
0
             && (ELF64_R_TYPE (irela[1].r_info)
14151
0
           == R_PPC64_TOCSAVE))
14152
0
      {
14153
0
        if (!tocsave_find (htab, INSERT,
14154
0
               &local_syms, irela + 1, input_bfd))
14155
0
          goto error_ret_free_internal;
14156
0
      }
14157
0
          else
14158
0
      stub_type.r2save = 1;
14159
0
        }
14160
14161
      /* Support for grouping stub sections.  */
14162
0
      id_sec = htab->sec_info[section->id].u.group->link_sec;
14163
14164
      /* Get the name of this stub.  */
14165
0
      stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14166
0
      if (!stub_name)
14167
0
        goto error_ret_free_internal;
14168
14169
0
      stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
14170
0
                 stub_name, false, false);
14171
0
      if (stub_entry != NULL)
14172
0
        {
14173
0
          free (stub_name);
14174
0
          if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
14175
0
      {
14176
        /* xgettext:c-format */
14177
0
        _bfd_error_handler
14178
0
          (_("%pB: cannot create stub entry %s"),
14179
0
           section->owner, stub_entry->root.string);
14180
0
        goto error_ret_free_internal;
14181
0
      }
14182
0
          continue;
14183
0
        }
14184
14185
0
      stub_entry = ppc_add_stub (stub_name, section, info);
14186
0
      free (stub_name);
14187
0
      if (stub_entry == NULL)
14188
0
        {
14189
0
        error_ret_free_internal:
14190
0
          if (elf_section_data (section)->relocs == NULL)
14191
0
      free (internal_relocs);
14192
0
        error_ret_free_local:
14193
0
          if (symtab_hdr->contents
14194
0
        != (unsigned char *) local_syms)
14195
0
      free (local_syms);
14196
0
          return false;
14197
0
        }
14198
14199
0
      stub_entry->type = stub_type;
14200
0
      if (stub_type.main == ppc_stub_plt_call)
14201
0
        {
14202
0
          stub_entry->target_value = sym_value;
14203
0
          stub_entry->target_section = sym_sec;
14204
0
        }
14205
0
      else
14206
0
        {
14207
0
          stub_entry->target_value = code_value;
14208
0
          stub_entry->target_section = code_sec;
14209
0
        }
14210
0
      stub_entry->h = hash;
14211
0
      stub_entry->plt_ent = plt_ent;
14212
0
      stub_entry->symtype
14213
0
        = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
14214
0
      stub_entry->other = hash ? hash->elf.other : sym->st_other;
14215
14216
0
      if (hash != NULL
14217
0
          && (hash->elf.root.type == bfd_link_hash_defined
14218
0
        || hash->elf.root.type == bfd_link_hash_defweak))
14219
0
        htab->stub_globals += 1;
14220
0
    }
14221
14222
        /* We're done with the internal relocs, free them.  */
14223
0
        if (elf_section_data (section)->relocs != internal_relocs)
14224
0
    free (internal_relocs);
14225
0
      }
14226
14227
0
    if (local_syms != NULL
14228
0
        && symtab_hdr->contents != (unsigned char *) local_syms)
14229
0
      {
14230
0
        if (!info->keep_memory)
14231
0
    free (local_syms);
14232
0
        else
14233
0
    symtab_hdr->contents = (unsigned char *) local_syms;
14234
0
      }
14235
0
  }
14236
14237
      /* We may have added some stubs.  Find out the new size of the
14238
   stub sections.  */
14239
0
      for (group = htab->group; group != NULL; group = group->next)
14240
0
  {
14241
0
    group->lr_restore = 0;
14242
0
    group->eh_size = 0;
14243
0
    if (group->stub_sec != NULL)
14244
0
      {
14245
0
        asection *stub_sec = group->stub_sec;
14246
14247
0
        stub_sec->rawsize = stub_sec->size;
14248
0
        stub_sec->size = 0;
14249
0
        stub_sec->reloc_count = 0;
14250
0
        stub_sec->flags &= ~SEC_RELOC;
14251
0
      }
14252
0
  }
14253
0
      if (htab->tga_group != NULL)
14254
0
  {
14255
    /* See emit_tga_desc and emit_tga_desc_eh_frame.  */
14256
0
    htab->tga_group->eh_size
14257
0
      = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14258
0
    htab->tga_group->lr_restore = 23 * 4;
14259
0
    htab->tga_group->stub_sec->size = 24 * 4;
14260
0
  }
14261
14262
0
      htab->brlt->rawsize = htab->brlt->size;
14263
0
      htab->brlt->size = 0;
14264
0
      htab->brlt->reloc_count = 0;
14265
0
      htab->brlt->flags &= ~SEC_RELOC;
14266
0
      if (htab->relbrlt != NULL)
14267
0
  htab->relbrlt->size = 0;
14268
14269
0
      if (htab->elf.srelrdyn != NULL)
14270
0
  {
14271
0
    htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
14272
0
    htab->elf.srelrdyn->size = 0;
14273
0
  }
14274
14275
0
      htab->stub_changed = false;
14276
0
      htab->stub_id = 0;
14277
0
      bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
14278
14279
0
      for (group = htab->group; group != NULL; group = group->next)
14280
0
  if (group->needs_save_res)
14281
0
    group->stub_sec->size += htab->sfpr->size;
14282
14283
0
      if (info->emitrelocations
14284
0
    && htab->glink != NULL && htab->glink->size != 0)
14285
0
  {
14286
0
    htab->glink->reloc_count = 1;
14287
0
    htab->glink->flags |= SEC_RELOC;
14288
0
  }
14289
14290
0
      if (htab->glink_eh_frame != NULL
14291
0
    && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
14292
0
    && htab->glink_eh_frame->output_section->size > 8)
14293
0
  {
14294
0
    size_t size = 0, align = 4;
14295
14296
0
    for (group = htab->group; group != NULL; group = group->next)
14297
0
      if (group->eh_size != 0)
14298
0
        size += (group->eh_size + 17 + align - 1) & -align;
14299
0
    if (htab->glink != NULL && htab->glink->size != 0)
14300
0
      size += (24 + align - 1) & -align;
14301
0
    if (size != 0)
14302
0
      size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14303
0
    align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14304
0
    size = (size + align - 1) & -align;
14305
0
    htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14306
0
    htab->glink_eh_frame->size = size;
14307
0
  }
14308
14309
0
      if (htab->params->plt_stub_align != 0)
14310
0
  for (group = htab->group; group != NULL; group = group->next)
14311
0
    if (group->stub_sec != NULL)
14312
0
      {
14313
0
        int align = abs (htab->params->plt_stub_align);
14314
0
        group->stub_sec->size
14315
0
    = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14316
0
      }
14317
14318
0
      if (htab->elf.srelrdyn != NULL)
14319
0
  {
14320
0
    bfd_vma r_offset;
14321
14322
0
    for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
14323
0
      if (!append_relr_off (htab, htab->brlt, r_offset))
14324
0
        return false;
14325
14326
0
    if (!got_and_plt_relr_for_local_syms (info))
14327
0
      return false;
14328
0
    elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14329
0
    if (htab->stub_error)
14330
0
      return false;
14331
14332
0
    bfd_vma *relr_addr = sort_relr (htab);
14333
0
    if (htab->relr_count != 0 && relr_addr == NULL)
14334
0
      return false;
14335
14336
0
    size_t i = 0;
14337
0
    while (i < htab->relr_count)
14338
0
      {
14339
0
        bfd_vma base = relr_addr[i];
14340
0
        htab->elf.srelrdyn->size += 8;
14341
0
        i++;
14342
        /* Handle possible duplicate address.  This can happen
14343
     as sections increase in size when adding stubs.  */
14344
0
        while (i < htab->relr_count
14345
0
         && relr_addr[i] == base)
14346
0
    i++;
14347
0
        base += 8;
14348
0
        while (1)
14349
0
    {
14350
0
      size_t start_i = i;
14351
0
      while (i < htab->relr_count
14352
0
       && relr_addr[i] - base < 63 * 8
14353
0
       && (relr_addr[i] - base) % 8 == 0)
14354
0
        i++;
14355
0
      if (i == start_i)
14356
0
        break;
14357
0
      htab->elf.srelrdyn->size += 8;
14358
0
      base += 63 * 8;
14359
0
    }
14360
0
      }
14361
0
    free (relr_addr);
14362
0
  }
14363
14364
0
      for (group = htab->group; group != NULL; group = group->next)
14365
0
  if (group->stub_sec != NULL
14366
0
      && group->stub_sec->rawsize != group->stub_sec->size
14367
0
      && (htab->stub_iteration <= STUB_SHRINK_ITER
14368
0
    || group->stub_sec->rawsize < group->stub_sec->size))
14369
0
    break;
14370
14371
0
      if (group == NULL
14372
0
    && (!htab->stub_changed
14373
0
        || htab->stub_iteration > STUB_SHRINK_ITER)
14374
0
    && (htab->brlt->rawsize == htab->brlt->size
14375
0
        || (htab->stub_iteration > STUB_SHRINK_ITER
14376
0
      && htab->brlt->rawsize > htab->brlt->size))
14377
0
    && (htab->elf.srelrdyn == NULL
14378
0
        || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14379
0
        || (htab->stub_iteration > STUB_SHRINK_ITER
14380
0
      && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
14381
0
    && (htab->glink_eh_frame == NULL
14382
0
        || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14383
0
    && (htab->tga_group == NULL
14384
0
        || htab->stub_iteration > 1))
14385
0
  break;
14386
14387
0
      if (htab->stub_iteration > STUB_SHRINK_ITER)
14388
0
  {
14389
0
    for (group = htab->group; group != NULL; group = group->next)
14390
0
      if (group->stub_sec != NULL
14391
0
    && group->stub_sec->size < group->stub_sec->rawsize)
14392
0
        group->stub_sec->size = group->stub_sec->rawsize;
14393
14394
0
    if (htab->brlt->size < htab->brlt->rawsize)
14395
0
      htab->brlt->size = htab->brlt->rawsize;
14396
14397
0
    if (htab->elf.srelrdyn != NULL
14398
0
        && htab->elf.srelrdyn->size < htab->elf.srelrdyn->rawsize)
14399
0
      htab->elf.srelrdyn->size = htab->elf.srelrdyn->rawsize;
14400
0
  }
14401
14402
      /* Ask the linker to do its stuff.  */
14403
0
      (*htab->params->layout_sections_again) ();
14404
0
    }
14405
14406
0
  if (htab->glink_eh_frame != NULL
14407
0
      && htab->glink_eh_frame->size != 0)
14408
0
    {
14409
0
      bfd_vma val;
14410
0
      bfd_byte *p, *last_fde;
14411
0
      size_t last_fde_len, size, align, pad;
14412
0
      struct map_stub *group;
14413
14414
      /* It is necessary to at least have a rough outline of the
14415
   linker generated CIEs and FDEs written before
14416
   bfd_elf_discard_info is run, in order for these FDEs to be
14417
   indexed in .eh_frame_hdr.  */
14418
0
      p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14419
0
      if (p == NULL)
14420
0
  return false;
14421
0
      htab->glink_eh_frame->contents = p;
14422
0
      htab->glink_eh_frame->alloced = 1;
14423
0
      last_fde = p;
14424
0
      align = 4;
14425
14426
0
      memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14427
      /* CIE length (rewrite in case little-endian).  */
14428
0
      last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
14429
0
      bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14430
0
      p += last_fde_len + 4;
14431
14432
0
      for (group = htab->group; group != NULL; group = group->next)
14433
0
  if (group->eh_size != 0)
14434
0
    {
14435
0
      group->eh_base = p - htab->glink_eh_frame->contents;
14436
0
      last_fde = p;
14437
0
      last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
14438
      /* FDE length.  */
14439
0
      bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14440
0
      p += 4;
14441
      /* CIE pointer.  */
14442
0
      val = p - htab->glink_eh_frame->contents;
14443
0
      bfd_put_32 (htab->elf.dynobj, val, p);
14444
0
      p += 4;
14445
      /* Offset to stub section, written later.  */
14446
0
      p += 4;
14447
      /* stub section size.  */
14448
0
      bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
14449
0
      p += 4;
14450
      /* Augmentation.  */
14451
0
      p += 1;
14452
      /* Make sure we don't have all nops.  This is enough for
14453
         elf-eh-frame.c to detect the last non-nop opcode.  */
14454
0
      p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
14455
0
      p = last_fde + last_fde_len + 4;
14456
0
    }
14457
0
      if (htab->glink != NULL && htab->glink->size != 0)
14458
0
  {
14459
0
    last_fde = p;
14460
0
    last_fde_len = ((24 + align - 1) & -align) - 4;
14461
    /* FDE length.  */
14462
0
    bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14463
0
    p += 4;
14464
    /* CIE pointer.  */
14465
0
    val = p - htab->glink_eh_frame->contents;
14466
0
    bfd_put_32 (htab->elf.dynobj, val, p);
14467
0
    p += 4;
14468
    /* Offset to .glink, written later.  */
14469
0
    p += 4;
14470
    /* .glink size.  */
14471
0
    bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14472
0
    p += 4;
14473
    /* Augmentation.  */
14474
0
    p += 1;
14475
14476
0
    *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
14477
0
    *p++ = DW_CFA_register;
14478
0
    *p++ = 65;
14479
0
    *p++ = htab->opd_abi ? 12 : 0;
14480
0
    *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
14481
0
    *p++ = DW_CFA_restore_extended;
14482
0
    *p++ = 65;
14483
0
    p += ((24 + align - 1) & -align) - 24;
14484
0
  }
14485
      /* Subsume any padding into the last FDE if user .eh_frame
14486
   sections are aligned more than glink_eh_frame.  Otherwise any
14487
   zero padding will be seen as a terminator.  */
14488
0
      align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14489
0
      size = p - htab->glink_eh_frame->contents;
14490
0
      pad = ((size + align - 1) & -align) - size;
14491
0
      htab->glink_eh_frame->size = size + pad;
14492
0
      bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14493
0
    }
14494
14495
0
  maybe_strip_output (info, htab->brlt);
14496
0
  if (htab->relbrlt != NULL)
14497
0
    maybe_strip_output (info, htab->relbrlt);
14498
0
  if (htab->glink_eh_frame != NULL)
14499
0
    maybe_strip_output (info, htab->glink_eh_frame);
14500
0
  if (htab->elf.srelrdyn != NULL)
14501
0
    maybe_strip_output (info, htab->elf.srelrdyn);
14502
14503
0
  return true;
14504
0
}
14505
14506
/* Called after we have determined section placement.  If sections
14507
   move, we'll be called again.  Provide a value for TOCstart.  */
14508
14509
bfd_vma
14510
ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
14511
28
{
14512
28
  asection *s;
14513
28
  bfd_vma TOCstart, adjust;
14514
14515
28
  if (info != NULL)
14516
0
    {
14517
0
      struct elf_link_hash_entry *h;
14518
0
      struct elf_link_hash_table *htab = elf_hash_table (info);
14519
14520
0
      if (is_elf_hash_table (&htab->root)
14521
0
    && htab->hgot != NULL)
14522
0
  h = htab->hgot;
14523
0
      else
14524
0
  {
14525
0
    h = (struct elf_link_hash_entry *)
14526
0
      bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14527
0
    if (is_elf_hash_table (&htab->root))
14528
0
      htab->hgot = h;
14529
0
  }
14530
0
      if (h != NULL
14531
0
    && h->root.type == bfd_link_hash_defined
14532
0
    && !h->root.linker_def
14533
0
    && (!is_elf_hash_table (&htab->root)
14534
0
        || h->def_regular))
14535
0
  {
14536
0
    TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
14537
0
    _bfd_set_gp_value (obfd, TOCstart);
14538
0
    return TOCstart;
14539
0
  }
14540
0
    }
14541
14542
  /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14543
     order.  The TOC starts where the first of these sections starts.  */
14544
28
  s = bfd_get_section_by_name (obfd, ".got");
14545
28
  if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14546
28
    s = bfd_get_section_by_name (obfd, ".toc");
14547
28
  if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14548
28
    s = bfd_get_section_by_name (obfd, ".tocbss");
14549
28
  if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14550
28
    s = bfd_get_section_by_name (obfd, ".plt");
14551
28
  if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14552
28
    {
14553
      /* This may happen for
14554
   o  references to TOC base (SYM@toc / TOC[tc0]) without a
14555
   .toc directive
14556
   o  bad linker script
14557
   o --gc-sections and empty TOC sections
14558
14559
   FIXME: Warn user?  */
14560
14561
      /* Look for a likely section.  We probably won't even be
14562
   using TOCstart.  */
14563
532
      for (s = obfd->sections; s != NULL; s = s->next)
14564
504
  if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14565
504
       | SEC_EXCLUDE))
14566
504
      == (SEC_ALLOC | SEC_SMALL_DATA))
14567
0
    break;
14568
28
      if (s == NULL)
14569
532
  for (s = obfd->sections; s != NULL; s = s->next)
14570
504
    if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
14571
504
        == (SEC_ALLOC | SEC_SMALL_DATA))
14572
0
      break;
14573
28
      if (s == NULL)
14574
90
  for (s = obfd->sections; s != NULL; s = s->next)
14575
89
    if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14576
89
        == SEC_ALLOC)
14577
27
      break;
14578
28
      if (s == NULL)
14579
1
  for (s = obfd->sections; s != NULL; s = s->next)
14580
1
    if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
14581
1
      break;
14582
28
    }
14583
14584
28
  TOCstart = 0;
14585
28
  if (s != NULL)
14586
28
    TOCstart = s->output_section->vma + s->output_offset;
14587
14588
  /* Force alignment.  */
14589
28
  adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14590
28
  TOCstart -= adjust;
14591
28
  _bfd_set_gp_value (obfd, TOCstart);
14592
14593
28
  if (info != NULL && s != NULL)
14594
0
    {
14595
0
      struct ppc_link_hash_table *htab = ppc_hash_table (info);
14596
14597
0
      if (htab != NULL)
14598
0
  {
14599
0
    if (htab->elf.hgot != NULL)
14600
0
      {
14601
0
        htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
14602
0
        htab->elf.hgot->root.u.def.section = s;
14603
0
      }
14604
0
  }
14605
0
      else
14606
0
  {
14607
0
    struct bfd_link_hash_entry *bh = NULL;
14608
0
    _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
14609
0
              s, TOC_BASE_OFF - adjust,
14610
0
              NULL, false, false, &bh);
14611
0
  }
14612
0
    }
14613
28
  return TOCstart;
14614
28
}
14615
14616
/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
14617
   write out any global entry stubs, and PLT relocations.  */
14618
14619
static bool
14620
build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
14621
0
{
14622
0
  struct bfd_link_info *info;
14623
0
  struct ppc_link_hash_table *htab;
14624
0
  struct plt_entry *ent;
14625
0
  asection *s;
14626
14627
0
  if (h->root.type == bfd_link_hash_indirect)
14628
0
    return true;
14629
14630
0
  info = inf;
14631
0
  htab = ppc_hash_table (info);
14632
0
  if (htab == NULL)
14633
0
    return false;
14634
14635
0
  for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14636
0
    if (ent->plt.offset != (bfd_vma) -1)
14637
0
      {
14638
  /* This symbol has an entry in the procedure linkage
14639
     table.  Set it up.  */
14640
0
  Elf_Internal_Rela rela;
14641
0
  asection *plt, *relplt;
14642
0
  bfd_byte *loc;
14643
14644
0
  if (use_local_plt (info, h))
14645
0
    {
14646
0
      if (!(h->def_regular
14647
0
      && (h->root.type == bfd_link_hash_defined
14648
0
          || h->root.type == bfd_link_hash_defweak)))
14649
0
        continue;
14650
0
      if (h->type == STT_GNU_IFUNC)
14651
0
        {
14652
0
    plt = htab->elf.iplt;
14653
0
    relplt = htab->elf.irelplt;
14654
0
    htab->elf.ifunc_resolvers = true;
14655
0
    if (htab->opd_abi)
14656
0
      rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14657
0
    else
14658
0
      rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14659
0
        }
14660
0
      else
14661
0
        {
14662
0
    plt = htab->pltlocal;
14663
0
    relplt = NULL;
14664
0
    if (bfd_link_pic (info)
14665
0
        && !(info->enable_dt_relr && !htab->opd_abi))
14666
0
      {
14667
0
        relplt = htab->relpltlocal;
14668
0
        if (htab->opd_abi)
14669
0
          rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14670
0
        else
14671
0
          rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14672
0
      }
14673
0
        }
14674
0
      rela.r_addend = defined_sym_val (h) + ent->addend;
14675
14676
0
      if (relplt == NULL)
14677
0
        {
14678
0
    loc = plt->contents + ent->plt.offset;
14679
0
    bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14680
0
    if (htab->opd_abi)
14681
0
      {
14682
0
        bfd_vma toc = elf_gp (info->output_bfd);
14683
0
        toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14684
0
        bfd_put_64 (info->output_bfd, toc, loc + 8);
14685
0
      }
14686
0
        }
14687
0
      else
14688
0
        {
14689
0
    rela.r_offset = (plt->output_section->vma
14690
0
         + plt->output_offset
14691
0
         + ent->plt.offset);
14692
0
    BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd, &rela,
14693
0
                  relplt));
14694
0
        }
14695
0
    }
14696
0
  else
14697
0
    {
14698
0
      rela.r_offset = (htab->elf.splt->output_section->vma
14699
0
           + htab->elf.splt->output_offset
14700
0
           + ent->plt.offset);
14701
0
      rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14702
0
      rela.r_addend = ent->addend;
14703
0
      loc = (htab->elf.srelplt->contents
14704
0
       + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14705
0
          / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14706
0
      if (h->type == STT_GNU_IFUNC && is_static_defined (h))
14707
0
        htab->elf.ifunc_resolvers = true;
14708
0
      BFD_ASSERT (swap_reloc_out (info->output_bfd, &rela,
14709
0
          loc, htab->elf.srelplt));
14710
0
    }
14711
0
      }
14712
14713
0
  if (!h->pointer_equality_needed)
14714
0
    return true;
14715
14716
0
  if (h->def_regular)
14717
0
    return true;
14718
14719
0
  s = htab->global_entry;
14720
0
  if (s == NULL || s->size == 0)
14721
0
    return true;
14722
14723
0
  for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14724
0
    if (ent->plt.offset != (bfd_vma) -1
14725
0
  && ent->addend == 0)
14726
0
      {
14727
0
  bfd_byte *p;
14728
0
  asection *plt;
14729
0
  bfd_vma off;
14730
14731
0
  p = s->contents + h->root.u.def.value;
14732
0
  plt = htab->elf.splt;
14733
0
  if (use_local_plt (info, h))
14734
0
    {
14735
0
      if (h->type == STT_GNU_IFUNC)
14736
0
        plt = htab->elf.iplt;
14737
0
      else
14738
0
        plt = htab->pltlocal;
14739
0
    }
14740
0
  off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
14741
0
  off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14742
14743
0
  if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14744
0
    {
14745
0
      info->callbacks->einfo
14746
0
        (_("%P: linkage table error against `%pT'\n"),
14747
0
         h->root.root.string);
14748
0
      bfd_set_error (bfd_error_bad_value);
14749
0
      htab->stub_error = true;
14750
0
    }
14751
14752
0
  htab->stub_count[ppc_stub_global_entry - 1] += 1;
14753
0
  if (htab->params->emit_stub_syms)
14754
0
    {
14755
0
      size_t len = strlen (h->root.root.string);
14756
0
      char *name = bfd_alloc (info->output_bfd,
14757
0
            sizeof "12345678.global_entry." + len);
14758
14759
0
      if (name == NULL)
14760
0
        return false;
14761
14762
0
      sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14763
0
      h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
14764
0
      if (h == NULL)
14765
0
        return false;
14766
0
      if (h->root.type == bfd_link_hash_new)
14767
0
        {
14768
0
    h->root.type = bfd_link_hash_defined;
14769
0
    h->root.u.def.section = s;
14770
0
    h->root.u.def.value = p - s->contents;
14771
0
    h->ref_regular = 1;
14772
0
    h->def_regular = 1;
14773
0
    h->ref_regular_nonweak = 1;
14774
0
    h->forced_local = 1;
14775
0
    h->non_elf = 0;
14776
0
    h->root.linker_def = 1;
14777
0
        }
14778
0
    }
14779
14780
0
  if (PPC_HA (off) != 0)
14781
0
    {
14782
0
      bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14783
0
      p += 4;
14784
0
    }
14785
0
  bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14786
0
  p += 4;
14787
0
  bfd_put_32 (s->owner, MTCTR_R12, p);
14788
0
  p += 4;
14789
0
  bfd_put_32 (s->owner, BCTR, p);
14790
0
  break;
14791
0
      }
14792
0
  return true;
14793
0
}
14794
14795
/* Write PLT relocs for locals.  */
14796
14797
static bool
14798
write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14799
0
{
14800
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
14801
0
  bfd *ibfd;
14802
14803
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14804
0
    {
14805
0
      struct got_entry **lgot_ents, **end_lgot_ents;
14806
0
      struct plt_entry **local_plt, **lplt, **end_local_plt;
14807
0
      Elf_Internal_Shdr *symtab_hdr;
14808
0
      bfd_size_type locsymcount;
14809
0
      Elf_Internal_Sym *local_syms = NULL;
14810
0
      struct plt_entry *ent;
14811
14812
0
      if (!is_ppc64_elf (ibfd))
14813
0
  continue;
14814
14815
0
      lgot_ents = elf_local_got_ents (ibfd);
14816
0
      if (!lgot_ents)
14817
0
  continue;
14818
14819
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
14820
0
      locsymcount = symtab_hdr->sh_info;
14821
0
      end_lgot_ents = lgot_ents + locsymcount;
14822
0
      local_plt = (struct plt_entry **) end_lgot_ents;
14823
0
      end_local_plt = local_plt + locsymcount;
14824
0
      for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14825
0
  for (ent = *lplt; ent != NULL; ent = ent->next)
14826
0
    if (ent->plt.offset != (bfd_vma) -1)
14827
0
      {
14828
0
        Elf_Internal_Sym *sym;
14829
0
        asection *sym_sec;
14830
0
        asection *plt, *relplt;
14831
0
        bfd_vma val;
14832
14833
0
        if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14834
0
            lplt - local_plt, ibfd))
14835
0
    {
14836
0
      if (symtab_hdr->contents != (unsigned char *) local_syms)
14837
0
        free (local_syms);
14838
0
      return false;
14839
0
    }
14840
14841
0
        val = sym->st_value + ent->addend;
14842
0
        if (sym_sec != NULL && sym_sec->output_section != NULL)
14843
0
    val += sym_sec->output_offset + sym_sec->output_section->vma;
14844
14845
0
        if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14846
0
    {
14847
0
      htab->elf.ifunc_resolvers = true;
14848
0
      plt = htab->elf.iplt;
14849
0
      relplt = htab->elf.irelplt;
14850
0
    }
14851
0
        else
14852
0
    {
14853
0
      plt = htab->pltlocal;
14854
0
      relplt = NULL;
14855
0
      if (bfd_link_pic (info)
14856
0
          && !(info->enable_dt_relr && !htab->opd_abi))
14857
0
        relplt = htab->relpltlocal;
14858
0
    }
14859
14860
0
        if (relplt == NULL)
14861
0
    {
14862
0
      bfd_byte *loc = plt->contents + ent->plt.offset;
14863
0
      bfd_put_64 (info->output_bfd, val, loc);
14864
0
      if (htab->opd_abi)
14865
0
        {
14866
0
          bfd_vma toc = elf_gp (ibfd);
14867
0
          bfd_put_64 (info->output_bfd, toc, loc + 8);
14868
0
        }
14869
0
    }
14870
0
        else
14871
0
    {
14872
0
      Elf_Internal_Rela rela;
14873
0
      rela.r_offset = (ent->plt.offset
14874
0
           + plt->output_offset
14875
0
           + plt->output_section->vma);
14876
0
      if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14877
0
        {
14878
0
          if (htab->opd_abi)
14879
0
      rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14880
0
          else
14881
0
      rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14882
0
        }
14883
0
      else
14884
0
        {
14885
0
          if (htab->opd_abi)
14886
0
      rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14887
0
          else
14888
0
      rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14889
0
        }
14890
0
      rela.r_addend = val;
14891
0
      BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd,
14892
0
               &rela, relplt));
14893
0
    }
14894
0
      }
14895
14896
0
      if (local_syms != NULL
14897
0
    && symtab_hdr->contents != (unsigned char *) local_syms)
14898
0
  {
14899
0
    if (!info->keep_memory)
14900
0
      free (local_syms);
14901
0
    else
14902
0
      symtab_hdr->contents = (unsigned char *) local_syms;
14903
0
  }
14904
0
    }
14905
0
  return true;
14906
0
}
14907
14908
/* Emit the static wrapper function preserving registers around a
14909
   __tls_get_addr_opt call.  */
14910
14911
static bool
14912
emit_tga_desc (struct ppc_link_hash_table *htab)
14913
0
{
14914
0
  asection *stub_sec = htab->tga_group->stub_sec;
14915
0
  unsigned int cfa_updt = 11 * 4;
14916
0
  bfd_byte *p;
14917
0
  bfd_vma to, from, delta;
14918
14919
0
  BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14920
0
        && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14921
0
        && htab->tga_desc_fd->elf.root.u.def.value == 0);
14922
0
  to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14923
0
  from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14924
0
  delta = to - from;
14925
0
  if (delta + (1 << 25) >= 1 << 26)
14926
0
    {
14927
0
      _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14928
0
      htab->stub_error = true;
14929
0
      return false;
14930
0
    }
14931
14932
0
  p = stub_sec->contents;
14933
0
  p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14934
0
  bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14935
0
  p += 4;
14936
0
  p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14937
0
  return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14938
0
}
14939
14940
/* Emit eh_frame describing the static wrapper function.  */
14941
14942
static bfd_byte *
14943
emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14944
0
{
14945
0
  unsigned int cfa_updt = 11 * 4;
14946
0
  unsigned int i;
14947
14948
0
  *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14949
0
  *p++ = DW_CFA_def_cfa_offset;
14950
0
  if (htab->opd_abi)
14951
0
    {
14952
0
      *p++ = 128;
14953
0
      *p++ = 1;
14954
0
    }
14955
0
  else
14956
0
    *p++ = 96;
14957
0
  *p++ = DW_CFA_offset_extended_sf;
14958
0
  *p++ = 65;
14959
0
  *p++ = (-16 / 8) & 0x7f;
14960
0
  for (i = 4; i < 12; i++)
14961
0
    {
14962
0
      *p++ = DW_CFA_offset + i;
14963
0
      *p++ = (htab->opd_abi ? 13 : 12) - i;
14964
0
    }
14965
0
  *p++ = DW_CFA_advance_loc + 10;
14966
0
  *p++ = DW_CFA_def_cfa_offset;
14967
0
  *p++ = 0;
14968
0
  for (i = 4; i < 12; i++)
14969
0
    *p++ = DW_CFA_restore + i;
14970
0
  *p++ = DW_CFA_advance_loc + 2;
14971
0
  *p++ = DW_CFA_restore_extended;
14972
0
  *p++ = 65;
14973
0
  return p;
14974
0
}
14975
14976
/* Build all the stubs associated with the current output file.
14977
   The stubs are kept in a hash table attached to the main linker
14978
   hash table.  This function is called via gldelf64ppc_finish.  */
14979
14980
bool
14981
ppc64_elf_build_stubs (struct bfd_link_info *info,
14982
           char **stats)
14983
0
{
14984
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
14985
0
  struct map_stub *group;
14986
0
  asection *stub_sec;
14987
0
  bfd_byte *p;
14988
0
  int stub_sec_count = 0;
14989
14990
0
  if (htab == NULL)
14991
0
    return false;
14992
14993
  /* Allocate memory to hold the linker stubs.  */
14994
0
  for (group = htab->group; group != NULL; group = group->next)
14995
0
    {
14996
0
      group->eh_size = 0;
14997
0
      group->lr_restore = 0;
14998
0
      if ((stub_sec = group->stub_sec) != NULL
14999
0
    && stub_sec->size != 0)
15000
0
  {
15001
0
    stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
15002
0
             stub_sec->size);
15003
0
    if (stub_sec->contents == NULL)
15004
0
      return false;
15005
0
    stub_sec->alloced = 1;
15006
0
    stub_sec->size = 0;
15007
0
  }
15008
0
    }
15009
15010
0
  if (htab->glink != NULL && htab->glink->size != 0)
15011
0
    {
15012
0
      unsigned int indx;
15013
0
      bfd_vma plt0;
15014
15015
      /* Build the .glink plt call stub.  */
15016
0
      if (htab->params->emit_stub_syms)
15017
0
  {
15018
0
    struct elf_link_hash_entry *h;
15019
0
    h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
15020
0
            true, false, false);
15021
0
    if (h == NULL)
15022
0
      return false;
15023
0
    if (h->root.type == bfd_link_hash_new)
15024
0
      {
15025
0
        h->root.type = bfd_link_hash_defined;
15026
0
        h->root.u.def.section = htab->glink;
15027
0
        h->root.u.def.value = 8;
15028
0
        h->ref_regular = 1;
15029
0
        h->def_regular = 1;
15030
0
        h->ref_regular_nonweak = 1;
15031
0
        h->forced_local = 1;
15032
0
        h->non_elf = 0;
15033
0
        h->root.linker_def = 1;
15034
0
      }
15035
0
  }
15036
0
      plt0 = (htab->elf.splt->output_section->vma
15037
0
        + htab->elf.splt->output_offset
15038
0
        - 16);
15039
0
      if (info->emitrelocations)
15040
0
  {
15041
0
    Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
15042
0
    if (r == NULL)
15043
0
      return false;
15044
0
    r->r_offset = (htab->glink->output_offset
15045
0
       + htab->glink->output_section->vma);
15046
0
    r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
15047
0
    r->r_addend = plt0;
15048
0
  }
15049
0
      p = htab->glink->contents;
15050
0
      plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
15051
0
      bfd_put_64 (htab->glink->owner, plt0, p);
15052
0
      p += 8;
15053
0
      if (htab->opd_abi)
15054
0
  {
15055
0
    bfd_put_32 (htab->glink->owner, MFLR_R12, p);
15056
0
    p += 4;
15057
0
    bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15058
0
    p += 4;
15059
0
    bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15060
0
    p += 4;
15061
0
    bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
15062
0
    p += 4;
15063
0
    bfd_put_32 (htab->glink->owner, MTLR_R12, p);
15064
0
    p += 4;
15065
0
    bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
15066
0
    p += 4;
15067
0
    bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15068
0
    p += 4;
15069
0
    bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
15070
0
    p += 4;
15071
0
    bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15072
0
    p += 4;
15073
0
    bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
15074
0
    p += 4;
15075
0
  }
15076
0
      else
15077
0
  {
15078
0
    unsigned int insn;
15079
15080
    /* 0:
15081
       .  .quad plt0-1f   # plt0 entry relative to 1:
15082
       #
15083
       # We get here with r12 initially @ a glink branch
15084
       # Load the address of _dl_runtime_resolve from plt0 and
15085
       # jump to it, with r0 set to the index of the PLT entry
15086
       # to be resolved and r11 the link map.
15087
       __glink_PLTresolve:
15088
       .  std %r2,24(%r1)   # optional
15089
       .  mflr %r0
15090
       .  bcl 20,31,1f
15091
       1:
15092
       .  mflr %r11
15093
       .  mtlr %r0
15094
       .  ld %r0,(0b-1b)(%r11)
15095
       .  sub %r12,%r12,%r11
15096
       .  add %r11,%r0,%r11
15097
       .  addi %r0,%r12,1b-2f
15098
       .  ld %r12,0(%r11)
15099
       .  srdi %r0,%r0,2
15100
       .  mtctr %r12
15101
       .  ld %r11,8(%r11)
15102
       .  bctr
15103
       2:
15104
       .  b __glink_PLTresolve
15105
       .  ...
15106
       .  b __glink_PLTresolve  */
15107
15108
0
    if (htab->has_plt_localentry0)
15109
0
      {
15110
0
        bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
15111
0
        p += 4;
15112
0
      }
15113
0
    bfd_put_32 (htab->glink->owner, MFLR_R0, p);
15114
0
    p += 4;
15115
0
    bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15116
0
    p += 4;
15117
0
    bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15118
0
    p += 4;
15119
0
    bfd_put_32 (htab->glink->owner, MTLR_R0, p);
15120
0
    p += 4;
15121
0
    if (htab->has_plt_localentry0)
15122
0
      insn = LD_R0_0R11 | (-20 & 0xfffc);
15123
0
    else
15124
0
      insn = LD_R0_0R11 | (-16 & 0xfffc);
15125
0
    bfd_put_32 (htab->glink->owner, insn, p);
15126
0
    p += 4;
15127
0
    bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
15128
0
    p += 4;
15129
0
    bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
15130
0
    p += 4;
15131
0
    bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
15132
0
    p += 4;
15133
0
    bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15134
0
    p += 4;
15135
0
    bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
15136
0
    p += 4;
15137
0
    bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15138
0
    p += 4;
15139
0
    bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
15140
0
    p += 4;
15141
0
  }
15142
0
      bfd_put_32 (htab->glink->owner, BCTR, p);
15143
0
      p += 4;
15144
0
      BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
15145
15146
      /* Build the .glink lazy link call stubs.  */
15147
0
      indx = 0;
15148
0
      while (p < htab->glink->contents + htab->glink->size)
15149
0
  {
15150
0
    if (htab->opd_abi)
15151
0
      {
15152
0
        if (indx < 0x8000)
15153
0
    {
15154
0
      bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
15155
0
      p += 4;
15156
0
    }
15157
0
        else
15158
0
    {
15159
0
      bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
15160
0
      p += 4;
15161
0
      bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
15162
0
            p);
15163
0
      p += 4;
15164
0
    }
15165
0
      }
15166
0
    bfd_put_32 (htab->glink->owner,
15167
0
          B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
15168
0
    indx++;
15169
0
    p += 4;
15170
0
  }
15171
0
    }
15172
15173
0
  if (htab->tga_group != NULL)
15174
0
    {
15175
0
      htab->tga_group->lr_restore = 23 * 4;
15176
0
      htab->tga_group->stub_sec->size = 24 * 4;
15177
0
      if (!emit_tga_desc (htab))
15178
0
  return false;
15179
0
      if (htab->glink_eh_frame != NULL
15180
0
    && htab->glink_eh_frame->size != 0)
15181
0
  {
15182
0
    size_t align = 4;
15183
15184
0
    p = htab->glink_eh_frame->contents;
15185
0
    p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15186
0
    p += 17;
15187
0
    htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15188
0
  }
15189
0
    }
15190
15191
  /* Build .glink global entry stubs, and PLT relocs for globals.  */
15192
0
  elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15193
15194
0
  if (!write_plt_relocs_for_local_syms (info))
15195
0
    return false;
15196
15197
0
  if (htab->brlt != NULL && htab->brlt->size != 0)
15198
0
    {
15199
0
      htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
15200
0
           htab->brlt->size);
15201
0
      if (htab->brlt->contents == NULL)
15202
0
  return false;
15203
0
      htab->brlt->alloced = 1;
15204
0
    }
15205
0
  if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
15206
0
    {
15207
0
      htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
15208
0
              htab->relbrlt->size);
15209
0
      if (htab->relbrlt->contents == NULL)
15210
0
  return false;
15211
0
      htab->relbrlt->alloced = 1;
15212
0
    }
15213
15214
  /* Build the stubs as directed by the stub hash table.  */
15215
0
  htab->stub_id = 0;
15216
0
  bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
15217
15218
0
  for (group = htab->group; group != NULL; group = group->next)
15219
0
    if (group->needs_save_res)
15220
0
      group->stub_sec->size += htab->sfpr->size;
15221
15222
0
  if (htab->relbrlt != NULL)
15223
0
    htab->relbrlt->reloc_count = 0;
15224
15225
0
  if (htab->params->plt_stub_align != 0)
15226
0
    for (group = htab->group; group != NULL; group = group->next)
15227
0
      if ((stub_sec = group->stub_sec) != NULL)
15228
0
  {
15229
0
    int align = abs (htab->params->plt_stub_align);
15230
0
    stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15231
0
  }
15232
15233
0
  for (group = htab->group; group != NULL; group = group->next)
15234
0
    if (group->needs_save_res)
15235
0
      {
15236
0
  stub_sec = group->stub_sec;
15237
0
  memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15238
0
    htab->sfpr->contents, htab->sfpr->size);
15239
0
  if (htab->params->emit_stub_syms)
15240
0
    {
15241
0
      unsigned int i;
15242
15243
0
      for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15244
0
        if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
15245
0
    return false;
15246
0
    }
15247
0
      }
15248
15249
0
  if (htab->glink_eh_frame != NULL
15250
0
      && htab->glink_eh_frame->size != 0)
15251
0
    {
15252
0
      bfd_vma val;
15253
0
      size_t align = 4;
15254
15255
0
      p = htab->glink_eh_frame->contents;
15256
0
      p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15257
15258
0
      for (group = htab->group; group != NULL; group = group->next)
15259
0
  if (group->eh_size != 0)
15260
0
    {
15261
      /* Offset to stub section.  */
15262
0
      val = (group->stub_sec->output_section->vma
15263
0
       + group->stub_sec->output_offset);
15264
0
      val -= (htab->glink_eh_frame->output_section->vma
15265
0
        + htab->glink_eh_frame->output_offset
15266
0
        + (p + 8 - htab->glink_eh_frame->contents));
15267
0
      if (val + 0x80000000 > 0xffffffff)
15268
0
        {
15269
0
    _bfd_error_handler
15270
0
      (_("%s offset too large for .eh_frame sdata4 encoding"),
15271
0
       group->stub_sec->name);
15272
0
    return false;
15273
0
        }
15274
0
      bfd_put_32 (htab->elf.dynobj, val, p + 8);
15275
0
      p += (group->eh_size + 17 + 3) & -4;
15276
0
    }
15277
0
      if (htab->glink != NULL && htab->glink->size != 0)
15278
0
  {
15279
    /* Offset to .glink.  */
15280
0
    val = (htab->glink->output_section->vma
15281
0
     + htab->glink->output_offset
15282
0
     + 8);
15283
0
    val -= (htab->glink_eh_frame->output_section->vma
15284
0
      + htab->glink_eh_frame->output_offset
15285
0
      + (p + 8 - htab->glink_eh_frame->contents));
15286
0
    if (val + 0x80000000 > 0xffffffff)
15287
0
      {
15288
0
        _bfd_error_handler
15289
0
    (_("%s offset too large for .eh_frame sdata4 encoding"),
15290
0
     htab->glink->name);
15291
0
        return false;
15292
0
      }
15293
0
    bfd_put_32 (htab->elf.dynobj, val, p + 8);
15294
0
    p += (24 + align - 1) & -align;
15295
0
  }
15296
0
    }
15297
15298
0
  if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15299
0
    {
15300
0
      htab->elf.srelrdyn->contents
15301
0
  = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15302
0
      if (htab->elf.srelrdyn->contents == NULL)
15303
0
  return false;
15304
0
      htab->elf.srelrdyn->alloced = 1;
15305
15306
0
      bfd_vma *relr_addr = sort_relr (htab);
15307
0
      if (htab->relr_count != 0 && relr_addr == NULL)
15308
0
  return false;
15309
15310
0
      size_t i = 0;
15311
0
      bfd_byte *loc = htab->elf.srelrdyn->contents;
15312
0
      while (i < htab->relr_count)
15313
0
  {
15314
0
    bfd_vma base = relr_addr[i];
15315
0
    BFD_ASSERT ((base & ((1 << RELR_ALIGN) - 1)) == 0);
15316
0
    bfd_put_64 (htab->elf.dynobj, base, loc);
15317
0
    loc += 8;
15318
0
    i++;
15319
0
    while (i < htab->relr_count
15320
0
     && relr_addr[i] == base)
15321
0
      {
15322
0
        htab->stub_error = true;
15323
0
        i++;
15324
0
      }
15325
0
    base += 8;
15326
0
    while (1)
15327
0
      {
15328
0
        bfd_vma bits = 0;
15329
0
        while (i < htab->relr_count
15330
0
         && relr_addr[i] - base < 63 * 8
15331
0
         && (relr_addr[i] - base) % 8 == 0)
15332
0
    {
15333
0
      bits |= (bfd_vma) 1 << ((relr_addr[i] - base) / 8);
15334
0
      i++;
15335
0
    }
15336
0
        if (bits == 0)
15337
0
    break;
15338
0
        bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15339
0
        loc += 8;
15340
0
        base += 63 * 8;
15341
0
      }
15342
0
  }
15343
0
      free (relr_addr);
15344
      /* Pad any excess with 1's, a do-nothing encoding.  */
15345
0
      while ((size_t) (loc - htab->elf.srelrdyn->contents)
15346
0
       < htab->elf.srelrdyn->size)
15347
0
  {
15348
0
    bfd_put_64 (htab->elf.dynobj, 1, loc);
15349
0
    loc += 8;
15350
0
  }
15351
0
    }
15352
0
  free (htab->relr);
15353
0
  htab->relr = NULL;
15354
15355
0
  for (group = htab->group; group != NULL; group = group->next)
15356
0
    if ((stub_sec = group->stub_sec) != NULL)
15357
0
      {
15358
0
  stub_sec_count += 1;
15359
0
  if (stub_sec->rawsize != stub_sec->size
15360
0
      && (htab->stub_iteration <= STUB_SHRINK_ITER
15361
0
    || stub_sec->rawsize < stub_sec->size))
15362
0
    break;
15363
0
      }
15364
15365
0
  if (group != NULL)
15366
0
    htab->stub_error = true;
15367
15368
0
  if (htab->stub_error)
15369
0
    {
15370
0
      _bfd_error_handler (_("stubs don't match calculated size"));
15371
0
      return false;
15372
0
    }
15373
15374
0
  if (stats != NULL)
15375
0
    {
15376
0
      char *groupmsg;
15377
0
      if (asprintf (&groupmsg,
15378
0
        ngettext ("linker stubs in %u group",
15379
0
            "linker stubs in %u groups",
15380
0
            stub_sec_count),
15381
0
        stub_sec_count) < 0)
15382
0
  *stats = NULL;
15383
0
      else
15384
0
  {
15385
0
    if (asprintf (stats, _("%s, iter %u\n"
15386
0
         "  branch         %lu\n"
15387
0
         "  long branch    %lu\n"
15388
0
         "  plt call       %lu\n"
15389
0
         "  global entry   %lu"),
15390
0
      groupmsg, htab->stub_iteration,
15391
0
      htab->stub_count[ppc_stub_long_branch - 1],
15392
0
      htab->stub_count[ppc_stub_plt_branch - 1],
15393
0
      htab->stub_count[ppc_stub_plt_call - 1],
15394
0
      htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15395
0
      *stats = NULL;
15396
0
    free (groupmsg);
15397
0
  }
15398
0
    }
15399
0
  return true;
15400
0
}
15401
15402
/* What to do when ld finds relocations against symbols defined in
15403
   discarded sections.  */
15404
15405
static unsigned int
15406
ppc64_elf_action_discarded (asection *sec)
15407
0
{
15408
0
  if (strcmp (".opd", sec->name) == 0)
15409
0
    return 0;
15410
15411
0
  if (strcmp (".toc", sec->name) == 0)
15412
0
    return 0;
15413
15414
0
  if (strcmp (".toc1", sec->name) == 0)
15415
0
    return 0;
15416
15417
0
  return _bfd_elf_default_action_discarded (sec);
15418
0
}
15419
15420
/* These are the dynamic relocations supported by glibc.  */
15421
15422
static bool
15423
ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15424
0
{
15425
0
  switch (r_type)
15426
0
    {
15427
0
    case R_PPC64_RELATIVE:
15428
0
    case R_PPC64_NONE:
15429
0
    case R_PPC64_ADDR64:
15430
0
    case R_PPC64_GLOB_DAT:
15431
0
    case R_PPC64_IRELATIVE:
15432
0
    case R_PPC64_JMP_IREL:
15433
0
    case R_PPC64_JMP_SLOT:
15434
0
    case R_PPC64_DTPMOD64:
15435
0
    case R_PPC64_DTPREL64:
15436
0
    case R_PPC64_TPREL64:
15437
0
    case R_PPC64_TPREL16_LO_DS:
15438
0
    case R_PPC64_TPREL16_DS:
15439
0
    case R_PPC64_TPREL16:
15440
0
    case R_PPC64_TPREL16_LO:
15441
0
    case R_PPC64_TPREL16_HI:
15442
0
    case R_PPC64_TPREL16_HIGH:
15443
0
    case R_PPC64_TPREL16_HA:
15444
0
    case R_PPC64_TPREL16_HIGHA:
15445
0
    case R_PPC64_TPREL16_HIGHER:
15446
0
    case R_PPC64_TPREL16_HIGHEST:
15447
0
    case R_PPC64_TPREL16_HIGHERA:
15448
0
    case R_PPC64_TPREL16_HIGHESTA:
15449
0
    case R_PPC64_ADDR16_LO_DS:
15450
0
    case R_PPC64_ADDR16_LO:
15451
0
    case R_PPC64_ADDR16_HI:
15452
0
    case R_PPC64_ADDR16_HIGH:
15453
0
    case R_PPC64_ADDR16_HA:
15454
0
    case R_PPC64_ADDR16_HIGHA:
15455
0
    case R_PPC64_REL30:
15456
0
    case R_PPC64_COPY:
15457
0
    case R_PPC64_UADDR64:
15458
0
    case R_PPC64_UADDR32:
15459
0
    case R_PPC64_ADDR32:
15460
0
    case R_PPC64_ADDR24:
15461
0
    case R_PPC64_ADDR16:
15462
0
    case R_PPC64_UADDR16:
15463
0
    case R_PPC64_ADDR16_DS:
15464
0
    case R_PPC64_ADDR16_HIGHER:
15465
0
    case R_PPC64_ADDR16_HIGHEST:
15466
0
    case R_PPC64_ADDR16_HIGHERA:
15467
0
    case R_PPC64_ADDR16_HIGHESTA:
15468
0
    case R_PPC64_ADDR14:
15469
0
    case R_PPC64_ADDR14_BRTAKEN:
15470
0
    case R_PPC64_ADDR14_BRNTAKEN:
15471
0
    case R_PPC64_REL32:
15472
0
    case R_PPC64_REL64:
15473
0
      return true;
15474
15475
0
    default:
15476
0
      return false;
15477
0
    }
15478
0
}
15479
15480
/* The RELOCATE_SECTION function is called by the ELF backend linker
15481
   to handle the relocations for a section.
15482
15483
   The relocs are always passed as Rela structures; if the section
15484
   actually uses Rel structures, the r_addend field will always be
15485
   zero.
15486
15487
   This function is responsible for adjust the section contents as
15488
   necessary, and (if using Rela relocs and generating a
15489
   relocatable output file) adjusting the reloc addend as
15490
   necessary.
15491
15492
   This function does not have to worry about setting the reloc
15493
   address or the reloc symbol index.
15494
15495
   LOCAL_SYMS is a pointer to the swapped in local symbols.
15496
15497
   LOCAL_SECTIONS is an array giving the section in the input file
15498
   corresponding to the st_shndx field of each local symbol.
15499
15500
   The global hash table entry for the global symbols can be found
15501
   via elf_sym_hashes (input_bfd).
15502
15503
   When generating relocatable output, this function must handle
15504
   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
15505
   going to be the section symbol corresponding to the output
15506
   section, which means that the addend must be adjusted
15507
   accordingly.  */
15508
15509
static int
15510
ppc64_elf_relocate_section (struct bfd_link_info *info,
15511
          bfd *input_bfd,
15512
          asection *input_section,
15513
          bfd_byte *contents,
15514
          Elf_Internal_Rela *relocs,
15515
          Elf_Internal_Sym *local_syms,
15516
          asection **local_sections)
15517
0
{
15518
0
  struct ppc_link_hash_table *htab;
15519
0
  Elf_Internal_Shdr *symtab_hdr;
15520
0
  struct elf_link_hash_entry **sym_hashes;
15521
0
  Elf_Internal_Rela *rel;
15522
0
  Elf_Internal_Rela *wrel;
15523
0
  Elf_Internal_Rela *relend;
15524
0
  Elf_Internal_Rela outrel;
15525
0
  bfd_byte *loc;
15526
0
  struct got_entry **local_got_ents;
15527
0
  bfd_vma TOCstart;
15528
0
  bool ret = true;
15529
0
  bool is_opd;
15530
  /* Assume 'at' branch hints.  */
15531
0
  bool is_isa_v2 = true;
15532
0
  bool warned_dynamic = false;
15533
0
  bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
15534
15535
  /* Initialize howto table if needed.  */
15536
0
  if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
15537
0
    ppc_howto_init ();
15538
15539
0
  htab = ppc_hash_table (info);
15540
0
  if (htab == NULL)
15541
0
    return false;
15542
15543
  /* Don't relocate stub sections.  */
15544
0
  if (input_section->owner == htab->params->stub_bfd)
15545
0
    return true;
15546
15547
0
  if (!is_ppc64_elf (input_bfd))
15548
0
    {
15549
0
      bfd_set_error (bfd_error_wrong_format);
15550
0
      return false;
15551
0
    }
15552
15553
0
  local_got_ents = elf_local_got_ents (input_bfd);
15554
0
  TOCstart = elf_gp (info->output_bfd);
15555
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
15556
0
  sym_hashes = elf_sym_hashes (input_bfd);
15557
0
  is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
15558
15559
0
  rel = wrel = relocs;
15560
0
  relend = relocs + input_section->reloc_count;
15561
0
  for (; rel < relend; wrel++, rel++)
15562
0
    {
15563
0
      enum elf_ppc64_reloc_type r_type;
15564
0
      bfd_vma addend;
15565
0
      bfd_reloc_status_type r;
15566
0
      Elf_Internal_Sym *sym;
15567
0
      asection *sec;
15568
0
      struct elf_link_hash_entry *h_elf;
15569
0
      struct ppc_link_hash_entry *h;
15570
0
      struct ppc_link_hash_entry *fdh;
15571
0
      const char *sym_name;
15572
0
      unsigned long r_symndx, toc_symndx;
15573
0
      bfd_vma toc_addend;
15574
0
      unsigned char tls_mask, tls_gd, tls_type;
15575
0
      unsigned char sym_type;
15576
0
      bfd_vma relocation;
15577
0
      bool unresolved_reloc, save_unresolved_reloc;
15578
0
      bool warned;
15579
0
      enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
15580
0
      unsigned int insn;
15581
0
      unsigned int mask;
15582
0
      struct ppc_stub_hash_entry *stub_entry;
15583
0
      bfd_vma max_br_offset;
15584
0
      bfd_vma from;
15585
0
      Elf_Internal_Rela orig_rel;
15586
0
      reloc_howto_type *howto;
15587
0
      struct reloc_howto_struct alt_howto;
15588
0
      uint64_t pinsn;
15589
0
      bfd_vma offset;
15590
15591
0
    again:
15592
0
      orig_rel = *rel;
15593
15594
0
      r_type = ELF64_R_TYPE (rel->r_info);
15595
0
      r_symndx = ELF64_R_SYM (rel->r_info);
15596
15597
      /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15598
   symbol of the previous ADDR64 reloc.  The symbol gives us the
15599
   proper TOC base to use.  */
15600
0
      if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
15601
0
    && wrel != relocs
15602
0
    && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
15603
0
    && is_opd)
15604
0
  r_symndx = ELF64_R_SYM (wrel[-1].r_info);
15605
15606
0
      sym = NULL;
15607
0
      sec = NULL;
15608
0
      h_elf = NULL;
15609
0
      sym_name = NULL;
15610
0
      unresolved_reloc = false;
15611
0
      warned = false;
15612
15613
0
      if (r_symndx < symtab_hdr->sh_info)
15614
0
  {
15615
    /* It's a local symbol.  */
15616
0
    struct _opd_sec_data *opd;
15617
15618
0
    sym = local_syms + r_symndx;
15619
0
    sec = local_sections[r_symndx];
15620
0
    sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
15621
0
    sym_type = ELF64_ST_TYPE (sym->st_info);
15622
0
    relocation = _bfd_elf_rela_local_sym (info->output_bfd,
15623
0
            sym, &sec, rel);
15624
0
    opd = get_opd_info (sec);
15625
0
    if (opd != NULL && opd->adjust != NULL)
15626
0
      {
15627
0
        long adjust = opd->adjust[OPD_NDX (sym->st_value
15628
0
             + rel->r_addend)];
15629
0
        if (adjust == -1)
15630
0
    relocation = 0;
15631
0
        else
15632
0
    {
15633
      /* If this is a relocation against the opd section sym
15634
         and we have edited .opd, adjust the reloc addend so
15635
         that ld -r and ld --emit-relocs output is correct.
15636
         If it is a reloc against some other .opd symbol,
15637
         then the symbol value will be adjusted later.  */
15638
0
      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15639
0
        rel->r_addend += adjust;
15640
0
      else
15641
0
        relocation += adjust;
15642
0
    }
15643
0
      }
15644
0
  }
15645
0
      else
15646
0
  {
15647
0
    bool ignored;
15648
15649
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15650
0
           r_symndx, symtab_hdr, sym_hashes,
15651
0
           h_elf, sec, relocation,
15652
0
           unresolved_reloc, warned, ignored);
15653
0
    sym_name = h_elf->root.root.string;
15654
0
    sym_type = h_elf->type;
15655
0
    if (sec != NULL
15656
0
        && sec->owner == info->output_bfd
15657
0
        && strcmp (sec->name, ".opd") == 0)
15658
0
      {
15659
        /* This is a symbol defined in a linker script.  All
15660
     such are defined in output sections, even those
15661
     defined by simple assignment from a symbol defined in
15662
     an input section.  Transfer the symbol to an
15663
     appropriate input .opd section, so that a branch to
15664
     this symbol will be mapped to the location specified
15665
     by the opd entry.  */
15666
0
        struct bfd_link_order *lo;
15667
0
        for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15668
0
    if (lo->type == bfd_indirect_link_order)
15669
0
      {
15670
0
        asection *isec = lo->u.indirect.section;
15671
0
        if (h_elf->root.u.def.value >= isec->output_offset
15672
0
      && h_elf->root.u.def.value < (isec->output_offset
15673
0
                  + isec->size))
15674
0
          {
15675
0
      h_elf->root.u.def.value -= isec->output_offset;
15676
0
      h_elf->root.u.def.section = isec;
15677
0
      sec = isec;
15678
0
      break;
15679
0
          }
15680
0
      }
15681
0
      }
15682
0
  }
15683
0
      h = ppc_elf_hash_entry (h_elf);
15684
15685
0
      if (sec != NULL && discarded_section (sec))
15686
0
  {
15687
0
    if (r_type < ARRAY_SIZE (ppc64_elf_howto_table)
15688
0
        && ppc64_elf_howto_table[r_type] != NULL)
15689
0
      _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15690
0
         input_bfd, input_section,
15691
0
         contents, rel->r_offset);
15692
0
    wrel->r_offset = rel->r_offset;
15693
0
    wrel->r_info = 0;
15694
0
    wrel->r_addend = 0;
15695
15696
    /* For ld -r, remove relocations in debug sections against
15697
       symbols defined in discarded sections.  Not done for
15698
       non-debug to preserve relocs in .eh_frame which the
15699
       eh_frame editing code expects to be present.  */
15700
0
    if (bfd_link_relocatable (info)
15701
0
        && (input_section->flags & SEC_DEBUGGING))
15702
0
      wrel--;
15703
15704
0
    continue;
15705
0
  }
15706
15707
0
      if (bfd_link_relocatable (info))
15708
0
  goto copy_reloc;
15709
15710
0
      if (h != NULL && &h->elf == htab->elf.hgot)
15711
0
  {
15712
0
    relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15713
0
    sec = bfd_abs_section_ptr;
15714
0
    unresolved_reloc = false;
15715
0
  }
15716
15717
      /* TLS optimizations.  Replace instruction sequences and relocs
15718
   based on information we collected in tls_optimize.  We edit
15719
   RELOCS so that --emit-relocs will output something sensible
15720
   for the final instruction stream.  */
15721
0
      tls_mask = 0;
15722
0
      tls_gd = 0;
15723
0
      toc_symndx = 0;
15724
0
      if (h != NULL)
15725
0
  tls_mask = h->tls_mask;
15726
0
      else if (local_got_ents != NULL)
15727
0
  {
15728
0
    struct plt_entry **local_plt = (struct plt_entry **)
15729
0
      (local_got_ents + symtab_hdr->sh_info);
15730
0
    unsigned char *lgot_masks = (unsigned char *)
15731
0
      (local_plt + symtab_hdr->sh_info);
15732
0
    tls_mask = lgot_masks[r_symndx];
15733
0
  }
15734
0
      if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
15735
0
    && (r_type == R_PPC64_TLS
15736
0
        || r_type == R_PPC64_TLSGD
15737
0
        || r_type == R_PPC64_TLSLD))
15738
0
  {
15739
    /* Check for toc tls entries.  */
15740
0
    unsigned char *toc_tls;
15741
15742
0
    if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15743
0
           &local_syms, rel, input_bfd))
15744
0
      return false;
15745
15746
0
    if (toc_tls)
15747
0
      tls_mask = *toc_tls;
15748
0
  }
15749
15750
      /* Check that tls relocs are used with tls syms, and non-tls
15751
   relocs are used with non-tls syms.  */
15752
0
      if (r_symndx != STN_UNDEF
15753
0
    && r_type != R_PPC64_NONE
15754
0
    && r_type < ARRAY_SIZE (ppc64_elf_howto_table)
15755
0
    && ppc64_elf_howto_table[r_type] != NULL
15756
0
    && (h == NULL
15757
0
        || h->elf.root.type == bfd_link_hash_defined
15758
0
        || h->elf.root.type == bfd_link_hash_defweak)
15759
0
    && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
15760
0
  {
15761
0
    if ((tls_mask & TLS_TLS) != 0
15762
0
        && (r_type == R_PPC64_TLS
15763
0
      || r_type == R_PPC64_TLSGD
15764
0
      || r_type == R_PPC64_TLSLD))
15765
      /* R_PPC64_TLS is OK against a symbol in the TOC.  */
15766
0
      ;
15767
0
    else
15768
0
      info->callbacks->einfo
15769
0
        (!IS_PPC64_TLS_RELOC (r_type)
15770
         /* xgettext:c-format */
15771
0
         ? _("%H: %s used with TLS symbol `%pT'\n")
15772
         /* xgettext:c-format */
15773
0
         : _("%H: %s used with non-TLS symbol `%pT'\n"),
15774
0
         input_bfd, input_section, rel->r_offset,
15775
0
         ppc64_elf_howto_table[r_type]->name,
15776
0
         sym_name);
15777
0
  }
15778
15779
      /* Ensure reloc mapping code below stays sane.  */
15780
0
      if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15781
0
    || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15782
0
    || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
15783
0
    || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15784
0
    || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15785
0
    || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15786
0
    || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
15787
0
    || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15788
0
    || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15789
0
    || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15790
0
  abort ();
15791
15792
0
      switch (r_type)
15793
0
  {
15794
0
  default:
15795
0
    break;
15796
15797
0
  case R_PPC64_LO_DS_OPT:
15798
0
    if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15799
0
      {
15800
0
        insn = bfd_get_32 (input_bfd,
15801
0
         contents + rel->r_offset - d_offset);
15802
0
        if ((insn & (0x3fu << 26)) != 58u << 26)
15803
0
    abort ();
15804
0
        insn += (14u << 26) - (58u << 26);
15805
0
        bfd_put_32 (input_bfd, insn,
15806
0
        contents + rel->r_offset - d_offset);
15807
0
        r_type = R_PPC64_TOC16_LO;
15808
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15809
0
      }
15810
0
    break;
15811
15812
0
  case R_PPC64_TOC16:
15813
0
  case R_PPC64_TOC16_LO:
15814
0
  case R_PPC64_TOC16_DS:
15815
0
  case R_PPC64_TOC16_LO_DS:
15816
0
    {
15817
      /* Check for toc tls entries.  */
15818
0
      unsigned char *toc_tls;
15819
0
      int retval;
15820
15821
0
      retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15822
0
           &local_syms, rel, input_bfd);
15823
0
      if (retval == 0)
15824
0
        return false;
15825
15826
0
      if (toc_tls)
15827
0
        {
15828
0
    tls_mask = *toc_tls;
15829
0
    if (r_type == R_PPC64_TOC16_DS
15830
0
        || r_type == R_PPC64_TOC16_LO_DS)
15831
0
      {
15832
0
        if ((tls_mask & TLS_TLS) != 0
15833
0
      && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15834
0
          goto toctprel;
15835
0
      }
15836
0
    else
15837
0
      {
15838
        /* If we found a GD reloc pair, then we might be
15839
           doing a GD->IE transition.  */
15840
0
        if (retval == 2)
15841
0
          {
15842
0
      tls_gd = TLS_GDIE;
15843
0
      if ((tls_mask & TLS_TLS) != 0
15844
0
          && (tls_mask & TLS_GD) == 0)
15845
0
        goto tls_ldgd_opt;
15846
0
          }
15847
0
        else if (retval == 3)
15848
0
          {
15849
0
      if ((tls_mask & TLS_TLS) != 0
15850
0
          && (tls_mask & TLS_LD) == 0)
15851
0
        goto tls_ldgd_opt;
15852
0
          }
15853
0
      }
15854
0
        }
15855
0
    }
15856
0
    break;
15857
15858
0
  case R_PPC64_GOT_TPREL16_HI:
15859
0
  case R_PPC64_GOT_TPREL16_HA:
15860
0
    if ((tls_mask & TLS_TLS) != 0
15861
0
        && (tls_mask & TLS_TPREL) == 0
15862
0
        && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15863
0
      {
15864
0
        rel->r_offset -= d_offset;
15865
0
        bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15866
0
        r_type = R_PPC64_NONE;
15867
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15868
0
      }
15869
0
    break;
15870
15871
0
  case R_PPC64_GOT_TPREL16_DS:
15872
0
  case R_PPC64_GOT_TPREL16_LO_DS:
15873
0
    if ((tls_mask & TLS_TLS) != 0
15874
0
        && (tls_mask & TLS_TPREL) == 0
15875
0
        && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15876
0
      {
15877
0
      toctprel:
15878
0
        insn = bfd_get_32 (input_bfd,
15879
0
         contents + rel->r_offset - d_offset);
15880
0
        insn &= 31 << 21;
15881
0
        insn |= 0x3c0d0000; /* addis 0,13,0 */
15882
0
        bfd_put_32 (input_bfd, insn,
15883
0
        contents + rel->r_offset - d_offset);
15884
0
        r_type = R_PPC64_TPREL16_HA;
15885
0
        if (toc_symndx != 0)
15886
0
    {
15887
0
      rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15888
0
      rel->r_addend = toc_addend;
15889
      /* We changed the symbol.  Start over in order to
15890
         get h, sym, sec etc. right.  */
15891
0
      goto again;
15892
0
    }
15893
0
        else
15894
0
    rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15895
0
      }
15896
0
    break;
15897
15898
0
  case R_PPC64_GOT_TPREL_PCREL34:
15899
0
    if ((tls_mask & TLS_TLS) != 0
15900
0
        && (tls_mask & TLS_TPREL) == 0
15901
0
        && offset_in_range (input_section, rel->r_offset, 8))
15902
0
      {
15903
        /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel  */
15904
0
        pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15905
0
        pinsn <<= 32;
15906
0
        pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15907
0
        pinsn += ((2ULL << 56) + (-1ULL << 52)
15908
0
      + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15909
0
        bfd_put_32 (input_bfd, pinsn >> 32,
15910
0
        contents + rel->r_offset);
15911
0
        bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15912
0
        contents + rel->r_offset + 4);
15913
0
        r_type = R_PPC64_TPREL34;
15914
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15915
0
      }
15916
0
    break;
15917
15918
0
  case R_PPC64_TLS:
15919
0
    if ((tls_mask & TLS_TLS) != 0
15920
0
        && (tls_mask & TLS_TPREL) == 0
15921
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
15922
0
      {
15923
0
        insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15924
0
        insn = bfd_elf_ppc_at_tls_transform (insn, 13);
15925
0
        if (insn == 0)
15926
0
    break;
15927
0
        if ((rel->r_offset & 3) == 0)
15928
0
    {
15929
0
      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15930
      /* Was PPC64_TLS which sits on insn boundary, now
15931
         PPC64_TPREL16_LO which is at low-order half-word.  */
15932
0
      rel->r_offset += d_offset;
15933
0
      r_type = R_PPC64_TPREL16_LO;
15934
0
      if (toc_symndx != 0)
15935
0
        {
15936
0
          rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15937
0
          rel->r_addend = toc_addend;
15938
          /* We changed the symbol.  Start over in order to
15939
       get h, sym, sec etc. right.  */
15940
0
          goto again;
15941
0
        }
15942
0
      else
15943
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15944
0
    }
15945
0
        else if ((rel->r_offset & 3) == 1)
15946
0
    {
15947
      /* For pcrel IE to LE we already have the full
15948
         offset and thus don't need an addi here.  A nop
15949
         or mr will do.  */
15950
0
      if ((insn & (0x3fu << 26)) == 14 << 26)
15951
0
        {
15952
          /* Extract regs from addi rt,ra,si.  */
15953
0
          unsigned int rt = (insn >> 21) & 0x1f;
15954
0
          unsigned int ra = (insn >> 16) & 0x1f;
15955
0
          if (rt == ra)
15956
0
      insn = NOP;
15957
0
          else
15958
0
      {
15959
        /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs.  */
15960
0
        insn = (rt << 16) | (ra << 21) | (ra << 11);
15961
0
        insn |= (31u << 26) | (444u << 1);
15962
0
      }
15963
0
        }
15964
0
      bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
15965
0
    }
15966
0
      }
15967
0
    break;
15968
15969
0
  case R_PPC64_GOT_TLSGD16_HI:
15970
0
  case R_PPC64_GOT_TLSGD16_HA:
15971
0
    tls_gd = TLS_GDIE;
15972
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15973
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
15974
0
      goto tls_gdld_hi;
15975
0
    break;
15976
15977
0
  case R_PPC64_GOT_TLSLD16_HI:
15978
0
  case R_PPC64_GOT_TLSLD16_HA:
15979
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15980
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
15981
0
      {
15982
0
      tls_gdld_hi:
15983
0
        if ((tls_mask & tls_gd) != 0)
15984
0
    r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15985
0
        + R_PPC64_GOT_TPREL16_DS);
15986
0
        else
15987
0
    {
15988
0
      rel->r_offset -= d_offset;
15989
0
      bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15990
0
      r_type = R_PPC64_NONE;
15991
0
    }
15992
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15993
0
      }
15994
0
    break;
15995
15996
0
  case R_PPC64_GOT_TLSGD16:
15997
0
  case R_PPC64_GOT_TLSGD16_LO:
15998
0
    tls_gd = TLS_GDIE;
15999
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16000
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
16001
0
      goto tls_ldgd_opt;
16002
0
    break;
16003
16004
0
  case R_PPC64_GOT_TLSLD16:
16005
0
  case R_PPC64_GOT_TLSLD16_LO:
16006
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16007
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
16008
0
      {
16009
0
        unsigned int insn1, insn2;
16010
16011
0
      tls_ldgd_opt:
16012
0
        offset = (bfd_vma) -1;
16013
        /* If not using the newer R_PPC64_TLSGD/LD to mark
16014
     __tls_get_addr calls, we must trust that the call
16015
     stays with its arg setup insns, ie. that the next
16016
     reloc is the __tls_get_addr call associated with
16017
     the current reloc.  Edit both insns.  */
16018
0
        if (input_section->nomark_tls_get_addr
16019
0
      && rel + 1 < relend
16020
0
      && branch_reloc_hash_match (input_bfd, rel + 1,
16021
0
                htab->tls_get_addr_fd,
16022
0
                htab->tga_desc_fd,
16023
0
                htab->tls_get_addr,
16024
0
                htab->tga_desc))
16025
0
    offset = rel[1].r_offset;
16026
        /* We read the low GOT_TLS (or TOC16) insn because we
16027
     need to keep the destination reg.  It may be
16028
     something other than the usual r3, and moved to r3
16029
     before the call by intervening code.  */
16030
0
        insn1 = bfd_get_32 (input_bfd,
16031
0
          contents + rel->r_offset - d_offset);
16032
0
        if ((tls_mask & tls_gd) != 0)
16033
0
    {
16034
      /* IE */
16035
0
      insn1 &= (0x1f << 21) | (0x1f << 16);
16036
0
      insn1 |= 58u << 26; /* ld */
16037
0
      insn2 = 0x7c636a14; /* add 3,3,13 */
16038
0
      if (offset != (bfd_vma) -1)
16039
0
        rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16040
0
      if (r_type == R_PPC64_TOC16
16041
0
          || r_type == R_PPC64_TOC16_LO)
16042
0
        r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
16043
0
      else
16044
0
        r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
16045
0
            + R_PPC64_GOT_TPREL16_DS);
16046
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16047
0
    }
16048
0
        else
16049
0
    {
16050
      /* LE */
16051
0
      insn1 &= 0x1f << 21;
16052
0
      insn1 |= 0x3c0d0000;  /* addis r,13,0 */
16053
0
      insn2 = 0x38630000; /* addi 3,3,0 */
16054
0
      if (tls_gd == 0)
16055
0
        {
16056
          /* Was an LD reloc.  */
16057
0
          r_symndx = STN_UNDEF;
16058
0
          rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16059
0
        }
16060
0
      else if (toc_symndx != 0)
16061
0
        {
16062
0
          r_symndx = toc_symndx;
16063
0
          rel->r_addend = toc_addend;
16064
0
        }
16065
0
      r_type = R_PPC64_TPREL16_HA;
16066
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16067
0
      if (offset != (bfd_vma) -1)
16068
0
        {
16069
0
          rel[1].r_info = ELF64_R_INFO (r_symndx,
16070
0
                R_PPC64_TPREL16_LO);
16071
0
          rel[1].r_offset = offset + d_offset;
16072
0
          rel[1].r_addend = rel->r_addend;
16073
0
        }
16074
0
    }
16075
0
        bfd_put_32 (input_bfd, insn1,
16076
0
        contents + rel->r_offset - d_offset);
16077
0
        if (offset != (bfd_vma) -1
16078
0
      && offset_in_range (input_section, offset, 4))
16079
0
    {
16080
0
      bfd_put_32 (input_bfd, insn2, contents + offset);
16081
0
      if (offset_in_range (input_section, offset + 4, 4))
16082
0
        {
16083
0
          insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16084
0
          if (insn2 == LD_R2_0R1 + STK_TOC (htab))
16085
0
      bfd_put_32 (input_bfd, NOP, contents + offset + 4);
16086
0
        }
16087
0
    }
16088
0
        if ((tls_mask & tls_gd) == 0
16089
0
      && (tls_gd == 0 || toc_symndx != 0))
16090
0
    {
16091
      /* We changed the symbol.  Start over in order
16092
         to get h, sym, sec etc. right.  */
16093
0
      goto again;
16094
0
    }
16095
0
      }
16096
0
    break;
16097
16098
0
  case R_PPC64_GOT_TLSGD_PCREL34:
16099
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16100
0
        && offset_in_range (input_section, rel->r_offset, 8))
16101
0
      {
16102
0
        pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16103
0
        pinsn <<= 32;
16104
0
        pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16105
0
        if ((tls_mask & TLS_GDIE) != 0)
16106
0
    {
16107
      /* IE, pla -> pld  */
16108
0
      pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
16109
0
      r_type = R_PPC64_GOT_TPREL_PCREL34;
16110
0
    }
16111
0
        else
16112
0
    {
16113
      /* LE, pla pcrel -> paddi r13  */
16114
0
      pinsn += (-1ULL << 52) + (13ULL << 16);
16115
0
      r_type = R_PPC64_TPREL34;
16116
0
    }
16117
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16118
0
        bfd_put_32 (input_bfd, pinsn >> 32,
16119
0
        contents + rel->r_offset);
16120
0
        bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16121
0
        contents + rel->r_offset + 4);
16122
0
      }
16123
0
    break;
16124
16125
0
  case R_PPC64_GOT_TLSLD_PCREL34:
16126
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16127
0
        && offset_in_range (input_section, rel->r_offset, 8))
16128
0
      {
16129
0
        pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16130
0
        pinsn <<= 32;
16131
0
        pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16132
0
        pinsn += (-1ULL << 52) + (13ULL << 16);
16133
0
        bfd_put_32 (input_bfd, pinsn >> 32,
16134
0
        contents + rel->r_offset);
16135
0
        bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16136
0
        contents + rel->r_offset + 4);
16137
0
        rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16138
0
        r_symndx = STN_UNDEF;
16139
0
        r_type = R_PPC64_TPREL34;
16140
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16141
0
        goto again;
16142
0
      }
16143
0
    break;
16144
16145
0
  case R_PPC64_TLSGD:
16146
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16147
0
        && rel + 1 < relend
16148
0
        && offset_in_range (input_section, rel->r_offset,
16149
0
          is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16150
0
          ? 8 : 4))
16151
0
      {
16152
0
        unsigned int insn2;
16153
0
        enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16154
16155
0
        offset = rel->r_offset;
16156
0
        if (is_plt_seq_reloc (r_type1))
16157
0
    {
16158
0
      bfd_put_32 (info->output_bfd, NOP, contents + offset);
16159
0
      if (r_type1 == R_PPC64_PLT_PCREL34
16160
0
          || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16161
0
        bfd_put_32 (info->output_bfd, NOP, contents + offset + 4);
16162
0
      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16163
0
      break;
16164
0
    }
16165
16166
0
        if (r_type1 == R_PPC64_PLTCALL)
16167
0
    bfd_put_32 (info->output_bfd, NOP, contents + offset + 4);
16168
16169
0
        if ((tls_mask & TLS_GDIE) != 0)
16170
0
    {
16171
      /* IE */
16172
0
      r_type = R_PPC64_NONE;
16173
0
      insn2 = 0x7c636a14; /* add 3,3,13 */
16174
0
    }
16175
0
        else
16176
0
    {
16177
      /* LE */
16178
0
      if (toc_symndx != 0)
16179
0
        {
16180
0
          r_symndx = toc_symndx;
16181
0
          rel->r_addend = toc_addend;
16182
0
        }
16183
0
      if (r_type1 == R_PPC64_REL24_NOTOC
16184
0
          || r_type1 == R_PPC64_REL24_P9NOTOC
16185
0
          || r_type1 == R_PPC64_PLTCALL_NOTOC)
16186
0
        {
16187
0
          r_type = R_PPC64_NONE;
16188
0
          insn2 = NOP;
16189
0
        }
16190
0
      else
16191
0
        {
16192
0
          rel->r_offset = offset + d_offset;
16193
0
          r_type = R_PPC64_TPREL16_LO;
16194
0
          insn2 = 0x38630000; /* addi 3,3,0 */
16195
0
        }
16196
0
    }
16197
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16198
        /* Zap the reloc on the _tls_get_addr call too.  */
16199
0
        BFD_ASSERT (offset == rel[1].r_offset);
16200
0
        rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16201
0
        bfd_put_32 (input_bfd, insn2, contents + offset);
16202
0
        if ((tls_mask & TLS_GDIE) == 0
16203
0
      && toc_symndx != 0
16204
0
      && r_type != R_PPC64_NONE)
16205
0
    goto again;
16206
0
      }
16207
0
    break;
16208
16209
0
  case R_PPC64_TLSLD:
16210
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16211
0
        && rel + 1 < relend
16212
0
        && offset_in_range (input_section, rel->r_offset,
16213
0
          is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16214
0
          ? 8 : 4))
16215
0
      {
16216
0
        unsigned int insn2;
16217
0
        enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16218
16219
0
        offset = rel->r_offset;
16220
0
        if (is_plt_seq_reloc (r_type1))
16221
0
    {
16222
0
      bfd_put_32 (info->output_bfd, NOP, contents + offset);
16223
0
      if (r_type1 == R_PPC64_PLT_PCREL34
16224
0
          || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16225
0
        bfd_put_32 (info->output_bfd, NOP, contents + offset + 4);
16226
0
      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16227
0
      break;
16228
0
    }
16229
16230
0
        if (r_type1 == R_PPC64_PLTCALL)
16231
0
    bfd_put_32 (info->output_bfd, NOP, contents + offset + 4);
16232
16233
0
        if (r_type1 == R_PPC64_REL24_NOTOC
16234
0
      || r_type1 == R_PPC64_REL24_P9NOTOC
16235
0
      || r_type1 == R_PPC64_PLTCALL_NOTOC)
16236
0
    {
16237
0
      r_type = R_PPC64_NONE;
16238
0
      insn2 = NOP;
16239
0
    }
16240
0
        else
16241
0
    {
16242
0
      rel->r_offset = offset + d_offset;
16243
0
      r_symndx = STN_UNDEF;
16244
0
      r_type = R_PPC64_TPREL16_LO;
16245
0
      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16246
0
      insn2 = 0x38630000; /* addi 3,3,0 */
16247
0
    }
16248
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16249
        /* Zap the reloc on the _tls_get_addr call too.  */
16250
0
        BFD_ASSERT (offset == rel[1].r_offset);
16251
0
        rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16252
0
        bfd_put_32 (input_bfd, insn2, contents + offset);
16253
0
        if (r_type != R_PPC64_NONE)
16254
0
    goto again;
16255
0
      }
16256
0
    break;
16257
16258
0
  case R_PPC64_DTPMOD64:
16259
0
    if (rel + 1 < relend
16260
0
        && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16261
0
        && rel[1].r_offset == rel->r_offset + 8)
16262
0
      {
16263
0
        if ((tls_mask & TLS_GD) == 0
16264
0
      && offset_in_range (input_section, rel->r_offset, 8))
16265
0
    {
16266
0
      rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
16267
0
      if ((tls_mask & TLS_GDIE) != 0)
16268
0
        r_type = R_PPC64_TPREL64;
16269
0
      else
16270
0
        {
16271
0
          bfd_put_64 (info->output_bfd, 1, contents + rel->r_offset);
16272
0
          r_type = R_PPC64_NONE;
16273
0
        }
16274
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16275
0
    }
16276
0
      }
16277
0
    else
16278
0
      {
16279
0
        if ((tls_mask & TLS_LD) == 0
16280
0
      && offset_in_range (input_section, rel->r_offset, 8))
16281
0
    {
16282
0
      bfd_put_64 (info->output_bfd, 1, contents + rel->r_offset);
16283
0
      r_type = R_PPC64_NONE;
16284
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16285
0
    }
16286
0
      }
16287
0
    break;
16288
16289
0
  case R_PPC64_TPREL64:
16290
0
    if ((tls_mask & TLS_TPREL) == 0)
16291
0
      {
16292
0
        r_type = R_PPC64_NONE;
16293
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16294
0
      }
16295
0
    break;
16296
16297
0
  case R_PPC64_ENTRY:
16298
0
    relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16299
0
    if (!bfd_link_pic (info)
16300
0
        && !info->traditional_format
16301
0
        && relocation + 0x80008000 <= 0xffffffff
16302
0
        && offset_in_range (input_section, rel->r_offset, 8))
16303
0
      {
16304
0
        unsigned int insn1, insn2;
16305
16306
0
        insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16307
0
        insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16308
0
        if ((insn1 & ~0xfffc) == LD_R2_0R12
16309
0
      && insn2 == ADD_R2_R2_R12)
16310
0
    {
16311
0
      bfd_put_32 (input_bfd,
16312
0
            LIS_R2 + PPC_HA (relocation),
16313
0
            contents + rel->r_offset);
16314
0
      bfd_put_32 (input_bfd,
16315
0
            ADDI_R2_R2 + PPC_LO (relocation),
16316
0
            contents + rel->r_offset + 4);
16317
0
    }
16318
0
      }
16319
0
    else
16320
0
      {
16321
0
        relocation -= (rel->r_offset
16322
0
           + input_section->output_offset
16323
0
           + input_section->output_section->vma);
16324
0
        if (relocation + 0x80008000 <= 0xffffffff
16325
0
      && offset_in_range (input_section, rel->r_offset, 8))
16326
0
    {
16327
0
      unsigned int insn1, insn2;
16328
16329
0
      insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16330
0
      insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16331
0
      if ((insn1 & ~0xfffc) == LD_R2_0R12
16332
0
          && insn2 == ADD_R2_R2_R12)
16333
0
        {
16334
0
          bfd_put_32 (input_bfd,
16335
0
          ADDIS_R2_R12 + PPC_HA (relocation),
16336
0
          contents + rel->r_offset);
16337
0
          bfd_put_32 (input_bfd,
16338
0
          ADDI_R2_R2 + PPC_LO (relocation),
16339
0
          contents + rel->r_offset + 4);
16340
0
        }
16341
0
    }
16342
0
      }
16343
0
    break;
16344
16345
0
  case R_PPC64_REL16_HA:
16346
    /* If we are generating a non-PIC executable, edit
16347
       .  0:  addis 2,12,.TOC.-0b@ha
16348
       .    addi 2,2,.TOC.-0b@l
16349
       used by ELFv2 global entry points to set up r2, to
16350
       .    lis 2,.TOC.@ha
16351
       .    addi 2,2,.TOC.@l
16352
       if .TOC. is in range.  */
16353
0
    if (!bfd_link_pic (info)
16354
0
        && !info->traditional_format
16355
0
        && !htab->opd_abi
16356
0
        && rel->r_addend == d_offset
16357
0
        && h != NULL && &h->elf == htab->elf.hgot
16358
0
        && rel + 1 < relend
16359
0
        && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16360
0
        && rel[1].r_offset == rel->r_offset + 4
16361
0
        && rel[1].r_addend == rel->r_addend + 4
16362
0
        && relocation + 0x80008000 <= 0xffffffff
16363
0
        && offset_in_range (input_section, rel->r_offset - d_offset, 8))
16364
0
      {
16365
0
        unsigned int insn1, insn2;
16366
0
        offset = rel->r_offset - d_offset;
16367
0
        insn1 = bfd_get_32 (input_bfd, contents + offset);
16368
0
        insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16369
0
        if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16370
0
      && (insn2 & 0xffff0000) == ADDI_R2_R2)
16371
0
    {
16372
0
      r_type = R_PPC64_ADDR16_HA;
16373
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16374
0
      rel->r_addend -= d_offset;
16375
0
      rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16376
0
      rel[1].r_addend -= d_offset + 4;
16377
0
      bfd_put_32 (input_bfd, LIS_R2, contents + offset);
16378
0
    }
16379
0
      }
16380
0
    break;
16381
0
  }
16382
16383
      /* Handle other relocations that tweak non-addend part of insn.  */
16384
0
      insn = 0;
16385
0
      max_br_offset = 1 << 25;
16386
0
      addend = rel->r_addend;
16387
0
      reloc_dest = DEST_NORMAL;
16388
0
      switch (r_type)
16389
0
  {
16390
0
  default:
16391
0
    break;
16392
16393
0
  case R_PPC64_TOCSAVE:
16394
0
    if (relocation + addend == (rel->r_offset
16395
0
              + input_section->output_offset
16396
0
              + input_section->output_section->vma)
16397
0
        && tocsave_find (htab, NO_INSERT,
16398
0
             &local_syms, rel, input_bfd)
16399
0
        && offset_in_range (input_section, rel->r_offset, 4))
16400
0
      {
16401
0
        insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16402
0
        if (insn == NOP
16403
0
      || insn == CROR_151515 || insn == CROR_313131)
16404
0
    bfd_put_32 (input_bfd,
16405
0
          STD_R2_0R1 + STK_TOC (htab),
16406
0
          contents + rel->r_offset);
16407
0
      }
16408
0
    break;
16409
16410
    /* Branch taken prediction relocations.  */
16411
0
  case R_PPC64_ADDR14_BRTAKEN:
16412
0
  case R_PPC64_REL14_BRTAKEN:
16413
0
    insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
16414
    /* Fall through.  */
16415
16416
    /* Branch not taken prediction relocations.  */
16417
0
  case R_PPC64_ADDR14_BRNTAKEN:
16418
0
  case R_PPC64_REL14_BRNTAKEN:
16419
0
    if (!offset_in_range (input_section, rel->r_offset, 4))
16420
0
      break;
16421
0
    insn |= bfd_get_32 (input_bfd,
16422
0
            contents + rel->r_offset) & ~(0x01 << 21);
16423
    /* Fall through.  */
16424
16425
0
  case R_PPC64_REL14:
16426
0
    max_br_offset = 1 << 15;
16427
    /* Fall through.  */
16428
16429
0
  case R_PPC64_REL24:
16430
0
  case R_PPC64_REL24_NOTOC:
16431
0
  case R_PPC64_REL24_P9NOTOC:
16432
0
  case R_PPC64_PLTCALL:
16433
0
  case R_PPC64_PLTCALL_NOTOC:
16434
    /* Calls to functions with a different TOC, such as calls to
16435
       shared objects, need to alter the TOC pointer.  This is
16436
       done using a linkage stub.  A REL24 branching to these
16437
       linkage stubs needs to be followed by a nop, as the nop
16438
       will be replaced with an instruction to restore the TOC
16439
       base pointer.  */
16440
0
    fdh = h;
16441
0
    if (h != NULL
16442
0
        && h->oh != NULL
16443
0
        && h->oh->is_func_descriptor)
16444
0
      fdh = ppc_follow_link (h->oh);
16445
0
    stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16446
0
             htab);
16447
0
    if ((r_type == R_PPC64_PLTCALL
16448
0
         || r_type == R_PPC64_PLTCALL_NOTOC)
16449
0
        && stub_entry != NULL
16450
0
        && stub_entry->type.main == ppc_stub_plt_call)
16451
0
      stub_entry = NULL;
16452
16453
0
    if (stub_entry != NULL
16454
0
        && (stub_entry->type.main == ppc_stub_plt_call
16455
0
      || stub_entry->type.r2save))
16456
0
      {
16457
0
        bool can_plt_call = false;
16458
16459
0
        if (r_type == R_PPC64_REL24_NOTOC
16460
0
      || r_type == R_PPC64_REL24_P9NOTOC)
16461
0
    {
16462
      /* NOTOC calls don't need to restore r2.  */
16463
0
      can_plt_call = true;
16464
0
    }
16465
0
        else if (stub_entry->type.main == ppc_stub_plt_call
16466
0
           && !htab->opd_abi
16467
0
           && htab->params->plt_localentry0 != 0
16468
0
           && h != NULL
16469
0
           && is_elfv2_localentry0 (&h->elf))
16470
0
    {
16471
      /* The function doesn't use or change r2.  */
16472
0
      can_plt_call = true;
16473
0
    }
16474
16475
        /* All of these stubs may modify r2, so there must be a
16476
     branch and link followed by a nop.  The nop is
16477
     replaced by an insn to restore r2.  */
16478
0
        else if (offset_in_range (input_section, rel->r_offset, 8))
16479
0
    {
16480
0
      unsigned long br;
16481
16482
0
      br = bfd_get_32 (input_bfd,
16483
0
           contents + rel->r_offset);
16484
0
      if ((br & 1) != 0)
16485
0
        {
16486
0
          unsigned long nop;
16487
16488
0
          nop = bfd_get_32 (input_bfd,
16489
0
          contents + rel->r_offset + 4);
16490
0
          if (nop == LD_R2_0R1 + STK_TOC (htab))
16491
0
      can_plt_call = true;
16492
0
          else if (nop == NOP
16493
0
             || nop == CROR_151515
16494
0
             || nop == CROR_313131)
16495
0
      {
16496
0
        if (h != NULL
16497
0
            && is_tls_get_addr (&h->elf, htab)
16498
0
            && htab->params->tls_get_addr_opt)
16499
0
          {
16500
            /* Special stub used, leave nop alone.  */
16501
0
          }
16502
0
        else
16503
0
          bfd_put_32 (input_bfd,
16504
0
          LD_R2_0R1 + STK_TOC (htab),
16505
0
          contents + rel->r_offset + 4);
16506
0
        can_plt_call = true;
16507
0
      }
16508
0
        }
16509
0
    }
16510
16511
0
        if (!can_plt_call && h != NULL)
16512
0
    {
16513
0
      const char *name = h->elf.root.root.string;
16514
16515
0
      if (*name == '.')
16516
0
        ++name;
16517
16518
0
      if (startswith (name, "__libc_start_main")
16519
0
          && (name[17] == 0 || name[17] == '@'))
16520
0
        {
16521
          /* Allow crt1 branch to go via a toc adjusting
16522
       stub.  Other calls that never return could do
16523
       the same, if we could detect such.  */
16524
0
          can_plt_call = true;
16525
0
        }
16526
0
    }
16527
16528
0
        if (!can_plt_call)
16529
0
    {
16530
      /* g++ as of 20130507 emits self-calls without a
16531
         following nop.  This is arguably wrong since we
16532
         have conflicting information.  On the one hand a
16533
         global symbol and on the other a local call
16534
         sequence, but don't error for this special case.
16535
         It isn't possible to cheaply verify we have
16536
         exactly such a call.  Allow all calls to the same
16537
         section.  */
16538
0
      asection *code_sec = sec;
16539
16540
0
      if (get_opd_info (sec) != NULL)
16541
0
        {
16542
0
          bfd_vma off = (relocation + addend
16543
0
             - sec->output_section->vma
16544
0
             - sec->output_offset);
16545
16546
0
          opd_entry_value (sec, off, &code_sec, NULL, false);
16547
0
        }
16548
0
      if (code_sec == input_section)
16549
0
        can_plt_call = true;
16550
0
    }
16551
16552
0
        if (!can_plt_call)
16553
0
    {
16554
0
      if (stub_entry->type.main == ppc_stub_plt_call)
16555
0
        info->callbacks->einfo
16556
          /* xgettext:c-format */
16557
0
          (_("%H: call to `%pT' lacks nop, can't restore toc; "
16558
0
       "(plt call stub)\n"),
16559
0
           input_bfd, input_section, rel->r_offset, sym_name);
16560
0
      else
16561
0
        info->callbacks->einfo
16562
          /* xgettext:c-format */
16563
0
          (_("%H: call to `%pT' lacks nop, can't restore toc; "
16564
0
       "(toc save/adjust stub)\n"),
16565
0
           input_bfd, input_section, rel->r_offset, sym_name);
16566
16567
0
      bfd_set_error (bfd_error_bad_value);
16568
0
      ret = false;
16569
0
    }
16570
16571
0
        if (can_plt_call
16572
0
      && stub_entry->type.main == ppc_stub_plt_call)
16573
0
    unresolved_reloc = false;
16574
0
      }
16575
16576
0
    if ((stub_entry == NULL
16577
0
         || stub_entry->type.main == ppc_stub_long_branch
16578
0
         || stub_entry->type.main == ppc_stub_plt_branch)
16579
0
        && get_opd_info (sec) != NULL)
16580
0
      {
16581
        /* The branch destination is the value of the opd entry. */
16582
0
        bfd_vma off = (relocation + addend
16583
0
           - sec->output_section->vma
16584
0
           - sec->output_offset);
16585
0
        bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
16586
0
        if (dest != (bfd_vma) -1)
16587
0
    {
16588
0
      relocation = dest;
16589
0
      addend = 0;
16590
0
      reloc_dest = DEST_OPD;
16591
0
    }
16592
0
      }
16593
16594
    /* If the branch is out of reach we ought to have a long
16595
       branch stub.  */
16596
0
    from = (rel->r_offset
16597
0
      + input_section->output_offset
16598
0
      + input_section->output_section->vma);
16599
16600
0
    relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16601
0
              ? fdh->elf.other
16602
0
              : sym->st_other);
16603
16604
0
    if (stub_entry != NULL
16605
0
        && (stub_entry->type.main == ppc_stub_long_branch
16606
0
      || stub_entry->type.main == ppc_stub_plt_branch))
16607
0
      {
16608
0
        if (stub_entry->type.sub == ppc_stub_toc
16609
0
      && !stub_entry->type.r2save
16610
0
      && (r_type == R_PPC64_ADDR14_BRTAKEN
16611
0
          || r_type == R_PPC64_ADDR14_BRNTAKEN
16612
0
          || (relocation + addend - from + max_br_offset
16613
0
        < 2 * max_br_offset)))
16614
    /* Don't use the stub if this branch is in range.  */
16615
0
    stub_entry = NULL;
16616
16617
0
        if (stub_entry != NULL
16618
0
      && stub_entry->type.sub >= ppc_stub_notoc
16619
0
      && ((r_type != R_PPC64_REL24_NOTOC
16620
0
           && r_type != R_PPC64_REL24_P9NOTOC)
16621
0
          || ((fdh ? fdh->elf.other : sym->st_other)
16622
0
        & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16623
0
      && (relocation + addend - from + max_br_offset
16624
0
          < 2 * max_br_offset))
16625
0
    stub_entry = NULL;
16626
16627
0
        if (stub_entry != NULL
16628
0
      && stub_entry->type.r2save
16629
0
      && (r_type == R_PPC64_REL24_NOTOC
16630
0
          || r_type == R_PPC64_REL24_P9NOTOC)
16631
0
      && (relocation + addend - from + max_br_offset
16632
0
          < 2 * max_br_offset))
16633
0
    stub_entry = NULL;
16634
0
      }
16635
16636
0
    if (stub_entry != NULL)
16637
0
      {
16638
        /* Munge up the value and addend so that we call the stub
16639
     rather than the procedure directly.  */
16640
0
        asection *stub_sec = stub_entry->group->stub_sec;
16641
16642
0
        if (stub_entry->type.main == ppc_stub_save_res)
16643
0
    relocation += (stub_sec->output_offset
16644
0
             + stub_sec->output_section->vma
16645
0
             + stub_sec->size - htab->sfpr->size
16646
0
             - htab->sfpr->output_offset
16647
0
             - htab->sfpr->output_section->vma);
16648
0
        else
16649
0
    relocation = (stub_entry->stub_offset
16650
0
            + stub_sec->output_offset
16651
0
            + stub_sec->output_section->vma);
16652
0
        addend = 0;
16653
0
        reloc_dest = DEST_STUB;
16654
16655
0
        if (((stub_entry->type.r2save
16656
0
        && (r_type == R_PPC64_REL24_NOTOC
16657
0
      || r_type == R_PPC64_REL24_P9NOTOC))
16658
0
       || ((stub_entry->type.main == ppc_stub_plt_call
16659
0
      && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16660
0
           && rel + 1 < relend
16661
0
           && rel[1].r_offset == rel->r_offset + 4
16662
0
           && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16663
0
      && !(stub_entry->type.main == ppc_stub_plt_call
16664
0
           && htab->params->tls_get_addr_opt
16665
0
           && h != NULL
16666
0
           && is_tls_get_addr (&h->elf, htab)))
16667
0
    {
16668
      /* Skip over the r2 store at the start of the stub.  */
16669
0
      relocation += 4;
16670
0
    }
16671
16672
0
        if ((r_type == R_PPC64_REL24_NOTOC
16673
0
       || r_type == R_PPC64_REL24_P9NOTOC)
16674
0
      && stub_entry->type.main == ppc_stub_plt_call
16675
0
      && stub_entry->type.sub >= ppc_stub_notoc)
16676
0
    htab->notoc_plt = 1;
16677
0
      }
16678
16679
0
    if (insn != 0)
16680
0
      {
16681
0
        if (is_isa_v2)
16682
0
    {
16683
      /* Set 'a' bit.  This is 0b00010 in BO field for branch
16684
         on CR(BI) insns (BO == 001at or 011at), and 0b01000
16685
         for branch on CTR insns (BO == 1a00t or 1a01t).  */
16686
0
      if ((insn & (0x14 << 21)) == (0x04 << 21))
16687
0
        insn |= 0x02 << 21;
16688
0
      else if ((insn & (0x14 << 21)) == (0x10 << 21))
16689
0
        insn |= 0x08 << 21;
16690
0
      else
16691
0
        break;
16692
0
    }
16693
0
        else
16694
0
    {
16695
      /* Invert 'y' bit if not the default.  */
16696
0
      if ((bfd_signed_vma) (relocation + addend - from) < 0)
16697
0
        insn ^= 0x01 << 21;
16698
0
    }
16699
16700
0
        bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16701
0
      }
16702
16703
    /* NOP out calls to undefined weak functions.
16704
       We can thus call a weak function without first
16705
       checking whether the function is defined.  */
16706
0
    else if (h != NULL
16707
0
       && h->elf.root.type == bfd_link_hash_undefweak
16708
0
       && h->elf.dynindx == -1
16709
0
       && (r_type == R_PPC64_REL24
16710
0
           || r_type == R_PPC64_REL24_NOTOC
16711
0
           || r_type == R_PPC64_REL24_P9NOTOC)
16712
0
       && relocation == 0
16713
0
       && addend == 0
16714
0
       && offset_in_range (input_section, rel->r_offset, 4))
16715
0
      {
16716
0
        bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
16717
0
        goto copy_reloc;
16718
0
      }
16719
0
    break;
16720
16721
0
  case R_PPC64_GOT16_DS:
16722
0
    if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16723
0
        || (bfd_link_pic (info)
16724
0
      && sec == bfd_abs_section_ptr)
16725
0
        || !htab->do_toc_opt)
16726
0
      break;
16727
0
    from = TOCstart + htab->sec_info[input_section->id].toc_off;
16728
0
    if (relocation + addend - from + 0x8000 < 0x10000
16729
0
        && sec != NULL
16730
0
        && sec->output_section != NULL
16731
0
        && !discarded_section (sec)
16732
0
        && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16733
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
16734
0
      {
16735
0
        insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16736
0
        if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16737
0
    {
16738
0
      insn += (14u << 26) - (58u << 26);
16739
0
      bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16740
0
      r_type = R_PPC64_TOC16;
16741
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16742
0
    }
16743
0
      }
16744
0
    break;
16745
16746
0
  case R_PPC64_GOT16_LO_DS:
16747
0
  case R_PPC64_GOT16_HA:
16748
0
    if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16749
0
        || (bfd_link_pic (info)
16750
0
      && sec == bfd_abs_section_ptr)
16751
0
        || !htab->do_toc_opt)
16752
0
      break;
16753
0
    from = TOCstart + htab->sec_info[input_section->id].toc_off;
16754
0
    if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
16755
0
        && sec != NULL
16756
0
        && sec->output_section != NULL
16757
0
        && !discarded_section (sec)
16758
0
        && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16759
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
16760
0
      {
16761
0
        insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16762
0
        if (r_type == R_PPC64_GOT16_LO_DS
16763
0
      && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16764
0
    {
16765
0
      insn += (14u << 26) - (58u << 26);
16766
0
      bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16767
0
      r_type = R_PPC64_TOC16_LO;
16768
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16769
0
    }
16770
0
        else if (r_type == R_PPC64_GOT16_HA
16771
0
           && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
16772
0
    {
16773
0
      r_type = R_PPC64_TOC16_HA;
16774
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16775
0
    }
16776
0
      }
16777
0
    break;
16778
16779
0
  case R_PPC64_GOT_PCREL34:
16780
0
    if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16781
0
        || (bfd_link_pic (info)
16782
0
      && sec == bfd_abs_section_ptr)
16783
0
        || !htab->do_toc_opt)
16784
0
      break;
16785
0
    from = (rel->r_offset
16786
0
      + input_section->output_section->vma
16787
0
      + input_section->output_offset);
16788
0
    if (!(relocation - from + (1ULL << 33) < 1ULL << 34
16789
0
    && sec != NULL
16790
0
    && sec->output_section != NULL
16791
0
    && !discarded_section (sec)
16792
0
    && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16793
0
    && offset_in_range (input_section, rel->r_offset, 8)))
16794
0
      break;
16795
16796
0
    offset = rel->r_offset;
16797
0
    pinsn = bfd_get_32 (input_bfd, contents + offset);
16798
0
    pinsn <<= 32;
16799
0
    pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16800
0
    if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16801
0
        != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16802
0
      break;
16803
16804
    /* Replace with paddi.  */
16805
0
    pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16806
0
    r_type = R_PPC64_PCREL34;
16807
0
    rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16808
0
    bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16809
0
    bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16810
    /* Fall through.  */
16811
16812
0
  case R_PPC64_PCREL34:
16813
0
    if (!htab->params->no_pcrel_opt
16814
0
        && rel + 1 < relend
16815
0
        && rel[1].r_offset == rel->r_offset
16816
0
        && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
16817
0
        && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16818
0
        && offset_in_range (input_section, rel->r_offset, 8))
16819
0
      {
16820
0
        offset = rel->r_offset;
16821
0
        pinsn = bfd_get_32 (input_bfd, contents + offset);
16822
0
        pinsn <<= 32;
16823
0
        pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16824
0
        if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16825
0
       == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16826
0
           | (14ULL << 26) /* paddi */))
16827
0
    {
16828
0
      bfd_vma off2 = rel[1].r_addend;
16829
0
      if (off2 == 0)
16830
        /* zero means next insn.  */
16831
0
        off2 = 8;
16832
0
      off2 += offset;
16833
0
      if (offset_in_range (input_section, off2, 4))
16834
0
        {
16835
0
          uint64_t pinsn2;
16836
0
          bfd_signed_vma addend_off;
16837
0
          pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16838
0
          pinsn2 <<= 32;
16839
0
          if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16840
0
      {
16841
0
        if (!offset_in_range (input_section, off2, 8))
16842
0
          break;
16843
0
        pinsn2 |= bfd_get_32 (input_bfd,
16844
0
            contents + off2 + 4);
16845
0
      }
16846
0
          if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16847
0
      {
16848
0
        addend += addend_off;
16849
0
        rel->r_addend = addend;
16850
0
        bfd_put_32 (input_bfd, pinsn >> 32,
16851
0
              contents + offset);
16852
0
        bfd_put_32 (input_bfd, pinsn,
16853
0
              contents + offset + 4);
16854
0
        bfd_put_32 (input_bfd, pinsn2 >> 32,
16855
0
              contents + off2);
16856
0
        if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16857
0
          bfd_put_32 (input_bfd, pinsn2,
16858
0
          contents + off2 + 4);
16859
0
      }
16860
0
        }
16861
0
    }
16862
0
      }
16863
0
    break;
16864
0
  }
16865
16866
0
      tls_type = 0;
16867
0
      save_unresolved_reloc = unresolved_reloc;
16868
0
      switch (r_type)
16869
0
  {
16870
0
  default:
16871
0
    if (r_type < ARRAY_SIZE (ppc64_elf_howto_table)
16872
0
        && ppc64_elf_howto_table[r_type] != NULL)
16873
      /* xgettext:c-format */
16874
0
      _bfd_error_handler (_("%pB: %s unsupported"),
16875
0
        input_bfd, ppc64_elf_howto_table[r_type]->name);
16876
0
    else
16877
      /* xgettext:c-format */
16878
0
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
16879
0
        input_bfd, r_type);
16880
16881
0
    bfd_set_error (bfd_error_bad_value);
16882
0
    ret = false;
16883
0
    goto copy_reloc;
16884
16885
0
  case R_PPC64_NONE:
16886
0
  case R_PPC64_TLS:
16887
0
  case R_PPC64_TLSGD:
16888
0
  case R_PPC64_TLSLD:
16889
0
  case R_PPC64_TOCSAVE:
16890
0
  case R_PPC64_GNU_VTINHERIT:
16891
0
  case R_PPC64_GNU_VTENTRY:
16892
0
  case R_PPC64_ENTRY:
16893
0
  case R_PPC64_PCREL_OPT:
16894
0
    goto copy_reloc;
16895
16896
    /* GOT16 relocations.  Like an ADDR16 using the symbol's
16897
       address in the GOT as relocation value instead of the
16898
       symbol's value itself.  Also, create a GOT entry for the
16899
       symbol and put the symbol value there.  */
16900
0
  case R_PPC64_GOT_TLSGD16:
16901
0
  case R_PPC64_GOT_TLSGD16_LO:
16902
0
  case R_PPC64_GOT_TLSGD16_HI:
16903
0
  case R_PPC64_GOT_TLSGD16_HA:
16904
0
  case R_PPC64_GOT_TLSGD_PCREL34:
16905
0
    tls_type = TLS_TLS | TLS_GD;
16906
0
    goto dogot;
16907
16908
0
  case R_PPC64_GOT_TLSLD16:
16909
0
  case R_PPC64_GOT_TLSLD16_LO:
16910
0
  case R_PPC64_GOT_TLSLD16_HI:
16911
0
  case R_PPC64_GOT_TLSLD16_HA:
16912
0
  case R_PPC64_GOT_TLSLD_PCREL34:
16913
0
    tls_type = TLS_TLS | TLS_LD;
16914
0
    goto dogot;
16915
16916
0
  case R_PPC64_GOT_TPREL16_DS:
16917
0
  case R_PPC64_GOT_TPREL16_LO_DS:
16918
0
  case R_PPC64_GOT_TPREL16_HI:
16919
0
  case R_PPC64_GOT_TPREL16_HA:
16920
0
  case R_PPC64_GOT_TPREL_PCREL34:
16921
0
    tls_type = TLS_TLS | TLS_TPREL;
16922
0
    goto dogot;
16923
16924
0
  case R_PPC64_GOT_DTPREL16_DS:
16925
0
  case R_PPC64_GOT_DTPREL16_LO_DS:
16926
0
  case R_PPC64_GOT_DTPREL16_HI:
16927
0
  case R_PPC64_GOT_DTPREL16_HA:
16928
0
  case R_PPC64_GOT_DTPREL_PCREL34:
16929
0
    tls_type = TLS_TLS | TLS_DTPREL;
16930
0
    goto dogot;
16931
16932
0
  case R_PPC64_GOT16:
16933
0
  case R_PPC64_GOT16_LO:
16934
0
  case R_PPC64_GOT16_HI:
16935
0
  case R_PPC64_GOT16_HA:
16936
0
  case R_PPC64_GOT16_DS:
16937
0
  case R_PPC64_GOT16_LO_DS:
16938
0
  case R_PPC64_GOT_PCREL34:
16939
0
  dogot:
16940
0
    {
16941
      /* Relocation is to the entry for this symbol in the global
16942
         offset table.  */
16943
0
      asection *got;
16944
0
      bfd_vma *offp;
16945
0
      bfd_vma off;
16946
0
      unsigned long indx = 0;
16947
0
      struct got_entry *ent;
16948
16949
0
      if (tls_type == (TLS_TLS | TLS_LD)
16950
0
    && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16951
0
        ent = ppc64_tlsld_got (input_bfd);
16952
0
      else
16953
0
        {
16954
0
    if (h != NULL)
16955
0
      {
16956
0
        if (!htab->elf.dynamic_sections_created
16957
0
      || h->elf.dynindx == -1
16958
0
      || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16959
0
      || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16960
          /* This is actually a static link, or it is a
16961
       -Bsymbolic link and the symbol is defined
16962
       locally, or the symbol was forced to be local
16963
       because of a version file.  */
16964
0
          ;
16965
0
        else
16966
0
          {
16967
0
      indx = h->elf.dynindx;
16968
0
      unresolved_reloc = false;
16969
0
          }
16970
0
        ent = h->elf.got.glist;
16971
0
      }
16972
0
    else
16973
0
      {
16974
0
        if (local_got_ents == NULL)
16975
0
          abort ();
16976
0
        ent = local_got_ents[r_symndx];
16977
0
      }
16978
16979
0
    for (; ent != NULL; ent = ent->next)
16980
0
      if (ent->addend == orig_rel.r_addend
16981
0
          && ent->owner == input_bfd
16982
0
          && ent->tls_type == tls_type)
16983
0
        break;
16984
0
        }
16985
16986
0
      if (ent == NULL)
16987
0
        abort ();
16988
0
      if (ent->is_indirect)
16989
0
        ent = ent->got.ent;
16990
0
      offp = &ent->got.offset;
16991
0
      got = ppc64_elf_tdata (ent->owner)->got;
16992
0
      if (got == NULL)
16993
0
        abort ();
16994
16995
      /* The offset must always be a multiple of 8.  We use the
16996
         least significant bit to record whether we have already
16997
         processed this entry.  */
16998
0
      off = *offp;
16999
0
      if ((off & 1) != 0)
17000
0
        off &= ~1;
17001
0
      else
17002
0
        {
17003
    /* Generate relocs for the dynamic linker, except in
17004
       the case of TLSLD where we'll use one entry per
17005
       module.  */
17006
0
    asection *relgot;
17007
0
    bool ifunc;
17008
17009
0
    *offp = off | 1;
17010
0
    relgot = NULL;
17011
0
    ifunc = (h != NULL
17012
0
       ? h->elf.type == STT_GNU_IFUNC
17013
0
       : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
17014
0
    if (ifunc)
17015
0
      {
17016
0
        relgot = htab->elf.irelplt;
17017
0
        if (indx == 0 || is_static_defined (&h->elf))
17018
0
          htab->elf.ifunc_resolvers = true;
17019
0
      }
17020
0
    else if (indx != 0
17021
0
       || (bfd_link_pic (info)
17022
0
           && (h == NULL
17023
0
         || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
17024
0
           && !(tls_type != 0
17025
0
          && bfd_link_executable (info)
17026
0
          && (h == NULL
17027
0
              || SYMBOL_REFERENCES_LOCAL (info,
17028
0
                  &h->elf)))
17029
0
           && (h != NULL
17030
0
         ? !bfd_is_abs_symbol (&h->elf.root)
17031
0
         : sym->st_shndx != SHN_ABS)))
17032
17033
0
      relgot = ppc64_elf_tdata (ent->owner)->relgot;
17034
0
    if (relgot != NULL)
17035
0
      {
17036
0
        outrel.r_offset = (got->output_section->vma
17037
0
               + got->output_offset
17038
0
               + off);
17039
0
        outrel.r_addend = orig_rel.r_addend;
17040
0
        if (tls_type & (TLS_LD | TLS_GD))
17041
0
          {
17042
0
      outrel.r_addend = 0;
17043
0
      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
17044
0
      if (tls_type == (TLS_TLS | TLS_GD))
17045
0
        {
17046
0
          BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd,
17047
0
                  &outrel,
17048
0
                  relgot));
17049
0
          outrel.r_offset += 8;
17050
0
          outrel.r_addend = orig_rel.r_addend;
17051
0
          outrel.r_info
17052
0
            = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
17053
0
        }
17054
0
          }
17055
0
        else if (tls_type == (TLS_TLS | TLS_DTPREL))
17056
0
          outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
17057
0
        else if (tls_type == (TLS_TLS | TLS_TPREL))
17058
0
          outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
17059
0
        else if (indx != 0)
17060
0
          outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
17061
0
        else
17062
0
          {
17063
0
      if (ifunc)
17064
0
        outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17065
0
      else
17066
0
        outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17067
17068
      /* Write the .got section contents for the sake
17069
         of prelink.  */
17070
0
      loc = got->contents + off;
17071
0
      bfd_put_64 (info->output_bfd,
17072
0
            outrel.r_addend + relocation, loc);
17073
0
          }
17074
17075
0
        if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
17076
0
          {
17077
0
      outrel.r_addend += relocation;
17078
0
      if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
17079
0
        {
17080
0
          if (htab->elf.tls_sec == NULL)
17081
0
            outrel.r_addend = 0;
17082
0
          else
17083
0
            outrel.r_addend -= htab->elf.tls_sec->vma;
17084
0
        }
17085
0
          }
17086
0
        if (!(info->enable_dt_relr
17087
0
        && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
17088
0
          BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd,
17089
0
                  &outrel, relgot));
17090
0
      }
17091
17092
    /* Init the .got section contents here if we're not
17093
       emitting a reloc.  */
17094
0
    else
17095
0
      {
17096
0
        relocation += orig_rel.r_addend;
17097
0
        if (tls_type != 0)
17098
0
          {
17099
0
      if (htab->elf.tls_sec == NULL)
17100
0
        relocation = 0;
17101
0
      else
17102
0
        {
17103
0
          if (tls_type & TLS_LD)
17104
0
            relocation = 0;
17105
0
          else
17106
0
            relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
17107
0
          if (tls_type & TLS_TPREL)
17108
0
            relocation += DTP_OFFSET - TP_OFFSET;
17109
0
        }
17110
17111
0
      if (tls_type & (TLS_GD | TLS_LD))
17112
0
        {
17113
0
          bfd_put_64 (info->output_bfd, relocation,
17114
0
          got->contents + off + 8);
17115
0
          relocation = 1;
17116
0
        }
17117
0
          }
17118
0
        bfd_put_64 (info->output_bfd, relocation,
17119
0
        got->contents + off);
17120
0
      }
17121
0
        }
17122
17123
0
      if (off >= (bfd_vma) -2)
17124
0
        abort ();
17125
17126
0
      relocation = got->output_section->vma + got->output_offset + off;
17127
0
      addend = 0;
17128
0
      if (!(r_type == R_PPC64_GOT_PCREL34
17129
0
      || r_type == R_PPC64_GOT_TLSGD_PCREL34
17130
0
      || r_type == R_PPC64_GOT_TLSLD_PCREL34
17131
0
      || r_type == R_PPC64_GOT_TPREL_PCREL34
17132
0
      || r_type == R_PPC64_GOT_DTPREL_PCREL34))
17133
0
        addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
17134
0
    }
17135
0
    break;
17136
17137
0
  case R_PPC64_PLT16_HA:
17138
0
  case R_PPC64_PLT16_HI:
17139
0
  case R_PPC64_PLT16_LO:
17140
0
  case R_PPC64_PLT16_LO_DS:
17141
0
  case R_PPC64_PLT_PCREL34:
17142
0
  case R_PPC64_PLT_PCREL34_NOTOC:
17143
0
  case R_PPC64_PLT32:
17144
0
  case R_PPC64_PLT64:
17145
0
  case R_PPC64_PLTSEQ:
17146
0
  case R_PPC64_PLTSEQ_NOTOC:
17147
0
  case R_PPC64_PLTCALL:
17148
0
  case R_PPC64_PLTCALL_NOTOC:
17149
    /* Relocation is to the entry for this symbol in the
17150
       procedure linkage table.  */
17151
0
    unresolved_reloc = true;
17152
0
    {
17153
0
      struct plt_entry **plt_list = NULL;
17154
0
      if (h != NULL)
17155
0
        plt_list = &h->elf.plt.plist;
17156
0
      else if (local_got_ents != NULL)
17157
0
        {
17158
0
    struct plt_entry **local_plt = (struct plt_entry **)
17159
0
      (local_got_ents + symtab_hdr->sh_info);
17160
0
    plt_list = local_plt + r_symndx;
17161
0
        }
17162
0
      if (plt_list)
17163
0
        {
17164
0
    struct plt_entry *ent;
17165
17166
0
    for (ent = *plt_list; ent != NULL; ent = ent->next)
17167
0
      if (ent->plt.offset != (bfd_vma) -1
17168
0
          && ent->addend == orig_rel.r_addend)
17169
0
        {
17170
0
          asection *plt;
17171
0
          bfd_vma got;
17172
17173
0
          plt = htab->elf.splt;
17174
0
          if (use_local_plt (info, elf_hash_entry (h)))
17175
0
      {
17176
0
        if (h != NULL
17177
0
            ? h->elf.type == STT_GNU_IFUNC
17178
0
            : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17179
0
          plt = htab->elf.iplt;
17180
0
        else
17181
0
          plt = htab->pltlocal;
17182
0
      }
17183
0
          relocation = (plt->output_section->vma
17184
0
            + plt->output_offset
17185
0
            + ent->plt.offset);
17186
0
          if (r_type == R_PPC64_PLT16_HA
17187
0
        || r_type == R_PPC64_PLT16_HI
17188
0
        || r_type == R_PPC64_PLT16_LO
17189
0
        || r_type == R_PPC64_PLT16_LO_DS)
17190
0
      {
17191
0
        got = (elf_gp (info->output_bfd)
17192
0
         + htab->sec_info[input_section->id].toc_off);
17193
0
        relocation -= got;
17194
0
      }
17195
0
          addend = 0;
17196
0
          unresolved_reloc = false;
17197
0
          break;
17198
0
        }
17199
0
        }
17200
0
    }
17201
0
    break;
17202
17203
0
  case R_PPC64_TOC:
17204
    /* Relocation value is TOC base.  */
17205
0
    relocation = TOCstart;
17206
0
    if (r_symndx == STN_UNDEF)
17207
0
      relocation += htab->sec_info[input_section->id].toc_off;
17208
0
    else if (unresolved_reloc)
17209
0
      ;
17210
0
    else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17211
0
      relocation += htab->sec_info[sec->id].toc_off;
17212
0
    else
17213
0
      unresolved_reloc = true;
17214
0
    if (unresolved_reloc
17215
0
        || (!is_opd
17216
0
      && h != NULL
17217
0
      && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
17218
0
      info->callbacks->einfo
17219
        /* xgettext:c-format */
17220
0
        (_("%H: %s against %pT is not supported\n"),
17221
0
         input_bfd, input_section, rel->r_offset,
17222
0
         ppc64_elf_howto_table[r_type]->name, sym_name);
17223
0
    goto dodyn;
17224
17225
    /* TOC16 relocs.  We want the offset relative to the TOC base,
17226
       which is the address of the start of the TOC plus 0x8000.
17227
       The TOC consists of sections .got, .toc, .tocbss, and .plt,
17228
       in this order.  */
17229
0
  case R_PPC64_TOC16:
17230
0
  case R_PPC64_TOC16_LO:
17231
0
  case R_PPC64_TOC16_HI:
17232
0
  case R_PPC64_TOC16_DS:
17233
0
  case R_PPC64_TOC16_LO_DS:
17234
0
  case R_PPC64_TOC16_HA:
17235
0
    addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
17236
0
    if (h != NULL)
17237
0
      goto dodyn;
17238
0
    break;
17239
17240
    /* Relocate against the beginning of the section.  */
17241
0
  case R_PPC64_SECTOFF:
17242
0
  case R_PPC64_SECTOFF_LO:
17243
0
  case R_PPC64_SECTOFF_HI:
17244
0
  case R_PPC64_SECTOFF_DS:
17245
0
  case R_PPC64_SECTOFF_LO_DS:
17246
0
  case R_PPC64_SECTOFF_HA:
17247
0
    if (sec != NULL)
17248
0
      addend -= sec->output_section->vma;
17249
0
    break;
17250
17251
0
  case R_PPC64_REL16:
17252
0
  case R_PPC64_REL16_LO:
17253
0
  case R_PPC64_REL16_HI:
17254
0
  case R_PPC64_REL16_HA:
17255
0
  case R_PPC64_REL16_HIGH:
17256
0
  case R_PPC64_REL16_HIGHA:
17257
0
  case R_PPC64_REL16_HIGHER:
17258
0
  case R_PPC64_REL16_HIGHERA:
17259
0
  case R_PPC64_REL16_HIGHEST:
17260
0
  case R_PPC64_REL16_HIGHESTA:
17261
0
  case R_PPC64_REL16_HIGHER34:
17262
0
  case R_PPC64_REL16_HIGHERA34:
17263
0
  case R_PPC64_REL16_HIGHEST34:
17264
0
  case R_PPC64_REL16_HIGHESTA34:
17265
0
  case R_PPC64_REL16DX_HA:
17266
0
  case R_PPC64_REL14:
17267
0
  case R_PPC64_REL14_BRNTAKEN:
17268
0
  case R_PPC64_REL14_BRTAKEN:
17269
0
  case R_PPC64_REL24:
17270
0
  case R_PPC64_REL24_NOTOC:
17271
0
  case R_PPC64_REL24_P9NOTOC:
17272
0
  case R_PPC64_PCREL34:
17273
0
  case R_PPC64_PCREL28:
17274
0
    break;
17275
17276
0
  case R_PPC64_TPREL16:
17277
0
  case R_PPC64_TPREL16_LO:
17278
0
  case R_PPC64_TPREL16_HI:
17279
0
  case R_PPC64_TPREL16_HA:
17280
0
  case R_PPC64_TPREL16_DS:
17281
0
  case R_PPC64_TPREL16_LO_DS:
17282
0
  case R_PPC64_TPREL16_HIGH:
17283
0
  case R_PPC64_TPREL16_HIGHA:
17284
0
  case R_PPC64_TPREL16_HIGHER:
17285
0
  case R_PPC64_TPREL16_HIGHERA:
17286
0
  case R_PPC64_TPREL16_HIGHEST:
17287
0
  case R_PPC64_TPREL16_HIGHESTA:
17288
0
    if (h != NULL
17289
0
        && h->elf.root.type == bfd_link_hash_undefweak
17290
0
        && h->elf.dynindx == -1
17291
0
        && offset_in_range (input_section, rel->r_offset - d_offset, 4))
17292
0
      {
17293
        /* Make this relocation against an undefined weak symbol
17294
     resolve to zero.  This is really just a tweak, since
17295
     code using weak externs ought to check that they are
17296
     defined before using them.  */
17297
0
        bfd_byte *p = contents + rel->r_offset - d_offset;
17298
17299
0
        insn = bfd_get_32 (input_bfd, p);
17300
0
        insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17301
0
        if (insn != 0)
17302
0
    bfd_put_32 (input_bfd, insn, p);
17303
0
        break;
17304
0
      }
17305
    /* Fall through.  */
17306
17307
0
  case R_PPC64_TPREL34:
17308
0
    if (htab->elf.tls_sec != NULL)
17309
0
      addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17310
    /* The TPREL16 relocs shouldn't really be used in shared
17311
       libs or with non-local symbols as that will result in
17312
       DT_TEXTREL being set, but support them anyway.  */
17313
0
    goto dodyn;
17314
17315
0
  case R_PPC64_DTPREL16:
17316
0
  case R_PPC64_DTPREL16_LO:
17317
0
  case R_PPC64_DTPREL16_HI:
17318
0
  case R_PPC64_DTPREL16_HA:
17319
0
  case R_PPC64_DTPREL16_DS:
17320
0
  case R_PPC64_DTPREL16_LO_DS:
17321
0
  case R_PPC64_DTPREL16_HIGH:
17322
0
  case R_PPC64_DTPREL16_HIGHA:
17323
0
  case R_PPC64_DTPREL16_HIGHER:
17324
0
  case R_PPC64_DTPREL16_HIGHERA:
17325
0
  case R_PPC64_DTPREL16_HIGHEST:
17326
0
  case R_PPC64_DTPREL16_HIGHESTA:
17327
0
  case R_PPC64_DTPREL34:
17328
0
    if (htab->elf.tls_sec != NULL)
17329
0
      addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17330
0
    break;
17331
17332
0
  case R_PPC64_ADDR64_LOCAL:
17333
0
    addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17334
0
                ? h->elf.other
17335
0
                : sym->st_other);
17336
0
    break;
17337
17338
0
  case R_PPC64_DTPMOD64:
17339
0
    relocation = 1;
17340
0
    addend = 0;
17341
0
    goto dodyn;
17342
17343
0
  case R_PPC64_TPREL64:
17344
0
    if (htab->elf.tls_sec != NULL)
17345
0
      addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17346
0
    goto dodyn;
17347
17348
0
  case R_PPC64_DTPREL64:
17349
0
    if (htab->elf.tls_sec != NULL)
17350
0
      addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17351
    /* Fall through.  */
17352
17353
    /* Relocations that may need to be propagated if this is a
17354
       dynamic object.  */
17355
0
  case R_PPC64_REL30:
17356
0
  case R_PPC64_REL32:
17357
0
  case R_PPC64_REL64:
17358
0
  case R_PPC64_ADDR14:
17359
0
  case R_PPC64_ADDR14_BRNTAKEN:
17360
0
  case R_PPC64_ADDR14_BRTAKEN:
17361
0
  case R_PPC64_ADDR16:
17362
0
  case R_PPC64_ADDR16_DS:
17363
0
  case R_PPC64_ADDR16_HA:
17364
0
  case R_PPC64_ADDR16_HI:
17365
0
  case R_PPC64_ADDR16_HIGH:
17366
0
  case R_PPC64_ADDR16_HIGHA:
17367
0
  case R_PPC64_ADDR16_HIGHER:
17368
0
  case R_PPC64_ADDR16_HIGHERA:
17369
0
  case R_PPC64_ADDR16_HIGHEST:
17370
0
  case R_PPC64_ADDR16_HIGHESTA:
17371
0
  case R_PPC64_ADDR16_LO:
17372
0
  case R_PPC64_ADDR16_LO_DS:
17373
0
  case R_PPC64_ADDR16_HIGHER34:
17374
0
  case R_PPC64_ADDR16_HIGHERA34:
17375
0
  case R_PPC64_ADDR16_HIGHEST34:
17376
0
  case R_PPC64_ADDR16_HIGHESTA34:
17377
0
  case R_PPC64_ADDR24:
17378
0
  case R_PPC64_ADDR32:
17379
0
  case R_PPC64_ADDR64:
17380
0
  case R_PPC64_UADDR16:
17381
0
  case R_PPC64_UADDR32:
17382
0
  case R_PPC64_UADDR64:
17383
0
  case R_PPC64_D34:
17384
0
  case R_PPC64_D34_LO:
17385
0
  case R_PPC64_D34_HI30:
17386
0
  case R_PPC64_D34_HA30:
17387
0
  case R_PPC64_D28:
17388
0
  dodyn:
17389
0
    if ((input_section->flags & SEC_ALLOC) == 0)
17390
0
      break;
17391
17392
0
    if (NO_OPD_RELOCS && is_opd)
17393
0
      break;
17394
17395
0
    if (bfd_link_pic (info)
17396
0
        ? ((h == NULL
17397
0
      || h->elf.dyn_relocs != NULL)
17398
0
     && ((h != NULL && pc_dynrelocs (h))
17399
0
         || must_be_dyn_reloc (info, r_type)))
17400
0
        : (h != NULL
17401
0
     ? h->elf.dyn_relocs != NULL
17402
0
     : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17403
0
      {
17404
0
        bool skip, relocate;
17405
0
        asection *sreloc;
17406
0
        bfd_vma out_off;
17407
0
        long indx = 0;
17408
17409
        /* When generating a dynamic object, these relocations
17410
     are copied into the output file to be resolved at run
17411
     time.  */
17412
17413
0
        skip = false;
17414
0
        relocate = false;
17415
17416
0
        out_off = _bfd_elf_section_offset (info->output_bfd, info,
17417
0
             input_section, rel->r_offset);
17418
0
        if (out_off == (bfd_vma) -1)
17419
0
    skip = true;
17420
0
        else if (out_off == (bfd_vma) -2)
17421
0
    skip = true, relocate = true;
17422
0
        out_off += (input_section->output_section->vma
17423
0
        + input_section->output_offset);
17424
0
        outrel.r_offset = out_off;
17425
0
        outrel.r_addend = rel->r_addend;
17426
17427
        /* Optimize unaligned reloc use.  */
17428
0
        if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17429
0
      || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17430
0
    r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17431
0
        else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17432
0
           || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17433
0
    r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17434
0
        else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17435
0
           || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17436
0
    r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17437
17438
0
        if (skip)
17439
0
    memset (&outrel, 0, sizeof outrel);
17440
0
        else if (h != NULL
17441
0
           && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
17442
0
           && !is_opd
17443
0
           && r_type != R_PPC64_TOC)
17444
0
    {
17445
0
      indx = h->elf.dynindx;
17446
0
      BFD_ASSERT (indx != -1);
17447
0
      outrel.r_info = ELF64_R_INFO (indx, r_type);
17448
0
    }
17449
0
        else
17450
0
    {
17451
      /* This symbol is local, or marked to become local,
17452
         or this is an opd section reloc which must point
17453
         at a local function.  */
17454
0
      outrel.r_addend += relocation;
17455
0
      if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
17456
0
        {
17457
0
          if (is_opd && h != NULL)
17458
0
      {
17459
        /* Lie about opd entries.  This case occurs
17460
           when building shared libraries and we
17461
           reference a function in another shared
17462
           lib.  The same thing happens for a weak
17463
           definition in an application that's
17464
           overridden by a strong definition in a
17465
           shared lib.  (I believe this is a generic
17466
           bug in binutils handling of weak syms.)
17467
           In these cases we won't use the opd
17468
           entry in this lib.  */
17469
0
        unresolved_reloc = false;
17470
0
      }
17471
0
          if (!is_opd
17472
0
        && r_type == R_PPC64_ADDR64
17473
0
        && (h != NULL
17474
0
            ? h->elf.type == STT_GNU_IFUNC
17475
0
            : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17476
0
      outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17477
0
          else
17478
0
      {
17479
0
        outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17480
17481
        /* We need to relocate .opd contents for ld.so.
17482
           Prelink also wants simple and consistent rules
17483
           for relocs.  This make all RELATIVE relocs have
17484
           *r_offset equal to r_addend.  */
17485
0
        relocate = true;
17486
0
      }
17487
0
        }
17488
0
      else
17489
0
        {
17490
0
          if (h != NULL
17491
0
        ? h->elf.type == STT_GNU_IFUNC
17492
0
        : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17493
0
      {
17494
0
        info->callbacks->einfo
17495
          /* xgettext:c-format */
17496
0
          (_("%H: %s for indirect "
17497
0
             "function `%pT' unsupported\n"),
17498
0
           input_bfd, input_section, rel->r_offset,
17499
0
           ppc64_elf_howto_table[r_type]->name,
17500
0
           sym_name);
17501
0
        ret = false;
17502
0
      }
17503
0
          else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
17504
0
      ;
17505
0
          else if (sec == NULL || sec->owner == NULL)
17506
0
      {
17507
0
        bfd_set_error (bfd_error_bad_value);
17508
0
        return false;
17509
0
      }
17510
0
          else
17511
0
      {
17512
0
        asection *osec = sec->output_section;
17513
17514
0
        if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17515
0
          {
17516
            /* TLS symbol values are relative to the
17517
         TLS segment.  Dynamic relocations for
17518
         local TLS symbols therefore can't be
17519
         reduced to a relocation against their
17520
         section symbol because it holds the
17521
         address of the section, not a value
17522
         relative to the TLS segment.  We could
17523
         change the .tdata dynamic section symbol
17524
         to be zero value but STN_UNDEF works
17525
         and is used elsewhere, eg. for TPREL64
17526
         GOT relocs against local TLS symbols.  */
17527
0
            osec = htab->elf.tls_sec;
17528
0
            indx = 0;
17529
0
          }
17530
0
        else
17531
0
          {
17532
0
            indx = elf_section_data (osec)->dynindx;
17533
0
            if (indx == 0)
17534
0
        {
17535
0
          if ((osec->flags & SEC_READONLY) == 0
17536
0
              && htab->elf.data_index_section != NULL)
17537
0
            osec = htab->elf.data_index_section;
17538
0
          else
17539
0
            osec = htab->elf.text_index_section;
17540
0
          indx = elf_section_data (osec)->dynindx;
17541
0
        }
17542
0
            BFD_ASSERT (indx != 0);
17543
0
          }
17544
17545
        /* We are turning this relocation into one
17546
           against a section symbol, so subtract out
17547
           the output section's address but not the
17548
           offset of the input section in the output
17549
           section.  */
17550
0
        outrel.r_addend -= osec->vma;
17551
0
      }
17552
17553
0
          outrel.r_info = ELF64_R_INFO (indx, r_type);
17554
0
        }
17555
0
    }
17556
17557
0
        if (!(info->enable_dt_relr
17558
0
        && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
17559
0
        && maybe_relr (ELF64_R_TYPE (orig_rel.r_info),
17560
0
           rel, input_section)))
17561
0
    {
17562
0
      sreloc = elf_section_data (input_section)->sreloc;
17563
0
      if (h != NULL
17564
0
          ? h->elf.type == STT_GNU_IFUNC
17565
0
          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17566
0
        {
17567
0
          sreloc = htab->elf.irelplt;
17568
0
          if (indx == 0 || is_static_defined (&h->elf))
17569
0
      htab->elf.ifunc_resolvers = true;
17570
0
        }
17571
0
      if (sreloc == NULL)
17572
0
        abort ();
17573
17574
0
      BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd,
17575
0
              &outrel, sreloc));
17576
0
    }
17577
17578
0
        if (!warned_dynamic
17579
0
      && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17580
0
    {
17581
0
      info->callbacks->einfo
17582
        /* xgettext:c-format */
17583
0
        (_("%X%P: %pB: %s against %pT "
17584
0
           "is not supported by glibc as a dynamic relocation\n"),
17585
0
         input_bfd,
17586
0
         ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17587
0
         sym_name);
17588
0
      warned_dynamic = true;
17589
0
    }
17590
17591
        /* If this reloc is against an external symbol, it will
17592
     be computed at runtime, so there's no need to do
17593
     anything now.  However, for the sake of prelink ensure
17594
     that the section contents are a known value.  */
17595
0
        if (!relocate)
17596
0
    {
17597
0
      unresolved_reloc = false;
17598
      /* The value chosen here is quite arbitrary as ld.so
17599
         ignores section contents except for the special
17600
         case of .opd where the contents might be accessed
17601
         before relocation.  Choose zero, as that won't
17602
         cause reloc overflow.  */
17603
0
      relocation = 0;
17604
0
      addend = 0;
17605
      /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17606
         to improve backward compatibility with older
17607
         versions of ld.  */
17608
0
      if (r_type == R_PPC64_ADDR64)
17609
0
        addend = outrel.r_addend;
17610
      /* Adjust pc_relative relocs to have zero in *r_offset.  */
17611
0
      else if (ppc64_elf_howto_table[r_type]->pc_relative)
17612
0
        addend = outrel.r_offset;
17613
0
    }
17614
0
      }
17615
0
    break;
17616
17617
0
  case R_PPC64_COPY:
17618
0
  case R_PPC64_GLOB_DAT:
17619
0
  case R_PPC64_JMP_SLOT:
17620
0
  case R_PPC64_JMP_IREL:
17621
0
  case R_PPC64_RELATIVE:
17622
    /* We shouldn't ever see these dynamic relocs in relocatable
17623
       files.  */
17624
    /* Fall through.  */
17625
17626
0
  case R_PPC64_PLTGOT16:
17627
0
  case R_PPC64_PLTGOT16_DS:
17628
0
  case R_PPC64_PLTGOT16_HA:
17629
0
  case R_PPC64_PLTGOT16_HI:
17630
0
  case R_PPC64_PLTGOT16_LO:
17631
0
  case R_PPC64_PLTGOT16_LO_DS:
17632
0
  case R_PPC64_PLTREL32:
17633
0
  case R_PPC64_PLTREL64:
17634
    /* These ones haven't been implemented yet.  */
17635
17636
0
    info->callbacks->einfo
17637
      /* xgettext:c-format */
17638
0
      (_("%P: %pB: %s is not supported for `%pT'\n"),
17639
0
       input_bfd,
17640
0
       ppc64_elf_howto_table[r_type]->name, sym_name);
17641
17642
0
    bfd_set_error (bfd_error_invalid_operation);
17643
0
    ret = false;
17644
0
    goto copy_reloc;
17645
0
  }
17646
17647
      /* Multi-instruction sequences that access the TOC can be
17648
   optimized, eg. addis ra,r2,0; addi rb,ra,x;
17649
   to   nop;         addi rb,r2,x;  */
17650
0
      switch (r_type)
17651
0
  {
17652
0
  default:
17653
0
    break;
17654
17655
0
  case R_PPC64_GOT_TLSLD16_HI:
17656
0
  case R_PPC64_GOT_TLSGD16_HI:
17657
0
  case R_PPC64_GOT_TPREL16_HI:
17658
0
  case R_PPC64_GOT_DTPREL16_HI:
17659
0
  case R_PPC64_GOT16_HI:
17660
0
  case R_PPC64_TOC16_HI:
17661
    /* These relocs would only be useful if building up an
17662
       offset to later add to r2, perhaps in an indexed
17663
       addressing mode instruction.  Don't try to optimize.
17664
       Unfortunately, the possibility of someone building up an
17665
       offset like this or even with the HA relocs, means that
17666
       we need to check the high insn when optimizing the low
17667
       insn.  */
17668
0
    break;
17669
17670
0
  case R_PPC64_PLTCALL_NOTOC:
17671
0
    if (!unresolved_reloc)
17672
0
      htab->notoc_plt = 1;
17673
    /* Fall through.  */
17674
0
  case R_PPC64_PLTCALL:
17675
0
    if (unresolved_reloc
17676
0
        && offset_in_range (input_section, rel->r_offset,
17677
0
          r_type == R_PPC64_PLTCALL ? 8 : 4))
17678
0
      {
17679
        /* No plt entry.  Make this into a direct call.  */
17680
0
        bfd_byte *p = contents + rel->r_offset;
17681
0
        insn = bfd_get_32 (input_bfd, p);
17682
0
        insn &= 1;
17683
0
        bfd_put_32 (input_bfd, B_DOT | insn, p);
17684
0
        if (r_type == R_PPC64_PLTCALL)
17685
0
    bfd_put_32 (input_bfd, NOP, p + 4);
17686
0
        unresolved_reloc = save_unresolved_reloc;
17687
0
        r_type = R_PPC64_REL24;
17688
0
      }
17689
0
    break;
17690
17691
0
  case R_PPC64_PLTSEQ_NOTOC:
17692
0
  case R_PPC64_PLTSEQ:
17693
0
    if (unresolved_reloc)
17694
0
      {
17695
0
        unresolved_reloc = false;
17696
0
        goto nop_it;
17697
0
      }
17698
0
    break;
17699
17700
0
  case R_PPC64_PLT_PCREL34_NOTOC:
17701
0
    if (!unresolved_reloc)
17702
0
      htab->notoc_plt = 1;
17703
    /* Fall through.  */
17704
0
  case R_PPC64_PLT_PCREL34:
17705
0
    if (unresolved_reloc
17706
0
        && offset_in_range (input_section, rel->r_offset, 8))
17707
0
      {
17708
0
        bfd_byte *p = contents + rel->r_offset;
17709
0
        bfd_put_32 (input_bfd, PNOP >> 32, p);
17710
0
        bfd_put_32 (input_bfd, PNOP, p + 4);
17711
0
        unresolved_reloc = false;
17712
0
        goto copy_reloc;
17713
0
      }
17714
0
    break;
17715
17716
0
  case R_PPC64_PLT16_HA:
17717
0
    if (unresolved_reloc)
17718
0
      {
17719
0
        unresolved_reloc = false;
17720
0
        goto nop_it;
17721
0
      }
17722
    /* Fall through.  */
17723
0
  case R_PPC64_GOT_TLSLD16_HA:
17724
0
  case R_PPC64_GOT_TLSGD16_HA:
17725
0
  case R_PPC64_GOT_TPREL16_HA:
17726
0
  case R_PPC64_GOT_DTPREL16_HA:
17727
0
  case R_PPC64_GOT16_HA:
17728
0
  case R_PPC64_TOC16_HA:
17729
0
    if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17730
0
        && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17731
0
        && !(bfd_link_pic (info)
17732
0
       && (h != NULL
17733
0
           ? bfd_is_abs_symbol (&h->elf.root)
17734
0
           : sec == bfd_abs_section_ptr)))
17735
0
      {
17736
0
        bfd_byte *p;
17737
0
      nop_it:
17738
0
        if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17739
0
    {
17740
0
      p = contents + (rel->r_offset & ~3);
17741
0
      bfd_put_32 (input_bfd, NOP, p);
17742
0
      goto copy_reloc;
17743
0
    }
17744
0
      }
17745
0
    break;
17746
17747
0
  case R_PPC64_PLT16_LO:
17748
0
  case R_PPC64_PLT16_LO_DS:
17749
0
    if (unresolved_reloc)
17750
0
      {
17751
0
        unresolved_reloc = false;
17752
0
        goto nop_it;
17753
0
      }
17754
    /* Fall through.  */
17755
0
  case R_PPC64_GOT_TLSLD16_LO:
17756
0
  case R_PPC64_GOT_TLSGD16_LO:
17757
0
  case R_PPC64_GOT_TPREL16_LO_DS:
17758
0
  case R_PPC64_GOT_DTPREL16_LO_DS:
17759
0
  case R_PPC64_GOT16_LO:
17760
0
  case R_PPC64_GOT16_LO_DS:
17761
0
  case R_PPC64_TOC16_LO:
17762
0
  case R_PPC64_TOC16_LO_DS:
17763
0
    if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17764
0
        && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17765
0
        && !(bfd_link_pic (info)
17766
0
       && (h != NULL
17767
0
           ? bfd_is_abs_symbol (&h->elf.root)
17768
0
           : sec == bfd_abs_section_ptr))
17769
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
17770
0
      {
17771
0
        bfd_byte *p = contents + (rel->r_offset & ~3);
17772
0
        insn = bfd_get_32 (input_bfd, p);
17773
0
        if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
17774
0
    {
17775
      /* Transform addic to addi when we change reg.  */
17776
0
      insn &= ~((0x3fu << 26) | (0x1f << 16));
17777
0
      insn |= (14u << 26) | (2 << 16);
17778
0
    }
17779
0
        else
17780
0
    {
17781
0
      insn &= ~(0x1f << 16);
17782
0
      insn |= 2 << 16;
17783
0
    }
17784
0
        bfd_put_32 (input_bfd, insn, p);
17785
0
      }
17786
0
    break;
17787
17788
0
  case R_PPC64_TPREL16_HA:
17789
0
    if (htab->do_tls_opt
17790
0
        && relocation + addend + 0x8000 < 0x10000
17791
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
17792
0
      {
17793
0
        bfd_byte *p = contents + (rel->r_offset & ~3);
17794
0
        bfd_put_32 (input_bfd, NOP, p);
17795
0
        goto copy_reloc;
17796
0
      }
17797
0
    break;
17798
17799
0
  case R_PPC64_TPREL16_LO:
17800
0
  case R_PPC64_TPREL16_LO_DS:
17801
0
    if (htab->do_tls_opt
17802
0
        && relocation + addend + 0x8000 < 0x10000
17803
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
17804
0
      {
17805
0
        bfd_byte *p = contents + (rel->r_offset & ~3);
17806
0
        insn = bfd_get_32 (input_bfd, p);
17807
0
        insn &= ~(0x1f << 16);
17808
0
        insn |= 13 << 16;
17809
0
        bfd_put_32 (input_bfd, insn, p);
17810
0
      }
17811
0
    break;
17812
0
  }
17813
17814
      /* Do any further special processing.  */
17815
0
      switch (r_type)
17816
0
  {
17817
0
  default:
17818
0
    break;
17819
17820
0
  case R_PPC64_REL16_HA:
17821
0
  case R_PPC64_REL16_HIGHA:
17822
0
  case R_PPC64_REL16_HIGHERA:
17823
0
  case R_PPC64_REL16_HIGHESTA:
17824
0
  case R_PPC64_REL16DX_HA:
17825
0
  case R_PPC64_ADDR16_HA:
17826
0
  case R_PPC64_ADDR16_HIGHA:
17827
0
  case R_PPC64_ADDR16_HIGHERA:
17828
0
  case R_PPC64_ADDR16_HIGHESTA:
17829
0
  case R_PPC64_TOC16_HA:
17830
0
  case R_PPC64_SECTOFF_HA:
17831
0
  case R_PPC64_TPREL16_HA:
17832
0
  case R_PPC64_TPREL16_HIGHA:
17833
0
  case R_PPC64_TPREL16_HIGHERA:
17834
0
  case R_PPC64_TPREL16_HIGHESTA:
17835
0
  case R_PPC64_DTPREL16_HA:
17836
0
  case R_PPC64_DTPREL16_HIGHA:
17837
0
  case R_PPC64_DTPREL16_HIGHERA:
17838
0
  case R_PPC64_DTPREL16_HIGHESTA:
17839
    /* It's just possible that this symbol is a weak symbol
17840
       that's not actually defined anywhere. In that case,
17841
       'sec' would be NULL, and we should leave the symbol
17842
       alone (it will be set to zero elsewhere in the link).  */
17843
0
    if (sec == NULL)
17844
0
      break;
17845
    /* Fall through.  */
17846
17847
0
  case R_PPC64_GOT16_HA:
17848
0
  case R_PPC64_PLTGOT16_HA:
17849
0
  case R_PPC64_PLT16_HA:
17850
0
  case R_PPC64_GOT_TLSGD16_HA:
17851
0
  case R_PPC64_GOT_TLSLD16_HA:
17852
0
  case R_PPC64_GOT_TPREL16_HA:
17853
0
  case R_PPC64_GOT_DTPREL16_HA:
17854
    /* Add 0x10000 if sign bit in 0:15 is set.
17855
       Bits 0:15 are not used.  */
17856
0
    addend += 0x8000;
17857
0
    break;
17858
17859
0
  case R_PPC64_D34_HA30:
17860
0
  case R_PPC64_ADDR16_HIGHERA34:
17861
0
  case R_PPC64_ADDR16_HIGHESTA34:
17862
0
  case R_PPC64_REL16_HIGHERA34:
17863
0
  case R_PPC64_REL16_HIGHESTA34:
17864
0
    if (sec != NULL)
17865
0
      addend += 1ULL << 33;
17866
0
    break;
17867
17868
0
  case R_PPC64_ADDR16_DS:
17869
0
  case R_PPC64_ADDR16_LO_DS:
17870
0
  case R_PPC64_GOT16_DS:
17871
0
  case R_PPC64_GOT16_LO_DS:
17872
0
  case R_PPC64_PLT16_LO_DS:
17873
0
  case R_PPC64_SECTOFF_DS:
17874
0
  case R_PPC64_SECTOFF_LO_DS:
17875
0
  case R_PPC64_TOC16_DS:
17876
0
  case R_PPC64_TOC16_LO_DS:
17877
0
  case R_PPC64_PLTGOT16_DS:
17878
0
  case R_PPC64_PLTGOT16_LO_DS:
17879
0
  case R_PPC64_GOT_TPREL16_DS:
17880
0
  case R_PPC64_GOT_TPREL16_LO_DS:
17881
0
  case R_PPC64_GOT_DTPREL16_DS:
17882
0
  case R_PPC64_GOT_DTPREL16_LO_DS:
17883
0
  case R_PPC64_TPREL16_DS:
17884
0
  case R_PPC64_TPREL16_LO_DS:
17885
0
  case R_PPC64_DTPREL16_DS:
17886
0
  case R_PPC64_DTPREL16_LO_DS:
17887
0
    if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17888
0
      break;
17889
0
    insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17890
0
    mask = 3;
17891
    /* If this reloc is against an lq, lxv, or stxv insn, then
17892
       the value must be a multiple of 16.  This is somewhat of
17893
       a hack, but the "correct" way to do this by defining _DQ
17894
       forms of all the _DS relocs bloats all reloc switches in
17895
       this file.  It doesn't make much sense to use these
17896
       relocs in data, so testing the insn should be safe.  */
17897
0
    if ((insn & (0x3fu << 26)) == (56u << 26)
17898
0
        || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
17899
0
      mask = 15;
17900
0
    relocation += addend;
17901
0
    addend = insn & (mask ^ 3);
17902
0
    if ((relocation & mask) != 0)
17903
0
      {
17904
0
        relocation ^= relocation & mask;
17905
0
        info->callbacks->einfo
17906
    /* xgettext:c-format */
17907
0
    (_("%H: error: %s not a multiple of %u\n"),
17908
0
     input_bfd, input_section, rel->r_offset,
17909
0
     ppc64_elf_howto_table[r_type]->name,
17910
0
     mask + 1);
17911
0
        bfd_set_error (bfd_error_bad_value);
17912
0
        ret = false;
17913
0
        goto copy_reloc;
17914
0
      }
17915
0
    break;
17916
0
  }
17917
17918
      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17919
   because such sections are not SEC_ALLOC and thus ld.so will
17920
   not process them.  */
17921
0
      howto = ppc64_elf_howto_table[r_type];
17922
0
      if (unresolved_reloc
17923
0
    && !((input_section->flags & SEC_DEBUGGING) != 0
17924
0
         && h->elf.def_dynamic)
17925
0
    && _bfd_elf_section_offset (info->output_bfd, info, input_section,
17926
0
              rel->r_offset) != (bfd_vma) -1)
17927
0
  {
17928
0
    info->callbacks->einfo
17929
      /* xgettext:c-format */
17930
0
      (_("%H: unresolvable %s against `%pT'\n"),
17931
0
       input_bfd, input_section, rel->r_offset,
17932
0
       howto->name,
17933
0
       h->elf.root.root.string);
17934
0
    ret = false;
17935
0
  }
17936
17937
      /* 16-bit fields in insns mostly have signed values, but a
17938
   few insns have 16-bit unsigned values.  Really, we should
17939
   have different reloc types.  */
17940
0
      if (howto->complain_on_overflow != complain_overflow_dont
17941
0
    && howto->dst_mask == 0xffff
17942
0
    && (input_section->flags & SEC_CODE) != 0
17943
0
    && offset_in_range (input_section, rel->r_offset & ~3, 4))
17944
0
  {
17945
0
    enum complain_overflow complain = complain_overflow_signed;
17946
17947
0
    insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17948
0
    if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
17949
0
      complain = complain_overflow_bitfield;
17950
0
    else if (howto->rightshift == 0
17951
0
       ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17952
0
          || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17953
0
          || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17954
0
       : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17955
0
          || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17956
0
          || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
17957
0
      complain = complain_overflow_unsigned;
17958
0
    if (howto->complain_on_overflow != complain)
17959
0
      {
17960
0
        alt_howto = *howto;
17961
0
        alt_howto.complain_on_overflow = complain;
17962
0
        howto = &alt_howto;
17963
0
      }
17964
0
  }
17965
17966
0
      switch (r_type)
17967
0
  {
17968
    /* Split field relocs aren't handled by _bfd_final_link_relocate.  */
17969
0
  case R_PPC64_D34:
17970
0
  case R_PPC64_D34_LO:
17971
0
  case R_PPC64_D34_HI30:
17972
0
  case R_PPC64_D34_HA30:
17973
0
  case R_PPC64_PCREL34:
17974
0
  case R_PPC64_GOT_PCREL34:
17975
0
  case R_PPC64_TPREL34:
17976
0
  case R_PPC64_DTPREL34:
17977
0
  case R_PPC64_GOT_TLSGD_PCREL34:
17978
0
  case R_PPC64_GOT_TLSLD_PCREL34:
17979
0
  case R_PPC64_GOT_TPREL_PCREL34:
17980
0
  case R_PPC64_GOT_DTPREL_PCREL34:
17981
0
  case R_PPC64_PLT_PCREL34:
17982
0
  case R_PPC64_PLT_PCREL34_NOTOC:
17983
0
  case R_PPC64_D28:
17984
0
  case R_PPC64_PCREL28:
17985
0
    if (!offset_in_range (input_section, rel->r_offset, 8))
17986
0
      r = bfd_reloc_outofrange;
17987
0
    else
17988
0
      {
17989
0
        relocation += addend;
17990
0
        if (howto->pc_relative)
17991
0
    relocation -= (rel->r_offset
17992
0
             + input_section->output_offset
17993
0
             + input_section->output_section->vma);
17994
0
        relocation >>= howto->rightshift;
17995
17996
0
        pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17997
0
        pinsn <<= 32;
17998
0
        pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17999
18000
0
        pinsn &= ~howto->dst_mask;
18001
0
        pinsn |= (((relocation << 16) | (relocation & 0xffff))
18002
0
      & howto->dst_mask);
18003
0
        bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
18004
0
        bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
18005
0
        r = bfd_reloc_ok;
18006
0
        if (howto->complain_on_overflow == complain_overflow_signed
18007
0
      && (relocation + (1ULL << (howto->bitsize - 1))
18008
0
          >= 1ULL << howto->bitsize))
18009
0
    r = bfd_reloc_overflow;
18010
0
      }
18011
0
    break;
18012
18013
0
  case R_PPC64_REL16DX_HA:
18014
0
    if (!offset_in_range (input_section, rel->r_offset, 4))
18015
0
      r = bfd_reloc_outofrange;
18016
0
    else
18017
0
      {
18018
0
        relocation += addend;
18019
0
        relocation -= (rel->r_offset
18020
0
           + input_section->output_offset
18021
0
           + input_section->output_section->vma);
18022
0
        relocation = (bfd_signed_vma) relocation >> 16;
18023
0
        insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
18024
0
        insn &= ~0x1fffc1;
18025
0
        insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
18026
0
        bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
18027
0
        r = bfd_reloc_ok;
18028
0
        if (relocation + 0x8000 > 0xffff)
18029
0
    r = bfd_reloc_overflow;
18030
0
      }
18031
0
    break;
18032
18033
0
  default:
18034
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
18035
0
          contents, rel->r_offset,
18036
0
          relocation, addend);
18037
0
  }
18038
18039
0
      if (r != bfd_reloc_ok)
18040
0
  {
18041
0
    char *more_info = NULL;
18042
0
    const char *reloc_name = howto->name;
18043
18044
0
    if (reloc_dest != DEST_NORMAL)
18045
0
      {
18046
0
        more_info = bfd_malloc (strlen (reloc_name) + 8);
18047
0
        if (more_info != NULL)
18048
0
    {
18049
0
      strcpy (more_info, reloc_name);
18050
0
      strcat (more_info, (reloc_dest == DEST_OPD
18051
0
              ? " (OPD)" : " (stub)"));
18052
0
      reloc_name = more_info;
18053
0
    }
18054
0
      }
18055
18056
0
    if (r == bfd_reloc_overflow)
18057
0
      {
18058
        /* On code like "if (foo) foo();" don't report overflow
18059
     on a branch to zero when foo is undefined.  */
18060
0
        if (!warned
18061
0
      && (reloc_dest == DEST_STUB
18062
0
          || !(h != NULL
18063
0
         && (h->elf.root.type == bfd_link_hash_undefweak
18064
0
             || h->elf.root.type == bfd_link_hash_undefined)
18065
0
         && is_branch_reloc (r_type))))
18066
0
    info->callbacks->reloc_overflow
18067
0
      (info, (struct bfd_link_hash_entry *) h, sym_name,
18068
0
       reloc_name, orig_rel.r_addend, input_bfd, input_section,
18069
0
       rel->r_offset);
18070
0
      }
18071
0
    else
18072
0
      {
18073
0
        info->callbacks->einfo
18074
    /* xgettext:c-format */
18075
0
    (_("%H: %s against `%pT': error %d\n"),
18076
0
     input_bfd, input_section, rel->r_offset,
18077
0
     reloc_name, sym_name, (int) r);
18078
0
        ret = false;
18079
0
      }
18080
0
    free (more_info);
18081
0
  }
18082
0
    copy_reloc:
18083
0
      if (wrel != rel)
18084
0
  *wrel = *rel;
18085
0
    }
18086
18087
0
  if (wrel != rel)
18088
0
    {
18089
0
      Elf_Internal_Shdr *rel_hdr;
18090
0
      size_t deleted = rel - wrel;
18091
18092
0
      rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
18093
0
      rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18094
0
      rel_hdr = _bfd_elf_single_rel_hdr (input_section);
18095
0
      rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18096
0
      input_section->reloc_count -= deleted;
18097
0
    }
18098
18099
  /* If we're emitting relocations, then shortly after this function
18100
     returns, reloc offsets and addends for this section will be
18101
     adjusted.  Worse, reloc symbol indices will be for the output
18102
     file rather than the input.  Save a copy of the relocs for
18103
     opd_entry_value.  */
18104
0
  if (is_opd
18105
0
      && (info->emitrelocations || bfd_link_relocatable (info))
18106
0
      && input_section->reloc_count != 0)
18107
0
    {
18108
0
      bfd_size_type amt;
18109
0
      amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
18110
0
      rel = bfd_alloc (input_bfd, amt);
18111
0
      ppc64_elf_section_data (input_section)->u.opd.u.relocs = rel;
18112
0
      if (rel == NULL)
18113
0
  return false;
18114
0
      memcpy (rel, relocs, amt);
18115
0
    }
18116
0
  return ret;
18117
0
}
18118
18119
/* Adjust the value of any local symbols in opd sections.  */
18120
18121
static int
18122
ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
18123
            const char *name ATTRIBUTE_UNUSED,
18124
            Elf_Internal_Sym *elfsym,
18125
            asection *input_sec,
18126
            struct elf_link_hash_entry *h)
18127
0
{
18128
0
  struct _opd_sec_data *opd;
18129
0
  long adjust;
18130
0
  bfd_vma value;
18131
18132
0
  if (h != NULL)
18133
0
    return 1;
18134
18135
0
  opd = get_opd_info (input_sec);
18136
0
  if (opd == NULL || opd->adjust == NULL)
18137
0
    return 1;
18138
18139
0
  value = elfsym->st_value - input_sec->output_offset;
18140
0
  if (!bfd_link_relocatable (info))
18141
0
    value -= input_sec->output_section->vma;
18142
18143
0
  adjust = opd->adjust[OPD_NDX (value)];
18144
0
  if (adjust == -1)
18145
0
    return 2;
18146
18147
0
  elfsym->st_value += adjust;
18148
0
  return 1;
18149
0
}
18150
18151
/* Finish up dynamic symbol handling.  We set the contents of various
18152
   dynamic sections here.  */
18153
18154
static bool
18155
ppc64_elf_finish_dynamic_symbol (struct bfd_link_info *info,
18156
         struct elf_link_hash_entry *h,
18157
         Elf_Internal_Sym *sym)
18158
0
{
18159
0
  struct ppc_link_hash_table *htab;
18160
0
  struct plt_entry *ent;
18161
18162
0
  htab = ppc_hash_table (info);
18163
18164
0
  if (!htab->opd_abi && !h->def_regular)
18165
0
    for (ent = h->plt.plist; ent != NULL; ent = ent->next)
18166
0
      if (ent->plt.offset != (bfd_vma) -1)
18167
0
  {
18168
    /* Mark the symbol as undefined, rather than as
18169
       defined in glink.  Leave the value if there were
18170
       any relocations where pointer equality matters
18171
       (this is a clue for the dynamic linker, to make
18172
       function pointer comparisons work between an
18173
       application and shared library), otherwise set it
18174
       to zero.  */
18175
0
    sym->st_shndx = SHN_UNDEF;
18176
0
    if (!h->pointer_equality_needed)
18177
0
      sym->st_value = 0;
18178
0
    else if (!h->ref_regular_nonweak)
18179
0
      {
18180
        /* This breaks function pointer comparisons, but
18181
     that is better than breaking tests for a NULL
18182
     function pointer.  */
18183
0
        sym->st_value = 0;
18184
0
      }
18185
0
    break;
18186
0
  }
18187
18188
0
  if (h->needs_copy
18189
0
      && (h->root.type == bfd_link_hash_defined
18190
0
    || h->root.type == bfd_link_hash_defweak)
18191
0
      && (h->root.u.def.section == htab->elf.sdynbss
18192
0
    || h->root.u.def.section == htab->elf.sdynrelro))
18193
0
    {
18194
      /* This symbol needs a copy reloc.  Set it up.  */
18195
0
      Elf_Internal_Rela rela;
18196
0
      asection *srel;
18197
18198
0
      if (h->dynindx == -1)
18199
0
  abort ();
18200
18201
0
      rela.r_offset = defined_sym_val (h);
18202
0
      rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18203
0
      rela.r_addend = 0;
18204
0
      if (h->root.u.def.section == htab->elf.sdynrelro)
18205
0
  srel = htab->elf.sreldynrelro;
18206
0
      else
18207
0
  srel = htab->elf.srelbss;
18208
0
      BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd, &rela, srel));
18209
0
    }
18210
18211
0
  return true;
18212
0
}
18213
18214
/* Used to decide how to sort relocs in an optimal manner for the
18215
   dynamic linker, before writing them out.  */
18216
18217
static enum elf_reloc_type_class
18218
ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18219
          const asection *rel_sec,
18220
          const Elf_Internal_Rela *rela)
18221
0
{
18222
0
  enum elf_ppc64_reloc_type r_type;
18223
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
18224
18225
0
  if (rel_sec == htab->elf.irelplt)
18226
0
    return reloc_class_ifunc;
18227
18228
0
  r_type = ELF64_R_TYPE (rela->r_info);
18229
0
  switch (r_type)
18230
0
    {
18231
0
    case R_PPC64_RELATIVE:
18232
0
      return reloc_class_relative;
18233
0
    case R_PPC64_JMP_SLOT:
18234
0
      return reloc_class_plt;
18235
0
    case R_PPC64_COPY:
18236
0
      return reloc_class_copy;
18237
0
    default:
18238
0
      return reloc_class_normal;
18239
0
    }
18240
0
}
18241
18242
/* Finish up the dynamic sections.  */
18243
18244
static bool
18245
ppc64_elf_finish_dynamic_sections (struct bfd_link_info *info,
18246
           bfd_byte *buf)
18247
0
{
18248
0
  struct ppc_link_hash_table *htab;
18249
0
  bfd *dynobj;
18250
0
  asection *sdyn;
18251
18252
0
  htab = ppc_hash_table (info);
18253
0
  if (htab == NULL)
18254
0
    return false;
18255
18256
0
  dynobj = htab->elf.dynobj;
18257
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
18258
18259
0
  if (htab->elf.dynamic_sections_created)
18260
0
    {
18261
0
      Elf64_External_Dyn *dyncon, *dynconend;
18262
18263
0
      if (sdyn == NULL || htab->elf.sgot == NULL)
18264
0
  abort ();
18265
18266
0
      dyncon = (Elf64_External_Dyn *) sdyn->contents;
18267
0
      dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
18268
0
      for (; dyncon < dynconend; dyncon++)
18269
0
  {
18270
0
    Elf_Internal_Dyn dyn;
18271
0
    asection *s;
18272
18273
0
    bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18274
18275
0
    switch (dyn.d_tag)
18276
0
      {
18277
0
      default:
18278
0
        continue;
18279
18280
0
      case DT_PPC64_GLINK:
18281
0
        s = htab->glink;
18282
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18283
        /* We stupidly defined DT_PPC64_GLINK to be the start
18284
     of glink rather than the first entry point, which is
18285
     what ld.so needs, and now have a bigger stub to
18286
     support automatic multiple TOCs.  */
18287
0
        dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
18288
0
        break;
18289
18290
0
      case DT_PPC64_OPD:
18291
0
        s = bfd_get_section_by_name (info->output_bfd, ".opd");
18292
0
        if (s == NULL)
18293
0
    continue;
18294
0
        dyn.d_un.d_ptr = s->vma;
18295
0
        break;
18296
18297
0
      case DT_PPC64_OPT:
18298
0
        if ((htab->do_multi_toc && htab->multi_toc_needed)
18299
0
      || htab->notoc_plt)
18300
0
    dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
18301
0
        if (htab->has_plt_localentry0)
18302
0
    dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
18303
0
        break;
18304
18305
0
      case DT_PPC64_OPDSZ:
18306
0
        s = bfd_get_section_by_name (info->output_bfd, ".opd");
18307
0
        if (s == NULL)
18308
0
    continue;
18309
0
        dyn.d_un.d_val = s->size;
18310
0
        break;
18311
18312
0
      case DT_PLTGOT:
18313
0
        s = htab->elf.splt;
18314
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18315
0
        break;
18316
18317
0
      case DT_JMPREL:
18318
0
        s = htab->elf.srelplt;
18319
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18320
0
        break;
18321
18322
0
      case DT_PLTRELSZ:
18323
0
        dyn.d_un.d_val = htab->elf.srelplt->size;
18324
0
        break;
18325
18326
0
      case DT_TEXTREL:
18327
0
        if (htab->elf.ifunc_resolvers)
18328
0
    info->callbacks->einfo
18329
0
      (_("%P: warning: text relocations and GNU indirect "
18330
0
         "functions may result in a segfault at runtime\n"));
18331
0
        continue;
18332
0
      }
18333
18334
0
    bfd_elf64_swap_dyn_out (info->output_bfd, &dyn, dyncon);
18335
0
  }
18336
0
    }
18337
18338
0
  if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18339
0
      && htab->elf.sgot->output_section != bfd_abs_section_ptr)
18340
0
    {
18341
      /* Fill in the first entry in the global offset table.
18342
   We use it to hold the link-time TOCbase.  */
18343
0
      bfd_put_64 (info->output_bfd,
18344
0
      elf_gp (info->output_bfd) + TOC_BASE_OFF,
18345
0
      htab->elf.sgot->contents);
18346
18347
      /* Set .got entry size.  */
18348
0
      elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18349
0
  = 8;
18350
0
    }
18351
18352
0
  if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18353
0
      && htab->elf.splt->output_section != bfd_abs_section_ptr)
18354
0
    {
18355
      /* Set .plt entry size.  */
18356
0
      elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
18357
0
  = PLT_ENTRY_SIZE (htab);
18358
0
    }
18359
18360
  /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18361
     brlt ourselves if emitrelocations.  */
18362
0
  if (htab->brlt != NULL
18363
0
      && htab->brlt->reloc_count != 0
18364
0
      && !_bfd_elf_link_output_relocs (info->output_bfd,
18365
0
               htab->brlt,
18366
0
               elf_section_data (htab->brlt)->rela.hdr,
18367
0
               elf_section_data (htab->brlt)->relocs,
18368
0
               NULL))
18369
0
    return false;
18370
18371
0
  if (htab->glink != NULL
18372
0
      && htab->glink->reloc_count != 0
18373
0
      && !_bfd_elf_link_output_relocs (info->output_bfd,
18374
0
               htab->glink,
18375
0
               elf_section_data (htab->glink)->rela.hdr,
18376
0
               elf_section_data (htab->glink)->relocs,
18377
0
               NULL))
18378
0
    return false;
18379
18380
18381
0
  if (htab->glink_eh_frame != NULL
18382
0
      && htab->glink_eh_frame->size != 0
18383
0
      && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18384
0
      && !_bfd_elf_write_linker_section_eh_frame (info->output_bfd, info,
18385
0
              htab->glink_eh_frame, buf))
18386
0
    return false;
18387
18388
  /* We need to handle writing out multiple GOT sections ourselves,
18389
     since we didn't add them to DYNOBJ.  We know dynobj is the first
18390
     bfd.  */
18391
0
  while ((dynobj = dynobj->link.next) != NULL)
18392
0
    {
18393
0
      asection *s;
18394
18395
0
      if (!is_ppc64_elf (dynobj))
18396
0
  continue;
18397
18398
0
      s = ppc64_elf_tdata (dynobj)->got;
18399
0
      if (s != NULL
18400
0
    && s->size != 0
18401
0
    && s->output_section != bfd_abs_section_ptr
18402
0
    && !bfd_set_section_contents (info->output_bfd, s->output_section,
18403
0
          s->contents, s->output_offset,
18404
0
          s->size))
18405
0
  return false;
18406
0
      s = ppc64_elf_tdata (dynobj)->relgot;
18407
0
      if (s != NULL
18408
0
    && s->size != 0
18409
0
    && s->output_section != bfd_abs_section_ptr
18410
0
    && !bfd_set_section_contents (info->output_bfd, s->output_section,
18411
0
          s->contents, s->output_offset,
18412
0
          s->size))
18413
0
  return false;
18414
0
    }
18415
18416
0
  return true;
18417
0
}
18418
18419
static bool
18420
ppc64_elf_free_cached_info (bfd *abfd)
18421
170k
{
18422
170k
  if (abfd->sections)
18423
348
    for (asection *opd = bfd_get_section_by_name (abfd, ".opd");
18424
359
   opd != NULL;
18425
348
   opd = bfd_get_next_section_by_name (NULL, opd))
18426
11
      if (opd->reloc_count == 0)
18427
11
  free (ppc64_elf_section_data (opd)->u.opd.u.contents);
18428
18429
170k
  return _bfd_elf_free_cached_info (abfd);
18430
170k
}
18431
18432
#include "elf64-target.h"
18433
18434
/* FreeBSD support */
18435
18436
#undef  TARGET_LITTLE_SYM
18437
#define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
18438
#undef  TARGET_LITTLE_NAME
18439
#define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
18440
18441
#undef  TARGET_BIG_SYM
18442
#define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
18443
#undef  TARGET_BIG_NAME
18444
#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18445
18446
#undef  ELF_OSABI
18447
#define ELF_OSABI       ELFOSABI_FREEBSD
18448
#undef  ELF_OSABI_EXACT
18449
#define ELF_OSABI_EXACT 1
18450
18451
#undef  elf64_bed
18452
#define elf64_bed elf64_powerpc_fbsd_bed
18453
18454
#include "elf64-target.h"