Coverage Report

Created: 2026-07-25 10:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf64-x86-64.c
Line
Count
Source
1
/* X86-64 specific support for ELF
2
   Copyright (C) 2000-2026 Free Software Foundation, Inc.
3
   Contributed by Jan Hubicka <jh@suse.cz>.
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
#include "elfxx-x86.h"
23
#include "dwarf2.h"
24
#include "libiberty.h"
25
#include "sframe.h"
26
#include "elf-solaris2.h"
27
28
#include "opcode/i386.h"
29
30
#ifdef CORE_HEADER
31
#include <stdarg.h>
32
#include CORE_HEADER
33
#endif
34
35
/* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
36
#define MINUS_ONE (~ (bfd_vma) 0)
37
38
/* Since both 32-bit and 64-bit x86-64 encode relocation type in the
39
   identical manner, we use ELF32_R_TYPE instead of ELF64_R_TYPE to get
40
   relocation type.  We also use ELF_ST_TYPE instead of ELF64_ST_TYPE
41
   since they are the same.  */
42
43
/* The relocation "howto" table.  Order of fields:
44
   type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
45
   special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset.  */
46
static reloc_howto_type x86_64_elf_howto_table[] =
47
{
48
  HOWTO(R_X86_64_NONE, 0, 0, 0, false, 0, complain_overflow_dont,
49
  bfd_elf_generic_reloc, "R_X86_64_NONE", false, 0, 0x00000000,
50
  false),
51
  HOWTO(R_X86_64_64, 0, 8, 64, false, 0, complain_overflow_dont,
52
  bfd_elf_generic_reloc, "R_X86_64_64", false, 0, MINUS_ONE,
53
  false),
54
  HOWTO(R_X86_64_PC32, 0, 4, 32, true, 0, complain_overflow_signed,
55
  bfd_elf_generic_reloc, "R_X86_64_PC32", false, 0, 0xffffffff,
56
  true),
57
  HOWTO(R_X86_64_GOT32, 0, 4, 32, false, 0, complain_overflow_signed,
58
  bfd_elf_generic_reloc, "R_X86_64_GOT32", false, 0, 0xffffffff,
59
  false),
60
  HOWTO(R_X86_64_PLT32, 0, 4, 32, true, 0, complain_overflow_signed,
61
  bfd_elf_generic_reloc, "R_X86_64_PLT32", false, 0, 0xffffffff,
62
  true),
63
  HOWTO(R_X86_64_COPY, 0, 4, 32, false, 0, complain_overflow_bitfield,
64
  bfd_elf_generic_reloc, "R_X86_64_COPY", false, 0, 0xffffffff,
65
  false),
66
  HOWTO(R_X86_64_GLOB_DAT, 0, 8, 64, false, 0, complain_overflow_dont,
67
  bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", false, 0, MINUS_ONE,
68
  false),
69
  HOWTO(R_X86_64_JUMP_SLOT, 0, 8, 64, false, 0, complain_overflow_dont,
70
  bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", false, 0, MINUS_ONE,
71
  false),
72
  HOWTO(R_X86_64_RELATIVE, 0, 8, 64, false, 0, complain_overflow_dont,
73
  bfd_elf_generic_reloc, "R_X86_64_RELATIVE", false, 0, MINUS_ONE,
74
  false),
75
  HOWTO(R_X86_64_GOTPCREL, 0, 4, 32, true, 0, complain_overflow_signed,
76
  bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", false, 0, 0xffffffff,
77
  true),
78
  HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_unsigned,
79
  bfd_elf_generic_reloc, "R_X86_64_32", false, 0, 0xffffffff,
80
  false),
81
  HOWTO(R_X86_64_32S, 0, 4, 32, false, 0, complain_overflow_signed,
82
  bfd_elf_generic_reloc, "R_X86_64_32S", false, 0, 0xffffffff,
83
  false),
84
  HOWTO(R_X86_64_16, 0, 2, 16, false, 0, complain_overflow_bitfield,
85
  bfd_elf_generic_reloc, "R_X86_64_16", false, 0, 0xffff, false),
86
  HOWTO(R_X86_64_PC16, 0, 2, 16, true, 0, complain_overflow_bitfield,
87
  bfd_elf_generic_reloc, "R_X86_64_PC16", false, 0, 0xffff, true),
88
  HOWTO(R_X86_64_8, 0, 1, 8, false, 0, complain_overflow_bitfield,
89
  bfd_elf_generic_reloc, "R_X86_64_8", false, 0, 0xff, false),
90
  HOWTO(R_X86_64_PC8, 0, 1, 8, true, 0, complain_overflow_signed,
91
  bfd_elf_generic_reloc, "R_X86_64_PC8", false, 0, 0xff, true),
92
  HOWTO(R_X86_64_DTPMOD64, 0, 8, 64, false, 0, complain_overflow_dont,
93
  bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", false, 0, MINUS_ONE,
94
  false),
95
  HOWTO(R_X86_64_DTPOFF64, 0, 8, 64, false, 0, complain_overflow_dont,
96
  bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", false, 0, MINUS_ONE,
97
  false),
98
  HOWTO(R_X86_64_TPOFF64, 0, 8, 64, false, 0, complain_overflow_dont,
99
  bfd_elf_generic_reloc, "R_X86_64_TPOFF64", false, 0, MINUS_ONE,
100
  false),
101
  HOWTO(R_X86_64_TLSGD, 0, 4, 32, true, 0, complain_overflow_signed,
102
  bfd_elf_generic_reloc, "R_X86_64_TLSGD", false, 0, 0xffffffff,
103
  true),
104
  HOWTO(R_X86_64_TLSLD, 0, 4, 32, true, 0, complain_overflow_signed,
105
  bfd_elf_generic_reloc, "R_X86_64_TLSLD", false, 0, 0xffffffff,
106
  true),
107
  HOWTO(R_X86_64_DTPOFF32, 0, 4, 32, false, 0, complain_overflow_signed,
108
  bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", false, 0, 0xffffffff,
109
  false),
110
  HOWTO(R_X86_64_GOTTPOFF, 0, 4, 32, true, 0, complain_overflow_signed,
111
  bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", false, 0,   0xffffffff,
112
  true),
113
  HOWTO(R_X86_64_TPOFF32, 0, 4, 32, false, 0, complain_overflow_signed,
114
  bfd_elf_generic_reloc, "R_X86_64_TPOFF32", false, 0, 0xffffffff,
115
  false),
116
  HOWTO(R_X86_64_PC64, 0, 8, 64, true, 0, complain_overflow_dont,
117
  bfd_elf_generic_reloc, "R_X86_64_PC64", false, 0, MINUS_ONE,
118
  true),
119
  HOWTO(R_X86_64_GOTOFF64, 0, 8, 64, false, 0, complain_overflow_dont,
120
  bfd_elf_generic_reloc, "R_X86_64_GOTOFF64", false, 0, MINUS_ONE,
121
  false),
122
  HOWTO(R_X86_64_GOTPC32, 0, 4, 32, true, 0, complain_overflow_signed,
123
  bfd_elf_generic_reloc, "R_X86_64_GOTPC32", false, 0, 0xffffffff,
124
  true),
125
  HOWTO(R_X86_64_GOT64, 0, 8, 64, false, 0, complain_overflow_signed,
126
  bfd_elf_generic_reloc, "R_X86_64_GOT64", false, 0, MINUS_ONE,
127
  false),
128
  HOWTO(R_X86_64_GOTPCREL64, 0, 8, 64, true, 0, complain_overflow_signed,
129
  bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", false, 0, MINUS_ONE,
130
  true),
131
  HOWTO(R_X86_64_GOTPC64, 0, 8, 64, true, 0, complain_overflow_signed,
132
  bfd_elf_generic_reloc, "R_X86_64_GOTPC64", false, 0, MINUS_ONE,
133
  true),
134
  HOWTO(R_X86_64_GOTPLT64, 0, 8, 64, false, 0, complain_overflow_signed,
135
  bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", false, 0, MINUS_ONE,
136
  false),
137
  HOWTO(R_X86_64_PLTOFF64, 0, 8, 64, false, 0, complain_overflow_signed,
138
  bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", false, 0, MINUS_ONE,
139
  false),
140
  HOWTO(R_X86_64_SIZE32, 0, 4, 32, false, 0, complain_overflow_unsigned,
141
  bfd_elf_generic_reloc, "R_X86_64_SIZE32", false, 0, 0xffffffff,
142
  false),
143
  HOWTO(R_X86_64_SIZE64, 0, 8, 64, false, 0, complain_overflow_dont,
144
  bfd_elf_generic_reloc, "R_X86_64_SIZE64", false, 0, MINUS_ONE,
145
  false),
146
  HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 4, 32, true, 0,
147
  complain_overflow_bitfield, bfd_elf_generic_reloc,
148
  "R_X86_64_GOTPC32_TLSDESC", false, 0, 0xffffffff, true),
149
  HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, false, 0,
150
  complain_overflow_dont, bfd_elf_generic_reloc,
151
  "R_X86_64_TLSDESC_CALL",
152
  false, 0, 0, false),
153
  HOWTO(R_X86_64_TLSDESC, 0, 8, 64, false, 0,
154
  complain_overflow_dont, bfd_elf_generic_reloc,
155
  "R_X86_64_TLSDESC", false, 0, MINUS_ONE, false),
156
  HOWTO(R_X86_64_IRELATIVE, 0, 8, 64, false, 0, complain_overflow_dont,
157
  bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", false, 0, MINUS_ONE,
158
  false),
159
  HOWTO(R_X86_64_RELATIVE64, 0, 8, 64, false, 0, complain_overflow_dont,
160
  bfd_elf_generic_reloc, "R_X86_64_RELATIVE64", false, 0, MINUS_ONE,
161
  false),
162
  HOWTO(R_X86_64_PC32_BND, 0, 4, 32, true, 0, complain_overflow_signed,
163
  bfd_elf_generic_reloc, "R_X86_64_PC32_BND", false, 0, 0xffffffff,
164
  true),
165
  HOWTO(R_X86_64_PLT32_BND, 0, 4, 32, true, 0, complain_overflow_signed,
166
  bfd_elf_generic_reloc, "R_X86_64_PLT32_BND", false, 0, 0xffffffff,
167
  true),
168
  HOWTO(R_X86_64_GOTPCRELX, 0, 4, 32, true, 0, complain_overflow_signed,
169
  bfd_elf_generic_reloc, "R_X86_64_GOTPCRELX", false, 0, 0xffffffff,
170
  true),
171
  HOWTO(R_X86_64_REX_GOTPCRELX, 0, 4, 32, true, 0, complain_overflow_signed,
172
  bfd_elf_generic_reloc, "R_X86_64_REX_GOTPCRELX", false, 0, 0xffffffff,
173
  true),
174
  HOWTO(R_X86_64_CODE_4_GOTPCRELX, 0, 4, 32, true, 0, complain_overflow_signed,
175
  bfd_elf_generic_reloc, "R_X86_64_CODE_4_GOTPCRELX", false, 0, 0xffffffff,
176
  true),
177
  HOWTO(R_X86_64_CODE_4_GOTTPOFF, 0, 4, 32, true, 0, complain_overflow_signed,
178
  bfd_elf_generic_reloc, "R_X86_64_CODE_4_GOTTPOFF", false, 0, 0xffffffff,
179
  true),
180
  HOWTO(R_X86_64_CODE_4_GOTPC32_TLSDESC, 0, 4, 32, true, 0,
181
  complain_overflow_bitfield, bfd_elf_generic_reloc,
182
  "R_X86_64_CODE_4_GOTPC32_TLSDESC", false, 0, 0xffffffff, true),
183
  HOWTO(R_X86_64_CODE_5_GOTPCRELX, 0, 4, 32, true, 0,
184
  complain_overflow_signed, bfd_elf_generic_reloc,
185
  "R_X86_64_CODE_5_GOTPCRELX", false, 0, 0xffffffff, true),
186
  HOWTO(R_X86_64_CODE_5_GOTTPOFF, 0, 4, 32, true, 0,
187
  complain_overflow_signed, bfd_elf_generic_reloc,
188
  "R_X86_64_CODE_5_GOTTPOFF", false, 0, 0xffffffff, true),
189
  HOWTO(R_X86_64_CODE_5_GOTPC32_TLSDESC, 0, 4, 32, true, 0,
190
  complain_overflow_bitfield, bfd_elf_generic_reloc,
191
  "R_X86_64_CODE_5_GOTPC32_TLSDESC", false, 0, 0xffffffff, true),
192
  HOWTO(R_X86_64_CODE_6_GOTPCRELX, 0, 4, 32, true, 0,
193
  complain_overflow_signed, bfd_elf_generic_reloc,
194
  "R_X86_64_CODE_6_GOTPCRELX", false, 0, 0xffffffff, true),
195
  HOWTO(R_X86_64_CODE_6_GOTTPOFF, 0, 4, 32, true, 0,
196
  complain_overflow_signed, bfd_elf_generic_reloc,
197
  "R_X86_64_CODE_6_GOTTPOFF", false, 0, 0xffffffff, true),
198
  HOWTO(R_X86_64_CODE_6_GOTPC32_TLSDESC, 0, 4, 32, true, 0,
199
  complain_overflow_bitfield, bfd_elf_generic_reloc,
200
  "R_X86_64_CODE_6_GOTPC32_TLSDESC", false, 0, 0xffffffff, true),
201
202
  /* We have a gap in the reloc numbers here.
203
     R_X86_64_standard counts the number up to this point, and
204
     R_X86_64_vt_offset is the value to subtract from a reloc type of
205
     R_X86_64_GNU_VT* to form an index into this table.  */
206
40.9k
#define R_X86_64_standard (R_X86_64_CODE_6_GOTPC32_TLSDESC + 1)
207
161
#define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
208
209
/* GNU extension to record C++ vtable hierarchy.  */
210
  HOWTO (R_X86_64_GNU_VTINHERIT, 0, 8, 0, false, 0, complain_overflow_dont,
211
   NULL, "R_X86_64_GNU_VTINHERIT", false, 0, 0, false),
212
213
/* GNU extension to record C++ vtable member usage.  */
214
  HOWTO (R_X86_64_GNU_VTENTRY, 0, 8, 0, false, 0, complain_overflow_dont,
215
   _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", false, 0, 0,
216
   false),
217
218
/* Use complain_overflow_bitfield on R_X86_64_32 for x32.  */
219
  HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_bitfield,
220
  bfd_elf_generic_reloc, "R_X86_64_32", false, 0, 0xffffffff,
221
  false)
