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