Coverage Report

Created: 2026-11-21 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-i386.c
Line
Count
Source
1
/* Intel 80386/80486-specific support for 32-bit ELF
2
   Copyright (C) 1993-2026 Free Software Foundation, Inc.
3
4
   This file is part of BFD, the Binary File Descriptor library.
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   MA 02110-1301, USA.  */
20
21
#include "elfxx-x86.h"
22
#include "elf-vxworks.h"
23
#include "dwarf2.h"
24
#include "elf-solaris2.h"
25
#include "opcode/i386.h"
26
27
/* 386 uses REL relocations instead of RELA.  */
28
#define USE_REL 1
29
30
static reloc_howto_type elf_howto_table[]=
31
{
32
  HOWTO(R_386_NONE, 0, 0, 0, false, 0, complain_overflow_dont,
33
  bfd_elf_generic_reloc, "R_386_NONE",
34
  true, 0x00000000, 0x00000000, false),
35
  HOWTO(R_386_32, 0, 4, 32, false, 0, complain_overflow_dont,
36
  bfd_elf_generic_reloc, "R_386_32",
37
  true, 0xffffffff, 0xffffffff, false),
38
  HOWTO(R_386_PC32, 0, 4, 32, true, 0, complain_overflow_dont,
39
  bfd_elf_generic_reloc, "R_386_PC32",
40
  true, 0xffffffff, 0xffffffff, true),
41
  HOWTO(R_386_GOT32, 0, 4, 32, false, 0, complain_overflow_dont,
42
  bfd_elf_generic_reloc, "R_386_GOT32",
43
  true, 0xffffffff, 0xffffffff, false),
44
  HOWTO(R_386_PLT32, 0, 4, 32, true, 0, complain_overflow_dont,
45
  bfd_elf_generic_reloc, "R_386_PLT32",
46
  true, 0xffffffff, 0xffffffff, true),
47
  HOWTO(R_386_COPY, 0, 4, 32, false, 0, complain_overflow_dont,
48
  bfd_elf_generic_reloc, "R_386_COPY",
49
  true, 0xffffffff, 0xffffffff, false),
50
  HOWTO(R_386_GLOB_DAT, 0, 4, 32, false, 0, complain_overflow_dont,
51
  bfd_elf_generic_reloc, "R_386_GLOB_DAT",
52
  true, 0xffffffff, 0xffffffff, false),
53
  HOWTO(R_386_JUMP_SLOT, 0, 4, 32, false, 0, complain_overflow_dont,
54
  bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
55
  true, 0xffffffff, 0xffffffff, false),
56
  HOWTO(R_386_RELATIVE, 0, 4, 32, false, 0, complain_overflow_dont,
57
  bfd_elf_generic_reloc, "R_386_RELATIVE",
58
  true, 0xffffffff, 0xffffffff, false),
59
  HOWTO(R_386_GOTOFF, 0, 4, 32, false, 0, complain_overflow_dont,
60
  bfd_elf_generic_reloc, "R_386_GOTOFF",
61
  true, 0xffffffff, 0xffffffff, false),
62
  HOWTO(R_386_GOTPC, 0, 4, 32, true, 0, complain_overflow_dont,
63
  bfd_elf_generic_reloc, "R_386_GOTPC",
64
  true, 0xffffffff, 0xffffffff, true),
65
66
  /* We have a gap in the reloc numbers here.
67
     R_386_standard counts the number up to this point, and
68
     R_386_ext_offset is the value to subtract from a reloc type of
69
     R_386_16 thru R_386_PC8 to form an index into this table.  */
70
4.52k
#define R_386_standard (R_386_GOTPC + 1)
71
1.61k
#define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
72
73
  /* These relocs are a GNU extension.  */
74
  HOWTO(R_386_TLS_TPOFF, 0, 4, 32, false, 0, complain_overflow_dont,
75
  bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
76
  true, 0xffffffff, 0xffffffff, false),
77
  HOWTO(R_386_TLS_IE, 0, 4, 32, false, 0, complain_overflow_dont,
78
  bfd_elf_generic_reloc, "R_386_TLS_IE",
79
  true, 0xffffffff, 0xffffffff, false),
80
  HOWTO(R_386_TLS_GOTIE, 0, 4, 32, false, 0, complain_overflow_dont,
81
  bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
82
  true, 0xffffffff, 0xffffffff, false),
83
  HOWTO(R_386_TLS_LE, 0, 4, 32, false, 0, complain_overflow_dont,
84
  bfd_elf_generic_reloc, "R_386_TLS_LE",
85
  true, 0xffffffff, 0xffffffff, false),
86
  HOWTO(R_386_TLS_GD, 0, 4, 32, false, 0, complain_overflow_dont,
87
  bfd_elf_generic_reloc, "R_386_TLS_GD",
88
  true, 0xffffffff, 0xffffffff, false),
89
  HOWTO(R_386_TLS_LDM, 0, 4, 32, false, 0, complain_overflow_dont,
90
  bfd_elf_generic_reloc, "R_386_TLS_LDM",
91
  true, 0xffffffff, 0xffffffff, false),
92
  HOWTO(R_386_16, 0, 2, 16, false, 0, complain_overflow_bitfield,
93
  bfd_elf_generic_reloc, "R_386_16",
94
  true, 0xffff, 0xffff, false),
95
  HOWTO(R_386_PC16, 0, 2, 16, true, 0, complain_overflow_bitfield,
96
  bfd_elf_generic_reloc, "R_386_PC16",
97
  true, 0xffff, 0xffff, true),
98
  HOWTO(R_386_8, 0, 1, 8, false, 0, complain_overflow_bitfield,
99
  bfd_elf_generic_reloc, "R_386_8",
100
  true, 0xff, 0xff, false),
101
  HOWTO(R_386_PC8, 0, 1, 8, true, 0, complain_overflow_signed,
102
  bfd_elf_generic_reloc, "R_386_PC8",
103
  true, 0xff, 0xff, true),
104
105
1.40k
#define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
106
864
#define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
107
  /* These are common with Solaris TLS implementation.  */
108
  HOWTO(R_386_TLS_LDO_32, 0, 4, 32, false, 0, complain_overflow_dont,
109
  bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
110
  true, 0xffffffff, 0xffffffff, false),
111
  HOWTO(R_386_TLS_IE_32, 0, 4, 32, false, 0, complain_overflow_dont,
112
  bfd_elf_generic_reloc, "R_386_TLS_IE_32",
113
  true, 0xffffffff, 0xffffffff, false),
114
  HOWTO(R_386_TLS_LE_32, 0, 4, 32, false, 0, complain_overflow_dont,
115
  bfd_elf_generic_reloc, "R_386_TLS_LE_32",
116
  true, 0xffffffff, 0xffffffff, false),
117
  HOWTO(R_386_TLS_DTPMOD32, 0, 4, 32, false, 0, complain_overflow_dont,
118
  bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
119
  true, 0xffffffff, 0xffffffff, false),
120
  HOWTO(R_386_TLS_DTPOFF32, 0, 4, 32, false, 0, complain_overflow_dont,
121
  bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
122
  true, 0xffffffff, 0xffffffff, false),
123
  HOWTO(R_386_TLS_TPOFF32, 0, 4, 32, false, 0, complain_overflow_dont,
124
  bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
125
  true, 0xffffffff, 0xffffffff, false),
126
  HOWTO(R_386_SIZE32, 0, 4, 32, false, 0, complain_overflow_dont,
127
  bfd_elf_generic_reloc, "R_386_SIZE32",
128
  true, 0xffffffff, 0xffffffff, false),
129
  HOWTO(R_386_TLS_GOTDESC, 0, 4, 32, false, 0, complain_overflow_dont,
130
  bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
131
  true, 0xffffffff, 0xffffffff, false),
132
  HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, false, 0, complain_overflow_dont,
133
  bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
134
  false, 0, 0, false),
135
  HOWTO(R_386_TLS_DESC, 0, 4, 32, false, 0, complain_overflow_dont,
136
  bfd_elf_generic_reloc, "R_386_TLS_DESC",
137
  true, 0xffffffff, 0xffffffff, false),
138
  HOWTO(R_386_IRELATIVE, 0, 4, 32, false, 0, complain_overflow_dont,
139
  bfd_elf_generic_reloc, "R_386_IRELATIVE",
140
  true, 0xffffffff, 0xffffffff, false),
141
  HOWTO(R_386_GOT32X, 0, 4, 32, false, 0, complain_overflow_dont,
142
  bfd_elf_generic_reloc, "R_386_GOT32X",
143
  true, 0xffffffff, 0xffffffff, false),
144
145
  /* Another gap.  */
146
696
#define R_386_ext2 (R_386_GOT32X + 1 - R_386_tls_offset)
147
264
#define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_ext2)
148
149
/* GNU extension to record C++ vtable hierarchy.  */
150
  HOWTO (R_386_GNU_VTINHERIT, /* type */
151
   0,     /* rightshift */
152
   4,     /* size */
153
   0,     /* bitsize */
154
   false,     /* pc_relative */
155
   0,     /* bitpos */
156
   complain_overflow_dont, /* complain_on_overflow */
157
   NULL,      /* special_function */
158
   "R_386_GNU_VTINHERIT", /* name */
159
   false,     /* partial_inplace */
160
   0,     /* src_mask */
161
   0,     /* dst_mask */
162
   false),    /* pcrel_offset */
163
164
/* GNU extension to record C++ vtable member usage.  */
165
  HOWTO (R_386_GNU_VTENTRY, /* type */
166
   0,     /* rightshift */
167
   4,     /* size */
168
   0,     /* bitsize */
169
   false,     /* pc_relative */
170
   0,     /* bitpos */
171
   complain_overflow_dont, /* complain_on_overflow */
172
   _bfd_elf_rel_vtable_reloc_fn, /* special_function */
173
   "R_386_GNU_VTENTRY", /* name */
174
   false,     /* partial_inplace */
175
   0,     /* src_mask */
176
   0,     /* dst_mask */
177
   false)     /* pcrel_offset */
178
179
132
#define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
180
181
};
182
183
#ifdef DEBUG_GEN_RELOC
184
#define TRACE(str) \
185
  fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
186
#else
187
#define TRACE(str)
188
#endif
189
190
static reloc_howto_type *
191
elf_i386_reloc_type_lookup (bfd *abfd,
192
          bfd_reloc_code_real_type code)
193
0
{
194
0
  switch (code)
195
0
    {
196
0
    case BFD_RELOC_NONE:
197
0
      TRACE ("BFD_RELOC_NONE");
198
0
      return &elf_howto_table[R_386_NONE];
199
200
0
    case BFD_RELOC_32:
201
0
      TRACE ("BFD_RELOC_32");
202
0
      return &elf_howto_table[R_386_32];
203
204
0
    case BFD_RELOC_CTOR:
205
0
      TRACE ("BFD_RELOC_CTOR");
206
0
      return &elf_howto_table[R_386_32];
207
208
0
    case BFD_RELOC_32_PCREL:
209
0
      TRACE ("BFD_RELOC_PC32");
210
0
      return &elf_howto_table[R_386_PC32];
211
212
0
    case BFD_RELOC_386_GOT32:
213
0
      TRACE ("BFD_RELOC_386_GOT32");
214
0
      return &elf_howto_table[R_386_GOT32];
215
216
0
    case BFD_RELOC_386_PLT32:
217
0
      TRACE ("BFD_RELOC_386_PLT32");
218
0
      return &elf_howto_table[R_386_PLT32];
219
220
0
    case BFD_RELOC_COPY:
221
0
      TRACE ("BFD_RELOC_COPY");
222
0
      return &elf_howto_table[R_386_COPY];
223
224
0
    case BFD_RELOC_GLOB_DAT:
225
0
      TRACE ("BFD_RELOC_GLOB_DAT");
226
0
      return &elf_howto_table[R_386_GLOB_DAT];
227
228
0
    case BFD_RELOC_JMP_SLOT:
229
0
      TRACE ("BFD_RELOC_JMP_SLOT");
230
0
      return &elf_howto_table[R_386_JUMP_SLOT];
231
232
0
    case BFD_RELOC_RELATIVE:
233
0
      TRACE ("BFD_RELOC_RELATIVE");
234
0
      return &elf_howto_table[R_386_RELATIVE];
235
236
0
    case BFD_RELOC_32_GOTOFF:
237
0
      TRACE ("BFD_RELOC_32_GOTOFF");
238
0
      return &elf_howto_table[R_386_GOTOFF];
239
240
0
    case BFD_RELOC_32_GOT_PCREL:
241
0
      TRACE ("BFD_RELOC_386_GOTPC");
242
0
      return &elf_howto_table[R_386_GOTPC];
243
244
      /* These relocs are a GNU extension.  */
245
0
    case BFD_RELOC_386_TLS_TPOFF:
246
0
      TRACE ("BFD_RELOC_386_TLS_TPOFF");
247
0
      return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
248
249
0
    case BFD_RELOC_386_TLS_IE:
250
0
      TRACE ("BFD_RELOC_386_TLS_IE");
251
0
      return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
252
253
0
    case BFD_RELOC_386_TLS_GOTIE:
254
0
      TRACE ("BFD_RELOC_386_TLS_GOTIE");
255
0
      return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
256
257
0
    case BFD_RELOC_386_TLS_LE:
258
0
      TRACE ("BFD_RELOC_386_TLS_LE");
259
0
      return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
260
261
0
    case BFD_RELOC_386_TLS_GD:
262
0
      TRACE ("BFD_RELOC_386_TLS_GD");
263
0
      return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
264
265
0
    case BFD_RELOC_386_TLS_LDM:
266
0
      TRACE ("BFD_RELOC_386_TLS_LDM");
267
0
      return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
268
269
0
    case BFD_RELOC_16:
270
0
      TRACE ("BFD_RELOC_16");
271
0
      return &elf_howto_table[R_386_16 - R_386_ext_offset];
272
273
0
    case BFD_RELOC_16_PCREL:
274
0
      TRACE ("BFD_RELOC_16_PCREL");
275
0
      return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
276
277
0
    case BFD_RELOC_8:
278
0
      TRACE ("BFD_RELOC_8");
279
0
      return &elf_howto_table[R_386_8 - R_386_ext_offset];
280
281
0
    case BFD_RELOC_8_PCREL:
282
0
      TRACE ("BFD_RELOC_8_PCREL");
283
0
      return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
284
285
    /* Common with Sun TLS implementation.  */
286
0
    case BFD_RELOC_386_TLS_LDO_32:
287
0
      TRACE ("BFD_RELOC_386_TLS_LDO_32");
288
0
      return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
289
290
0
    case BFD_RELOC_386_TLS_IE_32:
291
0
      TRACE ("BFD_RELOC_386_TLS_IE_32");
292
0
      return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
293
294
0
    case BFD_RELOC_386_TLS_LE_32:
295
0
      TRACE ("BFD_RELOC_386_TLS_LE_32");
296
0
      return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
297
298
0
    case BFD_RELOC_386_TLS_DTPMOD32:
299
0
      TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
300
0
      return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
301
302
0
    case BFD_RELOC_386_TLS_DTPOFF32:
303
0
      TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
304
0
      return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
305
306
0
    case BFD_RELOC_386_TLS_TPOFF32:
307
0
      TRACE ("BFD_RELOC_386_TLS_TPOFF32");
308
0
      return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
309
310
0
    case BFD_RELOC_SIZE32:
311
0
      TRACE ("BFD_RELOC_SIZE32");
312
0
      return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset];
313
314
0
    case BFD_RELOC_386_TLS_GOTDESC:
315
0
      TRACE ("BFD_RELOC_386_TLS_GOTDESC");
316
0
      return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
317
318
0
    case BFD_RELOC_386_TLS_DESC_CALL:
319
0
      TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
320
0
      return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
321
322
0
    case BFD_RELOC_386_TLS_DESC:
323
0
      TRACE ("BFD_RELOC_386_TLS_DESC");
324
0
      return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
325
326
0
    case BFD_RELOC_IRELATIVE:
327
0
      TRACE ("BFD_RELOC_IRELATIVE");
328
0
      return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset];
329
330
0
    case BFD_RELOC_386_GOT32X:
331
0
      TRACE ("BFD_RELOC_386_GOT32X");
332
0
      return &elf_howto_table[R_386_GOT32X - R_386_tls_offset];
333
334
0
    case BFD_RELOC_386_PC32_TO_PLT32:
335
0
      TRACE ("BFD_RELOC_X86_PC32_TO_PLT32");
336
0
      return &elf_howto_table[R_386_PLT32];
337
338
0
    case BFD_RELOC_VTABLE_INHERIT:
339
0
      TRACE ("BFD_RELOC_VTABLE_INHERIT");
340
0
      return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
341
342
0
    case BFD_RELOC_VTABLE_ENTRY:
343
0
      TRACE ("BFD_RELOC_VTABLE_ENTRY");
344
0
      return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
345
346
0
    default:
347
0
      TRACE ("Unknown");
348
      /* xgettext:c-format */
349
0
      _bfd_error_handler (_("%pB: unsupported relocation type: %#x"),
350
0
        abfd, (int) code);
351
0
      bfd_set_error (bfd_error_bad_value);
352
0
      return NULL;
353
0
    }
354
0
}
355
356
static reloc_howto_type *
357
elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
358
          const char *r_name)
359
0
{
360
0
  unsigned int i;
361
362
0
  for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
363
0
    if (elf_howto_table[i].name != NULL
364
0
  && strcasecmp (elf_howto_table[i].name, r_name) == 0)
365
0
      return &elf_howto_table[i];
366
367
0
  return NULL;
368
0
}
369
370
static reloc_howto_type *
371
elf_i386_rtype_to_howto (unsigned r_type)
372
1.25k
{
373
1.25k
  unsigned int indx;
374
375
1.25k
  if ((indx = r_type) >= R_386_standard
376
205
      && ((indx = r_type - R_386_ext_offset) - R_386_standard
377
205
    >= R_386_ext - R_386_standard)
378
168
      && ((indx = r_type - R_386_tls_offset) - R_386_ext
379
168
    >= R_386_ext2 - R_386_ext)
380
132
      && ((indx = r_type - R_386_vt_offset) - R_386_ext2
381
132
    >= R_386_vt - R_386_ext2))
382
104
      return NULL;
383
  /* PR 17512: file: 0f67f69d.  */
384
1.14k
  if (elf_howto_table [indx].type != r_type)
385
0
    return NULL;
386
1.14k
  return &elf_howto_table[indx];
387
1.14k
}
388
389
static bool
390
elf_i386_info_to_howto_rel (bfd *abfd,
391
          arelent *cache_ptr,
392
          Elf_Internal_Rela *dst)
393
1.25k
{
394
1.25k
  unsigned int r_type = ELF32_R_TYPE (dst->r_info);
395
396
1.25k
  if ((cache_ptr->howto = elf_i386_rtype_to_howto (r_type)) == NULL)
397
104
    {
398
      /* xgettext:c-format */
399
104
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
400
104
        abfd, r_type);
401
104
      bfd_set_error (bfd_error_bad_value);
402
104
      return false;
403
104
    }
404
405
1.14k
  return true;
406
1.25k
}
407
408
/* Return whether a symbol name implies a local label.  The UnixWare
409
   2.1 cc generates temporary symbols that start with .X, so we
410
   recognize them here.  FIXME: do other SVR4 compilers also use .X?.
411
   If so, we should move the .X recognition into
412
   _bfd_elf_is_local_label_name.  */
413
414
static bool
415
elf_i386_is_local_label_name (bfd *abfd, const char *name)
416
0
{
417
0
  if (name[0] == '.' && name[1] == 'X')
418
0
    return true;
419
420
0
  return _bfd_elf_is_local_label_name (abfd, name);
421
0
}
422

423
/* Support for core dump NOTE sections.  */
424
425
static bool
426
elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
427
0
{
428
0
  int offset;
429
0
  size_t size;
430
431
0
  if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
432
0
    {
433
0
      int pr_version = bfd_get_32 (abfd, note->descdata);
434
435
0
      if (pr_version != 1)
436
0
  return false;
437
438
      /* pr_cursig */
439
0
      elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20);
440
441
      /* pr_pid */
442
0
      elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
443
444
      /* pr_reg */
445
0
      offset = 28;
446
0
      size = bfd_get_32 (abfd, note->descdata + 8);
447
0
    }
448
0
  else
449
0
    {
450
0
      switch (note->descsz)
451
0
  {
452
0
  default:
453
0
    return false;
454
455
0
  case 144:   /* Linux/i386 */
456
    /* pr_cursig */
457
0
    elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
458
459
    /* pr_pid */
460
0
    elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
461
462
    /* pr_reg */
463
0
    offset = 72;
464
0
    size = 68;
465
466
0
    break;
467
0
  }
468
0
    }
469
470
  /* Make a ".reg/999" section.  */
471
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
472
0
            size, note->descpos + offset);
473
0
}
474
475
static bool
476
elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
477
0
{
478
0
  if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
479
0
    {
480
0
      int pr_version = bfd_get_32 (abfd, note->descdata);
481
482
0
      if (pr_version != 1)
483
0
  return false;
484
485
0
      elf_tdata (abfd)->core->program
486
0
  = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
487
0
      elf_tdata (abfd)->core->command
488
0
  = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
489
0
    }
490
0
  else
491
0
    {
492
0
      switch (note->descsz)
493
0
  {
494
0
  default:
495
0
    return false;
496
497
0
  case 124:   /* Linux/i386 elf_prpsinfo.  */
498
0
    elf_tdata (abfd)->core->pid
499
0
      = bfd_get_32 (abfd, note->descdata + 12);
500
0
    elf_tdata (abfd)->core->program
501
0
      = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
502
0
    elf_tdata (abfd)->core->command
503
0
      = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
504
0
  }
505
0
    }
506
507
  /* Note that for some reason, a spurious space is tacked
508
     onto the end of the args in some (at least one anyway)
509
     implementations, so strip it off if it exists.  */
510
0
  {
511
0
    char *command = elf_tdata (abfd)->core->command;
512
0
    int n = strlen (command);
513
514
0
    if (0 < n && command[n - 1] == ' ')
515
0
      command[n - 1] = '\0';
516
0
  }
517
518
0
  return true;
519
0
}
520

521
/* Functions for the i386 ELF linker.
522
523
   In order to gain some understanding of code in this file without
524
   knowing all the intricate details of the linker, note the
525
   following:
526
527
   Functions named elf_i386_* are called by external routines, other
528
   functions are only called locally.  elf_i386_* functions appear
529
   in this file more or less in the order in which they are called
530
   from external routines.  eg. elf_i386_scan_relocs is called
531
   early in the link process, elf_i386_finish_dynamic_sections is
532
   one of the last functions.  */
533
534
/* The size in bytes of an entry in the lazy procedure linkage table.  */
535
536
#define LAZY_PLT_ENTRY_SIZE 16
537
538
/* The size in bytes of an entry in the non-lazy procedure linkage
539
   table.  */
540
541
#define NON_LAZY_PLT_ENTRY_SIZE 8
542
543
/* The first entry in an absolute lazy procedure linkage table looks
544
   like this.  See the SVR4 ABI i386 supplement to see how this works.
545
   Will be padded to LAZY_PLT_ENTRY_SIZE with lazy_plt->plt0_pad_byte.  */
546
547
static const bfd_byte elf_i386_lazy_plt0_entry[12] =
548
{
549
  0xff, 0x35, /* pushl contents of address */
550
  0, 0, 0, 0, /* replaced with address of .got + 4.  */
551
  0xff, 0x25, /* jmp indirect */
552
  0, 0, 0, 0  /* replaced with address of .got + 8.  */
553
};
554
555
/* Subsequent entries in an absolute lazy procedure linkage table look
556
   like this.  */
557
558
static const bfd_byte elf_i386_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
559
{
560
  0xff, 0x25, /* jmp indirect */
561
  0, 0, 0, 0, /* replaced with address of this symbol in .got.  */
562
  0x68,   /* pushl immediate */
563
  0, 0, 0, 0, /* replaced with offset into relocation table.  */
564
  0xe9,   /* jmp relative */
565
  0, 0, 0, 0  /* replaced with offset to start of .plt.  */
566
};
567
568
/* The first entry in a PIC lazy procedure linkage table look like
569
   this.  Will be padded to LAZY_PLT_ENTRY_SIZE with
570
   lazy_plt->plt0_pad_byte.  */
571
572
static const bfd_byte elf_i386_pic_lazy_plt0_entry[12] =
573
{
574
  0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
575
  0xff, 0xa3, 8, 0, 0, 0  /* jmp *8(%ebx) */
576
};
577
578
/* Subsequent entries in a PIC lazy procedure linkage table look like
579
   this.  */