222
};
223
224
/* Map BFD relocs to the x86_64 elf relocs.  */
225
struct elf_reloc_map
226
{
227
  bfd_reloc_code_real_type bfd_reloc_val;
228
  unsigned char elf_reloc_val;
229
};
230
231
static const struct elf_reloc_map x86_64_reloc_map[] =
232
{
233
  { BFD_RELOC_NONE,   R_X86_64_NONE, },
234
  { BFD_RELOC_64,   R_X86_64_64,   },
235
  { BFD_RELOC_32_PCREL,   R_X86_64_PC32, },
236
  { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
237
  { BFD_RELOC_32_PLT_PCREL, R_X86_64_PLT32,},
238
  { BFD_RELOC_COPY,   R_X86_64_COPY, },
239
  { BFD_RELOC_GLOB_DAT,   R_X86_64_GLOB_DAT, },
240
  { BFD_RELOC_JMP_SLOT,   R_X86_64_JUMP_SLOT, },
241
  { BFD_RELOC_RELATIVE,   R_X86_64_RELATIVE, },
242
  { BFD_RELOC_X86_64_GOTPCREL,  R_X86_64_GOTPCREL, },
243
  { BFD_RELOC_32,   R_X86_64_32, },
244
  { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
245
  { BFD_RELOC_16,   R_X86_64_16, },
246
  { BFD_RELOC_16_PCREL,   R_X86_64_PC16, },
247
  { BFD_RELOC_8,    R_X86_64_8, },
248
  { BFD_RELOC_8_PCREL,    R_X86_64_PC8, },
249
  { BFD_RELOC_X86_64_DTPMOD64,  R_X86_64_DTPMOD64, },
250
  { BFD_RELOC_X86_64_DTPOFF64,  R_X86_64_DTPOFF64, },
251
  { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
252
  { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
253
  { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
254
  { BFD_RELOC_X86_64_DTPOFF32,  R_X86_64_DTPOFF32, },
255
  { BFD_RELOC_X86_64_GOTTPOFF,  R_X86_64_GOTTPOFF, },
256
  { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
257
  { BFD_RELOC_64_PCREL,   R_X86_64_PC64, },
258
  { BFD_RELOC_64_GOTOFF,  R_X86_64_GOTOFF64, },
259
  { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
260
  { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
261
  { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
262
  { BFD_RELOC_64_GOT_PCREL, R_X86_64_GOTPC64, },
263
  { BFD_RELOC_X86_64_GOTPLT64,  R_X86_64_GOTPLT64, },
264
  { BFD_RELOC_64_PLTOFF,  R_X86_64_PLTOFF64, },
265
  { BFD_RELOC_SIZE32,   R_X86_64_SIZE32, },
266
  { BFD_RELOC_SIZE64,   R_X86_64_SIZE64, },
267
  { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
268
  { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
269
  { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
270
  { BFD_RELOC_IRELATIVE,  R_X86_64_IRELATIVE, },
271
  { BFD_RELOC_X86_64_PC32_BND,  R_X86_64_PC32_BND, },
272
  { BFD_RELOC_X86_64_PLT32_BND, R_X86_64_PLT32_BND, },
273
  { BFD_RELOC_X86_64_GOTPCRELX, R_X86_64_GOTPCRELX, },
274
  { BFD_RELOC_X86_64_REX_GOTPCRELX, R_X86_64_REX_GOTPCRELX, },
275
  { BFD_RELOC_X86_64_CODE_4_GOTPCRELX, R_X86_64_CODE_4_GOTPCRELX, },
276
  { BFD_RELOC_X86_64_CODE_4_GOTTPOFF, R_X86_64_CODE_4_GOTTPOFF, },
277
  { BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC, R_X86_64_CODE_4_GOTPC32_TLSDESC, },
278
  { BFD_RELOC_X86_64_CODE_5_GOTPCRELX, R_X86_64_CODE_5_GOTPCRELX, },
279
  { BFD_RELOC_X86_64_CODE_5_GOTTPOFF, R_X86_64_CODE_5_GOTTPOFF, },
280
  { BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC, R_X86_64_CODE_5_GOTPC32_TLSDESC, },
281
  { BFD_RELOC_X86_64_CODE_6_GOTPCRELX, R_X86_64_CODE_6_GOTPCRELX, },
282
  { BFD_RELOC_X86_64_CODE_6_GOTTPOFF, R_X86_64_CODE_6_GOTTPOFF, },
283
  { BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC, R_X86_64_CODE_6_GOTPC32_TLSDESC, },
284
  { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
285
  { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
286
};
287
288
static reloc_howto_type *
289
elf_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
290
48.9k
{
291
48.9k
  unsigned i;
292
293
48.9k
  if (r_type == (unsigned int) R_X86_64_32)
294
7.96k
    {
295
7.96k
      if (ABI_64_P (abfd))
296
7.96k
  i = r_type;
297
3
      else
298
3
  i = ARRAY_SIZE (x86_64_elf_howto_table) - 1;
299
7.96k
    }
300
40.9k
  else if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
301
436
     || r_type >= (unsigned int) R_X86_64_max)
302
40.8k
    {
303
40.8k
      if (r_type >= (unsigned int) R_X86_64_standard)
304
919
  {
305
    /* xgettext:c-format */
306
919
    _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
307
919
            abfd, r_type);
308
919
    bfd_set_error (bfd_error_bad_value);
309
919
    return NULL;
310
919
  }
311
39.8k
      i = r_type;
312
39.8k
    }
313
161
  else
314
161
    i = r_type - (unsigned int) R_X86_64_vt_offset;
315
48.0k
  BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
316
48.0k
  return &x86_64_elf_howto_table[i];
317
48.9k
}
318
319
/* Given a BFD reloc type, return a HOWTO structure.  */
320
static reloc_howto_type *
321
elf_x86_64_reloc_type_lookup (bfd *abfd,
322
            bfd_reloc_code_real_type code)
323
66
{
324
66
  unsigned int i;
325
326
921
  for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
327
855
       i++)
328
921
    {
329
921
      if (x86_64_reloc_map[i].bfd_reloc_val == code)
330
66
  return elf_x86_64_rtype_to_howto (abfd,
331
66
            x86_64_reloc_map[i].elf_reloc_val);
332
921
    }
333
0
  return NULL;
334
66
}
335
336
static reloc_howto_type *
337
elf_x86_64_reloc_name_lookup (bfd *abfd,
338
            const char *r_name)
339
13
{
340
13
  unsigned int i;
341
342
13
  if (!ABI_64_P (abfd) && strcasecmp (r_name, "R_X86_64_32") == 0)
343
0
    {
344
      /* Get x32 R_X86_64_32.  */
345
0
      reloc_howto_type *reloc
346
0
  = &x86_64_elf_howto_table[ARRAY_SIZE (x86_64_elf_howto_table) - 1];
347
0
      BFD_ASSERT (reloc->type == (unsigned int) R_X86_64_32);
348
0
      return reloc;
349
0
    }
350
351
143
  for (i = 0; i < ARRAY_SIZE (x86_64_elf_howto_table); i++)
352
143
    if (x86_64_elf_howto_table[i].name != NULL
353
143
  && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
354
13
      return &x86_64_elf_howto_table[i];
355
356
0
  return NULL;
357
13
}
358
359
/* Given an x86_64 ELF reloc type, fill in an arelent structure.  */
360
361
static bool
362
elf_x86_64_info_to_howto (bfd *abfd, arelent *cache_ptr,
363
        Elf_Internal_Rela *dst)
364
48.8k
{
365
48.8k
  unsigned r_type;
366
367
48.8k
  r_type = ELF32_R_TYPE (dst->r_info);
368
48.8k
  cache_ptr->howto = elf_x86_64_rtype_to_howto (abfd, r_type);
369
48.8k
  if (cache_ptr->howto == NULL)
370
919
    return false;
371
47.9k
  BFD_ASSERT (r_type == cache_ptr->howto->type || cache_ptr->howto->type == R_X86_64_NONE);
372
47.9k
  return true;
373
48.8k
}
374

375
/* Support for core dump NOTE sections.  */
376
static bool
377
elf_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
378
144
{
379
144
  int offset;
380
144
  size_t size;
381
382
144
  switch (note->descsz)
383
144
    {
384
144
      default:
385
144
  return false;
386
387
0
      case 296:   /* sizeof(istruct elf_prstatus) on Linux/x32 */
388
  /* pr_cursig */
389
0
  elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
390
391
  /* pr_pid */
392
0
  elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
393
394
  /* pr_reg */
395
0
  offset = 72;
396
0
  size = 216;
397
398
0
  break;
399
400
0
      case 336:   /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
401
  /* pr_cursig */
402
0
  elf_tdata (abfd)->core->signal
403
0
    = bfd_get_16 (abfd, note->descdata + 12);
404
405
  /* pr_pid */
406
0
  elf_tdata (abfd)->core->lwpid
407
0
    = bfd_get_32 (abfd, note->descdata + 32);
408
409
  /* pr_reg */
410
0
  offset = 112;
411
0
  size = 216;
412
413
0
  break;
414
144
    }
415
416
  /* Make a ".reg/999" section.  */
417
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
418
0
            size, note->descpos + offset);
419
144
}
420
421
static bool
422
elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
423
79
{
424
79
  switch (note->descsz)
425
79
    {
426
79
      default:
427
79
  return false;
428
429
0
      case 124:
430
  /* sizeof (struct elf_external_linux_prpsinfo32_ugid16).  */
431
0
  elf_tdata (abfd)->core->pid
432
0
    = bfd_get_32 (abfd, note->descdata + 12);
433
0
  elf_tdata (abfd)->core->program
434
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
435
0
  elf_tdata (abfd)->core->command
436
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
437
0
  break;
438
439
0
    case 128:
440
  /* sizeof (struct elf_external_linux_prpsinfo32_ugid32).  */
441
0
  elf_tdata (abfd)->core->pid
442
0
    = bfd_get_32 (abfd, note->descdata + 12);
443
0
  elf_tdata (abfd)->core->program
444
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
445
0
  elf_tdata (abfd)->core->command
446
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
447
0
  break;
448
449
0
      case 136:
450
  /* sizeof (struct elf_prpsinfo) on Linux/x86_64.  */
451
0
  elf_tdata (abfd)->core->pid
452
0
    = bfd_get_32 (abfd, note->descdata + 24);
453
0
  elf_tdata (abfd)->core->program
454
0
   = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
455
0
  elf_tdata (abfd)->core->command
456
0
   = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
457
79
    }
458
459
  /* Note that for some reason, a spurious space is tacked
460
     onto the end of the args in some (at least one anyway)
461
     implementations, so strip it off if it exists.  */
462
463
0
  {
464
0
    char *command = elf_tdata (abfd)->core->command;
465
0
    int n = strlen (command);
466
467
0
    if (0 < n && command[n - 1] == ' ')
468
0
      command[n - 1] = '\0';
469
0
  }
470
471
0
  return true;
472
79
}
473
474
#ifdef CORE_HEADER
475
# if GCC_VERSION >= 8000
476
#  pragma GCC diagnostic push
477
#  pragma GCC diagnostic ignored "-Wstringop-truncation"
478
# endif
479
static char *
480
elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
481
          int note_type, ...)
482
0
{
483
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
484
0
  va_list ap;
485
0
  const char *fname, *psargs;
486
0
  long pid;
487
0
  int cursig;
488
0
  const void *gregs;
489
490
0
  switch (note_type)
491
0
    {
492
0
    default:
493
0
      return NULL;
494
495
0
    case NT_PRPSINFO:
496
0
      va_start (ap, note_type);
497
0
      fname = va_arg (ap, const char *);
498
0
      psargs = va_arg (ap, const char *);
499
0
      va_end (ap);
500
501
0
      if (bed->s->elfclass == ELFCLASS32)
502
0
  {
503
0
    prpsinfo32_t data;
504
0
    memset (&data, 0, sizeof (data));
505
0
    strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
506
0
    strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
507
0
    return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
508
0
             &data, sizeof (data));
509
0
  }
510
0
      else
511
0
  {
512
0
    prpsinfo64_t data;
513
0
    memset (&data, 0, sizeof (data));
514
0
    strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
515
0
    strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
516
0
    return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
517
0
             &data, sizeof (data));
518
0
  }
519
      /* NOTREACHED */
520
521
0
    case NT_PRSTATUS:
522
0
      va_start (ap, note_type);
523
0
      pid = va_arg (ap, long);
524
0
      cursig = va_arg (ap, int);
525
0
      gregs = va_arg (ap, const void *);
526
0
      va_end (ap);
527
528
0
      if (bed->s->elfclass == ELFCLASS32)
529
0
  {
530
0
    if (bed->elf_machine_code == EM_X86_64)
531
0
      {
532
0
        prstatusx32_t prstat;
533
0
        memset (&prstat, 0, sizeof (prstat));
534
0
        prstat.pr_pid = pid;
535
0
        prstat.pr_cursig = cursig;
536
0
        memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
537
0
        return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
538
0
           &prstat, sizeof (prstat));
539
0
      }
540
0
    else
541
0
      {
542
0
        prstatus32_t prstat;
543
0
        memset (&prstat, 0, sizeof (prstat));
544
0
        prstat.pr_pid = pid;
545
0
        prstat.pr_cursig = cursig;
546
0
        memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
547
0
        return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
548
0
           &prstat, sizeof (prstat));
549
0
      }
550
0
  }
551
0
      else
552
0
  {
553
0
    prstatus64_t prstat;
554
0
    memset (&prstat, 0, sizeof (prstat));
555
0
    prstat.pr_pid = pid;
556
0
    prstat.pr_cursig = cursig;
557
0
    memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
558
0
    return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
559
0
             &prstat, sizeof (prstat));
560
0
  }
561
0
    }
562
  /* NOTREACHED */
563
0
}
564
# if GCC_VERSION >= 8000
565
#  pragma GCC diagnostic pop
566
# endif
567
#endif
568

569
/* Functions for the x86-64 ELF linker.  */
570
571
/* The size in bytes of an entry in the global offset table.  */
572
573
0
#define GOT_ENTRY_SIZE 8
574
575
/* The size in bytes of an entry in the lazy procedure linkage table.  */
576
577
#define LAZY_PLT_ENTRY_SIZE 16
578
579
/* The size in bytes of an entry in the non-lazy procedure linkage
580
   table.  */
581
582
#define NON_LAZY_PLT_ENTRY_SIZE 8
583
584
/* The first entry in a lazy procedure linkage table looks like this.
585
   See the SVR4 ABI i386 supplement and the x86-64 ABI to see how this
586
   works.  */
587
588
static const bfd_byte elf_x86_64_lazy_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
589
{
590
  0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip)  */
591
  0xff, 0x25, 16, 0, 0, 0,  /* jmpq *GOT+16(%rip) */
592
  0x0f, 0x1f, 0x40, 0x00  /* nopl 0(%rax)       */
593
};
594
595
/* Subsequent entries in a lazy procedure linkage table look like this.  */
596
597
static const bfd_byte elf_x86_64_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
598
{
599
  0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
600
  0, 0, 0, 0, /* replaced with offset to this symbol in .got.  */
601
  0x68,   /* pushq immediate */
602
  0, 0, 0, 0, /* replaced with index into relocation table.  */
603
  0xe9,   /* jmp relative */
604
  0, 0, 0, 0  /* replaced with offset to start of .plt0.  */
605
};
606
607
/* The first entry in a lazy procedure linkage table with BND prefix
608
   like this.  */
609
610
static const bfd_byte elf_x86_64_lazy_bnd_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
611
{
612
  0xff, 0x35, 8, 0, 0, 0,   /* pushq GOT+8(%rip)        */
613
  0xf2, 0xff, 0x25, 16, 0, 0, 0,  /* bnd jmpq *GOT+16(%rip)   */
614
  0x0f, 0x1f, 0       /* nopl (%rax)        */
615
};
616
617
/* Subsequent entries for branches with BND prefx in a lazy procedure
618
   linkage table look like this.  */
619
620
static const bfd_byte elf_x86_64_lazy_bnd_plt_entry[LAZY_PLT_ENTRY_SIZE] =
621
{
622
  0x68, 0, 0, 0, 0,   /* pushq immediate        */
623
  0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative        */
624
  0x0f, 0x1f, 0x44, 0, 0  /* nopl 0(%rax,%rax,1)        */
625
};
626
627
/* The first entry in the IBT-enabled lazy procedure linkage table is the
628
   the same as the lazy PLT with BND prefix so that bound registers are
629
   preserved when control is passed to dynamic linker.  Subsequent
630
   entries for a IBT-enabled lazy procedure linkage table look like
631
   this.  */
632
633
static const bfd_byte elf_x86_64_lazy_bnd_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
634
{
635
  0xf3, 0x0f, 0x1e, 0xfa, /* endbr64          */
636
  0x68, 0, 0, 0, 0,   /* pushq immediate        */
637
  0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative        */
638
  0x90        /* nop            */
639
};
640
641
/* The first entry in the IBT-enabled lazy procedure linkage table
642
   is the same as the normal lazy PLT.  Subsequent entries for an
643
   IBT-enabled lazy procedure linkage table look like this.  */
644
645
static const bfd_byte elf_x86_64_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
646
{
647
  0xf3, 0x0f, 0x1e, 0xfa, /* endbr64          */
648
  0x68, 0, 0, 0, 0,   /* pushq immediate        */
649
  0xe9, 0, 0, 0, 0,   /* jmpq relative        */
650
  0x66, 0x90      /* xchg %ax,%ax         */
651
};
652
653
/* Entries in the non-lazey procedure linkage table look like this.  */
654
655
static const bfd_byte elf_x86_64_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
656
{
657
  0xff, 0x25,      /* jmpq *name@GOTPC(%rip)            */
658
  0, 0, 0, 0,      /* replaced with offset to this symbol in .got.  */
659
  0x66, 0x90       /* xchg %ax,%ax              */
660
};
661
662
/* Entries for branches with BND prefix in the non-lazey procedure
663
   linkage table look like this.  */
664
665
static const bfd_byte elf_x86_64_non_lazy_bnd_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
666
{
667
  0xf2, 0xff, 0x25,  /* bnd jmpq *name@GOTPC(%rip)          */
668
  0, 0, 0, 0,      /* replaced with offset to this symbol in .got.  */
669
  0x90         /* nop               */
670
};
671
672
/* Entries for IBT-enabled branches with BND prefix in the non-lazey
673
   procedure linkage table look like this.  They have the same size as
674
   the lazy PLT entry.  */
675
676
static const bfd_byte elf_x86_64_non_lazy_bnd_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
677
{
678
  0xf3, 0x0f, 0x1e, 0xfa, /* endbr64           */
679
  0xf2, 0xff, 0x25,   /* bnd jmpq *name@GOTPC(%rip)  */
680
  0, 0, 0, 0,  /* replaced with offset to this symbol in .got. */
681
  0x0f, 0x1f, 0x44, 0x00, 0x00  /* nopl 0x0(%rax,%rax,1)       */
682
};
683
684
/* Entries for branches with IBT-enabled in the non-lazey procedure
685
   linkage table look like this.  They have the same size as the lazy
686
   PLT entry.  */
687
688
static const bfd_byte elf_x86_64_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
689
{
690
  0xf3, 0x0f, 0x1e, 0xfa,      /* endbr64          */
691
  0xff, 0x25,          /* jmpq *name@GOTPC(%rip) */
692
  0, 0, 0, 0,  /* replaced with offset to this symbol in .got. */
693
  0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1)  */
694
};
695
696
/* The TLSDESC entry in a lazy procedure linkage table.  */
697
static const bfd_byte elf_x86_64_tlsdesc_plt_entry[LAZY_PLT_ENTRY_SIZE] =
698
{
699
  0xf3, 0x0f, 0x1e, 0xfa,      /* endbr64          */
700
  0xff, 0x35, 8, 0, 0, 0,      /* pushq GOT+8(%rip) */
701
  0xff, 0x25, 16, 0, 0, 0      /* jmpq *GOT+TDG(%rip) */
702
};
703
704
/* .eh_frame covering the lazy .plt section.  */
705
706
static const bfd_byte elf_x86_64_eh_frame_lazy_plt[] =
707
{
708
  PLT_CIE_LENGTH, 0, 0, 0,  /* CIE length */
709
  0, 0, 0, 0,     /* CIE ID */
710
  1,        /* CIE version */
711
  'z', 'R', 0,      /* Augmentation string */
712
  1,        /* Code alignment factor */
713
  0x78,       /* Data alignment factor */
714
  16,       /* Return address column */
715
  1,        /* Augmentation size */
716
  DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
717
  DW_CFA_def_cfa, 7, 8,   /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
718
  DW_CFA_offset + 16, 1,  /* DW_CFA_offset: r16 (rip) at cfa-8 */
719
  DW_CFA_nop, DW_CFA_nop,
720
721
  PLT_FDE_LENGTH, 0, 0, 0,  /* FDE length */
722
  PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
723
  0, 0, 0, 0,     /* R_X86_64_PC32 .plt goes here */
724
  0, 0, 0, 0,     /* .plt size goes here */
725
  0,        /* Augmentation size */
726
  DW_CFA_def_cfa_offset, 16,  /* DW_CFA_def_cfa_offset: 16 */
727
  DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
728
  DW_CFA_def_cfa_offset, 24,  /* DW_CFA_def_cfa_offset: 24 */
729
  DW_CFA_advance_loc + 10,  /* DW_CFA_advance_loc: 10 to __PLT__+16 */
730
  DW_CFA_def_cfa_expression,  /* DW_CFA_def_cfa_expression */
731
  11,       /* Block length */
732
  DW_OP_breg7, 8,   /* DW_OP_breg7 (rsp): 8 */
733
  DW_OP_breg16, 0,    /* DW_OP_breg16 (rip): 0 */
734
  DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
735
  DW_OP_lit3, DW_OP_shl, DW_OP_plus,
736
  DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
737
};
738
739
/* .eh_frame covering the lazy BND .plt section.  */
740
741
static const bfd_byte elf_x86_64_eh_frame_lazy_bnd_plt[] =
742
{
743
  PLT_CIE_LENGTH, 0, 0, 0,  /* CIE length */
744
  0, 0, 0, 0,     /* CIE ID */
745
  1,        /* CIE version */
746
  'z', 'R', 0,      /* Augmentation string */
747
  1,        /* Code alignment factor */
748
  0x78,       /* Data alignment factor */
749
  16,       /* Return address column */
750
  1,        /* Augmentation size */
751
  DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
752
  DW_CFA_def_cfa, 7, 8,   /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
753
  DW_CFA_offset + 16, 1,  /* DW_CFA_offset: r16 (rip) at cfa-8 */
754
  DW_CFA_nop, DW_CFA_nop,
755
756
  PLT_FDE_LENGTH, 0, 0, 0,  /* FDE length */
757
  PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
758
  0, 0, 0, 0,     /* R_X86_64_PC32 .plt goes here */
759
  0, 0, 0, 0,     /* .plt size goes here */
760
  0,        /* Augmentation size */
761
  DW_CFA_def_cfa_offset, 16,  /* DW_CFA_def_cfa_offset: 16 */
762
  DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
763
  DW_CFA_def_cfa_offset, 24,  /* DW_CFA_def_cfa_offset: 24 */
764
  DW_CFA_advance_loc + 10,  /* DW_CFA_advance_loc: 10 to __PLT__+16 */
765
  DW_CFA_def_cfa_expression,  /* DW_CFA_def_cfa_expression */
766
  11,       /* Block length */
767
  DW_OP_breg7, 8,   /* DW_OP_breg7 (rsp): 8 */
768
  DW_OP_breg16, 0,    /* DW_OP_breg16 (rip): 0 */
769
  DW_OP_lit15, DW_OP_and, DW_OP_lit5, DW_OP_ge,
770
  DW_OP_lit3, DW_OP_shl, DW_OP_plus,
771
  DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
772
};
773
774
/* .eh_frame covering the lazy .plt section with IBT-enabled and BND
775
   prefix.  */
776
777
static const bfd_byte elf_x86_64_eh_frame_lazy_bnd_ibt_plt[] =
778
{
779
  PLT_CIE_LENGTH, 0, 0, 0,  /* CIE length */
780
  0, 0, 0, 0,     /* CIE ID */
781
  1,        /* CIE version */
782
  'z', 'R', 0,      /* Augmentation string */
783
  1,        /* Code alignment factor */
784
  0x78,       /* Data alignment factor */
785
  16,       /* Return address column */
786
  1,        /* Augmentation size */
787
  DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
788
  DW_CFA_def_cfa, 7, 8,   /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
789
  DW_CFA_offset + 16, 1,  /* DW_CFA_offset: r16 (rip) at cfa-8 */
790
  DW_CFA_nop, DW_CFA_nop,
791
792
  PLT_FDE_LENGTH, 0, 0, 0,  /* FDE length */
793
  PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
794
  0, 0, 0, 0,     /* R_X86_64_PC32 .plt goes here */
795
  0, 0, 0, 0,     /* .plt size goes here */
796
  0,        /* Augmentation size */
797
  DW_CFA_def_cfa_offset, 16,  /* DW_CFA_def_cfa_offset: 16 */
798
  DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
799
  DW_CFA_def_cfa_offset, 24,  /* DW_CFA_def_cfa_offset: 24 */
800
  DW_CFA_advance_loc + 10,  /* DW_CFA_advance_loc: 10 to __PLT__+16 */
801
  DW_CFA_def_cfa_expression,  /* DW_CFA_def_cfa_expression */
802
  11,       /* Block length */
803
  DW_OP_breg7, 8,   /* DW_OP_breg7 (rsp): 8 */
804
  DW_OP_breg16, 0,    /* DW_OP_breg16 (rip): 0 */
805
  DW_OP_lit15, DW_OP_and, DW_OP_lit10, DW_OP_ge,
806
  DW_OP_lit3, DW_OP_shl, DW_OP_plus,
807
  DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
808
};
809
810
/* .eh_frame covering the lazy .plt section with IBT-enabled.  */
811
812
static const bfd_byte elf_x86_64_eh_frame_lazy_ibt_plt[] =
813
{
814
  PLT_CIE_LENGTH, 0, 0, 0,  /* CIE length */
815
  0, 0, 0, 0,     /* CIE ID */
816
  1,        /* CIE version */
817
  'z', 'R', 0,      /* Augmentation string */
818
  1,        /* Code alignment factor */
819
  0x78,       /* Data alignment factor */
820
  16,       /* Return address column */
821
  1,        /* Augmentation size */
822
  DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
823
  DW_CFA_def_cfa, 7, 8,   /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
824
  DW_CFA_offset + 16, 1,  /* DW_CFA_offset: r16 (rip) at cfa-8 */
825
  DW_CFA_nop, DW_CFA_nop,
826
827
  PLT_FDE_LENGTH, 0, 0, 0,  /* FDE length */
828
  PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
829
  0, 0, 0, 0,     /* R_X86_64_PC32 .plt goes here */
830
  0, 0, 0, 0,     /* .plt size goes here */
831
  0,        /* Augmentation size */
832
  DW_CFA_def_cfa_offset, 16,  /* DW_CFA_def_cfa_offset: 16 */
833
  DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
834
  DW_CFA_def_cfa_offset, 24,  /* DW_CFA_def_cfa_offset: 24 */
835
  DW_CFA_advance_loc + 10,  /* DW_CFA_advance_loc: 10 to __PLT__+16 */
836
  DW_CFA_def_cfa_expression,  /* DW_CFA_def_cfa_expression */
837
  11,       /* Block length */
838
  DW_OP_breg7, 8,   /* DW_OP_breg7 (rsp): 8 */
839
  DW_OP_breg16, 0,    /* DW_OP_breg16 (rip): 0 */
840
  DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
841
  DW_OP_lit3, DW_OP_shl, DW_OP_plus,
842
  DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
843
};
844
845
/* .eh_frame covering the non-lazy .plt section.  */
846
847
static const bfd_byte elf_x86_64_eh_frame_non_lazy_plt[] =
848
{
849
#define PLT_GOT_FDE_LENGTH    20
850
  PLT_CIE_LENGTH, 0, 0, 0,  /* CIE length */
851
  0, 0, 0, 0,     /* CIE ID */
852
  1,        /* CIE version */
853
  'z', 'R', 0,      /* Augmentation string */
854
  1,        /* Code alignment factor */
855
  0x78,       /* Data alignment factor */
856
  16,       /* Return address column */
857
  1,        /* Augmentation size */
858
  DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
859
  DW_CFA_def_cfa, 7, 8,   /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
860
  DW_CFA_offset + 16, 1,  /* DW_CFA_offset: r16 (rip) at cfa-8 */
861
  DW_CFA_nop, DW_CFA_nop,
862
863
  PLT_GOT_FDE_LENGTH, 0, 0, 0,  /* FDE length */
864
  PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
865
  0, 0, 0, 0,     /* the start of non-lazy .plt goes here */
866
  0, 0, 0, 0,     /* non-lazy .plt size goes here */
867
  0,        /* Augmentation size */
868
  DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop,
869
  DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
870
};
871
872
/* .sframe FRE covering the .plt section entry.  */
873
static const sframe_frame_row_entry elf_x86_64_sframe_plt0_fre1 =
874
{
875
  0, /* SFrame FRE start address.  */
876
  {16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes.  */
877
  SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info.  */
878
};
879
880
/* .sframe FRE covering the .plt section entry.  */
881
static const sframe_frame_row_entry elf_x86_64_sframe_plt0_fre2 =
882
{
883
  6, /* SFrame FRE start address.  */
884
  {24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes.  */
885
  SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info.  */
886
};
887
888
/* .sframe FRE covering the .plt section entry.  */
889
static const sframe_frame_row_entry elf_x86_64_sframe_pltn_fre1 =
890
{
891
  0, /* SFrame FRE start address.  */
892
  {8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes.  */
893
  SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info.  */
894
};
895
896
/* .sframe FRE covering the .plt section entry.  */
897
static const sframe_frame_row_entry elf_x86_64_sframe_pltn_fre2 =
898
{
899
  11, /* SFrame FRE start address.  */
900
  {16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes.  */
901
  SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info.  */
902
};
903
904
/* .sframe FRE covering the .plt section entry for IBT.  */
905
static const sframe_frame_row_entry elf_x86_64_sframe_ibt_pltn_fre2 =
906
{
907
  9, /* SFrame FRE start address.  */
908
  {16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes.  */
909
  SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info.  */
910
};
911
912
/* .sframe FRE covering the second .plt section entry.  */
913
static const sframe_frame_row_entry elf_x86_64_sframe_sec_pltn_fre1 =
914
{
915
  0, /* SFrame FRE start address.  */
916
  {8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes.  */
917
  SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info.  */
918
};
919
920
/* .sframe FRE covering the .plt.got section entry.  */
921
static const sframe_frame_row_entry elf_x86_64_sframe_pltgot_fre1 =
922
{
923
  0, /* SFrame FRE start address.  */
924
  {16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes.  */
925
  SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info.  */
926
};
927
928
/* SFrame helper object for non-lazy PLT.  */
929
static const struct elf_x86_sframe_plt elf_x86_64_sframe_non_lazy_plt =
930
{
931
  LAZY_PLT_ENTRY_SIZE,
932
  2, /* Number of FREs for PLT0.  */
933
  /* Array of SFrame FREs for plt0.  */
934
  { &elf_x86_64_sframe_plt0_fre1, &elf_x86_64_sframe_plt0_fre2 },
935
  LAZY_PLT_ENTRY_SIZE,
936
  1, /* Number of FREs for PLTn.  */
937
  /* Array of SFrame FREs for plt.  */
938
  { &elf_x86_64_sframe_sec_pltn_fre1 },
939
  0,
940
  0, /* There is no second PLT necessary.  */
941
  { },
942
  NON_LAZY_PLT_ENTRY_SIZE,
943
  1, /* Number of FREs for PLT GOT.  */
944
  /* Array of SFrame FREs for PLT GOT.  */
945
  { &elf_x86_64_sframe_pltgot_fre1 },
946
};
947
948
/* SFrame helper object for non-lazy IBT enabled PLT.  */
949
static const struct elf_x86_sframe_plt elf_x86_64_sframe_non_lazy_ibt_plt =
950
{
951
  LAZY_PLT_ENTRY_SIZE,
952
  2, /* Number of FREs for PLT0.  */
953
  /* Array of SFrame FREs for plt0.  */
954
  { &elf_x86_64_sframe_plt0_fre1, &elf_x86_64_sframe_plt0_fre2 },
955
  LAZY_PLT_ENTRY_SIZE,
956
  1, /* Number of FREs for PLTn.  */
957
  /* Array of SFrame FREs for plt.  */
958
  { &elf_x86_64_sframe_sec_pltn_fre1 },
959
  0,
960
  0, /* There is no second PLT necessary.  */
961
  { },
962
  LAZY_PLT_ENTRY_SIZE,
963
  1, /* Number of FREs for PLT GOT.  */
964
  /* Array of SFrame FREs for PLT GOT.  */
965
  { &elf_x86_64_sframe_pltgot_fre1 },
966
};
967
968
/* SFrame helper object for lazy PLT. */
969
static const struct elf_x86_sframe_plt elf_x86_64_sframe_plt =
970
{
971
  LAZY_PLT_ENTRY_SIZE,
972
  2, /* Number of FREs for PLT0.  */
973
  /* Array of SFrame FREs for plt0.  */
974
  { &elf_x86_64_sframe_plt0_fre1, &elf_x86_64_sframe_plt0_fre2 },
975
  LAZY_PLT_ENTRY_SIZE,
976
  2, /* Number of FREs for PLTn.  */
977
  /* Array of SFrame FREs for plt.  */
978
  { &elf_x86_64_sframe_pltn_fre1, &elf_x86_64_sframe_pltn_fre2 },
979
  NON_LAZY_PLT_ENTRY_SIZE,
980
  1, /* Number of FREs for second PLT.  */
981
  /* Array of SFrame FREs for second PLT.  */
982
  { &elf_x86_64_sframe_sec_pltn_fre1 },
983
  NON_LAZY_PLT_ENTRY_SIZE,
984
  1, /* Number of FREs for PLT GOT.  */
985
  /* Array of SFrame FREs for PLT GOT.  */
986
  { &elf_x86_64_sframe_pltgot_fre1 },
987
};
988
989
/* SFrame helper object for lazy PLT with IBT. */
990
static const struct elf_x86_sframe_plt elf_x86_64_sframe_ibt_plt =
991
{
992
  LAZY_PLT_ENTRY_SIZE,
993
  2, /* Number of FREs for PLT0.  */
994
  /* Array of SFrame FREs for plt0.  */
995
  { &elf_x86_64_sframe_plt0_fre1, &elf_x86_64_sframe_plt0_fre2 },
996
  LAZY_PLT_ENTRY_SIZE,
997
  2, /* Number of FREs for PLTn.  */
998
  /* Array of SFrame FREs for plt.  */
999
  { &elf_x86_64_sframe_pltn_fre1, &elf_x86_64_sframe_ibt_pltn_fre2 },
1000
  LAZY_PLT_ENTRY_SIZE,
1001
  1, /* Number of FREs for second PLT.  */
1002
  /* Array of SFrame FREs for second plt.  */
1003
  { &elf_x86_64_sframe_sec_pltn_fre1 },
1004
  LAZY_PLT_ENTRY_SIZE,
1005
  1, /* Number of FREs for PLT GOT.  */
1006
  /* Array of SFrame FREs for PLT GOT.  */
1007
  { &elf_x86_64_sframe_pltgot_fre1 },
1008
};
1009
1010
/* These are the standard parameters.  */
1011
static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_plt =
1012
  {
1013
    elf_x86_64_lazy_plt0_entry,   /* plt0_entry */
1014
    LAZY_PLT_ENTRY_SIZE,    /* plt0_entry_size */
1015
    elf_x86_64_lazy_plt_entry,    /* plt_entry */
1016
    LAZY_PLT_ENTRY_SIZE,    /* plt_entry_size */
1017
    elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
1018
    LAZY_PLT_ENTRY_SIZE,    /* plt_tlsdesc_entry_size */
1019
    6,          /* plt_tlsdesc_got1_offset */
1020
    12,         /* plt_tlsdesc_got2_offset */
1021
    10,         /* plt_tlsdesc_got1_insn_end */
1022
    16,         /* plt_tlsdesc_got2_insn_end */
1023
    2,          /* plt0_got1_offset */
1024
    8,          /* plt0_got2_offset */
1025
    12,         /* plt0_got2_insn_end */
1026
    2,          /* plt_got_offset */
1027
    7,          /* plt_reloc_offset */
1028
    12,         /* plt_plt_offset */
1029
    6,          /* plt_got_insn_size */
1030
    LAZY_PLT_ENTRY_SIZE,    /* plt_plt_insn_end */
1031
    6,          /* plt_lazy_offset */
1032
    elf_x86_64_lazy_plt0_entry,   /* pic_plt0_entry */
1033
    elf_x86_64_lazy_plt_entry,    /* pic_plt_entry */
1034
    elf_x86_64_eh_frame_lazy_plt, /* eh_frame_plt */
1035
    sizeof (elf_x86_64_eh_frame_lazy_plt) /* eh_frame_plt_size */
1036
  };
1037
1038
static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_plt =
1039
  {
1040
    elf_x86_64_non_lazy_plt_entry,  /* plt_entry */
1041
    elf_x86_64_non_lazy_plt_entry,  /* pic_plt_entry */
1042
    NON_LAZY_PLT_ENTRY_SIZE,    /* plt_entry_size */
1043
    2,          /* plt_got_offset */
1044
    6,          /* plt_got_insn_size */
1045
    elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
1046
    sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
1047
  };
1048
1049
static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_bnd_plt =
1050
  {
1051
    elf_x86_64_lazy_bnd_plt0_entry, /* plt0_entry */
1052
    LAZY_PLT_ENTRY_SIZE,    /* plt0_entry_size */
1053
    elf_x86_64_lazy_bnd_plt_entry,  /* plt_entry */
1054
    LAZY_PLT_ENTRY_SIZE,    /* plt_entry_size */
1055
    elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
1056
    LAZY_PLT_ENTRY_SIZE,    /* plt_tlsdesc_entry_size */
1057
    6,          /* plt_tlsdesc_got1_offset */
1058
    12,         /* plt_tlsdesc_got2_offset */
1059
    10,         /* plt_tlsdesc_got1_insn_end */
1060
    16,         /* plt_tlsdesc_got2_insn_end */
1061
    2,          /* plt0_got1_offset */
1062
    1+8,        /* plt0_got2_offset */
1063
    1+12,       /* plt0_got2_insn_end */
1064
    1+2,        /* plt_got_offset */
1065
    1,          /* plt_reloc_offset */
1066
    7,          /* plt_plt_offset */
1067
    1+6,        /* plt_got_insn_size */
1068
    11,         /* plt_plt_insn_end */
1069
    0,          /* plt_lazy_offset */
1070
    elf_x86_64_lazy_bnd_plt0_entry, /* pic_plt0_entry */
1071
    elf_x86_64_lazy_bnd_plt_entry,  /* pic_plt_entry */
1072
    elf_x86_64_eh_frame_lazy_bnd_plt, /* eh_frame_plt */
1073
    sizeof (elf_x86_64_eh_frame_lazy_bnd_plt) /* eh_frame_plt_size */
1074
  };
1075
1076
static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_bnd_plt =
1077
  {
1078
    elf_x86_64_non_lazy_bnd_plt_entry,  /* plt_entry */
1079
    elf_x86_64_non_lazy_bnd_plt_entry,  /* pic_plt_entry */
1080
    NON_LAZY_PLT_ENTRY_SIZE,    /* plt_entry_size */
1081
    1+2,        /* plt_got_offset */
1082
    1+6,        /* plt_got_insn_size */
1083
    elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
1084
    sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
1085
  };
1086
1087
static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_bnd_ibt_plt =
1088
  {
1089
    elf_x86_64_lazy_bnd_plt0_entry, /* plt0_entry */
1090
    LAZY_PLT_ENTRY_SIZE,    /* plt0_entry_size */
1091
    elf_x86_64_lazy_bnd_ibt_plt_entry,  /* plt_entry */
1092
    LAZY_PLT_ENTRY_SIZE,    /* plt_entry_size */
1093
    elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
1094
    LAZY_PLT_ENTRY_SIZE,    /* plt_tlsdesc_entry_size */
1095
    6,          /* plt_tlsdesc_got1_offset */
1096
    12,         /* plt_tlsdesc_got2_offset */
1097
    10,         /* plt_tlsdesc_got1_insn_end */
1098
    16,         /* plt_tlsdesc_got2_insn_end */
1099
    2,          /* plt0_got1_offset */
1100
    1+8,        /* plt0_got2_offset */
1101
    1+12,       /* plt0_got2_insn_end */
1102
    4+1+2,        /* plt_got_offset */
1103
    4+1,        /* plt_reloc_offset */
1104
    4+1+6,        /* plt_plt_offset */
1105
    4+1+6,        /* plt_got_insn_size */
1106
    4+1+5+5,        /* plt_plt_insn_end */
1107
    0,          /* plt_lazy_offset */
1108
    elf_x86_64_lazy_bnd_plt0_entry, /* pic_plt0_entry */
1109
    elf_x86_64_lazy_bnd_ibt_plt_entry,  /* pic_plt_entry */
1110
    elf_x86_64_eh_frame_lazy_bnd_ibt_plt, /* eh_frame_plt */
1111
    sizeof (elf_x86_64_eh_frame_lazy_bnd_ibt_plt) /* eh_frame_plt_size */
1112
  };
1113
1114
static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_ibt_plt =
1115
  {
1116
    elf_x86_64_lazy_plt0_entry,   /* plt0_entry */
1117
    LAZY_PLT_ENTRY_SIZE,    /* plt0_entry_size */
1118
    elf_x86_64_lazy_ibt_plt_entry,  /* plt_entry */
1119
    LAZY_PLT_ENTRY_SIZE,    /* plt_entry_size */
1120
    elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
1121
    LAZY_PLT_ENTRY_SIZE,    /* plt_tlsdesc_entry_size */
1122
    6,          /* plt_tlsdesc_got1_offset */
1123
    12,         /* plt_tlsdesc_got2_offset */
1124
    10,         /* plt_tlsdesc_got1_insn_end */
1125
    16,         /* plt_tlsdesc_got2_insn_end */
1126
    2,          /* plt0_got1_offset */
1127
    8,          /* plt0_got2_offset */
1128
    12,         /* plt0_got2_insn_end */
1129
    4+2,        /* plt_got_offset */
1130
    4+1,        /* plt_reloc_offset */
1131
    4+6,        /* plt_plt_offset */
1132
    4+6,        /* plt_got_insn_size */
1133
    4+5+5,        /* plt_plt_insn_end */
1134
    0,          /* plt_lazy_offset */
1135
    elf_x86_64_lazy_plt0_entry,   /* pic_plt0_entry */
1136
    elf_x86_64_lazy_ibt_plt_entry,  /* pic_plt_entry */
1137
    elf_x86_64_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
1138
    sizeof (elf_x86_64_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
1139
  };
1140
1141
static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_bnd_ibt_plt =
1142
  {
1143
    elf_x86_64_non_lazy_bnd_ibt_plt_entry, /* plt_entry */
1144
    elf_x86_64_non_lazy_bnd_ibt_plt_entry, /* pic_plt_entry */
1145
    LAZY_PLT_ENTRY_SIZE,    /* plt_entry_size */
1146
    4+1+2,        /* plt_got_offset */
1147
    4+1+6,        /* plt_got_insn_size */
1148
    elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
1149
    sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
1150
  };
1151
1152
static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_ibt_plt =
1153
  {
1154
    elf_x86_64_non_lazy_ibt_plt_entry,  /* plt_entry */
1155
    elf_x86_64_non_lazy_ibt_plt_entry,  /* pic_plt_entry */
1156
    LAZY_PLT_ENTRY_SIZE,    /* plt_entry_size */
1157
    4+2,        /* plt_got_offset */
1158
    4+6,        /* plt_got_insn_size */
1159
    elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
1160
    sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
1161
  };
1162
1163
static bool
1164
elf64_x86_64_elf_object_p (bfd *abfd)
1165
2.14k
{
1166
  /* Set the right machine number for an x86-64 elf64 file.  */
1167
2.14k
  bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
1168
2.14k
  return true;
1169
2.14k
}
1170
1171
static bool
1172
elf32_x86_64_elf_object_p (bfd *abfd)
1173
78
{
1174
  /* Set the right machine number for an x86-64 elf32 file.  */
1175
78
  bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
1176
78
  return true;
1177
78
}
1178
1179
/* Return TRUE if the TLS access code sequence support transition
1180
   from R_TYPE.  */
1181
1182
static enum elf_x86_tls_error_type
1183
elf_x86_64_check_tls_transition (bfd *abfd,
1184
         struct bfd_link_info *info,
1185
         asection *sec,
1186
         bfd_byte *contents,
1187
         Elf_Internal_Shdr *symtab_hdr,
1188
         struct elf_link_hash_entry **sym_hashes,
1189
         unsigned int r_type,
1190
         const Elf_Internal_Rela *rel,
1191
         const Elf_Internal_Rela *relend)
1192
0
{
1193
0
  unsigned int val;
1194
0
  unsigned long r_symndx;
1195
0
  bool largepic = false;
1196
0
  struct elf_link_hash_entry *h;
1197
0
  bfd_vma offset;
1198
0
  struct elf_x86_link_hash_table *htab;
1199
0
  bfd_byte *call;
1200
0
  bool indirect_call;
1201
1202
0
  htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1203
0
  offset = rel->r_offset;
1204
0
  switch (r_type)
1205
0
    {
1206
0
    case R_X86_64_TLSGD:
1207
0
    case R_X86_64_TLSLD:
1208
0
      if ((rel + 1) >= relend)
1209
0
  return elf_x86_tls_error_yes;
1210
1211
0
      if (r_type == R_X86_64_TLSGD)
1212
0
  {
1213
    /* Check transition from GD access model.  For 64bit, only
1214
    .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1215
    .word 0x6666; rex64; call __tls_get_addr@PLT
1216
       or
1217
    .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1218
    .byte 0x66; rex64
1219
    call *__tls_get_addr@GOTPCREL(%rip)
1220
    which may be converted to
1221
    addr32 call __tls_get_addr
1222
       can transit to different access model.  For 32bit, only
1223
    leaq foo@tlsgd(%rip), %rdi
1224
    .word 0x6666; rex64; call __tls_get_addr@PLT
1225
       or
1226
    leaq foo@tlsgd(%rip), %rdi
1227
    .byte 0x66; rex64
1228
    call *__tls_get_addr@GOTPCREL(%rip)
1229
    which may be converted to
1230
    addr32 call __tls_get_addr
1231
       can transit to different access model.  For largepic,
1232
       we also support:
1233
    leaq foo@tlsgd(%rip), %rdi
1234
    movabsq $__tls_get_addr@pltoff, %rax
1235
    addq $r15, %rax
1236
    call *%rax
1237
       or
1238
    leaq foo@tlsgd(%rip), %rdi
1239
    movabsq $__tls_get_addr@pltoff, %rax
1240
    addq $rbx, %rax
1241
    call *%rax  */
1242
1243
0
    static const unsigned char leaq[] = { 0x66, 0x48, 0x8d, 0x3d };
1244
1245
0
    if ((offset + 12) > sec->size)
1246
0
      return elf_x86_tls_error_yes;
1247
1248
0
    call = contents + offset + 4;
1249
0
    if (call[0] != 0x66
1250
0
        || !((call[1] == 0x48
1251
0
        && call[2] == 0xff
1252
0
        && call[3] == 0x15)
1253
0
       || (call[1] == 0x48
1254
0
           && call[2] == 0x67
1255
0
           && call[3] == 0xe8)
1256
0
       || (call[1] == 0x66
1257
0
           && call[2] == 0x48
1258
0
           && call[3] == 0xe8)))
1259
0
      {
1260
0
        if (!ABI_64_P (abfd)
1261
0
      || (offset + 19) > sec->size
1262
0
      || offset < 3
1263
0
      || memcmp (call - 7, leaq + 1, 3) != 0
1264
0
      || memcmp (call, "\x48\xb8", 2) != 0
1265
0
      || call[11] != 0x01
1266
0
      || call[13] != 0xff
1267
0
      || call[14] != 0xd0
1268
0
      || !((call[10] == 0x48 && call[12] == 0xd8)
1269
0
           || (call[10] == 0x4c && call[12] == 0xf8)))
1270
0
    return elf_x86_tls_error_yes;
1271
0
        largepic = true;
1272
0
      }
1273
0
    else if (ABI_64_P (abfd))
1274
0
      {
1275
0
        if (offset < 4
1276
0
      || memcmp (contents + offset - 4, leaq, 4) != 0)
1277
0
    return elf_x86_tls_error_yes;
1278
0
      }
1279
0
    else
1280
0
      {
1281
0
        if (offset < 3
1282
0
      || memcmp (contents + offset - 3, leaq + 1, 3) != 0)
1283
0
    return elf_x86_tls_error_yes;
1284
0
      }
1285
0
    indirect_call = call[2] == 0xff;
1286
0
  }
1287
0
      else
1288
0
  {
1289
    /* Check transition from LD access model.  Only
1290
    leaq foo@tlsld(%rip), %rdi;
1291
    call __tls_get_addr@PLT
1292
       or
1293
    leaq foo@tlsld(%rip), %rdi;
1294
    call *__tls_get_addr@GOTPCREL(%rip)
1295
    which may be converted to
1296
    addr32 call __tls_get_addr
1297
       can transit to different access model.  For largepic
1298
       we also support:
1299
    leaq foo@tlsld(%rip), %rdi
1300
    movabsq $__tls_get_addr@pltoff, %rax
1301
    addq $r15, %rax
1302
    call *%rax
1303
       or
1304
    leaq foo@tlsld(%rip), %rdi
1305
    movabsq $__tls_get_addr@pltoff, %rax
1306
    addq $rbx, %rax
1307
    call *%rax  */
1308
1309
0
    static const unsigned char lea[] = { 0x48, 0x8d, 0x3d };
1310
1311
0
    if (offset < 3 || (offset + 9) > sec->size)
1312
0
      return elf_x86_tls_error_yes;
1313
1314
0
    if (memcmp (contents + offset - 3, lea, 3) != 0)
1315
0
      return elf_x86_tls_error_yes;
1316
1317
0
    call = contents + offset + 4;
1318
0
    if (!(call[0] == 0xe8
1319
0
    || (call[0] == 0xff && call[1] == 0x15)
1320
0
    || (call[0] == 0x67 && call[1] == 0xe8)))
1321
0
      {
1322
0
        if (!ABI_64_P (abfd)
1323
0
      || (offset + 19) > sec->size
1324
0
      || memcmp (call, "\x48\xb8", 2) != 0
1325
0
      || call[11] != 0x01
1326
0
      || call[13] != 0xff
1327
0
      || call[14] != 0xd0
1328
0
      || !((call[10] == 0x48 && call[12] == 0xd8)
1329
0
           || (call[10] == 0x4c && call[12] == 0xf8)))
1330
0
    return elf_x86_tls_error_yes;
1331
0
        largepic = true;
1332
0
      }
1333
0
    indirect_call = call[0] == 0xff;
1334
0
  }
1335
1336
0
      r_symndx = htab->r_sym (rel[1].r_info);
1337
0
      if (r_symndx < symtab_hdr->sh_info)
1338
0
  return elf_x86_tls_error_yes;
1339
1340
0
      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1341
0
      if (h == NULL
1342
0
    || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
1343
0
  return elf_x86_tls_error_yes;
1344
0
      else
1345
0
  {
1346
0
    r_type = (ELF32_R_TYPE (rel[1].r_info)
1347
0
        & ~R_X86_64_converted_reloc_bit);
1348
0
    if (largepic)
1349
0
      return (r_type == R_X86_64_PLTOFF64
1350
0
        ? elf_x86_tls_error_none
1351
0
        : elf_x86_tls_error_yes);
1352
0
    else if (indirect_call)
1353
0
      return ((r_type == R_X86_64_GOTPCRELX
1354
0
         || r_type == R_X86_64_GOTPCREL)
1355
0
        ? elf_x86_tls_error_none
1356
0
        : elf_x86_tls_error_yes);
1357
0
    else
1358
0
      return ((r_type == R_X86_64_PC32
1359
0
         || r_type == R_X86_64_PLT32)
1360
0
        ? elf_x86_tls_error_none
1361
0
        : elf_x86_tls_error_yes);
1362
0
  }
1363
1364
0
    case R_X86_64_CODE_4_GOTTPOFF:
1365
      /* Check transition from IE access model:
1366
    mov foo@gottpoff(%rip), %reg
1367
    add foo@gottpoff(%rip), %reg
1368
    where reg is one of r16 to r31.
1369
         For x32 also:
1370
    movrs foo@gottpoff(%rip), %reg
1371
    where no REX prefix is present.  */
1372
1373
0
      if (offset < 4
1374
0
    || (offset + 4) > sec->size)
1375
0
  return elf_x86_tls_error_yes;
1376
1377
0
      if (!ABI_64_P (abfd)
1378
0
    && contents[offset - 4] == 0x0f
1379
0
    && contents[offset - 3] == 0x38
1380
0
    && contents[offset - 2] == 0x8b)
1381
0
  goto check_gottpoff_modrm;
1382
1383
0
      if (contents[offset - 4] != 0xd5)
1384
0
  return elf_x86_tls_error_yes;
1385
1386
0
      goto check_gottpoff;
1387
1388
0
    case R_X86_64_CODE_5_GOTTPOFF:
1389
      /* Check transition from IE access model:
1390
    movrs foo@gottpoff(%rip), %reg
1391
    where reg isn't one of r16 to r31.  */
1392
0
      if (offset < 5
1393
0
    || (offset + 4) > sec->size
1394
0
    || (contents[offset - 5] | (ABI_64_P (abfd) ? 7 : 0xf)) != 0x4f
1395
0
    || contents[offset - 4] != 0x0f
1396
0
    || contents[offset - 3] != 0x38
1397
0
    || contents[offset - 2] != 0x8b)
1398
0
  return elf_x86_tls_error_yes;
1399
1400
0
      goto check_gottpoff_modrm;
1401
1402
0
    case R_X86_64_CODE_6_GOTTPOFF:
1403
      /* Check transition from IE access model:
1404
    add %reg1, foo@gottpoff(%rip), %reg2
1405
    movrs foo@gottpoff(%rip), %reg
1406
    where reg1/reg2/reg are one of r16 to r31.  */
1407
1408
0
      if (offset < 6
1409
0
    || (offset + 4) > sec->size
1410
0
    || contents[offset - 6] != 0x62)
1411
0
  return elf_x86_tls_error_yes;
1412
1413
0
      val = bfd_get_8 (abfd, contents + offset - 2);
1414
0
      if (val != 0x01 && val != 0x03 && val != 0x8b)
1415
0
  return elf_x86_tls_error_add_movrs;
1416
1417
0
      goto check_gottpoff_modrm;
1418
1419
0
    case R_X86_64_GOTTPOFF:
1420
      /* Check transition from IE access model:
1421
    mov foo@gottpoff(%rip), %reg
1422
    add foo@gottpoff(%rip), %reg
1423
       */
1424
1425
      /* Check REX prefix first.  */
1426
0
      if (offset >= 3 && (offset + 4) <= sec->size)
1427
0
  {
1428
0
    val = bfd_get_8 (abfd, contents + offset - 3);
1429
0
    if (val != 0x48 && val != 0x4c)
1430
0
      {
1431
        /* X32 may have 0x44 REX prefix or no REX prefix.  */
1432
0
        if (ABI_64_P (abfd))
1433
0
    return elf_x86_tls_error_yes;
1434
0
      }
1435
0
  }
1436
0
      else
1437
0
  {
1438
    /* X32 may not have any REX prefix.  */
1439
0
    if (ABI_64_P (abfd))
1440
0
      return elf_x86_tls_error_yes;
1441
0
    if (offset < 2 || (offset + 3) > sec->size)
1442
0
      return elf_x86_tls_error_yes;
1443
0
  }
1444
1445
0
 check_gottpoff:
1446
0
      val = bfd_get_8 (abfd, contents + offset - 2);
1447
0
      if (val != 0x8b && val != 0x03)
1448
0
  return elf_x86_tls_error_add_mov;
1449
1450
0
 check_gottpoff_modrm:
1451
0
      val = bfd_get_8 (abfd, contents + offset - 1);
1452
0
      return ((val & 0xc7) == 5
1453
0
        ? elf_x86_tls_error_none
1454
0
        : elf_x86_tls_error_yes);
1455
1456
0
    case R_X86_64_CODE_4_GOTPC32_TLSDESC:
1457
      /* Check transition from GDesc access model:
1458
    lea x@tlsdesc(%rip), %reg
1459
   where reg is one of r16 to r31.  */
1460
1461
0
      if (offset < 4
1462
0
    || (offset + 4) > sec->size
1463
0
    || contents[offset - 4] != 0xd5)
1464
0
  return elf_x86_tls_error_yes;
1465
1466
0
      goto check_tlsdesc;
1467
1468
0
    case R_X86_64_GOTPC32_TLSDESC:
1469
      /* Check transition from GDesc access model:
1470
    leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
1471
    rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
1472
1473
   Make sure it's a leaq adding rip to a 32-bit offset
1474
   into any register, although it's probably almost always
1475
   going to be rax.  */
1476
1477
0
      if (offset < 3 || (offset + 4) > sec->size)
1478
0
  return elf_x86_tls_error_yes;
1479
1480
0
      val = bfd_get_8 (abfd, contents + offset - 3);
1481
0
      val &= 0xfb;
1482
0
      if (val != 0x48 && (ABI_64_P (abfd) || val != 0x40))
1483
0
  return elf_x86_tls_error_yes;
1484
1485
0
 check_tlsdesc:
1486
0
      if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1487
0
  return elf_x86_tls_error_lea;
1488
1489
0
      val = bfd_get_8 (abfd, contents + offset - 1);
1490
0
      return ((val & 0xc7) == 0x05
1491
0
        ? elf_x86_tls_error_none
1492
0
        : elf_x86_tls_error_yes);
1493
1494
0
    case R_X86_64_TLSDESC_CALL:
1495
      /* It has been checked in elf_x86_64_tls_transition.  */
1496
0
      return elf_x86_tls_error_none;
1497
1498
0
    default:
1499
0
      abort ();
1500
0
    }
1501
0
}
1502
1503
/* Return TRUE if the TLS access transition is OK or no transition
1504
   will be performed.  Update R_TYPE if there is a transition.  */
1505
1506
static bool
1507
elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
1508
         asection *sec, bfd_byte *contents,
1509
         Elf_Internal_Shdr *symtab_hdr,
1510
         struct elf_link_hash_entry **sym_hashes,
1511
         unsigned int *r_type, int tls_type,
1512
         const Elf_Internal_Rela *rel,
1513
         const Elf_Internal_Rela *relend,
1514
         struct elf_link_hash_entry *h,
1515
         Elf_Internal_Sym *sym,
1516
         bool from_relocate_section)
1517
0
{
1518
0
  unsigned int from_type = *r_type;
1519
0
  unsigned int to_type = from_type;
1520
0
  bool check = true;
1521
0
  bfd_vma offset;
1522
0
  bfd_byte *call;
1523
1524
  /* Skip TLS transition for functions.  */
1525
0
  if (h != NULL
1526
0
      && (h->type == STT_FUNC
1527
0
    || h->type == STT_GNU_IFUNC))
1528
0
    return true;
1529
1530
0
  switch (from_type)
1531
0
    {
1532
0
    case R_X86_64_TLSDESC_CALL:
1533
      /* Check valid GDesc call:
1534
    call *x@tlscall(%rax) <--- LP64 mode.
1535
    call *x@tlscall(%eax) <--- X32 mode.
1536
       */
1537
0
      offset = rel->r_offset;
1538
0
      call = NULL;
1539
0
      if (offset + 2 <= sec->size)
1540
0
  {
1541
0
    unsigned int prefix;
1542
0
    call = contents + offset;
1543
0
    prefix = 0;
1544
0
    if (!ABI_64_P (abfd))
1545
0
      {
1546
        /* Check for call *x@tlscall(%eax).  */
1547
0
        if (call[0] == 0x67)
1548
0
    {
1549
0
      prefix = 1;
1550
0
      if (offset + 3 > sec->size)
1551
0
        call = NULL;
1552
0
    }
1553
0
      }
1554
1555
    /* Make sure that it's a call *x@tlscall(%rax).  */
1556
0
    if (call != NULL
1557
0
        && (call[prefix] != 0xff || call[1 + prefix] != 0x10))
1558
0
      call = NULL;
1559
0
  }
1560
1561
0
      if (call == NULL)
1562
0
  {
1563
0
    _bfd_x86_elf_link_report_tls_transition_error
1564
0
      (info, abfd, sec, symtab_hdr, h, sym, rel,
1565
0
       "R_X86_64_TLSDESC_CALL", NULL,
1566
0
       elf_x86_tls_error_indirect_call);
1567
0
    return false;
1568
0
  }
1569
1570
      /* Fall through.  */
1571
1572
0
    case R_X86_64_TLSGD:
1573
0
    case R_X86_64_GOTPC32_TLSDESC:
1574
0
    case R_X86_64_CODE_4_GOTPC32_TLSDESC:
1575
0
    case R_X86_64_GOTTPOFF:
1576
0
    case R_X86_64_CODE_4_GOTTPOFF:
1577
0
    case R_X86_64_CODE_5_GOTTPOFF:
1578
0
    case R_X86_64_CODE_6_GOTTPOFF:
1579
0
      if (bfd_link_executable (info))
1580
0
  {
1581
0
    if (h == NULL)
1582
0
      to_type = R_X86_64_TPOFF32;
1583
0
    else
1584
0
      to_type = R_X86_64_GOTTPOFF;
1585
0
  }
1586
1587
      /* When we are called from elf_x86_64_relocate_section, there may
1588
   be additional transitions based on TLS_TYPE.  */
1589
0
      if (from_relocate_section)
1590
0
  {
1591
0
    unsigned int new_to_type = to_type;
1592
1593
0
    if (TLS_TRANSITION_IE_TO_LE_P (info, h, tls_type))
1594
0
      new_to_type = R_X86_64_TPOFF32;
1595
1596
0
    if (to_type == R_X86_64_TLSGD
1597
0
        || to_type == R_X86_64_GOTPC32_TLSDESC
1598
0
        || to_type == R_X86_64_CODE_4_GOTPC32_TLSDESC
1599
0
        || to_type == R_X86_64_TLSDESC_CALL)
1600
0
      {
1601
0
        if (tls_type == GOT_TLS_IE)
1602
0
    new_to_type = R_X86_64_GOTTPOFF;
1603
0
      }
1604
1605
    /* We checked the transition before when we were called from
1606
       elf_x86_64_scan_relocs.  We only want to check the new
1607
       transition which hasn't been checked before.  */
1608
0
    check = (new_to_type != to_type
1609
0
       && (from_type == to_type
1610
0
           || (from_type == R_X86_64_CODE_4_GOTTPOFF
1611
0
         && to_type == R_X86_64_GOTTPOFF)
1612
0
           || (from_type == R_X86_64_CODE_5_GOTTPOFF
1613
0
         && to_type == R_X86_64_GOTTPOFF)
1614
0
           || (from_type == R_X86_64_CODE_6_GOTTPOFF
1615
0
         && to_type == R_X86_64_GOTTPOFF)));
1616
0
    to_type = new_to_type;
1617
0
  }
1618
1619
0
      break;
1620
1621
0
    case R_X86_64_TLSLD:
1622
0
      if (bfd_link_executable (info))
1623
0
  to_type = R_X86_64_TPOFF32;
1624
0
      break;
1625
1626
0
    default:
1627
0
      return true;
1628
0
    }
1629
1630
0
  if ((elf_section_type (sec) != SHT_PROGBITS
1631
0
       || (sec->flags & SEC_CODE) == 0))
1632
0
    {
1633
0
      reloc_howto_type *howto = elf_x86_64_rtype_to_howto (abfd,
1634
0
                 from_type);
1635
0
      _bfd_x86_elf_link_report_tls_invalid_section_error
1636
0
  (abfd, sec, symtab_hdr, h, sym, howto);
1637
0
      return false;
1638
0
    }
1639
1640
  /* Return TRUE if there is no transition.  */
1641
0
  if (from_type == to_type
1642
0
      || (from_type == R_X86_64_CODE_4_GOTTPOFF
1643
0
    && to_type == R_X86_64_GOTTPOFF)
1644
0
      || (from_type == R_X86_64_CODE_5_GOTTPOFF
1645
0
    && to_type == R_X86_64_GOTTPOFF)
1646
0
      || (from_type == R_X86_64_CODE_6_GOTTPOFF
1647
0
    && to_type == R_X86_64_GOTTPOFF))
1648
0
    return true;
1649
1650
  /* Check if the transition can be performed.  */
1651
0
  enum elf_x86_tls_error_type tls_error;
1652
0
  if (check
1653
0
      && ((tls_error = elf_x86_64_check_tls_transition (abfd, info, sec,
1654
0
              contents,
1655
0
              symtab_hdr,
1656
0
              sym_hashes,
1657
0
              from_type, rel,
1658
0
              relend))
1659
0
    != elf_x86_tls_error_none))
1660
1661
0
    {
1662
0
      reloc_howto_type *from, *to;
1663
1664
0
      from = &x86_64_elf_howto_table[from_type];
1665
0
      to = &x86_64_elf_howto_table[to_type];
1666
1667
0
      if (from == NULL || to == NULL)
1668
0
  return false;
1669
1670
0
      _bfd_x86_elf_link_report_tls_transition_error
1671
0
  (info, abfd, sec, symtab_hdr, h, sym, rel, from->name,
1672
0
   to->name, tls_error);
1673
1674
0
      return false;
1675
0
    }
1676
1677
0
  *r_type = to_type;
1678
0
  return true;
1679
0
}
1680
1681
static bool
1682
elf_x86_64_need_pic (struct bfd_link_info *info,
1683
         bfd *input_bfd, asection *sec,
1684
         struct elf_link_hash_entry *h,
1685
         Elf_Internal_Shdr *symtab_hdr,
1686
         Elf_Internal_Sym *isym,
1687
         reloc_howto_type *howto)
1688
0
{
1689
0
  const char *v = "";
1690
0
  const char *und = "";
1691
0
  const char *pic = "";
1692
0
  const char *object;
1693
1694
0
  const char *name;
1695
0
  if (h)
1696
0
    {
1697
0
      name = h->root.root.string;
1698
0
      switch (ELF_ST_VISIBILITY (h->other))
1699
0
  {
1700
0
  case STV_HIDDEN:
1701
0
    v = _("hidden symbol ");
1702
0
    break;
1703
0
  case STV_INTERNAL:
1704
0
    v = _("internal symbol ");
1705
0
    break;
1706
0
  case STV_PROTECTED:
1707
0
    v = _("protected symbol ");
1708
0
    break;
1709
0
  default:
1710
0
    if (((struct elf_x86_link_hash_entry *) h)->def_protected)
1711
0
      v = _("protected symbol ");
1712
0
    else
1713
0
      v = _("symbol ");
1714
0
    pic = NULL;
1715
0
    break;
1716
0
  }
1717
1718
0
      if (!SYMBOL_DEFINED_NON_SHARED_P (h) && !h->def_dynamic)
1719
0
  und = _("undefined ");
1720
0
    }
1721
0
  else
1722
0
    {
1723
0
      name = bfd_elf_sym_name (input_bfd, symtab_hdr, isym, NULL);
1724
0
      pic = NULL;
1725
0
    }
1726
1727
0
  if (bfd_link_dll (info))
1728
0
    {
1729
0
      object = _("a shared object");
1730
0
      if (!pic)
1731
0
  pic = (howto->type == R_X86_64_TPOFF32
1732
0
         ? _("; local-exec is incompatible with -shared")
1733
0
         : _("; recompile with -fPIC"));
1734
0
    }
1735
0
  else
1736
0
    {
1737
0
      if (bfd_link_pie (info))
1738
0
  object = _("a PIE object");
1739
0
      else
1740
0
  object = _("a PDE object");
1741
0
      if (!pic)
1742
0
  pic = _("; recompile with -fPIE");
1743
0
    }
1744
1745
  /* xgettext:c-format */
1746
0
  _bfd_error_handler (_("%pB: relocation %s against %s%s`%s' can "
1747
0
      "not be used when making %s%s"),
1748
0
          input_bfd, howto->name, und, v, name,
1749
0
          object, pic);
1750
0
  bfd_set_error (bfd_error_bad_value);
1751
0
  sec->check_relocs_failed = 1;
1752
0
  return false;
1753
0
}
1754
1755
/* Move the R bits to the B bits in EVEX payload byte 1.  */
1756
static unsigned int evex_move_r_to_b (unsigned int byte1, bool copy)
1757
0
{
1758
0
  byte1 = (byte1 & ~(1 << 5)) | ((byte1 & (1 << 7)) >> 2); /* R3 -> B3 */
1759
0
  byte1 = (byte1 & ~(1 << 3)) | ((~byte1 & (1 << 4)) >> 1); /* R4 -> B4 */
1760
1761
  /* Set both R bits, as they're inverted.  */
1762
0
  if (!copy)
1763
0
    byte1 |= (1 << 4) | (1 << 7);
1764
1765
0
  return byte1;
1766
0
}
1767
1768
/* With the local symbol, foo, we convert
1769
   mov foo@GOTPCREL(%rip), %reg
1770
   movrs foo@GOTPCREL(%rip), %reg
1771
   to
1772
   lea foo(%rip), %reg
1773
   and convert
1774
   call/jmp *foo@GOTPCREL(%rip)
1775
   to
1776
   nop call foo/jmp foo nop
1777
   When PIC is false, convert
1778
   test %reg, foo@GOTPCREL(%rip)
1779
   to
1780
   test $foo, %reg
1781
   and convert
1782
   push foo@GOTPCREL(%rip)
1783
   to
1784
   push $foo
1785
   and convert
1786
   binop foo@GOTPCREL(%rip), %reg
1787
   to
1788
   binop $foo, %reg
1789
   where binop is one of adc, add, and, cmp, imul, or, sbb, sub, xor
1790
   instructions.  */
1791
1792
static bool
1793
elf_x86_64_convert_load_reloc (bfd *abfd,
1794
             asection *input_section,
1795
             bfd_byte *contents,
1796
             unsigned int *r_type_p,
1797
             Elf_Internal_Rela *irel,
1798
             struct elf_link_hash_entry *h,
1799
             bool *converted,
1800
             struct bfd_link_info *link_info)
1801
0
{
1802
0
  struct elf_x86_link_hash_table *htab;
1803
0
  bool is_pic;
1804
0
  bool no_overflow;
1805
0
  bool relocx;
1806
0
  bool is_branch = false;
1807
0
  bool to_reloc_pc32;
1808
0
  bool abs_symbol;
1809
0
  bool local_ref;
1810
0
  asection *tsec = NULL;
1811
0
  bfd_signed_vma raddend;
1812
0
  unsigned int opcode;
1813
0
  unsigned int modrm;
1814
0
  unsigned char evex[3] = { 0, 0, 0 };
1815
0
  unsigned int r_type = *r_type_p;
1816
0
  unsigned int r_symndx;
1817
0
  bfd_vma roff = irel->r_offset;
1818
0
  bfd_vma abs_relocation;
1819
0
  reloc_howto_type *howto;
1820
0
  bfd_reloc_status_type r;
1821
0
  Elf_Internal_Sym *isym;
1822
0
  bfd_vma relocation;
1823
1824
0
  switch (r_type)
1825
0
    {
1826
0
    default:
1827
0
      if (roff < 2)
1828
0
  return true;
1829
0
      relocx = (r_type == R_X86_64_GOTPCRELX);
1830
0
      break;
1831
1832
0
    case R_X86_64_REX_GOTPCRELX:
1833
0
      if (roff < 3)
1834
0
  return true;
1835
0
      relocx = true;
1836
0
      break;
1837
1838
0
    case R_X86_64_CODE_4_GOTPCRELX:
1839
0
      if (roff < 4)
1840
0
  return true;
1841
1842
      /* Skip if this isn't a REX2 instruction, nor un-prefixed MOVRS.  */
1843
0
      opcode = bfd_get_8 (abfd, contents + roff - 4);
1844
0
      if (opcode != 0xd5
1845
0
    && (opcode != 0x0f
1846
0
        || bfd_get_8 (abfd, contents + roff - 3) != 0x38
1847
0
        || bfd_get_8 (abfd, contents + roff - 2) != 0x8b))
1848
0
  return true;
1849
1850
0
      relocx = true;
1851
0
      break;
1852
1853
0
    case R_X86_64_CODE_5_GOTPCRELX:
1854
0
      if (roff < 5)
1855
0
  return true;
1856
1857
      /* Skip if this isn't REX-prefixed MOVRS.  */
1858
0
      if ((bfd_get_8 (abfd, contents + roff - 5) | 0xf) != 0x4f
1859
0
    || bfd_get_8 (abfd, contents + roff - 4) != 0x0f
1860
0
    || bfd_get_8 (abfd, contents + roff - 3) != 0x38
1861
0
    || bfd_get_8 (abfd, contents + roff - 2) != 0x8b)
1862
0
  return true;
1863
1864
0
      relocx = true;
1865
0
      break;
1866
1867
0
    case R_X86_64_CODE_6_GOTPCRELX:
1868
0
      if (roff < 6)
1869
0
  return true;
1870
1871
      /* Skip if this isn't an EVEX instruction.  */
1872
0
      if (bfd_get_8 (abfd, contents + roff - 6) != 0x62)
1873
0
  return true;
1874
1875
0
      evex[0] = bfd_get_8 (abfd, contents + roff - 5);
1876
0
      evex[1] = bfd_get_8 (abfd, contents + roff - 4);
1877
0
      evex[2] = bfd_get_8 (abfd, contents + roff - 3);
1878
1879
      /* Skip if this isn't a Map 4 NP instruction.  */
1880
0
      if ((evex[0] & 7) != 4
1881
0
    || (evex[1] & 3) != 0
1882
0
    || (evex[2] & 0xe0) != 0)
1883
0
  return true;
1884
1885
0
      relocx = true;
1886
0
      break;
1887
0
    }
1888
1889
0
  raddend = irel->r_addend;
1890
  /* Addend for 32-bit PC-relative relocation must be -4.  */
1891
0
  if (raddend != -4)
1892
0
    return true;
1893
1894
0
  htab = elf_x86_hash_table (link_info, X86_64_ELF_DATA);
1895
0
  is_pic = bfd_link_pic (link_info);
1896
1897
  /* TRUE if --no-relax is used.  */
1898
0
  no_overflow = link_info->disable_target_specific_optimizations > 1;
1899
1900
0
  r_symndx = htab->r_sym (irel->r_info);
1901
1902
0
  opcode = bfd_get_8 (abfd, contents + roff - 2);
1903
0
  modrm = bfd_get_8 (abfd, contents + roff - 1);
1904
0
  if (opcode == 0xff)
1905
0
    {
1906
0
      switch (modrm & 0x38)
1907
0
  {
1908
0
  case 0x10: /* CALL */
1909
0
  case 0x20: /* JMP */
1910
0
    is_branch = true;
1911
0
    break;
1912
1913
0
  case 0x30: /* PUSH */
1914
0
    break;
1915
1916
0
  default:
1917
0
    return true;
1918
0
  }
1919
0
    }
1920
1921
  /* Convert mov to lea since it has been done for a while.  */
1922
0
  if (opcode != 0x8b)
1923
0
    {
1924
      /* Only convert R_X86_64_GOTPCRELX, R_X86_64_REX_GOTPCRELX
1925
   and R_X86_64_CODE_<n>_GOTPCRELX for call, jmp or one of adc,
1926
   add, and, cmp, or, sbb, sub, test, xor instructions.  */
1927
0
      if (!relocx)
1928
0
  return true;
1929
0
    }
1930
1931
  /* We convert only to R_X86_64_PC32:
1932
     1. Branch.
1933
     2. R_X86_64_GOTPCREL since we can't modify REX byte.
1934
     3. no_overflow is true.
1935
     4. PIC.
1936
     */
1937
0
  to_reloc_pc32 = (is_branch
1938
0
       || !relocx
1939
0
       || no_overflow
1940
0
       || is_pic);
1941
1942
0
  abs_symbol = false;
1943
0
  abs_relocation = 0;
1944
1945
  /* Get the symbol referred to by the reloc.  */
1946
0
  if (h == NULL)
1947
0
    {
1948
0
      isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd,
1949
0
            r_symndx);
1950
1951
      /* Skip relocation against undefined symbols.  */
1952
0
      if (isym->st_shndx == SHN_UNDEF)
1953
0
  return true;
1954
1955
0
      local_ref = true;
1956
0
      if (isym->st_shndx == SHN_ABS)
1957
0
  {
1958
0
    tsec = bfd_abs_section_ptr;
1959
0
    abs_symbol = true;
1960
0
    abs_relocation = isym->st_value;
1961
0
  }
1962
0
      else if (isym->st_shndx == SHN_COMMON)
1963
0
  tsec = bfd_com_section_ptr;
1964
0
      else if (isym->st_shndx == SHN_X86_64_LCOMMON)
1965
0
  tsec = &bfd_elf_large_com_section;
1966
0
      else
1967
0
  tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1968
0
    }
1969
0
  else
1970
0
    {
1971
      /* Undefined weak symbol is only bound locally in executable
1972
   and its reference is resolved as 0 without relocation
1973
   overflow.  We can only perform this optimization for
1974
   GOTPCRELX relocations since we need to modify REX byte.
1975
   It is OK convert mov with R_X86_64_GOTPCREL to
1976
   R_X86_64_PC32.  */
1977
0
      struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
1978
1979
0
      isym = NULL;
1980
0
      tsec = NULL;
1981
1982
0
      abs_symbol = ABS_SYMBOL_P (h);
1983
0
      abs_relocation = h->root.u.def.value;
1984
1985
      /* NB: Also set linker_def via SYMBOL_REFERENCES_LOCAL_P.  */
1986
0
      local_ref = SYMBOL_REFERENCES_LOCAL_P (link_info, h);
1987
0
      if ((relocx || opcode == 0x8b)
1988
0
    && (h->root.type == bfd_link_hash_undefweak
1989
0
        && !eh->linker_def
1990
0
        && local_ref))
1991
0
  {
1992
0
    if (is_branch)
1993
0
      {
1994
        /* Skip for branch instructions since R_X86_64_PC32
1995
     may overflow.  */
1996
0
        if (no_overflow)
1997
0
    return true;
1998
0
      }
1999
0
    else if (relocx)
2000
0
      {
2001
        /* For non-branch instructions, we can convert to
2002
     R_X86_64_32/R_X86_64_32S since we know if there
2003
     is a REX byte.  */
2004
0
        to_reloc_pc32 = false;
2005
0
      }
2006
2007
    /* Since we don't know the current PC when PIC is true,
2008
       we can't convert to R_X86_64_PC32.  */
2009
0
    if (to_reloc_pc32 && is_pic)
2010
0
      return true;
2011
2012
0
    goto convert;
2013
0
  }
2014
      /* Avoid optimizing GOTPCREL relocations againt _DYNAMIC since
2015
   ld.so may use its link-time address.  */
2016
0
      else if (h->start_stop
2017
0
         || eh->linker_def
2018
0
         || ((h->def_regular
2019
0
        || h->root.type == bfd_link_hash_defined
2020
0
        || h->root.type == bfd_link_hash_defweak)
2021
0
       && h != htab->elf.hdynamic
2022
0
       && local_ref))
2023
0
  {
2024
    /* bfd_link_hash_new or bfd_link_hash_undefined is
2025
       set by an assignment in a linker script in
2026
       bfd_elf_record_link_assignment.  start_stop is set
2027
       on __start_SECNAME/__stop_SECNAME which mark section
2028
       SECNAME.  */
2029
0
    if (h->start_stop
2030
0
        || eh->linker_def
2031
0
        || (h->def_regular
2032
0
      && (h->root.type == bfd_link_hash_new
2033
0
          || h->root.type == bfd_link_hash_undefined
2034
0
          || ((h->root.type == bfd_link_hash_defined
2035
0
         || h->root.type == bfd_link_hash_defweak)
2036
0
        && h->root.u.def.section == bfd_und_section_ptr))))
2037
0
      {
2038
        /* Skip since R_X86_64_32/R_X86_64_32S may overflow.  */
2039
0
        if (no_overflow)
2040
0
    return true;
2041
0
        if (h->start_stop)
2042
0
    tsec = h->root.u.def.section;
2043
0
        else if (h == htab->elf.hehdr_start)
2044
0
    {
2045
      /* Use the lowest-addressed section to estimate the
2046
         __ehdr_start symbol value.  */
2047
0
      asection *sec;
2048
0
      tsec = NULL;
2049
0
      for (sec = link_info->output_bfd->sections;
2050
0
           sec != NULL;
2051
0
           sec = sec->next)
2052
0
        if ((sec->flags & SEC_LOAD) != 0
2053
0
      && (tsec == NULL || tsec->vma > sec->vma))
2054
0
          tsec = sec;
2055
2056
0
    }
2057
0
        goto convert;
2058
0
      }
2059
0
    tsec = h->root.u.def.section;
2060
0
  }
2061
0
      else
2062
0
  return true;
2063
0
    }
2064
2065
0
  if (tsec == NULL)
2066
0
    return false;
2067
2068
  /* Don't convert GOTPCREL relocation against large section.  */
2069
0
  if (elf_section_data (tsec) !=  NULL
2070
0
      && (elf_section_flags (tsec) & SHF_X86_64_LARGE) != 0)
2071
0
    return true;
2072
2073
  /* Skip since R_X86_64_PC32/R_X86_64_32/R_X86_64_32S may overflow.  */
2074
0
  if (no_overflow)
2075
0
    return true;
2076
2077
0
 convert:
2078
  /* Compute relocation value so that it can be used later to check for
2079
     overflow against the converted relocation.  */
2080
0
  if (h == NULL)
2081
0
    {
2082
      /* Make a copy of IREL so that _bfd_elf_rela_local_sym won't
2083
   change IREL.  */
2084
0
      Elf_Internal_Rela rel = *irel;
2085
0
      relocation = _bfd_elf_rela_local_sym (link_info->output_bfd, isym,
2086
0
              &tsec, &rel);
2087
      /* Use the updated r_addend.  */
2088
0
      raddend = rel.r_addend;
2089
0
    }
2090
0
  else if (tsec != NULL)
2091
0
    relocation = (h->root.u.def.value
2092
0
      + tsec->output_section->vma
2093
0
      + tsec->output_offset);
2094
0
  else
2095
0
    relocation = 0;
2096
2097
0
  if (is_branch)
2098
0
    {
2099
      /* We have "call/jmp *foo@GOTPCREL(%rip)".  */
2100
0
      unsigned int nop;
2101
0
      unsigned int disp;
2102
0
      bfd_vma nop_offset;
2103
2104
0
      r_type = R_X86_64_PC32;
2105
2106
      /* Skip if the converted relocation will overflow.  */
2107
0
      howto = &x86_64_elf_howto_table[r_type];
2108
0
      r = _bfd_final_link_relocate (howto, abfd, input_section,
2109
0
            contents, irel->r_offset,
2110
0
            relocation, raddend);
2111
0
      if (r == bfd_reloc_overflow)
2112
0
  return true;
2113
2114
      /* Convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX to
2115
   R_X86_64_PC32.  */
2116
0
      if (modrm == 0x25)
2117
0
  {
2118
    /* Convert to "jmp foo nop".  */
2119
0
    modrm = 0xe9;
2120
0
    nop = NOP_OPCODE;
2121
0
    nop_offset = irel->r_offset + 3;
2122
0
    disp = bfd_get_32 (abfd, contents + irel->r_offset);
2123
0
    irel->r_offset -= 1;
2124
0
    bfd_put_32 (abfd, disp, contents + irel->r_offset);
2125
0
  }
2126
0
      else
2127
0
  {
2128
0
    struct elf_x86_link_hash_entry *eh
2129
0
      = (struct elf_x86_link_hash_entry *) h;
2130
2131
    /* Convert to "nop call foo".  ADDR_PREFIX_OPCODE
2132
       is a nop prefix.  */
2133
0
    modrm = 0xe8;
2134
    /* To support TLS optimization, always use addr32 prefix for
2135
       "call *__tls_get_addr@GOTPCREL(%rip)".  */
2136
0
    if (eh && eh->tls_get_addr)
2137
0
      {
2138
0
        nop = 0x67;
2139
0
        nop_offset = irel->r_offset - 2;
2140
0
      }
2141
0
    else
2142
0
      {
2143
0
        nop = htab->params->call_nop_byte;
2144
0
        if (htab->params->call_nop_as_suffix)
2145
0
    {
2146
0
      nop_offset = irel->r_offset + 3;
2147
0
      disp = bfd_get_32 (abfd, contents + irel->r_offset);
2148
0
      irel->r_offset -= 1;
2149
0
      bfd_put_32 (abfd, disp, contents + irel->r_offset);
2150
0
    }
2151
0
        else
2152
0
    nop_offset = irel->r_offset - 2;
2153
0
      }
2154
0
  }
2155
0
      bfd_put_8 (abfd, nop, contents + nop_offset);
2156
0
      bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
2157
0
    }
2158
0
  else if (r_type == R_X86_64_CODE_6_GOTPCRELX && opcode != 0x8b)
2159
0
    {
2160
0
      bool move_v_r = false;
2161
2162
      /* R_X86_64_PC32 isn't supported.  */
2163
0
      if (to_reloc_pc32)
2164
0
  return true;
2165
2166
0
      if (opcode == 0x85)
2167
0
  {
2168
    /* Convert "ctest<cc> %reg, foo@GOTPCREL(%rip)" to
2169
       "ctest<cc> $foo, %reg".  */
2170
0
    modrm = 0xc0 | (modrm & 0x38) >> 3;
2171
0
    opcode = 0xf7;
2172
0
  }
2173
0
      else if ((opcode | 0x3a) == 0x3b)
2174
0
  {
2175
    /* Don't convert (non-NDD) forms with memory destination.  */
2176
0
    if (!(evex[2] & 0x10) && (opcode | 0x38) != 0x3b)
2177
0
      return true;
2178
2179
    /* Don't convert non-commutative insns with the memory operand
2180
       2nd.  */
2181
0
    if ((evex[2] & 0x10) && (opcode | 0x38) != 0x3b
2182
0
        && (opcode == 0x19 /* SBB */
2183
0
      || opcode == 0x29 /* SUB */))
2184
0
      return true;
2185
2186
    /* Convert "binop foo@GOTPCREL(%rip), %reg" to
2187
       "binop $foo, %reg", or alike for 3-operand forms.  */
2188
0
    modrm = 0xc0 | ((modrm & 0x38) >> 3) | (opcode & 0x38);
2189
0
    opcode = 0x81;
2190
0
  }
2191
0
      else if (opcode == 0xaf)
2192
0
  {
2193
0
    if (!(evex[2] & 0x10))
2194
0
      {
2195
        /* Convert "imul foo@GOTPCREL(%rip), %reg" to
2196
           "imul $foo, %reg, %reg".  */
2197
0
        modrm = 0xc0 | ((modrm & 0x38) >> 3) | (modrm & 0x38);
2198
0
      }
2199
0
    else
2200
0
      {
2201
        /* Convert "imul foo@GOTPCREL(%rip), %reg1, %reg2" to
2202
           "imul $foo, %reg1, %reg2".  */
2203
0
        modrm = 0xc0 | ((modrm & 0x38) >> 3) | (~evex[1] & 0x38);
2204
0
        move_v_r = true;
2205
0
      }
2206
0
    opcode = 0x69;
2207
0
  }
2208
0
      else
2209
0
  return true;
2210
2211
      /* Use R_X86_64_32 with 32-bit operand to avoid relocation
2212
   overflow when sign-extending imm32 to 64 bits.  */
2213
0
      r_type = evex[1] & 0x80 ? R_X86_64_32S : R_X86_64_32;
2214
2215
      /* Skip if the converted relocation will overflow.  */
2216
0
      howto = elf_x86_64_rtype_to_howto (abfd, r_type);
2217
0
      r = _bfd_final_link_relocate (howto, abfd, input_section,
2218
0
            contents, irel->r_offset,
2219
0
            relocation, 0);
2220
0
      if (r == bfd_reloc_overflow)
2221
0
  return true;
2222
2223
0
      if (abs_relocation) /* Bogus; should be abs_symbol.  */
2224
0
  {
2225
    /* Check if R_X86_64_32S/R_X86_64_32 fits.  */
2226
0
    if (r_type == R_X86_64_32S)
2227
0
      {
2228
0
        if ((abs_relocation + 0x80000000) > 0xffffffff)
2229
0
    return true;
2230
0
      }
2231
0
    else
2232
0
      {
2233
0
        if (abs_relocation > 0xffffffff)
2234
0
    return true;
2235
0
      }
2236
0
  }
2237
2238
0
      bfd_put_8 (abfd, opcode, contents + roff - 2);
2239
0
      bfd_put_8 (abfd, modrm, contents + roff - 1);
2240
2241
0
      evex[0] = evex_move_r_to_b (evex[0], opcode == 0x69 && !move_v_r);
2242
0
      if (move_v_r)
2243
0
  {
2244
    /* Move the top two V bits to the R bits in EVEX payload byte 1.
2245
       Note that evex_move_r_to_b() set both R bits.  */
2246
0
    if (!(evex[1] & (1 << 6)))
2247
0
      evex[0] &= ~(1 << 7); /* V3 -> R3 */
2248
0
    if (!(evex[2] & (1 << 3)))
2249
0
      evex[0] &= ~(1 << 4); /* V4 -> R4 */
2250
    /* Set all V bits, as they're inverted.  */
2251
0
    evex[1] |= 0xf << 3;
2252
0
    evex[2] |= 1 << 3;
2253
    /* Clear the ND (ZU) bit (it ought to be ignored anyway).  */
2254
0
    evex[2] &= ~(1 << 4);
2255
0
    bfd_put_8 (abfd, evex[2], contents + roff - 3);
2256
0
    bfd_put_8 (abfd, evex[1], contents + roff - 4);
2257
0
  }
2258
0
      bfd_put_8 (abfd, evex[0], contents + roff - 5);
2259
2260
      /* No addend for R_X86_64_32/R_X86_64_32S relocations.  */
2261
0
      irel->r_addend = 0;
2262
0
    }
2263
0
  else
2264
0
    {
2265
0
      unsigned int rex = 0;
2266
0
      unsigned int rex_mask = REX_R;
2267
0
      unsigned int rex2 = 0;
2268
0
      unsigned int rex2_mask = REX_R | REX_R << 4;
2269
0
      unsigned int movrs = 0;
2270
0
      bool rex_w = false;
2271
2272
0
      if (r_type == R_X86_64_CODE_6_GOTPCRELX)
2273
0
  {
2274
    /* Synthesize a REX2 prefix from EVEX, just enough for the LEA
2275
       and MOV case below.  */
2276
0
    unsigned int p;
2277
2278
0
    p = bfd_get_8 (abfd, contents + roff - 5);
2279
0
    if (!(p & 0x80))
2280
0
      rex2 |= REX_R;
2281
0
    if (!(p & 0x10))
2282
0
      rex2 |= REX_R << 4;
2283
0
    if (bfd_get_8 (abfd, contents + roff - 4) & 0x80)
2284
0
      {
2285
0
        rex2 |= REX_W;
2286
0
        rex_w = true;
2287
0
      }
2288
0
    movrs = 6;
2289
0
  }
2290
0
      else if (r_type == R_X86_64_CODE_5_GOTPCRELX)
2291
0
  {
2292
0
    rex = bfd_get_8 (abfd, contents + roff - 5);
2293
0
    rex_w = (rex & REX_W) != 0;
2294
0
    movrs = 5;
2295
0
  }
2296
0
      else if (r_type == R_X86_64_CODE_4_GOTPCRELX)
2297
0
  {
2298
0
    if (bfd_get_8 (abfd, contents + roff - 4) == 0xd5)
2299
0
      {
2300
        /* Make sure even an all-zero payload leaves a non-zero value
2301
     in the variable.  */
2302
0
        rex2 = bfd_get_8 (abfd, contents + roff - 3) | 0x100;
2303
0
        rex2_mask |= 0x100;
2304
0
        rex_w = (rex2 & REX_W) != 0;
2305
0
      }
2306
0
    else if (bfd_get_8 (abfd, contents + roff - 4) == 0x0f)
2307
0
      movrs = 4;
2308
0
  }
2309
0
      else if (r_type == R_X86_64_REX_GOTPCRELX)
2310
0
  {
2311
0
    rex = bfd_get_8 (abfd, contents + roff - 3);
2312
0
    rex_w = (rex & REX_W) != 0;
2313
0
  }
2314
2315
0
      if (opcode == 0x8b)
2316
0
  {
2317
0
    if (abs_symbol && local_ref && relocx)
2318
0
      to_reloc_pc32 = false;
2319
2320
0
    if (to_reloc_pc32)
2321
0
      {
2322
        /* Convert "mov foo@GOTPCREL(%rip), %reg" to
2323
     "lea foo(%rip), %reg".  */
2324
0
        opcode = 0x8d;
2325
0
        r_type = R_X86_64_PC32;
2326
2327
        /* Skip if the converted relocation will overflow.  */
2328
0
        howto = &x86_64_elf_howto_table[r_type];
2329
0
        r = _bfd_final_link_relocate (howto, abfd, input_section,
2330
0
              contents, irel->r_offset,
2331
0
              relocation,
2332
0
              raddend);
2333
0
        if (r == bfd_reloc_overflow)
2334
0
    return true;
2335
2336
        /* For MOVRS move a possible REX prefix as necessary.  */
2337
0
        if (movrs == 5)
2338
0
    bfd_put_8 (abfd, rex, contents + roff - 3);
2339
0
      }
2340
0
    else
2341
0
      {
2342
        /* Convert "mov foo@GOTPCREL(%rip), %reg" to
2343
     "mov $foo, %reg".  */
2344
0
        opcode = 0xc7;
2345
0
        modrm = 0xc0 | (modrm & 0x38) >> 3;
2346
0
        if (rex_w && ABI_64_P (link_info->output_bfd))
2347
0
    {
2348
      /* Keep the REX_W bit in REX byte for LP64.  */
2349
0
      r_type = R_X86_64_32S;
2350
0
      goto rewrite_modrm_rex;
2351
0
    }
2352
0
        else
2353
0
    {
2354
      /* If the REX_W bit in REX byte isn't needed,
2355
         use R_X86_64_32 and clear the W bit to avoid
2356
         sign-extend imm32 to imm64.  */
2357
0
      r_type = R_X86_64_32;
2358
      /* Clear the W bit in REX byte and REX2 payload.  */
2359
0
      rex_mask |= REX_W;
2360
0
      rex2_mask |= REX_W;
2361
0
      goto rewrite_modrm_rex;
2362
0
    }
2363
0
      }
2364
0
  }
2365
0
      else
2366
0
  {
2367
    /* R_X86_64_PC32 isn't supported.  */
2368
0
    if (to_reloc_pc32)
2369
0
      return true;
2370
2371
0
    if (opcode == 0x85 && !(rex2 & (REX2_M << 4)))
2372
0
      {
2373
        /* Convert "test %reg, foo@GOTPCREL(%rip)" to
2374
     "test $foo, %reg".  */
2375
0
        modrm = 0xc0 | (modrm & 0x38) >> 3;
2376
0
        opcode = 0xf7;
2377
0
      }
2378
0
    else if ((opcode | 0x38) == 0x3b && !(rex2 & (REX2_M << 4)))
2379
0
      {
2380
        /* Convert "binop foo@GOTPCREL(%rip), %reg" to
2381
     "binop $foo, %reg".  */
2382
0
        modrm = 0xc0 | ((modrm & 0x38) >> 3) | (opcode & 0x38);
2383
0
        opcode = 0x81;
2384
0
      }
2385
0
    else if (opcode == 0xaf && (rex2 & (REX2_M << 4)))
2386
0
      {
2387
        /* Convert "imul foo@GOTPCREL(%rip), %reg" to
2388
     "imul $foo, %reg, %reg".  */
2389
0
        modrm = 0xc0 | ((modrm & 0x38) >> 3) | (modrm & 0x38);
2390
0
        rex_mask = 0;
2391
0
        rex2_mask = REX2_M << 4;
2392
0
        opcode = 0x69;
2393
0
      }
2394
0
    else if (opcode == 0xff && !(rex2 & (REX2_M << 4)))
2395
0
      {
2396
        /* Convert "push foo@GOTPCREL(%rip)" to
2397
     "push $foo".  */
2398
0
        bfd_put_8 (abfd, 0x68, contents + roff - 1);
2399
0
        if (rex)
2400
0
    {
2401
0
      bfd_put_8 (abfd, 0x2e, contents + roff - 3);
2402
0
      bfd_put_8 (abfd, rex, contents + roff - 2);
2403
0
    }
2404
0
        else if (rex2)
2405
0
    {
2406
0
      bfd_put_8 (abfd, 0x2e, contents + roff - 4);
2407
0
      bfd_put_8 (abfd, 0xd5, contents + roff - 3);
2408
0
      bfd_put_8 (abfd, rex2, contents + roff - 2);
2409
0
    }
2410
0
        else
2411
0
    bfd_put_8 (abfd, 0x2e, contents + roff - 2);
2412
2413
0
        r_type = R_X86_64_32S;
2414
        /* No addend for R_X86_64_32S relocations.  */
2415
0
        irel->r_addend = 0;
2416
0
        goto finish;
2417
0
      }
2418
0
    else
2419
0
      return true;
2420
2421
    /* Use R_X86_64_32 with 32-bit operand to avoid relocation
2422
       overflow when sign-extending imm32 to imm64.  */
2423
0
    r_type = rex_w ? R_X86_64_32S : R_X86_64_32;
2424
2425
0
  rewrite_modrm_rex:
2426
    /* Skip if the converted relocation will overflow.  */
2427
0
    howto = elf_x86_64_rtype_to_howto (abfd, r_type);
2428
0
    r = _bfd_final_link_relocate (howto, abfd, input_section,
2429
0
          contents, irel->r_offset,
2430
0
          relocation, 0);
2431
0
    if (r == bfd_reloc_overflow)
2432
0
      return true;
2433
2434
0
    if (abs_relocation)
2435
0
      {
2436
        /* Check if R_X86_64_32S/R_X86_64_32 fits.  */
2437
0
        if (r_type == R_X86_64_32S)
2438
0
    {
2439
0
      if ((abs_relocation + 0x80000000) > 0xffffffff)
2440
0
        return true;
2441
0
    }
2442
0
        else
2443
0
    {
2444
0
      if (abs_relocation > 0xffffffff)
2445
0
        return true;
2446
0
    }
2447
0
      }
2448
2449
0
    bfd_put_8 (abfd, modrm, contents + roff - 1);
2450
2451
0
    if (rex)
2452
0
      {
2453
        /* Move the R bit to the B bit in REX byte.  */
2454
0
        rex = (rex & ~rex_mask) | (rex & REX_R) >> 2;
2455
0
        bfd_put_8 (abfd, rex, contents + roff - 3);
2456
0
      }
2457
0
    else if (rex2)
2458
0
      {
2459
        /* Move the R bits to the B bits in REX2 payload byte.  */
2460
0
        rex2 = ((rex2 & ~rex2_mask)
2461
0
          | (rex2 & (REX_R | REX_R << 4)) >> 2);
2462
0
        bfd_put_8 (abfd, rex2, contents + roff - 3);
2463
0
      }
2464
2465
    /* No addend for R_X86_64_32/R_X86_64_32S relocations.  */
2466
0
    irel->r_addend = 0;
2467
0
  }
2468
2469
0
      bfd_put_8 (abfd, opcode, contents + roff - 2);
2470
2471
      /* For MOVRS zap the 0f38 or EVEX prefix, applying meaningless CS
2472
   segment overrides instead.  When necessary also install the REX2
2473
   prefix and payload (which may not have been written yet).  */
2474
0
      if (movrs)
2475
0
  {
2476
0
    bfd_put_8 (abfd, 0x2e, contents + roff - movrs);
2477
0
    bfd_put_8 (abfd, 0x2e, contents + roff - movrs + 1);
2478
0
    if (movrs == 6)
2479
0
      {
2480
0
        bfd_put_8 (abfd, 0xd5, contents + roff - 4);
2481
0
        bfd_put_8 (abfd, rex2, contents + roff - 3);
2482
0
      }
2483
0
  }
2484
0
    }
2485
2486
0
 finish:
2487
0
  *r_type_p = r_type;
2488
0
  irel->r_info = htab->r_info (r_symndx,
2489
0
             r_type | R_X86_64_converted_reloc_bit);
2490
2491
0
  *converted = true;
2492
2493
0
  return true;
2494
0
}
2495
2496
/* Look through the relocs for a section during the first phase, and
2497
   calculate needed space in the global offset table, and procedure
2498
   linkage table.  */
2499
2500
static bool
2501
elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info,
2502
      asection *sec,
2503
      const Elf_Internal_Rela *relocs)
2504
0
{
2505
0
  struct elf_x86_link_hash_table *htab;
2506
0
  Elf_Internal_Shdr *symtab_hdr;
2507
0
  struct elf_link_hash_entry **sym_hashes;
2508
0
  const Elf_Internal_Rela *rel;
2509
0
  const Elf_Internal_Rela *rel_end;
2510
0
  bfd_byte *contents;
2511
0
  bool converted;
2512
2513
0
  if (bfd_link_relocatable (info))
2514
0
    return true;
2515
2516
0
  htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
2517
0
  if (htab == NULL)
2518
0
    {
2519
0
      sec->check_relocs_failed = 1;
2520
0
      return false;
2521
0
    }
2522
2523
0
  BFD_ASSERT (is_x86_elf (abfd, htab));
2524
2525
  /* Get the section contents.  */
2526
0
  if (elf_section_data (sec)->this_hdr.contents != NULL)
2527
0
    contents = elf_section_data (sec)->this_hdr.contents;
2528
0
  else if (!_bfd_elf_mmap_section_contents (abfd, sec, &contents))
2529
0
    {
2530
0
      sec->check_relocs_failed = 1;
2531
0
      return false;
2532
0
    }
2533
2534
0
  symtab_hdr = &elf_symtab_hdr (abfd);
2535
0
  sym_hashes = elf_sym_hashes (abfd);
2536
2537
0
  converted = false;
2538
2539
0
  rel_end = relocs + sec->reloc_count;
2540
0
  for (rel = relocs; rel < rel_end; rel++)
2541
0
    {
2542
0
      unsigned int r_type;
2543
0
      unsigned int r_symndx;
2544
0
      struct elf_link_hash_entry *h;
2545
0
      struct elf_x86_link_hash_entry *eh;
2546
0
      Elf_Internal_Sym *isym;
2547
0
      const char *name;
2548
0
      bool size_reloc;
2549
0
      bool converted_reloc;
2550
0
      bool no_dynreloc;
2551
0
      reloc_howto_type *howto;
2552
2553
0
      r_symndx = htab->r_sym (rel->r_info);
2554
0
      r_type = ELF32_R_TYPE (rel->r_info);
2555
2556
      /* Don't check R_X86_64_NONE.  */
2557
0
      if (r_type == R_X86_64_NONE)
2558
0
  continue;
2559
2560
0
      if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
2561
0
  {
2562
    /* xgettext:c-format */
2563
0
    _bfd_error_handler (_("%pB: bad symbol index: %d"),
2564
0
            abfd, r_symndx);
2565
0
    goto error_return;
2566
0
  }
2567
2568
0
      howto = elf_x86_64_rtype_to_howto (abfd, r_type);
2569
0
      if (howto == NULL)
2570
0
  {
2571
0
    _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2572
0
            abfd, r_type);
2573
0
    goto error_return;
2574
0
  }
2575
0
      if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
2576
0
  {
2577
    /* xgettext:c-format */
2578
0
    _bfd_error_handler
2579
0
      (_("%pB: bad reloc offset (%#" PRIx64 " > %#" PRIx64 ") for"
2580
0
         " section `%pA'"), abfd, (uint64_t) rel->r_offset,
2581
0
       (uint64_t) sec->size, sec);
2582
0
    goto error_return;
2583
0
  }
2584
2585
0
      if (r_symndx < symtab_hdr->sh_info)
2586
0
  {
2587
    /* A local symbol.  */
2588
0
    isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
2589
0
          abfd, r_symndx);
2590
0
    if (isym == NULL)
2591
0
      goto error_return;
2592
2593
    /* Check relocation against local STT_GNU_IFUNC symbol.  */
2594
0
    if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
2595
0
      {
2596
0
        h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel,
2597
0
               true);
2598
0
        if (h == NULL)
2599
0
    goto error_return;
2600
2601
        /* Fake a STT_GNU_IFUNC symbol.  */
2602
0
        h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
2603
0
                  isym, NULL);
2604
0
        if (h->root.root.string == bfd_symbol_error_name)
2605
0
    goto error_return;
2606
2607
0
        h->type = STT_GNU_IFUNC;
2608
0
        h->def_regular = 1;
2609
0
        h->ref_regular = 1;
2610
0
        h->forced_local = 1;
2611
0
        h->root.type = bfd_link_hash_defined;
2612
0
      }
2613
0
    else
2614
0
      h = NULL;
2615
0
  }
2616
0
      else
2617
0
  {
2618
0
    isym = NULL;
2619
0
    h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
2620
0
              symtab_hdr->sh_info,
2621
0
              NUM_SHDR_ENTRIES (symtab_hdr));
2622
0
  }
2623
2624
      /* Check invalid x32 relocations.  */
2625
0
      if (!ABI_64_P (abfd))
2626
0
  switch (r_type)
2627
0
    {
2628
0
    default:
2629
0
      break;
2630
2631
0
    case R_X86_64_DTPOFF64:
2632
0
    case R_X86_64_TPOFF64:
2633
0
    case R_X86_64_GOTOFF64:
2634
0
    case R_X86_64_GOT64:
2635
0
    case R_X86_64_GOTPCREL64:
2636
0
    case R_X86_64_GOTPC64:
2637
0
    case R_X86_64_GOTPLT64:
2638
0
    case R_X86_64_PLTOFF64:
2639
0
        {
2640
0
    if (h)
2641
0
      name = h->root.root.string;
2642
0
    else
2643
0
      name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2644
0
             NULL);
2645
0
    _bfd_error_handler
2646
      /* xgettext:c-format */
2647
0
      (_("%pB: relocation %s against symbol `%s' isn't "
2648
0
         "supported in x32 mode"), abfd,
2649
0
       x86_64_elf_howto_table[r_type].name, name);
2650
0
    bfd_set_error (bfd_error_bad_value);
2651
0
    goto error_return;
2652
0
        }
2653
0
      break;
2654
0
    }
2655
2656
0
      eh = (struct elf_x86_link_hash_entry *) h;
2657
2658
0
      if (h != NULL)
2659
0
  {
2660
    /* It is referenced by a non-shared object. */
2661
0
    h->ref_regular = 1;
2662
0
  }
2663
2664
0
      converted_reloc = false;
2665
0
      if ((r_type == R_X86_64_GOTPCREL
2666
0
     || r_type == R_X86_64_GOTPCRELX
2667
0
     || r_type == R_X86_64_REX_GOTPCRELX
2668
0
     || r_type == R_X86_64_CODE_4_GOTPCRELX
2669
0
     || r_type == R_X86_64_CODE_5_GOTPCRELX
2670
0
     || r_type == R_X86_64_CODE_6_GOTPCRELX)
2671
0
    && (h == NULL || h->type != STT_GNU_IFUNC))
2672
0
  {
2673
0
    Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
2674
0
    if (!elf_x86_64_convert_load_reloc (abfd, sec, contents,
2675
0
                &r_type, irel, h,
2676
0
                &converted_reloc, info))
2677
0
      goto error_return;
2678
2679
0
    if (converted_reloc)
2680
0
      converted = true;
2681
0
  }
2682
2683
0
      if (!_bfd_elf_x86_valid_reloc_p (sec, info, htab, rel, h, isym,
2684
0
               symtab_hdr, &no_dynreloc))
2685
0
  goto error_return;
2686
2687
0
      if (! elf_x86_64_tls_transition (info, abfd, sec, contents,
2688
0
               symtab_hdr, sym_hashes,
2689
0
               &r_type, GOT_UNKNOWN,
2690
0
               rel, rel_end, h, isym, false))
2691
0
  goto error_return;
2692
2693
      /* Check if _GLOBAL_OFFSET_TABLE_ is referenced.  */
2694
0
      if (h == htab->elf.hgot)
2695
0
  htab->got_referenced = true;
2696
2697
0
      switch (r_type)
2698
0
  {
2699
0
  case R_X86_64_TLSLD:
2700
0
    htab->tls_ld_or_ldm_got.refcount = 1;
2701
0
    goto create_got;
2702
2703
0
  case R_X86_64_TPOFF32:
2704
0
    if (!bfd_link_executable (info))
2705
0
      {
2706
0
        elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
2707
0
           &x86_64_elf_howto_table[r_type]);
2708
0
        goto error_return;
2709
0
      }
2710
0
    if (eh != NULL)
2711
0
      eh->zero_undefweak &= 0x2;
2712
0
    break;
2713
2714
0
  case R_X86_64_TLSDESC_CALL:
2715
0
    htab->has_tls_desc_call = 1;
2716
0
    goto need_got;
2717
2718
0
  case R_X86_64_GOTTPOFF:
2719
0
  case R_X86_64_CODE_4_GOTTPOFF:
2720
0
  case R_X86_64_CODE_5_GOTTPOFF:
2721
0
  case R_X86_64_CODE_6_GOTTPOFF:
2722
0
    if (!bfd_link_executable (info))
2723
0
      info->flags |= DF_STATIC_TLS;
2724
    /* Fall through */
2725
2726
0
  case R_X86_64_GOT32:
2727
0
  case R_X86_64_GOTPCREL:
2728
0
  case R_X86_64_GOTPCRELX:
2729
0
  case R_X86_64_REX_GOTPCRELX:
2730
0
  case R_X86_64_CODE_4_GOTPCRELX:
2731
0
  case R_X86_64_CODE_5_GOTPCRELX:
2732
0
  case R_X86_64_CODE_6_GOTPCRELX:
2733
0
  case R_X86_64_TLSGD:
2734
0
  case R_X86_64_GOT64:
2735
0
  case R_X86_64_GOTPCREL64:
2736
0
  case R_X86_64_GOTPLT64:
2737
0
  case R_X86_64_GOTPC32_TLSDESC:
2738
0
  case R_X86_64_CODE_4_GOTPC32_TLSDESC:
2739
0
need_got:
2740
    /* This symbol requires a global offset table entry.  */
2741
0
    {
2742
0
      int tls_type, old_tls_type;
2743
2744
0
      switch (r_type)
2745
0
        {
2746
0
        default:
2747
0
    tls_type = GOT_NORMAL;
2748
0
    if (h)
2749
0
      {
2750
0
        if (ABS_SYMBOL_P (h))
2751
0
          tls_type = GOT_ABS;
2752
0
      }
2753
0
    else if (isym->st_shndx == SHN_ABS)
2754
0
      tls_type = GOT_ABS;
2755
0
    break;
2756
0
        case R_X86_64_TLSGD:
2757
0
    tls_type = GOT_TLS_GD;
2758
0
    break;
2759
0
        case R_X86_64_GOTTPOFF:
2760
0
        case R_X86_64_CODE_4_GOTTPOFF:
2761
0
        case R_X86_64_CODE_5_GOTTPOFF:
2762
0
        case R_X86_64_CODE_6_GOTTPOFF:
2763
0
    tls_type = GOT_TLS_IE;
2764
0
    break;
2765
0
        case R_X86_64_GOTPC32_TLSDESC:
2766
0
        case R_X86_64_CODE_4_GOTPC32_TLSDESC:
2767
0
        case R_X86_64_TLSDESC_CALL:
2768
0
    tls_type = GOT_TLS_GDESC;
2769
0
    break;
2770
0
        }
2771
2772
0
      if (tls_type >= GOT_TLS_GD
2773
0
    && tls_type <= GOT_TLS_GDESC
2774
0
    && (elf_section_type (sec) != SHT_PROGBITS
2775
0
        || (sec->flags & SEC_CODE) == 0))
2776
0
        {
2777
0
    _bfd_x86_elf_link_report_tls_invalid_section_error
2778
0
      (abfd, sec, symtab_hdr, h, isym, howto);
2779
0
    goto error_return;
2780
0
        }
2781
2782
0
      if (h != NULL)
2783
0
        {
2784
0
    h->got.refcount = 1;
2785
0
    old_tls_type = eh->tls_type;
2786
0
        }
2787
0
      else
2788
0
        {
2789
0
    bfd_signed_vma *local_got_refcounts;
2790
2791
0
    if (!elf_x86_allocate_local_got_info (abfd,
2792
0
                  symtab_hdr->sh_info))
2793
0
      goto error_return;
2794
2795
    /* This is a global offset table entry for a local symbol.  */
2796
0
    local_got_refcounts = elf_local_got_refcounts (abfd);
2797
0
    local_got_refcounts[r_symndx] = 1;
2798
0
    old_tls_type
2799
0
      = elf_x86_local_got_tls_type (abfd) [r_symndx];
2800
0
        }
2801
2802
      /* If a TLS symbol is accessed using IE at least once,
2803
         there is no point to use dynamic model for it.  */
2804
0
      if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
2805
0
    && (! GOT_TLS_GD_ANY_P (old_tls_type)
2806
0
        || tls_type != GOT_TLS_IE))
2807
0
        {
2808
0
    if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
2809
0
      tls_type = old_tls_type;
2810
0
    else if (GOT_TLS_GD_ANY_P (old_tls_type)
2811
0
       && GOT_TLS_GD_ANY_P (tls_type))
2812
0
      tls_type |= old_tls_type;
2813
0
    else
2814
0
      {
2815
0
        if (h)
2816
0
          name = h->root.root.string;
2817
0
        else
2818
0
          name = bfd_elf_sym_name (abfd, symtab_hdr,
2819
0
                 isym, NULL);
2820
0
        _bfd_error_handler
2821
          /* xgettext:c-format */
2822
0
          (_("%pB: '%s' accessed both as normal and"
2823
0
       " thread local symbol"),
2824
0
           abfd, name);
2825
0
        bfd_set_error (bfd_error_bad_value);
2826
0
        goto error_return;
2827
0
      }
2828
0
        }
2829
2830
0
      if (old_tls_type != tls_type)
2831
0
        {
2832
0
    if (eh != NULL)
2833
0
      eh->tls_type = tls_type;
2834
0
    else
2835
0
      elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
2836
0
        }
2837
0
    }
2838
    /* Fall through */
2839
2840
0
  case R_X86_64_GOTOFF64:
2841
0
  case R_X86_64_GOTPC32:
2842
0
  case R_X86_64_GOTPC64:
2843
0
  create_got:
2844
0
    if (eh != NULL)
2845
0
      eh->zero_undefweak &= 0x2;
2846
0
    break;
2847
2848
0
  case R_X86_64_PLT32:
2849
    /* This symbol requires a procedure linkage table entry.  We
2850
       actually build the entry in adjust_dynamic_symbol,
2851
       because this might be a case of linking PIC code which is
2852
       never referenced by a dynamic object, in which case we
2853
       don't need to generate a procedure linkage table entry
2854
       after all.  */
2855
2856
    /* If this is a local symbol, we resolve it directly without
2857
       creating a procedure linkage table entry.  */
2858
0
    if (h == NULL)
2859
0
      continue;
2860
2861
0
    eh->zero_undefweak &= 0x2;
2862
0
    h->needs_plt = 1;
2863
0
    h->plt.refcount = 1;
2864
0
    break;
2865
2866
0
  case R_X86_64_PLTOFF64:
2867
    /* This tries to form the 'address' of a function relative
2868
       to GOT.  For global symbols we need a PLT entry.  */
2869
0
    if (h != NULL)
2870
0
      {
2871
0
        h->needs_plt = 1;
2872
0
        h->plt.refcount = 1;
2873
0
      }
2874
0
    goto create_got;
2875
2876
0
  case R_X86_64_SIZE32:
2877
0
  case R_X86_64_SIZE64:
2878
0
    size_reloc = true;
2879
0
    goto do_size;
2880
2881
0
  case R_X86_64_32:
2882
0
    if (!ABI_64_P (abfd))
2883
0
      goto pointer;
2884
    /* Fall through.  */
2885
0
  case R_X86_64_8:
2886
0
  case R_X86_64_16:
2887
0
  case R_X86_64_32S:
2888
    /* Check relocation overflow as these relocs may lead to
2889
       run-time relocation overflow.  Don't error out for
2890
       sections we don't care about, such as debug sections or
2891
       when relocation overflow check is disabled.  */
2892
0
    if (!htab->params->no_reloc_overflow_check
2893
0
        && !converted_reloc
2894
0
        && (bfd_link_pic (info)
2895
0
      || (bfd_link_executable (info)
2896
0
          && h != NULL
2897
0
          && !h->def_regular
2898
0
          && h->def_dynamic
2899
0
          && (sec->flags & SEC_READONLY) == 0)))
2900
0
      {
2901
0
        elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
2902
0
           &x86_64_elf_howto_table[r_type]);
2903
0
        goto error_return;
2904
0
      }
2905
    /* Fall through.  */
2906
2907
0
  case R_X86_64_PC8:
2908
0
  case R_X86_64_PC16:
2909
0
  case R_X86_64_PC32:
2910
0
  case R_X86_64_PC64:
2911
0
  case R_X86_64_64:
2912
0
  pointer:
2913
0
    if (eh != NULL && (sec->flags & SEC_CODE) != 0)
2914
0
      eh->zero_undefweak |= 0x2;
2915
    /* We are called after all symbols have been resolved.  Only
2916
       relocation against STT_GNU_IFUNC symbol must go through
2917
       PLT.  */
2918
0
    if (h != NULL
2919
0
        && (bfd_link_executable (info)
2920
0
      || h->type == STT_GNU_IFUNC))
2921
0
      {
2922
0
        bool func_pointer_ref = false;
2923
2924
0
        if (r_type == R_X86_64_PC32)
2925
0
    {
2926
      /* Since something like ".long foo - ." may be used
2927
         as pointer, make sure that PLT is used if foo is
2928
         a function defined in a shared library.  */
2929
0
      if ((sec->flags & SEC_CODE) == 0)
2930
0
        {
2931
0
          h->pointer_equality_needed = 1;
2932
0
          if (bfd_link_pie (info)
2933
0
        && h->type == STT_FUNC
2934
0
        && !h->def_regular
2935
0
        && h->def_dynamic)
2936
0
      {
2937
0
        h->needs_plt = 1;
2938
0
        h->plt.refcount = 1;
2939
0
      }
2940
0
        }
2941
0
    }
2942
0
        else if (r_type != R_X86_64_PC64)
2943
0
    {
2944
      /* At run-time, R_X86_64_64 can be resolved for both
2945
         x86-64 and x32. But R_X86_64_32 and R_X86_64_32S
2946
         can only be resolved for x32.  Function pointer
2947
         reference doesn't need PLT for pointer equality.  */
2948
0
      if ((sec->flags & SEC_READONLY) == 0
2949
0
          && (r_type == R_X86_64_64
2950
0
        || (!ABI_64_P (abfd)
2951
0
            && (r_type == R_X86_64_32
2952
0
          || r_type == R_X86_64_32S))))
2953
0
        func_pointer_ref = true;
2954
2955
      /* IFUNC symbol needs pointer equality in PDE so that
2956
         function pointer reference will be resolved to its
2957
         PLT entry directly.  */
2958
0
      if (!func_pointer_ref
2959
0
          || (bfd_link_pde (info)
2960
0
        && h->type == STT_GNU_IFUNC))
2961
0
        h->pointer_equality_needed = 1;
2962
0
    }
2963
2964
0
        if (!func_pointer_ref)
2965
0
    {
2966
      /* If this reloc is in a read-only section, we might
2967
         need a copy reloc.  We can't check reliably at this
2968
         stage whether the section is read-only, as input
2969
         sections have not yet been mapped to output sections.
2970
         Tentatively set the flag for now, and correct in
2971
         adjust_dynamic_symbol.  */
2972
0
      h->non_got_ref = 1;
2973
2974
0
      if (!elf_has_indirect_extern_access (sec->owner))
2975
0
        eh->non_got_ref_without_indirect_extern_access = 1;
2976
2977
      /* We may need a .plt entry if the symbol is a function
2978
         defined in a shared lib or is a function referenced
2979
         from the code or read-only section.  */
2980
0
      if (!h->def_regular
2981
0
          || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
2982
0
        h->plt.refcount = 1;
2983
2984
0
      if (htab->elf.target_os != is_solaris
2985
0
          && h->pointer_equality_needed
2986
0
          && h->type == STT_FUNC
2987
0
          && eh->def_protected
2988
0
          && !SYMBOL_DEFINED_NON_SHARED_P (h)
2989
0
          && h->def_dynamic)
2990
0
        {
2991
          /* Disallow non-canonical reference to canonical
2992
       protected function.  */
2993
0
          _bfd_error_handler
2994
      /* xgettext:c-format */
2995
0
      (_("%pB: non-canonical reference to canonical "
2996
0
         "protected function `%s' in %pB"),
2997
0
       abfd, h->root.root.string,
2998
0
       h->root.u.def.section->owner);
2999
0
          bfd_set_error (bfd_error_bad_value);
3000
0
          goto error_return;
3001
0
        }
3002
0
    }
3003
0
      }
3004
3005
0
    size_reloc = false;
3006
0
  do_size:
3007
0
    if (!no_dynreloc
3008
0
        && NEED_DYNAMIC_RELOCATION_P (true, info, true, h, sec,
3009
0
              r_type,
3010
0
              htab->pointer_r_type))
3011
0
      {
3012
0
        struct elf_dyn_relocs *p;
3013
0
        struct elf_dyn_relocs **head;
3014
3015
        /* If this is a global symbol, we count the number of
3016
     relocations we need for this symbol.  */
3017
0
        if (h != NULL)
3018
0
    head = &h->dyn_relocs;
3019
0
        else
3020
0
    {
3021
      /* Track dynamic relocs needed for local syms too.
3022
         We really need local syms available to do this
3023
         easily.  Oh well.  */
3024
0
      asection *s;
3025
0
      void **vpp;
3026
3027
0
      isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
3028
0
            abfd, r_symndx);
3029
0
      if (isym == NULL)
3030
0
        goto error_return;
3031
3032
0
      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3033
0
      if (s == NULL)
3034
0
        s = sec;
3035
3036
      /* Beware of type punned pointers vs strict aliasing
3037
         rules.  */
3038
0
      vpp = &(elf_section_data (s)->local_dynrel);
3039
0
      head = (struct elf_dyn_relocs **)vpp;
3040
0
    }
3041
3042
0
        p = *head;
3043
0
        if (p == NULL || p->sec != sec)
3044
0
    {
3045
0
      size_t amt = sizeof *p;
3046
3047
0
      p = ((struct elf_dyn_relocs *)
3048
0
           bfd_alloc (htab->elf.dynobj, amt));
3049
0
      if (p == NULL)
3050
0
        goto error_return;
3051
0
      p->next = *head;
3052
0
      *head = p;
3053
0
      p->sec = sec;
3054
0
      p->count = 0;
3055
0
      p->pc_count = 0;
3056
0
    }
3057
3058
0
        p->count += 1;
3059
        /* Count size relocation as PC-relative relocation.  */
3060
0
        if (X86_PCREL_TYPE_P (true, r_type) || size_reloc)
3061
0
    p->pc_count += 1;
3062
0
      }
3063
0
    break;
3064
3065
0
  case R_X86_64_CODE_5_GOTPC32_TLSDESC:
3066
0
  case R_X86_64_CODE_6_GOTPC32_TLSDESC:
3067
0
      {
3068
        /* These relocations are added only for completeness and
3069
     aren't be used.  */
3070
0
        if (h)
3071
0
    name = h->root.root.string;
3072
0
        else
3073
0
    name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
3074
0
           NULL);
3075
0
        _bfd_error_handler
3076
    /* xgettext:c-format */
3077
0
    (_("%pB: unsupported relocation %s against symbol `%s'"),
3078
0
     abfd, x86_64_elf_howto_table[r_type].name, name);
3079
0
      }
3080
0
    break;
3081
3082
    /* This relocation describes the C++ object vtable hierarchy.
3083
       Reconstruct it for later use during GC.  */
3084
0
  case R_X86_64_GNU_VTINHERIT:
3085
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3086
0
      goto error_return;
3087
0
    break;
3088
3089
    /* This relocation describes which C++ vtable entries are actually
3090
       used.  Record for later use during GC.  */
3091
0
  case R_X86_64_GNU_VTENTRY:
3092
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3093
0
      goto error_return;
3094
0
    break;
3095
3096
0
  default:
3097
0
    break;
3098
0
  }
3099
0
    }
