OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-6.8/] [bfd/] [elf32-i386.c] - Blame information for rev 225

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.