Coverage Report

Created: 2025-07-08 11:15

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