Coverage Report

Created: 2026-03-10 08:46

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