Coverage Report

Created: 2026-03-10 08:46

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