Coverage Report

Created: 2026-03-10 08:46

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
39
#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_can_gc_sections 1
87
#define elf_backend_can_refcount 1
88
#define elf_backend_rela_normal 1
89
#define elf_backend_dtrel_excludes_plt 1
90
#define elf_backend_default_execstack 0
91
92
#define bfd_elf64_mkobject          ppc64_elf_mkobject
93
#define bfd_elf64_bfd_free_cached_info        ppc64_elf_free_cached_info
94
#define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
95
#define bfd_elf64_bfd_reloc_name_lookup       ppc64_elf_reloc_name_lookup
96
#define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
97
#define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
98
#define bfd_elf64_new_section_hook        ppc64_elf_new_section_hook
99
#define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
100
#define bfd_elf64_get_synthetic_symtab        ppc64_elf_get_synthetic_symtab
101
#define bfd_elf64_bfd_link_just_syms        ppc64_elf_link_just_syms
102
#define bfd_elf64_bfd_gc_sections       ppc64_elf_gc_sections
103
104
#define elf_backend_object_p          ppc64_elf_object_p
105
#define elf_backend_grok_prstatus       ppc64_elf_grok_prstatus
106
#define elf_backend_grok_psinfo         ppc64_elf_grok_psinfo
107
#define elf_backend_write_core_note       ppc64_elf_write_core_note
108
#define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
109
#define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
110
#define elf_backend_add_symbol_hook       ppc64_elf_add_symbol_hook
111
#define elf_backend_check_directives        ppc64_elf_before_check_relocs
112
#define elf_backend_notice_as_needed        ppc64_elf_notice_as_needed
113
#define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
114
#define elf_backend_check_relocs        ppc64_elf_check_relocs
115
#define elf_backend_relocs_compatible       _bfd_elf_relocs_compatible
116
#define elf_backend_gc_keep         ppc64_elf_gc_keep
117
#define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
118
#define elf_backend_gc_mark_hook        ppc64_elf_gc_mark_hook
119
#define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
120
#define elf_backend_hide_symbol         ppc64_elf_hide_symbol
121
#define elf_backend_maybe_function_sym        ppc64_elf_maybe_function_sym
122
#define elf_backend_early_size_sections       ppc64_elf_edit
123
#define elf_backend_late_size_sections        ppc64_elf_late_size_sections
124
#define elf_backend_hash_symbol         ppc64_elf_hash_symbol
125
#define elf_backend_init_index_section        _bfd_elf_init_2_index_sections
126
#define elf_backend_action_discarded        ppc64_elf_action_discarded
127
#define elf_backend_relocate_section        ppc64_elf_relocate_section
128
#define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
129
#define elf_backend_reloc_type_class        ppc64_elf_reloc_type_class
130
#define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
131
#define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
132
#define elf_backend_special_sections        ppc64_elf_special_sections
133
#define elf_backend_section_flags       ppc64_elf_section_flags
134
#define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
135
#define elf_backend_merge_symbol        ppc64_elf_merge_symbol
136
#define elf_backend_get_reloc_section       bfd_get_section_by_name
137
138
/* The name of the dynamic interpreter.  This is put in the .interp
139
   section.  */
140
0
#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
141
142
/* The size in bytes of an entry in the procedure linkage table.  */
143
0
#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
144
0
#define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
145
146
/* The initial size of the plt reserved for the dynamic linker.  */
147
0
#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
148
149
/* Offsets to some stack save slots.  */
150
#define STK_LR 16
151
0
#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
152
/* This one is dodgy.  ELFv2 does not have a linker word, so use the
153
   CR save slot.  Used only by optimised __tls_get_addr call stub,
154
   relying on __tls_get_addr_opt not saving CR..  */
155
0
#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
156
157
/* TOC base pointers offset from start of TOC.  */
158
25
#define TOC_BASE_OFF  0x8000
159
/* TOC base alignment.  */
160
24
#define TOC_BASE_ALIGN  256
161
162
/* Offset of tp and dtp pointers from start of TLS block.  */
163
0
#define TP_OFFSET 0x7000
164
0
#define DTP_OFFSET  0x8000
165
166
/* .plt call stub instructions.  The normal stub is like this, but
167
   sometimes the .plt entry crosses a 64k boundary and we need to
168
   insert an addi to adjust r11.  */
169
#define STD_R2_0R1  0xf8410000  /* std   %r2,0+40(%r1)       */
170
#define ADDIS_R11_R2  0x3d620000  /* addis %r11,%r2,xxx@ha     */
171
#define LD_R12_0R11 0xe98b0000  /* ld  %r12,xxx+0@l(%r11)  */
172
#define MTCTR_R12 0x7d8903a6  /* mtctr %r12        */
173
#define LD_R2_0R11  0xe84b0000  /* ld  %r2,xxx+8@l(%r11)   */
174
#define LD_R11_0R11 0xe96b0000  /* ld  %r11,xxx+16@l(%r11) */
175
#define BCTR    0x4e800420  /* bctr          */
176
177
#define ADDI_R11_R11  0x396b0000  /* addi %r11,%r11,off@l  */
178
#define ADDI_R12_R11  0x398b0000  /* addi %r12,%r11,off@l  */
179
#define ADDI_R12_R12  0x398c0000  /* addi %r12,%r12,off@l  */
180
#define ADDIS_R2_R2 0x3c420000  /* addis %r2,%r2,off@ha  */
181
0
#define ADDI_R2_R2  0x38420000  /* addi  %r2,%r2,off@l   */
182
183
#define XOR_R2_R12_R12  0x7d826278  /* xor   %r2,%r12,%r12   */
184
#define ADD_R11_R11_R2  0x7d6b1214  /* add   %r11,%r11,%r2   */
185
#define XOR_R11_R12_R12 0x7d8b6278  /* xor   %r11,%r12,%r12  */
186
#define ADD_R2_R2_R11 0x7c425a14  /* add   %r2,%r2,%r11  */
187
#define CMPLDI_R2_0 0x28220000  /* cmpldi %r2,0    */
188
#define BNECTR    0x4ca20420  /* bnectr+     */
189
#define BNECTR_P4 0x4ce20420  /* bnectr+     */
190
191
#define LD_R12_0R2  0xe9820000  /* ld  %r12,xxx+0(%r2) */
192
#define LD_R11_0R2  0xe9620000  /* ld  %r11,xxx+0(%r2) */
193
#define LD_R2_0R2 0xe8420000  /* ld  %r2,xxx+0(%r2)  */
194
195
0
#define LD_R2_0R1 0xe8410000  /* ld  %r2,0(%r1)  */
196
0
#define LD_R2_0R12  0xe84c0000  /* ld  %r2,0(%r12)   */
197
0
#define ADD_R2_R2_R12 0x7c426214  /* add   %r2,%r2,%r12  */
198
199
#define LI_R11_0  0x39600000  /* li    %r11,0   */
200
#define LIS_R2    0x3c400000  /* lis %r2,xxx@ha   */
201
#define LIS_R11   0x3d600000  /* lis %r11,xxx@ha    */
202
#define LIS_R12   0x3d800000  /* lis %r12,xxx@ha    */
203
0
#define ADDIS_R2_R12  0x3c4c0000  /* addis %r2,%r12,xxx@ha  */
204
#define ADDIS_R12_R2  0x3d820000  /* addis %r12,%r2,xxx@ha  */
205
#define ADDIS_R12_R11 0x3d8b0000  /* addis %r12,%r11,xxx@ha */
206
#define ADDIS_R12_R12 0x3d8c0000  /* addis %r12,%r12,xxx@ha */
207
#define ORIS_R12_R12_0  0x658c0000  /* oris  %r12,%r12,xxx@hi */
208
#define ORI_R11_R11_0 0x616b0000  /* ori   %r11,%r11,xxx@l  */
209
#define ORI_R12_R12_0 0x618c0000  /* ori   %r12,%r12,xxx@l  */
210
#define LD_R12_0R12 0xe98c0000  /* ld  %r12,xxx@l(%r12) */
211
#define SLDI_R11_R11_34 0x796b1746  /* sldi  %r11,%r11,34     */
212
#define SLDI_R12_R12_32 0x799c07c6  /* sldi  %r12,%r12,32     */
213
#define LDX_R12_R11_R12 0x7d8b602a  /* ldx   %r12,%r11,%r12   */
214
#define ADD_R12_R11_R12 0x7d8b6214  /* add   %r12,%r11,%r12   */
215
0
#define PADDI_R12_PC  0x0610000039800000ULL
216
0
#define PLD_R12_PC  0x04100000e5800000ULL
217
0
#define PNOP    0x0700000000000000ULL
218
219
/* __glink_PLTresolve stub instructions.  We enter with the index in
220
   R0 for ELFv1, and the address of a glink branch in R12 for ELFv2.  */
221
#define GLINK_PLTRESOLVE_SIZE(htab)     \
222
0
  (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
223
          /* 0:       */
224
          /*  .quad plt0-1f   */
225
          /* __glink:     */
226
#define MFLR_R12  0x7d8802a6  /*  mflr %12      */
227
#define BCL_20_31 0x429f0005  /*  bcl 20,31,1f    */
228
          /* 1:       */
229
#define MFLR_R11  0x7d6802a6  /*  mflr %11      */
230
          /*  ld %2,(0b-1b)(%11)    */
231
#define MTLR_R12  0x7d8803a6  /*  mtlr %12      */
232
#define ADD_R11_R2_R11  0x7d625a14  /*  add %11,%2,%11    */
233
          /*  ld %12,0(%11)   */
234
          /*  ld %2,8(%11)    */
235
          /*  mtctr %12     */
236
          /*  ld %11,16(%11)    */
237
          /*  bctr      */
238
239
#define MFLR_R0   0x7c0802a6  /* mflr %r0     */
240
#define MTLR_R0   0x7c0803a6  /* mtlr %r0     */
241
#define SUB_R12_R12_R11 0x7d8b6050  /* subf %r12,%r11,%r12    */
242
#define ADDI_R0_R12 0x380c0000  /* addi %r0,%r12,0    */
243
#define SRDI_R0_R0_2  0x7800f082  /* rldicl %r0,%r0,62,2    */
244
0
#define LD_R0_0R11  0xe80b0000  /* ld %r0,0(%r11)   */
245
#define ADD_R11_R0_R11  0x7d605a14  /* add %r11,%r0,%r11    */
246
247
/* Pad with this.  */
248
0
#define NOP   0x60000000
249
250
/* Some other nops.  */
251
0
#define CROR_151515 0x4def7b82
252
0
#define CROR_313131 0x4ffffb82
253
254
/* .glink entries for the first 32k functions are two instructions.  */
255
#define LI_R0_0   0x38000000  /* li    %r0,0    */
256
0
#define B_DOT   0x48000000  /* b     .    */
257
258
/* After that, we need two instructions to load the index, followed by
259
   a branch.  */
260
#define LIS_R0_0  0x3c000000  /* lis   %r0,0    */
261
#define ORI_R0_R0_0 0x60000000  /* ori   %r0,%r0,0  */
262
263
/* Instructions used by the save and restore reg functions.  */
264
#define STD_R0_0R1  0xf8010000  /* std   %r0,0(%r1) */
265
#define STD_R0_0R12 0xf80c0000  /* std   %r0,0(%r12)  */
266
#define LD_R0_0R1 0xe8010000  /* ld    %r0,0(%r1) */
267
#define LD_R0_0R12  0xe80c0000  /* ld    %r0,0(%r12)  */
268
#define STFD_FR0_0R1  0xd8010000  /* stfd  %fr0,0(%r1)  */
269
#define LFD_FR0_0R1 0xc8010000  /* lfd   %fr0,0(%r1)  */
270
#define LI_R12_0  0x39800000  /* li    %r12,0   */
271
#define STVX_VR0_R12_R0 0x7c0c01ce  /* stvx  %v0,%r12,%r0 */
272
#define LVX_VR0_R12_R0  0x7c0c00ce  /* lvx   %v0,%r12,%r0 */
273
#define MTLR_R0   0x7c0803a6  /* mtlr  %r0    */
274
#define BLR   0x4e800020  /* blr      */
275
276
/* Since .opd is an array of descriptors and each entry will end up
277
   with identical R_PPC64_RELATIVE relocs, there is really no need to
278
   propagate .opd relocs;  The dynamic linker should be taught to
279
   relocate .opd without reloc entries.  */
280
#ifndef NO_OPD_RELOCS
281
0
#define NO_OPD_RELOCS 0
282
#endif
283
284
#ifndef ARRAY_SIZE
285
6.52k
#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
286
#endif
287
288
static inline int
289
abiversion (bfd *abfd)
290
15
{
291
15
  return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
292
15
}
293
294
static inline void
295
set_abiversion (bfd *abfd, int ver)
296
0
{
297
0
  elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
298
0
  elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
299
0
}
300
301
#define is_ppc64_elf(bfd) \
302
61
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
303
61
   && elf_object_id (bfd) == PPC64_ELF_DATA)
304

305
/* Relocation HOWTO's.  */
306
/* Like other ELF RELA targets that don't apply multiple
307
   field-altering relocations to the same localation, src_mask is
308
   always zero and pcrel_offset is the same as pc_relative.
309
   PowerPC can always use a zero bitpos, even when the field is not at
310
   the LSB.  For example, a REL24 could use rightshift=2, bisize=24
311
   and bitpos=2 which matches the ABI description, or as we do here,
312
   rightshift=0, bitsize=26 and bitpos=0.  */
313
#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
314
      complain, special_func)       \
315
  HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
316
   complain_overflow_ ## complain, special_func,    \
317
   #type, false, 0, mask, pc_relative)
318
319
static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
320
321
static reloc_howto_type ppc64_elf_howto_raw[] =
322
{
323
  /* This reloc does nothing.  */
324
  HOW (R_PPC64_NONE, 0, 0, 0, 0, false, dont,
325
       bfd_elf_generic_reloc),
326
327
  /* A standard 32 bit relocation.  */
328
  HOW (R_PPC64_ADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
329
       bfd_elf_generic_reloc),
330
331
  /* An absolute 26 bit branch; the lower two bits must be zero.
332
     FIXME: we don't check that, we just clear them.  */
333
  HOW (R_PPC64_ADDR24, 4, 26, 0x03fffffc, 0, false, bitfield,
334
       bfd_elf_generic_reloc),
335
336
  /* A standard 16 bit relocation.  */
337
  HOW (R_PPC64_ADDR16, 2, 16, 0xffff, 0, false, bitfield,
338
       bfd_elf_generic_reloc),
339
340
  /* A 16 bit relocation without overflow.  */
341
  HOW (R_PPC64_ADDR16_LO, 2, 16, 0xffff, 0, false, dont,
342
       bfd_elf_generic_reloc),
343
344
  /* Bits 16-31 of an address.  */
345
  HOW (R_PPC64_ADDR16_HI, 2, 16, 0xffff, 16, false, signed,
346
       bfd_elf_generic_reloc),
347
348
  /* Bits 16-31 of an address, plus 1 if the contents of the low 16
349
     bits, treated as a signed number, is negative.  */
350
  HOW (R_PPC64_ADDR16_HA, 2, 16, 0xffff, 16, false, signed,
351
       ppc64_elf_ha_reloc),
352
353
  /* An absolute 16 bit branch; the lower two bits must be zero.
354
     FIXME: we don't check that, we just clear them.  */
355
  HOW (R_PPC64_ADDR14, 4, 16, 0x0000fffc, 0, false, signed,
356
       ppc64_elf_branch_reloc),
357
358
  /* An absolute 16 bit branch, for which bit 10 should be set to
359
     indicate that the branch is expected to be taken.  The lower two
360
     bits must be zero.  */
361
  HOW (R_PPC64_ADDR14_BRTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
362
       ppc64_elf_brtaken_reloc),
363
364
  /* An absolute 16 bit branch, for which bit 10 should be set to
365
     indicate that the branch is not expected to be taken.  The lower
366
     two bits must be zero.  */
367
  HOW (R_PPC64_ADDR14_BRNTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
368
       ppc64_elf_brtaken_reloc),
369
370
  /* A relative 26 bit branch; the lower two bits must be zero.  */
371
  HOW (R_PPC64_REL24, 4, 26, 0x03fffffc, 0, true, signed,
372
       ppc64_elf_branch_reloc),
373
374
  /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer.  */
375
  HOW (R_PPC64_REL24_NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
376
       ppc64_elf_branch_reloc),
377
378
  /* Another variant, when p10 insns can't be used on stubs.  */
379
  HOW (R_PPC64_REL24_P9NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
380
       ppc64_elf_branch_reloc),
381
382
  /* A relative 16 bit branch; the lower two bits must be zero.  */
383
  HOW (R_PPC64_REL14, 4, 16, 0x0000fffc, 0, true, signed,
384
       ppc64_elf_branch_reloc),
385
386
  /* A relative 16 bit branch.  Bit 10 should be set to indicate that
387
     the branch is expected to be taken.  The lower two bits must be
388
     zero.  */
389
  HOW (R_PPC64_REL14_BRTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
390
       ppc64_elf_brtaken_reloc),
391
392
  /* A relative 16 bit branch.  Bit 10 should be set to indicate that
393
     the branch is not expected to be taken.  The lower two bits must
394
     be zero.  */
395
  HOW (R_PPC64_REL14_BRNTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
396
       ppc64_elf_brtaken_reloc),
397
398
  /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
399
     symbol.  */
400
  HOW (R_PPC64_GOT16, 2, 16, 0xffff, 0, false, signed,
401
       ppc64_elf_unhandled_reloc),
402
403
  /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
404
     the symbol.  */
405
  HOW (R_PPC64_GOT16_LO, 2, 16, 0xffff, 0, false, dont,
406
       ppc64_elf_unhandled_reloc),
407
408
  /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
409
     the symbol.  */
410
  HOW (R_PPC64_GOT16_HI, 2, 16, 0xffff, 16, false, signed,
411
       ppc64_elf_unhandled_reloc),
412
413
  /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
414
     the symbol.  */
415
  HOW (R_PPC64_GOT16_HA, 2, 16, 0xffff, 16, false, signed,
416
       ppc64_elf_unhandled_reloc),
417
418
  /* This is used only by the dynamic linker.  The symbol should exist
419
     both in the object being run and in some shared library.  The
420
     dynamic linker copies the data addressed by the symbol from the
421
     shared library into the object, because the object being
422
     run has to have the data at some particular address.  */
423
  HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
424
       ppc64_elf_unhandled_reloc),
425
426
  /* Like R_PPC64_ADDR64, but used when setting global offset table
427
     entries.  */
428
  HOW (R_PPC64_GLOB_DAT, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
429
       ppc64_elf_unhandled_reloc),
430
431
  /* Created by the link editor.  Marks a procedure linkage table
432
     entry for a symbol.  */
433
  HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
434
       ppc64_elf_unhandled_reloc),
435
436
  /* Used only by the dynamic linker.  When the object is run, this
437
     doubleword64 is set to the load address of the object, plus the
438
     addend.  */
439
  HOW (R_PPC64_RELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
440
       bfd_elf_generic_reloc),
441
442
  /* Like R_PPC64_ADDR32, but may be unaligned.  */
443
  HOW (R_PPC64_UADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
444
       bfd_elf_generic_reloc),
445
446
  /* Like R_PPC64_ADDR16, but may be unaligned.  */
447
  HOW (R_PPC64_UADDR16, 2, 16, 0xffff, 0, false, bitfield,
448
       bfd_elf_generic_reloc),
449
450
  /* 32-bit PC relative.  */
451
  HOW (R_PPC64_REL32, 4, 32, 0xffffffff, 0, true, signed,
452
       bfd_elf_generic_reloc),
453
454
  /* 32-bit relocation to the symbol's procedure linkage table.  */
455
  HOW (R_PPC64_PLT32, 4, 32, 0xffffffff, 0, false, bitfield,
456
       ppc64_elf_unhandled_reloc),
457
458
  /* 32-bit PC relative relocation to the symbol's procedure linkage table.
459
     FIXME: R_PPC64_PLTREL32 not supported.  */
460
  HOW (R_PPC64_PLTREL32, 4, 32, 0xffffffff, 0, true, signed,
461
       ppc64_elf_unhandled_reloc),
462
463
  /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
464
     the symbol.  */
465
  HOW (R_PPC64_PLT16_LO, 2, 16, 0xffff, 0, false, dont,
466
       ppc64_elf_unhandled_reloc),
467
468
  /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
469
     the symbol.  */
470
  HOW (R_PPC64_PLT16_HI, 2, 16, 0xffff, 16, false, signed,
471
       ppc64_elf_unhandled_reloc),
472
473
  /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
474
     the symbol.  */
475
  HOW (R_PPC64_PLT16_HA, 2, 16, 0xffff, 16, false, signed,
476
       ppc64_elf_unhandled_reloc),
477
478
  /* 16-bit section relative relocation.  */
479
  HOW (R_PPC64_SECTOFF, 2, 16, 0xffff, 0, false, signed,
480
       ppc64_elf_sectoff_reloc),
481
482
  /* Like R_PPC64_SECTOFF, but no overflow warning.  */
483
  HOW (R_PPC64_SECTOFF_LO, 2, 16, 0xffff, 0, false, dont,
484
       ppc64_elf_sectoff_reloc),
485
486
  /* 16-bit upper half section relative relocation.  */
487
  HOW (R_PPC64_SECTOFF_HI, 2, 16, 0xffff, 16, false, signed,
488
       ppc64_elf_sectoff_reloc),
489
490
  /* 16-bit upper half adjusted section relative relocation.  */
491
  HOW (R_PPC64_SECTOFF_HA, 2, 16, 0xffff, 16, false, signed,
492
       ppc64_elf_sectoff_ha_reloc),
493
494
  /* Like R_PPC64_REL24 without touching the two least significant bits.  */
495
  HOW (R_PPC64_REL30, 4, 30, 0xfffffffc, 2, true, dont,
496
       bfd_elf_generic_reloc),
497
498
  /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
499
500
  /* A standard 64-bit relocation.  */
501
  HOW (R_PPC64_ADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
502
       bfd_elf_generic_reloc),
503
504
  /* The bits 32-47 of an address.  */
505
  HOW (R_PPC64_ADDR16_HIGHER, 2, 16, 0xffff, 32, false, dont,
506
       bfd_elf_generic_reloc),
507
508
  /* The bits 32-47 of an address, plus 1 if the contents of the low
509
     16 bits, treated as a signed number, is negative.  */
510
  HOW (R_PPC64_ADDR16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
511
       ppc64_elf_ha_reloc),
512
513
  /* The bits 48-63 of an address.  */
514
  HOW (R_PPC64_ADDR16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
515
       bfd_elf_generic_reloc),
516
517
  /* The bits 48-63 of an address, plus 1 if the contents of the low
518
     16 bits, treated as a signed number, is negative.  */
519
  HOW (R_PPC64_ADDR16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
520
       ppc64_elf_ha_reloc),
521
522
  /* Like ADDR64, but may be unaligned.  */
523
  HOW (R_PPC64_UADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
524
       bfd_elf_generic_reloc),
525
526
  /* 64-bit relative relocation.  */
527
  HOW (R_PPC64_REL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
528
       bfd_elf_generic_reloc),
529
530
  /* 64-bit relocation to the symbol's procedure linkage table.  */
531
  HOW (R_PPC64_PLT64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
532
       ppc64_elf_unhandled_reloc),
533
534
  /* 64-bit PC relative relocation to the symbol's procedure linkage
535
     table.  */
536
  /* FIXME: R_PPC64_PLTREL64 not supported.  */
537
  HOW (R_PPC64_PLTREL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
538
       ppc64_elf_unhandled_reloc),
539
540
  /* 16 bit TOC-relative relocation.  */
541
  /* R_PPC64_TOC16    47     half16*  S + A - .TOC.  */
542
  HOW (R_PPC64_TOC16, 2, 16, 0xffff, 0, false, signed,
543
       ppc64_elf_toc_reloc),
544
545
  /* 16 bit TOC-relative relocation without overflow.  */
546
  /* R_PPC64_TOC16_LO   48     half16  #lo (S + A - .TOC.)  */
547
  HOW (R_PPC64_TOC16_LO, 2, 16, 0xffff, 0, false, dont,
548
       ppc64_elf_toc_reloc),
549
550
  /* 16 bit TOC-relative relocation, high 16 bits.  */
551
  /* R_PPC64_TOC16_HI   49     half16  #hi (S + A - .TOC.)  */
552
  HOW (R_PPC64_TOC16_HI, 2, 16, 0xffff, 16, false, signed,
553
       ppc64_elf_toc_reloc),
554
555
  /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
556
     contents of the low 16 bits, treated as a signed number, is
557
     negative.  */
558
  /* R_PPC64_TOC16_HA   50     half16  #ha (S + A - .TOC.)  */
559
  HOW (R_PPC64_TOC16_HA, 2, 16, 0xffff, 16, false, signed,
560
       ppc64_elf_toc_ha_reloc),
561
562
  /* 64-bit relocation; insert value of TOC base (.TOC.).  */
563
  /* R_PPC64_TOC      51     doubleword64  .TOC.  */
564
  HOW (R_PPC64_TOC, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
565
       ppc64_elf_toc64_reloc),
566
567
  /* Like R_PPC64_GOT16, but also informs the link editor that the
568
     value to relocate may (!) refer to a PLT entry which the link
569
     editor (a) may replace with the symbol value.  If the link editor
570
     is unable to fully resolve the symbol, it may (b) create a PLT
571
     entry and store the address to the new PLT entry in the GOT.
572
     This permits lazy resolution of function symbols at run time.
573
     The link editor may also skip all of this and just (c) emit a
574
     R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
575
  /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
576
    HOW (R_PPC64_PLTGOT16, 2, 16, 0xffff, 0, false,signed,
577
    ppc64_elf_unhandled_reloc),
578
579
  /* Like R_PPC64_PLTGOT16, but without overflow.  */
580
  /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
581
  HOW (R_PPC64_PLTGOT16_LO, 2, 16, 0xffff, 0, false, dont,
582
       ppc64_elf_unhandled_reloc),
583
584
  /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
585
  /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
586
  HOW (R_PPC64_PLTGOT16_HI, 2, 16, 0xffff, 16, false, signed,
587
       ppc64_elf_unhandled_reloc),
588
589
  /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
590
     1 if the contents of the low 16 bits, treated as a signed number,
591
     is negative.  */
592
  /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
593
  HOW (R_PPC64_PLTGOT16_HA, 2, 16, 0xffff, 16, false, signed,
594
       ppc64_elf_unhandled_reloc),
595
596
  /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
597
  HOW (R_PPC64_ADDR16_DS, 2, 16, 0xfffc, 0, false, signed,
598
       bfd_elf_generic_reloc),
599
600
  /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
601
  HOW (R_PPC64_ADDR16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
602
       bfd_elf_generic_reloc),
603
604
  /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
605
  HOW (R_PPC64_GOT16_DS, 2, 16, 0xfffc, 0, false, signed,
606
       ppc64_elf_unhandled_reloc),
607
608
  /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
609
  HOW (R_PPC64_GOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
610
       ppc64_elf_unhandled_reloc),
611
612
  /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
613
  HOW (R_PPC64_PLT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
614
       ppc64_elf_unhandled_reloc),
615
616
  /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
617
  HOW (R_PPC64_SECTOFF_DS, 2, 16, 0xfffc, 0, false, signed,
618
       ppc64_elf_sectoff_reloc),
619
620
  /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
621
  HOW (R_PPC64_SECTOFF_LO_DS, 2, 16, 0xfffc, 0, false, dont,
622
       ppc64_elf_sectoff_reloc),
623
624
  /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
625
  HOW (R_PPC64_TOC16_DS, 2, 16, 0xfffc, 0, false, signed,
626
       ppc64_elf_toc_reloc),
627
628
  /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
629
  HOW (R_PPC64_TOC16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
630
       ppc64_elf_toc_reloc),
631
632
  /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
633
  /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
634
  HOW (R_PPC64_PLTGOT16_DS, 2, 16, 0xfffc, 0, false, signed,
635
       ppc64_elf_unhandled_reloc),
636
637
  /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
638
  /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
639
  HOW (R_PPC64_PLTGOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
640
       ppc64_elf_unhandled_reloc),
641
642
  /* Marker relocs for TLS.  */
643
  HOW (R_PPC64_TLS, 4, 32, 0, 0, false, dont,
644
       bfd_elf_generic_reloc),
645
646
  HOW (R_PPC64_TLSGD, 4, 32, 0, 0, false, dont,
647
       bfd_elf_generic_reloc),
648
649
  HOW (R_PPC64_TLSLD, 4, 32, 0, 0, false, dont,
650
       bfd_elf_generic_reloc),
651
652
  /* Marker reloc for optimizing r2 save in prologue rather than on
653
     each plt call stub.  */
654
  HOW (R_PPC64_TOCSAVE, 4, 32, 0, 0, false, dont,
655
       bfd_elf_generic_reloc),
656
657
  /* Marker relocs on inline plt call instructions.  */
658
  HOW (R_PPC64_PLTSEQ, 4, 32, 0, 0, false, dont,
659
       bfd_elf_generic_reloc),
660
661
  HOW (R_PPC64_PLTCALL, 4, 32, 0, 0, false, dont,
662
       bfd_elf_generic_reloc),
663
664
  /* Computes the load module index of the load module that contains the
665
     definition of its TLS sym.  */
666
  HOW (R_PPC64_DTPMOD64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
667
       ppc64_elf_unhandled_reloc),
668
669
  /* Computes a dtv-relative displacement, the difference between the value
670
     of sym+add and the base address of the thread-local storage block that
671
     contains the definition of sym, minus 0x8000.  */
672
  HOW (R_PPC64_DTPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
673
       ppc64_elf_unhandled_reloc),
674
675
  /* A 16 bit dtprel reloc.  */
676
  HOW (R_PPC64_DTPREL16, 2, 16, 0xffff, 0, false, signed,
677
       ppc64_elf_unhandled_reloc),
678
679
  /* Like DTPREL16, but no overflow.  */
680
  HOW (R_PPC64_DTPREL16_LO, 2, 16, 0xffff, 0, false, dont,
681
       ppc64_elf_unhandled_reloc),
682
683
  /* Like DTPREL16_LO, but next higher group of 16 bits.  */
684
  HOW (R_PPC64_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
685
       ppc64_elf_unhandled_reloc),
686
687
  /* Like DTPREL16_HI, but adjust for low 16 bits.  */
688
  HOW (R_PPC64_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
689
       ppc64_elf_unhandled_reloc),
690
691
  /* Like DTPREL16_HI, but next higher group of 16 bits.  */
692
  HOW (R_PPC64_DTPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
693
       ppc64_elf_unhandled_reloc),
694
695
  /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
696
  HOW (R_PPC64_DTPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
697
       ppc64_elf_unhandled_reloc),
698
699
  /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
700
  HOW (R_PPC64_DTPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
701
       ppc64_elf_unhandled_reloc),
702
703
  /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
704
  HOW (R_PPC64_DTPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
705
       ppc64_elf_unhandled_reloc),
706
707
  /* Like DTPREL16, but for insns with a DS field.  */
708
  HOW (R_PPC64_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
709
       ppc64_elf_unhandled_reloc),
710
711
  /* Like DTPREL16_DS, but no overflow.  */
712
  HOW (R_PPC64_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
713
       ppc64_elf_unhandled_reloc),
714
715
  /* Computes a tp-relative displacement, the difference between the value of
716
     sym+add and the value of the thread pointer (r13).  */
717
  HOW (R_PPC64_TPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
718
       ppc64_elf_unhandled_reloc),
719
720
  /* A 16 bit tprel reloc.  */
721
  HOW (R_PPC64_TPREL16, 2, 16, 0xffff, 0, false, signed,
722
       ppc64_elf_unhandled_reloc),
723
724
  /* Like TPREL16, but no overflow.  */
725
  HOW (R_PPC64_TPREL16_LO, 2, 16, 0xffff, 0, false, dont,
726
       ppc64_elf_unhandled_reloc),
727
728
  /* Like TPREL16_LO, but next higher group of 16 bits.  */
729
  HOW (R_PPC64_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
730
       ppc64_elf_unhandled_reloc),
731
732
  /* Like TPREL16_HI, but adjust for low 16 bits.  */
733
  HOW (R_PPC64_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
734
       ppc64_elf_unhandled_reloc),
735
736
  /* Like TPREL16_HI, but next higher group of 16 bits.  */
737
  HOW (R_PPC64_TPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
738
       ppc64_elf_unhandled_reloc),
739
740
  /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
741
  HOW (R_PPC64_TPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
742
       ppc64_elf_unhandled_reloc),
743
744
  /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
745
  HOW (R_PPC64_TPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
746
       ppc64_elf_unhandled_reloc),
747
748
  /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
749
  HOW (R_PPC64_TPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
750
       ppc64_elf_unhandled_reloc),
751
752
  /* Like TPREL16, but for insns with a DS field.  */
753
  HOW (R_PPC64_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
754
       ppc64_elf_unhandled_reloc),
755
756
  /* Like TPREL16_DS, but no overflow.  */
757
  HOW (R_PPC64_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
758
       ppc64_elf_unhandled_reloc),
759
760
  /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
761
     with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
762
     to the first entry relative to the TOC base (r2).  */
763
  HOW (R_PPC64_GOT_TLSGD16, 2, 16, 0xffff, 0, false, signed,
764
       ppc64_elf_unhandled_reloc),
765
766
  /* Like GOT_TLSGD16, but no overflow.  */
767
  HOW (R_PPC64_GOT_TLSGD16_LO, 2, 16, 0xffff, 0, false, dont,
768
       ppc64_elf_unhandled_reloc),
769
770
  /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
771
  HOW (R_PPC64_GOT_TLSGD16_HI, 2, 16, 0xffff, 16, false, signed,
772
       ppc64_elf_unhandled_reloc),
773
774
  /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
775
  HOW (R_PPC64_GOT_TLSGD16_HA, 2, 16, 0xffff, 16, false, signed,
776
       ppc64_elf_unhandled_reloc),
777
778
  /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
779
     with values (sym+add)@dtpmod and zero, and computes the offset to the
780
     first entry relative to the TOC base (r2).  */
781
  HOW (R_PPC64_GOT_TLSLD16, 2, 16, 0xffff, 0, false, signed,
782
       ppc64_elf_unhandled_reloc),
783
784
  /* Like GOT_TLSLD16, but no overflow.  */
785
  HOW (R_PPC64_GOT_TLSLD16_LO, 2, 16, 0xffff, 0, false, dont,
786
       ppc64_elf_unhandled_reloc),
787
788
  /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
789
  HOW (R_PPC64_GOT_TLSLD16_HI, 2, 16, 0xffff, 16, false, signed,
790
       ppc64_elf_unhandled_reloc),
791
792
  /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
793
  HOW (R_PPC64_GOT_TLSLD16_HA, 2, 16, 0xffff, 16, false, signed,
794
       ppc64_elf_unhandled_reloc),
795
796
  /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
797
     the offset to the entry relative to the TOC base (r2).  */
798
  HOW (R_PPC64_GOT_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
799
       ppc64_elf_unhandled_reloc),
800
801
  /* Like GOT_DTPREL16_DS, but no overflow.  */
802
  HOW (R_PPC64_GOT_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
803
       ppc64_elf_unhandled_reloc),
804
805
  /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
806
  HOW (R_PPC64_GOT_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
807
       ppc64_elf_unhandled_reloc),
808
809
  /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
810
  HOW (R_PPC64_GOT_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
811
       ppc64_elf_unhandled_reloc),
812
813
  /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
814
     offset to the entry relative to the TOC base (r2).  */
815
  HOW (R_PPC64_GOT_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
816
       ppc64_elf_unhandled_reloc),
817
818
  /* Like GOT_TPREL16_DS, but no overflow.  */
819
  HOW (R_PPC64_GOT_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
820
       ppc64_elf_unhandled_reloc),
821
822
  /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
823
  HOW (R_PPC64_GOT_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
824
       ppc64_elf_unhandled_reloc),
825
826
  /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
827
  HOW (R_PPC64_GOT_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
828
       ppc64_elf_unhandled_reloc),
829
830
  HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
831
       ppc64_elf_unhandled_reloc),
832
833
  HOW (R_PPC64_IRELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
834
       bfd_elf_generic_reloc),
835
836
  /* A 16 bit relative relocation.  */
837
  HOW (R_PPC64_REL16, 2, 16, 0xffff, 0, true, signed,
838
       bfd_elf_generic_reloc),
839
840
  /* A 16 bit relative relocation without overflow.  */
841
  HOW (R_PPC64_REL16_LO, 2, 16, 0xffff, 0, true, dont,
842
       bfd_elf_generic_reloc),
843
844
  /* The high order 16 bits of a relative address.  */
845
  HOW (R_PPC64_REL16_HI, 2, 16, 0xffff, 16, true, signed,
846
       bfd_elf_generic_reloc),
847
848
  /* The high order 16 bits of a relative address, plus 1 if the contents of
849
     the low 16 bits, treated as a signed number, is negative.  */
850
  HOW (R_PPC64_REL16_HA, 2, 16, 0xffff, 16, true, signed,
851
       ppc64_elf_ha_reloc),
852
853
  HOW (R_PPC64_REL16_HIGH, 2, 16, 0xffff, 16, true, dont,
854
       bfd_elf_generic_reloc),
855
856
  HOW (R_PPC64_REL16_HIGHA, 2, 16, 0xffff, 16, true, dont,
857
       ppc64_elf_ha_reloc),
858
859
  HOW (R_PPC64_REL16_HIGHER, 2, 16, 0xffff, 32, true, dont,
860
       bfd_elf_generic_reloc),
861
862
  HOW (R_PPC64_REL16_HIGHERA, 2, 16, 0xffff, 32, true, dont,
863
       ppc64_elf_ha_reloc),
864
865
  HOW (R_PPC64_REL16_HIGHEST, 2, 16, 0xffff, 48, true, dont,
866
       bfd_elf_generic_reloc),
867
868
  HOW (R_PPC64_REL16_HIGHESTA, 2, 16, 0xffff, 48, true, dont,
869
       ppc64_elf_ha_reloc),
870
871
  /* Like R_PPC64_REL16_HA but for split field in addpcis.  */
872
  HOW (R_PPC64_REL16DX_HA, 4, 16, 0x1fffc1, 16, true, signed,
873
       ppc64_elf_ha_reloc),
874
875
  /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
876
  HOW (R_PPC64_16DX_HA, 4, 16, 0x1fffc1, 16, false, signed,
877
       ppc64_elf_ha_reloc),
878
879
  /* Like R_PPC64_ADDR16_HI, but no overflow.  */
880
  HOW (R_PPC64_ADDR16_HIGH, 2, 16, 0xffff, 16, false, dont,
881
       bfd_elf_generic_reloc),
882
883
  /* Like R_PPC64_ADDR16_HA, but no overflow.  */
884
  HOW (R_PPC64_ADDR16_HIGHA, 2, 16, 0xffff, 16, false, dont,
885
       ppc64_elf_ha_reloc),
886
887
  /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
888
  HOW (R_PPC64_DTPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
889
       ppc64_elf_unhandled_reloc),
890
891
  /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
892
  HOW (R_PPC64_DTPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
893
       ppc64_elf_unhandled_reloc),
894
895
  /* Like R_PPC64_TPREL16_HI, but no overflow.  */
896
  HOW (R_PPC64_TPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
897
       ppc64_elf_unhandled_reloc),
898
899
  /* Like R_PPC64_TPREL16_HA, but no overflow.  */
900
  HOW (R_PPC64_TPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
901
       ppc64_elf_unhandled_reloc),
902
903
  /* Marker reloc on ELFv2 large-model function entry.  */
904
  HOW (R_PPC64_ENTRY, 4, 32, 0, 0, false, dont,
905
       bfd_elf_generic_reloc),
906
907
  /* Like ADDR64, but use local entry point of function.  */
908
  HOW (R_PPC64_ADDR64_LOCAL, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
909
       bfd_elf_generic_reloc),
910
911
  HOW (R_PPC64_PLTSEQ_NOTOC, 4, 32, 0, 0, false, dont,
912
       bfd_elf_generic_reloc),
913
914
  HOW (R_PPC64_PLTCALL_NOTOC, 4, 32, 0, 0, false, dont,
915
       bfd_elf_generic_reloc),
916
917
  HOW (R_PPC64_PCREL_OPT, 4, 32, 0, 0, false, dont,
918
       bfd_elf_generic_reloc),
919
920
  HOW (R_PPC64_D34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
921
       ppc64_elf_prefix_reloc),
922
923
  HOW (R_PPC64_D34_LO, 8, 34, 0x3ffff0000ffffULL, 0, false, dont,
924
       ppc64_elf_prefix_reloc),
925
926
  HOW (R_PPC64_D34_HI30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
927
       ppc64_elf_prefix_reloc),
928
929
  HOW (R_PPC64_D34_HA30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
930
       ppc64_elf_prefix_reloc),
931
932
  HOW (R_PPC64_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
933
       ppc64_elf_prefix_reloc),
934
935
  HOW (R_PPC64_GOT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
936
       ppc64_elf_unhandled_reloc),
937
938
  HOW (R_PPC64_PLT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
939
       ppc64_elf_unhandled_reloc),
940
941
  HOW (R_PPC64_PLT_PCREL34_NOTOC, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
942
       ppc64_elf_unhandled_reloc),
943
944
  HOW (R_PPC64_TPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
945
       ppc64_elf_unhandled_reloc),
946
947
  HOW (R_PPC64_DTPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
948
       ppc64_elf_unhandled_reloc),
949
950
  HOW (R_PPC64_GOT_TLSGD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
951
       ppc64_elf_unhandled_reloc),
952
953
  HOW (R_PPC64_GOT_TLSLD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
954
       ppc64_elf_unhandled_reloc),
955
956
  HOW (R_PPC64_GOT_TPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
957
       ppc64_elf_unhandled_reloc),
958
959
  HOW (R_PPC64_GOT_DTPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
960
       ppc64_elf_unhandled_reloc),
961
962
  HOW (R_PPC64_ADDR16_HIGHER34, 2, 16, 0xffff, 34, false, dont,
963
       bfd_elf_generic_reloc),
964
965
  HOW (R_PPC64_ADDR16_HIGHERA34, 2, 16, 0xffff, 34, false, dont,
966
       ppc64_elf_ha_reloc),
967
968
  HOW (R_PPC64_ADDR16_HIGHEST34, 2, 16, 0xffff, 50, false, dont,
969
       bfd_elf_generic_reloc),
970
971
  HOW (R_PPC64_ADDR16_HIGHESTA34, 2, 16, 0xffff, 50, false, dont,
972
       ppc64_elf_ha_reloc),
973
974
  HOW (R_PPC64_REL16_HIGHER34, 2, 16, 0xffff, 34, true, dont,
975
       bfd_elf_generic_reloc),
976
977
  HOW (R_PPC64_REL16_HIGHERA34, 2, 16, 0xffff, 34, true, dont,
978
       ppc64_elf_ha_reloc),
979
980
  HOW (R_PPC64_REL16_HIGHEST34, 2, 16, 0xffff, 50, true, dont,
981
       bfd_elf_generic_reloc),
982
983
  HOW (R_PPC64_REL16_HIGHESTA34, 2, 16, 0xffff, 50, true, dont,
984
       ppc64_elf_ha_reloc),
985
986
  HOW (R_PPC64_D28, 8, 28, 0xfff0000ffffULL, 0, false, signed,
987
       ppc64_elf_prefix_reloc),
988
989
  HOW (R_PPC64_PCREL28, 8, 28, 0xfff0000ffffULL, 0, true, signed,
990
       ppc64_elf_prefix_reloc),
991
992
  /* GNU extension to record C++ vtable hierarchy.  */
993
  HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
994
       NULL),
995
996
  /* GNU extension to record C++ vtable member usage.  */
997
  HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
998
       NULL),
999
};
1000
1001

1002
/* Initialize the ppc64_elf_howto_table, so that linear accesses can
1003
   be done.  */
1004
1005
static void
1006
ppc_howto_init (void)
1007
3
{
1008
3
  unsigned int i, type;
1009
1010
489
  for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1011
486
    {
1012
486
      type = ppc64_elf_howto_raw[i].type;
1013
486
      BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
1014
486
      ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1015
486
    }
1016
3
}
1017
1018
static reloc_howto_type *
1019
ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
1020
0
{
1021
0
  enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1022
1023
0
  if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1024
    /* Initialize howto table if needed.  */
1025
0
    ppc_howto_init ();
1026
1027
0
  switch (code)
1028
0
    {
1029
0
    default:
1030
      /* xgettext:c-format */
1031
0
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1032
0
        (int) code);
1033
0
      bfd_set_error (bfd_error_bad_value);
1034
0
      return NULL;
1035
1036
0
    case BFD_RELOC_NONE:      r = R_PPC64_NONE;
1037
0
      break;
1038
0
    case BFD_RELOC_32:        r = R_PPC64_ADDR32;
1039
0
      break;
1040
0
    case BFD_RELOC_PPC_BA26:      r = R_PPC64_ADDR24;
1041
0
      break;
1042
0
    case BFD_RELOC_16:        r = R_PPC64_ADDR16;
1043
0
      break;
1044
0
    case BFD_RELOC_LO16:      r = R_PPC64_ADDR16_LO;
1045
0
      break;
1046
0
    case BFD_RELOC_HI16:      r = R_PPC64_ADDR16_HI;
1047
0
      break;
1048
0
    case BFD_RELOC_PPC64_ADDR16_HIGH:   r = R_PPC64_ADDR16_HIGH;
1049
0
      break;
1050
0
    case BFD_RELOC_HI16_S:      r = R_PPC64_ADDR16_HA;
1051
0
      break;
1052
0
    case BFD_RELOC_PPC64_ADDR16_HIGHA:    r = R_PPC64_ADDR16_HIGHA;
1053
0
      break;
1054
0
    case BFD_RELOC_PPC_BA16:      r = R_PPC64_ADDR14;
1055
0
      break;
1056
0
    case BFD_RELOC_PPC_BA16_BRTAKEN:    r = R_PPC64_ADDR14_BRTAKEN;
1057
0
      break;
1058
0
    case BFD_RELOC_PPC_BA16_BRNTAKEN:   r = R_PPC64_ADDR14_BRNTAKEN;
1059
0
      break;
1060
0
    case BFD_RELOC_PPC_B26:     r = R_PPC64_REL24;
1061
0
      break;
1062
0
    case BFD_RELOC_PPC64_REL24_NOTOC:   r = R_PPC64_REL24_NOTOC;
1063
0
      break;
1064
0
    case BFD_RELOC_PPC64_REL24_P9NOTOC:   r = R_PPC64_REL24_P9NOTOC;
1065
0
      break;
1066
0
    case BFD_RELOC_PPC_B16:     r = R_PPC64_REL14;
1067
0
      break;
1068
0
    case BFD_RELOC_PPC_B16_BRTAKEN:   r = R_PPC64_REL14_BRTAKEN;
1069
0
      break;
1070
0
    case BFD_RELOC_PPC_B16_BRNTAKEN:    r = R_PPC64_REL14_BRNTAKEN;
1071
0
      break;
1072
0
    case BFD_RELOC_16_GOTOFF:     r = R_PPC64_GOT16;
1073
0
      break;
1074
0
    case BFD_RELOC_LO16_GOTOFF:     r = R_PPC64_GOT16_LO;
1075
0
      break;
1076
0
    case BFD_RELOC_HI16_GOTOFF:     r = R_PPC64_GOT16_HI;
1077
0
      break;
1078
0
    case BFD_RELOC_HI16_S_GOTOFF:   r = R_PPC64_GOT16_HA;
1079
0
      break;
1080
0
    case BFD_RELOC_COPY:      r = R_PPC64_COPY;
1081
0
      break;
1082
0
    case BFD_RELOC_GLOB_DAT:      r = R_PPC64_GLOB_DAT;
1083
0
      break;
1084
0
    case BFD_RELOC_32_PCREL:      r = R_PPC64_REL32;
1085
0
      break;
1086
0
    case BFD_RELOC_32_PLTOFF:     r = R_PPC64_PLT32;
1087
0
      break;
1088
0
    case BFD_RELOC_32_PLT_PCREL:    r = R_PPC64_PLTREL32;
1089
0
      break;
1090
0
    case BFD_RELOC_LO16_PLTOFF:     r = R_PPC64_PLT16_LO;
1091
0
      break;
1092
0
    case BFD_RELOC_HI16_PLTOFF:     r = R_PPC64_PLT16_HI;
1093
0
      break;
1094
0
    case BFD_RELOC_HI16_S_PLTOFF:   r = R_PPC64_PLT16_HA;
1095
0
      break;
1096
0
    case BFD_RELOC_16_BASEREL:      r = R_PPC64_SECTOFF;
1097
0
      break;
1098
0
    case BFD_RELOC_LO16_BASEREL:    r = R_PPC64_SECTOFF_LO;
1099
0
      break;
1100
0
    case BFD_RELOC_HI16_BASEREL:    r = R_PPC64_SECTOFF_HI;
1101
0
      break;
1102
0
    case BFD_RELOC_HI16_S_BASEREL:    r = R_PPC64_SECTOFF_HA;
1103
0
      break;
1104
0
    case BFD_RELOC_CTOR:      r = R_PPC64_ADDR64;
1105
0
      break;
1106
0
    case BFD_RELOC_64:        r = R_PPC64_ADDR64;
1107
0
      break;
1108
0
    case BFD_RELOC_PPC64_HIGHER:    r = R_PPC64_ADDR16_HIGHER;
1109
0
      break;
1110
0
    case BFD_RELOC_PPC64_HIGHER_S:    r = R_PPC64_ADDR16_HIGHERA;
1111
0
      break;
1112
0
    case BFD_RELOC_PPC64_HIGHEST:   r = R_PPC64_ADDR16_HIGHEST;
1113
0
      break;
1114
0
    case BFD_RELOC_PPC64_HIGHEST_S:   r = R_PPC64_ADDR16_HIGHESTA;
1115
0
      break;
1116
0
    case BFD_RELOC_64_PCREL:      r = R_PPC64_REL64;
1117
0
      break;
1118
0
    case BFD_RELOC_64_PLTOFF:     r = R_PPC64_PLT64;
1119
0
      break;
1120
0
    case BFD_RELOC_64_PLT_PCREL:    r = R_PPC64_PLTREL64;
1121
0
      break;
1122
0
    case BFD_RELOC_PPC_TOC16:     r = R_PPC64_TOC16;
1123
0
      break;
1124
0
    case BFD_RELOC_PPC64_TOC16_LO:    r = R_PPC64_TOC16_LO;
1125
0
      break;
1126
0
    case BFD_RELOC_PPC64_TOC16_HI:    r = R_PPC64_TOC16_HI;
1127
0
      break;
1128
0
    case BFD_RELOC_PPC64_TOC16_HA:    r = R_PPC64_TOC16_HA;
1129
0
      break;
1130
0
    case BFD_RELOC_PPC64_TOC:     r = R_PPC64_TOC;
1131
0
      break;
1132
0
    case BFD_RELOC_PPC64_PLTGOT16:    r = R_PPC64_PLTGOT16;
1133
0
      break;
1134
0
    case BFD_RELOC_PPC64_PLTGOT16_LO:   r = R_PPC64_PLTGOT16_LO;
1135
0
      break;
1136
0
    case BFD_RELOC_PPC64_PLTGOT16_HI:   r = R_PPC64_PLTGOT16_HI;
1137
0
      break;
1138
0
    case BFD_RELOC_PPC64_PLTGOT16_HA:   r = R_PPC64_PLTGOT16_HA;
1139
0
      break;
1140
0
    case BFD_RELOC_PPC64_ADDR16_DS:   r = R_PPC64_ADDR16_DS;
1141
0
      break;
1142
0
    case BFD_RELOC_PPC64_ADDR16_LO_DS:    r = R_PPC64_ADDR16_LO_DS;
1143
0
      break;
1144
0
    case BFD_RELOC_PPC64_GOT16_DS:    r = R_PPC64_GOT16_DS;
1145
0
      break;
1146
0
    case BFD_RELOC_PPC64_GOT16_LO_DS:   r = R_PPC64_GOT16_LO_DS;
1147
0
      break;
1148
0
    case BFD_RELOC_PPC64_PLT16_LO_DS:   r = R_PPC64_PLT16_LO_DS;
1149
0
      break;
1150
0
    case BFD_RELOC_PPC64_SECTOFF_DS:    r = R_PPC64_SECTOFF_DS;
1151
0
      break;
1152
0
    case BFD_RELOC_PPC64_SECTOFF_LO_DS:   r = R_PPC64_SECTOFF_LO_DS;
1153
0
      break;
1154
0
    case BFD_RELOC_PPC64_TOC16_DS:    r = R_PPC64_TOC16_DS;
1155
0
      break;
1156
0
    case BFD_RELOC_PPC64_TOC16_LO_DS:   r = R_PPC64_TOC16_LO_DS;
1157
0
      break;
1158
0
    case BFD_RELOC_PPC64_PLTGOT16_DS:   r = R_PPC64_PLTGOT16_DS;
1159
0
      break;
1160
0
    case BFD_RELOC_PPC64_PLTGOT16_LO_DS:  r = R_PPC64_PLTGOT16_LO_DS;
1161
0
      break;
1162
0
    case BFD_RELOC_PPC64_TLS_PCREL:
1163
0
    case BFD_RELOC_PPC_TLS:     r = R_PPC64_TLS;
1164
0
      break;
1165
0
    case BFD_RELOC_PPC_TLSGD:     r = R_PPC64_TLSGD;
1166
0
      break;
1167
0
    case BFD_RELOC_PPC_TLSLD:     r = R_PPC64_TLSLD;
1168
0
      break;
1169
0
    case BFD_RELOC_PPC_DTPMOD:      r = R_PPC64_DTPMOD64;
1170
0
      break;
1171
0
    case BFD_RELOC_PPC_TPREL16:     r = R_PPC64_TPREL16;
1172
0
      break;
1173
0
    case BFD_RELOC_PPC_TPREL16_LO:    r = R_PPC64_TPREL16_LO;
1174
0
      break;
1175
0
    case BFD_RELOC_PPC_TPREL16_HI:    r = R_PPC64_TPREL16_HI;
1176
0
      break;
1177
0
    case BFD_RELOC_PPC64_TPREL16_HIGH:    r = R_PPC64_TPREL16_HIGH;
1178
0
      break;
1179
0
    case BFD_RELOC_PPC_TPREL16_HA:    r = R_PPC64_TPREL16_HA;
1180
0
      break;
1181
0
    case BFD_RELOC_PPC64_TPREL16_HIGHA:   r = R_PPC64_TPREL16_HIGHA;
1182
0
      break;
1183
0
    case BFD_RELOC_PPC_TPREL:     r = R_PPC64_TPREL64;
1184
0
      break;
1185
0
    case BFD_RELOC_PPC_DTPREL16:    r = R_PPC64_DTPREL16;
1186
0
      break;
1187
0
    case BFD_RELOC_PPC_DTPREL16_LO:   r = R_PPC64_DTPREL16_LO;
1188
0
      break;
1189
0
    case BFD_RELOC_PPC_DTPREL16_HI:   r = R_PPC64_DTPREL16_HI;
1190
0
      break;
1191
0
    case BFD_RELOC_PPC64_DTPREL16_HIGH:   r = R_PPC64_DTPREL16_HIGH;
1192
0
      break;
1193
0
    case BFD_RELOC_PPC_DTPREL16_HA:   r = R_PPC64_DTPREL16_HA;
1194
0
      break;
1195
0
    case BFD_RELOC_PPC64_DTPREL16_HIGHA:  r = R_PPC64_DTPREL16_HIGHA;
1196
0
      break;
1197
0
    case BFD_RELOC_PPC_DTPREL:      r = R_PPC64_DTPREL64;
1198
0
      break;
1199
0
    case BFD_RELOC_PPC_GOT_TLSGD16:   r = R_PPC64_GOT_TLSGD16;
1200
0
      break;
1201
0
    case BFD_RELOC_PPC_GOT_TLSGD16_LO:    r = R_PPC64_GOT_TLSGD16_LO;
1202
0
      break;
1203
0
    case BFD_RELOC_PPC_GOT_TLSGD16_HI:    r = R_PPC64_GOT_TLSGD16_HI;
1204
0
      break;
1205
0
    case BFD_RELOC_PPC_GOT_TLSGD16_HA:    r = R_PPC64_GOT_TLSGD16_HA;
1206
0
      break;
1207
0
    case BFD_RELOC_PPC_GOT_TLSLD16:   r = R_PPC64_GOT_TLSLD16;
1208
0
      break;
1209
0
    case BFD_RELOC_PPC_GOT_TLSLD16_LO:    r = R_PPC64_GOT_TLSLD16_LO;
1210
0
      break;
1211
0
    case BFD_RELOC_PPC_GOT_TLSLD16_HI:    r = R_PPC64_GOT_TLSLD16_HI;
1212
0
      break;
1213
0
    case BFD_RELOC_PPC_GOT_TLSLD16_HA:    r = R_PPC64_GOT_TLSLD16_HA;
1214
0
      break;
1215
0
    case BFD_RELOC_PPC_GOT_TPREL16:   r = R_PPC64_GOT_TPREL16_DS;
1216
0
      break;
1217
0
    case BFD_RELOC_PPC_GOT_TPREL16_LO:    r = R_PPC64_GOT_TPREL16_LO_DS;
1218
0
      break;
1219
0
    case BFD_RELOC_PPC_GOT_TPREL16_HI:    r = R_PPC64_GOT_TPREL16_HI;
1220
0
      break;
1221
0
    case BFD_RELOC_PPC_GOT_TPREL16_HA:    r = R_PPC64_GOT_TPREL16_HA;
1222
0
      break;
1223
0
    case BFD_RELOC_PPC_GOT_DTPREL16:    r = R_PPC64_GOT_DTPREL16_DS;
1224
0
      break;
1225
0
    case BFD_RELOC_PPC_GOT_DTPREL16_LO:   r = R_PPC64_GOT_DTPREL16_LO_DS;
1226
0
      break;
1227
0
    case BFD_RELOC_PPC_GOT_DTPREL16_HI:   r = R_PPC64_GOT_DTPREL16_HI;
1228
0
      break;
1229
0
    case BFD_RELOC_PPC_GOT_DTPREL16_HA:   r = R_PPC64_GOT_DTPREL16_HA;
1230
0
      break;
1231
0
    case BFD_RELOC_PPC64_TPREL16_DS:    r = R_PPC64_TPREL16_DS;
1232
0
      break;
1233
0
    case BFD_RELOC_PPC64_TPREL16_LO_DS:   r = R_PPC64_TPREL16_LO_DS;
1234
0
      break;
1235
0
    case BFD_RELOC_PPC64_TPREL16_HIGHER:  r = R_PPC64_TPREL16_HIGHER;
1236
0
      break;
1237
0
    case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1238
0
      break;
1239
0
    case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1240
0
      break;
1241
0
    case BFD_RELOC_PPC64_TPREL16_HIGHESTA:  r = R_PPC64_TPREL16_HIGHESTA;
1242
0
      break;
1243
0
    case BFD_RELOC_PPC64_DTPREL16_DS:   r = R_PPC64_DTPREL16_DS;
1244
0
      break;
1245
0
    case BFD_RELOC_PPC64_DTPREL16_LO_DS:  r = R_PPC64_DTPREL16_LO_DS;
1246
0
      break;
1247
0
    case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1248
0
      break;
1249
0
    case BFD_RELOC_PPC64_DTPREL16_HIGHERA:  r = R_PPC64_DTPREL16_HIGHERA;
1250
0
      break;
1251
0
    case BFD_RELOC_PPC64_DTPREL16_HIGHEST:  r = R_PPC64_DTPREL16_HIGHEST;
1252
0
      break;
1253
0
    case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1254
0
      break;
1255
0
    case BFD_RELOC_16_PCREL:      r = R_PPC64_REL16;
1256
0
      break;
1257
0
    case BFD_RELOC_LO16_PCREL:      r = R_PPC64_REL16_LO;
1258
0
      break;
1259
0
    case BFD_RELOC_HI16_PCREL:      r = R_PPC64_REL16_HI;
1260
0
      break;
1261
0
    case BFD_RELOC_HI16_S_PCREL:    r = R_PPC64_REL16_HA;
1262
0
      break;
1263
0
    case BFD_RELOC_PPC64_REL16_HIGH:    r = R_PPC64_REL16_HIGH;
1264
0
      break;
1265
0
    case BFD_RELOC_PPC64_REL16_HIGHA:   r = R_PPC64_REL16_HIGHA;
1266
0
      break;
1267
0
    case BFD_RELOC_PPC64_REL16_HIGHER:    r = R_PPC64_REL16_HIGHER;
1268
0
      break;
1269
0
    case BFD_RELOC_PPC64_REL16_HIGHERA:   r = R_PPC64_REL16_HIGHERA;
1270
0
      break;
1271
0
    case BFD_RELOC_PPC64_REL16_HIGHEST:   r = R_PPC64_REL16_HIGHEST;
1272
0
      break;
1273
0
    case BFD_RELOC_PPC64_REL16_HIGHESTA:  r = R_PPC64_REL16_HIGHESTA;
1274
0
      break;
1275
0
    case BFD_RELOC_PPC_16DX_HA:     r = R_PPC64_16DX_HA;
1276
0
      break;
1277
0
    case BFD_RELOC_PPC_REL16DX_HA:    r = R_PPC64_REL16DX_HA;
1278
0
      break;
1279
0
    case BFD_RELOC_PPC64_ENTRY:     r = R_PPC64_ENTRY;
1280
0
      break;
1281
0
    case BFD_RELOC_PPC64_ADDR64_LOCAL:    r = R_PPC64_ADDR64_LOCAL;
1282
0
      break;
1283
0
    case BFD_RELOC_PPC64_D34:     r = R_PPC64_D34;
1284
0
      break;
1285
0
    case BFD_RELOC_PPC64_D34_LO:    r = R_PPC64_D34_LO;
1286
0
      break;
1287
0
    case BFD_RELOC_PPC64_D34_HI30:    r = R_PPC64_D34_HI30;
1288
0
      break;
1289
0
    case BFD_RELOC_PPC64_D34_HA30:    r = R_PPC64_D34_HA30;
1290
0
      break;
1291
0
    case BFD_RELOC_PPC64_PCREL34:   r = R_PPC64_PCREL34;
1292
0
      break;
1293
0
    case BFD_RELOC_PPC64_GOT_PCREL34:   r = R_PPC64_GOT_PCREL34;
1294
0
      break;
1295
0
    case BFD_RELOC_PPC64_PLT_PCREL34:   r = R_PPC64_PLT_PCREL34;
1296
0
      break;
1297
0
    case BFD_RELOC_PPC64_TPREL34:   r = R_PPC64_TPREL34;
1298
0
      break;
1299
0
    case BFD_RELOC_PPC64_DTPREL34:    r = R_PPC64_DTPREL34;
1300
0
      break;
1301
0
    case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
1302
0
      break;
1303
0
    case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
1304
0
      break;
1305
0
    case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
1306
0
      break;
1307
0
    case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34:  r = R_PPC64_GOT_DTPREL_PCREL34;
1308
0
      break;
1309
0
    case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1310
0
      break;
1311
0
    case BFD_RELOC_PPC64_ADDR16_HIGHERA34:  r = R_PPC64_ADDR16_HIGHERA34;
1312
0
      break;
1313
0
    case BFD_RELOC_PPC64_ADDR16_HIGHEST34:  r = R_PPC64_ADDR16_HIGHEST34;
1314
0
      break;
1315
0
    case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1316
0
      break;
1317
0
    case BFD_RELOC_PPC64_REL16_HIGHER34:  r = R_PPC64_REL16_HIGHER34;
1318
0
      break;
1319
0
    case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1320
0
      break;
1321
0
    case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1322
0
      break;
1323
0
    case BFD_RELOC_PPC64_REL16_HIGHESTA34:  r = R_PPC64_REL16_HIGHESTA34;
1324
0
      break;
1325
0
    case BFD_RELOC_PPC64_D28:     r = R_PPC64_D28;
1326
0
      break;
1327
0
    case BFD_RELOC_PPC64_PCREL28:   r = R_PPC64_PCREL28;
1328
0
      break;
1329
0
    case BFD_RELOC_VTABLE_INHERIT:    r = R_PPC64_GNU_VTINHERIT;
1330
0
      break;
1331
0
    case BFD_RELOC_VTABLE_ENTRY:    r = R_PPC64_GNU_VTENTRY;
1332
0
      break;
1333
0
    }
1334
1335
0
  return ppc64_elf_howto_table[r];
1336
0
};
1337
1338
static reloc_howto_type *
1339
ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
1340
0
{
1341
0
  unsigned int i;
1342
0
  static char *compat_map[][2] = {
1343
0
    { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1344
0
    { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1345
0
    { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1346
0
    { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1347
0
  };
1348
1349
0
  for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1350
0
    if (ppc64_elf_howto_raw[i].name != NULL
1351
0
  && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1352
0
      return &ppc64_elf_howto_raw[i];
1353
1354
  /* Handle old names of relocations in case they were used by
1355
     .reloc directives.
1356
     FIXME: Remove this soon.  Mapping the reloc names is very likely
1357
     completely unnecessary.  */
1358
0
  for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1359
0
    if (strcasecmp (compat_map[i][0], r_name) == 0)
1360
0
      {
1361
0
  _bfd_error_handler (_("warning: %s should be used rather than %s"),
1362
0
          compat_map[i][1], compat_map[i][0]);
1363
0
  return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1364
0
      }
1365
1366
0
  return NULL;
1367
0
}
1368
1369
/* Set the howto pointer for a PowerPC ELF reloc.  */
1370
1371
static bool
1372
ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1373
       Elf_Internal_Rela *dst)
1374
3.02k
{
1375
3.02k
  unsigned int type;
1376
1377
  /* Initialize howto table if needed.  */
1378
3.02k
  if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1379
3
    ppc_howto_init ();
1380
1381
3.02k
  type = ELF64_R_TYPE (dst->r_info);
1382
3.02k
  if (type >= ARRAY_SIZE (ppc64_elf_howto_table)
1383
2.91k
      || ppc64_elf_howto_table[type] == NULL)
1384
131
    {
1385
      /* xgettext:c-format */
1386
131
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1387
131
        abfd, type);
1388
131
      bfd_set_error (bfd_error_bad_value);
1389
131
      return false;
1390
131
    }
1391
2.88k
  cache_ptr->howto = ppc64_elf_howto_table[type];
1392
2.88k
  return true;
1393
3.02k
}
1394
1395
/* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
1396
1397
static bfd_reloc_status_type
1398
ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1399
        void *data, asection *input_section,
1400
        bfd *output_bfd, char **error_message)
1401
17
{
1402
17
  enum elf_ppc64_reloc_type r_type;
1403
17
  long insn;
1404
17
  bfd_size_type octets;
1405
17
  bfd_vma value;
1406
1407
  /* If this is a relocatable link (output_bfd test tells us), just
1408
     call the generic function.  Any adjustment will be done at final
1409
     link time.  */
1410
17
  if (output_bfd != NULL)
1411
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1412
0
          input_section, output_bfd, error_message);
1413
1414
  /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1415
     We won't actually be using the low bits, so trashing them
1416
     doesn't matter.  */
1417
17
  r_type = reloc_entry->howto->type;
1418
17
  if (r_type == R_PPC64_ADDR16_HIGHERA34
1419
1
      || r_type == R_PPC64_ADDR16_HIGHESTA34
1420
1
      || r_type == R_PPC64_REL16_HIGHERA34
1421
1
      || r_type == R_PPC64_REL16_HIGHESTA34)
1422
16
    reloc_entry->addend += 1ULL << 33;
1423
1
  else
1424
1
    reloc_entry->addend += 1U << 15;
1425
17
  if (r_type != R_PPC64_REL16DX_HA)
1426
17
    return bfd_reloc_continue;
1427
1428
0
  value = 0;
1429
0
  if (!bfd_is_com_section (symbol->section))
1430
0
    value = symbol->value;
1431
0
  value += (reloc_entry->addend
1432
0
      + symbol->section->output_offset
1433
0
      + symbol->section->output_section->vma);
1434
0
  value -= (reloc_entry->address
1435
0
      + input_section->output_offset
1436
0
      + input_section->output_section->vma);
1437
0
  value = (bfd_signed_vma) value >> 16;
1438
1439
0
  octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1440
0
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1441
0
          input_section, octets))
1442
0
    return bfd_reloc_outofrange;
1443
1444
0
  insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1445
0
  insn &= ~0x1fffc1;
1446
0
  insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1447
0
  bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1448
0
  if (value + 0x8000 > 0xffff)
1449
0
    return bfd_reloc_overflow;
1450
0
  return bfd_reloc_ok;
1451
0
}
1452
1453
static bfd_reloc_status_type
1454
ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1455
      void *data, asection *input_section,
1456
      bfd *output_bfd, char **error_message)
1457
111
{
1458
111
  if (output_bfd != NULL)
1459
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1460
0
          input_section, output_bfd, error_message);
1461
1462
111
  if (symbol->section->owner == NULL
1463
61
      || !is_ppc64_elf (symbol->section->owner))
1464
50
    return bfd_reloc_continue;
1465
1466
61
  if (strcmp (symbol->section->name, ".opd") == 0
1467
0
      && (symbol->section->owner->flags & DYNAMIC) == 0)
1468
0
    {
1469
0
      bfd_vma dest = opd_entry_value (symbol->section,
1470
0
              symbol->value + reloc_entry->addend,
1471
0
              NULL, NULL, false);
1472
0
      if (dest != (bfd_vma) -1)
1473
0
  reloc_entry->addend = dest - (symbol->value
1474
0
              + symbol->section->output_section->vma
1475
0
              + symbol->section->output_offset);
1476
0
    }
1477
61
  else
1478
61
    {
1479
61
      elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1480
1481
61
      if (symbol->section->owner != abfd
1482
0
    && abiversion (symbol->section->owner) >= 2)
1483
0
  {
1484
0
    unsigned int i;
1485
1486
0
    for (i = 0; i < symbol->section->owner->symcount; ++i)
1487
0
      {
1488
0
        asymbol *symdef = symbol->section->owner->outsymbols[i];
1489
1490
0
        if (strcmp (symdef->name, symbol->name) == 0)
1491
0
    {
1492
0
      elfsym = (elf_symbol_type *) symdef;
1493
0
      break;
1494
0
    }
1495
0
      }
1496
0
  }
1497
61
      reloc_entry->addend
1498
61
  += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1499
61
    }
1500
61
  return bfd_reloc_continue;
1501
111
}
1502
1503
static bfd_reloc_status_type
1504
ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1505
       void *data, asection *input_section,
1506
       bfd *output_bfd, char **error_message)
1507
37
{
1508
37
  long insn;
1509
37
  enum elf_ppc64_reloc_type r_type;
1510
37
  bfd_size_type octets;
1511
  /* Assume 'at' branch hints.  */
1512
37
  bool is_isa_v2 = true;
1513
1514
  /* If this is a relocatable link (output_bfd test tells us), just
1515
     call the generic function.  Any adjustment will be done at final
1516
     link time.  */
1517
37
  if (output_bfd != NULL)
1518
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1519
0
          input_section, output_bfd, error_message);
1520
1521
37
  octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1522
37
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1523
37
          input_section, octets))
1524
1
    return bfd_reloc_outofrange;
1525
1526
36
  insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1527
36
  insn &= ~(0x01 << 21);
1528
36
  r_type = reloc_entry->howto->type;
1529
36
  if (r_type == R_PPC64_ADDR14_BRTAKEN
1530
21
      || r_type == R_PPC64_REL14_BRTAKEN)
1531
30
    insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
1532
1533
36
  if (is_isa_v2)
1534
36
    {
1535
      /* Set 'a' bit.  This is 0b00010 in BO field for branch
1536
   on CR(BI) insns (BO == 001at or 011at), and 0b01000
1537
   for branch on CTR insns (BO == 1a00t or 1a01t).  */
1538
36
      if ((insn & (0x14 << 21)) == (0x04 << 21))
1539
13
  insn |= 0x02 << 21;
1540
23
      else if ((insn & (0x14 << 21)) == (0x10 << 21))
1541
4
  insn |= 0x08 << 21;
1542
19
      else
1543
19
  goto out;
1544
36
    }
1545
0
  else
1546
0
    {
1547
0
      bfd_vma target = 0;
1548
0
      bfd_vma from;
1549
1550
0
      if (!bfd_is_com_section (symbol->section))
1551
0
  target = symbol->value;
1552
0
      target += symbol->section->output_section->vma;
1553
0
      target += symbol->section->output_offset;
1554
0
      target += reloc_entry->addend;
1555
1556
0
      from = (reloc_entry->address
1557
0
        + input_section->output_offset
1558
0
        + input_section->output_section->vma);
1559
1560
      /* Invert 'y' bit if not the default.  */
1561
0
      if ((bfd_signed_vma) (target - from) < 0)
1562
0
  insn ^= 0x01 << 21;
1563
0
    }
1564
17
  bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1565
36
 out:
1566
36
  return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1567
36
         input_section, output_bfd, error_message);
1568
17
}
1569
1570
static bfd_reloc_status_type
1571
ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1572
       void *data, asection *input_section,
1573
       bfd *output_bfd, char **error_message)
1574
11
{
1575
  /* If this is a relocatable link (output_bfd test tells us), just
1576
     call the generic function.  Any adjustment will be done at final
1577
     link time.  */
1578
11
  if (output_bfd != NULL)
1579
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1580
0
          input_section, output_bfd, error_message);
1581
1582
  /* Subtract the symbol section base address.  */
1583
11
  reloc_entry->addend -= symbol->section->output_section->vma;
1584
11
  return bfd_reloc_continue;
1585
11
}
1586
1587
static bfd_reloc_status_type
1588
ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1589
          void *data, asection *input_section,
1590
          bfd *output_bfd, char **error_message)
1591
18
{
1592
  /* If this is a relocatable link (output_bfd test tells us), just
1593
     call the generic function.  Any adjustment will be done at final
1594
     link time.  */
1595
18
  if (output_bfd != NULL)
1596
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1597
0
          input_section, output_bfd, error_message);
1598
1599
  /* Subtract the symbol section base address.  */
1600
18
  reloc_entry->addend -= symbol->section->output_section->vma;
1601
1602
  /* Adjust the addend for sign extension of the low 16 bits.  */
1603
18
  reloc_entry->addend += 0x8000;
1604
18
  return bfd_reloc_continue;
1605
18
}
1606
1607
static bfd_reloc_status_type
1608
ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1609
         void *data, asection *input_section,
1610
         bfd *output_bfd, char **error_message)
1611
24
{
1612
24
  bfd_vma TOCstart;
1613
1614
  /* If this is a relocatable link (output_bfd test tells us), just
1615
     call the generic function.  Any adjustment will be done at final
1616
     link time.  */
1617
24
  if (output_bfd != NULL)
1618
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1619
0
          input_section, output_bfd, error_message);
1620
1621
24
  TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1622
24
  if (TOCstart == 0)
1623
23
    TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1624
1625
  /* Subtract the TOC base address.  */
1626
24
  reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1627
24
  return bfd_reloc_continue;
1628
24
}
1629
1630
static bfd_reloc_status_type
1631
ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1632
      void *data, asection *input_section,
1633
      bfd *output_bfd, char **error_message)
1634
1
{
1635
1
  bfd_vma TOCstart;
1636
1637
  /* If this is a relocatable link (output_bfd test tells us), just
1638
     call the generic function.  Any adjustment will be done at final
1639
     link time.  */
1640
1
  if (output_bfd != NULL)
1641
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1642
0
          input_section, output_bfd, error_message);
1643
1644
1
  TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1645
1
  if (TOCstart == 0)
1646
1
    TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1647
1648
  /* Subtract the TOC base address.  */
1649
1
  reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1650
1651
  /* Adjust the addend for sign extension of the low 16 bits.  */
1652
1
  reloc_entry->addend += 0x8000;
1653
1
  return bfd_reloc_continue;
1654
1
}
1655
1656
static bfd_reloc_status_type
1657
ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1658
           void *data, asection *input_section,
1659
           bfd *output_bfd, char **error_message)
1660
0
{
1661
0
  bfd_vma TOCstart;
1662
0
  bfd_size_type octets;
1663
1664
  /* If this is a relocatable link (output_bfd test tells us), just
1665
     call the generic function.  Any adjustment will be done at final
1666
     link time.  */
1667
0
  if (output_bfd != NULL)
1668
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1669
0
          input_section, output_bfd, error_message);
1670
1671
0
  octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1672
0
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1673
0
          input_section, octets))
1674
0
    return bfd_reloc_outofrange;
1675
1676
0
  TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1677
0
  if (TOCstart == 0)
1678
0
    TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1679
1680
0
  bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1681
0
  return bfd_reloc_ok;
1682
0
}
1683
1684
static bfd_reloc_status_type
1685
ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1686
      void *data, asection *input_section,
1687
      bfd *output_bfd, char **error_message)
1688
2
{
1689
2
  uint64_t insn;
1690
2
  bfd_vma targ;
1691
2
  bfd_size_type octets;
1692
1693
2
  if (output_bfd != NULL)
1694
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1695
0
          input_section, output_bfd, error_message);
1696
1697
2
  octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1698
2
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1699
2
          input_section, octets))
1700
1
    return bfd_reloc_outofrange;
1701
1702
1
  insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1703
1
  insn <<= 32;
1704
1
  insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
1705
1706
1
  targ = (symbol->section->output_section->vma
1707
1
    + symbol->section->output_offset
1708
1
    + reloc_entry->addend);
1709
1
  if (!bfd_is_com_section (symbol->section))
1710
1
    targ += symbol->value;
1711
1
  if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1712
0
    targ += 1ULL << 33;
1713
1
  if (reloc_entry->howto->pc_relative)
1714
0
    {
1715
0
      bfd_vma from = (reloc_entry->address
1716
0
          + input_section->output_offset
1717
0
          + input_section->output_section->vma);
1718
0
      targ -=from;
1719
0
    }
1720
1
  targ >>= reloc_entry->howto->rightshift;
1721
1
  insn &= ~reloc_entry->howto->dst_mask;
1722
1
  insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1723
1
  bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
1724
1
  bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
1725
1
  if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1726
1
      && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1727
1
    >= 1ULL << reloc_entry->howto->bitsize))
1728
0
    return bfd_reloc_overflow;
1729
1
  return bfd_reloc_ok;
1730
1
}
1731
1732
static bfd_reloc_status_type
1733
ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1734
         void *data, asection *input_section,
1735
         bfd *output_bfd, char **error_message)
1736
28
{
1737
  /* If this is a relocatable link (output_bfd test tells us), just
1738
     call the generic function.  Any adjustment will be done at final
1739
     link time.  */
1740
28
  if (output_bfd != NULL)
1741
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1742
0
          input_section, output_bfd, error_message);
1743
1744
28
  if (error_message != NULL)
1745
28
    *error_message = bfd_asprintf (_("generic linker can't handle %s"),
1746
28
           reloc_entry->howto->name);
1747
28
  return bfd_reloc_dangerous;
1748
28
}
1749
1750
/* Track GOT entries needed for a given symbol.  We might need more
1751
   than one got entry per symbol.  */
1752
struct got_entry
1753
{
1754
  struct got_entry *next;
1755
1756
  /* The symbol addend that we'll be placing in the GOT.  */
1757
  bfd_vma addend;
1758
1759
  /* Unlike other ELF targets, we use separate GOT entries for the same
1760
     symbol referenced from different input files.  This is to support
1761
     automatic multiple TOC/GOT sections, where the TOC base can vary
1762
     from one input file to another.  After partitioning into TOC groups
1763
     we merge entries within the group.
1764
1765
     Point to the BFD owning this GOT entry.  */
1766
  bfd *owner;
1767
1768
  /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1769
     TLS_TPREL or TLS_DTPREL for tls entries.  */
1770
  unsigned char tls_type;
1771
1772
  /* Non-zero if got.ent points to real entry.  */
1773
  unsigned char is_indirect;
1774
1775
  /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
1776
  union
1777
  {
1778
    bfd_signed_vma refcount;
1779
    bfd_vma offset;
1780
    struct got_entry *ent;
1781
  } got;
1782
};
1783
1784
/* The same for PLT.  */
1785
struct plt_entry
1786
{
1787
  struct plt_entry *next;
1788
1789
  bfd_vma addend;
1790
1791
  union
1792
  {
1793
    bfd_signed_vma refcount;
1794
    bfd_vma offset;
1795
  } plt;
1796
};
1797
1798
struct ppc64_elf_obj_tdata
1799
{
1800
  struct elf_obj_tdata elf;
1801
1802
  /* Shortcuts to dynamic linker sections.  */
1803
  asection *got;
1804
  asection *relgot;
1805
1806
  /* Used during garbage collection.  We attach global symbols defined
1807
     on removed .opd entries to this section so that the sym is removed.  */
1808
  asection *deleted_section;
1809
1810
  /* TLS local dynamic got entry handling.  Support for multiple GOT
1811
     sections means we potentially need one of these for each input bfd.  */
1812
  struct got_entry tlsld_got;
1813
1814
  /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1815
     the reloc to be in the range -32768 to 32767.  */
1816
  unsigned int has_small_toc_reloc : 1;
1817
1818
  /* Set if toc/got ha relocs detected not using r2, or lo reloc
1819
     instruction not one we handle.  */
1820
  unsigned int unexpected_toc_insn : 1;
1821
1822
  /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1823
     this file.  */
1824
  unsigned int has_optrel : 1;
1825
};
1826
1827
#define ppc64_elf_tdata(bfd) \
1828
0
  ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1829
1830
#define ppc64_tlsld_got(bfd) \
1831
0
  (&ppc64_elf_tdata (bfd)->tlsld_got)
1832
1833
/* Override the generic function because we store some extras.  */
1834
1835
static bool
1836
ppc64_elf_mkobject (bfd *abfd)
1837
32.3k
{
1838
32.3k
  return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata));
1839
32.3k
}
1840
1841
/* Fix bad default arch selected for a 64 bit input bfd when the
1842
   default is 32 bit.  Also select arch based on apuinfo.  */
1843
1844
static bool
1845
ppc64_elf_object_p (bfd *abfd)
1846
400
{
1847
400
  if (!abfd->arch_info->the_default)
1848
0
    return true;
1849
1850
400
  if (abfd->arch_info->bits_per_word == 32)
1851
0
    {
1852
0
      Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1853
1854
0
      if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1855
0
  {
1856
    /* Relies on arch after 32 bit default being 64 bit default.  */
1857
0
    abfd->arch_info = abfd->arch_info->next;
1858
0
    BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1859
0
  }
1860
0
    }
1861
400
  return _bfd_elf_ppc_set_arch (abfd);
1862
400
}
1863
1864
/* Support for core dump NOTE sections.  */
1865
1866
static bool
1867
ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1868
0
{
1869
0
  size_t offset, size;
1870
1871
0
  if (note->descsz != 504)
1872
0
    return false;
1873
1874
  /* pr_cursig */
1875
0
  elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1876
1877
  /* pr_pid */
1878
0
  elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1879
1880
  /* pr_reg */
1881
0
  offset = 112;
1882
0
  size = 384;
1883
1884
  /* Make a ".reg/999" section.  */
1885
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1886
0
            size, note->descpos + offset);
1887
0
}
1888
1889
static bool
1890
ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1891
0
{
1892
0
  if (note->descsz != 136)
1893
0
    return false;
1894
1895
0
  elf_tdata (abfd)->core->pid
1896
0
    = bfd_get_32 (abfd, note->descdata + 24);
1897
0
  elf_tdata (abfd)->core->program
1898
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1899
0
  elf_tdata (abfd)->core->command
1900
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1901
1902
0
  return true;
1903
0
}
1904
1905
static char *
1906
ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1907
         ...)
1908
0
{
1909
0
  switch (note_type)
1910
0
    {
1911
0
    default:
1912
0
      return NULL;
1913
1914
0
    case NT_PRPSINFO:
1915
0
      {
1916
0
  char data[136] ATTRIBUTE_NONSTRING;
1917
0
  va_list ap;
1918
1919
0
  va_start (ap, note_type);
1920
0
  memset (data, 0, sizeof (data));
1921
0
  strncpy (data + 40, va_arg (ap, const char *), 16);
1922
#if GCC_VERSION == 8000 || GCC_VERSION == 8001
1923
  DIAGNOSTIC_PUSH;
1924
  /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1925
     -Wstringop-truncation:
1926
     https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1927
   */
1928
  DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1929
#endif
1930
0
  strncpy (data + 56, va_arg (ap, const char *), 80);
1931
#if GCC_VERSION == 8000 || GCC_VERSION == 8001
1932
  DIAGNOSTIC_POP;
1933
#endif
1934
0
  va_end (ap);
1935
0
  return elfcore_write_note (abfd, buf, bufsiz,
1936
0
           "CORE", note_type, data, sizeof (data));
1937
0
      }
1938
1939
0
    case NT_PRSTATUS:
1940
0
      {
1941
0
  char data[504];
1942
0
  va_list ap;
1943
0
  long pid;
1944
0
  int cursig;
1945
0
  const void *greg;
1946
1947
0
  va_start (ap, note_type);
1948
0
  memset (data, 0, 112);
1949
0
  pid = va_arg (ap, long);
1950
0
  bfd_put_32 (abfd, pid, data + 32);
1951
0
  cursig = va_arg (ap, int);
1952
0
  bfd_put_16 (abfd, cursig, data + 12);
1953
0
  greg = va_arg (ap, const void *);
1954
0
  memcpy (data + 112, greg, 384);
1955
0
  memset (data + 496, 0, 8);
1956
0
  va_end (ap);
1957
0
  return elfcore_write_note (abfd, buf, bufsiz,
1958
0
           "CORE", note_type, data, sizeof (data));
1959
0
      }
1960
0
    }
1961
0
}
1962
1963
/* Add extra PPC sections.  */
1964
1965
static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1966
{
1967
  { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
1968
  { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1969
  { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1970
  { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1971
  { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1972
  { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1973
  { NULL,         0,  0, 0,      0 }
1974
};
1975
1976
enum _ppc64_sec_type {
1977
  sec_normal = 0,
1978
  sec_opd = 1,
1979
  sec_toc = 2,
1980
  sec_stub = 3
1981
};
1982
1983
struct _ppc64_elf_section_data
1984
{
1985
  struct bfd_elf_section_data elf;
1986
1987
  union
1988
  {
1989
    /* An array with one entry for each opd function descriptor,
1990
       and some spares since opd entries may be either 16 or 24 bytes.  */
1991
0
#define OPD_NDX(OFF) ((OFF) >> 4)
1992
    struct _opd_sec_data
1993
    {
1994
      /* Points to the function code section for local opd entries.  */
1995
      asection **func_sec;
1996
1997
      /* After editing .opd, adjust references to opd local syms.  */
1998
      long *adjust;
1999
2000
      union
2001
      {
2002
  /* A copy of relocs before they are modified for --emit-relocs.  */
2003
  Elf_Internal_Rela *relocs;
2004
2005
  /* Section contents.  */
2006
  bfd_byte *contents;
2007
      } u;
2008
    } opd;
2009
2010
    /* An array for toc sections, indexed by offset/8.  */
2011
    struct _toc_sec_data
2012
    {
2013
      /* Specifies the relocation symbol index used at a given toc offset.  */
2014
      unsigned *symndx;
2015
2016
      /* And the relocation addend.  */
2017
      bfd_vma *add;
2018
    } toc;
2019
2020
    /* Stub debugging.  */
2021
    struct ppc_stub_hash_entry *last_ent;
2022
  } u;
2023
2024
  enum _ppc64_sec_type sec_type:2;
2025
2026
  /* Flag set when small branches are detected.  Used to
2027
     select suitable defaults for the stub group size.  */
2028
  unsigned int has_14bit_branch:1;
2029
2030
  /* Flag set when PLTCALL relocs are detected.  */
2031
  unsigned int has_pltcall:1;
2032
2033
  /* Flag set when section has PLT/GOT/TOC relocations that can be
2034
     optimised.  */
2035
  unsigned int has_optrel:1;
2036
};
2037
2038
#define ppc64_elf_section_data(sec) \
2039
0
  ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2040
2041
static bool
2042
ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2043
6.56k
{
2044
6.56k
  struct _ppc64_elf_section_data *sdata;
2045
2046
6.56k
  sdata = bfd_zalloc (abfd, sizeof (*sdata));
2047
6.56k
  if (sdata == NULL)
2048
0
    return false;
2049
6.56k
  sec->used_by_bfd = sdata;
2050
2051
6.56k
  return _bfd_elf_new_section_hook (abfd, sec);
2052
6.56k
}
2053
2054
static bool
2055
ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2056
6.14k
{
2057
6.14k
  const char *name = hdr->bfd_section->name;
2058
2059
6.14k
  if (startswith (name, ".sbss")
2060
6.06k
      || startswith (name, ".sdata"))
2061
173
    hdr->bfd_section->flags |= SEC_SMALL_DATA;
2062
2063
6.14k
  return true;
2064
6.14k
}
2065
2066
static struct _opd_sec_data *
2067
get_opd_info (asection * sec)
2068
0
{
2069
0
  if (sec != NULL
2070
0
      && ppc64_elf_section_data (sec) != NULL
2071
0
      && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2072
0
    return &ppc64_elf_section_data (sec)->u.opd;
2073
0
  return NULL;
2074
0
}
2075

2076
/* Parameters for the qsort hook.  */
2077
static bool synthetic_relocatable;
2078
static const asection *synthetic_opd;
2079
2080
/* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
2081
2082
static int
2083
compare_symbols (const void *ap, const void *bp)
2084
0
{
2085
0
  const asymbol *a = *(const asymbol **) ap;
2086
0
  const asymbol *b = *(const asymbol **) bp;
2087
2088
  /* Section symbols first.  */
2089
0
  if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2090
0
    return -1;
2091
0
  if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2092
0
    return 1;
2093
2094
  /* then .opd symbols.  */
2095
0
  if (synthetic_opd != NULL)
2096
0
    {
2097
0
      if (strcmp (a->section->name, ".opd") == 0
2098
0
    && strcmp (b->section->name, ".opd") != 0)
2099
0
  return -1;
2100
0
      if (strcmp (a->section->name, ".opd") != 0
2101
0
    && strcmp (b->section->name, ".opd") == 0)
2102
0
  return 1;
2103
0
    }
2104
2105
  /* then other code symbols.  */
2106
0
  if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2107
0
       == (SEC_CODE | SEC_ALLOC))
2108
0
      && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2109
0
    != (SEC_CODE | SEC_ALLOC)))
2110
0
    return -1;
2111
2112
0
  if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2113
0
       != (SEC_CODE | SEC_ALLOC))
2114
0
      && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2115
0
    == (SEC_CODE | SEC_ALLOC)))
2116
0
    return 1;
2117
2118
0
  if (synthetic_relocatable)
2119
0
    {
2120
0
      if (a->section->id < b->section->id)
2121
0
  return -1;
2122
2123
0
      if (a->section->id > b->section->id)
2124
0
  return 1;
2125
0
    }
2126
2127
0
  if (a->value + a->section->vma < b->value + b->section->vma)
2128
0
    return -1;
2129
2130
0
  if (a->value + a->section->vma > b->value + b->section->vma)
2131
0
    return 1;
2132
2133
  /* For syms with the same value, prefer strong dynamic global function
2134
     syms over other syms.  */
2135
0
  if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2136
0
    return -1;
2137
2138
0
  if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2139
0
    return 1;
2140
2141
0
  if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2142
0
    return -1;
2143
2144
0
  if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2145
0
    return 1;
2146
2147
0
  if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2148
0
    return -1;
2149
2150
0
  if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2151
0
    return 1;
2152
2153
0
  if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2154
0
    return -1;
2155
2156
0
  if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2157
0
    return 1;
2158
2159
  /* Finally, sort on where the symbol is in memory.  The symbols will
2160
     be in at most two malloc'd blocks, one for static syms, one for
2161
     dynamic syms, and we distinguish the two blocks above by testing
2162
     BSF_DYNAMIC.  Since we are sorting the symbol pointers which were
2163
     originally in the same order as the symbols (and we're not
2164
     sorting the symbols themselves), this ensures a stable sort.  */
2165
0
  if (a < b)
2166
0
    return -1;
2167
0
  if (a > b)
2168
0
    return 1;
2169
0
  return 0;
2170
0
}
2171
2172
/* Search SYMS for a symbol of the given VALUE.  */
2173
2174
static asymbol *
2175
sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2176
         bfd_vma value)
2177
0
{
2178
0
  size_t mid;
2179
2180
0
  if (id == (unsigned) -1)
2181
0
    {
2182
0
      while (lo < hi)
2183
0
  {
2184
0
    mid = (lo + hi) >> 1;
2185
0
    if (syms[mid]->value + syms[mid]->section->vma < value)
2186
0
      lo = mid + 1;
2187
0
    else if (syms[mid]->value + syms[mid]->section->vma > value)
2188
0
      hi = mid;
2189
0
    else
2190
0
      return syms[mid];
2191
0
  }
2192
0
    }
2193
0
  else
2194
0
    {
2195
0
      while (lo < hi)
2196
0
  {
2197
0
    mid = (lo + hi) >> 1;
2198
0
    if (syms[mid]->section->id < id)
2199
0
      lo = mid + 1;
2200
0
    else if (syms[mid]->section->id > id)
2201
0
      hi = mid;
2202
0
    else if (syms[mid]->value < value)
2203
0
      lo = mid + 1;
2204
0
    else if (syms[mid]->value > value)
2205
0
      hi = mid;
2206
0
    else
2207
0
      return syms[mid];
2208
0
  }
2209
0
    }
2210
0
  return NULL;
2211
0
}
2212
2213
static bool
2214
section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2215
0
{
2216
0
  bfd_vma vma = *(bfd_vma *) ptr;
2217
0
  return ((section->flags & SEC_ALLOC) != 0
2218
0
    && section->vma <= vma
2219
0
    && vma < section->vma + section->size);
2220
0
}
2221
2222
/* Create synthetic symbols, effectively restoring "dot-symbol" function
2223
   entry syms.  Also generate @plt symbols for the glink branch table.
2224
   Returns count of synthetic symbols in RET or -1 on error.  */
2225
2226
static long
2227
ppc64_elf_get_synthetic_symtab (bfd *abfd,
2228
        long static_count, asymbol **static_syms,
2229
        long dyn_count, asymbol **dyn_syms,
2230
        asymbol **ret)
2231
15
{
2232
15
  asymbol *s;
2233
15
  size_t i, j, count;
2234
15
  char *names;
2235
15
  size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2236
15
  asection *opd = NULL;
2237
15
  bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2238
15
  asymbol **syms;
2239
15
  int abi = abiversion (abfd);
2240
2241
15
  *ret = NULL;
2242
2243
15
  if (abi < 2)
2244
14
    {
2245
14
      opd = bfd_get_section_by_name (abfd, ".opd");
2246
14
      if (opd == NULL && abi == 1)
2247
0
  return 0;
2248
14
    }
2249
2250
15
  syms = NULL;
2251
15
  codesecsym = 0;
2252
15
  codesecsymend = 0;
2253
15
  secsymend = 0;
2254
15
  opdsymend = 0;
2255
15
  symcount = 0;
2256
15
  if (opd != NULL)
2257
0
    {
2258
0
      symcount = static_count;
2259
0
      if (!relocatable)
2260
0
  symcount += dyn_count;
2261
0
      if (symcount == 0)
2262
0
  return 0;
2263
2264
0
      syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2265
0
      if (syms == NULL)
2266
0
  return -1;
2267
2268
0
      if (!relocatable && static_count != 0 && dyn_count != 0)
2269
0
  {
2270
    /* Use both symbol tables.  */
2271
0
    memcpy (syms, static_syms, static_count * sizeof (*syms));
2272
0
    memcpy (syms + static_count, dyn_syms,
2273
0
      (dyn_count + 1) * sizeof (*syms));
2274
0
  }
2275
0
      else if (!relocatable && static_count == 0)
2276
0
  memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2277
0
      else
2278
0
  memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2279
2280
      /* Trim uninteresting symbols.  Interesting symbols are section,
2281
   function, and notype symbols.  */
2282
0
      for (i = 0, j = 0; i < symcount; ++i)
2283
0
  if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2284
0
             | BSF_RELC | BSF_SRELC)) == 0)
2285
0
    syms[j++] = syms[i];
2286
0
      symcount = j;
2287
2288
0
      synthetic_relocatable = relocatable;
2289
0
      synthetic_opd = opd;
2290
0
      qsort (syms, symcount, sizeof (*syms), compare_symbols);
2291
2292
0
      if (!relocatable && symcount > 1)
2293
0
  {
2294
    /* Trim duplicate syms, since we may have merged the normal
2295
       and dynamic symbols.  Actually, we only care about syms
2296
       that have different values, so trim any with the same
2297
       value.  Don't consider ifunc and ifunc resolver symbols
2298
       duplicates however, because GDB wants to know whether a
2299
       text symbol is an ifunc resolver.  */
2300
0
    for (i = 1, j = 1; i < symcount; ++i)
2301
0
      {
2302
0
        const asymbol *s0 = syms[i - 1];
2303
0
        const asymbol *s1 = syms[i];
2304
2305
0
        if ((s0->value + s0->section->vma
2306
0
       != s1->value + s1->section->vma)
2307
0
      || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2308
0
          != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2309
0
    syms[j++] = syms[i];
2310
0
      }
2311
0
    symcount = j;
2312
0
  }
2313
2314
0
      i = 0;
2315
      /* Note that here and in compare_symbols we can't compare opd and
2316
   sym->section directly.  With separate debug info files, the
2317
   symbols will be extracted from the debug file while abfd passed
2318
   to this function is the real binary.  */
2319
0
      if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2320
0
    && strcmp (syms[i]->section->name, ".opd") == 0)
2321
0
  ++i;
2322
0
      codesecsym = i;
2323
2324
0
      for (; i < symcount; ++i)
2325
0
  if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2326
0
           | SEC_THREAD_LOCAL))
2327
0
       != (SEC_CODE | SEC_ALLOC))
2328
0
      || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2329
0
    break;
2330
0
      codesecsymend = i;
2331
2332
0
      for (; i < symcount; ++i)
2333
0
  if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2334
0
    break;
2335
0
      secsymend = i;
2336
2337
0
      for (; i < symcount; ++i)
2338
0
  if (strcmp (syms[i]->section->name, ".opd") != 0)
2339
0
    break;
2340
0
      opdsymend = i;
2341
2342
0
      for (; i < symcount; ++i)
2343
0
  if (((syms[i]->section->flags
2344
0
        & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2345
0
      != (SEC_CODE | SEC_ALLOC))
2346
0
    break;
2347
0
      symcount = i;
2348
0
    }
2349
15
  count = 0;
2350
2351
15
  if (relocatable)
2352
11
    {
2353
11
      bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2354
11
      arelent *r;
2355
11
      size_t size;
2356
11
      size_t relcount;
2357
2358
11
      if (opdsymend == secsymend)
2359
11
  goto done;
2360
2361
0
      slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2362
0
      relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2363
0
      if (relcount == 0)
2364
0
  goto done;
2365
2366
0
      if (!(*slurp_relocs) (abfd, opd, static_syms, false))
2367
0
  {
2368
0
    count = -1;
2369
0
    goto done;
2370
0
  }
2371
2372
0
      size = 0;
2373
0
      for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2374
0
  {
2375
0
    asymbol *sym;
2376
2377
0
    while (r < opd->relocation + relcount
2378
0
     && r->address < syms[i]->value + opd->vma)
2379
0
      ++r;
2380
2381
0
    if (r == opd->relocation + relcount)
2382
0
      break;
2383
2384
0
    if (r->address != syms[i]->value + opd->vma)
2385
0
      continue;
2386
2387
0
    if (r->howto->type != R_PPC64_ADDR64)
2388
0
      continue;
2389
2390
0
    sym = *r->sym_ptr_ptr;
2391
0
    if (!sym_exists_at (syms, opdsymend, symcount,
2392
0
            sym->section->id, sym->value + r->addend))
2393
0
      {
2394
0
        ++count;
2395
0
        size += sizeof (asymbol);
2396
0
        size += strlen (syms[i]->name) + 2;
2397
0
      }
2398
0
  }
2399
2400
0
      if (size == 0)
2401
0
  goto done;
2402
0
      s = *ret = bfd_malloc (size);
2403
0
      if (s == NULL)
2404
0
  {
2405
0
    count = -1;
2406
0
    goto done;
2407
0
  }
2408
2409
0
      names = (char *) (s + count);
2410
2411
0
      for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2412
0
  {
2413
0
    asymbol *sym;
2414
2415
0
    while (r < opd->relocation + relcount
2416
0
     && r->address < syms[i]->value + opd->vma)
2417
0
      ++r;
2418
2419
0
    if (r == opd->relocation + relcount)
2420
0
      break;
2421
2422
0
    if (r->address != syms[i]->value + opd->vma)
2423
0
      continue;
2424
2425
0
    if (r->howto->type != R_PPC64_ADDR64)
2426
0
      continue;
2427
2428
0
    sym = *r->sym_ptr_ptr;
2429
0
    if (!sym_exists_at (syms, opdsymend, symcount,
2430
0
            sym->section->id, sym->value + r->addend))
2431
0
      {
2432
0
        size_t len;
2433
2434
0
        *s = *syms[i];
2435
0
        s->flags |= BSF_SYNTHETIC;
2436
0
        s->section = sym->section;
2437
0
        s->value = sym->value + r->addend;
2438
0
        s->name = names;
2439
0
        *names++ = '.';
2440
0
        len = strlen (syms[i]->name);
2441
0
        memcpy (names, syms[i]->name, len + 1);
2442
0
        names += len + 1;
2443
        /* Have udata.p point back to the original symbol this
2444
     synthetic symbol was derived from.  */
2445
0
        s->udata.p = syms[i];
2446
0
        s++;
2447
0
      }
2448
0
  }
2449
0
    }
2450
4
  else
2451
4
    {
2452
4
      bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2453
4
      bfd_byte *contents = NULL;
2454
4
      size_t size;
2455
4
      size_t plt_count = 0;
2456
4
      bfd_vma glink_vma = 0, resolv_vma = 0;
2457
4
      asection *dynamic, *glink = NULL, *relplt = NULL;
2458
4
      arelent *p;
2459
2460
4
      if (opd != NULL
2461
0
    && ((opd->flags & SEC_HAS_CONTENTS) == 0
2462
0
        || !bfd_malloc_and_get_section (abfd, opd, &contents)))
2463
0
  {
2464
0
  free_contents_and_exit_err:
2465
0
    count = -1;
2466
4
  free_contents_and_exit:
2467
4
    free (contents);
2468
4
    goto done;
2469
0
  }
2470
2471
4
      size = 0;
2472
4
      for (i = secsymend; i < opdsymend; ++i)
2473
0
  {
2474
0
    bfd_vma ent;
2475
2476
    /* Ignore bogus symbols.  */
2477
0
    if (syms[i]->value > opd->size - 8)
2478
0
      continue;
2479
2480
0
    ent = bfd_get_64 (abfd, contents + syms[i]->value);
2481
0
    if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2482
0
      {
2483
0
        ++count;
2484
0
        size += sizeof (asymbol);
2485
0
        size += strlen (syms[i]->name) + 2;
2486
0
      }
2487
0
  }
2488
2489
      /* Get start of .glink stubs from DT_PPC64_GLINK.  */
2490
4
      if (dyn_count != 0
2491
0
    && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2492
0
  {
2493
0
    bfd_byte *dynbuf, *extdyn, *extdynend;
2494
0
    size_t extdynsize;
2495
0
    void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2496
2497
0
    if ((dynamic->flags & SEC_HAS_CONTENTS) == 0
2498
0
        || !bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2499
0
      goto free_contents_and_exit_err;
2500
2501
0
    extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2502
0
    swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2503
2504
0
    for (extdyn = dynbuf, extdynend = dynbuf + dynamic->size;
2505
0
         (size_t) (extdynend - extdyn) >= extdynsize;
2506
0
         extdyn += extdynsize)
2507
0
      {
2508
0
        Elf_Internal_Dyn dyn;
2509
0
        (*swap_dyn_in) (abfd, extdyn, &dyn);
2510
2511
0
        if (dyn.d_tag == DT_NULL)
2512
0
    break;
2513
2514
0
        if (dyn.d_tag == DT_PPC64_GLINK)
2515
0
    {
2516
      /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2517
         See comment in ppc64_elf_finish_dynamic_sections. */
2518
0
      glink_vma = dyn.d_un.d_val + 8 * 4;
2519
      /* The .glink section usually does not survive the final
2520
         link; search for the section (usually .text) where the
2521
         glink stubs now reside.  */
2522
0
      glink = bfd_sections_find_if (abfd, section_covers_vma,
2523
0
            &glink_vma);
2524
0
      break;
2525
0
    }
2526
0
      }
2527
2528
0
    free (dynbuf);
2529
0
  }
2530
2531
4
      if (glink != NULL)
2532
0
  {
2533
    /* Determine __glink trampoline by reading the relative branch
2534
       from the first glink stub.  */
2535
0
    bfd_byte buf[4];
2536
0
    unsigned int off = 0;
2537
2538
0
    while (bfd_get_section_contents (abfd, glink, buf,
2539
0
             glink_vma + off - glink->vma, 4))
2540
0
      {
2541
0
        unsigned int insn = bfd_get_32 (abfd, buf);
2542
0
        insn ^= B_DOT;
2543
0
        if ((insn & ~0x3fffffc) == 0)
2544
0
    {
2545
0
      resolv_vma
2546
0
        = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2547
0
      break;
2548
0
    }
2549
0
        off += 4;
2550
0
        if (off > 4)
2551
0
    break;
2552
0
      }
2553
2554
0
    if (resolv_vma)
2555
0
      size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2556
2557
0
    relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2558
0
    if (relplt != NULL)
2559
0
      {
2560
0
        slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2561
0
        if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
2562
0
    goto free_contents_and_exit_err;
2563
2564
0
        plt_count = NUM_SHDR_ENTRIES (&elf_section_data (relplt)->this_hdr);
2565
0
        size += plt_count * sizeof (asymbol);
2566
2567
0
        p = relplt->relocation;
2568
0
        for (i = 0; i < plt_count; i++, p++)
2569
0
    {
2570
0
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2571
0
      if (p->addend != 0)
2572
0
        size += sizeof ("+0x") - 1 + 16;
2573
0
    }
2574
0
      }
2575
0
  }
2576
2577
4
      if (size == 0)
2578
4
  goto free_contents_and_exit;
2579
0
      s = *ret = bfd_malloc (size);
2580
0
      if (s == NULL)
2581
0
  goto free_contents_and_exit_err;
2582
2583
0
      names = (char *) (s + count + plt_count + (resolv_vma != 0));
2584
2585
0
      for (i = secsymend; i < opdsymend; ++i)
2586
0
  {
2587
0
    bfd_vma ent;
2588
2589
0
    if (syms[i]->value > opd->size - 8)
2590
0
      continue;
2591
2592
0
    ent = bfd_get_64 (abfd, contents + syms[i]->value);
2593
0
    if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2594
0
      {
2595
0
        size_t lo, hi;
2596
0
        size_t len;
2597
0
        asection *sec = abfd->sections;
2598
2599
0
        *s = *syms[i];
2600
0
        lo = codesecsym;
2601
0
        hi = codesecsymend;
2602
0
        while (lo < hi)
2603
0
    {
2604
0
      size_t mid = (lo + hi) >> 1;
2605
0
      if (syms[mid]->section->vma < ent)
2606
0
        lo = mid + 1;
2607
0
      else if (syms[mid]->section->vma > ent)
2608
0
        hi = mid;
2609
0
      else
2610
0
        {
2611
0
          sec = syms[mid]->section;
2612
0
          break;
2613
0
        }
2614
0
    }
2615
2616
0
        if (lo >= hi && lo > codesecsym)
2617
0
    sec = syms[lo - 1]->section;
2618
2619
0
        for (; sec != NULL; sec = sec->next)
2620
0
    {
2621
0
      if (sec->vma > ent)
2622
0
        break;
2623
      /* SEC_LOAD may not be set if SEC is from a separate debug
2624
         info file.  */
2625
0
      if ((sec->flags & SEC_ALLOC) == 0)
2626
0
        break;
2627
0
      if ((sec->flags & SEC_CODE) != 0)
2628
0
        s->section = sec;
2629
0
    }
2630
0
        s->flags |= BSF_SYNTHETIC;
2631
0
        s->value = ent - s->section->vma;
2632
0
        s->name = names;
2633
0
        *names++ = '.';
2634
0
        len = strlen (syms[i]->name);
2635
0
        memcpy (names, syms[i]->name, len + 1);
2636
0
        names += len + 1;
2637
        /* Have udata.p point back to the original symbol this
2638
     synthetic symbol was derived from.  */
2639
0
        s->udata.p = syms[i];
2640
0
        s++;
2641
0
      }
2642
0
  }
2643
0
      free (contents);
2644
2645
0
      if (glink != NULL && relplt != NULL)
2646
0
  {
2647
0
    if (resolv_vma)
2648
0
      {
2649
        /* Add a symbol for the main glink trampoline.  */
2650
0
        memset (s, 0, sizeof *s);
2651
0
        s->the_bfd = abfd;
2652
0
        s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2653
0
        s->section = glink;
2654
0
        s->value = resolv_vma - glink->vma;
2655
0
        s->name = names;
2656
0
        memcpy (names, "__glink_PLTresolve",
2657
0
          sizeof ("__glink_PLTresolve"));
2658
0
        names += sizeof ("__glink_PLTresolve");
2659
0
        s++;
2660
0
        count++;
2661
0
      }
2662
2663
    /* FIXME: It would be very much nicer to put sym@plt on the
2664
       stub rather than on the glink branch table entry.  The
2665
       objdump disassembler would then use a sensible symbol
2666
       name on plt calls.  The difficulty in doing so is
2667
       a) finding the stubs, and,
2668
       b) matching stubs against plt entries, and,
2669
       c) there can be multiple stubs for a given plt entry.
2670
2671
       Solving (a) could be done by code scanning, but older
2672
       ppc64 binaries used different stubs to current code.
2673
       (b) is the tricky one since you need to known the toc
2674
       pointer for at least one function that uses a pic stub to
2675
       be able to calculate the plt address referenced.
2676
       (c) means gdb would need to set multiple breakpoints (or
2677
       find the glink branch itself) when setting breakpoints
2678
       for pending shared library loads.  */
2679
0
    p = relplt->relocation;
2680
0
    for (i = 0; i < plt_count; i++, p++)
2681
0
      {
2682
0
        size_t len;
2683
2684
0
        *s = **p->sym_ptr_ptr;
2685
        /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
2686
     we are defining a symbol, ensure one of them is set.  */
2687
0
        if ((s->flags & BSF_LOCAL) == 0)
2688
0
    s->flags |= BSF_GLOBAL;
2689
0
        s->flags |= BSF_SYNTHETIC;
2690
0
        s->section = glink;
2691
0
        s->value = glink_vma - glink->vma;
2692
0
        s->name = names;
2693
0
        s->udata.p = NULL;
2694
0
        len = strlen ((*p->sym_ptr_ptr)->name);
2695
0
        memcpy (names, (*p->sym_ptr_ptr)->name, len);
2696
0
        names += len;
2697
0
        if (p->addend != 0)
2698
0
    {
2699
0
      memcpy (names, "+0x", sizeof ("+0x") - 1);
2700
0
      names += sizeof ("+0x") - 1;
2701
0
      bfd_sprintf_vma (abfd, names, p->addend);
2702
0
      names += strlen (names);
2703
0
    }
2704
0
        memcpy (names, "@plt", sizeof ("@plt"));
2705
0
        names += sizeof ("@plt");
2706
0
        s++;
2707
0
        if (abi < 2)
2708
0
    {
2709
0
      glink_vma += 8;
2710
0
      if (i >= 0x8000)
2711
0
        glink_vma += 4;
2712
0
    }
2713
0
        else
2714
0
    glink_vma += 4;
2715
0
      }
2716
0
    count += plt_count;
2717
0
  }
2718
0
    }
2719
2720
15
 done:
2721
15
  free (syms);
2722
15
  return count;
2723
15
}
2724

2725
/* The following functions are specific to the ELF linker, while
2726
   functions above are used generally.  Those named ppc64_elf_* are
2727
   called by the main ELF linker code.  They appear in this file more
2728
   or less in the order in which they are called.  eg.
2729
   ppc64_elf_check_relocs is called early in the link process,
2730
   ppc64_elf_finish_dynamic_sections is one of the last functions
2731
   called.
2732
2733
   PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2734
   functions have both a function code symbol and a function descriptor
2735
   symbol.  A call to foo in a relocatable object file looks like:
2736
2737
   .    .text
2738
   .  x:
2739
   .    bl  .foo
2740
   .    nop
2741
2742
   The function definition in another object file might be:
2743
2744
   .    .section .opd
2745
   .  foo:  .quad .foo
2746
   .    .quad .TOC.@tocbase
2747
   .    .quad 0
2748
   .
2749
   .    .text
2750
   .  .foo: blr
2751
2752
   When the linker resolves the call during a static link, the branch
2753
   unsurprisingly just goes to .foo and the .opd information is unused.
2754
   If the function definition is in a shared library, things are a little
2755
   different:  The call goes via a plt call stub, the opd information gets
2756
   copied to the plt, and the linker patches the nop.
2757
2758
   .  x:
2759
   .    bl  .foo_stub
2760
   .    ld  2,40(1)
2761
   .
2762
   .
2763
   .  .foo_stub:
2764
   .    std 2,40(1)     # in practice, the call stub
2765
   .    addis 11,2,Lfoo@toc@ha  # is slightly optimized, but
2766
   .    addi  11,11,Lfoo@toc@l  # this is the general idea
2767
   .    ld  12,0(11)
2768
   .    ld  2,8(11)
2769
   .    mtctr 12
2770
   .    ld  11,16(11)
2771
   .    bctr
2772
   .
2773
   .    .section .plt
2774
   .  Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2775
2776
   The "reloc ()" notation is supposed to indicate that the linker emits
2777
   an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
2778
   copying.
2779
2780
   What are the difficulties here?  Well, firstly, the relocations
2781
   examined by the linker in check_relocs are against the function code
2782
   sym .foo, while the dynamic relocation in the plt is emitted against
2783
   the function descriptor symbol, foo.  Somewhere along the line, we need
2784
   to carefully copy dynamic link information from one symbol to the other.
2785
   Secondly, the generic part of the elf linker will make .foo a dynamic
2786
   symbol as is normal for most other backends.  We need foo dynamic
2787
   instead, at least for an application final link.  However, when
2788
   creating a shared library containing foo, we need to have both symbols
2789
   dynamic so that references to .foo are satisfied during the early
2790
   stages of linking.  Otherwise the linker might decide to pull in a
2791
   definition from some other object, eg. a static library.
2792
2793
   Update: As of August 2004, we support a new convention.  Function
2794
   calls may use the function descriptor symbol, ie. "bl foo".  This
2795
   behaves exactly as "bl .foo".  */
2796
2797
/* Of those relocs that might be copied as dynamic relocs, this
2798
   function selects those that must be copied when linking a shared
2799
   library or PIE, even when the symbol is local.  */
2800
2801
static int
2802
must_be_dyn_reloc (struct bfd_link_info *info,
2803
       enum elf_ppc64_reloc_type r_type)
2804
0
{
2805
0
  switch (r_type)
2806
0
    {
2807
0
    default:
2808
      /* Only relative relocs can be resolved when the object load
2809
   address isn't fixed.  DTPREL64 is excluded because the
2810
   dynamic linker needs to differentiate global dynamic from
2811
   local dynamic __tls_index pairs when PPC64_OPT_TLS is set.  */
2812
0
      return 1;
2813
2814
0
    case R_PPC64_REL32:
2815
0
    case R_PPC64_REL64:
2816
0
    case R_PPC64_REL30:
2817
0
    case R_PPC64_TOC16:
2818
0
    case R_PPC64_TOC16_DS:
2819
0
    case R_PPC64_TOC16_LO:
2820
0
    case R_PPC64_TOC16_HI:
2821
0
    case R_PPC64_TOC16_HA:
2822
0
    case R_PPC64_TOC16_LO_DS:
2823
0
      return 0;
2824
2825
0
    case R_PPC64_TPREL16:
2826
0
    case R_PPC64_TPREL16_LO:
2827
0
    case R_PPC64_TPREL16_HI:
2828
0
    case R_PPC64_TPREL16_HA:
2829
0
    case R_PPC64_TPREL16_DS:
2830
0
    case R_PPC64_TPREL16_LO_DS:
2831
0
    case R_PPC64_TPREL16_HIGH:
2832
0
    case R_PPC64_TPREL16_HIGHA:
2833
0
    case R_PPC64_TPREL16_HIGHER:
2834
0
    case R_PPC64_TPREL16_HIGHERA:
2835
0
    case R_PPC64_TPREL16_HIGHEST:
2836
0
    case R_PPC64_TPREL16_HIGHESTA:
2837
0
    case R_PPC64_TPREL64:
2838
0
    case R_PPC64_TPREL34:
2839
      /* These relocations are relative but in a shared library the
2840
   linker doesn't know the thread pointer base.  */
2841
0
      return bfd_link_dll (info);
2842
0
    }
2843
0
}
2844
2845
/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2846
   copying dynamic variables from a shared lib into an app's .dynbss
2847
   section, and instead use a dynamic relocation to point into the
2848
   shared lib.  With code that gcc generates it is vital that this be
2849
   enabled;  In the PowerPC64 ELFv1 ABI the address of a function is
2850
   actually the address of a function descriptor which resides in the
2851
   .opd section.  gcc uses the descriptor directly rather than going
2852
   via the GOT as some other ABIs do, which means that initialized
2853
   function pointers reference the descriptor.  Thus, a function
2854
   pointer initialized to the address of a function in a shared
2855
   library will either require a .dynbss copy and a copy reloc, or a
2856
   dynamic reloc.  Using a .dynbss copy redefines the function
2857
   descriptor symbol to point to the copy.  This presents a problem as
2858
   a PLT entry for that function is also initialized from the function
2859
   descriptor symbol and the copy may not be initialized first.  */
2860
0
#define ELIMINATE_COPY_RELOCS 1
2861
2862
/* Section name for stubs is the associated section name plus this
2863
   string.  */
2864
0
#define STUB_SUFFIX ".stub"
2865
2866
/* Linker stubs.
2867
   ppc_stub_long_branch:
2868
   Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2869
   destination, but a 24 bit branch in a stub section will reach.
2870
   .  b dest
2871
2872
   ppc_stub_plt_branch:
2873
   Similar to the above, but a 24 bit branch in the stub section won't
2874
   reach its destination.
2875
   .  addis %r12,%r2,xxx@toc@ha
2876
   .  ld  %r12,xxx@toc@l(%r12)
2877
   .  mtctr %r12
2878
   .  bctr
2879
2880
   ppc_stub_plt_call:
2881
   Used to call a function in a shared library.  If it so happens that
2882
   the plt entry referenced crosses a 64k boundary, then an extra
2883
   "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2884
   An r2save variant starts with "std %r2,40(%r1)".
2885
   .  addis %r11,%r2,xxx@toc@ha
2886
   .  ld  %r12,xxx+0@toc@l(%r11)
2887
   .  mtctr %r12
2888
   .  ld  %r2,xxx+8@toc@l(%r11)
2889
   .  ld  %r11,xxx+16@toc@l(%r11)
2890
   .  bctr
2891
2892
   ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2893
   code to adjust the value and save r2 to support multiple toc sections.
2894
   A ppc_stub_long_branch with an r2 offset looks like:
2895
   .  std %r2,40(%r1)
2896
   .  addis %r2,%r2,off@ha
2897
   .  addi  %r2,%r2,off@l
2898
   .  b dest
2899
2900
   A ppc_stub_plt_branch with an r2 offset looks like:
2901
   .  std %r2,40(%r1)
2902
   .  addis %r12,%r2,xxx@toc@ha
2903
   .  ld  %r12,xxx@toc@l(%r12)
2904
   .  addis %r2,%r2,off@ha
2905
   .  addi  %r2,%r2,off@l
2906
   .  mtctr %r12
2907
   .  bctr
2908
2909
   All of the above stubs are shown as their ELFv1 variants.  ELFv2
2910
   variants exist too, simpler for plt calls since a new toc pointer
2911
   and static chain are not loaded by the stub.  In addition, ELFv2
2912
   has some more complex stubs to handle calls marked with NOTOC
2913
   relocs from functions where r2 is not a valid toc pointer.
2914
   ppc_stub_long_branch_p9notoc:
2915
   .  mflr  %r12
2916
   .  bcl 20,31,1f
2917
   .  1:
2918
   .  mflr  %r11
2919
   .  mtlr  %r12
2920
   .  addis %r12,%r11,dest-1b@ha
2921
   .  addi  %r12,%r12,dest-1b@l
2922
   .  b dest
2923
2924
   ppc_stub_plt_branch_p9notoc:
2925
   .  mflr  %r12
2926
   .  bcl 20,31,1f
2927
   .  1:
2928
   .  mflr  %r11
2929
   .  mtlr  %r12
2930
   .  lis %r12,xxx-1b@highest
2931
   .  ori %r12,%r12,xxx-1b@higher
2932
   .  sldi  %r12,%r12,32
2933
   .  oris  %r12,%r12,xxx-1b@high
2934
   .  ori %r12,%r12,xxx-1b@l
2935
   .  add %r12,%r11,%r12
2936
   .  mtctr %r12
2937
   .  bctr
2938
2939
   ppc_stub_plt_call_p9notoc:
2940
   .  mflr  %r12
2941
   .  bcl 20,31,1f
2942
   .  1:
2943
   .  mflr  %r11
2944
   .  mtlr  %r12
2945
   .  lis %r12,xxx-1b@highest
2946
   .  ori %r12,%r12,xxx-1b@higher
2947
   .  sldi  %r12,%r12,32
2948
   .  oris  %r12,%r12,xxx-1b@high
2949
   .  ori %r12,%r12,xxx-1b@l
2950
   .  ldx %r12,%r11,%r12
2951
   .  mtctr %r12
2952
   .  bctr
2953
2954
   There are also ELFv1 power10 variants of these stubs.
2955
   ppc_stub_long_branch_notoc:
2956
   .  pla %r12,dest@pcrel
2957
   .  b dest
2958
   ppc_stub_plt_branch_notoc:
2959
   .  lis %r11,(dest-1f)@highesta34
2960
   .  ori %r11,%r11,(dest-1f)@highera34
2961
   .  sldi  %r11,%r11,34
2962
   . 1: pla %r12,dest@pcrel
2963
   .  add %r12,%r11,%r12
2964
   .  mtctr %r12
2965
   .  bctr
2966
   ppc_stub_plt_call_notoc:
2967
   .  lis %r11,(xxx-1f)@highesta34
2968
   .  ori %r11,%r11,(xxx-1f)@highera34
2969
   .  sldi  %r11,%r11,34
2970
   . 1: pla %r12,xxx@pcrel
2971
   .  ldx %r12,%r11,%r12
2972
   .  mtctr %r12
2973
   .  bctr
2974
2975
   In cases where the high instructions would add zero, they are
2976
   omitted and following instructions modified in some cases.
2977
   For example, a power10 ppc_stub_plt_call_notoc might simplify down
2978
   to
2979
   .  pld %r12,xxx@pcrel
2980
   .  mtctr %r12
2981
   .  bctr
2982
2983
   Stub variants may be merged.  For example, if printf is called from
2984
   code with the tocsave optimization (ie. r2 saved in function
2985
   prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2986
   and from other code without the tocsave optimization requiring a
2987
   ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2988
   type will be created.  Calls with the tocsave optimization will
2989
   enter this stub after the instruction saving r2.  A similar
2990
   situation exists when calls are marked with R_PPC64_REL24_NOTOC
2991
   relocations.  These require a ppc_stub_plt_call_notoc linkage stub
2992
   to call an external function like printf.  If other calls to printf
2993
   require a ppc_stub_plt_call linkage stub then a single
2994
   ppc_stub_plt_call_notoc linkage stub may be used for both types of
2995
   call.  */
2996
2997
enum ppc_stub_main_type
2998
{
2999
  ppc_stub_none,
3000
  ppc_stub_long_branch,
3001
  ppc_stub_plt_branch,
3002
  ppc_stub_plt_call,
3003
  ppc_stub_global_entry,
3004
  ppc_stub_save_res
3005
};
3006
3007
/* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
3008
   these variations.  */
3009
3010
enum ppc_stub_sub_type
3011
{
3012
  ppc_stub_toc,
3013
  ppc_stub_notoc,
3014
  ppc_stub_p9notoc
3015
};
3016
3017
struct ppc_stub_type
3018
{
3019
  ENUM_BITFIELD (ppc_stub_main_type) main : 3;
3020
  ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
3021
  unsigned int r2save : 1;
3022
};
3023
3024
/* Information on stub grouping.  */
3025
struct map_stub
3026
{
3027
  /* The stub section.  */
3028
  asection *stub_sec;
3029
  /* This is the section to which stubs in the group will be attached.  */
3030
  asection *link_sec;
3031
  /* Next group.  */
3032
  struct map_stub *next;
3033
  /* Whether to emit a copy of register save/restore functions in this
3034
     group.  */
3035
  int needs_save_res;
3036
  /* Current offset within stubs after the insn restoring lr in a
3037
     _notoc or _both stub using bcl for pc-relative addressing, or
3038
     after the insn restoring lr in a __tls_get_addr_opt plt stub.  */
3039
  unsigned int lr_restore;
3040
  /* Accumulated size of EH info emitted to describe return address
3041
     if stubs modify lr.  Does not include 17 byte FDE header.  */
3042
  unsigned int eh_size;
3043
  /* Offset in glink_eh_frame to the start of EH info for this group.  */
3044
  unsigned int eh_base;
3045
};
3046
3047
struct ppc_stub_hash_entry
3048
{
3049
  /* Base hash table entry structure.  */
3050
  struct bfd_hash_entry root;
3051
3052
  struct ppc_stub_type type;
3053
3054
  /* Group information.  */
3055
  struct map_stub *group;
3056
3057
  /* Offset within stub_sec of the beginning of this stub.  */
3058
  bfd_vma stub_offset;
3059
3060
  /* Given the symbol's value and its section we can determine its final
3061
     value when building the stubs (so the stub knows where to jump.  */
3062
  bfd_vma target_value;
3063
  asection *target_section;
3064
3065
  /* The symbol table entry, if any, that this was derived from.  */
3066
  struct ppc_link_hash_entry *h;
3067
  struct plt_entry *plt_ent;
3068
3069
  /* Symbol type.  */
3070
  unsigned char symtype;
3071
3072
  /* Symbol st_other.  */
3073
  unsigned char other;
3074
3075
  /* Debug: Track hash table traversal.  */
3076
  unsigned int id;
3077
};
3078
3079
struct ppc_branch_hash_entry
3080
{
3081
  /* Base hash table entry structure.  */
3082
  struct bfd_hash_entry root;
3083
3084
  /* Offset within branch lookup table.  */
3085
  unsigned int offset;
3086
3087
  /* Generation marker.  */
3088
  unsigned int iter;
3089
};
3090
3091
/* Used to track dynamic relocations.  */
3092
struct ppc_dyn_relocs
3093
{
3094
  struct ppc_dyn_relocs *next;
3095
3096
  /* The input section of the reloc.  */
3097
  asection *sec;
3098
3099
  /* Total number of relocs copied for the input section.  */
3100
  unsigned int count;
3101
3102
  /* Number of pc-relative relocs copied for the input section.  */
3103
  unsigned int pc_count;
3104
3105
  /* Number of relocs that might become R_PPC64_RELATIVE.  */
3106
  unsigned int rel_count;
3107
};
3108
3109
struct ppc_local_dyn_relocs
3110
{
3111
  struct ppc_local_dyn_relocs *next;
3112
3113
  /* The input section of the reloc.  */
3114
  asection *sec;
3115
3116
  /* Total number of relocs copied for the input section.  */
3117
  unsigned int count;
3118
3119
  /* Number of relocs that might become R_PPC64_RELATIVE.  */
3120
  unsigned int rel_count : 31;
3121
3122
  /* Whether this entry is for STT_GNU_IFUNC symbols.  */
3123
  unsigned int ifunc : 1;
3124
};
3125
3126
struct ppc_link_hash_entry
3127
{
3128
  struct elf_link_hash_entry elf;
3129
3130
  union
3131
  {
3132
    /* A pointer to the most recently used stub hash entry against this
3133
       symbol.  */
3134
    struct ppc_stub_hash_entry *stub_cache;
3135
3136
    /* A pointer to the next symbol starting with a '.'  */
3137
    struct ppc_link_hash_entry *next_dot_sym;
3138
  } u;
3139
3140
  /* Link between function code and descriptor symbols.  */
3141
  struct ppc_link_hash_entry *oh;
3142
3143
  /* Flag function code and descriptor symbols.  */
3144
  unsigned int is_func:1;
3145
  unsigned int is_func_descriptor:1;
3146
  unsigned int fake:1;
3147
3148
  /* Whether global opd/toc sym has been adjusted or not.
3149
     After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3150
     should be set for all globals defined in any opd/toc section.  */
3151
  unsigned int adjust_done:1;
3152
3153
  /* Set if this is an out-of-line register save/restore function,
3154
     with non-standard calling convention.  */
3155
  unsigned int save_res:1;
3156
3157
  /* Set if a duplicate symbol with non-zero localentry is detected,
3158
     even when the duplicate symbol does not provide a definition.  */
3159
  unsigned int non_zero_localentry:1;
3160
3161
  /* Contexts in which symbol is used in the GOT (or TOC).
3162
     Bits are or'd into the mask as the corresponding relocs are
3163
     encountered during check_relocs, with TLS_TLS being set when any
3164
     of the other TLS bits are set.  tls_optimize clears bits when
3165
     optimizing to indicate the corresponding GOT entry type is not
3166
     needed.  If set, TLS_TLS is never cleared.  tls_optimize may also
3167
     set TLS_GDIE when a GD reloc turns into an IE one.
3168
     These flags are also kept for local symbols.  */
3169
0
#define TLS_TLS    1  /* Any TLS reloc.  */
3170
0
#define TLS_GD     2  /* GD reloc. */
3171
0
#define TLS_LD     4  /* LD reloc. */
3172
0
#define TLS_TPREL  8  /* TPREL reloc, => IE. */
3173
0
#define TLS_DTPREL  16  /* DTPREL reloc, => LD. */
3174
0
#define TLS_MARK  32  /* __tls_get_addr call marked. */
3175
0
#define TLS_GDIE  64  /* GOT TPREL reloc resulting from GD->IE. */
3176
0
#define TLS_EXPLICIT   256  /* TOC section TLS reloc, not stored. */
3177
  unsigned char tls_mask;
3178
3179
  /* The above field is also used to mark function symbols.  In which
3180
     case TLS_TLS will be 0.  */
3181
0
#define PLT_IFUNC  2  /* STT_GNU_IFUNC.  */
3182
0
#define PLT_KEEP   4  /* inline plt call requires plt entry.  */
3183
0
#define NON_GOT        256  /* local symbol plt, not stored.  */
3184
};
3185
3186
static inline struct ppc_link_hash_entry *
3187
ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3188
0
{
3189
0
  return (struct ppc_link_hash_entry *) ent;
3190
0
}
3191
3192
static inline struct elf_link_hash_entry *
3193
elf_hash_entry (struct ppc_link_hash_entry *ent)
3194
0
{
3195
0
  return (struct elf_link_hash_entry *) ent;
3196
0
}
3197
3198
/* ppc64 ELF linker hash table.  */
3199
3200
struct ppc_link_hash_table
3201
{
3202
  struct elf_link_hash_table elf;
3203
3204
  /* The stub hash table.  */
3205
  struct bfd_hash_table stub_hash_table;
3206
3207
  /* Another hash table for plt_branch stubs.  */
3208
  struct bfd_hash_table branch_hash_table;
3209
3210
  /* Hash table for function prologue tocsave.  */
3211
  htab_t tocsave_htab;
3212
3213
  /* Various options and other info passed from the linker.  */
3214
  struct ppc64_elf_params *params;
3215
3216
  /* The size of sec_info below.  */
3217
  unsigned int sec_info_arr_size;
3218
3219
  /* Per-section array of extra section info.  Done this way rather
3220
     than as part of ppc64_elf_section_data so we have the info for
3221
     non-ppc64 sections.  */
3222
  struct
3223
  {
3224
    /* Along with elf_gp, specifies the TOC pointer used by this section.  */
3225
    bfd_vma toc_off;
3226
3227
    union
3228
    {
3229
      /* The section group that this section belongs to.  */
3230
      struct map_stub *group;
3231
      /* A temp section list pointer.  */
3232
      asection *list;
3233
    } u;
3234
  } *sec_info;
3235
3236
  /* Linked list of groups.  */
3237
  struct map_stub *group;
3238
3239
  /* Temp used when calculating TOC pointers.  */
3240
  bfd_vma toc_curr;
3241
  bfd *toc_bfd;
3242
  asection *toc_first_sec;
3243
3244
  /* Used when adding symbols.  */
3245
  struct ppc_link_hash_entry *dot_syms;
3246
3247
  /* Shortcuts to get to dynamic linker sections.  */
3248
  asection *glink;
3249
  asection *global_entry;
3250
  asection *sfpr;
3251
  asection *pltlocal;
3252
  asection *relpltlocal;
3253
  asection *brlt;
3254
  asection *relbrlt;
3255
  asection *glink_eh_frame;
3256
3257
  /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3258
  struct ppc_link_hash_entry *tls_get_addr;
3259
  struct ppc_link_hash_entry *tls_get_addr_fd;
3260
  struct ppc_link_hash_entry *tga_desc;
3261
  struct ppc_link_hash_entry *tga_desc_fd;
3262
  struct map_stub *tga_group;
3263
3264
  /* The size of reliplt used by got entry relocs.  */
3265
  bfd_size_type got_reli_size;
3266
3267
  /* DT_RELR array of section/r_offset.  */
3268
  size_t relr_alloc;
3269
  size_t relr_count;
3270
  struct
3271
  {
3272
    asection *sec;
3273
    bfd_vma off;
3274
  } *relr;
3275
3276
  /* Statistics.  */
3277
  unsigned long stub_count[ppc_stub_save_res];
3278
3279
  /* Number of stubs against global syms.  */
3280
  unsigned long stub_globals;
3281
3282
  /* Set if we're linking code with function descriptors.  */
3283
  unsigned int opd_abi:1;
3284
3285
  /* Support for multiple toc sections.  */
3286
  unsigned int do_multi_toc:1;
3287
  unsigned int multi_toc_needed:1;
3288
  unsigned int second_toc_pass:1;
3289
  unsigned int do_toc_opt:1;
3290
3291
  /* Set if tls optimization is enabled.  */
3292
  unsigned int do_tls_opt:1;
3293
3294
  /* Set if inline plt calls should be converted to direct calls.  */
3295
  unsigned int can_convert_all_inline_plt:1;
3296
3297
  /* Set if a stub_offset changed.  */
3298
  unsigned int stub_changed:1;
3299
3300
  /* Set on error.  */
3301
  unsigned int stub_error:1;
3302
3303
  /* Whether func_desc_adjust needs to be run over symbols.  */
3304
  unsigned int need_func_desc_adj:1;
3305
3306
  /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized.  */
3307
  unsigned int has_plt_localentry0:1;
3308
3309
  /* Whether calls are made via the PLT from NOTOC functions.  */
3310
  unsigned int notoc_plt:1;
3311
3312
  /* Whether any code linked seems to be Power10.  */
3313
  unsigned int has_power10_relocs:1;
3314
3315
  /* Incremented once for each stub sized.  */
3316
  unsigned int stub_id;
3317
3318
  /* Incremented every time we size stubs.  */
3319
  unsigned int stub_iteration;
3320
3321
/* After 20 iterations of stub sizing we no longer allow stubs to
3322
   shrink.  This is to break out of a pathological case where adding
3323
   stubs or increasing their size on one iteration decreases section
3324
   gaps (perhaps due to alignment), which then results in smaller
3325
   stubs on the next iteration.  */
3326
0
#define STUB_SHRINK_ITER 20
3327
};
3328
3329
/* Rename some of the generic section flags to better document how they
3330
   are used here.  */
3331
3332
/* Nonzero if this section has TLS related relocations.  */
3333
0
#define has_tls_reloc sec_flg0
3334
3335
/* Nonzero if this section has a call to __tls_get_addr lacking marker
3336
   relocations.  */
3337
0
#define nomark_tls_get_addr sec_flg1
3338
3339
/* Nonzero if this section has any toc or got relocs.  */
3340
0
#define has_toc_reloc sec_flg2
3341
3342
/* Nonzero if this section has a call to another section that uses
3343
   the toc or got.  */
3344
0
#define makes_toc_func_call sec_flg3
3345
3346
/* Recursion protection when determining above flag.  */
3347
0
#define call_check_in_progress sec_flg4
3348
0
#define call_check_done sec_flg5
3349
3350
/* Get the ppc64 ELF linker hash table from a link_info structure.  */
3351
3352
#define ppc_hash_table(p) \
3353
0
  ((is_elf_hash_table ((p)->hash)          \
3354
0
    && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3355
0
   ? (struct ppc_link_hash_table *) (p)->hash : NULL)
3356
3357
#define ppc_stub_hash_lookup(table, string, create, copy) \
3358
0
  ((struct ppc_stub_hash_entry *) \
3359
0
   bfd_hash_lookup ((table), (string), (create), (copy)))
3360
3361
#define ppc_branch_hash_lookup(table, string, create, copy) \
3362
0
  ((struct ppc_branch_hash_entry *) \
3363
0
   bfd_hash_lookup ((table), (string), (create), (copy)))
3364
3365
/* Create an entry in the stub hash table.  */
3366
3367
static struct bfd_hash_entry *
3368
stub_hash_newfunc (struct bfd_hash_entry *entry,
3369
       struct bfd_hash_table *table,
3370
       const char *string)
3371
0
{
3372
  /* Allocate the structure if it has not already been allocated by a
3373
     subclass.  */
3374
0
  if (entry == NULL)
3375
0
    {
3376
0
      entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3377
0
      if (entry == NULL)
3378
0
  return entry;
3379
0
    }
3380
3381
  /* Call the allocation method of the superclass.  */
3382
0
  entry = bfd_hash_newfunc (entry, table, string);
3383
0
  if (entry != NULL)
3384
0
    {
3385
0
      struct ppc_stub_hash_entry *eh;
3386
3387
      /* Initialize the local fields.  */
3388
0
      eh = (struct ppc_stub_hash_entry *) entry;
3389
0
      eh->type.main = ppc_stub_none;
3390
0
      eh->type.sub = ppc_stub_toc;
3391
0
      eh->type.r2save = 0;
3392
0
      eh->group = NULL;
3393
0
      eh->stub_offset = 0;
3394
0
      eh->target_value = 0;
3395
0
      eh->target_section = NULL;
3396
0
      eh->h = NULL;
3397
0
      eh->plt_ent = NULL;
3398
0
      eh->symtype = 0;
3399
0
      eh->other = 0;
3400
0
      eh->id = 0;
3401
0
    }
3402
3403
0
  return entry;
3404
0
}
3405
3406
/* Create an entry in the branch hash table.  */
3407
3408
static struct bfd_hash_entry *
3409
branch_hash_newfunc (struct bfd_hash_entry *entry,
3410
         struct bfd_hash_table *table,
3411
         const char *string)
3412
0
{
3413
  /* Allocate the structure if it has not already been allocated by a
3414
     subclass.  */
3415
0
  if (entry == NULL)
3416
0
    {
3417
0
      entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3418
0
      if (entry == NULL)
3419
0
  return entry;
3420
0
    }
3421
3422
  /* Call the allocation method of the superclass.  */
3423
0
  entry = bfd_hash_newfunc (entry, table, string);
3424
0
  if (entry != NULL)
3425
0
    {
3426
0
      struct ppc_branch_hash_entry *eh;
3427
3428
      /* Initialize the local fields.  */
3429
0
      eh = (struct ppc_branch_hash_entry *) entry;
3430
0
      eh->offset = 0;
3431
0
      eh->iter = 0;
3432
0
    }
3433
3434
0
  return entry;
3435
0
}
3436
3437
/* Create an entry in a ppc64 ELF linker hash table.  */
3438
3439
static struct bfd_hash_entry *
3440
link_hash_newfunc (struct bfd_hash_entry *entry,
3441
       struct bfd_hash_table *table,
3442
       const char *string)
3443
0
{
3444
  /* Allocate the structure if it has not already been allocated by a
3445
     subclass.  */
3446
0
  if (entry == NULL)
3447
0
    {
3448
0
      entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3449
0
      if (entry == NULL)
3450
0
  return entry;
3451
0
    }
3452
3453
  /* Call the allocation method of the superclass.  */
3454
0
  entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3455
0
  if (entry != NULL)
3456
0
    {
3457
0
      struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3458
3459
0
      memset (&eh->u.stub_cache, 0,
3460
0
        (sizeof (struct ppc_link_hash_entry)
3461
0
         - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3462
3463
      /* When making function calls, old ABI code references function entry
3464
   points (dot symbols), while new ABI code references the function
3465
   descriptor symbol.  We need to make any combination of reference and
3466
   definition work together, without breaking archive linking.
3467
3468
   For a defined function "foo" and an undefined call to "bar":
3469
   An old object defines "foo" and ".foo", references ".bar" (possibly
3470
   "bar" too).
3471
   A new object defines "foo" and references "bar".
3472
3473
   A new object thus has no problem with its undefined symbols being
3474
   satisfied by definitions in an old object.  On the other hand, the
3475
   old object won't have ".bar" satisfied by a new object.
3476
3477
   Keep a list of newly added dot-symbols.  */
3478
3479
0
      if (string[0] == '.')
3480
0
  {
3481
0
    struct ppc_link_hash_table *htab;
3482
3483
0
    htab = (struct ppc_link_hash_table *) table;
3484
0
    eh->u.next_dot_sym = htab->dot_syms;
3485
0
    htab->dot_syms = eh;
3486
0
  }
3487
0
    }
3488
3489
0
  return entry;
3490
0
}
3491
3492
struct tocsave_entry
3493
{
3494
  asection *sec;
3495
  bfd_vma offset;
3496
};
3497
3498
static hashval_t
3499
tocsave_htab_hash (const void *p)
3500
0
{
3501
0
  const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3502
0
  return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3503
0
}
3504
3505
static int
3506
tocsave_htab_eq (const void *p1, const void *p2)
3507
0
{
3508
0
  const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3509
0
  const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3510
0
  return e1->sec == e2->sec && e1->offset == e2->offset;
3511
0
}
3512
3513
/* Destroy a ppc64 ELF linker hash table.  */
3514
3515
static void
3516
ppc64_elf_link_hash_table_free (bfd *obfd)
3517
0
{
3518
0
  struct ppc_link_hash_table *htab;
3519
3520
0
  htab = (struct ppc_link_hash_table *) obfd->link.hash;
3521
0
  free (htab->relr);
3522
0
  if (htab->tocsave_htab)
3523
0
    htab_delete (htab->tocsave_htab);
3524
0
  bfd_hash_table_free (&htab->branch_hash_table);
3525
0
  bfd_hash_table_free (&htab->stub_hash_table);
3526
0
  _bfd_elf_link_hash_table_free (obfd);
3527
0
}
3528
3529
/* Create a ppc64 ELF linker hash table.  */
3530
3531
static struct bfd_link_hash_table *
3532
ppc64_elf_link_hash_table_create (bfd *abfd)
3533
0
{
3534
0
  struct ppc_link_hash_table *htab;
3535
0
  size_t amt = sizeof (struct ppc_link_hash_table);
3536
3537
0
  htab = bfd_zmalloc (amt);
3538
0
  if (htab == NULL)
3539
0
    return NULL;
3540
3541
0
  if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3542
0
              sizeof (struct ppc_link_hash_entry)))
3543
0
    {
3544
0
      free (htab);
3545
0
      return NULL;
3546
0
    }
3547
3548
  /* Init the stub hash table too.  */
3549
0
  if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3550
0
          sizeof (struct ppc_stub_hash_entry)))
3551
0
    {
3552
0
      _bfd_elf_link_hash_table_free (abfd);
3553
0
      return NULL;
3554
0
    }
3555
3556
  /* And the branch hash table.  */
3557
0
  if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3558
0
          sizeof (struct ppc_branch_hash_entry)))
3559
0
    {
3560
0
      bfd_hash_table_free (&htab->stub_hash_table);
3561
0
      _bfd_elf_link_hash_table_free (abfd);
3562
0
      return NULL;
3563
0
    }
3564
3565
0
  htab->tocsave_htab = htab_try_create (1024,
3566
0
          tocsave_htab_hash,
3567
0
          tocsave_htab_eq,
3568
0
          NULL);
3569
0
  if (htab->tocsave_htab == NULL)
3570
0
    {
3571
0
      ppc64_elf_link_hash_table_free (abfd);
3572
0
      return NULL;
3573
0
    }
3574
0
  htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3575
3576
  /* Initializing two fields of the union is just cosmetic.  We really
3577
     only care about glist, but when compiled on a 32-bit host the
3578
     bfd_vma fields are larger.  Setting the bfd_vma to zero makes
3579
     debugger inspection of these fields look nicer.  */
3580
0
  htab->elf.init_got_refcount.refcount = 0;
3581
0
  htab->elf.init_got_refcount.glist = NULL;
3582
0
  htab->elf.init_plt_refcount.refcount = 0;
3583
0
  htab->elf.init_plt_refcount.glist = NULL;
3584
0
  htab->elf.init_got_offset.offset = 0;
3585
0
  htab->elf.init_got_offset.glist = NULL;
3586
0
  htab->elf.init_plt_offset.offset = 0;
3587
0
  htab->elf.init_plt_offset.glist = NULL;
3588
3589
0
  return &htab->elf.root;
3590
0
}
3591
3592
/* Create sections for linker generated code.  */
3593
3594
static bool
3595
create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3596
0
{
3597
0
  struct ppc_link_hash_table *htab;
3598
0
  flagword flags;
3599
3600
0
  htab = ppc_hash_table (info);
3601
3602
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3603
0
     | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3604
0
  if (htab->params->save_restore_funcs)
3605
0
    {
3606
      /* Create .sfpr for code to save and restore fp regs.  */
3607
0
      htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3608
0
                   flags);
3609
0
      if (htab->sfpr == NULL
3610
0
    || !bfd_set_section_alignment (htab->sfpr, 2))
3611
0
  return false;
3612
0
    }
3613
3614
0
  if (bfd_link_relocatable (info))
3615
0
    return true;
3616
3617
  /* Create .glink for lazy dynamic linking support.  */
3618
0
  htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3619
0
                flags);
3620
0
  if (htab->glink == NULL
3621
0
      || !bfd_set_section_alignment (htab->glink, 3))
3622
0
    return false;
3623
3624
  /* The part of .glink used by global entry stubs, separate so that
3625
     it can be aligned appropriately without affecting htab->glink.  */
3626
0
  htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3627
0
                 flags);
3628
0
  if (htab->global_entry == NULL
3629
0
      || !bfd_set_section_alignment (htab->global_entry, 2))
3630
0
    return false;
3631
3632
0
  if (!info->no_ld_generated_unwind_info)
3633
0
    {
3634
0
      flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3635
0
         | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3636
0
      htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3637
0
                 ".eh_frame",
3638
0
                 flags);
3639
0
      if (htab->glink_eh_frame == NULL
3640
0
    || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
3641
0
  return false;
3642
0
    }
3643
3644
0
  flags = SEC_ALLOC | SEC_LINKER_CREATED;
3645
0
  htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3646
0
  if (htab->elf.iplt == NULL
3647
0
      || !bfd_set_section_alignment (htab->elf.iplt, 3))
3648
0
    return false;
3649
3650
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3651
0
     | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3652
0
  htab->elf.irelplt
3653
0
    = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3654
0
  if (htab->elf.irelplt == NULL
3655
0
      || !bfd_set_section_alignment (htab->elf.irelplt, 3))
3656
0
    return false;
3657
3658
  /* Create branch lookup table for plt_branch stubs.  */
3659
0
  flags = (SEC_ALLOC | SEC_LOAD
3660
0
     | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3661
0
  htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3662
0
               flags);
3663
0
  if (htab->brlt == NULL
3664
0
      || !bfd_set_section_alignment (htab->brlt, 3))
3665
0
    return false;
3666
3667
  /* Local plt entries, put in .branch_lt but a separate section for
3668
     convenience.  */
3669
0
  htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3670
0
                   flags);
3671
0
  if (htab->pltlocal == NULL
3672
0
      || !bfd_set_section_alignment (htab->pltlocal, 3))
3673
0
    return false;
3674
3675
0
  if (!bfd_link_pic (info))
3676
0
    return true;
3677
3678
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3679
0
     | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3680
0
  htab->relbrlt
3681
0
    = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3682
0
  if (htab->relbrlt == NULL
3683
0
      || !bfd_set_section_alignment (htab->relbrlt, 3))
3684
0
    return false;
3685
3686
0
  htab->relpltlocal
3687
0
    = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3688
0
  if (htab->relpltlocal == NULL
3689
0
      || !bfd_set_section_alignment (htab->relpltlocal, 3))
3690
0
    return false;
3691
3692
0
  return true;
3693
0
}
3694
3695
/* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
3696
3697
bool
3698
ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3699
       struct ppc64_elf_params *params)
3700
0
{
3701
0
  struct ppc_link_hash_table *htab;
3702
3703
0
  elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3704
3705
/* Always hook our dynamic sections into the first bfd, which is the
3706
   linker created stub bfd.  This ensures that the GOT header is at
3707
   the start of the output TOC section.  */
3708
0
  htab = ppc_hash_table (info);
3709
0
  htab->elf.dynobj = params->stub_bfd;
3710
0
  htab->params = params;
3711
3712
0
  return create_linkage_sections (htab->elf.dynobj, info);
3713
0
}
3714
3715
/* Build a name for an entry in the stub hash table.  */
3716
3717
static char *
3718
ppc_stub_name (const asection *input_section,
3719
         const asection *sym_sec,
3720
         const struct ppc_link_hash_entry *h,
3721
         const Elf_Internal_Rela *rel)
3722
0
{
3723
0
  char *stub_name;
3724
0
  ssize_t len;
3725
3726
  /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3727
     offsets from a sym as a branch target?  In fact, we could
3728
     probably assume the addend is always zero.  */
3729
0
  BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3730
3731
0
  if (h)
3732
0
    {
3733
0
      len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3734
0
      stub_name = bfd_malloc (len);
3735
0
      if (stub_name == NULL)
3736
0
  return stub_name;
3737
3738
0
      len = sprintf (stub_name, "%08x.%s+%x",
3739
0
         input_section->id & 0xffffffff,
3740
0
         h->elf.root.root.string,
3741
0
         (int) rel->r_addend & 0xffffffff);
3742
0
    }
3743
0
  else
3744
0
    {
3745
0
      len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3746
0
      stub_name = bfd_malloc (len);
3747
0
      if (stub_name == NULL)
3748
0
  return stub_name;
3749
3750
0
      len = sprintf (stub_name, "%08x.%x:%x+%x",
3751
0
         input_section->id & 0xffffffff,
3752
0
         sym_sec->id & 0xffffffff,
3753
0
         (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3754
0
         (int) rel->r_addend & 0xffffffff);
3755
0
    }
3756
0
  if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3757
0
    stub_name[len - 2] = 0;
3758
0
  return stub_name;
3759
0
}
3760
3761
/* If mixing power10 with non-power10 code and --power10-stubs is not
3762
   specified (or is auto) then there may be multiple stub types for any
3763
   given symbol.  Up to three classes of stubs are stored in separate
3764
   stub_hash_table entries having the same key string.  The entries
3765
   will always be adjacent on entry->root.next chain, even if hash
3766
   table resizing occurs.  This function selects the correct entry to
3767
   use.  */
3768
3769
static struct ppc_stub_hash_entry *
3770
select_alt_stub (struct ppc_stub_hash_entry *entry,
3771
     enum elf_ppc64_reloc_type r_type)
3772
0
{
3773
0
  enum ppc_stub_sub_type subt;
3774
3775
0
  switch (r_type)
3776
0
    {
3777
0
    case R_PPC64_REL24_NOTOC:
3778
0
      subt = ppc_stub_notoc;
3779
0
      break;
3780
0
    case R_PPC64_REL24_P9NOTOC:
3781
0
      subt = ppc_stub_p9notoc;
3782
0
      break;
3783
0
    default:
3784
0
      subt = ppc_stub_toc;
3785
0
      break;
3786
0
    }
3787
3788
0
  while (entry != NULL && entry->type.sub != subt)
3789
0
    {
3790
0
      const char *stub_name = entry->root.string;
3791
3792
0
      entry = (struct ppc_stub_hash_entry *) entry->root.next;
3793
0
      if (entry != NULL
3794
0
    && entry->root.string != stub_name)
3795
0
  entry = NULL;
3796
0
    }
3797
3798
0
  return entry;
3799
0
}
3800
3801
/* Look up an entry in the stub hash.  Stub entries are cached because
3802
   creating the stub name takes a bit of time.  */
3803
3804
static struct ppc_stub_hash_entry *
3805
ppc_get_stub_entry (const asection *input_section,
3806
        const asection *sym_sec,
3807
        struct ppc_link_hash_entry *h,
3808
        const Elf_Internal_Rela *rel,
3809
        struct ppc_link_hash_table *htab)
3810
0
{
3811
0
  struct ppc_stub_hash_entry *stub_entry;
3812
0
  struct map_stub *group;
3813
3814
  /* If this input section is part of a group of sections sharing one
3815
     stub section, then use the id of the first section in the group.
3816
     Stub names need to include a section id, as there may well be
3817
     more than one stub used to reach say, printf, and we need to
3818
     distinguish between them.  */
3819
0
  group = htab->sec_info[input_section->id].u.group;
3820
0
  if (group == NULL)
3821
0
    return NULL;
3822
3823
0
  if (h != NULL && h->u.stub_cache != NULL
3824
0
      && h->u.stub_cache->h == h
3825
0
      && h->u.stub_cache->group == group)
3826
0
    {
3827
0
      stub_entry = h->u.stub_cache;
3828
0
    }
3829
0
  else
3830
0
    {
3831
0
      char *stub_name;
3832
3833
0
      stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3834
0
      if (stub_name == NULL)
3835
0
  return NULL;
3836
3837
0
      stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3838
0
           stub_name, false, false);
3839
0
      if (h != NULL)
3840
0
  h->u.stub_cache = stub_entry;
3841
3842
0
      free (stub_name);
3843
0
    }
3844
3845
0
  if (stub_entry != NULL && htab->params->power10_stubs == -1)
3846
0
    stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
3847
3848
0
  return stub_entry;
3849
0
}
3850
3851
/* Add a new stub entry to the stub hash.  Not all fields of the new
3852
   stub entry are initialised.  */
3853
3854
static struct ppc_stub_hash_entry *
3855
ppc_add_stub (const char *stub_name,
3856
        asection *section,
3857
        struct bfd_link_info *info)
3858
0
{
3859
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
3860
0
  struct map_stub *group;
3861
0
  asection *link_sec;
3862
0
  asection *stub_sec;
3863
0
  struct ppc_stub_hash_entry *stub_entry;
3864
3865
0
  group = htab->sec_info[section->id].u.group;
3866
0
  link_sec = group->link_sec;
3867
0
  stub_sec = group->stub_sec;
3868
0
  if (stub_sec == NULL)
3869
0
    {
3870
0
      size_t namelen;
3871
0
      bfd_size_type len;
3872
0
      char *s_name;
3873
3874
0
      namelen = strlen (link_sec->name);
3875
0
      len = namelen + sizeof (STUB_SUFFIX);
3876
0
      s_name = bfd_alloc (htab->params->stub_bfd, len);
3877
0
      if (s_name == NULL)
3878
0
  return NULL;
3879
3880
0
      memcpy (s_name, link_sec->name, namelen);
3881
0
      memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3882
0
      stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3883
0
      if (stub_sec == NULL)
3884
0
  return NULL;
3885
0
      group->stub_sec = stub_sec;
3886
0
    }
3887
3888
  /* Enter this entry into the linker stub hash table.  */
3889
0
  stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3890
0
             true, true);
3891
0
  if (stub_entry == NULL)
3892
0
    {
3893
      /* xgettext:c-format */
3894
0
      _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3895
0
        section->owner, stub_name);
3896
0
      return NULL;
3897
0
    }
3898
3899
0
  stub_entry->group = group;
3900
0
  stub_entry->stub_offset = 0;
3901
0
  return stub_entry;
3902
0
}
3903
3904
/* A stub has already been created, but it may not be the required
3905
   type.  We shouldn't be transitioning from plt_call to long_branch
3906
   stubs or vice versa, but we might be upgrading from plt_call to
3907
   plt_call with r2save for example.  */
3908
3909
static bool
3910
ppc_merge_stub (struct ppc_link_hash_table *htab,
3911
    struct ppc_stub_hash_entry *stub_entry,
3912
    struct ppc_stub_type stub_type,
3913
    enum elf_ppc64_reloc_type r_type)
3914
0
{
3915
0
  struct ppc_stub_type old_type = stub_entry->type;
3916
3917
0
  if (old_type.main == ppc_stub_save_res)
3918
0
    return true;
3919
3920
0
  if (htab->params->power10_stubs == -1)
3921
0
    {
3922
      /* For --power10-stubs=auto, don't merge _notoc and other
3923
   varieties of stubs.  */
3924
0
      struct ppc_stub_hash_entry *alt_stub;
3925
3926
0
      alt_stub = select_alt_stub (stub_entry, r_type);
3927
0
      if (alt_stub == NULL)
3928
0
  {
3929
0
    alt_stub = ((struct ppc_stub_hash_entry *)
3930
0
          stub_hash_newfunc (NULL,
3931
0
           &htab->stub_hash_table,
3932
0
           stub_entry->root.string));
3933
0
    if (alt_stub == NULL)
3934
0
      return false;
3935
3936
0
    *alt_stub = *stub_entry;
3937
0
    stub_entry->root.next = &alt_stub->root;
3938
3939
    /* Sort notoc stubs first, then toc stubs, then p9notoc.
3940
       Not that it matters, this just puts smaller stubs first.  */
3941
0
    if (stub_type.sub == ppc_stub_notoc)
3942
0
      alt_stub = stub_entry;
3943
0
    else if (stub_type.sub == ppc_stub_p9notoc
3944
0
       && alt_stub->root.next
3945
0
       && alt_stub->root.next->string == alt_stub->root.string)
3946
0
      {
3947
0
        struct ppc_stub_hash_entry *next
3948
0
    = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3949
0
        alt_stub->type = next->type;
3950
0
        alt_stub = next;
3951
0
      }
3952
0
    alt_stub->type = stub_type;
3953
0
    return true;
3954
0
  }
3955
0
      stub_entry = alt_stub;
3956
0
    }
3957
3958
0
  old_type = stub_entry->type;
3959
0
  if (old_type.main == ppc_stub_plt_branch)
3960
0
    old_type.main = ppc_stub_long_branch;
3961
3962
0
  if (old_type.main != stub_type.main
3963
0
      || (old_type.sub != stub_type.sub
3964
0
    && old_type.sub != ppc_stub_toc
3965
0
    && stub_type.sub != ppc_stub_toc))
3966
0
    abort ();
3967
3968
0
  stub_entry->type.sub |= stub_type.sub;
3969
0
  stub_entry->type.r2save |= stub_type.r2save;
3970
0
  return true;
3971
0
}
3972
3973
/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3974
   not already done.  */
3975
3976
static bool
3977
create_got_section (bfd *abfd, struct bfd_link_info *info)
3978
0
{
3979
0
  asection *got, *relgot;
3980
0
  flagword flags;
3981
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
3982
3983
0
  if (!is_ppc64_elf (abfd))
3984
0
    return false;
3985
0
  if (htab == NULL)
3986
0
    return false;
3987
3988
0
  if (!htab->elf.sgot
3989
0
      && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3990
0
    return false;
3991
3992
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3993
0
     | SEC_LINKER_CREATED);
3994
3995
0
  got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3996
0
  if (!got
3997
0
      || !bfd_set_section_alignment (got, 3))
3998
0
    return false;
3999
4000
0
  relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4001
0
                 flags | SEC_READONLY);
4002
0
  if (!relgot
4003
0
      || !bfd_set_section_alignment (relgot, 3))
4004
0
    return false;
4005
4006
0
  ppc64_elf_tdata (abfd)->got = got;
4007
0
  ppc64_elf_tdata (abfd)->relgot = relgot;
4008
0
  return true;
4009
0
}
4010
4011
/* Follow indirect and warning symbol links.  */
4012
4013
static inline struct bfd_link_hash_entry *
4014
follow_link (struct bfd_link_hash_entry *h)
4015
0
{
4016
0
  while (h->type == bfd_link_hash_indirect
4017
0
   || h->type == bfd_link_hash_warning)
4018
0
    h = h->u.i.link;
4019
0
  return h;
4020
0
}
4021
4022
static inline struct elf_link_hash_entry *
4023
elf_follow_link (struct elf_link_hash_entry *h)
4024
0
{
4025
0
  return (struct elf_link_hash_entry *) follow_link (&h->root);
4026
0
}
4027
4028
static inline struct ppc_link_hash_entry *
4029
ppc_follow_link (struct ppc_link_hash_entry *h)
4030
0
{
4031
0
  return ppc_elf_hash_entry (elf_follow_link (&h->elf));
4032
0
}
4033
4034
/* Merge PLT info on FROM with that on TO.  */
4035
4036
static void
4037
move_plt_plist (struct ppc_link_hash_entry *from,
4038
    struct ppc_link_hash_entry *to)
4039
0
{
4040
0
  if (from->elf.plt.plist != NULL)
4041
0
    {
4042
0
      if (to->elf.plt.plist != NULL)
4043
0
  {
4044
0
    struct plt_entry **entp;
4045
0
    struct plt_entry *ent;
4046
4047
0
    for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4048
0
      {
4049
0
        struct plt_entry *dent;
4050
4051
0
        for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4052
0
    if (dent->addend == ent->addend)
4053
0
      {
4054
0
        dent->plt.refcount += ent->plt.refcount;
4055
0
        *entp = ent->next;
4056
0
        break;
4057
0
      }
4058
0
        if (dent == NULL)
4059
0
    entp = &ent->next;
4060
0
      }
4061
0
    *entp = to->elf.plt.plist;
4062
0
  }
4063
4064
0
      to->elf.plt.plist = from->elf.plt.plist;
4065
0
      from->elf.plt.plist = NULL;
4066
0
    }
4067
0
}
4068
4069
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
4070
4071
static void
4072
ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4073
        struct elf_link_hash_entry *dir,
4074
        struct elf_link_hash_entry *ind)
4075
0
{
4076
0
  struct ppc_link_hash_entry *edir, *eind;
4077
4078
0
  edir = ppc_elf_hash_entry (dir);
4079
0
  eind = ppc_elf_hash_entry (ind);
4080
4081
0
  edir->is_func |= eind->is_func;
4082
0
  edir->is_func_descriptor |= eind->is_func_descriptor;
4083
0
  edir->tls_mask |= eind->tls_mask;
4084
0
  if (eind->oh != NULL)
4085
0
    edir->oh = ppc_follow_link (eind->oh);
4086
4087
0
  if (edir->elf.versioned != versioned_hidden)
4088
0
    edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4089
0
  edir->elf.ref_regular |= eind->elf.ref_regular;
4090
0
  edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4091
0
  edir->elf.non_got_ref |= eind->elf.non_got_ref;
4092
0
  edir->elf.needs_plt |= eind->elf.needs_plt;
4093
0
  edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4094
4095
  /* If we were called to copy over info for a weak sym, don't copy
4096
     dyn_relocs, plt/got info, or dynindx.  We used to copy dyn_relocs
4097
     in order to simplify readonly_dynrelocs and save a field in the
4098
     symbol hash entry, but that means dyn_relocs can't be used in any
4099
     tests about a specific symbol, or affect other symbol flags which
4100
     are then tested.  */
4101
0
  if (eind->elf.root.type != bfd_link_hash_indirect)
4102
0
    return;
4103
4104
  /* Copy over any dynamic relocs we may have on the indirect sym.  */
4105
0
  if (ind->dyn_relocs != NULL)
4106
0
    {
4107
0
      if (dir->dyn_relocs != NULL)
4108
0
  {
4109
0
    struct ppc_dyn_relocs **pp;
4110
0
    struct ppc_dyn_relocs *p;
4111
4112
    /* Add reloc counts against the indirect sym to the direct sym
4113
       list.  Merge any entries against the same section.  */
4114
0
    for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
4115
0
         (p = *pp) != NULL;
4116
0
         )
4117
0
      {
4118
0
        struct ppc_dyn_relocs *q;
4119
4120
0
        for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4121
0
       q != NULL;
4122
0
       q = q->next)
4123
0
    if (q->sec == p->sec)
4124
0
      {
4125
0
        q->count += p->count;
4126
0
        q->pc_count += p->pc_count;
4127
0
        q->rel_count += p->rel_count;
4128
0
        *pp = p->next;
4129
0
        break;
4130
0
      }
4131
0
        if (q == NULL)
4132
0
    pp = &p->next;
4133
0
      }
4134
0
    *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4135
0
  }
4136
4137
0
      dir->dyn_relocs = ind->dyn_relocs;
4138
0
      ind->dyn_relocs = NULL;
4139
0
    }
4140
4141
  /* Copy over got entries that we may have already seen to the
4142
     symbol which just became indirect.  */
4143
0
  if (eind->elf.got.glist != NULL)
4144
0
    {
4145
0
      if (edir->elf.got.glist != NULL)
4146
0
  {
4147
0
    struct got_entry **entp;
4148
0
    struct got_entry *ent;
4149
4150
0
    for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4151
0
      {
4152
0
        struct got_entry *dent;
4153
4154
0
        for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4155
0
    if (dent->addend == ent->addend
4156
0
        && dent->owner == ent->owner
4157
0
        && dent->tls_type == ent->tls_type)
4158
0
      {
4159
0
        dent->got.refcount += ent->got.refcount;
4160
0
        *entp = ent->next;
4161
0
        break;
4162
0
      }
4163
0
        if (dent == NULL)
4164
0
    entp = &ent->next;
4165
0
      }
4166
0
    *entp = edir->elf.got.glist;
4167
0
  }
4168
4169
0
      edir->elf.got.glist = eind->elf.got.glist;
4170
0
      eind->elf.got.glist = NULL;
4171
0
    }
4172
4173
  /* And plt entries.  */
4174
0
  move_plt_plist (eind, edir);
4175
4176
0
  if (eind->elf.dynindx != -1)
4177
0
    {
4178
0
      if (edir->elf.dynindx != -1)
4179
0
  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4180
0
        edir->elf.dynstr_index);
4181
0
      edir->elf.dynindx = eind->elf.dynindx;
4182
0
      edir->elf.dynstr_index = eind->elf.dynstr_index;
4183
0
      eind->elf.dynindx = -1;
4184
0
      eind->elf.dynstr_index = 0;
4185
0
    }
4186
0
}
4187
4188
/* Find the function descriptor hash entry from the given function code
4189
   hash entry FH.  Link the entries via their OH fields.  */
4190
4191
static struct ppc_link_hash_entry *
4192
lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4193
0
{
4194
0
  struct ppc_link_hash_entry *fdh = fh->oh;
4195
4196
0
  if (fdh == NULL)
4197
0
    {
4198
0
      const char *fd_name = fh->elf.root.root.string + 1;
4199
4200
0
      fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4201
0
                  false, false, false));
4202
0
      if (fdh == NULL)
4203
0
  return fdh;
4204
4205
0
      fdh->is_func_descriptor = 1;
4206
0
      fdh->oh = fh;
4207
0
      fh->is_func = 1;
4208
0
      fh->oh = fdh;
4209
0
    }
4210
4211
0
  fdh = ppc_follow_link (fdh);
4212
0
  fdh->is_func_descriptor = 1;
4213
0
  fdh->oh = fh;
4214
0
  return fdh;
4215
0
}
4216
4217
/* Make a fake function descriptor sym for the undefined code sym FH.  */
4218
4219
static struct ppc_link_hash_entry *
4220
make_fdh (struct bfd_link_info *info,
4221
    struct ppc_link_hash_entry *fh)
4222
0
{
4223
0
  bfd *abfd = fh->elf.root.u.undef.abfd;
4224
0
  struct bfd_link_hash_entry *bh = NULL;
4225
0
  struct ppc_link_hash_entry *fdh;
4226
0
  flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4227
0
        ? BSF_WEAK
4228
0
        : BSF_GLOBAL);
4229
4230
0
  if (!_bfd_generic_link_add_one_symbol (info, abfd,
4231
0
           fh->elf.root.root.string + 1,
4232
0
           flags, bfd_und_section_ptr, 0,
4233
0
           NULL, false, false, &bh))
4234
0
    return NULL;
4235
4236
0
  fdh = (struct ppc_link_hash_entry *) bh;
4237
0
  fdh->elf.non_elf = 0;
4238
0
  fdh->fake = 1;
4239
0
  fdh->is_func_descriptor = 1;
4240
0
  fdh->oh = fh;
4241
0
  fh->is_func = 1;
4242
0
  fh->oh = fdh;
4243
0
  return fdh;
4244
0
}
4245
4246
/* Fix function descriptor symbols defined in .opd sections to be
4247
   function type.  */
4248
4249
static bool
4250
ppc64_elf_add_symbol_hook (bfd *ibfd,
4251
         struct bfd_link_info *info,
4252
         Elf_Internal_Sym *isym,
4253
         const char **name,
4254
         flagword *flags ATTRIBUTE_UNUSED,
4255
         asection **sec,
4256
         bfd_vma *value)
4257
0
{
4258
0
  if (*sec != NULL
4259
0
      && strcmp ((*sec)->name, ".opd") == 0)
4260
0
    {
4261
0
      asection *code_sec;
4262
4263
0
      if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4264
0
      || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4265
0
  isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4266
4267
      /* If the symbol is a function defined in .opd, and the function
4268
   code is in a discarded group, let it appear to be undefined.  */
4269
0
      if (!bfd_link_relocatable (info)
4270
0
    && (*sec)->reloc_count != 0
4271
0
    && opd_entry_value (*sec, *value, &code_sec, NULL,
4272
0
            false) != (bfd_vma) -1
4273
0
    && discarded_section (code_sec))
4274
0
  {
4275
0
    *sec = bfd_und_section_ptr;
4276
0
    isym->st_shndx = SHN_UNDEF;
4277
0
  }
4278
0
    }
4279
0
  else if (*sec != NULL
4280
0
     && strcmp ((*sec)->name, ".toc") == 0
4281
0
     && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4282
0
    {
4283
0
      struct ppc_link_hash_table *htab = ppc_hash_table (info);
4284
0
      if (htab != NULL)
4285
0
  htab->params->object_in_toc = 1;
4286
0
    }
4287
4288
0
  if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4289
0
    {
4290
0
      if (abiversion (ibfd) == 0)
4291
0
  set_abiversion (ibfd, 2);
4292
0
      else if (abiversion (ibfd) == 1)
4293
0
  {
4294
0
    _bfd_error_handler (_("symbol '%s' has invalid st_other"
4295
0
        " for ABI version 1"), *name);
4296
0
    bfd_set_error (bfd_error_bad_value);
4297
0
    return false;
4298
0
  }
4299
0
    }
4300
4301
0
  return true;
4302
0
}
4303
4304
/* Merge non-visibility st_other attributes: local entry point.  */
4305
4306
static void
4307
ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4308
          unsigned int st_other,
4309
          bool definition,
4310
          bool dynamic)
4311
0
{
4312
0
  if (definition && (!dynamic || !h->def_regular))
4313
0
    h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
4314
0
    | ELF_ST_VISIBILITY (h->other));
4315
0
}
4316
4317
/* Hook called on merging a symbol.  We use this to clear "fake" since
4318
   we now have a real symbol.  */
4319
4320
static bool
4321
ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4322
      const Elf_Internal_Sym *isym,
4323
      asection **psec ATTRIBUTE_UNUSED,
4324
      bool newdef ATTRIBUTE_UNUSED,
4325
      bool olddef ATTRIBUTE_UNUSED,
4326
      bfd *oldbfd ATTRIBUTE_UNUSED,
4327
      const asection *oldsec ATTRIBUTE_UNUSED)
4328
0
{
4329
0
  ppc_elf_hash_entry (h)->fake = 0;
4330
0
  if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4331
0
    ppc_elf_hash_entry (h)->non_zero_localentry = 1;
4332
0
  return true;
4333
0
}
4334
4335
/* This function makes an old ABI object reference to ".bar" cause the
4336
   inclusion of a new ABI object archive that defines "bar".
4337
   NAME is a symbol defined in an archive.  Return a symbol in the hash
4338
   table that might be satisfied by the archive symbols.  */
4339
4340
static struct bfd_link_hash_entry *
4341
ppc64_elf_archive_symbol_lookup (bfd *abfd,
4342
         struct bfd_link_info *info,
4343
         const char *name)
4344
0
{
4345
0
  struct bfd_link_hash_entry *h;
4346
0
  char *dot_name;
4347
0
  size_t len;
4348
4349
0
  h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4350
0
  if (h != NULL
4351
0
      && ppc_hash_table (info) != NULL
4352
      /* Don't return this sym if it is a fake function descriptor
4353
   created by add_symbol_adjust.  */
4354
0
      && !((struct ppc_link_hash_entry *) h)->fake)
4355
0
    return h;
4356
4357
0
  if (name[0] == '.')
4358
0
    return h;
4359
4360
0
  len = strlen (name);
4361
0
  dot_name = bfd_alloc (abfd, len + 2);
4362
0
  if (dot_name == NULL)
4363
0
    return (struct bfd_link_hash_entry *) -1;
4364
0
  dot_name[0] = '.';
4365
0
  memcpy (dot_name + 1, name, len + 1);
4366
0
  h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4367
0
  bfd_release (abfd, dot_name);
4368
0
  if (h != NULL)
4369
0
    return h;
4370
4371
0
  if (strcmp (name, "__tls_get_addr_opt") == 0)
4372
0
    h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
4373
0
  return h;
4374
0
}
4375
4376
/* This function satisfies all old ABI object references to ".bar" if a
4377
   new ABI object defines "bar".  Well, at least, undefined dot symbols
4378
   are made weak.  This stops later archive searches from including an
4379
   object if we already have a function descriptor definition.  It also
4380
   prevents the linker complaining about undefined symbols.
4381
   We also check and correct mismatched symbol visibility here.  The
4382
   most restrictive visibility of the function descriptor and the
4383
   function entry symbol is used.  */
4384
4385
static bool
4386
add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4387
0
{
4388
0
  struct ppc_link_hash_table *htab;
4389
0
  struct ppc_link_hash_entry *fdh;
4390
4391
0
  if (eh->elf.root.type == bfd_link_hash_warning)
4392
0
    eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4393
4394
0
  if (eh->elf.root.type == bfd_link_hash_indirect)
4395
0
    return true;
4396
4397
0
  if (eh->elf.root.root.string[0] != '.')
4398
0
    abort ();
4399
4400
0
  htab = ppc_hash_table (info);
4401
0
  if (htab == NULL)
4402
0
    return false;
4403
4404
0
  fdh = lookup_fdh (eh, htab);
4405
0
  if (fdh == NULL
4406
0
      && !bfd_link_relocatable (info)
4407
0
      && (eh->elf.root.type == bfd_link_hash_undefined
4408
0
    || eh->elf.root.type == bfd_link_hash_undefweak)
4409
0
      && eh->elf.ref_regular)
4410
0
    {
4411
      /* Make an undefined function descriptor sym, in order to
4412
   pull in an --as-needed shared lib.  Archives are handled
4413
   elsewhere.  */
4414
0
      fdh = make_fdh (info, eh);
4415
0
      if (fdh == NULL)
4416
0
  return false;
4417
0
    }
4418
4419
0
  if (fdh != NULL)
4420
0
    {
4421
0
      unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4422
0
      unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4423
4424
      /* Make both descriptor and entry symbol have the most
4425
   constraining visibility of either symbol.  */
4426
0
      if (entry_vis < descr_vis)
4427
0
  fdh->elf.other += entry_vis - descr_vis;
4428
0
      else if (entry_vis > descr_vis)
4429
0
  eh->elf.other += descr_vis - entry_vis;
4430
4431
      /* Propagate reference flags from entry symbol to function
4432
   descriptor symbol.  */
4433
0
      fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4434
0
      fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4435
0
      fdh->elf.ref_regular |= eh->elf.ref_regular;
4436
0
      fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4437
4438
0
      if (!fdh->elf.forced_local
4439
0
    && fdh->elf.dynindx == -1
4440
0
    && fdh->elf.versioned != versioned_hidden
4441
0
    && (bfd_link_dll (info)
4442
0
        || fdh->elf.def_dynamic
4443
0
        || fdh->elf.ref_dynamic)
4444
0
    && (eh->elf.ref_regular
4445
0
        || eh->elf.def_regular))
4446
0
  {
4447
0
    if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4448
0
      return false;
4449
0
  }
4450
0
    }
4451
4452
0
  return true;
4453
0
}
4454
4455
/* Set up opd section info and abiversion for IBFD, and process list
4456
   of dot-symbols we made in link_hash_newfunc.  */
4457
4458
static bool
4459
ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4460
0
{
4461
0
  struct ppc_link_hash_table *htab;
4462
0
  struct ppc_link_hash_entry **p, *eh;
4463
0
  asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4464
4465
0
  if (opd != NULL && opd->size != 0)
4466
0
    {
4467
0
      if (ppc64_elf_section_data (opd)->sec_type == sec_normal)
4468
0
  ppc64_elf_section_data (opd)->sec_type = sec_opd;
4469
0
      else if (ppc64_elf_section_data (opd)->sec_type != sec_opd)
4470
0
  BFD_FAIL ();
4471
4472
0
      if (abiversion (ibfd) == 0)
4473
0
  set_abiversion (ibfd, 1);
4474
0
      else if (abiversion (ibfd) >= 2)
4475
0
  {
4476
    /* xgettext:c-format */
4477
0
    _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4478
0
            ibfd, abiversion (ibfd));
4479
0
    bfd_set_error (bfd_error_bad_value);
4480
0
    return false;
4481
0
  }
4482
0
    }
4483
4484
0
  if (is_ppc64_elf (info->output_bfd))
4485
0
    {
4486
      /* For input files without an explicit abiversion in e_flags
4487
   we should have flagged any with symbol st_other bits set
4488
   as ELFv2 and above flagged those with .opd as ELFv1.
4489
   Set the output abiversion if not yet set, and for any input
4490
   still ambiguous, take its abiversion from the output.
4491
   Differences in ABI are reported later.  */
4492
0
      if (abiversion (info->output_bfd) == 0)
4493
0
  set_abiversion (info->output_bfd, abiversion (ibfd));
4494
0
      else if (abiversion (ibfd) == 0)
4495
0
  set_abiversion (ibfd, abiversion (info->output_bfd));
4496
0
    }
4497
4498
0
  htab = ppc_hash_table (info);
4499
0
  if (htab == NULL)
4500
0
    return true;
4501
4502
0
  if (opd != NULL && opd->size != 0
4503
0
      && (ibfd->flags & DYNAMIC) == 0
4504
0
      && (opd->flags & SEC_RELOC) != 0
4505
0
      && opd->reloc_count != 0
4506
0
      && !bfd_is_abs_section (opd->output_section)
4507
0
      && info->gc_sections)
4508
0
    {
4509
      /* Garbage collection needs some extra help with .opd sections.
4510
   We don't want to necessarily keep everything referenced by
4511
   relocs in .opd, as that would keep all functions.  Instead,
4512
   if we reference an .opd symbol (a function descriptor), we
4513
   want to keep the function code symbol's section.  This is
4514
   easy for global symbols, but for local syms we need to keep
4515
   information about the associated function section.  */
4516
0
      bfd_size_type amt;
4517
0
      asection **opd_sym_map;
4518
0
      Elf_Internal_Shdr *symtab_hdr;
4519
0
      Elf_Internal_Rela *relocs, *rel_end, *rel;
4520
4521
0
      amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4522
0
      opd_sym_map = bfd_zalloc (ibfd, amt);
4523
0
      if (opd_sym_map == NULL)
4524
0
  return false;
4525
0
      ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4526
0
      relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4527
0
            info->keep_memory);
4528
0
      if (relocs == NULL)
4529
0
  return false;
4530
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
4531
0
      rel_end = relocs + opd->reloc_count - 1;
4532
0
      for (rel = relocs; rel < rel_end; rel++)
4533
0
  {
4534
0
    enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4535
0
    unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4536
4537
0
    if (r_type == R_PPC64_ADDR64
4538
0
        && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4539
0
        && r_symndx < symtab_hdr->sh_info)
4540
0
      {
4541
0
        Elf_Internal_Sym *isym;
4542
0
        asection *s;
4543
4544
0
        isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4545
0
              r_symndx);
4546
0
        if (isym == NULL)
4547
0
    {
4548
0
      if (elf_section_data (opd)->relocs != relocs)
4549
0
        free (relocs);
4550
0
      return false;
4551
0
    }
4552
4553
0
        s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4554
0
        if (s != NULL && s != opd)
4555
0
    opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4556
0
      }
4557
0
  }
4558
0
      if (elf_section_data (opd)->relocs != relocs)
4559
0
  free (relocs);
4560
0
    }
4561
4562
0
  p = &htab->dot_syms;
4563
0
  while ((eh = *p) != NULL)
4564
0
    {
4565
0
      *p = NULL;
4566
0
      if (&eh->elf == htab->elf.hgot)
4567
0
  ;
4568
0
      else if (htab->elf.hgot == NULL
4569
0
         && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4570
0
  htab->elf.hgot = &eh->elf;
4571
0
      else if (abiversion (ibfd) <= 1)
4572
0
  {
4573
0
    htab->need_func_desc_adj = 1;
4574
0
    if (!add_symbol_adjust (eh, info))
4575
0
      return false;
4576
0
  }
4577
0
      p = &eh->u.next_dot_sym;
4578
0
    }
4579
0
  return true;
4580
0
}
4581
4582
/* Undo hash table changes when an --as-needed input file is determined
4583
   not to be needed.  */
4584
4585
static bool
4586
ppc64_elf_notice_as_needed (bfd *ibfd,
4587
          struct bfd_link_info *info,
4588
          enum notice_asneeded_action act)
4589
0
{
4590
0
  if (act == notice_not_needed)
4591
0
    {
4592
0
      struct ppc_link_hash_table *htab = ppc_hash_table (info);
4593
4594
0
      if (htab == NULL)
4595
0
  return false;
4596
4597
0
      htab->dot_syms = NULL;
4598
0
    }
4599
0
  return _bfd_elf_notice_as_needed (ibfd, info, act);
4600
0
}
4601
4602
/* If --just-symbols against a final linked binary, then assume we need
4603
   toc adjusting stubs when calling functions defined there.  */
4604
4605
static void
4606
ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4607
0
{
4608
0
  if ((sec->flags & SEC_CODE) != 0
4609
0
      && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4610
0
      && is_ppc64_elf (sec->owner))
4611
0
    {
4612
0
      if (abiversion (sec->owner) >= 2
4613
0
    || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4614
0
  sec->has_toc_reloc = 1;
4615
0
    }
4616
0
  _bfd_elf_link_just_syms (sec, info);
4617
0
}
4618
4619
static struct plt_entry **
4620
update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4621
           unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4622
0
{
4623
0
  struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4624
0
  struct plt_entry **local_plt;
4625
0
  unsigned char *local_got_tls_masks;
4626
4627
0
  if (local_got_ents == NULL)
4628
0
    {
4629
0
      bfd_size_type size = symtab_hdr->sh_info;
4630
4631
0
      size *= (sizeof (*local_got_ents)
4632
0
         + sizeof (*local_plt)
4633
0
         + sizeof (*local_got_tls_masks));
4634
0
      local_got_ents = bfd_zalloc (abfd, size);
4635
0
      if (local_got_ents == NULL)
4636
0
  return NULL;
4637
0
      elf_local_got_ents (abfd) = local_got_ents;
4638
0
    }
4639
4640
0
  if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4641
0
    {
4642
0
      struct got_entry *ent;
4643
4644
0
      for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4645
0
  if (ent->addend == r_addend
4646
0
      && ent->owner == abfd
4647
0
      && ent->tls_type == tls_type)
4648
0
    break;
4649
0
      if (ent == NULL)
4650
0
  {
4651
0
    size_t amt = sizeof (*ent);
4652
0
    ent = bfd_alloc (abfd, amt);
4653
0
    if (ent == NULL)
4654
0
      return NULL;
4655
0
    ent->next = local_got_ents[r_symndx];
4656
0
    ent->addend = r_addend;
4657
0
    ent->owner = abfd;
4658
0
    ent->tls_type = tls_type;
4659
0
    ent->is_indirect = false;
4660
0
    ent->got.refcount = 0;
4661
0
    local_got_ents[r_symndx] = ent;
4662
0
  }
4663
0
      ent->got.refcount += 1;
4664
0
    }
4665
4666
0
  local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4667
0
  local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4668
0
  local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4669
4670
0
  return local_plt + r_symndx;
4671
0
}
4672
4673
static bool
4674
update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4675
0
{
4676
0
  struct plt_entry *ent;
4677
4678
0
  for (ent = *plist; ent != NULL; ent = ent->next)
4679
0
    if (ent->addend == addend)
4680
0
      break;
4681
0
  if (ent == NULL)
4682
0
    {
4683
0
      size_t amt = sizeof (*ent);
4684
0
      ent = bfd_alloc (abfd, amt);
4685
0
      if (ent == NULL)
4686
0
  return false;
4687
0
      ent->next = *plist;
4688
0
      ent->addend = addend;
4689
0
      ent->plt.refcount = 0;
4690
0
      *plist = ent;
4691
0
    }
4692
0
  ent->plt.refcount += 1;
4693
0
  return true;
4694
0
}
4695
4696
static bool
4697
is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4698
0
{
4699
0
  return (r_type == R_PPC64_REL24
4700
0
    || r_type == R_PPC64_REL24_NOTOC
4701
0
    || r_type == R_PPC64_REL24_P9NOTOC
4702
0
    || r_type == R_PPC64_REL14
4703
0
    || r_type == R_PPC64_REL14_BRTAKEN
4704
0
    || r_type == R_PPC64_REL14_BRNTAKEN
4705
0
    || r_type == R_PPC64_ADDR24
4706
0
    || r_type == R_PPC64_ADDR14
4707
0
    || r_type == R_PPC64_ADDR14_BRTAKEN
4708
0
    || r_type == R_PPC64_ADDR14_BRNTAKEN
4709
0
    || r_type == R_PPC64_PLTCALL
4710
0
    || r_type == R_PPC64_PLTCALL_NOTOC);
4711
0
}
4712
4713
/* Relocs on inline plt call sequence insns prior to the call.  */
4714
4715
static bool
4716
is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4717
0
{
4718
0
  return (r_type == R_PPC64_PLT16_HA
4719
0
    || r_type == R_PPC64_PLT16_HI
4720
0
    || r_type == R_PPC64_PLT16_LO
4721
0
    || r_type == R_PPC64_PLT16_LO_DS
4722
0
    || r_type == R_PPC64_PLT_PCREL34
4723
0
    || r_type == R_PPC64_PLT_PCREL34_NOTOC
4724
0
    || r_type == R_PPC64_PLTSEQ
4725
0
    || r_type == R_PPC64_PLTSEQ_NOTOC);
4726
0
}
4727
4728
/* Of relocs which might appear paired with TLSGD and TLSLD marker
4729
   relocs, return true for those that operate on a dword.  */
4730
4731
static bool
4732
is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4733
0
{
4734
0
  return (r_type == R_PPC64_PLT_PCREL34
4735
0
    || r_type == R_PPC64_PLT_PCREL34_NOTOC
4736
0
    || r_type == R_PPC64_PLTCALL);
4737
0
}
4738
4739
/* The RELR encoding doesn't allow odd addresses, so RELR_ALIGN must
4740
   be at least 1.  R_PPC64_RELATIVE relocs require alignment of 2**3.
4741
   We use 3 here to avoid complexity in relocate_section, where for a
4742
   value of 1 we'd need to test for not just an output RELATIVE reloc
4743
   near the call to maybe_relr but also UADDR64 and some conditions on
4744
   the symbol.  See PR30824.  */
4745
0
#define RELR_ALIGN 3
4746
4747
static bool
4748
maybe_relr (enum elf_ppc64_reloc_type r_type,
4749
      const Elf_Internal_Rela *rel,
4750
      const asection *sec)
4751
0
{
4752
0
  return ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
4753
0
    && (rel->r_offset & ((1 << RELR_ALIGN) - 1)) == 0
4754
0
    && sec->alignment_power >= RELR_ALIGN);
4755
0
}
4756
4757
/* Like bfd_reloc_offset_in_range but without a howto.  Return true
4758
   iff a field of SIZE bytes at OFFSET is within SEC limits.  */
4759
4760
static bool
4761
offset_in_range (asection *sec, bfd_vma offset, size_t size)
4762
0
{
4763
0
  return offset <= sec->size && size <= sec->size - offset;
4764
0
}
4765
4766
/* Look through the relocs for a section during the first phase, and
4767
   calculate needed space in the global offset table, procedure
4768
   linkage table, and dynamic reloc sections.  */
4769
4770
static bool
4771
ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4772
      asection *sec, const Elf_Internal_Rela *relocs)
4773
0
{
4774
0
  struct ppc_link_hash_table *htab;
4775
0
  Elf_Internal_Shdr *symtab_hdr;
4776
0
  struct elf_link_hash_entry **sym_hashes;
4777
0
  const Elf_Internal_Rela *rel;
4778
0
  const Elf_Internal_Rela *rel_end;
4779
0
  asection *sreloc;
4780
0
  struct elf_link_hash_entry *tga, *dottga;
4781
0
  bool is_opd;
4782
4783
0
  if (bfd_link_relocatable (info))
4784
0
    return true;
4785
4786
0
  BFD_ASSERT (is_ppc64_elf (abfd));
4787
4788
0
  htab = ppc_hash_table (info);
4789
0
  if (htab == NULL)
4790
0
    return false;
4791
4792
0
  tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4793
0
            false, false, true);
4794
0
  dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4795
0
         false, false, true);
4796
0
  symtab_hdr = &elf_symtab_hdr (abfd);
4797
0
  sym_hashes = elf_sym_hashes (abfd);
4798
0
  sreloc = NULL;
4799
0
  is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4800
0
  rel_end = relocs + sec->reloc_count;
4801
0
  for (rel = relocs; rel < rel_end; rel++)
4802
0
    {
4803
0
      unsigned long r_symndx;
4804
0
      struct elf_link_hash_entry *h;
4805
0
      Elf_Internal_Sym *isym;
4806
0
      enum elf_ppc64_reloc_type r_type;
4807
0
      int tls_type;
4808
0
      struct _ppc64_elf_section_data *ppc64_sec;
4809
0
      struct plt_entry **ifunc, **plt_list;
4810
4811
0
      r_symndx = ELF64_R_SYM (rel->r_info);
4812
0
      if (r_symndx < symtab_hdr->sh_info)
4813
0
  {
4814
0
    h = NULL;
4815
0
    isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
4816
0
    if (isym == NULL)
4817
0
      return false;
4818
0
  }
4819
0
      else
4820
0
  {
4821
0
    isym = NULL;
4822
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4823
0
    h = elf_follow_link (h);
4824
4825
0
    if (h == htab->elf.hgot)
4826
0
      sec->has_toc_reloc = 1;
4827
0
  }
4828
4829
0
      r_type = ELF64_R_TYPE (rel->r_info);
4830
0
      switch (r_type)
4831
0
  {
4832
0
  case R_PPC64_D34:
4833
0
  case R_PPC64_D34_LO:
4834
0
  case R_PPC64_D34_HI30:
4835
0
  case R_PPC64_D34_HA30:
4836
0
  case R_PPC64_D28:
4837
0
  case R_PPC64_TPREL34:
4838
0
  case R_PPC64_DTPREL34:
4839
0
  case R_PPC64_PCREL34:
4840
0
  case R_PPC64_GOT_PCREL34:
4841
0
  case R_PPC64_GOT_TLSGD_PCREL34:
4842
0
  case R_PPC64_GOT_TLSLD_PCREL34:
4843
0
  case R_PPC64_GOT_TPREL_PCREL34:
4844
0
  case R_PPC64_GOT_DTPREL_PCREL34:
4845
0
  case R_PPC64_PLT_PCREL34:
4846
0
  case R_PPC64_PLT_PCREL34_NOTOC:
4847
0
  case R_PPC64_PCREL28:
4848
0
    htab->has_power10_relocs = 1;
4849
0
    break;
4850
0
  default:
4851
0
    break;
4852
0
  }
4853
4854
0
      switch (r_type)
4855
0
  {
4856
0
  case R_PPC64_PLT16_HA:
4857
0
  case R_PPC64_GOT_TLSLD16_HA:
4858
0
  case R_PPC64_GOT_TLSGD16_HA:
4859
0
  case R_PPC64_GOT_TPREL16_HA:
4860
0
  case R_PPC64_GOT_DTPREL16_HA:
4861
0
  case R_PPC64_GOT16_HA:
4862
0
  case R_PPC64_TOC16_HA:
4863
0
  case R_PPC64_PLT16_LO:
4864
0
  case R_PPC64_PLT16_LO_DS:
4865
0
  case R_PPC64_GOT_TLSLD16_LO:
4866
0
  case R_PPC64_GOT_TLSGD16_LO:
4867
0
  case R_PPC64_GOT_TPREL16_LO_DS:
4868
0
  case R_PPC64_GOT_DTPREL16_LO_DS:
4869
0
  case R_PPC64_GOT16_LO:
4870
0
  case R_PPC64_GOT16_LO_DS:
4871
0
  case R_PPC64_TOC16_LO:
4872
0
  case R_PPC64_TOC16_LO_DS:
4873
0
  case R_PPC64_GOT_PCREL34:
4874
0
    ppc64_elf_tdata (abfd)->has_optrel = 1;
4875
0
    ppc64_elf_section_data (sec)->has_optrel = 1;
4876
0
    break;
4877
0
  default:
4878
0
    break;
4879
0
  }
4880
4881
0
      ifunc = NULL;
4882
0
      if (h != NULL)
4883
0
  {
4884
0
    if (h->type == STT_GNU_IFUNC)
4885
0
      {
4886
0
        h->needs_plt = 1;
4887
0
        ifunc = &h->plt.plist;
4888
0
      }
4889
0
  }
4890
0
      else
4891
0
  {
4892
0
    if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4893
0
      {
4894
0
        ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4895
0
               rel->r_addend,
4896
0
               NON_GOT | PLT_IFUNC);
4897
0
        if (ifunc == NULL)
4898
0
    return false;
4899
0
      }
4900
0
  }
4901
4902
0
      tls_type = 0;
4903
0
      switch (r_type)
4904
0
  {
4905
0
  case R_PPC64_PLTSEQ:
4906
0
  case R_PPC64_PLTSEQ_NOTOC:
4907
    /* Inline plt call code emitted by gcc doesn't support
4908
       modifying the tls_index words to short-circuit
4909
       __tls_get_addr calls.  See PR32387.  */
4910
0
    if (h != NULL && (h == tga || h == dottga))
4911
0
      htab->params->tls_get_addr_opt = 0;
4912
0
    break;
4913
4914
0
  case R_PPC64_TLSGD:
4915
0
  case R_PPC64_TLSLD:
4916
    /* These special tls relocs tie a call to __tls_get_addr with
4917
       its parameter symbol.  */
4918
0
    if (h != NULL)
4919
0
      ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
4920
0
    else
4921
0
      if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4922
0
          rel->r_addend,
4923
0
          NON_GOT | TLS_TLS | TLS_MARK))
4924
0
        return false;
4925
0
    sec->has_tls_reloc = 1;
4926
0
    break;
4927
4928
0
  case R_PPC64_GOT_TLSLD16:
4929
0
  case R_PPC64_GOT_TLSLD16_LO:
4930
0
  case R_PPC64_GOT_TLSLD16_HI:
4931
0
  case R_PPC64_GOT_TLSLD16_HA:
4932
0
  case R_PPC64_GOT_TLSLD_PCREL34:
4933
0
    tls_type = TLS_TLS | TLS_LD;
4934
0
    goto dogottls;
4935
4936
0
  case R_PPC64_GOT_TLSGD16:
4937
0
  case R_PPC64_GOT_TLSGD16_LO:
4938
0
  case R_PPC64_GOT_TLSGD16_HI:
4939
0
  case R_PPC64_GOT_TLSGD16_HA:
4940
0
  case R_PPC64_GOT_TLSGD_PCREL34:
4941
0
    tls_type = TLS_TLS | TLS_GD;
4942
0
    goto dogottls;
4943
4944
0
  case R_PPC64_GOT_TPREL16_DS:
4945
0
  case R_PPC64_GOT_TPREL16_LO_DS:
4946
0
  case R_PPC64_GOT_TPREL16_HI:
4947
0
  case R_PPC64_GOT_TPREL16_HA:
4948
0
  case R_PPC64_GOT_TPREL_PCREL34:
4949
0
    if (bfd_link_dll (info))
4950
0
      info->flags |= DF_STATIC_TLS;
4951
0
    tls_type = TLS_TLS | TLS_TPREL;
4952
0
    goto dogottls;
4953
4954
0
  case R_PPC64_GOT_DTPREL16_DS:
4955
0
  case R_PPC64_GOT_DTPREL16_LO_DS:
4956
0
  case R_PPC64_GOT_DTPREL16_HI:
4957
0
  case R_PPC64_GOT_DTPREL16_HA:
4958
0
  case R_PPC64_GOT_DTPREL_PCREL34:
4959
0
    tls_type = TLS_TLS | TLS_DTPREL;
4960
0
  dogottls:
4961
0
    sec->has_tls_reloc = 1;
4962
0
    goto dogot;
4963
4964
0
  case R_PPC64_GOT16:
4965
0
  case R_PPC64_GOT16_LO:
4966
0
  case R_PPC64_GOT16_HI:
4967
0
  case R_PPC64_GOT16_HA:
4968
0
  case R_PPC64_GOT16_DS:
4969
0
  case R_PPC64_GOT16_LO_DS:
4970
0
  case R_PPC64_GOT_PCREL34:
4971
0
  dogot:
4972
    /* This symbol requires a global offset table entry.  */
4973
0
    sec->has_toc_reloc = 1;
4974
0
    if (r_type == R_PPC64_GOT_TLSLD16
4975
0
        || r_type == R_PPC64_GOT_TLSGD16
4976
0
        || r_type == R_PPC64_GOT_TPREL16_DS
4977
0
        || r_type == R_PPC64_GOT_DTPREL16_DS
4978
0
        || r_type == R_PPC64_GOT16
4979
0
        || r_type == R_PPC64_GOT16_DS)
4980
0
      {
4981
0
        htab->do_multi_toc = 1;
4982
0
        ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4983
0
      }
4984
4985
0
    if (ppc64_elf_tdata (abfd)->got == NULL
4986
0
        && !create_got_section (abfd, info))
4987
0
      return false;
4988
4989
0
    if (h != NULL)
4990
0
      {
4991
0
        struct ppc_link_hash_entry *eh;
4992
0
        struct got_entry *ent;
4993
4994
0
        eh = ppc_elf_hash_entry (h);
4995
0
        for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4996
0
    if (ent->addend == rel->r_addend
4997
0
        && ent->owner == abfd
4998
0
        && ent->tls_type == tls_type)
4999
0
      break;
5000
0
        if (ent == NULL)
5001
0
    {
5002
0
      size_t amt = sizeof (*ent);
5003
0
      ent = bfd_alloc (abfd, amt);
5004
0
      if (ent == NULL)
5005
0
        return false;
5006
0
      ent->next = eh->elf.got.glist;
5007
0
      ent->addend = rel->r_addend;
5008
0
      ent->owner = abfd;
5009
0
      ent->tls_type = tls_type;
5010
0
      ent->is_indirect = false;
5011
0
      ent->got.refcount = 0;
5012
0
      eh->elf.got.glist = ent;
5013
0
    }
5014
0
        ent->got.refcount += 1;
5015
0
        eh->tls_mask |= tls_type;
5016
0
      }
5017
0
    else
5018
      /* This is a global offset table entry for a local symbol.  */
5019
0
      if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5020
0
          rel->r_addend, tls_type))
5021
0
        return false;
5022
0
    break;
5023
5024
0
  case R_PPC64_PLT16_HA:
5025
0
  case R_PPC64_PLT16_HI:
5026
0
  case R_PPC64_PLT16_LO:
5027
0
  case R_PPC64_PLT16_LO_DS:
5028
0
  case R_PPC64_PLT_PCREL34:
5029
0
  case R_PPC64_PLT_PCREL34_NOTOC:
5030
0
  case R_PPC64_PLT32:
5031
0
  case R_PPC64_PLT64:
5032
    /* This symbol requires a procedure linkage table entry.  */
5033
0
    plt_list = ifunc;
5034
0
    if (h != NULL)
5035
0
      {
5036
0
        h->needs_plt = 1;
5037
0
        if (h->root.root.string[0] == '.'
5038
0
      && h->root.root.string[1] != '\0')
5039
0
    ppc_elf_hash_entry (h)->is_func = 1;
5040
0
        ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
5041
0
        plt_list = &h->plt.plist;
5042
0
      }
5043
0
    if (plt_list == NULL)
5044
0
      plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5045
0
                rel->r_addend,
5046
0
                NON_GOT | PLT_KEEP);
5047
0
    if (!update_plt_info (abfd, plt_list, rel->r_addend))
5048
0
      return false;
5049
0
    break;
5050
5051
    /* The following relocations don't need to propagate the
5052
       relocation if linking a shared object since they are
5053
       section relative.  */
5054
0
  case R_PPC64_SECTOFF:
5055
0
  case R_PPC64_SECTOFF_LO:
5056
0
  case R_PPC64_SECTOFF_HI:
5057
0
  case R_PPC64_SECTOFF_HA:
5058
0
  case R_PPC64_SECTOFF_DS:
5059
0
  case R_PPC64_SECTOFF_LO_DS:
5060
0
  case R_PPC64_DTPREL16:
5061
0
  case R_PPC64_DTPREL16_LO:
5062
0
  case R_PPC64_DTPREL16_HI:
5063
0
  case R_PPC64_DTPREL16_HA:
5064
0
  case R_PPC64_DTPREL16_DS:
5065
0
  case R_PPC64_DTPREL16_LO_DS:
5066
0
  case R_PPC64_DTPREL16_HIGH:
5067
0
  case R_PPC64_DTPREL16_HIGHA:
5068
0
  case R_PPC64_DTPREL16_HIGHER:
5069
0
  case R_PPC64_DTPREL16_HIGHERA:
5070
0
  case R_PPC64_DTPREL16_HIGHEST:
5071
0
  case R_PPC64_DTPREL16_HIGHESTA:
5072
0
    break;
5073
5074
    /* Nor do these.  */
5075
0
  case R_PPC64_REL16:
5076
0
  case R_PPC64_REL16_LO:
5077
0
  case R_PPC64_REL16_HI:
5078
0
  case R_PPC64_REL16_HA:
5079
0
  case R_PPC64_REL16_HIGH:
5080
0
  case R_PPC64_REL16_HIGHA:
5081
0
  case R_PPC64_REL16_HIGHER:
5082
0
  case R_PPC64_REL16_HIGHERA:
5083
0
  case R_PPC64_REL16_HIGHEST:
5084
0
  case R_PPC64_REL16_HIGHESTA:
5085
0
  case R_PPC64_REL16_HIGHER34:
5086
0
  case R_PPC64_REL16_HIGHERA34:
5087
0
  case R_PPC64_REL16_HIGHEST34:
5088
0
  case R_PPC64_REL16_HIGHESTA34:
5089
0
  case R_PPC64_REL16DX_HA:
5090
0
    break;
5091
5092
    /* Not supported as a dynamic relocation.  */
5093
0
  case R_PPC64_ADDR64_LOCAL:
5094
0
    if (bfd_link_pic (info))
5095
0
      {
5096
0
        if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5097
0
    ppc_howto_init ();
5098
        /* xgettext:c-format */
5099
0
        info->callbacks->einfo (_("%H: %s unsupported "
5100
0
          "in shared libraries and PIEs\n"),
5101
0
              abfd, sec, rel->r_offset,
5102
0
              ppc64_elf_howto_table[r_type]->name);
5103
0
        bfd_set_error (bfd_error_bad_value);
5104
0
        return false;
5105
0
      }
5106
0
    break;
5107
5108
0
  case R_PPC64_TOC16:
5109
0
  case R_PPC64_TOC16_DS:
5110
0
    htab->do_multi_toc = 1;
5111
0
    ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5112
    /* Fall through.  */
5113
0
  case R_PPC64_TOC16_LO:
5114
0
  case R_PPC64_TOC16_HI:
5115
0
  case R_PPC64_TOC16_HA:
5116
0
  case R_PPC64_TOC16_LO_DS:
5117
0
    sec->has_toc_reloc = 1;
5118
0
    if (h != NULL && bfd_link_executable (info))
5119
0
      {
5120
        /* We may need a copy reloc.  */
5121
0
        h->non_got_ref = 1;
5122
        /* Strongly prefer a copy reloc over a dynamic reloc.
5123
     glibc ld.so as of 2019-08 will error out if one of
5124
     these relocations is emitted.  */
5125
0
        h->needs_copy = 1;
5126
0
        goto dodyn;
5127
0
      }
5128
0
    break;
5129
5130
    /* Marker reloc.  */
5131
0
  case R_PPC64_ENTRY:
5132
0
    break;
5133
5134
    /* This relocation describes the C++ object vtable hierarchy.
5135
       Reconstruct it for later use during GC.  */
5136
0
  case R_PPC64_GNU_VTINHERIT:
5137
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5138
0
      return false;
5139
0
    break;
5140
5141
    /* This relocation describes which C++ vtable entries are actually
5142
       used.  Record for later use during GC.  */
5143
0
  case R_PPC64_GNU_VTENTRY:
5144
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5145
0
      return false;
5146
0
    break;
5147
5148
0
  case R_PPC64_REL14:
5149
0
  case R_PPC64_REL14_BRTAKEN:
5150
0
  case R_PPC64_REL14_BRNTAKEN:
5151
0
    {
5152
0
      asection *dest = NULL;
5153
5154
      /* Heuristic: If jumping outside our section, chances are
5155
         we are going to need a stub.  */
5156
0
      if (h != NULL)
5157
0
        {
5158
    /* If the sym is weak it may be overridden later, so
5159
       don't assume we know where a weak sym lives.  */
5160
0
    if (h->root.type == bfd_link_hash_defined)
5161
0
      dest = h->root.u.def.section;
5162
0
        }
5163
0
      else
5164
0
        dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5165
5166
0
      if (dest != sec)
5167
0
        ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5168
0
    }
5169
0
    goto rel24;
5170
5171
0
  case R_PPC64_PLTCALL:
5172
0
  case R_PPC64_PLTCALL_NOTOC:
5173
0
    ppc64_elf_section_data (sec)->has_pltcall = 1;
5174
    /* Fall through.  */
5175
5176
0
  case R_PPC64_REL24:
5177
0
  case R_PPC64_REL24_NOTOC:
5178
0
  case R_PPC64_REL24_P9NOTOC:
5179
0
  rel24:
5180
0
    plt_list = ifunc;
5181
0
    if (h != NULL)
5182
0
      {
5183
0
        h->needs_plt = 1;
5184
0
        if (h->root.root.string[0] == '.'
5185
0
      && h->root.root.string[1] != '\0')
5186
0
    ppc_elf_hash_entry (h)->is_func = 1;
5187
5188
0
        if (h == tga || h == dottga)
5189
0
    {
5190
0
      sec->has_tls_reloc = 1;
5191
0
      if (rel != relocs
5192
0
          && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5193
0
        || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5194
        /* We have a new-style __tls_get_addr call with
5195
           a marker reloc.  */
5196
0
        ;
5197
0
      else
5198
        /* Mark this section as having an old-style call.  */
5199
0
        sec->nomark_tls_get_addr = 1;
5200
0
    }
5201
0
        plt_list = &h->plt.plist;
5202
0
      }
5203
5204
    /* We may need a .plt entry if the function this reloc
5205
       refers to is in a shared lib.  */
5206
0
    if (plt_list
5207
0
        && !update_plt_info (abfd, plt_list, rel->r_addend))
5208
0
      return false;
5209
0
    break;
5210
5211
0
  case R_PPC64_ADDR14:
5212
0
  case R_PPC64_ADDR14_BRNTAKEN:
5213
0
  case R_PPC64_ADDR14_BRTAKEN:
5214
0
  case R_PPC64_ADDR24:
5215
0
    goto dodyn;
5216
5217
0
  case R_PPC64_TPREL64:
5218
0
    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5219
0
    if (bfd_link_dll (info))
5220
0
      info->flags |= DF_STATIC_TLS;
5221
0
    goto dotlstoc;
5222
5223
0
  case R_PPC64_DTPMOD64:
5224
0
    if (rel + 1 < rel_end
5225
0
        && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5226
0
        && rel[1].r_offset == rel->r_offset + 8)
5227
0
      tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5228
0
    else
5229
0
      tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5230
0
    goto dotlstoc;
5231
5232
0
  case R_PPC64_DTPREL64:
5233
0
    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5234
0
    if (rel != relocs
5235
0
        && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5236
0
        && rel[-1].r_offset == rel->r_offset - 8)
5237
      /* This is the second reloc of a dtpmod, dtprel pair.
5238
         Don't mark with TLS_DTPREL.  */
5239
0
      goto dodyn;
5240
5241
0
  dotlstoc:
5242
0
    sec->has_tls_reloc = 1;
5243
0
    if (h != NULL)
5244
0
      ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
5245
0
    else
5246
0
      if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5247
0
          rel->r_addend, tls_type))
5248
0
        return false;
5249
5250
0
    ppc64_sec = ppc64_elf_section_data (sec);
5251
0
    if (ppc64_sec->sec_type == sec_normal)
5252
0
      {
5253
0
        bfd_size_type amt;
5254
5255
        /* One extra to simplify get_tls_mask.  */
5256
0
        amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5257
0
        ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5258
0
        if (ppc64_sec->u.toc.symndx == NULL)
5259
0
    return false;
5260
0
        amt = sec->size * sizeof (bfd_vma) / 8;
5261
0
        ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5262
0
        if (ppc64_sec->u.toc.add == NULL)
5263
0
    return false;
5264
0
        ppc64_sec->sec_type = sec_toc;
5265
0
      }
5266
0
    if (ppc64_sec->sec_type != sec_toc
5267
0
        || rel->r_offset % 8 != 0)
5268
0
      {
5269
0
        info->callbacks->einfo (_("%H: %s unsupported here\n"),
5270
0
              abfd, sec, rel->r_offset,
5271
0
              ppc64_elf_howto_table[r_type]->name);
5272
0
        bfd_set_error (bfd_error_bad_value);
5273
0
        return false;
5274
0
      }
5275
0
    ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5276
0
    ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5277
5278
    /* Mark the second slot of a GD or LD entry.
5279
       -1 to indicate GD and -2 to indicate LD.  */
5280
0
    if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5281
0
      ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5282
0
    else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5283
0
      ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5284
0
    goto dodyn;
5285
5286
0
  case R_PPC64_TPREL16_HI:
5287
0
  case R_PPC64_TPREL16_HA:
5288
0
  case R_PPC64_TPREL16_HIGH:
5289
0
  case R_PPC64_TPREL16_HIGHA:
5290
0
  case R_PPC64_TPREL16_HIGHER:
5291
0
  case R_PPC64_TPREL16_HIGHERA:
5292
0
  case R_PPC64_TPREL16_HIGHEST:
5293
0
  case R_PPC64_TPREL16_HIGHESTA:
5294
0
    sec->has_tls_reloc = 1;
5295
    /* Fall through.  */
5296
0
  case R_PPC64_TPREL34:
5297
0
  case R_PPC64_TPREL16:
5298
0
  case R_PPC64_TPREL16_DS:
5299
0
  case R_PPC64_TPREL16_LO:
5300
0
  case R_PPC64_TPREL16_LO_DS:
5301
0
    if (bfd_link_dll (info))
5302
0
      info->flags |= DF_STATIC_TLS;
5303
0
    goto dodyn;
5304
5305
0
  case R_PPC64_ADDR64:
5306
0
    if (is_opd
5307
0
        && rel + 1 < rel_end
5308
0
        && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5309
0
      {
5310
0
        if (h != NULL)
5311
0
    ppc_elf_hash_entry (h)->is_func = 1;
5312
0
      }
5313
    /* Fall through.  */
5314
5315
0
  case R_PPC64_ADDR16:
5316
0
  case R_PPC64_ADDR16_DS:
5317
0
  case R_PPC64_ADDR16_HA:
5318
0
  case R_PPC64_ADDR16_HI:
5319
0
  case R_PPC64_ADDR16_HIGH:
5320
0
  case R_PPC64_ADDR16_HIGHA:
5321
0
  case R_PPC64_ADDR16_HIGHER:
5322
0
  case R_PPC64_ADDR16_HIGHERA:
5323
0
  case R_PPC64_ADDR16_HIGHEST:
5324
0
  case R_PPC64_ADDR16_HIGHESTA:
5325
0
  case R_PPC64_ADDR16_LO:
5326
0
  case R_PPC64_ADDR16_LO_DS:
5327
0
  case R_PPC64_D34:
5328
0
  case R_PPC64_D34_LO:
5329
0
  case R_PPC64_D34_HI30:
5330
0
  case R_PPC64_D34_HA30:
5331
0
  case R_PPC64_ADDR16_HIGHER34:
5332
0
  case R_PPC64_ADDR16_HIGHERA34:
5333
0
  case R_PPC64_ADDR16_HIGHEST34:
5334
0
  case R_PPC64_ADDR16_HIGHESTA34:
5335
0
  case R_PPC64_D28:
5336
0
    if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5337
0
        && rel->r_addend == 0)
5338
0
      {
5339
        /* We may need a .plt entry if this reloc refers to a
5340
     function in a shared lib.  */
5341
0
        if (!update_plt_info (abfd, &h->plt.plist, 0))
5342
0
    return false;
5343
0
        h->pointer_equality_needed = 1;
5344
0
      }
5345
    /* Fall through.  */
5346
5347
0
  case R_PPC64_REL30:
5348
0
  case R_PPC64_REL32:
5349
0
  case R_PPC64_REL64:
5350
0
  case R_PPC64_ADDR32:
5351
0
  case R_PPC64_UADDR16:
5352
0
  case R_PPC64_UADDR32:
5353
0
  case R_PPC64_UADDR64:
5354
0
  case R_PPC64_TOC:
5355
0
    if (h != NULL && bfd_link_executable (info))
5356
      /* We may need a copy reloc.  */
5357
0
      h->non_got_ref = 1;
5358
5359
    /* Don't propagate .opd relocs.  */
5360
0
    if (NO_OPD_RELOCS && is_opd)
5361
0
      break;
5362
5363
    /* Set up information for symbols that might need dynamic
5364
       relocations.  At this point in linking we have read all
5365
       the input files and resolved most symbols, but have not
5366
       yet decided whether symbols are dynamic or finalized
5367
       symbol flags.  In some cases we might be setting dynamic
5368
       reloc info for symbols that do not end up needing such.
5369
       That's OK, adjust_dynamic_symbol and allocate_dynrelocs
5370
       work together with this code.  */
5371
0
  dodyn:
5372
0
    if ((h != NULL
5373
0
         && !SYMBOL_REFERENCES_LOCAL (info, h))
5374
0
        || (bfd_link_pic (info)
5375
0
      && (h != NULL
5376
0
          ? !bfd_is_abs_symbol (&h->root)
5377
0
          : isym->st_shndx != SHN_ABS)
5378
0
      && must_be_dyn_reloc (info, r_type))
5379
0
        || (!bfd_link_pic (info)
5380
0
      && ifunc != NULL))
5381
0
      {
5382
        /* We must copy these reloc types into the output file.
5383
     Create a reloc section in dynobj and make room for
5384
     this reloc.  */
5385
0
        if (sreloc == NULL)
5386
0
    {
5387
0
      sreloc = _bfd_elf_make_dynamic_reloc_section
5388
0
        (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
5389
5390
0
      if (sreloc == NULL)
5391
0
        return false;
5392
0
    }
5393
5394
        /* If this is a global symbol, we count the number of
5395
     relocations we need for this symbol.  */
5396
0
        if (h != NULL)
5397
0
    {
5398
0
      struct ppc_dyn_relocs *p;
5399
0
      struct ppc_dyn_relocs **head;
5400
5401
0
      head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
5402
0
      p = *head;
5403
0
      if (p == NULL || p->sec != sec)
5404
0
        {
5405
0
          p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5406
0
          if (p == NULL)
5407
0
      return false;
5408
0
          p->next = *head;
5409
0
          *head = p;
5410
0
          p->sec = sec;
5411
0
          p->count = 0;
5412
0
          p->pc_count = 0;
5413
0
          p->rel_count = 0;
5414
0
        }
5415
0
      p->count += 1;
5416
0
      if (!must_be_dyn_reloc (info, r_type))
5417
0
        p->pc_count += 1;
5418
0
      if (maybe_relr (r_type, rel, sec))
5419
0
        p->rel_count += 1;
5420
0
    }
5421
0
        else
5422
0
    {
5423
      /* Track dynamic relocs needed for local syms too.  */
5424
0
      struct ppc_local_dyn_relocs *p;
5425
0
      struct ppc_local_dyn_relocs **head;
5426
0
      bool is_ifunc;
5427
0
      asection *s;
5428
0
      void *vpp;
5429
5430
0
      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5431
0
      if (s == NULL)
5432
0
        s = sec;
5433
5434
0
      vpp = &elf_section_data (s)->local_dynrel;
5435
0
      head = (struct ppc_local_dyn_relocs **) vpp;
5436
0
      is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5437
0
      p = *head;
5438
0
      if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5439
0
        p = p->next;
5440
0
      if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5441
0
        {
5442
0
          p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5443
0
          if (p == NULL)
5444
0
      return false;
5445
0
          p->next = *head;
5446
0
          *head = p;
5447
0
          p->sec = sec;
5448
0
          p->count = 0;
5449
0
          p->rel_count = 0;
5450
0
          p->ifunc = is_ifunc;
5451
0
        }
5452
0
      p->count += 1;
5453
0
      if (maybe_relr (r_type, rel, sec))
5454
0
        p->rel_count += 1;
5455
0
    }
5456
0
      }
5457
0
    break;
5458
5459
0
  default:
5460
0
    break;
5461
0
  }
5462
0
    }
5463
5464
0
  return true;
5465
0
}
5466
5467
/* Merge backend specific data from an object file to the output
5468
   object file when linking.  */
5469
5470
static bool
5471
ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5472
0
{
5473
0
  bfd *obfd = info->output_bfd;
5474
0
  unsigned long iflags, oflags;
5475
5476
0
  if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5477
0
    return true;
5478
5479
0
  if (!is_ppc64_elf (ibfd))
5480
0
    return true;
5481
5482
0
  if (!_bfd_generic_verify_endian_match (ibfd, info))
5483
0
    return false;
5484
5485
0
  iflags = elf_elfheader (ibfd)->e_flags;
5486
0
  oflags = elf_elfheader (obfd)->e_flags;
5487
5488
0
  if (iflags & ~EF_PPC64_ABI)
5489
0
    {
5490
0
      _bfd_error_handler
5491
  /* xgettext:c-format */
5492
0
  (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5493
0
      bfd_set_error (bfd_error_bad_value);
5494
0
      return false;
5495
0
    }
5496
0
  else if (iflags != oflags && iflags != 0)
5497
0
    {
5498
0
      _bfd_error_handler
5499
  /* xgettext:c-format */
5500
0
  (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5501
0
   ibfd, iflags, oflags);
5502
0
      bfd_set_error (bfd_error_bad_value);
5503
0
      return false;
5504
0
    }
5505
5506
0
  if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5507
0
    return false;
5508
5509
  /* Merge Tag_compatibility attributes and any common GNU ones.  */
5510
0
  return _bfd_elf_merge_object_attributes (ibfd, info);
5511
0
}
5512
5513
static bool
5514
ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5515
34
{
5516
  /* Print normal ELF private data.  */
5517
34
  _bfd_elf_print_private_bfd_data (abfd, ptr);
5518
5519
34
  if (elf_elfheader (abfd)->e_flags != 0)
5520
2
    {
5521
2
      FILE *file = ptr;
5522
5523
2
      fprintf (file, _("private flags = 0x%lx:"),
5524
2
         elf_elfheader (abfd)->e_flags);
5525
5526
2
      if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5527
1
  fprintf (file, _(" [abiv%ld]"),
5528
1
     elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5529
2
      fputc ('\n', file);
5530
2
    }
5531
5532
34
  return true;
5533
34
}
5534
5535
/* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
5536
   of the code entry point, and its section, which must be in the same
5537
   object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
5538
5539
static bfd_vma
5540
opd_entry_value (asection *opd_sec,
5541
     bfd_vma offset,
5542
     asection **code_sec,
5543
     bfd_vma *code_off,
5544
     bool in_code_sec)
5545
0
{
5546
0
  bfd *opd_bfd = opd_sec->owner;
5547
0
  Elf_Internal_Rela *relocs;
5548
0
  Elf_Internal_Rela *lo, *hi, *look;
5549
0
  bfd_vma val;
5550
5551
0
  if (!is_ppc64_elf (opd_bfd))
5552
0
    return (bfd_vma) -1;
5553
5554
0
  if (ppc64_elf_section_data (opd_sec)->sec_type == sec_normal)
5555
0
    ppc64_elf_section_data (opd_sec)->sec_type = sec_opd;
5556
0
  else if (ppc64_elf_section_data (opd_sec)->sec_type != sec_opd)
5557
0
    return (bfd_vma) -1;
5558
5559
  /* No relocs implies we are linking a --just-symbols object, or looking
5560
     at a final linked executable with addr2line or somesuch.  */
5561
0
  if (opd_sec->reloc_count == 0)
5562
0
    {
5563
0
      bfd_byte *contents = ppc64_elf_section_data (opd_sec)->u.opd.u.contents;
5564
5565
0
      if (contents == NULL)
5566
0
  {
5567
0
    if ((opd_sec->flags & SEC_HAS_CONTENTS) == 0
5568
0
        || !bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5569
0
      return (bfd_vma) -1;
5570
0
    ppc64_elf_section_data (opd_sec)->u.opd.u.contents = contents;
5571
0
  }
5572
5573
      /* PR 17512: file: 64b9dfbb.  */
5574
0
      if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5575
0
  return (bfd_vma) -1;
5576
5577
0
      val = bfd_get_64 (opd_bfd, contents + offset);
5578
0
      if (code_sec != NULL)
5579
0
  {
5580
0
    asection *sec, *likely = NULL;
5581
5582
0
    if (in_code_sec)
5583
0
      {
5584
0
        sec = *code_sec;
5585
0
        if (sec->vma <= val
5586
0
      && val < sec->vma + sec->size)
5587
0
    likely = sec;
5588
0
        else
5589
0
    val = -1;
5590
0
      }
5591
0
    else
5592
0
      for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5593
0
        if (sec->vma <= val
5594
0
      && (sec->flags & SEC_LOAD) != 0
5595
0
      && (sec->flags & SEC_ALLOC) != 0)
5596
0
    likely = sec;
5597
0
    if (likely != NULL)
5598
0
      {
5599
0
        *code_sec = likely;
5600
0
        if (code_off != NULL)
5601
0
    *code_off = val - likely->vma;
5602
0
      }
5603
0
  }
5604
0
      return val;
5605
0
    }
5606
5607
0
  relocs = ppc64_elf_section_data (opd_sec)->u.opd.u.relocs;
5608
0
  if (relocs == NULL)
5609
0
    relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
5610
  /* PR 17512: file: df8e1fd6.  */
5611
0
  if (relocs == NULL)
5612
0
    return (bfd_vma) -1;
5613
5614
  /* Go find the opd reloc at the sym address.  */
5615
0
  lo = relocs;
5616
0
  hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5617
0
  val = (bfd_vma) -1;
5618
0
  while (lo < hi)
5619
0
    {
5620
0
      look = lo + (hi - lo) / 2;
5621
0
      if (look->r_offset < offset)
5622
0
  lo = look + 1;
5623
0
      else if (look->r_offset > offset)
5624
0
  hi = look;
5625
0
      else
5626
0
  {
5627
0
    Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5628
5629
0
    if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5630
0
        && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5631
0
      {
5632
0
        unsigned long symndx = ELF64_R_SYM (look->r_info);
5633
0
        asection *sec = NULL;
5634
5635
0
        if (symndx >= symtab_hdr->sh_info
5636
0
      && elf_sym_hashes (opd_bfd) != NULL)
5637
0
    {
5638
0
      struct elf_link_hash_entry **sym_hashes;
5639
0
      struct elf_link_hash_entry *rh;
5640
5641
0
      sym_hashes = elf_sym_hashes (opd_bfd);
5642
0
      rh = sym_hashes[symndx - symtab_hdr->sh_info];
5643
0
      if (rh != NULL)
5644
0
        {
5645
0
          rh = elf_follow_link (rh);
5646
0
          if (rh->root.type != bfd_link_hash_defined
5647
0
        && rh->root.type != bfd_link_hash_defweak)
5648
0
      break;
5649
0
          if (rh->root.u.def.section->owner == opd_bfd)
5650
0
      {
5651
0
        val = rh->root.u.def.value;
5652
0
        sec = rh->root.u.def.section;
5653
0
      }
5654
0
        }
5655
0
    }
5656
5657
0
        if (sec == NULL)
5658
0
    {
5659
0
      Elf_Internal_Sym *sym;
5660
5661
0
      if (symndx < symtab_hdr->sh_info)
5662
0
        {
5663
0
          sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5664
0
          if (sym == NULL)
5665
0
      {
5666
0
        size_t symcnt = symtab_hdr->sh_info;
5667
0
        sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5668
0
                  symcnt, 0,
5669
0
                  NULL, NULL, NULL);
5670
0
        if (sym == NULL)
5671
0
          break;
5672
0
        symtab_hdr->contents = (bfd_byte *) sym;
5673
0
      }
5674
0
          sym += symndx;
5675
0
        }
5676
0
      else
5677
0
        {
5678
0
          sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5679
0
              1, symndx,
5680
0
              NULL, NULL, NULL);
5681
0
          if (sym == NULL)
5682
0
      break;
5683
0
        }
5684
0
      sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5685
0
      if (sec != NULL)
5686
0
        {
5687
0
          BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5688
0
          val = sym->st_value;
5689
0
        }
5690
0
      if (symndx >= symtab_hdr->sh_info)
5691
0
        free (sym);
5692
0
      if (sec == NULL)
5693
0
        break;
5694
0
    }
5695
5696
0
        val += look->r_addend;
5697
0
        if (code_off != NULL)
5698
0
    *code_off = val;
5699
0
        if (code_sec != NULL)
5700
0
    {
5701
0
      if (in_code_sec && *code_sec != sec)
5702
0
        return -1;
5703
0
      else
5704
0
        *code_sec = sec;
5705
0
    }
5706
0
        if (sec->output_section != NULL)
5707
0
    val += sec->output_section->vma + sec->output_offset;
5708
0
      }
5709
0
    break;
5710
0
  }
5711
0
    }
5712
5713
0
  return val;
5714
0
}
5715
5716
/* If the ELF symbol SYM might be a function in SEC, return the
5717
   function size and set *CODE_OFF to the function's entry point,
5718
   otherwise return zero.  */
5719
5720
static bfd_size_type
5721
ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5722
            bfd_vma *code_off)
5723
5.69k
{
5724
5.69k
  bfd_size_type size;
5725
5.69k
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
5726
5727
5.69k
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5728
5.69k
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5729
1.53k
    return 0;
5730
5731
4.15k
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5732
5733
  /* In theory we should check that the symbol's type satisfies
5734
     _bfd_elf_is_function_type(), but there are some function-like
5735
     symbols which would fail this test.  (eg _start).  Instead
5736
     we check for hidden, local, notype symbols with zero size.
5737
     This type of symbol is generated by the annobin plugin for gcc
5738
     and clang, and should not be considered to be a function symbol.  */
5739
4.15k
  if (size == 0
5740
2.49k
      && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5741
392
      && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5742
332
      && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5743
48
    return 0;
5744
5745
4.11k
  if (strcmp (sym->section->name, ".opd") == 0)
5746
0
    {
5747
0
      struct _opd_sec_data *opd = get_opd_info (sym->section);
5748
0
      bfd_vma symval = sym->value;
5749
5750
0
      if (opd != NULL
5751
0
    && opd->adjust != NULL
5752
0
    && elf_section_data (sym->section)->relocs != NULL)
5753
0
  {
5754
    /* opd_entry_value will use cached relocs that have been
5755
       adjusted, but with raw symbols.  That means both local
5756
       and global symbols need adjusting.  */
5757
0
    long adjust = opd->adjust[OPD_NDX (symval)];
5758
0
    if (adjust == -1)
5759
0
      return 0;
5760
0
    symval += adjust;
5761
0
  }
5762
5763
0
      if (opd_entry_value (sym->section, symval,
5764
0
         &sec, code_off, true) == (bfd_vma) -1)
5765
0
  return 0;
5766
      /* An old ABI binary with dot-syms has a size of 24 on the .opd
5767
   symbol.  This size has nothing to do with the code size of the
5768
   function, which is what we're supposed to return, but the
5769
   code size isn't available without looking up the dot-sym.
5770
   However, doing that would be a waste of time particularly
5771
   since elf_find_function will look at the dot-sym anyway.
5772
   Now, elf_find_function will keep the largest size of any
5773
   function sym found at the code address of interest, so return
5774
   1 here to avoid it incorrectly caching a larger function size
5775
   for a small function.  This does mean we return the wrong
5776
   size for a new-ABI function of size 24, but all that does is
5777
   disable caching for such functions.  */
5778
0
      if (size == 24)
5779
0
  size = 1;
5780
0
    }
5781
4.11k
  else
5782
4.11k
    {
5783
4.11k
      if (sym->section != sec)
5784
4.04k
  return 0;
5785
68
      *code_off = sym->value;
5786
68
    }
5787
5788
  /* Do not return 0 for the function's size.  */
5789
68
  return size ? size : 1;
5790
4.11k
}
5791
5792
/* Return true if symbol is a strong function defined in an ELFv2
5793
   object with st_other localentry bits of zero, ie. its local entry
5794
   point coincides with its global entry point.  */
5795
5796
static bool
5797
is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5798
0
{
5799
0
  return (h != NULL
5800
0
    && h->type == STT_FUNC
5801
0
    && h->root.type == bfd_link_hash_defined
5802
0
    && (STO_PPC64_LOCAL_MASK & h->other) == 0
5803
0
    && !ppc_elf_hash_entry (h)->non_zero_localentry
5804
0
    && is_ppc64_elf (h->root.u.def.section->owner)
5805
0
    && abiversion (h->root.u.def.section->owner) >= 2);
5806
0
}
5807
5808
/* Return true if symbol is defined in a regular object file.  */
5809
5810
static bool
5811
is_static_defined (struct elf_link_hash_entry *h)
5812
0
{
5813
0
  return ((h->root.type == bfd_link_hash_defined
5814
0
     || h->root.type == bfd_link_hash_defweak)
5815
0
    && h->root.u.def.section != NULL
5816
0
    && h->root.u.def.section->output_section != NULL);
5817
0
}
5818
5819
/* If FDH is a function descriptor symbol, return the associated code
5820
   entry symbol if it is defined.  Return NULL otherwise.  */
5821
5822
static struct ppc_link_hash_entry *
5823
defined_code_entry (struct ppc_link_hash_entry *fdh)
5824
0
{
5825
0
  if (fdh->is_func_descriptor)
5826
0
    {
5827
0
      struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5828
0
      if (fh->elf.root.type == bfd_link_hash_defined
5829
0
    || fh->elf.root.type == bfd_link_hash_defweak)
5830
0
  return fh;
5831
0
    }
5832
0
  return NULL;
5833
0
}
5834
5835
/* If FH is a function code entry symbol, return the associated
5836
   function descriptor symbol if it is defined.  Return NULL otherwise.  */
5837
5838
static struct ppc_link_hash_entry *
5839
defined_func_desc (struct ppc_link_hash_entry *fh)
5840
0
{
5841
0
  if (fh->oh != NULL
5842
0
      && fh->oh->is_func_descriptor)
5843
0
    {
5844
0
      struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5845
0
      if (fdh->elf.root.type == bfd_link_hash_defined
5846
0
    || fdh->elf.root.type == bfd_link_hash_defweak)
5847
0
  return fdh;
5848
0
    }
5849
0
  return NULL;
5850
0
}
5851
5852
/* Given H is a symbol that satisfies is_static_defined, return the
5853
   value in the output file.  */
5854
5855
static bfd_vma
5856
defined_sym_val (struct elf_link_hash_entry *h)
5857
0
{
5858
0
  return (h->root.u.def.section->output_section->vma
5859
0
    + h->root.u.def.section->output_offset
5860
0
    + h->root.u.def.value);
5861
0
}
5862
5863
/* Return true if H matches __tls_get_addr or one of its variants.  */
5864
5865
static bool
5866
is_tls_get_addr (struct elf_link_hash_entry *h,
5867
     struct ppc_link_hash_table *htab)
5868
0
{
5869
0
  return (h == elf_hash_entry (htab->tls_get_addr_fd)
5870
0
    || h == elf_hash_entry (htab->tga_desc_fd)
5871
0
    || h == elf_hash_entry (htab->tls_get_addr)
5872
0
    || h == elf_hash_entry (htab->tga_desc));
5873
0
}
5874
5875
static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
5876
5877
/* Garbage collect sections, after first dealing with dot-symbols.  */
5878
5879
static bool
5880
ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5881
0
{
5882
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
5883
5884
0
  if (htab != NULL && htab->need_func_desc_adj)
5885
0
    {
5886
0
      elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5887
0
      htab->need_func_desc_adj = 0;
5888
0
    }
5889
0
  return bfd_elf_gc_sections (abfd, info);
5890
0
}
5891
5892
/* Mark all our entry sym sections, both opd and code section.  */
5893
5894
static void
5895
ppc64_elf_gc_keep (struct bfd_link_info *info)
5896
0
{
5897
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
5898
0
  struct bfd_sym_chain *sym;
5899
5900
0
  if (htab == NULL)
5901
0
    return;
5902
5903
0
  for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5904
0
    {
5905
0
      struct ppc_link_hash_entry *eh, *fh;
5906
0
      asection *sec;
5907
5908
0
      eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5909
0
                 false, false, true));
5910
0
      if (eh == NULL)
5911
0
  continue;
5912
0
      if (eh->elf.root.type != bfd_link_hash_defined
5913
0
    && eh->elf.root.type != bfd_link_hash_defweak)
5914
0
  continue;
5915
5916
0
      fh = defined_code_entry (eh);
5917
0
      if (fh != NULL)
5918
0
  {
5919
0
    sec = fh->elf.root.u.def.section;
5920
0
    sec->flags |= SEC_KEEP;
5921
0
  }
5922
0
      else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5923
0
         && opd_entry_value (eh->elf.root.u.def.section,
5924
0
           eh->elf.root.u.def.value,
5925
0
           &sec, NULL, false) != (bfd_vma) -1)
5926
0
  sec->flags |= SEC_KEEP;
5927
5928
0
      sec = eh->elf.root.u.def.section;
5929
0
      sec->flags |= SEC_KEEP;
5930
0
    }
5931
0
}
5932
5933
/* Mark sections containing dynamically referenced symbols.  When
5934
   building shared libraries, we must assume that any visible symbol is
5935
   referenced.  */
5936
5937
static bool
5938
ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5939
0
{
5940
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
5941
0
  struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
5942
0
  struct ppc_link_hash_entry *fdh;
5943
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
5944
5945
  /* Dynamic linking info is on the func descriptor sym.  */
5946
0
  fdh = defined_func_desc (eh);
5947
0
  if (fdh != NULL)
5948
0
    eh = fdh;
5949
5950
0
  if ((eh->elf.root.type == bfd_link_hash_defined
5951
0
       || eh->elf.root.type == bfd_link_hash_defweak)
5952
0
      && (!eh->elf.start_stop
5953
0
    || eh->elf.root.ldscript_def
5954
0
    || !info->start_stop_gc)
5955
0
      && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5956
0
    || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5957
0
        && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5958
0
        && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5959
0
        && (!bfd_link_executable (info)
5960
0
      || info->gc_keep_exported
5961
0
      || info->export_dynamic
5962
0
      || (eh->elf.dynamic
5963
0
          && d != NULL
5964
0
          && (*d->match) (&d->head, NULL,
5965
0
              eh->elf.root.root.string)))
5966
0
        && (eh->elf.versioned >= versioned
5967
0
      || !bfd_hide_sym_by_version (info->version_info,
5968
0
                 eh->elf.root.root.string)))))
5969
0
    {
5970
0
      asection *code_sec;
5971
0
      struct ppc_link_hash_entry *fh;
5972
5973
0
      eh->elf.root.u.def.section->flags |= SEC_KEEP;
5974
5975
      /* Function descriptor syms cause the associated
5976
   function code sym section to be marked.  */
5977
0
      fh = defined_code_entry (eh);
5978
0
      if (fh != NULL)
5979
0
  {
5980
0
    code_sec = fh->elf.root.u.def.section;
5981
0
    code_sec->flags |= SEC_KEEP;
5982
0
  }
5983
0
      else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5984
0
         && opd_entry_value (eh->elf.root.u.def.section,
5985
0
           eh->elf.root.u.def.value,
5986
0
           &code_sec, NULL, false) != (bfd_vma) -1)
5987
0
  code_sec->flags |= SEC_KEEP;
5988
0
    }
5989
5990
0
  return true;
5991
0
}
5992
5993
/* Return the section that should be marked against GC for a given
5994
   relocation.  */
5995
5996
static asection *
5997
ppc64_elf_gc_mark_hook (asection *sec,
5998
      struct bfd_link_info *info,
5999
      struct elf_reloc_cookie *cookie,
6000
      struct elf_link_hash_entry *h,
6001
      unsigned int symndx)
6002
0
{
6003
0
  asection *rsec;
6004
6005
  /* Syms return NULL if we're marking .opd, so we avoid marking all
6006
     function sections, as all functions are referenced in .opd.  */
6007
0
  rsec = NULL;
6008
0
  if (get_opd_info (sec) != NULL)
6009
0
    return rsec;
6010
6011
0
  if (h != NULL)
6012
0
    {
6013
0
      enum elf_ppc64_reloc_type r_type;
6014
0
      struct ppc_link_hash_entry *eh, *fh, *fdh;
6015
6016
0
      r_type = ELF64_R_TYPE (cookie->rel->r_info);
6017
0
      switch (r_type)
6018
0
  {
6019
0
  case R_PPC64_GNU_VTINHERIT:
6020
0
  case R_PPC64_GNU_VTENTRY:
6021
0
    break;
6022
6023
0
  default:
6024
0
    switch (h->root.type)
6025
0
      {
6026
0
      case bfd_link_hash_defined:
6027
0
      case bfd_link_hash_defweak:
6028
0
        eh = ppc_elf_hash_entry (h);
6029
0
        fdh = defined_func_desc (eh);
6030
0
        if (fdh != NULL)
6031
0
    {
6032
      /* -mcall-aixdesc code references the dot-symbol on
6033
         a call reloc.  Mark the function descriptor too
6034
         against garbage collection.  */
6035
0
      fdh->elf.mark = 1;
6036
0
      if (fdh->elf.is_weakalias)
6037
0
        weakdef (&fdh->elf)->mark = 1;
6038
0
      eh = fdh;
6039
0
    }
6040
6041
        /* Function descriptor syms cause the associated
6042
     function code sym section to be marked.  */
6043
0
        fh = defined_code_entry (eh);
6044
0
        if (fh != NULL)
6045
0
    {
6046
      /* They also mark their opd section.  */
6047
0
      eh->elf.root.u.def.section->gc_mark = 1;
6048
6049
0
      rsec = fh->elf.root.u.def.section;
6050
0
    }
6051
0
        else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6052
0
           && opd_entry_value (eh->elf.root.u.def.section,
6053
0
             eh->elf.root.u.def.value,
6054
0
             &rsec, NULL, false) != (bfd_vma) -1)
6055
0
    eh->elf.root.u.def.section->gc_mark = 1;
6056
0
        else
6057
0
    rsec = h->root.u.def.section;
6058
0
        break;
6059
6060
0
      case bfd_link_hash_common:
6061
0
        rsec = h->root.u.c.p->section;
6062
0
        break;
6063
6064
0
      default:
6065
0
        return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
6066
0
      }
6067
0
  }
6068
0
    }
6069
0
  else
6070
0
    {
6071
0
      struct _opd_sec_data *opd;
6072
6073
0
      rsec = _bfd_get_local_sym_section (cookie, symndx);
6074
0
      opd = get_opd_info (rsec);
6075
0
      if (opd != NULL && opd->func_sec != NULL)
6076
0
  {
6077
0
    rsec->gc_mark = 1;
6078
6079
0
    struct ppc_link_hash_table *htab = ppc_hash_table (info);
6080
0
    Elf_Internal_Sym *sym
6081
0
      = bfd_sym_from_r_symndx (&htab->elf.sym_cache, cookie->abfd,
6082
0
             symndx);
6083
0
    if (sym)
6084
0
      {
6085
0
        bfd_vma addr = sym->st_value + cookie->rel->r_addend;
6086
0
        rsec = opd->func_sec[OPD_NDX (addr)];
6087
0
      }
6088
0
  }
6089
0
    }
6090
6091
0
  return rsec;
6092
0
}
6093
6094
/* The maximum size of .sfpr.  */
6095
0
#define SFPR_MAX (218*4)
6096
6097
struct sfpr_def_parms
6098
{
6099
  const char name[12];
6100
  unsigned char lo, hi;
6101
  bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6102
  bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
6103
};
6104
6105
/* Auto-generate _save*, _rest* functions in .sfpr.
6106
   If STUB_SEC is non-null, define alias symbols in STUB_SEC
6107
   instead.  */
6108
6109
static bool
6110
sfpr_define (struct bfd_link_info *info,
6111
       const struct sfpr_def_parms *parm,
6112
       asection *stub_sec)
6113
0
{
6114
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
6115
0
  unsigned int i;
6116
0
  size_t len = strlen (parm->name);
6117
0
  bool writing = false;
6118
0
  char sym[16];
6119
6120
0
  if (htab == NULL)
6121
0
    return false;
6122
6123
0
  memcpy (sym, parm->name, len);
6124
0
  sym[len + 2] = 0;
6125
6126
0
  for (i = parm->lo; i <= parm->hi; i++)
6127
0
    {
6128
0
      struct ppc_link_hash_entry *h;
6129
6130
0
      sym[len + 0] = i / 10 + '0';
6131
0
      sym[len + 1] = i % 10 + '0';
6132
0
      h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
6133
0
                writing, true, true));
6134
0
      if (stub_sec != NULL)
6135
0
  {
6136
0
    if (h != NULL
6137
0
        && h->elf.root.type == bfd_link_hash_defined
6138
0
        && h->elf.root.u.def.section == htab->sfpr)
6139
0
      {
6140
0
        struct elf_link_hash_entry *s;
6141
0
        char buf[32];
6142
0
        sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6143
0
        s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
6144
0
        if (s == NULL)
6145
0
    return false;
6146
0
        if (s->root.type == bfd_link_hash_new)
6147
0
    {
6148
0
      s->root.type = bfd_link_hash_defined;
6149
0
      s->root.u.def.section = stub_sec;
6150
0
      s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6151
0
           + h->elf.root.u.def.value);
6152
0
      s->ref_regular = 1;
6153
0
      s->def_regular = 1;
6154
0
      s->ref_regular_nonweak = 1;
6155
0
      s->forced_local = 1;
6156
0
      s->non_elf = 0;
6157
0
      s->root.linker_def = 1;
6158
0
    }
6159
0
      }
6160
0
    continue;
6161
0
  }
6162
0
      if (h != NULL)
6163
0
  {
6164
0
    h->save_res = 1;
6165
0
    if (!h->elf.def_regular)
6166
0
      {
6167
0
        h->elf.root.type = bfd_link_hash_defined;
6168
0
        h->elf.root.u.def.section = htab->sfpr;
6169
0
        h->elf.root.u.def.value = htab->sfpr->size;
6170
0
        h->elf.type = STT_FUNC;
6171
0
        h->elf.def_regular = 1;
6172
0
        h->elf.non_elf = 0;
6173
0
        _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6174
0
        writing = true;
6175
0
        if (htab->sfpr->contents == NULL)
6176
0
    {
6177
0
      htab->sfpr->contents
6178
0
        = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6179
0
      if (htab->sfpr->contents == NULL)
6180
0
        return false;
6181
0
      htab->sfpr->alloced = 1;
6182
0
    }
6183
0
      }
6184
0
  }
6185
0
      if (writing)
6186
0
  {
6187
0
    bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6188
0
    if (i != parm->hi)
6189
0
      p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6190
0
    else
6191
0
      p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6192
0
    htab->sfpr->size = p - htab->sfpr->contents;
6193
0
  }
6194
0
    }
6195
6196
0
  return true;
6197
0
}
6198
6199
static bfd_byte *
6200
savegpr0 (bfd *abfd, bfd_byte *p, int r)
6201
0
{
6202
0
  bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6203
0
  return p + 4;
6204
0
}
6205
6206
static bfd_byte *
6207
savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6208
0
{
6209
0
  p = savegpr0 (abfd, p, r);
6210
0
  bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6211
0
  p = p + 4;
6212
0
  bfd_put_32 (abfd, BLR, p);
6213
0
  return p + 4;
6214
0
}
6215
6216
static bfd_byte *
6217
restgpr0 (bfd *abfd, bfd_byte *p, int r)
6218
0
{
6219
0
  bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6220
0
  return p + 4;
6221
0
}
6222
6223
static bfd_byte *
6224
restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6225
0
{
6226
0
  bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6227
0
  p = p + 4;
6228
0
  p = restgpr0 (abfd, p, r);
6229
0
  bfd_put_32 (abfd, MTLR_R0, p);
6230
0
  p = p + 4;
6231
0
  if (r == 29)
6232
0
    {
6233
0
      p = restgpr0 (abfd, p, 30);
6234
0
      p = restgpr0 (abfd, p, 31);
6235
0
    }
6236
0
  bfd_put_32 (abfd, BLR, p);
6237
0
  return p + 4;
6238
0
}
6239
6240
static bfd_byte *
6241
savegpr1 (bfd *abfd, bfd_byte *p, int r)
6242
0
{
6243
0
  bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6244
0
  return p + 4;
6245
0
}
6246
6247
static bfd_byte *
6248
savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6249
0
{
6250
0
  p = savegpr1 (abfd, p, r);
6251
0
  bfd_put_32 (abfd, BLR, p);
6252
0
  return p + 4;
6253
0
}
6254
6255
static bfd_byte *
6256
restgpr1 (bfd *abfd, bfd_byte *p, int r)
6257
0
{
6258
0
  bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6259
0
  return p + 4;
6260
0
}
6261
6262
static bfd_byte *
6263
restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6264
0
{
6265
0
  p = restgpr1 (abfd, p, r);
6266
0
  bfd_put_32 (abfd, BLR, p);
6267
0
  return p + 4;
6268
0
}
6269
6270
static bfd_byte *
6271
savefpr (bfd *abfd, bfd_byte *p, int r)
6272
0
{
6273
0
  bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6274
0
  return p + 4;
6275
0
}
6276
6277
static bfd_byte *
6278
savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6279
0
{
6280
0
  p = savefpr (abfd, p, r);
6281
0
  bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6282
0
  p = p + 4;
6283
0
  bfd_put_32 (abfd, BLR, p);
6284
0
  return p + 4;
6285
0
}
6286
6287
static bfd_byte *
6288
restfpr (bfd *abfd, bfd_byte *p, int r)
6289
0
{
6290
0
  bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6291
0
  return p + 4;
6292
0
}
6293
6294
static bfd_byte *
6295
restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6296
0
{
6297
0
  bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6298
0
  p = p + 4;
6299
0
  p = restfpr (abfd, p, r);
6300
0
  bfd_put_32 (abfd, MTLR_R0, p);
6301
0
  p = p + 4;
6302
0
  if (r == 29)
6303
0
    {
6304
0
      p = restfpr (abfd, p, 30);
6305
0
      p = restfpr (abfd, p, 31);
6306
0
    }
6307
0
  bfd_put_32 (abfd, BLR, p);
6308
0
  return p + 4;
6309
0
}
6310
6311
static bfd_byte *
6312
savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6313
0
{
6314
0
  p = savefpr (abfd, p, r);
6315
0
  bfd_put_32 (abfd, BLR, p);
6316
0
  return p + 4;
6317
0
}
6318
6319
static bfd_byte *
6320
restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6321
0
{
6322
0
  p = restfpr (abfd, p, r);
6323
0
  bfd_put_32 (abfd, BLR, p);
6324
0
  return p + 4;
6325
0
}
6326
6327
static bfd_byte *
6328
savevr (bfd *abfd, bfd_byte *p, int r)
6329
0
{
6330
0
  bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6331
0
  p = p + 4;
6332
0
  bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6333
0
  return p + 4;
6334
0
}
6335
6336
static bfd_byte *
6337
savevr_tail (bfd *abfd, bfd_byte *p, int r)
6338
0
{
6339
0
  p = savevr (abfd, p, r);
6340
0
  bfd_put_32 (abfd, BLR, p);
6341
0
  return p + 4;
6342
0
}
6343
6344
static bfd_byte *
6345
restvr (bfd *abfd, bfd_byte *p, int r)
6346
0
{
6347
0
  bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6348
0
  p = p + 4;
6349
0
  bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6350
0
  return p + 4;
6351
0
}
6352
6353
static bfd_byte *
6354
restvr_tail (bfd *abfd, bfd_byte *p, int r)
6355
0
{
6356
0
  p = restvr (abfd, p, r);
6357
0
  bfd_put_32 (abfd, BLR, p);
6358
0
  return p + 4;
6359
0
}
6360
6361
#define STDU_R1_0R1 0xf8210001
6362
#define ADDI_R1_R1  0x38210000
6363
6364
/* Emit prologue of wrapper preserving regs around a call to
6365
   __tls_get_addr_opt.  */
6366
6367
static bfd_byte *
6368
tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6369
0
{
6370
0
  unsigned int i;
6371
6372
0
  bfd_put_32 (obfd, MFLR_R0, p);
6373
0
  p += 4;
6374
0
  bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6375
0
  p += 4;
6376
6377
0
  if (htab->opd_abi)
6378
0
    {
6379
0
      for (i = 4; i < 12; i++)
6380
0
  {
6381
0
    bfd_put_32 (obfd,
6382
0
          STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6383
0
    p += 4;
6384
0
  }
6385
0
      bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6386
0
      p += 4;
6387
0
    }
6388
0
  else
6389
0
    {
6390
0
      for (i = 4; i < 12; i++)
6391
0
  {
6392
0
    bfd_put_32 (obfd,
6393
0
          STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6394
0
    p += 4;
6395
0
  }
6396
0
      bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6397
0
      p += 4;
6398
0
    }
6399
0
  return p;
6400
0
}
6401
6402
/* Emit epilogue of wrapper preserving regs around a call to
6403
   __tls_get_addr_opt.  */
6404
6405
static bfd_byte *
6406
tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6407
0
{
6408
0
  unsigned int i;
6409
6410
0
  if (htab->opd_abi)
6411
0
    {
6412
0
      for (i = 4; i < 12; i++)
6413
0
  {
6414
0
    bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6415
0
    p += 4;
6416
0
  }
6417
0
      bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6418
0
      p += 4;
6419
0
    }
6420
0
  else
6421
0
    {
6422
0
      for (i = 4; i < 12; i++)
6423
0
  {
6424
0
    bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6425
0
    p += 4;
6426
0
  }
6427
0
      bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6428
0
      p += 4;
6429
0
    }
6430
0
  bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6431
0
  p += 4;
6432
0
  bfd_put_32 (obfd, MTLR_R0, p);
6433
0
  p += 4;
6434
0
  bfd_put_32 (obfd, BLR, p);
6435
0
  p += 4;
6436
0
  return p;
6437
0
}
6438
6439
/* Called via elf_link_hash_traverse to transfer dynamic linking
6440
   information on function code symbol entries to their corresponding
6441
   function descriptor symbol entries.  Must not be called twice for
6442
   any given code symbol.  */
6443
6444
static bool
6445
func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6446
0
{
6447
0
  struct bfd_link_info *info;
6448
0
  struct ppc_link_hash_table *htab;
6449
0
  struct ppc_link_hash_entry *fh;
6450
0
  struct ppc_link_hash_entry *fdh;
6451
0
  bool force_local;
6452
6453
0
  fh = ppc_elf_hash_entry (h);
6454
0
  if (fh->elf.root.type == bfd_link_hash_indirect)
6455
0
    return true;
6456
6457
0
  if (!fh->is_func)
6458
0
    return true;
6459
6460
0
  if (fh->elf.root.root.string[0] != '.'
6461
0
      || fh->elf.root.root.string[1] == '\0')
6462
0
    return true;
6463
6464
0
  info = inf;
6465
0
  htab = ppc_hash_table (info);
6466
0
  if (htab == NULL)
6467
0
    return false;
6468
6469
  /* Find the corresponding function descriptor symbol.  */
6470
0
  fdh = lookup_fdh (fh, htab);
6471
6472
  /* Resolve undefined references to dot-symbols as the value
6473
     in the function descriptor, if we have one in a regular object.
6474
     This is to satisfy cases like ".quad .foo".  Calls to functions
6475
     in dynamic objects are handled elsewhere.  */
6476
0
  if ((fh->elf.root.type == bfd_link_hash_undefined
6477
0
       || fh->elf.root.type == bfd_link_hash_undefweak)
6478
0
      && (fdh->elf.root.type == bfd_link_hash_defined
6479
0
    || fdh->elf.root.type == bfd_link_hash_defweak)
6480
0
      && get_opd_info (fdh->elf.root.u.def.section) != NULL
6481
0
      && opd_entry_value (fdh->elf.root.u.def.section,
6482
0
        fdh->elf.root.u.def.value,
6483
0
        &fh->elf.root.u.def.section,
6484
0
        &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
6485
0
    {
6486
0
      fh->elf.root.type = fdh->elf.root.type;
6487
0
      fh->elf.forced_local = 1;
6488
0
      fh->elf.def_regular = fdh->elf.def_regular;
6489
0
      fh->elf.def_dynamic = fdh->elf.def_dynamic;
6490
0
    }
6491
6492
0
  if (!fh->elf.dynamic)
6493
0
    {
6494
0
      struct plt_entry *ent;
6495
6496
0
      for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6497
0
  if (ent->plt.refcount > 0)
6498
0
    break;
6499
0
      if (ent == NULL)
6500
0
  {
6501
0
    if (fdh != NULL && fdh->fake)
6502
0
      _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6503
0
    return true;
6504
0
  }
6505
0
    }
6506
6507
  /* Create a descriptor as undefined if necessary.  */
6508
0
  if (fdh == NULL
6509
0
      && !bfd_link_executable (info)
6510
0
      && (fh->elf.root.type == bfd_link_hash_undefined
6511
0
    || fh->elf.root.type == bfd_link_hash_undefweak))
6512
0
    {
6513
0
      fdh = make_fdh (info, fh);
6514
0
      if (fdh == NULL)
6515
0
  return false;
6516
0
    }
6517
6518
  /* We can't support overriding of symbols on a fake descriptor.  */
6519
0
  if (fdh != NULL
6520
0
      && fdh->fake
6521
0
      && (fh->elf.root.type == bfd_link_hash_defined
6522
0
    || fh->elf.root.type == bfd_link_hash_defweak))
6523
0
    _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6524
6525
  /* Transfer dynamic linking information to the function descriptor.  */
6526
0
  if (fdh != NULL)
6527
0
    {
6528
0
      fdh->elf.ref_regular |= fh->elf.ref_regular;
6529
0
      fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6530
0
      fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6531
0
      fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6532
0
      fdh->elf.dynamic |= fh->elf.dynamic;
6533
0
      fdh->elf.needs_plt |= (fh->elf.needs_plt
6534
0
           || fh->elf.type == STT_FUNC
6535
0
           || fh->elf.type == STT_GNU_IFUNC);
6536
0
      move_plt_plist (fh, fdh);
6537
6538
0
      if (!fdh->elf.forced_local
6539
0
    && fh->elf.dynindx != -1)
6540
0
  if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6541
0
    return false;
6542
0
    }
6543
6544
  /* Now that the info is on the function descriptor, clear the
6545
     function code sym info.  Any function code syms for which we
6546
     don't have a definition in a regular file, we force local.
6547
     This prevents a shared library from exporting syms that have
6548
     been imported from another library.  Function code syms that
6549
     are really in the library we must leave global to prevent the
6550
     linker dragging in a definition from a static library.  */
6551
0
  force_local = (!fh->elf.def_regular
6552
0
     || fdh == NULL
6553
0
     || !fdh->elf.def_regular
6554
0
     || fdh->elf.forced_local);
6555
0
  _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6556
6557
0
  return true;
6558
0
}
6559
6560
static const struct sfpr_def_parms save_res_funcs[] =
6561
  {
6562
    { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6563
    { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6564
    { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6565
    { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6566
    { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6567
    { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6568
    { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6569
    { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6570
    { "._savef", 14, 31, savefpr, savefpr1_tail },
6571
    { "._restf", 14, 31, restfpr, restfpr1_tail },
6572
    { "_savevr_", 20, 31, savevr, savevr_tail },
6573
    { "_restvr_", 20, 31, restvr, restvr_tail }
6574
  };
6575
6576
/* Called near the start of bfd_elf_size_dynamic_sections.  We use
6577
   this hook to a) run the edit functions in this file, b) provide
6578
   some gcc support functions, and c) transfer dynamic linking
6579
   information gathered so far on function code symbol entries, to
6580
   their corresponding function descriptor symbol entries.  */
6581
6582
static bool
6583
ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6584
0
{
6585
0
  struct ppc_link_hash_table *htab;
6586
6587
0
  htab = ppc_hash_table (info);
6588
0
  if (htab == NULL)
6589
0
    return false;
6590
6591
  /* Call back into the linker, which then runs the edit functions.  */
6592
0
  htab->params->edit ();
6593
6594
  /* Provide any missing _save* and _rest* functions.  */
6595
0
  if (htab->sfpr != NULL)
6596
0
    {
6597
0
      unsigned int i;
6598
6599
0
      htab->sfpr->size = 0;
6600
0
      for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6601
0
  if (!sfpr_define (info, &save_res_funcs[i], NULL))
6602
0
    return false;
6603
0
      if (htab->sfpr->size == 0)
6604
0
  htab->sfpr->flags |= SEC_EXCLUDE;
6605
0
    }
6606
6607
0
  if (bfd_link_relocatable (info))
6608
0
    return true;
6609
6610
0
  if (htab->elf.hgot != NULL)
6611
0
    {
6612
0
      _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
6613
      /* Make .TOC. defined so as to prevent it being made dynamic.
6614
   The wrong value here is fixed later in ppc64_elf_set_toc.  */
6615
0
      if (!htab->elf.hgot->def_regular
6616
0
    || htab->elf.hgot->root.type != bfd_link_hash_defined)
6617
0
  {
6618
0
    htab->elf.hgot->root.type = bfd_link_hash_defined;
6619
0
    htab->elf.hgot->root.u.def.value = 0;
6620
0
    htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6621
0
    htab->elf.hgot->def_regular = 1;
6622
0
    htab->elf.hgot->root.linker_def = 1;
6623
0
  }
6624
0
      htab->elf.hgot->type = STT_OBJECT;
6625
0
      htab->elf.hgot->other
6626
0
  = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6627
0
    }
6628
6629
0
  return true;
6630
0
}
6631
6632
/* Return true if we have dynamic relocs against H or any of its weak
6633
   aliases, that apply to read-only sections.  Cannot be used after
6634
   size_dynamic_sections.  */
6635
6636
static bool
6637
alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6638
0
{
6639
0
  struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6640
0
  do
6641
0
    {
6642
0
      if (_bfd_elf_readonly_dynrelocs (&eh->elf))
6643
0
  return true;
6644
0
      eh = ppc_elf_hash_entry (eh->elf.u.alias);
6645
0
    }
6646
0
  while (eh != NULL && &eh->elf != h);
6647
6648
0
  return false;
6649
0
}
6650
6651
/* Return whether EH has pc-relative dynamic relocs.  */
6652
6653
static bool
6654
pc_dynrelocs (struct ppc_link_hash_entry *eh)
6655
0
{
6656
0
  struct ppc_dyn_relocs *p;
6657
6658
0
  for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
6659
0
    if (p->pc_count != 0)
6660
0
      return true;
6661
0
  return false;
6662
0
}
6663
6664
/* Return true if a global entry stub will be created for H.  Valid
6665
   for ELFv2 before plt entries have been allocated.  */
6666
6667
static bool
6668
global_entry_stub (struct elf_link_hash_entry *h)
6669
0
{
6670
0
  struct plt_entry *pent;
6671
6672
0
  if (!h->pointer_equality_needed
6673
0
      || h->def_regular)
6674
0
    return false;
6675
6676
0
  for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6677
0
    if (pent->plt.refcount > 0
6678
0
  && pent->addend == 0)
6679
0
      return true;
6680
6681
0
  return false;
6682
0
}
6683
6684
/* Adjust a symbol defined by a dynamic object and referenced by a
6685
   regular object.  The current definition is in some section of the
6686
   dynamic object, but we're not including those sections.  We have to
6687
   change the definition to something the rest of the link can
6688
   understand.  */
6689
6690
static bool
6691
ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6692
         struct elf_link_hash_entry *h)
6693
0
{
6694
0
  struct ppc_link_hash_table *htab;
6695
0
  asection *s, *srel;
6696
6697
0
  htab = ppc_hash_table (info);
6698
0
  if (htab == NULL)
6699
0
    return false;
6700
6701
  /* Deal with function syms.  */
6702
0
  if (h->type == STT_FUNC
6703
0
      || h->type == STT_GNU_IFUNC
6704
0
      || h->needs_plt)
6705
0
    {
6706
0
      bool local = (ppc_elf_hash_entry (h)->save_res
6707
0
        || SYMBOL_CALLS_LOCAL (info, h)
6708
0
        || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6709
      /* Discard dyn_relocs when non-pic if we've decided that a
6710
   function symbol is local and not an ifunc.  We keep dynamic
6711
   relocs for ifuncs when local rather than always emitting a
6712
   plt call stub for them and defining the symbol on the call
6713
   stub.  We can't do that for ELFv1 anyway (a function symbol
6714
   is defined on a descriptor, not code) and it can be faster at
6715
   run-time due to not needing to bounce through a stub.  The
6716
   dyn_relocs for ifuncs will be applied even in a static
6717
   executable.  */
6718
0
      if (!bfd_link_pic (info)
6719
0
    && h->type != STT_GNU_IFUNC
6720
0
    && local)
6721
0
  h->dyn_relocs = NULL;
6722
6723
      /* Clear procedure linkage table information for any symbol that
6724
   won't need a .plt entry.  */
6725
0
      struct plt_entry *ent;
6726
0
      for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6727
0
  if (ent->plt.refcount > 0)
6728
0
    break;
6729
0
      if (ent == NULL
6730
0
    || (h->type != STT_GNU_IFUNC
6731
0
        && local
6732
0
        && (htab->can_convert_all_inline_plt
6733
0
      || (ppc_elf_hash_entry (h)->tls_mask
6734
0
          & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6735
0
  {
6736
0
    h->plt.plist = NULL;
6737
0
    h->needs_plt = 0;
6738
0
    h->pointer_equality_needed = 0;
6739
0
  }
6740
0
      else if (abiversion (info->output_bfd) >= 2)
6741
0
  {
6742
    /* Taking a function's address in a read/write section
6743
       doesn't require us to define the function symbol in the
6744
       executable on a global entry stub.  A dynamic reloc can
6745
       be used instead.  The reason we prefer a few more dynamic
6746
       relocs is that calling via a global entry stub costs a
6747
       few more instructions, and pointer_equality_needed causes
6748
       extra work in ld.so when resolving these symbols.  */
6749
0
    if (global_entry_stub (h))
6750
0
      {
6751
0
        if (!_bfd_elf_readonly_dynrelocs (h))
6752
0
    {
6753
0
      h->pointer_equality_needed = 0;
6754
      /* If we haven't seen a branch reloc and the symbol
6755
         isn't an ifunc then we don't need a plt entry.  */
6756
0
      if (!h->needs_plt)
6757
0
        h->plt.plist = NULL;
6758
0
    }
6759
0
        else if (!bfd_link_pic (info))
6760
    /* We are going to be defining the function symbol on the
6761
       plt stub, so no dyn_relocs needed when non-pic.  */
6762
0
    h->dyn_relocs = NULL;
6763
0
      }
6764
6765
    /* ELFv2 function symbols can't have copy relocs.  */
6766
0
    return true;
6767
0
  }
6768
0
      else if (!h->needs_plt
6769
0
         && !_bfd_elf_readonly_dynrelocs (h))
6770
0
  {
6771
    /* If we haven't seen a branch reloc and the symbol isn't an
6772
       ifunc then we don't need a plt entry.  */
6773
0
    h->plt.plist = NULL;
6774
0
    h->pointer_equality_needed = 0;
6775
0
    return true;
6776
0
  }
6777
0
    }
6778
0
  else
6779
0
    h->plt.plist = NULL;
6780
6781
  /* If this is a weak symbol, and there is a real definition, the
6782
     processor independent code will have arranged for us to see the
6783
     real definition first, and we can just use the same value.  */
6784
0
  if (h->is_weakalias)
6785
0
    {
6786
0
      struct elf_link_hash_entry *def = weakdef (h);
6787
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6788
0
      h->root.u.def.section = def->root.u.def.section;
6789
0
      h->root.u.def.value = def->root.u.def.value;
6790
0
      if (def->root.u.def.section == htab->elf.sdynbss
6791
0
    || def->root.u.def.section == htab->elf.sdynrelro)
6792
0
  h->dyn_relocs = NULL;
6793
0
      return true;
6794
0
    }
6795
6796
  /* If we are creating a shared library, we must presume that the
6797
     only references to the symbol are via the global offset table.
6798
     For such cases we need not do anything here; the relocations will
6799
     be handled correctly by relocate_section.  */
6800
0
  if (!bfd_link_executable (info))
6801
0
    return true;
6802
6803
  /* If there are no references to this symbol that do not use the
6804
     GOT, we don't need to generate a copy reloc.  */
6805
0
  if (!h->non_got_ref)
6806
0
    return true;
6807
6808
  /* Don't generate a copy reloc for symbols defined in the executable.  */
6809
0
  if (!h->def_dynamic || !h->ref_regular || h->def_regular
6810
6811
      /* If -z nocopyreloc was given, don't generate them either.  */
6812
0
      || info->nocopyreloc
6813
6814
      /* If we don't find any dynamic relocs in read-only sections, then
6815
   we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
6816
0
      || (ELIMINATE_COPY_RELOCS
6817
0
    && !h->needs_copy
6818
0
    && !alias_readonly_dynrelocs (h))
6819
6820
      /* Protected variables do not work with .dynbss.  The copy in
6821
   .dynbss won't be used by the shared library with the protected
6822
   definition for the variable.  Text relocations are preferable
6823
   to an incorrect program.  */
6824
0
      || h->protected_def)
6825
0
    return true;
6826
6827
0
  if (h->type == STT_FUNC
6828
0
      || h->type == STT_GNU_IFUNC)
6829
0
    {
6830
      /* .dynbss copies of function symbols only work if we have
6831
   ELFv1 dot-symbols.  ELFv1 compilers since 2004 default to not
6832
   use dot-symbols and set the function symbol size to the text
6833
   size of the function rather than the size of the descriptor.
6834
   That's wrong for copying a descriptor.  */
6835
0
      if (ppc_elf_hash_entry (h)->oh == NULL
6836
0
    || !(h->size == 24 || h->size == 16))
6837
0
  return true;
6838
6839
      /* We should never get here, but unfortunately there are old
6840
   versions of gcc (circa gcc-3.2) that improperly for the
6841
   ELFv1 ABI put initialized function pointers, vtable refs and
6842
   suchlike in read-only sections.  Allow them to proceed, but
6843
   warn that this might break at runtime.  */
6844
0
      info->callbacks->einfo
6845
0
  (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6846
0
     "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6847
0
   h->root.root.string);
6848
0
    }
6849
6850
  /* This is a reference to a symbol defined by a dynamic object which
6851
     is not a function.  */
6852
6853
  /* We must allocate the symbol in our .dynbss section, which will
6854
     become part of the .bss section of the executable.  There will be
6855
     an entry for this symbol in the .dynsym section.  The dynamic
6856
     object will contain position independent code, so all references
6857
     from the dynamic object to this symbol will go through the global
6858
     offset table.  The dynamic linker will use the .dynsym entry to
6859
     determine the address it must put in the global offset table, so
6860
     both the dynamic object and the regular object will refer to the
6861
     same memory location for the variable.  */
6862
0
  if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6863
0
    {
6864
0
      s = htab->elf.sdynrelro;
6865
0
      srel = htab->elf.sreldynrelro;
6866
0
    }
6867
0
  else
6868
0
    {
6869
0
      s = htab->elf.sdynbss;
6870
0
      srel = htab->elf.srelbss;
6871
0
    }
6872
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6873
0
    {
6874
      /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6875
   linker to copy the initial value out of the dynamic object
6876
   and into the runtime process image.  */
6877
0
      srel->size += sizeof (Elf64_External_Rela);
6878
0
      h->needs_copy = 1;
6879
0
    }
6880
6881
  /* We no longer want dyn_relocs.  */
6882
0
  h->dyn_relocs = NULL;
6883
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
6884
0
}
6885
6886
/* If given a function descriptor symbol, hide both the function code
6887
   sym and the descriptor.  */
6888
static void
6889
ppc64_elf_hide_symbol (struct bfd_link_info *info,
6890
           struct elf_link_hash_entry *h,
6891
           bool force_local)
6892
0
{
6893
0
  struct ppc_link_hash_entry *eh;
6894
0
  _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6895
6896
0
  if (ppc_hash_table (info) == NULL)
6897
0
    return;
6898
6899
0
  eh = ppc_elf_hash_entry (h);
6900
0
  if (eh->is_func_descriptor)
6901
0
    {
6902
0
      struct ppc_link_hash_entry *fh = eh->oh;
6903
6904
0
      if (fh == NULL)
6905
0
  {
6906
0
    const char *p, *q;
6907
0
    struct elf_link_hash_table *htab = elf_hash_table (info);
6908
0
    char save;
6909
6910
    /* We aren't supposed to use alloca in BFD because on
6911
       systems which do not have alloca the version in libiberty
6912
       calls xmalloc, which might cause the program to crash
6913
       when it runs out of memory.  This function doesn't have a
6914
       return status, so there's no way to gracefully return an
6915
       error.  So cheat.  We know that string[-1] can be safely
6916
       accessed;  It's either a string in an ELF string table,
6917
       or allocated in an objalloc structure.  */
6918
6919
0
    p = eh->elf.root.root.string - 1;
6920
0
    save = *p;
6921
0
    *(char *) p = '.';
6922
0
    fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6923
0
               false, false));
6924
0
    *(char *) p = save;
6925
6926
    /* Unfortunately, if it so happens that the string we were
6927
       looking for was allocated immediately before this string,
6928
       then we overwrote the string terminator.  That's the only
6929
       reason the lookup should fail.  */
6930
0
    if (fh == NULL)
6931
0
      {
6932
0
        q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6933
0
        while (q >= eh->elf.root.root.string && *q == *p)
6934
0
    --q, --p;
6935
0
        if (q < eh->elf.root.root.string && *p == '.')
6936
0
    fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6937
0
                     false, false));
6938
0
      }
6939
0
    if (fh != NULL)
6940
0
      {
6941
0
        eh->oh = fh;
6942
0
        fh->oh = eh;
6943
0
      }
6944
0
  }
6945
0
      if (fh != NULL)
6946
0
  _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6947
0
    }
6948
0
}
6949
6950
static bool
6951
get_sym_h (struct elf_link_hash_entry **hp,
6952
     Elf_Internal_Sym **symp,
6953
     asection **symsecp,
6954
     unsigned char **tls_maskp,
6955
     Elf_Internal_Sym **locsymsp,
6956
     unsigned long r_symndx,
6957
     bfd *ibfd)
6958
0
{
6959
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6960
6961
0
  if (r_symndx >= symtab_hdr->sh_info)
6962
0
    {
6963
0
      struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6964
0
      struct elf_link_hash_entry *h;
6965
6966
0
      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6967
0
      h = elf_follow_link (h);
6968
6969
0
      if (hp != NULL)
6970
0
  *hp = h;
6971
6972
0
      if (symp != NULL)
6973
0
  *symp = NULL;
6974
6975
0
      if (symsecp != NULL)
6976
0
  {
6977
0
    asection *symsec = NULL;
6978
0
    if (h->root.type == bfd_link_hash_defined
6979
0
        || h->root.type == bfd_link_hash_defweak)
6980
0
      symsec = h->root.u.def.section;
6981
0
    *symsecp = symsec;
6982
0
  }
6983
6984
0
      if (tls_maskp != NULL)
6985
0
  *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
6986
0
    }
6987
0
  else
6988
0
    {
6989
0
      Elf_Internal_Sym *sym;
6990
0
      Elf_Internal_Sym *locsyms = *locsymsp;
6991
6992
0
      if (locsyms == NULL)
6993
0
  {
6994
0
    locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6995
0
    if (locsyms == NULL)
6996
0
      locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6997
0
              symtab_hdr->sh_info,
6998
0
              0, NULL, NULL, NULL);
6999
0
    if (locsyms == NULL)
7000
0
      return false;
7001
0
    *locsymsp = locsyms;
7002
0
  }
7003
0
      sym = locsyms + r_symndx;
7004
7005
0
      if (hp != NULL)
7006
0
  *hp = NULL;
7007
7008
0
      if (symp != NULL)
7009
0
  *symp = sym;
7010
7011
0
      if (symsecp != NULL)
7012
0
  *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7013
7014
0
      if (tls_maskp != NULL)
7015
0
  {
7016
0
    struct got_entry **lgot_ents;
7017
0
    unsigned char *tls_mask;
7018
7019
0
    tls_mask = NULL;
7020
0
    lgot_ents = elf_local_got_ents (ibfd);
7021
0
    if (lgot_ents != NULL)
7022
0
      {
7023
0
        struct plt_entry **local_plt = (struct plt_entry **)
7024
0
    (lgot_ents + symtab_hdr->sh_info);
7025
0
        unsigned char *lgot_masks = (unsigned char *)
7026
0
    (local_plt + symtab_hdr->sh_info);
7027
0
        tls_mask = &lgot_masks[r_symndx];
7028
0
      }
7029
0
    *tls_maskp = tls_mask;
7030
0
  }
7031
0
    }
7032
0
  return true;
7033
0
}
7034
7035
/* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
7036
   error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7037
   type suitable for optimization, and 1 otherwise.  */
7038
7039
static int
7040
get_tls_mask (unsigned char **tls_maskp,
7041
        unsigned long *toc_symndx,
7042
        bfd_vma *toc_addend,
7043
        Elf_Internal_Sym **locsymsp,
7044
        const Elf_Internal_Rela *rel,
7045
        bfd *ibfd)
7046
0
{
7047
0
  unsigned long r_symndx;
7048
0
  int next_r;
7049
0
  struct elf_link_hash_entry *h;
7050
0
  Elf_Internal_Sym *sym;
7051
0
  asection *sec;
7052
0
  bfd_vma off;
7053
7054
0
  r_symndx = ELF64_R_SYM (rel->r_info);
7055
0
  if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7056
0
    return 0;
7057
7058
0
  if ((*tls_maskp != NULL
7059
0
       && (**tls_maskp & TLS_TLS) != 0
7060
0
       && **tls_maskp != (TLS_TLS | TLS_MARK))
7061
0
      || sec == NULL
7062
0
      || ppc64_elf_section_data (sec) == NULL
7063
0
      || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7064
0
    return 1;
7065
7066
  /* Look inside a TOC section too.  */
7067
0
  if (h != NULL)
7068
0
    {
7069
0
      BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7070
0
      off = h->root.u.def.value;
7071
0
    }
7072
0
  else
7073
0
    off = sym->st_value;
7074
0
  off += rel->r_addend;
7075
0
  BFD_ASSERT (off % 8 == 0);
7076
0
  r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7077
0
  next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7078
0
  if (toc_symndx != NULL)
7079
0
    *toc_symndx = r_symndx;
7080
0
  if (toc_addend != NULL)
7081
0
    *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7082
0
  if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7083
0
    return 0;
7084
0
  if ((h == NULL || is_static_defined (h))
7085
0
      && (next_r == -1 || next_r == -2))
7086
0
    return 1 - next_r;
7087
0
  return 1;
7088
0
}
7089
7090
/* Find (or create) an entry in the tocsave hash table.  */
7091
7092
static struct tocsave_entry *
7093
tocsave_find (struct ppc_link_hash_table *htab,
7094
        enum insert_option insert,
7095
        Elf_Internal_Sym **local_syms,
7096
        const Elf_Internal_Rela *irela,
7097
        bfd *ibfd)
7098
0
{
7099
0
  unsigned long r_indx;
7100
0
  struct elf_link_hash_entry *h;
7101
0
  Elf_Internal_Sym *sym;
7102
0
  struct tocsave_entry ent, *p;
7103
0
  hashval_t hash;
7104
0
  struct tocsave_entry **slot;
7105
7106
0
  r_indx = ELF64_R_SYM (irela->r_info);
7107
0
  if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7108
0
    return NULL;
7109
0
  if (ent.sec == NULL || ent.sec->output_section == NULL)
7110
0
    {
7111
0
      _bfd_error_handler
7112
0
  (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7113
0
      return NULL;
7114
0
    }
7115
7116
0
  if (h != NULL)
7117
0
    ent.offset = h->root.u.def.value;
7118
0
  else
7119
0
    ent.offset = sym->st_value;
7120
0
  ent.offset += irela->r_addend;
7121
7122
0
  hash = tocsave_htab_hash (&ent);
7123
0
  slot = ((struct tocsave_entry **)
7124
0
    htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7125
0
  if (slot == NULL)
7126
0
    return NULL;
7127
7128
0
  if (*slot == NULL)
7129
0
    {
7130
0
      p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7131
0
      if (p == NULL)
7132
0
  return NULL;
7133
0
      *p = ent;
7134
0
      *slot = p;
7135
0
    }
7136
0
  return *slot;
7137
0
}
7138
7139
/* Adjust all global syms defined in opd sections.  In gcc generated
7140
   code for the old ABI, these will already have been done.  */
7141
7142
static bool
7143
adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7144
0
{
7145
0
  struct ppc_link_hash_entry *eh;
7146
0
  asection *sym_sec;
7147
0
  struct _opd_sec_data *opd;
7148
7149
0
  if (h->root.type == bfd_link_hash_indirect)
7150
0
    return true;
7151
7152
0
  if (h->root.type != bfd_link_hash_defined
7153
0
      && h->root.type != bfd_link_hash_defweak)
7154
0
    return true;
7155
7156
0
  eh = ppc_elf_hash_entry (h);
7157
0
  if (eh->adjust_done)
7158
0
    return true;
7159
7160
0
  sym_sec = eh->elf.root.u.def.section;
7161
0
  opd = get_opd_info (sym_sec);
7162
0
  if (opd != NULL && opd->adjust != NULL)
7163
0
    {
7164
0
      long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7165
0
      if (adjust == -1)
7166
0
  {
7167
    /* This entry has been deleted.  */
7168
0
    asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7169
0
    if (dsec == NULL)
7170
0
      {
7171
0
        for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7172
0
    if (discarded_section (dsec))
7173
0
      {
7174
0
        ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7175
0
        break;
7176
0
      }
7177
0
      }
7178
0
    eh->elf.root.u.def.value = 0;
7179
0
    eh->elf.root.u.def.section = dsec;
7180
0
  }
7181
0
      else
7182
0
  eh->elf.root.u.def.value += adjust;
7183
0
      eh->adjust_done = 1;
7184
0
    }
7185
0
  return true;
7186
0
}
7187
7188
/* Handles decrementing dynamic reloc counts for the reloc specified by
7189
   R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7190
   have already been determined.  */
7191
7192
static bool
7193
dec_dynrel_count (const Elf_Internal_Rela *rel,
7194
      asection *sec,
7195
      struct bfd_link_info *info,
7196
      Elf_Internal_Sym **local_syms,
7197
      struct elf_link_hash_entry *h,
7198
      Elf_Internal_Sym *sym)
7199
0
{
7200
0
  enum elf_ppc64_reloc_type r_type;
7201
0
  asection *sym_sec = NULL;
7202
7203
  /* Can this reloc be dynamic?  This switch, and later tests here
7204
     should be kept in sync with the code in check_relocs.  */
7205
0
  r_type = ELF64_R_TYPE (rel->r_info);
7206
0
  switch (r_type)
7207
0
    {
7208
0
    default:
7209
0
      return true;
7210
7211
0
    case R_PPC64_TOC16:
7212
0
    case R_PPC64_TOC16_DS:
7213
0
    case R_PPC64_TOC16_LO:
7214
0
    case R_PPC64_TOC16_HI:
7215
0
    case R_PPC64_TOC16_HA:
7216
0
    case R_PPC64_TOC16_LO_DS:
7217
0
      if (h == NULL)
7218
0
  return true;
7219
0
      break;
7220
7221
0
    case R_PPC64_TPREL16:
7222
0
    case R_PPC64_TPREL16_LO:
7223
0
    case R_PPC64_TPREL16_HI:
7224
0
    case R_PPC64_TPREL16_HA:
7225
0
    case R_PPC64_TPREL16_DS:
7226
0
    case R_PPC64_TPREL16_LO_DS:
7227
0
    case R_PPC64_TPREL16_HIGH:
7228
0
    case R_PPC64_TPREL16_HIGHA:
7229
0
    case R_PPC64_TPREL16_HIGHER:
7230
0
    case R_PPC64_TPREL16_HIGHERA:
7231
0
    case R_PPC64_TPREL16_HIGHEST:
7232
0
    case R_PPC64_TPREL16_HIGHESTA:
7233
0
    case R_PPC64_TPREL64:
7234
0
    case R_PPC64_TPREL34:
7235
0
    case R_PPC64_DTPMOD64:
7236
0
    case R_PPC64_DTPREL64:
7237
0
    case R_PPC64_ADDR64:
7238
0
    case R_PPC64_REL30:
7239
0
    case R_PPC64_REL32:
7240
0
    case R_PPC64_REL64:
7241
0
    case R_PPC64_ADDR14:
7242
0
    case R_PPC64_ADDR14_BRNTAKEN:
7243
0
    case R_PPC64_ADDR14_BRTAKEN:
7244
0
    case R_PPC64_ADDR16:
7245
0
    case R_PPC64_ADDR16_DS:
7246
0
    case R_PPC64_ADDR16_HA:
7247
0
    case R_PPC64_ADDR16_HI:
7248
0
    case R_PPC64_ADDR16_HIGH:
7249
0
    case R_PPC64_ADDR16_HIGHA:
7250
0
    case R_PPC64_ADDR16_HIGHER:
7251
0
    case R_PPC64_ADDR16_HIGHERA:
7252
0
    case R_PPC64_ADDR16_HIGHEST:
7253
0
    case R_PPC64_ADDR16_HIGHESTA:
7254
0
    case R_PPC64_ADDR16_LO:
7255
0
    case R_PPC64_ADDR16_LO_DS:
7256
0
    case R_PPC64_ADDR24:
7257
0
    case R_PPC64_ADDR32:
7258
0
    case R_PPC64_UADDR16:
7259
0
    case R_PPC64_UADDR32:
7260
0
    case R_PPC64_UADDR64:
7261
0
    case R_PPC64_TOC:
7262
0
    case R_PPC64_D34:
7263
0
    case R_PPC64_D34_LO:
7264
0
    case R_PPC64_D34_HI30:
7265
0
    case R_PPC64_D34_HA30:
7266
0
    case R_PPC64_ADDR16_HIGHER34:
7267
0
    case R_PPC64_ADDR16_HIGHERA34:
7268
0
    case R_PPC64_ADDR16_HIGHEST34:
7269
0
    case R_PPC64_ADDR16_HIGHESTA34:
7270
0
    case R_PPC64_D28:
7271
0
      break;
7272
0
    }
7273
7274
0
  if (local_syms != NULL)
7275
0
    {
7276
0
      unsigned long r_symndx;
7277
0
      bfd *ibfd = sec->owner;
7278
7279
0
      r_symndx = ELF64_R_SYM (rel->r_info);
7280
0
      if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7281
0
  return false;
7282
0
    }
7283
7284
0
  if ((h != NULL
7285
0
       && !SYMBOL_REFERENCES_LOCAL (info, h))
7286
0
      || (bfd_link_pic (info)
7287
0
    && (h != NULL
7288
0
        ? !bfd_is_abs_symbol (&h->root)
7289
0
        : sym_sec != bfd_abs_section_ptr)
7290
0
    && must_be_dyn_reloc (info, r_type))
7291
0
      || (!bfd_link_pic (info)
7292
0
    && (h != NULL
7293
0
        ? h->type == STT_GNU_IFUNC
7294
0
        : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
7295
0
    ;
7296
0
  else
7297
0
    return true;
7298
7299
0
  if (h != NULL)
7300
0
    {
7301
0
      struct ppc_dyn_relocs *p;
7302
0
      struct ppc_dyn_relocs **pp;
7303
0
      pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
7304
7305
      /* elf_gc_sweep may have already removed all dyn relocs associated
7306
   with local syms for a given section.  Also, symbol flags are
7307
   changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7308
   report a dynreloc miscount.  */
7309
0
      if (*pp == NULL && info->gc_sections)
7310
0
  return true;
7311
7312
0
      while ((p = *pp) != NULL)
7313
0
  {
7314
0
    if (p->sec == sec)
7315
0
      {
7316
0
        if (!must_be_dyn_reloc (info, r_type))
7317
0
    p->pc_count -= 1;
7318
0
        if (maybe_relr (r_type, rel, sec))
7319
0
    p->rel_count -= 1;
7320
0
        p->count -= 1;
7321
0
        if (p->count == 0)
7322
0
    *pp = p->next;
7323
0
        return true;
7324
0
      }
7325
0
    pp = &p->next;
7326
0
  }
7327
0
    }
7328
0
  else
7329
0
    {
7330
0
      struct ppc_local_dyn_relocs *p;
7331
0
      struct ppc_local_dyn_relocs **pp;
7332
0
      void *vpp;
7333
0
      bool is_ifunc;
7334
7335
0
      if (local_syms == NULL)
7336
0
  sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7337
0
      if (sym_sec == NULL)
7338
0
  sym_sec = sec;
7339
7340
0
      vpp = &elf_section_data (sym_sec)->local_dynrel;
7341
0
      pp = (struct ppc_local_dyn_relocs **) vpp;
7342
7343
0
      if (*pp == NULL && info->gc_sections)
7344
0
  return true;
7345
7346
0
      is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7347
0
      while ((p = *pp) != NULL)
7348
0
  {
7349
0
    if (p->sec == sec && p->ifunc == is_ifunc)
7350
0
      {
7351
0
        if (maybe_relr (r_type, rel, sec))
7352
0
    p->rel_count -= 1;
7353
0
        p->count -= 1;
7354
0
        if (p->count == 0)
7355
0
    *pp = p->next;
7356
0
        return true;
7357
0
      }
7358
0
    pp = &p->next;
7359
0
  }
7360
0
    }
7361
7362
  /* xgettext:c-format */
7363
0
  _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7364
0
          sec->owner, sec);
7365
0
  bfd_set_error (bfd_error_bad_value);
7366
0
  return false;
7367
0
}
7368
7369
/* Remove unused Official Procedure Descriptor entries.  Currently we
7370
   only remove those associated with functions in discarded link-once
7371
   sections, or weakly defined functions that have been overridden.  It
7372
   would be possible to remove many more entries for statically linked
7373
   applications.  */
7374
7375
bool
7376
ppc64_elf_edit_opd (struct bfd_link_info *info)
7377
0
{
7378
0
  bfd *ibfd;
7379
0
  bool some_edited = false;
7380
0
  asection *need_pad = NULL;
7381
0
  struct ppc_link_hash_table *htab;
7382
7383
0
  htab = ppc_hash_table (info);
7384
0
  if (htab == NULL)
7385
0
    return false;
7386
7387
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7388
0
    {
7389
0
      asection *sec;
7390
0
      Elf_Internal_Rela *relstart, *rel, *relend;
7391
0
      Elf_Internal_Shdr *symtab_hdr;
7392
0
      Elf_Internal_Sym *local_syms;
7393
0
      struct _opd_sec_data *opd;
7394
0
      bool need_edit, add_aux_fields, broken;
7395
0
      bfd_size_type cnt_16b = 0;
7396
7397
0
      if (!is_ppc64_elf (ibfd))
7398
0
  continue;
7399
7400
0
      sec = bfd_get_section_by_name (ibfd, ".opd");
7401
0
      if (sec == NULL
7402
0
    || sec->size == 0
7403
0
    || (sec->flags & SEC_HAS_CONTENTS) == 0)
7404
0
  continue;
7405
7406
0
      if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7407
0
  continue;
7408
7409
0
      if (sec->output_section == bfd_abs_section_ptr)
7410
0
  continue;
7411
7412
      /* Look through the section relocs.  */
7413
0
      if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7414
0
  continue;
7415
7416
0
      local_syms = NULL;
7417
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
7418
7419
      /* Read the relocations.  */
7420
0
      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7421
0
              info->keep_memory);
7422
0
      if (relstart == NULL)
7423
0
  return false;
7424
7425
      /* First run through the relocs to check they are sane, and to
7426
   determine whether we need to edit this opd section.  */
7427
0
      need_edit = false;
7428
0
      broken = false;
7429
0
      need_pad = sec;
7430
0
      relend = relstart + sec->reloc_count;
7431
0
      for (rel = relstart; rel < relend; )
7432
0
  {
7433
0
    enum elf_ppc64_reloc_type r_type;
7434
0
    unsigned long r_symndx;
7435
0
    asection *sym_sec;
7436
0
    struct elf_link_hash_entry *h;
7437
0
    Elf_Internal_Sym *sym;
7438
0
    bfd_vma offset;
7439
7440
    /* .opd contains an array of 16 or 24 byte entries.  We're
7441
       only interested in the reloc pointing to a function entry
7442
       point.  */
7443
0
    offset = rel->r_offset;
7444
0
    if (rel + 1 == relend
7445
0
        || rel[1].r_offset != offset + 8)
7446
0
      {
7447
        /* If someone messes with .opd alignment then after a
7448
     "ld -r" we might have padding in the middle of .opd.
7449
     Also, there's nothing to prevent someone putting
7450
     something silly in .opd with the assembler.  No .opd
7451
     optimization for them!  */
7452
0
      broken_opd:
7453
0
        _bfd_error_handler
7454
0
    (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7455
0
        broken = true;
7456
0
        break;
7457
0
      }
7458
7459
0
    if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7460
0
        || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7461
0
      {
7462
0
        _bfd_error_handler
7463
    /* xgettext:c-format */
7464
0
    (_("%pB: unexpected reloc type %u in .opd section"),
7465
0
     ibfd, r_type);
7466
0
        broken = true;
7467
0
        break;
7468
0
      }
7469
7470
0
    r_symndx = ELF64_R_SYM (rel->r_info);
7471
0
    if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7472
0
        r_symndx, ibfd))
7473
0
      goto error_ret;
7474
7475
0
    if (sym_sec == NULL || sym_sec->owner == NULL)
7476
0
      {
7477
0
        const char *sym_name;
7478
0
        if (h != NULL)
7479
0
    sym_name = h->root.root.string;
7480
0
        else
7481
0
    sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7482
0
               sym_sec);
7483
7484
0
        _bfd_error_handler
7485
    /* xgettext:c-format */
7486
0
    (_("%pB: undefined sym `%s' in .opd section"),
7487
0
     ibfd, sym_name);
7488
0
        broken = true;
7489
0
        break;
7490
0
      }
7491
7492
    /* opd entries are always for functions defined in the
7493
       current input bfd.  If the symbol isn't defined in the
7494
       input bfd, then we won't be using the function in this
7495
       bfd;  It must be defined in a linkonce section in another
7496
       bfd, or is weak.  It's also possible that we are
7497
       discarding the function due to a linker script /DISCARD/,
7498
       which we test for via the output_section.  */
7499
0
    if (sym_sec->owner != ibfd
7500
0
        || sym_sec->output_section == bfd_abs_section_ptr)
7501
0
      need_edit = true;
7502
7503
0
    rel += 2;
7504
0
    if (rel + 1 == relend
7505
0
        || (rel + 2 < relend
7506
0
      && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7507
0
      ++rel;
7508
7509
0
    if (rel == relend)
7510
0
      {
7511
0
        if (sec->size == offset + 24)
7512
0
    {
7513
0
      need_pad = NULL;
7514
0
      break;
7515
0
    }
7516
0
        if (sec->size == offset + 16)
7517
0
    {
7518
0
      cnt_16b++;
7519
0
      break;
7520
0
    }
7521
0
        goto broken_opd;
7522
0
      }
7523
0
    else if (rel + 1 < relend
7524
0
       && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7525
0
       && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7526
0
      {
7527
0
        if (rel[0].r_offset == offset + 16)
7528
0
    cnt_16b++;
7529
0
        else if (rel[0].r_offset != offset + 24)
7530
0
    goto broken_opd;
7531
0
      }
7532
0
    else
7533
0
      goto broken_opd;
7534
0
  }
7535
7536
0
      add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7537
7538
0
      if (!broken && (need_edit || add_aux_fields))
7539
0
  {
7540
0
    Elf_Internal_Rela *write_rel;
7541
0
    Elf_Internal_Shdr *rel_hdr;
7542
0
    bfd_byte *rptr, *wptr;
7543
0
    bfd_byte *new_contents;
7544
0
    bfd_size_type amt;
7545
7546
0
    new_contents = NULL;
7547
0
    amt = OPD_NDX (sec->size) * sizeof (long);
7548
0
    opd = &ppc64_elf_section_data (sec)->u.opd;
7549
0
    opd->adjust = bfd_zalloc (sec->owner, amt);
7550
0
    if (opd->adjust == NULL)
7551
0
      return false;
7552
7553
    /* This seems a waste of time as input .opd sections are all
7554
       zeros as generated by gcc, but I suppose there's no reason
7555
       this will always be so.  We might start putting something in
7556
       the third word of .opd entries.  */
7557
0
    if ((sec->flags & SEC_IN_MEMORY) == 0)
7558
0
      {
7559
0
        bfd_byte *loc;
7560
0
        if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7561
0
    {
7562
0
      free (loc);
7563
0
    error_ret:
7564
0
      if (symtab_hdr->contents != (unsigned char *) local_syms)
7565
0
        free (local_syms);
7566
0
      if (elf_section_data (sec)->relocs != relstart)
7567
0
        free (relstart);
7568
0
      return false;
7569
0
    }
7570
0
        sec->contents = loc;
7571
0
        sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7572
0
      }
7573
7574
0
    elf_section_data (sec)->relocs = relstart;
7575
7576
0
    new_contents = sec->contents;
7577
0
    if (add_aux_fields)
7578
0
      {
7579
0
        new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7580
0
        if (new_contents == NULL)
7581
0
    return false;
7582
0
        need_pad = NULL;
7583
0
      }
7584
0
    wptr = new_contents;
7585
0
    rptr = sec->contents;
7586
0
    write_rel = relstart;
7587
0
    for (rel = relstart; rel < relend; )
7588
0
      {
7589
0
        unsigned long r_symndx;
7590
0
        asection *sym_sec;
7591
0
        struct elf_link_hash_entry *h;
7592
0
        struct ppc_link_hash_entry *fdh = NULL;
7593
0
        Elf_Internal_Sym *sym;
7594
0
        long opd_ent_size;
7595
0
        Elf_Internal_Rela *next_rel;
7596
0
        bool skip;
7597
7598
0
        r_symndx = ELF64_R_SYM (rel->r_info);
7599
0
        if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7600
0
            r_symndx, ibfd))
7601
0
    goto error_ret;
7602
7603
0
        next_rel = rel + 2;
7604
0
        if (next_rel + 1 == relend
7605
0
      || (next_rel + 2 < relend
7606
0
          && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7607
0
    ++next_rel;
7608
7609
        /* See if the .opd entry is full 24 byte or
7610
     16 byte (with fd_aux entry overlapped with next
7611
     fd_func).  */
7612
0
        opd_ent_size = 24;
7613
0
        if (next_rel == relend)
7614
0
    {
7615
0
      if (sec->size == rel->r_offset + 16)
7616
0
        opd_ent_size = 16;
7617
0
    }
7618
0
        else if (next_rel->r_offset == rel->r_offset + 16)
7619
0
    opd_ent_size = 16;
7620
7621
0
        if (h != NULL
7622
0
      && h->root.root.string[0] == '.')
7623
0
    {
7624
0
      fdh = ppc_elf_hash_entry (h)->oh;
7625
0
      if (fdh != NULL)
7626
0
        {
7627
0
          fdh = ppc_follow_link (fdh);
7628
0
          if (fdh->elf.root.type != bfd_link_hash_defined
7629
0
        && fdh->elf.root.type != bfd_link_hash_defweak)
7630
0
      fdh = NULL;
7631
0
        }
7632
0
    }
7633
7634
0
        skip = (sym_sec->owner != ibfd
7635
0
          || sym_sec->output_section == bfd_abs_section_ptr);
7636
0
        if (skip)
7637
0
    {
7638
0
      if (fdh != NULL && sym_sec->owner == ibfd)
7639
0
        {
7640
          /* Arrange for the function descriptor sym
7641
       to be dropped.  */
7642
0
          fdh->elf.root.u.def.value = 0;
7643
0
          fdh->elf.root.u.def.section = sym_sec;
7644
0
        }
7645
0
      opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7646
7647
0
      if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7648
0
        rel = next_rel;
7649
0
      else
7650
0
        while (1)
7651
0
          {
7652
0
      if (!dec_dynrel_count (rel, sec, info,
7653
0
                 NULL, h, sym))
7654
0
        goto error_ret;
7655
7656
0
      if (++rel == next_rel)
7657
0
        break;
7658
7659
0
      r_symndx = ELF64_R_SYM (rel->r_info);
7660
0
      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7661
0
          r_symndx, ibfd))
7662
0
        goto error_ret;
7663
0
          }
7664
0
    }
7665
0
        else
7666
0
    {
7667
      /* We'll be keeping this opd entry.  */
7668
0
      long adjust;
7669
7670
0
      if (fdh != NULL)
7671
0
        {
7672
          /* Redefine the function descriptor symbol to
7673
       this location in the opd section.  It is
7674
       necessary to update the value here rather
7675
       than using an array of adjustments as we do
7676
       for local symbols, because various places
7677
       in the generic ELF code use the value
7678
       stored in u.def.value.  */
7679
0
          fdh->elf.root.u.def.value = wptr - new_contents;
7680
0
          fdh->adjust_done = 1;
7681
0
        }
7682
7683
      /* Local syms are a bit tricky.  We could
7684
         tweak them as they can be cached, but
7685
         we'd need to look through the local syms
7686
         for the function descriptor sym which we
7687
         don't have at the moment.  So keep an
7688
         array of adjustments.  */
7689
0
      adjust = (wptr - new_contents) - (rptr - sec->contents);
7690
0
      opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7691
7692
0
      if (wptr != rptr)
7693
0
        memcpy (wptr, rptr, opd_ent_size);
7694
0
      wptr += opd_ent_size;
7695
0
      if (add_aux_fields && opd_ent_size == 16)
7696
0
        {
7697
0
          memset (wptr, '\0', 8);
7698
0
          wptr += 8;
7699
0
        }
7700
7701
      /* We need to adjust any reloc offsets to point to the
7702
         new opd entries.  */
7703
0
      for ( ; rel != next_rel; ++rel)
7704
0
        {
7705
0
          rel->r_offset += adjust;
7706
0
          if (write_rel != rel)
7707
0
      memcpy (write_rel, rel, sizeof (*rel));
7708
0
          ++write_rel;
7709
0
        }
7710
0
    }
7711
7712
0
        rptr += opd_ent_size;
7713
0
      }
7714
7715
0
    sec->size = wptr - new_contents;
7716
0
    sec->reloc_count = write_rel - relstart;
7717
0
    if (add_aux_fields)
7718
0
      {
7719
0
        free (sec->contents);
7720
0
        sec->contents = new_contents;
7721
0
      }
7722
7723
    /* Fudge the header size too, as this is used later in
7724
       elf_bfd_final_link if we are emitting relocs.  */
7725
0
    rel_hdr = _bfd_elf_single_rel_hdr (sec);
7726
0
    rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7727
0
    some_edited = true;
7728
0
  }
7729
0
      else if (elf_section_data (sec)->relocs != relstart)
7730
0
  free (relstart);
7731
7732
0
      if (local_syms != NULL
7733
0
    && symtab_hdr->contents != (unsigned char *) local_syms)
7734
0
  {
7735
0
    if (!info->keep_memory)
7736
0
      free (local_syms);
7737
0
    else
7738
0
      symtab_hdr->contents = (unsigned char *) local_syms;
7739
0
  }
7740
0
    }
7741
7742
0
  if (some_edited)
7743
0
    elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7744
7745
  /* If we are doing a final link and the last .opd entry is just 16 byte
7746
     long, add a 8 byte padding after it.  */
7747
0
  if (need_pad != NULL && !bfd_link_relocatable (info))
7748
0
    {
7749
0
      bfd_byte *p;
7750
7751
0
      if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7752
0
  {
7753
0
    BFD_ASSERT (need_pad->size > 0);
7754
7755
0
    p = bfd_malloc (need_pad->size + 8);
7756
0
    if (p == NULL)
7757
0
      return false;
7758
7759
0
    if (!bfd_get_section_contents (need_pad->owner, need_pad,
7760
0
           p, 0, need_pad->size))
7761
0
      return false;
7762
7763
0
    need_pad->contents = p;
7764
0
    need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7765
0
  }
7766
0
      else
7767
0
  {
7768
0
    p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7769
0
    if (p == NULL)
7770
0
      return false;
7771
7772
0
    need_pad->contents = p;
7773
0
  }
7774
7775
0
      memset (need_pad->contents + need_pad->size, 0, 8);
7776
0
      need_pad->size += 8;
7777
0
    }
7778
7779
0
  return true;
7780
0
}
7781
7782
/* Analyze inline PLT call relocations to see whether calls to locally
7783
   defined functions can be converted to direct calls.  */
7784
7785
bool
7786
ppc64_elf_inline_plt (struct bfd_link_info *info)
7787
0
{
7788
0
  struct ppc_link_hash_table *htab;
7789
0
  bfd *ibfd;
7790
0
  asection *sec;
7791
0
  bfd_vma low_vma, high_vma, limit;
7792
7793
0
  htab = ppc_hash_table (info);
7794
0
  if (htab == NULL)
7795
0
    return false;
7796
7797
  /* A bl insn can reach -0x2000000 to 0x1fffffc.  The limit is
7798
     reduced somewhat to cater for possible stubs that might be added
7799
     between the call and its destination.  */
7800
0
  if (htab->params->group_size < 0)
7801
0
    {
7802
0
      limit = -htab->params->group_size;
7803
0
      if (limit == 1)
7804
0
  limit = 0x1e00000;
7805
0
    }
7806
0
  else
7807
0
    {
7808
0
      limit = htab->params->group_size;
7809
0
      if (limit == 1)
7810
0
  limit = 0x1c00000;
7811
0
    }
7812
7813
0
  low_vma = -1;
7814
0
  high_vma = 0;
7815
0
  for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7816
0
    if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7817
0
      {
7818
0
  if (low_vma > sec->vma)
7819
0
    low_vma = sec->vma;
7820
0
  if (high_vma < sec->vma + sec->size)
7821
0
    high_vma = sec->vma + sec->size;
7822
0
      }
7823
7824
  /* If a "bl" can reach anywhere in local code sections, then we can
7825
     convert all inline PLT sequences to direct calls when the symbol
7826
     is local.  */
7827
0
  if (high_vma - low_vma < limit)
7828
0
    {
7829
0
      htab->can_convert_all_inline_plt = 1;
7830
0
      return true;
7831
0
    }
7832
7833
  /* Otherwise, go looking through relocs for cases where a direct
7834
     call won't reach.  Mark the symbol on any such reloc to disable
7835
     the optimization and keep the PLT entry as it seems likely that
7836
     this will be better than creating trampolines.  Note that this
7837
     will disable the optimization for all inline PLT calls to a
7838
     particular symbol, not just those that won't reach.  The
7839
     difficulty in doing a more precise optimization is that the
7840
     linker needs to make a decision depending on whether a
7841
     particular R_PPC64_PLTCALL insn can be turned into a direct
7842
     call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7843
     the sequence, and there is nothing that ties those relocs
7844
     together except their symbol.  */
7845
7846
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7847
0
    {
7848
0
      Elf_Internal_Shdr *symtab_hdr;
7849
0
      Elf_Internal_Sym *local_syms;
7850
7851
0
      if (!is_ppc64_elf (ibfd))
7852
0
  continue;
7853
7854
0
      local_syms = NULL;
7855
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
7856
7857
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7858
0
  if (ppc64_elf_section_data (sec)->has_pltcall
7859
0
      && !bfd_is_abs_section (sec->output_section))
7860
0
    {
7861
0
      Elf_Internal_Rela *relstart, *rel, *relend;
7862
7863
      /* Read the relocations.  */
7864
0
      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7865
0
              info->keep_memory);
7866
0
      if (relstart == NULL)
7867
0
        return false;
7868
7869
0
      relend = relstart + sec->reloc_count;
7870
0
      for (rel = relstart; rel < relend; rel++)
7871
0
        {
7872
0
    enum elf_ppc64_reloc_type r_type;
7873
0
    unsigned long r_symndx;
7874
0
    asection *sym_sec;
7875
0
    struct elf_link_hash_entry *h;
7876
0
    Elf_Internal_Sym *sym;
7877
0
    unsigned char *tls_maskp;
7878
7879
0
    r_type = ELF64_R_TYPE (rel->r_info);
7880
0
    if (r_type != R_PPC64_PLTCALL
7881
0
        && r_type != R_PPC64_PLTCALL_NOTOC)
7882
0
      continue;
7883
7884
0
    r_symndx = ELF64_R_SYM (rel->r_info);
7885
0
    if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7886
0
        r_symndx, ibfd))
7887
0
      {
7888
0
        if (elf_section_data (sec)->relocs != relstart)
7889
0
          free (relstart);
7890
0
        if (symtab_hdr->contents != (bfd_byte *) local_syms)
7891
0
          free (local_syms);
7892
0
        return false;
7893
0
      }
7894
7895
0
    if (sym_sec != NULL && sym_sec->output_section != NULL)
7896
0
      {
7897
0
        bfd_vma from, to;
7898
0
        if (h != NULL)
7899
0
          to = h->root.u.def.value;
7900
0
        else
7901
0
          to = sym->st_value;
7902
0
        to += (rel->r_addend
7903
0
         + sym_sec->output_offset
7904
0
         + sym_sec->output_section->vma);
7905
0
        from = (rel->r_offset
7906
0
          + sec->output_offset
7907
0
          + sec->output_section->vma);
7908
0
        if (to - from + limit < 2 * limit
7909
0
      && !(r_type == R_PPC64_PLTCALL_NOTOC
7910
0
           && (((h ? h->other : sym->st_other)
7911
0
          & STO_PPC64_LOCAL_MASK)
7912
0
         > 1 << STO_PPC64_LOCAL_BIT)))
7913
0
          *tls_maskp &= ~PLT_KEEP;
7914
0
      }
7915
0
        }
7916
0
      if (elf_section_data (sec)->relocs != relstart)
7917
0
        free (relstart);
7918
0
    }
7919
7920
0
      if (local_syms != NULL
7921
0
    && symtab_hdr->contents != (unsigned char *) local_syms)
7922
0
  {
7923
0
    if (!info->keep_memory)
7924
0
      free (local_syms);
7925
0
    else
7926
0
      symtab_hdr->contents = (unsigned char *) local_syms;
7927
0
  }
7928
0
    }
7929
7930
0
  return true;
7931
0
}
7932
7933
/* Set htab->tls_get_addr and various other info specific to TLS.
7934
   This needs to run before dynamic symbols are processed in
7935
   bfd_elf_size_dynamic_sections.  */
7936
7937
bool
7938
ppc64_elf_tls_setup (struct bfd_link_info *info)
7939
0
{
7940
0
  struct ppc_link_hash_table *htab;
7941
0
  struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
7942
7943
0
  htab = ppc_hash_table (info);
7944
0
  if (htab == NULL)
7945
0
    return false;
7946
7947
  /* Move dynamic linking info to the function descriptor sym.  */
7948
0
  if (htab->need_func_desc_adj)
7949
0
    {
7950
0
      elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7951
0
      htab->need_func_desc_adj = 0;
7952
0
    }
7953
7954
0
  if (abiversion (info->output_bfd) == 1)
7955
0
    htab->opd_abi = 1;
7956
7957
0
  if (htab->params->no_multi_toc)
7958
0
    htab->do_multi_toc = 0;
7959
0
  else if (!htab->do_multi_toc)
7960
0
    htab->params->no_multi_toc = 1;
7961
7962
  /* Default to --no-plt-localentry, as this option can cause problems
7963
     with symbol interposition.  For example, glibc libpthread.so and
7964
     libc.so duplicate many pthread symbols, with a fallback
7965
     implementation in libc.so.  In some cases the fallback does more
7966
     work than the pthread implementation.  __pthread_condattr_destroy
7967
     is one such symbol: the libpthread.so implementation is
7968
     localentry:0 while the libc.so implementation is localentry:8.
7969
     An app that "cleverly" uses dlopen to only load necessary
7970
     libraries at runtime may omit loading libpthread.so when not
7971
     running multi-threaded, which then results in the libc.so
7972
     fallback symbols being used and ld.so complaining.  Now there
7973
     are workarounds in ld (see non_zero_localentry) to detect the
7974
     pthread situation, but that may not be the only case where
7975
     --plt-localentry can cause trouble.  */
7976
0
  if (htab->params->plt_localentry0 < 0)
7977
0
    htab->params->plt_localentry0 = 0;
7978
0
  if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7979
0
    {
7980
      /* The issue is that __glink_PLTresolve saves r2, which is done
7981
   because glibc ld.so _dl_runtime_resolve restores r2 to support
7982
   a glibc plt call optimisation where global entry code is
7983
   skipped on calls that resolve to the same binary.  The
7984
   __glink_PLTresolve save of r2 is incompatible with code
7985
   making tail calls, because the tail call might go via the
7986
   resolver and thus overwrite the proper saved r2.  */
7987
0
      _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7988
0
          "power10 pc-relative code"));
7989
0
      htab->params->plt_localentry0 = 0;
7990
0
    }
7991
0
  if (htab->params->plt_localentry0
7992
0
      && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7993
0
             false, false, false) == NULL)
7994
0
    _bfd_error_handler
7995
0
      (_("warning: --plt-localentry is especially dangerous without "
7996
0
   "ld.so support to detect ABI violations"));
7997
7998
0
  tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7999
0
            false, false, true);
8000
0
  htab->tls_get_addr = ppc_elf_hash_entry (tga);
8001
0
  tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8002
0
         false, false, true);
8003
0
  htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
8004
8005
0
  desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
8006
0
             false, false, true);
8007
0
  htab->tga_desc = ppc_elf_hash_entry (desc);
8008
0
  desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
8009
0
          false, false, true);
8010
0
  htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
8011
8012
0
  if (htab->params->tls_get_addr_opt)
8013
0
    {
8014
0
      struct elf_link_hash_entry *opt, *opt_fd;
8015
8016
0
      opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8017
0
          false, false, true);
8018
0
      opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8019
0
             false, false, true);
8020
0
      if (opt_fd != NULL
8021
0
    && (opt_fd->root.type == bfd_link_hash_defined
8022
0
        || opt_fd->root.type == bfd_link_hash_defweak))
8023
0
  {
8024
    /* If glibc supports an optimized __tls_get_addr call stub,
8025
       signalled by the presence of __tls_get_addr_opt, and we'll
8026
       be calling __tls_get_addr via a plt call stub, then
8027
       make __tls_get_addr point to __tls_get_addr_opt.  */
8028
0
    if (!(htab->elf.dynamic_sections_created
8029
0
    && tga_fd != NULL
8030
0
    && (tga_fd->type == STT_FUNC
8031
0
        || tga_fd->needs_plt)
8032
0
    && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8033
0
         || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
8034
0
      tga_fd = NULL;
8035
0
    if (!(htab->elf.dynamic_sections_created
8036
0
    && desc_fd != NULL
8037
0
    && (desc_fd->type == STT_FUNC
8038
0
        || desc_fd->needs_plt)
8039
0
    && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
8040
0
         || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
8041
0
      desc_fd = NULL;
8042
8043
0
    if (tga_fd != NULL || desc_fd != NULL)
8044
0
      {
8045
0
        struct plt_entry *ent = NULL;
8046
8047
0
        if (tga_fd != NULL)
8048
0
    for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8049
0
      if (ent->plt.refcount > 0)
8050
0
        break;
8051
0
        if (ent == NULL && desc_fd != NULL)
8052
0
    for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8053
0
      if (ent->plt.refcount > 0)
8054
0
        break;
8055
0
        if (ent != NULL)
8056
0
    {
8057
0
      if (tga_fd != NULL)
8058
0
        {
8059
0
          tga_fd->root.type = bfd_link_hash_indirect;
8060
0
          tga_fd->root.u.i.link = &opt_fd->root;
8061
0
          tga_fd->root.u.i.warning = NULL;
8062
0
          ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8063
0
        }
8064
0
      if (desc_fd != NULL)
8065
0
        {
8066
0
          desc_fd->root.type = bfd_link_hash_indirect;
8067
0
          desc_fd->root.u.i.link = &opt_fd->root;
8068
0
          desc_fd->root.u.i.warning = NULL;
8069
0
          ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8070
0
        }
8071
0
      opt_fd->mark = 1;
8072
0
      if (opt_fd->dynindx != -1)
8073
0
        {
8074
          /* Use __tls_get_addr_opt in dynamic relocations.  */
8075
0
          opt_fd->dynindx = -1;
8076
0
          _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8077
0
                opt_fd->dynstr_index);
8078
0
          if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8079
0
      return false;
8080
0
        }
8081
0
      if (tga_fd != NULL)
8082
0
        {
8083
0
          htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
8084
0
          tga = elf_hash_entry (htab->tls_get_addr);
8085
0
          if (opt != NULL && tga != NULL)
8086
0
      {
8087
0
        tga->root.type = bfd_link_hash_indirect;
8088
0
        tga->root.u.i.link = &opt->root;
8089
0
        tga->root.u.i.warning = NULL;
8090
0
        ppc64_elf_copy_indirect_symbol (info, opt, tga);
8091
0
        opt->mark = 1;
8092
0
        _bfd_elf_link_hash_hide_symbol (info, opt,
8093
0
                tga->forced_local);
8094
0
        htab->tls_get_addr = ppc_elf_hash_entry (opt);
8095
0
      }
8096
0
          htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8097
0
          htab->tls_get_addr_fd->is_func_descriptor = 1;
8098
0
          if (htab->tls_get_addr != NULL)
8099
0
      {
8100
0
        htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8101
0
        htab->tls_get_addr->is_func = 1;
8102
0
      }
8103
0
        }
8104
0
      if (desc_fd != NULL)
8105
0
        {
8106
0
          htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8107
0
          if (opt != NULL && desc != NULL)
8108
0
      {
8109
0
        desc->root.type = bfd_link_hash_indirect;
8110
0
        desc->root.u.i.link = &opt->root;
8111
0
        desc->root.u.i.warning = NULL;
8112
0
        ppc64_elf_copy_indirect_symbol (info, opt, desc);
8113
0
        opt->mark = 1;
8114
0
        _bfd_elf_link_hash_hide_symbol (info, opt,
8115
0
                desc->forced_local);
8116
0
        htab->tga_desc = ppc_elf_hash_entry (opt);
8117
0
      }
8118
0
          htab->tga_desc_fd->oh = htab->tga_desc;
8119
0
          htab->tga_desc_fd->is_func_descriptor = 1;
8120
0
          if (htab->tga_desc != NULL)
8121
0
      {
8122
0
        htab->tga_desc->oh = htab->tga_desc_fd;
8123
0
        htab->tga_desc->is_func = 1;
8124
0
      }
8125
0
        }
8126
0
    }
8127
0
      }
8128
0
  }
8129
0
      else if (htab->params->tls_get_addr_opt < 0)
8130
0
  htab->params->tls_get_addr_opt = 0;
8131
0
    }
8132
8133
0
  if (htab->tga_desc_fd != NULL
8134
0
      && htab->params->tls_get_addr_opt
8135
0
      && htab->params->no_tls_get_addr_regsave == -1)
8136
0
    htab->params->no_tls_get_addr_regsave = 0;
8137
8138
0
  return true;
8139
0
}
8140
8141
/* Return TRUE iff REL is a branch reloc with a global symbol matching
8142
   any of HASH1, HASH2, HASH3, or HASH4.  */
8143
8144
static bool
8145
branch_reloc_hash_match (bfd *ibfd,
8146
       Elf_Internal_Rela *rel,
8147
       struct ppc_link_hash_entry *hash1,
8148
       struct ppc_link_hash_entry *hash2,
8149
       struct ppc_link_hash_entry *hash3,
8150
       struct ppc_link_hash_entry *hash4)
8151
0
{
8152
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8153
0
  enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8154
0
  unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8155
8156
0
  if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8157
0
    {
8158
0
      struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8159
0
      struct elf_link_hash_entry *h;
8160
8161
0
      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8162
0
      h = elf_follow_link (h);
8163
0
      if (h == elf_hash_entry (hash1)
8164
0
    || h == elf_hash_entry (hash2)
8165
0
    || h == elf_hash_entry (hash3)
8166
0
    || h == elf_hash_entry (hash4))
8167
0
  return true;
8168
0
    }
8169
0
  return false;
8170
0
}
8171
8172
/* Run through all the TLS relocs looking for optimization
8173
   opportunities.  The linker has been hacked (see ppc64elf.em) to do
8174
   a preliminary section layout so that we know the TLS segment
8175
   offsets.  We can't optimize earlier because some optimizations need
8176
   to know the tp offset, and we need to optimize before allocating
8177
   dynamic relocations.  */
8178
8179
bool
8180
ppc64_elf_tls_optimize (struct bfd_link_info *info)
8181
0
{
8182
0
  bfd *ibfd;
8183
0
  asection *sec;
8184
0
  struct ppc_link_hash_table *htab;
8185
0
  unsigned char *toc_ref;
8186
0
  int pass;
8187
8188
0
  if (!bfd_link_executable (info))
8189
0
    return true;
8190
8191
0
  htab = ppc_hash_table (info);
8192
0
  if (htab == NULL)
8193
0
    return false;
8194
8195
0
  htab->do_tls_opt = 1;
8196
8197
  /* Make two passes over the relocs.  On the first pass, mark toc
8198
     entries involved with tls relocs, and check that tls relocs
8199
     involved in setting up a tls_get_addr call are indeed followed by
8200
     such a call.  If they are not, we can't do any tls optimization.
8201
     On the second pass twiddle tls_mask flags to notify
8202
     relocate_section that optimization can be done, and adjust got
8203
     and plt refcounts.  */
8204
0
  toc_ref = NULL;
8205
0
  for (pass = 0; pass < 2; ++pass)
8206
0
    for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8207
0
      {
8208
0
  Elf_Internal_Sym *locsyms = NULL;
8209
0
  asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8210
8211
0
  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8212
0
    if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8213
0
      {
8214
0
        Elf_Internal_Rela *relstart, *rel, *relend;
8215
0
        bool found_tls_get_addr_arg = 0;
8216
8217
        /* Read the relocations.  */
8218
0
        relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8219
0
                info->keep_memory);
8220
0
        if (relstart == NULL)
8221
0
    {
8222
0
      free (toc_ref);
8223
0
      return false;
8224
0
    }
8225
8226
0
        relend = relstart + sec->reloc_count;
8227
0
        for (rel = relstart; rel < relend; rel++)
8228
0
    {
8229
0
      enum elf_ppc64_reloc_type r_type;
8230
0
      unsigned long r_symndx;
8231
0
      struct elf_link_hash_entry *h;
8232
0
      Elf_Internal_Sym *sym;
8233
0
      asection *sym_sec;
8234
0
      unsigned char *tls_mask;
8235
0
      unsigned int tls_set, tls_clear, tls_type = 0;
8236
0
      bfd_vma value;
8237
0
      bool ok_tprel, is_local;
8238
0
      long toc_ref_index = 0;
8239
0
      int expecting_tls_get_addr = 0;
8240
0
      bool ret = false;
8241
8242
0
      r_symndx = ELF64_R_SYM (rel->r_info);
8243
0
      if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8244
0
          r_symndx, ibfd))
8245
0
        {
8246
0
        err_free_rel:
8247
0
          if (elf_section_data (sec)->relocs != relstart)
8248
0
      free (relstart);
8249
0
          free (toc_ref);
8250
0
          if (elf_symtab_hdr (ibfd).contents
8251
0
        != (unsigned char *) locsyms)
8252
0
      free (locsyms);
8253
0
          return ret;
8254
0
        }
8255
8256
0
      if (h != NULL)
8257
0
        {
8258
0
          if (h->root.type == bfd_link_hash_defined
8259
0
        || h->root.type == bfd_link_hash_defweak)
8260
0
      value = h->root.u.def.value;
8261
0
          else if (h->root.type == bfd_link_hash_undefweak)
8262
0
      value = 0;
8263
0
          else
8264
0
      {
8265
0
        found_tls_get_addr_arg = 0;
8266
0
        continue;
8267
0
      }
8268
0
        }
8269
0
      else
8270
        /* Symbols referenced by TLS relocs must be of type
8271
           STT_TLS.  So no need for .opd local sym adjust.  */
8272
0
        value = sym->st_value;
8273
8274
0
      ok_tprel = false;
8275
0
      is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8276
0
      if (is_local)
8277
0
        {
8278
0
          if (h != NULL
8279
0
        && h->root.type == bfd_link_hash_undefweak)
8280
0
      ok_tprel = true;
8281
0
          else if (sym_sec != NULL
8282
0
             && sym_sec->output_section != NULL)
8283
0
      {
8284
0
        value += sym_sec->output_offset;
8285
0
        value += sym_sec->output_section->vma;
8286
0
        value -= htab->elf.tls_sec->vma + TP_OFFSET;
8287
        /* Note that even though the prefix insns
8288
           allow a 1<<33 offset we use the same test
8289
           as for addis;addi.  There may be a mix of
8290
           pcrel and non-pcrel code and the decision
8291
           to optimise is per symbol, not per TLS
8292
           sequence.  */
8293
0
        ok_tprel = value + 0x80008000ULL < 1ULL << 32;
8294
0
      }
8295
0
        }
8296
8297
0
      r_type = ELF64_R_TYPE (rel->r_info);
8298
      /* If this section has old-style __tls_get_addr calls
8299
         without marker relocs, then check that each
8300
         __tls_get_addr call reloc is preceded by a reloc
8301
         that conceivably belongs to the __tls_get_addr arg
8302
         setup insn.  If we don't find matching arg setup
8303
         relocs, don't do any tls optimization.  */
8304
0
      if (pass == 0
8305
0
          && sec->nomark_tls_get_addr
8306
0
          && h != NULL
8307
0
          && is_tls_get_addr (h, htab)
8308
0
          && !found_tls_get_addr_arg
8309
0
          && is_branch_reloc (r_type))
8310
0
        {
8311
0
          info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8312
0
            "TLS optimization disabled\n"),
8313
0
                ibfd, sec, rel->r_offset);
8314
0
          ret = true;
8315
0
          goto err_free_rel;
8316
0
        }
8317
8318
0
      found_tls_get_addr_arg = 0;
8319
0
      switch (r_type)
8320
0
        {
8321
0
        case R_PPC64_GOT_TLSLD16:
8322
0
        case R_PPC64_GOT_TLSLD16_LO:
8323
0
        case R_PPC64_GOT_TLSLD_PCREL34:
8324
0
          expecting_tls_get_addr = 1;
8325
0
          found_tls_get_addr_arg = 1;
8326
          /* Fall through.  */
8327
8328
0
        case R_PPC64_GOT_TLSLD16_HI:
8329
0
        case R_PPC64_GOT_TLSLD16_HA:
8330
          /* These relocs should never be against a symbol
8331
       defined in a shared lib.  Leave them alone if
8332
       that turns out to be the case.  */
8333
0
          if (!is_local)
8334
0
      continue;
8335
8336
          /* LD -> LE */
8337
0
          tls_set = 0;
8338
0
          tls_clear = TLS_LD;
8339
0
          tls_type = TLS_TLS | TLS_LD;
8340
0
          break;
8341
8342
0
        case R_PPC64_GOT_TLSGD16:
8343
0
        case R_PPC64_GOT_TLSGD16_LO:
8344
0
        case R_PPC64_GOT_TLSGD_PCREL34:
8345
0
          expecting_tls_get_addr = 1;
8346
0
          found_tls_get_addr_arg = 1;
8347
          /* Fall through. */
8348
8349
0
        case R_PPC64_GOT_TLSGD16_HI:
8350
0
        case R_PPC64_GOT_TLSGD16_HA:
8351
0
          if (ok_tprel)
8352
      /* GD -> LE */
8353
0
      tls_set = 0;
8354
0
          else
8355
      /* GD -> IE */
8356
0
      tls_set = TLS_TLS | TLS_GDIE;
8357
0
          tls_clear = TLS_GD;
8358
0
          tls_type = TLS_TLS | TLS_GD;
8359
0
          break;
8360
8361
0
        case R_PPC64_GOT_TPREL_PCREL34:
8362
0
        case R_PPC64_GOT_TPREL16_DS:
8363
0
        case R_PPC64_GOT_TPREL16_LO_DS:
8364
0
        case R_PPC64_GOT_TPREL16_HI:
8365
0
        case R_PPC64_GOT_TPREL16_HA:
8366
0
          if (ok_tprel)
8367
0
      {
8368
        /* IE -> LE */
8369
0
        tls_set = 0;
8370
0
        tls_clear = TLS_TPREL;
8371
0
        tls_type = TLS_TLS | TLS_TPREL;
8372
0
        break;
8373
0
      }
8374
0
          continue;
8375
8376
0
        case R_PPC64_TLSLD:
8377
0
          if (!is_local)
8378
0
      continue;
8379
          /* Fall through.  */
8380
0
        case R_PPC64_TLSGD:
8381
0
          if (rel + 1 < relend
8382
0
        && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8383
0
      {
8384
0
        if (pass != 0
8385
0
            && (ELF64_R_TYPE (rel[1].r_info)
8386
0
          != R_PPC64_PLTSEQ)
8387
0
            && (ELF64_R_TYPE (rel[1].r_info)
8388
0
          != R_PPC64_PLTSEQ_NOTOC))
8389
0
          {
8390
0
            r_symndx = ELF64_R_SYM (rel[1].r_info);
8391
0
            if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8392
0
                r_symndx, ibfd))
8393
0
        goto err_free_rel;
8394
0
            if (h != NULL)
8395
0
        {
8396
0
          struct plt_entry *ent = NULL;
8397
8398
0
          for (ent = h->plt.plist;
8399
0
               ent != NULL;
8400
0
               ent = ent->next)
8401
0
            if (ent->addend == rel[1].r_addend)
8402
0
              break;
8403
8404
0
          if (ent != NULL
8405
0
              && ent->plt.refcount > 0)
8406
0
            ent->plt.refcount -= 1;
8407
0
        }
8408
0
          }
8409
0
        continue;
8410
0
      }
8411
0
          found_tls_get_addr_arg = 1;
8412
          /* Fall through.  */
8413
8414
0
        case R_PPC64_TLS:
8415
0
        case R_PPC64_TOC16:
8416
0
        case R_PPC64_TOC16_LO:
8417
0
          if (sym_sec == NULL || sym_sec != toc)
8418
0
      continue;
8419
8420
          /* Mark this toc entry as referenced by a TLS
8421
       code sequence.  We can do that now in the
8422
       case of R_PPC64_TLS, and after checking for
8423
       tls_get_addr for the TOC16 relocs.  */
8424
0
          if (toc_ref == NULL)
8425
0
      toc_ref
8426
0
        = bfd_zmalloc (toc->output_section->rawsize / 8);
8427
0
          if (toc_ref == NULL)
8428
0
      goto err_free_rel;
8429
8430
0
          if (h != NULL)
8431
0
      value = h->root.u.def.value;
8432
0
          else
8433
0
      value = sym->st_value;
8434
0
          value += rel->r_addend;
8435
0
          if (value % 8 != 0)
8436
0
      continue;
8437
0
          BFD_ASSERT (value < toc->size
8438
0
          && toc->output_offset % 8 == 0);
8439
0
          toc_ref_index = (value + toc->output_offset) / 8;
8440
0
          if (r_type == R_PPC64_TLS
8441
0
        || r_type == R_PPC64_TLSGD
8442
0
        || r_type == R_PPC64_TLSLD)
8443
0
      {
8444
0
        toc_ref[toc_ref_index] = 1;
8445
0
        continue;
8446
0
      }
8447
8448
0
          if (pass != 0 && toc_ref[toc_ref_index] == 0)
8449
0
      continue;
8450
8451
0
          tls_set = 0;
8452
0
          tls_clear = 0;
8453
0
          expecting_tls_get_addr = 2;
8454
0
          break;
8455
8456
0
        case R_PPC64_TPREL64:
8457
0
          if (pass == 0
8458
0
        || sec != toc
8459
0
        || toc_ref == NULL
8460
0
        || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8461
0
      continue;
8462
0
          if (ok_tprel)
8463
0
      {
8464
        /* IE -> LE */
8465
0
        tls_set = TLS_EXPLICIT;
8466
0
        tls_clear = TLS_TPREL;
8467
0
        break;
8468
0
      }
8469
0
          continue;
8470
8471
0
        case R_PPC64_DTPMOD64:
8472
0
          if (pass == 0
8473
0
        || sec != toc
8474
0
        || toc_ref == NULL
8475
0
        || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8476
0
      continue;
8477
0
          if (rel + 1 < relend
8478
0
        && (rel[1].r_info
8479
0
            == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8480
0
        && rel[1].r_offset == rel->r_offset + 8)
8481
0
      {
8482
0
        if (ok_tprel)
8483
          /* GD -> LE */
8484
0
          tls_set = TLS_EXPLICIT | TLS_GD;
8485
0
        else
8486
          /* GD -> IE */
8487
0
          tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8488
0
        tls_clear = TLS_GD;
8489
0
      }
8490
0
          else
8491
0
      {
8492
0
        if (!is_local)
8493
0
          continue;
8494
8495
        /* LD -> LE */
8496
0
        tls_set = TLS_EXPLICIT;
8497
0
        tls_clear = TLS_LD;
8498
0
      }
8499
0
          break;
8500
8501
0
        case R_PPC64_TPREL16_HA:
8502
0
          if (pass == 0)
8503
0
      {
8504
0
        unsigned char buf[4];
8505
0
        unsigned int insn;
8506
0
        bfd_vma off = rel->r_offset & ~3;
8507
0
        if (!bfd_get_section_contents (ibfd, sec, buf,
8508
0
               off, 4))
8509
0
          goto err_free_rel;
8510
0
        insn = bfd_get_32 (ibfd, buf);
8511
        /* addis rt,13,imm */
8512
0
        if ((insn & ((0x3fu << 26) | 0x1f << 16))
8513
0
            != ((15u << 26) | (13 << 16)))
8514
0
          {
8515
            /* xgettext:c-format */
8516
0
            info->callbacks->minfo
8517
0
        (_("%H: warning: %s unexpected insn %#x.\n"),
8518
0
         ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8519
0
            htab->do_tls_opt = 0;
8520
0
          }
8521
0
      }
8522
0
          continue;
8523
8524
0
        case R_PPC64_TPREL16_HI:
8525
0
        case R_PPC64_TPREL16_HIGH:
8526
0
        case R_PPC64_TPREL16_HIGHA:
8527
0
        case R_PPC64_TPREL16_HIGHER:
8528
0
        case R_PPC64_TPREL16_HIGHERA:
8529
0
        case R_PPC64_TPREL16_HIGHEST:
8530
0
        case R_PPC64_TPREL16_HIGHESTA:
8531
          /* These can all be used in sequences along with
8532
       TPREL16_LO or TPREL16_LO_DS in ways we aren't
8533
       able to verify easily.  */
8534
0
          htab->do_tls_opt = 0;
8535
0
          continue;
8536
8537
0
        default:
8538
0
          continue;
8539
0
        }
8540
8541
0
      if (pass == 0)
8542
0
        {
8543
0
          if (!expecting_tls_get_addr
8544
0
        || !sec->nomark_tls_get_addr)
8545
0
      continue;
8546
8547
0
          if (rel + 1 < relend
8548
0
        && branch_reloc_hash_match (ibfd, rel + 1,
8549
0
                  htab->tls_get_addr_fd,
8550
0
                  htab->tga_desc_fd,
8551
0
                  htab->tls_get_addr,
8552
0
                  htab->tga_desc))
8553
0
      {
8554
0
        if (expecting_tls_get_addr == 2)
8555
0
          {
8556
            /* Check for toc tls entries.  */
8557
0
            unsigned char *toc_tls;
8558
0
            int retval;
8559
8560
0
            retval = get_tls_mask (&toc_tls, NULL, NULL,
8561
0
                 &locsyms,
8562
0
                 rel, ibfd);
8563
0
            if (retval == 0)
8564
0
        goto err_free_rel;
8565
0
            if (toc_tls != NULL)
8566
0
        {
8567
0
          if ((*toc_tls & TLS_TLS) != 0
8568
0
              && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8569
0
            found_tls_get_addr_arg = 1;
8570
0
          if (retval > 1)
8571
0
            toc_ref[toc_ref_index] = 1;
8572
0
        }
8573
0
          }
8574
0
        continue;
8575
0
      }
8576
8577
          /* Uh oh, we didn't find the expected call.  We
8578
       could just mark this symbol to exclude it
8579
       from tls optimization but it's safer to skip
8580
       the entire optimization.  */
8581
          /* xgettext:c-format */
8582
0
          info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8583
0
            "TLS optimization disabled\n"),
8584
0
                ibfd, sec, rel->r_offset);
8585
0
          ret = true;
8586
0
          goto err_free_rel;
8587
0
        }
8588
8589
      /* If we don't have old-style __tls_get_addr calls
8590
         without TLSGD/TLSLD marker relocs, and we haven't
8591
         found a new-style __tls_get_addr call with a
8592
         marker for this symbol, then we either have a
8593
         broken object file or an -mlongcall style
8594
         indirect call to __tls_get_addr without a marker.
8595
         Disable optimization in this case.  */
8596
0
      if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8597
0
          && (tls_set & TLS_EXPLICIT) == 0
8598
0
          && !sec->nomark_tls_get_addr
8599
0
          && ((*tls_mask & (TLS_TLS | TLS_MARK))
8600
0
        != (TLS_TLS | TLS_MARK)))
8601
0
        continue;
8602
8603
0
      if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8604
0
        {
8605
0
          struct plt_entry *ent = NULL;
8606
8607
0
          if (htab->tls_get_addr_fd != NULL)
8608
0
      for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8609
0
           ent != NULL;
8610
0
           ent = ent->next)
8611
0
        if (ent->addend == 0)
8612
0
          break;
8613
8614
0
          if (ent == NULL && htab->tga_desc_fd != NULL)
8615
0
      for (ent = htab->tga_desc_fd->elf.plt.plist;
8616
0
           ent != NULL;
8617
0
           ent = ent->next)
8618
0
        if (ent->addend == 0)
8619
0
          break;
8620
8621
0
          if (ent == NULL && htab->tls_get_addr != NULL)
8622
0
      for (ent = htab->tls_get_addr->elf.plt.plist;
8623
0
           ent != NULL;
8624
0
           ent = ent->next)
8625
0
        if (ent->addend == 0)
8626
0
          break;
8627
8628
0
          if (ent == NULL && htab->tga_desc != NULL)
8629
0
      for (ent = htab->tga_desc->elf.plt.plist;
8630
0
           ent != NULL;
8631
0
           ent = ent->next)
8632
0
        if (ent->addend == 0)
8633
0
          break;
8634
8635
0
          if (ent != NULL
8636
0
        && ent->plt.refcount > 0)
8637
0
      ent->plt.refcount -= 1;
8638
0
        }
8639
8640
0
      if (tls_clear == 0)
8641
0
        continue;
8642
8643
0
      if ((tls_set & TLS_EXPLICIT) == 0)
8644
0
        {
8645
0
          struct got_entry *ent;
8646
8647
          /* Adjust got entry for this reloc.  */
8648
0
          if (h != NULL)
8649
0
      ent = h->got.glist;
8650
0
          else
8651
0
      ent = elf_local_got_ents (ibfd)[r_symndx];
8652
8653
0
          for (; ent != NULL; ent = ent->next)
8654
0
      if (ent->addend == rel->r_addend
8655
0
          && ent->owner == ibfd
8656
0
          && ent->tls_type == tls_type)
8657
0
        break;
8658
0
          if (ent == NULL)
8659
0
      abort ();
8660
8661
0
          if (tls_set == 0)
8662
0
      {
8663
        /* We managed to get rid of a got entry.  */
8664
0
        if (ent->got.refcount > 0)
8665
0
          ent->got.refcount -= 1;
8666
0
      }
8667
0
        }
8668
0
      else
8669
0
        {
8670
          /* If we got rid of a DTPMOD/DTPREL reloc pair then
8671
       we'll lose one or two dyn relocs.  */
8672
0
          if (!dec_dynrel_count (rel, sec, info,
8673
0
               NULL, h, sym))
8674
0
      return false;
8675
8676
0
          if (tls_set == (TLS_EXPLICIT | TLS_GD))
8677
0
      {
8678
0
        if (!dec_dynrel_count (rel + 1, sec, info,
8679
0
             NULL, h, sym))
8680
0
          return false;
8681
0
      }
8682
0
        }
8683
8684
0
      *tls_mask |= tls_set & 0xff;
8685
0
      *tls_mask &= ~tls_clear;
8686
0
    }
8687
8688
0
        if (elf_section_data (sec)->relocs != relstart)
8689
0
    free (relstart);
8690
0
      }
8691
8692
0
  if (locsyms != NULL
8693
0
      && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8694
0
    {
8695
0
      if (!info->keep_memory)
8696
0
        free (locsyms);
8697
0
      else
8698
0
        elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8699
0
    }
8700
0
      }
8701
8702
0
  free (toc_ref);
8703
0
  return true;
8704
0
}
8705
8706
/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8707
   the values of any global symbols in a toc section that has been
8708
   edited.  Globals in toc sections should be a rarity, so this function
8709
   sets a flag if any are found in toc sections other than the one just
8710
   edited, so that further hash table traversals can be avoided.  */
8711
8712
struct adjust_toc_info
8713
{
8714
  asection *toc;
8715
  unsigned long *skip;
8716
  bool global_toc_syms;
8717
};
8718
8719
enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8720
8721
static bool
8722
adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8723
0
{
8724
0
  struct ppc_link_hash_entry *eh;
8725
0
  struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8726
0
  unsigned long i;
8727
8728
0
  if (h->root.type != bfd_link_hash_defined
8729
0
      && h->root.type != bfd_link_hash_defweak)
8730
0
    return true;
8731
8732
0
  eh = ppc_elf_hash_entry (h);
8733
0
  if (eh->adjust_done)
8734
0
    return true;
8735
8736
0
  if (eh->elf.root.u.def.section == toc_inf->toc)
8737
0
    {
8738
0
      if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8739
0
  i = toc_inf->toc->rawsize >> 3;
8740
0
      else
8741
0
  i = eh->elf.root.u.def.value >> 3;
8742
8743
0
      if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8744
0
  {
8745
0
    _bfd_error_handler
8746
0
      (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8747
0
    do
8748
0
      ++i;
8749
0
    while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8750
0
    eh->elf.root.u.def.value = (bfd_vma) i << 3;
8751
0
  }
8752
8753
0
      eh->elf.root.u.def.value -= toc_inf->skip[i];
8754
0
      eh->adjust_done = 1;
8755
0
    }
8756
0
  else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8757
0
    toc_inf->global_toc_syms = true;
8758
8759
0
  return true;
8760
0
}
8761
8762
/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8763
   on a _LO variety toc/got reloc.  */
8764
8765
static bool
8766
ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8767
0
{
8768
0
  return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8769
0
    || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8770
0
    || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8771
0
    || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8772
0
    || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8773
0
    || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8774
0
    || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8775
0
    || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8776
0
    || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8777
0
    || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8778
0
    || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8779
0
    || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8780
0
    || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8781
0
    || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8782
0
    || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8783
0
    || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8784
0
    || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8785
        /* Exclude lfqu by testing reloc.  If relocs are ever
8786
     defined for the reduced D field in psq_lu then those
8787
     will need testing too.  */
8788
0
        && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8789
0
    || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
8790
0
        && (insn & 1) == 0)
8791
0
    || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8792
0
    || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8793
        /* Exclude stfqu.  psq_stu as above for psq_lu.  */
8794
0
        && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8795
0
    || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
8796
0
        && (insn & 1) == 0));
8797
0
}
8798
8799
/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8800
     pld ra,symbol@got@pcrel
8801
     load/store rt,off(ra)
8802
   or
8803
     pla ra,symbol@pcrel
8804
     load/store rt,off(ra)
8805
   may be translated to
8806
     pload/pstore rt,symbol+off@pcrel
8807
     nop.
8808
   This function returns true if the optimization is possible, placing
8809
   the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8810
8811
   On entry to this function, the linker has already determined that
8812
   the pld can be replaced with pla: *PINSN1 is that pla insn,
8813
   while *PINSN2 is the second instruction.  */
8814
8815
static bool
8816
xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8817
0
{
8818
0
  uint64_t insn1 = *pinsn1;
8819
0
  uint64_t insn2 = *pinsn2;
8820
0
  bfd_signed_vma off;
8821
8822
0
  if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8823
0
    {
8824
      /* Check that regs match.  */
8825
0
      if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8826
0
  return false;
8827
8828
      /* P8LS or PMLS form, non-pcrel.  */
8829
0
      if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8830
0
  return false;
8831
8832
0
      *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8833
0
      *pinsn2 = PNOP;
8834
0
      off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8835
0
      *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8836
0
      return true;
8837
0
    }
8838
8839
0
  insn2 >>= 32;
8840
8841
  /* Check that regs match.  */
8842
0
  if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8843
0
    return false;
8844
8845
0
  switch ((insn2 >> 26) & 63)
8846
0
    {
8847
0
    default:
8848
0
      return false;
8849
8850
0
    case 32: /* lwz */
8851
0
    case 34: /* lbz */
8852
0
    case 36: /* stw */
8853
0
    case 38: /* stb */
8854
0
    case 40: /* lhz */
8855
0
    case 42: /* lha */
8856
0
    case 44: /* sth */
8857
0
    case 48: /* lfs */
8858
0
    case 50: /* lfd */
8859
0
    case 52: /* stfs */
8860
0
    case 54: /* stfd */
8861
      /* These are the PMLS cases, where we just need to tack a prefix
8862
   on the insn.  */
8863
0
      insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8864
0
         | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8865
0
      off = insn2 & 0xffff;
8866
0
      break;
8867
8868
0
    case 58: /* lwa, ld */
8869
0
      if ((insn2 & 1) != 0)
8870
0
  return false;
8871
0
      insn1 = ((1ULL << 58) | (1ULL << 52)
8872
0
         | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8873
0
         | (insn2 & (31ULL << 21)));
8874
0
      off = insn2 & 0xfffc;
8875
0
      break;
8876
8877
0
    case 57: /* lxsd, lxssp */
8878
0
      if ((insn2 & 3) < 2)
8879
0
  return false;
8880
0
      insn1 = ((1ULL << 58) | (1ULL << 52)
8881
0
         | ((40ULL | (insn2 & 3)) << 26)
8882
0
         | (insn2 & (31ULL << 21)));
8883
0
      off = insn2 & 0xfffc;
8884
0
      break;
8885
8886
0
    case 61: /* stxsd, stxssp, lxv, stxv  */
8887
0
      if ((insn2 & 3) == 0)
8888
0
  return false;
8889
0
      else if ((insn2 & 3) >= 2)
8890
0
  {
8891
0
    insn1 = ((1ULL << 58) | (1ULL << 52)
8892
0
       | ((44ULL | (insn2 & 3)) << 26)
8893
0
       | (insn2 & (31ULL << 21)));
8894
0
    off = insn2 & 0xfffc;
8895
0
  }
8896
0
      else
8897
0
  {
8898
0
    insn1 = ((1ULL << 58) | (1ULL << 52)
8899
0
       | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8900
0
       | (insn2 & (31ULL << 21)));
8901
0
    off = insn2 & 0xfff0;
8902
0
  }
8903
0
      break;
8904
8905
0
    case 56: /* lq */
8906
0
      insn1 = ((1ULL << 58) | (1ULL << 52)
8907
0
         | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8908
0
      off = insn2 & 0xffff;
8909
0
      break;
8910
8911
0
    case 6: /* lxvp, stxvp */
8912
0
      if ((insn2 & 0xe) != 0)
8913
0
  return false;
8914
0
      insn1 = ((1ULL << 58) | (1ULL << 52)
8915
0
         | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8916
0
         | (insn2 & (31ULL << 21)));
8917
0
      off = insn2 & 0xfff0;
8918
0
      break;
8919
8920
0
    case 62: /* std, stq */
8921
0
      if ((insn2 & 1) != 0)
8922
0
  return false;
8923
0
      insn1 = ((1ULL << 58) | (1ULL << 52)
8924
0
         | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8925
0
         | (insn2 & (31ULL << 21)));
8926
0
      off = insn2 & 0xfffc;
8927
0
      break;
8928
0
    }
8929
8930
0
  *pinsn1 = insn1;
8931
0
  *pinsn2 = (uint64_t) NOP << 32;
8932
0
  *poff = (off ^ 0x8000) - 0x8000;
8933
0
  return true;
8934
0
}
8935
8936
/* Examine all relocs referencing .toc sections in order to remove
8937
   unused .toc entries.  */
8938
8939
bool
8940
ppc64_elf_edit_toc (struct bfd_link_info *info)
8941
0
{
8942
0
  bfd *ibfd;
8943
0
  struct adjust_toc_info toc_inf;
8944
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
8945
8946
0
  htab->do_toc_opt = 1;
8947
0
  toc_inf.global_toc_syms = true;
8948
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8949
0
    {
8950
0
      asection *toc, *sec;
8951
0
      Elf_Internal_Shdr *symtab_hdr;
8952
0
      Elf_Internal_Sym *local_syms;
8953
0
      Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8954
0
      unsigned long *skip, *drop;
8955
0
      unsigned char *used;
8956
0
      unsigned char *keep, last, some_unused;
8957
8958
0
      if (!is_ppc64_elf (ibfd))
8959
0
  continue;
8960
8961
0
      toc = bfd_get_section_by_name (ibfd, ".toc");
8962
0
      if (toc == NULL
8963
0
    || toc->size == 0
8964
0
    || (toc->flags & SEC_HAS_CONTENTS) == 0
8965
0
    || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8966
0
    || discarded_section (toc))
8967
0
  continue;
8968
8969
0
      toc_relocs = NULL;
8970
0
      local_syms = NULL;
8971
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
8972
8973
      /* Look at sections dropped from the final link.  */
8974
0
      skip = NULL;
8975
0
      relstart = NULL;
8976
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8977
0
  {
8978
0
    if (sec->reloc_count == 0
8979
0
        || !discarded_section (sec)
8980
0
        || get_opd_info (sec)
8981
0
        || (sec->flags & SEC_ALLOC) == 0
8982
0
        || (sec->flags & SEC_DEBUGGING) != 0)
8983
0
      continue;
8984
8985
0
    relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
8986
0
    if (relstart == NULL)
8987
0
      goto error_ret;
8988
8989
    /* Run through the relocs to see which toc entries might be
8990
       unused.  */
8991
0
    for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8992
0
      {
8993
0
        enum elf_ppc64_reloc_type r_type;
8994
0
        unsigned long r_symndx;
8995
0
        asection *sym_sec;
8996
0
        struct elf_link_hash_entry *h;
8997
0
        Elf_Internal_Sym *sym;
8998
0
        bfd_vma val;
8999
9000
0
        r_type = ELF64_R_TYPE (rel->r_info);
9001
0
        switch (r_type)
9002
0
    {
9003
0
    default:
9004
0
      continue;
9005
9006
0
    case R_PPC64_TOC16:
9007
0
    case R_PPC64_TOC16_LO:
9008
0
    case R_PPC64_TOC16_HI:
9009
0
    case R_PPC64_TOC16_HA:
9010
0
    case R_PPC64_TOC16_DS:
9011
0
    case R_PPC64_TOC16_LO_DS:
9012
0
      break;
9013
0
    }
9014
9015
0
        r_symndx = ELF64_R_SYM (rel->r_info);
9016
0
        if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9017
0
            r_symndx, ibfd))
9018
0
    goto error_ret;
9019
9020
0
        if (sym_sec != toc)
9021
0
    continue;
9022
9023
0
        if (h != NULL)
9024
0
    val = h->root.u.def.value;
9025
0
        else
9026
0
    val = sym->st_value;
9027
0
        val += rel->r_addend;
9028
9029
0
        if (val >= toc->size)
9030
0
    continue;
9031
9032
        /* Anything in the toc ought to be aligned to 8 bytes.
9033
     If not, don't mark as unused.  */
9034
0
        if (val & 7)
9035
0
    continue;
9036
9037
0
        if (skip == NULL)
9038
0
    {
9039
0
      skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9040
0
      if (skip == NULL)
9041
0
        goto error_ret;
9042
0
    }
9043
9044
0
        skip[val >> 3] = ref_from_discarded;
9045
0
      }
9046
9047
0
    if (elf_section_data (sec)->relocs != relstart)
9048
0
      free (relstart);
9049
0
  }
9050
9051
      /* For largetoc loads of address constants, we can convert
9052
   .  addis rx,2,addr@got@ha
9053
   .  ld ry,addr@got@l(rx)
9054
   to
9055
   .  addis rx,2,addr@toc@ha
9056
   .  addi ry,rx,addr@toc@l
9057
   when addr is within 2G of the toc pointer.  This then means
9058
   that the word storing "addr" in the toc is no longer needed.  */
9059
9060
0
      if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9061
0
    && toc->output_section->rawsize < (bfd_vma) 1 << 31
9062
0
    && toc->reloc_count != 0)
9063
0
  {
9064
    /* Read toc relocs.  */
9065
0
    toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9066
0
              info->keep_memory);
9067
0
    if (toc_relocs == NULL)
9068
0
      goto error_ret;
9069
9070
0
    for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9071
0
      {
9072
0
        enum elf_ppc64_reloc_type r_type;
9073
0
        unsigned long r_symndx;
9074
0
        asection *sym_sec;
9075
0
        struct elf_link_hash_entry *h;
9076
0
        Elf_Internal_Sym *sym;
9077
0
        bfd_vma val, addr;
9078
9079
0
        r_type = ELF64_R_TYPE (rel->r_info);
9080
0
        if (r_type != R_PPC64_ADDR64)
9081
0
    continue;
9082
9083
0
        r_symndx = ELF64_R_SYM (rel->r_info);
9084
0
        if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9085
0
            r_symndx, ibfd))
9086
0
    goto error_ret;
9087
9088
0
        if (sym_sec == NULL
9089
0
      || sym_sec->output_section == NULL
9090
0
      || discarded_section (sym_sec))
9091
0
    continue;
9092
9093
0
        if (!SYMBOL_REFERENCES_LOCAL (info, h)
9094
0
      || (bfd_link_pic (info)
9095
0
          && sym_sec == bfd_abs_section_ptr))
9096
0
    continue;
9097
9098
0
        if (h != NULL)
9099
0
    {
9100
0
      if (h->type == STT_GNU_IFUNC)
9101
0
        continue;
9102
0
      val = h->root.u.def.value;
9103
0
    }
9104
0
        else
9105
0
    {
9106
0
      if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9107
0
        continue;
9108
0
      val = sym->st_value;
9109
0
    }
9110
0
        val += rel->r_addend;
9111
0
        val += sym_sec->output_section->vma + sym_sec->output_offset;
9112
9113
        /* We don't yet know the exact toc pointer value, but we
9114
     know it will be somewhere in the toc section.  Don't
9115
     optimize if the difference from any possible toc
9116
     pointer is outside [ff..f80008000, 7fff7fff].  */
9117
0
        addr = toc->output_section->vma + TOC_BASE_OFF;
9118
0
        if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9119
0
    continue;
9120
9121
0
        addr = toc->output_section->vma + toc->output_section->rawsize;
9122
0
        if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9123
0
    continue;
9124
9125
0
        if (skip == NULL)
9126
0
    {
9127
0
      skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9128
0
      if (skip == NULL)
9129
0
        goto error_ret;
9130
0
    }
9131
9132
0
        skip[rel->r_offset >> 3]
9133
0
    |= can_optimize | ((rel - toc_relocs) << 2);
9134
0
      }
9135
0
  }
9136
9137
0
      if (skip == NULL)
9138
0
  continue;
9139
9140
0
      used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9141
0
      if (used == NULL)
9142
0
  {
9143
0
  error_ret:
9144
0
    if (symtab_hdr->contents != (unsigned char *) local_syms)
9145
0
      free (local_syms);
9146
0
    if (sec != NULL
9147
0
        && elf_section_data (sec)->relocs != relstart)
9148
0
      free (relstart);
9149
0
    if (elf_section_data (toc)->relocs != toc_relocs)
9150
0
      free (toc_relocs);
9151
0
    free (skip);
9152
0
    return false;
9153
0
  }
9154
9155
      /* Now check all kept sections that might reference the toc.
9156
   Check the toc itself last.  */
9157
0
      for (sec = (ibfd->sections == toc && toc->next ? toc->next
9158
0
      : ibfd->sections);
9159
0
     sec != NULL;
9160
0
     sec = (sec == toc ? NULL
9161
0
      : sec->next == NULL ? toc
9162
0
      : sec->next == toc && toc->next ? toc->next
9163
0
      : sec->next))
9164
0
  {
9165
0
    int repeat;
9166
9167
0
    if (sec->reloc_count == 0
9168
0
        || discarded_section (sec)
9169
0
        || get_opd_info (sec)
9170
0
        || (sec->flags & SEC_ALLOC) == 0
9171
0
        || (sec->flags & SEC_DEBUGGING) != 0)
9172
0
      continue;
9173
9174
0
    relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9175
0
            info->keep_memory);
9176
0
    if (relstart == NULL)
9177
0
      {
9178
0
        free (used);
9179
0
        goto error_ret;
9180
0
      }
9181
9182
    /* Mark toc entries referenced as used.  */
9183
0
    do
9184
0
      {
9185
0
        repeat = 0;
9186
0
        for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9187
0
    {
9188
0
      enum elf_ppc64_reloc_type r_type;
9189
0
      unsigned long r_symndx;
9190
0
      asection *sym_sec;
9191
0
      struct elf_link_hash_entry *h;
9192
0
      Elf_Internal_Sym *sym;
9193
0
      bfd_vma val;
9194
9195
0
      r_type = ELF64_R_TYPE (rel->r_info);
9196
0
      switch (r_type)
9197
0
        {
9198
0
        case R_PPC64_TOC16:
9199
0
        case R_PPC64_TOC16_LO:
9200
0
        case R_PPC64_TOC16_HI:
9201
0
        case R_PPC64_TOC16_HA:
9202
0
        case R_PPC64_TOC16_DS:
9203
0
        case R_PPC64_TOC16_LO_DS:
9204
          /* In case we're taking addresses of toc entries.  */
9205
0
        case R_PPC64_ADDR64:
9206
0
          break;
9207
9208
0
        default:
9209
0
          continue;
9210
0
        }
9211
9212
0
      r_symndx = ELF64_R_SYM (rel->r_info);
9213
0
      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9214
0
          r_symndx, ibfd))
9215
0
        {
9216
0
          free (used);
9217
0
          goto error_ret;
9218
0
        }
9219
9220
0
      if (sym_sec != toc)
9221
0
        continue;
9222
9223
0
      if (h != NULL)
9224
0
        val = h->root.u.def.value;
9225
0
      else
9226
0
        val = sym->st_value;
9227
0
      val += rel->r_addend;
9228
9229
0
      if (val >= toc->size)
9230
0
        continue;
9231
9232
0
      if ((skip[val >> 3] & can_optimize) != 0)
9233
0
        {
9234
0
          bfd_vma off;
9235
0
          unsigned char opc;
9236
9237
0
          switch (r_type)
9238
0
      {
9239
0
      case R_PPC64_TOC16_HA:
9240
0
        break;
9241
9242
0
      case R_PPC64_TOC16_LO_DS:
9243
0
        off = rel->r_offset;
9244
0
        off += (bfd_big_endian (ibfd) ? -2 : 3);
9245
0
        if (!bfd_get_section_contents (ibfd, sec, &opc,
9246
0
               off, 1))
9247
0
          {
9248
0
            free (used);
9249
0
            goto error_ret;
9250
0
          }
9251
0
        if ((opc & (0x3f << 2)) == (58u << 2))
9252
0
          break;
9253
        /* Fall through.  */
9254
9255
0
      default:
9256
        /* Wrong sort of reloc, or not a ld.  We may
9257
           as well clear ref_from_discarded too.  */
9258
0
        skip[val >> 3] = 0;
9259
0
      }
9260
0
        }
9261
9262
0
      if (sec != toc)
9263
0
        used[val >> 3] = 1;
9264
      /* For the toc section, we only mark as used if this
9265
         entry itself isn't unused.  */
9266
0
      else if ((used[rel->r_offset >> 3]
9267
0
          || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9268
0
         && !used[val >> 3])
9269
0
        {
9270
          /* Do all the relocs again, to catch reference
9271
       chains.  */
9272
0
          repeat = 1;
9273
0
          used[val >> 3] = 1;
9274
0
        }
9275
0
    }
9276
0
      }
9277
0
    while (repeat);
9278
9279
0
    if (elf_section_data (sec)->relocs != relstart)
9280
0
      free (relstart);
9281
0
  }
9282
9283
      /* Merge the used and skip arrays.  Assume that TOC
9284
   doublewords not appearing as either used or unused belong
9285
   to an entry more than one doubleword in size.  */
9286
0
      for (drop = skip, keep = used, last = 0, some_unused = 0;
9287
0
     drop < skip + (toc->size + 7) / 8;
9288
0
     ++drop, ++keep)
9289
0
  {
9290
0
    if (*keep)
9291
0
      {
9292
0
        *drop &= ~ref_from_discarded;
9293
0
        if ((*drop & can_optimize) != 0)
9294
0
    some_unused = 1;
9295
0
        last = 0;
9296
0
      }
9297
0
    else if ((*drop & ref_from_discarded) != 0)
9298
0
      {
9299
0
        some_unused = 1;
9300
0
        last = ref_from_discarded;
9301
0
      }
9302
0
    else
9303
0
      *drop = last;
9304
0
  }
9305
9306
0
      free (used);
9307
9308
0
      if (some_unused)
9309
0
  {
9310
0
    bfd_byte *contents, *src;
9311
0
    unsigned long off;
9312
0
    Elf_Internal_Sym *sym;
9313
0
    bool local_toc_syms = false;
9314
9315
    /* Shuffle the toc contents, and at the same time convert the
9316
       skip array from booleans into offsets.  */
9317
0
    if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9318
0
      goto error_ret;
9319
9320
0
    elf_section_data (toc)->this_hdr.contents = contents;
9321
9322
0
    for (src = contents, off = 0, drop = skip;
9323
0
         src < contents + toc->size;
9324
0
         src += 8, ++drop)
9325
0
      {
9326
0
        if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9327
0
    off += 8;
9328
0
        else if (off != 0)
9329
0
    {
9330
0
      *drop = off;
9331
0
      memcpy (src - off, src, 8);
9332
0
    }
9333
0
      }
9334
0
    *drop = off;
9335
0
    toc->rawsize = toc->size;
9336
0
    toc->size = src - contents - off;
9337
9338
    /* Adjust addends for relocs against the toc section sym,
9339
       and optimize any accesses we can.  */
9340
0
    for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9341
0
      {
9342
0
        if (sec->reloc_count == 0
9343
0
      || discarded_section (sec))
9344
0
    continue;
9345
9346
0
        relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9347
0
                info->keep_memory);
9348
0
        if (relstart == NULL)
9349
0
    goto error_ret;
9350
9351
0
        for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9352
0
    {
9353
0
      enum elf_ppc64_reloc_type r_type;
9354
0
      unsigned long r_symndx;
9355
0
      asection *sym_sec;
9356
0
      struct elf_link_hash_entry *h;
9357
0
      bfd_vma val;
9358
9359
0
      r_type = ELF64_R_TYPE (rel->r_info);
9360
0
      switch (r_type)
9361
0
        {
9362
0
        default:
9363
0
          continue;
9364
9365
0
        case R_PPC64_TOC16:
9366
0
        case R_PPC64_TOC16_LO:
9367
0
        case R_PPC64_TOC16_HI:
9368
0
        case R_PPC64_TOC16_HA:
9369
0
        case R_PPC64_TOC16_DS:
9370
0
        case R_PPC64_TOC16_LO_DS:
9371
0
        case R_PPC64_ADDR64:
9372
0
          break;
9373
0
        }
9374
9375
0
      r_symndx = ELF64_R_SYM (rel->r_info);
9376
0
      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9377
0
          r_symndx, ibfd))
9378
0
        goto error_ret;
9379
9380
0
      if (sym_sec != toc)
9381
0
        continue;
9382
9383
0
      if (h != NULL)
9384
0
        val = h->root.u.def.value;
9385
0
      else
9386
0
        {
9387
0
          val = sym->st_value;
9388
0
          if (val != 0)
9389
0
      local_toc_syms = true;
9390
0
        }
9391
9392
0
      val += rel->r_addend;
9393
9394
0
      if (val > toc->rawsize)
9395
0
        val = toc->rawsize;
9396
0
      else if ((skip[val >> 3] & ref_from_discarded) != 0)
9397
0
        continue;
9398
0
      else if ((skip[val >> 3] & can_optimize) != 0)
9399
0
        {
9400
0
          Elf_Internal_Rela *tocrel
9401
0
      = toc_relocs + (skip[val >> 3] >> 2);
9402
0
          unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9403
9404
0
          switch (r_type)
9405
0
      {
9406
0
      case R_PPC64_TOC16_HA:
9407
0
        rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9408
0
        break;
9409
9410
0
      case R_PPC64_TOC16_LO_DS:
9411
0
        rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9412
0
        break;
9413
9414
0
      default:
9415
0
        if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9416
0
          ppc_howto_init ();
9417
0
        info->callbacks->einfo
9418
          /* xgettext:c-format */
9419
0
          (_("%H: %s references "
9420
0
             "optimized away TOC entry\n"),
9421
0
           ibfd, sec, rel->r_offset,
9422
0
           ppc64_elf_howto_table[r_type]->name);
9423
0
        bfd_set_error (bfd_error_bad_value);
9424
0
        goto error_ret;
9425
0
      }
9426
0
          rel->r_addend = tocrel->r_addend;
9427
0
          elf_section_data (sec)->relocs = relstart;
9428
0
          continue;
9429
0
        }
9430
9431
0
      if (h != NULL || sym->st_value != 0)
9432
0
        continue;
9433
9434
0
      rel->r_addend -= skip[val >> 3];
9435
0
      elf_section_data (sec)->relocs = relstart;
9436
0
    }
9437
9438
0
        if (elf_section_data (sec)->relocs != relstart)
9439
0
    free (relstart);
9440
0
      }
9441
9442
    /* We shouldn't have local or global symbols defined in the TOC,
9443
       but handle them anyway.  */
9444
0
    if (local_syms != NULL)
9445
0
      for (sym = local_syms;
9446
0
     sym < local_syms + symtab_hdr->sh_info;
9447
0
     ++sym)
9448
0
        if (sym->st_value != 0
9449
0
      && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9450
0
    {
9451
0
      unsigned long i;
9452
9453
0
      if (sym->st_value > toc->rawsize)
9454
0
        i = toc->rawsize >> 3;
9455
0
      else
9456
0
        i = sym->st_value >> 3;
9457
9458
0
      if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9459
0
        {
9460
0
          if (local_toc_syms)
9461
0
      _bfd_error_handler
9462
0
        (_("%s defined on removed toc entry"),
9463
0
         bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9464
0
          do
9465
0
      ++i;
9466
0
          while ((skip[i] & (ref_from_discarded | can_optimize)));
9467
0
          sym->st_value = (bfd_vma) i << 3;
9468
0
        }
9469
9470
0
      sym->st_value -= skip[i];
9471
0
      symtab_hdr->contents = (unsigned char *) local_syms;
9472
0
    }
9473
9474
    /* Adjust any global syms defined in this toc input section.  */
9475
0
    if (toc_inf.global_toc_syms)
9476
0
      {
9477
0
        toc_inf.toc = toc;
9478
0
        toc_inf.skip = skip;
9479
0
        toc_inf.global_toc_syms = false;
9480
0
        elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9481
0
              &toc_inf);
9482
0
      }
9483
9484
0
    if (toc->reloc_count != 0)
9485
0
      {
9486
0
        Elf_Internal_Shdr *rel_hdr;
9487
0
        Elf_Internal_Rela *wrel;
9488
0
        bfd_size_type sz;
9489
9490
        /* Remove unused toc relocs, and adjust those we keep.  */
9491
0
        if (toc_relocs == NULL)
9492
0
    toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9493
0
              info->keep_memory);
9494
0
        if (toc_relocs == NULL)
9495
0
    goto error_ret;
9496
9497
0
        wrel = toc_relocs;
9498
0
        for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9499
0
    if ((skip[rel->r_offset >> 3]
9500
0
         & (ref_from_discarded | can_optimize)) == 0)
9501
0
      {
9502
0
        wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9503
0
        wrel->r_info = rel->r_info;
9504
0
        wrel->r_addend = rel->r_addend;
9505
0
        ++wrel;
9506
0
      }
9507
0
    else if (!dec_dynrel_count (rel, toc, info,
9508
0
              &local_syms, NULL, NULL))
9509
0
      goto error_ret;
9510
9511
0
        elf_section_data (toc)->relocs = toc_relocs;
9512
0
        toc->reloc_count = wrel - toc_relocs;
9513
0
        rel_hdr = _bfd_elf_single_rel_hdr (toc);
9514
0
        sz = rel_hdr->sh_entsize;
9515
0
        rel_hdr->sh_size = toc->reloc_count * sz;
9516
0
      }
9517
0
  }
9518
0
      else if (elf_section_data (toc)->relocs != toc_relocs)
9519
0
  free (toc_relocs);
9520
9521
0
      if (local_syms != NULL
9522
0
    && symtab_hdr->contents != (unsigned char *) local_syms)
9523
0
  {
9524
0
    if (!info->keep_memory)
9525
0
      free (local_syms);
9526
0
    else
9527
0
      symtab_hdr->contents = (unsigned char *) local_syms;
9528
0
  }
9529
0
      free (skip);
9530
0
    }
9531
9532
  /* Look for cases where we can change an indirect GOT access to
9533
     a GOT relative or PC relative access, possibly reducing the
9534
     number of GOT entries.  */
9535
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9536
0
    {
9537
0
      asection *sec;
9538
0
      Elf_Internal_Shdr *symtab_hdr;
9539
0
      Elf_Internal_Sym *local_syms;
9540
0
      Elf_Internal_Rela *relstart, *rel;
9541
0
      bfd_vma got;
9542
9543
0
      if (!is_ppc64_elf (ibfd))
9544
0
  continue;
9545
9546
0
      if (!ppc64_elf_tdata (ibfd)->has_optrel)
9547
0
  continue;
9548
9549
0
      sec = ppc64_elf_tdata (ibfd)->got;
9550
0
      got = 0;
9551
0
      if (sec != NULL)
9552
0
  got = sec->output_section->vma + sec->output_offset + 0x8000;
9553
9554
0
      local_syms = NULL;
9555
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
9556
9557
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9558
0
  {
9559
0
    if (sec->reloc_count == 0
9560
0
        || !ppc64_elf_section_data (sec)->has_optrel
9561
0
        || discarded_section (sec))
9562
0
      continue;
9563
9564
0
    relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9565
0
            info->keep_memory);
9566
0
    if (relstart == NULL)
9567
0
      {
9568
0
      got_error_ret:
9569
0
        if (symtab_hdr->contents != (unsigned char *) local_syms)
9570
0
    free (local_syms);
9571
0
        if (sec != NULL
9572
0
      && elf_section_data (sec)->relocs != relstart)
9573
0
    free (relstart);
9574
0
        return false;
9575
0
      }
9576
9577
0
    for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9578
0
      {
9579
0
        enum elf_ppc64_reloc_type r_type;
9580
0
        unsigned long r_symndx;
9581
0
        Elf_Internal_Sym *sym;
9582
0
        asection *sym_sec;
9583
0
        struct elf_link_hash_entry *h;
9584
0
        struct got_entry *ent;
9585
0
        bfd_vma val, pc;
9586
0
        unsigned char buf[8];
9587
0
        unsigned int insn;
9588
0
        enum {no_check, check_lo, check_ha} insn_check;
9589
9590
0
        r_type = ELF64_R_TYPE (rel->r_info);
9591
0
        switch (r_type)
9592
0
    {
9593
0
    default:
9594
0
      insn_check = no_check;
9595
0
      break;
9596
9597
0
    case R_PPC64_PLT16_HA:
9598
0
    case R_PPC64_GOT_TLSLD16_HA:
9599
0
    case R_PPC64_GOT_TLSGD16_HA:
9600
0
    case R_PPC64_GOT_TPREL16_HA:
9601
0
    case R_PPC64_GOT_DTPREL16_HA:
9602
0
    case R_PPC64_GOT16_HA:
9603
0
    case R_PPC64_TOC16_HA:
9604
0
      insn_check = check_ha;
9605
0
      break;
9606
9607
0
    case R_PPC64_PLT16_LO:
9608
0
    case R_PPC64_PLT16_LO_DS:
9609
0
    case R_PPC64_GOT_TLSLD16_LO:
9610
0
    case R_PPC64_GOT_TLSGD16_LO:
9611
0
    case R_PPC64_GOT_TPREL16_LO_DS:
9612
0
    case R_PPC64_GOT_DTPREL16_LO_DS:
9613
0
    case R_PPC64_GOT16_LO:
9614
0
    case R_PPC64_GOT16_LO_DS:
9615
0
    case R_PPC64_TOC16_LO:
9616
0
    case R_PPC64_TOC16_LO_DS:
9617
0
      insn_check = check_lo;
9618
0
      break;
9619
0
    }
9620
9621
0
        if (insn_check != no_check)
9622
0
    {
9623
0
      bfd_vma off = rel->r_offset & ~3;
9624
9625
0
      if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9626
0
        goto got_error_ret;
9627
9628
0
      insn = bfd_get_32 (ibfd, buf);
9629
0
      if (insn_check == check_lo
9630
0
          ? !ok_lo_toc_insn (insn, r_type)
9631
0
          : ((insn & ((0x3fu << 26) | 0x1f << 16))
9632
0
       != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9633
0
        {
9634
0
          char str[12];
9635
9636
0
          ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9637
0
          sprintf (str, "%#08x", insn);
9638
0
          info->callbacks->einfo
9639
      /* xgettext:c-format */
9640
0
      (_("%H: got/toc optimization is not supported for"
9641
0
         " %s instruction\n"),
9642
0
       ibfd, sec, rel->r_offset & ~3, str);
9643
0
          continue;
9644
0
        }
9645
0
    }
9646
9647
0
        switch (r_type)
9648
0
    {
9649
    /* Note that we don't delete GOT entries for
9650
       R_PPC64_GOT16_DS since we'd need a lot more
9651
       analysis.  For starters, the preliminary layout is
9652
       before the GOT, PLT, dynamic sections and stubs are
9653
       laid out.  Then we'd need to allow for changes in
9654
       distance between sections caused by alignment.  */
9655
0
    default:
9656
0
      continue;
9657
9658
0
    case R_PPC64_GOT16_HA:
9659
0
    case R_PPC64_GOT16_LO_DS:
9660
0
    case R_PPC64_GOT_PCREL34:
9661
0
      break;
9662
0
    }
9663
9664
0
        r_symndx = ELF64_R_SYM (rel->r_info);
9665
0
        if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9666
0
            r_symndx, ibfd))
9667
0
    goto got_error_ret;
9668
9669
0
        if (sym_sec == NULL
9670
0
      || sym_sec->output_section == NULL
9671
0
      || discarded_section (sym_sec))
9672
0
    continue;
9673
9674
0
        if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9675
0
    continue;
9676
9677
0
        if (!SYMBOL_REFERENCES_LOCAL (info, h)
9678
0
      || (bfd_link_pic (info)
9679
0
          && sym_sec == bfd_abs_section_ptr))
9680
0
    continue;
9681
9682
0
        if (h != NULL)
9683
0
    val = h->root.u.def.value;
9684
0
        else
9685
0
    val = sym->st_value;
9686
0
        val += rel->r_addend;
9687
0
        val += sym_sec->output_section->vma + sym_sec->output_offset;
9688
9689
/* Fudge factor to allow for the fact that the preliminary layout
9690
   isn't exact.  Reduce limits by this factor.  */
9691
0
#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9692
9693
0
        switch (r_type)
9694
0
    {
9695
0
    default:
9696
0
      continue;
9697
9698
0
    case R_PPC64_GOT16_HA:
9699
0
      if (val - got + LIMIT_ADJUST (0x80008000ULL)
9700
0
          >= LIMIT_ADJUST (0x100000000ULL))
9701
0
        continue;
9702
9703
0
      if (!bfd_get_section_contents (ibfd, sec, buf,
9704
0
             rel->r_offset & ~3, 4))
9705
0
        goto got_error_ret;
9706
0
      insn = bfd_get_32 (ibfd, buf);
9707
0
      if (((insn & ((0x3fu << 26) | 0x1f << 16))
9708
0
           != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9709
0
        continue;
9710
0
      break;
9711
9712
0
    case R_PPC64_GOT16_LO_DS:
9713
0
      if (val - got + LIMIT_ADJUST (0x80008000ULL)
9714
0
          >= LIMIT_ADJUST (0x100000000ULL))
9715
0
        continue;
9716
0
      if (!bfd_get_section_contents (ibfd, sec, buf,
9717
0
             rel->r_offset & ~3, 4))
9718
0
        goto got_error_ret;
9719
0
      insn = bfd_get_32 (ibfd, buf);
9720
0
      if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
9721
0
        continue;
9722
0
      break;
9723
9724
0
    case R_PPC64_GOT_PCREL34:
9725
0
      pc = rel->r_offset;
9726
0
      pc += sec->output_section->vma + sec->output_offset;
9727
0
      if (val - pc + LIMIT_ADJUST (1ULL << 33)
9728
0
          >= LIMIT_ADJUST (1ULL << 34))
9729
0
        continue;
9730
0
      if (!bfd_get_section_contents (ibfd, sec, buf,
9731
0
             rel->r_offset & ~3, 8))
9732
0
        goto got_error_ret;
9733
0
      insn = bfd_get_32 (ibfd, buf);
9734
0
      if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9735
0
        continue;
9736
0
      insn = bfd_get_32 (ibfd, buf + 4);
9737
0
      if ((insn & (0x3fu << 26)) != 57u << 26)
9738
0
        continue;
9739
0
      break;
9740
0
    }
9741
0
#undef LIMIT_ADJUST
9742
9743
0
        if (h != NULL)
9744
0
    ent = h->got.glist;
9745
0
        else
9746
0
    {
9747
0
      struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9748
0
      ent = local_got_ents[r_symndx];
9749
0
    }
9750
0
        for (; ent != NULL; ent = ent->next)
9751
0
    if (ent->addend == rel->r_addend
9752
0
        && ent->owner == ibfd
9753
0
        && ent->tls_type == 0)
9754
0
      break;
9755
0
        BFD_ASSERT (ent && ent->got.refcount > 0);
9756
0
        ent->got.refcount -= 1;
9757
0
      }
9758
9759
0
    if (elf_section_data (sec)->relocs != relstart)
9760
0
      free (relstart);
9761
0
  }
9762
9763
0
      if (local_syms != NULL
9764
0
    && symtab_hdr->contents != (unsigned char *) local_syms)
9765
0
  {
9766
0
    if (!info->keep_memory)
9767
0
      free (local_syms);
9768
0
    else
9769
0
      symtab_hdr->contents = (unsigned char *) local_syms;
9770
0
  }
9771
0
    }
9772
9773
0
  return true;
9774
0
}
9775
9776
/* Return true iff input section I references the TOC using
9777
   instructions limited to +/-32k offsets.  */
9778
9779
bool
9780
ppc64_elf_has_small_toc_reloc (asection *i)
9781
0
{
9782
0
  return (is_ppc64_elf (i->owner)
9783
0
    && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9784
0
}
9785
9786
/* Allocate space for one GOT entry.  */
9787
9788
static void
9789
allocate_got (struct elf_link_hash_entry *h,
9790
        struct bfd_link_info *info,
9791
        struct got_entry *gent)
9792
0
{
9793
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
9794
0
  struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
9795
0
  int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9796
0
     ? 16 : 8);
9797
0
  int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9798
0
      ? 2 : 1) * sizeof (Elf64_External_Rela);
9799
0
  asection *got = ppc64_elf_tdata (gent->owner)->got;
9800
9801
0
  gent->got.offset = got->size;
9802
0
  got->size += entsize;
9803
9804
0
  if (h->type == STT_GNU_IFUNC)
9805
0
    {
9806
0
      htab->elf.irelplt->size += rentsize;
9807
0
      htab->got_reli_size += rentsize;
9808
0
    }
9809
0
  else if (((bfd_link_pic (info)
9810
0
       && (gent->tls_type == 0
9811
0
     ? !info->enable_dt_relr
9812
0
     : !(bfd_link_executable (info)
9813
0
         && SYMBOL_REFERENCES_LOCAL (info, h)))
9814
0
       && !bfd_is_abs_symbol (&h->root))
9815
0
      || (htab->elf.dynamic_sections_created
9816
0
    && h->dynindx != -1
9817
0
    && !SYMBOL_REFERENCES_LOCAL (info, h)))
9818
0
     && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9819
0
    {
9820
0
      asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9821
0
      relgot->size += rentsize;
9822
0
    }
9823
0
}
9824
9825
/* This function merges got entries in the same toc group.  */
9826
9827
static void
9828
merge_got_entries (struct got_entry **pent)
9829
0
{
9830
0
  struct got_entry *ent, *ent2;
9831
9832
0
  for (ent = *pent; ent != NULL; ent = ent->next)
9833
0
    if (!ent->is_indirect)
9834
0
      for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9835
0
  if (!ent2->is_indirect
9836
0
      && ent2->addend == ent->addend
9837
0
      && ent2->tls_type == ent->tls_type
9838
0
      && elf_gp (ent2->owner) == elf_gp (ent->owner))
9839
0
    {
9840
0
      ent2->is_indirect = true;
9841
0
      ent2->got.ent = ent;
9842
0
    }
9843
0
}
9844
9845
/* If H is undefined, make it dynamic if that makes sense.  */
9846
9847
static bool
9848
ensure_undef_dynamic (struct bfd_link_info *info,
9849
          struct elf_link_hash_entry *h)
9850
0
{
9851
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
9852
9853
0
  if (htab->dynamic_sections_created
9854
0
      && ((info->dynamic_undefined_weak != 0
9855
0
     && h->root.type == bfd_link_hash_undefweak)
9856
0
    || h->root.type == bfd_link_hash_undefined)
9857
0
      && h->dynindx == -1
9858
0
      && !h->forced_local
9859
0
      && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9860
0
    return bfd_elf_link_record_dynamic_symbol (info, h);
9861
0
  return true;
9862
0
}
9863
9864
/* Choose whether to use htab->iplt or htab->pltlocal rather than the
9865
   usual htab->elf.splt section for a PLT entry.  */
9866
9867
static inline
9868
bool use_local_plt (struct bfd_link_info *info,
9869
         struct elf_link_hash_entry *h)
9870
0
{
9871
0
  return (h == NULL
9872
0
    || h->dynindx == -1
9873
0
    || !elf_hash_table (info)->dynamic_sections_created);
9874
0
}
9875
9876
/* Allocate space in .plt, .got and associated reloc sections for
9877
   dynamic relocs.  */
9878
9879
static bool
9880
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9881
0
{
9882
0
  struct bfd_link_info *info;
9883
0
  struct ppc_link_hash_table *htab;
9884
0
  asection *s;
9885
0
  struct ppc_link_hash_entry *eh;
9886
0
  struct got_entry **pgent, *gent;
9887
9888
0
  if (h->root.type == bfd_link_hash_indirect)
9889
0
    return true;
9890
9891
0
  info = (struct bfd_link_info *) inf;
9892
0
  htab = ppc_hash_table (info);
9893
0
  if (htab == NULL)
9894
0
    return false;
9895
9896
0
  eh = ppc_elf_hash_entry (h);
9897
  /* Run through the TLS GD got entries first if we're changing them
9898
     to TPREL.  */
9899
0
  if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9900
0
    for (gent = h->got.glist; gent != NULL; gent = gent->next)
9901
0
      if (gent->got.refcount > 0
9902
0
    && (gent->tls_type & TLS_GD) != 0)
9903
0
  {
9904
    /* This was a GD entry that has been converted to TPREL.  If
9905
       there happens to be a TPREL entry we can use that one.  */
9906
0
    struct got_entry *ent;
9907
0
    for (ent = h->got.glist; ent != NULL; ent = ent->next)
9908
0
      if (ent->got.refcount > 0
9909
0
    && (ent->tls_type & TLS_TPREL) != 0
9910
0
    && ent->addend == gent->addend
9911
0
    && ent->owner == gent->owner)
9912
0
        {
9913
0
    gent->got.refcount = 0;
9914
0
    break;
9915
0
        }
9916
9917
    /* If not, then we'll be using our own TPREL entry.  */
9918
0
    if (gent->got.refcount != 0)
9919
0
      gent->tls_type = TLS_TLS | TLS_TPREL;
9920
0
  }
9921
9922
  /* Remove any list entry that won't generate a word in the GOT before
9923
     we call merge_got_entries.  Otherwise we risk merging to empty
9924
     entries.  */
9925
0
  pgent = &h->got.glist;
9926
0
  while ((gent = *pgent) != NULL)
9927
0
    if (gent->got.refcount > 0)
9928
0
      {
9929
0
  if ((gent->tls_type & TLS_LD) != 0
9930
0
      && SYMBOL_REFERENCES_LOCAL (info, h))
9931
0
    {
9932
0
      ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9933
0
      *pgent = gent->next;
9934
0
    }
9935
0
  else
9936
0
    pgent = &gent->next;
9937
0
      }
9938
0
    else
9939
0
      *pgent = gent->next;
9940
9941
0
  if (!htab->do_multi_toc)
9942
0
    merge_got_entries (&h->got.glist);
9943
9944
0
  for (gent = h->got.glist; gent != NULL; gent = gent->next)
9945
0
    if (!gent->is_indirect)
9946
0
      {
9947
  /* Ensure we catch all the cases where this symbol should
9948
     be made dynamic.  */
9949
0
  if (!ensure_undef_dynamic (info, h))
9950
0
    return false;
9951
9952
0
  if (!is_ppc64_elf (gent->owner))
9953
0
    abort ();
9954
9955
0
  allocate_got (h, info, gent);
9956
0
      }
9957
9958
  /* If no dynamic sections we can't have dynamic relocs, except for
9959
     IFUNCs which are handled even in static executables.  */
9960
0
  if (!htab->elf.dynamic_sections_created
9961
0
      && h->type != STT_GNU_IFUNC)
9962
0
    h->dyn_relocs = NULL;
9963
9964
  /* Discard relocs on undefined symbols that must be local.  */
9965
0
  else if (h->root.type == bfd_link_hash_undefined
9966
0
     && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9967
0
    h->dyn_relocs = NULL;
9968
9969
  /* Also discard relocs on undefined weak syms with non-default
9970
     visibility, or when dynamic_undefined_weak says so.  */
9971
0
  else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9972
0
    h->dyn_relocs = NULL;
9973
9974
0
  if (h->dyn_relocs != NULL)
9975
0
    {
9976
0
      struct ppc_dyn_relocs *p, **pp;
9977
9978
      /* In the shared -Bsymbolic case, discard space allocated for
9979
   dynamic pc-relative relocs against symbols which turn out to
9980
   be defined in regular objects.  For the normal shared case,
9981
   discard space for relocs that have become local due to symbol
9982
   visibility changes.  */
9983
0
      if (bfd_link_pic (info))
9984
0
  {
9985
    /* Relocs that use pc_count are those that appear on a call
9986
       insn, or certain REL relocs (see must_be_dyn_reloc) that
9987
       can be generated via assembly.  We want calls to
9988
       protected symbols to resolve directly to the function
9989
       rather than going via the plt.  If people want function
9990
       pointer comparisons to work as expected then they should
9991
       avoid writing weird assembly.  */
9992
0
    if (SYMBOL_CALLS_LOCAL (info, h))
9993
0
      {
9994
0
        for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
9995
0
       (p = *pp) != NULL;
9996
0
       )
9997
0
    {
9998
0
      p->count -= p->pc_count;
9999
0
      p->pc_count = 0;
10000
0
      if (p->count == 0)
10001
0
        *pp = p->next;
10002
0
      else
10003
0
        pp = &p->next;
10004
0
    }
10005
0
      }
10006
10007
0
    if (h->dyn_relocs != NULL)
10008
0
      {
10009
        /* Ensure we catch all the cases where this symbol
10010
     should be made dynamic.  */
10011
0
        if (!ensure_undef_dynamic (info, h))
10012
0
    return false;
10013
0
      }
10014
0
  }
10015
10016
      /* For a fixed position executable, discard space for
10017
   relocs against symbols which are not dynamic.  */
10018
0
      else if (h->type != STT_GNU_IFUNC)
10019
0
  {
10020
0
    if ((h->dynamic_adjusted
10021
0
         || (h->ref_regular
10022
0
       && h->root.type == bfd_link_hash_undefweak
10023
0
       && (info->dynamic_undefined_weak > 0
10024
0
           || !_bfd_elf_readonly_dynrelocs (h))))
10025
0
        && !h->def_regular
10026
0
        && !ELF_COMMON_DEF_P (h))
10027
0
      {
10028
        /* Ensure we catch all the cases where this symbol
10029
     should be made dynamic.  */
10030
0
        if (!ensure_undef_dynamic (info, h))
10031
0
    return false;
10032
10033
        /* But if that didn't work out, discard dynamic relocs.  */
10034
0
        if (h->dynindx == -1)
10035
0
    h->dyn_relocs = NULL;
10036
0
      }
10037
0
    else
10038
0
      h->dyn_relocs = NULL;
10039
0
  }
10040
10041
      /* Finally, allocate space.  */
10042
0
      for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
10043
0
  if (!discarded_section (p->sec))
10044
0
    {
10045
0
      unsigned int count;
10046
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
10047
0
      if (eh->elf.type == STT_GNU_IFUNC)
10048
0
        sreloc = htab->elf.irelplt;
10049
0
      count = p->count;
10050
0
      if (info->enable_dt_relr
10051
0
    && ((!NO_OPD_RELOCS
10052
0
         && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
10053
0
        || (eh->elf.type != STT_GNU_IFUNC
10054
0
      && SYMBOL_REFERENCES_LOCAL (info, h))))
10055
0
        count -= p->rel_count;
10056
0
      sreloc->size += count * sizeof (Elf64_External_Rela);
10057
0
    }
10058
0
    }
10059
10060
  /* We might need a PLT entry when the symbol
10061
     a) is dynamic, or
10062
     b) is an ifunc, or
10063
     c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10064
     d) has plt16 relocs and we are linking statically.  */
10065
0
  if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10066
0
      || h->type == STT_GNU_IFUNC
10067
0
      || (h->needs_plt && h->dynamic_adjusted)
10068
0
      || (h->needs_plt
10069
0
    && h->def_regular
10070
0
    && !htab->elf.dynamic_sections_created
10071
0
    && !htab->can_convert_all_inline_plt
10072
0
    && (ppc_elf_hash_entry (h)->tls_mask
10073
0
        & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10074
0
    {
10075
0
      struct plt_entry *pent;
10076
0
      bool doneone = false;
10077
0
      for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10078
0
  if (pent->plt.refcount > 0)
10079
0
    {
10080
0
      if (!ensure_undef_dynamic (info, h))
10081
0
        return false;
10082
10083
0
      if (use_local_plt (info, h))
10084
0
        {
10085
0
    if (h->type == STT_GNU_IFUNC)
10086
0
      {
10087
0
        s = htab->elf.iplt;
10088
0
        pent->plt.offset = s->size;
10089
0
        s->size += PLT_ENTRY_SIZE (htab);
10090
0
        s = htab->elf.irelplt;
10091
0
      }
10092
0
    else
10093
0
      {
10094
0
        s = htab->pltlocal;
10095
0
        pent->plt.offset = s->size;
10096
0
        s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10097
0
        s = NULL;
10098
0
        if (bfd_link_pic (info)
10099
0
      && !(info->enable_dt_relr && !htab->opd_abi))
10100
0
          s = htab->relpltlocal;
10101
0
      }
10102
0
        }
10103
0
      else
10104
0
        {
10105
    /* If this is the first .plt entry, make room for the special
10106
       first entry.  */
10107
0
    s = htab->elf.splt;
10108
0
    if (s->size == 0)
10109
0
      s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10110
10111
0
    pent->plt.offset = s->size;
10112
10113
    /* Make room for this entry.  */
10114
0
    s->size += PLT_ENTRY_SIZE (htab);
10115
10116
    /* Make room for the .glink code.  */
10117
0
    s = htab->glink;
10118
0
    if (s->size == 0)
10119
0
      s->size += GLINK_PLTRESOLVE_SIZE (htab);
10120
0
    if (htab->opd_abi)
10121
0
      {
10122
        /* We need bigger stubs past index 32767.  */
10123
0
        if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10124
0
          s->size += 4;
10125
0
        s->size += 2*4;
10126
0
      }
10127
0
    else
10128
0
      s->size += 4;
10129
10130
    /* We also need to make an entry in the .rela.plt section.  */
10131
0
    s = htab->elf.srelplt;
10132
0
        }
10133
0
      if (s != NULL)
10134
0
        s->size += sizeof (Elf64_External_Rela);
10135
0
      doneone = true;
10136
0
    }
10137
0
  else
10138
0
    pent->plt.offset = (bfd_vma) -1;
10139
0
      if (!doneone)
10140
0
  {
10141
0
    h->plt.plist = NULL;
10142
0
    h->needs_plt = 0;
10143
0
  }
10144
0
    }
10145
0
  else
10146
0
    {
10147
0
      h->plt.plist = NULL;
10148
0
      h->needs_plt = 0;
10149
0
    }
10150
10151
0
  return true;
10152
0
}
10153
10154
0
#define PPC_LO(v) ((v) & 0xffff)
10155
0
#define PPC_HI(v) (((v) >> 16) & 0xffff)
10156
0
#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10157
#define D34(v) \
10158
0
  ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10159
#define HA34(v) ((v + (1ULL << 33)) >> 34)
10160
10161
/* Called via elf_link_hash_traverse from ppc64_elf_late_size_sections
10162
   to set up space for global entry stubs.  These are put in glink,
10163
   after the branch table.  */
10164
10165
static bool
10166
size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10167
0
{
10168
0
  struct bfd_link_info *info;
10169
0
  struct ppc_link_hash_table *htab;
10170
0
  struct plt_entry *pent;
10171
0
  asection *s, *plt;
10172
10173
0
  if (h->root.type == bfd_link_hash_indirect)
10174
0
    return true;
10175
10176
0
  if (!h->pointer_equality_needed)
10177
0
    return true;
10178
10179
0
  if (h->def_regular)
10180
0
    return true;
10181
10182
0
  info = inf;
10183
0
  htab = ppc_hash_table (info);
10184
0
  if (htab == NULL)
10185
0
    return false;
10186
10187
0
  s = htab->global_entry;
10188
0
  plt = htab->elf.splt;
10189
0
  for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10190
0
    if (pent->plt.offset != (bfd_vma) -1
10191
0
  && pent->addend == 0)
10192
0
      {
10193
  /* For ELFv2, if this symbol is not defined in a regular file
10194
     and we are not generating a shared library or pie, then we
10195
     need to define the symbol in the executable on a call stub.
10196
     This is to avoid text relocations.  */
10197
0
  bfd_vma off, stub_align, stub_off, stub_size;
10198
0
  unsigned int align_power;
10199
10200
0
  stub_size = 16;
10201
0
  stub_off = s->size;
10202
0
  if (htab->params->plt_stub_align >= 0)
10203
0
    align_power = htab->params->plt_stub_align;
10204
0
  else
10205
0
    align_power = -htab->params->plt_stub_align;
10206
  /* Setting section alignment is delayed until we know it is
10207
     non-empty.  Otherwise the .text output section will be
10208
     aligned at least to plt_stub_align even when no global
10209
     entry stubs are needed.  */
10210
0
  if (!bfd_link_align_section (s, align_power))
10211
0
    return false;
10212
0
  stub_align = (bfd_vma) 1 << align_power;
10213
0
  if (htab->params->plt_stub_align >= 0
10214
0
      || ((((stub_off + stub_size - 1) & -stub_align)
10215
0
     - (stub_off & -stub_align))
10216
0
    > ((stub_size - 1) & -stub_align)))
10217
0
    stub_off = (stub_off + stub_align - 1) & -stub_align;
10218
0
  off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10219
0
  off -= stub_off + s->output_offset + s->output_section->vma;
10220
  /* Note that for --plt-stub-align negative we have a possible
10221
     dependency between stub offset and size.  Break that
10222
     dependency by assuming the max stub size when calculating
10223
     the stub offset.  */
10224
0
  if (PPC_HA (off) == 0)
10225
0
    stub_size -= 4;
10226
0
  h->root.type = bfd_link_hash_defined;
10227
0
  h->root.u.def.section = s;
10228
0
  h->root.u.def.value = stub_off;
10229
0
  s->size = stub_off + stub_size;
10230
0
  break;
10231
0
      }
10232
0
  return true;
10233
0
}
10234
10235
/* Set the sizes of the dynamic sections.  */
10236
10237
static bool
10238
ppc64_elf_late_size_sections (bfd *output_bfd,
10239
            struct bfd_link_info *info)
10240
0
{
10241
0
  struct ppc_link_hash_table *htab;
10242
0
  bfd *dynobj;
10243
0
  asection *s;
10244
0
  bool relocs;
10245
0
  bfd *ibfd;
10246
0
  struct got_entry *first_tlsld;
10247
10248
0
  htab = ppc_hash_table (info);
10249
0
  if (htab == NULL)
10250
0
    return false;
10251
10252
0
  dynobj = htab->elf.dynobj;
10253
0
  if (dynobj == NULL)
10254
0
    return true;
10255
10256
0
  if (htab->elf.dynamic_sections_created)
10257
0
    {
10258
      /* Set the contents of the .interp section to the interpreter.  */
10259
0
      if (bfd_link_executable (info) && !info->nointerp)
10260
0
  {
10261
0
    s = htab->elf.interp;
10262
0
    if (s == NULL)
10263
0
      abort ();
10264
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10265
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10266
0
    s->alloced = 1;
10267
0
  }
10268
0
    }
10269
10270
  /* Set up .got offsets for local syms, and space for local dynamic
10271
     relocs.  */
10272
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10273
0
    {
10274
0
      struct got_entry **lgot_ents;
10275
0
      struct got_entry **end_lgot_ents;
10276
0
      struct plt_entry **local_plt;
10277
0
      struct plt_entry **end_local_plt;
10278
0
      unsigned char *lgot_masks;
10279
0
      bfd_size_type locsymcount;
10280
0
      Elf_Internal_Shdr *symtab_hdr;
10281
0
      Elf_Internal_Sym *local_syms;
10282
0
      Elf_Internal_Sym *isym;
10283
10284
0
      if (!is_ppc64_elf (ibfd))
10285
0
  continue;
10286
10287
0
      for (s = ibfd->sections; s != NULL; s = s->next)
10288
0
  {
10289
0
    struct ppc_local_dyn_relocs *p;
10290
10291
0
    for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10292
0
      {
10293
0
        if (discarded_section (p->sec))
10294
0
    {
10295
      /* Input section has been discarded, either because
10296
         it is a copy of a linkonce section or due to
10297
         linker script /DISCARD/, so we'll be discarding
10298
         the relocs too.  */
10299
0
    }
10300
0
        else if (p->count != 0)
10301
0
    {
10302
0
      unsigned int count;
10303
0
      asection *srel;
10304
10305
0
      count = p->count;
10306
0
      if (info->enable_dt_relr
10307
0
          && ((!NO_OPD_RELOCS
10308
0
         && (ppc64_elf_section_data (p->sec)->sec_type
10309
0
             == sec_opd))
10310
0
        || !p->ifunc))
10311
0
        count -= p->rel_count;
10312
0
      srel = elf_section_data (p->sec)->sreloc;
10313
0
      if (p->ifunc)
10314
0
        srel = htab->elf.irelplt;
10315
0
      srel->size += count * sizeof (Elf64_External_Rela);
10316
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10317
0
        info->flags |= DF_TEXTREL;
10318
0
    }
10319
0
      }
10320
0
  }
10321
10322
0
      lgot_ents = elf_local_got_ents (ibfd);
10323
0
      if (!lgot_ents)
10324
0
  continue;
10325
10326
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
10327
0
      locsymcount = symtab_hdr->sh_info;
10328
0
      end_lgot_ents = lgot_ents + locsymcount;
10329
0
      local_plt = (struct plt_entry **) end_lgot_ents;
10330
0
      end_local_plt = local_plt + locsymcount;
10331
0
      lgot_masks = (unsigned char *) end_local_plt;
10332
0
      local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
10333
0
      if (local_syms == NULL && locsymcount != 0)
10334
0
  {
10335
0
    local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
10336
0
               0, NULL, NULL, NULL);
10337
0
    if (local_syms == NULL)
10338
0
      return false;
10339
0
  }
10340
0
      s = ppc64_elf_tdata (ibfd)->got;
10341
0
      for (isym = local_syms;
10342
0
     lgot_ents < end_lgot_ents;
10343
0
     ++lgot_ents, ++lgot_masks, isym++)
10344
0
  {
10345
0
    struct got_entry **pent, *ent;
10346
10347
0
    pent = lgot_ents;
10348
0
    while ((ent = *pent) != NULL)
10349
0
      if (ent->got.refcount > 0)
10350
0
        {
10351
0
    if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10352
0
      {
10353
0
        ppc64_tlsld_got (ibfd)->got.refcount += 1;
10354
0
        *pent = ent->next;
10355
0
      }
10356
0
    else
10357
0
      {
10358
0
        unsigned int ent_size = 8;
10359
0
        unsigned int rel_size = sizeof (Elf64_External_Rela);
10360
10361
0
        ent->got.offset = s->size;
10362
0
        if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10363
0
          {
10364
0
      ent_size *= 2;
10365
0
      rel_size *= 2;
10366
0
          }
10367
0
        s->size += ent_size;
10368
0
        if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10369
0
          {
10370
0
      htab->elf.irelplt->size += rel_size;
10371
0
      htab->got_reli_size += rel_size;
10372
0
          }
10373
0
        else if (bfd_link_pic (info)
10374
0
           && (ent->tls_type == 0
10375
0
         ? !info->enable_dt_relr
10376
0
         : !bfd_link_executable (info))
10377
0
           && isym->st_shndx != SHN_ABS)
10378
0
          {
10379
0
      asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10380
0
      srel->size += rel_size;
10381
0
          }
10382
0
        pent = &ent->next;
10383
0
      }
10384
0
        }
10385
0
      else
10386
0
        *pent = ent->next;
10387
0
  }
10388
0
      if (local_syms != NULL
10389
0
    && symtab_hdr->contents != (unsigned char *) local_syms)
10390
0
  {
10391
0
    if (!info->keep_memory)
10392
0
      free (local_syms);
10393
0
    else
10394
0
      symtab_hdr->contents = (unsigned char *) local_syms;
10395
0
  }
10396
10397
      /* Allocate space for plt calls to local syms.  */
10398
0
      lgot_masks = (unsigned char *) end_local_plt;
10399
0
      for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10400
0
  {
10401
0
    struct plt_entry *ent;
10402
10403
0
    for (ent = *local_plt; ent != NULL; ent = ent->next)
10404
0
      if (ent->plt.refcount > 0)
10405
0
        {
10406
0
    if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10407
0
      {
10408
0
        s = htab->elf.iplt;
10409
0
        ent->plt.offset = s->size;
10410
0
        s->size += PLT_ENTRY_SIZE (htab);
10411
0
        htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10412
0
      }
10413
0
    else if (htab->can_convert_all_inline_plt
10414
0
       || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10415
0
      ent->plt.offset = (bfd_vma) -1;
10416
0
    else
10417
0
      {
10418
0
        s = htab->pltlocal;
10419
0
        ent->plt.offset = s->size;
10420
0
        s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10421
0
        if (bfd_link_pic (info)
10422
0
      && !(info->enable_dt_relr && !htab->opd_abi))
10423
0
          htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10424
0
      }
10425
0
        }
10426
0
      else
10427
0
        ent->plt.offset = (bfd_vma) -1;
10428
0
  }
10429
0
    }
10430
10431
  /* Allocate global sym .plt and .got entries, and space for global
10432
     sym dynamic relocs.  */
10433
0
  elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10434
10435
0
  if (!htab->opd_abi && !bfd_link_pic (info))
10436
0
    elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10437
10438
0
  first_tlsld = NULL;
10439
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10440
0
    {
10441
0
      struct got_entry *ent;
10442
10443
0
      if (!is_ppc64_elf (ibfd))
10444
0
  continue;
10445
10446
0
      ent = ppc64_tlsld_got (ibfd);
10447
0
      if (ent->got.refcount > 0)
10448
0
  {
10449
0
    if (!htab->do_multi_toc && first_tlsld != NULL)
10450
0
      {
10451
0
        ent->is_indirect = true;
10452
0
        ent->got.ent = first_tlsld;
10453
0
      }
10454
0
    else
10455
0
      {
10456
0
        if (first_tlsld == NULL)
10457
0
    first_tlsld = ent;
10458
0
        s = ppc64_elf_tdata (ibfd)->got;
10459
0
        ent->got.offset = s->size;
10460
0
        ent->owner = ibfd;
10461
0
        s->size += 16;
10462
0
        if (bfd_link_dll (info))
10463
0
    {
10464
0
      asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10465
0
      srel->size += sizeof (Elf64_External_Rela);
10466
0
    }
10467
0
      }
10468
0
  }
10469
0
      else
10470
0
  ent->got.offset = (bfd_vma) -1;
10471
0
    }
10472
10473
  /* We now have determined the sizes of the various dynamic sections.
10474
     Allocate memory for them.  */
10475
0
  relocs = false;
10476
0
  for (s = dynobj->sections; s != NULL; s = s->next)
10477
0
    {
10478
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
10479
0
  continue;
10480
10481
0
      if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
10482
  /* These haven't been allocated yet;  don't strip.  */
10483
0
  continue;
10484
0
      else if (s == htab->elf.sgot
10485
0
         || s == htab->elf.splt
10486
0
         || s == htab->elf.iplt
10487
0
         || s == htab->pltlocal
10488
0
         || s == htab->glink
10489
0
         || s == htab->global_entry
10490
0
         || s == htab->elf.sdynbss
10491
0
         || s == htab->elf.sdynrelro)
10492
0
  {
10493
    /* Strip this section if we don't need it; see the
10494
       comment below.  */
10495
0
  }
10496
0
      else if (s == htab->glink_eh_frame)
10497
0
  {
10498
0
    if (!bfd_is_abs_section (s->output_section))
10499
      /* Not sized yet.  */
10500
0
      continue;
10501
0
  }
10502
0
      else if (startswith (s->name, ".rela"))
10503
0
  {
10504
0
    if (s->size != 0)
10505
0
      {
10506
0
        if (s != htab->elf.srelplt)
10507
0
    relocs = true;
10508
10509
        /* We use the reloc_count field as a counter if we need
10510
     to copy relocs into the output file.  */
10511
0
        s->reloc_count = 0;
10512
0
      }
10513
0
  }
10514
0
      else
10515
0
  {
10516
    /* It's not one of our sections, so don't allocate space.  */
10517
0
    continue;
10518
0
  }
10519
10520
0
      if (s->size == 0)
10521
0
  {
10522
    /* If we don't need this section, strip it from the
10523
       output file.  This is mostly to handle .rela.bss and
10524
       .rela.plt.  We must create both sections in
10525
       create_dynamic_sections, because they must be created
10526
       before the linker maps input sections to output
10527
       sections.  The linker does that before
10528
       adjust_dynamic_symbol is called, and it is that
10529
       function which decides whether anything needs to go
10530
       into these sections.  */
10531
0
    s->flags |= SEC_EXCLUDE;
10532
0
    continue;
10533
0
  }
10534
10535
0
      if (bfd_is_abs_section (s->output_section))
10536
0
  _bfd_error_handler (_("warning: discarding dynamic section %s"),
10537
0
          s->name);
10538
10539
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
10540
0
  continue;
10541
10542
      /* Allocate memory for the section contents.  We use bfd_zalloc
10543
   here in case unused entries are not reclaimed before the
10544
   section's contents are written out.  This should not happen,
10545
   but this way if it does we get a R_PPC64_NONE reloc in .rela
10546
   sections instead of garbage.
10547
   We also rely on the section contents being zero when writing
10548
   the GOT and .dynrelro.  */
10549
0
      s->contents = bfd_zalloc (dynobj, s->size);
10550
0
      if (s->contents == NULL)
10551
0
  return false;
10552
0
      s->alloced = 1;
10553
0
    }
10554
10555
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10556
0
    {
10557
0
      if (!is_ppc64_elf (ibfd))
10558
0
  continue;
10559
10560
0
      s = ppc64_elf_tdata (ibfd)->got;
10561
0
      if (s != NULL && s != htab->elf.sgot)
10562
0
  {
10563
0
    if (s->size == 0)
10564
0
      s->flags |= SEC_EXCLUDE;
10565
0
    else
10566
0
      {
10567
0
        s->contents = bfd_zalloc (ibfd, s->size);
10568
0
        if (s->contents == NULL)
10569
0
    return false;
10570
0
        s->alloced = 1;
10571
0
      }
10572
0
  }
10573
0
      s = ppc64_elf_tdata (ibfd)->relgot;
10574
0
      if (s != NULL)
10575
0
  {
10576
0
    if (s->size == 0)
10577
0
      s->flags |= SEC_EXCLUDE;
10578
0
    else
10579
0
      {
10580
0
        s->contents = bfd_zalloc (ibfd, s->size);
10581
0
        if (s->contents == NULL)
10582
0
    return false;
10583
0
        s->alloced = 1;
10584
0
        relocs = true;
10585
0
        s->reloc_count = 0;
10586
0
      }
10587
0
  }
10588
0
    }
10589
10590
0
  if (htab->elf.dynamic_sections_created)
10591
0
    {
10592
0
      bool tls_opt;
10593
10594
      /* Add some entries to the .dynamic section.  We fill in the
10595
   values later, in ppc64_elf_finish_dynamic_sections, but we
10596
   must add the entries now so that we get the correct size for
10597
   the .dynamic section.  The DT_DEBUG entry is filled in by the
10598
   dynamic linker and used by the debugger.  */
10599
0
#define add_dynamic_entry(TAG, VAL) \
10600
0
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10601
10602
0
      if (bfd_link_executable (info))
10603
0
  {
10604
0
    if (!add_dynamic_entry (DT_DEBUG, 0))
10605
0
      return false;
10606
0
  }
10607
10608
0
      if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10609
0
  {
10610
0
    if (!add_dynamic_entry (DT_PLTGOT, 0)
10611
0
        || !add_dynamic_entry (DT_PLTRELSZ, 0)
10612
0
        || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10613
0
        || !add_dynamic_entry (DT_JMPREL, 0)
10614
0
        || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10615
0
      return false;
10616
0
  }
10617
10618
0
      if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10619
0
  {
10620
0
    if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10621
0
        || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10622
0
      return false;
10623
0
  }
10624
10625
0
      tls_opt = (htab->params->tls_get_addr_opt
10626
0
     && ((htab->tls_get_addr_fd != NULL
10627
0
          && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10628
0
         || (htab->tga_desc_fd != NULL
10629
0
       && htab->tga_desc_fd->elf.plt.plist != NULL)));
10630
0
      if (tls_opt || !htab->opd_abi)
10631
0
  {
10632
0
    if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10633
0
      return false;
10634
0
  }
10635
10636
0
      if (relocs)
10637
0
  {
10638
0
    if (!add_dynamic_entry (DT_RELA, 0)
10639
0
        || !add_dynamic_entry (DT_RELASZ, 0)
10640
0
        || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10641
0
      return false;
10642
10643
    /* If any dynamic relocs apply to a read-only section,
10644
       then we need a DT_TEXTREL entry.  */
10645
0
    if ((info->flags & DF_TEXTREL) == 0)
10646
0
      elf_link_hash_traverse (&htab->elf,
10647
0
            _bfd_elf_maybe_set_textrel, info);
10648
10649
0
    if ((info->flags & DF_TEXTREL) != 0)
10650
0
      {
10651
0
        if (!add_dynamic_entry (DT_TEXTREL, 0))
10652
0
    return false;
10653
0
      }
10654
0
  }
10655
0
    }
10656
0
#undef add_dynamic_entry
10657
10658
0
  return true;
10659
0
}
10660
10661
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
10662
10663
static bool
10664
ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10665
0
{
10666
0
  if (h->plt.plist != NULL
10667
0
      && !h->def_regular
10668
0
      && !h->pointer_equality_needed)
10669
0
    return false;
10670
10671
0
  return _bfd_elf_hash_symbol (h);
10672
0
}
10673
10674
/* Determine the type of stub needed, if any, for a call.  */
10675
10676
static inline enum ppc_stub_main_type
10677
ppc_type_of_stub (asection *input_sec,
10678
      const Elf_Internal_Rela *rel,
10679
      struct ppc_link_hash_entry **hash,
10680
      struct plt_entry **plt_ent,
10681
      bfd_vma destination,
10682
      unsigned long local_off)
10683
0
{
10684
0
  struct ppc_link_hash_entry *h = *hash;
10685
0
  bfd_vma location;
10686
0
  bfd_vma branch_offset;
10687
0
  bfd_vma max_branch_offset;
10688
0
  enum elf_ppc64_reloc_type r_type;
10689
10690
0
  if (h != NULL)
10691
0
    {
10692
0
      struct plt_entry *ent;
10693
0
      struct ppc_link_hash_entry *fdh = h;
10694
0
      if (h->oh != NULL
10695
0
    && h->oh->is_func_descriptor)
10696
0
  {
10697
0
    fdh = ppc_follow_link (h->oh);
10698
0
    *hash = fdh;
10699
0
  }
10700
10701
0
      for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10702
0
  if (ent->addend == rel->r_addend
10703
0
      && ent->plt.offset != (bfd_vma) -1)
10704
0
    {
10705
0
      *plt_ent = ent;
10706
0
      return ppc_stub_plt_call;
10707
0
    }
10708
10709
      /* Here, we know we don't have a plt entry.  If we don't have a
10710
   either a defined function descriptor or a defined entry symbol
10711
   in a regular object file, then it is pointless trying to make
10712
   any other type of stub.  */
10713
0
      if (!is_static_defined (&fdh->elf)
10714
0
    && !is_static_defined (&h->elf))
10715
0
  return ppc_stub_none;
10716
0
    }
10717
0
  else if (elf_local_got_ents (input_sec->owner) != NULL)
10718
0
    {
10719
0
      Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10720
0
      struct plt_entry **local_plt = (struct plt_entry **)
10721
0
  elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10722
0
      unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10723
10724
0
      if (local_plt[r_symndx] != NULL)
10725
0
  {
10726
0
    struct plt_entry *ent;
10727
10728
0
    for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10729
0
      if (ent->addend == rel->r_addend
10730
0
    && ent->plt.offset != (bfd_vma) -1)
10731
0
        {
10732
0
    *plt_ent = ent;
10733
0
    return ppc_stub_plt_call;
10734
0
        }
10735
0
  }
10736
0
    }
10737
10738
  /* Determine where the call point is.  */
10739
0
  location = (input_sec->output_offset
10740
0
        + input_sec->output_section->vma
10741
0
        + rel->r_offset);
10742
10743
0
  branch_offset = destination - location;
10744
0
  r_type = ELF64_R_TYPE (rel->r_info);
10745
10746
  /* Determine if a long branch stub is needed.  */
10747
0
  max_branch_offset = 1 << 25;
10748
0
  if (r_type == R_PPC64_REL14
10749
0
      || r_type == R_PPC64_REL14_BRTAKEN
10750
0
      || r_type == R_PPC64_REL14_BRNTAKEN)
10751
0
    max_branch_offset = 1 << 15;
10752
10753
0
  if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10754
    /* We need a stub.  Figure out whether a long_branch or plt_branch
10755
       is needed later.  */
10756
0
    return ppc_stub_long_branch;
10757
10758
0
  return ppc_stub_none;
10759
0
}
10760
10761
/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10762
   then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10763
   .  mflr  %r12
10764
   .  bcl 20,31,1f
10765
   .1:  mflr  %r11
10766
   .  mtlr  %r12
10767
   .  lis %r12,xxx-1b@highest
10768
   .  ori %r12,%r12,xxx-1b@higher
10769
   .  sldi  %r12,%r12,32
10770
   .  oris  %r12,%r12,xxx-1b@high
10771
   .  ori %r12,%r12,xxx-1b@l
10772
   .  add/ldx %r12,%r11,%r12  */
10773
10774
static bfd_byte *
10775
build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
10776
0
{
10777
0
  bfd_put_32 (abfd, MFLR_R12, p);
10778
0
  p += 4;
10779
0
  bfd_put_32 (abfd, BCL_20_31, p);
10780
0
  p += 4;
10781
0
  bfd_put_32 (abfd, MFLR_R11, p);
10782
0
  p += 4;
10783
0
  bfd_put_32 (abfd, MTLR_R12, p);
10784
0
  p += 4;
10785
0
  if (off + 0x8000 < 0x10000)
10786
0
    {
10787
0
      if (load)
10788
0
  bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10789
0
      else
10790
0
  bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10791
0
      p += 4;
10792
0
    }
10793
0
  else if (off + 0x80008000ULL < 0x100000000ULL)
10794
0
    {
10795
0
      bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10796
0
      p += 4;
10797
0
      if (load)
10798
0
  bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10799
0
      else
10800
0
  bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10801
0
      p += 4;
10802
0
    }
10803
0
  else
10804
0
    {
10805
0
      if (off + 0x800000000000ULL < 0x1000000000000ULL)
10806
0
  {
10807
0
    bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10808
0
    p += 4;
10809
0
  }
10810
0
      else
10811
0
  {
10812
0
    bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10813
0
    p += 4;
10814
0
    if (((off >> 32) & 0xffff) != 0)
10815
0
      {
10816
0
        bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10817
0
        p += 4;
10818
0
      }
10819
0
  }
10820
0
      if (((off >> 32) & 0xffffffffULL) != 0)
10821
0
  {
10822
0
    bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10823
0
    p += 4;
10824
0
  }
10825
0
      if (PPC_HI (off) != 0)
10826
0
  {
10827
0
    bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10828
0
    p += 4;
10829
0
  }
10830
0
      if (PPC_LO (off) != 0)
10831
0
  {
10832
0
    bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10833
0
    p += 4;
10834
0
  }
10835
0
      if (load)
10836
0
  bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10837
0
      else
10838
0
  bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10839
0
      p += 4;
10840
0
    }
10841
0
  return p;
10842
0
}
10843
10844
static unsigned int
10845
size_offset (bfd_vma off)
10846
0
{
10847
0
  unsigned int size;
10848
0
  if (off + 0x8000 < 0x10000)
10849
0
    size = 4;
10850
0
  else if (off + 0x80008000ULL < 0x100000000ULL)
10851
0
    size = 8;
10852
0
  else
10853
0
    {
10854
0
      if (off + 0x800000000000ULL < 0x1000000000000ULL)
10855
0
  size = 4;
10856
0
      else
10857
0
  {
10858
0
    size = 4;
10859
0
    if (((off >> 32) & 0xffff) != 0)
10860
0
      size += 4;
10861
0
  }
10862
0
      if (((off >> 32) & 0xffffffffULL) != 0)
10863
0
  size += 4;
10864
0
      if (PPC_HI (off) != 0)
10865
0
  size += 4;
10866
0
      if (PPC_LO (off) != 0)
10867
0
  size += 4;
10868
0
      size += 4;
10869
0
    }
10870
0
  return size + 16;
10871
0
}
10872
10873
static unsigned int
10874
num_relocs_for_offset (bfd_vma off)
10875
0
{
10876
0
  unsigned int num_rel;
10877
0
  if (off + 0x8000 < 0x10000)
10878
0
    num_rel = 1;
10879
0
  else if (off + 0x80008000ULL < 0x100000000ULL)
10880
0
    num_rel = 2;
10881
0
  else
10882
0
    {
10883
0
      num_rel = 1;
10884
0
      if (off + 0x800000000000ULL >= 0x1000000000000ULL
10885
0
    && ((off >> 32) & 0xffff) != 0)
10886
0
  num_rel += 1;
10887
0
      if (PPC_HI (off) != 0)
10888
0
  num_rel += 1;
10889
0
      if (PPC_LO (off) != 0)
10890
0
  num_rel += 1;
10891
0
    }
10892
0
  return num_rel;
10893
0
}
10894
10895
static Elf_Internal_Rela *
10896
emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10897
      bfd_vma roff, bfd_vma targ, bfd_vma off)
10898
0
{
10899
0
  bfd_vma relative_targ = targ - (roff - 8);
10900
0
  if (bfd_big_endian (info->output_bfd))
10901
0
    roff += 2;
10902
0
  r->r_offset = roff;
10903
0
  r->r_addend = relative_targ + roff;
10904
0
  if (off + 0x8000 < 0x10000)
10905
0
    r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10906
0
  else if (off + 0x80008000ULL < 0x100000000ULL)
10907
0
    {
10908
0
      r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10909
0
      ++r;
10910
0
      roff += 4;
10911
0
      r->r_offset = roff;
10912
0
      r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10913
0
      r->r_addend = relative_targ + roff;
10914
0
    }
10915
0
  else
10916
0
    {
10917
0
      if (off + 0x800000000000ULL < 0x1000000000000ULL)
10918
0
  r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10919
0
      else
10920
0
  {
10921
0
    r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10922
0
    if (((off >> 32) & 0xffff) != 0)
10923
0
      {
10924
0
        ++r;
10925
0
        roff += 4;
10926
0
        r->r_offset = roff;
10927
0
        r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10928
0
        r->r_addend = relative_targ + roff;
10929
0
      }
10930
0
  }
10931
0
      if (((off >> 32) & 0xffffffffULL) != 0)
10932
0
  roff += 4;
10933
0
      if (PPC_HI (off) != 0)
10934
0
  {
10935
0
    ++r;
10936
0
    roff += 4;
10937
0
    r->r_offset = roff;
10938
0
    r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10939
0
    r->r_addend = relative_targ + roff;
10940
0
  }
10941
0
      if (PPC_LO (off) != 0)
10942
0
  {
10943
0
    ++r;
10944
0
    roff += 4;
10945
0
    r->r_offset = roff;
10946
0
    r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10947
0
    r->r_addend = relative_targ + roff;
10948
0
  }
10949
0
    }
10950
0
  return r;
10951
0
}
10952
10953
static bfd_byte *
10954
build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10955
          bool load)
10956
0
{
10957
0
  uint64_t insn;
10958
0
  if (off - odd + (1ULL << 33) < 1ULL << 34)
10959
0
    {
10960
0
      off -= odd;
10961
0
      if (odd)
10962
0
  {
10963
0
    bfd_put_32 (abfd, NOP, p);
10964
0
    p += 4;
10965
0
  }
10966
0
      if (load)
10967
0
  insn = PLD_R12_PC;
10968
0
      else
10969
0
  insn = PADDI_R12_PC;
10970
0
      insn |= D34 (off);
10971
0
      bfd_put_32 (abfd, insn >> 32, p);
10972
0
      p += 4;
10973
0
      bfd_put_32 (abfd, insn, p);
10974
0
    }
10975
  /* The minimum value for paddi is -0x200000000.  The minimum value
10976
     for li is -0x8000, which when shifted by 34 and added gives a
10977
     minimum value of -0x2000200000000.  The maximum value is
10978
     0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1.  */
10979
0
  else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10980
0
    {
10981
0
      off -= 8 - odd;
10982
0
      bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10983
0
      p += 4;
10984
0
      if (!odd)
10985
0
  {
10986
0
    bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10987
0
    p += 4;
10988
0
  }
10989
0
      insn = PADDI_R12_PC | D34 (off);
10990
0
      bfd_put_32 (abfd, insn >> 32, p);
10991
0
      p += 4;
10992
0
      bfd_put_32 (abfd, insn, p);
10993
0
      p += 4;
10994
0
      if (odd)
10995
0
  {
10996
0
    bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10997
0
    p += 4;
10998
0
  }
10999
0
      if (load)
11000
0
  bfd_put_32 (abfd, LDX_R12_R11_R12, p);
11001
0
      else
11002
0
  bfd_put_32 (abfd, ADD_R12_R11_R12, p);
11003
0
    }
11004
0
  else
11005
0
    {
11006
0
      off -= odd + 8;
11007
0
      bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
11008
0
      p += 4;
11009
0
      bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
11010
0
      p += 4;
11011
0
      if (odd)
11012
0
  {
11013
0
    bfd_put_32 (abfd, SLDI_R11_R11_34, p);
11014
0
    p += 4;
11015
0
  }
11016
0
      insn = PADDI_R12_PC | D34 (off);
11017
0
      bfd_put_32 (abfd, insn >> 32, p);
11018
0
      p += 4;
11019
0
      bfd_put_32 (abfd, insn, p);
11020
0
      p += 4;
11021
0
      if (!odd)
11022
0
  {
11023
0
    bfd_put_32 (abfd, SLDI_R11_R11_34, p);
11024
0
    p += 4;
11025
0
  }
11026
0
      if (load)
11027
0
  bfd_put_32 (abfd, LDX_R12_R11_R12, p);
11028
0
      else
11029
0
  bfd_put_32 (abfd, ADD_R12_R11_R12, p);
11030
0
    }
11031
0
  p += 4;
11032
0
  return p;
11033
0
}
11034
11035
static unsigned int
11036
size_power10_offset (bfd_vma off, int odd)
11037
0
{
11038
0
  if (off - odd + (1ULL << 33) < 1ULL << 34)
11039
0
    return odd + 8;
11040
0
  else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11041
0
    return 20;
11042
0
  else
11043
0
    return 24;
11044
0
}
11045
11046
static unsigned int
11047
num_relocs_for_power10_offset (bfd_vma off, int odd)
11048
0
{
11049
0
  if (off - odd + (1ULL << 33) < 1ULL << 34)
11050
0
    return 1;
11051
0
  else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11052
0
    return 2;
11053
0
  else
11054
0
    return 3;
11055
0
}
11056
11057
static Elf_Internal_Rela *
11058
emit_relocs_for_power10_offset (struct bfd_link_info *info,
11059
        Elf_Internal_Rela *r, bfd_vma roff,
11060
        bfd_vma targ, bfd_vma off, int odd)
11061
0
{
11062
0
  if (off - odd + (1ULL << 33) < 1ULL << 34)
11063
0
    roff += odd;
11064
0
  else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11065
0
    {
11066
0
      int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11067
0
      r->r_offset = roff + d_offset;
11068
0
      r->r_addend = targ + 8 - odd - d_offset;
11069
0
      r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11070
0
      ++r;
11071
0
      roff += 8 - odd;
11072
0
    }
11073
0
  else
11074
0
    {
11075
0
      int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11076
0
      r->r_offset = roff + d_offset;
11077
0
      r->r_addend = targ + 8 + odd - d_offset;
11078
0
      r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
11079
0
      ++r;
11080
0
      roff += 4;
11081
0
      r->r_offset = roff + d_offset;
11082
0
      r->r_addend = targ + 4 + odd - d_offset;
11083
0
      r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11084
0
      ++r;
11085
0
      roff += 4 + odd;
11086
0
    }
11087
0
  r->r_offset = roff;
11088
0
  r->r_addend = targ;
11089
0
  r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
11090
0
  return r;
11091
0
}
11092
11093
/* Emit .eh_frame opcode to advance pc by DELTA.  */
11094
11095
static bfd_byte *
11096
eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
11097
0
{
11098
0
  delta /= 4;
11099
0
  if (delta < 64)
11100
0
    *eh++ = DW_CFA_advance_loc + delta;
11101
0
  else if (delta < 256)
11102
0
    {
11103
0
      *eh++ = DW_CFA_advance_loc1;
11104
0
      *eh++ = delta;
11105
0
    }
11106
0
  else if (delta < 65536)
11107
0
    {
11108
0
      *eh++ = DW_CFA_advance_loc2;
11109
0
      bfd_put_16 (abfd, delta, eh);
11110
0
      eh += 2;
11111
0
    }
11112
0
  else
11113
0
    {
11114
0
      *eh++ = DW_CFA_advance_loc4;
11115
0
      bfd_put_32 (abfd, delta, eh);
11116
0
      eh += 4;
11117
0
    }
11118
0
  return eh;
11119
0
}
11120
11121
/* Size of required .eh_frame opcode to advance pc by DELTA.  */
11122
11123
static unsigned int
11124
eh_advance_size (unsigned int delta)
11125
0
{
11126
0
  if (delta < 64 * 4)
11127
    /* DW_CFA_advance_loc+[1..63].  */
11128
0
    return 1;
11129
0
  if (delta < 256 * 4)
11130
    /* DW_CFA_advance_loc1, byte.  */
11131
0
    return 2;
11132
0
  if (delta < 65536 * 4)
11133
    /* DW_CFA_advance_loc2, 2 bytes.  */
11134
0
    return 3;
11135
  /* DW_CFA_advance_loc4, 4 bytes.  */
11136
0
  return 5;
11137
0
}
11138
11139
/* With power7 weakly ordered memory model, it is possible for ld.so
11140
   to update a plt entry in one thread and have another thread see a
11141
   stale zero toc entry.  To avoid this we need some sort of acquire
11142
   barrier in the call stub.  One solution is to make the load of the
11143
   toc word seem to appear to depend on the load of the function entry
11144
   word.  Another solution is to test for r2 being zero, and branch to
11145
   the appropriate glink entry if so.
11146
11147
   .  fake dep barrier  compare
11148
   .  ld 12,xxx(2)    ld 12,xxx(2)
11149
   .  mtctr 12    mtctr 12
11150
   .  xor 11,12,12    ld 2,xxx+8(2)
11151
   .  add 2,2,11    cmpldi 2,0
11152
   .  ld 2,xxx+8(2)   bnectr+
11153
   .  bctr      b <glink_entry>
11154
11155
   The solution involving the compare turns out to be faster, so
11156
   that's what we use unless the branch won't reach.  */
11157
11158
0
#define ALWAYS_USE_FAKE_DEP 0
11159
0
#define ALWAYS_EMIT_R2SAVE 0
11160
11161
static inline unsigned int
11162
plt_stub_size (struct ppc_link_hash_table *htab,
11163
         struct ppc_stub_hash_entry *stub_entry,
11164
         bfd_vma off,
11165
         unsigned int odd)
11166
0
{
11167
0
  unsigned size;
11168
11169
0
  if (stub_entry->type.sub == ppc_stub_notoc)
11170
0
    {
11171
0
      size = 8 + size_power10_offset (off, odd);
11172
0
      if (stub_entry->type.r2save)
11173
0
  size += 4;
11174
0
    }
11175
0
  else if (stub_entry->type.sub == ppc_stub_p9notoc)
11176
0
    {
11177
0
      size = 8 + size_offset (off - 8);
11178
0
      if (stub_entry->type.r2save)
11179
0
  size += 4;
11180
0
    }
11181
0
  else
11182
0
    {
11183
0
      size = 12;
11184
0
      if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11185
0
  size += 4;
11186
0
      if (PPC_HA (off) != 0)
11187
0
  size += 4;
11188
0
      if (htab->opd_abi)
11189
0
  {
11190
0
    size += 4;
11191
0
    if (htab->params->plt_static_chain)
11192
0
      size += 4;
11193
0
    if (htab->params->plt_thread_safe
11194
0
        && htab->elf.dynamic_sections_created
11195
0
        && stub_entry->h != NULL
11196
0
        && stub_entry->h->elf.dynindx != -1)
11197
0
      size += 8;
11198
0
    if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11199
0
        != PPC_HA (off))
11200
0
      size += 4;
11201
0
  }
11202
0
    }
11203
0
  if (stub_entry->h != NULL
11204
0
      && is_tls_get_addr (&stub_entry->h->elf, htab)
11205
0
      && htab->params->tls_get_addr_opt)
11206
0
    {
11207
0
      if (!htab->params->no_tls_get_addr_regsave)
11208
0
  {
11209
0
    size += 30 * 4;
11210
0
    if (stub_entry->type.r2save)
11211
0
      size += 4;
11212
0
  }
11213
0
      else
11214
0
  {
11215
0
    size += 7 * 4;
11216
0
    if (stub_entry->type.r2save)
11217
0
      size += 6 * 4;
11218
0
  }
11219
0
    }
11220
0
  return size;
11221
0
}
11222
11223
/* Depending on the sign of plt_stub_align:
11224
   If positive, return the padding to align to a 2**plt_stub_align
11225
   boundary.
11226
   If negative, if this stub would cross fewer 2**plt_stub_align
11227
   boundaries if we align, then return the padding needed to do so.  */
11228
11229
static inline unsigned int
11230
plt_stub_pad (int plt_stub_align,
11231
        bfd_vma stub_off,
11232
        unsigned int stub_size)
11233
0
{
11234
0
  unsigned int stub_align;
11235
11236
0
  if (plt_stub_align >= 0)
11237
0
    stub_align = 1u << plt_stub_align;
11238
0
  else
11239
0
    {
11240
0
      stub_align = 1u << -plt_stub_align;
11241
0
      if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11242
0
    <= ((stub_size - 1) & -stub_align))
11243
0
  return 0;
11244
0
    }
11245
0
  return stub_align - 1 - ((stub_off - 1) & (stub_align - 1));
11246
0
}
11247
11248
/* Build a toc using .plt call stub.  */
11249
11250
static inline bfd_byte *
11251
build_plt_stub (struct ppc_link_hash_table *htab,
11252
    struct ppc_stub_hash_entry *stub_entry,
11253
    bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11254
0
{
11255
0
  bfd *obfd = htab->params->stub_bfd;
11256
0
  bool plt_load_toc = htab->opd_abi;
11257
0
  bool plt_static_chain = htab->params->plt_static_chain;
11258
0
  bool plt_thread_safe = (htab->params->plt_thread_safe
11259
0
        && htab->elf.dynamic_sections_created
11260
0
        && stub_entry->h != NULL
11261
0
        && stub_entry->h->elf.dynindx != -1);
11262
0
  bool use_fake_dep = plt_thread_safe;
11263
0
  bfd_vma cmp_branch_off = 0;
11264
11265
0
  if (!ALWAYS_USE_FAKE_DEP
11266
0
      && plt_load_toc
11267
0
      && plt_thread_safe
11268
0
      && !(stub_entry->h != NULL
11269
0
     && is_tls_get_addr (&stub_entry->h->elf, htab)
11270
0
     && htab->params->tls_get_addr_opt))
11271
0
    {
11272
0
      bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11273
0
      bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11274
0
        / PLT_ENTRY_SIZE (htab));
11275
0
      bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11276
0
      bfd_vma to, from;
11277
11278
0
      if (pltindex > 32768)
11279
0
  glinkoff += (pltindex - 32768) * 4;
11280
0
      to = (glinkoff
11281
0
      + htab->glink->output_offset
11282
0
      + htab->glink->output_section->vma);
11283
0
      from = (p - stub_entry->group->stub_sec->contents
11284
0
        + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11285
0
        + 4 * (PPC_HA (offset) != 0)
11286
0
        + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11287
0
         != PPC_HA (offset))
11288
0
        + 4 * (plt_static_chain != 0)
11289
0
        + 20
11290
0
        + stub_entry->group->stub_sec->output_offset
11291
0
        + stub_entry->group->stub_sec->output_section->vma);
11292
0
      cmp_branch_off = to - from;
11293
0
      use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11294
0
    }
11295
11296
0
  if (PPC_HA (offset) != 0)
11297
0
    {
11298
0
      if (r != NULL)
11299
0
  {
11300
0
    if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11301
0
      r[0].r_offset += 4;
11302
0
    r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11303
0
    r[1].r_offset = r[0].r_offset + 4;
11304
0
    r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11305
0
    r[1].r_addend = r[0].r_addend;
11306
0
    if (plt_load_toc)
11307
0
      {
11308
0
        if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11309
0
    {
11310
0
      r[2].r_offset = r[1].r_offset + 4;
11311
0
      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11312
0
      r[2].r_addend = r[0].r_addend;
11313
0
    }
11314
0
        else
11315
0
    {
11316
0
      r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11317
0
      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11318
0
      r[2].r_addend = r[0].r_addend + 8;
11319
0
      if (plt_static_chain)
11320
0
        {
11321
0
          r[3].r_offset = r[2].r_offset + 4;
11322
0
          r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11323
0
          r[3].r_addend = r[0].r_addend + 16;
11324
0
        }
11325
0
    }
11326
0
      }
11327
0
  }
11328
0
      if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11329
0
  bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),  p += 4;
11330
0
      if (plt_load_toc)
11331
0
  {
11332
0
    bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11333
0
    bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
11334
0
  }
11335
0
      else
11336
0
  {
11337
0
    bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11338
0
    bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
11339
0
  }
11340
0
      if (plt_load_toc
11341
0
    && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11342
0
  {
11343
0
    bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11344
0
    offset = 0;
11345
0
  }
11346
0
      bfd_put_32 (obfd, MTCTR_R12, p),        p += 4;
11347
0
      if (plt_load_toc)
11348
0
  {
11349
0
    if (use_fake_dep)
11350
0
      {
11351
0
        bfd_put_32 (obfd, XOR_R2_R12_R12, p),   p += 4;
11352
0
        bfd_put_32 (obfd, ADD_R11_R11_R2, p),   p += 4;
11353
0
      }
11354
0
    bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11355
0
    if (plt_static_chain)
11356
0
      bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11357
0
  }
11358
0
    }
11359
0
  else
11360
0
    {
11361
0
      if (r != NULL)
11362
0
  {
11363
0
    if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11364
0
      r[0].r_offset += 4;
11365
0
    r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11366
0
    if (plt_load_toc)
11367
0
      {
11368
0
        if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11369
0
    {
11370
0
      r[1].r_offset = r[0].r_offset + 4;
11371
0
      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11372
0
      r[1].r_addend = r[0].r_addend;
11373
0
    }
11374
0
        else
11375
0
    {
11376
0
      r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11377
0
      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11378
0
      r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11379
0
      if (plt_static_chain)
11380
0
        {
11381
0
          r[2].r_offset = r[1].r_offset + 4;
11382
0
          r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11383
0
          r[2].r_addend = r[0].r_addend + 8;
11384
0
        }
11385
0
    }
11386
0
      }
11387
0
  }
11388
0
      if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11389
0
  bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),  p += 4;
11390
0
      bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11391
0
      if (plt_load_toc
11392
0
    && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11393
0
  {
11394
0
    bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11395
0
    offset = 0;
11396
0
  }
11397
0
      bfd_put_32 (obfd, MTCTR_R12, p),        p += 4;
11398
0
      if (plt_load_toc)
11399
0
  {
11400
0
    if (use_fake_dep)
11401
0
      {
11402
0
        bfd_put_32 (obfd, XOR_R11_R12_R12, p),    p += 4;
11403
0
        bfd_put_32 (obfd, ADD_R2_R2_R11, p),    p += 4;
11404
0
      }
11405
0
    if (plt_static_chain)
11406
0
      bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11407
0
    bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11408
0
  }
11409
0
    }
11410
0
  if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11411
0
    {
11412
0
      bfd_put_32 (obfd, CMPLDI_R2_0, p),      p += 4;
11413
0
      bfd_put_32 (obfd, BNECTR_P4, p),        p += 4;
11414
0
      bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11415
0
    }
11416
0
  else
11417
0
    bfd_put_32 (obfd, BCTR, p),         p += 4;
11418
0
  return p;
11419
0
}
11420
11421
/* Build a special .plt call stub for __tls_get_addr.  */
11422
11423
#define LD_R0_0R3 0xe8030000
11424
#define LD_R12_0R3  0xe9830000
11425
#define MR_R0_R3  0x7c601b78
11426
#define CMPDI_R0_0  0x2c200000
11427
#define ADD_R3_R12_R13  0x7c6c6a14
11428
#define BEQLR   0x4d820020
11429
#define MR_R3_R0  0x7c030378
11430
#define BCTRL   0x4e800421
11431
11432
static bfd_byte *
11433
build_tls_get_addr_head (struct ppc_link_hash_table *htab,
11434
       struct ppc_stub_hash_entry *stub_entry,
11435
       bfd_byte *p)
11436
0
{
11437
0
  bfd *obfd = htab->params->stub_bfd;
11438
11439
0
  bfd_put_32 (obfd, LD_R0_0R3 + 0, p),    p += 4;
11440
0
  bfd_put_32 (obfd, LD_R12_0R3 + 8, p),   p += 4;
11441
0
  bfd_put_32 (obfd, CMPDI_R0_0, p),   p += 4;
11442
0
  bfd_put_32 (obfd, MR_R0_R3, p),   p += 4;
11443
0
  bfd_put_32 (obfd, ADD_R3_R12_R13, p),   p += 4;
11444
0
  bfd_put_32 (obfd, BEQLR, p),      p += 4;
11445
0
  bfd_put_32 (obfd, MR_R3_R0, p),   p += 4;
11446
11447
0
  if (!htab->params->no_tls_get_addr_regsave)
11448
0
    p = tls_get_addr_prologue (obfd, p, htab);
11449
0
  else if (stub_entry->type.r2save)
11450
0
    {
11451
0
      bfd_put_32 (obfd, MFLR_R0, p);
11452
0
      p += 4;
11453
0
      bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11454
0
      p += 4;
11455
0
    }
11456
0
  return p;
11457
0
}
11458
11459
static bfd_byte *
11460
build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11461
       struct ppc_stub_hash_entry *stub_entry,
11462
       bfd_byte *p,
11463
       bfd_byte *loc)
11464
0
{
11465
0
  bfd *obfd = htab->params->stub_bfd;
11466
11467
0
  if (!htab->params->no_tls_get_addr_regsave)
11468
0
    {
11469
0
      bfd_put_32 (obfd, BCTRL, p - 4);
11470
11471
0
      if (stub_entry->type.r2save)
11472
0
  {
11473
0
    bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11474
0
    p += 4;
11475
0
  }
11476
0
      p = tls_get_addr_epilogue (obfd, p, htab);
11477
0
    }
11478
0
  else if (stub_entry->type.r2save)
11479
0
    {
11480
0
      bfd_put_32 (obfd, BCTRL, p - 4);
11481
11482
0
      bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11483
0
      p += 4;
11484
0
      bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11485
0
      p += 4;
11486
0
      bfd_put_32 (obfd, MTLR_R0, p);
11487
0
      p += 4;
11488
0
      bfd_put_32 (obfd, BLR, p);
11489
0
      p += 4;
11490
0
    }
11491
11492
0
  if (htab->glink_eh_frame != NULL
11493
0
      && htab->glink_eh_frame->size != 0)
11494
0
    {
11495
0
      bfd_byte *base, *eh;
11496
11497
0
      base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11498
0
      eh = base + stub_entry->group->eh_size;
11499
11500
0
      if (!htab->params->no_tls_get_addr_regsave)
11501
0
  {
11502
0
    unsigned int cfa_updt, delta, i;
11503
11504
    /* After the bctrl, lr has been modified so we need to emit
11505
       .eh_frame info saying the return address is on the stack.  In
11506
       fact we must put the EH info at or before the call rather
11507
       than after it, because the EH info for a call needs to be
11508
       specified by that point.
11509
       See libgcc/unwind-dw2.c execute_cfa_program.
11510
       Any stack pointer update must be described immediately after
11511
       the instruction making the change, and since the stdu occurs
11512
       after saving regs we put all the reg saves and the cfa
11513
       change there.  */
11514
0
    cfa_updt = stub_entry->stub_offset + 18 * 4;
11515
0
    delta = cfa_updt - stub_entry->group->lr_restore;
11516
0
    stub_entry->group->lr_restore
11517
0
      = stub_entry->stub_offset + (p - loc) - 4;
11518
0
    eh = eh_advance (htab->elf.dynobj, eh, delta);
11519
0
    *eh++ = DW_CFA_def_cfa_offset;
11520
0
    if (htab->opd_abi)
11521
0
      {
11522
0
        *eh++ = 128;
11523
0
        *eh++ = 1;
11524
0
      }
11525
0
    else
11526
0
      *eh++ = 96;
11527
0
    *eh++ = DW_CFA_offset_extended_sf;
11528
0
    *eh++ = 65;
11529
0
    *eh++ = (-16 / 8) & 0x7f;
11530
0
    for (i = 4; i < 12; i++)
11531
0
      {
11532
0
        *eh++ = DW_CFA_offset + i;
11533
0
        *eh++ = (htab->opd_abi ? 13 : 12) - i;
11534
0
      }
11535
0
    *eh++ = (DW_CFA_advance_loc
11536
0
       + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11537
0
    *eh++ = DW_CFA_def_cfa_offset;
11538
0
    *eh++ = 0;
11539
0
    for (i = 4; i < 12; i++)
11540
0
      *eh++ = DW_CFA_restore + i;
11541
0
    *eh++ = DW_CFA_advance_loc + 2;
11542
0
    *eh++ = DW_CFA_restore_extended;
11543
0
    *eh++ = 65;
11544
0
    stub_entry->group->eh_size = eh - base;
11545
0
  }
11546
0
      else if (stub_entry->type.r2save)
11547
0
  {
11548
0
    unsigned int lr_used, delta;
11549
11550
0
    lr_used = stub_entry->stub_offset + (p - 20 - loc);
11551
0
    delta = lr_used - stub_entry->group->lr_restore;
11552
0
    stub_entry->group->lr_restore = lr_used + 16;
11553
0
    eh = eh_advance (htab->elf.dynobj, eh, delta);
11554
0
    *eh++ = DW_CFA_offset_extended_sf;
11555
0
    *eh++ = 65;
11556
0
    *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11557
0
    *eh++ = DW_CFA_advance_loc + 4;
11558
0
    *eh++ = DW_CFA_restore_extended;
11559
0
    *eh++ = 65;
11560
0
    stub_entry->group->eh_size = eh - base;
11561
0
  }
11562
0
    }
11563
0
  return p;
11564
0
}
11565
11566
static Elf_Internal_Rela *
11567
get_relocs (asection *sec, int count)
11568
0
{
11569
0
  Elf_Internal_Rela *relocs;
11570
0
  struct bfd_elf_section_data *elfsec_data;
11571
11572
0
  elfsec_data = elf_section_data (sec);
11573
0
  relocs = elfsec_data->relocs;
11574
0
  if (relocs == NULL)
11575
0
    {
11576
0
      bfd_size_type relsize;
11577
0
      relsize = sec->reloc_count * sizeof (*relocs);
11578
0
      relocs = bfd_malloc (relsize);
11579
0
      if (relocs == NULL)
11580
0
  return NULL;
11581
0
      elfsec_data->relocs = relocs;
11582
0
      elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11583
0
            sizeof (Elf_Internal_Shdr));
11584
0
      if (elfsec_data->rela.hdr == NULL)
11585
0
  return NULL;
11586
0
      elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11587
0
          * sizeof (Elf64_External_Rela));
11588
0
      elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11589
0
      sec->reloc_count = 0;
11590
0
    }
11591
0
  relocs += sec->reloc_count;
11592
0
  sec->reloc_count += count;
11593
0
  return relocs;
11594
0
}
11595
11596
static bool
11597
swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, bfd_byte *loc, asection *s)
11598
0
{
11599
0
  if ((size_t) (loc - s->contents) >= s->size)
11600
0
    return false;
11601
0
  bfd_elf64_swap_reloca_out (obfd, rel, loc);
11602
0
  return true;
11603
0
}
11604
11605
static bool
11606
count_and_swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, asection *s)
11607
0
{
11608
0
  bfd_byte *loc = s->contents;
11609
0
  loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
11610
0
  return swap_reloc_out (obfd, rel, loc, s);
11611
0
}
11612
11613
11614
/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11615
   forms, to the equivalent relocs against the global symbol given by
11616
   STUB_ENTRY->H.  */
11617
11618
static bool
11619
use_global_in_relocs (struct ppc_link_hash_table *htab,
11620
          struct ppc_stub_hash_entry *stub_entry,
11621
          Elf_Internal_Rela *r, unsigned int num_rel)
11622
0
{
11623
0
  struct elf_link_hash_entry **hashes;
11624
0
  unsigned long symndx;
11625
0
  struct ppc_link_hash_entry *h;
11626
0
  bfd_vma symval;
11627
11628
  /* Relocs are always against symbols in their own object file.  Fake
11629
     up global sym hashes for the stub bfd (which has no symbols).  */
11630
0
  hashes = elf_sym_hashes (htab->params->stub_bfd);
11631
0
  if (hashes == NULL)
11632
0
    {
11633
0
      bfd_size_type hsize;
11634
11635
      /* When called the first time, stub_globals will contain the
11636
   total number of symbols seen during stub sizing.  After
11637
   allocating, stub_globals is used as an index to fill the
11638
   hashes array.  */
11639
0
      hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11640
0
      hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11641
0
      if (hashes == NULL)
11642
0
  return false;
11643
0
      elf_sym_hashes (htab->params->stub_bfd) = hashes;
11644
0
      Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (htab->params->stub_bfd);
11645
0
      symtab_hdr->sh_entsize = sizeof (Elf64_External_Sym);
11646
0
      symtab_hdr->sh_size = (htab->stub_globals + 1) * symtab_hdr->sh_entsize;
11647
0
      htab->stub_globals = 1;
11648
0
    }
11649
0
  symndx = htab->stub_globals++;
11650
0
  h = stub_entry->h;
11651
0
  hashes[symndx] = &h->elf;
11652
0
  if (h->oh != NULL && h->oh->is_func)
11653
0
    h = ppc_follow_link (h->oh);
11654
0
  BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11655
0
        || h->elf.root.type == bfd_link_hash_defweak);
11656
0
  symval = defined_sym_val (&h->elf);
11657
0
  while (num_rel-- != 0)
11658
0
    {
11659
0
      r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11660
0
      if (h->elf.root.u.def.section != stub_entry->target_section)
11661
0
  {
11662
    /* H is an opd symbol.  The addend must be zero, and the
11663
       branch reloc is the only one we can convert.  */
11664
0
    r->r_addend = 0;
11665
0
    break;
11666
0
  }
11667
0
      else
11668
0
  r->r_addend -= symval;
11669
0
      --r;
11670
0
    }
11671
0
  return true;
11672
0
}
11673
11674
static bfd_vma
11675
get_r2off (struct bfd_link_info *info,
11676
     struct ppc_stub_hash_entry *stub_entry)
11677
0
{
11678
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
11679
0
  bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11680
11681
0
  if (r2off == 0)
11682
0
    {
11683
      /* Support linking -R objects.  Get the toc pointer from the
11684
   opd entry.  */
11685
0
      char buf[8];
11686
0
      if (!htab->opd_abi)
11687
0
  return r2off;
11688
0
      asection *opd = stub_entry->h->elf.root.u.def.section;
11689
0
      bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11690
11691
0
      if (strcmp (opd->name, ".opd") != 0
11692
0
    || opd->reloc_count != 0)
11693
0
  {
11694
0
    info->callbacks->einfo
11695
0
      (_("%P: cannot find opd entry toc for `%pT'\n"),
11696
0
       stub_entry->h->elf.root.root.string);
11697
0
    bfd_set_error (bfd_error_bad_value);
11698
0
    return (bfd_vma) -1;
11699
0
  }
11700
0
      if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11701
0
  return (bfd_vma) -1;
11702
0
      r2off = bfd_get_64 (opd->owner, buf);
11703
0
      r2off -= elf_gp (info->output_bfd);
11704
0
    }
11705
0
  r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11706
0
  return r2off;
11707
0
}
11708
11709
/* Debug dump.  */
11710
11711
static void
11712
dump_stub (const char *header,
11713
     struct ppc_stub_hash_entry *stub_entry,
11714
     size_t end_offset)
11715
0
{
11716
0
  const char *t1, *t2, *t3;
11717
0
  switch (stub_entry->type.main)
11718
0
    {
11719
0
    case ppc_stub_none:   t1 = "none";    break;
11720
0
    case ppc_stub_long_branch:  t1 = "long_branch"; break;
11721
0
    case ppc_stub_plt_branch: t1 = "plt_branch";  break;
11722
0
    case ppc_stub_plt_call: t1 = "plt_call";  break;
11723
0
    case ppc_stub_global_entry: t1 = "global_entry";  break;
11724
0
    case ppc_stub_save_res: t1 = "save_res";  break;
11725
0
    default:      t1 = "???";   break;
11726
0
    }
11727
0
  switch (stub_entry->type.sub)
11728
0
    {
11729
0
    case ppc_stub_toc:    t2 = "toc";   break;
11730
0
    case ppc_stub_notoc:  t2 = "notoc";   break;
11731
0
    case ppc_stub_p9notoc:  t2 = "p9notoc";   break;
11732
0
    default:      t2 = "???";   break;
11733
0
    }
11734
0
  t3 = stub_entry->type.r2save ? "r2save" : "";
11735
0
  fprintf (stderr, "%s id = %u type = %s:%s:%s\n",
11736
0
     header, stub_entry->id, t1, t2, t3);
11737
0
  fprintf (stderr, "name = %s\n", stub_entry->root.string);
11738
0
  fprintf (stderr, "offset = 0x%" PRIx64 ":", stub_entry->stub_offset);
11739
0
  for (size_t i = stub_entry->stub_offset; i < end_offset; i += 4)
11740
0
    {
11741
0
      asection *stub_sec = stub_entry->group->stub_sec;
11742
0
      uint32_t *p = (uint32_t *) (stub_sec->contents + i);
11743
0
      fprintf (stderr, " %08x", (uint32_t) bfd_get_32 (stub_sec->owner, p));
11744
0
    }
11745
0
  fprintf (stderr, "\n");
11746
0
}
11747
11748
static bool
11749
ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11750
0
{
11751
0
  struct ppc_stub_hash_entry *stub_entry;
11752
0
  struct ppc_branch_hash_entry *br_entry;
11753
0
  struct bfd_link_info *info;
11754
0
  struct ppc_link_hash_table *htab;
11755
0
  bfd *obfd;
11756
0
  bfd_byte *loc;
11757
0
  bfd_byte *p, *relp;
11758
0
  bfd_vma targ, off;
11759
0
  Elf_Internal_Rela *r;
11760
0
  asection *plt;
11761
0
  int num_rel;
11762
0
  int odd;
11763
0
  bool is_tga;
11764
11765
  /* Massage our args to the form they really have.  */
11766
0
  stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11767
0
  info = in_arg;
11768
11769
0
  htab = ppc_hash_table (info);
11770
0
  if (htab == NULL)
11771
0
    return false;
11772
11773
0
  struct _ppc64_elf_section_data *esd
11774
0
    = ppc64_elf_section_data (stub_entry->group->stub_sec);
11775
0
  ++htab->stub_id;
11776
0
  if (stub_entry->id != htab->stub_id
11777
0
      || (stub_entry->type.main != ppc_stub_save_res
11778
0
    && stub_entry->stub_offset < stub_entry->group->stub_sec->size))
11779
0
    {
11780
0
      BFD_ASSERT (0);
11781
0
      if (stub_entry->id != htab->stub_id)
11782
0
  fprintf (stderr, "Expected id %u, got %u\n",
11783
0
     htab->stub_id, stub_entry->id);
11784
0
      if (stub_entry->stub_offset < stub_entry->group->stub_sec->size)
11785
0
  fprintf (stderr, "Expected offset >= %" PRIx64 ", got %"
11786
0
     PRIx64 "\n", stub_entry->group->stub_sec->size,
11787
0
     stub_entry->stub_offset);
11788
0
      if (esd->sec_type == sec_stub)
11789
0
  dump_stub ("Previous:", esd->u.last_ent, stub_entry->stub_offset);
11790
0
      dump_stub ("Current:", stub_entry, 0);
11791
0
    }
11792
0
  if (esd->sec_type == sec_normal)
11793
0
    esd->sec_type = sec_stub;
11794
0
  if (esd->sec_type == sec_stub)
11795
0
    esd->u.last_ent = stub_entry;
11796
0
  loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11797
11798
0
  htab->stub_count[stub_entry->type.main - 1] += 1;
11799
0
  if (stub_entry->type.main == ppc_stub_long_branch
11800
0
      && stub_entry->type.sub == ppc_stub_toc)
11801
0
    {
11802
      /* Branches are relative.  This is where we are going to.  */
11803
0
      targ = (stub_entry->target_value
11804
0
        + stub_entry->target_section->output_offset
11805
0
        + stub_entry->target_section->output_section->vma);
11806
0
      targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11807
11808
      /* And this is where we are coming from.  */
11809
0
      off = (stub_entry->stub_offset
11810
0
       + stub_entry->group->stub_sec->output_offset
11811
0
       + stub_entry->group->stub_sec->output_section->vma);
11812
0
      off = targ - off;
11813
11814
0
      p = loc;
11815
0
      obfd = htab->params->stub_bfd;
11816
0
      if (stub_entry->type.r2save)
11817
0
  {
11818
0
    bfd_vma r2off = get_r2off (info, stub_entry);
11819
11820
0
    if (r2off == (bfd_vma) -1)
11821
0
      {
11822
0
        htab->stub_error = true;
11823
0
        return false;
11824
0
      }
11825
0
    bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11826
0
    p += 4;
11827
0
    if (PPC_HA (r2off) != 0)
11828
0
      {
11829
0
        bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11830
0
        p += 4;
11831
0
      }
11832
0
    if (PPC_LO (r2off) != 0)
11833
0
      {
11834
0
        bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11835
0
        p += 4;
11836
0
      }
11837
0
    off -= p - loc;
11838
0
  }
11839
0
      bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
11840
0
      p += 4;
11841
11842
0
      if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11843
0
  {
11844
0
    _bfd_error_handler
11845
0
      (_("long branch stub `%s' offset overflow"),
11846
0
       stub_entry->root.string);
11847
0
    htab->stub_error = true;
11848
0
    return false;
11849
0
  }
11850
11851
0
      if (info->emitrelocations)
11852
0
  {
11853
0
    r = get_relocs (stub_entry->group->stub_sec, 1);
11854
0
    if (r == NULL)
11855
0
      return false;
11856
0
    r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11857
0
    r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11858
0
    r->r_addend = targ;
11859
0
    if (stub_entry->h != NULL
11860
0
        && !use_global_in_relocs (htab, stub_entry, r, 1))
11861
0
      return false;
11862
0
  }
11863
0
    }
11864
0
  else if (stub_entry->type.main == ppc_stub_plt_branch
11865
0
     && stub_entry->type.sub == ppc_stub_toc)
11866
0
    {
11867
0
      br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11868
0
           stub_entry->root.string + 9,
11869
0
           false, false);
11870
0
      if (br_entry == NULL)
11871
0
  {
11872
0
    _bfd_error_handler (_("can't find branch stub `%s'"),
11873
0
            stub_entry->root.string);
11874
0
    htab->stub_error = true;
11875
0
    return false;
11876
0
  }
11877
11878
0
      targ = (stub_entry->target_value
11879
0
        + stub_entry->target_section->output_offset
11880
0
        + stub_entry->target_section->output_section->vma);
11881
0
      if (!stub_entry->type.r2save)
11882
0
  targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11883
11884
0
      bfd_put_64 (htab->brlt->owner, targ,
11885
0
      htab->brlt->contents + br_entry->offset);
11886
11887
0
      if (br_entry->iter == htab->stub_iteration)
11888
0
  {
11889
0
    br_entry->iter = 0;
11890
11891
0
    if (htab->relbrlt != NULL && !info->enable_dt_relr)
11892
0
      {
11893
        /* Create a reloc for the branch lookup table entry.  */
11894
0
        Elf_Internal_Rela rela;
11895
11896
0
        rela.r_offset = (br_entry->offset
11897
0
             + htab->brlt->output_offset
11898
0
             + htab->brlt->output_section->vma);
11899
0
        rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11900
0
        rela.r_addend = targ;
11901
11902
0
        BFD_ASSERT (count_and_swap_reloc_out (htab->relbrlt->owner, &rela,
11903
0
                htab->relbrlt));
11904
0
      }
11905
0
    else if (info->emitrelocations)
11906
0
      {
11907
0
        r = get_relocs (htab->brlt, 1);
11908
0
        if (r == NULL)
11909
0
    return false;
11910
        /* brlt, being SEC_LINKER_CREATED does not go through the
11911
     normal reloc processing.  Symbols and offsets are not
11912
     translated from input file to output file form, so
11913
     set up the offset per the output file.  */
11914
0
        r->r_offset = (br_entry->offset
11915
0
           + htab->brlt->output_offset
11916
0
           + htab->brlt->output_section->vma);
11917
0
        r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11918
0
        r->r_addend = targ;
11919
0
      }
11920
0
  }
11921
11922
0
      targ = (br_entry->offset
11923
0
        + htab->brlt->output_offset
11924
0
        + htab->brlt->output_section->vma);
11925
11926
0
      off = (elf_gp (info->output_bfd)
11927
0
       + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11928
0
      off = targ - off;
11929
11930
0
      if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11931
0
  {
11932
0
    info->callbacks->einfo
11933
0
      (_("%P: linkage table error against `%pT'\n"),
11934
0
       stub_entry->root.string);
11935
0
    bfd_set_error (bfd_error_bad_value);
11936
0
    htab->stub_error = true;
11937
0
    return false;
11938
0
  }
11939
11940
0
      if (info->emitrelocations)
11941
0
  {
11942
0
    r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11943
0
    if (r == NULL)
11944
0
      return false;
11945
0
    r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11946
0
    if (bfd_big_endian (info->output_bfd))
11947
0
      r[0].r_offset += 2;
11948
0
    if (stub_entry->type.r2save)
11949
0
      r[0].r_offset += 4;
11950
0
    r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11951
0
    r[0].r_addend = targ;
11952
0
    if (PPC_HA (off) != 0)
11953
0
      {
11954
0
        r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11955
0
        r[1].r_offset = r[0].r_offset + 4;
11956
0
        r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11957
0
        r[1].r_addend = r[0].r_addend;
11958
0
      }
11959
0
  }
11960
11961
0
      p = loc;
11962
0
      obfd = htab->params->stub_bfd;
11963
0
      if (!stub_entry->type.r2save)
11964
0
  {
11965
0
    if (PPC_HA (off) != 0)
11966
0
      {
11967
0
        bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11968
0
        p += 4;
11969
0
        bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11970
0
      }
11971
0
    else
11972
0
      bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11973
0
  }
11974
0
      else
11975
0
  {
11976
0
    bfd_vma r2off = get_r2off (info, stub_entry);
11977
11978
0
    if (r2off == (bfd_vma) -1)
11979
0
      {
11980
0
        htab->stub_error = true;
11981
0
        return false;
11982
0
      }
11983
11984
0
    bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11985
0
    p += 4;
11986
0
    if (PPC_HA (off) != 0)
11987
0
      {
11988
0
        bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11989
0
        p += 4;
11990
0
        bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11991
0
      }
11992
0
    else
11993
0
      bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11994
11995
0
    if (PPC_HA (r2off) != 0)
11996
0
      {
11997
0
        p += 4;
11998
0
        bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11999
0
      }
12000
0
    if (PPC_LO (r2off) != 0)
12001
0
      {
12002
0
        p += 4;
12003
0
        bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
12004
0
      }
12005
0
  }
12006
0
      p += 4;
12007
0
      bfd_put_32 (obfd, MTCTR_R12, p);
12008
0
      p += 4;
12009
0
      bfd_put_32 (obfd, BCTR, p);
12010
0
      p += 4;
12011
0
    }
12012
0
  else if (stub_entry->type.sub >= ppc_stub_notoc)
12013
0
    {
12014
0
      bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
12015
0
      p = loc;
12016
0
      off = (stub_entry->stub_offset
12017
0
       + stub_entry->group->stub_sec->output_offset
12018
0
       + stub_entry->group->stub_sec->output_section->vma);
12019
0
      obfd = htab->params->stub_bfd;
12020
0
      is_tga = (is_plt
12021
0
    && stub_entry->h != NULL
12022
0
    && is_tls_get_addr (&stub_entry->h->elf, htab)
12023
0
    && htab->params->tls_get_addr_opt);
12024
0
      if (is_tga)
12025
0
  {
12026
0
    p = build_tls_get_addr_head (htab, stub_entry, p);
12027
0
    off += p - loc;
12028
0
  }
12029
0
      if (stub_entry->type.r2save)
12030
0
  {
12031
0
    off += 4;
12032
0
    bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
12033
0
    p += 4;
12034
0
  }
12035
0
      if (is_plt)
12036
0
  {
12037
0
    targ = stub_entry->plt_ent->plt.offset & ~1;
12038
0
    if (targ >= (bfd_vma) -2)
12039
0
      abort ();
12040
12041
0
    plt = htab->elf.splt;
12042
0
    if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12043
0
      {
12044
0
        if (stub_entry->symtype == STT_GNU_IFUNC)
12045
0
    plt = htab->elf.iplt;
12046
0
        else
12047
0
    plt = htab->pltlocal;
12048
0
      }
12049
0
    targ += plt->output_offset + plt->output_section->vma;
12050
0
  }
12051
0
      else
12052
0
  targ = (stub_entry->target_value
12053
0
    + stub_entry->target_section->output_offset
12054
0
    + stub_entry->target_section->output_section->vma);
12055
0
      odd = off & 4;
12056
0
      off = targ - off;
12057
12058
0
      relp = p;
12059
0
      num_rel = 0;
12060
0
      if (stub_entry->type.sub == ppc_stub_notoc)
12061
0
  p = build_power10_offset (obfd, p, off, odd, is_plt);
12062
0
      else
12063
0
  {
12064
0
    if (htab->glink_eh_frame != NULL
12065
0
        && htab->glink_eh_frame->size != 0)
12066
0
      {
12067
0
        bfd_byte *base, *eh;
12068
0
        unsigned int lr_used, delta;
12069
12070
0
        base = (htab->glink_eh_frame->contents
12071
0
          + stub_entry->group->eh_base + 17);
12072
0
        eh = base + stub_entry->group->eh_size;
12073
0
        lr_used = stub_entry->stub_offset + (p - loc) + 8;
12074
0
        delta = lr_used - stub_entry->group->lr_restore;
12075
0
        stub_entry->group->lr_restore = lr_used + 8;
12076
0
        eh = eh_advance (htab->elf.dynobj, eh, delta);
12077
0
        *eh++ = DW_CFA_register;
12078
0
        *eh++ = 65;
12079
0
        *eh++ = 12;
12080
0
        *eh++ = DW_CFA_advance_loc + 2;
12081
0
        *eh++ = DW_CFA_restore_extended;
12082
0
        *eh++ = 65;
12083
0
        stub_entry->group->eh_size = eh - base;
12084
0
      }
12085
12086
    /* The notoc stubs calculate their target (either a PLT entry or
12087
       the global entry point of a function) relative to the PC
12088
       returned by the "bcl" two instructions past the start of the
12089
       sequence emitted by build_offset.  The offset is therefore 8
12090
       less than calculated from the start of the sequence.  */
12091
0
    off -= 8;
12092
0
    p = build_offset (obfd, p, off, is_plt);
12093
0
  }
12094
12095
0
      if (stub_entry->type.main == ppc_stub_long_branch)
12096
0
  {
12097
0
    bfd_vma from;
12098
0
    num_rel = 1;
12099
0
    from = (stub_entry->stub_offset
12100
0
      + stub_entry->group->stub_sec->output_offset
12101
0
      + stub_entry->group->stub_sec->output_section->vma
12102
0
      + (p - loc));
12103
0
    bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
12104
0
  }
12105
0
      else
12106
0
  {
12107
0
    bfd_put_32 (obfd, MTCTR_R12, p);
12108
0
    p += 4;
12109
0
    bfd_put_32 (obfd, BCTR, p);
12110
0
  }
12111
0
      p += 4;
12112
12113
0
      if (is_tga)
12114
0
  p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12115
12116
0
      if (info->emitrelocations)
12117
0
  {
12118
0
    bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
12119
0
    if (stub_entry->type.sub == ppc_stub_notoc)
12120
0
      num_rel += num_relocs_for_power10_offset (off, odd);
12121
0
    else
12122
0
      {
12123
0
        num_rel += num_relocs_for_offset (off);
12124
0
        roff += 16;
12125
0
      }
12126
0
    r = get_relocs (stub_entry->group->stub_sec, num_rel);
12127
0
    if (r == NULL)
12128
0
      return false;
12129
0
    if (stub_entry->type.sub == ppc_stub_notoc)
12130
0
      r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
12131
0
    else
12132
0
      r = emit_relocs_for_offset (info, r, roff, targ, off);
12133
0
    if (stub_entry->type.main == ppc_stub_long_branch)
12134
0
      {
12135
0
        ++r;
12136
0
        roff = p - 4 - stub_entry->group->stub_sec->contents;
12137
0
        r->r_offset = roff;
12138
0
        r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
12139
0
        r->r_addend = targ;
12140
0
        if (stub_entry->h != NULL
12141
0
      && !use_global_in_relocs (htab, stub_entry, r, num_rel))
12142
0
    return false;
12143
0
      }
12144
0
  }
12145
0
    }
12146
0
  else if (stub_entry->type.main == ppc_stub_plt_call)
12147
0
    {
12148
0
      if (stub_entry->h != NULL
12149
0
    && stub_entry->h->is_func_descriptor
12150
0
    && stub_entry->h->oh != NULL)
12151
0
  {
12152
0
    struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
12153
12154
    /* If the old-ABI "dot-symbol" is undefined make it weak so
12155
       we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
12156
0
    if (fh->elf.root.type == bfd_link_hash_undefined
12157
0
        && (stub_entry->h->elf.root.type == bfd_link_hash_defined
12158
0
      || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
12159
0
      fh->elf.root.type = bfd_link_hash_undefweak;
12160
0
  }
12161
12162
      /* Now build the stub.  */
12163
0
      targ = stub_entry->plt_ent->plt.offset & ~1;
12164
0
      if (targ >= (bfd_vma) -2)
12165
0
  abort ();
12166
12167
0
      plt = htab->elf.splt;
12168
0
      if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12169
0
  {
12170
0
    if (stub_entry->symtype == STT_GNU_IFUNC)
12171
0
      plt = htab->elf.iplt;
12172
0
    else
12173
0
      plt = htab->pltlocal;
12174
0
  }
12175
0
      targ += plt->output_offset + plt->output_section->vma;
12176
12177
0
      off = (elf_gp (info->output_bfd)
12178
0
       + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12179
0
      off = targ - off;
12180
12181
0
      if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
12182
0
  {
12183
0
    info->callbacks->einfo
12184
      /* xgettext:c-format */
12185
0
      (_("%P: linkage table error against `%pT'\n"),
12186
0
       stub_entry->h != NULL
12187
0
       ? stub_entry->h->elf.root.root.string
12188
0
       : "<local sym>");
12189
0
    bfd_set_error (bfd_error_bad_value);
12190
0
    htab->stub_error = true;
12191
0
    return false;
12192
0
  }
12193
12194
0
      r = NULL;
12195
0
      if (info->emitrelocations)
12196
0
  {
12197
0
    r = get_relocs (stub_entry->group->stub_sec,
12198
0
        ((PPC_HA (off) != 0)
12199
0
         + (htab->opd_abi
12200
0
            ? 2 + (htab->params->plt_static_chain
12201
0
             && PPC_HA (off + 16) == PPC_HA (off))
12202
0
            : 1)));
12203
0
    if (r == NULL)
12204
0
      return false;
12205
0
    r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
12206
0
    if (bfd_big_endian (info->output_bfd))
12207
0
      r[0].r_offset += 2;
12208
0
    r[0].r_addend = targ;
12209
0
  }
12210
0
      p = loc;
12211
0
      obfd = htab->params->stub_bfd;
12212
0
      is_tga = (stub_entry->h != NULL
12213
0
    && is_tls_get_addr (&stub_entry->h->elf, htab)
12214
0
    && htab->params->tls_get_addr_opt);
12215
0
      if (is_tga)
12216
0
  {
12217
0
    p = build_tls_get_addr_head (htab, stub_entry, p);
12218
0
    if (r != NULL)
12219
0
      r[0].r_offset += p - loc;
12220
0
  }
12221
0
      p = build_plt_stub (htab, stub_entry, p, off, r);
12222
0
      if (is_tga)
12223
0
  p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12224
0
    }
12225
0
  else if (stub_entry->type.main == ppc_stub_save_res)
12226
0
    return true;
12227
0
  else
12228
0
    {
12229
0
      BFD_FAIL ();
12230
0
      return false;
12231
0
    }
12232
12233
0
  stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
12234
12235
0
  if (htab->params->emit_stub_syms)
12236
0
    {
12237
0
      struct elf_link_hash_entry *h;
12238
0
      size_t len1, len2;
12239
0
      char *name;
12240
0
      static const char stub_str[][16] = { "long_branch",
12241
0
             "plt_branch",
12242
0
             "plt_call" };
12243
12244
0
      len1 = strlen (stub_str[stub_entry->type.main - 1]);
12245
0
      len2 = strlen (stub_entry->root.string);
12246
0
      name = bfd_alloc (info->output_bfd, len1 + len2 + 2);
12247
0
      if (name == NULL)
12248
0
  return false;
12249
0
      memcpy (name, stub_entry->root.string, 9);
12250
0
      memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
12251
0
      memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
12252
0
      h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
12253
0
      if (h == NULL)
12254
0
  return false;
12255
0
      if (h->root.type == bfd_link_hash_new)
12256
0
  {
12257
0
    h->root.type = bfd_link_hash_defined;
12258
0
    h->root.u.def.section = stub_entry->group->stub_sec;
12259
0
    h->root.u.def.value = stub_entry->stub_offset;
12260
0
    h->ref_regular = 1;
12261
0
    h->def_regular = 1;
12262
0
    h->ref_regular_nonweak = 1;
12263
0
    h->forced_local = 1;
12264
0
    h->non_elf = 0;
12265
0
    h->root.linker_def = 1;
12266
0
  }
12267
0
    }
12268
12269
0
  return true;
12270
0
}
12271
12272
/* As above, but don't actually build the stub.  Just bump offset so
12273
   we know stub section sizes, and select plt_branch stubs where
12274
   long_branch stubs won't do.  */
12275
12276
static bool
12277
ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
12278
0
{
12279
0
  struct ppc_stub_hash_entry *stub_entry;
12280
0
  struct bfd_link_info *info;
12281
0
  struct ppc_link_hash_table *htab;
12282
0
  asection *plt;
12283
0
  bfd_vma targ, off, r2off;
12284
0
  unsigned int size, pad, extra, lr_used, delta, odd;
12285
0
  bfd_vma stub_offset;
12286
12287
  /* Massage our args to the form they really have.  */
12288
0
  stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
12289
0
  info = in_arg;
12290
12291
0
  htab = ppc_hash_table (info);
12292
0
  if (htab == NULL)
12293
0
    return false;
12294
12295
  /* Fail if the target section could not be assigned to an output
12296
     section.  The user should fix his linker script.  */
12297
0
  if (stub_entry->target_section != NULL
12298
0
      && stub_entry->target_section->output_section == NULL
12299
0
      && info->non_contiguous_regions)
12300
0
    info->callbacks->fatal (_("%P: Could not assign `%pA' to an output section. "
12301
0
            "Retry without --enable-non-contiguous-regions.\n"),
12302
0
          stub_entry->target_section);
12303
12304
  /* Same for the group.  */
12305
0
  if (stub_entry->group->stub_sec != NULL
12306
0
      && stub_entry->group->stub_sec->output_section == NULL
12307
0
      && info->non_contiguous_regions)
12308
0
    info->callbacks->fatal (_("%P: Could not assign `%pA' to an output section. "
12309
0
            "Retry without --enable-non-contiguous-regions.\n"),
12310
0
          stub_entry->group->stub_sec);
12311
12312
  /* Make a note of the offset within the stubs for this entry.  */
12313
0
  stub_offset = stub_entry->group->stub_sec->size;
12314
0
  if (htab->stub_iteration > STUB_SHRINK_ITER
12315
0
      && stub_entry->stub_offset > stub_offset)
12316
0
    stub_offset = stub_entry->stub_offset;
12317
0
  stub_entry->id = ++htab->stub_id;
12318
12319
0
  if (stub_entry->h != NULL
12320
0
      && stub_entry->h->save_res
12321
0
      && stub_entry->h->elf.root.type == bfd_link_hash_defined
12322
0
      && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12323
0
    {
12324
      /* Don't make stubs to out-of-line register save/restore
12325
   functions.  Instead, emit copies of the functions.  */
12326
0
      stub_entry->group->needs_save_res = 1;
12327
0
      stub_entry->type.main = ppc_stub_save_res;
12328
0
      stub_entry->type.sub = ppc_stub_toc;
12329
0
      stub_entry->type.r2save = 0;
12330
0
      return true;
12331
0
    }
12332
12333
0
  if (stub_entry->type.main == ppc_stub_plt_branch)
12334
0
    {
12335
      /* Reset the stub type from the plt branch variant in case we now
12336
   can reach with a shorter stub.  */
12337
0
      stub_entry->type.main = ppc_stub_long_branch;
12338
0
    }
12339
12340
0
  if (stub_entry->type.main == ppc_stub_long_branch
12341
0
      && stub_entry->type.sub == ppc_stub_toc)
12342
0
    {
12343
0
      targ = (stub_entry->target_value
12344
0
        + stub_entry->target_section->output_offset
12345
0
        + stub_entry->target_section->output_section->vma);
12346
0
      targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12347
0
      off = (stub_offset
12348
0
       + stub_entry->group->stub_sec->output_offset
12349
0
       + stub_entry->group->stub_sec->output_section->vma);
12350
12351
0
      size = 4;
12352
0
      r2off = 0;
12353
0
      if (stub_entry->type.r2save)
12354
0
  {
12355
0
    r2off = get_r2off (info, stub_entry);
12356
0
    if (r2off == (bfd_vma) -1)
12357
0
      {
12358
0
        htab->stub_error = true;
12359
0
        return false;
12360
0
      }
12361
0
    size = 8;
12362
0
    if (PPC_HA (r2off) != 0)
12363
0
      size += 4;
12364
0
    if (PPC_LO (r2off) != 0)
12365
0
      size += 4;
12366
0
    off += size - 4;
12367
0
  }
12368
0
      off = targ - off;
12369
12370
      /* If the branch offset is too big, use a ppc_stub_plt_branch.
12371
   Do the same for -R objects without function descriptors.  */
12372
0
      if ((stub_entry->type.r2save
12373
0
     && r2off == 0
12374
0
     && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12375
0
    || off + (1 << 25) >= (bfd_vma) (1 << 26))
12376
0
  {
12377
0
    struct ppc_branch_hash_entry *br_entry;
12378
12379
0
    br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12380
0
               stub_entry->root.string + 9,
12381
0
               true, false);
12382
0
    if (br_entry == NULL)
12383
0
      {
12384
0
        _bfd_error_handler (_("can't build branch stub `%s'"),
12385
0
          stub_entry->root.string);
12386
0
        htab->stub_error = true;
12387
0
        return false;
12388
0
      }
12389
12390
0
    if (br_entry->iter != htab->stub_iteration)
12391
0
      {
12392
0
        br_entry->iter = htab->stub_iteration;
12393
0
        br_entry->offset = htab->brlt->size;
12394
0
        htab->brlt->size += 8;
12395
12396
0
        if (htab->relbrlt != NULL && !info->enable_dt_relr)
12397
0
    htab->relbrlt->size += sizeof (Elf64_External_Rela);
12398
0
        else if (info->emitrelocations)
12399
0
    {
12400
0
      htab->brlt->reloc_count += 1;
12401
0
      htab->brlt->flags |= SEC_RELOC;
12402
0
    }
12403
0
      }
12404
12405
0
    targ = (br_entry->offset
12406
0
      + htab->brlt->output_offset
12407
0
      + htab->brlt->output_section->vma);
12408
0
    off = (elf_gp (info->output_bfd)
12409
0
     + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12410
0
    off = targ - off;
12411
12412
0
    if (info->emitrelocations)
12413
0
      {
12414
0
        stub_entry->group->stub_sec->reloc_count
12415
0
    += 1 + (PPC_HA (off) != 0);
12416
0
        stub_entry->group->stub_sec->flags |= SEC_RELOC;
12417
0
      }
12418
12419
0
    stub_entry->type.main = ppc_stub_plt_branch;
12420
0
    if (!stub_entry->type.r2save)
12421
0
      {
12422
0
        size = 12;
12423
0
        if (PPC_HA (off) != 0)
12424
0
    size = 16;
12425
0
      }
12426
0
    else
12427
0
      {
12428
0
        size = 16;
12429
0
        if (PPC_HA (off) != 0)
12430
0
    size += 4;
12431
12432
0
        if (PPC_HA (r2off) != 0)
12433
0
    size += 4;
12434
0
        if (PPC_LO (r2off) != 0)
12435
0
    size += 4;
12436
0
      }
12437
0
    pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12438
0
    stub_offset += pad;
12439
0
  }
12440
0
      else if (info->emitrelocations)
12441
0
  {
12442
0
    stub_entry->group->stub_sec->reloc_count += 1;
12443
0
    stub_entry->group->stub_sec->flags |= SEC_RELOC;
12444
0
  }
12445
0
    }
12446
0
  else if (stub_entry->type.main == ppc_stub_long_branch)
12447
0
    {
12448
0
      off = (stub_offset
12449
0
       + stub_entry->group->stub_sec->output_offset
12450
0
       + stub_entry->group->stub_sec->output_section->vma);
12451
0
      size = 0;
12452
0
      if (stub_entry->type.r2save)
12453
0
  size = 4;
12454
0
      off += size;
12455
0
      targ = (stub_entry->target_value
12456
0
        + stub_entry->target_section->output_offset
12457
0
        + stub_entry->target_section->output_section->vma);
12458
0
      odd = off & 4;
12459
0
      off = targ - off;
12460
12461
0
      if (stub_entry->type.sub == ppc_stub_notoc)
12462
0
  extra = size_power10_offset (off, odd);
12463
0
      else
12464
0
  extra = size_offset (off - 8);
12465
      /* Include branch insn plus those in the offset sequence.  */
12466
0
      size += 4 + extra;
12467
12468
      /* If the branch can't reach, use a plt_branch.
12469
   The branch insn is at the end, or "extra" bytes along.  So
12470
   its offset will be "extra" bytes less that that already
12471
   calculated.  */
12472
0
      if (off - extra + (1 << 25) >= (bfd_vma) (1 << 26))
12473
0
  {
12474
0
    stub_entry->type.main = ppc_stub_plt_branch;
12475
0
    size += 4;
12476
0
    pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12477
0
    if (pad != 0)
12478
0
      {
12479
0
        stub_offset += pad;
12480
0
        off -= pad;
12481
0
        odd ^= pad & 4;
12482
0
        size -= extra;
12483
0
        if (stub_entry->type.sub == ppc_stub_notoc)
12484
0
    extra = size_power10_offset (off, odd);
12485
0
        else
12486
0
    extra = size_offset (off - 8);
12487
0
        size += extra;
12488
0
      }
12489
0
  }
12490
0
      else if (info->emitrelocations)
12491
0
  stub_entry->group->stub_sec->reloc_count +=1;
12492
12493
0
      if (info->emitrelocations)
12494
0
  {
12495
0
    unsigned int num_rel;
12496
0
    if (stub_entry->type.sub == ppc_stub_notoc)
12497
0
      num_rel = num_relocs_for_power10_offset (off, odd);
12498
0
    else
12499
0
      num_rel = num_relocs_for_offset (off - 8);
12500
0
    stub_entry->group->stub_sec->reloc_count += num_rel;
12501
0
    stub_entry->group->stub_sec->flags |= SEC_RELOC;
12502
0
  }
12503
12504
0
      if (stub_entry->type.sub != ppc_stub_notoc)
12505
0
  {
12506
    /* After the bcl, lr has been modified so we need to emit
12507
       .eh_frame info saying the return address is in r12.  */
12508
0
    lr_used = stub_offset + 8;
12509
0
    if (stub_entry->type.r2save)
12510
0
      lr_used += 4;
12511
    /* The eh_frame info will consist of a DW_CFA_advance_loc or
12512
       variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12513
       DW_CFA_restore_extended 65.  */
12514
0
    delta = lr_used - stub_entry->group->lr_restore;
12515
0
    stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12516
0
    stub_entry->group->lr_restore = lr_used + 8;
12517
0
  }
12518
0
    }
12519
0
  else if (stub_entry->type.sub >= ppc_stub_notoc)
12520
0
    {
12521
0
      BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
12522
0
      lr_used = 0;
12523
0
      if (stub_entry->h != NULL
12524
0
    && is_tls_get_addr (&stub_entry->h->elf, htab)
12525
0
    && htab->params->tls_get_addr_opt)
12526
0
  {
12527
0
    lr_used += 7 * 4;
12528
0
    if (!htab->params->no_tls_get_addr_regsave)
12529
0
      lr_used += 11 * 4;
12530
0
    else if (stub_entry->type.r2save)
12531
0
      lr_used += 2 * 4;
12532
0
  }
12533
0
      if (stub_entry->type.r2save)
12534
0
  lr_used += 4;
12535
0
      targ = stub_entry->plt_ent->plt.offset & ~1;
12536
0
      if (targ >= (bfd_vma) -2)
12537
0
  abort ();
12538
12539
0
      plt = htab->elf.splt;
12540
0
      if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12541
0
  {
12542
0
    if (stub_entry->symtype == STT_GNU_IFUNC)
12543
0
      plt = htab->elf.iplt;
12544
0
    else
12545
0
      plt = htab->pltlocal;
12546
0
  }
12547
0
      targ += plt->output_offset + plt->output_section->vma;
12548
0
      off = (stub_offset
12549
0
       + stub_entry->group->stub_sec->output_offset
12550
0
       + stub_entry->group->stub_sec->output_section->vma
12551
0
       + lr_used);
12552
0
      odd = off & 4;
12553
0
      off = targ - off;
12554
12555
0
      size = plt_stub_size (htab, stub_entry, off, odd);
12556
0
      pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12557
0
      if (pad != 0)
12558
0
  {
12559
0
    stub_offset += pad;
12560
0
    off -= pad;
12561
0
    odd ^= pad & 4;
12562
0
    size = plt_stub_size (htab, stub_entry, off, odd);
12563
0
  }
12564
12565
0
      if (info->emitrelocations)
12566
0
  {
12567
0
    unsigned int num_rel;
12568
0
    if (stub_entry->type.sub == ppc_stub_notoc)
12569
0
      num_rel = num_relocs_for_power10_offset (off, odd);
12570
0
    else
12571
0
      num_rel = num_relocs_for_offset (off - 8);
12572
0
    stub_entry->group->stub_sec->reloc_count += num_rel;
12573
0
    stub_entry->group->stub_sec->flags |= SEC_RELOC;
12574
0
  }
12575
12576
0
      if (stub_entry->type.sub != ppc_stub_notoc)
12577
0
  {
12578
    /* After the bcl, lr has been modified so we need to emit
12579
       .eh_frame info saying the return address is in r12.  */
12580
0
    lr_used += stub_offset + 8;
12581
    /* The eh_frame info will consist of a DW_CFA_advance_loc or
12582
       variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12583
       DW_CFA_restore_extended 65.  */
12584
0
    delta = lr_used - stub_entry->group->lr_restore;
12585
0
    stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12586
0
    stub_entry->group->lr_restore = lr_used + 8;
12587
0
  }
12588
0
      if (stub_entry->h != NULL
12589
0
    && is_tls_get_addr (&stub_entry->h->elf, htab)
12590
0
    && htab->params->tls_get_addr_opt)
12591
0
  {
12592
0
    if (!htab->params->no_tls_get_addr_regsave)
12593
0
      {
12594
0
        unsigned int cfa_updt = stub_offset + 18 * 4;
12595
0
        delta = cfa_updt - stub_entry->group->lr_restore;
12596
0
        stub_entry->group->eh_size += eh_advance_size (delta);
12597
0
        stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12598
0
        stub_entry->group->lr_restore = stub_offset + size - 4;
12599
0
      }
12600
0
    else if (stub_entry->type.r2save)
12601
0
      {
12602
0
        lr_used = stub_offset + size - 20;
12603
0
        delta = lr_used - stub_entry->group->lr_restore;
12604
0
        stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12605
0
        stub_entry->group->lr_restore = stub_offset + size - 4;
12606
0
      }
12607
0
  }
12608
0
    }
12609
0
  else if (stub_entry->type.main == ppc_stub_plt_call)
12610
0
    {
12611
0
      targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12612
0
      if (targ >= (bfd_vma) -2)
12613
0
  abort ();
12614
0
      plt = htab->elf.splt;
12615
0
      if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12616
0
  {
12617
0
    if (stub_entry->symtype == STT_GNU_IFUNC)
12618
0
      plt = htab->elf.iplt;
12619
0
    else
12620
0
      plt = htab->pltlocal;
12621
0
  }
12622
0
      targ += plt->output_offset + plt->output_section->vma;
12623
12624
0
      off = (elf_gp (info->output_bfd)
12625
0
       + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12626
0
      off = targ - off;
12627
12628
0
      size = plt_stub_size (htab, stub_entry, off, 0);
12629
0
      pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12630
0
      stub_offset += pad;
12631
12632
0
      if (info->emitrelocations)
12633
0
  {
12634
0
    stub_entry->group->stub_sec->reloc_count
12635
0
      += ((PPC_HA (off) != 0)
12636
0
    + (htab->opd_abi
12637
0
       ? 2 + (htab->params->plt_static_chain
12638
0
        && PPC_HA (off + 16) == PPC_HA (off))
12639
0
       : 1));
12640
0
    stub_entry->group->stub_sec->flags |= SEC_RELOC;
12641
0
  }
12642
12643
0
      if (stub_entry->h != NULL
12644
0
    && is_tls_get_addr (&stub_entry->h->elf, htab)
12645
0
    && htab->params->tls_get_addr_opt
12646
0
    && stub_entry->type.r2save)
12647
0
  {
12648
0
    if (!htab->params->no_tls_get_addr_regsave)
12649
0
      {
12650
        /* Adjustments to r1 need to be described.  */
12651
0
        unsigned int cfa_updt = stub_offset + 18 * 4;
12652
0
        delta = cfa_updt - stub_entry->group->lr_restore;
12653
0
        stub_entry->group->eh_size += eh_advance_size (delta);
12654
0
        stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12655
0
      }
12656
0
    else
12657
0
      {
12658
0
        lr_used = stub_offset + size - 20;
12659
        /* The eh_frame info will consist of a DW_CFA_advance_loc
12660
     or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12661
     DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65.  */
12662
0
        delta = lr_used - stub_entry->group->lr_restore;
12663
0
        stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12664
0
      }
12665
0
    stub_entry->group->lr_restore = stub_offset + size - 4;
12666
0
  }
12667
0
    }
12668
0
  else
12669
0
    {
12670
0
      BFD_FAIL ();
12671
0
      return false;
12672
0
    }
12673
12674
0
  if (stub_entry->stub_offset != stub_offset)
12675
0
    htab->stub_changed = true;
12676
0
  stub_entry->stub_offset = stub_offset;
12677
0
  stub_entry->group->stub_sec->size = stub_offset + size;
12678
0
  return true;
12679
0
}
12680
12681
/* Set up various things so that we can make a list of input sections
12682
   for each output section included in the link.  Returns -1 on error,
12683
   0 when no stubs will be needed, and 1 on success.  */
12684
12685
int
12686
ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12687
0
{
12688
0
  unsigned int id;
12689
0
  size_t amt;
12690
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
12691
12692
0
  if (htab == NULL)
12693
0
    return -1;
12694
12695
  /* The access to _bfd_section_id here is unlocked, so for the time
12696
     being this function cannot be called in multi-threaded mode.  */
12697
0
  BFD_ASSERT (!_bfd_threading_enabled ());
12698
12699
0
  htab->sec_info_arr_size = _bfd_section_id;
12700
0
  amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12701
0
  htab->sec_info = bfd_zalloc (info->output_bfd, amt);
12702
0
  if (htab->sec_info == NULL)
12703
0
    return -1;
12704
12705
  /* Set toc_off for com, und, abs and ind sections.  */
12706
0
  for (id = 0; id < 3; id++)
12707
0
    htab->sec_info[id].toc_off = TOC_BASE_OFF;
12708
12709
0
  return 1;
12710
0
}
12711
12712
/* Set up for first pass at multitoc partitioning.  */
12713
12714
void
12715
ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12716
0
{
12717
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
12718
12719
0
  htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12720
0
  htab->toc_bfd = NULL;
12721
0
  htab->toc_first_sec = NULL;
12722
0
}
12723
12724
/* The linker repeatedly calls this function for each TOC input section
12725
   and linker generated GOT section.  Group input bfds such that the toc
12726
   within a group is less than 64k in size.  */
12727
12728
bool
12729
ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12730
0
{
12731
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
12732
0
  bfd_vma addr, off, limit;
12733
12734
0
  if (htab == NULL)
12735
0
    return false;
12736
12737
0
  if (!htab->second_toc_pass)
12738
0
    {
12739
      /* Keep track of the first .toc or .got section for this input bfd.  */
12740
0
      bool new_bfd = htab->toc_bfd != isec->owner;
12741
12742
0
      if (new_bfd)
12743
0
  {
12744
0
    htab->toc_bfd = isec->owner;
12745
0
    htab->toc_first_sec = isec;
12746
0
  }
12747
12748
0
      addr = isec->output_offset + isec->output_section->vma;
12749
0
      off = addr - htab->toc_curr;
12750
0
      limit = 0x80008000;
12751
0
      if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12752
0
  limit = 0x10000;
12753
0
      if (off + isec->size > limit)
12754
0
  {
12755
0
    addr = (htab->toc_first_sec->output_offset
12756
0
      + htab->toc_first_sec->output_section->vma);
12757
0
    htab->toc_curr = addr;
12758
0
    htab->toc_curr &= -TOC_BASE_ALIGN;
12759
0
  }
12760
12761
      /* toc_curr is the base address of this toc group.  Set elf_gp
12762
   for the input section to be the offset relative to the
12763
   output toc base plus 0x8000.  Making the input elf_gp an
12764
   offset allows us to move the toc as a whole without
12765
   recalculating input elf_gp.  */
12766
0
      off = htab->toc_curr - elf_gp (info->output_bfd);
12767
0
      off += TOC_BASE_OFF;
12768
12769
      /* Die if someone uses a linker script that doesn't keep input
12770
   file .toc and .got together.  */
12771
0
      if (new_bfd
12772
0
    && elf_gp (isec->owner) != 0
12773
0
    && elf_gp (isec->owner) != off)
12774
0
  return false;
12775
12776
0
      elf_gp (isec->owner) = off;
12777
0
      return true;
12778
0
    }
12779
12780
  /* During the second pass toc_first_sec points to the start of
12781
     a toc group, and toc_curr is used to track the old elf_gp.
12782
     We use toc_bfd to ensure we only look at each bfd once.  */
12783
0
  if (htab->toc_bfd == isec->owner)
12784
0
    return true;
12785
0
  htab->toc_bfd = isec->owner;
12786
12787
0
  if (htab->toc_first_sec == NULL
12788
0
      || htab->toc_curr != elf_gp (isec->owner))
12789
0
    {
12790
0
      htab->toc_curr = elf_gp (isec->owner);
12791
0
      htab->toc_first_sec = isec;
12792
0
    }
12793
0
  addr = (htab->toc_first_sec->output_offset
12794
0
    + htab->toc_first_sec->output_section->vma);
12795
0
  off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12796
0
  elf_gp (isec->owner) = off;
12797
12798
0
  return true;
12799
0
}
12800
12801
/* Called via elf_link_hash_traverse to merge GOT entries for global
12802
   symbol H.  */
12803
12804
static bool
12805
merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12806
0
{
12807
0
  if (h->root.type == bfd_link_hash_indirect)
12808
0
    return true;
12809
12810
0
  merge_got_entries (&h->got.glist);
12811
12812
0
  return true;
12813
0
}
12814
12815
/* Called via elf_link_hash_traverse to allocate GOT entries for global
12816
   symbol H.  */
12817
12818
static bool
12819
reallocate_got (struct elf_link_hash_entry *h, void *inf)
12820
0
{
12821
0
  struct got_entry *gent;
12822
12823
0
  if (h->root.type == bfd_link_hash_indirect)
12824
0
    return true;
12825
12826
0
  for (gent = h->got.glist; gent != NULL; gent = gent->next)
12827
0
    if (!gent->is_indirect)
12828
0
      allocate_got (h, (struct bfd_link_info *) inf, gent);
12829
0
  return true;
12830
0
}
12831
12832
/* Called on the first multitoc pass after the last call to
12833
   ppc64_elf_next_toc_section.  This function removes duplicate GOT
12834
   entries.  */
12835
12836
bool
12837
ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12838
0
{
12839
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
12840
0
  struct bfd *ibfd, *ibfd2;
12841
0
  bool done_something;
12842
12843
0
  htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12844
12845
0
  if (!htab->do_multi_toc)
12846
0
    return false;
12847
12848
  /* Merge global sym got entries within a toc group.  */
12849
0
  elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12850
12851
  /* And tlsld_got.  */
12852
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12853
0
    {
12854
0
      struct got_entry *ent, *ent2;
12855
12856
0
      if (!is_ppc64_elf (ibfd))
12857
0
  continue;
12858
12859
0
      ent = ppc64_tlsld_got (ibfd);
12860
0
      if (!ent->is_indirect
12861
0
    && ent->got.offset != (bfd_vma) -1)
12862
0
  {
12863
0
    for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12864
0
      {
12865
0
        if (!is_ppc64_elf (ibfd2))
12866
0
    continue;
12867
12868
0
        ent2 = ppc64_tlsld_got (ibfd2);
12869
0
        if (!ent2->is_indirect
12870
0
      && ent2->got.offset != (bfd_vma) -1
12871
0
      && elf_gp (ibfd2) == elf_gp (ibfd))
12872
0
    {
12873
0
      ent2->is_indirect = true;
12874
0
      ent2->got.ent = ent;
12875
0
    }
12876
0
      }
12877
0
  }
12878
0
    }
12879
12880
  /* Zap sizes of got sections.  */
12881
0
  htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12882
0
  htab->elf.irelplt->size -= htab->got_reli_size;
12883
0
  htab->got_reli_size = 0;
12884
12885
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12886
0
    {
12887
0
      asection *got, *relgot;
12888
12889
0
      if (!is_ppc64_elf (ibfd))
12890
0
  continue;
12891
12892
0
      got = ppc64_elf_tdata (ibfd)->got;
12893
0
      if (got != NULL)
12894
0
  {
12895
0
    got->rawsize = got->size;
12896
0
    got->size = 0;
12897
0
    relgot = ppc64_elf_tdata (ibfd)->relgot;
12898
0
    relgot->rawsize = relgot->size;
12899
0
    relgot->size = 0;
12900
0
  }
12901
0
    }
12902
12903
  /* Now reallocate the got, local syms first.  We don't need to
12904
     allocate section contents again since we never increase size.  */
12905
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12906
0
    {
12907
0
      struct got_entry **lgot_ents;
12908
0
      struct got_entry **end_lgot_ents;
12909
0
      struct plt_entry **local_plt;
12910
0
      struct plt_entry **end_local_plt;
12911
0
      unsigned char *lgot_masks;
12912
0
      bfd_size_type locsymcount;
12913
0
      Elf_Internal_Shdr *symtab_hdr;
12914
0
      asection *s;
12915
0
      Elf_Internal_Sym *local_syms;
12916
0
      Elf_Internal_Sym *isym;
12917
12918
0
      if (!is_ppc64_elf (ibfd))
12919
0
  continue;
12920
12921
0
      lgot_ents = elf_local_got_ents (ibfd);
12922
0
      if (!lgot_ents)
12923
0
  continue;
12924
12925
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
12926
0
      locsymcount = symtab_hdr->sh_info;
12927
0
      end_lgot_ents = lgot_ents + locsymcount;
12928
0
      local_plt = (struct plt_entry **) end_lgot_ents;
12929
0
      end_local_plt = local_plt + locsymcount;
12930
0
      lgot_masks = (unsigned char *) end_local_plt;
12931
0
      local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
12932
0
      if (local_syms == NULL && locsymcount != 0)
12933
0
  {
12934
0
    local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
12935
0
               0, NULL, NULL, NULL);
12936
0
    if (local_syms == NULL)
12937
0
      return false;
12938
0
  }
12939
0
      s = ppc64_elf_tdata (ibfd)->got;
12940
0
      for (isym = local_syms;
12941
0
     lgot_ents < end_lgot_ents;
12942
0
     ++lgot_ents, ++lgot_masks, isym++)
12943
0
  {
12944
0
    struct got_entry *ent;
12945
12946
0
    for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12947
0
      {
12948
0
        unsigned int ent_size = 8;
12949
0
        unsigned int rel_size = sizeof (Elf64_External_Rela);
12950
12951
0
        ent->got.offset = s->size;
12952
0
        if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12953
0
    {
12954
0
      ent_size *= 2;
12955
0
      rel_size *= 2;
12956
0
    }
12957
0
        s->size += ent_size;
12958
0
        if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12959
0
    {
12960
0
      htab->elf.irelplt->size += rel_size;
12961
0
      htab->got_reli_size += rel_size;
12962
0
    }
12963
0
        else if (bfd_link_pic (info)
12964
0
           && (ent->tls_type == 0
12965
0
         ? !info->enable_dt_relr
12966
0
         : !bfd_link_executable (info))
12967
0
           && isym->st_shndx != SHN_ABS)
12968
0
    {
12969
0
      asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12970
0
      srel->size += rel_size;
12971
0
    }
12972
0
      }
12973
0
  }
12974
0
    }
12975
12976
0
  elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12977
12978
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12979
0
    {
12980
0
      struct got_entry *ent;
12981
12982
0
      if (!is_ppc64_elf (ibfd))
12983
0
  continue;
12984
12985
0
      ent = ppc64_tlsld_got (ibfd);
12986
0
      if (!ent->is_indirect
12987
0
    && ent->got.offset != (bfd_vma) -1)
12988
0
  {
12989
0
    asection *s = ppc64_elf_tdata (ibfd)->got;
12990
0
    ent->got.offset = s->size;
12991
0
    s->size += 16;
12992
0
    if (bfd_link_dll (info))
12993
0
      {
12994
0
        asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12995
0
        srel->size += sizeof (Elf64_External_Rela);
12996
0
      }
12997
0
  }
12998
0
    }
12999
13000
0
  done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
13001
0
  if (!done_something)
13002
0
    for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13003
0
      {
13004
0
  asection *got;
13005
13006
0
  if (!is_ppc64_elf (ibfd))
13007
0
    continue;
13008
13009
0
  got = ppc64_elf_tdata (ibfd)->got;
13010
0
  if (got != NULL)
13011
0
    {
13012
0
      done_something = got->rawsize != got->size;
13013
0
      if (done_something)
13014
0
        break;
13015
0
    }
13016
0
      }
13017
13018
0
  if (done_something)
13019
0
    (*htab->params->layout_sections_again) ();
13020
13021
  /* Set up for second pass over toc sections to recalculate elf_gp
13022
     on input sections.  */
13023
0
  htab->toc_bfd = NULL;
13024
0
  htab->toc_first_sec = NULL;
13025
0
  htab->second_toc_pass = true;
13026
0
  return done_something;
13027
0
}
13028
13029
/* Called after second pass of multitoc partitioning.  */
13030
13031
void
13032
ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
13033
0
{
13034
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
13035
13036
  /* After the second pass, toc_curr tracks the TOC offset used
13037
     for code sections below in ppc64_elf_next_input_section.  */
13038
0
  htab->toc_curr = TOC_BASE_OFF;
13039
0
}
13040
13041
/* No toc references were found in ISEC.  If the code in ISEC makes no
13042
   calls, then there's no need to use toc adjusting stubs when branching
13043
   into ISEC.  Actually, indirect calls from ISEC are OK as they will
13044
   load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
13045
   needed, and 2 if a cyclical call-graph was found but no other reason
13046
   for a stub was detected.  If called from the top level, a return of
13047
   2 means the same as a return of 0.  */
13048
13049
static int
13050
toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
13051
0
{
13052
0
  int ret;
13053
13054
  /* Mark this section as checked.  */
13055
0
  isec->call_check_done = 1;
13056
13057
  /* We know none of our code bearing sections will need toc stubs.  */
13058
0
  if ((isec->flags & SEC_LINKER_CREATED) != 0)
13059
0
    return 0;
13060
13061
0
  if (isec->size == 0)
13062
0
    return 0;
13063
13064
0
  if (isec->output_section == NULL)
13065
0
    return 0;
13066
13067
0
  ret = 0;
13068
0
  if (isec->reloc_count != 0)
13069
0
    {
13070
0
      Elf_Internal_Rela *relstart, *rel;
13071
0
      Elf_Internal_Sym *local_syms;
13072
0
      struct ppc_link_hash_table *htab;
13073
13074
0
      relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
13075
0
              info->keep_memory);
13076
0
      if (relstart == NULL)
13077
0
  return -1;
13078
13079
      /* Look for branches to outside of this section.  */
13080
0
      local_syms = NULL;
13081
0
      htab = ppc_hash_table (info);
13082
0
      if (htab == NULL)
13083
0
  return -1;
13084
13085
0
      for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
13086
0
  {
13087
0
    enum elf_ppc64_reloc_type r_type;
13088
0
    unsigned long r_symndx;
13089
0
    struct elf_link_hash_entry *h;
13090
0
    struct ppc_link_hash_entry *eh;
13091
0
    Elf_Internal_Sym *sym;
13092
0
    asection *sym_sec;
13093
0
    struct _opd_sec_data *opd;
13094
0
    bfd_vma sym_value;
13095
0
    bfd_vma dest;
13096
13097
0
    r_type = ELF64_R_TYPE (rel->r_info);
13098
0
    if (r_type != R_PPC64_REL24
13099
0
        && r_type != R_PPC64_REL24_NOTOC
13100
0
        && r_type != R_PPC64_REL24_P9NOTOC
13101
0
        && r_type != R_PPC64_REL14
13102
0
        && r_type != R_PPC64_REL14_BRTAKEN
13103
0
        && r_type != R_PPC64_REL14_BRNTAKEN
13104
0
        && r_type != R_PPC64_PLTCALL
13105
0
        && r_type != R_PPC64_PLTCALL_NOTOC)
13106
0
      continue;
13107
13108
0
    r_symndx = ELF64_R_SYM (rel->r_info);
13109
0
    if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
13110
0
        isec->owner))
13111
0
      {
13112
0
        ret = -1;
13113
0
        break;
13114
0
      }
13115
13116
    /* Calls to dynamic lib functions go through a plt call stub
13117
       that uses r2.  */
13118
0
    eh = ppc_elf_hash_entry (h);
13119
0
    if (eh != NULL
13120
0
        && (eh->elf.plt.plist != NULL
13121
0
      || (eh->oh != NULL
13122
0
          && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
13123
0
      {
13124
0
        ret = 1;
13125
0
        break;
13126
0
      }
13127
13128
0
    if (sym_sec == NULL)
13129
      /* Ignore other undefined symbols.  */
13130
0
      continue;
13131
13132
    /* Assume branches to other sections not included in the
13133
       link need stubs too, to cover -R and absolute syms.  */
13134
0
    if (sym_sec->output_section == NULL)
13135
0
      {
13136
0
        ret = 1;
13137
0
        break;
13138
0
      }
13139
13140
0
    if (h == NULL)
13141
0
      sym_value = sym->st_value;
13142
0
    else
13143
0
      {
13144
0
        if (h->root.type != bfd_link_hash_defined
13145
0
      && h->root.type != bfd_link_hash_defweak)
13146
0
    abort ();
13147
0
        sym_value = h->root.u.def.value;
13148
0
      }
13149
0
    sym_value += rel->r_addend;
13150
13151
    /* If this branch reloc uses an opd sym, find the code section.  */
13152
0
    opd = get_opd_info (sym_sec);
13153
0
    if (opd != NULL)
13154
0
      {
13155
0
        if (h == NULL && opd->adjust != NULL)
13156
0
    {
13157
0
      long adjust;
13158
13159
0
      adjust = opd->adjust[OPD_NDX (sym_value)];
13160
0
      if (adjust == -1)
13161
        /* Assume deleted functions won't ever be called.  */
13162
0
        continue;
13163
0
      sym_value += adjust;
13164
0
    }
13165
13166
0
        dest = opd_entry_value (sym_sec, sym_value,
13167
0
              &sym_sec, NULL, false);
13168
0
        if (dest == (bfd_vma) -1)
13169
0
    continue;
13170
0
      }
13171
0
    else
13172
0
      dest = (sym_value
13173
0
        + sym_sec->output_offset
13174
0
        + sym_sec->output_section->vma);
13175
13176
    /* Ignore branch to self.  */
13177
0
    if (sym_sec == isec)
13178
0
      continue;
13179
13180
    /* If the called function uses the toc, we need a stub.  */
13181
0
    if (sym_sec->has_toc_reloc
13182
0
        || sym_sec->makes_toc_func_call)
13183
0
      {
13184
0
        ret = 1;
13185
0
        break;
13186
0
      }
13187
13188
    /* Assume any branch that needs a long branch stub might in fact
13189
       need a plt_branch stub.  A plt_branch stub uses r2.  */
13190
0
    else if (dest - (isec->output_offset
13191
0
         + isec->output_section->vma
13192
0
         + rel->r_offset) + (1 << 25)
13193
0
       >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13194
0
                   ? h->other
13195
0
                   : sym->st_other))
13196
0
      {
13197
0
        ret = 1;
13198
0
        break;
13199
0
      }
13200
13201
    /* If calling back to a section in the process of being
13202
       tested, we can't say for sure that no toc adjusting stubs
13203
       are needed, so don't return zero.  */
13204
0
    else if (sym_sec->call_check_in_progress)
13205
0
      ret = 2;
13206
13207
    /* Branches to another section that itself doesn't have any TOC
13208
       references are OK.  Recursively call ourselves to check.  */
13209
0
    else if (!sym_sec->call_check_done)
13210
0
      {
13211
0
        int recur;
13212
13213
        /* Mark current section as indeterminate, so that other
13214
     sections that call back to current won't be marked as
13215
     known.  */
13216
0
        isec->call_check_in_progress = 1;
13217
0
        recur = toc_adjusting_stub_needed (info, sym_sec);
13218
0
        isec->call_check_in_progress = 0;
13219
13220
0
        if (recur != 0)
13221
0
    {
13222
0
      ret = recur;
13223
0
      if (recur != 2)
13224
0
        break;
13225
0
    }
13226
0
      }
13227
0
  }
13228
13229
0
      if (elf_symtab_hdr (isec->owner).contents
13230
0
    != (unsigned char *) local_syms)
13231
0
  free (local_syms);
13232
0
      if (elf_section_data (isec)->relocs != relstart)
13233
0
  free (relstart);
13234
0
    }
13235
13236
0
  if ((ret & 1) == 0
13237
0
      && isec->map_head.s != NULL
13238
0
      && (strcmp (isec->output_section->name, ".init") == 0
13239
0
    || strcmp (isec->output_section->name, ".fini") == 0))
13240
0
    {
13241
0
      if (isec->map_head.s->has_toc_reloc
13242
0
    || isec->map_head.s->makes_toc_func_call)
13243
0
  ret = 1;
13244
0
      else if (!isec->map_head.s->call_check_done)
13245
0
  {
13246
0
    int recur;
13247
0
    isec->call_check_in_progress = 1;
13248
0
    recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13249
0
    isec->call_check_in_progress = 0;
13250
0
    if (recur != 0)
13251
0
      ret = recur;
13252
0
  }
13253
0
    }
13254
13255
0
  if (ret == 1)
13256
0
    isec->makes_toc_func_call = 1;
13257
13258
0
  return ret;
13259
0
}
13260
13261
/* The linker repeatedly calls this function for each input section,
13262
   in the order that input sections are linked into output sections.
13263
   Build lists of input sections to determine groupings between which
13264
   we may insert linker stubs.  */
13265
13266
bool
13267
ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
13268
0
{
13269
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
13270
13271
0
  if (htab == NULL)
13272
0
    return false;
13273
13274
0
  if ((isec->output_section->flags & SEC_CODE) != 0
13275
0
      && isec->output_section->id < htab->sec_info_arr_size)
13276
0
    {
13277
      /* This happens to make the list in reverse order,
13278
   which is what we want.  */
13279
0
      htab->sec_info[isec->id].u.list
13280
0
  = htab->sec_info[isec->output_section->id].u.list;
13281
0
      htab->sec_info[isec->output_section->id].u.list = isec;
13282
0
    }
13283
13284
0
  if (htab->multi_toc_needed)
13285
0
    {
13286
      /* Analyse sections that aren't already flagged as needing a
13287
   valid toc pointer.  Exclude .fixup for the linux kernel.
13288
   .fixup contains branches, but only back to the function that
13289
   hit an exception.  */
13290
0
      if (!(isec->has_toc_reloc
13291
0
      || (isec->flags & SEC_CODE) == 0
13292
0
      || strcmp (isec->name, ".fixup") == 0
13293
0
      || isec->call_check_done))
13294
0
  {
13295
0
    if (toc_adjusting_stub_needed (info, isec) < 0)
13296
0
      return false;
13297
0
  }
13298
      /* Make all sections use the TOC assigned for this object file.
13299
   This will be wrong for pasted sections;  We fix that in
13300
   check_pasted_section().  */
13301
0
      if (elf_gp (isec->owner) != 0)
13302
0
  htab->toc_curr = elf_gp (isec->owner);
13303
0
    }
13304
13305
0
  htab->sec_info[isec->id].toc_off = htab->toc_curr;
13306
0
  return true;
13307
0
}
13308
13309
/* Check that all .init and .fini sections use the same toc, if they
13310
   have toc relocs.  */
13311
13312
static bool
13313
check_pasted_section (struct bfd_link_info *info, const char *name)
13314
0
{
13315
0
  asection *o = bfd_get_section_by_name (info->output_bfd, name);
13316
13317
0
  if (o != NULL)
13318
0
    {
13319
0
      struct ppc_link_hash_table *htab = ppc_hash_table (info);
13320
0
      bfd_vma toc_off = 0;
13321
0
      asection *i;
13322
13323
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13324
0
  if (i->has_toc_reloc)
13325
0
    {
13326
0
      if (toc_off == 0)
13327
0
        toc_off = htab->sec_info[i->id].toc_off;
13328
0
      else if (toc_off != htab->sec_info[i->id].toc_off)
13329
0
        return false;
13330
0
    }
13331
13332
0
      if (toc_off == 0)
13333
0
  for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13334
0
    if (i->makes_toc_func_call)
13335
0
      {
13336
0
        toc_off = htab->sec_info[i->id].toc_off;
13337
0
        break;
13338
0
      }
13339
13340
      /* Make sure the whole pasted function uses the same toc offset.  */
13341
0
      if (toc_off != 0)
13342
0
  for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13343
0
    htab->sec_info[i->id].toc_off = toc_off;
13344
0
    }
13345
0
  return true;
13346
0
}
13347
13348
bool
13349
ppc64_elf_check_init_fini (struct bfd_link_info *info)
13350
0
{
13351
0
  bool ret1 = check_pasted_section (info, ".init");
13352
0
  bool ret2 = check_pasted_section (info, ".fini");
13353
13354
0
  return ret1 && ret2;
13355
0
}
13356
13357
/* See whether we can group stub sections together.  Grouping stub
13358
   sections may result in fewer stubs.  More importantly, we need to
13359
   put all .init* and .fini* stubs at the beginning of the .init or
13360
   .fini output sections respectively, because glibc splits the
13361
   _init and _fini functions into multiple parts.  Putting a stub in
13362
   the middle of a function is not a good idea.  */
13363
13364
static bool
13365
group_sections (struct bfd_link_info *info,
13366
    bfd_size_type stub_group_size,
13367
    bool stubs_always_before_branch)
13368
0
{
13369
0
  struct ppc_link_hash_table *htab;
13370
0
  asection *osec;
13371
0
  bool suppress_size_errors;
13372
13373
0
  htab = ppc_hash_table (info);
13374
0
  if (htab == NULL)
13375
0
    return false;
13376
13377
0
  suppress_size_errors = false;
13378
0
  if (stub_group_size == 1)
13379
0
    {
13380
      /* Default values.  */
13381
0
      if (stubs_always_before_branch)
13382
0
  stub_group_size = 0x1e00000;
13383
0
      else
13384
0
  stub_group_size = 0x1c00000;
13385
0
      suppress_size_errors = true;
13386
0
    }
13387
13388
0
  for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
13389
0
    {
13390
0
      asection *tail;
13391
13392
0
      if (osec->id >= htab->sec_info_arr_size)
13393
0
  continue;
13394
13395
0
      tail = htab->sec_info[osec->id].u.list;
13396
0
      while (tail != NULL)
13397
0
  {
13398
0
    asection *curr;
13399
0
    asection *prev;
13400
0
    bfd_size_type total;
13401
0
    bool big_sec;
13402
0
    bfd_vma curr_toc;
13403
0
    struct map_stub *group;
13404
0
    bfd_size_type group_size;
13405
13406
0
    curr = tail;
13407
0
    total = tail->size;
13408
0
    group_size = (ppc64_elf_section_data (tail) != NULL
13409
0
      && ppc64_elf_section_data (tail)->has_14bit_branch
13410
0
      ? stub_group_size >> 10 : stub_group_size);
13411
13412
0
    big_sec = total > group_size;
13413
0
    if (big_sec && !suppress_size_errors)
13414
      /* xgettext:c-format */
13415
0
      _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
13416
0
        tail->owner, tail);
13417
0
    curr_toc = htab->sec_info[tail->id].toc_off;
13418
13419
0
    while ((prev = htab->sec_info[curr->id].u.list) != NULL
13420
0
     && ((total += curr->output_offset - prev->output_offset)
13421
0
         < (ppc64_elf_section_data (prev) != NULL
13422
0
      && ppc64_elf_section_data (prev)->has_14bit_branch
13423
0
      ? (group_size = stub_group_size >> 10) : group_size))
13424
0
     && htab->sec_info[prev->id].toc_off == curr_toc)
13425
0
      curr = prev;
13426
13427
    /* OK, the size from the start of CURR to the end is less
13428
       than group_size and thus can be handled by one stub
13429
       section.  (or the tail section is itself larger than
13430
       group_size, in which case we may be toast.)  We should
13431
       really be keeping track of the total size of stubs added
13432
       here, as stubs contribute to the final output section
13433
       size.  That's a little tricky, and this way will only
13434
       break if stubs added make the total size more than 2^25,
13435
       ie. for the default stub_group_size, if stubs total more
13436
       than 2097152 bytes, or nearly 75000 plt call stubs.  */
13437
0
    group = bfd_alloc (curr->owner, sizeof (*group));
13438
0
    if (group == NULL)
13439
0
      return false;
13440
0
    group->link_sec = curr;
13441
0
    group->stub_sec = NULL;
13442
0
    group->needs_save_res = 0;
13443
0
    group->lr_restore = 0;
13444
0
    group->eh_size = 0;
13445
0
    group->eh_base = 0;
13446
0
    group->next = htab->group;
13447
0
    htab->group = group;
13448
0
    do
13449
0
      {
13450
0
        prev = htab->sec_info[tail->id].u.list;
13451
        /* Set up this stub group.  */
13452
0
        htab->sec_info[tail->id].u.group = group;
13453
0
      }
13454
0
    while (tail != curr && (tail = prev) != NULL);
13455
13456
    /* But wait, there's more!  Input sections up to group_size
13457
       bytes before the stub section can be handled by it too.
13458
       Don't do this if we have a really large section after the
13459
       stubs, as adding more stubs increases the chance that
13460
       branches may not reach into the stub section.  */
13461
0
    if (!stubs_always_before_branch && !big_sec)
13462
0
      {
13463
0
        total = 0;
13464
0
        while (prev != NULL
13465
0
         && ((total += tail->output_offset - prev->output_offset)
13466
0
       < (ppc64_elf_section_data (prev) != NULL
13467
0
          && ppc64_elf_section_data (prev)->has_14bit_branch
13468
0
          ? (group_size = stub_group_size >> 10)
13469
0
          : group_size))
13470
0
         && htab->sec_info[prev->id].toc_off == curr_toc)
13471
0
    {
13472
0
      tail = prev;
13473
0
      prev = htab->sec_info[tail->id].u.list;
13474
0
      htab->sec_info[tail->id].u.group = group;
13475
0
    }
13476
0
      }
13477
0
    tail = prev;
13478
0
  }
13479
0
    }
13480
0
  return true;
13481
0
}
13482
13483
static const unsigned char glink_eh_frame_cie[] =
13484
{
13485
  0, 0, 0, 16,        /* length.  */
13486
  0, 0, 0, 0,       /* id.  */
13487
  1,          /* CIE version.  */
13488
  'z', 'R', 0,        /* Augmentation string.  */
13489
  4,          /* Code alignment.  */
13490
  0x78,         /* Data alignment.  */
13491
  65,         /* RA reg.  */
13492
  1,          /* Augmentation size.  */
13493
  DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding.  */
13494
  DW_CFA_def_cfa, 1, 0      /* def_cfa: r1 offset 0.  */
13495
};
13496
13497
/* Stripping output sections is normally done before dynamic section
13498
   symbols have been allocated.  This function is called later, and
13499
   handles cases like htab->brlt which is mapped to its own output
13500
   section.  */
13501
13502
static void
13503
maybe_strip_output (struct bfd_link_info *info, asection *isec)
13504
0
{
13505
0
  if (isec->size == 0
13506
0
      && isec->output_section->size == 0
13507
0
      && !(isec->output_section->flags & SEC_KEEP)
13508
0
      && !bfd_section_removed_from_list (info->output_bfd,
13509
0
           isec->output_section)
13510
0
      && elf_section_data (isec->output_section)->dynindx == 0)
13511
0
    {
13512
0
      isec->output_section->flags |= SEC_EXCLUDE;
13513
0
      bfd_section_list_remove (info->output_bfd, isec->output_section);
13514
0
      info->output_bfd->section_count--;
13515
0
    }
13516
0
}
13517
13518
/* Stash R_PPC64_RELATIVE reloc at input section SEC, r_offset OFF to
13519
   the array of such relocs.  */
13520
13521
static bool
13522
append_relr_off (struct ppc_link_hash_table *htab, asection *sec, bfd_vma off)
13523
0
{
13524
0
  if (htab->relr_count >= htab->relr_alloc)
13525
0
    {
13526
0
      if (htab->relr_alloc == 0)
13527
0
  htab->relr_alloc = 4096;
13528
0
      else
13529
0
  htab->relr_alloc *= 2;
13530
0
      htab->relr = bfd_realloc (htab->relr,
13531
0
        htab->relr_alloc * sizeof (*htab->relr));
13532
0
      if (htab->relr == NULL)
13533
0
  return false;
13534
0
    }
13535
0
  htab->relr[htab->relr_count].sec = sec;
13536
0
  htab->relr[htab->relr_count].off = off;
13537
0
  htab->relr_count++;
13538
0
  return true;
13539
0
}
13540
13541
/* qsort comparator for bfd_vma args.  */
13542
13543
static int
13544
compare_relr_address (const void *arg1, const void *arg2)
13545
0
{
13546
0
  bfd_vma a = *(bfd_vma *) arg1;
13547
0
  bfd_vma b = *(bfd_vma *) arg2;
13548
0
  return a < b ? -1 : a > b ? 1 : 0;
13549
0
}
13550
13551
/* Produce a malloc'd sorted array of reloc addresses from the info
13552
   stored by append_relr_off.  */
13553
13554
static bfd_vma *
13555
sort_relr (struct ppc_link_hash_table *htab)
13556
0
{
13557
0
  bfd_vma *addr = bfd_malloc (htab->relr_count * sizeof (*addr));
13558
0
  if (addr == NULL)
13559
0
    return NULL;
13560
13561
0
  for (size_t i = 0; i < htab->relr_count; i++)
13562
0
    addr[i] = (htab->relr[i].sec->output_section->vma
13563
0
         + htab->relr[i].sec->output_offset
13564
0
         + htab->relr[i].off);
13565
13566
0
  if (htab->relr_count > 1)
13567
0
    qsort (addr, htab->relr_count, sizeof (*addr), compare_relr_address);
13568
13569
0
  return addr;
13570
0
}
13571
13572
/* Look over GOT and PLT entries saved on elf_local_got_ents for all
13573
   input files, stashing info about needed relative relocs.  */
13574
13575
static bool
13576
got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13577
0
{
13578
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
13579
0
  bfd *ibfd;
13580
13581
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13582
0
    {
13583
0
      struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13584
0
      struct plt_entry **local_plt, **lplt, **end_local_plt;
13585
0
      Elf_Internal_Shdr *symtab_hdr;
13586
0
      bfd_size_type locsymcount;
13587
0
      Elf_Internal_Sym *local_syms;
13588
0
      Elf_Internal_Sym *isym;
13589
0
      struct plt_entry *pent;
13590
0
      struct got_entry *gent;
13591
13592
0
      if (!is_ppc64_elf (ibfd))
13593
0
  continue;
13594
13595
0
      lgot_ents = elf_local_got_ents (ibfd);
13596
0
      if (!lgot_ents)
13597
0
  continue;
13598
13599
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
13600
0
      locsymcount = symtab_hdr->sh_info;
13601
0
      local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
13602
0
      if (local_syms == NULL && locsymcount != 0)
13603
0
  {
13604
0
    local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
13605
0
               0, NULL, NULL, NULL);
13606
0
    if (local_syms == NULL)
13607
0
      return false;
13608
0
  }
13609
0
      end_lgot_ents = lgot_ents + locsymcount;
13610
0
      local_plt = (struct plt_entry **) end_lgot_ents;
13611
0
      end_local_plt = local_plt + locsymcount;
13612
0
      for (lgot = lgot_ents, isym = local_syms;
13613
0
     lgot < end_lgot_ents;
13614
0
     ++lgot, ++isym)
13615
0
  for (gent = *lgot; gent != NULL; gent = gent->next)
13616
0
    if (!gent->is_indirect
13617
0
        && gent->tls_type == 0
13618
0
        && gent->got.offset != (bfd_vma) -1
13619
0
        && isym->st_shndx != SHN_ABS)
13620
0
      {
13621
0
        asection *got = ppc64_elf_tdata (gent->owner)->got;
13622
0
        if (!append_relr_off (htab, got, gent->got.offset))
13623
0
    {
13624
0
      htab->stub_error = true;
13625
0
      return false;
13626
0
    }
13627
0
      }
13628
13629
0
      if (!htab->opd_abi)
13630
0
  for (lplt = local_plt, isym = local_syms;
13631
0
       lplt < end_local_plt;
13632
0
       ++lplt, ++isym)
13633
0
    for (pent = *lplt; pent != NULL; pent = pent->next)
13634
0
      if (pent->plt.offset != (bfd_vma) -1
13635
0
    && ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC)
13636
0
        {
13637
0
    if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13638
0
      {
13639
0
        if (symtab_hdr->contents != (unsigned char *) local_syms)
13640
0
          free (local_syms);
13641
0
        return false;
13642
0
      }
13643
0
        }
13644
13645
0
      if (local_syms != NULL
13646
0
    && symtab_hdr->contents != (unsigned char *) local_syms)
13647
0
  {
13648
0
    if (!info->keep_memory)
13649
0
      free (local_syms);
13650
0
    else
13651
0
      symtab_hdr->contents = (unsigned char *) local_syms;
13652
0
  }
13653
0
    }
13654
0
  return true;
13655
0
}
13656
13657
/* Stash info about needed GOT and PLT entry relative relocs for
13658
   global symbol H.  */
13659
13660
static bool
13661
got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13662
0
{
13663
0
  struct bfd_link_info *info;
13664
0
  struct ppc_link_hash_table *htab;
13665
0
  struct plt_entry *pent;
13666
0
  struct got_entry *gent;
13667
13668
0
  if (h->root.type == bfd_link_hash_indirect)
13669
0
    return true;
13670
13671
0
  info = (struct bfd_link_info *) inf;
13672
0
  htab = ppc_hash_table (info);
13673
0
  if (htab == NULL)
13674
0
    return false;
13675
13676
0
  if (h->type != STT_GNU_IFUNC
13677
0
      && h->def_regular
13678
0
      && (h->root.type == bfd_link_hash_defined
13679
0
    || h->root.type == bfd_link_hash_defweak))
13680
0
    {
13681
0
      if ((!htab->elf.dynamic_sections_created
13682
0
     || h->dynindx == -1
13683
0
     || SYMBOL_REFERENCES_LOCAL (info, h))
13684
0
    && !bfd_is_abs_symbol (&h->root))
13685
0
  for (gent = h->got.glist; gent != NULL; gent = gent->next)
13686
0
    if (!gent->is_indirect
13687
0
        && gent->tls_type == 0
13688
0
        && gent->got.offset != (bfd_vma) -1)
13689
0
      {
13690
0
        asection *got = ppc64_elf_tdata (gent->owner)->got;
13691
0
        if (!append_relr_off (htab, got, gent->got.offset))
13692
0
    {
13693
0
      htab->stub_error = true;
13694
0
      return false;
13695
0
    }
13696
0
      }
13697
13698
0
      if (!htab->opd_abi
13699
0
    && use_local_plt (info, h))
13700
0
  for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13701
0
    if (pent->plt.offset != (bfd_vma) -1)
13702
0
      {
13703
0
        if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13704
0
    {
13705
0
      htab->stub_error = true;
13706
0
      return false;
13707
0
    }
13708
0
      }
13709
0
    }
13710
0
  return true;
13711
0
}
13712
13713
/* Determine and set the size of the stub section for a final link.
13714
13715
   The basic idea here is to examine all the relocations looking for
13716
   PC-relative calls to a target that is unreachable with a "bl"
13717
   instruction.  */
13718
13719
bool
13720
ppc64_elf_size_stubs (struct bfd_link_info *info)
13721
0
{
13722
0
  bfd_size_type stub_group_size;
13723
0
  bool stubs_always_before_branch;
13724
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
13725
13726
0
  if (htab == NULL)
13727
0
    return false;
13728
13729
0
  if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13730
0
    htab->params->power10_stubs = 0;
13731
13732
0
  if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13733
0
    htab->params->plt_thread_safe = 1;
13734
0
  if (!htab->opd_abi)
13735
0
    htab->params->plt_thread_safe = 0;
13736
0
  else if (htab->params->plt_thread_safe == -1)
13737
0
    {
13738
0
      static const char *const thread_starter[] =
13739
0
  {
13740
0
    "pthread_create",
13741
    /* libstdc++ */
13742
0
    "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13743
    /* librt */
13744
0
    "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13745
0
    "mq_notify", "create_timer",
13746
    /* libanl */
13747
0
    "getaddrinfo_a",
13748
    /* libgomp */
13749
0
    "GOMP_parallel",
13750
0
    "GOMP_parallel_start",
13751
0
    "GOMP_parallel_loop_static",
13752
0
    "GOMP_parallel_loop_static_start",
13753
0
    "GOMP_parallel_loop_dynamic",
13754
0
    "GOMP_parallel_loop_dynamic_start",
13755
0
    "GOMP_parallel_loop_guided",
13756
0
    "GOMP_parallel_loop_guided_start",
13757
0
    "GOMP_parallel_loop_runtime",
13758
0
    "GOMP_parallel_loop_runtime_start",
13759
0
    "GOMP_parallel_sections",
13760
0
    "GOMP_parallel_sections_start",
13761
    /* libgo */
13762
0
    "__go_go",
13763
0
  };
13764
0
      unsigned i;
13765
13766
0
      for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13767
0
  {
13768
0
    struct elf_link_hash_entry *h;
13769
0
    h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13770
0
            false, false, true);
13771
0
    htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13772
0
    if (htab->params->plt_thread_safe)
13773
0
      break;
13774
0
  }
13775
0
    }
13776
0
  stubs_always_before_branch = htab->params->group_size < 0;
13777
0
  if (htab->params->group_size < 0)
13778
0
    stub_group_size = -htab->params->group_size;
13779
0
  else
13780
0
    stub_group_size = htab->params->group_size;
13781
13782
0
  if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13783
0
    return false;
13784
13785
0
  htab->tga_group = NULL;
13786
0
  if (!htab->params->no_tls_get_addr_regsave
13787
0
      && htab->tga_desc_fd != NULL
13788
0
      && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13789
0
    || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13790
0
      && htab->tls_get_addr_fd != NULL
13791
0
      && is_static_defined (&htab->tls_get_addr_fd->elf))
13792
0
    {
13793
0
      asection *sym_sec, *code_sec, *stub_sec;
13794
0
      bfd_vma sym_value;
13795
0
      struct _opd_sec_data *opd;
13796
13797
0
      sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13798
0
      sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13799
0
      code_sec = sym_sec;
13800
0
      opd = get_opd_info (sym_sec);
13801
0
      if (opd != NULL)
13802
0
  opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
13803
0
      htab->tga_group = htab->sec_info[code_sec->id].u.group;
13804
0
      stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13805
0
                htab->tga_group->link_sec);
13806
0
      if (stub_sec == NULL)
13807
0
  return false;
13808
0
      htab->tga_group->stub_sec = stub_sec;
13809
13810
0
      htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13811
0
      htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13812
0
      htab->tga_desc_fd->elf.root.u.def.value = 0;
13813
0
      htab->tga_desc_fd->elf.type = STT_FUNC;
13814
0
      htab->tga_desc_fd->elf.def_regular = 1;
13815
0
      htab->tga_desc_fd->elf.non_elf = 0;
13816
0
      _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
13817
0
    }
13818
13819
  /* Loop until no stubs added.  After iteration 20 of this loop we may
13820
     exit on a stub section shrinking.  */
13821
13822
0
  while (1)
13823
0
    {
13824
0
      bfd *input_bfd;
13825
0
      struct map_stub *group;
13826
13827
0
      htab->stub_iteration += 1;
13828
0
      htab->relr_count = 0;
13829
13830
0
      for (input_bfd = info->input_bfds;
13831
0
     input_bfd != NULL;
13832
0
     input_bfd = input_bfd->link.next)
13833
0
  {
13834
0
    Elf_Internal_Shdr *symtab_hdr;
13835
0
    asection *section;
13836
0
    Elf_Internal_Sym *local_syms = NULL;
13837
13838
0
    if (!is_ppc64_elf (input_bfd))
13839
0
      continue;
13840
13841
    /* We'll need the symbol table in a second.  */
13842
0
    symtab_hdr = &elf_symtab_hdr (input_bfd);
13843
0
    if (symtab_hdr->sh_info == 0)
13844
0
      continue;
13845
13846
    /* Walk over each section attached to the input bfd.  */
13847
0
    for (section = input_bfd->sections;
13848
0
         section != NULL;
13849
0
         section = section->next)
13850
0
      {
13851
0
        Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13852
0
        bool is_opd;
13853
13854
        /* If there aren't any relocs, then there's nothing more
13855
     to do.  */
13856
0
        if ((section->flags & SEC_RELOC) == 0
13857
0
      || (section->flags & SEC_ALLOC) == 0
13858
0
      || (section->flags & SEC_LOAD) == 0
13859
0
      || section->reloc_count == 0)
13860
0
    continue;
13861
13862
0
        if (!info->enable_dt_relr
13863
0
      && (section->flags & SEC_CODE) == 0)
13864
0
    continue;
13865
13866
        /* If this section is a link-once section that will be
13867
     discarded, then don't create any stubs.  */
13868
0
        if (section->output_section == NULL
13869
0
      || section->output_section->owner != info->output_bfd)
13870
0
    continue;
13871
13872
        /* Get the relocs.  */
13873
0
        internal_relocs
13874
0
    = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13875
0
               info->keep_memory);
13876
0
        if (internal_relocs == NULL)
13877
0
    goto error_ret_free_local;
13878
13879
0
        is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13880
13881
        /* Now examine each relocation.  */
13882
0
        irela = internal_relocs;
13883
0
        irelaend = irela + section->reloc_count;
13884
0
        for (; irela < irelaend; irela++)
13885
0
    {
13886
0
      enum elf_ppc64_reloc_type r_type;
13887
0
      unsigned int r_indx;
13888
0
      struct ppc_stub_type stub_type;
13889
0
      struct ppc_stub_hash_entry *stub_entry;
13890
0
      asection *sym_sec, *code_sec;
13891
0
      bfd_vma sym_value, code_value;
13892
0
      bfd_vma destination;
13893
0
      unsigned long local_off;
13894
0
      bool ok_dest;
13895
0
      struct ppc_link_hash_entry *hash;
13896
0
      struct ppc_link_hash_entry *fdh;
13897
0
      struct elf_link_hash_entry *h;
13898
0
      Elf_Internal_Sym *sym;
13899
0
      char *stub_name;
13900
0
      const asection *id_sec;
13901
0
      struct _opd_sec_data *opd;
13902
0
      struct plt_entry *plt_ent;
13903
13904
0
      r_type = ELF64_R_TYPE (irela->r_info);
13905
0
      r_indx = ELF64_R_SYM (irela->r_info);
13906
13907
0
      if (r_type >= R_PPC64_max)
13908
0
        {
13909
0
          bfd_set_error (bfd_error_bad_value);
13910
0
          goto error_ret_free_internal;
13911
0
        }
13912
13913
      /* Only look for stubs on branch instructions.  */
13914
0
      switch (r_type)
13915
0
        {
13916
0
        default:
13917
0
          if (info->enable_dt_relr
13918
0
        && maybe_relr (r_type, irela, section))
13919
0
      break;
13920
0
          continue;
13921
13922
0
        case R_PPC64_REL24:
13923
0
        case R_PPC64_REL24_NOTOC:
13924
0
        case R_PPC64_REL24_P9NOTOC:
13925
0
        case R_PPC64_REL14:
13926
0
        case R_PPC64_REL14_BRTAKEN:
13927
0
        case R_PPC64_REL14_BRNTAKEN:
13928
0
          if ((section->flags & SEC_CODE) != 0)
13929
0
      break;
13930
0
          continue;
13931
0
        }
13932
13933
      /* Now determine the call target, its name, value,
13934
         section.  */
13935
0
      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13936
0
          r_indx, input_bfd))
13937
0
        goto error_ret_free_internal;
13938
13939
0
      if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13940
0
        {
13941
          /* Only locally defined symbols can possibly use
13942
       relative relocations.  */
13943
0
          bfd_vma r_offset;
13944
0
          if ((sym_sec == NULL
13945
0
         || sym_sec->output_section == NULL)
13946
        /* No symbol is OK too.  */
13947
0
        && !(sym != NULL && sym->st_shndx == 0)
13948
        /* Hack for __ehdr_start, which is undefined
13949
           at this point.  */
13950
0
        && !(h != NULL && h->root.linker_def))
13951
0
      continue;
13952
0
          if (NO_OPD_RELOCS && is_opd)
13953
0
      continue;
13954
0
          if (!is_opd
13955
0
        && r_type == R_PPC64_ADDR64)
13956
0
      {
13957
0
        if (h != NULL
13958
0
            ? h->type == STT_GNU_IFUNC
13959
0
            : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13960
0
          continue;
13961
0
        if (h != NULL
13962
0
            ? bfd_is_abs_symbol (&h->root)
13963
0
            : sym->st_shndx == SHN_ABS)
13964
0
          continue;
13965
0
        if (h != NULL
13966
0
            && !SYMBOL_REFERENCES_LOCAL (info, h))
13967
0
          continue;
13968
0
      }
13969
0
          r_offset = _bfd_elf_section_offset (info->output_bfd,
13970
0
                info,
13971
0
                section,
13972
0
                irela->r_offset);
13973
0
          if (r_offset >= (bfd_vma) -2)
13974
0
      continue;
13975
0
          if (!append_relr_off (htab, section, r_offset))
13976
0
      goto error_ret_free_internal;
13977
0
          continue;
13978
0
        }
13979
13980
0
      hash = ppc_elf_hash_entry (h);
13981
0
      ok_dest = false;
13982
0
      fdh = NULL;
13983
0
      sym_value = 0;
13984
0
      if (hash == NULL)
13985
0
        {
13986
0
          sym_value = sym->st_value;
13987
0
          if (sym_sec != NULL
13988
0
        && sym_sec->output_section != NULL)
13989
0
      ok_dest = true;
13990
0
        }
13991
0
      else if (hash->elf.root.type == bfd_link_hash_defined
13992
0
         || hash->elf.root.type == bfd_link_hash_defweak)
13993
0
        {
13994
0
          sym_value = hash->elf.root.u.def.value;
13995
0
          if (sym_sec->output_section != NULL)
13996
0
      ok_dest = true;
13997
0
        }
13998
0
      else if (hash->elf.root.type == bfd_link_hash_undefweak
13999
0
         || hash->elf.root.type == bfd_link_hash_undefined)
14000
0
        {
14001
          /* Recognise an old ABI func code entry sym, and
14002
       use the func descriptor sym instead if it is
14003
       defined.  */
14004
0
          if (hash->elf.root.root.string[0] == '.'
14005
0
        && hash->oh != NULL)
14006
0
      {
14007
0
        fdh = ppc_follow_link (hash->oh);
14008
0
        if (fdh->elf.root.type == bfd_link_hash_defined
14009
0
            || fdh->elf.root.type == bfd_link_hash_defweak)
14010
0
          {
14011
0
            sym_sec = fdh->elf.root.u.def.section;
14012
0
            sym_value = fdh->elf.root.u.def.value;
14013
0
            if (sym_sec->output_section != NULL)
14014
0
        ok_dest = true;
14015
0
          }
14016
0
        else
14017
0
          fdh = NULL;
14018
0
      }
14019
0
        }
14020
0
      else
14021
0
        {
14022
0
          bfd_set_error (bfd_error_bad_value);
14023
0
          goto error_ret_free_internal;
14024
0
        }
14025
14026
0
      destination = 0;
14027
0
      local_off = 0;
14028
0
      if (ok_dest)
14029
0
        {
14030
0
          sym_value += irela->r_addend;
14031
0
          destination = (sym_value
14032
0
             + sym_sec->output_offset
14033
0
             + sym_sec->output_section->vma);
14034
0
          local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
14035
0
                  ? hash->elf.other
14036
0
                  : sym->st_other);
14037
0
        }
14038
14039
0
      code_sec = sym_sec;
14040
0
      code_value = sym_value;
14041
0
      opd = get_opd_info (sym_sec);
14042
0
      if (opd != NULL)
14043
0
        {
14044
0
          bfd_vma dest;
14045
14046
0
          if (hash == NULL && opd->adjust != NULL)
14047
0
      {
14048
0
        long adjust = opd->adjust[OPD_NDX (sym_value)];
14049
0
        if (adjust == -1)
14050
0
          continue;
14051
0
        code_value += adjust;
14052
0
        sym_value += adjust;
14053
0
      }
14054
0
          dest = opd_entry_value (sym_sec, sym_value,
14055
0
                &code_sec, &code_value, false);
14056
0
          if (dest != (bfd_vma) -1)
14057
0
      {
14058
0
        destination = dest;
14059
0
        if (fdh != NULL)
14060
0
          {
14061
            /* Fixup old ABI sym to point at code
14062
         entry.  */
14063
0
            hash->elf.root.type = bfd_link_hash_defweak;
14064
0
            hash->elf.root.u.def.section = code_sec;
14065
0
            hash->elf.root.u.def.value = code_value;
14066
0
          }
14067
0
      }
14068
0
        }
14069
14070
      /* Determine what (if any) linker stub is needed.  */
14071
0
      plt_ent = NULL;
14072
0
      stub_type.main = ppc_type_of_stub (section, irela, &hash,
14073
0
                 &plt_ent, destination,
14074
0
                 local_off);
14075
0
      stub_type.sub = ppc_stub_toc;
14076
0
      stub_type.r2save = 0;
14077
14078
0
      if (r_type == R_PPC64_REL24_NOTOC
14079
0
          || r_type == R_PPC64_REL24_P9NOTOC)
14080
0
        {
14081
0
          enum ppc_stub_sub_type notoc = ppc_stub_notoc;
14082
0
          if (htab->params->power10_stubs == 0
14083
0
        || (r_type == R_PPC64_REL24_P9NOTOC
14084
0
            && htab->params->power10_stubs != 1))
14085
0
      notoc = ppc_stub_p9notoc;
14086
0
          if (stub_type.main == ppc_stub_plt_call)
14087
0
      stub_type.sub = notoc;
14088
0
          else if (stub_type.main == ppc_stub_long_branch
14089
0
             || (code_sec != NULL
14090
0
           && code_sec->output_section != NULL
14091
0
           && (((hash ? hash->elf.other : sym->st_other)
14092
0
          & STO_PPC64_LOCAL_MASK)
14093
0
               > 1 << STO_PPC64_LOCAL_BIT)))
14094
0
      {
14095
0
        stub_type.main = ppc_stub_long_branch;
14096
0
        stub_type.sub = notoc;
14097
0
        stub_type.r2save = 0;
14098
0
      }
14099
0
        }
14100
0
      else if (stub_type.main != ppc_stub_plt_call)
14101
0
        {
14102
          /* Check whether we need a TOC adjusting stub.
14103
       Since the linker pastes together pieces from
14104
       different object files when creating the
14105
       _init and _fini functions, it may be that a
14106
       call to what looks like a local sym is in
14107
       fact a call needing a TOC adjustment.  */
14108
0
          if ((code_sec != NULL
14109
0
         && code_sec->output_section != NULL
14110
0
         && (code_sec->has_toc_reloc
14111
0
             || code_sec->makes_toc_func_call)
14112
0
         && (htab->sec_info[code_sec->id].toc_off
14113
0
             != htab->sec_info[section->id].toc_off))
14114
0
        || (((hash ? hash->elf.other : sym->st_other)
14115
0
             & STO_PPC64_LOCAL_MASK)
14116
0
            == 1 << STO_PPC64_LOCAL_BIT))
14117
0
      {
14118
0
        stub_type.main = ppc_stub_long_branch;
14119
0
        stub_type.sub = ppc_stub_toc;
14120
0
        stub_type.r2save = 1;
14121
0
      }
14122
0
        }
14123
14124
0
      if (stub_type.main == ppc_stub_none)
14125
0
        continue;
14126
14127
      /* __tls_get_addr calls might be eliminated.  */
14128
0
      if (stub_type.main != ppc_stub_plt_call
14129
0
          && hash != NULL
14130
0
          && is_tls_get_addr (&hash->elf, htab)
14131
0
          && section->has_tls_reloc
14132
0
          && irela != internal_relocs)
14133
0
        {
14134
          /* Get tls info.  */
14135
0
          unsigned char *tls_mask;
14136
14137
0
          if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
14138
0
           irela - 1, input_bfd))
14139
0
      goto error_ret_free_internal;
14140
0
          if ((*tls_mask & TLS_TLS) != 0
14141
0
        && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
14142
0
      continue;
14143
0
        }
14144
14145
0
      if (stub_type.main == ppc_stub_plt_call
14146
0
          && stub_type.sub == ppc_stub_toc)
14147
0
        {
14148
0
          if (!htab->opd_abi
14149
0
        && htab->params->plt_localentry0 != 0
14150
0
        && is_elfv2_localentry0 (&hash->elf))
14151
0
      htab->has_plt_localentry0 = 1;
14152
0
          else if (irela + 1 < irelaend
14153
0
             && irela[1].r_offset == irela->r_offset + 4
14154
0
             && (ELF64_R_TYPE (irela[1].r_info)
14155
0
           == R_PPC64_TOCSAVE))
14156
0
      {
14157
0
        if (!tocsave_find (htab, INSERT,
14158
0
               &local_syms, irela + 1, input_bfd))
14159
0
          goto error_ret_free_internal;
14160
0
      }
14161
0
          else
14162
0
      stub_type.r2save = 1;
14163
0
        }
14164
14165
      /* Support for grouping stub sections.  */
14166
0
      id_sec = htab->sec_info[section->id].u.group->link_sec;
14167
14168
      /* Get the name of this stub.  */
14169
0
      stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14170
0
      if (!stub_name)
14171
0
        goto error_ret_free_internal;
14172
14173
0
      stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
14174
0
                 stub_name, false, false);
14175
0
      if (stub_entry != NULL)
14176
0
        {
14177
0
          free (stub_name);
14178
0
          if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
14179
0
      {
14180
        /* xgettext:c-format */
14181
0
        _bfd_error_handler
14182
0
          (_("%pB: cannot create stub entry %s"),
14183
0
           section->owner, stub_entry->root.string);
14184
0
        goto error_ret_free_internal;
14185
0
      }
14186
0
          continue;
14187
0
        }
14188
14189
0
      stub_entry = ppc_add_stub (stub_name, section, info);
14190
0
      free (stub_name);
14191
0
      if (stub_entry == NULL)
14192
0
        {
14193
0
        error_ret_free_internal:
14194
0
          if (elf_section_data (section)->relocs == NULL)
14195
0
      free (internal_relocs);
14196
0
        error_ret_free_local:
14197
0
          if (symtab_hdr->contents
14198
0
        != (unsigned char *) local_syms)
14199
0
      free (local_syms);
14200
0
          return false;
14201
0
        }
14202
14203
0
      stub_entry->type = stub_type;
14204
0
      if (stub_type.main == ppc_stub_plt_call)
14205
0
        {
14206
0
          stub_entry->target_value = sym_value;
14207
0
          stub_entry->target_section = sym_sec;
14208
0
        }
14209
0
      else
14210
0
        {
14211
0
          stub_entry->target_value = code_value;
14212
0
          stub_entry->target_section = code_sec;
14213
0
        }
14214
0
      stub_entry->h = hash;
14215
0
      stub_entry->plt_ent = plt_ent;
14216
0
      stub_entry->symtype
14217
0
        = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
14218
0
      stub_entry->other = hash ? hash->elf.other : sym->st_other;
14219
14220
0
      if (hash != NULL
14221
0
          && (hash->elf.root.type == bfd_link_hash_defined
14222
0
        || hash->elf.root.type == bfd_link_hash_defweak))
14223
0
        htab->stub_globals += 1;
14224
0
    }
14225
14226
        /* We're done with the internal relocs, free them.  */
14227
0
        if (elf_section_data (section)->relocs != internal_relocs)
14228
0
    free (internal_relocs);
14229
0
      }
14230
14231
0
    if (local_syms != NULL
14232
0
        && symtab_hdr->contents != (unsigned char *) local_syms)
14233
0
      {
14234
0
        if (!info->keep_memory)
14235
0
    free (local_syms);
14236
0
        else
14237
0
    symtab_hdr->contents = (unsigned char *) local_syms;
14238
0
      }
14239
0
  }
14240
14241
      /* We may have added some stubs.  Find out the new size of the
14242
   stub sections.  */
14243
0
      for (group = htab->group; group != NULL; group = group->next)
14244
0
  {
14245
0
    group->lr_restore = 0;
14246
0
    group->eh_size = 0;
14247
0
    if (group->stub_sec != NULL)
14248
0
      {
14249
0
        asection *stub_sec = group->stub_sec;
14250
14251
0
        stub_sec->rawsize = stub_sec->size;
14252
0
        stub_sec->size = 0;
14253
0
        stub_sec->reloc_count = 0;
14254
0
        stub_sec->flags &= ~SEC_RELOC;
14255
0
      }
14256
0
  }
14257
0
      if (htab->tga_group != NULL)
14258
0
  {
14259
    /* See emit_tga_desc and emit_tga_desc_eh_frame.  */
14260
0
    htab->tga_group->eh_size
14261
0
      = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14262
0
    htab->tga_group->lr_restore = 23 * 4;
14263
0
    htab->tga_group->stub_sec->size = 24 * 4;
14264
0
  }
14265
14266
0
      htab->brlt->rawsize = htab->brlt->size;
14267
0
      htab->brlt->size = 0;
14268
0
      htab->brlt->reloc_count = 0;
14269
0
      htab->brlt->flags &= ~SEC_RELOC;
14270
0
      if (htab->relbrlt != NULL)
14271
0
  htab->relbrlt->size = 0;
14272
14273
0
      if (htab->elf.srelrdyn != NULL)
14274
0
  {
14275
0
    htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
14276
0
    htab->elf.srelrdyn->size = 0;
14277
0
  }
14278
14279
0
      htab->stub_changed = false;
14280
0
      htab->stub_id = 0;
14281
0
      bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
14282
14283
0
      for (group = htab->group; group != NULL; group = group->next)
14284
0
  if (group->needs_save_res)
14285
0
    group->stub_sec->size += htab->sfpr->size;
14286
14287
0
      if (info->emitrelocations
14288
0
    && htab->glink != NULL && htab->glink->size != 0)
14289
0
  {
14290
0
    htab->glink->reloc_count = 1;
14291
0
    htab->glink->flags |= SEC_RELOC;
14292
0
  }
14293
14294
0
      if (htab->glink_eh_frame != NULL
14295
0
    && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
14296
0
    && htab->glink_eh_frame->output_section->size > 8)
14297
0
  {
14298
0
    size_t size = 0, align = 4;
14299
14300
0
    for (group = htab->group; group != NULL; group = group->next)
14301
0
      if (group->eh_size != 0)
14302
0
        size += (group->eh_size + 17 + align - 1) & -align;
14303
0
    if (htab->glink != NULL && htab->glink->size != 0)
14304
0
      size += (24 + align - 1) & -align;
14305
0
    if (size != 0)
14306
0
      size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14307
0
    align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14308
0
    size = (size + align - 1) & -align;
14309
0
    htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14310
0
    htab->glink_eh_frame->size = size;
14311
0
  }
14312
14313
0
      if (htab->params->plt_stub_align != 0)
14314
0
  for (group = htab->group; group != NULL; group = group->next)
14315
0
    if (group->stub_sec != NULL)
14316
0
      {
14317
0
        int align = abs (htab->params->plt_stub_align);
14318
0
        group->stub_sec->size
14319
0
    = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14320
0
      }
14321
14322
0
      if (htab->elf.srelrdyn != NULL)
14323
0
  {
14324
0
    bfd_vma r_offset;
14325
14326
0
    for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
14327
0
      if (!append_relr_off (htab, htab->brlt, r_offset))
14328
0
        return false;
14329
14330
0
    if (!got_and_plt_relr_for_local_syms (info))
14331
0
      return false;
14332
0
    elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14333
0
    if (htab->stub_error)
14334
0
      return false;
14335
14336
0
    bfd_vma *relr_addr = sort_relr (htab);
14337
0
    if (htab->relr_count != 0 && relr_addr == NULL)
14338
0
      return false;
14339
14340
0
    size_t i = 0;
14341
0
    while (i < htab->relr_count)
14342
0
      {
14343
0
        bfd_vma base = relr_addr[i];
14344
0
        htab->elf.srelrdyn->size += 8;
14345
0
        i++;
14346
        /* Handle possible duplicate address.  This can happen
14347
     as sections increase in size when adding stubs.  */
14348
0
        while (i < htab->relr_count
14349
0
         && relr_addr[i] == base)
14350
0
    i++;
14351
0
        base += 8;
14352
0
        while (1)
14353
0
    {
14354
0
      size_t start_i = i;
14355
0
      while (i < htab->relr_count
14356
0
       && relr_addr[i] - base < 63 * 8
14357
0
       && (relr_addr[i] - base) % 8 == 0)
14358
0
        i++;
14359
0
      if (i == start_i)
14360
0
        break;
14361
0
      htab->elf.srelrdyn->size += 8;
14362
0
      base += 63 * 8;
14363
0
    }
14364
0
      }
14365
0
    free (relr_addr);
14366
0
  }
14367
14368
0
      for (group = htab->group; group != NULL; group = group->next)
14369
0
  if (group->stub_sec != NULL
14370
0
      && group->stub_sec->rawsize != group->stub_sec->size
14371
0
      && (htab->stub_iteration <= STUB_SHRINK_ITER
14372
0
    || group->stub_sec->rawsize < group->stub_sec->size))
14373
0
    break;
14374
14375
0
      if (group == NULL
14376
0
    && (!htab->stub_changed
14377
0
        || htab->stub_iteration > STUB_SHRINK_ITER)
14378
0
    && (htab->brlt->rawsize == htab->brlt->size
14379
0
        || (htab->stub_iteration > STUB_SHRINK_ITER
14380
0
      && htab->brlt->rawsize > htab->brlt->size))
14381
0
    && (htab->elf.srelrdyn == NULL
14382
0
        || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14383
0
        || (htab->stub_iteration > STUB_SHRINK_ITER
14384
0
      && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
14385
0
    && (htab->glink_eh_frame == NULL
14386
0
        || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14387
0
    && (htab->tga_group == NULL
14388
0
        || htab->stub_iteration > 1))
14389
0
  break;
14390
14391
0
      if (htab->stub_iteration > STUB_SHRINK_ITER)
14392
0
  {
14393
0
    for (group = htab->group; group != NULL; group = group->next)
14394
0
      if (group->stub_sec != NULL
14395
0
    && group->stub_sec->size < group->stub_sec->rawsize)
14396
0
        group->stub_sec->size = group->stub_sec->rawsize;
14397
14398
0
    if (htab->brlt->size < htab->brlt->rawsize)
14399
0
      htab->brlt->size = htab->brlt->rawsize;
14400
14401
0
    if (htab->elf.srelrdyn != NULL
14402
0
        && htab->elf.srelrdyn->size < htab->elf.srelrdyn->rawsize)
14403
0
      htab->elf.srelrdyn->size = htab->elf.srelrdyn->rawsize;
14404
0
  }
14405
14406
      /* Ask the linker to do its stuff.  */
14407
0
      (*htab->params->layout_sections_again) ();
14408
0
    }
14409
14410
0
  if (htab->glink_eh_frame != NULL
14411
0
      && htab->glink_eh_frame->size != 0)
14412
0
    {
14413
0
      bfd_vma val;
14414
0
      bfd_byte *p, *last_fde;
14415
0
      size_t last_fde_len, size, align, pad;
14416
0
      struct map_stub *group;
14417
14418
      /* It is necessary to at least have a rough outline of the
14419
   linker generated CIEs and FDEs written before
14420
   bfd_elf_discard_info is run, in order for these FDEs to be
14421
   indexed in .eh_frame_hdr.  */
14422
0
      p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14423
0
      if (p == NULL)
14424
0
  return false;
14425
0
      htab->glink_eh_frame->contents = p;
14426
0
      htab->glink_eh_frame->alloced = 1;
14427
0
      last_fde = p;
14428
0
      align = 4;
14429
14430
0
      memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14431
      /* CIE length (rewrite in case little-endian).  */
14432
0
      last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
14433
0
      bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14434
0
      p += last_fde_len + 4;
14435
14436
0
      for (group = htab->group; group != NULL; group = group->next)
14437
0
  if (group->eh_size != 0)
14438
0
    {
14439
0
      group->eh_base = p - htab->glink_eh_frame->contents;
14440
0
      last_fde = p;
14441
0
      last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
14442
      /* FDE length.  */
14443
0
      bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14444
0
      p += 4;
14445
      /* CIE pointer.  */
14446
0
      val = p - htab->glink_eh_frame->contents;
14447
0
      bfd_put_32 (htab->elf.dynobj, val, p);
14448
0
      p += 4;
14449
      /* Offset to stub section, written later.  */
14450
0
      p += 4;
14451
      /* stub section size.  */
14452
0
      bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
14453
0
      p += 4;
14454
      /* Augmentation.  */
14455
0
      p += 1;
14456
      /* Make sure we don't have all nops.  This is enough for
14457
         elf-eh-frame.c to detect the last non-nop opcode.  */
14458
0
      p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
14459
0
      p = last_fde + last_fde_len + 4;
14460
0
    }
14461
0
      if (htab->glink != NULL && htab->glink->size != 0)
14462
0
  {
14463
0
    last_fde = p;
14464
0
    last_fde_len = ((24 + align - 1) & -align) - 4;
14465
    /* FDE length.  */
14466
0
    bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14467
0
    p += 4;
14468
    /* CIE pointer.  */
14469
0
    val = p - htab->glink_eh_frame->contents;
14470
0
    bfd_put_32 (htab->elf.dynobj, val, p);
14471
0
    p += 4;
14472
    /* Offset to .glink, written later.  */
14473
0
    p += 4;
14474
    /* .glink size.  */
14475
0
    bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14476
0
    p += 4;
14477
    /* Augmentation.  */
14478
0
    p += 1;
14479
14480
0
    *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
14481
0
    *p++ = DW_CFA_register;
14482
0
    *p++ = 65;
14483
0
    *p++ = htab->opd_abi ? 12 : 0;
14484
0
    *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
14485
0
    *p++ = DW_CFA_restore_extended;
14486
0
    *p++ = 65;
14487
0
    p += ((24 + align - 1) & -align) - 24;
14488
0
  }
14489
      /* Subsume any padding into the last FDE if user .eh_frame
14490
   sections are aligned more than glink_eh_frame.  Otherwise any
14491
   zero padding will be seen as a terminator.  */
14492
0
      align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14493
0
      size = p - htab->glink_eh_frame->contents;
14494
0
      pad = ((size + align - 1) & -align) - size;
14495
0
      htab->glink_eh_frame->size = size + pad;
14496
0
      bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14497
0
    }
14498
14499
0
  maybe_strip_output (info, htab->brlt);
14500
0
  if (htab->relbrlt != NULL)
14501
0
    maybe_strip_output (info, htab->relbrlt);
14502
0
  if (htab->glink_eh_frame != NULL)
14503
0
    maybe_strip_output (info, htab->glink_eh_frame);
14504
0
  if (htab->elf.srelrdyn != NULL)
14505
0
    maybe_strip_output (info, htab->elf.srelrdyn);
14506
14507
0
  return true;
14508
0
}
14509
14510
/* Called after we have determined section placement.  If sections
14511
   move, we'll be called again.  Provide a value for TOCstart.  */
14512
14513
bfd_vma
14514
ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
14515
24
{
14516
24
  asection *s;
14517
24
  bfd_vma TOCstart, adjust;
14518
14519
24
  if (info != NULL)
14520
0
    {
14521
0
      struct elf_link_hash_entry *h;
14522
0
      struct elf_link_hash_table *htab = elf_hash_table (info);
14523
14524
0
      if (is_elf_hash_table (&htab->root)
14525
0
    && htab->hgot != NULL)
14526
0
  h = htab->hgot;
14527
0
      else
14528
0
  {
14529
0
    h = (struct elf_link_hash_entry *)
14530
0
      bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14531
0
    if (is_elf_hash_table (&htab->root))
14532
0
      htab->hgot = h;
14533
0
  }
14534
0
      if (h != NULL
14535
0
    && h->root.type == bfd_link_hash_defined
14536
0
    && !h->root.linker_def
14537
0
    && (!is_elf_hash_table (&htab->root)
14538
0
        || h->def_regular))
14539
0
  {
14540
0
    TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
14541
0
    _bfd_set_gp_value (obfd, TOCstart);
14542
0
    return TOCstart;
14543
0
  }
14544
0
    }
14545
14546
  /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14547
     order.  The TOC starts where the first of these sections starts.  */
14548
24
  s = bfd_get_section_by_name (obfd, ".got");
14549
24
  if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14550
24
    s = bfd_get_section_by_name (obfd, ".toc");
14551
24
  if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14552
24
    s = bfd_get_section_by_name (obfd, ".tocbss");
14553
24
  if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14554
24
    s = bfd_get_section_by_name (obfd, ".plt");
14555
24
  if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14556
24
    {
14557
      /* This may happen for
14558
   o  references to TOC base (SYM@toc / TOC[tc0]) without a
14559
   .toc directive
14560
   o  bad linker script
14561
   o --gc-sections and empty TOC sections
14562
14563
   FIXME: Warn user?  */
14564
14565
      /* Look for a likely section.  We probably won't even be
14566
   using TOCstart.  */
14567
542
      for (s = obfd->sections; s != NULL; s = s->next)
14568
518
  if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14569
518
       | SEC_EXCLUDE))
14570
518
      == (SEC_ALLOC | SEC_SMALL_DATA))
14571
0
    break;
14572
24
      if (s == NULL)
14573
542
  for (s = obfd->sections; s != NULL; s = s->next)
14574
518
    if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
14575
518
        == (SEC_ALLOC | SEC_SMALL_DATA))
14576
0
      break;
14577
24
      if (s == NULL)
14578
69
  for (s = obfd->sections; s != NULL; s = s->next)
14579
69
    if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14580
69
        == SEC_ALLOC)
14581
24
      break;
14582
24
      if (s == NULL)
14583
0
  for (s = obfd->sections; s != NULL; s = s->next)
14584
0
    if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
14585
0
      break;
14586
24
    }
14587
14588
24
  TOCstart = 0;
14589
24
  if (s != NULL)
14590
24
    TOCstart = s->output_section->vma + s->output_offset;
14591
14592
  /* Force alignment.  */
14593
24
  adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14594
24
  TOCstart -= adjust;
14595
24
  _bfd_set_gp_value (obfd, TOCstart);
14596
14597
24
  if (info != NULL && s != NULL)
14598
0
    {
14599
0
      struct ppc_link_hash_table *htab = ppc_hash_table (info);
14600
14601
0
      if (htab != NULL)
14602
0
  {
14603
0
    if (htab->elf.hgot != NULL)
14604
0
      {
14605
0
        htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
14606
0
        htab->elf.hgot->root.u.def.section = s;
14607
0
      }
14608
0
  }
14609
0
      else
14610
0
  {
14611
0
    struct bfd_link_hash_entry *bh = NULL;
14612
0
    _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
14613
0
              s, TOC_BASE_OFF - adjust,
14614
0
              NULL, false, false, &bh);
14615
0
  }
14616
0
    }
14617
24
  return TOCstart;
14618
24
}
14619
14620
/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
14621
   write out any global entry stubs, and PLT relocations.  */
14622
14623
static bool
14624
build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
14625
0
{
14626
0
  struct bfd_link_info *info;
14627
0
  struct ppc_link_hash_table *htab;
14628
0
  struct plt_entry *ent;
14629
0
  asection *s;
14630
14631
0
  if (h->root.type == bfd_link_hash_indirect)
14632
0
    return true;
14633
14634
0
  info = inf;
14635
0
  htab = ppc_hash_table (info);
14636
0
  if (htab == NULL)
14637
0
    return false;
14638
14639
0
  for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14640
0
    if (ent->plt.offset != (bfd_vma) -1)
14641
0
      {
14642
  /* This symbol has an entry in the procedure linkage
14643
     table.  Set it up.  */
14644
0
  Elf_Internal_Rela rela;
14645
0
  asection *plt, *relplt;
14646
0
  bfd_byte *loc;
14647
14648
0
  if (use_local_plt (info, h))
14649
0
    {
14650
0
      if (!(h->def_regular
14651
0
      && (h->root.type == bfd_link_hash_defined
14652
0
          || h->root.type == bfd_link_hash_defweak)))
14653
0
        continue;
14654
0
      if (h->type == STT_GNU_IFUNC)
14655
0
        {
14656
0
    plt = htab->elf.iplt;
14657
0
    relplt = htab->elf.irelplt;
14658
0
    htab->elf.ifunc_resolvers = true;
14659
0
    if (htab->opd_abi)
14660
0
      rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14661
0
    else
14662
0
      rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14663
0
        }
14664
0
      else
14665
0
        {
14666
0
    plt = htab->pltlocal;
14667
0
    relplt = NULL;
14668
0
    if (bfd_link_pic (info)
14669
0
        && !(info->enable_dt_relr && !htab->opd_abi))
14670
0
      {
14671
0
        relplt = htab->relpltlocal;
14672
0
        if (htab->opd_abi)
14673
0
          rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14674
0
        else
14675
0
          rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14676
0
      }
14677
0
        }
14678
0
      rela.r_addend = defined_sym_val (h) + ent->addend;
14679
14680
0
      if (relplt == NULL)
14681
0
        {
14682
0
    loc = plt->contents + ent->plt.offset;
14683
0
    bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14684
0
    if (htab->opd_abi)
14685
0
      {
14686
0
        bfd_vma toc = elf_gp (info->output_bfd);
14687
0
        toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14688
0
        bfd_put_64 (info->output_bfd, toc, loc + 8);
14689
0
      }
14690
0
        }
14691
0
      else
14692
0
        {
14693
0
    rela.r_offset = (plt->output_section->vma
14694
0
         + plt->output_offset
14695
0
         + ent->plt.offset);
14696
0
    BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd, &rela,
14697
0
                  relplt));
14698
0
        }
14699
0
    }
14700
0
  else
14701
0
    {
14702
0
      rela.r_offset = (htab->elf.splt->output_section->vma
14703
0
           + htab->elf.splt->output_offset
14704
0
           + ent->plt.offset);
14705
0
      rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14706
0
      rela.r_addend = ent->addend;
14707
0
      loc = (htab->elf.srelplt->contents
14708
0
       + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14709
0
          / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14710
0
      if (h->type == STT_GNU_IFUNC && is_static_defined (h))
14711
0
        htab->elf.ifunc_resolvers = true;
14712
0
      BFD_ASSERT (swap_reloc_out (info->output_bfd, &rela,
14713
0
          loc, htab->elf.srelplt));
14714
0
    }
14715
0
      }
14716
14717
0
  if (!h->pointer_equality_needed)
14718
0
    return true;
14719
14720
0
  if (h->def_regular)
14721
0
    return true;
14722
14723
0
  s = htab->global_entry;
14724
0
  if (s == NULL || s->size == 0)
14725
0
    return true;
14726
14727
0
  for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14728
0
    if (ent->plt.offset != (bfd_vma) -1
14729
0
  && ent->addend == 0)
14730
0
      {
14731
0
  bfd_byte *p;
14732
0
  asection *plt;
14733
0
  bfd_vma off;
14734
14735
0
  p = s->contents + h->root.u.def.value;
14736
0
  plt = htab->elf.splt;
14737
0
  if (use_local_plt (info, h))
14738
0
    {
14739
0
      if (h->type == STT_GNU_IFUNC)
14740
0
        plt = htab->elf.iplt;
14741
0
      else
14742
0
        plt = htab->pltlocal;
14743
0
    }
14744
0
  off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
14745
0
  off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14746
14747
0
  if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14748
0
    {
14749
0
      info->callbacks->einfo
14750
0
        (_("%P: linkage table error against `%pT'\n"),
14751
0
         h->root.root.string);
14752
0
      bfd_set_error (bfd_error_bad_value);
14753
0
      htab->stub_error = true;
14754
0
    }
14755
14756
0
  htab->stub_count[ppc_stub_global_entry - 1] += 1;
14757
0
  if (htab->params->emit_stub_syms)
14758
0
    {
14759
0
      size_t len = strlen (h->root.root.string);
14760
0
      char *name = bfd_alloc (info->output_bfd,
14761
0
            sizeof "12345678.global_entry." + len);
14762
14763
0
      if (name == NULL)
14764
0
        return false;
14765
14766
0
      sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14767
0
      h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
14768
0
      if (h == NULL)
14769
0
        return false;
14770
0
      if (h->root.type == bfd_link_hash_new)
14771
0
        {
14772
0
    h->root.type = bfd_link_hash_defined;
14773
0
    h->root.u.def.section = s;
14774
0
    h->root.u.def.value = p - s->contents;
14775
0
    h->ref_regular = 1;
14776
0
    h->def_regular = 1;
14777
0
    h->ref_regular_nonweak = 1;
14778
0
    h->forced_local = 1;
14779
0
    h->non_elf = 0;
14780
0
    h->root.linker_def = 1;
14781
0
        }
14782
0
    }
14783
14784
0
  if (PPC_HA (off) != 0)
14785
0
    {
14786
0
      bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14787
0
      p += 4;
14788
0
    }
14789
0
  bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14790
0
  p += 4;
14791
0
  bfd_put_32 (s->owner, MTCTR_R12, p);
14792
0
  p += 4;
14793
0
  bfd_put_32 (s->owner, BCTR, p);
14794
0
  break;
14795
0
      }
14796
0
  return true;
14797
0
}
14798
14799
/* Write PLT relocs for locals.  */
14800
14801
static bool
14802
write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14803
0
{
14804
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
14805
0
  bfd *ibfd;
14806
14807
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14808
0
    {
14809
0
      struct got_entry **lgot_ents, **end_lgot_ents;
14810
0
      struct plt_entry **local_plt, **lplt, **end_local_plt;
14811
0
      Elf_Internal_Shdr *symtab_hdr;
14812
0
      bfd_size_type locsymcount;
14813
0
      Elf_Internal_Sym *local_syms = NULL;
14814
0
      struct plt_entry *ent;
14815
14816
0
      if (!is_ppc64_elf (ibfd))
14817
0
  continue;
14818
14819
0
      lgot_ents = elf_local_got_ents (ibfd);
14820
0
      if (!lgot_ents)
14821
0
  continue;
14822
14823
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
14824
0
      locsymcount = symtab_hdr->sh_info;
14825
0
      end_lgot_ents = lgot_ents + locsymcount;
14826
0
      local_plt = (struct plt_entry **) end_lgot_ents;
14827
0
      end_local_plt = local_plt + locsymcount;
14828
0
      for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14829
0
  for (ent = *lplt; ent != NULL; ent = ent->next)
14830
0
    if (ent->plt.offset != (bfd_vma) -1)
14831
0
      {
14832
0
        Elf_Internal_Sym *sym;
14833
0
        asection *sym_sec;
14834
0
        asection *plt, *relplt;
14835
0
        bfd_vma val;
14836
14837
0
        if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14838
0
            lplt - local_plt, ibfd))
14839
0
    {
14840
0
      if (symtab_hdr->contents != (unsigned char *) local_syms)
14841
0
        free (local_syms);
14842
0
      return false;
14843
0
    }
14844
14845
0
        val = sym->st_value + ent->addend;
14846
0
        if (sym_sec != NULL && sym_sec->output_section != NULL)
14847
0
    val += sym_sec->output_offset + sym_sec->output_section->vma;
14848
14849
0
        if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14850
0
    {
14851
0
      htab->elf.ifunc_resolvers = true;
14852
0
      plt = htab->elf.iplt;
14853
0
      relplt = htab->elf.irelplt;
14854
0
    }
14855
0
        else
14856
0
    {
14857
0
      plt = htab->pltlocal;
14858
0
      relplt = NULL;
14859
0
      if (bfd_link_pic (info)
14860
0
          && !(info->enable_dt_relr && !htab->opd_abi))
14861
0
        relplt = htab->relpltlocal;
14862
0
    }
14863
14864
0
        if (relplt == NULL)
14865
0
    {
14866
0
      bfd_byte *loc = plt->contents + ent->plt.offset;
14867
0
      bfd_put_64 (info->output_bfd, val, loc);
14868
0
      if (htab->opd_abi)
14869
0
        {
14870
0
          bfd_vma toc = elf_gp (ibfd);
14871
0
          bfd_put_64 (info->output_bfd, toc, loc + 8);
14872
0
        }
14873
0
    }
14874
0
        else
14875
0
    {
14876
0
      Elf_Internal_Rela rela;
14877
0
      rela.r_offset = (ent->plt.offset
14878
0
           + plt->output_offset
14879
0
           + plt->output_section->vma);
14880
0
      if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14881
0
        {
14882
0
          if (htab->opd_abi)
14883
0
      rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14884
0
          else
14885
0
      rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14886
0
        }
14887
0
      else
14888
0
        {
14889
0
          if (htab->opd_abi)
14890
0
      rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14891
0
          else
14892
0
      rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14893
0
        }
14894
0
      rela.r_addend = val;
14895
0
      BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd,
14896
0
               &rela, relplt));
14897
0
    }
14898
0
      }
14899
14900
0
      if (local_syms != NULL
14901
0
    && symtab_hdr->contents != (unsigned char *) local_syms)
14902
0
  {
14903
0
    if (!info->keep_memory)
14904
0
      free (local_syms);
14905
0
    else
14906
0
      symtab_hdr->contents = (unsigned char *) local_syms;
14907
0
  }
14908
0
    }
14909
0
  return true;
14910
0
}
14911
14912
/* Emit the static wrapper function preserving registers around a
14913
   __tls_get_addr_opt call.  */
14914
14915
static bool
14916
emit_tga_desc (struct ppc_link_hash_table *htab)
14917
0
{
14918
0
  asection *stub_sec = htab->tga_group->stub_sec;
14919
0
  unsigned int cfa_updt = 11 * 4;
14920
0
  bfd_byte *p;
14921
0
  bfd_vma to, from, delta;
14922
14923
0
  BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14924
0
        && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14925
0
        && htab->tga_desc_fd->elf.root.u.def.value == 0);
14926
0
  to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14927
0
  from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14928
0
  delta = to - from;
14929
0
  if (delta + (1 << 25) >= 1 << 26)
14930
0
    {
14931
0
      _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14932
0
      htab->stub_error = true;
14933
0
      return false;
14934
0
    }
14935
14936
0
  p = stub_sec->contents;
14937
0
  p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14938
0
  bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14939
0
  p += 4;
14940
0
  p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14941
0
  return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14942
0
}
14943
14944
/* Emit eh_frame describing the static wrapper function.  */
14945
14946
static bfd_byte *
14947
emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14948
0
{
14949
0
  unsigned int cfa_updt = 11 * 4;
14950
0
  unsigned int i;
14951
14952
0
  *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14953
0
  *p++ = DW_CFA_def_cfa_offset;
14954
0
  if (htab->opd_abi)
14955
0
    {
14956
0
      *p++ = 128;
14957
0
      *p++ = 1;
14958
0
    }
14959
0
  else
14960
0
    *p++ = 96;
14961
0
  *p++ = DW_CFA_offset_extended_sf;
14962
0
  *p++ = 65;
14963
0
  *p++ = (-16 / 8) & 0x7f;
14964
0
  for (i = 4; i < 12; i++)
14965
0
    {
14966
0
      *p++ = DW_CFA_offset + i;
14967
0
      *p++ = (htab->opd_abi ? 13 : 12) - i;
14968
0
    }
14969
0
  *p++ = DW_CFA_advance_loc + 10;
14970
0
  *p++ = DW_CFA_def_cfa_offset;
14971
0
  *p++ = 0;
14972
0
  for (i = 4; i < 12; i++)
14973
0
    *p++ = DW_CFA_restore + i;
14974
0
  *p++ = DW_CFA_advance_loc + 2;
14975
0
  *p++ = DW_CFA_restore_extended;
14976
0
  *p++ = 65;
14977
0
  return p;
14978
0
}
14979
14980
/* Build all the stubs associated with the current output file.
14981
   The stubs are kept in a hash table attached to the main linker
14982
   hash table.  This function is called via gldelf64ppc_finish.  */
14983
14984
bool
14985
ppc64_elf_build_stubs (struct bfd_link_info *info,
14986
           char **stats)
14987
0
{
14988
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
14989
0
  struct map_stub *group;
14990
0
  asection *stub_sec;
14991
0
  bfd_byte *p;
14992
0
  int stub_sec_count = 0;
14993
14994
0
  if (htab == NULL)
14995
0
    return false;
14996
14997
  /* Allocate memory to hold the linker stubs.  */
14998
0
  for (group = htab->group; group != NULL; group = group->next)
14999
0
    {
15000
0
      group->eh_size = 0;
15001
0
      group->lr_restore = 0;
15002
0
      if ((stub_sec = group->stub_sec) != NULL
15003
0
    && stub_sec->size != 0)
15004
0
  {
15005
0
    stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
15006
0
             stub_sec->size);
15007
0
    if (stub_sec->contents == NULL)
15008
0
      return false;
15009
0
    stub_sec->alloced = 1;
15010
0
    stub_sec->size = 0;
15011
0
  }
15012
0
    }
15013
15014
0
  if (htab->glink != NULL && htab->glink->size != 0)
15015
0
    {
15016
0
      unsigned int indx;
15017
0
      bfd_vma plt0;
15018
15019
      /* Build the .glink plt call stub.  */
15020
0
      if (htab->params->emit_stub_syms)
15021
0
  {
15022
0
    struct elf_link_hash_entry *h;
15023
0
    h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
15024
0
            true, false, false);
15025
0
    if (h == NULL)
15026
0
      return false;
15027
0
    if (h->root.type == bfd_link_hash_new)
15028
0
      {
15029
0
        h->root.type = bfd_link_hash_defined;
15030
0
        h->root.u.def.section = htab->glink;
15031
0
        h->root.u.def.value = 8;
15032
0
        h->ref_regular = 1;
15033
0
        h->def_regular = 1;
15034
0
        h->ref_regular_nonweak = 1;
15035
0
        h->forced_local = 1;
15036
0
        h->non_elf = 0;
15037
0
        h->root.linker_def = 1;
15038
0
      }
15039
0
  }
15040
0
      plt0 = (htab->elf.splt->output_section->vma
15041
0
        + htab->elf.splt->output_offset
15042
0
        - 16);
15043
0
      if (info->emitrelocations)
15044
0
  {
15045
0
    Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
15046
0
    if (r == NULL)
15047
0
      return false;
15048
0
    r->r_offset = (htab->glink->output_offset
15049
0
       + htab->glink->output_section->vma);
15050
0
    r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
15051
0
    r->r_addend = plt0;
15052
0
  }
15053
0
      p = htab->glink->contents;
15054
0
      plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
15055
0
      bfd_put_64 (htab->glink->owner, plt0, p);
15056
0
      p += 8;
15057
0
      if (htab->opd_abi)
15058
0
  {
15059
0
    bfd_put_32 (htab->glink->owner, MFLR_R12, p);
15060
0
    p += 4;
15061
0
    bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15062
0
    p += 4;
15063
0
    bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15064
0
    p += 4;
15065
0
    bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
15066
0
    p += 4;
15067
0
    bfd_put_32 (htab->glink->owner, MTLR_R12, p);
15068
0
    p += 4;
15069
0
    bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
15070
0
    p += 4;
15071
0
    bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15072
0
    p += 4;
15073
0
    bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
15074
0
    p += 4;
15075
0
    bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15076
0
    p += 4;
15077
0
    bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
15078
0
    p += 4;
15079
0
  }
15080
0
      else
15081
0
  {
15082
0
    unsigned int insn;
15083
15084
    /* 0:
15085
       .  .quad plt0-1f   # plt0 entry relative to 1:
15086
       #
15087
       # We get here with r12 initially @ a glink branch
15088
       # Load the address of _dl_runtime_resolve from plt0 and
15089
       # jump to it, with r0 set to the index of the PLT entry
15090
       # to be resolved and r11 the link map.
15091
       __glink_PLTresolve:
15092
       .  std %r2,24(%r1)   # optional
15093
       .  mflr %r0
15094
       .  bcl 20,31,1f
15095
       1:
15096
       .  mflr %r11
15097
       .  mtlr %r0
15098
       .  ld %r0,(0b-1b)(%r11)
15099
       .  sub %r12,%r12,%r11
15100
       .  add %r11,%r0,%r11
15101
       .  addi %r0,%r12,1b-2f
15102
       .  ld %r12,0(%r11)
15103
       .  srdi %r0,%r0,2
15104
       .  mtctr %r12
15105
       .  ld %r11,8(%r11)
15106
       .  bctr
15107
       2:
15108
       .  b __glink_PLTresolve
15109
       .  ...
15110
       .  b __glink_PLTresolve  */
15111
15112
0
    if (htab->has_plt_localentry0)
15113
0
      {
15114
0
        bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
15115
0
        p += 4;
15116
0
      }
15117
0
    bfd_put_32 (htab->glink->owner, MFLR_R0, p);
15118
0
    p += 4;
15119
0
    bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15120
0
    p += 4;
15121
0
    bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15122
0
    p += 4;
15123
0
    bfd_put_32 (htab->glink->owner, MTLR_R0, p);
15124
0
    p += 4;
15125
0
    if (htab->has_plt_localentry0)
15126
0
      insn = LD_R0_0R11 | (-20 & 0xfffc);
15127
0
    else
15128
0
      insn = LD_R0_0R11 | (-16 & 0xfffc);
15129
0
    bfd_put_32 (htab->glink->owner, insn, p);
15130
0
    p += 4;
15131
0
    bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
15132
0
    p += 4;
15133
0
    bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
15134
0
    p += 4;
15135
0
    bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
15136
0
    p += 4;
15137
0
    bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15138
0
    p += 4;
15139
0
    bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
15140
0
    p += 4;
15141
0
    bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15142
0
    p += 4;
15143
0
    bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
15144
0
    p += 4;
15145
0
  }
15146
0
      bfd_put_32 (htab->glink->owner, BCTR, p);
15147
0
      p += 4;
15148
0
      BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
15149
15150
      /* Build the .glink lazy link call stubs.  */
15151
0
      indx = 0;
15152
0
      while (p < htab->glink->contents + htab->glink->size)
15153
0
  {
15154
0
    if (htab->opd_abi)
15155
0
      {
15156
0
        if (indx < 0x8000)
15157
0
    {
15158
0
      bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
15159
0
      p += 4;
15160
0
    }
15161
0
        else
15162
0
    {
15163
0
      bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
15164
0
      p += 4;
15165
0
      bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
15166
0
            p);
15167
0
      p += 4;
15168
0
    }
15169
0
      }
15170
0
    bfd_put_32 (htab->glink->owner,
15171
0
          B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
15172
0
    indx++;
15173
0
    p += 4;
15174
0
  }
15175
0
    }
15176
15177
0
  if (htab->tga_group != NULL)
15178
0
    {
15179
0
      htab->tga_group->lr_restore = 23 * 4;
15180
0
      htab->tga_group->stub_sec->size = 24 * 4;
15181
0
      if (!emit_tga_desc (htab))
15182
0
  return false;
15183
0
      if (htab->glink_eh_frame != NULL
15184
0
    && htab->glink_eh_frame->size != 0)
15185
0
  {
15186
0
    size_t align = 4;
15187
15188
0
    p = htab->glink_eh_frame->contents;
15189
0
    p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15190
0
    p += 17;
15191
0
    htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15192
0
  }
15193
0
    }
15194
15195
  /* Build .glink global entry stubs, and PLT relocs for globals.  */
15196
0
  elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15197
15198
0
  if (!write_plt_relocs_for_local_syms (info))
15199
0
    return false;
15200
15201
0
  if (htab->brlt != NULL && htab->brlt->size != 0)
15202
0
    {
15203
0
      htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
15204
0
           htab->brlt->size);
15205
0
      if (htab->brlt->contents == NULL)
15206
0
  return false;
15207
0
      htab->brlt->alloced = 1;
15208
0
    }
15209
0
  if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
15210
0
    {
15211
0
      htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
15212
0
              htab->relbrlt->size);
15213
0
      if (htab->relbrlt->contents == NULL)
15214
0
  return false;
15215
0
      htab->relbrlt->alloced = 1;
15216
0
    }
15217
15218
  /* Build the stubs as directed by the stub hash table.  */
15219
0
  htab->stub_id = 0;
15220
0
  bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
15221
15222
0
  for (group = htab->group; group != NULL; group = group->next)
15223
0
    if (group->needs_save_res)
15224
0
      group->stub_sec->size += htab->sfpr->size;
15225
15226
0
  if (htab->relbrlt != NULL)
15227
0
    htab->relbrlt->reloc_count = 0;
15228
15229
0
  if (htab->params->plt_stub_align != 0)
15230
0
    for (group = htab->group; group != NULL; group = group->next)
15231
0
      if ((stub_sec = group->stub_sec) != NULL)
15232
0
  {
15233
0
    int align = abs (htab->params->plt_stub_align);
15234
0
    stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15235
0
  }
15236
15237
0
  for (group = htab->group; group != NULL; group = group->next)
15238
0
    if (group->needs_save_res)
15239
0
      {
15240
0
  stub_sec = group->stub_sec;
15241
0
  memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15242
0
    htab->sfpr->contents, htab->sfpr->size);
15243
0
  if (htab->params->emit_stub_syms)
15244
0
    {
15245
0
      unsigned int i;
15246
15247
0
      for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15248
0
        if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
15249
0
    return false;
15250
0
    }
15251
0
      }
15252
15253
0
  if (htab->glink_eh_frame != NULL
15254
0
      && htab->glink_eh_frame->size != 0)
15255
0
    {
15256
0
      bfd_vma val;
15257
0
      size_t align = 4;
15258
15259
0
      p = htab->glink_eh_frame->contents;
15260
0
      p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15261
15262
0
      for (group = htab->group; group != NULL; group = group->next)
15263
0
  if (group->eh_size != 0)
15264
0
    {
15265
      /* Offset to stub section.  */
15266
0
      val = (group->stub_sec->output_section->vma
15267
0
       + group->stub_sec->output_offset);
15268
0
      val -= (htab->glink_eh_frame->output_section->vma
15269
0
        + htab->glink_eh_frame->output_offset
15270
0
        + (p + 8 - htab->glink_eh_frame->contents));
15271
0
      if (val + 0x80000000 > 0xffffffff)
15272
0
        {
15273
0
    _bfd_error_handler
15274
0
      (_("%s offset too large for .eh_frame sdata4 encoding"),
15275
0
       group->stub_sec->name);
15276
0
    return false;
15277
0
        }
15278
0
      bfd_put_32 (htab->elf.dynobj, val, p + 8);
15279
0
      p += (group->eh_size + 17 + 3) & -4;
15280
0
    }
15281
0
      if (htab->glink != NULL && htab->glink->size != 0)
15282
0
  {
15283
    /* Offset to .glink.  */
15284
0
    val = (htab->glink->output_section->vma
15285
0
     + htab->glink->output_offset
15286
0
     + 8);
15287
0
    val -= (htab->glink_eh_frame->output_section->vma
15288
0
      + htab->glink_eh_frame->output_offset
15289
0
      + (p + 8 - htab->glink_eh_frame->contents));
15290
0
    if (val + 0x80000000 > 0xffffffff)
15291
0
      {
15292
0
        _bfd_error_handler
15293
0
    (_("%s offset too large for .eh_frame sdata4 encoding"),
15294
0
     htab->glink->name);
15295
0
        return false;
15296
0
      }
15297
0
    bfd_put_32 (htab->elf.dynobj, val, p + 8);
15298
0
    p += (24 + align - 1) & -align;
15299
0
  }
15300
0
    }
15301
15302
0
  if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15303
0
    {
15304
0
      htab->elf.srelrdyn->contents
15305
0
  = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15306
0
      if (htab->elf.srelrdyn->contents == NULL)
15307
0
  return false;
15308
0
      htab->elf.srelrdyn->alloced = 1;
15309
15310
0
      bfd_vma *relr_addr = sort_relr (htab);
15311
0
      if (htab->relr_count != 0 && relr_addr == NULL)
15312
0
  return false;
15313
15314
0
      size_t i = 0;
15315
0
      bfd_byte *loc = htab->elf.srelrdyn->contents;
15316
0
      while (i < htab->relr_count)
15317
0
  {
15318
0
    bfd_vma base = relr_addr[i];
15319
0
    BFD_ASSERT ((base & ((1 << RELR_ALIGN) - 1)) == 0);
15320
0
    bfd_put_64 (htab->elf.dynobj, base, loc);
15321
0
    loc += 8;
15322
0
    i++;
15323
0
    while (i < htab->relr_count
15324
0
     && relr_addr[i] == base)
15325
0
      {
15326
0
        htab->stub_error = true;
15327
0
        i++;
15328
0
      }
15329
0
    base += 8;
15330
0
    while (1)
15331
0
      {
15332
0
        bfd_vma bits = 0;
15333
0
        while (i < htab->relr_count
15334
0
         && relr_addr[i] - base < 63 * 8
15335
0
         && (relr_addr[i] - base) % 8 == 0)
15336
0
    {
15337
0
      bits |= (bfd_vma) 1 << ((relr_addr[i] - base) / 8);
15338
0
      i++;
15339
0
    }
15340
0
        if (bits == 0)
15341
0
    break;
15342
0
        bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15343
0
        loc += 8;
15344
0
        base += 63 * 8;
15345
0
      }
15346
0
  }
15347
0
      free (relr_addr);
15348
      /* Pad any excess with 1's, a do-nothing encoding.  */
15349
0
      while ((size_t) (loc - htab->elf.srelrdyn->contents)
15350
0
       < htab->elf.srelrdyn->size)
15351
0
  {
15352
0
    bfd_put_64 (htab->elf.dynobj, 1, loc);
15353
0
    loc += 8;
15354
0
  }
15355
0
    }
15356
0
  free (htab->relr);
15357
0
  htab->relr = NULL;
15358
15359
0
  for (group = htab->group; group != NULL; group = group->next)
15360
0
    if ((stub_sec = group->stub_sec) != NULL)
15361
0
      {
15362
0
  stub_sec_count += 1;
15363
0
  if (stub_sec->rawsize != stub_sec->size
15364
0
      && (htab->stub_iteration <= STUB_SHRINK_ITER
15365
0
    || stub_sec->rawsize < stub_sec->size))
15366
0
    break;
15367
0
      }
15368
15369
0
  if (group != NULL)
15370
0
    htab->stub_error = true;
15371
15372
0
  if (htab->stub_error)
15373
0
    {
15374
0
      _bfd_error_handler (_("stubs don't match calculated size"));
15375
0
      return false;
15376
0
    }
15377
15378
0
  if (stats != NULL)
15379
0
    {
15380
0
      char *groupmsg;
15381
0
      if (asprintf (&groupmsg,
15382
0
        ngettext ("linker stubs in %u group",
15383
0
            "linker stubs in %u groups",
15384
0
            stub_sec_count),
15385
0
        stub_sec_count) < 0)
15386
0
  *stats = NULL;
15387
0
      else
15388
0
  {
15389
0
    if (asprintf (stats, _("%s, iter %u\n"
15390
0
         "  branch         %lu\n"
15391
0
         "  long branch    %lu\n"
15392
0
         "  plt call       %lu\n"
15393
0
         "  global entry   %lu"),
15394
0
      groupmsg, htab->stub_iteration,
15395
0
      htab->stub_count[ppc_stub_long_branch - 1],
15396
0
      htab->stub_count[ppc_stub_plt_branch - 1],
15397
0
      htab->stub_count[ppc_stub_plt_call - 1],
15398
0
      htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15399
0
      *stats = NULL;
15400
0
    free (groupmsg);
15401
0
  }
15402
0
    }
15403
0
  return true;
15404
0
}
15405
15406
/* What to do when ld finds relocations against symbols defined in
15407
   discarded sections.  */
15408
15409
static unsigned int
15410
ppc64_elf_action_discarded (asection *sec)
15411
0
{
15412
0
  if (strcmp (".opd", sec->name) == 0)
15413
0
    return 0;
15414
15415
0
  if (strcmp (".toc", sec->name) == 0)
15416
0
    return 0;
15417
15418
0
  if (strcmp (".toc1", sec->name) == 0)
15419
0
    return 0;
15420
15421
0
  return _bfd_elf_default_action_discarded (sec);
15422
0
}
15423
15424
/* These are the dynamic relocations supported by glibc.  */
15425
15426
static bool
15427
ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15428
0
{
15429
0
  switch (r_type)
15430
0
    {
15431
0
    case R_PPC64_RELATIVE:
15432
0
    case R_PPC64_NONE:
15433
0
    case R_PPC64_ADDR64:
15434
0
    case R_PPC64_GLOB_DAT:
15435
0
    case R_PPC64_IRELATIVE:
15436
0
    case R_PPC64_JMP_IREL:
15437
0
    case R_PPC64_JMP_SLOT:
15438
0
    case R_PPC64_DTPMOD64:
15439
0
    case R_PPC64_DTPREL64:
15440
0
    case R_PPC64_TPREL64:
15441
0
    case R_PPC64_TPREL16_LO_DS:
15442
0
    case R_PPC64_TPREL16_DS:
15443
0
    case R_PPC64_TPREL16:
15444
0
    case R_PPC64_TPREL16_LO:
15445
0
    case R_PPC64_TPREL16_HI:
15446
0
    case R_PPC64_TPREL16_HIGH:
15447
0
    case R_PPC64_TPREL16_HA:
15448
0
    case R_PPC64_TPREL16_HIGHA:
15449
0
    case R_PPC64_TPREL16_HIGHER:
15450
0
    case R_PPC64_TPREL16_HIGHEST:
15451
0
    case R_PPC64_TPREL16_HIGHERA:
15452
0
    case R_PPC64_TPREL16_HIGHESTA:
15453
0
    case R_PPC64_ADDR16_LO_DS:
15454
0
    case R_PPC64_ADDR16_LO:
15455
0
    case R_PPC64_ADDR16_HI:
15456
0
    case R_PPC64_ADDR16_HIGH:
15457
0
    case R_PPC64_ADDR16_HA:
15458
0
    case R_PPC64_ADDR16_HIGHA:
15459
0
    case R_PPC64_REL30:
15460
0
    case R_PPC64_COPY:
15461
0
    case R_PPC64_UADDR64:
15462
0
    case R_PPC64_UADDR32:
15463
0
    case R_PPC64_ADDR32:
15464
0
    case R_PPC64_ADDR24:
15465
0
    case R_PPC64_ADDR16:
15466
0
    case R_PPC64_UADDR16:
15467
0
    case R_PPC64_ADDR16_DS:
15468
0
    case R_PPC64_ADDR16_HIGHER:
15469
0
    case R_PPC64_ADDR16_HIGHEST:
15470
0
    case R_PPC64_ADDR16_HIGHERA:
15471
0
    case R_PPC64_ADDR16_HIGHESTA:
15472
0
    case R_PPC64_ADDR14:
15473
0
    case R_PPC64_ADDR14_BRTAKEN:
15474
0
    case R_PPC64_ADDR14_BRNTAKEN:
15475
0
    case R_PPC64_REL32:
15476
0
    case R_PPC64_REL64:
15477
0
      return true;
15478
15479
0
    default:
15480
0
      return false;
15481
0
    }
15482
0
}
15483
15484
/* The RELOCATE_SECTION function is called by the ELF backend linker
15485
   to handle the relocations for a section.
15486
15487
   The relocs are always passed as Rela structures; if the section
15488
   actually uses Rel structures, the r_addend field will always be
15489
   zero.
15490
15491
   This function is responsible for adjust the section contents as
15492
   necessary, and (if using Rela relocs and generating a
15493
   relocatable output file) adjusting the reloc addend as
15494
   necessary.
15495
15496
   This function does not have to worry about setting the reloc
15497
   address or the reloc symbol index.
15498
15499
   LOCAL_SYMS is a pointer to the swapped in local symbols.
15500
15501
   LOCAL_SECTIONS is an array giving the section in the input file
15502
   corresponding to the st_shndx field of each local symbol.
15503
15504
   The global hash table entry for the global symbols can be found
15505
   via elf_sym_hashes (input_bfd).
15506
15507
   When generating relocatable output, this function must handle
15508
   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
15509
   going to be the section symbol corresponding to the output
15510
   section, which means that the addend must be adjusted
15511
   accordingly.  */
15512
15513
static int
15514
ppc64_elf_relocate_section (bfd *output_bfd,
15515
          struct bfd_link_info *info,
15516
          bfd *input_bfd,
15517
          asection *input_section,
15518
          bfd_byte *contents,
15519
          Elf_Internal_Rela *relocs,
15520
          Elf_Internal_Sym *local_syms,
15521
          asection **local_sections)
15522
0
{
15523
0
  struct ppc_link_hash_table *htab;
15524
0
  Elf_Internal_Shdr *symtab_hdr;
15525
0
  struct elf_link_hash_entry **sym_hashes;
15526
0
  Elf_Internal_Rela *rel;
15527
0
  Elf_Internal_Rela *wrel;
15528
0
  Elf_Internal_Rela *relend;
15529
0
  Elf_Internal_Rela outrel;
15530
0
  bfd_byte *loc;
15531
0
  struct got_entry **local_got_ents;
15532
0
  bfd_vma TOCstart;
15533
0
  bool ret = true;
15534
0
  bool is_opd;
15535
  /* Assume 'at' branch hints.  */
15536
0
  bool is_isa_v2 = true;
15537
0
  bool warned_dynamic = false;
15538
0
  bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
15539
15540
  /* Initialize howto table if needed.  */
15541
0
  if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
15542
0
    ppc_howto_init ();
15543
15544
0
  htab = ppc_hash_table (info);
15545
0
  if (htab == NULL)
15546
0
    return false;
15547
15548
  /* Don't relocate stub sections.  */
15549
0
  if (input_section->owner == htab->params->stub_bfd)
15550
0
    return true;
15551
15552
0
  if (!is_ppc64_elf (input_bfd))
15553
0
    {
15554
0
      bfd_set_error (bfd_error_wrong_format);
15555
0
      return false;
15556
0
    }
15557
15558
0
  local_got_ents = elf_local_got_ents (input_bfd);
15559
0
  TOCstart = elf_gp (output_bfd);
15560
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
15561
0
  sym_hashes = elf_sym_hashes (input_bfd);
15562
0
  is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
15563
15564
0
  rel = wrel = relocs;
15565
0
  relend = relocs + input_section->reloc_count;
15566
0
  for (; rel < relend; wrel++, rel++)
15567
0
    {
15568
0
      enum elf_ppc64_reloc_type r_type;
15569
0
      bfd_vma addend;
15570
0
      bfd_reloc_status_type r;
15571
0
      Elf_Internal_Sym *sym;
15572
0
      asection *sec;
15573
0
      struct elf_link_hash_entry *h_elf;
15574
0
      struct ppc_link_hash_entry *h;
15575
0
      struct ppc_link_hash_entry *fdh;
15576
0
      const char *sym_name;
15577
0
      unsigned long r_symndx, toc_symndx;
15578
0
      bfd_vma toc_addend;
15579
0
      unsigned char tls_mask, tls_gd, tls_type;
15580
0
      unsigned char sym_type;
15581
0
      bfd_vma relocation;
15582
0
      bool unresolved_reloc, save_unresolved_reloc;
15583
0
      bool warned;
15584
0
      enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
15585
0
      unsigned int insn;
15586
0
      unsigned int mask;
15587
0
      struct ppc_stub_hash_entry *stub_entry;
15588
0
      bfd_vma max_br_offset;
15589
0
      bfd_vma from;
15590
0
      Elf_Internal_Rela orig_rel;
15591
0
      reloc_howto_type *howto;
15592
0
      struct reloc_howto_struct alt_howto;
15593
0
      uint64_t pinsn;
15594
0
      bfd_vma offset;
15595
15596
0
    again:
15597
0
      orig_rel = *rel;
15598
15599
0
      r_type = ELF64_R_TYPE (rel->r_info);
15600
0
      r_symndx = ELF64_R_SYM (rel->r_info);
15601
15602
      /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15603
   symbol of the previous ADDR64 reloc.  The symbol gives us the
15604
   proper TOC base to use.  */
15605
0
      if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
15606
0
    && wrel != relocs
15607
0
    && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
15608
0
    && is_opd)
15609
0
  r_symndx = ELF64_R_SYM (wrel[-1].r_info);
15610
15611
0
      sym = NULL;
15612
0
      sec = NULL;
15613
0
      h_elf = NULL;
15614
0
      sym_name = NULL;
15615
0
      unresolved_reloc = false;
15616
0
      warned = false;
15617
15618
0
      if (r_symndx < symtab_hdr->sh_info)
15619
0
  {
15620
    /* It's a local symbol.  */
15621
0
    struct _opd_sec_data *opd;
15622
15623
0
    sym = local_syms + r_symndx;
15624
0
    sec = local_sections[r_symndx];
15625
0
    sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
15626
0
    sym_type = ELF64_ST_TYPE (sym->st_info);
15627
0
    relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
15628
0
    opd = get_opd_info (sec);
15629
0
    if (opd != NULL && opd->adjust != NULL)
15630
0
      {
15631
0
        long adjust = opd->adjust[OPD_NDX (sym->st_value
15632
0
             + rel->r_addend)];
15633
0
        if (adjust == -1)
15634
0
    relocation = 0;
15635
0
        else
15636
0
    {
15637
      /* If this is a relocation against the opd section sym
15638
         and we have edited .opd, adjust the reloc addend so
15639
         that ld -r and ld --emit-relocs output is correct.
15640
         If it is a reloc against some other .opd symbol,
15641
         then the symbol value will be adjusted later.  */
15642
0
      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15643
0
        rel->r_addend += adjust;
15644
0
      else
15645
0
        relocation += adjust;
15646
0
    }
15647
0
      }
15648
0
  }
15649
0
      else
15650
0
  {
15651
0
    bool ignored;
15652
15653
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15654
0
           r_symndx, symtab_hdr, sym_hashes,
15655
0
           h_elf, sec, relocation,
15656
0
           unresolved_reloc, warned, ignored);
15657
0
    sym_name = h_elf->root.root.string;
15658
0
    sym_type = h_elf->type;
15659
0
    if (sec != NULL
15660
0
        && sec->owner == output_bfd
15661
0
        && strcmp (sec->name, ".opd") == 0)
15662
0
      {
15663
        /* This is a symbol defined in a linker script.  All
15664
     such are defined in output sections, even those
15665
     defined by simple assignment from a symbol defined in
15666
     an input section.  Transfer the symbol to an
15667
     appropriate input .opd section, so that a branch to
15668
     this symbol will be mapped to the location specified
15669
     by the opd entry.  */
15670
0
        struct bfd_link_order *lo;
15671
0
        for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15672
0
    if (lo->type == bfd_indirect_link_order)
15673
0
      {
15674
0
        asection *isec = lo->u.indirect.section;
15675
0
        if (h_elf->root.u.def.value >= isec->output_offset
15676
0
      && h_elf->root.u.def.value < (isec->output_offset
15677
0
                  + isec->size))
15678
0
          {
15679
0
      h_elf->root.u.def.value -= isec->output_offset;
15680
0
      h_elf->root.u.def.section = isec;
15681
0
      sec = isec;
15682
0
      break;
15683
0
          }
15684
0
      }
15685
0
      }
15686
0
  }
15687
0
      h = ppc_elf_hash_entry (h_elf);
15688
15689
0
      if (sec != NULL && discarded_section (sec))
15690
0
  {
15691
0
    if (r_type < ARRAY_SIZE (ppc64_elf_howto_table)
15692
0
        && ppc64_elf_howto_table[r_type] != NULL)
15693
0
      _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15694
0
         input_bfd, input_section,
15695
0
         contents, rel->r_offset);
15696
0
    wrel->r_offset = rel->r_offset;
15697
0
    wrel->r_info = 0;
15698
0
    wrel->r_addend = 0;
15699
15700
    /* For ld -r, remove relocations in debug sections against
15701
       symbols defined in discarded sections.  Not done for
15702
       non-debug to preserve relocs in .eh_frame which the
15703
       eh_frame editing code expects to be present.  */
15704
0
    if (bfd_link_relocatable (info)
15705
0
        && (input_section->flags & SEC_DEBUGGING))
15706
0
      wrel--;
15707
15708
0
    continue;
15709
0
  }
15710
15711
0
      if (bfd_link_relocatable (info))
15712
0
  goto copy_reloc;
15713
15714
0
      if (h != NULL && &h->elf == htab->elf.hgot)
15715
0
  {
15716
0
    relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15717
0
    sec = bfd_abs_section_ptr;
15718
0
    unresolved_reloc = false;
15719
0
  }
15720
15721
      /* TLS optimizations.  Replace instruction sequences and relocs
15722
   based on information we collected in tls_optimize.  We edit
15723
   RELOCS so that --emit-relocs will output something sensible
15724
   for the final instruction stream.  */
15725
0
      tls_mask = 0;
15726
0
      tls_gd = 0;
15727
0
      toc_symndx = 0;
15728
0
      if (h != NULL)
15729
0
  tls_mask = h->tls_mask;
15730
0
      else if (local_got_ents != NULL)
15731
0
  {
15732
0
    struct plt_entry **local_plt = (struct plt_entry **)
15733
0
      (local_got_ents + symtab_hdr->sh_info);
15734
0
    unsigned char *lgot_masks = (unsigned char *)
15735
0
      (local_plt + symtab_hdr->sh_info);
15736
0
    tls_mask = lgot_masks[r_symndx];
15737
0
  }
15738
0
      if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
15739
0
    && (r_type == R_PPC64_TLS
15740
0
        || r_type == R_PPC64_TLSGD
15741
0
        || r_type == R_PPC64_TLSLD))
15742
0
  {
15743
    /* Check for toc tls entries.  */
15744
0
    unsigned char *toc_tls;
15745
15746
0
    if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15747
0
           &local_syms, rel, input_bfd))
15748
0
      return false;
15749
15750
0
    if (toc_tls)
15751
0
      tls_mask = *toc_tls;
15752
0
  }
15753
15754
      /* Check that tls relocs are used with tls syms, and non-tls
15755
   relocs are used with non-tls syms.  */
15756
0
      if (r_symndx != STN_UNDEF
15757
0
    && r_type != R_PPC64_NONE
15758
0
    && r_type < ARRAY_SIZE (ppc64_elf_howto_table)
15759
0
    && ppc64_elf_howto_table[r_type] != NULL
15760
0
    && (h == NULL
15761
0
        || h->elf.root.type == bfd_link_hash_defined
15762
0
        || h->elf.root.type == bfd_link_hash_defweak)
15763
0
    && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
15764
0
  {
15765
0
    if ((tls_mask & TLS_TLS) != 0
15766
0
        && (r_type == R_PPC64_TLS
15767
0
      || r_type == R_PPC64_TLSGD
15768
0
      || r_type == R_PPC64_TLSLD))
15769
      /* R_PPC64_TLS is OK against a symbol in the TOC.  */
15770
0
      ;
15771
0
    else
15772
0
      info->callbacks->einfo
15773
0
        (!IS_PPC64_TLS_RELOC (r_type)
15774
         /* xgettext:c-format */
15775
0
         ? _("%H: %s used with TLS symbol `%pT'\n")
15776
         /* xgettext:c-format */
15777
0
         : _("%H: %s used with non-TLS symbol `%pT'\n"),
15778
0
         input_bfd, input_section, rel->r_offset,
15779
0
         ppc64_elf_howto_table[r_type]->name,
15780
0
         sym_name);
15781
0
  }
15782
15783
      /* Ensure reloc mapping code below stays sane.  */
15784
0
      if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15785
0
    || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15786
0
    || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
15787
0
    || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15788
0
    || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15789
0
    || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15790
0
    || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
15791
0
    || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15792
0
    || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15793
0
    || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15794
0
  abort ();
15795
15796
0
      switch (r_type)
15797
0
  {
15798
0
  default:
15799
0
    break;
15800
15801
0
  case R_PPC64_LO_DS_OPT:
15802
0
    if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15803
0
      {
15804
0
        insn = bfd_get_32 (input_bfd,
15805
0
         contents + rel->r_offset - d_offset);
15806
0
        if ((insn & (0x3fu << 26)) != 58u << 26)
15807
0
    abort ();
15808
0
        insn += (14u << 26) - (58u << 26);
15809
0
        bfd_put_32 (input_bfd, insn,
15810
0
        contents + rel->r_offset - d_offset);
15811
0
        r_type = R_PPC64_TOC16_LO;
15812
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15813
0
      }
15814
0
    break;
15815
15816
0
  case R_PPC64_TOC16:
15817
0
  case R_PPC64_TOC16_LO:
15818
0
  case R_PPC64_TOC16_DS:
15819
0
  case R_PPC64_TOC16_LO_DS:
15820
0
    {
15821
      /* Check for toc tls entries.  */
15822
0
      unsigned char *toc_tls;
15823
0
      int retval;
15824
15825
0
      retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15826
0
           &local_syms, rel, input_bfd);
15827
0
      if (retval == 0)
15828
0
        return false;
15829
15830
0
      if (toc_tls)
15831
0
        {
15832
0
    tls_mask = *toc_tls;
15833
0
    if (r_type == R_PPC64_TOC16_DS
15834
0
        || r_type == R_PPC64_TOC16_LO_DS)
15835
0
      {
15836
0
        if ((tls_mask & TLS_TLS) != 0
15837
0
      && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15838
0
          goto toctprel;
15839
0
      }
15840
0
    else
15841
0
      {
15842
        /* If we found a GD reloc pair, then we might be
15843
           doing a GD->IE transition.  */
15844
0
        if (retval == 2)
15845
0
          {
15846
0
      tls_gd = TLS_GDIE;
15847
0
      if ((tls_mask & TLS_TLS) != 0
15848
0
          && (tls_mask & TLS_GD) == 0)
15849
0
        goto tls_ldgd_opt;
15850
0
          }
15851
0
        else if (retval == 3)
15852
0
          {
15853
0
      if ((tls_mask & TLS_TLS) != 0
15854
0
          && (tls_mask & TLS_LD) == 0)
15855
0
        goto tls_ldgd_opt;
15856
0
          }
15857
0
      }
15858
0
        }
15859
0
    }
15860
0
    break;
15861
15862
0
  case R_PPC64_GOT_TPREL16_HI:
15863
0
  case R_PPC64_GOT_TPREL16_HA:
15864
0
    if ((tls_mask & TLS_TLS) != 0
15865
0
        && (tls_mask & TLS_TPREL) == 0
15866
0
        && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15867
0
      {
15868
0
        rel->r_offset -= d_offset;
15869
0
        bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15870
0
        r_type = R_PPC64_NONE;
15871
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15872
0
      }
15873
0
    break;
15874
15875
0
  case R_PPC64_GOT_TPREL16_DS:
15876
0
  case R_PPC64_GOT_TPREL16_LO_DS:
15877
0
    if ((tls_mask & TLS_TLS) != 0
15878
0
        && (tls_mask & TLS_TPREL) == 0
15879
0
        && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15880
0
      {
15881
0
      toctprel:
15882
0
        insn = bfd_get_32 (input_bfd,
15883
0
         contents + rel->r_offset - d_offset);
15884
0
        insn &= 31 << 21;
15885
0
        insn |= 0x3c0d0000; /* addis 0,13,0 */
15886
0
        bfd_put_32 (input_bfd, insn,
15887
0
        contents + rel->r_offset - d_offset);
15888
0
        r_type = R_PPC64_TPREL16_HA;
15889
0
        if (toc_symndx != 0)
15890
0
    {
15891
0
      rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15892
0
      rel->r_addend = toc_addend;
15893
      /* We changed the symbol.  Start over in order to
15894
         get h, sym, sec etc. right.  */
15895
0
      goto again;
15896
0
    }
15897
0
        else
15898
0
    rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15899
0
      }
15900
0
    break;
15901
15902
0
  case R_PPC64_GOT_TPREL_PCREL34:
15903
0
    if ((tls_mask & TLS_TLS) != 0
15904
0
        && (tls_mask & TLS_TPREL) == 0
15905
0
        && offset_in_range (input_section, rel->r_offset, 8))
15906
0
      {
15907
        /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel  */
15908
0
        pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15909
0
        pinsn <<= 32;
15910
0
        pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15911
0
        pinsn += ((2ULL << 56) + (-1ULL << 52)
15912
0
      + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15913
0
        bfd_put_32 (input_bfd, pinsn >> 32,
15914
0
        contents + rel->r_offset);
15915
0
        bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15916
0
        contents + rel->r_offset + 4);
15917
0
        r_type = R_PPC64_TPREL34;
15918
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15919
0
      }
15920
0
    break;
15921
15922
0
  case R_PPC64_TLS:
15923
0
    if ((tls_mask & TLS_TLS) != 0
15924
0
        && (tls_mask & TLS_TPREL) == 0
15925
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
15926
0
      {
15927
0
        insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15928
0
        insn = bfd_elf_ppc_at_tls_transform (insn, 13);
15929
0
        if (insn == 0)
15930
0
    break;
15931
0
        if ((rel->r_offset & 3) == 0)
15932
0
    {
15933
0
      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15934
      /* Was PPC64_TLS which sits on insn boundary, now
15935
         PPC64_TPREL16_LO which is at low-order half-word.  */
15936
0
      rel->r_offset += d_offset;
15937
0
      r_type = R_PPC64_TPREL16_LO;
15938
0
      if (toc_symndx != 0)
15939
0
        {
15940
0
          rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15941
0
          rel->r_addend = toc_addend;
15942
          /* We changed the symbol.  Start over in order to
15943
       get h, sym, sec etc. right.  */
15944
0
          goto again;
15945
0
        }
15946
0
      else
15947
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15948
0
    }
15949
0
        else if ((rel->r_offset & 3) == 1)
15950
0
    {
15951
      /* For pcrel IE to LE we already have the full
15952
         offset and thus don't need an addi here.  A nop
15953
         or mr will do.  */
15954
0
      if ((insn & (0x3fu << 26)) == 14 << 26)
15955
0
        {
15956
          /* Extract regs from addi rt,ra,si.  */
15957
0
          unsigned int rt = (insn >> 21) & 0x1f;
15958
0
          unsigned int ra = (insn >> 16) & 0x1f;
15959
0
          if (rt == ra)
15960
0
      insn = NOP;
15961
0
          else
15962
0
      {
15963
        /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs.  */
15964
0
        insn = (rt << 16) | (ra << 21) | (ra << 11);
15965
0
        insn |= (31u << 26) | (444u << 1);
15966
0
      }
15967
0
        }
15968
0
      bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
15969
0
    }
15970
0
      }
15971
0
    break;
15972
15973
0
  case R_PPC64_GOT_TLSGD16_HI:
15974
0
  case R_PPC64_GOT_TLSGD16_HA:
15975
0
    tls_gd = TLS_GDIE;
15976
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15977
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
15978
0
      goto tls_gdld_hi;
15979
0
    break;
15980
15981
0
  case R_PPC64_GOT_TLSLD16_HI:
15982
0
  case R_PPC64_GOT_TLSLD16_HA:
15983
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15984
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
15985
0
      {
15986
0
      tls_gdld_hi:
15987
0
        if ((tls_mask & tls_gd) != 0)
15988
0
    r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15989
0
        + R_PPC64_GOT_TPREL16_DS);
15990
0
        else
15991
0
    {
15992
0
      rel->r_offset -= d_offset;
15993
0
      bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15994
0
      r_type = R_PPC64_NONE;
15995
0
    }
15996
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15997
0
      }
15998
0
    break;
15999
16000
0
  case R_PPC64_GOT_TLSGD16:
16001
0
  case R_PPC64_GOT_TLSGD16_LO:
16002
0
    tls_gd = TLS_GDIE;
16003
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16004
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
16005
0
      goto tls_ldgd_opt;
16006
0
    break;
16007
16008
0
  case R_PPC64_GOT_TLSLD16:
16009
0
  case R_PPC64_GOT_TLSLD16_LO:
16010
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16011
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
16012
0
      {
16013
0
        unsigned int insn1, insn2;
16014
16015
0
      tls_ldgd_opt:
16016
0
        offset = (bfd_vma) -1;
16017
        /* If not using the newer R_PPC64_TLSGD/LD to mark
16018
     __tls_get_addr calls, we must trust that the call
16019
     stays with its arg setup insns, ie. that the next
16020
     reloc is the __tls_get_addr call associated with
16021
     the current reloc.  Edit both insns.  */
16022
0
        if (input_section->nomark_tls_get_addr
16023
0
      && rel + 1 < relend
16024
0
      && branch_reloc_hash_match (input_bfd, rel + 1,
16025
0
                htab->tls_get_addr_fd,
16026
0
                htab->tga_desc_fd,
16027
0
                htab->tls_get_addr,
16028
0
                htab->tga_desc))
16029
0
    offset = rel[1].r_offset;
16030
        /* We read the low GOT_TLS (or TOC16) insn because we
16031
     need to keep the destination reg.  It may be
16032
     something other than the usual r3, and moved to r3
16033
     before the call by intervening code.  */
16034
0
        insn1 = bfd_get_32 (input_bfd,
16035
0
          contents + rel->r_offset - d_offset);
16036
0
        if ((tls_mask & tls_gd) != 0)
16037
0
    {
16038
      /* IE */
16039
0
      insn1 &= (0x1f << 21) | (0x1f << 16);
16040
0
      insn1 |= 58u << 26; /* ld */
16041
0
      insn2 = 0x7c636a14; /* add 3,3,13 */
16042
0
      if (offset != (bfd_vma) -1)
16043
0
        rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16044
0
      if (r_type == R_PPC64_TOC16
16045
0
          || r_type == R_PPC64_TOC16_LO)
16046
0
        r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
16047
0
      else
16048
0
        r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
16049
0
            + R_PPC64_GOT_TPREL16_DS);
16050
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16051
0
    }
16052
0
        else
16053
0
    {
16054
      /* LE */
16055
0
      insn1 &= 0x1f << 21;
16056
0
      insn1 |= 0x3c0d0000;  /* addis r,13,0 */
16057
0
      insn2 = 0x38630000; /* addi 3,3,0 */
16058
0
      if (tls_gd == 0)
16059
0
        {
16060
          /* Was an LD reloc.  */
16061
0
          r_symndx = STN_UNDEF;
16062
0
          rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16063
0
        }
16064
0
      else if (toc_symndx != 0)
16065
0
        {
16066
0
          r_symndx = toc_symndx;
16067
0
          rel->r_addend = toc_addend;
16068
0
        }
16069
0
      r_type = R_PPC64_TPREL16_HA;
16070
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16071
0
      if (offset != (bfd_vma) -1)
16072
0
        {
16073
0
          rel[1].r_info = ELF64_R_INFO (r_symndx,
16074
0
                R_PPC64_TPREL16_LO);
16075
0
          rel[1].r_offset = offset + d_offset;
16076
0
          rel[1].r_addend = rel->r_addend;
16077
0
        }
16078
0
    }
16079
0
        bfd_put_32 (input_bfd, insn1,
16080
0
        contents + rel->r_offset - d_offset);
16081
0
        if (offset != (bfd_vma) -1
16082
0
      && offset_in_range (input_section, offset, 4))
16083
0
    {
16084
0
      bfd_put_32 (input_bfd, insn2, contents + offset);
16085
0
      if (offset_in_range (input_section, offset + 4, 4))
16086
0
        {
16087
0
          insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16088
0
          if (insn2 == LD_R2_0R1 + STK_TOC (htab))
16089
0
      bfd_put_32 (input_bfd, NOP, contents + offset + 4);
16090
0
        }
16091
0
    }
16092
0
        if ((tls_mask & tls_gd) == 0
16093
0
      && (tls_gd == 0 || toc_symndx != 0))
16094
0
    {
16095
      /* We changed the symbol.  Start over in order
16096
         to get h, sym, sec etc. right.  */
16097
0
      goto again;
16098
0
    }
16099
0
      }
16100
0
    break;
16101
16102
0
  case R_PPC64_GOT_TLSGD_PCREL34:
16103
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16104
0
        && offset_in_range (input_section, rel->r_offset, 8))
16105
0
      {
16106
0
        pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16107
0
        pinsn <<= 32;
16108
0
        pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16109
0
        if ((tls_mask & TLS_GDIE) != 0)
16110
0
    {
16111
      /* IE, pla -> pld  */
16112
0
      pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
16113
0
      r_type = R_PPC64_GOT_TPREL_PCREL34;
16114
0
    }
16115
0
        else
16116
0
    {
16117
      /* LE, pla pcrel -> paddi r13  */
16118
0
      pinsn += (-1ULL << 52) + (13ULL << 16);
16119
0
      r_type = R_PPC64_TPREL34;
16120
0
    }
16121
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16122
0
        bfd_put_32 (input_bfd, pinsn >> 32,
16123
0
        contents + rel->r_offset);
16124
0
        bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16125
0
        contents + rel->r_offset + 4);
16126
0
      }
16127
0
    break;
16128
16129
0
  case R_PPC64_GOT_TLSLD_PCREL34:
16130
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16131
0
        && offset_in_range (input_section, rel->r_offset, 8))
16132
0
      {
16133
0
        pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16134
0
        pinsn <<= 32;
16135
0
        pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16136
0
        pinsn += (-1ULL << 52) + (13ULL << 16);
16137
0
        bfd_put_32 (input_bfd, pinsn >> 32,
16138
0
        contents + rel->r_offset);
16139
0
        bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16140
0
        contents + rel->r_offset + 4);
16141
0
        rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16142
0
        r_symndx = STN_UNDEF;
16143
0
        r_type = R_PPC64_TPREL34;
16144
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16145
0
        goto again;
16146
0
      }
16147
0
    break;
16148
16149
0
  case R_PPC64_TLSGD:
16150
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16151
0
        && rel + 1 < relend
16152
0
        && offset_in_range (input_section, rel->r_offset,
16153
0
          is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16154
0
          ? 8 : 4))
16155
0
      {
16156
0
        unsigned int insn2;
16157
0
        enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16158
16159
0
        offset = rel->r_offset;
16160
0
        if (is_plt_seq_reloc (r_type1))
16161
0
    {
16162
0
      bfd_put_32 (output_bfd, NOP, contents + offset);
16163
0
      if (r_type1 == R_PPC64_PLT_PCREL34
16164
0
          || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16165
0
        bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16166
0
      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16167
0
      break;
16168
0
    }
16169
16170
0
        if (r_type1 == R_PPC64_PLTCALL)
16171
0
    bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16172
16173
0
        if ((tls_mask & TLS_GDIE) != 0)
16174
0
    {
16175
      /* IE */
16176
0
      r_type = R_PPC64_NONE;
16177
0
      insn2 = 0x7c636a14; /* add 3,3,13 */
16178
0
    }
16179
0
        else
16180
0
    {
16181
      /* LE */
16182
0
      if (toc_symndx != 0)
16183
0
        {
16184
0
          r_symndx = toc_symndx;
16185
0
          rel->r_addend = toc_addend;
16186
0
        }
16187
0
      if (r_type1 == R_PPC64_REL24_NOTOC
16188
0
          || r_type1 == R_PPC64_REL24_P9NOTOC
16189
0
          || r_type1 == R_PPC64_PLTCALL_NOTOC)
16190
0
        {
16191
0
          r_type = R_PPC64_NONE;
16192
0
          insn2 = NOP;
16193
0
        }
16194
0
      else
16195
0
        {
16196
0
          rel->r_offset = offset + d_offset;
16197
0
          r_type = R_PPC64_TPREL16_LO;
16198
0
          insn2 = 0x38630000; /* addi 3,3,0 */
16199
0
        }
16200
0
    }
16201
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16202
        /* Zap the reloc on the _tls_get_addr call too.  */
16203
0
        BFD_ASSERT (offset == rel[1].r_offset);
16204
0
        rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16205
0
        bfd_put_32 (input_bfd, insn2, contents + offset);
16206
0
        if ((tls_mask & TLS_GDIE) == 0
16207
0
      && toc_symndx != 0
16208
0
      && r_type != R_PPC64_NONE)
16209
0
    goto again;
16210
0
      }
16211
0
    break;
16212
16213
0
  case R_PPC64_TLSLD:
16214
0
    if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16215
0
        && rel + 1 < relend
16216
0
        && offset_in_range (input_section, rel->r_offset,
16217
0
          is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16218
0
          ? 8 : 4))
16219
0
      {
16220
0
        unsigned int insn2;
16221
0
        enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16222
16223
0
        offset = rel->r_offset;
16224
0
        if (is_plt_seq_reloc (r_type1))
16225
0
    {
16226
0
      bfd_put_32 (output_bfd, NOP, contents + offset);
16227
0
      if (r_type1 == R_PPC64_PLT_PCREL34
16228
0
          || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16229
0
        bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16230
0
      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16231
0
      break;
16232
0
    }
16233
16234
0
        if (r_type1 == R_PPC64_PLTCALL)
16235
0
    bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16236
16237
0
        if (r_type1 == R_PPC64_REL24_NOTOC
16238
0
      || r_type1 == R_PPC64_REL24_P9NOTOC
16239
0
      || r_type1 == R_PPC64_PLTCALL_NOTOC)
16240
0
    {
16241
0
      r_type = R_PPC64_NONE;
16242
0
      insn2 = NOP;
16243
0
    }
16244
0
        else
16245
0
    {
16246
0
      rel->r_offset = offset + d_offset;
16247
0
      r_symndx = STN_UNDEF;
16248
0
      r_type = R_PPC64_TPREL16_LO;
16249
0
      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16250
0
      insn2 = 0x38630000; /* addi 3,3,0 */
16251
0
    }
16252
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16253
        /* Zap the reloc on the _tls_get_addr call too.  */
16254
0
        BFD_ASSERT (offset == rel[1].r_offset);
16255
0
        rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16256
0
        bfd_put_32 (input_bfd, insn2, contents + offset);
16257
0
        if (r_type != R_PPC64_NONE)
16258
0
    goto again;
16259
0
      }
16260
0
    break;
16261
16262
0
  case R_PPC64_DTPMOD64:
16263
0
    if (rel + 1 < relend
16264
0
        && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16265
0
        && rel[1].r_offset == rel->r_offset + 8)
16266
0
      {
16267
0
        if ((tls_mask & TLS_GD) == 0
16268
0
      && offset_in_range (input_section, rel->r_offset, 8))
16269
0
    {
16270
0
      rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
16271
0
      if ((tls_mask & TLS_GDIE) != 0)
16272
0
        r_type = R_PPC64_TPREL64;
16273
0
      else
16274
0
        {
16275
0
          bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16276
0
          r_type = R_PPC64_NONE;
16277
0
        }
16278
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16279
0
    }
16280
0
      }
16281
0
    else
16282
0
      {
16283
0
        if ((tls_mask & TLS_LD) == 0
16284
0
      && offset_in_range (input_section, rel->r_offset, 8))
16285
0
    {
16286
0
      bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16287
0
      r_type = R_PPC64_NONE;
16288
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16289
0
    }
16290
0
      }
16291
0
    break;
16292
16293
0
  case R_PPC64_TPREL64:
16294
0
    if ((tls_mask & TLS_TPREL) == 0)
16295
0
      {
16296
0
        r_type = R_PPC64_NONE;
16297
0
        rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16298
0
      }
16299
0
    break;
16300
16301
0
  case R_PPC64_ENTRY:
16302
0
    relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16303
0
    if (!bfd_link_pic (info)
16304
0
        && !info->traditional_format
16305
0
        && relocation + 0x80008000 <= 0xffffffff
16306
0
        && offset_in_range (input_section, rel->r_offset, 8))
16307
0
      {
16308
0
        unsigned int insn1, insn2;
16309
16310
0
        insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16311
0
        insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16312
0
        if ((insn1 & ~0xfffc) == LD_R2_0R12
16313
0
      && insn2 == ADD_R2_R2_R12)
16314
0
    {
16315
0
      bfd_put_32 (input_bfd,
16316
0
            LIS_R2 + PPC_HA (relocation),
16317
0
            contents + rel->r_offset);
16318
0
      bfd_put_32 (input_bfd,
16319
0
            ADDI_R2_R2 + PPC_LO (relocation),
16320
0
            contents + rel->r_offset + 4);
16321
0
    }
16322
0
      }
16323
0
    else
16324
0
      {
16325
0
        relocation -= (rel->r_offset
16326
0
           + input_section->output_offset
16327
0
           + input_section->output_section->vma);
16328
0
        if (relocation + 0x80008000 <= 0xffffffff
16329
0
      && offset_in_range (input_section, rel->r_offset, 8))
16330
0
    {
16331
0
      unsigned int insn1, insn2;
16332
16333
0
      insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16334
0
      insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16335
0
      if ((insn1 & ~0xfffc) == LD_R2_0R12
16336
0
          && insn2 == ADD_R2_R2_R12)
16337
0
        {
16338
0
          bfd_put_32 (input_bfd,
16339
0
          ADDIS_R2_R12 + PPC_HA (relocation),
16340
0
          contents + rel->r_offset);
16341
0
          bfd_put_32 (input_bfd,
16342
0
          ADDI_R2_R2 + PPC_LO (relocation),
16343
0
          contents + rel->r_offset + 4);
16344
0
        }
16345
0
    }
16346
0
      }
16347
0
    break;
16348
16349
0
  case R_PPC64_REL16_HA:
16350
    /* If we are generating a non-PIC executable, edit
16351
       .  0:  addis 2,12,.TOC.-0b@ha
16352
       .    addi 2,2,.TOC.-0b@l
16353
       used by ELFv2 global entry points to set up r2, to
16354
       .    lis 2,.TOC.@ha
16355
       .    addi 2,2,.TOC.@l
16356
       if .TOC. is in range.  */
16357
0
    if (!bfd_link_pic (info)
16358
0
        && !info->traditional_format
16359
0
        && !htab->opd_abi
16360
0
        && rel->r_addend == d_offset
16361
0
        && h != NULL && &h->elf == htab->elf.hgot
16362
0
        && rel + 1 < relend
16363
0
        && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16364
0
        && rel[1].r_offset == rel->r_offset + 4
16365
0
        && rel[1].r_addend == rel->r_addend + 4
16366
0
        && relocation + 0x80008000 <= 0xffffffff
16367
0
        && offset_in_range (input_section, rel->r_offset - d_offset, 8))
16368
0
      {
16369
0
        unsigned int insn1, insn2;
16370
0
        offset = rel->r_offset - d_offset;
16371
0
        insn1 = bfd_get_32 (input_bfd, contents + offset);
16372
0
        insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16373
0
        if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16374
0
      && (insn2 & 0xffff0000) == ADDI_R2_R2)
16375
0
    {
16376
0
      r_type = R_PPC64_ADDR16_HA;
16377
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16378
0
      rel->r_addend -= d_offset;
16379
0
      rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16380
0
      rel[1].r_addend -= d_offset + 4;
16381
0
      bfd_put_32 (input_bfd, LIS_R2, contents + offset);
16382
0
    }
16383
0
      }
16384
0
    break;
16385
0
  }
16386
16387
      /* Handle other relocations that tweak non-addend part of insn.  */
16388
0
      insn = 0;
16389
0
      max_br_offset = 1 << 25;
16390
0
      addend = rel->r_addend;
16391
0
      reloc_dest = DEST_NORMAL;
16392
0
      switch (r_type)
16393
0
  {
16394
0
  default:
16395
0
    break;
16396
16397
0
  case R_PPC64_TOCSAVE:
16398
0
    if (relocation + addend == (rel->r_offset
16399
0
              + input_section->output_offset
16400
0
              + input_section->output_section->vma)
16401
0
        && tocsave_find (htab, NO_INSERT,
16402
0
             &local_syms, rel, input_bfd)
16403
0
        && offset_in_range (input_section, rel->r_offset, 4))
16404
0
      {
16405
0
        insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16406
0
        if (insn == NOP
16407
0
      || insn == CROR_151515 || insn == CROR_313131)
16408
0
    bfd_put_32 (input_bfd,
16409
0
          STD_R2_0R1 + STK_TOC (htab),
16410
0
          contents + rel->r_offset);
16411
0
      }
16412
0
    break;
16413
16414
    /* Branch taken prediction relocations.  */
16415
0
  case R_PPC64_ADDR14_BRTAKEN:
16416
0
  case R_PPC64_REL14_BRTAKEN:
16417
0
    insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
16418
    /* Fall through.  */
16419
16420
    /* Branch not taken prediction relocations.  */
16421
0
  case R_PPC64_ADDR14_BRNTAKEN:
16422
0
  case R_PPC64_REL14_BRNTAKEN:
16423
0
    if (!offset_in_range (input_section, rel->r_offset, 4))
16424
0
      break;
16425
0
    insn |= bfd_get_32 (input_bfd,
16426
0
            contents + rel->r_offset) & ~(0x01 << 21);
16427
    /* Fall through.  */
16428
16429
0
  case R_PPC64_REL14:
16430
0
    max_br_offset = 1 << 15;
16431
    /* Fall through.  */
16432
16433
0
  case R_PPC64_REL24:
16434
0
  case R_PPC64_REL24_NOTOC:
16435
0
  case R_PPC64_REL24_P9NOTOC:
16436
0
  case R_PPC64_PLTCALL:
16437
0
  case R_PPC64_PLTCALL_NOTOC:
16438
    /* Calls to functions with a different TOC, such as calls to
16439
       shared objects, need to alter the TOC pointer.  This is
16440
       done using a linkage stub.  A REL24 branching to these
16441
       linkage stubs needs to be followed by a nop, as the nop
16442
       will be replaced with an instruction to restore the TOC
16443
       base pointer.  */
16444
0
    fdh = h;
16445
0
    if (h != NULL
16446
0
        && h->oh != NULL
16447
0
        && h->oh->is_func_descriptor)
16448
0
      fdh = ppc_follow_link (h->oh);
16449
0
    stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16450
0
             htab);
16451
0
    if ((r_type == R_PPC64_PLTCALL
16452
0
         || r_type == R_PPC64_PLTCALL_NOTOC)
16453
0
        && stub_entry != NULL
16454
0
        && stub_entry->type.main == ppc_stub_plt_call)
16455
0
      stub_entry = NULL;
16456
16457
0
    if (stub_entry != NULL
16458
0
        && (stub_entry->type.main == ppc_stub_plt_call
16459
0
      || stub_entry->type.r2save))
16460
0
      {
16461
0
        bool can_plt_call = false;
16462
16463
0
        if (r_type == R_PPC64_REL24_NOTOC
16464
0
      || r_type == R_PPC64_REL24_P9NOTOC)
16465
0
    {
16466
      /* NOTOC calls don't need to restore r2.  */
16467
0
      can_plt_call = true;
16468
0
    }
16469
0
        else if (stub_entry->type.main == ppc_stub_plt_call
16470
0
           && !htab->opd_abi
16471
0
           && htab->params->plt_localentry0 != 0
16472
0
           && h != NULL
16473
0
           && is_elfv2_localentry0 (&h->elf))
16474
0
    {
16475
      /* The function doesn't use or change r2.  */
16476
0
      can_plt_call = true;
16477
0
    }
16478
16479
        /* All of these stubs may modify r2, so there must be a
16480
     branch and link followed by a nop.  The nop is
16481
     replaced by an insn to restore r2.  */
16482
0
        else if (offset_in_range (input_section, rel->r_offset, 8))
16483
0
    {
16484
0
      unsigned long br;
16485
16486
0
      br = bfd_get_32 (input_bfd,
16487
0
           contents + rel->r_offset);
16488
0
      if ((br & 1) != 0)
16489
0
        {
16490
0
          unsigned long nop;
16491
16492
0
          nop = bfd_get_32 (input_bfd,
16493
0
          contents + rel->r_offset + 4);
16494
0
          if (nop == LD_R2_0R1 + STK_TOC (htab))
16495
0
      can_plt_call = true;
16496
0
          else if (nop == NOP
16497
0
             || nop == CROR_151515
16498
0
             || nop == CROR_313131)
16499
0
      {
16500
0
        if (h != NULL
16501
0
            && is_tls_get_addr (&h->elf, htab)
16502
0
            && htab->params->tls_get_addr_opt)
16503
0
          {
16504
            /* Special stub used, leave nop alone.  */
16505
0
          }
16506
0
        else
16507
0
          bfd_put_32 (input_bfd,
16508
0
          LD_R2_0R1 + STK_TOC (htab),
16509
0
          contents + rel->r_offset + 4);
16510
0
        can_plt_call = true;
16511
0
      }
16512
0
        }
16513
0
    }
16514
16515
0
        if (!can_plt_call && h != NULL)
16516
0
    {
16517
0
      const char *name = h->elf.root.root.string;
16518
16519
0
      if (*name == '.')
16520
0
        ++name;
16521
16522
0
      if (startswith (name, "__libc_start_main")
16523
0
          && (name[17] == 0 || name[17] == '@'))
16524
0
        {
16525
          /* Allow crt1 branch to go via a toc adjusting
16526
       stub.  Other calls that never return could do
16527
       the same, if we could detect such.  */
16528
0
          can_plt_call = true;
16529
0
        }
16530
0
    }
16531
16532
0
        if (!can_plt_call)
16533
0
    {
16534
      /* g++ as of 20130507 emits self-calls without a
16535
         following nop.  This is arguably wrong since we
16536
         have conflicting information.  On the one hand a
16537
         global symbol and on the other a local call
16538
         sequence, but don't error for this special case.
16539
         It isn't possible to cheaply verify we have
16540
         exactly such a call.  Allow all calls to the same
16541
         section.  */
16542
0
      asection *code_sec = sec;
16543
16544
0
      if (get_opd_info (sec) != NULL)
16545
0
        {
16546
0
          bfd_vma off = (relocation + addend
16547
0
             - sec->output_section->vma
16548
0
             - sec->output_offset);
16549
16550
0
          opd_entry_value (sec, off, &code_sec, NULL, false);
16551
0
        }
16552
0
      if (code_sec == input_section)
16553
0
        can_plt_call = true;
16554
0
    }
16555
16556
0
        if (!can_plt_call)
16557
0
    {
16558
0
      if (stub_entry->type.main == ppc_stub_plt_call)
16559
0
        info->callbacks->einfo
16560
          /* xgettext:c-format */
16561
0
          (_("%H: call to `%pT' lacks nop, can't restore toc; "
16562
0
       "(plt call stub)\n"),
16563
0
           input_bfd, input_section, rel->r_offset, sym_name);
16564
0
      else
16565
0
        info->callbacks->einfo
16566
          /* xgettext:c-format */
16567
0
          (_("%H: call to `%pT' lacks nop, can't restore toc; "
16568
0
       "(toc save/adjust stub)\n"),
16569
0
           input_bfd, input_section, rel->r_offset, sym_name);
16570
16571
0
      bfd_set_error (bfd_error_bad_value);
16572
0
      ret = false;
16573
0
    }
16574
16575
0
        if (can_plt_call
16576
0
      && stub_entry->type.main == ppc_stub_plt_call)
16577
0
    unresolved_reloc = false;
16578
0
      }
16579
16580
0
    if ((stub_entry == NULL
16581
0
         || stub_entry->type.main == ppc_stub_long_branch
16582
0
         || stub_entry->type.main == ppc_stub_plt_branch)
16583
0
        && get_opd_info (sec) != NULL)
16584
0
      {
16585
        /* The branch destination is the value of the opd entry. */
16586
0
        bfd_vma off = (relocation + addend
16587
0
           - sec->output_section->vma
16588
0
           - sec->output_offset);
16589
0
        bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
16590
0
        if (dest != (bfd_vma) -1)
16591
0
    {
16592
0
      relocation = dest;
16593
0
      addend = 0;
16594
0
      reloc_dest = DEST_OPD;
16595
0
    }
16596
0
      }
16597
16598
    /* If the branch is out of reach we ought to have a long
16599
       branch stub.  */
16600
0
    from = (rel->r_offset
16601
0
      + input_section->output_offset
16602
0
      + input_section->output_section->vma);
16603
16604
0
    relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16605
0
              ? fdh->elf.other
16606
0
              : sym->st_other);
16607
16608
0
    if (stub_entry != NULL
16609
0
        && (stub_entry->type.main == ppc_stub_long_branch
16610
0
      || stub_entry->type.main == ppc_stub_plt_branch))
16611
0
      {
16612
0
        if (stub_entry->type.sub == ppc_stub_toc
16613
0
      && !stub_entry->type.r2save
16614
0
      && (r_type == R_PPC64_ADDR14_BRTAKEN
16615
0
          || r_type == R_PPC64_ADDR14_BRNTAKEN
16616
0
          || (relocation + addend - from + max_br_offset
16617
0
        < 2 * max_br_offset)))
16618
    /* Don't use the stub if this branch is in range.  */
16619
0
    stub_entry = NULL;
16620
16621
0
        if (stub_entry != NULL
16622
0
      && stub_entry->type.sub >= ppc_stub_notoc
16623
0
      && ((r_type != R_PPC64_REL24_NOTOC
16624
0
           && r_type != R_PPC64_REL24_P9NOTOC)
16625
0
          || ((fdh ? fdh->elf.other : sym->st_other)
16626
0
        & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16627
0
      && (relocation + addend - from + max_br_offset
16628
0
          < 2 * max_br_offset))
16629
0
    stub_entry = NULL;
16630
16631
0
        if (stub_entry != NULL
16632
0
      && stub_entry->type.r2save
16633
0
      && (r_type == R_PPC64_REL24_NOTOC
16634
0
          || r_type == R_PPC64_REL24_P9NOTOC)
16635
0
      && (relocation + addend - from + max_br_offset
16636
0
          < 2 * max_br_offset))
16637
0
    stub_entry = NULL;
16638
0
      }
16639
16640
0
    if (stub_entry != NULL)
16641
0
      {
16642
        /* Munge up the value and addend so that we call the stub
16643
     rather than the procedure directly.  */
16644
0
        asection *stub_sec = stub_entry->group->stub_sec;
16645
16646
0
        if (stub_entry->type.main == ppc_stub_save_res)
16647
0
    relocation += (stub_sec->output_offset
16648
0
             + stub_sec->output_section->vma
16649
0
             + stub_sec->size - htab->sfpr->size
16650
0
             - htab->sfpr->output_offset
16651
0
             - htab->sfpr->output_section->vma);
16652
0
        else
16653
0
    relocation = (stub_entry->stub_offset
16654
0
            + stub_sec->output_offset
16655
0
            + stub_sec->output_section->vma);
16656
0
        addend = 0;
16657
0
        reloc_dest = DEST_STUB;
16658
16659
0
        if (((stub_entry->type.r2save
16660
0
        && (r_type == R_PPC64_REL24_NOTOC
16661
0
      || r_type == R_PPC64_REL24_P9NOTOC))
16662
0
       || ((stub_entry->type.main == ppc_stub_plt_call
16663
0
      && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16664
0
           && rel + 1 < relend
16665
0
           && rel[1].r_offset == rel->r_offset + 4
16666
0
           && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16667
0
      && !(stub_entry->type.main == ppc_stub_plt_call
16668
0
           && htab->params->tls_get_addr_opt
16669
0
           && h != NULL
16670
0
           && is_tls_get_addr (&h->elf, htab)))
16671
0
    {
16672
      /* Skip over the r2 store at the start of the stub.  */
16673
0
      relocation += 4;
16674
0
    }
16675
16676
0
        if ((r_type == R_PPC64_REL24_NOTOC
16677
0
       || r_type == R_PPC64_REL24_P9NOTOC)
16678
0
      && stub_entry->type.main == ppc_stub_plt_call
16679
0
      && stub_entry->type.sub >= ppc_stub_notoc)
16680
0
    htab->notoc_plt = 1;
16681
0
      }
16682
16683
0
    if (insn != 0)
16684
0
      {
16685
0
        if (is_isa_v2)
16686
0
    {
16687
      /* Set 'a' bit.  This is 0b00010 in BO field for branch
16688
         on CR(BI) insns (BO == 001at or 011at), and 0b01000
16689
         for branch on CTR insns (BO == 1a00t or 1a01t).  */
16690
0
      if ((insn & (0x14 << 21)) == (0x04 << 21))
16691
0
        insn |= 0x02 << 21;
16692
0
      else if ((insn & (0x14 << 21)) == (0x10 << 21))
16693
0
        insn |= 0x08 << 21;
16694
0
      else
16695
0
        break;
16696
0
    }
16697
0
        else
16698
0
    {
16699
      /* Invert 'y' bit if not the default.  */
16700
0
      if ((bfd_signed_vma) (relocation + addend - from) < 0)
16701
0
        insn ^= 0x01 << 21;
16702
0
    }
16703
16704
0
        bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16705
0
      }
16706
16707
    /* NOP out calls to undefined weak functions.
16708
       We can thus call a weak function without first
16709
       checking whether the function is defined.  */
16710
0
    else if (h != NULL
16711
0
       && h->elf.root.type == bfd_link_hash_undefweak
16712
0
       && h->elf.dynindx == -1
16713
0
       && (r_type == R_PPC64_REL24
16714
0
           || r_type == R_PPC64_REL24_NOTOC
16715
0
           || r_type == R_PPC64_REL24_P9NOTOC)
16716
0
       && relocation == 0
16717
0
       && addend == 0
16718
0
       && offset_in_range (input_section, rel->r_offset, 4))
16719
0
      {
16720
0
        bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
16721
0
        goto copy_reloc;
16722
0
      }
16723
0
    break;
16724
16725
0
  case R_PPC64_GOT16_DS:
16726
0
    if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16727
0
        || (bfd_link_pic (info)
16728
0
      && sec == bfd_abs_section_ptr)
16729
0
        || !htab->do_toc_opt)
16730
0
      break;
16731
0
    from = TOCstart + htab->sec_info[input_section->id].toc_off;
16732
0
    if (relocation + addend - from + 0x8000 < 0x10000
16733
0
        && sec != NULL
16734
0
        && sec->output_section != NULL
16735
0
        && !discarded_section (sec)
16736
0
        && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16737
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
16738
0
      {
16739
0
        insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16740
0
        if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16741
0
    {
16742
0
      insn += (14u << 26) - (58u << 26);
16743
0
      bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16744
0
      r_type = R_PPC64_TOC16;
16745
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16746
0
    }
16747
0
      }
16748
0
    break;
16749
16750
0
  case R_PPC64_GOT16_LO_DS:
16751
0
  case R_PPC64_GOT16_HA:
16752
0
    if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16753
0
        || (bfd_link_pic (info)
16754
0
      && sec == bfd_abs_section_ptr)
16755
0
        || !htab->do_toc_opt)
16756
0
      break;
16757
0
    from = TOCstart + htab->sec_info[input_section->id].toc_off;
16758
0
    if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
16759
0
        && sec != NULL
16760
0
        && sec->output_section != NULL
16761
0
        && !discarded_section (sec)
16762
0
        && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16763
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
16764
0
      {
16765
0
        insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16766
0
        if (r_type == R_PPC64_GOT16_LO_DS
16767
0
      && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16768
0
    {
16769
0
      insn += (14u << 26) - (58u << 26);
16770
0
      bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16771
0
      r_type = R_PPC64_TOC16_LO;
16772
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16773
0
    }
16774
0
        else if (r_type == R_PPC64_GOT16_HA
16775
0
           && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
16776
0
    {
16777
0
      r_type = R_PPC64_TOC16_HA;
16778
0
      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16779
0
    }
16780
0
      }
16781
0
    break;
16782
16783
0
  case R_PPC64_GOT_PCREL34:
16784
0
    if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16785
0
        || (bfd_link_pic (info)
16786
0
      && sec == bfd_abs_section_ptr)
16787
0
        || !htab->do_toc_opt)
16788
0
      break;
16789
0
    from = (rel->r_offset
16790
0
      + input_section->output_section->vma
16791
0
      + input_section->output_offset);
16792
0
    if (!(relocation - from + (1ULL << 33) < 1ULL << 34
16793
0
    && sec != NULL
16794
0
    && sec->output_section != NULL
16795
0
    && !discarded_section (sec)
16796
0
    && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16797
0
    && offset_in_range (input_section, rel->r_offset, 8)))
16798
0
      break;
16799
16800
0
    offset = rel->r_offset;
16801
0
    pinsn = bfd_get_32 (input_bfd, contents + offset);
16802
0
    pinsn <<= 32;
16803
0
    pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16804
0
    if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16805
0
        != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16806
0
      break;
16807
16808
    /* Replace with paddi.  */
16809
0
    pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16810
0
    r_type = R_PPC64_PCREL34;
16811
0
    rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16812
0
    bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16813
0
    bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16814
    /* Fall through.  */
16815
16816
0
  case R_PPC64_PCREL34:
16817
0
    if (!htab->params->no_pcrel_opt
16818
0
        && rel + 1 < relend
16819
0
        && rel[1].r_offset == rel->r_offset
16820
0
        && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
16821
0
        && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16822
0
        && offset_in_range (input_section, rel->r_offset, 8))
16823
0
      {
16824
0
        offset = rel->r_offset;
16825
0
        pinsn = bfd_get_32 (input_bfd, contents + offset);
16826
0
        pinsn <<= 32;
16827
0
        pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16828
0
        if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16829
0
       == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16830
0
           | (14ULL << 26) /* paddi */))
16831
0
    {
16832
0
      bfd_vma off2 = rel[1].r_addend;
16833
0
      if (off2 == 0)
16834
        /* zero means next insn.  */
16835
0
        off2 = 8;
16836
0
      off2 += offset;
16837
0
      if (offset_in_range (input_section, off2, 4))
16838
0
        {
16839
0
          uint64_t pinsn2;
16840
0
          bfd_signed_vma addend_off;
16841
0
          pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16842
0
          pinsn2 <<= 32;
16843
0
          if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16844
0
      {
16845
0
        if (!offset_in_range (input_section, off2, 8))
16846
0
          break;
16847
0
        pinsn2 |= bfd_get_32 (input_bfd,
16848
0
            contents + off2 + 4);
16849
0
      }
16850
0
          if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16851
0
      {
16852
0
        addend += addend_off;
16853
0
        rel->r_addend = addend;
16854
0
        bfd_put_32 (input_bfd, pinsn >> 32,
16855
0
              contents + offset);
16856
0
        bfd_put_32 (input_bfd, pinsn,
16857
0
              contents + offset + 4);
16858
0
        bfd_put_32 (input_bfd, pinsn2 >> 32,
16859
0
              contents + off2);
16860
0
        if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16861
0
          bfd_put_32 (input_bfd, pinsn2,
16862
0
          contents + off2 + 4);
16863
0
      }
16864
0
        }
16865
0
    }
16866
0
      }
16867
0
    break;
16868
0
  }
16869
16870
0
      tls_type = 0;
16871
0
      save_unresolved_reloc = unresolved_reloc;
16872
0
      switch (r_type)
16873
0
  {
16874
0
  default:
16875
0
    if (r_type < ARRAY_SIZE (ppc64_elf_howto_table)
16876
0
        && ppc64_elf_howto_table[r_type] != NULL)
16877
      /* xgettext:c-format */
16878
0
      _bfd_error_handler (_("%pB: %s unsupported"),
16879
0
        input_bfd, ppc64_elf_howto_table[r_type]->name);
16880
0
    else
16881
      /* xgettext:c-format */
16882
0
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
16883
0
        input_bfd, r_type);
16884
16885
0
    bfd_set_error (bfd_error_bad_value);
16886
0
    ret = false;
16887
0
    goto copy_reloc;
16888
16889
0
  case R_PPC64_NONE:
16890
0
  case R_PPC64_TLS:
16891
0
  case R_PPC64_TLSGD:
16892
0
  case R_PPC64_TLSLD:
16893
0
  case R_PPC64_TOCSAVE:
16894
0
  case R_PPC64_GNU_VTINHERIT:
16895
0
  case R_PPC64_GNU_VTENTRY:
16896
0
  case R_PPC64_ENTRY:
16897
0
  case R_PPC64_PCREL_OPT:
16898
0
    goto copy_reloc;
16899
16900
    /* GOT16 relocations.  Like an ADDR16 using the symbol's
16901
       address in the GOT as relocation value instead of the
16902
       symbol's value itself.  Also, create a GOT entry for the
16903
       symbol and put the symbol value there.  */
16904
0
  case R_PPC64_GOT_TLSGD16:
16905
0
  case R_PPC64_GOT_TLSGD16_LO:
16906
0
  case R_PPC64_GOT_TLSGD16_HI:
16907
0
  case R_PPC64_GOT_TLSGD16_HA:
16908
0
  case R_PPC64_GOT_TLSGD_PCREL34:
16909
0
    tls_type = TLS_TLS | TLS_GD;
16910
0
    goto dogot;
16911
16912
0
  case R_PPC64_GOT_TLSLD16:
16913
0
  case R_PPC64_GOT_TLSLD16_LO:
16914
0
  case R_PPC64_GOT_TLSLD16_HI:
16915
0
  case R_PPC64_GOT_TLSLD16_HA:
16916
0
  case R_PPC64_GOT_TLSLD_PCREL34:
16917
0
    tls_type = TLS_TLS | TLS_LD;
16918
0
    goto dogot;
16919
16920
0
  case R_PPC64_GOT_TPREL16_DS:
16921
0
  case R_PPC64_GOT_TPREL16_LO_DS:
16922
0
  case R_PPC64_GOT_TPREL16_HI:
16923
0
  case R_PPC64_GOT_TPREL16_HA:
16924
0
  case R_PPC64_GOT_TPREL_PCREL34:
16925
0
    tls_type = TLS_TLS | TLS_TPREL;
16926
0
    goto dogot;
16927
16928
0
  case R_PPC64_GOT_DTPREL16_DS:
16929
0
  case R_PPC64_GOT_DTPREL16_LO_DS:
16930
0
  case R_PPC64_GOT_DTPREL16_HI:
16931
0
  case R_PPC64_GOT_DTPREL16_HA:
16932
0
  case R_PPC64_GOT_DTPREL_PCREL34:
16933
0
    tls_type = TLS_TLS | TLS_DTPREL;
16934
0
    goto dogot;
16935
16936
0
  case R_PPC64_GOT16:
16937
0
  case R_PPC64_GOT16_LO:
16938
0
  case R_PPC64_GOT16_HI:
16939
0
  case R_PPC64_GOT16_HA:
16940
0
  case R_PPC64_GOT16_DS:
16941
0
  case R_PPC64_GOT16_LO_DS:
16942
0
  case R_PPC64_GOT_PCREL34:
16943
0
  dogot:
16944
0
    {
16945
      /* Relocation is to the entry for this symbol in the global
16946
         offset table.  */
16947
0
      asection *got;
16948
0
      bfd_vma *offp;
16949
0
      bfd_vma off;
16950
0
      unsigned long indx = 0;
16951
0
      struct got_entry *ent;
16952
16953
0
      if (tls_type == (TLS_TLS | TLS_LD)
16954
0
    && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16955
0
        ent = ppc64_tlsld_got (input_bfd);
16956
0
      else
16957
0
        {
16958
0
    if (h != NULL)
16959
0
      {
16960
0
        if (!htab->elf.dynamic_sections_created
16961
0
      || h->elf.dynindx == -1
16962
0
      || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16963
0
      || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16964
          /* This is actually a static link, or it is a
16965
       -Bsymbolic link and the symbol is defined
16966
       locally, or the symbol was forced to be local
16967
       because of a version file.  */
16968
0
          ;
16969
0
        else
16970
0
          {
16971
0
      indx = h->elf.dynindx;
16972
0
      unresolved_reloc = false;
16973
0
          }
16974
0
        ent = h->elf.got.glist;
16975
0
      }
16976
0
    else
16977
0
      {
16978
0
        if (local_got_ents == NULL)
16979
0
          abort ();
16980
0
        ent = local_got_ents[r_symndx];
16981
0
      }
16982
16983
0
    for (; ent != NULL; ent = ent->next)
16984
0
      if (ent->addend == orig_rel.r_addend
16985
0
          && ent->owner == input_bfd
16986
0
          && ent->tls_type == tls_type)
16987
0
        break;
16988
0
        }
16989
16990
0
      if (ent == NULL)
16991
0
        abort ();
16992
0
      if (ent->is_indirect)
16993
0
        ent = ent->got.ent;
16994
0
      offp = &ent->got.offset;
16995
0
      got = ppc64_elf_tdata (ent->owner)->got;
16996
0
      if (got == NULL)
16997
0
        abort ();
16998
16999
      /* The offset must always be a multiple of 8.  We use the
17000
         least significant bit to record whether we have already
17001
         processed this entry.  */
17002
0
      off = *offp;
17003
0
      if ((off & 1) != 0)
17004
0
        off &= ~1;
17005
0
      else
17006
0
        {
17007
    /* Generate relocs for the dynamic linker, except in
17008
       the case of TLSLD where we'll use one entry per
17009
       module.  */
17010
0
    asection *relgot;
17011
0
    bool ifunc;
17012
17013
0
    *offp = off | 1;
17014
0
    relgot = NULL;
17015
0
    ifunc = (h != NULL
17016
0
       ? h->elf.type == STT_GNU_IFUNC
17017
0
       : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
17018
0
    if (ifunc)
17019
0
      {
17020
0
        relgot = htab->elf.irelplt;
17021
0
        if (indx == 0 || is_static_defined (&h->elf))
17022
0
          htab->elf.ifunc_resolvers = true;
17023
0
      }
17024
0
    else if (indx != 0
17025
0
       || (bfd_link_pic (info)
17026
0
           && (h == NULL
17027
0
         || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
17028
0
           && !(tls_type != 0
17029
0
          && bfd_link_executable (info)
17030
0
          && (h == NULL
17031
0
              || SYMBOL_REFERENCES_LOCAL (info,
17032
0
                  &h->elf)))
17033
0
           && (h != NULL
17034
0
         ? !bfd_is_abs_symbol (&h->elf.root)
17035
0
         : sym->st_shndx != SHN_ABS)))
17036
17037
0
      relgot = ppc64_elf_tdata (ent->owner)->relgot;
17038
0
    if (relgot != NULL)
17039
0
      {
17040
0
        outrel.r_offset = (got->output_section->vma
17041
0
               + got->output_offset
17042
0
               + off);
17043
0
        outrel.r_addend = orig_rel.r_addend;
17044
0
        if (tls_type & (TLS_LD | TLS_GD))
17045
0
          {
17046
0
      outrel.r_addend = 0;
17047
0
      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
17048
0
      if (tls_type == (TLS_TLS | TLS_GD))
17049
0
        {
17050
0
          BFD_ASSERT (count_and_swap_reloc_out (output_bfd,
17051
0
                  &outrel,
17052
0
                  relgot));
17053
0
          outrel.r_offset += 8;
17054
0
          outrel.r_addend = orig_rel.r_addend;
17055
0
          outrel.r_info
17056
0
            = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
17057
0
        }
17058
0
          }
17059
0
        else if (tls_type == (TLS_TLS | TLS_DTPREL))
17060
0
          outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
17061
0
        else if (tls_type == (TLS_TLS | TLS_TPREL))
17062
0
          outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
17063
0
        else if (indx != 0)
17064
0
          outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
17065
0
        else
17066
0
          {
17067
0
      if (ifunc)
17068
0
        outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17069
0
      else
17070
0
        outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17071
17072
      /* Write the .got section contents for the sake
17073
         of prelink.  */
17074
0
      loc = got->contents + off;
17075
0
      bfd_put_64 (output_bfd, outrel.r_addend + relocation,
17076
0
            loc);
17077
0
          }
17078
17079
0
        if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
17080
0
          {
17081
0
      outrel.r_addend += relocation;
17082
0
      if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
17083
0
        {
17084
0
          if (htab->elf.tls_sec == NULL)
17085
0
            outrel.r_addend = 0;
17086
0
          else
17087
0
            outrel.r_addend -= htab->elf.tls_sec->vma;
17088
0
        }
17089
0
          }
17090
0
        if (!(info->enable_dt_relr
17091
0
        && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
17092
0
          BFD_ASSERT (count_and_swap_reloc_out (output_bfd,
17093
0
                  &outrel, relgot));
17094
0
      }
17095
17096
    /* Init the .got section contents here if we're not
17097
       emitting a reloc.  */
17098
0
    else
17099
0
      {
17100
0
        relocation += orig_rel.r_addend;
17101
0
        if (tls_type != 0)
17102
0
          {
17103
0
      if (htab->elf.tls_sec == NULL)
17104
0
        relocation = 0;
17105
0
      else
17106
0
        {
17107
0
          if (tls_type & TLS_LD)
17108
0
            relocation = 0;
17109
0
          else
17110
0
            relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
17111
0
          if (tls_type & TLS_TPREL)
17112
0
            relocation += DTP_OFFSET - TP_OFFSET;
17113
0
        }
17114
17115
0
      if (tls_type & (TLS_GD | TLS_LD))
17116
0
        {
17117
0
          bfd_put_64 (output_bfd, relocation,
17118
0
          got->contents + off + 8);
17119
0
          relocation = 1;
17120
0
        }
17121
0
          }
17122
0
        bfd_put_64 (output_bfd, relocation,
17123
0
        got->contents + off);
17124
0
      }
17125
0
        }
17126
17127
0
      if (off >= (bfd_vma) -2)
17128
0
        abort ();
17129
17130
0
      relocation = got->output_section->vma + got->output_offset + off;
17131
0
      addend = 0;
17132
0
      if (!(r_type == R_PPC64_GOT_PCREL34
17133
0
      || r_type == R_PPC64_GOT_TLSGD_PCREL34
17134
0
      || r_type == R_PPC64_GOT_TLSLD_PCREL34
17135
0
      || r_type == R_PPC64_GOT_TPREL_PCREL34
17136
0
      || r_type == R_PPC64_GOT_DTPREL_PCREL34))
17137
0
        addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
17138
0
    }
17139
0
    break;
17140
17141
0
  case R_PPC64_PLT16_HA:
17142
0
  case R_PPC64_PLT16_HI:
17143
0
  case R_PPC64_PLT16_LO:
17144
0
  case R_PPC64_PLT16_LO_DS:
17145
0
  case R_PPC64_PLT_PCREL34:
17146
0
  case R_PPC64_PLT_PCREL34_NOTOC:
17147
0
  case R_PPC64_PLT32:
17148
0
  case R_PPC64_PLT64:
17149
0
  case R_PPC64_PLTSEQ:
17150
0
  case R_PPC64_PLTSEQ_NOTOC:
17151
0
  case R_PPC64_PLTCALL:
17152
0
  case R_PPC64_PLTCALL_NOTOC:
17153
    /* Relocation is to the entry for this symbol in the
17154
       procedure linkage table.  */
17155
0
    unresolved_reloc = true;
17156
0
    {
17157
0
      struct plt_entry **plt_list = NULL;
17158
0
      if (h != NULL)
17159
0
        plt_list = &h->elf.plt.plist;
17160
0
      else if (local_got_ents != NULL)
17161
0
        {
17162
0
    struct plt_entry **local_plt = (struct plt_entry **)
17163
0
      (local_got_ents + symtab_hdr->sh_info);
17164
0
    plt_list = local_plt + r_symndx;
17165
0
        }
17166
0
      if (plt_list)
17167
0
        {
17168
0
    struct plt_entry *ent;
17169
17170
0
    for (ent = *plt_list; ent != NULL; ent = ent->next)
17171
0
      if (ent->plt.offset != (bfd_vma) -1
17172
0
          && ent->addend == orig_rel.r_addend)
17173
0
        {
17174
0
          asection *plt;
17175
0
          bfd_vma got;
17176
17177
0
          plt = htab->elf.splt;
17178
0
          if (use_local_plt (info, elf_hash_entry (h)))
17179
0
      {
17180
0
        if (h != NULL
17181
0
            ? h->elf.type == STT_GNU_IFUNC
17182
0
            : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17183
0
          plt = htab->elf.iplt;
17184
0
        else
17185
0
          plt = htab->pltlocal;
17186
0
      }
17187
0
          relocation = (plt->output_section->vma
17188
0
            + plt->output_offset
17189
0
            + ent->plt.offset);
17190
0
          if (r_type == R_PPC64_PLT16_HA
17191
0
        || r_type == R_PPC64_PLT16_HI
17192
0
        || r_type == R_PPC64_PLT16_LO
17193
0
        || r_type == R_PPC64_PLT16_LO_DS)
17194
0
      {
17195
0
        got = (elf_gp (output_bfd)
17196
0
         + htab->sec_info[input_section->id].toc_off);
17197
0
        relocation -= got;
17198
0
      }
17199
0
          addend = 0;
17200
0
          unresolved_reloc = false;
17201
0
          break;
17202
0
        }
17203
0
        }
17204
0
    }
17205
0
    break;
17206
17207
0
  case R_PPC64_TOC:
17208
    /* Relocation value is TOC base.  */
17209
0
    relocation = TOCstart;
17210
0
    if (r_symndx == STN_UNDEF)
17211
0
      relocation += htab->sec_info[input_section->id].toc_off;
17212
0
    else if (unresolved_reloc)
17213
0
      ;
17214
0
    else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17215
0
      relocation += htab->sec_info[sec->id].toc_off;
17216
0
    else
17217
0
      unresolved_reloc = true;
17218
0
    if (unresolved_reloc
17219
0
        || (!is_opd
17220
0
      && h != NULL
17221
0
      && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
17222
0
      info->callbacks->einfo
17223
        /* xgettext:c-format */
17224
0
        (_("%H: %s against %pT is not supported\n"),
17225
0
         input_bfd, input_section, rel->r_offset,
17226
0
         ppc64_elf_howto_table[r_type]->name, sym_name);
17227
0
    goto dodyn;
17228
17229
    /* TOC16 relocs.  We want the offset relative to the TOC base,
17230
       which is the address of the start of the TOC plus 0x8000.
17231
       The TOC consists of sections .got, .toc, .tocbss, and .plt,
17232
       in this order.  */
17233
0
  case R_PPC64_TOC16:
17234
0
  case R_PPC64_TOC16_LO:
17235
0
  case R_PPC64_TOC16_HI:
17236
0
  case R_PPC64_TOC16_DS:
17237
0
  case R_PPC64_TOC16_LO_DS:
17238
0
  case R_PPC64_TOC16_HA:
17239
0
    addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
17240
0
    if (h != NULL)
17241
0
      goto dodyn;
17242
0
    break;
17243
17244
    /* Relocate against the beginning of the section.  */
17245
0
  case R_PPC64_SECTOFF:
17246
0
  case R_PPC64_SECTOFF_LO:
17247
0
  case R_PPC64_SECTOFF_HI:
17248
0
  case R_PPC64_SECTOFF_DS:
17249
0
  case R_PPC64_SECTOFF_LO_DS:
17250
0
  case R_PPC64_SECTOFF_HA:
17251
0
    if (sec != NULL)
17252
0
      addend -= sec->output_section->vma;
17253
0
    break;
17254
17255
0
  case R_PPC64_REL16:
17256
0
  case R_PPC64_REL16_LO:
17257
0
  case R_PPC64_REL16_HI:
17258
0
  case R_PPC64_REL16_HA:
17259
0
  case R_PPC64_REL16_HIGH:
17260
0
  case R_PPC64_REL16_HIGHA:
17261
0
  case R_PPC64_REL16_HIGHER:
17262
0
  case R_PPC64_REL16_HIGHERA:
17263
0
  case R_PPC64_REL16_HIGHEST:
17264
0
  case R_PPC64_REL16_HIGHESTA:
17265
0
  case R_PPC64_REL16_HIGHER34:
17266
0
  case R_PPC64_REL16_HIGHERA34:
17267
0
  case R_PPC64_REL16_HIGHEST34:
17268
0
  case R_PPC64_REL16_HIGHESTA34:
17269
0
  case R_PPC64_REL16DX_HA:
17270
0
  case R_PPC64_REL14:
17271
0
  case R_PPC64_REL14_BRNTAKEN:
17272
0
  case R_PPC64_REL14_BRTAKEN:
17273
0
  case R_PPC64_REL24:
17274
0
  case R_PPC64_REL24_NOTOC:
17275
0
  case R_PPC64_REL24_P9NOTOC:
17276
0
  case R_PPC64_PCREL34:
17277
0
  case R_PPC64_PCREL28:
17278
0
    break;
17279
17280
0
  case R_PPC64_TPREL16:
17281
0
  case R_PPC64_TPREL16_LO:
17282
0
  case R_PPC64_TPREL16_HI:
17283
0
  case R_PPC64_TPREL16_HA:
17284
0
  case R_PPC64_TPREL16_DS:
17285
0
  case R_PPC64_TPREL16_LO_DS:
17286
0
  case R_PPC64_TPREL16_HIGH:
17287
0
  case R_PPC64_TPREL16_HIGHA:
17288
0
  case R_PPC64_TPREL16_HIGHER:
17289
0
  case R_PPC64_TPREL16_HIGHERA:
17290
0
  case R_PPC64_TPREL16_HIGHEST:
17291
0
  case R_PPC64_TPREL16_HIGHESTA:
17292
0
    if (h != NULL
17293
0
        && h->elf.root.type == bfd_link_hash_undefweak
17294
0
        && h->elf.dynindx == -1
17295
0
        && offset_in_range (input_section, rel->r_offset - d_offset, 4))
17296
0
      {
17297
        /* Make this relocation against an undefined weak symbol
17298
     resolve to zero.  This is really just a tweak, since
17299
     code using weak externs ought to check that they are
17300
     defined before using them.  */
17301
0
        bfd_byte *p = contents + rel->r_offset - d_offset;
17302
17303
0
        insn = bfd_get_32 (input_bfd, p);
17304
0
        insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17305
0
        if (insn != 0)
17306
0
    bfd_put_32 (input_bfd, insn, p);
17307
0
        break;
17308
0
      }
17309
    /* Fall through.  */
17310
17311
0
  case R_PPC64_TPREL34:
17312
0
    if (htab->elf.tls_sec != NULL)
17313
0
      addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17314
    /* The TPREL16 relocs shouldn't really be used in shared
17315
       libs or with non-local symbols as that will result in
17316
       DT_TEXTREL being set, but support them anyway.  */
17317
0
    goto dodyn;
17318
17319
0
  case R_PPC64_DTPREL16:
17320
0
  case R_PPC64_DTPREL16_LO:
17321
0
  case R_PPC64_DTPREL16_HI:
17322
0
  case R_PPC64_DTPREL16_HA:
17323
0
  case R_PPC64_DTPREL16_DS:
17324
0
  case R_PPC64_DTPREL16_LO_DS:
17325
0
  case R_PPC64_DTPREL16_HIGH:
17326
0
  case R_PPC64_DTPREL16_HIGHA:
17327
0
  case R_PPC64_DTPREL16_HIGHER:
17328
0
  case R_PPC64_DTPREL16_HIGHERA:
17329
0
  case R_PPC64_DTPREL16_HIGHEST:
17330
0
  case R_PPC64_DTPREL16_HIGHESTA:
17331
0
  case R_PPC64_DTPREL34:
17332
0
    if (htab->elf.tls_sec != NULL)
17333
0
      addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17334
0
    break;
17335
17336
0
  case R_PPC64_ADDR64_LOCAL:
17337
0
    addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17338
0
                ? h->elf.other
17339
0
                : sym->st_other);
17340
0
    break;
17341
17342
0
  case R_PPC64_DTPMOD64:
17343
0
    relocation = 1;
17344
0
    addend = 0;
17345
0
    goto dodyn;
17346
17347
0
  case R_PPC64_TPREL64:
17348
0
    if (htab->elf.tls_sec != NULL)
17349
0
      addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17350
0
    goto dodyn;
17351
17352
0
  case R_PPC64_DTPREL64:
17353
0
    if (htab->elf.tls_sec != NULL)
17354
0
      addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17355
    /* Fall through.  */
17356
17357
    /* Relocations that may need to be propagated if this is a
17358
       dynamic object.  */
17359
0
  case R_PPC64_REL30:
17360
0
  case R_PPC64_REL32:
17361
0
  case R_PPC64_REL64:
17362
0
  case R_PPC64_ADDR14:
17363
0
  case R_PPC64_ADDR14_BRNTAKEN:
17364
0
  case R_PPC64_ADDR14_BRTAKEN:
17365
0
  case R_PPC64_ADDR16:
17366
0
  case R_PPC64_ADDR16_DS:
17367
0
  case R_PPC64_ADDR16_HA:
17368
0
  case R_PPC64_ADDR16_HI:
17369
0
  case R_PPC64_ADDR16_HIGH:
17370
0
  case R_PPC64_ADDR16_HIGHA:
17371
0
  case R_PPC64_ADDR16_HIGHER:
17372
0
  case R_PPC64_ADDR16_HIGHERA:
17373
0
  case R_PPC64_ADDR16_HIGHEST:
17374
0
  case R_PPC64_ADDR16_HIGHESTA:
17375
0
  case R_PPC64_ADDR16_LO:
17376
0
  case R_PPC64_ADDR16_LO_DS:
17377
0
  case R_PPC64_ADDR16_HIGHER34:
17378
0
  case R_PPC64_ADDR16_HIGHERA34:
17379
0
  case R_PPC64_ADDR16_HIGHEST34:
17380
0
  case R_PPC64_ADDR16_HIGHESTA34:
17381
0
  case R_PPC64_ADDR24:
17382
0
  case R_PPC64_ADDR32:
17383
0
  case R_PPC64_ADDR64:
17384
0
  case R_PPC64_UADDR16:
17385
0
  case R_PPC64_UADDR32:
17386
0
  case R_PPC64_UADDR64:
17387
0
  case R_PPC64_D34:
17388
0
  case R_PPC64_D34_LO:
17389
0
  case R_PPC64_D34_HI30:
17390
0
  case R_PPC64_D34_HA30:
17391
0
  case R_PPC64_D28:
17392
0
  dodyn:
17393
0
    if ((input_section->flags & SEC_ALLOC) == 0)
17394
0
      break;
17395
17396
0
    if (NO_OPD_RELOCS && is_opd)
17397
0
      break;
17398
17399
0
    if (bfd_link_pic (info)
17400
0
        ? ((h == NULL
17401
0
      || h->elf.dyn_relocs != NULL)
17402
0
     && ((h != NULL && pc_dynrelocs (h))
17403
0
         || must_be_dyn_reloc (info, r_type)))
17404
0
        : (h != NULL
17405
0
     ? h->elf.dyn_relocs != NULL
17406
0
     : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17407
0
      {
17408
0
        bool skip, relocate;
17409
0
        asection *sreloc;
17410
0
        bfd_vma out_off;
17411
0
        long indx = 0;
17412
17413
        /* When generating a dynamic object, these relocations
17414
     are copied into the output file to be resolved at run
17415
     time.  */
17416
17417
0
        skip = false;
17418
0
        relocate = false;
17419
17420
0
        out_off = _bfd_elf_section_offset (output_bfd, info,
17421
0
             input_section, rel->r_offset);
17422
0
        if (out_off == (bfd_vma) -1)
17423
0
    skip = true;
17424
0
        else if (out_off == (bfd_vma) -2)
17425
0
    skip = true, relocate = true;
17426
0
        out_off += (input_section->output_section->vma
17427
0
        + input_section->output_offset);
17428
0
        outrel.r_offset = out_off;
17429
0
        outrel.r_addend = rel->r_addend;
17430
17431
        /* Optimize unaligned reloc use.  */
17432
0
        if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17433
0
      || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17434
0
    r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17435
0
        else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17436
0
           || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17437
0
    r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17438
0
        else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17439
0
           || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17440
0
    r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17441
17442
0
        if (skip)
17443
0
    memset (&outrel, 0, sizeof outrel);
17444
0
        else if (h != NULL
17445
0
           && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
17446
0
           && !is_opd
17447
0
           && r_type != R_PPC64_TOC)
17448
0
    {
17449
0
      indx = h->elf.dynindx;
17450
0
      BFD_ASSERT (indx != -1);
17451
0
      outrel.r_info = ELF64_R_INFO (indx, r_type);
17452
0
    }
17453
0
        else
17454
0
    {
17455
      /* This symbol is local, or marked to become local,
17456
         or this is an opd section reloc which must point
17457
         at a local function.  */
17458
0
      outrel.r_addend += relocation;
17459
0
      if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
17460
0
        {
17461
0
          if (is_opd && h != NULL)
17462
0
      {
17463
        /* Lie about opd entries.  This case occurs
17464
           when building shared libraries and we
17465
           reference a function in another shared
17466
           lib.  The same thing happens for a weak
17467
           definition in an application that's
17468
           overridden by a strong definition in a
17469
           shared lib.  (I believe this is a generic
17470
           bug in binutils handling of weak syms.)
17471
           In these cases we won't use the opd
17472
           entry in this lib.  */
17473
0
        unresolved_reloc = false;
17474
0
      }
17475
0
          if (!is_opd
17476
0
        && r_type == R_PPC64_ADDR64
17477
0
        && (h != NULL
17478
0
            ? h->elf.type == STT_GNU_IFUNC
17479
0
            : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17480
0
      outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17481
0
          else
17482
0
      {
17483
0
        outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17484
17485
        /* We need to relocate .opd contents for ld.so.
17486
           Prelink also wants simple and consistent rules
17487
           for relocs.  This make all RELATIVE relocs have
17488
           *r_offset equal to r_addend.  */
17489
0
        relocate = true;
17490
0
      }
17491
0
        }
17492
0
      else
17493
0
        {
17494
0
          if (h != NULL
17495
0
        ? h->elf.type == STT_GNU_IFUNC
17496
0
        : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17497
0
      {
17498
0
        info->callbacks->einfo
17499
          /* xgettext:c-format */
17500
0
          (_("%H: %s for indirect "
17501
0
             "function `%pT' unsupported\n"),
17502
0
           input_bfd, input_section, rel->r_offset,
17503
0
           ppc64_elf_howto_table[r_type]->name,
17504
0
           sym_name);
17505
0
        ret = false;
17506
0
      }
17507
0
          else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
17508
0
      ;
17509
0
          else if (sec == NULL || sec->owner == NULL)
17510
0
      {
17511
0
        bfd_set_error (bfd_error_bad_value);
17512
0
        return false;
17513
0
      }
17514
0
          else
17515
0
      {
17516
0
        asection *osec = sec->output_section;
17517
17518
0
        if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17519
0
          {
17520
            /* TLS symbol values are relative to the
17521
         TLS segment.  Dynamic relocations for
17522
         local TLS symbols therefore can't be
17523
         reduced to a relocation against their
17524
         section symbol because it holds the
17525
         address of the section, not a value
17526
         relative to the TLS segment.  We could
17527
         change the .tdata dynamic section symbol
17528
         to be zero value but STN_UNDEF works
17529
         and is used elsewhere, eg. for TPREL64
17530
         GOT relocs against local TLS symbols.  */
17531
0
            osec = htab->elf.tls_sec;
17532
0
            indx = 0;
17533
0
          }
17534
0
        else
17535
0
          {
17536
0
            indx = elf_section_data (osec)->dynindx;
17537
0
            if (indx == 0)
17538
0
        {
17539
0
          if ((osec->flags & SEC_READONLY) == 0
17540
0
              && htab->elf.data_index_section != NULL)
17541
0
            osec = htab->elf.data_index_section;
17542
0
          else
17543
0
            osec = htab->elf.text_index_section;
17544
0
          indx = elf_section_data (osec)->dynindx;
17545
0
        }
17546
0
            BFD_ASSERT (indx != 0);
17547
0
          }
17548
17549
        /* We are turning this relocation into one
17550
           against a section symbol, so subtract out
17551
           the output section's address but not the
17552
           offset of the input section in the output
17553
           section.  */
17554
0
        outrel.r_addend -= osec->vma;
17555
0
      }
17556
17557
0
          outrel.r_info = ELF64_R_INFO (indx, r_type);
17558
0
        }
17559
0
    }
17560
17561
0
        if (!(info->enable_dt_relr
17562
0
        && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
17563
0
        && maybe_relr (ELF64_R_TYPE (orig_rel.r_info),
17564
0
           rel, input_section)))
17565
0
    {
17566
0
      sreloc = elf_section_data (input_section)->sreloc;
17567
0
      if (h != NULL
17568
0
          ? h->elf.type == STT_GNU_IFUNC
17569
0
          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17570
0
        {
17571
0
          sreloc = htab->elf.irelplt;
17572
0
          if (indx == 0 || is_static_defined (&h->elf))
17573
0
      htab->elf.ifunc_resolvers = true;
17574
0
        }
17575
0
      if (sreloc == NULL)
17576
0
        abort ();
17577
17578
0
      BFD_ASSERT (count_and_swap_reloc_out (output_bfd, &outrel,
17579
0
              sreloc));
17580
0
    }
17581
17582
0
        if (!warned_dynamic
17583
0
      && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17584
0
    {
17585
0
      info->callbacks->einfo
17586
        /* xgettext:c-format */
17587
0
        (_("%X%P: %pB: %s against %pT "
17588
0
           "is not supported by glibc as a dynamic relocation\n"),
17589
0
         input_bfd,
17590
0
         ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17591
0
         sym_name);
17592
0
      warned_dynamic = true;
17593
0
    }
17594
17595
        /* If this reloc is against an external symbol, it will
17596
     be computed at runtime, so there's no need to do
17597
     anything now.  However, for the sake of prelink ensure
17598
     that the section contents are a known value.  */
17599
0
        if (!relocate)
17600
0
    {
17601
0
      unresolved_reloc = false;
17602
      /* The value chosen here is quite arbitrary as ld.so
17603
         ignores section contents except for the special
17604
         case of .opd where the contents might be accessed
17605
         before relocation.  Choose zero, as that won't
17606
         cause reloc overflow.  */
17607
0
      relocation = 0;
17608
0
      addend = 0;
17609
      /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17610
         to improve backward compatibility with older
17611
         versions of ld.  */
17612
0
      if (r_type == R_PPC64_ADDR64)
17613
0
        addend = outrel.r_addend;
17614
      /* Adjust pc_relative relocs to have zero in *r_offset.  */
17615
0
      else if (ppc64_elf_howto_table[r_type]->pc_relative)
17616
0
        addend = outrel.r_offset;
17617
0
    }
17618
0
      }
17619
0
    break;
17620
17621
0
  case R_PPC64_COPY:
17622
0
  case R_PPC64_GLOB_DAT:
17623
0
  case R_PPC64_JMP_SLOT:
17624
0
  case R_PPC64_JMP_IREL:
17625
0
  case R_PPC64_RELATIVE:
17626
    /* We shouldn't ever see these dynamic relocs in relocatable
17627
       files.  */
17628
    /* Fall through.  */
17629
17630
0
  case R_PPC64_PLTGOT16:
17631
0
  case R_PPC64_PLTGOT16_DS:
17632
0
  case R_PPC64_PLTGOT16_HA:
17633
0
  case R_PPC64_PLTGOT16_HI:
17634
0
  case R_PPC64_PLTGOT16_LO:
17635
0
  case R_PPC64_PLTGOT16_LO_DS:
17636
0
  case R_PPC64_PLTREL32:
17637
0
  case R_PPC64_PLTREL64:
17638
    /* These ones haven't been implemented yet.  */
17639
17640
0
    info->callbacks->einfo
17641
      /* xgettext:c-format */
17642
0
      (_("%P: %pB: %s is not supported for `%pT'\n"),
17643
0
       input_bfd,
17644
0
       ppc64_elf_howto_table[r_type]->name, sym_name);
17645
17646
0
    bfd_set_error (bfd_error_invalid_operation);
17647
0
    ret = false;
17648
0
    goto copy_reloc;
17649
0
  }
17650
17651
      /* Multi-instruction sequences that access the TOC can be
17652
   optimized, eg. addis ra,r2,0; addi rb,ra,x;
17653
   to   nop;         addi rb,r2,x;  */
17654
0
      switch (r_type)
17655
0
  {
17656
0
  default:
17657
0
    break;
17658
17659
0
  case R_PPC64_GOT_TLSLD16_HI:
17660
0
  case R_PPC64_GOT_TLSGD16_HI:
17661
0
  case R_PPC64_GOT_TPREL16_HI:
17662
0
  case R_PPC64_GOT_DTPREL16_HI:
17663
0
  case R_PPC64_GOT16_HI:
17664
0
  case R_PPC64_TOC16_HI:
17665
    /* These relocs would only be useful if building up an
17666
       offset to later add to r2, perhaps in an indexed
17667
       addressing mode instruction.  Don't try to optimize.
17668
       Unfortunately, the possibility of someone building up an
17669
       offset like this or even with the HA relocs, means that
17670
       we need to check the high insn when optimizing the low
17671
       insn.  */
17672
0
    break;
17673
17674
0
  case R_PPC64_PLTCALL_NOTOC:
17675
0
    if (!unresolved_reloc)
17676
0
      htab->notoc_plt = 1;
17677
    /* Fall through.  */
17678
0
  case R_PPC64_PLTCALL:
17679
0
    if (unresolved_reloc
17680
0
        && offset_in_range (input_section, rel->r_offset,
17681
0
          r_type == R_PPC64_PLTCALL ? 8 : 4))
17682
0
      {
17683
        /* No plt entry.  Make this into a direct call.  */
17684
0
        bfd_byte *p = contents + rel->r_offset;
17685
0
        insn = bfd_get_32 (input_bfd, p);
17686
0
        insn &= 1;
17687
0
        bfd_put_32 (input_bfd, B_DOT | insn, p);
17688
0
        if (r_type == R_PPC64_PLTCALL)
17689
0
    bfd_put_32 (input_bfd, NOP, p + 4);
17690
0
        unresolved_reloc = save_unresolved_reloc;
17691
0
        r_type = R_PPC64_REL24;
17692
0
      }
17693
0
    break;
17694
17695
0
  case R_PPC64_PLTSEQ_NOTOC:
17696
0
  case R_PPC64_PLTSEQ:
17697
0
    if (unresolved_reloc)
17698
0
      {
17699
0
        unresolved_reloc = false;
17700
0
        goto nop_it;
17701
0
      }
17702
0
    break;
17703
17704
0
  case R_PPC64_PLT_PCREL34_NOTOC:
17705
0
    if (!unresolved_reloc)
17706
0
      htab->notoc_plt = 1;
17707
    /* Fall through.  */
17708
0
  case R_PPC64_PLT_PCREL34:
17709
0
    if (unresolved_reloc
17710
0
        && offset_in_range (input_section, rel->r_offset, 8))
17711
0
      {
17712
0
        bfd_byte *p = contents + rel->r_offset;
17713
0
        bfd_put_32 (input_bfd, PNOP >> 32, p);
17714
0
        bfd_put_32 (input_bfd, PNOP, p + 4);
17715
0
        unresolved_reloc = false;
17716
0
        goto copy_reloc;
17717
0
      }
17718
0
    break;
17719
17720
0
  case R_PPC64_PLT16_HA:
17721
0
    if (unresolved_reloc)
17722
0
      {
17723
0
        unresolved_reloc = false;
17724
0
        goto nop_it;
17725
0
      }
17726
    /* Fall through.  */
17727
0
  case R_PPC64_GOT_TLSLD16_HA:
17728
0
  case R_PPC64_GOT_TLSGD16_HA:
17729
0
  case R_PPC64_GOT_TPREL16_HA:
17730
0
  case R_PPC64_GOT_DTPREL16_HA:
17731
0
  case R_PPC64_GOT16_HA:
17732
0
  case R_PPC64_TOC16_HA:
17733
0
    if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17734
0
        && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17735
0
        && !(bfd_link_pic (info)
17736
0
       && (h != NULL
17737
0
           ? bfd_is_abs_symbol (&h->elf.root)
17738
0
           : sec == bfd_abs_section_ptr)))
17739
0
      {
17740
0
        bfd_byte *p;
17741
0
      nop_it:
17742
0
        if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17743
0
    {
17744
0
      p = contents + (rel->r_offset & ~3);
17745
0
      bfd_put_32 (input_bfd, NOP, p);
17746
0
      goto copy_reloc;
17747
0
    }
17748
0
      }
17749
0
    break;
17750
17751
0
  case R_PPC64_PLT16_LO:
17752
0
  case R_PPC64_PLT16_LO_DS:
17753
0
    if (unresolved_reloc)
17754
0
      {
17755
0
        unresolved_reloc = false;
17756
0
        goto nop_it;
17757
0
      }
17758
    /* Fall through.  */
17759
0
  case R_PPC64_GOT_TLSLD16_LO:
17760
0
  case R_PPC64_GOT_TLSGD16_LO:
17761
0
  case R_PPC64_GOT_TPREL16_LO_DS:
17762
0
  case R_PPC64_GOT_DTPREL16_LO_DS:
17763
0
  case R_PPC64_GOT16_LO:
17764
0
  case R_PPC64_GOT16_LO_DS:
17765
0
  case R_PPC64_TOC16_LO:
17766
0
  case R_PPC64_TOC16_LO_DS:
17767
0
    if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17768
0
        && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17769
0
        && !(bfd_link_pic (info)
17770
0
       && (h != NULL
17771
0
           ? bfd_is_abs_symbol (&h->elf.root)
17772
0
           : sec == bfd_abs_section_ptr))
17773
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
17774
0
      {
17775
0
        bfd_byte *p = contents + (rel->r_offset & ~3);
17776
0
        insn = bfd_get_32 (input_bfd, p);
17777
0
        if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
17778
0
    {
17779
      /* Transform addic to addi when we change reg.  */
17780
0
      insn &= ~((0x3fu << 26) | (0x1f << 16));
17781
0
      insn |= (14u << 26) | (2 << 16);
17782
0
    }
17783
0
        else
17784
0
    {
17785
0
      insn &= ~(0x1f << 16);
17786
0
      insn |= 2 << 16;
17787
0
    }
17788
0
        bfd_put_32 (input_bfd, insn, p);
17789
0
      }
17790
0
    break;
17791
17792
0
  case R_PPC64_TPREL16_HA:
17793
0
    if (htab->do_tls_opt
17794
0
        && relocation + addend + 0x8000 < 0x10000
17795
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
17796
0
      {
17797
0
        bfd_byte *p = contents + (rel->r_offset & ~3);
17798
0
        bfd_put_32 (input_bfd, NOP, p);
17799
0
        goto copy_reloc;
17800
0
      }
17801
0
    break;
17802
17803
0
  case R_PPC64_TPREL16_LO:
17804
0
  case R_PPC64_TPREL16_LO_DS:
17805
0
    if (htab->do_tls_opt
17806
0
        && relocation + addend + 0x8000 < 0x10000
17807
0
        && offset_in_range (input_section, rel->r_offset & ~3, 4))
17808
0
      {
17809
0
        bfd_byte *p = contents + (rel->r_offset & ~3);
17810
0
        insn = bfd_get_32 (input_bfd, p);
17811
0
        insn &= ~(0x1f << 16);
17812
0
        insn |= 13 << 16;
17813
0
        bfd_put_32 (input_bfd, insn, p);
17814
0
      }
17815
0
    break;
17816
0
  }
17817
17818
      /* Do any further special processing.  */
17819
0
      switch (r_type)
17820
0
  {
17821
0
  default:
17822
0
    break;
17823
17824
0
  case R_PPC64_REL16_HA:
17825
0
  case R_PPC64_REL16_HIGHA:
17826
0
  case R_PPC64_REL16_HIGHERA:
17827
0
  case R_PPC64_REL16_HIGHESTA:
17828
0
  case R_PPC64_REL16DX_HA:
17829
0
  case R_PPC64_ADDR16_HA:
17830
0
  case R_PPC64_ADDR16_HIGHA:
17831
0
  case R_PPC64_ADDR16_HIGHERA:
17832
0
  case R_PPC64_ADDR16_HIGHESTA:
17833
0
  case R_PPC64_TOC16_HA:
17834
0
  case R_PPC64_SECTOFF_HA:
17835
0
  case R_PPC64_TPREL16_HA:
17836
0
  case R_PPC64_TPREL16_HIGHA:
17837
0
  case R_PPC64_TPREL16_HIGHERA:
17838
0
  case R_PPC64_TPREL16_HIGHESTA:
17839
0
  case R_PPC64_DTPREL16_HA:
17840
0
  case R_PPC64_DTPREL16_HIGHA:
17841
0
  case R_PPC64_DTPREL16_HIGHERA:
17842
0
  case R_PPC64_DTPREL16_HIGHESTA:
17843
    /* It's just possible that this symbol is a weak symbol
17844
       that's not actually defined anywhere. In that case,
17845
       'sec' would be NULL, and we should leave the symbol
17846
       alone (it will be set to zero elsewhere in the link).  */
17847
0
    if (sec == NULL)
17848
0
      break;
17849
    /* Fall through.  */
17850
17851
0
  case R_PPC64_GOT16_HA:
17852
0
  case R_PPC64_PLTGOT16_HA:
17853
0
  case R_PPC64_PLT16_HA:
17854
0
  case R_PPC64_GOT_TLSGD16_HA:
17855
0
  case R_PPC64_GOT_TLSLD16_HA:
17856
0
  case R_PPC64_GOT_TPREL16_HA:
17857
0
  case R_PPC64_GOT_DTPREL16_HA:
17858
    /* Add 0x10000 if sign bit in 0:15 is set.
17859
       Bits 0:15 are not used.  */
17860
0
    addend += 0x8000;
17861
0
    break;
17862
17863
0
  case R_PPC64_D34_HA30:
17864
0
  case R_PPC64_ADDR16_HIGHERA34:
17865
0
  case R_PPC64_ADDR16_HIGHESTA34:
17866
0
  case R_PPC64_REL16_HIGHERA34:
17867
0
  case R_PPC64_REL16_HIGHESTA34:
17868
0
    if (sec != NULL)
17869
0
      addend += 1ULL << 33;
17870
0
    break;
17871
17872
0
  case R_PPC64_ADDR16_DS:
17873
0
  case R_PPC64_ADDR16_LO_DS:
17874
0
  case R_PPC64_GOT16_DS:
17875
0
  case R_PPC64_GOT16_LO_DS:
17876
0
  case R_PPC64_PLT16_LO_DS:
17877
0
  case R_PPC64_SECTOFF_DS:
17878
0
  case R_PPC64_SECTOFF_LO_DS:
17879
0
  case R_PPC64_TOC16_DS:
17880
0
  case R_PPC64_TOC16_LO_DS:
17881
0
  case R_PPC64_PLTGOT16_DS:
17882
0
  case R_PPC64_PLTGOT16_LO_DS:
17883
0
  case R_PPC64_GOT_TPREL16_DS:
17884
0
  case R_PPC64_GOT_TPREL16_LO_DS:
17885
0
  case R_PPC64_GOT_DTPREL16_DS:
17886
0
  case R_PPC64_GOT_DTPREL16_LO_DS:
17887
0
  case R_PPC64_TPREL16_DS:
17888
0
  case R_PPC64_TPREL16_LO_DS:
17889
0
  case R_PPC64_DTPREL16_DS:
17890
0
  case R_PPC64_DTPREL16_LO_DS:
17891
0
    if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17892
0
      break;
17893
0
    insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17894
0
    mask = 3;
17895
    /* If this reloc is against an lq, lxv, or stxv insn, then
17896
       the value must be a multiple of 16.  This is somewhat of
17897
       a hack, but the "correct" way to do this by defining _DQ
17898
       forms of all the _DS relocs bloats all reloc switches in
17899
       this file.  It doesn't make much sense to use these
17900
       relocs in data, so testing the insn should be safe.  */
17901
0
    if ((insn & (0x3fu << 26)) == (56u << 26)
17902
0
        || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
17903
0
      mask = 15;
17904
0
    relocation += addend;
17905
0
    addend = insn & (mask ^ 3);
17906
0
    if ((relocation & mask) != 0)
17907
0
      {
17908
0
        relocation ^= relocation & mask;
17909
0
        info->callbacks->einfo
17910
    /* xgettext:c-format */
17911
0
    (_("%H: error: %s not a multiple of %u\n"),
17912
0
     input_bfd, input_section, rel->r_offset,
17913
0
     ppc64_elf_howto_table[r_type]->name,
17914
0
     mask + 1);
17915
0
        bfd_set_error (bfd_error_bad_value);
17916
0
        ret = false;
17917
0
        goto copy_reloc;
17918
0
      }
17919
0
    break;
17920
0
  }
17921
17922
      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17923
   because such sections are not SEC_ALLOC and thus ld.so will
17924
   not process them.  */
17925
0
      howto = ppc64_elf_howto_table[r_type];
17926
0
      if (unresolved_reloc
17927
0
    && !((input_section->flags & SEC_DEBUGGING) != 0
17928
0
         && h->elf.def_dynamic)
17929
0
    && _bfd_elf_section_offset (output_bfd, info, input_section,
17930
0
              rel->r_offset) != (bfd_vma) -1)
17931
0
  {
17932
0
    info->callbacks->einfo
17933
      /* xgettext:c-format */
17934
0
      (_("%H: unresolvable %s against `%pT'\n"),
17935
0
       input_bfd, input_section, rel->r_offset,
17936
0
       howto->name,
17937
0
       h->elf.root.root.string);
17938
0
    ret = false;
17939
0
  }
17940
17941
      /* 16-bit fields in insns mostly have signed values, but a
17942
   few insns have 16-bit unsigned values.  Really, we should
17943
   have different reloc types.  */
17944
0
      if (howto->complain_on_overflow != complain_overflow_dont
17945
0
    && howto->dst_mask == 0xffff
17946
0
    && (input_section->flags & SEC_CODE) != 0
17947
0
    && offset_in_range (input_section, rel->r_offset & ~3, 4))
17948
0
  {
17949
0
    enum complain_overflow complain = complain_overflow_signed;
17950
17951
0
    insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17952
0
    if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
17953
0
      complain = complain_overflow_bitfield;
17954
0
    else if (howto->rightshift == 0
17955
0
       ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17956
0
          || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17957
0
          || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17958
0
       : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17959
0
          || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17960
0
          || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
17961
0
      complain = complain_overflow_unsigned;
17962
0
    if (howto->complain_on_overflow != complain)
17963
0
      {
17964
0
        alt_howto = *howto;
17965
0
        alt_howto.complain_on_overflow = complain;
17966
0
        howto = &alt_howto;
17967
0
      }
17968
0
  }
17969
17970
0
      switch (r_type)
17971
0
  {
17972
    /* Split field relocs aren't handled by _bfd_final_link_relocate.  */
17973
0
  case R_PPC64_D34:
17974
0
  case R_PPC64_D34_LO:
17975
0
  case R_PPC64_D34_HI30:
17976
0
  case R_PPC64_D34_HA30:
17977
0
  case R_PPC64_PCREL34:
17978
0
  case R_PPC64_GOT_PCREL34:
17979
0
  case R_PPC64_TPREL34:
17980
0
  case R_PPC64_DTPREL34:
17981
0
  case R_PPC64_GOT_TLSGD_PCREL34:
17982
0
  case R_PPC64_GOT_TLSLD_PCREL34:
17983
0
  case R_PPC64_GOT_TPREL_PCREL34:
17984
0
  case R_PPC64_GOT_DTPREL_PCREL34:
17985
0
  case R_PPC64_PLT_PCREL34:
17986
0
  case R_PPC64_PLT_PCREL34_NOTOC:
17987
0
  case R_PPC64_D28:
17988
0
  case R_PPC64_PCREL28:
17989
0
    if (!offset_in_range (input_section, rel->r_offset, 8))
17990
0
      r = bfd_reloc_outofrange;
17991
0
    else
17992
0
      {
17993
0
        relocation += addend;
17994
0
        if (howto->pc_relative)
17995
0
    relocation -= (rel->r_offset
17996
0
             + input_section->output_offset
17997
0
             + input_section->output_section->vma);
17998
0
        relocation >>= howto->rightshift;
17999
18000
0
        pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
18001
0
        pinsn <<= 32;
18002
0
        pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
18003
18004
0
        pinsn &= ~howto->dst_mask;
18005
0
        pinsn |= (((relocation << 16) | (relocation & 0xffff))
18006
0
      & howto->dst_mask);
18007
0
        bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
18008
0
        bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
18009
0
        r = bfd_reloc_ok;
18010
0
        if (howto->complain_on_overflow == complain_overflow_signed
18011
0
      && (relocation + (1ULL << (howto->bitsize - 1))
18012
0
          >= 1ULL << howto->bitsize))
18013
0
    r = bfd_reloc_overflow;
18014
0
      }
18015
0
    break;
18016
18017
0
  case R_PPC64_REL16DX_HA:
18018
0
    if (!offset_in_range (input_section, rel->r_offset, 4))
18019
0
      r = bfd_reloc_outofrange;
18020
0
    else
18021
0
      {
18022
0
        relocation += addend;
18023
0
        relocation -= (rel->r_offset
18024
0
           + input_section->output_offset
18025
0
           + input_section->output_section->vma);
18026
0
        relocation = (bfd_signed_vma) relocation >> 16;
18027
0
        insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
18028
0
        insn &= ~0x1fffc1;
18029
0
        insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
18030
0
        bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
18031
0
        r = bfd_reloc_ok;
18032
0
        if (relocation + 0x8000 > 0xffff)
18033
0
    r = bfd_reloc_overflow;
18034
0
      }
18035
0
    break;
18036
18037
0
  default:
18038
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
18039
0
          contents, rel->r_offset,
18040
0
          relocation, addend);
18041
0
  }
18042
18043
0
      if (r != bfd_reloc_ok)
18044
0
  {
18045
0
    char *more_info = NULL;
18046
0
    const char *reloc_name = howto->name;
18047
18048
0
    if (reloc_dest != DEST_NORMAL)
18049
0
      {
18050
0
        more_info = bfd_malloc (strlen (reloc_name) + 8);
18051
0
        if (more_info != NULL)
18052
0
    {
18053
0
      strcpy (more_info, reloc_name);
18054
0
      strcat (more_info, (reloc_dest == DEST_OPD
18055
0
              ? " (OPD)" : " (stub)"));
18056
0
      reloc_name = more_info;
18057
0
    }
18058
0
      }
18059
18060
0
    if (r == bfd_reloc_overflow)
18061
0
      {
18062
        /* On code like "if (foo) foo();" don't report overflow
18063
     on a branch to zero when foo is undefined.  */
18064
0
        if (!warned
18065
0
      && (reloc_dest == DEST_STUB
18066
0
          || !(h != NULL
18067
0
         && (h->elf.root.type == bfd_link_hash_undefweak
18068
0
             || h->elf.root.type == bfd_link_hash_undefined)
18069
0
         && is_branch_reloc (r_type))))
18070
0
    info->callbacks->reloc_overflow
18071
0
      (info, (struct bfd_link_hash_entry *) h, sym_name,
18072
0
       reloc_name, orig_rel.r_addend, input_bfd, input_section,
18073
0
       rel->r_offset);
18074
0
      }
18075
0
    else
18076
0
      {
18077
0
        info->callbacks->einfo
18078
    /* xgettext:c-format */
18079
0
    (_("%H: %s against `%pT': error %d\n"),
18080
0
     input_bfd, input_section, rel->r_offset,
18081
0
     reloc_name, sym_name, (int) r);
18082
0
        ret = false;
18083
0
      }
18084
0
    free (more_info);
18085
0
  }
18086
0
    copy_reloc:
18087
0
      if (wrel != rel)
18088
0
  *wrel = *rel;
18089
0
    }
18090
18091
0
  if (wrel != rel)
18092
0
    {
18093
0
      Elf_Internal_Shdr *rel_hdr;
18094
0
      size_t deleted = rel - wrel;
18095
18096
0
      rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
18097
0
      rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18098
0
      rel_hdr = _bfd_elf_single_rel_hdr (input_section);
18099
0
      rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18100
0
      input_section->reloc_count -= deleted;
18101
0
    }
18102
18103
  /* If we're emitting relocations, then shortly after this function
18104
     returns, reloc offsets and addends for this section will be
18105
     adjusted.  Worse, reloc symbol indices will be for the output
18106
     file rather than the input.  Save a copy of the relocs for
18107
     opd_entry_value.  */
18108
0
  if (is_opd
18109
0
      && (info->emitrelocations || bfd_link_relocatable (info))
18110
0
      && input_section->reloc_count != 0)
18111
0
    {
18112
0
      bfd_size_type amt;
18113
0
      amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
18114
0
      rel = bfd_alloc (input_bfd, amt);
18115
0
      ppc64_elf_section_data (input_section)->u.opd.u.relocs = rel;
18116
0
      if (rel == NULL)
18117
0
  return false;
18118
0
      memcpy (rel, relocs, amt);
18119
0
    }
18120
0
  return ret;
18121
0
}
18122
18123
/* Adjust the value of any local symbols in opd sections.  */
18124
18125
static int
18126
ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
18127
            const char *name ATTRIBUTE_UNUSED,
18128
            Elf_Internal_Sym *elfsym,
18129
            asection *input_sec,
18130
            struct elf_link_hash_entry *h)
18131
0
{
18132
0
  struct _opd_sec_data *opd;
18133
0
  long adjust;
18134
0
  bfd_vma value;
18135
18136
0
  if (h != NULL)
18137
0
    return 1;
18138
18139
0
  opd = get_opd_info (input_sec);
18140
0
  if (opd == NULL || opd->adjust == NULL)
18141
0
    return 1;
18142
18143
0
  value = elfsym->st_value - input_sec->output_offset;
18144
0
  if (!bfd_link_relocatable (info))
18145
0
    value -= input_sec->output_section->vma;
18146
18147
0
  adjust = opd->adjust[OPD_NDX (value)];
18148
0
  if (adjust == -1)
18149
0
    return 2;
18150
18151
0
  elfsym->st_value += adjust;
18152
0
  return 1;
18153
0
}
18154
18155
/* Finish up dynamic symbol handling.  We set the contents of various
18156
   dynamic sections here.  */
18157
18158
static bool
18159
ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
18160
         struct bfd_link_info *info,
18161
         struct elf_link_hash_entry *h,
18162
         Elf_Internal_Sym *sym)
18163
0
{
18164
0
  struct ppc_link_hash_table *htab;
18165
0
  struct plt_entry *ent;
18166
18167
0
  htab = ppc_hash_table (info);
18168
18169
0
  if (!htab->opd_abi && !h->def_regular)
18170
0
    for (ent = h->plt.plist; ent != NULL; ent = ent->next)
18171
0
      if (ent->plt.offset != (bfd_vma) -1)
18172
0
  {
18173
    /* Mark the symbol as undefined, rather than as
18174
       defined in glink.  Leave the value if there were
18175
       any relocations where pointer equality matters
18176
       (this is a clue for the dynamic linker, to make
18177
       function pointer comparisons work between an
18178
       application and shared library), otherwise set it
18179
       to zero.  */
18180
0
    sym->st_shndx = SHN_UNDEF;
18181
0
    if (!h->pointer_equality_needed)
18182
0
      sym->st_value = 0;
18183
0
    else if (!h->ref_regular_nonweak)
18184
0
      {
18185
        /* This breaks function pointer comparisons, but
18186
     that is better than breaking tests for a NULL
18187
     function pointer.  */
18188
0
        sym->st_value = 0;
18189
0
      }
18190
0
    break;
18191
0
  }
18192
18193
0
  if (h->needs_copy
18194
0
      && (h->root.type == bfd_link_hash_defined
18195
0
    || h->root.type == bfd_link_hash_defweak)
18196
0
      && (h->root.u.def.section == htab->elf.sdynbss
18197
0
    || h->root.u.def.section == htab->elf.sdynrelro))
18198
0
    {
18199
      /* This symbol needs a copy reloc.  Set it up.  */
18200
0
      Elf_Internal_Rela rela;
18201
0
      asection *srel;
18202
18203
0
      if (h->dynindx == -1)
18204
0
  abort ();
18205
18206
0
      rela.r_offset = defined_sym_val (h);
18207
0
      rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18208
0
      rela.r_addend = 0;
18209
0
      if (h->root.u.def.section == htab->elf.sdynrelro)
18210
0
  srel = htab->elf.sreldynrelro;
18211
0
      else
18212
0
  srel = htab->elf.srelbss;
18213
0
      BFD_ASSERT (count_and_swap_reloc_out (output_bfd, &rela, srel));
18214
0
    }
18215
18216
0
  return true;
18217
0
}
18218
18219
/* Used to decide how to sort relocs in an optimal manner for the
18220
   dynamic linker, before writing them out.  */
18221
18222
static enum elf_reloc_type_class
18223
ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18224
          const asection *rel_sec,
18225
          const Elf_Internal_Rela *rela)
18226
0
{
18227
0
  enum elf_ppc64_reloc_type r_type;
18228
0
  struct ppc_link_hash_table *htab = ppc_hash_table (info);
18229
18230
0
  if (rel_sec == htab->elf.irelplt)
18231
0
    return reloc_class_ifunc;
18232
18233
0
  r_type = ELF64_R_TYPE (rela->r_info);
18234
0
  switch (r_type)
18235
0
    {
18236
0
    case R_PPC64_RELATIVE:
18237
0
      return reloc_class_relative;
18238
0
    case R_PPC64_JMP_SLOT:
18239
0
      return reloc_class_plt;
18240
0
    case R_PPC64_COPY:
18241
0
      return reloc_class_copy;
18242
0
    default:
18243
0
      return reloc_class_normal;
18244
0
    }
18245
0
}
18246
18247
/* Finish up the dynamic sections.  */
18248
18249
static bool
18250
ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
18251
           struct bfd_link_info *info,
18252
           bfd_byte *buf)
18253
0
{
18254
0
  struct ppc_link_hash_table *htab;
18255
0
  bfd *dynobj;
18256
0
  asection *sdyn;
18257
18258
0
  htab = ppc_hash_table (info);
18259
0
  if (htab == NULL)
18260
0
    return false;
18261
18262
0
  dynobj = htab->elf.dynobj;
18263
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
18264
18265
0
  if (htab->elf.dynamic_sections_created)
18266
0
    {
18267
0
      Elf64_External_Dyn *dyncon, *dynconend;
18268
18269
0
      if (sdyn == NULL || htab->elf.sgot == NULL)
18270
0
  abort ();
18271
18272
0
      dyncon = (Elf64_External_Dyn *) sdyn->contents;
18273
0
      dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
18274
0
      for (; dyncon < dynconend; dyncon++)
18275
0
  {
18276
0
    Elf_Internal_Dyn dyn;
18277
0
    asection *s;
18278
18279
0
    bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18280
18281
0
    switch (dyn.d_tag)
18282
0
      {
18283
0
      default:
18284
0
        continue;
18285
18286
0
      case DT_PPC64_GLINK:
18287
0
        s = htab->glink;
18288
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18289
        /* We stupidly defined DT_PPC64_GLINK to be the start
18290
     of glink rather than the first entry point, which is
18291
     what ld.so needs, and now have a bigger stub to
18292
     support automatic multiple TOCs.  */
18293
0
        dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
18294
0
        break;
18295
18296
0
      case DT_PPC64_OPD:
18297
0
        s = bfd_get_section_by_name (output_bfd, ".opd");
18298
0
        if (s == NULL)
18299
0
    continue;
18300
0
        dyn.d_un.d_ptr = s->vma;
18301
0
        break;
18302
18303
0
      case DT_PPC64_OPT:
18304
0
        if ((htab->do_multi_toc && htab->multi_toc_needed)
18305
0
      || htab->notoc_plt)
18306
0
    dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
18307
0
        if (htab->has_plt_localentry0)
18308
0
    dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
18309
0
        break;
18310
18311
0
      case DT_PPC64_OPDSZ:
18312
0
        s = bfd_get_section_by_name (output_bfd, ".opd");
18313
0
        if (s == NULL)
18314
0
    continue;
18315
0
        dyn.d_un.d_val = s->size;
18316
0
        break;
18317
18318
0
      case DT_PLTGOT:
18319
0
        s = htab->elf.splt;
18320
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18321
0
        break;
18322
18323
0
      case DT_JMPREL:
18324
0
        s = htab->elf.srelplt;
18325
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18326
0
        break;
18327
18328
0
      case DT_PLTRELSZ:
18329
0
        dyn.d_un.d_val = htab->elf.srelplt->size;
18330
0
        break;
18331
18332
0
      case DT_TEXTREL:
18333
0
        if (htab->elf.ifunc_resolvers)
18334
0
    info->callbacks->einfo
18335
0
      (_("%P: warning: text relocations and GNU indirect "
18336
0
         "functions may result in a segfault at runtime\n"));
18337
0
        continue;
18338
0
      }
18339
18340
0
    bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
18341
0
  }
18342
0
    }
18343
18344
0
  if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18345
0
      && htab->elf.sgot->output_section != bfd_abs_section_ptr)
18346
0
    {
18347
      /* Fill in the first entry in the global offset table.
18348
   We use it to hold the link-time TOCbase.  */
18349
0
      bfd_put_64 (output_bfd,
18350
0
      elf_gp (output_bfd) + TOC_BASE_OFF,
18351
0
      htab->elf.sgot->contents);
18352
18353
      /* Set .got entry size.  */
18354
0
      elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18355
0
  = 8;
18356
0
    }
18357
18358
0
  if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18359
0
      && htab->elf.splt->output_section != bfd_abs_section_ptr)
18360
0
    {
18361
      /* Set .plt entry size.  */
18362
0
      elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
18363
0
  = PLT_ENTRY_SIZE (htab);
18364
0
    }
18365
18366
  /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18367
     brlt ourselves if emitrelocations.  */
18368
0
  if (htab->brlt != NULL
18369
0
      && htab->brlt->reloc_count != 0
18370
0
      && !_bfd_elf_link_output_relocs (output_bfd,
18371
0
               htab->brlt,
18372
0
               elf_section_data (htab->brlt)->rela.hdr,
18373
0
               elf_section_data (htab->brlt)->relocs,
18374
0
               NULL))
18375
0
    return false;
18376
18377
0
  if (htab->glink != NULL
18378
0
      && htab->glink->reloc_count != 0
18379
0
      && !_bfd_elf_link_output_relocs (output_bfd,
18380
0
               htab->glink,
18381
0
               elf_section_data (htab->glink)->rela.hdr,
18382
0
               elf_section_data (htab->glink)->relocs,
18383
0
               NULL))
18384
0
    return false;
18385
18386
18387
0
  if (htab->glink_eh_frame != NULL
18388
0
      && htab->glink_eh_frame->size != 0
18389
0
      && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18390
0
      && !_bfd_elf_write_linker_section_eh_frame (output_bfd, info,
18391
0
              htab->glink_eh_frame, buf))
18392
0
    return false;
18393
18394
  /* We need to handle writing out multiple GOT sections ourselves,
18395
     since we didn't add them to DYNOBJ.  We know dynobj is the first
18396
     bfd.  */
18397
0
  while ((dynobj = dynobj->link.next) != NULL)
18398
0
    {
18399
0
      asection *s;
18400
18401
0
      if (!is_ppc64_elf (dynobj))
18402
0
  continue;
18403
18404
0
      s = ppc64_elf_tdata (dynobj)->got;
18405
0
      if (s != NULL
18406
0
    && s->size != 0
18407
0
    && s->output_section != bfd_abs_section_ptr
18408
0
    && !bfd_set_section_contents (output_bfd, s->output_section,
18409
0
          s->contents, s->output_offset,
18410
0
          s->size))
18411
0
  return false;
18412
0
      s = ppc64_elf_tdata (dynobj)->relgot;
18413
0
      if (s != NULL
18414
0
    && s->size != 0
18415
0
    && s->output_section != bfd_abs_section_ptr
18416
0
    && !bfd_set_section_contents (output_bfd, s->output_section,
18417
0
          s->contents, s->output_offset,
18418
0
          s->size))
18419
0
  return false;
18420
0
    }
18421
18422
0
  return true;
18423
0
}
18424
18425
static bool
18426
ppc64_elf_free_cached_info (bfd *abfd)
18427
152k
{
18428
152k
  if (abfd->sections)
18429
239
    for (asection *opd = bfd_get_section_by_name (abfd, ".opd");
18430
239
   opd != NULL;
18431
239
   opd = bfd_get_next_section_by_name (NULL, opd))
18432
0
      if (opd->reloc_count == 0)
18433
0
  free (ppc64_elf_section_data (opd)->u.opd.u.contents);
18434
18435
152k
  return _bfd_elf_free_cached_info (abfd);
18436
152k
}
18437
18438
#include "elf64-target.h"
18439
18440
/* FreeBSD support */
18441
18442
#undef  TARGET_LITTLE_SYM
18443
#define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
18444
#undef  TARGET_LITTLE_NAME
18445
#define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
18446
18447
#undef  TARGET_BIG_SYM
18448
#define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
18449
#undef  TARGET_BIG_NAME
18450
#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18451
18452
#undef  ELF_OSABI
18453
#define ELF_OSABI       ELFOSABI_FREEBSD
18454
#undef  ELF_OSABI_EXACT
18455
#define ELF_OSABI_EXACT 1
18456
18457
#undef  elf64_bed
18458
#define elf64_bed elf64_powerpc_fbsd_bed
18459
18460
#include "elf64-target.h"