580
581
static const bfd_byte elf_i386_pic_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
582
{
583
  0xff, 0xa3, /* jmp *offset(%ebx) */
584
  0, 0, 0, 0, /* replaced with offset of this symbol in .got.  */
585
  0x68,   /* pushl immediate */
586
  0, 0, 0, 0, /* replaced with offset into relocation table.  */
587
  0xe9,   /* jmp relative */
588
  0, 0, 0, 0  /* replaced with offset to start of .plt.  */
589
};
590
591
/* Entries in the non-lazy procedure linkage table look like this.  */
592
593
static const bfd_byte elf_i386_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
594
{
595
  0xff, 0x25, /* jmp indirect */
596
  0, 0, 0, 0, /* replaced with offset of this symbol in .got.  */
597
  0x66, 0x90  /* xchg %ax,%ax  */
598
};
599
600
/* Entries in the PIC non-lazy procedure linkage table look like
601
   this.  */
602
603
static const bfd_byte elf_i386_pic_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
604
{
605
  0xff, 0xa3, /* jmp *offset(%ebx)  */
606
  0, 0, 0, 0, /* replaced with offset of this symbol in .got.  */
607
  0x66, 0x90  /* xchg %ax,%ax  */
608
};
609
610
/* The first entry in an absolute IBT-enabled lazy procedure linkage
611
   table looks like this.  */
612
613
static const bfd_byte elf_i386_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
614
{
615
  0xff, 0x35, 0, 0, 0, 0, /* pushl GOT[1]       */
616
  0xff, 0x25, 0, 0, 0, 0, /* jmp *GOT[2]        */
617
  0x0f, 0x1f, 0x40, 0x00  /* nopl 0(%rax)       */
618
};
619
620
/* Subsequent entries for an absolute IBT-enabled lazy procedure linkage
621
   table look like this.  Subsequent entries for a PIC IBT-enabled lazy
622
   procedure linkage table are the same.  */
623
624
static const bfd_byte elf_i386_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
625
{
626
  0xf3, 0x0f, 0x1e, 0xfb, /* endbr32          */
627
  0x68, 0, 0, 0, 0,   /* pushl immediate        */
628
  0xe9, 0, 0, 0, 0,   /* jmp relative         */
629
  0x66, 0x90      /* xchg %ax,%ax         */
630
};
631
632
/* The first entry in a PIC IBT-enabled lazy procedure linkage table
633
   look like.  */
634
635
static const bfd_byte elf_i386_pic_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
636
{
637
  0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx)      */
638
  0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx)       */
639
  0x0f, 0x1f, 0x40, 0x00  /* nopl 0(%rax)       */
640
};
641
642
/* Entries for branches with IBT-enabled in the absolute non-lazey
643
   procedure linkage table look like this.  They have the same size
644
   as the lazy PLT entry.  */
645
646
static const bfd_byte elf_i386_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
647
{
648
  0xf3, 0x0f, 0x1e, 0xfb,      /* endbr32         */
649
  0xff, 0x25, 0, 0, 0, 0,      /* jmp *name@GOT       */
650
  0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
651
};
652
653
/* Entries for branches with IBT-enabled in the PIC non-lazey procedure
654
   linkage table look like this.  They have the same size as the lazy
655
   PLT entry.  */
656
657
static const bfd_byte elf_i386_pic_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
658
{
659
  0xf3, 0x0f, 0x1e, 0xfb,      /* endbr32         */
660
  0xff, 0xa3, 0, 0, 0, 0,      /* jmp *name@GOT(%ebx)   */
661
  0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
662
};
663
664
/* .eh_frame covering the lazy .plt section.  */
665
666
static const bfd_byte elf_i386_eh_frame_lazy_plt[] =
667
{
668
  PLT_CIE_LENGTH, 0, 0, 0,  /* CIE length */
669
  0, 0, 0, 0,     /* CIE ID */
670
  1,        /* CIE version */
671
  'z', 'R', 0,      /* Augmentation string */
672
  1,        /* Code alignment factor */
673
  0x7c,       /* Data alignment factor */
674
  8,        /* Return address column */
675
  1,        /* Augmentation size */
676
  DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
677
  DW_CFA_def_cfa, 4, 4,   /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
678
  DW_CFA_offset + 8, 1,   /* DW_CFA_offset: r8 (eip) at cfa-4 */
679
  DW_CFA_nop, DW_CFA_nop,
680
681
  PLT_FDE_LENGTH, 0, 0, 0,  /* FDE length */
682
  PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
683
  0, 0, 0, 0,     /* R_386_PC32 .plt goes here */
684
  0, 0, 0, 0,     /* .plt size goes here */
685
  0,        /* Augmentation size */
686
  DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
687
  DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
688
  DW_CFA_def_cfa_offset, 12,  /* DW_CFA_def_cfa_offset: 12 */
689
  DW_CFA_advance_loc + 10,  /* DW_CFA_advance_loc: 10 to __PLT__+16 */
690
  DW_CFA_def_cfa_expression,  /* DW_CFA_def_cfa_expression */
691
  11,       /* Block length */
692
  DW_OP_breg4, 4,   /* DW_OP_breg4 (esp): 4 */
693
  DW_OP_breg8, 0,   /* DW_OP_breg8 (eip): 0 */
694
  DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
695
  DW_OP_lit2, DW_OP_shl, DW_OP_plus,
696
  DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
697
};
698
699
/* .eh_frame covering the lazy .plt section with IBT-enabled.  */
700
701
static const bfd_byte elf_i386_eh_frame_lazy_ibt_plt[] =
702
{
703
  PLT_CIE_LENGTH, 0, 0, 0,  /* CIE length */
704
  0, 0, 0, 0,     /* CIE ID */
705
  1,        /* CIE version */
706
  'z', 'R', 0,      /* Augmentation string */
707
  1,        /* Code alignment factor */
708
  0x7c,       /* Data alignment factor */
709
  8,        /* Return address column */
710
  1,        /* Augmentation size */
711
  DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
712
  DW_CFA_def_cfa, 4, 4,   /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
713
  DW_CFA_offset + 8, 1,   /* DW_CFA_offset: r8 (eip) at cfa-4 */
714
  DW_CFA_nop, DW_CFA_nop,
715
716
  PLT_FDE_LENGTH, 0, 0, 0,  /* FDE length */
717
  PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
718
  0, 0, 0, 0,     /* R_386_PC32 .plt goes here */
719
  0, 0, 0, 0,     /* .plt size goes here */
720
  0,        /* Augmentation size */
721
  DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
722
  DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
723
  DW_CFA_def_cfa_offset, 12,  /* DW_CFA_def_cfa_offset: 12 */
724
  DW_CFA_advance_loc + 10,  /* DW_CFA_advance_loc: 10 to __PLT__+16 */
725
  DW_CFA_def_cfa_expression,  /* DW_CFA_def_cfa_expression */
726
  11,       /* Block length */
727
  DW_OP_breg4, 4,   /* DW_OP_breg4 (esp): 4 */
728
  DW_OP_breg8, 0,   /* DW_OP_breg8 (eip): 0 */
729
  DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
730
  DW_OP_lit2, DW_OP_shl, DW_OP_plus,
731
  DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
732
};
733
734
/* .eh_frame covering the non-lazy .plt section.  */
735
736
static const bfd_byte elf_i386_eh_frame_non_lazy_plt[] =
737
{
738
#define PLT_GOT_FDE_LENGTH    16
739
  PLT_CIE_LENGTH, 0, 0, 0,  /* CIE length */
740
  0, 0, 0, 0,     /* CIE ID */
741
  1,        /* CIE version */
742
  'z', 'R', 0,      /* Augmentation string */
743
  1,        /* Code alignment factor */
744
  0x7c,       /* Data alignment factor */
745
  8,        /* Return address column */
746
  1,        /* Augmentation size */
747
  DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
748
  DW_CFA_def_cfa, 4, 4,   /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
749
  DW_CFA_offset + 8, 1,   /* DW_CFA_offset: r8 (eip) at cfa-4 */
750
  DW_CFA_nop, DW_CFA_nop,
751
752
  PLT_GOT_FDE_LENGTH, 0, 0, 0,  /* FDE length */
753
  PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
754
  0, 0, 0, 0,     /* the start of non-lazy .plt goes here */
755
  0, 0, 0, 0,     /* non-lazy .plt size goes here */
756
  0,        /* Augmentation size */
757
  DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
758
};
759
760
/* These are the standard parameters.  */
761
static const struct elf_x86_lazy_plt_layout elf_i386_lazy_plt =
762
  {
763
    elf_i386_lazy_plt0_entry,   /* plt0_entry */
764
    sizeof (elf_i386_lazy_plt0_entry),  /* plt0_entry_size */
765
    elf_i386_lazy_plt_entry,    /* plt_entry */
766
    LAZY_PLT_ENTRY_SIZE,    /* plt_entry_size */
767
    NULL,       /* plt_tlsdesc_entry */
768
    0,          /* plt_tlsdesc_entry_size*/
769
    0,          /* plt_tlsdesc_got1_offset */
770
    0,          /* plt_tlsdesc_got2_offset */
771
    0,          /* plt_tlsdesc_got1_insn_end */
772
    0,          /* plt_tlsdesc_got2_insn_end */
773
    2,          /* plt0_got1_offset */
774
    8,          /* plt0_got2_offset */
775
    0,          /* plt0_got2_insn_end */
776
    2,          /* plt_got_offset */
777
    7,          /* plt_reloc_offset */
778
    12,         /* plt_plt_offset */
779
    0,          /* plt_got_insn_size */
780
    0,          /* plt_plt_insn_end */
781
    6,          /* plt_lazy_offset */
782
    elf_i386_pic_lazy_plt0_entry, /* pic_plt0_entry */
783
    elf_i386_pic_lazy_plt_entry,  /* pic_plt_entry */
784
    elf_i386_eh_frame_lazy_plt,   /* eh_frame_plt */
785
    sizeof (elf_i386_eh_frame_lazy_plt) /* eh_frame_plt_size */
786
  };
787
788
static const struct elf_x86_non_lazy_plt_layout elf_i386_non_lazy_plt =
789
  {
790
    elf_i386_non_lazy_plt_entry,  /* plt_entry */
791
    elf_i386_pic_non_lazy_plt_entry,  /* pic_plt_entry */
792
    NON_LAZY_PLT_ENTRY_SIZE,    /* plt_entry_size */
793
    2,          /* plt_got_offset */
794
    0,          /* plt_got_insn_size */
795
    elf_i386_eh_frame_non_lazy_plt, /* eh_frame_plt */
796
    sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
797
  };
798
799
static const struct elf_x86_lazy_plt_layout elf_i386_lazy_ibt_plt =
800
  {
801
    elf_i386_lazy_ibt_plt0_entry, /* plt0_entry */
802
    sizeof (elf_i386_lazy_ibt_plt0_entry), /* plt0_entry_size */
803
    elf_i386_lazy_ibt_plt_entry,  /* plt_entry */
804
    LAZY_PLT_ENTRY_SIZE,    /* plt_entry_size */
805
    NULL,       /* plt_tlsdesc_entry */
806
    0,          /* plt_tlsdesc_entry_size*/
807
    0,          /* plt_tlsdesc_got1_offset */
808
    0,          /* plt_tlsdesc_got2_offset */
809
    0,          /* plt_tlsdesc_got1_insn_end */
810
    0,          /* plt_tlsdesc_got2_insn_end */
811
    2,          /* plt0_got1_offset */
812
    8,          /* plt0_got2_offset */
813
    0,          /* plt0_got2_insn_end */
814
    4+2,        /* plt_got_offset */
815
    4+1,        /* plt_reloc_offset */
816
    4+6,        /* plt_plt_offset */
817
    0,          /* plt_got_insn_size */
818
    0,          /* plt_plt_insn_end */
819
    0,          /* plt_lazy_offset */
820
    elf_i386_pic_lazy_ibt_plt0_entry, /* pic_plt0_entry */
821
    elf_i386_lazy_ibt_plt_entry,  /* pic_plt_entry */
822
    elf_i386_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
823
    sizeof (elf_i386_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
824
  };
825
826
static const struct elf_x86_non_lazy_plt_layout elf_i386_non_lazy_ibt_plt =
827
  {
828
    elf_i386_non_lazy_ibt_plt_entry,  /* plt_entry */
829
    elf_i386_pic_non_lazy_ibt_plt_entry,/* pic_plt_entry */
830
    LAZY_PLT_ENTRY_SIZE,    /* plt_entry_size */
831
    4+2,        /* plt_got_offset */
832
    0,          /* plt_got_insn_size */
833
    elf_i386_eh_frame_non_lazy_plt, /* eh_frame_plt */
834
    sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
835
  };
836

837
838
/* On VxWorks, the .rel.plt.unloaded section has absolute relocations
839
   for the PLTResolve stub and then for each PLT entry.  */
840
0
#define PLTRESOLVE_RELOCS_SHLIB 0
841
0
#define PLTRESOLVE_RELOCS 2
842
0
#define PLT_NON_JUMP_SLOT_RELOCS 2
843
844
/* Return TRUE if the TLS access code sequence support transition
845
   from R_TYPE.  */
846
847
static enum elf_x86_tls_error_type
848
elf_i386_check_tls_transition (asection *sec,
849
             bfd_byte *contents,
850
             Elf_Internal_Shdr *symtab_hdr,
851
             struct elf_link_hash_entry **sym_hashes,
852
             unsigned int r_type,
853
             const Elf_Internal_Rela *rel,
854
             const Elf_Internal_Rela *relend)
855
0
{
856
0
  unsigned int val, type, reg;
857
0
  unsigned long r_symndx;
858
0
  struct elf_link_hash_entry *h;
859
0
  bfd_vma offset;
860
0
  bfd_byte *call;
861
0
  bool indirect_call;
862
863
0
  offset = rel->r_offset;
864
0
  switch (r_type)
865
0
    {
866
0
    case R_386_TLS_GD:
867
0
    case R_386_TLS_LDM:
868
0
      if (offset < 2 || (rel + 1) >= relend)
869
0
  return elf_x86_tls_error_yes;
870
871
0
      indirect_call = false;
872
0
      call = contents + offset + 4;
873
0
      val = *(call - 5);
874
0
      type = *(call - 6);
875
0
      if (r_type == R_386_TLS_GD)
876
0
  {
877
    /* Check transition from GD access model.  Only
878
    leal foo@tlsgd(,%ebx,1), %eax
879
    call ___tls_get_addr@PLT
880
       or
881
    leal foo@tlsgd(%ebx) %eax
882
    call ___tls_get_addr@PLT
883
    nop
884
       or
885
    leal foo@tlsgd(%reg), %eax
886
    call *___tls_get_addr@GOT(%reg)
887
    which may be converted to
888
    addr32 call ___tls_get_addr
889
       can transit to different access model.  */
890
0
    if ((offset + 10) > sec->size
891
0
        || (type != 0x8d && type != 0x04))
892
0
      return elf_x86_tls_error_yes;
893
894
0
    if (type == 0x04)
895
0
      {
896
        /* leal foo@tlsgd(,%ebx,1), %eax
897
     call ___tls_get_addr@PLT  */
898
0
        if (offset < 3)
899
0
    return elf_x86_tls_error_yes;
900
901
0
        if (*(call - 7) != 0x8d
902
0
      || val != 0x1d
903
0
      || call[0] != 0xe8)
904
0
    return elf_x86_tls_error_yes;
905
0
      }
906
0
    else
907
0
      {
908
        /* This must be
909
      leal foo@tlsgd(%ebx), %eax
910
      call ___tls_get_addr@PLT
911
      nop
912
     or
913
      leal foo@tlsgd(%reg), %eax
914
      call *___tls_get_addr@GOT(%reg)
915
      which may be converted to
916
      addr32 call ___tls_get_addr
917
918
     %eax can't be used as the GOT base register since it
919
     is used to pass parameter to ___tls_get_addr.  */
920
0
        reg = val & 7;
921
0
        if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
922
0
    return elf_x86_tls_error_yes;
923
924
0
        indirect_call = call[0] == 0xff;
925
0
        if (!(reg == 3 && call[0] == 0xe8 && call[5] == 0x90)
926
0
      && !(call[0] == 0x67 && call[1] == 0xe8)
927
0
      && !(indirect_call
928
0
           && (call[1] & 0xf8) == 0x90
929
0
           && (call[1] & 0x7) == reg))
930
0
    return elf_x86_tls_error_yes;
931
0
      }
932
0
  }
933
0
      else
934
0
  {
935
    /* Check transition from LD access model.  Only
936
    leal foo@tlsldm(%ebx), %eax
937
    call ___tls_get_addr@PLT
938
       or
939
    leal foo@tlsldm(%reg), %eax
940
    call *___tls_get_addr@GOT(%reg)
941
    which may be converted to
942
    addr32 call ___tls_get_addr
943
       can transit to different access model.  */
944
0
    if (type != 0x8d || (offset + 9) > sec->size)
945
0
      return elf_x86_tls_error_yes;
946
947
    /* %eax can't be used as the GOT base register since it is
948
       used to pass parameter to ___tls_get_addr.  */
949
0
    reg = val & 7;
950
0
    if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
951
0
      return elf_x86_tls_error_yes;
952
953
0
    indirect_call = call[0] == 0xff;
954
0
    if (!(reg == 3 && call[0] == 0xe8)
955
0
        && !(call[0] == 0x67 && call[1] == 0xe8)
956
0
        && !(indirect_call
957
0
       && (call[1] & 0xf8) == 0x90
958
0
       && (call[1] & 0x7) == reg))
959
0
      return elf_x86_tls_error_yes;
960
0
  }
961
962
0
      r_symndx = ELF32_R_SYM (rel[1].r_info);
963
0
      if (r_symndx < symtab_hdr->sh_info)
964
0
  return elf_x86_tls_error_yes;
965
966
0
      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
967
0
      if (h == NULL
968
0
    || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
969
0
  return elf_x86_tls_error_yes;
970
0
      else if (indirect_call)
971
0
  return ((ELF32_R_TYPE (rel[1].r_info) == R_386_GOT32X
972
0
     || ELF32_R_TYPE (rel[1].r_info) == R_386_GOT32)
973
0
    ? elf_x86_tls_error_none
974
0
    : elf_x86_tls_error_yes);
975
0
      else
976
0
  return ((ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
977
0
    || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32)
978
0
    ? elf_x86_tls_error_none
979
0
    : elf_x86_tls_error_yes);
980
981
0
    case R_386_TLS_IE:
982
      /* Check transition from IE access model:
983
    movl foo@indntpoff, %eax
984
    movl foo@indntpoff, %reg
985
    addl foo@indntpoff, %reg
986
       */
987
988
0
      if (offset < 1 || (offset + 4) > sec->size)
989
0
  return elf_x86_tls_error_yes;
990
991
      /* Check "movl foo@indntpoff, %eax" first.  */
992
0
      val = bfd_get_8 (abfd, contents + offset - 1);
993
0
      if (val == 0xa1)
994
0
  return elf_x86_tls_error_none;
995
996
0
      if (offset < 2)
997
0
  return elf_x86_tls_error_yes;
998
999
      /* Check movl|addl foo@indntpoff, %reg.   */
1000
0
      type = bfd_get_8 (abfd, contents + offset - 2);
1001
0
      if (type != 0x8b && type != 0x03)
1002
0
  return elf_x86_tls_error_add_mov;
1003
0
      return ((val & 0xc7) == 0x05
1004
0
        ? elf_x86_tls_error_none
1005
0
        : elf_x86_tls_error_yes);
1006
1007
0
    case R_386_TLS_GOTIE:
1008
0
    case R_386_TLS_IE_32:
1009
      /* Check transition from {IE_32,GOTIE} access model:
1010
    subl foo@{tpoff,gontoff}(%reg1), %reg2
1011
    movl foo@{tpoff,gontoff}(%reg1), %reg2
1012
    addl foo@{tpoff,gontoff}(%reg1), %reg2
1013
       */
1014
1015
0
      if (offset < 2 || (offset + 4) > sec->size)
1016
0
  return elf_x86_tls_error_yes;
1017
1018
0
      val = bfd_get_8 (abfd, contents + offset - 1);
1019
0
      if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1020
0
  return elf_x86_tls_error_yes;
1021
1022
0
      type = bfd_get_8 (abfd, contents + offset - 2);
1023
0
      return (type == 0x8b || type == 0x2b || type == 0x03
1024
0
        ? elf_x86_tls_error_none
1025
0
        : elf_x86_tls_error_add_sub_mov);
1026
1027
0
    case R_386_TLS_GOTDESC:
1028
      /* Check transition from GDesc access model:
1029
    leal x@tlsdesc(%ebx), %eax
1030
1031
   Make sure it's a leal adding ebx to a 32-bit offset
1032
   into any register, although it's probably almost always
1033
   going to be eax.  */
1034
1035
0
      if (offset < 2 || (offset + 4) > sec->size)
1036
0
  return elf_x86_tls_error_yes;
1037
1038
0
      if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1039
0
  return elf_x86_tls_error_lea;
1040
1041
0
      val = bfd_get_8 (abfd, contents + offset - 1);
1042
0
      return ((val & 0xc7) == 0x83
1043
0
        ? elf_x86_tls_error_none
1044
0
        : elf_x86_tls_error_yes);
1045
1046
0
    case R_386_TLS_DESC_CALL:
1047
      /* It has been checked in elf_i386_tls_transition.  */
1048
0
      return elf_x86_tls_error_none;
1049
1050
0
    default:
1051
0
      abort ();
1052
0
    }
1053
0
}
1054
1055
/* Return TRUE if the TLS access transition is OK or no transition
1056
   will be performed.  Update R_TYPE if there is a transition.  */
1057
1058
static bool
1059
elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1060
       asection *sec, bfd_byte *contents,
1061
       Elf_Internal_Shdr *symtab_hdr,
1062
       struct elf_link_hash_entry **sym_hashes,
1063
       unsigned int *r_type, int tls_type,
1064
       const Elf_Internal_Rela *rel,
1065
       const Elf_Internal_Rela *relend,
1066
       struct elf_link_hash_entry *h,
1067
       const Elf_Internal_Sym *sym,
1068
       bool from_relocate_section)
1069
0
{
1070
0
  unsigned int from_type = *r_type;
1071
0
  unsigned int to_type = from_type;
1072
0
  bool check = true;
1073
0
  unsigned int to_le_type, to_ie_type;
1074
0
  bfd_vma offset;
1075
0
  bfd_byte *call;
1076
1077
  /* Skip TLS transition for functions.  */
1078
0
  if (h != NULL
1079
0
      && (h->type == STT_FUNC
1080
0
    || h->type == STT_GNU_IFUNC))
1081
0
    return true;
1082
1083
0
  if (get_elf_backend_data (abfd)->target_os == is_solaris)
1084
0
    {
1085
      /* NB: Solaris only supports R_386_TLS_LE and R_386_TLS_IE.  */
1086
0
      to_le_type = R_386_TLS_LE;
1087
0
      to_ie_type = R_386_TLS_IE;
1088
0
    }
1089
0
  else
1090
0
    {
1091
0
      to_le_type = R_386_TLS_LE_32;
1092
0
      to_ie_type = R_386_TLS_IE_32;
1093
0
    }
1094
1095
0
  switch (from_type)
1096
0
    {
1097
0
    case R_386_TLS_DESC_CALL:
1098
      /* Check valid GDesc call:
1099
    call *x@tlscall(%eax)
1100
       */
1101
0
      offset = rel->r_offset;
1102
0
      call = NULL;
1103
0
      if (offset + 2 <= sec->size)
1104
0
  {
1105
    /* Make sure that it's a call *x@tlscall(%eax).  */
1106
0
    call = contents + offset;
1107
0
    if (call[0] != 0xff || call[1] != 0x10)
1108
0
      call = NULL;
1109
0
  }
1110
1111
0
      if (call == NULL)
1112
0
  {
1113
0
    _bfd_x86_elf_link_report_tls_transition_error
1114
0
      (info, abfd, sec, symtab_hdr, h, sym, rel,
1115
0
       "R_386_TLS_DESC_CALL", NULL,
1116
0
       elf_x86_tls_error_indirect_call);
1117
1118
0
    return false;
1119
0
  }
1120
1121
      /* Fall through.  */
1122
1123
0
    case R_386_TLS_GD:
1124
0
    case R_386_TLS_GOTDESC:
1125
0
    case R_386_TLS_IE_32:
1126
0
    case R_386_TLS_IE:
1127
0
    case R_386_TLS_GOTIE:
1128
0
      if (bfd_link_executable (info))
1129
0
  {
1130
0
    if (h == NULL)
1131
0
      to_type = to_le_type;
1132
0
    else if (from_type != R_386_TLS_IE
1133
0
       && from_type != R_386_TLS_GOTIE)
1134
0
      to_type = to_ie_type;
1135
0
  }
1136
1137
      /* When we are called from elf_i386_relocate_section, there may
1138
   be additional transitions based on TLS_TYPE.  */
1139
0
      if (from_relocate_section)
1140
0
  {
1141
0
    unsigned int new_to_type = to_type;
1142
1143
0
    if (TLS_TRANSITION_IE_TO_LE_P (info, h, tls_type))
1144
0
      new_to_type = to_le_type;
1145
1146
0
    if (to_type == R_386_TLS_GD
1147
0
        || to_type == R_386_TLS_GOTDESC
1148
0
        || to_type == R_386_TLS_DESC_CALL)
1149
0
      {
1150
0
        if (tls_type == GOT_TLS_IE_POS)
1151
0
    new_to_type = R_386_TLS_GOTIE;
1152
0
        else if (tls_type & GOT_TLS_IE)
1153
0
    new_to_type = to_ie_type;
1154
0
      }
1155
1156
    /* We checked the transition before when we were called from
1157
       elf_i386_scan_relocs.  We only want to check the new
1158
       transition which hasn't been checked before.  */
1159
0
    check = new_to_type != to_type && from_type == to_type;
1160
0
    to_type = new_to_type;
1161
0
  }
1162
1163
0
      break;
1164
1165
0
    case R_386_TLS_LDM:
1166
0
      if (bfd_link_executable (info))
1167
0
  to_type = to_le_type;
1168
0
      break;
1169
1170
0
    default:
1171
0
      return true;
1172
0
    }
1173
1174
0
  if ((elf_section_type (sec) != SHT_PROGBITS
1175
0
       || (sec->flags & SEC_CODE) == 0))
1176
0
    {
1177
0
      reloc_howto_type *howto = elf_i386_rtype_to_howto (from_type);
1178
0
      _bfd_x86_elf_link_report_error (abfd, sec, symtab_hdr, h, sym,
1179
0
              howto, elf_x86_error_tls);
1180
0
      return false;
1181
0
    }
1182
1183
  /* Return TRUE if there is no transition.  */
1184
0
  if (from_type == to_type)
1185
0
    return true;
1186
1187
  /* Check if the transition can be performed.  */
1188
0
  enum elf_x86_tls_error_type tls_error;
1189
0
  if (check
1190
0
      && ((tls_error = elf_i386_check_tls_transition (sec, contents,
1191
0
                  symtab_hdr,
1192
0
                  sym_hashes,
1193
0
                  from_type, rel,
1194
0
                  relend))
1195
0
    != elf_x86_tls_error_none))
1196
0
    {
1197
0
      reloc_howto_type *from, *to;
1198
1199
0
      from = elf_i386_rtype_to_howto (from_type);
1200
0
      to = elf_i386_rtype_to_howto (to_type);
1201
1202
0
      _bfd_x86_elf_link_report_tls_transition_error
1203
0
  (info, abfd, sec, symtab_hdr, h, sym, rel, from->name,
1204
0
   to->name, tls_error);
1205
1206
0
      return false;
1207
0
    }
1208
1209
0
  *r_type = to_type;
1210
0
  return true;
1211
0
}
1212
1213
/* With the local symbol, foo, we convert
1214
   mov foo@GOT[(%reg1)], %reg2
1215
   to
1216
   lea foo[@GOTOFF(%reg1)], %reg2
1217
   and convert
1218
   call/jmp *foo@GOT[(%reg)]
1219
   to
1220
   nop call foo/jmp foo nop
1221
   When PIC is false, convert
1222
   test %reg1, foo@GOT[(%reg2)]
1223
   to
1224
   test $foo, %reg1
1225
   and convert
1226
   push foo@GOT[(%reg)]
1227
   to
1228
   push $foo
1229
   and convert
1230
   binop foo@GOT[(%reg1)], %reg2
1231
   to
1232
   binop $foo, %reg2
1233
   where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1234
   instructions.  */
1235
1236
static
1237
bool
1238
elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
1239
           bfd_byte *contents,
1240
           unsigned int *r_type_p,
1241
           Elf_Internal_Rela *irel,
1242
           struct elf_link_hash_entry *h,
1243
           bool *converted,
1244
           struct bfd_link_info *link_info)
