Coverage Report

Created: 2026-11-21 09:00

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