3100
3101
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
3102
0
    {
3103
0
      if (!converted)
3104
0
  _bfd_elf_munmap_section_contents (sec, contents);
3105
0
      else
3106
0
  {
3107
    /* Cache the section contents for elf_link_input_bfd if any
3108
       load is converted or --no-keep-memory isn't used.  */
3109
0
    elf_section_data (sec)->this_hdr.contents = contents;
3110
0
    info->cache_size += sec->size;
3111
0
  }
3112
0
    }
3113
3114
  /* Cache relocations if any load is converted.  */
3115
0
  if (elf_section_data (sec)->relocs != relocs && converted)
3116
0
    elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
3117
3118
0
  return true;
3119
3120
0
 error_return:
3121
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
3122
0
    _bfd_elf_munmap_section_contents (sec, contents);
3123
0
  sec->check_relocs_failed = 1;
3124
0
  return false;
3125
0
}
3126
3127
static bool
3128
elf_x86_64_early_size_sections (struct bfd_link_info *info)
3129
0
{
3130
0
  bfd *abfd;
3131
3132
  /* Scan relocations after rel_from_abs has been set on __ehdr_start.  */
3133
0
  for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
3134
0
    if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
3135
0
  && !_bfd_elf_link_iterate_on_relocs (abfd, info,
3136
0
               elf_x86_64_scan_relocs))
