Coverage Report

Created: 2026-07-25 10:20

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
5.12k
#define R_386_standard (R_386_GOTPC + 1)
71
1.95k
#define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
72
73
  /* These relocs are a GNU extension.  */
74
  HOWTO(R_386_TLS_TPOFF, 0, 4, 32, false, 0, complain_overflow_dont,
75
  bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
76
  true, 0xffffffff, 0xffffffff, false),
77
  HOWTO(R_386_TLS_IE, 0, 4, 32, false, 0, complain_overflow_dont,
78
  bfd_elf_generic_reloc, "R_386_TLS_IE",
79
  true, 0xffffffff, 0xffffffff, false),
80
  HOWTO(R_386_TLS_GOTIE, 0, 4, 32, false, 0, complain_overflow_dont,
81
  bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
82
  true, 0xffffffff, 0xffffffff, false),
83
  HOWTO(R_386_TLS_LE, 0, 4, 32, false, 0, complain_overflow_dont,
84
  bfd_elf_generic_reloc, "R_386_TLS_LE",
85
  true, 0xffffffff, 0xffffffff, false),
86
  HOWTO(R_386_TLS_GD, 0, 4, 32, false, 0, complain_overflow_dont,
87
  bfd_elf_generic_reloc, "R_386_TLS_GD",
88
  true, 0xffffffff, 0xffffffff, false),
89
  HOWTO(R_386_TLS_LDM, 0, 4, 32, false, 0, complain_overflow_dont,
90
  bfd_elf_generic_reloc, "R_386_TLS_LDM",
91
  true, 0xffffffff, 0xffffffff, false),
92
  HOWTO(R_386_16, 0, 2, 16, false, 0, complain_overflow_bitfield,
93
  bfd_elf_generic_reloc, "R_386_16",
94
  true, 0xffff, 0xffff, false),
95
  HOWTO(R_386_PC16, 0, 2, 16, true, 0, complain_overflow_bitfield,
96
  bfd_elf_generic_reloc, "R_386_PC16",
97
  true, 0xffff, 0xffff, true),
98
  HOWTO(R_386_8, 0, 1, 8, false, 0, complain_overflow_bitfield,
99
  bfd_elf_generic_reloc, "R_386_8",
100
  true, 0xff, 0xff, false),
101
  HOWTO(R_386_PC8, 0, 1, 8, true, 0, complain_overflow_signed,
102
  bfd_elf_generic_reloc, "R_386_PC8",
103
  true, 0xff, 0xff, true),
104
105
1.68k
#define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
106
972
#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
744
#define R_386_ext2 (R_386_GOT32X + 1 - R_386_tls_offset)
147
258
#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
129
#define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
180
181
};
182
183
#ifdef DEBUG_GEN_RELOC
184
#define TRACE(str) \
185
  fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
186
#else
187
#define TRACE(str)
188
#endif
189
190
static reloc_howto_type *
191
elf_i386_reloc_type_lookup (bfd *abfd,
192
          bfd_reloc_code_real_type code)
193
0
{
194
0
  switch (code)
195
0
    {
196
0
    case BFD_RELOC_NONE:
197
0
      TRACE ("BFD_RELOC_NONE");
198
0
      return &elf_howto_table[R_386_NONE];
199
200
0
    case BFD_RELOC_32:
201
0
      TRACE ("BFD_RELOC_32");
202
0
      return &elf_howto_table[R_386_32];
203
204
0
    case BFD_RELOC_CTOR:
205
0
      TRACE ("BFD_RELOC_CTOR");
206
0
      return &elf_howto_table[R_386_32];
207
208
0
    case BFD_RELOC_32_PCREL:
209
0
      TRACE ("BFD_RELOC_PC32");
210
0
      return &elf_howto_table[R_386_PC32];
211
212
0
    case BFD_RELOC_386_GOT32:
213
0
      TRACE ("BFD_RELOC_386_GOT32");
214
0
      return &elf_howto_table[R_386_GOT32];
215
216
0
    case BFD_RELOC_386_PLT32:
217
0
      TRACE ("BFD_RELOC_386_PLT32");
218
0
      return &elf_howto_table[R_386_PLT32];
219
220
0
    case BFD_RELOC_COPY:
221
0
      TRACE ("BFD_RELOC_COPY");
222
0
      return &elf_howto_table[R_386_COPY];
223
224
0
    case BFD_RELOC_GLOB_DAT:
225
0
      TRACE ("BFD_RELOC_GLOB_DAT");
226
0
      return &elf_howto_table[R_386_GLOB_DAT];
227
228
0
    case BFD_RELOC_JMP_SLOT:
229
0
      TRACE ("BFD_RELOC_JMP_SLOT");
230
0
      return &elf_howto_table[R_386_JUMP_SLOT];
231
232
0
    case BFD_RELOC_RELATIVE:
233
0
      TRACE ("BFD_RELOC_RELATIVE");
234
0
      return &elf_howto_table[R_386_RELATIVE];
235
236
0
    case BFD_RELOC_32_GOTOFF:
237
0
      TRACE ("BFD_RELOC_32_GOTOFF");
238
0
      return &elf_howto_table[R_386_GOTOFF];
239
240
0
    case BFD_RELOC_32_GOT_PCREL:
241
0
      TRACE ("BFD_RELOC_386_GOTPC");
242
0
      return &elf_howto_table[R_386_GOTPC];
243
244
      /* These relocs are a GNU extension.  */
245
0
    case BFD_RELOC_386_TLS_TPOFF:
246
0
      TRACE ("BFD_RELOC_386_TLS_TPOFF");
247
0
      return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
248
249
0
    case BFD_RELOC_386_TLS_IE:
250
0
      TRACE ("BFD_RELOC_386_TLS_IE");
251
0
      return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
252
253
0
    case BFD_RELOC_386_TLS_GOTIE:
254
0
      TRACE ("BFD_RELOC_386_TLS_GOTIE");
255
0
      return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
256
257
0
    case BFD_RELOC_386_TLS_LE:
258
0
      TRACE ("BFD_RELOC_386_TLS_LE");
259
0
      return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
260
261
0
    case BFD_RELOC_386_TLS_GD:
262
0
      TRACE ("BFD_RELOC_386_TLS_GD");
263
0
      return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
264
265
0
    case BFD_RELOC_386_TLS_LDM:
266
0
      TRACE ("BFD_RELOC_386_TLS_LDM");
267
0
      return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
268
269
0
    case BFD_RELOC_16:
270
0
      TRACE ("BFD_RELOC_16");
271
0
      return &elf_howto_table[R_386_16 - R_386_ext_offset];
272
273
0
    case BFD_RELOC_16_PCREL:
274
0
      TRACE ("BFD_RELOC_16_PCREL");
275
0
      return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
276
277
0
    case BFD_RELOC_8:
278
0
      TRACE ("BFD_RELOC_8");
279
0
      return &elf_howto_table[R_386_8 - R_386_ext_offset];
280
281
0
    case BFD_RELOC_8_PCREL:
282
0
      TRACE ("BFD_RELOC_8_PCREL");
283
0
      return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
284
285
    /* Common with Sun TLS implementation.  */
286
0
    case BFD_RELOC_386_TLS_LDO_32:
287
0
      TRACE ("BFD_RELOC_386_TLS_LDO_32");
288
0
      return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
289
290
0
    case BFD_RELOC_386_TLS_IE_32:
291
0
      TRACE ("BFD_RELOC_386_TLS_IE_32");
292
0
      return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
293
294
0
    case BFD_RELOC_386_TLS_LE_32:
295
0
      TRACE ("BFD_RELOC_386_TLS_LE_32");
296
0
      return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
297
298
0
    case BFD_RELOC_386_TLS_DTPMOD32:
299
0
      TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
300
0
      return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
301
302
0
    case BFD_RELOC_386_TLS_DTPOFF32:
303
0
      TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
304
0
      return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
305
306
0
    case BFD_RELOC_386_TLS_TPOFF32:
307
0
      TRACE ("BFD_RELOC_386_TLS_TPOFF32");
308
0
      return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
309
310
0
    case BFD_RELOC_SIZE32:
311
0
      TRACE ("BFD_RELOC_SIZE32");
312
0
      return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset];
313
314
0
    case BFD_RELOC_386_TLS_GOTDESC:
315
0
      TRACE ("BFD_RELOC_386_TLS_GOTDESC");
316
0
      return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
317
318
0
    case BFD_RELOC_386_TLS_DESC_CALL:
319
0
      TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
320
0
      return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
321
322
0
    case BFD_RELOC_386_TLS_DESC:
323
0
      TRACE ("BFD_RELOC_386_TLS_DESC");
324
0
      return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
325
326
0
    case BFD_RELOC_IRELATIVE:
327
0
      TRACE ("BFD_RELOC_IRELATIVE");
328
0
      return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset];
329
330
0
    case BFD_RELOC_386_GOT32X:
331
0
      TRACE ("BFD_RELOC_386_GOT32X");
332
0
      return &elf_howto_table[R_386_GOT32X - R_386_tls_offset];
333
334
0
    case BFD_RELOC_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
1.32k
{
369
1.32k
  unsigned int indx;
370
371
1.32k
  if ((indx = r_type) >= R_386_standard
372
261
      && ((indx = r_type - R_386_ext_offset) - R_386_standard
373
261
    >= R_386_ext - R_386_standard)
374
228
      && ((indx = r_type - R_386_tls_offset) - R_386_ext
375
228
    >= R_386_ext2 - R_386_ext)
376
129
      && ((indx = r_type - R_386_vt_offset) - R_386_ext2
377
129
    >= R_386_vt - R_386_ext2))
378
120
      return NULL;
379
  /* PR 17512: file: 0f67f69d.  */
380
1.20k
  if (elf_howto_table [indx].type != r_type)
381
0
    return NULL;
382
1.20k
  return &elf_howto_table[indx];
383
1.20k
}
384
385
static bool
386
elf_i386_info_to_howto_rel (bfd *abfd,
387
          arelent *cache_ptr,
388
          Elf_Internal_Rela *dst)
389
1.32k
{
390
1.32k
  unsigned int r_type = ELF32_R_TYPE (dst->r_info);
391
392
1.32k
  if ((cache_ptr->howto = elf_i386_rtype_to_howto (r_type)) == NULL)
393
120
    {
394
      /* xgettext:c-format */
395
120
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
396
120
        abfd, r_type);
397
120
      bfd_set_error (bfd_error_bad_value);
398
120
      return false;
399
120
    }
400
401
1.20k
  return true;
