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