Coverage Report

Created: 2026-09-14 08:07

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