1245
0
{
1246
0
  struct elf_x86_link_hash_table *htab;
1247
0
  unsigned int opcode;
1248
0
  unsigned int modrm;
1249
0
  bool baseless;
1250
0
  Elf_Internal_Sym *isym;
1251
0
  unsigned int addend;
1252
0
  unsigned int nop;
1253
0
  bfd_vma nop_offset;
1254
0
  bool is_pic, is_branch = false;
1255
0
  bool to_reloc_32;
1256
0
  bool abs_symbol;
1257
0
  unsigned int r_type;
1258
0
  unsigned int r_symndx;
1259
0
  bfd_vma roff = irel->r_offset;
1260
0
  bool local_ref;
1261
0
  struct elf_x86_link_hash_entry *eh;
1262
1263
0
  if (roff < 2)
1264
0
    return true;
1265
1266
  /* Addend for R_386_GOT32X relocations must be 0.  */
1267
0
  addend = bfd_get_32 (abfd, contents + roff);
1268
0
  if (addend != 0)
1269
0
    return true;
1270
1271
0
  htab = elf_x86_hash_table (link_info, I386_ELF_DATA);
1272
0
  if (htab == NULL || ! is_x86_elf (abfd, htab))
1273
0
    {
1274
0
      bfd_set_error (bfd_error_wrong_format);
1275
0
      return false;
1276
0
    }
1277
1278
0
  is_pic = bfd_link_pic (link_info);
1279
1280
0
  r_type = *r_type_p;
1281
0
  r_symndx = ELF32_R_SYM (irel->r_info);
1282
1283
0
  modrm = bfd_get_8 (abfd, contents + roff - 1);
1284
0
  baseless = (modrm & 0xc7) == 0x5;
1285
1286
0
  if (h)
1287
0
    {
1288
      /* NB: Also set linker_def via SYMBOL_REFERENCES_LOCAL_P.  */
1289
0
      local_ref = SYMBOL_REFERENCES_LOCAL_P (link_info, h);
1290
0
      isym = NULL;
1291
0
      abs_symbol = ABS_SYMBOL_P (h);
1292
0
    }
1293
0
  else
1294
0
    {
1295
0
      local_ref = true;
1296
0
      isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd,
1297
0
            r_symndx);
1298
0
      abs_symbol = isym->st_shndx == SHN_ABS;
1299
0
    }
1300
1301
0
  if (baseless && is_pic)
1302
0
    {
1303
      /* For PIC, disallow R_386_GOT32X without a base register
1304
   since we don't know what the GOT base is.  */
1305
0
      const char *name;
1306
1307
0
      if (h == NULL)
1308
0
  name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1309
0
      else
1310
0
  name = h->root.root.string;
1311
1312
0
      _bfd_error_handler
1313
  /* xgettext:c-format */
1314
0
  (_("%pB: direct GOT relocation R_386_GOT32X against `%s' without base"
1315
0
     " register can not be used when making a shared object"),
1316
0
   abfd, name);
1317
0
      return false;
1318
0
    }
1319
1320
0
  opcode = bfd_get_8 (abfd, contents + roff - 2);
1321
1322
0
  if (opcode == 0xff)
1323
0
    {
1324
0
      switch (modrm & 0x38)
1325
0
  {
1326
0
  case 0x10: /* CALL */
1327
0
  case 0x20: /* JMP */
1328
0
    is_branch = true;
1329
0
    break;
1330
1331
0
  case 0x30: /* PUSH */
1332
0
    break;
1333
1334
0
  default:
1335
0
    return true;
1336
0
  }
1337
0
    }
1338
1339
  /* Convert to R_386_32 if PIC is false (if PIC is true we already know
1340
     there is a base register).  */
1341
0
  to_reloc_32 = !is_pic;
1342
1343
0
  eh = elf_x86_hash_entry (h);
1344
1345
  /* Try to convert R_386_GOT32X.  Get the symbol referred to by the
1346
     reloc.  */
1347
0
  if (h == NULL)
1348
0
    {
1349
0
      if (is_branch)
1350
  /* Convert "call/jmp *foo@GOT[(%reg)]".  */
1351
0
  goto convert_branch;
1352
0
      else
1353
  /* Convert "mov foo@GOT[(%reg1)], %reg2",
1354
     "test %reg1, foo@GOT(%reg2)" and
1355
     "binop foo@GOT[(%reg1)], %reg2". */
1356
0
  goto convert_load;
1357
0
    }
1358
1359
  /* Undefined weak symbol is only bound locally in executable
1360
     and its reference is resolved as 0.  */
1361
0
  if (h->root.type == bfd_link_hash_undefweak
1362
0
      && !eh->linker_def
1363
0
      && local_ref)
1364
0
    {
1365
0
      if (is_branch)
1366
0
  {
1367
    /* No direct branch to 0 for PIC.  */
1368
0
    if (is_pic)
1369
0
      return true;
1370
0
    else
1371
0
      goto convert_branch;
1372
0
  }
1373
0
      else
1374
0
  {
1375
    /* We can convert load of address 0 to R_386_32.  */
1376
0
    to_reloc_32 = true;
1377
0
    goto convert_load;
1378
0
  }
1379
0
    }
1380
1381
0
  if (is_branch)
1382
0
    {
1383
      /* We have "call/jmp *foo@GOT[(%reg)]".  */
1384
0
      if ((h->root.type == bfd_link_hash_defined
1385
0
     || h->root.type == bfd_link_hash_defweak)
1386
0
    && local_ref)
1387
0
  {
1388
    /* The function is locally defined.   */
1389
0
  convert_branch:
1390
    /* Convert R_386_GOT32X to R_386_PC32.  */
1391
0
    if (modrm == 0x15 || (modrm & 0xf8) == 0x90)
1392
0
      {
1393
        /* Convert to "nop call foo".  ADDR_PREFIX_OPCODE
1394
     is a nop prefix.  */
1395
0
        modrm = 0xe8;
1396
        /* To support TLS optimization, always use addr32 prefix
1397
     for "call *___tls_get_addr@GOT(%reg)".  */
1398
0
        if (eh && eh->tls_get_addr)
1399
0
    {
1400
0
      nop = 0x67;
1401
0
      nop_offset = irel->r_offset - 2;
1402
0
    }
1403
0
        else
1404
0
    {
1405
0
      nop = htab->params->call_nop_byte;
1406
0
      if (htab->params->call_nop_as_suffix)
1407
0
        {
1408
0
          nop_offset = roff + 3;
1409
0
          irel->r_offset -= 1;
1410
0
        }
1411
0
      else
1412
0
        nop_offset = roff - 2;
1413
0
    }
1414
0
      }
1415
0
    else
1416
0
      {
1417
        /* Convert to "jmp foo nop".  */
1418
0
        modrm = 0xe9;
1419
0
        nop = NOP_OPCODE;
1420
0
        nop_offset = roff + 3;
1421
0
        irel->r_offset -= 1;
1422
0
      }
1423
1424
0
    bfd_put_8 (abfd, nop, contents + nop_offset);
1425
0
    bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1426
    /* When converting to PC-relative relocation, we
1427
       need to adjust addend by -4.  */
1428
0
    bfd_put_32 (abfd, -4, contents + irel->r_offset);
1429
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_386_PC32);
1430
0
    *r_type_p = R_386_PC32;
1431
0
    *converted = true;
1432
0
  }
1433
0
    }
1434
0
  else
1435
0
    {
1436
      /* We have "mov foo@GOT[(%re1g)], %reg2",
1437
   "test %reg1, foo@GOT(%reg2)",
1438
   "push foo@GOT[(%reg)]", or
1439
   "binop foo@GOT[(%reg1)], %reg2".
1440
1441
   Avoid optimizing _DYNAMIC since ld.so may use its
1442
   link-time address.  */
1443
0
      if (h == htab->elf.hdynamic)
1444
0
  return true;
1445
1446
      /* def_regular is set by an assignment in a linker script in
1447
   bfd_elf_record_link_assignment.  start_stop is set on
1448
   __start_SECNAME/__stop_SECNAME which mark section SECNAME.  */
1449
0
      if (h->start_stop
1450
0
    || eh->linker_def
1451
0
    || ((h->def_regular
1452
0
         || h->root.type == bfd_link_hash_defined
1453
0
         || h->root.type == bfd_link_hash_defweak)
1454
0
        && local_ref))
1455
0
  {
1456
0
  convert_load:
1457
0
    if (opcode == 0x8b)
1458
0
      {
1459
0
        if (abs_symbol && local_ref)
1460
0
    to_reloc_32 = true;
1461
1462
0
        if (to_reloc_32)
1463
0
    {
1464
      /* Convert "mov foo@GOT[(%reg1)], %reg2" to
1465
         "mov $foo, %reg2" with R_386_32.  */
1466
0
      r_type = R_386_32;
1467
0
      modrm = 0xc0 | (modrm & 0x38) >> 3;
1468
0
      bfd_put_8 (abfd, modrm, contents + roff - 1);
1469
0
      opcode = 0xc7;
1470
0
    }
1471
0
        else
1472
0
    {
1473
      /* Convert "mov foo@GOT(%reg1), %reg2" to
1474
         "lea foo@GOTOFF(%reg1), %reg2".  */
1475
0
      r_type = R_386_GOTOFF;
1476
0
      opcode = 0x8d;
1477
0
    }
1478
0
      }
1479
0
    else
1480
0
      {
1481
        /* Only R_386_32 is supported.  */
1482
0
        if (!to_reloc_32)
1483
0
    return true;
1484
1485
0
        if (opcode == 0x85)
1486
0
    {
1487
      /* Convert "test %reg1, foo@GOT(%reg2)" to
1488
         "test $foo, %reg1".  */
1489
0
      modrm = 0xc0 | (modrm & 0x38) >> 3;
1490
0
      opcode = 0xf7;
1491
0
    }
1492
0
        else if ((opcode | 0x38) == 0x3b)
1493
0
    {
1494
      /* Convert "binop foo@GOT(%reg1), %reg2" to
1495
         "binop $foo, %reg2".  */
1496
0
      modrm = 0xc0 | ((modrm & 0x38) >> 3) | (opcode & 0x38);
1497
0
      opcode = 0x81;
1498
0
    }
1499
0
        else if (opcode == 0xff)
1500
0
    {
1501
      /* Convert "push foo@GOT(%reg)" to
1502
         "push $foo".  */
1503
0
      modrm = 0x68; /* Really the opcode.  */
1504
0
      opcode = 0x2e; /* Really a meaningless %cs: prefix.  */
1505
0
    }
1506
0
        else
1507
0
    return true;
1508
1509
0
        bfd_put_8 (abfd, modrm, contents + roff - 1);
1510
0
        r_type = R_386_32;
1511
0
      }
1512
1513
0
    bfd_put_8 (abfd, opcode, contents + roff - 2);
1514
0
    irel->r_info = ELF32_R_INFO (r_symndx, r_type);
1515
0
    *r_type_p = r_type;
1516
0
    *converted = true;
1517
0
  }
1518
0
    }
1519
1520
0
  return true;
1521
0
}
1522
1523
/* Look through the relocs for a section during the first phase, and
1524
   calculate needed space in the global offset table, and procedure
1525
   linkage table.  */
1526
1527
static bool
1528
elf_i386_scan_relocs (bfd *abfd,
1529
          struct bfd_link_info *info,
1530
          asection *sec,
1531
          const Elf_Internal_Rela *relocs)
1532
0
{
1533
0
  struct elf_x86_link_hash_table *htab;
1534
0
  Elf_Internal_Shdr *symtab_hdr;
1535
0
  struct elf_link_hash_entry **sym_hashes;
1536
0
  const Elf_Internal_Rela *rel;
1537
0
  const Elf_Internal_Rela *rel_end;
1538
0
  bfd_byte *contents;
1539
0
  bool converted;
1540
1541
0
  if (bfd_link_relocatable (info))
1542
0
    return true;
1543
1544
0
  htab = elf_x86_hash_table (info, I386_ELF_DATA);
1545
0
  if (htab == NULL)
1546
0
    {
1547
0
      sec->check_relocs_failed = 1;
1548
0
      return false;
1549
0
    }
1550
1551
0
  BFD_ASSERT (is_x86_elf (abfd, htab));
1552
1553
  /* Get the section contents.  */
1554
0
  if (elf_section_data (sec)->this_hdr.contents != NULL)
1555
0
    contents = elf_section_data (sec)->this_hdr.contents;
1556
0
  else if (!_bfd_elf_mmap_section_contents (abfd, sec, &contents))
1557
0
    {
1558
0
      sec->check_relocs_failed = 1;
1559
0
      return false;
1560
0
    }
1561
1562
0
  symtab_hdr = &elf_symtab_hdr (abfd);
1563
0
  sym_hashes = elf_sym_hashes (abfd);
1564
1565
0
  converted = false;
1566
1567
0
  rel_end = relocs + sec->reloc_count;
1568
0
  for (rel = relocs; rel < rel_end; rel++)
1569
0
    {
1570
0
      unsigned int r_type;
1571
0
      unsigned int r_symndx;
1572
0
      struct elf_link_hash_entry *h;
1573
0
      struct elf_x86_link_hash_entry *eh;
1574
0
      const Elf_Internal_Sym *isym;
1575
0
      const char *name;
1576
0
      bool size_reloc;
1577
0
      bool no_dynreloc;
1578
0
      reloc_howto_type *howto;
1579
1580
0
      r_symndx = ELF32_R_SYM (rel->r_info);
1581
0
      r_type = ELF32_R_TYPE (rel->r_info);
1582
1583
      /* Don't check R_386_NONE.  */
1584
0
      if (r_type == R_386_NONE)
1585
0
  continue;
1586
1587
0
      howto = elf_i386_rtype_to_howto (r_type);
1588
0
      if (rel->r_offset + bfd_get_reloc_size (howto) > sec->size)
1589
0
  {
1590
    /* xgettext:c-format */
1591
0
    _bfd_error_handler
1592
0
      (_("%pB: bad reloc offset (%#" PRIx32 " > %#" PRIx32 ") for"
1593
0
         " section `%pA'"), abfd, (uint32_t) rel->r_offset,
1594
0
       (uint32_t) sec->size, sec);
1595
0
    goto error_return;
1596
0
  }
1597
1598
0
      if (r_symndx < symtab_hdr->sh_info)
1599
0
  {
1600
    /* A local symbol.  */
1601
0
    isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1602
0
          abfd, r_symndx);
1603
0
    if (isym == NULL)
1604
0
      goto error_return;
1605
1606
    /* Check relocation against local STT_GNU_IFUNC symbol.  */
1607
0
    if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1608
0
      {
1609
0
        h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel, true);
1610
0
        if (h == NULL)
1611
0
    goto error_return;
1612
1613
        /* Fake a STT_GNU_IFUNC symbol.  */
1614
0
        h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1615
0
                  isym, NULL);
1616
0
        if (h->root.root.string == bfd_symbol_error_name)
1617
0
    goto error_return;
1618
0
        h->type = STT_GNU_IFUNC;
1619
0
        h->def_regular = 1;
1620
0
        h->ref_regular = 1;
1621
0
        h->forced_local = 1;
1622
0
        h->root.type = bfd_link_hash_defined;
1623
0
      }
1624
0
    else
1625
0
      h = NULL;
1626
0
  }
1627
0
      else
1628
0
  {
1629
0
    isym = NULL;
1630
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1631
0
    while (h->root.type == bfd_link_hash_indirect
1632
0
     || h->root.type == bfd_link_hash_warning)
1633
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
1634
0
  }
1635
1636
0
      eh = (struct elf_x86_link_hash_entry *) h;
1637
0
      if (h != NULL)
1638
0
  {
1639
0
    if (r_type == R_386_GOTOFF)
1640
0
      eh->gotoff_ref = 1;
1641
1642
    /* It is referenced by a non-shared object. */
1643
0
    h->ref_regular = 1;
1644
0
  }
1645
1646
0
      if (r_type == R_386_GOT32X
1647
0
    && (h == NULL || h->type != STT_GNU_IFUNC))
1648
0
  {
1649
0
    Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
1650
0
    if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents,
1651
0
              &r_type, irel, h,
1652
0
              &converted, info))
1653
0
      goto error_return;
1654
0
  }
1655
1656
0
      if (!_bfd_elf_x86_valid_reloc_p (sec, info, htab, rel, h, isym,
1657
0
               symtab_hdr, &no_dynreloc))
1658
0
  return false;
1659
1660
0
      if (! elf_i386_tls_transition (info, abfd, sec, contents,
1661
0
             symtab_hdr, sym_hashes,
1662
0
             &r_type, GOT_UNKNOWN,
1663
0
             rel, rel_end, h, isym, false))
1664
0
  goto error_return;
1665
1666
      /* Check if _GLOBAL_OFFSET_TABLE_ is referenced.  */
1667
0
      if (h == htab->elf.hgot)
1668
0
  htab->got_referenced = true;
1669
1670
0
      switch (r_type)
1671
0
  {
1672
0
  case R_386_TLS_LDM:
1673
0
    htab->tls_ld_or_ldm_got.refcount = 1;
1674
0
    goto create_got;
1675
1676
0
  case R_386_PLT32:
1677
    /* This symbol requires a procedure linkage table entry.  We
1678
       actually build the entry in adjust_dynamic_symbol,
1679
       because this might be a case of linking PIC code which is
1680
       never referenced by a dynamic object, in which case we
1681
       don't need to generate a procedure linkage table entry
1682
       after all.  */
1683
1684
    /* If this is a local symbol, we resolve it directly without
1685
       creating a procedure linkage table entry.  */
1686
0
    if (h == NULL)
1687
0
      continue;
1688
1689
0
    eh->zero_undefweak &= 0x2;
1690
0
    h->needs_plt = 1;
1691
0
    h->plt.refcount = 1;
1692
0
    break;
1693
1694
0
  case R_386_SIZE32:
1695
0
    size_reloc = true;
1696
0
    goto do_size;
1697
1698
0
  case R_386_TLS_DESC_CALL:
1699
0
    htab->has_tls_desc_call = 1;
1700
0
    goto need_got;
1701
1702
0
  case R_386_TLS_IE_32:
1703
0
  case R_386_TLS_IE:
1704
0
  case R_386_TLS_GOTIE:
1705
0
    if (!bfd_link_executable (info))
1706
0
      info->flags |= DF_STATIC_TLS;
1707
    /* Fall through */
1708
1709
0
  case R_386_GOT32:
1710
0
  case R_386_GOT32X:
1711
0
  case R_386_TLS_GD:
1712
0
  case R_386_TLS_GOTDESC:
1713
0
 need_got:
1714
    /* This symbol requires a global offset table entry.  */
1715
0
    {
1716
0
      int tls_type, old_tls_type;
1717
1718
0
      switch (r_type)
1719
0
        {
1720
0
        default:
1721
0
        case R_386_GOT32:
1722
0
        case R_386_GOT32X:
1723
0
    tls_type = GOT_NORMAL;
1724
0
    break;
1725
0
        case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1726
0
        case R_386_TLS_GOTDESC:
1727
0
        case R_386_TLS_DESC_CALL:
1728
0
    tls_type = GOT_TLS_GDESC; break;
1729
0
        case R_386_TLS_IE_32:
1730
0
    if (ELF32_R_TYPE (rel->r_info) == r_type)
1731
0
      tls_type = GOT_TLS_IE_NEG;
1732
0
    else
1733
      /* If this is a GD->IE transition, we may use either of
1734
         R_386_TLS_TPOFF and R_386_TLS_TPOFF32.  */
1735
0
      tls_type = GOT_TLS_IE;
1736
0
    break;
1737
0
        case R_386_TLS_IE:
1738
0
        case R_386_TLS_GOTIE:
1739
0
    tls_type = GOT_TLS_IE_POS; break;
1740
0
        }
1741
1742
0
      if (tls_type >= GOT_TLS_GD
1743
0
    && tls_type <= GOT_TLS_GDESC
1744
0
    && ((h != NULL
1745
0
         ? h->type != STT_TLS
1746
0
         : ELF_ST_TYPE (isym->st_info) != STT_TLS)
1747
0
        || elf_section_type (sec) != SHT_PROGBITS
1748
0
        || (sec->flags & SEC_CODE) == 0))
1749
0
        {
1750
0
    _bfd_x86_elf_link_report_error (abfd, sec, symtab_hdr,
1751
0
            h, isym, howto,
1752
0
            elf_x86_error_tls);
1753
0
    goto error_return;
1754
0
        }
1755
1756
0
      if (h != NULL)
1757
0
        {
1758
0
    h->got.refcount = 1;
1759
0
    old_tls_type = elf_x86_hash_entry (h)->tls_type;
1760
0
        }
1761
0
      else
1762
0
        {
1763
0
    bfd_signed_vma *local_got_refcounts;
1764
1765
0
    if (!elf_x86_allocate_local_got_info (abfd,
1766
0
                  symtab_hdr->sh_info))
1767
0
          goto error_return;
1768
1769
    /* This is a global offset table entry for a local symbol.  */
1770
0
    local_got_refcounts = elf_local_got_refcounts (abfd);
1771
0
    local_got_refcounts[r_symndx] = 1;
1772
0
    old_tls_type = elf_x86_local_got_tls_type (abfd) [r_symndx];
1773
0
        }
1774
1775
0
      if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1776
0
        tls_type |= old_tls_type;
1777
      /* If a TLS symbol is accessed using IE at least once,
1778
         there is no point to use dynamic model for it.  */
1779
0
      else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1780
0
         && (! GOT_TLS_GD_ANY_P (old_tls_type)
1781
0
       || (tls_type & GOT_TLS_IE) == 0))
1782
0
        {
1783
0
    if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1784
0
      tls_type = old_tls_type;
1785
0
    else if (GOT_TLS_GD_ANY_P (old_tls_type)
1786
0
       && GOT_TLS_GD_ANY_P (tls_type))
1787
0
      tls_type |= old_tls_type;
1788
0
    else
1789
0
      {
1790
0
        if (h)
1791
0
          name = h->root.root.string;
1792
0
        else
1793
0
          name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1794
0
                 NULL);
1795
0
        _bfd_error_handler
1796
          /* xgettext:c-format */
1797
0
          (_("%pB: `%s' accessed both as normal and "
1798
0
       "thread local symbol"),
1799
0
           abfd, name);
1800
0
        bfd_set_error (bfd_error_bad_value);
1801
0
        goto error_return;
1802
0
      }
1803
0
        }
