Coverage Report

Created: 2026-09-14 08:07

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