402
1.32k
}
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 (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 (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
74
{
2062
74
  const char *name;
2063
2064
74
  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
74
  if (strcmp (name, ".reloc") == 0)
2083
0
    hdr->sh_type = SHT_PROGBITS;
2084
2085
74
  return true;
2086
74
}
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 (struct bfd_link_info *info,
2111
         bfd *input_bfd,
2112
         asection *input_section,
2113
         bfd_byte *contents,
2114
         Elf_Internal_Rela *relocs,
2115
         Elf_Internal_Sym *local_syms,
2116
         asection **local_sections)
2117
0
{
2118
0
  struct elf_x86_link_hash_table *htab;
2119
0
  Elf_Internal_Shdr *symtab_hdr;
2120
0
  struct elf_link_hash_entry **sym_hashes;
2121
0
  bfd_vma *local_got_offsets;
2122
0
  bfd_vma *local_tlsdesc_gotents;
2123
0
  Elf_Internal_Rela *rel;
2124
0
  Elf_Internal_Rela *wrel;
2125
0
  Elf_Internal_Rela *relend;
2126
0
  bool is_vxworks_tls;
2127
0
  unsigned expected_tls_le;
2128
0
  unsigned plt_entry_size;
2129
2130
  /* Skip if check_relocs or scan_relocs failed.  */
2131
0
  if (input_section->check_relocs_failed)
2132
0
    return false;
2133
2134
0
  htab = elf_x86_hash_table (info, I386_ELF_DATA);
2135
0
  if (htab == NULL)
2136
0
    return false;
2137
2138
0
  if (!is_x86_elf (input_bfd, htab))
2139
0
    {
2140
0
      bfd_set_error (bfd_error_wrong_format);
2141
0
      return false;
2142
0
    }
2143
2144
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
2145
0
  sym_hashes = elf_sym_hashes (input_bfd);
2146
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
2147
0
  local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
2148
  /* We have to handle relocations in vxworks .tls_vars sections
2149
     specially, because the dynamic loader is 'weird'.  */
2150
0
  is_vxworks_tls = (htab->elf.target_os == is_vxworks
2151
0
        && bfd_link_pic (info)
2152
0
        && !strcmp (input_section->output_section->name,
2153
0
        ".tls_vars"));
2154
2155
0
  _bfd_x86_elf_set_tls_module_base (info);
2156
2157
0
  plt_entry_size = htab->plt.plt_entry_size;
2158
2159
0
  rel = wrel = relocs;
2160
0
  relend = relocs + input_section->reloc_count;
2161
0
  for (; rel < relend; wrel++, rel++)
2162
0
    {
2163
0
      unsigned int r_type, r_type_tls;
2164
0
      reloc_howto_type *howto;
2165
0
      unsigned long r_symndx;
2166
0
      struct elf_link_hash_entry *h;
2167
0
      struct elf_x86_link_hash_entry *eh;
2168
0
      Elf_Internal_Sym *sym;
2169
0
      asection *sec;
2170
0
      bfd_vma off, offplt, plt_offset;
2171
0
      bfd_vma relocation;
2172
0
      bool unresolved_reloc;
2173
0
      bfd_reloc_status_type r;
2174
0
      unsigned int indx;
2175
0
      int tls_type;
2176
0
      bfd_vma st_size;
2177
0
      asection *resolved_plt;
2178
0
      bool resolved_to_zero;
2179
0
      bool relative_reloc;
2180
2181
0
      r_type = ELF32_R_TYPE (rel->r_info);
2182
0
      if (r_type == R_386_GNU_VTINHERIT
2183
0
    || r_type == R_386_GNU_VTENTRY)
2184
0
  {
2185
0
    if (wrel != rel)
2186
0
      *wrel = *rel;
2187
0
    continue;
2188
0
  }
2189
2190
0
      howto = elf_i386_rtype_to_howto (r_type);
2191
0
      if (howto == NULL)
2192
0
  return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
2193
2194
0
      r_symndx = ELF32_R_SYM (rel->r_info);
2195
0
      h = NULL;
2196
0
      sym = NULL;
2197
0
      sec = NULL;
2198
0
      unresolved_reloc = false;
2199
0
      if (r_symndx < symtab_hdr->sh_info)
2200
0
  {
2201
0
    sym = local_syms + r_symndx;
2202
0
    sec = local_sections[r_symndx];
2203
0
    relocation = (sec->output_section->vma
2204
0
      + sec->output_offset
2205
0
      + sym->st_value);
2206
0
    st_size = sym->st_size;
2207
2208
0
    if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2209
0
        && ((sec->flags & SEC_MERGE) != 0
2210
0
      || (bfd_link_relocatable (info)
2211
0
          && sec->output_offset != 0)))
2212
0
      {
2213
0
        bfd_vma addend;
2214
0
        bfd_byte *where = contents + rel->r_offset;
2215
2216
0
        switch (bfd_get_reloc_size (howto))
2217
0
    {
2218
0
    case 1:
2219
0
      addend = bfd_get_8 (input_bfd, where);
2220
0
      if (howto->pc_relative)
2221
0
        {
2222
0
          addend = (addend ^ 0x80) - 0x80;
2223
0
          addend += 1;
2224
0
        }
2225
0
      break;
2226
0
    case 2:
2227
0
      addend = bfd_get_16 (input_bfd, where);
2228
0
      if (howto->pc_relative)
2229
0
        {
2230
0
          addend = (addend ^ 0x8000) - 0x8000;
2231
0
          addend += 2;
2232
0
        }
2233
0
      break;
2234
0
    case 4:
2235
0
      addend = bfd_get_32 (input_bfd, where);
2236
0
      if (howto->pc_relative)
2237
0
        {
2238
0
          addend = (addend ^ 0x80000000) - 0x80000000;
2239
0
          addend += 4;
2240
0
        }
2241
0
      break;
2242
0
    default:
2243
0
      abort ();
2244
0
    }
2245
2246
0
        if (bfd_link_relocatable (info))
2247
0
    addend += sec->output_offset;
2248
0
        else
2249
0
    {
2250
0
      asection *msec = sec;
2251
0
      addend = _bfd_elf_rel_local_sym (info->output_bfd, sym, &msec,
2252
0
               addend);
2253
0
      addend -= relocation;
2254
0
      addend += msec->output_section->vma + msec->output_offset;
2255
0
    }
2256
2257
0
        switch (bfd_get_reloc_size (howto))
2258
0
    {
2259
0
    case 1:
2260
      /* FIXME: overflow checks.  */
2261
0
      if (howto->pc_relative)
2262
0
        addend -= 1;
2263
0
      bfd_put_8 (input_bfd, addend, where);
2264
0
      break;
2265
0
    case 2:
2266
0
      if (howto->pc_relative)
2267
0
        addend -= 2;
2268
0
      bfd_put_16 (input_bfd, addend, where);
2269
0
      break;
2270
0
    case 4:
2271
0
      if (howto->pc_relative)
2272
0
        addend -= 4;
2273
0
      bfd_put_32 (input_bfd, addend, where);
2274
0
      break;
2275
0
    }
2276
0
      }
2277
0
    else if (!bfd_link_relocatable (info)
2278
0
       && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2279
0
      {
2280
        /* Relocate against local STT_GNU_IFUNC symbol.  */
2281
0
        h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd, rel,
2282
0
               false);
2283
0
        if (h == NULL)
2284
0
    abort ();
2285
2286
        /* Set STT_GNU_IFUNC symbol value.  */
2287
0
        h->root.u.def.value = sym->st_value;
2288
0
        h->root.u.def.section = sec;
2289
0
      }
2290
0
  }
2291
0
      else
2292
0
  {
2293
0
    bool warned ATTRIBUTE_UNUSED;
2294
0
    bool ignored ATTRIBUTE_UNUSED;
2295
2296
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2297
0
           r_symndx, symtab_hdr, sym_hashes,
2298
0
           h, sec, relocation,
2299
0
           unresolved_reloc, warned, ignored);
2300
0
    st_size = h->size;
2301
0
  }
2302
2303
0
      if (sec != NULL && discarded_section (sec))
2304
0
  {
2305
0
    _bfd_clear_contents (howto, input_bfd, input_section,
2306
0
             contents, rel->r_offset);
2307
0
    wrel->r_offset = rel->r_offset;
2308
0
    wrel->r_info = 0;
2309
0
    wrel->r_addend = 0;
2310
2311
    /* For ld -r, remove relocations in debug sections against
2312
       sections defined in discarded sections.  Not done for
2313
       eh_frame editing code expects to be present.  */
2314
0
     if (bfd_link_relocatable (info)
2315
0
         && (input_section->flags & SEC_DEBUGGING))
2316
0
       wrel--;
2317
2318
0
     continue;
2319
0
  }
2320
2321
0
      if (bfd_link_relocatable (info))
2322
0
  {
2323
0
    if (wrel != rel)
2324
0
      *wrel = *rel;
2325
0
    continue;
2326
0
  }
2327
2328
0
      eh = (struct elf_x86_link_hash_entry *) h;
2329
2330
      /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2331
   it here if it is defined in a non-shared object.  */
2332
0
      if (h != NULL
2333
0
    && h->type == STT_GNU_IFUNC
2334
0
    && h->def_regular)
2335
0
  {
2336
0
    asection *gotplt, *base_got;
2337
0
    bfd_vma plt_index;
2338
0
    const char *name;
2339
2340
0
    if ((input_section->flags & SEC_ALLOC) == 0)
2341
0
      {
2342
        /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2343
           STT_GNU_IFUNC symbol as STT_FUNC.  */
2344
0
        if (elf_section_type (input_section) == SHT_NOTE)
2345
0
    goto skip_ifunc;
2346
        /* Dynamic relocs are not propagated for SEC_DEBUGGING
2347
     sections because such sections are not SEC_ALLOC and
2348
     thus ld.so will not process them.  */
2349
0
        if ((input_section->flags & SEC_DEBUGGING) != 0)
2350
0
    continue;
2351
0
        abort ();
2352
0
      }
2353
2354
    /* STT_GNU_IFUNC symbol must go through PLT.  */
2355
0
    if (htab->elf.splt != NULL)
2356
0
      {
2357
0
        if (htab->plt_second != NULL)
2358
0
    {
2359
0
      resolved_plt = htab->plt_second;
2360
0
      plt_offset = eh->plt_second.offset;
2361
0
    }
2362
0
        else
2363
0
    {
2364
0
      resolved_plt = htab->elf.splt;
2365
0
      plt_offset = h->plt.offset;
2366
0
    }
2367
0
        gotplt = htab->elf.sgotplt;
2368
0
      }
2369
0
    else
2370
0
      {
2371
0
        resolved_plt = htab->elf.iplt;
2372
0
        plt_offset = h->plt.offset;
2373
0
        gotplt = htab->elf.igotplt;
2374
0
      }
2375
2376
0
    switch (r_type)
2377
0
      {
2378
0
      default:
2379
0
        break;
2380
2381
0
      case R_386_GOT32:
2382
0
      case R_386_GOT32X:
2383
0
        base_got = htab->elf.sgot;
2384
0
        off = h->got.offset;
2385
2386
0
        if (base_got == NULL)
2387
0
    abort ();
2388
2389
0
        if (off == (bfd_vma) -1)
2390
0
    {
2391
      /* We can't use h->got.offset here to save state, or
2392
         even just remember the offset, as finish_dynamic_symbol
2393
         would use that as offset into .got.  */
2394
2395
0
      if (h->plt.offset == (bfd_vma) -1)
2396
0
        abort ();
2397
2398
0
      if (htab->elf.splt != NULL)
2399
0
        {
2400
0
          plt_index = (h->plt.offset / plt_entry_size
2401
0
           - htab->plt.has_plt0);
2402
0
          off = (plt_index + 3) * 4;
2403
0
          base_got = htab->elf.sgotplt;
2404
0
        }
2405
0
      else
2406
0
        {
2407
0
          plt_index = h->plt.offset / plt_entry_size;
2408
0
          off = plt_index * 4;
2409
0
          base_got = htab->elf.igotplt;
2410
0
        }
2411
2412
0
      if (h->dynindx == -1
2413
0
          || h->forced_local
2414
0
          || info->symbolic)
2415
0
        {
2416
          /* This references the local defitionion.  We must
2417
       initialize this entry in the global offset table.
2418
       Since the offset must always be a multiple of 4,
2419
       we use the least significant bit to record
2420
       whether we have initialized it already.
2421
2422
       When doing a dynamic link, we create a .rela.got
2423
       relocation entry to initialize the value.  This
2424
       is done in the finish_dynamic_symbol routine.   */
2425
0
          if ((off & 1) != 0)
2426
0
      off &= ~1;
2427
0
          else
2428
0
      {
2429
0
        bfd_put_32 (info->output_bfd, relocation,
2430
0
              base_got->contents + off);
2431
0
        h->got.offset |= 1;
2432
0
      }
2433
0
        }
2434
2435
0
      relocation = off;
2436
0
    }
2437
0
        else
2438
0
    relocation = (base_got->output_section->vma
2439
0
            + base_got->output_offset + off
2440
0
            - gotplt->output_section->vma
2441
0
            - gotplt->output_offset);
2442
2443
0
        if (rel->r_offset > 1
2444
0
      && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2445
0
      && *(contents + rel->r_offset - 2) != 0x8d)
2446
0
    {
2447
0
      if (bfd_link_pic (info))
2448
0
        goto disallow_got32;
2449
2450
      /* Add the GOT base if there is no base register.  */
2451
0
      relocation += (gotplt->output_section->vma
2452
0
         + gotplt->output_offset);
2453
0
    }
2454
0
        else if (htab->elf.splt == NULL)
2455
0
    {
2456
      /* Adjust for static executables.  */
2457
0
      relocation += gotplt->output_offset;
2458
0
    }
2459
2460
0
        goto do_relocation;
2461
0
      }
2462
2463
0
    if (h->plt.offset == (bfd_vma) -1)
2464
0
      {
2465
        /* Handle static pointers of STT_GNU_IFUNC symbols.  */
2466
0
        if (r_type == R_386_32
2467
0
      && (input_section->flags & SEC_CODE) == 0)
2468
0
    goto do_ifunc_pointer;
2469
0
        goto bad_ifunc_reloc;
2470
0
      }
2471
2472
0
    relocation = (resolved_plt->output_section->vma
2473
0
      + resolved_plt->output_offset + plt_offset);
2474
2475
0
    switch (r_type)
2476
0
      {
2477
0
      default:
2478
0
      bad_ifunc_reloc:
2479
0
        if (h->root.root.string)
2480
0
    name = h->root.root.string;
2481
0
        else
2482
0
    name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2483
0
           NULL);
2484
0
        _bfd_error_handler
2485
    /* xgettext:c-format */
2486
0
    (_("%pB: relocation %s against STT_GNU_IFUNC "
2487
0
       "symbol `%s' isn't supported"), input_bfd,
2488
0
     howto->name, name);
2489
0
        bfd_set_error (bfd_error_bad_value);
2490
0
        return false;
2491
2492
0
      case R_386_32:
2493
        /* Generate dynamic relcoation only when there is a
2494
     non-GOT reference in a shared object.  */
2495
0
        if ((bfd_link_pic (info) && h->non_got_ref)
2496
0
      || h->plt.offset == (bfd_vma) -1)
2497
0
    {
2498
0
      Elf_Internal_Rela outrel;
2499
0
      asection *sreloc;
2500
0
      bfd_vma offset;
2501
2502
0
    do_ifunc_pointer:
2503
      /* Need a dynamic relocation to get the real function
2504
         adddress.  */
2505
0
      offset = _bfd_elf_section_offset (info->output_bfd,
2506
0
                info,
2507
0
                input_section,
2508
0
                rel->r_offset);
2509
0
      if (offset == (bfd_vma) -1
2510
0
          || offset == (bfd_vma) -2)
2511
0
        abort ();
2512
2513
0
      outrel.r_offset = (input_section->output_section->vma
2514
0
             + input_section->output_offset
2515
0
             + offset);
2516
2517
0
      if (POINTER_LOCAL_IFUNC_P (info, h))
2518
0
        {
2519
0
          info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
2520
0
                h->root.root.string,
2521
0
                h->root.u.def.section->owner);
2522
2523
          /* This symbol is resolved locally.  */
2524
0
          outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
2525
2526
0
          if (htab->params->report_relative_reloc)
2527
0
      _bfd_x86_elf_link_report_relative_reloc
2528
0
        (info, input_section, h, sym,
2529
0
         "R_386_IRELATIVE", &outrel);
2530
2531
0
          bfd_put_32 (info->output_bfd,
2532
0
          (h->root.u.def.value
2533
0
           + h->root.u.def.section->output_section->vma
2534
0
           + h->root.u.def.section->output_offset),
2535
0
          contents + offset);
2536
0
        }
2537
0
      else
2538
0
        outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2539
2540
      /* Dynamic relocations are stored in
2541
         1. .rel.ifunc section in PIC object.
2542
         2. .rel.got section in dynamic executable.
2543
         3. .rel.iplt section in static executable.  */
2544
0
      if (bfd_link_pic (info))
2545
0
        sreloc = htab->elf.irelifunc;
2546
0
      else if (htab->elf.splt != NULL)
2547
0
        sreloc = htab->elf.srelgot;
2548
0
      else
2549
0
        sreloc = htab->elf.irelplt;
2550
0
      _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
2551
2552
      /* If this reloc is against an external symbol, we
2553
         do not want to fiddle with the addend.  Otherwise,
2554
         we need to include the symbol value so that it
2555
         becomes an addend for the dynamic reloc.  For an
2556
         internal symbol, we have updated addend.  */
2557
0
      continue;
2558
0
    }
2559
        /* FALLTHROUGH */
2560
0
      case R_386_PC32:
2561
0
      case R_386_PLT32:
2562
0
        goto do_relocation;
2563
2564
0
      case R_386_GOTOFF:
2565
        /* NB: We can't use the PLT entry as the function address
2566
     for PIC since the PIC register may not be set up
2567
     properly for indirect call. */
2568
0
        if (bfd_link_pic (info))
2569
0
    goto bad_ifunc_reloc;
2570
0
        relocation -= (gotplt->output_section->vma
2571
0
           + gotplt->output_offset);
2572
0
        goto do_relocation;
2573
0
      }
2574
0
  }