1804
1805
0
      if (old_tls_type != tls_type)
1806
0
        {
1807
0
    if (h != NULL)
1808
0
      elf_x86_hash_entry (h)->tls_type = tls_type;
1809
0
    else
1810
0
      elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
1811
0
        }
1812
0
    }
1813
    /* Fall through */
1814
1815
0
  case R_386_GOTOFF:
1816
0
  case R_386_GOTPC:
1817
0
  create_got:
1818
0
    if (r_type != R_386_TLS_IE)
1819
0
      {
1820
0
        if (eh != NULL)
1821
0
    {
1822
0
      eh->zero_undefweak &= 0x2;
1823
1824
      /* Need GOT to resolve undefined weak symbol to 0.  */
1825
0
      if (r_type == R_386_GOTOFF
1826
0
          && h->root.type == bfd_link_hash_undefweak
1827
0
          && bfd_link_executable (info))
1828
0
        htab->got_referenced = true;
1829
0
    }
1830
0
        break;
1831
0
      }
1832
    /* Fall through */
1833
1834
0
  case R_386_TLS_LE_32:
1835
0
  case R_386_TLS_LE:
1836
0
    if (eh != NULL)
1837
0
      eh->zero_undefweak &= 0x2;
1838
0
    if (bfd_link_executable (info))
1839
0
      break;
1840
0
    info->flags |= DF_STATIC_TLS;
1841
0
    goto do_relocation;
1842
1843
0
  case R_386_32:
1844
0
  case R_386_PC32:
1845
0
    if (eh != NULL && (sec->flags & SEC_CODE) != 0)
1846
0
      eh->zero_undefweak |= 0x2;
1847
0
  do_relocation:
1848
    /* We are called after all symbols have been resolved.  Only
1849
       relocation against STT_GNU_IFUNC symbol must go through
1850
       PLT.  */
1851
0
    if (h != NULL
1852
0
        && (bfd_link_executable (info)
1853
0
      || h->type == STT_GNU_IFUNC))
1854
0
      {
1855
0
        bool func_pointer_ref = false;
1856
1857
0
        if (r_type == R_386_PC32)
1858
0
    {
1859
      /* Since something like ".long foo - ." may be used
1860
         as pointer, make sure that PLT is used if foo is
1861
         a function defined in a shared library.  */
1862
0
      if ((sec->flags & SEC_CODE) == 0)
1863
0
        h->pointer_equality_needed = 1;
1864
0
      else if (h->type == STT_GNU_IFUNC
1865
0
         && bfd_link_pic (info))
1866
0
        {
1867
0
          _bfd_error_handler
1868
      /* xgettext:c-format */
1869
0
      (_("%pB: unsupported non-PIC call to IFUNC `%s'"),
1870
0
       abfd, h->root.root.string);
1871
0
          bfd_set_error (bfd_error_bad_value);
1872
0
          goto error_return;
1873
0
        }
1874
0
    }
1875
0
        else
1876
0
    {
1877
      /* R_386_32 can be resolved at run-time.  Function
1878
         pointer reference doesn't need PLT for pointer
1879
         equality.  */
1880
0
      if (r_type == R_386_32
1881
0
          && (sec->flags & SEC_READONLY) == 0)
1882
0
        func_pointer_ref = true;
1883
1884
      /* IFUNC symbol needs pointer equality in PDE so that
1885
         function pointer reference will be resolved to its
1886
         PLT entry directly.  */
1887
0
      if (!func_pointer_ref
1888
0
          || (bfd_link_pde (info)
1889
0
        && h->type == STT_GNU_IFUNC))
1890
0
        h->pointer_equality_needed = 1;
1891
0
    }
1892
1893
0
        if (!func_pointer_ref)
1894
0
    {
1895
      /* If this reloc is in a read-only section, we might
1896
         need a copy reloc.  We can't check reliably at this
1897
         stage whether the section is read-only, as input
1898
         sections have not yet been mapped to output sections.
1899
         Tentatively set the flag for now, and correct in
1900
         adjust_dynamic_symbol.  */
1901
0
      h->non_got_ref = 1;
1902
1903
0
      if (!elf_has_indirect_extern_access (sec->owner))
1904
0
        eh->non_got_ref_without_indirect_extern_access = 1;
1905
1906
      /* We may need a .plt entry if the symbol is a function
1907
         defined in a shared lib or is a function referenced
1908
         from the code or read-only section.  */
1909
0
      if (!h->def_regular
1910
0
          || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
1911
0
        h->plt.refcount = 1;
1912
1913
0
      if (htab->elf.target_os != is_solaris
1914
0
          && h->pointer_equality_needed
1915
0
          && h->type == STT_FUNC
1916
0
          && eh->def_protected
1917
0
          && !SYMBOL_DEFINED_NON_SHARED_P (h)
1918
0
          && h->def_dynamic)
1919
0
        {
1920
          /* Disallow non-canonical reference to canonical
1921
       protected function.  */
1922
0
          _bfd_error_handler
1923
      /* xgettext:c-format */
1924
0
      (_("%pB: non-canonical reference to canonical "
1925
0
         "protected function `%s' in %pB"),
1926
0
       abfd, h->root.root.string,
1927
0
       h->root.u.def.section->owner);
1928
0
          bfd_set_error (bfd_error_bad_value);
1929
0
          goto error_return;
1930
0
        }
1931
0
    }
1932
0
      }
1933
1934
0
    size_reloc = false;
1935
0
  do_size:
1936
0
    if (!no_dynreloc
1937
0
        && NEED_DYNAMIC_RELOCATION_P (false, info, false, h, sec,
1938
0
              r_type, R_386_32))
1939
0
      {
1940
0
        struct elf_dyn_relocs *p;
1941
0
        struct elf_dyn_relocs **head;
1942
1943
        /* If this is a global symbol, we count the number of
1944
     relocations we need for this symbol.  */
1945
0
        if (h != NULL)
1946
0
    {
1947
0
      head = &h->dyn_relocs;
1948
0
    }
1949
0
        else
1950
0
    {
1951
      /* Track dynamic relocs needed for local syms too.  */
1952
0
      void **vpp;
1953
0
      asection *s;
1954
1955
0
      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1956
0
      if (s == NULL)
1957
0
        s = sec;
1958
1959
0
      vpp = &elf_section_data (s)->local_dynrel;
1960
0
      head = (struct elf_dyn_relocs **)vpp;
1961
0
    }
1962
1963
0
        p = *head;
1964
0
        if (p == NULL || p->sec != sec)
1965
0
    {
1966
0
      size_t amt = sizeof *p;
1967
0
      p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
1968
0
                 amt);
1969
0
      if (p == NULL)
1970
0
        goto error_return;
1971
0
      p->next = *head;
1972
0
      *head = p;
1973
0
      p->sec = sec;
1974
0
      p->count = 0;
1975
0
      p->pc_count = 0;
1976
0
    }
1977
1978
0
        p->count += 1;
1979
        /* Count size relocation as PC-relative relocation.  */
1980
0
        if (r_type == R_386_PC32 || size_reloc)
1981
0
    p->pc_count += 1;
1982
0
      }
1983
0
    break;
1984
1985
    /* This relocation describes the C++ object vtable hierarchy.
1986
       Reconstruct it for later use during GC.  */
1987
0
  case R_386_GNU_VTINHERIT:
1988
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1989
0
      goto error_return;
1990
0
    break;
1991
1992
    /* This relocation describes which C++ vtable entries are actually
1993
       used.  Record for later use during GC.  */
1994
0
  case R_386_GNU_VTENTRY:
1995
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1996
0
      goto error_return;
1997
0
    break;
1998
1999
0
  default:
2000
0
    break;
2001
0
  }
2002
0
    }
2003
2004
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
2005
0
    {
2006
0
      if (!converted)
2007
0
  _bfd_elf_munmap_section_contents (sec, contents);
2008
0
      else
2009
0
  {
2010
    /* Cache the section contents for elf_link_input_bfd if any
2011
       load is converted or --no-keep-memory isn't used.  */
2012
0
    elf_section_data (sec)->this_hdr.contents = contents;
2013
0
    info->cache_size += sec->size;
2014
0
  }
2015
0
    }
2016
2017
  /* Cache relocations if any load is converted.  */
2018
0
  if (elf_section_data (sec)->relocs != relocs && converted)
2019
0
    elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
2020
2021
0
  return true;
2022
2023
0
 error_return:
2024
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
2025
0
    _bfd_elf_munmap_section_contents (sec, contents);
2026
0
  sec->check_relocs_failed = 1;
2027
0
  return false;
2028
0
}
2029
2030
static bool
2031
elf_i386_early_size_sections (struct bfd_link_info *info)
2032
0
{
2033
0
  bfd *abfd;
2034
2035
  /* Scan relocations after rel_from_abs has been set on __ehdr_start.  */
2036
0
  for (abfd = info->input_bfds;
2037
0
       abfd != (bfd *) NULL;
2038
0
       abfd = abfd->link.next)
2039
0
    if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2040
0
  && !_bfd_elf_link_iterate_on_relocs (abfd, info,
2041
0
               elf_i386_scan_relocs))
2042
0
      return false;
2043
2044
0
  return _bfd_x86_elf_early_size_sections (info);
2045
0
}
2046
2047
/* Set the correct type for an x86 ELF section.  We do this by the
2048
   section name, which is a hack, but ought to work.  */
2049
2050
static bool
2051
elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2052
      Elf_Internal_Shdr *hdr,
2053
      asection *sec)
2054
148
{
2055
148
  const char *name;
2056
2057
148
  name = bfd_section_name (sec);
2058
2059
  /* This is an ugly, but unfortunately necessary hack that is
2060
     needed when producing EFI binaries on x86. It tells
2061
     elf.c:elf_fake_sections() not to consider ".reloc" as a section
2062
     containing ELF relocation info.  We need this hack in order to
2063
     be able to generate ELF binaries that can be translated into
2064
     EFI applications (which are essentially COFF objects).  Those
2065
     files contain a COFF ".reloc" section inside an ELFNN object,
2066
     which would normally cause BFD to segfault because it would
2067
     attempt to interpret this section as containing relocation
2068
     entries for section "oc".  With this hack enabled, ".reloc"
2069
     will be treated as a normal data section, which will avoid the
2070
     segfault.  However, you won't be able to create an ELFNN binary
2071
     with a section named "oc" that needs relocations, but that's
2072
     the kind of ugly side-effects you get when detecting section
2073
     types based on their names...  In practice, this limitation is
2074
     unlikely to bite.  */
2075
148
  if (strcmp (name, ".reloc") == 0)
2076
0
    hdr->sh_type = SHT_PROGBITS;
2077
2078
148
  return true;
2079
148
}
2080
2081
/* Return the relocation value for @tpoff relocation
2082
   if STT_TLS virtual address is ADDRESS.  */
2083
2084
static bfd_vma
2085
elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
2086
0
{
2087
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
2088
0
  elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2089
0
  bfd_vma static_tls_size;
2090
2091
  /* If tls_sec is NULL, we should have signalled an error already.  */
2092
0
  if (htab->tls_sec == NULL)
2093
0
    return 0;
2094
2095
  /* Consider special static TLS alignment requirements.  */
2096
0
  static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2097
0
  return static_tls_size + htab->tls_sec->vma - address;
2098
0
}
2099
2100
/* Relocate an i386 ELF section.  */
2101
2102
static int
2103
elf_i386_relocate_section (struct bfd_link_info *info,
2104
         bfd *input_bfd,
2105
         asection *input_section,
2106
         bfd_byte *contents,
2107
         Elf_Internal_Rela *relocs,
2108
         Elf_Internal_Sym *local_syms,
2109
         asection **local_sections)
2110
0
{
2111
0
  struct elf_x86_link_hash_table *htab;
2112
0
  Elf_Internal_Shdr *symtab_hdr;
2113
0
  struct elf_link_hash_entry **sym_hashes;
2114
0
  bfd_vma *local_got_offsets;
2115
0
  bfd_vma *local_tlsdesc_gotents;
2116
0
  Elf_Internal_Rela *rel;
2117
0
  Elf_Internal_Rela *wrel;
2118
0
  Elf_Internal_Rela *relend;
2119
0
  bool is_vxworks_tls;
2120
0
  unsigned expected_tls_le;
2121
0
  unsigned plt_entry_size;
2122
2123
  /* Skip if check_relocs or scan_relocs failed.  */
2124
0
  if (input_section->check_relocs_failed)
2125
0
    return false;
2126
2127
0
  htab = elf_x86_hash_table (info, I386_ELF_DATA);
2128
0
  if (htab == NULL)
2129
0
    return false;
2130
2131
0
  if (!is_x86_elf (input_bfd, htab))
2132
0
    {
2133
0
      bfd_set_error (bfd_error_wrong_format);
2134
0
      return false;
2135
0
    }
2136
2137
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
2138
0
  sym_hashes = elf_sym_hashes (input_bfd);
2139
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
2140
0
  local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
2141
  /* We have to handle relocations in vxworks .tls_vars sections
2142
     specially, because the dynamic loader is 'weird'.  */
2143
0
  is_vxworks_tls = (htab->elf.target_os == is_vxworks
2144
0
        && bfd_link_pic (info)
2145
0
        && !strcmp (input_section->output_section->name,
2146
0
        ".tls_vars"));
2147
2148
0
  _bfd_x86_elf_set_tls_module_base (info);
2149
2150
0
  plt_entry_size = htab->plt.plt_entry_size;
2151
2152
0
  rel = wrel = relocs;
2153
0
  relend = relocs + input_section->reloc_count;
2154
0
  for (; rel < relend; wrel++, rel++)
2155
0
    {
2156
0
      unsigned int r_type, r_type_tls;
2157
0
      reloc_howto_type *howto;
2158
0
      unsigned long r_symndx;
2159
0
      struct elf_link_hash_entry *h;
2160
0
      struct elf_x86_link_hash_entry *eh;
2161
0
      const Elf_Internal_Sym *sym;
2162
0
      asection *sec;
2163
0
      bfd_vma off, offplt, plt_offset;
2164
0
      bfd_vma relocation;
2165
0
      bool unresolved_reloc;
2166
0
      bfd_reloc_status_type r;
2167
0
      unsigned int indx;
2168
0
      int tls_type;
2169
0
      bfd_vma st_size;
2170
0
      asection *resolved_plt;
2171
0
      bool resolved_to_zero;
2172
0
      bool relative_reloc;
2173
2174
0
      r_type = ELF32_R_TYPE (rel->r_info);
2175
0
      if (r_type == R_386_GNU_VTINHERIT
2176
0
    || r_type == R_386_GNU_VTENTRY)
2177
0
  {
2178
0
    if (wrel != rel)
2179
0
      *wrel = *rel;
2180
0
    continue;
2181
0
  }
2182
2183
0
      howto = elf_i386_rtype_to_howto (r_type);
2184
0
      if (howto == NULL)
2185
0
  return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
2186
2187
0
      r_symndx = ELF32_R_SYM (rel->r_info);
2188
0
      h = NULL;
2189
0
      sym = NULL;
2190
0
      sec = NULL;
2191
0
      unresolved_reloc = false;
2192
0
      if (r_symndx < symtab_hdr->sh_info)
2193
0
  {
2194
0
    sym = local_syms + r_symndx;
2195
0
    sec = local_sections[r_symndx];
2196
0
    relocation = (sec->output_section->vma
2197
0
      + sec->output_offset
2198
0
      + sym->st_value);
2199
0
    st_size = sym->st_size;
2200
2201
0
    if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2202
0
        && ((sec->flags & SEC_MERGE) != 0
2203
0
      || (bfd_link_relocatable (info)
2204
0
          && sec->output_offset != 0)))
2205
0
      {
2206
0
        bfd_vma addend;
2207
0
        bfd_byte *where = contents + rel->r_offset;
2208
2209
0
        switch (bfd_get_reloc_size (howto))
2210
0
    {
2211
0
    case 1:
2212
0
      addend = bfd_get_8 (input_bfd, where);
2213
0
      if (howto->pc_relative)
2214
0
        {
2215
0
          addend = (addend ^ 0x80) - 0x80;
2216
0
          addend += 1;
2217
0
        }
2218
0
      break;
2219
0
    case 2:
2220
0
      addend = bfd_get_16 (input_bfd, where);
2221
0
      if (howto->pc_relative)
2222
0
        {
2223
0
          addend = (addend ^ 0x8000) - 0x8000;
2224
0
          addend += 2;
2225
0
        }
2226
0
      break;
2227
0
    case 4:
2228
0
      addend = bfd_get_32 (input_bfd, where);
2229
0
      if (howto->pc_relative)
2230
0
        {
2231
0
          addend = (addend ^ 0x80000000) - 0x80000000;
2232
0
          addend += 4;
2233
0
        }
2234
0
      break;
2235
0
    default:
2236
0
      abort ();
2237
0
    }
2238
2239
0
        if (bfd_link_relocatable (info))
2240
0
    addend += sec->output_offset;
2241
0
        else
2242
0
    {
2243
0
      asection *msec = sec;
2244
0
      addend = _bfd_elf_rel_local_sym (info->output_bfd, sym, &msec,
2245
0
               addend);
2246
0
      addend -= relocation;
2247
0
      addend += msec->output_section->vma + msec->output_offset;
2248
0
    }
2249
2250
0
        switch (bfd_get_reloc_size (howto))
2251
0
    {
2252
0
    case 1:
2253
      /* FIXME: overflow checks.  */
2254
0
      if (howto->pc_relative)
2255
0
        addend -= 1;
2256
0
      bfd_put_8 (input_bfd, addend, where);
2257
0
      break;
2258
0
    case 2:
2259
0
      if (howto->pc_relative)
2260
0
        addend -= 2;
2261
0
      bfd_put_16 (input_bfd, addend, where);
2262
0
      break;
2263
0
    case 4:
2264
0
      if (howto->pc_relative)
2265
0
        addend -= 4;
2266
0
      bfd_put_32 (input_bfd, addend, where);
2267
0
      break;
2268
0
    }
2269
0
      }
2270
0
    else if (!bfd_link_relocatable (info)
2271
0
       && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2272
0
      {
2273
        /* Relocate against local STT_GNU_IFUNC symbol.  */
2274
0
        h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd, rel,
2275
0
               false);
2276
0
        if (h == NULL)
2277
0
    abort ();
2278
2279
        /* Set STT_GNU_IFUNC symbol value.  */
2280
0
        h->root.u.def.value = sym->st_value;
2281
0
        h->root.u.def.section = sec;
2282
0
      }
2283
0
  }
2284
0
      else
2285
0
  {
2286
0
    bool warned ATTRIBUTE_UNUSED;
2287
0
    bool ignored ATTRIBUTE_UNUSED;
2288
2289
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2290
0
           r_symndx, symtab_hdr, sym_hashes,
2291
0
           h, sec, relocation,
2292
0
           unresolved_reloc, warned, ignored);
2293
0
    st_size = h->size;
2294
0
  }
2295
2296
0
      if (sec != NULL && discarded_section (sec))
2297
0
  {
2298
0
    _bfd_clear_contents (howto, input_bfd, input_section,
2299
0
             contents, rel->r_offset);
2300
0
    wrel->r_offset = rel->r_offset;
2301
0
    wrel->r_info = 0;
2302
0
    wrel->r_addend = 0;
2303
2304
    /* For ld -r, remove relocations in debug sections against
2305
       sections defined in discarded sections.  Not done for
2306
       eh_frame editing code expects to be present.  */
2307
0
     if (bfd_link_relocatable (info)
2308
0
         && (input_section->flags & SEC_DEBUGGING))
2309
0
       wrel--;
2310
2311
0
     continue;
2312
0
  }
2313
2314
0
      if (bfd_link_relocatable (info))
2315
0
  {
2316
0
    if (wrel != rel)
2317
0
      *wrel = *rel;
2318
0
    continue;
2319
0
  }
2320
2321
0
      eh = (struct elf_x86_link_hash_entry *) h;
2322
2323
      /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2324
   it here if it is defined in a non-shared object.  */
2325
0
      if (h != NULL
2326
0
    && h->type == STT_GNU_IFUNC
2327
0
    && h->def_regular)