3137
0
      return false;
3138
3139
0
  return _bfd_x86_elf_early_size_sections (info);
3140
0
}
3141
3142
/* Return the relocation value for @tpoff relocation
3143
   if STT_TLS virtual address is ADDRESS.  */
3144
3145
static bfd_vma
3146
elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
3147
0
{
3148
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
3149
0
  elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
3150
0
  bfd_vma static_tls_size;
3151
3152
  /* If tls_segment is NULL, we should have signalled an error already.  */
3153
0
  if (htab->tls_sec == NULL)
3154
0
    return 0;
3155
3156
  /* Consider special static TLS alignment requirements.  */
3157
0
  static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
3158
0
  return address - static_tls_size - htab->tls_sec->vma;
3159
0
}
3160
3161
/* Relocate an x86_64 ELF section.  */
3162
3163
static int
3164
elf_x86_64_relocate_section (struct bfd_link_info *info,
3165
           bfd *input_bfd,
3166
           asection *input_section,
3167
           bfd_byte *contents,
3168
           Elf_Internal_Rela *relocs,
3169
           Elf_Internal_Sym *local_syms,
3170
           asection **local_sections)
3171
0
{
3172
0
  struct elf_x86_link_hash_table *htab;
3173
0
  Elf_Internal_Shdr *symtab_hdr;
3174
0
  struct elf_link_hash_entry **sym_hashes;
3175
0
  bfd_vma *local_got_offsets;
3176
0
  bfd_vma *local_tlsdesc_gotents;
3177
0
  Elf_Internal_Rela *rel;
3178
0
  Elf_Internal_Rela *wrel;
3179
0
  Elf_Internal_Rela *relend;
3180
0
  unsigned int plt_entry_size;
3181
0
  bool status;
3182
3183
  /* Skip if check_relocs or scan_relocs failed.  */
3184
0
  if (input_section->check_relocs_failed)
3185
0
    return false;
3186
3187
0
  htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
3188
0
  if (htab == NULL)
3189
0
    return false;
3190
3191
0
  if (!is_x86_elf (input_bfd, htab))
3192
0
    {
3193
0
      bfd_set_error (bfd_error_wrong_format);
3194
0
      return false;
3195
0
    }
3196
3197
0
  plt_entry_size = htab->plt.plt_entry_size;
3198
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
3199
0
  sym_hashes = elf_sym_hashes (input_bfd);
3200
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
3201
0
  local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
3202
3203
0
  _bfd_x86_elf_set_tls_module_base (info);
3204
3205
0
  status = true;
3206
0
  rel = wrel = relocs;
3207
0
  relend = relocs + input_section->reloc_count;
3208
0
  for (; rel < relend; wrel++, rel++)
3209
0
    {
3210
0
      unsigned int r_type, r_type_tls;
3211
0
      reloc_howto_type *howto;
3212
0
      unsigned long r_symndx;
3213
0
      struct elf_link_hash_entry *h;
3214
0
      struct elf_x86_link_hash_entry *eh;
3215
0
      Elf_Internal_Sym *sym;
3216
0
      asection *sec;
3217
0
      bfd_vma off, offplt, plt_offset;
3218
0
      bfd_vma relocation;
3219
0
      bool unresolved_reloc;
3220
0
      bfd_reloc_status_type r;
3221
0
      int tls_type;
3222
0
      asection *base_got, *resolved_plt;
3223
0
      bfd_vma st_size;
3224
0
      bool resolved_to_zero;
3225
0
      bool relative_reloc;
3226
0
      bool converted_reloc;
3227
0
      bool need_copy_reloc_in_pie;
3228
0
      bool no_copyreloc_p;
3229
3230
0
      r_type = ELF32_R_TYPE (rel->r_info);
3231
0
      if (r_type == (int) R_X86_64_GNU_VTINHERIT
3232
0
    || r_type == (int) R_X86_64_GNU_VTENTRY)
3233
0
  {
3234
0
    if (wrel != rel)
3235
0
      *wrel = *rel;
3236
0
    continue;
3237
0
  }
3238
3239
0
      r_symndx = htab->r_sym (rel->r_info);
3240
0
      converted_reloc = (r_type & R_X86_64_converted_reloc_bit) != 0;
3241
0
      if (converted_reloc)
3242
0
  {
3243
0
    r_type &= ~R_X86_64_converted_reloc_bit;
3244
0
    rel->r_info = htab->r_info (r_symndx, r_type);
3245
0
  }
3246
3247
0
      howto = elf_x86_64_rtype_to_howto (input_bfd, r_type);
3248
0
      if (howto == NULL)
3249
0
  return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
3250
3251
0
      h = NULL;
3252
0
      sym = NULL;
3253
0
      sec = NULL;
3254
0
      unresolved_reloc = false;
3255
0
      if (r_symndx < symtab_hdr->sh_info)
3256
0
  {
3257
0
    sym = local_syms + r_symndx;
3258
0
    sec = local_sections[r_symndx];
3259
3260
0
    relocation = _bfd_elf_rela_local_sym (info->output_bfd,
3261
0
            sym, &sec, rel);
3262
0
    st_size = sym->st_size;
3263
3264
    /* Relocate against local STT_GNU_IFUNC symbol.  */
3265
0
    if (!bfd_link_relocatable (info)
3266
0
        && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3267
0
      {
3268
0
        h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd,
3269
0
               rel, false);
3270
0
        if (h == NULL)
3271
0
    abort ();
3272
3273
        /* Set STT_GNU_IFUNC symbol value.  */
3274
0
        h->root.u.def.value = sym->st_value;
3275
0
        h->root.u.def.section = sec;
3276
0
      }
3277
0
  }
3278
0
      else
3279
0
  {
3280
0
    bool warned ATTRIBUTE_UNUSED;
3281
0
    bool ignored ATTRIBUTE_UNUSED;
3282
3283
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3284
0
           r_symndx, symtab_hdr, sym_hashes,
3285
0
           h, sec, relocation,
3286
0
           unresolved_reloc, warned, ignored);
3287
0
    st_size = h->size;
3288
0
  }
3289
3290
0
      if (sec != NULL && discarded_section (sec))
3291
0
  {
3292
0
    _bfd_clear_contents (howto, input_bfd, input_section,
3293
0
             contents, rel->r_offset);
3294
0
    wrel->r_offset = rel->r_offset;
3295
0
    wrel->r_info = 0;
3296
0
    wrel->r_addend = 0;
3297
3298
    /* For ld -r, remove relocations in debug sections against
3299
       sections defined in discarded sections, including sframe
3300
       sections.  Not done for eh_frame editing code expects to
3301
       be present.  NB: Since sframe code keeps R_X86_64_NONE
3302
       reloc as is, its r_offset is wrong, we must not generate
3303
       R_X86_64_NONE reloc in sframe section.  */
3304
0
     if (bfd_link_relocatable (info)
3305
0
         && ((input_section->flags & SEC_DEBUGGING) != 0
3306
0
       || elf_section_type (input_section) == SHT_GNU_SFRAME))
3307
0
       wrel--;
3308
3309
0
    continue;
3310
0
  }
3311
3312
0
      if (bfd_link_relocatable (info))
3313
0
  {
3314
0
    if (wrel != rel)
3315
0
      *wrel = *rel;
3316
0
    continue;
3317
0
  }
3318
3319
0
      if (rel->r_addend == 0 && !ABI_64_P (info->output_bfd))
3320
0
  {
3321
0
    if (r_type == R_X86_64_64)
3322
0
      {
3323
        /* For x32, treat R_X86_64_64 like R_X86_64_32 and
3324
     zero-extend it to 64bit if addend is zero.  */
3325
0
        r_type = R_X86_64_32;
3326
0
        memset (contents + rel->r_offset + 4, 0, 4);
3327
0
      }
3328
0
    else if (r_type == R_X86_64_SIZE64)
3329
0
      {
3330
        /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
3331
     zero-extend it to 64bit if addend is zero.  */
3332
0
        r_type = R_X86_64_SIZE32;
3333
0
        memset (contents + rel->r_offset + 4, 0, 4);
3334
0
      }
3335
0
  }
3336
3337
0
      eh = (struct elf_x86_link_hash_entry *) h;
3338
3339
      /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3340
   it here if it is defined in a non-shared object.  */
3341
0
      if (h != NULL
3342
0
    && h->type == STT_GNU_IFUNC
3343
0
    && h->def_regular)
3344
0
  {
3345
0
    bfd_vma plt_index;
3346
0
    const char *name;
3347
3348
0
    if ((input_section->flags & SEC_ALLOC) == 0)
3349
0
      {
3350
        /* If this is a SHT_NOTE section without SHF_ALLOC, treat
3351
           STT_GNU_IFUNC symbol as STT_FUNC.  */
3352
0
        if (elf_section_type (input_section) == SHT_NOTE)
3353
0
    goto skip_ifunc;
3354
        /* Dynamic relocs are not propagated for SEC_DEBUGGING
3355
     sections because such sections are not SEC_ALLOC and
3356
     thus ld.so will not process them.  */
3357
0
        if ((input_section->flags & SEC_DEBUGGING) != 0)
3358
0
    continue;
3359
0
        abort ();
3360
0
      }
3361
3362
0
    switch (r_type)
3363
0
      {
3364
0
      default:
3365
0
        break;
3366
3367
0
      case R_X86_64_GOTPCREL:
3368
0
      case R_X86_64_GOTPCRELX:
3369
0
      case R_X86_64_REX_GOTPCRELX:
3370
0
      case R_X86_64_CODE_4_GOTPCRELX:
3371
0
      case R_X86_64_CODE_5_GOTPCRELX:
3372
0
      case R_X86_64_CODE_6_GOTPCRELX:
3373
0
      case R_X86_64_GOTPCREL64:
3374
0
        base_got = htab->elf.sgot;
3375
0
        off = h->got.offset;
3376
3377
0
        if (base_got == NULL)
3378
0
    abort ();
3379
3380
0
        if (off == (bfd_vma) -1)
3381
0
    {
3382
      /* We can't use h->got.offset here to save state, or
3383
         even just remember the offset, as finish_dynamic_symbol
3384
         would use that as offset into .got.  */
3385
3386
0
      if (h->plt.offset == (bfd_vma) -1)
3387
0
        abort ();
3388
3389
0
      if (htab->elf.splt != NULL)
3390
0
        {
3391
0
          plt_index = (h->plt.offset / plt_entry_size
3392
0
           - htab->plt.has_plt0);
3393
0
          off = (plt_index + 3) * GOT_ENTRY_SIZE;
3394
0
          base_got = htab->elf.sgotplt;
3395
0
        }
3396
0
      else
3397
0
        {
3398
0
          plt_index = h->plt.offset / plt_entry_size;
3399
0
          off = plt_index * GOT_ENTRY_SIZE;
3400
0
          base_got = htab->elf.igotplt;
3401
0
        }
3402
3403
0
      if (h->dynindx == -1
3404
0
          || h->forced_local
3405
0
          || info->symbolic)
3406
0
        {
3407
          /* This references the local defitionion.  We must
3408
       initialize this entry in the global offset table.
3409
       Since the offset must always be a multiple of 8,
3410
       we use the least significant bit to record
3411
       whether we have initialized it already.
3412
3413
       When doing a dynamic link, we create a .rela.got
3414
       relocation entry to initialize the value.  This
3415
       is done in the finish_dynamic_symbol routine.   */
3416
0
          if ((off & 1) != 0)
3417
0
      off &= ~1;
3418
0
          else
3419
0
      {
3420
0
        bfd_put_64 (info->output_bfd, relocation,
3421
0
              base_got->contents + off);
3422
        /* Note that this is harmless for the GOTPLT64
3423
           case, as -1 | 1 still is -1.  */
3424
0
        h->got.offset |= 1;
3425
0
      }
3426
0
        }
3427
0
    }
3428
3429
0
        relocation = (base_got->output_section->vma
3430
0
          + base_got->output_offset + off);
3431
3432
0
        goto do_relocation;
3433
0
      }
3434
3435
0
    if (h->plt.offset == (bfd_vma) -1)
3436
0
      {
3437
        /* Handle static pointers of STT_GNU_IFUNC symbols.  */
3438
0
        if (r_type == htab->pointer_r_type
3439
0
      && (input_section->flags & SEC_CODE) == 0)
3440
0
    goto do_ifunc_pointer;
3441
0
        goto bad_ifunc_reloc;
3442
0
      }
3443
3444
    /* STT_GNU_IFUNC symbol must go through PLT.  */
3445
0
    if (htab->elf.splt != NULL)
3446
0
      {
3447
0
        if (htab->plt_second != NULL)
3448
0
    {
3449
0
      resolved_plt = htab->plt_second;
3450
0
      plt_offset = eh->plt_second.offset;
3451
0
    }
3452
0
        else
3453
0
    {
3454
0
      resolved_plt = htab->elf.splt;
3455
0
      plt_offset =  h->plt.offset;
3456
0
    }
3457
0
      }
3458
0
    else
3459
0
      {
3460
0
        resolved_plt = htab->elf.iplt;
3461
0
        plt_offset =  h->plt.offset;
3462
0
      }
3463
3464
0
    relocation = (resolved_plt->output_section->vma
3465
0
      + resolved_plt->output_offset + plt_offset);
3466
3467
0
    switch (r_type)
3468
0
      {
3469
0
      default:
3470
0
      bad_ifunc_reloc:
3471
0
        if (h->root.root.string)
3472
0
    name = h->root.root.string;
3473
0
        else
3474
0
    name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3475
0
           NULL);
3476
0
        _bfd_error_handler
3477
    /* xgettext:c-format */
3478
0
    (_("%pB: relocation %s against STT_GNU_IFUNC "
3479
0
       "symbol `%s' isn't supported"), input_bfd,
3480
0
     howto->name, name);
3481
0
        bfd_set_error (bfd_error_bad_value);
3482
0
        return false;
3483
3484
0
      case R_X86_64_32S:
3485
0
        if (bfd_link_pic (info))
3486
0
    abort ();
3487
0
        goto do_relocation;
3488
3489
0
      case R_X86_64_32:
3490
0
        if (ABI_64_P (info->output_bfd))
3491
0
    goto do_relocation;
3492
        /* FALLTHROUGH */
3493
0
      case R_X86_64_64:
3494
0
      do_ifunc_pointer:
3495
0
        if (rel->r_addend != 0)
3496
0
    {
3497
0
      if (h->root.root.string)
3498
0
        name = h->root.root.string;
3499
0
      else
3500
0
        name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3501
0
               sym, NULL);
3502
0
      _bfd_error_handler
3503
        /* xgettext:c-format */
3504
0
        (_("%pB: relocation %s against STT_GNU_IFUNC "
3505
0
           "symbol `%s' has non-zero addend: %" PRId64),
3506
0
         input_bfd, howto->name, name, (int64_t) rel->r_addend);
3507
0
      bfd_set_error (bfd_error_bad_value);
3508
0
      return false;
3509
0
    }
3510
3511
        /* Generate dynamic relcoation only when there is a
3512
     non-GOT reference in a shared object or there is no
3513
     PLT.  */
3514
0
        if ((bfd_link_pic (info) && h->non_got_ref)
3515
0
      || h->plt.offset == (bfd_vma) -1)
3516
0
    {
3517
0
      Elf_Internal_Rela outrel;
3518
0
      asection *sreloc;
3519
3520
      /* Need a dynamic relocation to get the real function
3521
         address.  */
3522
0
      outrel.r_offset = _bfd_elf_section_offset (info->output_bfd,
3523
0
                   info,
3524
0
                   input_section,
3525
0
                   rel->r_offset);
3526
0
      if (outrel.r_offset == (bfd_vma) -1
3527
0
          || outrel.r_offset == (bfd_vma) -2)
3528
0
        abort ();
3529
3530
0
      outrel.r_offset += (input_section->output_section->vma
3531
0
              + input_section->output_offset);
3532
3533
0
      if (POINTER_LOCAL_IFUNC_P (info, h))
3534
0
        {
3535
0
          info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3536
0
                h->root.root.string,
3537
0
                h->root.u.def.section->owner);
3538
3539
          /* This symbol is resolved locally.  */
3540
0
          outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
3541
0
          outrel.r_addend = (h->root.u.def.value
3542
0
           + h->root.u.def.section->output_section->vma
3543
0
           + h->root.u.def.section->output_offset);
3544
3545
0
          if (htab->params->report_relative_reloc)
3546
0
      _bfd_x86_elf_link_report_relative_reloc
3547
0
        (info, input_section, h, sym,
3548
0
         "R_X86_64_IRELATIVE", &outrel);
3549
0
        }
3550
0
      else
3551
0
        {
3552
0
          outrel.r_info = htab->r_info (h->dynindx, r_type);
3553
0
          outrel.r_addend = 0;
3554
0
        }
3555
3556
      /* Dynamic relocations are stored in
3557
         1. .rela.ifunc section in PIC object.
3558
         2. .rela.got section in dynamic executable.
3559
         3. .rela.iplt section in static executable.  */
3560
0
      if (bfd_link_pic (info))
3561
0
        sreloc = htab->elf.irelifunc;
3562
0
      else if (htab->elf.splt != NULL)
3563
0
        sreloc = htab->elf.srelgot;
3564
0
      else
3565
0
        sreloc = htab->elf.irelplt;
3566
0
      _bfd_elf_append_rela (info->output_bfd, sreloc, &outrel);
3567
3568
      /* If this reloc is against an external symbol, we
3569
         do not want to fiddle with the addend.  Otherwise,
3570
         we need to include the symbol value so that it
3571
         becomes an addend for the dynamic reloc.  For an
3572
         internal symbol, we have updated addend.  */
3573
0
      continue;
3574
0
    }