2575
2576
0
    skip_ifunc:
2577
0
      resolved_to_zero = (eh != NULL
2578
0
        && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
2579
2580
0
      switch (r_type)
2581
0
  {
2582
0
  case R_386_GOT32X:
2583
0
  case R_386_GOT32:
2584
    /* Relocation is to the entry for this symbol in the global
2585
       offset table.  */
2586
0
    if (htab->elf.sgot == NULL)
2587
0
      abort ();
2588
2589
0
    relative_reloc = false;
2590
0
    if (h != NULL)
2591
0
      {
2592
0
        off = h->got.offset;
2593
0
        if (RESOLVED_LOCALLY_P (info, h, htab))
2594
0
    {
2595
      /* We must initialize this entry in the global offset
2596
         table.  Since the offset must always be a multiple
2597
         of 4, we use the least significant bit to record
2598
         whether we have initialized it already.
2599
2600
         When doing a dynamic link, we create a .rel.got
2601
         relocation entry to initialize the value.  This
2602
         is done in the finish_dynamic_symbol routine.  */
2603
0
      if ((off & 1) != 0)
2604
0
        off &= ~1;
2605
0
      else
2606
0
        {
2607
0
          bfd_put_32 (info->output_bfd, relocation,
2608
0
          htab->elf.sgot->contents + off);
2609
0
          h->got.offset |= 1;
2610
          /* NB: Don't generate relative relocation here if
2611
       it has been generated by DT_RELR.  */
2612
0
          if (!info->enable_dt_relr
2613
0
        && GENERATE_RELATIVE_RELOC_P (info, h))
2614
0
      {
2615
        /* PR ld/21402: If this symbol isn't dynamic
2616
           in PIC, generate R_386_RELATIVE here.  */
2617
0
        eh->no_finish_dynamic_symbol = 1;
2618
0
        relative_reloc = true;
2619
0
      }
2620
0
        }
2621
0
    }
2622
0
        else
2623
0
    unresolved_reloc = false;
2624
0
      }
2625
0
    else
2626
0
      {
2627
0
        if (local_got_offsets == NULL)
2628
0
    abort ();
2629
2630
0
        off = local_got_offsets[r_symndx];
2631
2632
        /* The offset must always be a multiple of 4.  We use
2633
     the least significant bit to record whether we have
2634
     already generated the necessary reloc.  */
2635
0
        if ((off & 1) != 0)
2636
0
    off &= ~1;
2637
0
        else
2638
0
    {
2639
0
      bfd_put_32 (info->output_bfd, relocation,
2640
0
            htab->elf.sgot->contents + off);
2641
0
      local_got_offsets[r_symndx] |= 1;
2642
2643
      /* NB: Don't generate relative relocation here if it
2644
         has been generated by DT_RELR.  */
2645
0
      if (!info->enable_dt_relr && bfd_link_pic (info))
2646
0
        relative_reloc = true;
2647
0
    }
2648
0
      }
2649
2650
0
    if (relative_reloc)
2651
0
      {
2652
0
        asection *s;
2653
0
        Elf_Internal_Rela outrel;
2654
2655
0
        s = htab->elf.srelgot;
2656
0
        if (s == NULL)
2657
0
    abort ();
2658
2659
0
        outrel.r_offset = (htab->elf.sgot->output_section->vma
2660
0
         + htab->elf.sgot->output_offset
2661
0
         + off);
2662
0
        outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2663
2664
0
        if (htab->params->report_relative_reloc)
2665
0
    _bfd_x86_elf_link_report_relative_reloc
2666
0
      (info, input_section, h, sym, "R_386_RELATIVE",
2667
0
       &outrel);
2668
2669
0
        _bfd_elf_append_rel (info->output_bfd, s, &outrel);
2670
0
      }
2671
2672
0
    if (off >= (bfd_vma) -2)
2673
0
      abort ();
2674
2675
0
    relocation = (htab->elf.sgot->output_section->vma
2676
0
      + htab->elf.sgot->output_offset + off);
2677
0
    if (rel->r_offset > 1
2678
0
        && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2679
0
        && *(contents + rel->r_offset - 2) != 0x8d)
2680
0
      {
2681
0
        if (bfd_link_pic (info))
2682
0
    {
2683
      /* For PIC, disallow R_386_GOT32 without a base
2684
         register, except for "lea foo@GOT, %reg", since
2685
         we don't know what the GOT base is.  */
2686
0
      const char *name;
2687
2688
0
    disallow_got32:
2689
0
      if (h == NULL || h->root.root.string == NULL)
2690
0
        name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2691
0
               NULL);
2692
0
      else
2693
0
        name = h->root.root.string;
2694
2695
0
      _bfd_error_handler
2696
        /* xgettext:c-format */
2697
0
        (_("%pB: direct GOT relocation %s against `%s'"
2698
0
           " without base register can not be used"
2699
0
           " when making a shared object"),
2700
0
         input_bfd, howto->name, name);
2701
0
      bfd_set_error (bfd_error_bad_value);
2702
0
      return false;
2703
0
    }
2704
0
      }
2705
0
    else
2706
0
      {
2707
        /* Subtract the .got.plt section address only with a base
2708
     register.  */
2709
0
        relocation -= (htab->elf.sgotplt->output_section->vma
2710
0
           + htab->elf.sgotplt->output_offset);
2711
0
      }
2712
2713
0
    break;
2714
2715
0
  case R_386_GOTOFF:
2716
    /* Relocation is relative to the start of the global offset
2717
       table.  */
2718
2719
    /* Check to make sure it isn't a protected function or data
2720
       symbol for shared library since it may not be local when
2721
       used as function address or with copy relocation.  We also
2722
       need to make sure that a symbol is referenced locally.  */
2723
0
    if (!bfd_link_executable (info) && h)
2724
0
      {
2725
0
        if (!h->def_regular)
2726
0
    {
2727
0
      const char *v;
2728
2729
0
      switch (ELF_ST_VISIBILITY (h->other))
2730
0
        {
2731
0
        case STV_HIDDEN:
2732
0
          v = _("hidden symbol");
2733
0
          break;
2734
0
        case STV_INTERNAL:
2735
0
          v = _("internal symbol");
2736
0
          break;
2737
0
        case STV_PROTECTED:
2738
0
          v = _("protected symbol");
2739
0
          break;
2740
0
        default:
2741
0
          v = _("symbol");
2742
0
          break;
2743
0
        }
2744
2745
0
      _bfd_error_handler
2746
        /* xgettext:c-format */
2747
0
        (_("%pB: relocation R_386_GOTOFF against undefined %s"
2748
0
           " `%s' can not be used when making a shared object"),
2749
0
         input_bfd, v, h->root.root.string);
2750
0
      bfd_set_error (bfd_error_bad_value);
2751
0
      return false;
2752
0
    }
2753
0
        else if (!SYMBOL_REFERENCES_LOCAL_P (info, h)
2754
0
           && (h->type == STT_FUNC
2755
0
         || h->type == STT_OBJECT)
2756
0
           && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2757
0
    {
2758
0
      _bfd_error_handler
2759
        /* xgettext:c-format */
2760
0
        (_("%pB: relocation R_386_GOTOFF against protected %s"
2761
0
           " `%s' can not be used when making a shared object"),
2762
0
         input_bfd,
2763
0
         h->type == STT_FUNC ? "function" : "data",
2764
0
         h->root.root.string);
2765
0
      bfd_set_error (bfd_error_bad_value);
2766
0
      return false;
2767
0
    }
2768
0
      }
2769
2770
    /* Note that sgot is not involved in this
2771
       calculation.  We always want the start of .got.plt.  If we
2772
       defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2773
       permitted by the ABI, we might have to change this
2774
       calculation.  */
2775
0
    relocation -= htab->elf.sgotplt->output_section->vma
2776
0
      + htab->elf.sgotplt->output_offset;
2777
0
    break;
2778
2779
0
  case R_386_GOTPC:
2780
    /* Use global offset table as symbol value.  */
2781
0
    relocation = htab->elf.sgotplt->output_section->vma
2782
0
           + htab->elf.sgotplt->output_offset;
2783
0
    unresolved_reloc = false;
2784
0
    break;
2785
2786
0
  case R_386_PLT32:
2787
    /* Relocation is to the entry for this symbol in the
2788
       procedure linkage table.  */
2789
2790
    /* Resolve a PLT32 reloc against a local symbol directly,
2791
       without using the procedure linkage table.  */
2792
0
    if (h == NULL)
2793
0
      break;
2794
2795
0
    if ((h->plt.offset == (bfd_vma) -1
2796
0
         && eh->plt_got.offset == (bfd_vma) -1)
2797
0
        || htab->elf.splt == NULL)
2798
0
      {
2799
        /* We didn't make a PLT entry for this symbol.  This
2800
     happens when statically linking PIC code, or when
2801
     using -Bsymbolic.  */
2802
0
        break;
2803
0
      }
2804
2805
0
    if (h->plt.offset != (bfd_vma) -1)
2806
0
      {
2807
0
        if (htab->plt_second != NULL)
2808
0
    {
2809
0
      resolved_plt = htab->plt_second;
2810
0
      plt_offset = eh->plt_second.offset;
2811
0
    }
2812
0
        else
2813
0
    {
2814
0
      resolved_plt = htab->elf.splt;
2815
0
      plt_offset = h->plt.offset;
2816
0
    }
2817
0
      }
2818
0
    else
2819
0
      {
2820
0
        resolved_plt = htab->plt_got;
2821
0
        plt_offset = eh->plt_got.offset;
2822
0
      }
2823
2824
0
    relocation = (resolved_plt->output_section->vma
2825
0
      + resolved_plt->output_offset
2826
0
      + plt_offset);
2827
0
    unresolved_reloc = false;
2828
0
    break;
2829
2830
0
  case R_386_SIZE32:
2831
    /* Set to symbol size.  */
2832
0
    relocation = st_size;
2833
    /* Fall through.  */
2834
2835
0
  case R_386_32:
2836
0
  case R_386_PC32:
2837
0
    if ((input_section->flags & SEC_ALLOC) == 0
2838
0
        || is_vxworks_tls)
2839
0
      break;
2840
2841
0
    if (GENERATE_DYNAMIC_RELOCATION_P (false, info, eh, r_type,
2842
0
               sec, false,
2843
0
               resolved_to_zero,
2844
0
               (r_type == R_386_PC32)))
2845
0
      {
2846
0
        Elf_Internal_Rela outrel;
2847
0
        bool skip, relocate;
2848
0
        bool generate_dynamic_reloc = true;
2849
0
        asection *sreloc;
2850
2851
        /* When generating a shared object, these relocations
2852
     are copied into the output file to be resolved at run
2853
     time.  */
2854
2855
0
        skip = false;
2856
0
        relocate = false;
2857
2858
0
        outrel.r_offset =
2859
0
    _bfd_elf_section_offset (info->output_bfd, info,
2860
0
           input_section, rel->r_offset);
2861
0
        if (outrel.r_offset == (bfd_vma) -1)
2862
0
    skip = true;
2863
0
        else if (outrel.r_offset == (bfd_vma) -2)
2864
0
    skip = true, relocate = true;
2865
0
        outrel.r_offset += (input_section->output_section->vma
2866
0
          + input_section->output_offset);
2867
2868
0
        if (skip)
2869
0
    memset (&outrel, 0, sizeof outrel);
2870
0
        else if (COPY_INPUT_RELOC_P (false, info, h, r_type))
2871
0
    outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2872
0
        else
2873
0
    {
2874
      /* This symbol is local, or marked to become local.  */
2875
0
      relocate = true;
2876
      /* NB: Don't generate relative relocation here if it
2877
         has been generated by DT_RELR.  */
2878
0
      if (info->enable_dt_relr)
2879
0
        generate_dynamic_reloc = false;
2880
0
      else
2881
0
        {
2882
0
          outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2883
2884
0
          if (htab->params->report_relative_reloc)
2885
0
      _bfd_x86_elf_link_report_relative_reloc
2886
0
        (info, input_section, h, sym, "R_386_RELATIVE",
2887
0
         &outrel);
2888
0
        }
2889
0
    }
2890
2891
0
        if (generate_dynamic_reloc)
2892
0
    {
2893
0
      sreloc = elf_section_data (input_section)->sreloc;
2894
2895
0
      if (sreloc == NULL || sreloc->contents == NULL)
2896
0
        {
2897
0
          r = bfd_reloc_notsupported;
2898
0
          goto check_relocation_error;
2899
0
        }
2900
2901
0
      _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
2902
0
    }
2903
2904
        /* If this reloc is against an external symbol, we do
2905
     not want to fiddle with the addend.  Otherwise, we
2906
     need to include the symbol value so that it becomes
2907
     an addend for the dynamic reloc.  */
2908
0
        if (! relocate)
2909
0
    continue;
2910
0
      }
2911
0
    break;
2912
2913
0
  case R_386_TLS_IE:
2914
0
    if (!bfd_link_executable (info))
2915
0
      {
2916
0
        Elf_Internal_Rela outrel;
2917
0
        asection *sreloc;
2918
2919
0
        outrel.r_offset = rel->r_offset
2920
0
        + input_section->output_section->vma
2921
0
        + input_section->output_offset;
2922
0
        outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2923
2924
0
        if (htab->params->report_relative_reloc)
2925
0
    _bfd_x86_elf_link_report_relative_reloc
2926
0
      (info, input_section, h, sym, "R_386_RELATIVE",
2927
0
       &outrel);
2928
2929
0
        sreloc = elf_section_data (input_section)->sreloc;
2930
0
        if (sreloc == NULL)
2931
0
    abort ();
2932
0
        _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
2933
0
      }
2934
    /* Fall through */
2935
2936
0
  case R_386_TLS_GD:
2937
0
  case R_386_TLS_GOTDESC:
2938
0
  case R_386_TLS_DESC_CALL:
2939
0
  case R_386_TLS_IE_32:
2940
0
  case R_386_TLS_GOTIE:
2941
0
    tls_type = GOT_UNKNOWN;
2942
0
    if (h == NULL && local_got_offsets)
2943
0
      tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
2944
0
    else if (h != NULL)
2945
0
      tls_type = elf_x86_hash_entry(h)->tls_type;
2946
0
    if (tls_type == GOT_TLS_IE)
2947
0
      tls_type = GOT_TLS_IE_NEG;
2948
2949
0
     r_type_tls = r_type;
2950
0
    if (! elf_i386_tls_transition (info, input_bfd,
2951
0
           input_section, contents,
2952
0
           symtab_hdr, sym_hashes,
2953
0
           &r_type_tls, tls_type, rel,
2954
0
           relend, h, sym, true))
2955
0
      return false;
2956
2957
0
    expected_tls_le = htab->elf.target_os == is_solaris
2958
0
      ? R_386_TLS_LE : R_386_TLS_LE_32;
2959
0
    if (r_type_tls == expected_tls_le)
2960
0
      {
2961
        /* NB: Solaris only supports R_386_TLS_GD->R_386_TLS_LE.  */
2962
0
        BFD_ASSERT (! unresolved_reloc
2963
0
        && (htab->elf.target_os != is_solaris
2964
0
            || (htab->elf.target_os == is_solaris
2965
0
          && (r_type == R_386_TLS_GD
2966
0
              || r_type == R_386_TLS_IE
2967
0
              || r_type == R_386_TLS_GOTIE))));
2968
0
        if (r_type == R_386_TLS_GD)
2969
0
    {
2970
0
      unsigned int type;
2971
0
      bfd_vma roff;
2972
2973
      /* GD->LE transition.  */
2974
0
      type = *(contents + rel->r_offset - 2);
2975
0
      if (type == 0x04)
2976
0
        {
2977
          /* Change
2978
        leal foo@tlsgd(,%ebx,1), %eax
2979
        call ___tls_get_addr@PLT
2980
       into:
2981
        movl %gs:0, %eax
2982
        subl $foo@tpoff, %eax
2983
       (6 byte form of subl).  */
2984
0
          roff = rel->r_offset + 5;
2985
0
        }
2986
0
      else
2987
0
        {
2988
          /* Change
2989
        leal foo@tlsgd(%ebx), %eax
2990
        call ___tls_get_addr@PLT
2991
        nop
2992
       or
2993
        leal foo@tlsgd(%reg), %eax
2994
        call *___tls_get_addr@GOT(%reg)
2995
        which may be converted to
2996
        addr32 call ___tls_get_addr
2997
       into:
2998
        movl %gs:0, %eax; subl $foo@tpoff, %eax
2999
       (6 byte form of subl).  */
3000
0
          roff = rel->r_offset + 6;
3001
0
        }
3002
0
      memcpy (contents + roff - 8,
3003
0
        "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3004
0
      bfd_put_32 (info->output_bfd,
3005
0
            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 (info->output_bfd, val ^ 0x86,
3032
0
           contents + roff - 1);
3033
0
      bfd_put_32 (info->output_bfd,
3034
0
            -elf_i386_tpoff (info, relocation),
3035
0
            contents + roff);
3036
0
      continue;
3037
0
    }
3038
0
        else if (r_type == R_386_TLS_DESC_CALL)
3039
0
    {
3040
      /* GDesc -> LE transition.
3041
         It's originally:
3042
         call *(%eax)
3043
         Turn it into:
3044
         xchg %ax,%ax  */
3045
3046
0
      bfd_vma roff;
3047
3048
0
      roff = rel->r_offset;
3049
0
      bfd_put_8 (info->output_bfd, 0x66, contents + roff);
3050
0
      bfd_put_8 (info->output_bfd, 0x90, contents + roff + 1);
3051
0
      continue;
3052
0
    }
3053
0
        else if (r_type == R_386_TLS_IE)
3054
0
    {
3055
0
      unsigned int val;
3056
3057
      /* IE->LE transition:
3058
         Originally it can be one of:
3059
         movl foo, %eax
3060
         movl foo, %reg
3061
         addl foo, %reg
3062
         We change it into:
3063
         movl $foo, %eax
3064
         movl $foo, %reg
3065
         addl $foo, %reg.  */
3066
0
      val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3067
0
      if (val == 0xa1)
3068
0
        {
3069
          /* movl foo, %eax.  */
3070
0
          bfd_put_8 (info->output_bfd, 0xb8,
3071
0
         contents + rel->r_offset - 1);
3072
0
        }
3073
0
      else
3074
0
        {
3075
0
          unsigned int type;
3076
3077
0
          type = bfd_get_8 (input_bfd,
3078
0
          contents + rel->r_offset - 2);
3079
0
          switch (type)
3080
0
      {
3081
0
      case 0x8b:
3082
        /* movl */
3083
0
        bfd_put_8 (info->output_bfd, 0xc7,
3084
0
             contents + rel->r_offset - 2);
3085
0
        bfd_put_8 (info->output_bfd,
3086
0
             0xc0 | ((val >> 3) & 7),
3087
0
             contents + rel->r_offset - 1);
3088
0
        break;
3089
0
      case 0x03:
3090
        /* addl */
3091
0
        bfd_put_8 (info->output_bfd, 0x81,
3092
0
             contents + rel->r_offset - 2);
3093
0
        bfd_put_8 (info->output_bfd,
3094
0
             0xc0 | ((val >> 3) & 7),
3095
0
             contents + rel->r_offset - 1);
3096
0
        break;
3097
0
      default:
3098
0
        BFD_FAIL ();
3099
0
        break;
3100
0
      }
3101
0
        }
3102
0
      bfd_put_32 (info->output_bfd,
3103
0
            -elf_i386_tpoff (info, relocation),
3104
0
            contents + rel->r_offset);
3105
0
      continue;
3106
0
    }
3107
0
        else
3108
0
    {
3109
0
      unsigned int val, type;
3110
3111
      /* {IE_32,GOTIE}->LE transition:
3112
         Originally it can be one of:
3113
         subl foo(%reg1), %reg2
3114
         movl foo(%reg1), %reg2
3115
         addl foo(%reg1), %reg2
3116
         We change it into:
3117
         subl $foo, %reg2
3118
         movl $foo, %reg2 (6 byte form)
3119
         addl $foo, %reg2.  */
3120
0
      type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3121
0
      val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3122
0
      if (type == 0x8b)
3123
0
        {
3124
          /* movl */
3125
0
          bfd_put_8 (info->output_bfd, 0xc7,
3126
0
         contents + rel->r_offset - 2);
3127
0
          bfd_put_8 (info->output_bfd, 0xc0 | ((val >> 3) & 7),
3128
0
         contents + rel->r_offset - 1);
3129
0
        }
3130
0
      else if (type == 0x2b)
3131
0
        {
3132
          /* subl */
3133
0
          bfd_put_8 (info->output_bfd, 0x81,
3134
0
         contents + rel->r_offset - 2);
3135
0
          bfd_put_8 (info->output_bfd, 0xe8 | ((val >> 3) & 7),
3136
0
         contents + rel->r_offset - 1);
3137
0
        }
3138
0
      else if (type == 0x03)
3139
0
        {
3140
          /* addl */
3141
0
          bfd_put_8 (info->output_bfd, 0x81,
3142
0
         contents + rel->r_offset - 2);
3143
0
          bfd_put_8 (info->output_bfd, 0xc0 | ((val >> 3) & 7),
3144
0
         contents + rel->r_offset - 1);
3145
0
        }
3146
0
      else
3147
0
        BFD_FAIL ();
3148
0
      if (r_type == R_386_TLS_GOTIE)
3149
0
        bfd_put_32 (info->output_bfd,
3150
0
        -elf_i386_tpoff (info, relocation),
3151
0
        contents + rel->r_offset);
3152
0
      else
3153
0
        bfd_put_32 (info->output_bfd,
3154
0
        elf_i386_tpoff (info, relocation),
3155
0
        contents + rel->r_offset);
3156
0
      continue;
3157
0
    }
3158
0
      }
3159
3160
0
    if (htab->elf.sgot == NULL)
3161
0
      abort ();
3162
3163
0
    if (h != NULL)
3164
0
      {
3165
0
        off = h->got.offset;
3166
0
        offplt = elf_x86_hash_entry (h)->tlsdesc_got;
3167
0
      }
3168
0
    else
3169
0
      {
3170
0
        if (local_got_offsets == NULL)
3171
0
    abort ();
3172
3173
0
        off = local_got_offsets[r_symndx];
3174
0
        offplt = local_tlsdesc_gotents[r_symndx];
3175
0
      }
3176
3177
0
    if ((off & 1) != 0)
3178
0
      off &= ~1;
3179
0
    else
3180
0
      {
3181
0
        Elf_Internal_Rela outrel;
3182
0
        int dr_type;
3183
0
        asection *sreloc;
3184
3185
0
        if (htab->elf.srelgot == NULL)
3186
0
    abort ();
3187
3188
0
        indx = h && h->dynindx != -1 ? h->dynindx : 0;
3189
3190
0
        if (GOT_TLS_GDESC_P (tls_type))
3191
0
    {
3192
0
      outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3193
0
      BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3194
0
            <= htab->elf.sgotplt->size);
3195
0
      outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3196
0
             + htab->elf.sgotplt->output_offset
3197
0
             + offplt
3198
0
             + htab->sgotplt_jump_table_size);
3199
0
      sreloc = htab->rel_tls_desc;
3200
0
      _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
3201
0
      if (indx == 0)
3202
0
        {
3203
0
          BFD_ASSERT (! unresolved_reloc);
3204
0
          bfd_put_32 (info->output_bfd,
3205
0
          relocation - _bfd_x86_elf_dtpoff_base (info),
3206
0
          htab->elf.sgotplt->contents + offplt
3207
0
          + htab->sgotplt_jump_table_size + 4);
3208
0
        }
3209
0
      else
3210
0
        {
3211
0
          bfd_put_32 (info->output_bfd, 0,
3212
0
          htab->elf.sgotplt->contents + offplt
3213
0
          + htab->sgotplt_jump_table_size + 4);
3214
0
        }
3215
0
    }
3216
3217
0
        sreloc = htab->elf.srelgot;
3218
3219
0
        outrel.r_offset = (htab->elf.sgot->output_section->vma
3220
0
         + htab->elf.sgot->output_offset + off);
3221
3222
0
        if (GOT_TLS_GD_P (tls_type))
3223
0
    dr_type = R_386_TLS_DTPMOD32;
3224
0
        else if (GOT_TLS_GDESC_P (tls_type))
3225
0
    goto dr_done;
3226
0
        else if (tls_type == GOT_TLS_IE_POS)
3227
0
    dr_type = R_386_TLS_TPOFF;
3228
0
        else
3229
0
    dr_type = R_386_TLS_TPOFF32;
3230
3231
0
        if (dr_type == R_386_TLS_TPOFF && indx == 0)
3232
0
    bfd_put_32 (info->output_bfd,
3233
0
          relocation - _bfd_x86_elf_dtpoff_base (info),
3234
0
          htab->elf.sgot->contents + off);
3235
0
        else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3236
0
    bfd_put_32 (info->output_bfd,
3237
0
          _bfd_x86_elf_dtpoff_base (info) - relocation,
3238
0
          htab->elf.sgot->contents + off);
3239
0
        else if (dr_type != R_386_TLS_DESC)
3240
0
    bfd_put_32 (info->output_bfd, 0,
3241
0
          htab->elf.sgot->contents + off);
3242
0
        outrel.r_info = ELF32_R_INFO (indx, dr_type);
3243
3244
0
        _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
3245
3246
0
        if (GOT_TLS_GD_P (tls_type))
3247
0
    {
3248
0
      if (indx == 0)
3249
0
        {
3250
0
          BFD_ASSERT (! unresolved_reloc);
3251
0
          bfd_put_32 (info->output_bfd,
3252
0
          relocation - _bfd_x86_elf_dtpoff_base (info),
3253
0
          htab->elf.sgot->contents + off + 4);
3254
0
        }
3255
0
      else
3256
0
        {
3257
0
          bfd_put_32 (info->output_bfd, 0,
3258
0
          htab->elf.sgot->contents + off + 4);
3259
0
          outrel.r_info = ELF32_R_INFO (indx,
3260
0
                R_386_TLS_DTPOFF32);
3261
0
          outrel.r_offset += 4;
3262
0
          _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
3263
0
        }
3264
0
    }
3265
0
        else if (tls_type == GOT_TLS_IE_BOTH)
3266
0
    {
3267
0
      bfd_put_32 (info->output_bfd,
3268
0
            (indx == 0
3269
0
             ? relocation - _bfd_x86_elf_dtpoff_base (info)
3270
0
             : 0),
3271
0
            htab->elf.sgot->contents + off + 4);
3272
0
      outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3273
0
      outrel.r_offset += 4;
3274
0
      _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
3275
0
    }
3276
3277
0
      dr_done:
3278
0
        if (h != NULL)
3279
0
    h->got.offset |= 1;
3280
0
        else
3281
0
    local_got_offsets[r_symndx] |= 1;
3282
0
      }
3283
3284
0
    if (off >= (bfd_vma) -2
3285
0
        && ! GOT_TLS_GDESC_P (tls_type))
3286
0
      abort ();
3287
0
    if (r_type_tls == R_386_TLS_GOTDESC
3288
0
        || r_type_tls == R_386_TLS_DESC_CALL)
3289
0
      {
3290
0
        relocation = htab->sgotplt_jump_table_size + offplt;
3291
0
        unresolved_reloc = false;
3292
0
      }
3293
0
    else if (r_type_tls == r_type)
3294
0
      {
3295
0
        bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
3296
0
            + htab->elf.sgotplt->output_offset;
3297
0
        relocation = htab->elf.sgot->output_section->vma
3298
0
    + htab->elf.sgot->output_offset + off - g_o_t;
3299
0
        if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3300
0
      && tls_type == GOT_TLS_IE_BOTH)
3301
0
    relocation += 4;
3302
0
        if (r_type == R_386_TLS_IE)
3303
0
    relocation += g_o_t;
3304
0
        unresolved_reloc = false;
3305
0
      }
3306
0
    else if (r_type == R_386_TLS_GD)
3307
0
      {
3308
0
        unsigned int val, type;
3309
0
        bfd_vma roff;
3310
3311
        /* GD->IE transition.  */
3312
0
        type = *(contents + rel->r_offset - 2);
3313
0
        val = *(contents + rel->r_offset - 1);
3314
0
        if (type == 0x04)
3315
0
    {
3316
      /* Change
3317
      leal foo@tlsgd(,%ebx,1), %eax
3318
      call ___tls_get_addr@PLT
3319
         into:
3320
      movl %gs:0, %eax
3321
      subl $foo@gottpoff(%ebx), %eax.  */
3322
0
      val >>= 3;
3323
0
      roff = rel->r_offset - 3;
3324
0
    }
3325
0
        else
3326
0
    {
3327
      /* Change
3328
      leal foo@tlsgd(%ebx), %eax
3329
      call ___tls_get_addr@PLT
3330
      nop
3331
         or
3332
      leal foo@tlsgd(%reg), %eax
3333
      call *___tls_get_addr@GOT(%reg)
3334
      which may be converted to
3335
      addr32 call ___tls_get_addr
3336
         into:
3337
      movl %gs:0, %eax;
3338
      subl $foo@gottpoff(%reg), %eax.  */
3339
0
      roff = rel->r_offset - 2;
3340
0
    }
3341
0
        memcpy (contents + roff,
3342
0
          "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3343
0
        contents[roff + 7] = 0x80 | (val & 7);
3344
        /* If foo is used only with foo@gotntpoff(%reg) and
3345
     foo@indntpoff, but not with foo@gottpoff(%reg), change
3346
     subl $foo@gottpoff(%reg), %eax
3347
     into:
3348
     addl $foo@gotntpoff(%reg), %eax.  */
3349
0
        if (tls_type == GOT_TLS_IE_POS)
3350
0
    contents[roff + 6] = 0x03;
3351
0
        bfd_put_32 (info->output_bfd,
3352
0
        htab->elf.sgot->output_section->vma
3353
0
        + htab->elf.sgot->output_offset + off
3354
0
        - htab->elf.sgotplt->output_section->vma
3355
0
        - htab->elf.sgotplt->output_offset,
3356
0
        contents + roff + 8);
3357
        /* Skip R_386_PLT32 and R_386_GOT32X.  */
3358
0
        rel++;
3359
0
        wrel++;
3360
0
        continue;
3361
0
      }
3362
0
    else if (r_type == R_386_TLS_GOTDESC)
3363
0
      {
3364
        /* GDesc -> IE transition.
3365
     It's originally something like:
3366
     leal x@tlsdesc(%ebx), %eax
3367
3368
     Change it to:
3369
     movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3370
     or:
3371
     movl x@gottpoff(%ebx), %eax # before negl %eax
3372
3373
     Registers other than %eax may be set up here.  */
3374
3375
0
        bfd_vma roff;
3376
3377
        /* First, make sure it's a leal adding ebx to a 32-bit
3378
     offset into any register, although it's probably
3379
     almost always going to be eax.  */
3380
0
        roff = rel->r_offset;
3381
3382
        /* Now modify the instruction as appropriate.  */
3383
        /* To turn a leal into a movl in the form we use it, it
3384
     suffices to change the first byte from 0x8d to 0x8b.
3385
     aoliva FIXME: should we decide to keep the leal, all
3386
     we have to do is remove the statement below, and
3387
     adjust the relaxation of R_386_TLS_DESC_CALL.  */
3388
0
        bfd_put_8 (info->output_bfd, 0x8b, contents + roff - 2);
3389
3390
0
        if (tls_type == GOT_TLS_IE_BOTH)
3391
0
    off += 4;
3392
3393
0
        bfd_put_32 (info->output_bfd,
3394
0
        htab->elf.sgot->output_section->vma
3395
0
        + htab->elf.sgot->output_offset + off
3396
0
        - htab->elf.sgotplt->output_section->vma
3397
0
        - htab->elf.sgotplt->output_offset,
3398
0
        contents + roff);
3399
0
        continue;
3400
0
      }
3401
0
    else if (r_type == R_386_TLS_DESC_CALL)
3402
0
      {
3403
        /* GDesc -> IE transition.
3404
     It's originally:
3405
     call *(%eax)
3406
3407
     Change it to:
3408
     xchg %ax,%ax
3409
     or
3410
     negl %eax
3411
     depending on how we transformed the TLS_GOTDESC above.
3412
        */
3413
3414
0
        bfd_vma roff;
3415
3416
0
        roff = rel->r_offset;
3417
3418
        /* Now modify the instruction as appropriate.  */
3419
0
        if (tls_type != GOT_TLS_IE_NEG)
3420
0
    {
3421
      /* xchg %ax,%ax */
3422
0
      bfd_put_8 (info->output_bfd, 0x66, contents + roff);
3423
0
      bfd_put_8 (info->output_bfd, 0x90, contents + roff + 1);
3424
0
    }
3425
0
        else
3426
0
    {
3427
      /* negl %eax */
3428
0
      bfd_put_8 (info->output_bfd, 0xf7, contents + roff);
3429
0
      bfd_put_8 (info->output_bfd, 0xd8, contents + roff + 1);
3430
0
    }
3431
3432
0
        continue;
3433
0
      }
3434
0
    else
3435
0
      BFD_ASSERT (false);
3436
0
    break;
3437
3438
0
  case R_386_TLS_LDM:
3439
0
    if (! elf_i386_tls_transition (info, input_bfd,
3440
0
           input_section, contents,
3441
0
           symtab_hdr, sym_hashes,
3442
0
           &r_type, GOT_UNKNOWN, rel,
3443
0
           relend, h, sym, true))
3444
0
      return false;
3445
3446
0
    if (r_type != R_386_TLS_LDM)
3447
0
      {
3448
        /* LD->LE transition.  Change
3449
      leal foo@tlsldm(%ebx) %eax
3450
      call ___tls_get_addr@PLT
3451
     into:
3452
      movl %gs:0, %eax
3453
      nop
3454
      leal 0(%esi,1), %esi
3455
     or change
3456
      leal foo@tlsldm(%reg) %eax
3457
      call *___tls_get_addr@GOT(%reg)
3458
      which may be converted to
3459
      addr32 call ___tls_get_addr
3460
     into:
3461
      movl %gs:0, %eax
3462
      leal 0(%esi), %esi  */
3463
0
        expected_tls_le = htab->elf.target_os == is_solaris
3464
0
    ? R_386_TLS_LE : R_386_TLS_LE_32;
3465
0
        BFD_ASSERT (r_type == expected_tls_le);
3466
0
        if (*(contents + rel->r_offset + 4) == 0xff
3467
0
      || *(contents + rel->r_offset + 4) == 0x67)
3468
0
    memcpy (contents + rel->r_offset - 2,
3469
0
      "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
3470
0
        else
3471
0
    memcpy (contents + rel->r_offset - 2,
3472
0
      "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3473
        /* Skip R_386_PC32/R_386_PLT32.  */
3474
0
        rel++;
3475
0
        wrel++;
3476
0
        continue;
3477
0
      }
3478
3479
0
    if (htab->elf.sgot == NULL)
3480
0
      abort ();
3481
3482
0
    off = htab->tls_ld_or_ldm_got.offset;
3483
0
    if (off & 1)
3484
0
      off &= ~1;
3485
0
    else
3486
0
      {
3487
0
        Elf_Internal_Rela outrel;
3488
3489
0
        if (htab->elf.srelgot == NULL)
3490
0
    abort ();
3491
3492
0
        outrel.r_offset = (htab->elf.sgot->output_section->vma
3493
0
         + htab->elf.sgot->output_offset + off);
3494
3495
0
        bfd_put_32 (info->output_bfd, 0,
3496
0
        htab->elf.sgot->contents + off);
3497
0
        bfd_put_32 (info->output_bfd, 0,
3498
0
        htab->elf.sgot->contents + off + 4);
3499
0
        outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3500
0
        _bfd_elf_append_rel (info->output_bfd, htab->elf.srelgot, &outrel);
3501
0
        htab->tls_ld_or_ldm_got.offset |= 1;
3502
0
      }
3503
0
    relocation = htab->elf.sgot->output_section->vma
3504
0
           + htab->elf.sgot->output_offset + off
3505
0
           - htab->elf.sgotplt->output_section->vma
3506
0
           - htab->elf.sgotplt->output_offset;
3507
0
    unresolved_reloc = false;
3508
0
    break;
3509
3510
0
  case R_386_TLS_LDO_32:
3511
0
    if (!bfd_link_executable (info)
3512
0
        || (input_section->flags & SEC_CODE) == 0)
3513
0
      relocation -= _bfd_x86_elf_dtpoff_base (info);
3514
0
    else
3515
      /* When converting LDO to LE, we must negate.  */
3516
0
      relocation = -elf_i386_tpoff (info, relocation);
3517
0
    break;
3518
3519
0
  case R_386_TLS_LE_32:
3520
0
  case R_386_TLS_LE:
3521
0
    if (!bfd_link_executable (info))
3522
0
      {
3523
0
        Elf_Internal_Rela outrel;
3524
0
        asection *sreloc;
3525
3526
0
        outrel.r_offset = rel->r_offset
3527
0
        + input_section->output_section->vma
3528
0
        + input_section->output_offset;
3529
0
        if (h != NULL && h->dynindx != -1)
3530
0
    indx = h->dynindx;
3531
0
        else
3532
0
    indx = 0;
3533
0
        if (r_type == R_386_TLS_LE_32)
3534
0
    outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3535
0
        else
3536
0
    outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3537
0
        sreloc = elf_section_data (input_section)->sreloc;
3538
0
        if (sreloc == NULL)
3539
0
    abort ();
3540
0
        _bfd_elf_append_rel (info->output_bfd, sreloc, &outrel);
3541
0
        if (indx)
3542
0
    continue;
3543
0
        else if (r_type == R_386_TLS_LE_32)
3544
0
    relocation = _bfd_x86_elf_dtpoff_base (info) - relocation;
3545
0
        else
3546
0
    relocation -= _bfd_x86_elf_dtpoff_base (info);
3547
0
      }
3548
0
    else if (r_type == R_386_TLS_LE_32)
3549
0
      relocation = elf_i386_tpoff (info, relocation);
3550
0
    else
3551
0
      relocation = -elf_i386_tpoff (info, relocation);
3552
0
    break;
3553
3554
0
  default:
3555
0
    break;
3556
0
  }
3557
3558
      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3559
   because such sections are not SEC_ALLOC and thus ld.so will
3560
   not process them.  */
3561
0
      if (unresolved_reloc
3562
0
    && !((input_section->flags & SEC_DEBUGGING) != 0
3563
0
         && h->def_dynamic)
3564
0
    && _bfd_elf_section_offset (info->output_bfd, info, input_section,
3565
0
              rel->r_offset) != (bfd_vma) -1)
3566
0
  {
3567
0
    _bfd_error_handler
3568
      /* xgettext:c-format */
3569
0
      (_("%pB(%pA+%#" PRIx64 "): unresolvable %s relocation against symbol `%s'"),
3570
0
       input_bfd,
3571
0
       input_section,
3572
0
       (uint64_t) rel->r_offset,
3573
0
       howto->name,
3574
0
       h->root.root.string);
3575
0
    return false;
3576
0
  }
3577
3578
0
    do_relocation:
3579
0
      r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3580
0
            contents, rel->r_offset,
3581
0
            relocation, 0);
3582
3583
0
    check_relocation_error:
3584
0
      if (r != bfd_reloc_ok)
3585
0
  {
3586
0
    const char *name;
3587
3588
0
    if (h != NULL)
3589
0
      name = h->root.root.string;
3590
0
    else
3591
0
      {
3592
0
        name = bfd_elf_string_from_elf_section (input_bfd,
3593
0
                  symtab_hdr->sh_link,
3594
0
                  sym->st_name);
3595
0
        if (name == NULL)
3596
0
    return false;
3597
0
        if (*name == '\0')
3598
0
    name = bfd_section_name (sec);
3599
0
      }
3600
3601
0
    if (r == bfd_reloc_overflow)
3602
0
      (*info->callbacks->reloc_overflow)
3603
0
        (info, (h ? &h->root : NULL), name, howto->name,
3604
0
         (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3605
0
    else
3606
0
      {
3607
0
        _bfd_error_handler
3608
    /* xgettext:c-format */
3609
0
    (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
3610
0
     input_bfd, input_section,
3611
0
     (uint64_t) rel->r_offset, name, (int) r);
3612
0
        return false;
3613
0
      }
3614
0
  }
3615
3616
0
      if (wrel != rel)
3617
0
  *wrel = *rel;
3618
0
    }
3619
3620
0
  if (wrel != rel)
3621
0
    {
3622
0
      Elf_Internal_Shdr *rel_hdr;
3623
0
      size_t deleted = rel - wrel;
3624
3625
0
      rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
3626
0
      rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3627
0
      rel_hdr = _bfd_elf_single_rel_hdr (input_section);
3628
0
      rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3629
0
      input_section->reloc_count -= deleted;
3630
0
    }
3631
3632
0
  return true;
3633
0
}
3634
3635
/* Finish up dynamic symbol handling.  We set the contents of various
3636
   dynamic sections here.  */
3637
3638
static bool
3639
elf_i386_finish_dynamic_symbol (struct bfd_link_info *info,
3640
        struct elf_link_hash_entry *h,
3641
        Elf_Internal_Sym *sym)
3642
0
{
3643
0
  struct elf_x86_link_hash_table *htab;
3644
0
  unsigned plt_entry_size;
3645
0
  struct elf_x86_link_hash_entry *eh;
3646
0
  bool local_undefweak;
3647
0
  bool use_plt_second;
3648
3649
0
  htab = elf_x86_hash_table (info, I386_ELF_DATA);
3650
3651
0
  plt_entry_size = htab->plt.plt_entry_size;
3652
3653
  /* Use the second PLT section only if there is .plt section.  */
3654
0
  use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
3655
3656
0
  eh = (struct elf_x86_link_hash_entry *) h;
3657
0
  if (eh->no_finish_dynamic_symbol)
3658
0
    abort ();
3659
3660
  /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
3661
     resolved undefined weak symbols in executable so that their
3662
     references have value 0 at run-time.  */
3663
0
  local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
3664
3665
0
  if (h->plt.offset != (bfd_vma) -1)
3666
0
    {
3667
0
      bfd_vma plt_index, plt_offset;
3668
0
      bfd_vma got_offset;
3669
0
      Elf_Internal_Rela rel;
3670
0
      bfd_byte *loc;
3671
0
      asection *plt, *resolved_plt, *gotplt, *relplt;
3672
3673
      /* When building a static executable, use .iplt, .igot.plt and
3674
   .rel.iplt sections for STT_GNU_IFUNC symbols.  */
3675
0
      if (htab->elf.splt != NULL)
3676
0
  {
3677
0
    plt = htab->elf.splt;
3678
0
    gotplt = htab->elf.sgotplt;
3679
0
    relplt = htab->elf.srelplt;
3680
0
  }
3681
0
      else
3682
0
  {
3683
0
    plt = htab->elf.iplt;
3684
0
    gotplt = htab->elf.igotplt;
3685
0
    relplt = htab->elf.irelplt;
3686
0
  }
3687
3688
0
      VERIFY_PLT_ENTRY (info, h, plt, gotplt, relplt, local_undefweak)
3689
3690
      /* Get the index in the procedure linkage table which
3691
   corresponds to this symbol.  This is the index of this symbol
3692
   in all the symbols for which we are making plt entries.  The
3693
   first entry in the procedure linkage table is reserved.
3694
3695
   Get the offset into the .got table of the entry that
3696
   corresponds to this function.  Each .got entry is 4 bytes.
3697
   The first three are reserved.
3698
3699
   For static executables, we don't reserve anything.  */
3700
3701
0
      if (plt == htab->elf.splt)
3702
0
  {
3703
0
    got_offset = (h->plt.offset / plt_entry_size
3704
0
      - htab->plt.has_plt0);
3705
0
    got_offset = (got_offset + 3) * 4;
3706
0
  }
3707
0
      else
3708
0
  {
3709
0
    got_offset = h->plt.offset / plt_entry_size;
3710
0
    got_offset = got_offset * 4;
3711
0
  }
3712
3713
      /* Fill in the entry in the procedure linkage table and update
3714
   the first slot.  */
3715
0
      memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
3716
0
        plt_entry_size);
3717
3718
0
      if (use_plt_second)
3719
0
  {
3720
0
    const bfd_byte *plt_entry;
3721
0
    if (bfd_link_pic (info))
3722
0
      plt_entry = htab->non_lazy_plt->pic_plt_entry;
3723
0
    else
3724
0
      plt_entry = htab->non_lazy_plt->plt_entry;
3725
0
    memcpy (htab->plt_second->contents + eh->plt_second.offset,
3726
0
      plt_entry, htab->non_lazy_plt->plt_entry_size);
3727
3728
0
    resolved_plt = htab->plt_second;
3729
0
    plt_offset = eh->plt_second.offset;
3730
0
  }
3731
0
      else
3732
0
  {
3733
0
    resolved_plt = plt;
3734
0
    plt_offset = h->plt.offset;
3735
0
  }
3736
3737
0
      if (! bfd_link_pic (info))
3738
0
  {
3739
0
    bfd_put_32 (info->output_bfd,
3740
0
          (gotplt->output_section->vma
3741
0
           + gotplt->output_offset
3742
0
           + got_offset),
3743
0
          resolved_plt->contents + plt_offset
3744
0
          + htab->plt.plt_got_offset);
3745
3746
0
    if (htab->elf.target_os == is_vxworks)
3747
0
      {
3748
0
        int s, k, reloc_index;
3749
3750
        /* Create the R_386_32 relocation referencing the GOT
3751
     for this PLT entry.  */
3752
3753
        /* S: Current slot number (zero-based).  */
3754
0
        s = ((h->plt.offset - htab->plt.plt_entry_size)
3755
0
       / htab->plt.plt_entry_size);
3756
        /* K: Number of relocations for PLTResolve. */
3757
0
        if (bfd_link_pic (info))
3758
0
    k = PLTRESOLVE_RELOCS_SHLIB;
3759
0
        else
3760
0
    k = PLTRESOLVE_RELOCS;
3761
        /* Skip the PLTresolve relocations, and the relocations for
3762
     the other PLT slots. */
3763
0
        reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3764
0
        loc = (htab->srelplt2->contents + reloc_index
3765
0
         * sizeof (Elf32_External_Rel));
3766
3767
0
        rel.r_offset = (plt->output_section->vma
3768
0
            + plt->output_offset
3769
0
            + h->plt.offset + 2),
3770
0
        rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3771
0
        bfd_elf32_swap_reloc_out (info->output_bfd, &rel, loc);
3772
3773
        /* Create the R_386_32 relocation referencing the beginning of
3774
     the PLT for this GOT entry.  */
3775
0
        rel.r_offset = (htab->elf.sgotplt->output_section->vma
3776
0
            + htab->elf.sgotplt->output_offset
3777
0
            + got_offset);
3778
0
        rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3779
0
        bfd_elf32_swap_reloc_out (info->output_bfd, &rel,
3780
0
          loc + sizeof (Elf32_External_Rel));
3781
0
      }
3782
0
  }
3783
0
      else
3784
0
  {
3785
0
    bfd_put_32 (info->output_bfd, got_offset,
3786
0
          resolved_plt->contents + plt_offset
3787
0
          + htab->plt.plt_got_offset);
3788
0
  }
3789
3790
      /* Fill in the entry in the global offset table.  Leave the entry
3791
   as zero for undefined weak symbol in PIE.  No PLT relocation
3792
   against undefined weak symbol in PIE.  */
3793
0
      if (!local_undefweak)
3794
0
  {
3795
0
    if (htab->plt.has_plt0)
3796
0
      bfd_put_32 (info->output_bfd,
3797
0
      (plt->output_section->vma
3798
0
       + plt->output_offset
3799
0
       + h->plt.offset
3800
0
       + htab->lazy_plt->plt_lazy_offset),
3801
0
      gotplt->contents + got_offset);
3802
3803
    /* Fill in the entry in the .rel.plt section.  */
3804
0
    rel.r_offset = (gotplt->output_section->vma
3805
0
        + gotplt->output_offset
3806
0
        + got_offset);
3807
0
    if (PLT_LOCAL_IFUNC_P (info, h))
3808
0
      {
3809
0
        info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3810
0
              h->root.root.string,
3811
0
              h->root.u.def.section->owner);
3812
3813
        /* If an STT_GNU_IFUNC symbol is locally defined, generate
3814
     R_386_IRELATIVE instead of R_386_JUMP_SLOT.  Store addend
3815
     in the .got.plt section.  */
3816
0
        bfd_put_32 (info->output_bfd,
3817
0
        (h->root.u.def.value
3818
0
         + h->root.u.def.section->output_section->vma
3819
0
         + h->root.u.def.section->output_offset),
3820
0
        gotplt->contents + got_offset);
3821
0
        rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3822
3823
0
        if (htab->params->report_relative_reloc)
3824
0
    _bfd_x86_elf_link_report_relative_reloc
3825
0
      (info, relplt, h, sym, "R_386_IRELATIVE", &rel);
3826
3827
        /* R_386_IRELATIVE comes last.  */
3828
0
        plt_index = htab->next_irelative_index--;
3829
0
      }
3830
0
    else
3831
0
      {
3832
0
        rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3833
0
        plt_index = htab->next_jump_slot_index++;
3834
0
      }
3835
3836
0
    loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
3837
0
    bfd_elf32_swap_reloc_out (info->output_bfd, &rel, loc);
3838
3839
    /* Don't fill the second and third slots in PLT entry for
3840
       static executables nor without PLT0.  */
3841
0
    if (plt == htab->elf.splt && htab->plt.has_plt0)
3842
0
      {
3843
0
        bfd_put_32 (info->output_bfd,
3844
0
        plt_index * sizeof (Elf32_External_Rel),
3845
0
        plt->contents + h->plt.offset
3846
0
        + htab->lazy_plt->plt_reloc_offset);
3847
0
        bfd_put_32 (info->output_bfd,
3848
0
        - (h->plt.offset
3849
0
           + htab->lazy_plt->plt_plt_offset + 4),
3850
0
        (plt->contents + h->plt.offset
3851
0
         + htab->lazy_plt->plt_plt_offset));
3852
0
      }
3853
0
  }
3854
0
    }
3855
0
  else if (eh->plt_got.offset != (bfd_vma) -1)
3856
0
    {
3857
0
      bfd_vma got_offset, plt_offset;
3858
0
      asection *plt, *got, *gotplt;
3859
0
      const bfd_byte *got_plt_entry;
3860
3861
      /* Set the entry in the GOT procedure linkage table.  */
3862
0
      plt = htab->plt_got;
3863
0
      got = htab->elf.sgot;
3864
0
      gotplt = htab->elf.sgotplt;
3865
0
      got_offset = h->got.offset;
3866
3867
0
      if (got_offset == (bfd_vma) -1
3868
0
    || plt == NULL
3869
0
    || got == NULL
3870
0
    || gotplt == NULL)
3871
0
  abort ();
3872
3873
      /* Fill in the entry in the GOT procedure linkage table.  */
3874
0
      if (! bfd_link_pic (info))
3875
0
  {
3876
0
    got_plt_entry = htab->non_lazy_plt->plt_entry;
3877
0
    got_offset += got->output_section->vma + got->output_offset;
3878
0
  }
3879
0
      else
3880
0
  {
3881
0
    got_plt_entry = htab->non_lazy_plt->pic_plt_entry;
3882
0
    got_offset += (got->output_section->vma
3883
0
       + got->output_offset
3884
0
       - gotplt->output_section->vma
3885
0
       - gotplt->output_offset);
3886
0
  }
3887
3888
0
      plt_offset = eh->plt_got.offset;
3889
0
      memcpy (plt->contents + plt_offset, got_plt_entry,
3890
0
        htab->non_lazy_plt->plt_entry_size);
3891
0
      bfd_put_32 (info->output_bfd, got_offset,
3892
0
      (plt->contents + plt_offset
3893
0
       + htab->non_lazy_plt->plt_got_offset));
3894
0
    }
3895
3896
0
  if (!local_undefweak
3897
0
      && !h->def_regular
3898
0
      && (h->plt.offset != (bfd_vma) -1
3899
0
    || eh->plt_got.offset != (bfd_vma) -1))
3900
0
    {
3901
      /* Mark the symbol as undefined, rather than as defined in
3902
   the .plt section.  Leave the value if there were any
3903
   relocations where pointer equality matters (this is a clue
3904
   for the dynamic linker, to make function pointer
3905
   comparisons work between an application and shared
3906
   library), otherwise set it to zero.  If a function is only
3907
   called from a binary, there is no need to slow down
3908
   shared libraries because of that.  */
3909
0
      sym->st_shndx = SHN_UNDEF;
3910
0
      if (!h->pointer_equality_needed)
3911
0
  sym->st_value = 0;
3912
0
    }
3913
3914
0
  _bfd_x86_elf_link_fixup_ifunc_symbol (info, htab, h, sym);
3915
3916
  /* Don't generate dynamic GOT relocation against undefined weak
3917
     symbol in executable.  */
3918
0
  if (h->got.offset != (bfd_vma) -1
3919
0
      && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry(h)->tls_type)
3920
0
      && (elf_x86_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
3921
0
      && !local_undefweak)
3922
0
    {
3923
0
      Elf_Internal_Rela rel;
3924
0
      asection *relgot = htab->elf.srelgot;
3925
0
      const char *relative_reloc_name = NULL;
3926
0
      bool generate_dynamic_reloc = true;
3927
3928
      /* This symbol has an entry in the global offset table.  Set it
3929
   up.  */
3930
3931
0
      if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3932
0
  abort ();
3933
3934
0
      rel.r_offset = (htab->elf.sgot->output_section->vma
3935
0
          + htab->elf.sgot->output_offset
3936
0
          + (h->got.offset & ~(bfd_vma) 1));
3937
3938
      /* If this is a static link, or it is a -Bsymbolic link and the
3939
   symbol is defined locally or was forced to be local because
3940
   of a version file, we just want to emit a RELATIVE reloc.
3941
   The entry in the global offset table will already have been
3942
   initialized in the relocate_section function.  */
3943
0
      if (h->def_regular
3944
0
    && h->type == STT_GNU_IFUNC)
3945
0
  {
3946
0
    if (h->plt.offset == (bfd_vma) -1)
3947
0
      {
3948
        /* STT_GNU_IFUNC is referenced without PLT.  */
3949
0
        if (htab->elf.splt == NULL)
3950
0
    {
3951
      /* use .rel[a].iplt section to store .got relocations
3952
         in static executable.  */
3953
0
      relgot = htab->elf.irelplt;
3954
0
    }
3955
0
        if (SYMBOL_REFERENCES_LOCAL_P (info, h))
3956
0
    {
3957
0
      info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3958
0
            h->root.root.string,
3959
0
            h->root.u.def.section->owner);
3960
3961
0
      bfd_put_32 (info->output_bfd,
3962
0
            (h->root.u.def.value
3963
0
             + h->root.u.def.section->output_section->vma
3964
0
             + h->root.u.def.section->output_offset),
3965
0
            htab->elf.sgot->contents + h->got.offset);
3966
0
      rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3967
0
      relative_reloc_name = "R_386_IRELATIVE";
3968
0
    }
3969
0
        else
3970
0
    goto do_glob_dat;
3971
0
      }
3972
0
    else if (bfd_link_pic (info))
3973
0
      {
3974
        /* Generate R_386_GLOB_DAT.  */
3975
0
        goto do_glob_dat;
3976
0
      }
3977
0
    else
3978
0
      {
3979
0
        asection *plt;
3980
0
        bfd_vma plt_offset;
3981
3982
0
        if (!h->pointer_equality_needed)
3983
0
    abort ();
3984
3985
        /* For non-shared object, we can't use .got.plt, which
3986
     contains the real function addres if we need pointer
3987
     equality.  We load the GOT entry with the PLT entry.  */
3988
0
        if (htab->plt_second != NULL)
3989
0
    {
3990
0
      plt = htab->plt_second;
3991
0
      plt_offset = eh->plt_second.offset;
3992
0
    }
3993
0
        else
3994
0
    {
3995
0
      plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3996
0
      plt_offset = h->plt.offset;
3997
0
    }
3998
0
        bfd_put_32 (info->output_bfd,
3999
0
        (plt->output_section->vma
4000
0
         + plt->output_offset + plt_offset),
4001
0
        htab->elf.sgot->contents + h->got.offset);
4002
0
        return true;
4003
0
      }
4004
0
  }
4005
0
      else if (bfd_link_pic (info)
4006
0
         && SYMBOL_REFERENCES_LOCAL_P (info, h))
4007
0
  {
4008
0
    BFD_ASSERT((h->got.offset & 1) != 0);
4009
0
    if (info->enable_dt_relr)
4010
0
      generate_dynamic_reloc = false;
4011
0
    else
4012
0
      {
4013
0
        rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4014
0
        relative_reloc_name = "R_386_RELATIVE";
4015
0
      }
4016
0
  }
4017
0
      else
4018
0
  {
4019
0
    BFD_ASSERT((h->got.offset & 1) == 0);
4020
0
  do_glob_dat:
4021
0
    bfd_put_32 (info->output_bfd, 0,
4022
0
          htab->elf.sgot->contents + h->got.offset);
4023
0
    rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
4024
0
  }
4025
4026
0
      if (generate_dynamic_reloc)
4027
0
  {
4028
0
    if (relative_reloc_name != NULL
4029
0
        && htab->params->report_relative_reloc)
4030
0
      _bfd_x86_elf_link_report_relative_reloc
4031
0
        (info, relgot, h, sym, relative_reloc_name, &rel);
4032
4033
0
    _bfd_elf_append_rel (info->output_bfd, relgot, &rel);
4034
0
  }
4035
0
    }