2328
0
  {
2329
0
    asection *gotplt, *base_got;
2330
0
    bfd_vma plt_index;
2331
0
    const char *name;
2332
2333
0
    if ((input_section->flags & SEC_ALLOC) == 0)
2334
0
      {
2335
        /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2336
           STT_GNU_IFUNC symbol as STT_FUNC.  */
2337
0
        if (elf_section_type (input_section) == SHT_NOTE)
2338
0
    goto skip_ifunc;
2339
        /* Dynamic relocs are not propagated for SEC_DEBUGGING
2340
     sections because such sections are not SEC_ALLOC and
2341
     thus ld.so will not process them.  */
2342
0
        if ((input_section->flags & SEC_DEBUGGING) != 0)
2343
0
    continue;
2344
0
        abort ();
2345
0
      }
2346
2347
    /* STT_GNU_IFUNC symbol must go through PLT.  */
2348
0
    if (htab->elf.splt != NULL)
2349
0
      {
2350
0
        if (htab->plt_second != NULL)
2351
0
    {
2352
0
      resolved_plt = htab->plt_second;
2353
0
      plt_offset = eh->plt_second.offset;
2354
0
    }
2355
0
        else
2356
0
    {
2357
0
      resolved_plt = htab->elf.splt;
2358
0
      plt_offset = h->plt.offset;
2359
0
    }
2360
0
        gotplt = htab->elf.sgotplt;
2361
0
      }
2362
0
    else
2363
0
      {
2364
0
        resolved_plt = htab->elf.iplt;
2365
0
        plt_offset = h->plt.offset;
2366
0
        gotplt = htab->elf.igotplt;
2367
0
      }
2368
2369
0
    switch (r_type)
2370
0
      {
2371
0
      default:
2372
0
        break;
2373
2374
0
      case R_386_GOT32:
2375
0
      case R_386_GOT32X:
2376
0
        base_got = htab->elf.sgot;
2377
0
        off = h->got.offset;
2378
2379
0
        if (base_got == NULL)
2380
0
    abort ();
2381
2382
0
        if (off == (bfd_vma) -1)
2383
0
    {
2384
      /* We can't use h->got.offset here to save state, or
2385
         even just remember the offset, as finish_dynamic_symbol
2386
         would use that as offset into .got.  */
2387
2388
0
      if (h->plt.offset == (bfd_vma) -1)
2389
0
        abort ();
2390
2391
0
      if (htab->elf.splt != NULL)
2392
0
        {
2393
0
          plt_index = (h->plt.offset / plt_entry_size
2394
0
           - htab->plt.has_plt0);
2395
0
          off = (plt_index + 3) * 4;
2396
0
          base_got = htab->elf.sgotplt;
2397
0
        }
2398
0
      else
2399
0
        {
2400
0
          plt_index = h->plt.offset / plt_entry_size;
2401
0
          off = plt_index * 4;
2402
0
          base_got = htab->elf.igotplt;
2403
0
        }
2404
2405
0
      if (h->dynindx == -1
2406
0
          || h->forced_local
2407
0
          || info->symbolic)
2408
0
        {
2409
          /* This references the local defitionion.  We must
2410
       initialize this entry in the global offset table.
2411
       Since the offset must always be a multiple of 4,
2412
       we use the least significant bit to record
2413
       whether we have initialized it already.
2414
2415
       When doing a dynamic link, we create a .rela.got
2416
       relocation entry to initialize the value.  This
2417
       is done in the finish_dynamic_symbol routine.   */
2418
0
          if ((off & 1) != 0)
2419
0
      off &= ~1;
2420
0
          else
2421
0
      {
2422
0
        bfd_put_32 (info->output_bfd, relocation,
2423
0
              base_got->contents + off);
2424
0
        h->got.offset |= 1;
2425
0
      }
2426
0
        }
2427
2428
0
      relocation = off;
2429
0
    }
2430
0
        else
2431
0
    relocation = (base_got->output_section->vma
2432
0
            + base_got->output_offset + off
2433
0
            - gotplt->output_section->vma
2434
0
            - gotplt->output_offset);
2435
2436
0
        if (rel->r_offset > 1
2437
0
      && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2438
0
      && *(contents + rel->r_offset - 2) != 0x8d)
2439
0
    {
2440
0
      if (bfd_link_pic (info))
2441
0
        goto disallow_got32;
2442
2443
      /* Add the GOT base if there is no base register.  */
2444
0
      relocation += (gotplt->output_section->vma
2445
0
         + gotplt->output_offset);
2446
0
    }
2447
0
        else if (htab->elf.splt == NULL)
2448
0
    {
2449
      /* Adjust for static executables.  */
2450
0
      relocation += gotplt->output_offset;
2451
0
    }
2452
2453
0
        goto do_relocation;
2454
0
      }
2455
2456
0
    if (h->plt.offset == (bfd_vma) -1)
2457
0
      {
2458
        /* Handle static pointers of STT_GNU_IFUNC symbols.  */
2459
0
        if (r_type == R_386_32
2460
0
      && (input_section->flags & SEC_CODE) == 0)
2461
0
    goto do_ifunc_pointer;
2462
0
        goto bad_ifunc_reloc;
2463
0
      }
2464
2465
0
    relocation = (resolved_plt->output_section->vma
2466
0
      + resolved_plt->output_offset + plt_offset);
2467
2468
0
    switch (r_type)
2469
0
      {
2470
0
      default:
2471
0
      bad_ifunc_reloc:
2472
0
        if (h->root.root.string)
2473
0
    name = h->root.root.string;
2474
0
        else
2475
0
    name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2476
0
           NULL);
2477
0
        _bfd_error_handler
2478
    /* xgettext:c-format */
2479
0
    (_("%pB: relocation %s against STT_GNU_IFUNC "
2480
0
       "symbol `%s' isn't supported"), input_bfd,
2481
0
     howto->name, name);
2482
0
        bfd_set_error (bfd_error_bad_value);
2483
0
        return false;
2484
2485
0
      case R_386_32:
2486
        /* Generate dynamic relcoation only when there is a
2487
     non-GOT reference in a shared object.  */
2488
0
        if ((bfd_link_pic (info) && h->non_got_ref)
2489
0
      || h->plt.offset == (bfd_vma) -1)
2490
0
    {
2491
0
      Elf_Internal_Rela outrel;
2492
0
      asection *sreloc;
2493
0
      bfd_vma offset;
2494
2495
0
    do_ifunc_pointer:
2496
      /* Need a dynamic relocation to get the real function
2497
         adddress.  */
2498
0
      offset = _bfd_elf_section_offset (info->output_bfd,
2499
0
                info,
2500
0
                input_section,
2501
0
                rel->r_offset);
2502
0
      if (offset == (bfd_vma) -1
2503
0
          || offset == (bfd_vma) -2)
2504
0
        abort ();
2505
2506
0
      outrel.r_offset = (input_section->output_section->vma
2507
0
             + input_section->output_offset
2508
0
             + offset);
2509
2510
0
      if (POINTER_LOCAL_IFUNC_P (info, h))
2511
0
        {
2512
0
          info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
2513
0
                h->root.root.string,
2514
0
                h->root.u.def.section->owner);
2515
2516
          /* This symbol is resolved locally.  */
2517
0
          outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
2518
2519
0
          if (htab->params->report_relative_reloc)
2520
0
      _bfd_x86_elf_link_report_relative_reloc
2521
0
        (info, input_section, h, sym,
2522
0
         "R_386_IRELATIVE", &outrel);
2523
2524
0
          bfd_put_32 (info->output_bfd,
2525
0
          (h->root.u.def.value
2526
0
           + h->root.u.def.section->output_section->vma
2527
0
           + h->root.u.def.section->output_offset),
2528
0
          contents + offset);
2529
0
        }
2530
0
      else
2531
0
        outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2532
2533
      /* Dynamic relocations are stored in
2534
         1. .rel.ifunc section in PIC object.
2535
         2. .rel.got section in dynamic executable.
2536
         3. .rel.iplt section in static executable.  */
2537
0
      if (bfd_link_pic (info))
2538
0
        sreloc = htab->elf.irelifunc;
2539
0
      else if (htab->elf.splt != NULL)
2540
0
        sreloc = htab->elf.srelgot;
2541
0
      else
2542
0
        sreloc = htab->elf.irelplt;
2543
0
      _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
2544
2545
      /* If this reloc is against an external symbol, we
2546
         do not want to fiddle with the addend.  Otherwise,
2547
         we need to include the symbol value so that it
2548
         becomes an addend for the dynamic reloc.  For an
2549
         internal symbol, we have updated addend.  */
2550
0
      continue;
2551
0
    }
2552
        /* FALLTHROUGH */
2553
0
      case R_386_PC32:
2554
0
      case R_386_PLT32:
2555
0
        goto do_relocation;
2556
2557
0
      case R_386_GOTOFF:
2558
        /* NB: We can't use the PLT entry as the function address
2559
     for PIC since the PIC register may not be set up
2560
     properly for indirect call. */
2561
0
        if (bfd_link_pic (info))
2562
0
    goto bad_ifunc_reloc;
2563
0
        relocation -= (gotplt->output_section->vma
2564
0
           + gotplt->output_offset);
2565
0
        goto do_relocation;
2566
0
      }
2567
0
  }
2568
2569
0
    skip_ifunc:
2570
0
      resolved_to_zero = (eh != NULL
2571
0
        && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
2572
2573
0
      switch (r_type)
2574
0
  {
2575
0
  case R_386_GOT32X:
2576
0
  case R_386_GOT32:
2577
    /* Since we don't allow non-alloced sections to create GOT/PLT
2578
       entries, issue an error if a non-alloced section references
2579
       GOT.  */
2580
0
    if ((input_section->flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
2581
0
      {
2582
0
  non_alloc_error:
2583
0
        _bfd_x86_elf_link_report_error (input_bfd, input_section,
2584
0
                symtab_hdr, h, sym, howto,
2585
0
                elf_x86_error_non_alloc);
2586
0
        return false;
2587
0
      }
2588
2589
    /* Relocation is to the entry for this symbol in the global
2590
       offset table.  */
2591
0
    if (htab->elf.sgot == NULL)
2592
0
      abort ();
2593
2594
0
    relative_reloc = false;
2595
0
    if (h != NULL)
2596
0
      {
2597
0
        off = h->got.offset;
2598
0
        if (RESOLVED_LOCALLY_P (info, h, htab))
2599
0
    {
2600
      /* We must initialize this entry in the global offset
2601
         table.  Since the offset must always be a multiple
2602
         of 4, we use the least significant bit to record
2603
         whether we have initialized it already.
2604
2605
         When doing a dynamic link, we create a .rel.got
2606
         relocation entry to initialize the value.  This
2607
         is done in the finish_dynamic_symbol routine.  */
2608
0
      if ((off & 1) != 0)
2609
0
        off &= ~1;
2610
0
      else
2611
0
        {
2612
0
          bfd_put_32 (info->output_bfd, relocation,
2613
0
          htab->elf.sgot->contents + off);
2614
0
          h->got.offset |= 1;
2615
          /* NB: Don't generate relative relocation here if
2616
       it has been generated by DT_RELR.  */
2617
0
          if (!info->enable_dt_relr
2618
0
        && GENERATE_RELATIVE_RELOC_P (info, h))
2619
0
      {
2620
        /* PR ld/21402: If this symbol isn't dynamic
2621
           in PIC, generate R_386_RELATIVE here.  */
2622
0
        eh->no_finish_dynamic_symbol = 1;
2623
0
        relative_reloc = true;
2624
0
      }
2625
0
        }
2626
0
    }
2627
0
        else
2628
0
    unresolved_reloc = false;
2629
0
      }
2630
0
    else
2631
0
      {
2632
0
        if (local_got_offsets == NULL)
2633
0
    abort ();
2634
2635
0
        off = local_got_offsets[r_symndx];
2636
2637
        /* The offset must always be a multiple of 4.  We use
2638
     the least significant bit to record whether we have
2639
     already generated the necessary reloc.  */
2640
0
        if ((off & 1) != 0)
2641
0
    off &= ~1;
2642
0
        else
2643
0
    {
2644
0
      bfd_put_32 (info->output_bfd, relocation,
2645
0
            htab->elf.sgot->contents + off);
2646
0
      local_got_offsets[r_symndx] |= 1;
2647
2648
      /* NB: Don't generate relative relocation here if it
2649
         has been generated by DT_RELR.  */
2650
0
      if (!info->enable_dt_relr && bfd_link_pic (info))
2651
0
        relative_reloc = true;
2652
0
    }
2653
0
      }
2654
2655
0
    if (relative_reloc)
2656
0
      {
2657
0
        asection *s;
2658
0
        Elf_Internal_Rela outrel;
2659
2660
0
        s = htab->elf.srelgot;
2661
0
        if (s == NULL)
2662
0
    abort ();
2663
2664
0
        outrel.r_offset = (htab->elf.sgot->output_section->vma
2665
0
         + htab->elf.sgot->output_offset
2666
0
         + off);
2667
0
        outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2668
2669
0
        if (htab->params->report_relative_reloc)
2670
0
    _bfd_x86_elf_link_report_relative_reloc
2671
0
      (info, input_section, h, sym, "R_386_RELATIVE",
2672
0
       &outrel);
2673
2674
0
        _bfd_elf_append_rel (info->output_bfd, s, &outrel);
2675
0
      }
2676
2677
0
    if (off >= (bfd_vma) -2)
2678
0
      abort ();
2679
2680
0
    relocation = (htab->elf.sgot->output_section->vma
2681
0
      + htab->elf.sgot->output_offset + off);
2682
0
    if (rel->r_offset > 1
2683
0
        && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2684
0
        && *(contents + rel->r_offset - 2) != 0x8d)
2685
0
      {
2686
0
        if (bfd_link_pic (info))
2687
0
    {
2688
      /* For PIC, disallow R_386_GOT32 without a base
2689
         register, except for "lea foo@GOT, %reg", since
2690
         we don't know what the GOT base is.  */
2691
0
      const char *name;
2692
2693
0
    disallow_got32:
2694
0
      if (h == NULL || h->root.root.string == NULL)
2695
0
        name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2696
0
               NULL);
2697
0
      else
2698
0
        name = h->root.root.string;
2699
2700
0
      _bfd_error_handler
2701
        /* xgettext:c-format */
2702
0
        (_("%pB: direct GOT relocation %s against `%s'"
2703
0
           " without base register can not be used"
2704
0
           " when making a shared object"),
2705
0
         input_bfd, howto->name, name);
2706
0
      bfd_set_error (bfd_error_bad_value);
2707
0
      return false;
2708
0
    }
2709
0
      }
2710
0
    else
2711
0
      {
2712
        /* Subtract the .got.plt section address only with a base
2713
     register.  */
2714
0
        relocation -= (htab->elf.sgotplt->output_section->vma
2715
0
           + htab->elf.sgotplt->output_offset);
2716
0
      }
2717
2718
0
    break;
2719
2720
0
  case R_386_GOTOFF:
2721
0
    if ((input_section->flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
2722
0
      goto non_alloc_error;
2723
2724
    /* Relocation is relative to the start of the global offset
2725
       table.  */
2726
2727
    /* Check to make sure it isn't a protected function or data
2728
       symbol for shared library since it may not be local when
2729
       used as function address or with copy relocation.  We also
2730
       need to make sure that a symbol is referenced locally.  */
2731
0
    if (!bfd_link_executable (info) && h)
2732
0
      {
2733
0
        if (!h->def_regular)
2734
0
    {
2735
0
      const char *v;
2736
2737
0
      switch (ELF_ST_VISIBILITY (h->other))
2738
0
        {
2739
0
        case STV_HIDDEN:
2740
0
          v = _("hidden symbol");
2741
0
          break;
2742
0
        case STV_INTERNAL:
2743
0
          v = _("internal symbol");
2744
0
          break;
2745
0
        case STV_PROTECTED:
2746
0
          v = _("protected symbol");
2747
0
          break;
2748
0
        default:
2749
0
          v = _("symbol");
2750
0
          break;
2751
0
        }
2752
2753
0
      _bfd_error_handler
2754
        /* xgettext:c-format */
2755
0
        (_("%pB: relocation R_386_GOTOFF against undefined %s"
2756
0
           " `%s' can not be used when making a shared object"),
2757
0
         input_bfd, v, h->root.root.string);
2758
0
      bfd_set_error (bfd_error_bad_value);
2759
0
      return false;
2760
0
    }
2761
0
        else if (!SYMBOL_REFERENCES_LOCAL_P (info, h)
2762
0
           && (h->type == STT_FUNC
2763
0
         || h->type == STT_OBJECT)
2764
0
           && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2765
0
    {
2766
0
      _bfd_error_handler
2767
        /* xgettext:c-format */
2768
0
        (_("%pB: relocation R_386_GOTOFF against protected %s"
2769
0
           " `%s' can not be used when making a shared object"),
2770
0
         input_bfd,
2771
0
         h->type == STT_FUNC ? "function" : "data",
2772
0
         h->root.root.string);
2773
0
      bfd_set_error (bfd_error_bad_value);
2774
0
      return false;
2775
0
    }
2776
0
      }
2777
2778
    /* Note that sgot is not involved in this
2779
       calculation.  We always want the start of .got.plt.  If we
2780
       defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2781
       permitted by the ABI, we might have to change this
2782
       calculation.  */
2783
0
    relocation -= htab->elf.sgotplt->output_section->vma
2784
0
      + htab->elf.sgotplt->output_offset;
2785
0
    break;
2786
2787
0
  case R_386_GOTPC:
2788
0
    if ((input_section->flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
2789
0
      goto non_alloc_error;
2790
2791
    /* Use global offset table as symbol value.  */
2792
0
    relocation = htab->elf.sgotplt->output_section->vma
2793
0
           + htab->elf.sgotplt->output_offset;
2794
0
    unresolved_reloc = false;
2795
0
    break;
2796
2797
0
  case R_386_PLT32:
2798
0
    if ((input_section->flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
2799
0
      goto non_alloc_error;
2800
2801
    /* Relocation is to the entry for this symbol in the
2802
       procedure linkage table.  */
2803
2804
    /* Resolve a PLT32 reloc against a local symbol directly,
2805
       without using the procedure linkage table.  */
2806
0
    if (h == NULL)
2807
0
      break;
2808
2809
0
    if ((h->plt.offset == (bfd_vma) -1
2810
0
         && eh->plt_got.offset == (bfd_vma) -1)
2811
0
        || htab->elf.splt == NULL)
2812
0
      {
2813
        /* We didn't make a PLT entry for this symbol.  This
2814
     happens when statically linking PIC code, or when
2815
     using -Bsymbolic.  */
2816
0
        break;
2817
0
      }
2818
2819
0
    if (h->plt.offset != (bfd_vma) -1)
2820
0
      {
2821
0
        if (htab->plt_second != NULL)
2822
0
    {
2823
0
      resolved_plt = htab->plt_second;
2824
0
      plt_offset = eh->plt_second.offset;
2825
0
    }
2826
0
        else
2827
0
    {
2828
0
      resolved_plt = htab->elf.splt;
2829
0
      plt_offset = h->plt.offset;
2830
0
    }
2831
0
      }
2832
0
    else
2833
0
      {
2834
0
        resolved_plt = htab->plt_got;
2835
0
        plt_offset = eh->plt_got.offset;
2836
0
      }
2837
2838
0
    relocation = (resolved_plt->output_section->vma
2839
0
      + resolved_plt->output_offset
2840
0
      + plt_offset);
2841
0
    unresolved_reloc = false;
2842
0
    break;
2843
2844
0
  case R_386_SIZE32:
2845
    /* Set to symbol size.  */
2846
0
    relocation = st_size;
2847
    /* Fall through.  */
2848
2849
0
  case R_386_32:
2850
0
  case R_386_PC32:
2851
0
    if ((input_section->flags & SEC_ALLOC) == 0
2852
0
        || is_vxworks_tls)
2853
0
      break;
2854
2855
0
    if (GENERATE_DYNAMIC_RELOCATION_P (false, info, eh, r_type,
2856
0
               sec, false,
2857
0
               resolved_to_zero,
2858
0
               (r_type == R_386_PC32)))
2859
0
      {
2860
0
        Elf_Internal_Rela outrel;
2861
0
        bool skip, relocate;
2862
0
        bool generate_dynamic_reloc = true;
2863
0
        asection *sreloc;
2864
2865
        /* When generating a shared object, these relocations
2866
     are copied into the output file to be resolved at run
2867
     time.  */
2868
2869
0
        skip = false;
2870
0
        relocate = false;
2871
2872
0
        outrel.r_offset =
2873
0
    _bfd_elf_section_offset (info->output_bfd, info,
2874
0
           input_section, rel->r_offset);
2875
0
        if (outrel.r_offset == (bfd_vma) -1)
2876
0
    skip = true;
2877
0
        else if (outrel.r_offset == (bfd_vma) -2)
2878
0
    skip = true, relocate = true;
2879
0
        outrel.r_offset += (input_section->output_section->vma
2880
0
          + input_section->output_offset);
2881
2882
0
        if (skip)
2883
0
    memset (&outrel, 0, sizeof outrel);
2884
0
        else if (COPY_INPUT_RELOC_P (false, info, h, r_type))
2885
0
    outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2886
0
        else
2887
0
    {
2888
      /* This symbol is local, or marked to become local.  */
2889
0
      relocate = true;
2890
      /* NB: Don't generate relative relocation here if it
2891
         has been generated by DT_RELR.  */
2892
0
      if (info->enable_dt_relr)
2893
0
        generate_dynamic_reloc = false;
2894
0
      else
2895
0
        {
2896
0
          outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2897
2898
0
          if (htab->params->report_relative_reloc)
2899
0
      _bfd_x86_elf_link_report_relative_reloc
2900
0
        (info, input_section, h, sym, "R_386_RELATIVE",
2901
0
         &outrel);
2902
0
        }
2903
0
    }
2904
2905
0
        if (generate_dynamic_reloc)
2906
0
    {
2907
0
      sreloc = elf_section_data (input_section)->sreloc;
2908
2909
0
      if (sreloc == NULL || sreloc->contents == NULL)
2910
0
        {
2911
0
          r = bfd_reloc_notsupported;
2912
0
          goto check_relocation_error;
2913
0
        }
2914
2915
0
      _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
2916
0
    }
2917
2918
        /* If this reloc is against an external symbol, we do
2919
     not want to fiddle with the addend.  Otherwise, we
2920
     need to include the symbol value so that it becomes
2921
     an addend for the dynamic reloc.  */
2922
0
        if (! relocate)
2923
0
    continue;
2924
0
      }
2925
0
    break;
2926
2927
0
  case R_386_TLS_IE:
2928
0
    if ((input_section->flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
2929
0
      goto non_alloc_error;
2930
2931
0
    if (!bfd_link_executable (info))
2932
0
      {
2933
0
        Elf_Internal_Rela outrel;
2934
0
        asection *sreloc;
2935
2936
0
        outrel.r_offset = rel->r_offset
2937
0
        + input_section->output_section->vma
2938
0
        + input_section->output_offset;
2939
0
        outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2940
2941
0
        if (htab->params->report_relative_reloc)
2942
0
    _bfd_x86_elf_link_report_relative_reloc
2943
0
      (info, input_section, h, sym, "R_386_RELATIVE",
2944
0
       &outrel);
2945
2946
0
        sreloc = elf_section_data (input_section)->sreloc;
2947
0
        if (sreloc == NULL)
2948
0
    abort ();
2949
0
        _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
2950
0
      }
2951
    /* Fall through */
2952
2953
0
  case R_386_TLS_GD:
2954
0
  case R_386_TLS_GOTDESC:
2955
0
  case R_386_TLS_DESC_CALL:
2956
0
  case R_386_TLS_IE_32:
2957
0
  case R_386_TLS_GOTIE:
2958
0
    if ((input_section->flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
2959
0
      goto non_alloc_error;
2960
2961
0
    tls_type = GOT_UNKNOWN;
2962
0
    if (h == NULL && local_got_offsets)
2963
0
      tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
2964
0
    else if (h != NULL)
2965
0
      tls_type = elf_x86_hash_entry(h)->tls_type;
2966
0
    if (tls_type == GOT_TLS_IE)
2967
0
      tls_type = GOT_TLS_IE_NEG;
2968
2969
0
     r_type_tls = r_type;
2970
0
    if (! elf_i386_tls_transition (info, input_bfd,
2971
0
           input_section, contents,
2972
0
           symtab_hdr, sym_hashes,
2973
0
           &r_type_tls, tls_type, rel,
2974
0
           relend, h, sym, true))
2975
0
      return false;
2976
2977
0
    expected_tls_le = htab->elf.target_os == is_solaris
2978
0
      ? R_386_TLS_LE : R_386_TLS_LE_32;
2979
0
    if (r_type_tls == expected_tls_le)
2980
0
      {
2981
        /* NB: Solaris only supports R_386_TLS_GD->R_386_TLS_LE.  */
2982
0
        BFD_ASSERT (! unresolved_reloc
2983
0
        && (htab->elf.target_os != is_solaris
2984
0
            || (htab->elf.target_os == is_solaris
2985
0
          && (r_type == R_386_TLS_GD
2986
0
              || r_type == R_386_TLS_IE
2987
0
              || r_type == R_386_TLS_GOTIE))));
2988
0
        if (r_type == R_386_TLS_GD)
2989
0
    {
2990
0
      unsigned int type;
2991
0
      bfd_vma roff;
2992
2993
      /* GD->LE transition.  */
2994
0
      type = *(contents + rel->r_offset - 2);
2995
0
      if (type == 0x04)
2996
0
        {
2997
          /* Change
2998
        leal foo@tlsgd(,%ebx,1), %eax
2999
        call ___tls_get_addr@PLT
3000
       into:
3001
        movl %gs:0, %eax
3002
        subl $foo@tpoff, %eax
3003
       (6 byte form of subl).  */
3004
0
          roff = rel->r_offset + 5;
3005
0
        }
3006
0
      else
3007
0
        {
3008
          /* Change
3009
        leal foo@tlsgd(%ebx), %eax
3010
        call ___tls_get_addr@PLT
3011
        nop
3012
       or
3013
        leal foo@tlsgd(%reg), %eax
3014
        call *___tls_get_addr@GOT(%reg)
3015
        which may be converted to
3016
        addr32 call ___tls_get_addr
3017
       into:
3018
        movl %gs:0, %eax; subl $foo@tpoff, %eax
3019
       (6 byte form of subl).  */
3020
0
          roff = rel->r_offset + 6;
3021
0
        }
3022
0
      memcpy (contents + roff - 8,
3023
0
        "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3024
0
      bfd_put_32 (info->output_bfd,
3025
0
            elf_i386_tpoff (info, relocation),
3026
0
            contents + roff);
3027
      /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X.  */
3028
0
      rel++;
3029
0
      wrel++;
3030
0
      continue;
3031
0
    }
3032
0
        else if (r_type == R_386_TLS_GOTDESC)
3033
0
    {
3034
      /* GDesc -> LE transition.
3035
         It's originally something like:
3036
         leal x@tlsdesc(%ebx), %eax
3037
3038
         leal x@ntpoff, %eax
3039
3040
         Registers other than %eax may be set up here.  */
3041
3042
0
      unsigned int val;
3043
0
      bfd_vma roff;
3044
3045
0
      roff = rel->r_offset;
3046
0
      val = bfd_get_8 (input_bfd, contents + roff - 1);
3047
3048
      /* Now modify the instruction as appropriate.  */
3049
      /* aoliva FIXME: remove the above and xor the byte
3050
         below with 0x86.  */
3051
0
      bfd_put_8 (info->output_bfd, val ^ 0x86,
3052
0
           contents + roff - 1);
3053
0
      bfd_put_32 (info->output_bfd,
3054
0
            -elf_i386_tpoff (info, relocation),
3055
0
            contents + roff);
3056
0
      continue;
3057
0
    }
3058
0
        else if (r_type == R_386_TLS_DESC_CALL)
3059
0
    {
3060
      /* GDesc -> LE transition.
3061
         It's originally:
3062
         call *(%eax)
3063
         Turn it into:
3064
         xchg %ax,%ax  */
3065
3066
0
      bfd_vma roff;
3067
3068
0
      roff = rel->r_offset;
3069
0
      bfd_put_8 (info->output_bfd, 0x66, contents + roff);
3070
0
      bfd_put_8 (info->output_bfd, 0x90, contents + roff + 1);
3071
0
      continue;
3072
0
    }
3073
0
        else if (r_type == R_386_TLS_IE)
3074
0
    {
3075
0
      unsigned int val;
3076
3077
      /* IE->LE transition:
3078
         Originally it can be one of:
3079
         movl foo, %eax
3080
         movl foo, %reg
3081
         addl foo, %reg
3082
         We change it into:
3083
         movl $foo, %eax
3084
         movl $foo, %reg
3085
         addl $foo, %reg.  */
3086
0
      val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3087
0
      if (val == 0xa1)
3088
0
        {
3089
          /* movl foo, %eax.  */
3090
0
          bfd_put_8 (info->output_bfd, 0xb8,
3091
0
         contents + rel->r_offset - 1);
3092
0
        }
3093
0
      else
3094
0
        {
3095
0
          unsigned int type;
3096
3097
0
          type = bfd_get_8 (input_bfd,
3098
0
          contents + rel->r_offset - 2);
3099
0
          switch (type)
3100
0
      {
3101
0
      case 0x8b:
3102
        /* movl */
3103
0
        bfd_put_8 (info->output_bfd, 0xc7,
3104
0
             contents + rel->r_offset - 2);
3105
0
        bfd_put_8 (info->output_bfd,
3106
0
             0xc0 | ((val >> 3) & 7),
3107
0
             contents + rel->r_offset - 1);
3108
0
        break;
3109
0
      case 0x03:
3110
        /* addl */
3111
0
        bfd_put_8 (info->output_bfd, 0x81,
3112
0
             contents + rel->r_offset - 2);
3113
0
        bfd_put_8 (info->output_bfd,
3114
0
             0xc0 | ((val >> 3) & 7),
3115
0
             contents + rel->r_offset - 1);
3116
0
        break;
3117
0
      default:
3118
0
        BFD_FAIL ();
3119
0
        break;
3120
0
      }
3121
0
        }
3122
0
      bfd_put_32 (info->output_bfd,
3123
0
            -elf_i386_tpoff (info, relocation),
3124
0
            contents + rel->r_offset);
3125
0
      continue;
3126
0
    }
3127
0
        else
3128
0
    {
3129
0
      unsigned int val, type;
3130
3131
      /* {IE_32,GOTIE}->LE transition:
3132
         Originally it can be one of:
3133
         subl foo(%reg1), %reg2
3134
         movl foo(%reg1), %reg2
3135
         addl foo(%reg1), %reg2
3136
         We change it into:
3137
         subl $foo, %reg2
3138
         movl $foo, %reg2 (6 byte form)
3139
         addl $foo, %reg2.  */
3140
0
      type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3141
0
      val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3142
0
      if (type == 0x8b)
3143
0
        {
3144
          /* movl */
3145
0
          bfd_put_8 (info->output_bfd, 0xc7,
3146
0
         contents + rel->r_offset - 2);
3147
0
          bfd_put_8 (info->output_bfd, 0xc0 | ((val >> 3) & 7),
3148
0
         contents + rel->r_offset - 1);
3149
0
        }
3150
0
      else if (type == 0x2b)
3151
0
        {
3152
          /* subl */
3153
0
          bfd_put_8 (info->output_bfd, 0x81,
3154
0
         contents + rel->r_offset - 2);
3155
0
          bfd_put_8 (info->output_bfd, 0xe8 | ((val >> 3) & 7),
3156
0
         contents + rel->r_offset - 1);
3157
0
        }
3158
0
      else if (type == 0x03)
3159
0
        {
3160
          /* addl */
3161
0
          bfd_put_8 (info->output_bfd, 0x81,
3162
0
         contents + rel->r_offset - 2);
3163
0
          bfd_put_8 (info->output_bfd, 0xc0 | ((val >> 3) & 7),
3164
0
         contents + rel->r_offset - 1);
3165
0
        }
3166
0
      else
3167
0
        BFD_FAIL ();
3168
0
      if (r_type == R_386_TLS_GOTIE)
3169
0
        bfd_put_32 (info->output_bfd,
3170
0
        -elf_i386_tpoff (info, relocation),
3171
0
        contents + rel->r_offset);
3172
0
      else
3173
0
        bfd_put_32 (info->output_bfd,
3174
0
        elf_i386_tpoff (info, relocation),
3175
0
        contents + rel->r_offset);
3176
0
      continue;
3177
0
    }
3178
0
      }
3179
3180
0
    if (htab->elf.sgot == NULL)
3181
0
      abort ();
3182
3183
0
    if (h != NULL)
3184
0
      {
3185
0
        off = h->got.offset;
3186
0
        offplt = elf_x86_hash_entry (h)->tlsdesc_got;
3187
0
      }
3188
0
    else
3189
0
      {
3190
0
        if (local_got_offsets == NULL)
3191
0
    abort ();
3192
3193
0
        off = local_got_offsets[r_symndx];
3194
0
        offplt = local_tlsdesc_gotents[r_symndx];
3195
0
      }
3196
3197
0
    if ((off & 1) != 0)
3198
0
      off &= ~1;
3199
0
    else
3200
0
      {
3201
0
        Elf_Internal_Rela outrel;
3202
0
        int dr_type;
3203
0
        asection *sreloc;
3204
3205
0
        if (htab->elf.srelgot == NULL)
3206
0
    abort ();
3207
3208
0
        indx = h && h->dynindx != -1 ? h->dynindx : 0;
3209
3210
0
        if (GOT_TLS_GDESC_P (tls_type))
3211
0
    {
3212
0
      outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3213
0
      BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3214
0
            <= htab->elf.sgotplt->size);
3215
0
      outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3216
0
             + htab->elf.sgotplt->output_offset
3217
0
             + offplt
3218
0
             + htab->sgotplt_jump_table_size);
3219
0
      sreloc = htab->rel_tls_desc;
3220
0
      _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
3221
0
      if (indx == 0)
3222
0
        {
3223
0
          BFD_ASSERT (! unresolved_reloc);
3224
0
          bfd_put_32 (info->output_bfd,
3225
0
          relocation - _bfd_x86_elf_dtpoff_base (info),
3226
0
          htab->elf.sgotplt->contents + offplt
3227
0
          + htab->sgotplt_jump_table_size + 4);
3228
0
        }
3229
0
      else
3230
0
        {
3231
0
          bfd_put_32 (info->output_bfd, 0,
3232
0
          htab->elf.sgotplt->contents + offplt
3233
0
          + htab->sgotplt_jump_table_size + 4);
3234
0
        }
3235
0
    }
3236
3237
0
        sreloc = htab->elf.srelgot;
3238
3239
0
        outrel.r_offset = (htab->elf.sgot->output_section->vma
3240
0
         + htab->elf.sgot->output_offset + off);
3241
3242
0
        if (GOT_TLS_GD_P (tls_type))
3243
0
    dr_type = R_386_TLS_DTPMOD32;
3244
0
        else if (GOT_TLS_GDESC_P (tls_type))
3245
0
    goto dr_done;
3246
0
        else if (tls_type == GOT_TLS_IE_POS)
3247
0
    dr_type = R_386_TLS_TPOFF;
3248
0
        else
3249
0
    dr_type = R_386_TLS_TPOFF32;
3250
3251
0
        if (dr_type == R_386_TLS_TPOFF && indx == 0)
3252
0
    bfd_put_32 (info->output_bfd,
3253
0
          relocation - _bfd_x86_elf_dtpoff_base (info),
3254
0
          htab->elf.sgot->contents + off);
3255
0
        else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3256
0
    bfd_put_32 (info->output_bfd,
3257
0
          _bfd_x86_elf_dtpoff_base (info) - relocation,
3258
0
          htab->elf.sgot->contents + off);
3259
0
        else if (dr_type != R_386_TLS_DESC)
3260
0
    bfd_put_32 (info->output_bfd, 0,
3261
0
          htab->elf.sgot->contents + off);
3262
0
        outrel.r_info = ELF32_R_INFO (indx, dr_type);
3263
3264
0
        _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
3265
3266
0
        if (GOT_TLS_GD_P (tls_type))
3267
0
    {
3268
0
      if (indx == 0)
3269
0
        {
3270
0
          BFD_ASSERT (! unresolved_reloc);
3271
0
          bfd_put_32 (info->output_bfd,
3272
0
          relocation - _bfd_x86_elf_dtpoff_base (info),
3273
0
          htab->elf.sgot->contents + off + 4);
3274
0
        }
3275
0
      else
3276
0
        {
3277
0
          bfd_put_32 (info->output_bfd, 0,
3278
0
          htab->elf.sgot->contents + off + 4);
3279
0
          outrel.r_info = ELF32_R_INFO (indx,
3280
0
                R_386_TLS_DTPOFF32);
3281
0
          outrel.r_offset += 4;
3282
0
          _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
3283
0
        }
3284
0
    }
3285
0
        else if (tls_type == GOT_TLS_IE_BOTH)
3286
0
    {
3287
0
      bfd_put_32 (info->output_bfd,
3288
0
            (indx == 0
3289
0
             ? relocation - _bfd_x86_elf_dtpoff_base (info)
3290
0
             : 0),
3291
0
            htab->elf.sgot->contents + off + 4);
3292
0
      outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3293
0
      outrel.r_offset += 4;
3294
0
      _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
3295
0
    }
3296
3297
0
      dr_done:
3298
0
        if (h != NULL)
3299
0
    h->got.offset |= 1;
3300
0
        else
3301
0
    local_got_offsets[r_symndx] |= 1;
3302
0
      }
3303
3304
0
    if (off >= (bfd_vma) -2
3305
0
        && ! GOT_TLS_GDESC_P (tls_type))
3306
0
      abort ();
3307
0
    if (r_type_tls == R_386_TLS_GOTDESC
3308
0
        || r_type_tls == R_386_TLS_DESC_CALL)
3309
0
      {
3310
0
        relocation = htab->sgotplt_jump_table_size + offplt;
3311
0
        unresolved_reloc = false;
3312
0
      }
3313
0
    else if (r_type_tls == r_type)
3314
0
      {
3315
0
        bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
3316
0
            + htab->elf.sgotplt->output_offset;
3317
0
        relocation = htab->elf.sgot->output_section->vma
3318
0
    + htab->elf.sgot->output_offset + off - g_o_t;
3319
0
        if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3320
0
      && tls_type == GOT_TLS_IE_BOTH)
3321
0
    relocation += 4;
3322
0
        if (r_type == R_386_TLS_IE)
3323
0
    relocation += g_o_t;
3324
0
        unresolved_reloc = false;
3325
0
      }
3326
0
    else if (r_type == R_386_TLS_GD)
3327
0
      {
3328
0
        unsigned int val, type;
3329
0
        bfd_vma roff;
3330
3331
        /* GD->IE transition.  */
3332
0
        type = *(contents + rel->r_offset - 2);
3333
0
        val = *(contents + rel->r_offset - 1);
3334
0
        if (type == 0x04)
3335
0
    {
3336
      /* Change
3337
      leal foo@tlsgd(,%ebx,1), %eax
3338
      call ___tls_get_addr@PLT
3339
         into:
3340
      movl %gs:0, %eax
3341
      subl $foo@gottpoff(%ebx), %eax.  */
3342
0
      val >>= 3;
3343
0
      roff = rel->r_offset - 3;
3344
0
    }
3345
0
        else
3346
0
    {
3347
      /* Change
3348
      leal foo@tlsgd(%ebx), %eax
3349
      call ___tls_get_addr@PLT
3350
      nop
3351
         or
3352
      leal foo@tlsgd(%reg), %eax
3353
      call *___tls_get_addr@GOT(%reg)
3354
      which may be converted to
3355
      addr32 call ___tls_get_addr
3356
         into:
3357
      movl %gs:0, %eax;
3358
      subl $foo@gottpoff(%reg), %eax.  */
3359
0
      roff = rel->r_offset - 2;
3360
0
    }
3361
0
        memcpy (contents + roff,
3362
0
          "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3363
0
        contents[roff + 7] = 0x80 | (val & 7);
3364
        /* If foo is used only with foo@gotntpoff(%reg) and
3365
     foo@indntpoff, but not with foo@gottpoff(%reg), change
3366
     subl $foo@gottpoff(%reg), %eax
3367
     into:
3368
     addl $foo@gotntpoff(%reg), %eax.  */
3369
0
        if (tls_type == GOT_TLS_IE_POS)
3370
0
    contents[roff + 6] = 0x03;
3371
0
        bfd_put_32 (info->output_bfd,
3372
0
        htab->elf.sgot->output_section->vma
3373
0
        + htab->elf.sgot->output_offset + off
3374
0
        - htab->elf.sgotplt->output_section->vma
3375
0
        - htab->elf.sgotplt->output_offset,
3376
0
        contents + roff + 8);
3377
        /* Skip R_386_PLT32 and R_386_GOT32X.  */
3378
0
        rel++;
3379
0
        wrel++;
3380
0
        continue;
3381
0
      }
3382
0
    else if (r_type == R_386_TLS_GOTDESC)
3383
0
      {
3384
        /* GDesc -> IE transition.
3385
     It's originally something like:
3386
     leal x@tlsdesc(%ebx), %eax
3387
3388
     Change it to:
3389
     movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3390
     or:
3391
     movl x@gottpoff(%ebx), %eax # before negl %eax
3392
3393
     Registers other than %eax may be set up here.  */
3394
3395
0
        bfd_vma roff;
3396
3397
        /* First, make sure it's a leal adding ebx to a 32-bit
3398
     offset into any register, although it's probably
3399
     almost always going to be eax.  */
3400
0
        roff = rel->r_offset;
3401
3402
        /* Now modify the instruction as appropriate.  */
3403
        /* To turn a leal into a movl in the form we use it, it
3404
     suffices to change the first byte from 0x8d to 0x8b.
3405
     aoliva FIXME: should we decide to keep the leal, all
3406
     we have to do is remove the statement below, and
3407
     adjust the relaxation of R_386_TLS_DESC_CALL.  */
3408
0
        bfd_put_8 (info->output_bfd, 0x8b, contents + roff - 2);
3409
3410
0
        if (tls_type == GOT_TLS_IE_BOTH)
3411
0
    off += 4;
3412
3413
0
        bfd_put_32 (info->output_bfd,
3414
0
        htab->elf.sgot->output_section->vma
3415
0
        + htab->elf.sgot->output_offset + off
3416
0
        - htab->elf.sgotplt->output_section->vma
3417
0
        - htab->elf.sgotplt->output_offset,
3418
0
        contents + roff);
3419
0
        continue;
3420
0
      }
3421
0
    else if (r_type == R_386_TLS_DESC_CALL)
3422
0
      {
3423
        /* GDesc -> IE transition.
3424
     It's originally:
3425
     call *(%eax)
3426
3427
     Change it to:
3428
     xchg %ax,%ax
3429
     or
3430
     negl %eax
3431
     depending on how we transformed the TLS_GOTDESC above.
3432
        */
3433
3434
0
        bfd_vma roff;
3435
3436
0
        roff = rel->r_offset;
3437
3438
        /* Now modify the instruction as appropriate.  */
3439
0
        if (tls_type != GOT_TLS_IE_NEG)
3440
0
    {
3441
      /* xchg %ax,%ax */
3442
0
      bfd_put_8 (info->output_bfd, 0x66, contents + roff);
3443
0
      bfd_put_8 (info->output_bfd, 0x90, contents + roff + 1);
3444
0
    }
3445
0
        else
3446
0
    {
3447
      /* negl %eax */
3448
0
      bfd_put_8 (info->output_bfd, 0xf7, contents + roff);
3449
0
      bfd_put_8 (info->output_bfd, 0xd8, contents + roff + 1);
3450
0
    }
3451
3452
0
        continue;
3453
0
      }
3454
0
    else
3455
0
      BFD_ASSERT (false);
3456
0
    break;
3457
3458
0
  case R_386_TLS_LDM:
3459
0
    if ((input_section->flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
3460
0
      goto non_alloc_error;
3461
3462
0
    if (! elf_i386_tls_transition (info, input_bfd,
3463
0
           input_section, contents,
3464
0
           symtab_hdr, sym_hashes,
3465
0
           &r_type, GOT_UNKNOWN, rel,
3466
0
           relend, h, sym, true))
3467
0
      return false;
3468
3469
0
    if (r_type != R_386_TLS_LDM)
3470
0
      {
3471
        /* LD->LE transition.  Change
3472
      leal foo@tlsldm(%ebx) %eax
3473
      call ___tls_get_addr@PLT
3474
     into:
3475
      movl %gs:0, %eax
3476
      nop
3477
      leal 0(%esi,1), %esi
3478
     or change
3479
      leal foo@tlsldm(%reg) %eax
3480
      call *___tls_get_addr@GOT(%reg)
3481
      which may be converted to
3482
      addr32 call ___tls_get_addr
3483
     into:
3484
      movl %gs:0, %eax
3485
      leal 0(%esi), %esi  */
3486
0
        expected_tls_le = htab->elf.target_os == is_solaris
3487
0
    ? R_386_TLS_LE : R_386_TLS_LE_32;
3488
0
        BFD_ASSERT (r_type == expected_tls_le);
3489
0
        if (*(contents + rel->r_offset + 4) == 0xff
3490
0
      || *(contents + rel->r_offset + 4) == 0x67)
3491
0
    memcpy (contents + rel->r_offset - 2,
3492
0
      "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
3493
0
        else
3494
0
    memcpy (contents + rel->r_offset - 2,
3495
0
      "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3496
        /* Skip R_386_PC32/R_386_PLT32.  */
3497
0
        rel++;
3498
0
        wrel++;
3499
0
        continue;
3500
0
      }
3501
3502
0
    if (htab->elf.sgot == NULL)
3503
0
      abort ();
3504
3505
0
    off = htab->tls_ld_or_ldm_got.offset;
3506
0
    if (off & 1)
3507
0
      off &= ~1;
3508
0
    else
3509
0
      {
3510
0
        Elf_Internal_Rela outrel;
3511
3512
0
        if (htab->elf.srelgot == NULL)
3513
0
    abort ();
3514
3515
0
        outrel.r_offset = (htab->elf.sgot->output_section->vma
3516
0
         + htab->elf.sgot->output_offset + off);
3517
3518
0
        bfd_put_32 (info->output_bfd, 0,
3519
0
        htab->elf.sgot->contents + off);
3520
0
        bfd_put_32 (info->output_bfd, 0,
3521
0
        htab->elf.sgot->contents + off + 4);
3522
0
        outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3523
0
        _bfd_elf_append_rel (info->output_bfd, htab->elf.srelgot, &outrel);
3524
0
        htab->tls_ld_or_ldm_got.offset |= 1;
3525
0
      }
3526
0
    relocation = htab->elf.sgot->output_section->vma
3527
0
           + htab->elf.sgot->output_offset + off
3528
0
           - htab->elf.sgotplt->output_section->vma
3529
0
           - htab->elf.sgotplt->output_offset;
3530
0
    unresolved_reloc = false;
3531
0
    break;
3532
3533
0
  case R_386_TLS_LDO_32:
3534
0
    if ((input_section->flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
3535
0
      goto non_alloc_error;
3536
3537
0
    if (!bfd_link_executable (info)
3538
0
        || (input_section->flags & SEC_CODE) == 0)
3539
0
      relocation -= _bfd_x86_elf_dtpoff_base (info);
3540
0
    else
3541
      /* When converting LDO to LE, we must negate.  */
3542
0
      relocation = -elf_i386_tpoff (info, relocation);
3543
0
    break;
3544
3545
0
  case R_386_TLS_LE_32:
3546
0
  case R_386_TLS_LE:
3547
0
    if ((input_section->flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
3548
0
      goto non_alloc_error;
3549
3550
0
    if (!bfd_link_executable (info))
3551
0
      {
3552
0
        Elf_Internal_Rela outrel;
3553
0
        asection *sreloc;
3554
3555
0
        outrel.r_offset = rel->r_offset
3556
0
        + input_section->output_section->vma
3557
0
        + input_section->output_offset;
3558
0
        if (h != NULL && h->dynindx != -1)
3559
0
    indx = h->dynindx;
3560
0
        else
3561
0
    indx = 0;
3562
0
        if (r_type == R_386_TLS_LE_32)
3563
0
    outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3564
0
        else
3565
0
    outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3566
0
        sreloc = elf_section_data (input_section)->sreloc;
3567
0
        if (sreloc == NULL)
3568
0
    abort ();
3569
0
        _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
3570
0
        if (indx)
3571
0
    continue;
3572
0
        else if (r_type == R_386_TLS_LE_32)
3573
0
    relocation = _bfd_x86_elf_dtpoff_base (info) - relocation;
3574
0
        else
3575
0
    relocation -= _bfd_x86_elf_dtpoff_base (info);
3576
0
      }
3577
0
    else if (r_type == R_386_TLS_LE_32)
3578
0
      relocation = elf_i386_tpoff (info, relocation);
3579
0
    else
3580
0
      relocation = -elf_i386_tpoff (info, relocation);
3581
0
    break;
3582
3583
0
  default:
3584
0
    break;
3585
0
  }
3586
3587
      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3588
   because such sections are not SEC_ALLOC and thus ld.so will
3589
   not process them.  */
3590
0
      if (unresolved_reloc
3591
0
    && !((input_section->flags & SEC_DEBUGGING) != 0
3592
0
         && h->def_dynamic)
3593
0
    && _bfd_elf_section_offset (info->output_bfd, info, input_section,
3594
0
              rel->r_offset) != (bfd_vma) -1)
3595
0
  {
3596
0
    _bfd_error_handler
3597
      /* xgettext:c-format */
3598
0
      (_("%pB(%pA+%#" PRIx64 "): unresolvable %s relocation against symbol `%s'"),
3599
0
       input_bfd,
3600
0
       input_section,
3601
0
       (uint64_t) rel->r_offset,
3602
0
       howto->name,
3603
0
       h->root.root.string);
3604
0
    return false;
3605
0
  }
3606
3607
0
    do_relocation:
3608
0
      r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3609
0
            contents, rel->r_offset,
3610
0
            relocation, 0);
3611
3612
0
    check_relocation_error:
3613
0
      if (r != bfd_reloc_ok)
3614
0
  {
3615
0
    const char *name;
3616
3617
0
    if (h != NULL)
3618
0
      name = h->root.root.string;
3619
0
    else
3620
0
      {
3621
0
        name = bfd_elf_string_from_elf_section (input_bfd,
3622
0
                  symtab_hdr->sh_link,
3623
0
                  sym->st_name);
3624
0
        if (name == NULL)
3625
0
    return false;
3626
0
        if (*name == '\0')
3627
0
    name = bfd_section_name (sec);
3628
0
      }
3629
3630
0
    if (r == bfd_reloc_overflow)
3631
0
      (*info->callbacks->reloc_overflow)
3632
0
        (info, (h ? &h->root : NULL), name, howto->name,
3633
0
         (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3634
0
    else
3635
0
      {
3636
0
        _bfd_x86_elf_link_report_relocation_error (input_bfd,
3637
0
               input_section,
3638
0
               name, rel,
3639
0
               howto, r);
3640
0
        return false;
3641
0
      }
3642
0
  }
3643
3644
0
      if (wrel != rel)
3645
0
  *wrel = *rel;
3646
0
    }
3647
3648
0
  if (wrel != rel)
3649
0
    {
3650
0
      Elf_Internal_Shdr *rel_hdr;
3651
0
      size_t deleted = rel - wrel;
3652
3653
0
      rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
3654
0
      rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3655
0
      rel_hdr = _bfd_elf_single_rel_hdr (input_section);
3656
0
      rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3657
0
      input_section->reloc_count -= deleted;
3658
0
    }
3659
3660
0
  return true;
3661
0
}
3662
3663
/* Finish up dynamic symbol handling.  We set the contents of various
3664
   dynamic sections here.  */
3665
3666
static bool
3667
elf_i386_finish_dynamic_symbol (struct bfd_link_info *info,
3668
        struct elf_link_hash_entry *h,
3669
        Elf_Internal_Sym *sym)
3670
0
{
3671
0
  struct elf_x86_link_hash_table *htab;
3672
0
  unsigned plt_entry_size;
3673
0
  struct elf_x86_link_hash_entry *eh;
3674
0
  bool local_undefweak;
3675
0
  bool use_plt_second;
3676
3677
0
  htab = elf_x86_hash_table (info, I386_ELF_DATA);
3678
3679
0
  plt_entry_size = htab->plt.plt_entry_size;
3680
3681
  /* Use the second PLT section only if there is .plt section.  */
3682
0
  use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
3683
3684
0
  eh = (struct elf_x86_link_hash_entry *) h;
3685
0
  if (eh->no_finish_dynamic_symbol)
3686
0
    abort ();
3687
3688
  /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
3689
     resolved undefined weak symbols in executable so that their
3690
     references have value 0 at run-time.  */
3691
0
  local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
3692
3693
0
  if (h->plt.offset != (bfd_vma) -1)
3694
0
    {
3695
0
      bfd_vma plt_index, plt_offset;
3696
0
      bfd_vma got_offset;
3697
0
      Elf_Internal_Rela rel;
3698
0
      bfd_byte *loc;
3699
0
      asection *plt, *resolved_plt, *gotplt, *relplt;
3700
3701
      /* When building a static executable, use .iplt, .igot.plt and
3702
   .rel.iplt sections for STT_GNU_IFUNC symbols.  */
3703
0
      if (htab->elf.splt != NULL)
3704
0
  {
3705
0
    plt = htab->elf.splt;
3706
0
    gotplt = htab->elf.sgotplt;
3707
0
    relplt = htab->elf.srelplt;
3708
0
  }
3709
0
      else
3710
0
  {
3711
0
    plt = htab->elf.iplt;
3712
0
    gotplt = htab->elf.igotplt;
3713
0
    relplt = htab->elf.irelplt;
3714
0
  }
3715
3716
0
      VERIFY_PLT_ENTRY (info, h, plt, gotplt, relplt, local_undefweak)
3717
3718
      /* Get the index in the procedure linkage table which
3719
   corresponds to this symbol.  This is the index of this symbol
3720
   in all the symbols for which we are making plt entries.  The
3721
   first entry in the procedure linkage table is reserved.
3722
3723
   Get the offset into the .got table of the entry that
3724
   corresponds to this function.  Each .got entry is 4 bytes.
3725
   The first three are reserved.
3726
3727
   For static executables, we don't reserve anything.  */
3728
3729
0
      if (plt == htab->elf.splt)
3730
0
  {
3731
0
    got_offset = (h->plt.offset / plt_entry_size
3732
0
      - htab->plt.has_plt0);
3733
0
    got_offset = (got_offset + 3) * 4;
3734
0
  }
3735
0
      else
3736
0
  {
3737
0
    got_offset = h->plt.offset / plt_entry_size;
3738
0
    got_offset = got_offset * 4;
3739
0
  }
3740
3741
      /* Fill in the entry in the procedure linkage table and update
3742
   the first slot.  */
3743
0
      memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
3744
0
        plt_entry_size);
3745
3746
0
      if (use_plt_second)
3747
0
  {
3748
0
    const bfd_byte *plt_entry;
3749
0
    if (bfd_link_pic (info))
3750
0
      plt_entry = htab->non_lazy_plt->pic_plt_entry;
3751
0
    else
3752
0
      plt_entry = htab->non_lazy_plt->plt_entry;
3753
0
    memcpy (htab->plt_second->contents + eh->plt_second.offset,
3754
0
      plt_entry, htab->non_lazy_plt->plt_entry_size);
3755
3756
0
    resolved_plt = htab->plt_second;
3757
0
    plt_offset = eh->plt_second.offset;
3758
0
  }
3759
0
      else
3760
0
  {
3761
0
    resolved_plt = plt;
3762
0
    plt_offset = h->plt.offset;
3763
0
  }
3764
3765
0
      if (! bfd_link_pic (info))
3766
0
  {
3767
0
    bfd_put_32 (info->output_bfd,
3768
0
          (gotplt->output_section->vma
3769
0
           + gotplt->output_offset
3770
0
           + got_offset),
3771
0
          resolved_plt->contents + plt_offset
3772
0
          + htab->plt.plt_got_offset);
3773
3774
0
    if (htab->elf.target_os == is_vxworks)
3775
0
      {
3776
0
        int s, k, reloc_index;
3777
3778
        /* Create the R_386_32 relocation referencing the GOT
3779
     for this PLT entry.  */
3780
3781
        /* S: Current slot number (zero-based).  */
3782
0
        s = ((h->plt.offset - htab->plt.plt_entry_size)
3783
0
       / htab->plt.plt_entry_size);
3784
        /* K: Number of relocations for PLTResolve. */
3785
0
        if (bfd_link_pic (info))
3786
0
    k = PLTRESOLVE_RELOCS_SHLIB;
3787
0
        else
3788
0
    k = PLTRESOLVE_RELOCS;
3789
        /* Skip the PLTresolve relocations, and the relocations for
3790
     the other PLT slots. */
3791
0
        reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3792
0
        loc = (htab->srelplt2->contents + reloc_index
3793
0
         * sizeof (Elf32_External_Rel));
3794
3795
0
        rel.r_offset = (plt->output_section->vma
3796
0
            + plt->output_offset
3797
0
            + h->plt.offset + 2),
3798
0
        rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3799
0
        bfd_elf32_swap_reloc_out (info->output_bfd, &rel, loc);
3800
3801
        /* Create the R_386_32 relocation referencing the beginning of
3802
     the PLT for this GOT entry.  */
3803
0
        rel.r_offset = (htab->elf.sgotplt->output_section->vma
3804
0
            + htab->elf.sgotplt->output_offset
3805
0
            + got_offset);
3806
0
        rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3807
0
        bfd_elf32_swap_reloc_out (info->output_bfd, &rel,
3808
0
          loc + sizeof (Elf32_External_Rel));
3809
0
      }
3810
0
  }
3811
0
      else
3812
0
  {
3813
0
    bfd_put_32 (info->output_bfd, got_offset,
3814
0
          resolved_plt->contents + plt_offset
3815
0
          + htab->plt.plt_got_offset);
3816
0
  }
3817
3818
      /* Fill in the entry in the global offset table.  Leave the entry
3819
   as zero for undefined weak symbol in PIE.  No PLT relocation
3820
   against undefined weak symbol in PIE.  */
3821
0
      if (!local_undefweak)
3822
0
  {
3823
0
    if (htab->plt.has_plt0)
3824
0
      bfd_put_32 (info->output_bfd,
3825
0
      (plt->output_section->vma
3826
0
       + plt->output_offset
3827
0
       + h->plt.offset
3828
0
       + htab->lazy_plt->plt_lazy_offset),
3829
0
      gotplt->contents + got_offset);
3830
3831
    /* Fill in the entry in the .rel.plt section.  */
3832
0
    rel.r_offset = (gotplt->output_section->vma
3833
0
        + gotplt->output_offset
3834
0
        + got_offset);
3835
0
    if (PLT_LOCAL_IFUNC_P (info, h))
3836
0
      {
3837
0
        info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3838
0
              h->root.root.string,
3839
0
              h->root.u.def.section->owner);
3840
3841
        /* If an STT_GNU_IFUNC symbol is locally defined, generate
3842
     R_386_IRELATIVE instead of R_386_JUMP_SLOT.  Store addend
3843
     in the .got.plt section.  */
3844
0
        bfd_put_32 (info->output_bfd,
3845
0
        (h->root.u.def.value
3846
0
         + h->root.u.def.section->output_section->vma
3847
0
         + h->root.u.def.section->output_offset),
3848
0
        gotplt->contents + got_offset);
3849
0
        rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3850
3851
0
        if (htab->params->report_relative_reloc)
3852
0
    _bfd_x86_elf_link_report_relative_reloc
3853
0
      (info, relplt, h, sym, "R_386_IRELATIVE", &rel);
3854
3855
        /* R_386_IRELATIVE comes last.  */
3856
0
        plt_index = htab->next_irelative_index--;
3857
0
      }
3858
0
    else
3859
0
      {
3860
0
        rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3861
0
        plt_index = htab->next_jump_slot_index++;
3862
0
      }
3863
3864
0
    loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
3865
0
    bfd_elf32_swap_reloc_out (info->output_bfd, &rel, loc);
3866
3867
    /* Don't fill the second and third slots in PLT entry for
3868
       static executables nor without PLT0.  */
3869
0
    if (plt == htab->elf.splt && htab->plt.has_plt0)
3870
0
      {
3871
0
        bfd_put_32 (info->output_bfd,
3872
0
        plt_index * sizeof (Elf32_External_Rel),
3873
0
        plt->contents + h->plt.offset
3874
0
        + htab->lazy_plt->plt_reloc_offset);
3875
0
        bfd_put_32 (info->output_bfd,
3876
0
        - (h->plt.offset
3877
0
           + htab->lazy_plt->plt_plt_offset + 4),
3878
0
        (plt->contents + h->plt.offset
3879
0
         + htab->lazy_plt->plt_plt_offset));
3880
0
      }
3881
0
  }
3882
0
    }
3883
0
  else if (eh->plt_got.offset != (bfd_vma) -1)
3884
0
    {
3885
0
      bfd_vma got_offset, plt_offset;
3886
0
      asection *plt, *got, *gotplt;
3887
0
      const bfd_byte *got_plt_entry;
3888
3889
      /* Set the entry in the GOT procedure linkage table.  */
3890
0
      plt = htab->plt_got;
3891
0
      got = htab->elf.sgot;
3892
0
      gotplt = htab->elf.sgotplt;
3893
0
      got_offset = h->got.offset;
3894
3895
0
      if (got_offset == (bfd_vma) -1
3896
0
    || plt == NULL
3897
0
    || got == NULL
3898
0
    || gotplt == NULL)
3899
0
  abort ();
3900
3901
      /* Fill in the entry in the GOT procedure linkage table.  */
3902
0
      if (! bfd_link_pic (info))
3903
0
  {
3904
0
    got_plt_entry = htab->non_lazy_plt->plt_entry;
3905
0
    got_offset += got->output_section->vma + got->output_offset;
3906
0
  }
3907
0
      else
3908
0
  {
3909
0
    got_plt_entry = htab->non_lazy_plt->pic_plt_entry;
3910
0
    got_offset += (got->output_section->vma
3911
0
       + got->output_offset
3912
0
       - gotplt->output_section->vma
3913
0
       - gotplt->output_offset);
3914
0
  }
3915
3916
0
      plt_offset = eh->plt_got.offset;
3917
0
      memcpy (plt->contents + plt_offset, got_plt_entry,
3918
0
        htab->non_lazy_plt->plt_entry_size);
3919
0
      bfd_put_32 (info->output_bfd, got_offset,
3920
0
      (plt->contents + plt_offset
3921
0
       + htab->non_lazy_plt->plt_got_offset));
3922
0
    }
3923
3924
0
  if (!local_undefweak
3925
0
      && !h->def_regular
3926
0
      && (h->plt.offset != (bfd_vma) -1
3927
0
    || eh->plt_got.offset != (bfd_vma) -1))
3928
0
    {
3929
      /* Mark the symbol as undefined, rather than as defined in
3930
   the .plt section.  Leave the value if there were any
3931
   relocations where pointer equality matters (this is a clue
3932
   for the dynamic linker, to make function pointer
3933
   comparisons work between an application and shared
3934
   library), otherwise set it to zero.  If a function is only
3935
   called from a binary, there is no need to slow down
3936
   shared libraries because of that.  */
3937
0
      sym->st_shndx = SHN_UNDEF;
3938
0
      if (!h->pointer_equality_needed)
3939
0
  sym->st_value = 0;
3940
0
    }
3941
3942
0
  _bfd_x86_elf_link_fixup_ifunc_symbol (info, htab, h, sym);
3943
3944
  /* Don't generate dynamic GOT relocation against undefined weak
3945
     symbol in executable.  */
3946
0
  if (h->got.offset != (bfd_vma) -1
3947
0
      && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry(h)->tls_type)
3948
0
      && (elf_x86_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
3949
0
      && !local_undefweak)
3950
0
    {
3951
0
      Elf_Internal_Rela rel;
3952
0
      asection *relgot = htab->elf.srelgot;
3953
0
      const char *relative_reloc_name = NULL;
3954
0
      bool generate_dynamic_reloc = true;
3955
3956
      /* This symbol has an entry in the global offset table.  Set it
3957
   up.  */
3958
3959
0
      if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3960
0
  abort ();
3961
3962
0
      rel.r_offset = (htab->elf.sgot->output_section->vma
3963
0
          + htab->elf.sgot->output_offset
3964
0
          + (h->got.offset & ~(bfd_vma) 1));
3965
3966
      /* If this is a static link, or it is a -Bsymbolic link and the
3967
   symbol is defined locally or was forced to be local because
3968
   of a version file, we just want to emit a RELATIVE reloc.
3969
   The entry in the global offset table will already have been
3970
   initialized in the relocate_section function.  */
3971
0
      if (h->def_regular
3972
0
    && h->type == STT_GNU_IFUNC)
3973
0
  {
3974
0
    if (h->plt.offset == (bfd_vma) -1)
3975
0
      {
3976
        /* STT_GNU_IFUNC is referenced without PLT.  */
3977
0
        if (htab->elf.splt == NULL)
3978
0
    {
3979
      /* use .rel[a].iplt section to store .got relocations
3980
         in static executable.  */
3981
0
      relgot = htab->elf.irelplt;
3982
0
    }
3983
0
        if (SYMBOL_REFERENCES_LOCAL_P (info, h))
3984
0
    {
3985
0
      info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3986
0
            h->root.root.string,
3987
0
            h->root.u.def.section->owner);
3988
3989
0
      bfd_put_32 (info->output_bfd,
3990
0
            (h->root.u.def.value
3991
0
             + h->root.u.def.section->output_section->vma
3992
0
             + h->root.u.def.section->output_offset),
3993
0
            htab->elf.sgot->contents + h->got.offset);
3994
0
      rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3995
0
      relative_reloc_name = "R_386_IRELATIVE";
3996
0
    }
3997
0
        else
3998
0
    goto do_glob_dat;
3999
0
      }
4000
0
    else if (bfd_link_pic (info))
4001
0
      {
4002
        /* Generate R_386_GLOB_DAT.  */
4003
0
        goto do_glob_dat;
4004
0
      }
4005
0
    else
4006
0
      {
4007
0
        asection *plt;
4008
0
        bfd_vma plt_offset;
4009
4010
0
        if (!h->pointer_equality_needed)
4011
0
    abort ();
4012
4013
        /* For non-shared object, we can't use .got.plt, which
4014
     contains the real function addres if we need pointer
4015
     equality.  We load the GOT entry with the PLT entry.  */
4016
0
        if (htab->plt_second != NULL)
4017
0
    {
4018
0
      plt = htab->plt_second;
4019
0
      plt_offset = eh->plt_second.offset;
4020
0
    }
4021
0
        else
4022
0
    {
4023
0
      plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4024
0
      plt_offset = h->plt.offset;
4025
0
    }
4026
0
        bfd_put_32 (info->output_bfd,
4027
0
        (plt->output_section->vma
4028
0
         + plt->output_offset + plt_offset),
4029
0
        htab->elf.sgot->contents + h->got.offset);
4030
0
        return true;
4031
0
      }
4032
0
  }
4033
0
      else if (bfd_link_pic (info)
4034
0
         && SYMBOL_REFERENCES_LOCAL_P (info, h))
4035
0
  {
4036
0
    BFD_ASSERT((h->got.offset & 1) != 0);
4037
0
    if (info->enable_dt_relr)
4038
0
      generate_dynamic_reloc = false;
4039
0
    else
4040
0
      {
4041
0
        rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4042
0
        relative_reloc_name = "R_386_RELATIVE";
4043
0
      }
4044
0
  }
4045
0
      else
4046
0
  {
4047
0
    BFD_ASSERT((h->got.offset & 1) == 0);
4048
0
  do_glob_dat:
4049
0
    bfd_put_32 (info->output_bfd, 0,
4050
0
          htab->elf.sgot->contents + h->got.offset);
4051
0
    rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
4052
0
  }
4053
4054
0
      if (generate_dynamic_reloc)
4055
0
  {
4056
0
    if (relative_reloc_name != NULL
4057
0
        && htab->params->report_relative_reloc)
4058
0
      _bfd_x86_elf_link_report_relative_reloc
4059
0
        (info, relgot, h, sym, relative_reloc_name, &rel);
4060
4061
0
    _bfd_elf_append_rel (info->output_bfd, relgot, &rel);
4062
0
  }
4063
0
    }
4064
4065
0
  if (h->needs_copy)
4066
0
    {
4067
0
      Elf_Internal_Rela rel;
4068
0
      asection *s;
4069
4070
      /* This symbol needs a copy reloc.  Set it up.  */
4071
0
      VERIFY_COPY_RELOC (h, htab)
4072
4073
0
      rel.r_offset = (h->root.u.def.value
4074
0
          + h->root.u.def.section->output_section->vma
4075
0
          + h->root.u.def.section->output_offset);
4076
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
4077
0
      if (h->root.u.def.section == htab->elf.sdynrelro)
4078
0
  s = htab->elf.sreldynrelro;
4079
0
      else
4080
0
  s = htab->elf.srelbss;
4081
0
      _bfd_elf_append_rel (info->output_bfd, s, &rel);
4082
0
    }
4083
4084
0
  return true;
4085
0
}
4086
4087
/* Finish up local dynamic symbol handling.  We set the contents of
4088
   various dynamic sections here.  */
4089
4090
static int
4091
elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
4092
0
{
4093
0
  struct elf_link_hash_entry *h
4094
0
    = (struct elf_link_hash_entry *) *slot;
4095
0
  struct bfd_link_info *info
4096
0
    = (struct bfd_link_info *) inf;
4097
4098
0
  return elf_i386_finish_dynamic_symbol (info, h, NULL);
4099
0
}
4100
4101
/* Finish up undefined weak symbol handling in PIE.  Fill its PLT entry
4102
   here since undefined weak symbol may not be dynamic and may not be
4103
   called for elf_i386_finish_dynamic_symbol.  */
4104
4105
static bool
4106
elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
4107
              void *inf)
4108
0
{
4109
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
4110
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
4111
4112
0
  if (h->root.type != bfd_link_hash_undefweak
4113
0
      || h->dynindx != -1)
4114
0
    return true;
4115
4116
0
  return elf_i386_finish_dynamic_symbol (info, h, NULL);
4117
0
}
4118
4119
/* Used to decide how to sort relocs in an optimal manner for the
4120
   dynamic linker, before writing them out.  */
4121
4122
static enum elf_reloc_type_class
4123
elf_i386_reloc_type_class (const struct bfd_link_info *info,
4124
         const asection *rel_sec ATTRIBUTE_UNUSED,
4125
         const Elf_Internal_Rela *rela)
4126
0
{
4127
0
  bfd *abfd = info->output_bfd;
4128
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
4129
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4130
4131
0
  if (htab->dynsym != NULL
4132
0
      && htab->dynsym->contents != NULL)
4133
0
    {
4134
      /* Check relocation against STT_GNU_IFUNC symbol if there are
4135
   dynamic symbols.  */
4136
0
      unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
4137
0
      if (r_symndx != STN_UNDEF)
4138
0
  {
4139
0
    Elf_Internal_Sym sym;
4140
0
    if (!bed->s->swap_symbol_in (abfd,
4141
0
               (htab->dynsym->contents
4142
0
          + r_symndx * sizeof (Elf32_External_Sym)),
4143
0
               0, &sym))
4144
0
      abort ();
4145
4146
0
    if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
4147
0
      return reloc_class_ifunc;
4148
0
  }
4149
0
    }
4150
4151
0
  switch (ELF32_R_TYPE (rela->r_info))
4152
0
    {
4153
0
    case R_386_IRELATIVE:
4154
0
      return reloc_class_ifunc;
4155
0
    case R_386_RELATIVE:
4156
0
      return reloc_class_relative;
4157
0
    case R_386_JUMP_SLOT:
4158
0
      return reloc_class_plt;
4159
0
    case R_386_COPY:
4160
0
      return reloc_class_copy;
4161
0
    default:
4162
0
      return reloc_class_normal;
4163
0
    }
4164
0
}
4165
4166
/* Finish up the dynamic sections.  */
4167
4168
static bool
4169
elf_i386_finish_dynamic_sections (struct bfd_link_info *info,
4170
          bfd_byte *buf)
4171
0
{
4172
0
  struct elf_x86_link_hash_table *htab;
4173
4174
0
  htab = _bfd_x86_elf_finish_dynamic_sections (info, buf);
4175
0
  if (htab == NULL)
4176
0
    return false;
4177
4178
0
  if (!htab->elf.dynamic_sections_created)
4179
0
    return true;
4180
4181
0
  if (htab->elf.splt && htab->elf.splt->size > 0)
4182
0
    {
4183
0
      if (bfd_is_abs_section (htab->elf.splt->output_section))
4184
0
  {
4185
0
    info->callbacks->fatal
4186
0
      (_("%P: discarded output section: `%pA'\n"),
4187
0
       htab->elf.splt);
4188
0
    return false;
4189
0
  }
4190
4191
      /* UnixWare sets the entsize of .plt to 4, although that doesn't
4192
   really seem like the right value.  */
4193
0
      elf_section_data (htab->elf.splt->output_section)
4194
0
  ->this_hdr.sh_entsize = 4;
4195
4196
0
      if (htab->plt.has_plt0)
4197
0
  {
4198
    /* Fill in the special first entry in the procedure linkage
4199
       table.  */
4200
0
    memcpy (htab->elf.splt->contents, htab->plt.plt0_entry,
4201
0
      htab->lazy_plt->plt0_entry_size);
4202
0
    memset (htab->elf.splt->contents + htab->lazy_plt->plt0_entry_size,
4203
0
      htab->plt0_pad_byte,
4204
0
      htab->plt.plt_entry_size - htab->lazy_plt->plt0_entry_size);
4205
0
    if (!bfd_link_pic (info))
4206
0
      {
4207
0
        bfd_put_32 (info->output_bfd,
4208
0
        (htab->elf.sgotplt->output_section->vma
4209
0
         + htab->elf.sgotplt->output_offset
4210
0
         + 4),
4211
0
        htab->elf.splt->contents
4212
0
        + htab->lazy_plt->plt0_got1_offset);
4213
0
        bfd_put_32 (info->output_bfd,
4214
0
        (htab->elf.sgotplt->output_section->vma
4215
0
         + htab->elf.sgotplt->output_offset
4216
0
         + 8),
4217
0
        htab->elf.splt->contents
4218
0
        + htab->lazy_plt->plt0_got2_offset);
4219
4220
0
        if (htab->elf.target_os == is_vxworks)
4221
0
    {
4222
0
      Elf_Internal_Rela rel;
4223
0
      int num_plts = (htab->elf.splt->size
4224
0
          / htab->plt.plt_entry_size) - 1;
4225
0
      unsigned char *p;
4226
0
      asection *srelplt2 = htab->srelplt2;
4227
4228
      /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4229
         + 4.  On IA32 we use REL relocations so the
4230
         addend goes in the PLT directly.  */
4231
0
      rel.r_offset = (htab->elf.splt->output_section->vma
4232
0
          + htab->elf.splt->output_offset
4233
0
          + htab->lazy_plt->plt0_got1_offset);
4234
0
      rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4235
0
               R_386_32);
4236
0
      bfd_elf32_swap_reloc_out (info->output_bfd, &rel,
4237
0
              srelplt2->contents);
4238
      /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4239
         + 8.  */
4240
0
      rel.r_offset = (htab->elf.splt->output_section->vma
4241
0
          + htab->elf.splt->output_offset
4242
0
          + htab->lazy_plt->plt0_got2_offset);
4243
0
      rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4244
0
               R_386_32);
4245
0
      bfd_elf32_swap_reloc_out (info->output_bfd, &rel,
4246
0
              srelplt2->contents +
4247
0
              sizeof (Elf32_External_Rel));
4248
      /* Correct the .rel.plt.unloaded relocations.  */
4249
0
      p = srelplt2->contents;
4250
0
      if (bfd_link_pic (info))
4251
0
        p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4252
0
      else
4253
0
        p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4254
4255
0
      for (; num_plts; num_plts--)
4256
0
        {
4257
0
          bfd_elf32_swap_reloc_in (info->output_bfd, p, &rel);
4258
0
          rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4259
0
             R_386_32);
4260
0
          bfd_elf32_swap_reloc_out (info->output_bfd, &rel, p);
4261
0
          p += sizeof (Elf32_External_Rel);
4262
4263
0
          bfd_elf32_swap_reloc_in (info->output_bfd, p, &rel);
4264
0
          rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
4265
0
             R_386_32);
4266
0
          bfd_elf32_swap_reloc_out (info->output_bfd, &rel, p);
4267
0
          p += sizeof (Elf32_External_Rel);
4268
0
        }
4269
0
    }
4270
0
      }
4271
0
  }
4272
0
    }
4273
4274
  /* Fill PLT entries for undefined weak symbols in PIE.  */
4275
0
  if (bfd_link_pie (info))
4276
0
    bfd_hash_traverse (&info->hash->table,
4277
0
           elf_i386_pie_finish_undefweak_symbol,
4278
0
           info);
4279
4280
0
  return true;
4281
0
}
4282
4283
/* Fill PLT/GOT entries and allocate dynamic relocations for local
4284
   STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4285
   It has to be done before elf_link_sort_relocs is called so that
4286
   dynamic relocations are properly sorted.  */
4287
4288
static bool
4289
elf_i386_output_arch_local_syms
4290
  (struct bfd_link_info *info,
4291
   void *flaginfo ATTRIBUTE_UNUSED,
4292
   int (*func) (void *, const char *,
4293
    Elf_Internal_Sym *,
4294
    asection *,
4295
    struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4296
0
{
4297
0
  struct elf_x86_link_hash_table *htab
4298
0
    = elf_x86_hash_table (info, I386_ELF_DATA);
4299
0
  if (htab == NULL)
4300
0
    return false;
4301
4302
  /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols if
4303
     needed.  */
4304
0
  if (htab->has_loc_hash_table)
4305
0
    htab_traverse (htab->loc_hash_table,
4306
0
       elf_i386_finish_local_dynamic_symbol,
4307
0
       info);
4308
4309
0
  return true;
4310
0
}
4311
4312
/* Similar to _bfd_elf_get_synthetic_symtab.  Support PLTs with all
4313
   dynamic relocations.   */
4314
4315
static long
4316
elf_i386_get_synthetic_symtab (bfd *abfd,
4317
             long symcount ATTRIBUTE_UNUSED,
4318
             asymbol **syms ATTRIBUTE_UNUSED,
4319
             long dynsymcount,
4320
             asymbol **dynsyms,
4321
             asymbol **ret)
4322
96
{
4323
96
  long count, i, n;
4324
96
  int j;
4325
96
  bfd_byte *plt_contents;
4326
96
  long relsize;
4327
96
  const struct elf_x86_lazy_plt_layout *lazy_plt;
4328
96
  const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4329
96
  const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4330
96
  const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
4331
96
  asection *plt;
4332
96
  bfd_vma got_addr;
4333
96
  enum elf_x86_plt_type plt_type;
4334
96
  struct elf_x86_plt plts[] =
4335
96
    {
4336
96
      { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4337
96
      { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
4338
96
      { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4339
96
      { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
4340
96
    };
4341
4342
96
  *ret = NULL;
4343
4344
96
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4345
84
    return 0;
4346
4347
12
  if (dynsymcount <= 0)
4348
9
    return 0;
4349
4350
3
  relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4351
3
  if (relsize <= 0)
4352
0
    return -1;
4353
4354
3
  non_lazy_plt = NULL;
4355
  /* Silence GCC 6.  */
4356
3
  lazy_plt = NULL;
4357
3
  non_lazy_ibt_plt = NULL;
4358
3
  lazy_ibt_plt = NULL;
4359
3
  switch (get_elf_backend_data (abfd)->target_os)
4360
3
    {
4361
3
    case is_normal:
4362
3
    case is_solaris:
4363
3
      non_lazy_plt = &elf_i386_non_lazy_plt;
4364
3
      lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4365
3
      non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4366
      /* Fall through */
4367
3
    case is_vxworks:
4368
3
      lazy_plt = &elf_i386_lazy_plt;
4369
3
      break;
4370
0
    default:
4371
0
      abort ();
4372
3
    }
4373
4374
3
  got_addr = 0;
4375
4376
3
  count = 0;
4377
12
  for (j = 0; plts[j].name != NULL; j++)
4378
9
    {
4379
9
      plt = bfd_get_section_by_name (abfd, plts[j].name);
4380
9
      if (plt == NULL || plt->size == 0)
4381
7
  continue;
4382
4383
      /* Get the PLT section contents.  */
4384
2
      plt_contents = (bfd_byte *) bfd_malloc (plt->size);
4385
2
      if (plt_contents == NULL)
4386
0
  break;
4387
2
      if (!bfd_get_section_contents (abfd, (asection *) plt,
4388
2
             plt_contents, 0, plt->size))
4389
0
  {
4390
0
    free (plt_contents);
4391
0
    break;
4392
0
  }
4393
4394
      /* Check what kind of PLT it is.  */
4395
2
      plt_type = plt_unknown;
4396
2
      if (plts[j].type == plt_unknown
4397
2
    && (plt->size >= (lazy_plt->plt0_entry_size
4398
2
          + lazy_plt->plt_entry_size)))
4399
2
  {
4400
    /* Match lazy PLT first.  */
4401
2
    if (memcmp (plt_contents, lazy_plt->plt0_entry,
4402
2
          lazy_plt->plt0_got1_offset) == 0)
4403
0
      {
4404
        /* The fist entry in the lazy IBT PLT is the same as the
4405
     normal lazy PLT.  */
4406
0
        if (lazy_ibt_plt != NULL
4407
0
      && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4408
0
            lazy_ibt_plt->plt_entry,
4409
0
            lazy_ibt_plt->plt_reloc_offset) == 0))
4410
0
    plt_type = plt_lazy | plt_second;
4411
0
        else
4412
0
    plt_type = plt_lazy;
4413
0
      }
4414
2
    else if (memcmp (plt_contents, lazy_plt->pic_plt0_entry,
4415
2
         lazy_plt->plt0_got1_offset) == 0)
4416
2
      {
4417
        /* The fist entry in the PIC lazy IBT PLT is the same as
4418
     the normal PIC lazy PLT.  */
4419
2
        if (lazy_ibt_plt != NULL
4420
2
      && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4421
2
            lazy_ibt_plt->pic_plt_entry,
4422
2
            lazy_ibt_plt->plt_reloc_offset) == 0))
4423
0
    plt_type = plt_lazy | plt_pic | plt_second;
4424
2
        else
4425
2
    plt_type = plt_lazy | plt_pic;
4426
2
      }
4427
2
  }
4428
4429
2
      if (non_lazy_plt != NULL
4430
2
    && (plt_type == plt_unknown || plt_type == plt_non_lazy)
4431
0
    && plt->size >= non_lazy_plt->plt_entry_size)
4432
0
  {
4433
    /* Match non-lazy PLT.  */
4434
0
    if (memcmp (plt_contents, non_lazy_plt->plt_entry,
4435
0
          non_lazy_plt->plt_got_offset) == 0)
4436
0
      plt_type = plt_non_lazy;
4437
0
    else if (memcmp (plt_contents, non_lazy_plt->pic_plt_entry,
4438
0
         non_lazy_plt->plt_got_offset) == 0)
4439
0
      plt_type = plt_pic;
4440
0
  }
4441
4442
2
      if ((non_lazy_ibt_plt != NULL)
4443
2
    && (plt_type == plt_unknown || plt_type == plt_second)
4444
0
    && plt->size >= non_lazy_ibt_plt->plt_entry_size)
4445
0
  {
4446
0
    if (memcmp (plt_contents,
4447
0
          non_lazy_ibt_plt->plt_entry,
4448
0
          non_lazy_ibt_plt->plt_got_offset) == 0)
4449
0
      {
4450
        /* Match IBT PLT.  */
4451
0
        plt_type = plt_second;
4452
0
        non_lazy_plt = non_lazy_ibt_plt;
4453
0
      }
4454
0
    else if (memcmp (plt_contents,
4455
0
         non_lazy_ibt_plt->pic_plt_entry,
4456
0
         non_lazy_ibt_plt->plt_got_offset) == 0)
4457
0
      {
4458
        /* Match PIC IBT PLT.  */
4459
0
        plt_type = plt_second | plt_pic;
4460
0
        non_lazy_plt = non_lazy_ibt_plt;
4461
0
      }
4462
0
  }
4463
4464
2
      if (plt_type == plt_unknown)
4465
0
  {
4466
0
    free (plt_contents);
4467
0
    continue;
4468
0
  }
4469
4470
2
      plts[j].sec = plt;
4471
2
      plts[j].type = plt_type;
4472
4473
2
      if ((plt_type & plt_lazy))
4474
2
  {
4475
2
    plts[j].plt_got_offset = lazy_plt->plt_got_offset;
4476
2
    plts[j].plt_entry_size = lazy_plt->plt_entry_size;
4477
    /* Skip PLT0 in lazy PLT.  */
4478
2
    i = 1;
4479
2
  }
4480
0
      else
4481
0
  {
4482
0
    plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
4483
0
    plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
4484
0
    i = 0;
4485
0
  }
4486
4487
      /* Skip lazy PLT when the second PLT is used.  */
4488
2
      if ((plt_type & (plt_lazy | plt_second))
4489
2
    == (plt_lazy | plt_second))
4490
0
  plts[j].count = 0;
4491
2
      else
4492
2
  {
4493
2
    n = plt->size / plts[j].plt_entry_size;
4494
2
    plts[j].count = n;
4495
2
    count += n - i;
4496
2
  }
4497
4498
2
      plts[j].contents = plt_contents;
4499
4500
      /* The _GLOBAL_OFFSET_TABLE_ address is needed.  */
4501
2
      if ((plt_type & plt_pic))
4502
2
  got_addr = (bfd_vma) -1;
4503
2
    }
4504
4505
3
  return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
4506
3
              got_addr, plts, dynsyms,
4507
3
              ret);
4508
3
}
4509
4510
/* Set up i386 GNU properties.  Return the first relocatable ELF input
4511
   with GNU properties if found.  Otherwise, return NULL.  */
4512
4513
static bfd *
4514
elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
4515
0
{
4516
0
  struct elf_x86_init_table init_table;
4517
4518
0
  switch (get_elf_backend_data (info->output_bfd)->target_os)
4519
0
    {
4520
0
    case is_normal:
4521
0
    case is_solaris:
4522
0
      init_table.plt0_pad_byte = 0x0;
4523
0
      init_table.lazy_plt = &elf_i386_lazy_plt;
4524
0
      init_table.non_lazy_plt = &elf_i386_non_lazy_plt;
4525
0
      init_table.lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4526
0
      init_table.non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4527
0
      break;
4528
0
    case is_vxworks:
4529
0
      init_table.plt0_pad_byte = 0x90;
4530
0
      init_table.lazy_plt = &elf_i386_lazy_plt;
4531
0
      init_table.non_lazy_plt = NULL;
4532
0
      init_table.lazy_ibt_plt = NULL;
4533
0
      init_table.non_lazy_ibt_plt = NULL;
4534
0
      break;
4535
0
    default:
4536
0
      abort ();
4537
0
    }
4538
4539
0
  init_table.r_info = elf32_r_info;
4540
0
  init_table.r_sym = elf32_r_sym;
4541
4542
0
  return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
4543
0
}
4544
4545
static void
4546
elf_i386_add_glibc_version_dependency
4547
  (struct elf_find_verdep_info *rinfo)
4548
0
{
4549
0
  int i = 0;
4550
0
  const char *version[4] = { NULL, NULL, NULL, NULL };
4551
0
  bool auto_version[4] = { false, false, false, false };
4552
0
  struct elf_x86_link_hash_table *htab;
4553
4554
0
  if (rinfo->info->enable_dt_relr)
4555
0
    {
4556
0
      version[i] = "GLIBC_ABI_DT_RELR";
4557
0
      i++;
4558
0
    }
4559
4560
0
  htab = elf_x86_hash_table (rinfo->info, I386_ELF_DATA);
4561
0
  if (htab != NULL)
4562
0
    {
4563
0
      if (htab->params->gnu2_tls_version_tag && htab->has_tls_desc_call)
4564
0
  {
4565
0
    version[i] = "GLIBC_ABI_GNU2_TLS";
4566
    /* 2 == auto, enable if libc.so defines the GLIBC_ABI_GNU2_TLS
4567
       version.  */
4568
0
    if (htab->params->gnu2_tls_version_tag == 2)
4569
0
      auto_version[i] = true;
4570
0
    i++;
4571
0
  }
4572
0
      if (htab->params->gnu_tls_version_tag
4573
0
    && htab->has_tls_get_addr_call)
4574
0
  {
4575
0
    version[i] = "GLIBC_ABI_GNU_TLS";
4576
    /* 2 == auto, enable if libc.so defines the GLIBC_ABI_GNU_TLS
4577
       version.  */
4578
0
    if (htab->params->gnu_tls_version_tag == 2)
4579
0
      auto_version[i] = true;
4580
0
    i++;
4581
0
  }
4582
0
    }
4583
4584
0
  if (i != 0)
4585
0
    _bfd_elf_link_add_glibc_version_dependency (rinfo, version,
4586
0
            auto_version);
4587
0
}
4588
4589
#define TARGET_LITTLE_SYM   i386_elf32_vec
4590
#define TARGET_LITTLE_NAME    "elf32-i386"
4591
#define ELF_ARCH      bfd_arch_i386
4592
#define ELF_TARGET_ID     I386_ELF_DATA
4593
#define ELF_MACHINE_CODE    EM_386
4594
#define ELF_MAXPAGESIZE     0x1000
4595
#define ELF_OSABI     ELFOSABI_GNU
4596
4597
#define elf_backend_can_gc_sections 1
4598
#define elf_backend_can_refcount  1
4599
#define elf_backend_want_got_plt  1
4600
#define elf_backend_plt_readonly  1
4601
#define elf_backend_want_plt_sym  0
4602
#define elf_backend_got_header_size 12
4603
#define elf_backend_plt_alignment 4
4604
#define elf_backend_dtrel_excludes_plt  1
4605
#define elf_backend_caches_rawsize  1
4606
#define elf_backend_want_dynrelro 1
4607
4608
/* Support RELA for objdump of prelink objects.  */
4609
#define elf_info_to_howto         elf_i386_info_to_howto_rel
4610
#define elf_info_to_howto_rel         elf_i386_info_to_howto_rel
4611
4612
#define bfd_elf32_bfd_is_local_label_name     elf_i386_is_local_label_name
4613
#define bfd_elf32_bfd_reloc_type_lookup       elf_i386_reloc_type_lookup
4614
#define bfd_elf32_bfd_reloc_name_lookup       elf_i386_reloc_name_lookup
4615
#define bfd_elf32_get_synthetic_symtab        elf_i386_get_synthetic_symtab
4616
4617
#define elf_backend_relocs_compatible       _bfd_elf_relocs_compatible
4618
#define elf_backend_early_size_sections       elf_i386_early_size_sections
4619
#define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
4620
#define elf_backend_fake_sections       elf_i386_fake_sections
4621
#define elf_backend_finish_dynamic_sections   elf_i386_finish_dynamic_sections
4622
#define elf_backend_finish_dynamic_symbol     elf_i386_finish_dynamic_symbol
4623
#define elf_backend_output_arch_local_syms     elf_i386_output_arch_local_syms
4624
#define elf_backend_grok_prstatus       elf_i386_grok_prstatus
4625
#define elf_backend_grok_psinfo         elf_i386_grok_psinfo
4626
#define elf_backend_reloc_type_class        elf_i386_reloc_type_class
4627
#define elf_backend_relocate_section        elf_i386_relocate_section
4628
#define elf_backend_setup_gnu_properties      elf_i386_link_setup_gnu_properties
4629
#define elf_backend_hide_symbol         _bfd_x86_elf_hide_symbol
4630
#define elf_backend_add_glibc_version_dependency \
4631
  elf_i386_add_glibc_version_dependency
4632
4633
#define elf_backend_linux_prpsinfo32_ugid16 true
4634
4635
#define elf32_bed           elf32_i386_bed
4636
4637
#include "elf32-target.h"
4638
4639
#undef elf_backend_add_glibc_version_dependency
4640
4641
/* FreeBSD support.  */
4642
4643
#undef  TARGET_LITTLE_SYM
4644
#define TARGET_LITTLE_SYM   i386_elf32_fbsd_vec
4645
#undef  TARGET_LITTLE_NAME
4646
#define TARGET_LITTLE_NAME    "elf32-i386-freebsd"
4647
#undef  ELF_OSABI
4648
#define ELF_OSABI     ELFOSABI_FREEBSD
4649
#undef  ELF_OSABI_EXACT
4650
#define ELF_OSABI_EXACT     1
4651
4652
/* The kernel recognizes executables as valid only if they carry a
4653
   "FreeBSD" label in the ELF header.  So we put this label on all
4654
   executables and (for simplicity) also all other object files.  */
4655
4656
static bool
4657
elf_i386_fbsd_init_file_header (bfd *abfd, struct bfd_link_info *info)
4658
5
{
4659
5
  if (!_bfd_elf_init_file_header (abfd, info))
4660
0
    return false;
4661
4662
#ifdef OLD_FREEBSD_ABI_LABEL
4663
  {
4664
    /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard.  */
4665
    Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4666
    memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4667
  }
4668
#endif
4669
5
  return true;
4670
5
}
4671
4672
#undef  elf_backend_init_file_header
4673
#define elf_backend_init_file_header  elf_i386_fbsd_init_file_header
4674
#undef  elf32_bed
4675
#define elf32_bed       elf32_i386_fbsd_bed
4676
4677
#include "elf32-target.h"
4678
4679
#undef elf_backend_init_file_header
4680
4681
#ifdef OBJ_MAYBE_ELF_SOLARIS2
4682
4683
/* Solaris 2.  */
4684
4685
#undef  TARGET_LITTLE_SYM
4686
#define TARGET_LITTLE_SYM   i386_elf32_sol2_vec
4687
#undef  TARGET_LITTLE_NAME
4688
#define TARGET_LITTLE_NAME    "elf32-i386-sol2"
4689
4690
#undef  ELF_MAXPAGESIZE
4691
#define ELF_MAXPAGESIZE     0x10000
4692
4693
#undef  ELF_TARGET_OS
4694
#define ELF_TARGET_OS     is_solaris
4695
4696
#undef ELF_OSABI
4697
#define ELF_OSABI     ELFOSABI_SOLARIS
4698
#undef ELF_OSABI_EXACT
4699
4700
#undef  elf32_bed
4701
#define elf32_bed     elf32_i386_sol2_bed
4702
4703
/* The 32-bit static TLS arena size is rounded to the nearest 8-byte
4704
   boundary.  */
4705
#undef  elf_backend_static_tls_alignment
4706
#define elf_backend_static_tls_alignment 8
4707
4708
/* The Solaris 2 ABI requires a plt symbol on all platforms.
4709
4710
   Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4711
   File, p.63.  */
4712
#undef  elf_backend_want_plt_sym
4713
#define elf_backend_want_plt_sym  1
4714
4715
#undef  elf_backend_add_symbol_hook
4716
#define elf_backend_add_symbol_hook     elf_solaris2_add_symbol_hook
4717
4718
#include "elf32-target.h"
4719
4720
#endif /* OBJ_MAYBE_ELF_SOLARIS2 */
4721
4722
/* Intel MCU support.  */
4723
4724
static bool
4725
elf32_iamcu_elf_object_p (bfd *abfd)
4726
50
{
4727
  /* Set the right machine number for an IAMCU elf32 file.  */
4728
50
  bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
4729
  return true;
4730
50
}
4731
4732
#undef  TARGET_LITTLE_SYM
4733
#define TARGET_LITTLE_SYM   iamcu_elf32_vec
4734
#undef  TARGET_LITTLE_NAME
4735
#define TARGET_LITTLE_NAME    "elf32-iamcu"
4736
#undef  ELF_ARCH
4737
#define ELF_ARCH      bfd_arch_iamcu
4738
4739
#undef  ELF_MACHINE_CODE
4740
#define ELF_MACHINE_CODE    EM_IAMCU
4741
4742
#undef  ELF_MAXPAGESIZE
4743
#define ELF_MAXPAGESIZE     0x1000
4744
4745
#undef  ELF_TARGET_OS
4746
#undef  ELF_OSABI
4747
#define ELF_OSABI     ELFOSABI_GNU
4748
#undef  ELF_OSABI_EXACT
4749
4750
#undef  elf32_bed
4751
#define elf32_bed     elf32_iamcu_bed
4752
4753
#undef  elf_backend_object_p
4754
#define elf_backend_object_p    elf32_iamcu_elf_object_p
4755
4756
#undef  elf_backend_static_tls_alignment
4757
4758
#undef  elf_backend_want_plt_sym
4759
#define elf_backend_want_plt_sym  0
4760
#undef  elf_backend_add_symbol_hook
4761
4762
#include "elf32-target.h"
4763
4764
/* Restore defaults.  */
4765
#undef  ELF_ARCH
4766
#define ELF_ARCH      bfd_arch_i386
4767
#undef  ELF_MACHINE_CODE
4768
#define ELF_MACHINE_CODE    EM_386
4769
#undef  elf_backend_object_p
4770
4771
#ifdef OBJ_MAYBE_ELF_VXWORKS
4772
4773
/* VxWorks support.  */
4774
4775
#undef  TARGET_LITTLE_SYM
4776
#define TARGET_LITTLE_SYM   i386_elf32_vxworks_vec
4777
#undef  TARGET_LITTLE_NAME
4778
#define TARGET_LITTLE_NAME    "elf32-i386-vxworks"
4779
#undef  ELF_MAXPAGESIZE
4780
#define ELF_MAXPAGESIZE     0x1000
4781
#undef  elf_backend_plt_alignment
4782
#define elf_backend_plt_alignment 4
4783
4784
#undef  ELF_TARGET_OS
4785
#define ELF_TARGET_OS   is_vxworks
4786
#undef  ELF_OSABI
4787
#undef  ELF_OSABI_EXACT
4788
4789
#undef elf_backend_relocs_compatible
4790
#undef elf_backend_add_symbol_hook
4791
#define elf_backend_add_symbol_hook \
4792
  elf_vxworks_add_symbol_hook
4793
#undef elf_backend_link_output_symbol_hook
4794
#define elf_backend_link_output_symbol_hook \
4795
  elf_vxworks_link_output_symbol_hook
4796
#undef elf_backend_emit_relocs
4797
#define elf_backend_emit_relocs     elf_vxworks_emit_relocs
4798
#undef elf_backend_final_write_processing
4799
#define elf_backend_final_write_processing \
4800
  elf_vxworks_final_write_processing
4801
#undef elf_backend_static_tls_alignment
4802
4803
/* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
4804
   define it.  */
4805
#undef elf_backend_want_plt_sym
4806
#define elf_backend_want_plt_sym  1
4807
4808
#undef  elf32_bed
4809
#define elf32_bed       elf32_i386_vxworks_bed
4810
4811
#include "elf32-target.h"
4812
4813
#endif /* OBJ_MAYBE_ELF_VXWORKS */