Coverage Report

Created: 2024-05-21 06:29

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