4036
4037
0
  if (h->needs_copy)
4038
0
    {
4039
0
      Elf_Internal_Rela rel;
4040
0
      asection *s;
4041
4042
      /* This symbol needs a copy reloc.  Set it up.  */
4043
0
      VERIFY_COPY_RELOC (h, htab)
4044
4045
0
      rel.r_offset = (h->root.u.def.value
4046
0
          + h->root.u.def.section->output_section->vma
4047
0
          + h->root.u.def.section->output_offset);
4048
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
4049
0
      if (h->root.u.def.section == htab->elf.sdynrelro)
4050
0
  s = htab->elf.sreldynrelro;
4051
0
      else
4052
0
  s = htab->elf.srelbss;
4053
0
      _bfd_elf_append_rel (info->output_bfd, s, &rel);
4054
0
    }
4055
4056
0
  return true;
4057
0
}
4058
4059
/* Finish up local dynamic symbol handling.  We set the contents of
4060
   various dynamic sections here.  */
4061
4062
static int
4063
elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
4064
0
{
4065
0
  struct elf_link_hash_entry *h
4066
0
    = (struct elf_link_hash_entry *) *slot;
4067
0
  struct bfd_link_info *info
4068
0
    = (struct bfd_link_info *) inf;
4069
4070
0
  return elf_i386_finish_dynamic_symbol (info, h, NULL);
4071
0
}
4072
4073
/* Finish up undefined weak symbol handling in PIE.  Fill its PLT entry
4074
   here since undefined weak symbol may not be dynamic and may not be
4075
   called for elf_i386_finish_dynamic_symbol.  */