3575
        /* FALLTHROUGH */
3576
0
      case R_X86_64_PC32:
3577
0
      case R_X86_64_PC64:
3578
0
      case R_X86_64_PLT32:
3579
0
        goto do_relocation;
3580
0
      }
3581
0
  }
3582
3583
0
    skip_ifunc:
3584
0
      resolved_to_zero = (eh != NULL
3585
0
        && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
3586
3587
      /* When generating a shared object, the relocations handled here are
3588
   copied into the output file to be resolved at run time.  */
3589
0
      switch (r_type)
3590
0
  {
3591
0
  case R_X86_64_GOT32:
3592
0
  case R_X86_64_GOT64:
3593
    /* Relocation is to the entry for this symbol in the global
3594
       offset table.  */
3595
0
  case R_X86_64_GOTPCREL:
3596
0
  case R_X86_64_GOTPCRELX:
3597
0
  case R_X86_64_REX_GOTPCRELX:
3598
0
  case R_X86_64_CODE_4_GOTPCRELX:
3599
0
  case R_X86_64_CODE_5_GOTPCRELX:
3600
0
  case R_X86_64_CODE_6_GOTPCRELX:
3601
0
  case R_X86_64_GOTPCREL64:
3602
    /* Use global offset table entry as symbol value.  */
3603
0
  case R_X86_64_GOTPLT64:
3604
    /* This is obsolete and treated the same as GOT64.  */
3605
0
    base_got = htab->elf.sgot;
3606
3607
0
    if (htab->elf.sgot == NULL)
3608
0
      abort ();
3609
3610
0
    relative_reloc = false;
3611
0
    if (h != NULL)
3612
0
      {
3613
0
        off = h->got.offset;
3614
0
        if (h->needs_plt
3615
0
      && h->plt.offset != (bfd_vma)-1
3616
0
      && off == (bfd_vma)-1)
3617
0
    {
3618
      /* We can't use h->got.offset here to save
3619
         state, or even just remember the offset, as
3620
         finish_dynamic_symbol would use that as offset into
3621
         .got.  */
3622
0
      bfd_vma plt_index = (h->plt.offset / plt_entry_size
3623
0
               - htab->plt.has_plt0);
3624
0
      off = (plt_index + 3) * GOT_ENTRY_SIZE;
3625
0
      base_got = htab->elf.sgotplt;
3626
0
    }
3627
3628
0
        if (RESOLVED_LOCALLY_P (info, h, htab))
3629
0
    {
3630
      /* We must initialize this entry in the global offset
3631
         table.  Since the offset must always be a multiple
3632
         of 8, we use the least significant bit to record
3633
         whether we have initialized it already.
3634
3635
         When doing a dynamic link, we create a .rela.got
3636
         relocation entry to initialize the value.  This is
3637
         done in the finish_dynamic_symbol routine.  */
3638
0
      if ((off & 1) != 0)
3639
0
        off &= ~1;
3640
0
      else
3641
0
        {
3642
0
          bfd_put_64 (info->output_bfd, relocation,
3643
0
          base_got->contents + off);
3644
          /* Note that this is harmless for the GOTPLT64 case,
3645
       as -1 | 1 still is -1.  */
3646
0
          h->got.offset |= 1;
3647
3648
          /* NB: Don't generate relative relocation here if
3649
       it has been generated by DT_RELR.  */
3650
0
          if (!info->enable_dt_relr
3651
0
        && GENERATE_RELATIVE_RELOC_P (info, h))
3652
0
      {
3653
        /* If this symbol isn't dynamic in PIC,
3654
           generate R_X86_64_RELATIVE here.  */
3655
0
        eh->no_finish_dynamic_symbol = 1;
3656
0
        relative_reloc = true;
3657
0
      }
3658
0
        }
3659
0
    }
3660
0
        else
3661
0
    unresolved_reloc = false;
3662
0
      }
3663
0
    else
3664
0
      {
3665
0
        if (local_got_offsets == NULL)
3666
0
    abort ();
3667
3668
0
        off = local_got_offsets[r_symndx];
3669
3670
        /* The offset must always be a multiple of 8.  We use
3671
     the least significant bit to record whether we have
3672
     already generated the necessary reloc.  */
3673
0
        if ((off & 1) != 0)
3674
0
    off &= ~1;
3675
0
        else
3676
0
    {
3677
0
      bfd_put_64 (info->output_bfd, relocation,
3678
0
            base_got->contents + off);
3679
0
      local_got_offsets[r_symndx] |= 1;
3680
3681
      /* NB: GOTPCREL relocations against local absolute
3682
         symbol store relocation value in the GOT slot
3683
         without relative relocation.  Don't generate
3684
         relative relocation here if it has been generated
3685
         by DT_RELR.  */
3686
0
      if (!info->enable_dt_relr
3687
0
          && bfd_link_pic (info)
3688
0
          && !(sym->st_shndx == SHN_ABS
3689
0
         && (r_type == R_X86_64_GOTPCREL
3690
0
             || r_type == R_X86_64_GOTPCRELX
3691
0
             || r_type == R_X86_64_REX_GOTPCRELX
3692
0
             || r_type == R_X86_64_CODE_4_GOTPCRELX
3693
0
             || r_type == R_X86_64_CODE_5_GOTPCRELX
3694
0
             || r_type == R_X86_64_CODE_6_GOTPCRELX)))
3695
0
        relative_reloc = true;
3696
0
    }
3697
0
      }
3698
3699
0
    if (relative_reloc)
3700
0
      {
3701
0
        asection *s;
3702
0
        Elf_Internal_Rela outrel;
3703
3704
        /* We need to generate a R_X86_64_RELATIVE reloc
3705
     for the dynamic linker.  */
3706
0
        s = htab->elf.srelgot;
3707
0
        if (s == NULL)
3708
0
    abort ();
3709
3710
0
        outrel.r_offset = (base_got->output_section->vma
3711
0
         + base_got->output_offset
3712
0
         + off);
3713
0
        outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
3714
0
        outrel.r_addend = relocation;
3715
3716
0
        if (htab->params->report_relative_reloc)
3717
0
    _bfd_x86_elf_link_report_relative_reloc
3718
0
      (info, input_section, h, sym, "R_X86_64_RELATIVE",
3719
0
       &outrel);
3720
3721
0
        _bfd_elf_append_rela (info->output_bfd, s, &outrel);
3722
0
      }
3723
3724
0
    if (off >= (bfd_vma) -2)
3725
0
      abort ();
3726
3727
0
    relocation = base_got->output_section->vma
3728
0
           + base_got->output_offset + off;
3729
0
    if (r_type != R_X86_64_GOTPCREL
3730
0
        && r_type != R_X86_64_GOTPCRELX
3731
0
        && r_type != R_X86_64_REX_GOTPCRELX
3732
0
        && r_type != R_X86_64_CODE_4_GOTPCRELX
3733
0
        && r_type != R_X86_64_CODE_5_GOTPCRELX
3734
0
        && r_type != R_X86_64_CODE_6_GOTPCRELX
3735
0
        && r_type != R_X86_64_GOTPCREL64)
3736
0
      relocation -= htab->elf.sgotplt->output_section->vma
3737
0
        - htab->elf.sgotplt->output_offset;
3738
3739
0
    break;
3740
3741
0
  case R_X86_64_GOTOFF64:
3742
    /* Relocation is relative to the start of the global offset
3743
       table.  */
3744
3745
    /* Check to make sure it isn't a protected function or data
3746
       symbol for shared library since it may not be local when
3747
       used as function address or with copy relocation.  We also
3748
       need to make sure that a symbol is referenced locally.  */
3749
0
    if (bfd_link_pic (info) && h)
3750
0
      {
3751
0
        if (!h->def_regular)
3752
0
    {
3753
0
      const char *v;
3754
3755
0
      switch (ELF_ST_VISIBILITY (h->other))
3756
0
        {
3757
0
        case STV_HIDDEN:
3758
0
          v = _("hidden symbol");
3759
0
          break;
3760
0
        case STV_INTERNAL:
3761
0
          v = _("internal symbol");
3762
0
          break;
3763
0
        case STV_PROTECTED:
3764
0
          v = _("protected symbol");
3765
0
          break;
3766
0
        default:
3767
0
          v = _("symbol");
3768
0
          break;
3769
0
        }
3770
3771
0
      _bfd_error_handler
3772
        /* xgettext:c-format */
3773
0
        (_("%pB: relocation R_X86_64_GOTOFF64 against undefined %s"
3774
0
           " `%s' can not be used when making a shared object"),
3775
0
         input_bfd, v, h->root.root.string);
3776
0
      bfd_set_error (bfd_error_bad_value);
3777
0
      return false;
3778
0
    }
3779
0
        else if (!bfd_link_executable (info)
3780
0
           && !SYMBOL_REFERENCES_LOCAL_P (info, h)
3781
0
           && (h->type == STT_FUNC
3782
0
         || h->type == STT_OBJECT)
3783
0
           && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3784
0
    {
3785
0
      _bfd_error_handler
3786
        /* xgettext:c-format */
3787
0
        (_("%pB: relocation R_X86_64_GOTOFF64 against protected %s"
3788
0
           " `%s' can not be used when making a shared object"),
3789
0
         input_bfd,
3790
0
         h->type == STT_FUNC ? "function" : "data",
3791
0
         h->root.root.string);
3792
0
      bfd_set_error (bfd_error_bad_value);
3793
0
      return false;
3794
0
    }
3795
0
      }
3796
3797
    /* Note that sgot is not involved in this
3798
       calculation.  We always want the start of .got.plt.  If we
3799
       defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3800
       permitted by the ABI, we might have to change this
3801
       calculation.  */
3802
0
    relocation -= htab->elf.sgotplt->output_section->vma
3803
0
      + htab->elf.sgotplt->output_offset;
3804
0
    break;
3805
3806
0
  case R_X86_64_GOTPC32:
3807
0
  case R_X86_64_GOTPC64:
3808
    /* Use global offset table as symbol value.  */
3809
0
    relocation = htab->elf.sgotplt->output_section->vma
3810
0
           + htab->elf.sgotplt->output_offset;
3811
0
    unresolved_reloc = false;
3812
0
    break;
3813
3814
0
  case R_X86_64_PLTOFF64:
3815
    /* Relocation is PLT entry relative to GOT.  For local
3816
       symbols it's the symbol itself relative to GOT.  */
3817
0
    if (h != NULL
3818
        /* See PLT32 handling.  */
3819
0
        && (h->plt.offset != (bfd_vma) -1
3820
0
      || eh->plt_got.offset != (bfd_vma) -1)
3821
0
        && htab->elf.splt != NULL)
3822
0
      {
3823
0
        if (eh->plt_got.offset != (bfd_vma) -1)
3824
0
    {
3825
      /* Use the GOT PLT.  */
3826
0
      resolved_plt = htab->plt_got;
3827
0
      plt_offset = eh->plt_got.offset;
3828
0
    }
3829
0
        else if (htab->plt_second != NULL)
3830
0
    {
3831
0
      resolved_plt = htab->plt_second;
3832
0
      plt_offset = eh->plt_second.offset;
3833
0
    }
3834
0
        else
3835
0
    {
3836
0
      resolved_plt = htab->elf.splt;
3837
0
      plt_offset = h->plt.offset;
3838
0
    }
3839
3840
0
        relocation = (resolved_plt->output_section->vma
3841
0
          + resolved_plt->output_offset
3842
0
          + plt_offset);
3843
0
        unresolved_reloc = false;
3844
0
      }
3845
3846
0
    relocation -= htab->elf.sgotplt->output_section->vma
3847
0
      + htab->elf.sgotplt->output_offset;
3848
0
    break;
3849
3850
0
  case R_X86_64_PLT32:
3851
    /* Relocation is to the entry for this symbol in the
3852
       procedure linkage table.  */
3853
3854
    /* Resolve a PLT32 reloc against a local symbol directly,
3855
       without using the procedure linkage table.  */
3856
0
    if (h == NULL)
3857
0
      break;
3858
3859
0
    if ((h->plt.offset == (bfd_vma) -1
3860
0
         && eh->plt_got.offset == (bfd_vma) -1)
3861
0
        || htab->elf.splt == NULL)
3862
0
      {
3863
        /* We didn't make a PLT entry for this symbol.  This
3864
     happens when statically linking PIC code, or when
3865
     using -Bsymbolic.  */
3866
0
        break;
3867
0
      }
3868
3869
0
  use_plt:
3870
0
    if (h->plt.offset != (bfd_vma) -1)
3871
0
      {
3872
0
        if (htab->plt_second != NULL)
3873
0
    {
3874
0
      resolved_plt = htab->plt_second;
3875
0
      plt_offset = eh->plt_second.offset;
3876
0
    }
3877
0
        else
3878
0
    {
3879
0
      resolved_plt = htab->elf.splt;
3880
0
      plt_offset = h->plt.offset;
3881
0
    }
3882
0
      }
3883
0
    else
3884
0
      {
3885
        /* Use the GOT PLT.  */
3886
0
        resolved_plt = htab->plt_got;
3887
0
        plt_offset = eh->plt_got.offset;
3888
0
      }
3889
3890
0
    relocation = (resolved_plt->output_section->vma
3891
0
      + resolved_plt->output_offset
3892
0
      + plt_offset);
3893
0
    unresolved_reloc = false;
3894
0
    break;
3895
3896
0
  case R_X86_64_SIZE32:
3897
0
  case R_X86_64_SIZE64:
3898
    /* Set to symbol size.  */
3899
0
    relocation = st_size;
3900
0
    goto direct;
3901
3902
0
  case R_X86_64_PC8:
3903
0
  case R_X86_64_PC16:
3904
0
  case R_X86_64_PC32:
3905
    /* Don't complain about -fPIC if the symbol is undefined when
3906
       building executable unless it is unresolved weak symbol,
3907
       references a dynamic definition in PIE or -z nocopyreloc
3908
       is used.  */
3909
0
    no_copyreloc_p
3910
0
      = (info->nocopyreloc
3911
0
         || (h != NULL
3912
0
       && !h->root.linker_def
3913
0
       && !h->root.ldscript_def
3914
0
       && eh->def_protected));
3915
3916
0
    if ((input_section->flags & SEC_ALLOC) != 0
3917
0
        && (input_section->flags & SEC_READONLY) != 0
3918
0
        && h != NULL
3919
0
        && ((bfd_link_executable (info)
3920
0
       && ((h->root.type == bfd_link_hash_undefweak
3921
0
      && (eh == NULL
3922
0
          || !UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
3923
0
                 eh)))
3924
0
           || (bfd_link_pie (info)
3925
0
         && !SYMBOL_DEFINED_NON_SHARED_P (h)
3926
0
         && h->def_dynamic)
3927
0
           || (no_copyreloc_p
3928
0
         && h->def_dynamic
3929
0
         && !(h->root.u.def.section->flags & SEC_CODE))))
3930
0
      || (bfd_link_pie (info)
3931
0
          && h->root.type == bfd_link_hash_undefweak)
3932
0
      || bfd_link_dll (info)))
3933
0
      {
3934
0
        bool fail = false;
3935
0
        if (SYMBOL_REFERENCES_LOCAL_P (info, h))
3936
0
    {
3937
      /* Symbol is referenced locally.  Make sure it is
3938
         defined locally.  */
3939
0
      fail = !SYMBOL_DEFINED_NON_SHARED_P (h);
3940
0
    }
3941
0
        else if (bfd_link_pie (info))
3942
0
    {
3943
      /* We can only use PC-relative relocations in PIE
3944
         from non-code sections.  */
3945
0
      if (h->root.type == bfd_link_hash_undefweak
3946
0
          || (h->type == STT_FUNC
3947
0
        && (sec->flags & SEC_CODE) != 0))
3948
0
        fail = true;
3949
0
    }
3950
0
        else if (no_copyreloc_p || bfd_link_dll (info))
3951
0
    {
3952
      /* Symbol doesn't need copy reloc and isn't
3953
         referenced locally.  Don't allow PC-relative
3954
         relocations against default and protected
3955
         symbols since address of protected function
3956
         and location of protected data may not be in
3957
         the shared object.   */
3958
0
      fail = (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3959
0
        || ELF_ST_VISIBILITY (h->other) == STV_PROTECTED);
3960
0
    }
3961
3962
0
        if (fail)
3963
0
    return elf_x86_64_need_pic (info, input_bfd, input_section,
3964
0
              h, NULL, NULL, howto);
3965
0
      }
3966
    /* Since x86-64 has PC-relative PLT, we can use PLT in PIE
3967
       as function address.  */
3968
0
    else if (h != NULL
3969
0
       && (input_section->flags & SEC_CODE) == 0
3970
0
       && bfd_link_pie (info)
3971
0
       && h->type == STT_FUNC
3972
0
       && !h->def_regular
3973
0
       && h->def_dynamic)
3974
0
      goto use_plt;
3975
    /* Fall through.  */
3976
3977
0
  case R_X86_64_8:
3978
0
  case R_X86_64_16:
3979
0
  case R_X86_64_32:
3980
0
  case R_X86_64_PC64:
3981
0
  case R_X86_64_64:
3982
    /* FIXME: The ABI says the linker should make sure the value is
3983
       the same when it's zeroextended to 64 bit.  */
3984
3985
0
  direct:
3986
0
    if ((input_section->flags & SEC_ALLOC) == 0)
3987
0
      break;
3988
3989
0
    need_copy_reloc_in_pie = (bfd_link_pie (info)
3990
0
            && h != NULL
3991
0
            && (h->needs_copy
3992
0
          || eh->needs_copy
3993
0
          || (h->root.type
3994
0
              == bfd_link_hash_undefined))
3995
0
            && (X86_PCREL_TYPE_P (true, r_type)
3996
0
          || X86_SIZE_TYPE_P (true,
3997
0
                  r_type)));
3998
3999
0
    if (GENERATE_DYNAMIC_RELOCATION_P (true, info, eh, r_type, sec,
4000
0
               need_copy_reloc_in_pie,
4001
0
               resolved_to_zero, false))
4002
0
      {
4003
0
        Elf_Internal_Rela outrel;
4004
0
        bool skip, relocate;
4005
0
        bool generate_dynamic_reloc = true;
4006
0
        asection *sreloc;
4007
0
        const char *relative_reloc_name = NULL;
4008
4009
        /* When generating a shared object, these relocations
4010
     are copied into the output file to be resolved at run
4011
     time.  */
4012
0
        skip = false;
4013
0
        relocate = false;
4014
4015
0
        outrel.r_offset =
4016
0
    _bfd_elf_section_offset (info->output_bfd, info,
4017
0
           input_section, rel->r_offset);
4018
0
        if (outrel.r_offset == (bfd_vma) -1)
4019
0
    skip = true;
4020
0
        else if (outrel.r_offset == (bfd_vma) -2)
4021
0
    skip = true, relocate = true;
4022
4023
0
        outrel.r_offset += (input_section->output_section->vma
4024
0
          + input_section->output_offset);
4025
4026
0
        if (skip)
4027
0
    memset (&outrel, 0, sizeof outrel);
4028
4029
0
        else if (COPY_INPUT_RELOC_P (true, info, h, r_type))
4030
0
    {
4031
0
      outrel.r_info = htab->r_info (h->dynindx, r_type);
4032
0
      outrel.r_addend = rel->r_addend;
4033
0
    }
4034
0
        else
4035
0
    {
4036
      /* This symbol is local, or marked to become local.
4037
         When relocation overflow check is disabled, we
4038
         convert R_X86_64_32 to dynamic R_X86_64_RELATIVE.  */
4039
0
      if (r_type == htab->pointer_r_type
4040
0
          || (r_type == R_X86_64_32
4041
0
        && htab->params->no_reloc_overflow_check))
4042
0
        {
4043
0
          relocate = true;
4044
          /* NB: Don't generate relative relocation here if
4045
       it has been generated by DT_RELR.  */
4046
0
          if (info->enable_dt_relr)
4047
0
      generate_dynamic_reloc = false;
4048
0
          else
4049
0
      {
4050
0
        outrel.r_info =
4051
0
          htab->r_info (0, R_X86_64_RELATIVE);
4052
0
        outrel.r_addend = relocation + rel->r_addend;
4053
0
        relative_reloc_name = "R_X86_64_RELATIVE";
4054
0
      }
4055
0
        }
4056
0
      else if (r_type == R_X86_64_64
4057
0
         && !ABI_64_P (info->output_bfd))
4058
0
        {
4059
0
          relocate = true;
4060
0
          outrel.r_info = htab->r_info (0,
4061
0
                R_X86_64_RELATIVE64);
4062
0
          outrel.r_addend = relocation + rel->r_addend;
4063
0
          relative_reloc_name = "R_X86_64_RELATIVE64";
4064
          /* Check addend overflow.  */
4065
0
          if ((outrel.r_addend & 0x80000000)
4066
0
        != (rel->r_addend & 0x80000000))
4067
0
      {
4068
0
        const char *name;
4069
0
        int addend = rel->r_addend;
4070
0
        if (h && h->root.root.string)
4071
0
          name = h->root.root.string;
4072
0
        else
4073
0
          name = bfd_elf_sym_name (input_bfd, symtab_hdr,
4074
0
                 sym, NULL);
4075
0
        _bfd_error_handler
4076
          /* xgettext:c-format */
4077
0
          (_("%pB: addend %s%#x in relocation %s against "
4078
0
             "symbol `%s' at %#" PRIx64
4079
0
             " in section `%pA' is out of range"),
4080
0
           input_bfd, addend < 0 ? "-" : "", addend,
4081
0
           howto->name, name, (uint64_t) rel->r_offset,
4082
0
           input_section);
4083
0
        bfd_set_error (bfd_error_bad_value);
4084
0
        return false;
4085
0
      }
4086
0
        }
4087
0
      else
4088
0
        {
4089
0
          long sindx;
4090
4091
0
          if (bfd_is_abs_section (sec))
4092
0
      sindx = 0;
4093
0
          else if (sec == NULL || sec->owner == NULL)
4094
0
      {
4095
0
        bfd_set_error (bfd_error_bad_value);
4096
0
        return false;
4097
0
      }
4098
0
          else
4099
0
      {
4100
0
        asection *osec;
4101
4102
        /* We are turning this relocation into one
4103
           against a section symbol.  It would be
4104
           proper to subtract the symbol's value,
4105
           osec->vma, from the emitted reloc addend,
4106
           but ld.so expects buggy relocs.  */
4107
0
        osec = sec->output_section;
4108
0
        sindx = elf_section_data (osec)->dynindx;
4109
0
        if (sindx == 0)
4110
0
          {
4111
0
            asection *oi = htab->elf.text_index_section;
4112
0
            sindx = elf_section_data (oi)->dynindx;
4113
0
          }
4114
0
        BFD_ASSERT (sindx != 0);
4115
0
      }
4116
4117
0
          outrel.r_info = htab->r_info (sindx, r_type);
4118
0
          outrel.r_addend = relocation + rel->r_addend;
4119
0
        }
4120
0
    }
4121
4122
0
        if (generate_dynamic_reloc)
4123
0
    {
4124
0
      sreloc = elf_section_data (input_section)->sreloc;
4125
4126
0
      if (sreloc == NULL || sreloc->contents == NULL)
4127
0
        {
4128
0
          r = bfd_reloc_notsupported;
4129
0
          goto check_relocation_error;
4130
0
        }
4131
4132
0
      if (relative_reloc_name
4133
0
          && htab->params->report_relative_reloc)
4134
0
        _bfd_x86_elf_link_report_relative_reloc
4135
0
          (info, input_section, h, sym,
4136
0
           relative_reloc_name, &outrel);
4137
4138
0
      _bfd_elf_append_rela (info->output_bfd, sreloc, &outrel);
4139
0
    }
4140
4141
        /* If this reloc is against an external symbol, we do
4142
     not want to fiddle with the addend.  Otherwise, we
4143
     need to include the symbol value so that it becomes
4144
     an addend for the dynamic reloc.  */
4145
0
        if (! relocate)
4146
0
    continue;
4147
0
      }
4148
4149
0
    break;
4150
4151
0
  case R_X86_64_TLSGD:
4152
0
  case R_X86_64_GOTPC32_TLSDESC:
4153
0
  case R_X86_64_CODE_4_GOTPC32_TLSDESC:
4154
0
  case R_X86_64_TLSDESC_CALL:
4155
0
  case R_X86_64_GOTTPOFF:
4156
0
  case R_X86_64_CODE_4_GOTTPOFF:
4157
0
  case R_X86_64_CODE_5_GOTTPOFF:
4158
0
  case R_X86_64_CODE_6_GOTTPOFF:
4159
0
    tls_type = GOT_UNKNOWN;
4160
0
    if (h == NULL && local_got_offsets)
4161
0
      tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
4162
0
    else if (h != NULL)
4163
0
      tls_type = elf_x86_hash_entry (h)->tls_type;
4164
4165
0
    r_type_tls = r_type;
4166
0
    if (! elf_x86_64_tls_transition (info, input_bfd,
4167
0
             input_section, contents,
4168
0
             symtab_hdr, sym_hashes,
4169
0
             &r_type_tls, tls_type, rel,
4170
0
             relend, h, sym, true))
4171
0
      return false;
4172
4173
0
    if (r_type_tls == R_X86_64_TPOFF32)
4174
0
      {
4175
0
        bfd_vma roff = rel->r_offset;
4176
4177
0
        if (roff >= input_section->size)
4178
0
    goto corrupt_input;
4179
4180
0
        BFD_ASSERT (! unresolved_reloc);
4181
4182
0
        if (r_type == R_X86_64_TLSGD)
4183
0
    {
4184
      /* GD->LE transition.  For 64bit, change
4185
      .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4186
      .word 0x6666; rex64; call __tls_get_addr@PLT
4187
         or
4188
      .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4189
      .byte 0x66; rex64
4190
      call *__tls_get_addr@GOTPCREL(%rip)
4191
      which may be converted to
4192
      addr32 call __tls_get_addr
4193
         into:
4194
      movq %fs:0, %rax
4195
      leaq foo@tpoff(%rax), %rax
4196
         For 32bit, change
4197
      leaq foo@tlsgd(%rip), %rdi
4198
      .word 0x6666; rex64; call __tls_get_addr@PLT
4199
         or
4200
      leaq foo@tlsgd(%rip), %rdi
4201
      .byte 0x66; rex64
4202
      call *__tls_get_addr@GOTPCREL(%rip)
4203
      which may be converted to
4204
      addr32 call __tls_get_addr
4205
         into:
4206
      movl %fs:0, %eax
4207
      leaq foo@tpoff(%rax), %rax
4208
         For largepic, change:
4209
      leaq foo@tlsgd(%rip), %rdi
4210
      movabsq $__tls_get_addr@pltoff, %rax
4211
      addq %r15, %rax
4212
      call *%rax
4213
         into:
4214
      movq %fs:0, %rax
4215
      leaq foo@tpoff(%rax), %rax
4216
      nopw 0x0(%rax,%rax,1)  */
4217
0
      int largepic = 0;
4218
0
      if (ABI_64_P (info->output_bfd))
4219
0
        {
4220
0
          if (roff + 5 >= input_section->size)
4221
0
      goto corrupt_input;
4222
0
          if (contents[roff + 5] == 0xb8)
4223
0
      {
4224
0
        if (roff < 3
4225
0
            || (roff - 3 + 22) > input_section->size)
4226
0
          {
4227
0
          corrupt_input:
4228
0
            info->callbacks->fatal
4229
0
        (_("%P: corrupt input: %pB\n"),
4230
0
         input_bfd);
4231
0
            return false;
4232
0
          }
4233
0
        memcpy (contents + roff - 3,
4234
0
          "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
4235
0
          "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
4236
0
        largepic = 1;
4237
0
      }
4238
0
          else
4239
0
      {
4240
0
        if (roff < 4
4241
0
            || (roff - 4 + 16) > input_section->size)
4242
0
          goto corrupt_input;
4243
0
        memcpy (contents + roff - 4,
4244
0
          "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
4245
0
          16);
4246
0
      }
4247
0
        }
4248
0
      else
4249
0
        {
4250
0
          if (roff < 3
4251
0
        || (roff - 3 + 15) > input_section->size)
4252
0
      goto corrupt_input;
4253
0
          memcpy (contents + roff - 3,
4254
0
            "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
4255
0
            15);
4256
0
        }
4257
4258
0
      if (roff + 8 + largepic >= input_section->size)
4259
0
        goto corrupt_input;
4260
4261
0
      bfd_put_32 (info->output_bfd,
4262
0
            elf_x86_64_tpoff (info, relocation),
4263
0
            contents + roff + 8 + largepic);
4264
      /* Skip R_X86_64_PC32, R_X86_64_PLT32,
4265
         R_X86_64_GOTPCRELX and R_X86_64_PLTOFF64.  */
4266
0
      rel++;
4267
0
      wrel++;
4268
0
      continue;
4269
0
    }
4270
0
        else if (r_type == R_X86_64_GOTPC32_TLSDESC)
4271
0
    {
4272
      /* GDesc -> LE transition.
4273
         It's originally something like:
4274
         leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
4275
         rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
4276
4277
         Change it to:
4278
         movq $x@tpoff, %rax <--- LP64 mode.
4279
         rex movl $x@tpoff, %eax <--- X32 mode.
4280
       */
4281
4282
0
      unsigned int val, type;
4283
4284
0
      if (roff < 3)
4285
0
        goto corrupt_input;
4286
0
      type = bfd_get_8 (input_bfd, contents + roff - 3);
4287
0
      val = bfd_get_8 (input_bfd, contents + roff - 1);
4288
0
      bfd_put_8 (info->output_bfd,
4289
0
           (type & 0x48) | ((type >> 2) & 1),
4290
0
           contents + roff - 3);
4291
0
      bfd_put_8 (info->output_bfd, 0xc7, contents + roff - 2);
4292
0
      bfd_put_8 (info->output_bfd, 0xc0 | ((val >> 3) & 7),
4293
0
           contents + roff - 1);
4294
0
      bfd_put_32 (info->output_bfd,
4295
0
            elf_x86_64_tpoff (info, relocation),
4296
0
            contents + roff);
4297
0
      continue;
4298
0
    }
4299
0
        else if (r_type == R_X86_64_CODE_4_GOTPC32_TLSDESC)
4300
0
    {
4301
      /* GDesc -> LE transition.
4302
         It's originally something like:
4303
         lea x@tlsdesc(%rip), %reg
4304
4305
         Change it to:
4306
         mov $x@tpoff, %reg
4307
         where reg is one of r16 to r31.  */
4308
4309
0
      unsigned int val, rex2;
4310
0
      unsigned int rex2_mask = REX_R | REX_R << 4;
4311
4312
0
      if (roff < 4)
4313
0
        goto corrupt_input;
4314
0
      rex2 = bfd_get_8 (input_bfd, contents + roff - 3);
4315
0
      val = bfd_get_8 (input_bfd, contents + roff - 1);
4316
      /* Move the R bits to the B bits in REX2 payload
4317
         byte.  */
4318
0
      bfd_put_8 (info->output_bfd,
4319
0
           ((rex2 & ~rex2_mask)
4320
0
            | (rex2 & rex2_mask) >> 2),
4321
0
           contents + roff - 3);
4322
0
      bfd_put_8 (info->output_bfd, 0xc7, contents + roff - 2);
4323
0
      bfd_put_8 (info->output_bfd, 0xc0 | ((val >> 3) & 7),
4324
0
           contents + roff - 1);
4325
0
      bfd_put_32 (info->output_bfd,
4326
0
            elf_x86_64_tpoff (info, relocation),
4327
0
            contents + roff);
4328
0
      continue;
4329
0
    }
4330
0
        else if (r_type == R_X86_64_TLSDESC_CALL)
4331
0
    {
4332
      /* GDesc -> LE transition.
4333
         It's originally:
4334
         call *(%rax) <--- LP64 mode.
4335
         call *(%eax) <--- X32 mode.
4336
         Turn it into:
4337
         xchg %ax,%ax <-- LP64 mode.
4338
         nopl (%rax)  <-- X32 mode.
4339
       */
4340
0
      unsigned int prefix = 0;
4341
0
      if (!ABI_64_P (input_bfd))
4342
0
        {
4343
          /* Check for call *x@tlscall(%eax).  */
4344
0
          if (contents[roff] == 0x67)
4345
0
      prefix = 1;
4346
0
        }
4347
0
      if (prefix)
4348
0
        {
4349
0
          if (roff + 2 >= input_section->size)
4350
0
      goto corrupt_input;
4351
4352
0
          bfd_put_8 (info->output_bfd, 0x0f, contents + roff);
4353
0
          bfd_put_8 (info->output_bfd, 0x1f, contents + roff + 1);
4354
0
          bfd_put_8 (info->output_bfd, 0x00, contents + roff + 2);
4355
0
        }
4356
0
      else
4357
0
        {
4358
0
          if (roff + 1 >= input_section->size)
4359
0
      goto corrupt_input;
4360
4361
0
          bfd_put_8 (info->output_bfd, 0x66, contents + roff);
4362
0
          bfd_put_8 (info->output_bfd, 0x90, contents + roff + 1);
4363
0
        }
4364
0
      continue;
4365
0
    }
4366
0
        else if (r_type == R_X86_64_GOTTPOFF)
4367
0
    {
4368
      /* IE->LE transition:
4369
         For 64bit, originally it can be one of:
4370
         movq foo@gottpoff(%rip), %reg
4371
         addq foo@gottpoff(%rip), %reg
4372
         We change it into:
4373
         movq $foo, %reg
4374
         leaq foo(%reg), %reg
4375
         addq $foo, %reg.
4376
         For 32bit, originally it can be one of:
4377
         movq foo@gottpoff(%rip), %reg
4378
         addl foo@gottpoff(%rip), %reg
4379
         We change it into:
4380
         movq $foo, %reg
4381
         leal foo(%reg), %reg
4382
         addl $foo, %reg. */
4383
4384
0
      unsigned int val, type, reg;
4385
4386
0
      if (roff >= 3)
4387
0
        val = bfd_get_8 (input_bfd, contents + roff - 3);
4388
0
      else
4389
0
        {
4390
0
          if (roff < 2)
4391
0
      goto corrupt_input;
4392
0
          val = 0;
4393
0
        }
4394
0
      type = bfd_get_8 (input_bfd, contents + roff - 2);
4395
0
      reg = bfd_get_8 (input_bfd, contents + roff - 1);
4396
0
      reg >>= 3;
4397
0
      if (type == 0x8b)
4398
0
        {
4399
          /* movq */
4400
0
          if (val == 0x4c)
4401
0
      {
4402
0
        if (roff < 3)
4403
0
          goto corrupt_input;
4404
0
        bfd_put_8 (info->output_bfd, 0x49,
4405
0
             contents + roff - 3);
4406
0
      }
4407
0
          else if (!ABI_64_P (info->output_bfd) && val == 0x44)
4408
0
      {
4409
0
        if (roff < 3)
4410
0
          goto corrupt_input;
4411
0
        bfd_put_8 (info->output_bfd, 0x41,
4412
0
             contents + roff - 3);
4413
0
      }
4414
0
          bfd_put_8 (info->output_bfd, 0xc7,
4415
0
         contents + roff - 2);
4416
0
          bfd_put_8 (info->output_bfd, 0xc0 | reg,
4417
0
         contents + roff - 1);
4418
0
        }
4419
0
      else if (reg == 4)
4420
0
        {
4421
          /* addq/addl -> addq/addl - addressing with %rsp/%r12
4422
       is special  */
4423
0
          if (val == 0x4c)
4424
0
      {
4425
0
        if (roff < 3)
4426
0
          goto corrupt_input;
4427
0
        bfd_put_8 (info->output_bfd, 0x49,
4428
0
             contents + roff - 3);
4429
0
      }
4430
0
          else if (!ABI_64_P (info->output_bfd) && val == 0x44)
4431
0
      {
4432
0
        if (roff < 3)
4433
0
          goto corrupt_input;
4434
0
        bfd_put_8 (info->output_bfd, 0x41,
4435
0
             contents + roff - 3);
4436
0
      }
4437
0
          bfd_put_8 (info->output_bfd, 0x81,
4438
0
         contents + roff - 2);
4439
0
          bfd_put_8 (info->output_bfd, 0xc0 | reg,
4440
0
         contents + roff - 1);
4441
0
        }
4442
0
      else
4443
0
        {
4444
          /* addq/addl -> leaq/leal */
4445
0
          if (val == 0x4c)
4446
0
      {
4447
0
        if (roff < 3)
4448
0
          goto corrupt_input;
4449
0
        bfd_put_8 (info->output_bfd, 0x4d,
4450
0
             contents + roff - 3);
4451
0
      }
4452
0
          else if (!ABI_64_P (info->output_bfd) && val == 0x44)
4453
0
      {
4454
0
        if (roff < 3)
4455
0
          goto corrupt_input;
4456
0
        bfd_put_8 (info->output_bfd, 0x45,
4457
0
             contents + roff - 3);
4458
0
      }
4459
0
          bfd_put_8 (info->output_bfd, 0x8d,
4460
0
         contents + roff - 2);
4461
0
          bfd_put_8 (info->output_bfd, 0x80 | reg | (reg << 3),
4462
0
         contents + roff - 1);
4463
0
        }
4464
0
      bfd_put_32 (info->output_bfd,
4465
0
            elf_x86_64_tpoff (info, relocation),
4466
0
            contents + roff);
4467
0
      continue;
4468
0
    }
4469
0
        else if (r_type == R_X86_64_CODE_4_GOTTPOFF)
4470
0
    {
4471
      /* IE->LE transition:
4472
         Originally it can be one of:
4473
         mov foo@gottpoff(%rip), %reg
4474
         add foo@gottpoff(%rip), %reg
4475
         movrs foo@gottpoff(%rip), %reg
4476
         We change it into:
4477
         mov $foo@tpoff, %reg
4478
         add $foo@tpoff, %reg
4479
         where reg is one of r16 to r31, except for MOVRS, where
4480
         it's not one of r8 to r31 and no REX byte is present.  */
4481
4482
0
      unsigned int rex2, type, reg;
4483
0
      unsigned int rex2_mask = REX_R | REX_R << 4;
4484
4485
0
      if (roff < 4)
4486
0
        goto corrupt_input;
4487
4488
0
      rex2 = bfd_get_8 (input_bfd, contents + roff - 3);
4489
0
      type = bfd_get_8 (input_bfd, contents + roff - 2);
4490
0
      reg = bfd_get_8 (input_bfd, contents + roff - 1);
4491
0
      reg >>= 3;
4492
      /* Move the R bits to the B bits in REX2 payload
4493
         byte.  */
4494
0
      if (type == 0x8b)
4495
0
        {
4496
          /* For MOVRS emit meaningless CS prefixes.  */
4497
0
          if (bfd_get_8 (input_bfd, contents + roff - 4) == 0x0f)
4498
0
      {
4499
0
        bfd_put_8 (info->output_bfd, 0x2e,
4500
0
             contents + roff - 4);
4501
0
        rex2 = 0x2e;
4502
0
        rex2_mask = 0;
4503
0
      }
4504
0
          type = 0xc7;
4505
0
        }
4506
0
      else
4507
0
        type = 0x81;
4508
0
      bfd_put_8 (info->output_bfd,
4509
0
           ((rex2 & ~rex2_mask)
4510
0
            | (rex2 & rex2_mask) >> 2),
4511
0
           contents + roff - 3);
4512
0
      bfd_put_8 (info->output_bfd, type,
4513
0
           contents + roff - 2);
4514
0
      bfd_put_8 (info->output_bfd, 0xc0 | reg,
4515
0
           contents + roff - 1);
4516
0
      bfd_put_32 (info->output_bfd,
4517
0
            elf_x86_64_tpoff (info, relocation),
4518
0
            contents + roff);
4519
0
      continue;
4520
0
    }
4521
0
        else if (r_type == R_X86_64_CODE_5_GOTTPOFF)
4522
0
    {
4523
      /* IE->LE transition:
4524
         Originally it is
4525
         movrs foo@gottpoff(%rip), %reg
4526
         We change it into:
4527
         mov $foo@tpoff, %reg
4528
         where reg isn't one of r16 to r31, but a REX
4529
         byte is present.  */
4530
0
      unsigned int rex = bfd_get_8 (input_bfd, contents + roff - 5);
4531
4532
      /* Move REX.R to REX.B.  */
4533
0
      rex = (rex & ~(REX_R | REX_B))
4534
0
      | ((rex & REX_R) / (REX_R / REX_B));
4535
4536
0
      unsigned int reg = bfd_get_8 (input_bfd, contents + roff - 1);
4537
0
      reg >>= 3;
4538
4539
      /* Replace 0f38 by meaningless CS prefixes, shifting the REX
4540
         prefix forward.  */
4541
0
      bfd_put_8 (info->output_bfd, 0x2e, contents + roff - 5);
4542
0
      bfd_put_8 (info->output_bfd, 0x2e, contents + roff - 4);
4543
0
      bfd_put_8 (info->output_bfd, rex, contents + roff - 3);
4544
0
      bfd_put_8 (info->output_bfd, 0xc7, contents + roff - 2);
4545
0
      bfd_put_8 (info->output_bfd, 0xc0 | reg, contents + roff - 1);
4546
4547
0
      bfd_put_32 (info->output_bfd,
4548
0
            elf_x86_64_tpoff (info, relocation),
4549
0
            contents + roff);
4550
0
      continue;
4551
0
    }
4552
0
        else if (r_type == R_X86_64_CODE_6_GOTTPOFF)
4553
0
    {
4554
      /* IE->LE transition:
4555
         Originally it is
4556
         add %reg1, foo@gottpoff(%rip), %reg2
4557
         or
4558
         add foo@gottpoff(%rip), %reg1, %reg2
4559
         or
4560
         movrs foo@gottpoff(%rip), %reg
4561
         We change it into:
4562
         add $foo@tpoff, %reg1, %reg2
4563
         mov $foo@tpoff, %reg
4564
         where reg is one of r16 to r31.  */
4565
0
      unsigned int type, reg, byte1;
4566
4567
0
      if (roff < 6)
4568
0
        goto corrupt_input;
4569
4570
0
      byte1 = bfd_get_8 (input_bfd, contents + roff - 5);
4571
0
      type = bfd_get_8 (input_bfd, contents + roff - 2);
4572
0
      reg = bfd_get_8 (input_bfd, contents + roff - 1);
4573
0
      reg >>= 3;
4574
4575
0
      if (type == 0x8b)
4576
0
        {
4577
          /* Convert MOVRS to REX2-encoded MOV.  */
4578
0
          unsigned int rex2 = 0;
4579
4580
          /* Move the EVEX R bits to the REX2 B ones.  */
4581
0
          if (!(byte1 & (1 << 7)))
4582
0
      rex2 |= REX_B;
4583
0
          if (!(byte1 & (1 << 4)))
4584
0
      rex2 |= REX_B << 4;
4585
          /* Propagate the EVEX W bit to the REX2 one.  */
4586
0
          type = bfd_get_8 (input_bfd, contents + roff - 4);
4587
0
          if (type & (1 << 7))
4588
0
      rex2 |= REX_W;
4589
4590
4591
0
          bfd_put_8 (info->output_bfd, 0x2e, contents + roff - 6);
4592
0
          bfd_put_8 (info->output_bfd, 0x2e, contents + roff - 5);
4593
0
          bfd_put_8 (info->output_bfd, 0xd5, contents + roff - 4);
4594
0
          bfd_put_8 (info->output_bfd, rex2, contents + roff - 3);
4595
0
          bfd_put_8 (info->output_bfd, 0xc7, contents + roff - 2);
4596
0
          bfd_put_8 (info->output_bfd, 0xc0 | reg,
4597
0
         contents + roff - 1);
4598
0
          bfd_put_32 (info->output_bfd,
4599
0
          elf_x86_64_tpoff (info, relocation),
4600
0
          contents + roff);
4601
0
          continue;
4602
0
        }
4603
4604
0
      byte1 = evex_move_r_to_b (byte1, false);
4605
0
      bfd_put_8 (info->output_bfd, byte1, contents + roff - 5);
4606
0
      bfd_put_8 (info->output_bfd, 0x81, contents + roff - 2);
4607
0
      bfd_put_8 (info->output_bfd, 0xc0 | reg, contents + roff - 1);
4608
0
      bfd_put_32 (info->output_bfd,
4609
0
            elf_x86_64_tpoff (info, relocation),
4610
0
            contents + roff);
4611
0
      continue;
4612
0
    }
4613
0
        else
4614
0
    BFD_ASSERT (false);
4615
0
      }
4616
4617
0
    if (htab->elf.sgot == NULL)
4618
0
      abort ();
4619
4620
0
    if (h != NULL)
4621
0
      {
4622
0
        off = h->got.offset;
4623
0
        offplt = elf_x86_hash_entry (h)->tlsdesc_got;
4624
0
      }
4625
0
    else
4626
0
      {
4627
0
        if (local_got_offsets == NULL)
4628
0
    abort ();
4629
4630
0
        off = local_got_offsets[r_symndx];
4631
0
        offplt = local_tlsdesc_gotents[r_symndx];
4632
0
      }
4633
4634
0
    if ((off & 1) != 0)
4635
0
      off &= ~1;
4636
0
    else
4637
0
      {
4638
0
        Elf_Internal_Rela outrel;
4639
0
        int dr_type, indx;
4640
0
        asection *sreloc;
4641
4642
0
        if (htab->elf.srelgot == NULL)
4643
0
    abort ();
4644
4645
0
        indx = h && h->dynindx != -1 ? h->dynindx : 0;
4646
4647
0
        if (GOT_TLS_GDESC_P (tls_type))
4648
0
    {
4649
0
      outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
4650
0
      BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
4651
0
            + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
4652
0
      outrel.r_offset = (htab->elf.sgotplt->output_section->vma
4653
0
             + htab->elf.sgotplt->output_offset
4654
0
             + offplt
4655
0
             + htab->sgotplt_jump_table_size);
4656
0
      sreloc = htab->rel_tls_desc;
4657
0
      if (indx == 0)
4658
0
        outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
4659
0
      else
4660
0
        outrel.r_addend = 0;
4661
0
      _bfd_elf_append_rela (info->output_bfd, sreloc, &outrel);
4662
0
    }
4663
4664
0
        sreloc = htab->elf.srelgot;
4665
4666
0
        outrel.r_offset = (htab->elf.sgot->output_section->vma
4667
0
         + htab->elf.sgot->output_offset + off);
4668
4669
0
        if (GOT_TLS_GD_P (tls_type))
4670
0
    dr_type = R_X86_64_DTPMOD64;
4671
0
        else if (GOT_TLS_GDESC_P (tls_type))
4672
0
    goto dr_done;
4673
0
        else
4674
0
    dr_type = R_X86_64_TPOFF64;
4675
4676
0
        bfd_put_64 (info->output_bfd, 0, htab->elf.sgot->contents + off);
4677
0
        outrel.r_addend = 0;
4678
0
        if ((dr_type == R_X86_64_TPOFF64
4679
0
       || dr_type == R_X86_64_TLSDESC) && indx == 0)
4680
0
    outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
4681
0
        outrel.r_info = htab->r_info (indx, dr_type);
4682
4683
0
        _bfd_elf_append_rela (info->output_bfd, sreloc, &outrel);
4684
4685
0
        if (GOT_TLS_GD_P (tls_type))
4686
0
    {
4687
0
      if (indx == 0)
4688
0
        {
4689
0
          BFD_ASSERT (! unresolved_reloc);
4690
0
          bfd_put_64 (info->output_bfd,
4691
0
          relocation - _bfd_x86_elf_dtpoff_base (info),
4692
0
          htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4693
0
        }
4694
0
      else
4695
0
        {
4696
0
          bfd_put_64 (info->output_bfd, 0,
4697
0
          htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4698
0
          outrel.r_info = htab->r_info (indx,
4699
0
                R_X86_64_DTPOFF64);
4700
0
          outrel.r_offset += GOT_ENTRY_SIZE;
4701
0
          _bfd_elf_append_rela (info->output_bfd, sreloc, &outrel);
4702
0
        }
4703
0
    }
4704
4705
0
      dr_done:
4706
0
        if (h != NULL)
4707
0
    h->got.offset |= 1;
4708
0
        else
4709
0
    local_got_offsets[r_symndx] |= 1;
4710
0
      }
4711
4712
0
    if (off >= (bfd_vma) -2
4713
0
        && ! GOT_TLS_GDESC_P (tls_type))
4714
0
      abort ();
4715
0
    if (r_type_tls == r_type)
4716
0
      {
4717
0
        if (r_type == R_X86_64_GOTPC32_TLSDESC
4718
0
      || r_type == R_X86_64_CODE_4_GOTPC32_TLSDESC
4719
0
      || r_type == R_X86_64_TLSDESC_CALL)
4720
0
    relocation = htab->elf.sgotplt->output_section->vma
4721
0
      + htab->elf.sgotplt->output_offset
4722
0
      + offplt + htab->sgotplt_jump_table_size;
4723
0
        else
4724
0
    relocation = htab->elf.sgot->output_section->vma
4725
0
      + htab->elf.sgot->output_offset + off;
4726
0
        unresolved_reloc = false;
4727
0
      }
4728
0
    else
4729
0
      {
4730
0
        bfd_vma roff = rel->r_offset;
4731
4732
0
        if (r_type == R_X86_64_TLSGD)
4733
0
    {
4734
      /* GD->IE transition.  For 64bit, change
4735
      .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4736
      .word 0x6666; rex64; call __tls_get_addr@PLT
4737
         or
4738
      .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4739
      .byte 0x66; rex64
4740
      call *__tls_get_addr@GOTPCREL(%rip
4741
      which may be converted to
4742
      addr32 call __tls_get_addr
4743
         into:
4744
      movq %fs:0, %rax
4745
      addq foo@gottpoff(%rip), %rax
4746
         For 32bit, change
4747
      leaq foo@tlsgd(%rip), %rdi
4748
      .word 0x6666; rex64; call __tls_get_addr@PLT
4749
         or
4750
      leaq foo@tlsgd(%rip), %rdi
4751
      .byte 0x66; rex64;
4752
      call *__tls_get_addr@GOTPCREL(%rip)
4753
      which may be converted to
4754
      addr32 call __tls_get_addr
4755
         into:
4756
      movl %fs:0, %eax
4757
      addq foo@gottpoff(%rip), %rax
4758
         For largepic, change:
4759
      leaq foo@tlsgd(%rip), %rdi
4760
      movabsq $__tls_get_addr@pltoff, %rax
4761
      addq %r15, %rax
4762
      call *%rax
4763
         into:
4764
      movq %fs:0, %rax
4765
      addq foo@gottpoff(%rax), %rax
4766
      nopw 0x0(%rax,%rax,1)  */
4767
0
      int largepic = 0;
4768
0
      if (ABI_64_P (info->output_bfd))
4769
0
        {
4770
0
          if (contents[roff + 5] == 0xb8)
4771
0
      {
4772
0
        if (roff < 3
4773
0
            || (roff - 3 + 22) > input_section->size)
4774
0
          goto corrupt_input;
4775
0
        memcpy (contents + roff - 3,
4776
0
          "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
4777
0
          "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
4778
0
        largepic = 1;
4779
0
      }
4780
0
          else
4781
0
      {
4782
0
        if (roff < 4
4783
0
            || (roff - 4 + 16) > input_section->size)
4784
0
          goto corrupt_input;
4785
0
        memcpy (contents + roff - 4,
4786
0
          "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
4787
0
          16);
4788
0
      }
4789
0
        }
4790
0
      else
4791
0
        {
4792
0
          if (roff < 3
4793
0
        || (roff - 3 + 15) > input_section->size)
4794
0
      goto corrupt_input;
4795
0
          memcpy (contents + roff - 3,
4796
0
            "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
4797
0
            15);
4798
0
        }
4799
4800
0
      relocation = (htab->elf.sgot->output_section->vma
4801
0
        + htab->elf.sgot->output_offset + off
4802
0
        - roff
4803
0
        - largepic
4804
0
        - input_section->output_section->vma
4805
0
        - input_section->output_offset
4806
0
        - 12);
4807
0
      bfd_put_32 (info->output_bfd, relocation,
4808
0
            contents + roff + 8 + largepic);
4809
      /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64.  */
4810
0
      rel++;
4811
0
      wrel++;
4812
0
      continue;
4813
0
    }
4814
0
        else if (r_type == R_X86_64_GOTPC32_TLSDESC
4815
0
           || r_type == R_X86_64_CODE_4_GOTPC32_TLSDESC)
4816
0
    {
4817
      /* GDesc -> IE transition.
4818
         It's originally something like:
4819
         leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
4820
         rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
4821
4822
         Change it to:
4823
         # before xchg %ax,%ax in LP64 mode.
4824
         movq x@gottpoff(%rip), %rax
4825
         # before nopl (%rax) in X32 mode.
4826
         rex movl x@gottpoff(%rip), %eax
4827
      */
4828
4829
      /* Now modify the instruction as appropriate. To
4830
         turn a lea into a mov in the form we use it, it
4831
         suffices to change the second byte from 0x8d to
4832
         0x8b.  */
4833
0
      if (roff < 2)
4834
0
        goto corrupt_input;
4835
0
      bfd_put_8 (info->output_bfd, 0x8b, contents + roff - 2);
4836
4837
0
      bfd_put_32 (info->output_bfd,
4838
0
            htab->elf.sgot->output_section->vma
4839
0
            + htab->elf.sgot->output_offset + off
4840
0
            - rel->r_offset
4841
0
            - input_section->output_section->vma
4842
0
            - input_section->output_offset
4843
0
            - 4,
4844
0
            contents + roff);
4845
0
      continue;
4846
0
    }
4847
0
        else if (r_type == R_X86_64_TLSDESC_CALL)
4848
0
    {
4849
      /* GDesc -> IE transition.
4850
         It's originally:
4851
         call *(%rax) <--- LP64 mode.
4852
         call *(%eax) <--- X32 mode.
4853
4854
         Change it to:
4855
         xchg %ax, %ax <-- LP64 mode.
4856
         nopl (%rax)  <-- X32 mode.
4857
       */
4858
4859
0
      unsigned int prefix = 0;
4860
0
      if (!ABI_64_P (input_bfd))
4861
0
        {
4862
          /* Check for call *x@tlscall(%eax).  */
4863
0
          if (contents[roff] == 0x67)
4864
0
      prefix = 1;
4865
0
        }
4866
0
      if (prefix)
4867
0
        {
4868
0
          bfd_put_8 (info->output_bfd, 0x0f, contents + roff);
4869
0
          bfd_put_8 (info->output_bfd, 0x1f, contents + roff + 1);
4870
0
          bfd_put_8 (info->output_bfd, 0x00, contents + roff + 2);
4871
0
        }
4872
0
      else
4873
0
        {
4874
0
          bfd_put_8 (info->output_bfd, 0x66, contents + roff);
4875
0
          bfd_put_8 (info->output_bfd, 0x90, contents + roff + 1);
4876
0
        }
4877
0
      continue;
4878
0
    }
4879
0
        else
4880
0
    BFD_ASSERT (false);
4881
0
      }
4882
0
    break;
4883
4884
0
  case R_X86_64_TLSLD:
4885
0
    if (! elf_x86_64_tls_transition (info, input_bfd,
4886
0
             input_section, contents,
4887
0
             symtab_hdr, sym_hashes,
4888
0
             &r_type, GOT_UNKNOWN, rel,
4889
0
             relend, h, sym, true))
4890
0
      return false;
4891
4892
0
    if (r_type != R_X86_64_TLSLD)
4893
0
      {
4894
        /* LD->LE transition:
4895
      leaq foo@tlsld(%rip), %rdi
4896
      call __tls_get_addr@PLT
4897
     For 64bit, we change it into:
4898
      .word 0x6666; .byte 0x66; movq %fs:0, %rax
4899
     For 32bit, we change it into:
4900
      nopl 0x0(%rax); movl %fs:0, %eax
4901
     Or
4902
      leaq foo@tlsld(%rip), %rdi;
4903
      call *__tls_get_addr@GOTPCREL(%rip)
4904
      which may be converted to
4905
      addr32 call __tls_get_addr
4906
     For 64bit, we change it into:
4907
      .word 0x6666; .word 0x6666; movq %fs:0, %rax
4908
     For 32bit, we change it into:
4909
      nopw 0x0(%rax); movl %fs:0, %eax
4910
     For largepic, change:
4911
      leaq foo@tlsgd(%rip), %rdi
4912
      movabsq $__tls_get_addr@pltoff, %rax
4913
      addq %rbx, %rax
4914
      call *%rax
4915
     into
4916
      data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
4917
      movq %fs:0, %eax  */
4918
4919
0
        BFD_ASSERT (r_type == R_X86_64_TPOFF32);
4920
0
        if (ABI_64_P (info->output_bfd))
4921
0
    {
4922
0
      if ((rel->r_offset + 5) >= input_section->size)
4923
0
        goto corrupt_input;
4924
0
      if (contents[rel->r_offset + 5] == 0xb8)
4925
0
        {
4926
0
          if (rel->r_offset < 3
4927
0
        || (rel->r_offset - 3 + 22) > input_section->size)
4928
0
      goto corrupt_input;
4929
0
          memcpy (contents + rel->r_offset - 3,
4930
0
            "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
4931
0
            "\x64\x48\x8b\x04\x25\0\0\0", 22);
4932
0
        }
4933
0
      else if (contents[rel->r_offset + 4] == 0xff
4934
0
         || contents[rel->r_offset + 4] == 0x67)
4935
0
        {
4936
0
          if (rel->r_offset < 3
4937
0
        || (rel->r_offset - 3 + 13) > input_section->size)
4938
0
      goto corrupt_input;
4939
0
          memcpy (contents + rel->r_offset - 3,
4940
0
            "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0",
4941
0
            13);
4942
4943
0
        }
4944
0
      else
4945
0
        {
4946
0
          if (rel->r_offset < 3
4947
0
        || (rel->r_offset - 3 + 12) > input_section->size)
4948
0
      goto corrupt_input;
4949
0
          memcpy (contents + rel->r_offset - 3,
4950
0
            "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
4951
0
        }
4952
0
    }
4953
0
        else
4954
0
    {
4955
0
      if ((rel->r_offset + 4) >= input_section->size)
4956
0
        goto corrupt_input;
4957
0
      if (contents[rel->r_offset + 4] == 0xff)
4958
0
        {
4959
0
          if (rel->r_offset < 3
4960
0
        || (rel->r_offset - 3 + 13) > input_section->size)
4961
0
      goto corrupt_input;
4962
0
          memcpy (contents + rel->r_offset - 3,
4963
0
            "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0",
4964
0
            13);
4965
0
        }
4966
0
      else
4967
0
        {
4968
0
          if (rel->r_offset < 3
4969
0
        || (rel->r_offset - 3 + 12) > input_section->size)
4970
0
      goto corrupt_input;
4971
0
          memcpy (contents + rel->r_offset - 3,
4972
0
            "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
4973
0
        }
4974
0
    }
4975
        /* Skip R_X86_64_PC32, R_X86_64_PLT32, R_X86_64_GOTPCRELX
4976
     and R_X86_64_PLTOFF64.  */
4977
0
        rel++;
4978
0
        wrel++;
4979
0
        continue;
4980
0
      }
4981
4982
0
    if (htab->elf.sgot == NULL)
4983
0
      abort ();
4984
4985
0
    off = htab->tls_ld_or_ldm_got.offset;
4986
0
    if (off & 1)
4987
0
      off &= ~1;
4988
0
    else
4989
0
      {
4990
0
        Elf_Internal_Rela outrel;
4991
4992
0
        if (htab->elf.srelgot == NULL)
4993
0
    abort ();
4994
4995
0
        outrel.r_offset = (htab->elf.sgot->output_section->vma
4996
0
         + htab->elf.sgot->output_offset + off);
4997
4998
0
        bfd_put_64 (info->output_bfd, 0,
4999
0
        htab->elf.sgot->contents + off);
5000
0
        bfd_put_64 (info->output_bfd, 0,
5001
0
        htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
5002
0
        outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
5003
0
        outrel.r_addend = 0;
5004
0
        _bfd_elf_append_rela (info->output_bfd,
5005
0
            htab->elf.srelgot, &outrel);
5006
0
        htab->tls_ld_or_ldm_got.offset |= 1;
5007
0
      }
5008
0
    relocation = htab->elf.sgot->output_section->vma
5009
0
           + htab->elf.sgot->output_offset + off;
5010
0
    unresolved_reloc = false;
5011
0
    break;
5012
5013
0
  case R_X86_64_DTPOFF32:
5014
0
    if (!bfd_link_executable (info)
5015
0
        || (input_section->flags & SEC_CODE) == 0)
5016
0
      relocation -= _bfd_x86_elf_dtpoff_base (info);
5017
0
    else
5018
0
      relocation = elf_x86_64_tpoff (info, relocation);
5019
0
    break;
5020
5021
0
  case R_X86_64_TPOFF32:
5022
0
  case R_X86_64_TPOFF64:
5023
0
    BFD_ASSERT (bfd_link_executable (info));
5024
0
    relocation = elf_x86_64_tpoff (info, relocation);
5025
0
    break;
5026
5027
0
  case R_X86_64_DTPOFF64:
5028
0
    BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
5029
0
    relocation -= _bfd_x86_elf_dtpoff_base (info);
5030
0
    break;
5031
5032
0
  default:
5033
0
    break;
5034
0
  }
5035
5036
      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
5037
   because such sections are not SEC_ALLOC and thus ld.so will
5038
   not process them.  */
5039
0
      if (unresolved_reloc
5040
0
    && !((input_section->flags & SEC_DEBUGGING) != 0
5041
0
         && h->def_dynamic)
5042
0
    && _bfd_elf_section_offset (info->output_bfd, info, input_section,
5043
0
              rel->r_offset) != (bfd_vma) -1)
5044
0
  {
5045
0
    switch (r_type)
5046
0
      {
5047
0
      case R_X86_64_32S:
5048
0
        sec = h->root.u.def.section;
5049
0
        if ((info->nocopyreloc || eh->def_protected)
5050
0
      && !(h->root.u.def.section->flags & SEC_CODE))
5051
0
    return elf_x86_64_need_pic (info, input_bfd, input_section,
5052
0
              h, NULL, NULL, howto);
5053
        /* Fall through.  */
5054
5055
0
      default:
5056
0
        _bfd_error_handler
5057
    /* xgettext:c-format */
5058
0
    (_("%pB(%pA+%#" PRIx64 "): "
5059
0
       "unresolvable %s relocation against symbol `%s'"),
5060
0
     input_bfd,
5061
0
     input_section,
5062
0
     (uint64_t) rel->r_offset,
5063
0
     howto->name,
5064
0
     h->root.root.string);
5065
0
        return false;
5066
0
      }
5067
0
  }
5068
5069
0
    do_relocation:
5070
0
      r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5071
0
            contents, rel->r_offset,
5072
0
            relocation, rel->r_addend);
5073
5074
0
    check_relocation_error:
5075
0
      if (r != bfd_reloc_ok)
5076
0
  {
5077
0
    const char *name;
5078
5079
0
    if (h != NULL)
5080
0
      name = h->root.root.string;
5081
0
    else
5082
0
      {
5083
0
        name = bfd_elf_string_from_elf_section (input_bfd,
5084
0
                  symtab_hdr->sh_link,
5085
0
                  sym->st_name);
5086
0
        if (name == NULL)
5087
0
    return false;
5088
0
        if (*name == '\0')
5089
0
    name = bfd_section_name (sec);
5090
0
      }
5091
5092
0
    if (r == bfd_reloc_overflow)
5093
0
      {
5094
0
        if (converted_reloc)
5095
0
    {
5096
0
      info->callbacks->einfo
5097
0
        ("%X%H:", input_bfd, input_section, rel->r_offset);
5098
0
      info->callbacks->einfo
5099
0
        (_(" failed to convert GOTPCREL relocation against "
5100
0
           "'%s'; relink with --no-relax\n"),
5101
0
         name);
5102
0
      status = false;
5103
0
      continue;
5104
0
    }
5105
0
        (*info->callbacks->reloc_overflow)
5106
0
    (info, (h ? &h->root : NULL), name, howto->name,
5107
0
     (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5108
0
      }
5109
0
    else
5110
0
      {
5111
0
        _bfd_error_handler
5112
    /* xgettext:c-format */
5113
0
    (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
5114
0
     input_bfd, input_section,
5115
0
     (uint64_t) rel->r_offset, name, (int) r);
5116
0
        return false;
5117
0
      }
5118
0
  }
5119
5120
0
      if (wrel != rel)
5121
0
  *wrel = *rel;
5122
0
    }
5123
5124
0
  if (wrel != rel)
5125
0
    {
5126
0
      Elf_Internal_Shdr *rel_hdr;
5127
0
      size_t deleted = rel - wrel;
5128
5129
0
      rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
5130
0
      rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5131
0
      rel_hdr = _bfd_elf_single_rel_hdr (input_section);
5132
0
      rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5133
0
      input_section->reloc_count -= deleted;
5134
0
    }
5135
5136
0
  return status;
5137
0
}
5138
5139
/* Finish up dynamic symbol handling.  We set the contents of various
5140
   dynamic sections here.  */
5141
5142
static bool
5143
elf_x86_64_finish_dynamic_symbol (struct bfd_link_info *info,
5144
          struct elf_link_hash_entry *h,
5145
          Elf_Internal_Sym *sym)
5146
0
{
5147
0
  struct elf_x86_link_hash_table *htab;
5148
0
  bool use_plt_second;
5149
0
  struct elf_x86_link_hash_entry *eh;
5150
0
  bool local_undefweak;
5151
5152
0
  htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
5153
5154
  /* Use the second PLT section only if there is .plt section.  */
5155
0
  use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
5156
5157
0
  eh = (struct elf_x86_link_hash_entry *) h;
5158
0
  if (eh->no_finish_dynamic_symbol)
5159
0
    abort ();
5160
5161
  /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
5162
     resolved undefined weak symbols in executable so that their
5163
     references have value 0 at run-time.  */
5164
0
  local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
5165
5166
0
  if (h->plt.offset != (bfd_vma) -1)
5167
0
    {
5168
0
      bfd_vma plt_index;
5169
0
      bfd_vma got_offset, plt_offset;
5170
0
      Elf_Internal_Rela rela;
5171
0
      bfd_byte *loc;
5172
0
      asection *plt, *gotplt, *relplt, *resolved_plt;
5173
0
      elf_backend_data *bed;
5174
0
      bfd_vma plt_got_pcrel_offset;
5175
5176
      /* When building a static executable, use .iplt, .igot.plt and
5177
   .rela.iplt sections for STT_GNU_IFUNC symbols.  */
5178
0
      if (htab->elf.splt != NULL)
5179
0
  {
5180
0
    plt = htab->elf.splt;
5181
0
    gotplt = htab->elf.sgotplt;
5182
0
    relplt = htab->elf.srelplt;
5183
0
  }
5184
0
      else
5185
0
  {
5186
0
    plt = htab->elf.iplt;
5187
0
    gotplt = htab->elf.igotplt;
5188
0
    relplt = htab->elf.irelplt;
5189
0
  }
5190
5191
0
      VERIFY_PLT_ENTRY (info, h, plt, gotplt, relplt, local_undefweak)
5192
5193
      /* Get the index in the procedure linkage table which
5194
   corresponds to this symbol.  This is the index of this symbol
5195
   in all the symbols for which we are making plt entries.  The
5196
   first entry in the procedure linkage table is reserved.
5197
5198
   Get the offset into the .got table of the entry that
5199
   corresponds to this function.  Each .got entry is GOT_ENTRY_SIZE
5200
   bytes. The first three are reserved for the dynamic linker.
5201
5202
   For static executables, we don't reserve anything.  */
5203
5204
0
      if (plt == htab->elf.splt)
5205
0
  {
5206
0
    got_offset = (h->plt.offset / htab->plt.plt_entry_size
5207
0
      - htab->plt.has_plt0);
5208
0
    got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
5209
0
  }
5210
0
      else
5211
0
  {
5212
0
    got_offset = h->plt.offset / htab->plt.plt_entry_size;
5213
0
    got_offset = got_offset * GOT_ENTRY_SIZE;
5214
0
  }
5215
5216
      /* Fill in the entry in the procedure linkage table.  */
5217
0
      memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
5218
0
        htab->plt.plt_entry_size);
5219
0
      if (use_plt_second)
5220
0
  {
5221
0
    memcpy (htab->plt_second->contents + eh->plt_second.offset,
5222
0
      htab->non_lazy_plt->plt_entry,
5223
0
      htab->non_lazy_plt->plt_entry_size);
5224
5225
0
    resolved_plt = htab->plt_second;
5226
0
    plt_offset = eh->plt_second.offset;
5227
0
  }
5228
0
      else
5229
0
  {
5230
0
    resolved_plt = plt;
5231
0
    plt_offset = h->plt.offset;
5232
0
  }
5233
5234
      /* Insert the relocation positions of the plt section.  */
5235
5236
      /* Put offset the PC-relative instruction referring to the GOT entry,
5237
   subtracting the size of that instruction.  */
5238
0
      plt_got_pcrel_offset = (gotplt->output_section->vma
5239
0
            + gotplt->output_offset
5240
0
            + got_offset
5241
0
            - resolved_plt->output_section->vma
5242
0
            - resolved_plt->output_offset
5243
0
            - plt_offset
5244
0
            - htab->plt.plt_got_insn_size);
5245
5246
      /* Check PC-relative offset overflow in PLT entry.  */
5247
0
      if ((plt_got_pcrel_offset + 0x80000000) > 0xffffffff)
5248
  /* xgettext:c-format */
5249
0
  info->callbacks->fatal (_("%pB: PC-relative offset overflow in PLT entry for `%s'\n"),
5250
0
        info->output_bfd, h->root.root.string);
5251
5252
0
      bfd_put_32 (info->output_bfd, plt_got_pcrel_offset,
5253
0
      (resolved_plt->contents + plt_offset
5254
0
       + htab->plt.plt_got_offset));
5255
5256
      /* Fill in the entry in the global offset table, initially this
5257
   points to the second part of the PLT entry.  Leave the entry
5258
   as zero for undefined weak symbol in PIE.  No PLT relocation
5259
   against undefined weak symbol in PIE.  */
5260
0
      if (!local_undefweak)
5261
0
  {
5262
0
    if (htab->plt.has_plt0)
5263
0
      bfd_put_64 (info->output_bfd, (plt->output_section->vma
5264
0
             + plt->output_offset
5265
0
             + h->plt.offset
5266
0
             + htab->lazy_plt->plt_lazy_offset),
5267
0
      gotplt->contents + got_offset);
5268
5269
    /* Fill in the entry in the .rela.plt section.  */
5270
0
    rela.r_offset = (gotplt->output_section->vma
5271
0
         + gotplt->output_offset
5272
0
         + got_offset);
5273
0
    if (PLT_LOCAL_IFUNC_P (info, h))
5274
0
      {
5275
0
        if (h->root.u.def.section == NULL)
5276
0
    return false;
5277
5278
0
        info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
5279
0
              h->root.root.string,
5280
0
              h->root.u.def.section->owner);
5281
5282
        /* If an STT_GNU_IFUNC symbol is locally defined, generate
5283
     R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT.  */
5284
0
        rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
5285
0
        rela.r_addend = (h->root.u.def.value
5286
0
             + h->root.u.def.section->output_section->vma
5287
0
             + h->root.u.def.section->output_offset);
5288
5289
0
        if (htab->params->report_relative_reloc)
5290
0
    _bfd_x86_elf_link_report_relative_reloc
5291
0
      (info, relplt, h, sym, "R_X86_64_IRELATIVE", &rela);
5292
5293
        /* R_X86_64_IRELATIVE comes last.  */
5294
0
        plt_index = htab->next_irelative_index--;
5295
0
      }
5296
0
    else
5297
0
      {
5298
0
        rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
5299
0
        if (htab->params->mark_plt)
5300
0
    rela.r_addend = (resolved_plt->output_section->vma
5301
0
         + plt_offset
5302
0
         + htab->plt.plt_indirect_branch_offset);
5303
0
        else
5304
0
    rela.r_addend = 0;
5305
0
        plt_index = htab->next_jump_slot_index++;
5306
0
      }
5307
5308
    /* Don't fill the second and third slots in PLT entry for
5309
       static executables nor without PLT0.  */
5310
0
    if (plt == htab->elf.splt && htab->plt.has_plt0)
5311
0
      {
5312
0
        bfd_vma plt0_offset
5313
0
    = h->plt.offset + htab->lazy_plt->plt_plt_insn_end;
5314
5315
        /* Put relocation index.  */
5316
0
        bfd_put_32 (info->output_bfd, plt_index,
5317
0
        (plt->contents + h->plt.offset
5318
0
         + htab->lazy_plt->plt_reloc_offset));
5319
5320
        /* Put offset for jmp .PLT0 and check for overflow.  We don't
5321
     check relocation index for overflow since branch displacement
5322
     will overflow first.  */
5323
0
        if (plt0_offset > 0x80000000)
5324
    /* xgettext:c-format */
5325
0
    info->callbacks->fatal (_("%pB: branch displacement overflow in PLT entry for `%s'\n"),
5326
0
          info->output_bfd, h->root.root.string);
5327
0
        bfd_put_32 (info->output_bfd, - plt0_offset,
5328
0
        (plt->contents + h->plt.offset
5329
0
         + htab->lazy_plt->plt_plt_offset));
5330
0
      }
5331
5332
0
    bed = get_elf_backend_data (info->output_bfd);
5333
0
    loc = relplt->contents + plt_index * bed->s->sizeof_rela;
5334
0
    bed->s->swap_reloca_out (info->output_bfd, &rela, loc);
5335
0
  }
5336
0
    }
5337
0
  else if (eh->plt_got.offset != (bfd_vma) -1)
5338
0
    {
5339
0
      bfd_vma got_offset, plt_offset;
5340
0
      asection *plt, *got;
5341
0
      bool got_after_plt;
5342
0
      int32_t got_pcrel_offset;
5343
5344
      /* Set the entry in the GOT procedure linkage table.  */
5345
0
      plt = htab->plt_got;
5346
0
      got = htab->elf.sgot;
5347
0
      got_offset = h->got.offset;
5348
5349
0
      if (got_offset == (bfd_vma) -1
5350
0
    || (h->type == STT_GNU_IFUNC && h->def_regular)
5351
0
    || plt == NULL
5352
0
    || got == NULL)
5353
0
  abort ();
5354
5355
      /* Use the non-lazy PLT entry template for the GOT PLT since they
5356
   are the identical.  */
5357
      /* Fill in the entry in the GOT procedure linkage table.  */
5358
0
      plt_offset = eh->plt_got.offset;
5359
0
      memcpy (plt->contents + plt_offset,
5360
0
        htab->non_lazy_plt->plt_entry,
5361
0
        htab->non_lazy_plt->plt_entry_size);
5362
5363
      /* Put offset the PC-relative instruction referring to the GOT
5364
   entry, subtracting the size of that instruction.  */
5365
0
      got_pcrel_offset = (got->output_section->vma
5366
0
        + got->output_offset
5367
0
        + got_offset
5368
0
        - plt->output_section->vma
5369
0
        - plt->output_offset
5370
0
        - plt_offset
5371
0
        - htab->non_lazy_plt->plt_got_insn_size);
5372
5373
      /* Check PC-relative offset overflow in GOT PLT entry.  */
5374
0
      got_after_plt = got->output_section->vma > plt->output_section->vma;
5375
0
      if ((got_after_plt && got_pcrel_offset < 0)
5376
0
    || (!got_after_plt && got_pcrel_offset > 0))
5377
  /* xgettext:c-format */
5378
0
  info->callbacks->fatal (_("%pB: PC-relative offset overflow in GOT PLT entry for `%s'\n"),
5379
0
        info->output_bfd, h->root.root.string);
5380
5381
0
      bfd_put_32 (info->output_bfd, got_pcrel_offset,
5382
0
      (plt->contents + plt_offset
5383
0
       + htab->non_lazy_plt->plt_got_offset));
5384
0
    }
5385
5386
0
  if (!local_undefweak
5387
0
      && !h->def_regular
5388
0
      && (h->plt.offset != (bfd_vma) -1
5389
0
    || eh->plt_got.offset != (bfd_vma) -1))
5390
0
    {
5391
      /* Mark the symbol as undefined, rather than as defined in
5392
   the .plt section.  Leave the value if there were any
5393
   relocations where pointer equality matters (this is a clue
5394
   for the dynamic linker, to make function pointer
5395
   comparisons work between an application and shared
5396
   library), otherwise set it to zero.  If a function is only
5397
   called from a binary, there is no need to slow down
5398
   shared libraries because of that.  */
5399
0
      sym->st_shndx = SHN_UNDEF;
5400
0
      if (!h->pointer_equality_needed)
5401
0
  sym->st_value = 0;
5402
0
    }
5403
5404
0
  _bfd_x86_elf_link_fixup_ifunc_symbol (info, htab, h, sym);
5405
5406
  /* Don't generate dynamic GOT relocation against undefined weak
5407
     symbol in executable.  */
5408
0
  if (h->got.offset != (bfd_vma) -1
5409
0
      && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
5410
0
      && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
5411
0
      && !local_undefweak)
5412
0
    {
5413
0
      Elf_Internal_Rela rela;
5414
0
      asection *relgot = htab->elf.srelgot;
5415
0
      const char *relative_reloc_name = NULL;
5416
0
      bool generate_dynamic_reloc = true;
5417
5418
      /* This symbol has an entry in the global offset table.  Set it
5419
   up.  */
5420
0
      if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
5421
0
  abort ();
5422
5423
0
      rela.r_offset = (htab->elf.sgot->output_section->vma
5424
0
           + htab->elf.sgot->output_offset
5425
0
           + (h->got.offset &~ (bfd_vma) 1));
5426
5427
      /* If this is a static link, or it is a -Bsymbolic link and the
5428
   symbol is defined locally or was forced to be local because
5429
   of a version file, we just want to emit a RELATIVE reloc.
5430
   The entry in the global offset table will already have been
5431
   initialized in the relocate_section function.  */
5432
0
      if (h->def_regular
5433
0
    && h->type == STT_GNU_IFUNC)
5434
0
  {
5435
0
    if (h->plt.offset == (bfd_vma) -1)
5436
0
      {
5437
        /* STT_GNU_IFUNC is referenced without PLT.  */
5438
0
        if (htab->elf.splt == NULL)
5439
0
    {
5440
      /* use .rel[a].iplt section to store .got relocations
5441
         in static executable.  */
5442
0
      relgot = htab->elf.irelplt;
5443
0
    }
5444
0
        if (SYMBOL_REFERENCES_LOCAL_P (info, h))
5445
0
    {
5446
0
      if (h->root.u.def.section == NULL)
5447
0
        return false;
5448
5449
0
      info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
5450
0
            h->root.root.string,
5451
0
            h->root.u.def.section->owner);
5452
5453
0
      rela.r_info = htab->r_info (0,
5454
0
                R_X86_64_IRELATIVE);
5455
0
      rela.r_addend = (h->root.u.def.value
5456
0
           + h->root.u.def.section->output_section->vma
5457
0
           + h->root.u.def.section->output_offset);
5458
0
      relative_reloc_name = "R_X86_64_IRELATIVE";
5459
0
    }
5460
0
        else
5461
0
    goto do_glob_dat;
5462
0
      }
5463
0
    else if (bfd_link_pic (info))
5464
0
      {
5465
        /* Generate R_X86_64_GLOB_DAT.  */
5466
0
        goto do_glob_dat;
5467
0
      }
5468
0
    else
5469
0
      {
5470
0
        asection *plt;
5471
0
        bfd_vma plt_offset;
5472
5473
0
        if (!h->pointer_equality_needed)
5474
0
    abort ();
5475
5476
        /* For non-shared object, we can't use .got.plt, which
5477
     contains the real function addres if we need pointer
5478
     equality.  We load the GOT entry with the PLT entry.  */
5479
0
        if (htab->plt_second != NULL)
5480
0
    {
5481
0
      plt = htab->plt_second;
5482
0
      plt_offset = eh->plt_second.offset;
5483
0
    }
5484
0
        else
5485
0
    {
5486
0
      plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
5487
0
      plt_offset =  h->plt.offset;
5488
0
    }
5489
0
        bfd_put_64 (info->output_bfd, (plt->output_section->vma
5490
0
               + plt->output_offset
5491
0
               + plt_offset),
5492
0
        htab->elf.sgot->contents + h->got.offset);
5493
0
        return true;
5494
0
      }
5495
0
  }
5496
0
      else if (bfd_link_pic (info)
5497
0
         && SYMBOL_REFERENCES_LOCAL_P (info, h))
5498
0
  {
5499
0
    if (!SYMBOL_DEFINED_NON_SHARED_P (h))
5500
0
      return false;
5501
0
    BFD_ASSERT((h->got.offset & 1) != 0);
5502
0
    if (info->enable_dt_relr)
5503
0
      generate_dynamic_reloc = false;
5504
0
    else
5505
0
      {
5506
0
        rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
5507
0
        rela.r_addend = (h->root.u.def.value
5508
0
             + h->root.u.def.section->output_section->vma
5509
0
             + h->root.u.def.section->output_offset);
5510
0
        relative_reloc_name = "R_X86_64_RELATIVE";
5511
0
      }
5512
0
  }
5513
0
      else
5514
0
  {
5515
0
    BFD_ASSERT((h->got.offset & 1) == 0);
5516
0
  do_glob_dat:
5517
0
    bfd_put_64 (info->output_bfd, 0,
5518
0
          htab->elf.sgot->contents + h->got.offset);
5519
0
    rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
5520
0
    rela.r_addend = 0;
5521
0
  }
5522
5523
0
      if (generate_dynamic_reloc)
5524
0
  {
5525
    /* If the relgot section has not been created, then
5526
       generate an error instead of a reloc.  cf PR 32638.  */
5527
0
    if (relgot == NULL || relgot->size == 0)
5528
0
      {
5529
0
        info->callbacks->fatal (_("%pB: Unable to generate dynamic relocs because a suitable section does not exist\n"),
5530
0
              info->output_bfd);
5531
0
        return false;
5532
0
      }
5533
    
5534
0
    if (relative_reloc_name != NULL
5535
0
        && htab->params->report_relative_reloc)
5536
0
      _bfd_x86_elf_link_report_relative_reloc
5537
0
        (info, relgot, h, sym, relative_reloc_name, &rela);
5538
5539
0
    _bfd_elf_append_rela (info->output_bfd, relgot, &rela);
5540
0
  }
5541
0
    }
5542
5543
0
  if (h->needs_copy)
5544
0
    {
5545
0
      Elf_Internal_Rela rela;
5546
0
      asection *s;
5547
5548
      /* This symbol needs a copy reloc.  Set it up.  */
5549
0
      VERIFY_COPY_RELOC (h, htab)
5550
5551
0
      rela.r_offset = (h->root.u.def.value
5552
0
           + h->root.u.def.section->output_section->vma
5553
0
           + h->root.u.def.section->output_offset);
5554
0
      rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
5555
0
      rela.r_addend = 0;
5556
0
      if (h->root.u.def.section == htab->elf.sdynrelro)
5557
0
  s = htab->elf.sreldynrelro;
5558
0
      else
5559
0
  s = htab->elf.srelbss;
5560
0
      _bfd_elf_append_rela (info->output_bfd, s, &rela);
5561
0
    }
5562
5563
0
  return true;
5564
0
}
5565
5566
/* Finish up local dynamic symbol handling.  We set the contents of
5567
   various dynamic sections here.  */
5568
5569
static int
5570
elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
5571
0
{
5572
0
  struct elf_link_hash_entry *h
5573
0
    = (struct elf_link_hash_entry *) *slot;
5574
0
  struct bfd_link_info *info
5575
0
    = (struct bfd_link_info *) inf;
5576
5577
0
  return elf_x86_64_finish_dynamic_symbol (info, h, NULL);
5578
0
}
5579
5580
/* Finish up undefined weak symbol handling in PIE.  Fill its PLT entry
5581
   here since undefined weak symbol may not be dynamic and may not be
5582
   called for elf_x86_64_finish_dynamic_symbol.  */
5583
5584
static bool
5585
elf_x86_64_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
5586
          void *inf)
5587
0
{
5588
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
5589
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
5590
5591
0
  if (h->root.type != bfd_link_hash_undefweak
5592
0
      || h->dynindx != -1)
5593
0
    return true;
5594
5595
0
  return elf_x86_64_finish_dynamic_symbol (info, h, NULL);
5596
0
}
5597
5598
/* Used to decide how to sort relocs in an optimal manner for the
5599
   dynamic linker, before writing them out.  */
5600
5601
static enum elf_reloc_type_class
5602
elf_x86_64_reloc_type_class (const struct bfd_link_info *info,
5603
           const asection *rel_sec ATTRIBUTE_UNUSED,
5604
           const Elf_Internal_Rela *rela)
5605
0
{
5606
0
  bfd *abfd = info->output_bfd;
5607
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
5608
0
  struct elf_x86_link_hash_table *htab
5609
0
    = elf_x86_hash_table (info, X86_64_ELF_DATA);
5610
5611
0
  if (htab->elf.dynsym != NULL
5612
0
      && htab->elf.dynsym->contents != NULL)
5613
0
    {
5614
      /* Check relocation against STT_GNU_IFUNC symbol if there are
5615
   dynamic symbols.  */
5616
0
      unsigned long r_symndx = htab->r_sym (rela->r_info);
5617
0
      if (r_symndx != STN_UNDEF)
5618
0
  {
5619
0
    Elf_Internal_Sym sym;
5620
0
    if (!bed->s->swap_symbol_in (abfd,
5621
0
               (htab->elf.dynsym->contents
5622
0
          + r_symndx * bed->s->sizeof_sym),
5623
0
               0, &sym))
5624
0
      abort ();
5625
5626
0
    if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
5627
0
      return reloc_class_ifunc;
5628
0
  }
5629
0
    }
5630
5631
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
5632
0
    {
5633
0
    case R_X86_64_IRELATIVE:
5634
0
      return reloc_class_ifunc;
5635
0
    case R_X86_64_RELATIVE:
5636
0
    case R_X86_64_RELATIVE64:
5637
0
      return reloc_class_relative;
5638
0
    case R_X86_64_JUMP_SLOT:
5639
0
      return reloc_class_plt;
5640
0
    case R_X86_64_COPY:
5641
0
      return reloc_class_copy;
5642
0
    default:
5643
0
      return reloc_class_normal;
5644
0
    }
5645
0
}
5646
5647
/* Finish up the dynamic sections.  */
5648
5649
static bool
5650
elf_x86_64_finish_dynamic_sections (struct bfd_link_info *info,
5651
            bfd_byte *buf)
5652
0
{
5653
0
  struct elf_x86_link_hash_table *htab;
5654
5655
0
  htab = _bfd_x86_elf_finish_dynamic_sections (info, buf);
5656
0
  if (htab == NULL)
5657
0
    return false;
5658
5659
0
  if (! htab->elf.dynamic_sections_created)
5660
0
    return true;
5661
5662
0
  if (htab->elf.splt && htab->elf.splt->size > 0)
5663
0
    {
5664
0
      if (bfd_is_abs_section (htab->elf.splt->output_section))
5665
0
  {
5666
0
    info->callbacks->fatal
5667
0
      (_("%P: discarded output section: `%pA'\n"),
5668
0
       htab->elf.splt);
5669
0
    return false;
5670
0
  }
5671
5672
0
      elf_section_data (htab->elf.splt->output_section)
5673
0
  ->this_hdr.sh_entsize = htab->plt.plt_entry_size;
5674
5675
0
      if (htab->plt.has_plt0)
5676
0
  {
5677
    /* Fill in the special first entry in the procedure linkage
5678
       table.  */
5679
0
    memcpy (htab->elf.splt->contents,
5680
0
      htab->lazy_plt->plt0_entry,
5681
0
      htab->lazy_plt->plt0_entry_size);
5682
    /* Add offset for pushq GOT+8(%rip), since the instruction
5683
       uses 6 bytes subtract this value.  */
5684
0
    bfd_put_32 (info->output_bfd,
5685
0
          (htab->elf.sgotplt->output_section->vma
5686
0
           + htab->elf.sgotplt->output_offset
5687
0
           + 8
5688
0
           - htab->elf.splt->output_section->vma
5689
0
           - htab->elf.splt->output_offset
5690
0
           - 6),
5691
0
          (htab->elf.splt->contents
5692
0
           + htab->lazy_plt->plt0_got1_offset));
5693
    /* Add offset for the PC-relative instruction accessing
5694
       GOT+16, subtracting the offset to the end of that
5695
       instruction.  */
5696
0
    bfd_put_32 (info->output_bfd,
5697
0
          (htab->elf.sgotplt->output_section->vma
5698
0
           + htab->elf.sgotplt->output_offset
5699
0
           + 16
5700
0
           - htab->elf.splt->output_section->vma
5701
0
           - htab->elf.splt->output_offset
5702
0
           - htab->lazy_plt->plt0_got2_insn_end),
5703
0
          (htab->elf.splt->contents
5704
0
           + htab->lazy_plt->plt0_got2_offset));
5705
0
  }
5706
5707
0
      if (htab->elf.tlsdesc_plt)
5708
0
  {
5709
0
    bfd_put_64 (info->output_bfd, 0,
5710
0
          htab->elf.sgot->contents + htab->elf.tlsdesc_got);
5711
5712
0
    memcpy (htab->elf.splt->contents + htab->elf.tlsdesc_plt,
5713
0
      htab->lazy_plt->plt_tlsdesc_entry,
5714
0
      htab->lazy_plt->plt_tlsdesc_entry_size);
5715
5716
    /* Add offset for pushq GOT+8(%rip), since ENDBR64 uses 4
5717
       bytes and the instruction uses 6 bytes, subtract these
5718
       values.  */
5719
0
    bfd_put_32 (info->output_bfd,
5720
0
          (htab->elf.sgotplt->output_section->vma
5721
0
           + htab->elf.sgotplt->output_offset
5722
0
           + 8
5723
0
           - htab->elf.splt->output_section->vma
5724
0
           - htab->elf.splt->output_offset
5725
0
           - htab->elf.tlsdesc_plt
5726
0
           - htab->lazy_plt->plt_tlsdesc_got1_insn_end),
5727
0
          (htab->elf.splt->contents
5728
0
           + htab->elf.tlsdesc_plt
5729
0
           + htab->lazy_plt->plt_tlsdesc_got1_offset));
5730
    /* Add offset for indirect branch via GOT+TDG, where TDG
5731
       stands for htab->tlsdesc_got, subtracting the offset
5732
       to the end of that instruction.  */
5733
0
    bfd_put_32 (info->output_bfd,
5734
0
          (htab->elf.sgot->output_section->vma
5735
0
           + htab->elf.sgot->output_offset
5736
0
           + htab->elf.tlsdesc_got
5737
0
           - htab->elf.splt->output_section->vma
5738
0
           - htab->elf.splt->output_offset
5739
0
           - htab->elf.tlsdesc_plt
5740
0
           - htab->lazy_plt->plt_tlsdesc_got2_insn_end),
5741
0
          (htab->elf.splt->contents
5742
0
           + htab->elf.tlsdesc_plt
5743
0
           + htab->lazy_plt->plt_tlsdesc_got2_offset));
5744
0
  }
5745
0
    }
5746
5747
  /* Fill PLT entries for undefined weak symbols in PIE.  */
5748
0
  if (bfd_link_pie (info))
5749
0
    bfd_hash_traverse (&info->hash->table,
5750
0
           elf_x86_64_pie_finish_undefweak_symbol,
5751
0
           info);
5752
5753
0
  return true;
5754
0
}
5755
5756
/* Fill PLT/GOT entries and allocate dynamic relocations for local
5757
   STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
5758
   It has to be done before elf_link_sort_relocs is called so that
5759
   dynamic relocations are properly sorted.  */
5760
5761
static bool
5762
elf_x86_64_output_arch_local_syms
5763
  (struct bfd_link_info *info,
5764
   void *flaginfo ATTRIBUTE_UNUSED,
5765
   int (*func) (void *, const char *,
5766
    Elf_Internal_Sym *,
5767
    asection *,
5768
    struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
5769
0
{
5770
0
  struct elf_x86_link_hash_table *htab
5771
0
    = elf_x86_hash_table (info, X86_64_ELF_DATA);
5772
0
  if (htab == NULL)
5773
0
    return false;
5774
5775
  /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols if
5776
     needed.  */
5777
0
  if (htab->has_loc_hash_table)
5778
0
    htab_traverse (htab->loc_hash_table,
5779
0
       elf_x86_64_finish_local_dynamic_symbol,
5780
0
       info);
5781
5782
0
  return true;
5783
0
}
5784
5785
/* Similar to _bfd_elf_get_synthetic_symtab.  Support PLTs with all
5786
   dynamic relocations.   */
5787
5788
static long
5789
elf_x86_64_get_synthetic_symtab (bfd *abfd,
5790
         long symcount ATTRIBUTE_UNUSED,
5791
         asymbol **syms ATTRIBUTE_UNUSED,
5792
         long dynsymcount,
5793
         asymbol **dynsyms,
5794
         asymbol **ret)
5795
446
{
5796
446
  long count, i, n;
5797
446
  int j;
5798
446
  bfd_byte *plt_contents;
5799
446
  long relsize;
5800
446
  const struct elf_x86_lazy_plt_layout *lazy_plt;
5801
446
  const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
5802
446
  const struct elf_x86_lazy_plt_layout *lazy_bnd_plt;
5803
446
  const struct elf_x86_non_lazy_plt_layout *non_lazy_bnd_plt;
5804
446
  const struct elf_x86_lazy_plt_layout *lazy_bnd_ibt_plt;
5805
446
  const struct elf_x86_non_lazy_plt_layout *non_lazy_bnd_ibt_plt;
5806
446
  const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
5807
446
  const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
5808
446
  asection *plt;
5809
446
  enum elf_x86_plt_type plt_type;
5810
446
  struct elf_x86_plt plts[] =
5811
446
    {
5812
446
      { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
5813
446
      { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
5814
446
      { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
5815
446
      { ".plt.bnd", NULL, NULL, plt_second, 0, 0, 0, 0 },
5816
446
      { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
5817
446
    };
5818
5819
446
  *ret = NULL;
5820
5821
446
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
5822
368
    return 0;
5823
5824
78
  if (dynsymcount <= 0)
5825
27
    return 0;
5826
5827
51
  relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
5828
51
  if (relsize <= 0)
5829
1
    return -1;
5830
5831
50
  lazy_plt = &elf_x86_64_lazy_plt;
5832
50
  non_lazy_plt = &elf_x86_64_non_lazy_plt;
5833
50
  lazy_ibt_plt = &elf_x86_64_lazy_ibt_plt;
5834
50
  non_lazy_ibt_plt = &elf_x86_64_non_lazy_ibt_plt;
5835
50
  if (ABI_64_P (abfd))
5836
50
    {
5837
50
      lazy_bnd_ibt_plt = &elf_x86_64_lazy_bnd_ibt_plt;
5838
50
      non_lazy_bnd_ibt_plt = &elf_x86_64_non_lazy_bnd_ibt_plt;
5839
50
      lazy_bnd_plt = &elf_x86_64_lazy_bnd_plt;
5840
50
      non_lazy_bnd_plt = &elf_x86_64_non_lazy_bnd_plt;
5841
50
    }
5842
0
  else
5843
0
    {
5844
0
      lazy_bnd_ibt_plt = NULL;
5845
0
      non_lazy_bnd_ibt_plt = NULL;
5846
0
      lazy_bnd_plt = NULL;
5847
0
      non_lazy_bnd_plt = NULL;
5848
0
    }
5849
5850
50
  count = 0;
5851
246
  for (j = 0; plts[j].name != NULL; j++)
5852
197
    {
5853
197
      plt = bfd_get_section_by_name (abfd, plts[j].name);
5854
197
      if (plt == NULL
5855
63
    || plt->size == 0
5856
63
    || (plt->flags & SEC_HAS_CONTENTS) == 0)
5857
134
  continue;
5858
5859
      /* Get the PLT section contents.  */
5860
63
      if (!_bfd_elf_mmap_section_contents (abfd, plt, &plt_contents))
5861
1
  break;
5862
5863
      /* Check what kind of PLT it is.  */
5864
62
      plt_type = plt_unknown;
5865
62
      if (plts[j].type == plt_unknown
5866
40
    && (plt->size >= (lazy_plt->plt_entry_size
5867
40
          + lazy_plt->plt_entry_size)))
5868
25
  {
5869
    /* Match lazy PLT first.  Need to check the first two
5870
       instructions.   */
5871
25
    if ((memcmp (plt_contents, lazy_plt->plt0_entry,
5872
25
           lazy_plt->plt0_got1_offset) == 0)
5873
19
        && (memcmp (plt_contents + 6, lazy_plt->plt0_entry + 6,
5874
19
        2) == 0))
5875
18
      {
5876
18
        if (memcmp (plt_contents + lazy_ibt_plt->plt_entry_size,
5877
18
        lazy_ibt_plt->plt_entry,
5878
18
        lazy_ibt_plt->plt_reloc_offset) == 0)
5879
0
    {
5880
      /* The fist entry in the lazy IBT PLT is the same as
5881
         the lazy PLT.  */
5882
0
      plt_type = plt_lazy | plt_second;
5883
0
      lazy_plt = lazy_ibt_plt;
5884
0
    }
5885
18
        else
5886
18
    plt_type = plt_lazy;
5887
18
      }
5888
7
    else if (lazy_bnd_plt != NULL
5889
7
       && (memcmp (plt_contents, lazy_bnd_plt->plt0_entry,
5890
7
             lazy_bnd_plt->plt0_got1_offset) == 0)
5891
1
       && (memcmp (plt_contents + 6,
5892
1
             lazy_bnd_plt->plt0_entry + 6, 3) == 0))
5893
0
      {
5894
0
        plt_type = plt_lazy | plt_second;
5895
        /* The fist entry in the lazy BND IBT PLT is the same as
5896
     the lazy BND PLT.  */
5897
0
        if (memcmp (plt_contents
5898
0
        + lazy_bnd_ibt_plt->plt_entry_size,
5899
0
        lazy_bnd_ibt_plt->plt_entry,
5900
0
        lazy_bnd_ibt_plt->plt_reloc_offset) == 0)
5901
0
    lazy_plt = lazy_bnd_ibt_plt;
5902
0
        else
5903
0
    lazy_plt = lazy_bnd_plt;
5904
0
      }
5905
25
  }
5906
5907
62
      if (non_lazy_plt != NULL
5908
62
    && (plt_type == plt_unknown || plt_type == plt_non_lazy)
5909
44
    && plt->size >= non_lazy_plt->plt_entry_size)
5910
44
  {
5911
    /* Match non-lazy PLT.  */
5912
44
    if (memcmp (plt_contents, non_lazy_plt->plt_entry,
5913
44
          non_lazy_plt->plt_got_offset) == 0)
5914
19
      plt_type = plt_non_lazy;
5915
44
  }
5916
5917
62
      if (plt_type == plt_unknown || plt_type == plt_second)
5918
25
  {
5919
25
    if (plt->size >= non_lazy_ibt_plt->plt_entry_size
5920
23
        && (memcmp (plt_contents,
5921
23
        non_lazy_ibt_plt->plt_entry,
5922
23
        non_lazy_ibt_plt->plt_got_offset) == 0))
5923
0
      {
5924
        /* Match IBT PLT.  */
5925
0
        plt_type = plt_second;
5926
0
        non_lazy_plt = non_lazy_ibt_plt;
5927
0
      }
5928
25
    else if (non_lazy_bnd_plt != NULL)
5929
25
      {
5930
25
        if (plt->size >= non_lazy_bnd_plt->plt_entry_size
5931
25
      && (memcmp (plt_contents, non_lazy_bnd_plt->plt_entry,
5932
25
            non_lazy_bnd_plt->plt_got_offset) == 0))
5933
0
    {
5934
      /* Match BND PLT.  */
5935
0
      plt_type = plt_second;
5936
0
      non_lazy_plt = non_lazy_bnd_plt;
5937
0
    }
5938
25
        else if (plt->size >= non_lazy_bnd_ibt_plt->plt_entry_size
5939
23
           && (memcmp (plt_contents,
5940
23
           non_lazy_bnd_ibt_plt->plt_entry,
5941
23
           non_lazy_bnd_ibt_plt->plt_got_offset)
5942
23
         == 0))
5943
0
    {
5944
      /* Match BND IBT PLT.  */
5945
0
      plt_type = plt_second;
5946
0
      non_lazy_plt = non_lazy_bnd_ibt_plt;
5947
0
    }
5948
25
      }
5949
25
  }
5950
5951
62
      if (plt_type == plt_unknown)
5952
25
  {
5953
25
    _bfd_elf_munmap_section_contents (plt, plt_contents);
5954
25
    continue;
5955
25
  }
5956
5957
37
      plts[j].sec = plt;
5958
37
      plts[j].type = plt_type;
5959
5960
37
      if ((plt_type & plt_lazy))
5961
18
  {
5962
18
    plts[j].plt_got_offset = lazy_plt->plt_got_offset;
5963
18
    plts[j].plt_got_insn_size = lazy_plt->plt_got_insn_size;
5964
18
    plts[j].plt_entry_size = lazy_plt->plt_entry_size;
5965
    /* Skip PLT0 in lazy PLT.  */
5966
18
    i = 1;
5967
18
  }
5968
19
      else
5969
19
  {
5970
19
    plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
5971
19
    plts[j].plt_got_insn_size = non_lazy_plt->plt_got_insn_size;
5972
19
    plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
5973
19
    i = 0;
5974
19
  }
5975
5976
      /* Skip lazy PLT when the second PLT is used.  */
5977
37
      if (plt_type == (plt_lazy | plt_second))
5978
0
  plts[j].count = 0;
5979
37
      else
5980
37
  {
5981
37
    n = plt->size / plts[j].plt_entry_size;
5982
37
    plts[j].count = n;
5983
37
    count += n - i;
5984
37
  }
5985
5986
37
      plts[j].contents = plt_contents;
5987
37
    }
5988
5989
50
  return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
5990
50
              (bfd_vma) 0, plts, dynsyms,
5991
50
              ret);
5992
51
}
5993
5994
/* Handle an x86-64 specific section when reading an object file.  This
5995
   is called when elfcode.h finds a section with an unknown type.  */
5996
5997
static bool
5998
elf_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
5999
            const char *name, int shindex)
6000
15.2k
{
6001
15.2k
  if (hdr->sh_type != SHT_X86_64_UNWIND)
6002
11.0k
    return false;
6003
6004
4.25k
  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6005
4
    return false;
6006
6007
4.25k
  return true;
6008
4.25k
}
6009
6010
/* Hook called by the linker routine which adds symbols from an object
6011
   file.  We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
6012
   of .bss.  */
6013
6014
static bool
6015
elf_x86_64_add_symbol_hook (bfd *abfd,
6016
          struct bfd_link_info *info ATTRIBUTE_UNUSED,
6017
          Elf_Internal_Sym *sym,
6018
          const char **namep ATTRIBUTE_UNUSED,
6019
          flagword *flagsp ATTRIBUTE_UNUSED,
6020
          asection **secp,
6021
          bfd_vma *valp)
6022
0
{
6023
0
  asection *lcomm;
6024
6025
0
  switch (sym->st_shndx)
6026
0
    {
6027
0
    case SHN_X86_64_LCOMMON:
6028
0
      lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
6029
0
      if (lcomm == NULL)
6030
0
  {
6031
0
    lcomm = bfd_make_section_with_flags (abfd,
6032
0
                 "LARGE_COMMON",
6033
0
                 (SEC_ALLOC
6034
0
            | SEC_IS_COMMON
6035
0
            | SEC_LINKER_CREATED));
6036
0
    if (lcomm == NULL)
6037
0
      return false;
6038
0
    elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
6039
0
  }
6040
0
      *secp = lcomm;
6041
0
      *valp = sym->st_size;
6042
0
      return true;
6043
0
    }
6044
6045
0
  return true;
6046
0
}
6047
6048
6049
/* Given a BFD section, try to locate the corresponding ELF section
6050
   index.  */
6051
6052
static bool
6053
elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6054
           asection *sec, int *index_return)
6055
413
{
6056
413
  if (sec == &bfd_elf_large_com_section)
6057
0
    {
6058
0
      *index_return = SHN_X86_64_LCOMMON;
6059
0
      return true;
6060
0
    }
6061
413
  return false;
6062
413
}
6063
6064
/* Process a symbol.  */
6065
6066
static void
6067
elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
6068
            asymbol *asym)
6069
49.1k
{
6070
49.1k
  elf_symbol_type *elfsym = (elf_symbol_type *) asym;
6071
6072
49.1k
  switch (elfsym->internal_elf_sym.st_shndx)
6073
49.1k
    {
6074
19
    case SHN_X86_64_LCOMMON:
6075
19
      asym->section = &bfd_elf_large_com_section;
6076
19
      asym->value = elfsym->internal_elf_sym.st_size;
6077
      /* Common symbol doesn't set BSF_GLOBAL.  */
6078
19
      asym->flags &= ~BSF_GLOBAL;
6079
19
      break;
6080
49.1k
    }
6081
49.1k
}
6082
6083
static bool
6084
elf_x86_64_common_definition (Elf_Internal_Sym *sym)
6085
0
{
6086
0
  return (sym->st_shndx == SHN_COMMON
6087
0
    || sym->st_shndx == SHN_X86_64_LCOMMON);
6088
0
}
6089
6090
static unsigned int
6091
elf_x86_64_common_section_index (asection *sec)
6092
0
{
6093
0
  if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
6094
0
    return SHN_COMMON;
6095
0
  else
6096
0
    return SHN_X86_64_LCOMMON;
6097
0
}
6098
6099
static asection *
6100
elf_x86_64_common_section (asection *sec)
6101
0
{
6102
0
  if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
6103
0
    return bfd_com_section_ptr;
6104
0
  else
6105
0
    return &bfd_elf_large_com_section;
6106
0
}
6107
6108
static bool
6109
elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
6110
       const Elf_Internal_Sym *sym,
6111
       asection **psec,
6112
       bool newdef,
6113
       bool olddef,
6114
       bfd *oldbfd,
6115
       const asection *oldsec)
6116
0
{
6117
  /* A normal common symbol and a large common symbol result in a
6118
     normal common symbol.  We turn the large common symbol into a
6119
     normal one.  */
6120
0
  if (!olddef
6121
0
      && h->root.type == bfd_link_hash_common
6122
0
      && !newdef
6123
0
      && bfd_is_com_section (*psec)
6124
0
      && oldsec != *psec)
6125
0
    {
6126
0
      if (sym->st_shndx == SHN_COMMON
6127
0
    && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
6128
0
  {
6129
0
    h->root.u.c.p->section
6130
0
      = bfd_make_section_old_way (oldbfd, "COMMON");
6131
0
    h->root.u.c.p->section->flags = SEC_ALLOC;
6132
0
  }
6133
0
      else if (sym->st_shndx == SHN_X86_64_LCOMMON
6134
0
         && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
6135
0
  *psec = bfd_com_section_ptr;
6136
0
    }
6137
6138
0
  return true;
6139
0
}
6140
6141
static bool
6142
elf_x86_64_section_flags (const Elf_Internal_Shdr *hdr)
6143
114k
{
6144
114k
  if ((hdr->sh_flags & SHF_X86_64_LARGE) != 0)
6145
4.59k
    hdr->bfd_section->flags |= SEC_ELF_LARGE;
6146
6147
114k
  return true;
6148
114k
}
6149
6150
static bool
6151
elf_x86_64_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
6152
        Elf_Internal_Shdr *hdr, asection *sec)
6153
885
{
6154
885
  if (sec->flags & SEC_ELF_LARGE)
6155
26
    hdr->sh_flags |= SHF_X86_64_LARGE;
6156
6157
885
  return true;
6158
885
}
6159
6160
static bool
6161
elf_x86_64_copy_private_section_data (bfd *ibfd, asection *isec,
6162
              bfd *obfd, asection *osec,
6163
              struct bfd_link_info *link_info)
6164
967
{
6165
967
  if (!_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec, link_info))
6166
0
    return false;
6167
6168
  /* objcopy --set-section-flags without "large" drops SHF_X86_64_LARGE.  */
6169
967
  if (link_info == NULL && ibfd != obfd)
6170
967
    elf_section_flags (osec) &= ~SHF_X86_64_LARGE;
6171
6172
967
  return true;
6173
967
}
6174
6175
static int
6176
elf_x86_64_additional_program_headers (bfd *abfd,
6177
               struct bfd_link_info *info ATTRIBUTE_UNUSED)
6178
18
{
6179
18
  asection *s;
6180
18
  int count = 0;
6181
6182
  /* Check to see if we need a large readonly segment.  */
6183
18
  s = bfd_get_section_by_name (abfd, ".lrodata");
6184
18
  if (s && (s->flags & SEC_LOAD))
6185
0
    count++;
6186
6187
  /* Check to see if we need a large data segment.  Since .lbss sections
6188
     is placed right after the .bss section, there should be no need for
6189
     a large data segment just because of .lbss.  */
6190
18
  s = bfd_get_section_by_name (abfd, ".ldata");
6191
18
  if (s && (s->flags & SEC_LOAD))
6192
0
    count++;
6193
6194
18
  return count;
6195
18
}
6196
6197
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
6198
6199
static bool
6200
elf_x86_64_relocs_compatible (const bfd_target *input,
6201
            const bfd_target *output)
6202
0
{
6203
0
  return ((xvec_get_elf_backend_data (input)->s->elfclass
6204
0
     == xvec_get_elf_backend_data (output)->s->elfclass)
6205
0
    && _bfd_elf_relocs_compatible (input, output));
6206
0
}
6207
6208
/* Set up x86-64 GNU properties.  Return the first relocatable ELF input
6209
   with GNU properties if found.  Otherwise, return NULL.  */
6210
6211
static bfd *
6212
elf_x86_64_link_setup_gnu_properties (struct bfd_link_info *info)
6213
0
{
6214
0
  struct elf_x86_init_table init_table;
6215
0
  elf_backend_data *bed;
6216
0
  struct elf_x86_link_hash_table *htab;
6217
6218
0
  if ((int) R_X86_64_standard >= (int) R_X86_64_converted_reloc_bit
6219
0
      || (int) R_X86_64_max <= (int) R_X86_64_converted_reloc_bit
6220
0
      || ((int) (R_X86_64_GNU_VTINHERIT | R_X86_64_converted_reloc_bit)
6221
0
    != (int) R_X86_64_GNU_VTINHERIT)
6222
0
      || ((int) (R_X86_64_GNU_VTENTRY | R_X86_64_converted_reloc_bit)
6223
0
    != (int) R_X86_64_GNU_VTENTRY))
6224
0
    abort ();
6225
6226
  /* This is unused for x86-64.  */
6227
0
  init_table.plt0_pad_byte = 0x90;
6228
6229
0
  bed = get_elf_backend_data (info->output_bfd);
6230
0
  htab = elf_x86_hash_table (info, bed->target_id);
6231
0
  if (!htab)
6232
0
    abort ();
6233
6234
0
  init_table.lazy_plt = &elf_x86_64_lazy_plt;
6235
0
  init_table.non_lazy_plt = &elf_x86_64_non_lazy_plt;
6236
6237
0
  init_table.lazy_ibt_plt = &elf_x86_64_lazy_ibt_plt;
6238
0
  init_table.non_lazy_ibt_plt = &elf_x86_64_non_lazy_ibt_plt;
6239
6240
0
  if (ABI_64_P (info->output_bfd))
6241
0
    {
6242
0
      init_table.sframe_lazy_plt = &elf_x86_64_sframe_plt;
6243
0
      init_table.sframe_non_lazy_plt = &elf_x86_64_sframe_non_lazy_plt;
6244
0
      init_table.sframe_lazy_ibt_plt = &elf_x86_64_sframe_ibt_plt;
6245
0
      init_table.sframe_non_lazy_ibt_plt = &elf_x86_64_sframe_non_lazy_ibt_plt;
6246
0
    }
6247
0
  else
6248
0
    {
6249
      /* SFrame is not supported for non AMD64.  */
6250
0
      init_table.sframe_lazy_plt = NULL;
6251
0
      init_table.sframe_non_lazy_plt = NULL;
6252
0
    }
6253
6254
0
  if (ABI_64_P (info->output_bfd))
6255
0
    {
6256
0
      init_table.r_info = elf64_r_info;
6257
0
      init_table.r_sym = elf64_r_sym;
6258
0
    }
6259
0
  else
6260
0
    {
6261
0
      init_table.r_info = elf32_r_info;
6262
0
      init_table.r_sym = elf32_r_sym;
6263
0
    }
6264
6265
0
  return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
6266
0
}
6267
6268
static void
6269
elf_x86_64_add_glibc_version_dependency
6270
  (struct elf_find_verdep_info *rinfo)
6271
0
{
6272
0
  int i = 0, mark_plt = -1;
6273
0
  const char *version[4] = { NULL, NULL, NULL, NULL };
6274
0
  bool auto_version[4] = { false, false, false, false };
6275
0
  struct elf_x86_link_hash_table *htab;
6276
6277
0
  if (rinfo->info->enable_dt_relr)
6278
0
    {
6279
0
      version[i] = "GLIBC_ABI_DT_RELR";
6280
0
      i++;
6281
0
    }
6282
6283
0
  htab = elf_x86_hash_table (rinfo->info, X86_64_ELF_DATA);
6284
0
  if (htab != NULL)
6285
0
    {
6286
0
      if (htab->params->gnu2_tls_version_tag && htab->has_tls_desc_call)
6287
0
  {
6288
0
    version[i] = "GLIBC_ABI_GNU2_TLS";
6289
    /* 2 == auto, enable if libc.so defines the GLIBC_ABI_GNU2_TLS
6290
       version.  */
6291
0
    if (htab->params->gnu2_tls_version_tag == 2)
6292
0
      auto_version[i] = true;
6293
0
    i++;
6294
0
  }
6295
0
      if (htab->params->mark_plt)
6296
0
  {
6297
0
    mark_plt = i;
6298
0
    auto_version[i] = true;
6299
0
    version[i] = "GLIBC_ABI_DT_X86_64_PLT";
6300
0
    i++;
6301
0
  }
6302
0
    }
6303
6304
0
  if (i == 0
6305
0
      || !_bfd_elf_link_add_glibc_version_dependency (rinfo, version,
6306
0
                  auto_version))
6307
0
    return;
6308
6309
0
  if (mark_plt < 0 || auto_version[mark_plt])
6310
0
    return;
6311
6312
  /* Add the GLIBC_2.36 version dependency if libc.so doesn't have
6313
     GLIBC_ABI_DT_X86_64_PLT.  */
6314
0
  version[0] = "GLIBC_2.36";
6315
0
  auto_version[0] = false;
6316
0
  version[1] = NULL;
6317
0
  _bfd_elf_link_add_glibc_version_dependency (rinfo, version,
6318
0
                auto_version);
6319
0
}
6320
6321
static const struct bfd_elf_special_section
6322
elf_x86_64_special_sections[]=
6323
{
6324
  { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
6325
  { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
6326
  { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
6327
  { STRING_COMMA_LEN (".lbss"),      -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
6328
  { STRING_COMMA_LEN (".ldata"),     -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
6329
  { STRING_COMMA_LEN (".lrodata"),     -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
6330
  { NULL,     0,      0, 0,      0 }
6331
};
6332
6333
#define TARGET_LITTLE_SYM       x86_64_elf64_vec
6334
#define TARGET_LITTLE_NAME        "elf64-x86-64"
6335
#define ELF_ARCH          bfd_arch_i386
6336
#define ELF_TARGET_ID         X86_64_ELF_DATA
6337
#define ELF_MACHINE_CODE        EM_X86_64
6338
#define ELF_OSABI         ELFOSABI_GNU
6339
#define ELF_MAXPAGESIZE         0x1000
6340
#define ELF_COMMONPAGESIZE        ELF_MAXPAGESIZE
6341
6342
#define elf_backend_can_gc_sections     1
6343
#define elf_backend_can_refcount      1
6344
#define elf_backend_want_got_plt      1
6345
#define elf_backend_plt_readonly      1
6346
#define elf_backend_want_plt_sym      0
6347
#define elf_backend_got_header_size     (GOT_ENTRY_SIZE*3)
6348
#define elf_backend_rela_normal       1
6349
#define elf_backend_plt_alignment     4
6350
#define elf_backend_caches_rawsize      1
6351
#define elf_backend_dtrel_excludes_plt      1
6352
#define elf_backend_want_dynrelro     1
6353
6354
#define elf_info_to_howto       elf_x86_64_info_to_howto
6355
6356
#define bfd_elf64_bfd_copy_private_section_data \
6357
  elf_x86_64_copy_private_section_data
6358
#define bfd_elf64_bfd_reloc_type_lookup     elf_x86_64_reloc_type_lookup
6359
#define bfd_elf64_bfd_reloc_name_lookup \
6360
  elf_x86_64_reloc_name_lookup
6361
6362
#define elf_backend_relocs_compatible     elf_x86_64_relocs_compatible
6363
#define elf_backend_early_size_sections     elf_x86_64_early_size_sections
6364
#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
6365
#define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
6366
#define elf_backend_finish_dynamic_symbol   elf_x86_64_finish_dynamic_symbol
6367
#define elf_backend_output_arch_local_syms  elf_x86_64_output_arch_local_syms
6368
#define elf_backend_grok_prstatus     elf_x86_64_grok_prstatus
6369
#define elf_backend_grok_psinfo       elf_x86_64_grok_psinfo
6370
#ifdef CORE_HEADER
6371
#define elf_backend_write_core_note     elf_x86_64_write_core_note
6372
#endif
6373
#define elf_backend_reloc_type_class      elf_x86_64_reloc_type_class
6374
#define elf_backend_relocate_section      elf_x86_64_relocate_section
6375
#define elf_backend_init_index_section      _bfd_elf_init_1_index_section
6376
#define elf_backend_object_p        elf64_x86_64_elf_object_p
6377
#define bfd_elf64_get_synthetic_symtab      elf_x86_64_get_synthetic_symtab
6378
6379
#define elf_backend_section_from_shdr \
6380
  elf_x86_64_section_from_shdr
6381
6382
#define elf_backend_section_from_bfd_section \
6383
  elf_x86_64_elf_section_from_bfd_section
6384
#define elf_backend_add_symbol_hook \
6385
  elf_x86_64_add_symbol_hook
6386
#define elf_backend_symbol_processing \
6387
  elf_x86_64_symbol_processing
6388
#define elf_backend_common_section_index \
6389
  elf_x86_64_common_section_index
6390
#define elf_backend_common_section \
6391
  elf_x86_64_common_section
6392
#define elf_backend_common_definition \
6393
  elf_x86_64_common_definition
6394
#define elf_backend_merge_symbol \
6395
  elf_x86_64_merge_symbol
6396
#define elf_backend_special_sections \
6397
  elf_x86_64_special_sections
6398
#define elf_backend_section_flags     elf_x86_64_section_flags
6399
#define elf_backend_fake_sections     elf_x86_64_fake_sections
6400
#define elf_backend_additional_program_headers \
6401
  elf_x86_64_additional_program_headers
6402
#define elf_backend_setup_gnu_properties \
6403
  elf_x86_64_link_setup_gnu_properties
6404
#define elf_backend_hide_symbol \
6405
  _bfd_x86_elf_hide_symbol
6406
#define elf_backend_add_glibc_version_dependency \
6407
  elf_x86_64_add_glibc_version_dependency
6408
6409
#undef  elf64_bed
6410
#define elf64_bed elf64_x86_64_bed
6411
6412
#include "elf64-target.h"
6413
6414
#undef elf_backend_add_glibc_version_dependency
6415
6416
/* FreeBSD support.  */
6417
6418
#undef  TARGET_LITTLE_SYM
6419
#define TARGET_LITTLE_SYM       x86_64_elf64_fbsd_vec
6420
#undef  TARGET_LITTLE_NAME
6421
#define TARGET_LITTLE_NAME        "elf64-x86-64-freebsd"
6422
6423
#undef  ELF_OSABI
6424
#define ELF_OSABI         ELFOSABI_FREEBSD
6425
#undef  ELF_OSABI_EXACT
6426
#define ELF_OSABI_EXACT         1
6427
6428
#undef  elf64_bed
6429
#define elf64_bed elf64_x86_64_fbsd_bed
6430
6431
#include "elf64-target.h"
6432
6433
#ifdef OBJ_MAYBE_ELF_SOLARIS2
6434
6435
/* Solaris 2 support.  */
6436
6437
static bool
6438
elf_x86_64_solaris2_add_symbol_hook (bfd *abfd,
6439
             struct bfd_link_info *info,
6440
             Elf_Internal_Sym *sym,
6441
             const char **namep,
6442
             flagword *flagsp,
6443
             asection **secp,
6444
             bfd_vma *valp)
6445
0
{
6446
0
  return (elf_x86_64_add_symbol_hook (abfd, info, sym, namep,
6447
0
              flagsp, secp, valp)
6448
0
    && elf_solaris2_add_symbol_hook (abfd, info, sym, namep,
6449
0
             flagsp, secp, valp));
6450
0
}
6451
6452
#undef  TARGET_LITTLE_SYM
6453
#define TARGET_LITTLE_SYM       x86_64_elf64_sol2_vec
6454
#undef  TARGET_LITTLE_NAME
6455
#define TARGET_LITTLE_NAME        "elf64-x86-64-sol2"
6456
6457
#undef  ELF_MAXPAGESIZE
6458
#define ELF_MAXPAGESIZE         0x100000
6459
6460
#undef  ELF_TARGET_OS
6461
#define ELF_TARGET_OS         is_solaris
6462
6463
#undef ELF_OSABI
6464
#define ELF_OSABI         ELFOSABI_SOLARIS
6465
#undef ELF_OSABI_EXACT
6466
6467
#undef  elf64_bed
6468
#define elf64_bed         elf64_x86_64_sol2_bed
6469
6470
/* The 64-bit static TLS arena size is rounded to the nearest 16-byte
6471
   boundary.  */
6472
#undef  elf_backend_static_tls_alignment
6473
#define elf_backend_static_tls_alignment    16
6474
6475
/* The Solaris 2 ABI requires a plt symbol on all platforms.
6476
6477
   Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
6478
   File, p.63.  */
6479
#undef  elf_backend_want_plt_sym
6480
#define elf_backend_want_plt_sym      1
6481
6482
#undef  elf_backend_add_symbol_hook
6483
#define elf_backend_add_symbol_hook     elf_x86_64_solaris2_add_symbol_hook
6484
6485
#include "elf64-target.h"
6486
6487
#endif /* OBJ_MAYBE_ELF_SOLARIS2 */
6488
6489
/* Restore defaults.  */
6490
#undef  ELF_OSABI
6491
#undef  ELF_OSABI_EXACT
6492
#undef  elf_backend_static_tls_alignment
6493
#undef  elf_backend_want_plt_sym
6494
#define elf_backend_want_plt_sym  0
6495
#undef  elf_backend_add_symbol_hook
6496
#define elf_backend_add_symbol_hook elf_x86_64_add_symbol_hook
6497
6498
/* 32bit x86-64 support.  */
6499
6500
#undef  TARGET_LITTLE_SYM
6501
#define TARGET_LITTLE_SYM       x86_64_elf32_vec
6502
#undef  TARGET_LITTLE_NAME
6503
#define TARGET_LITTLE_NAME        "elf32-x86-64"
6504
#undef  elf32_bed
6505
#define elf32_bed         elf32_x86_64_bed
6506
6507
#undef ELF_ARCH
6508
#define ELF_ARCH          bfd_arch_i386
6509
6510
#undef  ELF_MAXPAGESIZE
6511
#define ELF_MAXPAGESIZE         0x1000
6512
6513
#undef  ELF_TARGET_OS
6514
#undef  ELF_OSABI
6515
#define ELF_OSABI         ELFOSABI_GNU
6516
#undef  ELF_OSABI_EXACT
6517
6518
#define bfd_elf32_bfd_copy_private_section_data \
6519
  elf_x86_64_copy_private_section_data
6520
#define bfd_elf32_bfd_reloc_type_lookup \
6521
  elf_x86_64_reloc_type_lookup
6522
#define bfd_elf32_bfd_reloc_name_lookup \
6523
  elf_x86_64_reloc_name_lookup
6524
#define bfd_elf32_get_synthetic_symtab \
6525
  elf_x86_64_get_synthetic_symtab
6526
6527
#undef elf_backend_object_p
6528
#define elf_backend_object_p \
6529
  elf32_x86_64_elf_object_p
6530
6531
#undef elf_backend_bfd_from_remote_memory
6532
#define elf_backend_bfd_from_remote_memory \
6533
  _bfd_elf32_bfd_from_remote_memory
6534
6535
#undef elf_backend_add_glibc_version_dependency
6536
#define elf_backend_add_glibc_version_dependency \
6537
  elf_x86_64_add_glibc_version_dependency
6538
6539
#undef elf_backend_size_info
6540
#define elf_backend_size_info \
6541
  _bfd_elf32_size_info
6542
6543
#include "elf32-target.h"