4076
4077
static bool
4078
elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
4079
              void *inf)
4080
0
{
4081
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
4082
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
4083
4084
0
  if (h->root.type != bfd_link_hash_undefweak
4085
0
      || h->dynindx != -1)
4086
0
    return true;
4087
4088
0
  return elf_i386_finish_dynamic_symbol (info, h, NULL);
4089
0
}
4090
4091
/* Used to decide how to sort relocs in an optimal manner for the
4092
   dynamic linker, before writing them out.  */
4093
4094
static enum elf_reloc_type_class
4095
elf_i386_reloc_type_class (const struct bfd_link_info *info,
4096
         const asection *rel_sec ATTRIBUTE_UNUSED,
4097
         const Elf_Internal_Rela *rela)
4098
0
{
4099
0
  bfd *abfd = info->output_bfd;
4100
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
4101
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4102
4103
0
  if (htab->dynsym != NULL
4104
0
      && htab->dynsym->contents != NULL)
4105
0
    {
4106
      /* Check relocation against STT_GNU_IFUNC symbol if there are
4107
   dynamic symbols.  */
4108
0
      unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
4109
0
      if (r_symndx != STN_UNDEF)
4110
0
  {
4111
0
    Elf_Internal_Sym sym;
4112
0
    if (!bed->s->swap_symbol_in (abfd,
4113
0
               (htab->dynsym->contents
4114
0
          + r_symndx * sizeof (Elf32_External_Sym)),
4115
0
               0, &sym))
4116
0
      abort ();
4117
4118
0
    if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
4119
0
      return reloc_class_ifunc;
4120
0
  }
4121
0
    }
4122
4123
0
  switch (ELF32_R_TYPE (rela->r_info))
4124
0
    {
4125
0
    case R_386_IRELATIVE:
4126
0
      return reloc_class_ifunc;
4127
0
    case R_386_RELATIVE:
4128
0
      return reloc_class_relative;
4129
0
    case R_386_JUMP_SLOT:
4130
0
      return reloc_class_plt;
4131
0
    case R_386_COPY:
4132
0
      return reloc_class_copy;
4133
0
    default:
4134
0
      return reloc_class_normal;
4135
0
    }
4136
0
}
4137
4138
/* Finish up the dynamic sections.  */
4139
4140
static bool
4141
elf_i386_finish_dynamic_sections (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 (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 (info->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 (info->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 (info->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 (info->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 (info->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 (info->output_bfd, &rel, p);
4233
0
          p += sizeof (Elf32_External_Rel);
4234
4235
0
          bfd_elf32_swap_reloc_in (info->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 (info->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
  (struct bfd_link_info *info,
4263
   void *flaginfo ATTRIBUTE_UNUSED,
4264
   int (*func) (void *, const char *,
4265
    Elf_Internal_Sym *,
4266
    asection *,
4267
    struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4268
0
{
4269
0
  struct elf_x86_link_hash_table *htab
4270
0
    = elf_x86_hash_table (info, I386_ELF_DATA);
4271
0
  if (htab == NULL)
4272
0
    return false;
4273
4274
  /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols if
4275
     needed.  */
4276
0
  if (htab->has_loc_hash_table)
4277
0
    htab_traverse (htab->loc_hash_table,
4278
0
       elf_i386_finish_local_dynamic_symbol,
4279
0
       info);
4280
4281
0
  return true;
4282
0
}
4283
4284
/* Similar to _bfd_elf_get_synthetic_symtab.  Support PLTs with all
4285
   dynamic relocations.   */
4286
4287
static long
4288
elf_i386_get_synthetic_symtab (bfd *abfd,
4289
             long symcount ATTRIBUTE_UNUSED,
4290
             asymbol **syms ATTRIBUTE_UNUSED,
4291
             long dynsymcount,
4292
             asymbol **dynsyms,
4293
             asymbol **ret)
4294
98
{
4295
98
  long count, i, n;
4296
98
  int j;
4297
98
  bfd_byte *plt_contents;
4298
98
  long relsize;
4299
98
  const struct elf_x86_lazy_plt_layout *lazy_plt;
4300
98
  const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4301
98
  const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4302
98
  const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
4303
98
  asection *plt;
4304
98
  bfd_vma got_addr;
4305
98
  enum elf_x86_plt_type plt_type;
4306
98
  struct elf_x86_plt plts[] =
4307
98
    {
4308
98
      { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4309
98
      { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
4310
98
      { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4311
98
      { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
4312
98
    };
4313
4314
98
  *ret = NULL;
4315
4316
98
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4317
84
    return 0;
4318
4319
14
  if (dynsymcount <= 0)
4320
7
    return 0;
4321
4322
7
  relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4323
7
  if (relsize <= 0)
4324
0
    return -1;
4325
4326
7
  non_lazy_plt = NULL;
4327
  /* Silence GCC 6.  */
4328
7
  lazy_plt = NULL;
4329
7
  non_lazy_ibt_plt = NULL;
4330
7
  lazy_ibt_plt = NULL;
4331
7
  switch (get_elf_backend_data (abfd)->target_os)
4332
7
    {
4333
7
    case is_normal:
4334
7
    case is_solaris:
4335
7
      non_lazy_plt = &elf_i386_non_lazy_plt;
4336
7
      lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4337
7
      non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4338
      /* Fall through */
4339
7
    case is_vxworks:
4340
7
      lazy_plt = &elf_i386_lazy_plt;
4341
7
      break;
4342
0
    default:
4343
0
      abort ();
4344
7
    }
4345
4346
7
  got_addr = 0;
4347
4348
7
  count = 0;
4349
28
  for (j = 0; plts[j].name != NULL; j++)
4350
21
    {
4351
21
      plt = bfd_get_section_by_name (abfd, plts[j].name);
4352
21
      if (plt == NULL || plt->size == 0)
4353
15
  continue;
4354
4355
      /* Get the PLT section contents.  */
4356
6
      plt_contents = (bfd_byte *) bfd_malloc (plt->size);
4357
6
      if (plt_contents == NULL)
4358
0
  break;
4359
6
      if (!bfd_get_section_contents (abfd, (asection *) plt,
4360
6
             plt_contents, 0, plt->size))
4361
0
  {
4362
0
    free (plt_contents);
4363
0
    break;
4364
0
  }
4365
4366
      /* Check what kind of PLT it is.  */
4367
6
      plt_type = plt_unknown;
4368
6
      if (plts[j].type == plt_unknown
4369
6
    && (plt->size >= (lazy_plt->plt0_entry_size
4370
6
          + lazy_plt->plt_entry_size)))
4371
6
  {
4372
    /* Match lazy PLT first.  */
4373
6
    if (memcmp (plt_contents, lazy_plt->plt0_entry,
4374
6
          lazy_plt->plt0_got1_offset) == 0)
4375
2
      {
4376
        /* The fist entry in the lazy IBT PLT is the same as the
4377
     normal lazy PLT.  */
4378
2
        if (lazy_ibt_plt != NULL
4379
2
      && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4380
2
            lazy_ibt_plt->plt_entry,
4381
2
            lazy_ibt_plt->plt_reloc_offset) == 0))
4382
0
    plt_type = plt_lazy | plt_second;
4383
2
        else
4384
2
    plt_type = plt_lazy;
4385
2
      }
4386
4
    else if (memcmp (plt_contents, lazy_plt->pic_plt0_entry,
4387
4
         lazy_plt->plt0_got1_offset) == 0)
4388
4
      {
4389
        /* The fist entry in the PIC lazy IBT PLT is the same as
4390
     the normal PIC lazy PLT.  */
4391
4
        if (lazy_ibt_plt != NULL
4392
4
      && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4393
4
            lazy_ibt_plt->pic_plt_entry,
4394
4
            lazy_ibt_plt->plt_reloc_offset) == 0))
4395
0
    plt_type = plt_lazy | plt_pic | plt_second;
4396
4
        else
4397
4
    plt_type = plt_lazy | plt_pic;
4398
4
      }
4399
6
  }
4400
4401
6
      if (non_lazy_plt != NULL
4402
6
    && (plt_type == plt_unknown || plt_type == plt_non_lazy)
4403
0
    && plt->size >= non_lazy_plt->plt_entry_size)
4404
0
  {
4405
    /* Match non-lazy PLT.  */
4406
0
    if (memcmp (plt_contents, non_lazy_plt->plt_entry,
4407
0
          non_lazy_plt->plt_got_offset) == 0)
4408
0
      plt_type = plt_non_lazy;
4409
0
    else if (memcmp (plt_contents, non_lazy_plt->pic_plt_entry,
4410
0
         non_lazy_plt->plt_got_offset) == 0)
4411
0
      plt_type = plt_pic;
4412
0
  }
4413
4414
6
      if ((non_lazy_ibt_plt != NULL)
4415
6
    && (plt_type == plt_unknown || plt_type == plt_second)
4416
0
    && plt->size >= non_lazy_ibt_plt->plt_entry_size)
4417
0
  {
4418
0
    if (memcmp (plt_contents,
4419
0
          non_lazy_ibt_plt->plt_entry,
4420
0
          non_lazy_ibt_plt->plt_got_offset) == 0)
4421
0
      {
4422
        /* Match IBT PLT.  */
4423
0
        plt_type = plt_second;
4424
0
        non_lazy_plt = non_lazy_ibt_plt;
4425
0
      }
4426
0
    else if (memcmp (plt_contents,
4427
0
         non_lazy_ibt_plt->pic_plt_entry,
4428
0
         non_lazy_ibt_plt->plt_got_offset) == 0)
4429
0
      {
4430
        /* Match PIC IBT PLT.  */
4431
0
        plt_type = plt_second | plt_pic;
4432
0
        non_lazy_plt = non_lazy_ibt_plt;
4433
0
      }
4434
0
  }
4435
4436
6
      if (plt_type == plt_unknown)
4437
0
  {
4438
0
    free (plt_contents);
4439
0
    continue;
4440
0
  }
4441
4442
6
      plts[j].sec = plt;
4443
6
      plts[j].type = plt_type;
4444
4445
6
      if ((plt_type & plt_lazy))
4446
6
  {
4447
6
    plts[j].plt_got_offset = lazy_plt->plt_got_offset;
4448
6
    plts[j].plt_entry_size = lazy_plt->plt_entry_size;
4449
    /* Skip PLT0 in lazy PLT.  */
4450
6
    i = 1;
4451
6
  }
4452
0
      else
4453
0
  {
4454
0
    plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
4455
0
    plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
4456
0
    i = 0;
4457
0
  }
4458
4459
      /* Skip lazy PLT when the second PLT is used.  */
4460
6
      if ((plt_type & (plt_lazy | plt_second))
4461
6
    == (plt_lazy | plt_second))
4462
0
  plts[j].count = 0;
4463
6
      else
4464
6
  {
4465
6
    n = plt->size / plts[j].plt_entry_size;
4466
6
    plts[j].count = n;
4467
6
    count += n - i;
4468
6
  }
4469
4470
6
      plts[j].contents = plt_contents;
4471
4472
      /* The _GLOBAL_OFFSET_TABLE_ address is needed.  */
4473
6
      if ((plt_type & plt_pic))
4474
4
  got_addr = (bfd_vma) -1;
4475
6
    }
4476
4477
7
  return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
4478
7
              got_addr, plts, dynsyms,
4479
7
              ret);
4480
7
}
4481
4482
/* Set up i386 GNU properties.  Return the first relocatable ELF input
4483
   with GNU properties if found.  Otherwise, return NULL.  */
4484
4485
static bfd *
4486
elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
4487
0
{
4488
0
  struct elf_x86_init_table init_table;
4489
4490
0
  switch (get_elf_backend_data (info->output_bfd)->target_os)
4491
0
    {
4492
0
    case is_normal:
4493
0
    case is_solaris:
4494
0
      init_table.plt0_pad_byte = 0x0;
4495
0
      init_table.lazy_plt = &elf_i386_lazy_plt;
4496
0
      init_table.non_lazy_plt = &elf_i386_non_lazy_plt;
4497
0
      init_table.lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4498
0
      init_table.non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4499
0
      break;
4500
0
    case is_vxworks:
4501
0
      init_table.plt0_pad_byte = 0x90;
4502
0
      init_table.lazy_plt = &elf_i386_lazy_plt;
4503
0
      init_table.non_lazy_plt = NULL;
4504
0
      init_table.lazy_ibt_plt = NULL;
4505
0
      init_table.non_lazy_ibt_plt = NULL;
4506
0
      break;
4507
0
    default:
4508
0
      abort ();
4509
0
    }
4510
4511
0
  init_table.r_info = elf32_r_info;
4512
0
  init_table.r_sym = elf32_r_sym;
4513
4514
0
  return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
4515
0
}
4516
4517
static void
4518
elf_i386_add_glibc_version_dependency
4519
  (struct elf_find_verdep_info *rinfo)
4520
0
{
4521
0
  int i = 0;
4522
0
  const char *version[4] = { NULL, NULL, NULL, NULL };
4523
0
  bool auto_version[4] = { false, false, false, false };
4524
0
  struct elf_x86_link_hash_table *htab;
4525
4526
0
  if (rinfo->info->enable_dt_relr)
4527
0
    {
4528
0
      version[i] = "GLIBC_ABI_DT_RELR";
4529
0
      i++;
4530
0
    }
4531
4532
0
  htab = elf_x86_hash_table (rinfo->info, I386_ELF_DATA);
4533
0
  if (htab != NULL)
4534
0
    {
4535
0
      if (htab->params->gnu2_tls_version_tag && htab->has_tls_desc_call)
4536
0
  {
4537
0
    version[i] = "GLIBC_ABI_GNU2_TLS";
4538
    /* 2 == auto, enable if libc.so defines the GLIBC_ABI_GNU2_TLS
4539
       version.  */
4540
0
    if (htab->params->gnu2_tls_version_tag == 2)
4541
0
      auto_version[i] = true;
4542
0
    i++;
4543
0
  }
4544
0
      if (htab->params->gnu_tls_version_tag
4545
0
    && htab->has_tls_get_addr_call)
4546
0
  {
4547
0
    version[i] = "GLIBC_ABI_GNU_TLS";
4548
    /* 2 == auto, enable if libc.so defines the GLIBC_ABI_GNU_TLS
4549
       version.  */
4550
0
    if (htab->params->gnu_tls_version_tag == 2)
4551
0
      auto_version[i] = true;
4552
0
    i++;
4553
0
  }
4554
0
    }
4555
4556
0
  if (i != 0)
4557
0
    _bfd_elf_link_add_glibc_version_dependency (rinfo, version,
4558
0
            auto_version);
4559
0
}
4560
4561
#define TARGET_LITTLE_SYM   i386_elf32_vec
4562
#define TARGET_LITTLE_NAME    "elf32-i386"
4563
#define ELF_ARCH      bfd_arch_i386
4564
#define ELF_TARGET_ID     I386_ELF_DATA
4565
#define ELF_MACHINE_CODE    EM_386
4566
#define ELF_MAXPAGESIZE     0x1000
4567
#define ELF_OSABI     ELFOSABI_GNU
4568
4569
#define elf_backend_can_gc_sections 1
4570
#define elf_backend_can_refcount  1
4571
#define elf_backend_want_got_plt  1
4572
#define elf_backend_plt_readonly  1
4573
#define elf_backend_want_plt_sym  0
4574
#define elf_backend_got_header_size 12
4575
#define elf_backend_plt_alignment 4
4576
#define elf_backend_dtrel_excludes_plt  1
4577
#define elf_backend_caches_rawsize  1
4578
#define elf_backend_want_dynrelro 1
4579
4580
/* Support RELA for objdump of prelink objects.  */
4581
#define elf_info_to_howto         elf_i386_info_to_howto_rel
4582
#define elf_info_to_howto_rel         elf_i386_info_to_howto_rel
4583
4584
#define bfd_elf32_bfd_is_local_label_name     elf_i386_is_local_label_name
4585
#define bfd_elf32_bfd_reloc_type_lookup       elf_i386_reloc_type_lookup
4586
#define bfd_elf32_bfd_reloc_name_lookup       elf_i386_reloc_name_lookup
4587
#define bfd_elf32_get_synthetic_symtab        elf_i386_get_synthetic_symtab
4588
4589
#define elf_backend_relocs_compatible       _bfd_elf_relocs_compatible
4590
#define elf_backend_early_size_sections       elf_i386_early_size_sections
4591
#define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
4592
#define elf_backend_fake_sections       elf_i386_fake_sections
4593
#define elf_backend_finish_dynamic_sections   elf_i386_finish_dynamic_sections
4594
#define elf_backend_finish_dynamic_symbol     elf_i386_finish_dynamic_symbol
4595
#define elf_backend_output_arch_local_syms     elf_i386_output_arch_local_syms
4596
#define elf_backend_grok_prstatus       elf_i386_grok_prstatus
4597
#define elf_backend_grok_psinfo         elf_i386_grok_psinfo
4598
#define elf_backend_reloc_type_class        elf_i386_reloc_type_class
4599
#define elf_backend_relocate_section        elf_i386_relocate_section
4600
#define elf_backend_setup_gnu_properties      elf_i386_link_setup_gnu_properties
4601
#define elf_backend_hide_symbol         _bfd_x86_elf_hide_symbol
4602
#define elf_backend_add_glibc_version_dependency \
4603
  elf_i386_add_glibc_version_dependency
4604
4605
#define elf_backend_linux_prpsinfo32_ugid16 true
4606
4607
#define elf32_bed           elf32_i386_bed
4608
4609
#include "elf32-target.h"
4610
4611
#undef elf_backend_add_glibc_version_dependency
4612
4613
/* FreeBSD support.  */
4614
4615
#undef  TARGET_LITTLE_SYM
4616
#define TARGET_LITTLE_SYM   i386_elf32_fbsd_vec
4617
#undef  TARGET_LITTLE_NAME
4618
#define TARGET_LITTLE_NAME    "elf32-i386-freebsd"
4619
#undef  ELF_OSABI
4620
#define ELF_OSABI     ELFOSABI_FREEBSD
4621
#undef  ELF_OSABI_EXACT
4622
#define ELF_OSABI_EXACT     1
4623
4624
/* The kernel recognizes executables as valid only if they carry a
4625
   "FreeBSD" label in the ELF header.  So we put this label on all
4626
   executables and (for simplicity) also all other object files.  */
4627
4628
static bool
4629
elf_i386_fbsd_init_file_header (bfd *abfd, struct bfd_link_info *info)
4630
0
{
4631
0
  if (!_bfd_elf_init_file_header (abfd, info))
4632
0
    return false;
4633
4634
#ifdef OLD_FREEBSD_ABI_LABEL
4635
  {
4636
    /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard.  */
4637
    Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4638
    memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4639
  }
4640
#endif
4641
0
  return true;
4642
0
}
4643
4644
#undef  elf_backend_init_file_header
4645
#define elf_backend_init_file_header  elf_i386_fbsd_init_file_header
4646
#undef  elf32_bed
4647
#define elf32_bed       elf32_i386_fbsd_bed
4648
4649
#include "elf32-target.h"
4650
4651
#undef elf_backend_init_file_header
4652
4653
#ifdef OBJ_MAYBE_ELF_SOLARIS2
4654
4655
/* Solaris 2.  */
4656
4657
#undef  TARGET_LITTLE_SYM
4658
#define TARGET_LITTLE_SYM   i386_elf32_sol2_vec
4659
#undef  TARGET_LITTLE_NAME
4660
#define TARGET_LITTLE_NAME    "elf32-i386-sol2"
4661
4662
#undef  ELF_MAXPAGESIZE
4663
#define ELF_MAXPAGESIZE     0x10000
4664
4665
#undef  ELF_TARGET_OS
4666
#define ELF_TARGET_OS     is_solaris
4667
4668
#undef ELF_OSABI
4669
#define ELF_OSABI     ELFOSABI_SOLARIS
4670
#undef ELF_OSABI_EXACT
4671
4672
#undef  elf32_bed
4673
#define elf32_bed     elf32_i386_sol2_bed
4674
4675
/* The 32-bit static TLS arena size is rounded to the nearest 8-byte
4676
   boundary.  */
4677
#undef  elf_backend_static_tls_alignment
4678
#define elf_backend_static_tls_alignment 8
4679
4680
/* The Solaris 2 ABI requires a plt symbol on all platforms.
4681
4682
   Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4683
   File, p.63.  */
4684
#undef  elf_backend_want_plt_sym
4685
#define elf_backend_want_plt_sym  1
4686
4687
#undef  elf_backend_add_symbol_hook
4688
#define elf_backend_add_symbol_hook     elf_solaris2_add_symbol_hook
4689
4690
#include "elf32-target.h"
4691
4692
#endif /* OBJ_MAYBE_ELF_SOLARIS2 */
4693
4694
/* Intel MCU support.  */
4695
4696
static bool
4697
elf32_iamcu_elf_object_p (bfd *abfd)
4698
90
{
4699
  /* Set the right machine number for an IAMCU elf32 file.  */
4700
90
  bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
4701
  return true;
4702
90
}
4703
4704
#undef  TARGET_LITTLE_SYM
4705
#define TARGET_LITTLE_SYM   iamcu_elf32_vec
4706
#undef  TARGET_LITTLE_NAME
4707
#define TARGET_LITTLE_NAME    "elf32-iamcu"
4708
#undef  ELF_ARCH
4709
#define ELF_ARCH      bfd_arch_iamcu
4710
4711
#undef  ELF_MACHINE_CODE
4712
#define ELF_MACHINE_CODE    EM_IAMCU
4713
4714
#undef  ELF_MAXPAGESIZE
4715
#define ELF_MAXPAGESIZE     0x1000
4716
4717
#undef  ELF_TARGET_OS
4718
#undef  ELF_OSABI
4719
#define ELF_OSABI     ELFOSABI_GNU
4720
#undef  ELF_OSABI_EXACT
4721
4722
#undef  elf32_bed
4723
#define elf32_bed     elf32_iamcu_bed
4724
4725
#undef  elf_backend_object_p
4726
#define elf_backend_object_p    elf32_iamcu_elf_object_p
4727
4728
#undef  elf_backend_static_tls_alignment
4729
4730
#undef  elf_backend_want_plt_sym
4731
#define elf_backend_want_plt_sym  0
4732
#undef  elf_backend_add_symbol_hook
4733
4734
#include "elf32-target.h"
4735
4736
/* Restore defaults.  */
4737
#undef  ELF_ARCH
4738
#define ELF_ARCH      bfd_arch_i386
4739
#undef  ELF_MACHINE_CODE
4740
#define ELF_MACHINE_CODE    EM_386
4741
#undef  elf_backend_object_p
4742
4743
#ifdef OBJ_MAYBE_ELF_VXWORKS
4744
4745
/* VxWorks support.  */
4746
4747
#undef  TARGET_LITTLE_SYM
4748
#define TARGET_LITTLE_SYM   i386_elf32_vxworks_vec
4749
#undef  TARGET_LITTLE_NAME
4750
#define TARGET_LITTLE_NAME    "elf32-i386-vxworks"
4751
#undef  ELF_MAXPAGESIZE
4752
#define ELF_MAXPAGESIZE     0x1000
4753
#undef  elf_backend_plt_alignment
4754
#define elf_backend_plt_alignment 4
4755
4756
#undef  ELF_TARGET_OS
4757
#define ELF_TARGET_OS   is_vxworks
4758
#undef  ELF_OSABI
4759
#undef  ELF_OSABI_EXACT
4760
4761
#undef elf_backend_relocs_compatible
4762
#undef elf_backend_add_symbol_hook
4763
#define elf_backend_add_symbol_hook \
4764
  elf_vxworks_add_symbol_hook
4765
#undef elf_backend_link_output_symbol_hook
4766
#define elf_backend_link_output_symbol_hook \
4767
  elf_vxworks_link_output_symbol_hook
4768
#undef elf_backend_emit_relocs
4769
#define elf_backend_emit_relocs     elf_vxworks_emit_relocs
4770
#undef elf_backend_final_write_processing
4771
#define elf_backend_final_write_processing \
4772
  elf_vxworks_final_write_processing
4773
#undef elf_backend_static_tls_alignment
4774
4775
/* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
4776
   define it.  */
4777
#undef elf_backend_want_plt_sym
4778
#define elf_backend_want_plt_sym  1
4779
4780
#undef  elf32_bed
4781
#define elf32_bed       elf32_i386_vxworks_bed
4782
4783
#include "elf32-target.h"
4784
4785
#endif /* OBJ_MAYBE_ELF_VXWORKS */