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
a7b16ceb
L
2558 if (htab->elf.sgotplt)
2559 {
2560 /* Don't allocate .got.plt section if there are no GOT nor PLT
2561 entries. */
2562 if ((htab->elf.sgotplt->size
2563 == get_elf_backend_data (output_bfd)->got_header_size)
2564 && (htab->elf.splt == NULL
2565 || htab->elf.splt->size == 0)
2566 && (htab->elf.sgot == NULL
2567 || htab->elf.sgot->size == 0)
2568 && (htab->elf.iplt == NULL
2569 || htab->elf.iplt->size == 0)
2570 && (htab->elf.igotplt == NULL
2571 || htab->elf.igotplt->size == 0))
2572 htab->elf.sgotplt->size = 0;
2573 }
2574
5a15f56f
AM
2575 /* We now have determined the sizes of the various dynamic sections.
2576 Allocate memory for them. */
b34976b6 2577 relocs = FALSE;
252b5132
RH
2578 for (s = dynobj->sections; s != NULL; s = s->next)
2579 {
eac338cf
PB
2580 bfd_boolean strip_section = TRUE;
2581
252b5132
RH
2582 if ((s->flags & SEC_LINKER_CREATED) == 0)
2583 continue;
2584
6de2ae4a
L
2585 if (s == htab->elf.splt
2586 || s == htab->elf.sgot
2587 || s == htab->elf.sgotplt
2588 || s == htab->elf.iplt
2589 || s == htab->elf.igotplt
75ff4589 2590 || s == htab->sdynbss)
252b5132 2591 {
6725bdbf
AM
2592 /* Strip this section if we don't need it; see the
2593 comment below. */
eac338cf
PB
2594 /* We'd like to strip these sections if they aren't needed, but if
2595 we've exported dynamic symbols from them we must leave them.
2596 It's too late to tell BFD to get rid of the symbols. */
2597
7325306f 2598 if (htab->elf.hplt != NULL)
eac338cf 2599 strip_section = FALSE;
252b5132 2600 }
0112cd26 2601 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
252b5132 2602 {
6de2ae4a
L
2603 if (s->size != 0
2604 && s != htab->elf.srelplt
2605 && s != htab->srelplt2)
b34976b6 2606 relocs = TRUE;
252b5132 2607
0ac8d2ca
AM
2608 /* We use the reloc_count field as a counter if we need
2609 to copy relocs into the output file. */
5ae0bfb6 2610 s->reloc_count = 0;
252b5132 2611 }
6725bdbf 2612 else
252b5132
RH
2613 {
2614 /* It's not one of our sections, so don't allocate space. */
2615 continue;
2616 }
2617
c456f082 2618 if (s->size == 0)
252b5132 2619 {
0ac8d2ca
AM
2620 /* If we don't need this section, strip it from the
2621 output file. This is mostly to handle .rel.bss and
2622 .rel.plt. We must create both sections in
2623 create_dynamic_sections, because they must be created
2624 before the linker maps input sections to output
2625 sections. The linker does that before
2626 adjust_dynamic_symbol is called, and it is that
2627 function which decides whether anything needs to go
2628 into these sections. */
c456f082
AM
2629 if (strip_section)
2630 s->flags |= SEC_EXCLUDE;
252b5132
RH
2631 continue;
2632 }
2633
c456f082
AM
2634 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2635 continue;
2636
f69da49f
AM
2637 /* Allocate memory for the section contents. We use bfd_zalloc
2638 here in case unused entries are not reclaimed before the
2639 section's contents are written out. This should not happen,
2640 but this way if it does, we get a R_386_NONE reloc instead
2641 of garbage. */
a50b1753 2642 s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
6725bdbf 2643 if (s->contents == NULL)
b34976b6 2644 return FALSE;
252b5132
RH
2645 }
2646
ebe50bae 2647 if (htab->elf.dynamic_sections_created)
252b5132
RH
2648 {
2649 /* Add some entries to the .dynamic section. We fill in the
2650 values later, in elf_i386_finish_dynamic_sections, but we
2651 must add the entries now so that we get the correct size for
2652 the .dynamic section. The DT_DEBUG entry is filled in by the
2653 dynamic linker and used by the debugger. */
dc810e39 2654#define add_dynamic_entry(TAG, VAL) \
5a580b3a 2655 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 2656
36af4a4e 2657 if (info->executable)
252b5132 2658 {
dc810e39 2659 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 2660 return FALSE;
252b5132
RH
2661 }
2662
6de2ae4a 2663 if (htab->elf.splt->size != 0)
252b5132 2664 {
dc810e39
AM
2665 if (!add_dynamic_entry (DT_PLTGOT, 0)
2666 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2667 || !add_dynamic_entry (DT_PLTREL, DT_REL)
2668 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 2669 return FALSE;
252b5132
RH
2670 }
2671
2672 if (relocs)
2673 {
dc810e39
AM
2674 if (!add_dynamic_entry (DT_REL, 0)
2675 || !add_dynamic_entry (DT_RELSZ, 0)
2676 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
b34976b6 2677 return FALSE;
252b5132 2678
0c715baa
AM
2679 /* If any dynamic relocs apply to a read-only section,
2680 then we need a DT_TEXTREL entry. */
248866a8 2681 if ((info->flags & DF_TEXTREL) == 0)
eb4ff4d6
L
2682 elf_link_hash_traverse (&htab->elf,
2683 elf_i386_readonly_dynrelocs, info);
0c715baa
AM
2684
2685 if ((info->flags & DF_TEXTREL) != 0)
2686 {
2687 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 2688 return FALSE;
0c715baa 2689 }
252b5132 2690 }
7a2b07ff
NS
2691 if (htab->is_vxworks
2692 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
2693 return FALSE;
252b5132 2694 }
dc810e39 2695#undef add_dynamic_entry
252b5132 2696
b34976b6 2697 return TRUE;
252b5132
RH
2698}
2699
67a4f2b7
AO
2700static bfd_boolean
2701elf_i386_always_size_sections (bfd *output_bfd,
2702 struct bfd_link_info *info)
2703{
2704 asection *tls_sec = elf_hash_table (info)->tls_sec;
2705
2706 if (tls_sec)
2707 {
2708 struct elf_link_hash_entry *tlsbase;
2709
2710 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2711 "_TLS_MODULE_BASE_",
2712 FALSE, FALSE, FALSE);
2713
2714 if (tlsbase && tlsbase->type == STT_TLS)
2715 {
4dfe6ac6 2716 struct elf_i386_link_hash_table *htab;
67a4f2b7
AO
2717 struct bfd_link_hash_entry *bh = NULL;
2718 const struct elf_backend_data *bed
2719 = get_elf_backend_data (output_bfd);
2720
4dfe6ac6
NC
2721 htab = elf_i386_hash_table (info);
2722 if (htab == NULL)
2723 return FALSE;
2724
67a4f2b7
AO
2725 if (!(_bfd_generic_link_add_one_symbol
2726 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2727 tls_sec, 0, NULL, FALSE,
2728 bed->collect, &bh)))
2729 return FALSE;
9f03412a 2730
4dfe6ac6 2731 htab->tls_module_base = bh;
9f03412a 2732
67a4f2b7
AO
2733 tlsbase = (struct elf_link_hash_entry *)bh;
2734 tlsbase->def_regular = 1;
2735 tlsbase->other = STV_HIDDEN;
2736 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2737 }
2738 }
2739
2740 return TRUE;
2741}
2742
38701953
AM
2743/* Set the correct type for an x86 ELF section. We do this by the
2744 section name, which is a hack, but ought to work. */
2745
b34976b6 2746static bfd_boolean
55fd94b0
AM
2747elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2748 Elf_Internal_Shdr *hdr,
2749 asection *sec)
38701953 2750{
91d6fa6a 2751 const char *name;
38701953
AM
2752
2753 name = bfd_get_section_name (abfd, sec);
2754
2755 /* This is an ugly, but unfortunately necessary hack that is
2756 needed when producing EFI binaries on x86. It tells
2757 elf.c:elf_fake_sections() not to consider ".reloc" as a section
2758 containing ELF relocation info. We need this hack in order to
2759 be able to generate ELF binaries that can be translated into
2760 EFI applications (which are essentially COFF objects). Those
2761 files contain a COFF ".reloc" section inside an ELFNN object,
2762 which would normally cause BFD to segfault because it would
2763 attempt to interpret this section as containing relocation
2764 entries for section "oc". With this hack enabled, ".reloc"
2765 will be treated as a normal data section, which will avoid the
2766 segfault. However, you won't be able to create an ELFNN binary
2767 with a section named "oc" that needs relocations, but that's
2768 the kind of ugly side-effects you get when detecting section
2769 types based on their names... In practice, this limitation is
2770 unlikely to bite. */
2771 if (strcmp (name, ".reloc") == 0)
2772 hdr->sh_type = SHT_PROGBITS;
2773
b34976b6 2774 return TRUE;
38701953
AM
2775}
2776
9f03412a
AO
2777/* _TLS_MODULE_BASE_ needs to be treated especially when linking
2778 executables. Rather than setting it to the beginning of the TLS
2779 section, we have to set it to the end. This function may be called
2780 multiple times, it is idempotent. */
2781
2782static void
eb4ff4d6 2783elf_i386_set_tls_module_base (struct bfd_link_info *info)
9f03412a 2784{
4dfe6ac6 2785 struct elf_i386_link_hash_table *htab;
9f03412a
AO
2786 struct bfd_link_hash_entry *base;
2787
2788 if (!info->executable)
2789 return;
2790
4dfe6ac6
NC
2791 htab = elf_i386_hash_table (info);
2792 if (htab == NULL)
2793 return;
9f03412a 2794
4dfe6ac6
NC
2795 base = htab->tls_module_base;
2796 if (base == NULL)
9f03412a
AO
2797 return;
2798
4dfe6ac6 2799 base->u.def.value = htab->elf.tls_size;
9f03412a
AO
2800}
2801
13ae64f3
JJ
2802/* Return the base VMA address which should be subtracted from real addresses
2803 when resolving @dtpoff relocation.
2804 This is PT_TLS segment p_vaddr. */
2805
2806static bfd_vma
eb4ff4d6 2807elf_i386_dtpoff_base (struct bfd_link_info *info)
13ae64f3 2808{
e1918d23
AM
2809 /* If tls_sec is NULL, we should have signalled an error already. */
2810 if (elf_hash_table (info)->tls_sec == NULL)
6a30718d 2811 return 0;
e1918d23 2812 return elf_hash_table (info)->tls_sec->vma;
13ae64f3
JJ
2813}
2814
2815/* Return the relocation value for @tpoff relocation
2816 if STT_TLS virtual address is ADDRESS. */
2817
2818static bfd_vma
eb4ff4d6 2819elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
13ae64f3 2820{
e1918d23 2821 struct elf_link_hash_table *htab = elf_hash_table (info);
13ae64f3 2822
e1918d23
AM
2823 /* If tls_sec is NULL, we should have signalled an error already. */
2824 if (htab->tls_sec == NULL)
6a30718d 2825 return 0;
e1918d23 2826 return htab->tls_size + htab->tls_sec->vma - address;
13ae64f3
JJ
2827}
2828
252b5132
RH
2829/* Relocate an i386 ELF section. */
2830
b34976b6 2831static bfd_boolean
55fd94b0
AM
2832elf_i386_relocate_section (bfd *output_bfd,
2833 struct bfd_link_info *info,
2834 bfd *input_bfd,
2835 asection *input_section,
2836 bfd_byte *contents,
2837 Elf_Internal_Rela *relocs,
2838 Elf_Internal_Sym *local_syms,
2839 asection **local_sections)
252b5132 2840{
6725bdbf 2841 struct elf_i386_link_hash_table *htab;
252b5132
RH
2842 Elf_Internal_Shdr *symtab_hdr;
2843 struct elf_link_hash_entry **sym_hashes;
2844 bfd_vma *local_got_offsets;
67a4f2b7 2845 bfd_vma *local_tlsdesc_gotents;
252b5132
RH
2846 Elf_Internal_Rela *rel;
2847 Elf_Internal_Rela *relend;
3348747a 2848 bfd_boolean is_vxworks_tls;
252b5132 2849
0ffa91dd 2850 BFD_ASSERT (is_i386_elf (input_bfd));
6bbec505 2851
6725bdbf 2852 htab = elf_i386_hash_table (info);
4dfe6ac6
NC
2853 if (htab == NULL)
2854 return FALSE;
0ffa91dd 2855 symtab_hdr = &elf_symtab_hdr (input_bfd);
252b5132
RH
2856 sym_hashes = elf_sym_hashes (input_bfd);
2857 local_got_offsets = elf_local_got_offsets (input_bfd);
67a4f2b7 2858 local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
3348747a
NS
2859 /* We have to handle relocations in vxworks .tls_vars sections
2860 specially, because the dynamic loader is 'weird'. */
2861 is_vxworks_tls = (htab->is_vxworks && info->shared
2862 && !strcmp (input_section->output_section->name,
2863 ".tls_vars"));
252b5132 2864
eb4ff4d6 2865 elf_i386_set_tls_module_base (info);
9f03412a 2866
252b5132
RH
2867 rel = relocs;
2868 relend = relocs + input_section->reloc_count;
2869 for (; rel < relend; rel++)
2870 {
13ae64f3 2871 unsigned int r_type;
252b5132
RH
2872 reloc_howto_type *howto;
2873 unsigned long r_symndx;
2874 struct elf_link_hash_entry *h;
2875 Elf_Internal_Sym *sym;
2876 asection *sec;
67a4f2b7 2877 bfd_vma off, offplt;
252b5132 2878 bfd_vma relocation;
b34976b6 2879 bfd_boolean unresolved_reloc;
252b5132 2880 bfd_reloc_status_type r;
1b452ec6 2881 unsigned int indx;
13ae64f3 2882 int tls_type;
252b5132
RH
2883
2884 r_type = ELF32_R_TYPE (rel->r_info);
55fd94b0
AM
2885 if (r_type == R_386_GNU_VTINHERIT
2886 || r_type == R_386_GNU_VTENTRY)
252b5132 2887 continue;
dc47f327 2888
55fd94b0 2889 if ((indx = r_type) >= R_386_standard
13ae64f3
JJ
2890 && ((indx = r_type - R_386_ext_offset) - R_386_standard
2891 >= R_386_ext - R_386_standard)
2892 && ((indx = r_type - R_386_tls_offset) - R_386_ext
cbe950e9 2893 >= R_386_irelative - R_386_ext))
252b5132 2894 {
6ba842b6 2895 (*_bfd_error_handler)
d003868e
AM
2896 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
2897 input_bfd, input_section, r_type);
252b5132 2898 bfd_set_error (bfd_error_bad_value);
b34976b6 2899 return FALSE;
252b5132 2900 }
1b452ec6 2901 howto = elf_howto_table + indx;
252b5132
RH
2902
2903 r_symndx = ELF32_R_SYM (rel->r_info);
252b5132
RH
2904 h = NULL;
2905 sym = NULL;
2906 sec = NULL;
b34976b6 2907 unresolved_reloc = FALSE;
252b5132
RH
2908 if (r_symndx < symtab_hdr->sh_info)
2909 {
2910 sym = local_syms + r_symndx;
2911 sec = local_sections[r_symndx];
2912 relocation = (sec->output_section->vma
2913 + sec->output_offset
2914 + sym->st_value);
ab96bf03
AM
2915
2916 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2917 && ((sec->flags & SEC_MERGE) != 0
2918 || (info->relocatable
2919 && sec->output_offset != 0)))
f8df10f4 2920 {
f8df10f4 2921 bfd_vma addend;
4a335f3d 2922 bfd_byte *where = contents + rel->r_offset;
f8df10f4 2923
4a335f3d 2924 switch (howto->size)
f8df10f4 2925 {
4a335f3d
AM
2926 case 0:
2927 addend = bfd_get_8 (input_bfd, where);
2928 if (howto->pc_relative)
2929 {
2930 addend = (addend ^ 0x80) - 0x80;
2931 addend += 1;
2932 }
2933 break;
2934 case 1:
2935 addend = bfd_get_16 (input_bfd, where);
2936 if (howto->pc_relative)
2937 {
2938 addend = (addend ^ 0x8000) - 0x8000;
2939 addend += 2;
2940 }
2941 break;
2942 case 2:
2943 addend = bfd_get_32 (input_bfd, where);
2944 if (howto->pc_relative)
2945 {
2946 addend = (addend ^ 0x80000000) - 0x80000000;
2947 addend += 4;
2948 }
2949 break;
2950 default:
2951 abort ();
f8df10f4
JJ
2952 }
2953
ab96bf03
AM
2954 if (info->relocatable)
2955 addend += sec->output_offset;
2956 else
2957 {
2958 asection *msec = sec;
2959 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2960 addend);
2961 addend -= relocation;
2962 addend += msec->output_section->vma + msec->output_offset;
2963 }
4a335f3d
AM
2964
2965 switch (howto->size)
2966 {
2967 case 0:
16a10388 2968 /* FIXME: overflow checks. */
4a335f3d
AM
2969 if (howto->pc_relative)
2970 addend -= 1;
2971 bfd_put_8 (input_bfd, addend, where);
4a335f3d
AM
2972 break;
2973 case 1:
2974 if (howto->pc_relative)
2975 addend -= 2;
2976 bfd_put_16 (input_bfd, addend, where);
4a335f3d
AM
2977 break;
2978 case 2:
2979 if (howto->pc_relative)
2980 addend -= 4;
2981 bfd_put_32 (input_bfd, addend, where);
2982 break;
2983 }
f8df10f4 2984 }
1f85278f
L
2985 else if (!info->relocatable
2986 && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
c25bc9fc
L
2987 {
2988 /* Relocate against local STT_GNU_IFUNC symbol. */
9fff0c39
L
2989 h = elf_i386_get_local_sym_hash (htab, input_bfd, rel,
2990 FALSE);
c25bc9fc
L
2991 if (h == NULL)
2992 abort ();
2993
2994 /* Set STT_GNU_IFUNC symbol value. */
2995 h->root.u.def.value = sym->st_value;
2996 h->root.u.def.section = sec;
2997 }
252b5132
RH
2998 }
2999 else
3000 {
3d540e93 3001 bfd_boolean warned ATTRIBUTE_UNUSED;
ffb2e45b 3002
b2a8e766
AM
3003 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3004 r_symndx, symtab_hdr, sym_hashes,
3005 h, sec, relocation,
3006 unresolved_reloc, warned);
252b5132
RH
3007 }
3008
ab96bf03 3009 if (sec != NULL && elf_discarded_section (sec))
0672748a
L
3010 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3011 rel, relend, howto, contents);
9635fe29 3012
ab96bf03
AM
3013 if (info->relocatable)
3014 continue;
3015
cbe950e9
L
3016 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3017 it here if it is defined in a non-shared object. */
3018 if (h != NULL
3019 && h->type == STT_GNU_IFUNC
3020 && h->def_regular)
3021 {
3022 asection *plt, *gotplt, *base_got;
3023 bfd_vma plt_index;
4c544807 3024 const char *name;
cbe950e9
L
3025
3026 if ((input_section->flags & SEC_ALLOC) == 0
3027 || h->plt.offset == (bfd_vma) -1)
3028 abort ();
3029
3030 /* STT_GNU_IFUNC symbol must go through PLT. */
6de2ae4a 3031 if (htab->elf.splt != NULL)
cbe950e9 3032 {
6de2ae4a
L
3033 plt = htab->elf.splt;
3034 gotplt = htab->elf.sgotplt;
cbe950e9
L
3035 }
3036 else
3037 {
6de2ae4a
L
3038 plt = htab->elf.iplt;
3039 gotplt = htab->elf.igotplt;
cbe950e9
L
3040 }
3041
3042 relocation = (plt->output_section->vma
3043 + plt->output_offset + h->plt.offset);
3044
3045 switch (r_type)
3046 {
3047 default:
4c544807
L
3048 if (h->root.root.string)
3049 name = h->root.root.string;
3050 else
3051 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3052 NULL);
cbe950e9
L
3053 (*_bfd_error_handler)
3054 (_("%B: relocation %s against STT_GNU_IFUNC "
3055 "symbol `%s' isn't handled by %s"), input_bfd,
3056 elf_howto_table[r_type].name,
4c544807 3057 name, __FUNCTION__);
cbe950e9
L
3058 bfd_set_error (bfd_error_bad_value);
3059 return FALSE;
3060
3061 case R_386_32:
710ab287
L
3062 /* Generate dynamic relcoation only when there is a
3063 non-GOF reference in a shared object. */
3064 if (info->shared && h->non_got_ref)
3065 {
3066 Elf_Internal_Rela outrel;
3067 bfd_byte *loc;
3068 asection *sreloc;
3069 bfd_vma offset;
3070
c25bc9fc
L
3071 /* Need a dynamic relocation to get the real function
3072 adddress. */
710ab287
L
3073 offset = _bfd_elf_section_offset (output_bfd,
3074 info,
3075 input_section,
3076 rel->r_offset);
3077 if (offset == (bfd_vma) -1
3078 || offset == (bfd_vma) -2)
3079 abort ();
3080
3081 outrel.r_offset = (input_section->output_section->vma
3082 + input_section->output_offset
3083 + offset);
3084
3085 if (h->dynindx == -1
44c4ea11
L
3086 || h->forced_local
3087 || info->executable)
710ab287
L
3088 {
3089 /* This symbol is resolved locally. */
3090 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3091 bfd_put_32 (output_bfd,
3092 (h->root.u.def.value
3093 + h->root.u.def.section->output_section->vma
3094 + h->root.u.def.section->output_offset),
3095 contents + offset);
3096 }
3097 else
3098 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3099
6de2ae4a 3100 sreloc = htab->elf.irelifunc;
710ab287
L
3101 loc = sreloc->contents;
3102 loc += (sreloc->reloc_count++
3103 * sizeof (Elf32_External_Rel));
3104 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3105
3106 /* If this reloc is against an external symbol, we
3107 do not want to fiddle with the addend. Otherwise,
3108 we need to include the symbol value so that it
3109 becomes an addend for the dynamic reloc. For an
3110 internal symbol, we have updated addend. */
3111 continue;
3112 }
3113
cbe950e9
L
3114 case R_386_PC32:
3115 case R_386_PLT32:
3116 goto do_relocation;
3117
3118 case R_386_GOT32:
6de2ae4a 3119 base_got = htab->elf.sgot;
cbe950e9
L
3120 off = h->got.offset;
3121
7afd84dc 3122 if (base_got == NULL)
cbe950e9
L
3123 abort ();
3124
7afd84dc 3125 if (off == (bfd_vma) -1)
cbe950e9 3126 {
7afd84dc
L
3127 /* We can't use h->got.offset here to save state, or
3128 even just remember the offset, as finish_dynamic_symbol
3129 would use that as offset into .got. */
6bbec505 3130
6de2ae4a 3131 if (htab->elf.splt != NULL)
7afd84dc
L
3132 {
3133 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3134 off = (plt_index + 3) * 4;
6de2ae4a 3135 base_got = htab->elf.sgotplt;
7afd84dc 3136 }
cbe950e9
L
3137 else
3138 {
7afd84dc
L
3139 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3140 off = plt_index * 4;
6de2ae4a 3141 base_got = htab->elf.igotplt;
7afd84dc
L
3142 }
3143
3144 if (h->dynindx == -1
3145 || h->forced_local
3146 || info->symbolic)
3147 {
3148 /* This references the local defitionion. We must
3149 initialize this entry in the global offset table.
3150 Since the offset must always be a multiple of 8,
3151 we use the least significant bit to record
3152 whether we have initialized it already.
3153
3154 When doing a dynamic link, we create a .rela.got
3155 relocation entry to initialize the value. This
3156 is done in the finish_dynamic_symbol routine. */
3157 if ((off & 1) != 0)
3158 off &= ~1;
3159 else
3160 {
3161 bfd_put_32 (output_bfd, relocation,
3162 base_got->contents + off);
3163 h->got.offset |= 1;
3164 }
cbe950e9 3165 }
cbe950e9 3166
7afd84dc 3167 relocation = off;
cbe950e9 3168
7afd84dc 3169 /* Adjust for static executables. */
6de2ae4a 3170 if (htab->elf.splt == NULL)
7afd84dc
L
3171 relocation += gotplt->output_offset;
3172 }
3173 else
0018b0a3
L
3174 {
3175 relocation = (base_got->output_section->vma
3176 + base_got->output_offset + off
3177 - gotplt->output_section->vma
3178 - gotplt->output_offset);
3179 /* Adjust for static executables. */
6de2ae4a 3180 if (htab->elf.splt == NULL)
0018b0a3
L
3181 relocation += gotplt->output_offset;
3182 }
3183
cbe950e9
L
3184 goto do_relocation;
3185
3186 case R_386_GOTOFF:
3187 relocation -= (gotplt->output_section->vma
3188 + gotplt->output_offset);
3189 goto do_relocation;
3190 }
3191 }
3192
252b5132
RH
3193 switch (r_type)
3194 {
3195 case R_386_GOT32:
3196 /* Relocation is to the entry for this symbol in the global
3197 offset table. */
6de2ae4a 3198 if (htab->elf.sgot == NULL)
ffb2e45b 3199 abort ();
252b5132
RH
3200
3201 if (h != NULL)
3202 {
b34976b6 3203 bfd_boolean dyn;
252b5132
RH
3204
3205 off = h->got.offset;
ebe50bae 3206 dyn = htab->elf.dynamic_sections_created;
26e41594 3207 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
252b5132 3208 || (info->shared
586119b3 3209 && SYMBOL_REFERENCES_LOCAL (info, h))
ef5aade5
L
3210 || (ELF_ST_VISIBILITY (h->other)
3211 && h->root.type == bfd_link_hash_undefweak))
252b5132
RH
3212 {
3213 /* This is actually a static link, or it is a
3214 -Bsymbolic link and the symbol is defined
3215 locally, or the symbol was forced to be local
3216 because of a version file. We must initialize
3217 this entry in the global offset table. Since the
3218 offset must always be a multiple of 4, we use the
3219 least significant bit to record whether we have
3220 initialized it already.
3221
3222 When doing a dynamic link, we create a .rel.got
3223 relocation entry to initialize the value. This
3224 is done in the finish_dynamic_symbol routine. */
3225 if ((off & 1) != 0)
3226 off &= ~1;
3227 else
3228 {
3229 bfd_put_32 (output_bfd, relocation,
6de2ae4a 3230 htab->elf.sgot->contents + off);
252b5132
RH
3231 h->got.offset |= 1;
3232 }
3233 }
8c694914 3234 else
b34976b6 3235 unresolved_reloc = FALSE;
252b5132
RH
3236 }
3237 else
3238 {
ffb2e45b
AM
3239 if (local_got_offsets == NULL)
3240 abort ();
252b5132
RH
3241
3242 off = local_got_offsets[r_symndx];
3243
3244 /* The offset must always be a multiple of 4. We use
83be169b
AM
3245 the least significant bit to record whether we have
3246 already generated the necessary reloc. */
252b5132
RH
3247 if ((off & 1) != 0)
3248 off &= ~1;
3249 else
3250 {
6725bdbf 3251 bfd_put_32 (output_bfd, relocation,
6de2ae4a 3252 htab->elf.sgot->contents + off);
252b5132
RH
3253
3254 if (info->shared)
3255 {
947216bf
AM
3256 asection *s;
3257 Elf_Internal_Rela outrel;
3258 bfd_byte *loc;
252b5132 3259
6de2ae4a 3260 s = htab->elf.srelgot;
947216bf 3261 if (s == NULL)
ffb2e45b 3262 abort ();
252b5132 3263
6de2ae4a
L
3264 outrel.r_offset = (htab->elf.sgot->output_section->vma
3265 + htab->elf.sgot->output_offset
252b5132
RH
3266 + off);
3267 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
947216bf
AM
3268 loc = s->contents;
3269 loc += s->reloc_count++ * sizeof (Elf32_External_Rel);
0ac8d2ca 3270 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
252b5132
RH
3271 }
3272
3273 local_got_offsets[r_symndx] |= 1;
3274 }
252b5132
RH
3275 }
3276
ffb2e45b
AM
3277 if (off >= (bfd_vma) -2)
3278 abort ();
3279
6de2ae4a
L
3280 relocation = htab->elf.sgot->output_section->vma
3281 + htab->elf.sgot->output_offset + off
3282 - htab->elf.sgotplt->output_section->vma
3283 - htab->elf.sgotplt->output_offset;
252b5132
RH
3284 break;
3285
3286 case R_386_GOTOFF:
3287 /* Relocation is relative to the start of the global offset
3288 table. */
3289
90f487df
L
3290 /* Check to make sure it isn't a protected function symbol
3291 for shared library since it may not be local when used
41bed6dd
L
3292 as function address. We also need to make sure that a
3293 symbol is defined locally. */
3294 if (info->shared && h)
90f487df 3295 {
41bed6dd
L
3296 if (!h->def_regular)
3297 {
3298 const char *v;
3299
3300 switch (ELF_ST_VISIBILITY (h->other))
3301 {
3302 case STV_HIDDEN:
3303 v = _("hidden symbol");
3304 break;
3305 case STV_INTERNAL:
3306 v = _("internal symbol");
3307 break;
3308 case STV_PROTECTED:
3309 v = _("protected symbol");
3310 break;
3311 default:
3312 v = _("symbol");
3313 break;
3314 }
3315
3316 (*_bfd_error_handler)
3317 (_("%B: relocation R_386_GOTOFF against undefined %s `%s' can not be used when making a shared object"),
3318 input_bfd, v, h->root.root.string);
3319 bfd_set_error (bfd_error_bad_value);
3320 return FALSE;
3321 }
3322 else if (!info->executable
3323 && h->type == STT_FUNC
3324 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3325 {
3326 (*_bfd_error_handler)
3327 (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
3328 input_bfd, h->root.root.string);
3329 bfd_set_error (bfd_error_bad_value);
3330 return FALSE;
3331 }
90f487df
L
3332 }
3333
8c37241b
JJ
3334 /* Note that sgot is not involved in this
3335 calculation. We always want the start of .got.plt. If we
3336 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
252b5132
RH
3337 permitted by the ABI, we might have to change this
3338 calculation. */
6de2ae4a
L
3339 relocation -= htab->elf.sgotplt->output_section->vma
3340 + htab->elf.sgotplt->output_offset;
252b5132
RH
3341 break;
3342
3343 case R_386_GOTPC:
3344 /* Use global offset table as symbol value. */
6de2ae4a
L
3345 relocation = htab->elf.sgotplt->output_section->vma
3346 + htab->elf.sgotplt->output_offset;
b34976b6 3347 unresolved_reloc = FALSE;
252b5132
RH
3348 break;
3349
3350 case R_386_PLT32:
3351 /* Relocation is to the entry for this symbol in the
3352 procedure linkage table. */
3353
dd5724d5 3354 /* Resolve a PLT32 reloc against a local symbol directly,
83be169b 3355 without using the procedure linkage table. */
252b5132
RH
3356 if (h == NULL)
3357 break;
3358
dd5724d5 3359 if (h->plt.offset == (bfd_vma) -1
6de2ae4a 3360 || htab->elf.splt == NULL)
252b5132
RH
3361 {
3362 /* We didn't make a PLT entry for this symbol. This
83be169b
AM
3363 happens when statically linking PIC code, or when
3364 using -Bsymbolic. */
252b5132
RH
3365 break;
3366 }
3367
6de2ae4a
L
3368 relocation = (htab->elf.splt->output_section->vma
3369 + htab->elf.splt->output_offset
252b5132 3370 + h->plt.offset);
b34976b6 3371 unresolved_reloc = FALSE;
252b5132
RH
3372 break;
3373
3374 case R_386_32:
3375 case R_386_PC32:
3348747a
NS
3376 if ((input_section->flags & SEC_ALLOC) == 0
3377 || is_vxworks_tls)
ec338859
AM
3378 break;
3379
12d0ee4a 3380 if ((info->shared
ef5aade5
L
3381 && (h == NULL
3382 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3383 || h->root.type != bfd_link_hash_undefweak)
12d0ee4a 3384 && (r_type != R_386_PC32
f6c52c13 3385 || !SYMBOL_CALLS_LOCAL (info, h)))
a23b6845
AM
3386 || (ELIMINATE_COPY_RELOCS
3387 && !info->shared
12d0ee4a
AM
3388 && h != NULL
3389 && h->dynindx != -1
f5385ebf
AM
3390 && !h->non_got_ref
3391 && ((h->def_dynamic
3392 && !h->def_regular)
28d0b90e 3393 || h->root.type == bfd_link_hash_undefweak
0f88be7a 3394 || h->root.type == bfd_link_hash_undefined)))
252b5132 3395 {
947216bf
AM
3396 Elf_Internal_Rela outrel;
3397 bfd_byte *loc;
b34976b6 3398 bfd_boolean skip, relocate;
0c715baa 3399 asection *sreloc;
252b5132
RH
3400
3401 /* When generating a shared object, these relocations
3402 are copied into the output file to be resolved at run
3403 time. */
3404
b34976b6
AM
3405 skip = FALSE;
3406 relocate = FALSE;
252b5132 3407
c629eae0
JJ
3408 outrel.r_offset =
3409 _bfd_elf_section_offset (output_bfd, info, input_section,
3410 rel->r_offset);
3411 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 3412 skip = TRUE;
0bb2d96a 3413 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 3414 skip = TRUE, relocate = TRUE;
252b5132
RH
3415 outrel.r_offset += (input_section->output_section->vma
3416 + input_section->output_offset);
3417
3418 if (skip)
0bb2d96a 3419 memset (&outrel, 0, sizeof outrel);
5a15f56f
AM
3420 else if (h != NULL
3421 && h->dynindx != -1
3422 && (r_type == R_386_PC32
3423 || !info->shared
55255dae 3424 || !SYMBOLIC_BIND (info, h)
f5385ebf 3425 || !h->def_regular))
0bb2d96a 3426 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
252b5132
RH
3427 else
3428 {
5a15f56f 3429 /* This symbol is local, or marked to become local. */
b34976b6 3430 relocate = TRUE;
5a15f56f 3431 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
252b5132
RH
3432 }
3433
cbe950e9 3434 sreloc = elf_section_data (input_section)->sreloc;
e7c33416
NC
3435
3436 BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
0c715baa 3437
947216bf
AM
3438 loc = sreloc->contents;
3439 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
e7c33416 3440
0c715baa 3441 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
252b5132
RH
3442
3443 /* If this reloc is against an external symbol, we do
3444 not want to fiddle with the addend. Otherwise, we
3445 need to include the symbol value so that it becomes
3446 an addend for the dynamic reloc. */
0f88be7a 3447 if (! relocate)
252b5132
RH
3448 continue;
3449 }
252b5132
RH
3450 break;
3451
37e55690 3452 case R_386_TLS_IE:
1d85728f 3453 if (!info->executable)
37e55690 3454 {
947216bf
AM
3455 Elf_Internal_Rela outrel;
3456 bfd_byte *loc;
37e55690 3457 asection *sreloc;
37e55690
JJ
3458
3459 outrel.r_offset = rel->r_offset
3460 + input_section->output_section->vma
3461 + input_section->output_offset;
3462 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3463 sreloc = elf_section_data (input_section)->sreloc;
3464 if (sreloc == NULL)
3465 abort ();
947216bf
AM
3466 loc = sreloc->contents;
3467 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
37e55690
JJ
3468 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3469 }
3470 /* Fall through */
3471
13ae64f3 3472 case R_386_TLS_GD:
67a4f2b7
AO
3473 case R_386_TLS_GOTDESC:
3474 case R_386_TLS_DESC_CALL:
13ae64f3 3475 case R_386_TLS_IE_32:
37e55690 3476 case R_386_TLS_GOTIE:
13ae64f3
JJ
3477 tls_type = GOT_UNKNOWN;
3478 if (h == NULL && local_got_offsets)
3479 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
3480 else if (h != NULL)
142411ca 3481 tls_type = elf_i386_hash_entry(h)->tls_type;
37e55690
JJ
3482 if (tls_type == GOT_TLS_IE)
3483 tls_type = GOT_TLS_IE_NEG;
142411ca
L
3484
3485 if (! elf_i386_tls_transition (info, input_bfd,
3486 input_section, contents,
3487 symtab_hdr, sym_hashes,
3488 &r_type, tls_type, rel,
4c544807 3489 relend, h, r_symndx))
142411ca 3490 return FALSE;
13ae64f3
JJ
3491
3492 if (r_type == R_386_TLS_LE_32)
3493 {
82e51918 3494 BFD_ASSERT (! unresolved_reloc);
13ae64f3
JJ
3495 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3496 {
142411ca 3497 unsigned int type;
13ae64f3
JJ
3498 bfd_vma roff;
3499
3500 /* GD->LE transition. */
13ae64f3 3501 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
13ae64f3
JJ
3502 if (type == 0x04)
3503 {
3504 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3505 Change it into:
3506 movl %gs:0, %eax; subl $foo@tpoff, %eax
3507 (6 byte form of subl). */
13ae64f3
JJ
3508 memcpy (contents + rel->r_offset - 3,
3509 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
142411ca 3510 roff = rel->r_offset + 5;
13ae64f3
JJ
3511 }
3512 else
3513 {
142411ca
L
3514 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3515 Change it into:
3516 movl %gs:0, %eax; subl $foo@tpoff, %eax
3517 (6 byte form of subl). */
3518 memcpy (contents + rel->r_offset - 2,
3519 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3520 roff = rel->r_offset + 6;
13ae64f3 3521 }
eb4ff4d6 3522 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
13ae64f3 3523 contents + roff);
a3fadc9a 3524 /* Skip R_386_PC32/R_386_PLT32. */
13ae64f3
JJ
3525 rel++;
3526 continue;
3527 }
67a4f2b7
AO
3528 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3529 {
3530 /* GDesc -> LE transition.
3531 It's originally something like:
3532 leal x@tlsdesc(%ebx), %eax
3533
3534 leal x@ntpoff, %eax
3535
3536 Registers other than %eax may be set up here. */
3537
142411ca 3538 unsigned int val;
67a4f2b7
AO
3539 bfd_vma roff;
3540
67a4f2b7 3541 roff = rel->r_offset;
67a4f2b7 3542 val = bfd_get_8 (input_bfd, contents + roff - 1);
67a4f2b7
AO
3543
3544 /* Now modify the instruction as appropriate. */
3545 /* aoliva FIXME: remove the above and xor the byte
3546 below with 0x86. */
3547 bfd_put_8 (output_bfd, val ^ 0x86,
3548 contents + roff - 1);
eb4ff4d6 3549 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
67a4f2b7
AO
3550 contents + roff);
3551 continue;
3552 }
3553 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3554 {
3555 /* GDesc -> LE transition.
3556 It's originally:
3557 call *(%eax)
3558 Turn it into:
142411ca 3559 xchg %ax,%ax */
67a4f2b7 3560
67a4f2b7 3561 bfd_vma roff;
6bbec505 3562
67a4f2b7 3563 roff = rel->r_offset;
10efb593 3564 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
3565 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3566 continue;
3567 }
37e55690 3568 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
13ae64f3 3569 {
142411ca 3570 unsigned int val;
13ae64f3
JJ
3571
3572 /* IE->LE transition:
37e55690
JJ
3573 Originally it can be one of:
3574 movl foo, %eax
3575 movl foo, %reg
3576 addl foo, %reg
3577 We change it into:
3578 movl $foo, %eax
3579 movl $foo, %reg
3580 addl $foo, %reg. */
37e55690 3581 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
37e55690
JJ
3582 if (val == 0xa1)
3583 {
3584 /* movl foo, %eax. */
55fd94b0
AM
3585 bfd_put_8 (output_bfd, 0xb8,
3586 contents + rel->r_offset - 1);
37e55690 3587 }
299bf759 3588 else
37e55690 3589 {
142411ca
L
3590 unsigned int type;
3591
55fd94b0
AM
3592 type = bfd_get_8 (input_bfd,
3593 contents + rel->r_offset - 2);
299bf759 3594 switch (type)
26e41594 3595 {
299bf759
L
3596 case 0x8b:
3597 /* movl */
299bf759
L
3598 bfd_put_8 (output_bfd, 0xc7,
3599 contents + rel->r_offset - 2);
3600 bfd_put_8 (output_bfd,
3601 0xc0 | ((val >> 3) & 7),
3602 contents + rel->r_offset - 1);
3603 break;
3604 case 0x03:
3605 /* addl */
299bf759
L
3606 bfd_put_8 (output_bfd, 0x81,
3607 contents + rel->r_offset - 2);
3608 bfd_put_8 (output_bfd,
3609 0xc0 | ((val >> 3) & 7),
3610 contents + rel->r_offset - 1);
3611 break;
3612 default:
3613 BFD_FAIL ();
3614 break;
26e41594 3615 }
37e55690 3616 }
eb4ff4d6 3617 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
37e55690
JJ
3618 contents + rel->r_offset);
3619 continue;
3620 }
3621 else
3622 {
3623 unsigned int val, type;
3624
3625 /* {IE_32,GOTIE}->LE transition:
3626 Originally it can be one of:
13ae64f3 3627 subl foo(%reg1), %reg2
13ae64f3 3628 movl foo(%reg1), %reg2
37e55690 3629 addl foo(%reg1), %reg2
13ae64f3
JJ
3630 We change it into:
3631 subl $foo, %reg2
37e55690
JJ
3632 movl $foo, %reg2 (6 byte form)
3633 addl $foo, %reg2. */
13ae64f3
JJ
3634 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3635 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
13ae64f3
JJ
3636 if (type == 0x8b)
3637 {
3638 /* movl */
13ae64f3
JJ
3639 bfd_put_8 (output_bfd, 0xc7,
3640 contents + rel->r_offset - 2);
3641 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3642 contents + rel->r_offset - 1);
3643 }
3644 else if (type == 0x2b)
3645 {
3646 /* subl */
13ae64f3
JJ
3647 bfd_put_8 (output_bfd, 0x81,
3648 contents + rel->r_offset - 2);
3649 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3650 contents + rel->r_offset - 1);
3651 }
37e55690
JJ
3652 else if (type == 0x03)
3653 {
3654 /* addl */
3655 bfd_put_8 (output_bfd, 0x81,
3656 contents + rel->r_offset - 2);
3657 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3658 contents + rel->r_offset - 1);
3659 }
13ae64f3
JJ
3660 else
3661 BFD_FAIL ();
37e55690 3662 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
eb4ff4d6 3663 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
37e55690
JJ
3664 contents + rel->r_offset);
3665 else
eb4ff4d6 3666 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
37e55690 3667 contents + rel->r_offset);
13ae64f3
JJ
3668 continue;
3669 }
3670 }
3671
6de2ae4a 3672 if (htab->elf.sgot == NULL)
13ae64f3
JJ
3673 abort ();
3674
3675 if (h != NULL)
67a4f2b7
AO
3676 {
3677 off = h->got.offset;
3678 offplt = elf_i386_hash_entry (h)->tlsdesc_got;
3679 }
13ae64f3
JJ
3680 else
3681 {
3682 if (local_got_offsets == NULL)
3683 abort ();
3684
3685 off = local_got_offsets[r_symndx];
67a4f2b7 3686 offplt = local_tlsdesc_gotents[r_symndx];
13ae64f3
JJ
3687 }
3688
3689 if ((off & 1) != 0)
3690 off &= ~1;
26e41594 3691 else
13ae64f3 3692 {
947216bf
AM
3693 Elf_Internal_Rela outrel;
3694 bfd_byte *loc;
91d6fa6a 3695 int dr_type;
67a4f2b7 3696 asection *sreloc;
13ae64f3 3697
6de2ae4a 3698 if (htab->elf.srelgot == NULL)
13ae64f3
JJ
3699 abort ();
3700
67a4f2b7
AO
3701 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3702
3703 if (GOT_TLS_GDESC_P (tls_type))
3704 {
3705 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3706 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
6de2ae4a
L
3707 <= htab->elf.sgotplt->size);
3708 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3709 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
3710 + offplt
3711 + htab->sgotplt_jump_table_size);
6de2ae4a 3712 sreloc = htab->elf.srelplt;
67a4f2b7 3713 loc = sreloc->contents;
5ae0bfb6
RS
3714 loc += (htab->next_tls_desc_index++
3715 * sizeof (Elf32_External_Rel));
67a4f2b7
AO
3716 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3717 <= sreloc->contents + sreloc->size);
3718 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3719 if (indx == 0)
3720 {
3721 BFD_ASSERT (! unresolved_reloc);
3722 bfd_put_32 (output_bfd,
eb4ff4d6 3723 relocation - elf_i386_dtpoff_base (info),
6de2ae4a 3724 htab->elf.sgotplt->contents + offplt
67a4f2b7
AO
3725 + htab->sgotplt_jump_table_size + 4);
3726 }
3727 else
3728 {
3729 bfd_put_32 (output_bfd, 0,
6de2ae4a 3730 htab->elf.sgotplt->contents + offplt
67a4f2b7
AO
3731 + htab->sgotplt_jump_table_size + 4);
3732 }
3733 }
3734
6de2ae4a 3735 sreloc = htab->elf.srelgot;
67a4f2b7 3736
6de2ae4a
L
3737 outrel.r_offset = (htab->elf.sgot->output_section->vma
3738 + htab->elf.sgot->output_offset + off);
13ae64f3 3739
67a4f2b7 3740 if (GOT_TLS_GD_P (tls_type))
13ae64f3 3741 dr_type = R_386_TLS_DTPMOD32;
67a4f2b7
AO
3742 else if (GOT_TLS_GDESC_P (tls_type))
3743 goto dr_done;
37e55690
JJ
3744 else if (tls_type == GOT_TLS_IE_POS)
3745 dr_type = R_386_TLS_TPOFF;
13ae64f3
JJ
3746 else
3747 dr_type = R_386_TLS_TPOFF32;
67a4f2b7 3748
37e55690 3749 if (dr_type == R_386_TLS_TPOFF && indx == 0)
eb4ff4d6
L
3750 bfd_put_32 (output_bfd,
3751 relocation - elf_i386_dtpoff_base (info),
6de2ae4a 3752 htab->elf.sgot->contents + off);
37e55690 3753 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
eb4ff4d6
L
3754 bfd_put_32 (output_bfd,
3755 elf_i386_dtpoff_base (info) - relocation,
6de2ae4a 3756 htab->elf.sgot->contents + off);
67a4f2b7 3757 else if (dr_type != R_386_TLS_DESC)
c366c25e 3758 bfd_put_32 (output_bfd, 0,
6de2ae4a 3759 htab->elf.sgot->contents + off);
13ae64f3 3760 outrel.r_info = ELF32_R_INFO (indx, dr_type);
67a4f2b7
AO
3761
3762 loc = sreloc->contents;
3763 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3764 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3765 <= sreloc->contents + sreloc->size);
13ae64f3
JJ
3766 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3767
67a4f2b7 3768 if (GOT_TLS_GD_P (tls_type))
13ae64f3
JJ
3769 {
3770 if (indx == 0)
3771 {
82e51918 3772 BFD_ASSERT (! unresolved_reloc);
13ae64f3 3773 bfd_put_32 (output_bfd,
eb4ff4d6 3774 relocation - elf_i386_dtpoff_base (info),
6de2ae4a 3775 htab->elf.sgot->contents + off + 4);
13ae64f3
JJ
3776 }
3777 else
3778 {
3779 bfd_put_32 (output_bfd, 0,
6de2ae4a 3780 htab->elf.sgot->contents + off + 4);
13ae64f3
JJ
3781 outrel.r_info = ELF32_R_INFO (indx,
3782 R_386_TLS_DTPOFF32);
3783 outrel.r_offset += 4;
67a4f2b7 3784 sreloc->reloc_count++;
947216bf 3785 loc += sizeof (Elf32_External_Rel);
67a4f2b7
AO
3786 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3787 <= sreloc->contents + sreloc->size);
947216bf 3788 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
13ae64f3
JJ
3789 }
3790 }
37e55690
JJ
3791 else if (tls_type == GOT_TLS_IE_BOTH)
3792 {
3793 bfd_put_32 (output_bfd,
eb4ff4d6
L
3794 (indx == 0
3795 ? relocation - elf_i386_dtpoff_base (info)
3796 : 0),
6de2ae4a 3797 htab->elf.sgot->contents + off + 4);
37e55690
JJ
3798 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3799 outrel.r_offset += 4;
67a4f2b7 3800 sreloc->reloc_count++;
947216bf 3801 loc += sizeof (Elf32_External_Rel);
37e55690
JJ
3802 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3803 }
13ae64f3 3804
67a4f2b7 3805 dr_done:
13ae64f3
JJ
3806 if (h != NULL)
3807 h->got.offset |= 1;
3808 else
3809 local_got_offsets[r_symndx] |= 1;
3810 }
3811
67a4f2b7
AO
3812 if (off >= (bfd_vma) -2
3813 && ! GOT_TLS_GDESC_P (tls_type))
13ae64f3 3814 abort ();
67a4f2b7
AO
3815 if (r_type == R_386_TLS_GOTDESC
3816 || r_type == R_386_TLS_DESC_CALL)
3817 {
3818 relocation = htab->sgotplt_jump_table_size + offplt;
3819 unresolved_reloc = FALSE;
3820 }
3821 else if (r_type == ELF32_R_TYPE (rel->r_info))
13ae64f3 3822 {
6de2ae4a
L
3823 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
3824 + htab->elf.sgotplt->output_offset;
3825 relocation = htab->elf.sgot->output_section->vma
3826 + htab->elf.sgot->output_offset + off - g_o_t;
37e55690
JJ
3827 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3828 && tls_type == GOT_TLS_IE_BOTH)
3829 relocation += 4;
3830 if (r_type == R_386_TLS_IE)
8c37241b 3831 relocation += g_o_t;
b34976b6 3832 unresolved_reloc = FALSE;
13ae64f3 3833 }
67a4f2b7 3834 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
13ae64f3
JJ
3835 {
3836 unsigned int val, type;
3837 bfd_vma roff;
3838
3839 /* GD->IE transition. */
13ae64f3 3840 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
13ae64f3
JJ
3841 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3842 if (type == 0x04)
3843 {
3844 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3845 Change it into:
3846 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
13ae64f3 3847 val >>= 3;
142411ca 3848 roff = rel->r_offset - 3;
13ae64f3
JJ
3849 }
3850 else
3851 {
3852 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3853 Change it into:
3854 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
13ae64f3
JJ
3855 roff = rel->r_offset - 2;
3856 }
3857 memcpy (contents + roff,
3858 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3859 contents[roff + 7] = 0x80 | (val & 7);
37e55690
JJ
3860 /* If foo is used only with foo@gotntpoff(%reg) and
3861 foo@indntpoff, but not with foo@gottpoff(%reg), change
3862 subl $foo@gottpoff(%reg), %eax
3863 into:
3864 addl $foo@gotntpoff(%reg), %eax. */
ebcfb3c0
JJ
3865 if (tls_type == GOT_TLS_IE_POS)
3866 contents[roff + 6] = 0x03;
8c37241b 3867 bfd_put_32 (output_bfd,
6de2ae4a
L
3868 htab->elf.sgot->output_section->vma
3869 + htab->elf.sgot->output_offset + off
3870 - htab->elf.sgotplt->output_section->vma
3871 - htab->elf.sgotplt->output_offset,
13ae64f3
JJ
3872 contents + roff + 8);
3873 /* Skip R_386_PLT32. */
3874 rel++;
3875 continue;
3876 }
67a4f2b7
AO
3877 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3878 {
3879 /* GDesc -> IE transition.
3880 It's originally something like:
3881 leal x@tlsdesc(%ebx), %eax
3882
3883 Change it to:
142411ca 3884 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
67a4f2b7
AO
3885 or:
3886 movl x@gottpoff(%ebx), %eax # before negl %eax
3887
3888 Registers other than %eax may be set up here. */
3889
67a4f2b7
AO
3890 bfd_vma roff;
3891
3892 /* First, make sure it's a leal adding ebx to a 32-bit
3893 offset into any register, although it's probably
3894 almost always going to be eax. */
3895 roff = rel->r_offset;
67a4f2b7
AO
3896
3897 /* Now modify the instruction as appropriate. */
3898 /* To turn a leal into a movl in the form we use it, it
3899 suffices to change the first byte from 0x8d to 0x8b.
3900 aoliva FIXME: should we decide to keep the leal, all
3901 we have to do is remove the statement below, and
3902 adjust the relaxation of R_386_TLS_DESC_CALL. */
3903 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3904
3905 if (tls_type == GOT_TLS_IE_BOTH)
3906 off += 4;
3907
3908 bfd_put_32 (output_bfd,
6de2ae4a
L
3909 htab->elf.sgot->output_section->vma
3910 + htab->elf.sgot->output_offset + off
3911 - htab->elf.sgotplt->output_section->vma
3912 - htab->elf.sgotplt->output_offset,
67a4f2b7
AO
3913 contents + roff);
3914 continue;
3915 }
3916 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3917 {
3918 /* GDesc -> IE transition.
3919 It's originally:
3920 call *(%eax)
3921
3922 Change it to:
142411ca 3923 xchg %ax,%ax
67a4f2b7
AO
3924 or
3925 negl %eax
3926 depending on how we transformed the TLS_GOTDESC above.
3927 */
3928
67a4f2b7
AO
3929 bfd_vma roff;
3930
67a4f2b7 3931 roff = rel->r_offset;
67a4f2b7
AO
3932
3933 /* Now modify the instruction as appropriate. */
3934 if (tls_type != GOT_TLS_IE_NEG)
3935 {
10efb593
L
3936 /* xchg %ax,%ax */
3937 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
3938 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3939 }
3940 else
3941 {
3942 /* negl %eax */
3943 bfd_put_8 (output_bfd, 0xf7, contents + roff);
3944 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3945 }
3946
3947 continue;
3948 }
3949 else
3950 BFD_ASSERT (FALSE);
13ae64f3
JJ
3951 break;
3952
3953 case R_386_TLS_LDM:
142411ca
L
3954 if (! elf_i386_tls_transition (info, input_bfd,
3955 input_section, contents,
3956 symtab_hdr, sym_hashes,
3957 &r_type, GOT_UNKNOWN, rel,
4c544807 3958 relend, h, r_symndx))
142411ca 3959 return FALSE;
13ae64f3 3960
142411ca
L
3961 if (r_type != R_386_TLS_LDM)
3962 {
13ae64f3 3963 /* LD->LE transition:
13ae64f3
JJ
3964 leal foo(%reg), %eax; call ___tls_get_addr.
3965 We change it into:
3966 movl %gs:0, %eax; nop; leal 0(%esi,1), %esi. */
142411ca 3967 BFD_ASSERT (r_type == R_386_TLS_LE_32);
13ae64f3
JJ
3968 memcpy (contents + rel->r_offset - 2,
3969 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
a3fadc9a 3970 /* Skip R_386_PC32/R_386_PLT32. */
13ae64f3
JJ
3971 rel++;
3972 continue;
3973 }
3974
6de2ae4a 3975 if (htab->elf.sgot == NULL)
13ae64f3
JJ
3976 abort ();
3977
3978 off = htab->tls_ldm_got.offset;
3979 if (off & 1)
3980 off &= ~1;
3981 else
3982 {
947216bf
AM
3983 Elf_Internal_Rela outrel;
3984 bfd_byte *loc;
13ae64f3 3985
6de2ae4a 3986 if (htab->elf.srelgot == NULL)
13ae64f3
JJ
3987 abort ();
3988
6de2ae4a
L
3989 outrel.r_offset = (htab->elf.sgot->output_section->vma
3990 + htab->elf.sgot->output_offset + off);
13ae64f3
JJ
3991
3992 bfd_put_32 (output_bfd, 0,
6de2ae4a 3993 htab->elf.sgot->contents + off);
13ae64f3 3994 bfd_put_32 (output_bfd, 0,
6de2ae4a 3995 htab->elf.sgot->contents + off + 4);
13ae64f3 3996 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
6de2ae4a
L
3997 loc = htab->elf.srelgot->contents;
3998 loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
13ae64f3
JJ
3999 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
4000 htab->tls_ldm_got.offset |= 1;
4001 }
6de2ae4a
L
4002 relocation = htab->elf.sgot->output_section->vma
4003 + htab->elf.sgot->output_offset + off
4004 - htab->elf.sgotplt->output_section->vma
4005 - htab->elf.sgotplt->output_offset;
b34976b6 4006 unresolved_reloc = FALSE;
13ae64f3
JJ
4007 break;
4008
4009 case R_386_TLS_LDO_32:
a45bb67d 4010 if (info->shared || (input_section->flags & SEC_CODE) == 0)
eb4ff4d6 4011 relocation -= elf_i386_dtpoff_base (info);
13ae64f3
JJ
4012 else
4013 /* When converting LDO to LE, we must negate. */
eb4ff4d6 4014 relocation = -elf_i386_tpoff (info, relocation);
13ae64f3
JJ
4015 break;
4016
4017 case R_386_TLS_LE_32:
13ae64f3 4018 case R_386_TLS_LE:
1d85728f 4019 if (!info->executable)
37e55690 4020 {
947216bf 4021 Elf_Internal_Rela outrel;
37e55690 4022 asection *sreloc;
947216bf 4023 bfd_byte *loc;
37e55690
JJ
4024
4025 outrel.r_offset = rel->r_offset
4026 + input_section->output_section->vma
4027 + input_section->output_offset;
4028 if (h != NULL && h->dynindx != -1)
4029 indx = h->dynindx;
4030 else
4031 indx = 0;
4032 if (r_type == R_386_TLS_LE_32)
4033 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
4034 else
4035 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
4036 sreloc = elf_section_data (input_section)->sreloc;
4037 if (sreloc == NULL)
4038 abort ();
947216bf
AM
4039 loc = sreloc->contents;
4040 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
37e55690
JJ
4041 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
4042 if (indx)
4043 continue;
4044 else if (r_type == R_386_TLS_LE_32)
eb4ff4d6 4045 relocation = elf_i386_dtpoff_base (info) - relocation;
37e55690 4046 else
eb4ff4d6 4047 relocation -= elf_i386_dtpoff_base (info);
37e55690
JJ
4048 }
4049 else if (r_type == R_386_TLS_LE_32)
eb4ff4d6 4050 relocation = elf_i386_tpoff (info, relocation);
37e55690 4051 else
eb4ff4d6 4052 relocation = -elf_i386_tpoff (info, relocation);
13ae64f3
JJ
4053 break;
4054
252b5132
RH
4055 default:
4056 break;
4057 }
4058
239e1f3a
AM
4059 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4060 because such sections are not SEC_ALLOC and thus ld.so will
4061 not process them. */
8c694914 4062 if (unresolved_reloc
239e1f3a 4063 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 4064 && h->def_dynamic))
6a30718d
JJ
4065 {
4066 (*_bfd_error_handler)
843fe662 4067 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
d003868e
AM
4068 input_bfd,
4069 input_section,
6a30718d 4070 (long) rel->r_offset,
843fe662 4071 howto->name,
6a30718d 4072 h->root.root.string);
b34976b6 4073 return FALSE;
6a30718d 4074 }
83be169b 4075
cbe950e9 4076do_relocation:
252b5132
RH
4077 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4078 contents, rel->r_offset,
55fd94b0 4079 relocation, 0);
252b5132 4080
cf5c0c5b 4081 if (r != bfd_reloc_ok)
252b5132 4082 {
cf5c0c5b 4083 const char *name;
ffb2e45b 4084
cf5c0c5b
AM
4085 if (h != NULL)
4086 name = h->root.root.string;
4087 else
4088 {
4089 name = bfd_elf_string_from_elf_section (input_bfd,
4090 symtab_hdr->sh_link,
4091 sym->st_name);
4092 if (name == NULL)
b34976b6 4093 return FALSE;
cf5c0c5b
AM
4094 if (*name == '\0')
4095 name = bfd_section_name (input_bfd, sec);
4096 }
ffb2e45b 4097
cf5c0c5b
AM
4098 if (r == bfd_reloc_overflow)
4099 {
cf5c0c5b 4100 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
4101 (info, (h ? &h->root : NULL), name, howto->name,
4102 (bfd_vma) 0, input_bfd, input_section,
4103 rel->r_offset)))
b34976b6 4104 return FALSE;
cf5c0c5b
AM
4105 }
4106 else
4107 {
4108 (*_bfd_error_handler)
d003868e
AM
4109 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
4110 input_bfd, input_section,
cf5c0c5b 4111 (long) rel->r_offset, name, (int) r);
b34976b6 4112 return FALSE;
cf5c0c5b 4113 }
252b5132
RH
4114 }
4115 }
4116
b34976b6 4117 return TRUE;
252b5132
RH
4118}
4119
4120/* Finish up dynamic symbol handling. We set the contents of various
4121 dynamic sections here. */
4122
b34976b6 4123static bfd_boolean
55fd94b0
AM
4124elf_i386_finish_dynamic_symbol (bfd *output_bfd,
4125 struct bfd_link_info *info,
4126 struct elf_link_hash_entry *h,
4127 Elf_Internal_Sym *sym)
252b5132 4128{
6725bdbf 4129 struct elf_i386_link_hash_table *htab;
252b5132 4130
6725bdbf 4131 htab = elf_i386_hash_table (info);
4dfe6ac6
NC
4132 if (htab == NULL)
4133 return FALSE;
252b5132
RH
4134
4135 if (h->plt.offset != (bfd_vma) -1)
4136 {
252b5132
RH
4137 bfd_vma plt_index;
4138 bfd_vma got_offset;
947216bf
AM
4139 Elf_Internal_Rela rel;
4140 bfd_byte *loc;
cbe950e9
L
4141 asection *plt, *gotplt, *relplt;
4142
4143 /* When building a static executable, use .iplt, .igot.plt and
4144 .rel.iplt sections for STT_GNU_IFUNC symbols. */
6de2ae4a 4145 if (htab->elf.splt != NULL)
cbe950e9 4146 {
6de2ae4a
L
4147 plt = htab->elf.splt;
4148 gotplt = htab->elf.sgotplt;
4149 relplt = htab->elf.srelplt;
cbe950e9
L
4150 }
4151 else
4152 {
6de2ae4a
L
4153 plt = htab->elf.iplt;
4154 gotplt = htab->elf.igotplt;
4155 relplt = htab->elf.irelplt;
cbe950e9 4156 }
252b5132
RH
4157
4158 /* This symbol has an entry in the procedure linkage table. Set
4159 it up. */
4160
cbe950e9
L
4161 if ((h->dynindx == -1
4162 && !((h->forced_local || info->executable)
4163 && h->def_regular
4164 && h->type == STT_GNU_IFUNC))
4165 || plt == NULL
4166 || gotplt == NULL
4167 || relplt == NULL)
ffb2e45b 4168 abort ();
252b5132
RH
4169
4170 /* Get the index in the procedure linkage table which
4171 corresponds to this symbol. This is the index of this symbol
4172 in all the symbols for which we are making plt entries. The
cbe950e9 4173 first entry in the procedure linkage table is reserved.
252b5132 4174
cbe950e9 4175 Get the offset into the .got table of the entry that
252b5132 4176 corresponds to this function. Each .got entry is 4 bytes.
cbe950e9 4177 The first three are reserved.
6bbec505 4178
cbe950e9
L
4179 For static executables, we don't reserve anything. */
4180
6de2ae4a 4181 if (plt == htab->elf.splt)
cbe950e9
L
4182 {
4183 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
4184 got_offset = (plt_index + 3) * 4;
4185 }
4186 else
4187 {
4188 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
4189 got_offset = plt_index * 4;
4190 }
252b5132
RH
4191
4192 /* Fill in the entry in the procedure linkage table. */
4193 if (! info->shared)
4194 {
cbe950e9 4195 memcpy (plt->contents + h->plt.offset, elf_i386_plt_entry,
252b5132
RH
4196 PLT_ENTRY_SIZE);
4197 bfd_put_32 (output_bfd,
cbe950e9
L
4198 (gotplt->output_section->vma
4199 + gotplt->output_offset
252b5132 4200 + got_offset),
cbe950e9 4201 plt->contents + h->plt.offset + 2);
eac338cf
PB
4202
4203 if (htab->is_vxworks)
4204 {
4205 int s, k, reloc_index;
4206
4207 /* Create the R_386_32 relocation referencing the GOT
4208 for this PLT entry. */
4209
4210 /* S: Current slot number (zero-based). */
4211 s = (h->plt.offset - PLT_ENTRY_SIZE) / PLT_ENTRY_SIZE;
4212 /* K: Number of relocations for PLTResolve. */
4213 if (info->shared)
4214 k = PLTRESOLVE_RELOCS_SHLIB;
4215 else
4216 k = PLTRESOLVE_RELOCS;
4217 /* Skip the PLTresolve relocations, and the relocations for
4218 the other PLT slots. */
4219 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
4220 loc = (htab->srelplt2->contents + reloc_index
4221 * sizeof (Elf32_External_Rel));
4222
6de2ae4a
L
4223 rel.r_offset = (htab->elf.splt->output_section->vma
4224 + htab->elf.splt->output_offset
eac338cf 4225 + h->plt.offset + 2),
7325306f 4226 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
eac338cf
PB
4227 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4228
4229 /* Create the R_386_32 relocation referencing the beginning of
4230 the PLT for this GOT entry. */
6de2ae4a
L
4231 rel.r_offset = (htab->elf.sgotplt->output_section->vma
4232 + htab->elf.sgotplt->output_offset
eac338cf 4233 + got_offset);
7325306f 4234 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
eac338cf
PB
4235 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4236 loc + sizeof (Elf32_External_Rel));
4237 }
252b5132
RH
4238 }
4239 else
4240 {
cbe950e9 4241 memcpy (plt->contents + h->plt.offset, elf_i386_pic_plt_entry,
252b5132
RH
4242 PLT_ENTRY_SIZE);
4243 bfd_put_32 (output_bfd, got_offset,
cbe950e9 4244 plt->contents + h->plt.offset + 2);
252b5132
RH
4245 }
4246
cbe950e9 4247 /* Don't fill PLT entry for static executables. */
6de2ae4a 4248 if (plt == htab->elf.splt)
cbe950e9
L
4249 {
4250 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
4251 plt->contents + h->plt.offset + 7);
4252 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
4253 plt->contents + h->plt.offset + 12);
4254 }
252b5132
RH
4255
4256 /* Fill in the entry in the global offset table. */
4257 bfd_put_32 (output_bfd,
cbe950e9
L
4258 (plt->output_section->vma
4259 + plt->output_offset
252b5132
RH
4260 + h->plt.offset
4261 + 6),
cbe950e9 4262 gotplt->contents + got_offset);
252b5132
RH
4263
4264 /* Fill in the entry in the .rel.plt section. */
cbe950e9
L
4265 rel.r_offset = (gotplt->output_section->vma
4266 + gotplt->output_offset
252b5132 4267 + got_offset);
cbe950e9
L
4268 if (h->dynindx == -1
4269 || ((info->executable
4270 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4271 && h->def_regular
4272 && h->type == STT_GNU_IFUNC))
4273 {
4274 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4275 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
4276 in the .got.plt section. */
4277 bfd_put_32 (output_bfd,
4278 (h->root.u.def.value
4279 + h->root.u.def.section->output_section->vma
4280 + h->root.u.def.section->output_offset),
4281 gotplt->contents + got_offset);
4282 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
4283 }
4284 else
4285 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
4286 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
0ac8d2ca 4287 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
252b5132 4288
f5385ebf 4289 if (!h->def_regular)
252b5132
RH
4290 {
4291 /* Mark the symbol as undefined, rather than as defined in
c6585bbb
JJ
4292 the .plt section. Leave the value if there were any
4293 relocations where pointer equality matters (this is a clue
51b64d56
AM
4294 for the dynamic linker, to make function pointer
4295 comparisons work between an application and shared
c6585bbb
JJ
4296 library), otherwise set it to zero. If a function is only
4297 called from a binary, there is no need to slow down
4298 shared libraries because of that. */
252b5132 4299 sym->st_shndx = SHN_UNDEF;
f5385ebf 4300 if (!h->pointer_equality_needed)
c6585bbb 4301 sym->st_value = 0;
252b5132
RH
4302 }
4303 }
4304
13ae64f3 4305 if (h->got.offset != (bfd_vma) -1
67a4f2b7 4306 && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
37e55690 4307 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
252b5132 4308 {
947216bf
AM
4309 Elf_Internal_Rela rel;
4310 bfd_byte *loc;
252b5132
RH
4311
4312 /* This symbol has an entry in the global offset table. Set it
4313 up. */
4314
6de2ae4a 4315 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
ffb2e45b 4316 abort ();
252b5132 4317
6de2ae4a
L
4318 rel.r_offset = (htab->elf.sgot->output_section->vma
4319 + htab->elf.sgot->output_offset
dc810e39 4320 + (h->got.offset & ~(bfd_vma) 1));
252b5132 4321
dd5724d5
AM
4322 /* If this is a static link, or it is a -Bsymbolic link and the
4323 symbol is defined locally or was forced to be local because
4324 of a version file, we just want to emit a RELATIVE reloc.
252b5132
RH
4325 The entry in the global offset table will already have been
4326 initialized in the relocate_section function. */
710ab287 4327 if (h->def_regular
0018b0a3
L
4328 && h->type == STT_GNU_IFUNC)
4329 {
710ab287
L
4330 if (info->shared)
4331 {
4332 /* Generate R_386_GLOB_DAT. */
4333 goto do_glob_dat;
4334 }
4335 else
4336 {
cd2b2c10
L
4337 asection *plt;
4338
710ab287
L
4339 if (!h->pointer_equality_needed)
4340 abort ();
4341
4342 /* For non-shared object, we can't use .got.plt, which
4343 contains the real function addres if we need pointer
4344 equality. We load the GOT entry with the PLT entry. */
cd2b2c10 4345 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
710ab287
L
4346 bfd_put_32 (output_bfd,
4347 (plt->output_section->vma
4348 + plt->output_offset + h->plt.offset),
6de2ae4a 4349 htab->elf.sgot->contents + h->got.offset);
710ab287
L
4350 return TRUE;
4351 }
0018b0a3
L
4352 }
4353 else if (info->shared
4354 && SYMBOL_REFERENCES_LOCAL (info, h))
dd5724d5 4355 {
6725bdbf 4356 BFD_ASSERT((h->got.offset & 1) != 0);
dd5724d5
AM
4357 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4358 }
252b5132
RH
4359 else
4360 {
dd5724d5 4361 BFD_ASSERT((h->got.offset & 1) == 0);
710ab287 4362do_glob_dat:
6725bdbf 4363 bfd_put_32 (output_bfd, (bfd_vma) 0,
6de2ae4a 4364 htab->elf.sgot->contents + h->got.offset);
252b5132
RH
4365 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
4366 }
4367
6de2ae4a
L
4368 loc = htab->elf.srelgot->contents;
4369 loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
0ac8d2ca 4370 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
252b5132
RH
4371 }
4372
f5385ebf 4373 if (h->needs_copy)
252b5132 4374 {
947216bf
AM
4375 Elf_Internal_Rela rel;
4376 bfd_byte *loc;
252b5132
RH
4377
4378 /* This symbol needs a copy reloc. Set it up. */
4379
ffb2e45b
AM
4380 if (h->dynindx == -1
4381 || (h->root.type != bfd_link_hash_defined
4382 && h->root.type != bfd_link_hash_defweak)
4383 || htab->srelbss == NULL)
4384 abort ();
252b5132
RH
4385
4386 rel.r_offset = (h->root.u.def.value
4387 + h->root.u.def.section->output_section->vma
4388 + h->root.u.def.section->output_offset);
4389 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
947216bf
AM
4390 loc = htab->srelbss->contents;
4391 loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rel);
0ac8d2ca 4392 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
252b5132
RH
4393 }
4394
c25bc9fc
L
4395 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
4396 be NULL for local symbols.
4397
eac338cf
PB
4398 On VxWorks, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it
4399 is relative to the ".got" section. */
c25bc9fc
L
4400 if (sym != NULL
4401 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
4402 || (!htab->is_vxworks && h == htab->elf.hgot)))
252b5132
RH
4403 sym->st_shndx = SHN_ABS;
4404
b34976b6 4405 return TRUE;
252b5132
RH
4406}
4407
c25bc9fc
L
4408/* Finish up local dynamic symbol handling. We set the contents of
4409 various dynamic sections here. */
4410
4411static bfd_boolean
4412elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
4413{
4414 struct elf_link_hash_entry *h
4415 = (struct elf_link_hash_entry *) *slot;
4416 struct bfd_link_info *info
4417 = (struct bfd_link_info *) inf;
4418
4419 return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
4420 h, NULL);
4421}
4422
38701953
AM
4423/* Used to decide how to sort relocs in an optimal manner for the
4424 dynamic linker, before writing them out. */
4425
4426static enum elf_reloc_type_class
55fd94b0 4427elf_i386_reloc_type_class (const Elf_Internal_Rela *rela)
38701953 4428{
55fd94b0 4429 switch (ELF32_R_TYPE (rela->r_info))
38701953
AM
4430 {
4431 case R_386_RELATIVE:
4432 return reloc_class_relative;
4433 case R_386_JUMP_SLOT:
4434 return reloc_class_plt;
4435 case R_386_COPY:
4436 return reloc_class_copy;
4437 default:
4438 return reloc_class_normal;
4439 }
4440}
4441
252b5132
RH
4442/* Finish up the dynamic sections. */
4443
b34976b6 4444static bfd_boolean
55fd94b0
AM
4445elf_i386_finish_dynamic_sections (bfd *output_bfd,
4446 struct bfd_link_info *info)
252b5132 4447{
6725bdbf 4448 struct elf_i386_link_hash_table *htab;
252b5132 4449 bfd *dynobj;
252b5132
RH
4450 asection *sdyn;
4451
6725bdbf 4452 htab = elf_i386_hash_table (info);
4dfe6ac6
NC
4453 if (htab == NULL)
4454 return FALSE;
4455
ebe50bae 4456 dynobj = htab->elf.dynobj;
252b5132
RH
4457 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4458
ebe50bae 4459 if (htab->elf.dynamic_sections_created)
252b5132 4460 {
252b5132
RH
4461 Elf32_External_Dyn *dyncon, *dynconend;
4462
6de2ae4a 4463 if (sdyn == NULL || htab->elf.sgot == NULL)
ffb2e45b 4464 abort ();
252b5132
RH
4465
4466 dyncon = (Elf32_External_Dyn *) sdyn->contents;
eea6121a 4467 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
252b5132
RH
4468 for (; dyncon < dynconend; dyncon++)
4469 {
4470 Elf_Internal_Dyn dyn;
51b64d56 4471 asection *s;
252b5132
RH
4472
4473 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4474
4475 switch (dyn.d_tag)
4476 {
4477 default:
7a2b07ff
NS
4478 if (htab->is_vxworks
4479 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
4480 break;
0ac8d2ca 4481 continue;
252b5132
RH
4482
4483 case DT_PLTGOT:
6de2ae4a 4484 s = htab->elf.sgotplt;
8c37241b 4485 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6725bdbf
AM
4486 break;
4487
252b5132 4488 case DT_JMPREL:
6de2ae4a 4489 s = htab->elf.srelplt;
6348e046 4490 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
252b5132
RH
4491 break;
4492
4493 case DT_PLTRELSZ:
6de2ae4a 4494 s = htab->elf.srelplt;
eea6121a 4495 dyn.d_un.d_val = s->size;
252b5132
RH
4496 break;
4497
4498 case DT_RELSZ:
4499 /* My reading of the SVR4 ABI indicates that the
4500 procedure linkage table relocs (DT_JMPREL) should be
4501 included in the overall relocs (DT_REL). This is
4502 what Solaris does. However, UnixWare can not handle
4503 that case. Therefore, we override the DT_RELSZ entry
6348e046 4504 here to make it not include the JMPREL relocs. */
6de2ae4a 4505 s = htab->elf.srelplt;
6348e046
AM
4506 if (s == NULL)
4507 continue;
eea6121a 4508 dyn.d_un.d_val -= s->size;
6348e046
AM
4509 break;
4510
4511 case DT_REL:
4512 /* We may not be using the standard ELF linker script.
4513 If .rel.plt is the first .rel section, we adjust
4514 DT_REL to not include it. */
6de2ae4a 4515 s = htab->elf.srelplt;
6348e046
AM
4516 if (s == NULL)
4517 continue;
4518 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
4519 continue;
eea6121a 4520 dyn.d_un.d_ptr += s->size;
252b5132
RH
4521 break;
4522 }
0ac8d2ca
AM
4523
4524 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
252b5132
RH
4525 }
4526
4527 /* Fill in the first entry in the procedure linkage table. */
6de2ae4a 4528 if (htab->elf.splt && htab->elf.splt->size > 0)
252b5132
RH
4529 {
4530 if (info->shared)
eac338cf 4531 {
6de2ae4a 4532 memcpy (htab->elf.splt->contents, elf_i386_pic_plt0_entry,
eac338cf 4533 sizeof (elf_i386_pic_plt0_entry));
6de2ae4a 4534 memset (htab->elf.splt->contents + sizeof (elf_i386_pic_plt0_entry),
eac338cf
PB
4535 htab->plt0_pad_byte,
4536 PLT_ENTRY_SIZE - sizeof (elf_i386_pic_plt0_entry));
4537 }
252b5132
RH
4538 else
4539 {
6de2ae4a 4540 memcpy (htab->elf.splt->contents, elf_i386_plt0_entry,
eac338cf 4541 sizeof(elf_i386_plt0_entry));
6de2ae4a 4542 memset (htab->elf.splt->contents + sizeof (elf_i386_plt0_entry),
eac338cf
PB
4543 htab->plt0_pad_byte,
4544 PLT_ENTRY_SIZE - sizeof (elf_i386_plt0_entry));
252b5132 4545 bfd_put_32 (output_bfd,
6de2ae4a
L
4546 (htab->elf.sgotplt->output_section->vma
4547 + htab->elf.sgotplt->output_offset
6725bdbf 4548 + 4),
6de2ae4a 4549 htab->elf.splt->contents + 2);
252b5132 4550 bfd_put_32 (output_bfd,
6de2ae4a
L
4551 (htab->elf.sgotplt->output_section->vma
4552 + htab->elf.sgotplt->output_offset
6725bdbf 4553 + 8),
6de2ae4a 4554 htab->elf.splt->contents + 8);
eac338cf
PB
4555
4556 if (htab->is_vxworks)
4557 {
4558 Elf_Internal_Rela rel;
7325306f 4559
eac338cf
PB
4560 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
4561 On IA32 we use REL relocations so the addend goes in
4562 the PLT directly. */
6de2ae4a
L
4563 rel.r_offset = (htab->elf.splt->output_section->vma
4564 + htab->elf.splt->output_offset
eac338cf 4565 + 2);
7325306f 4566 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
eac338cf
PB
4567 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4568 htab->srelplt2->contents);
4569 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
6de2ae4a
L
4570 rel.r_offset = (htab->elf.splt->output_section->vma
4571 + htab->elf.splt->output_offset
eac338cf 4572 + 8);
7325306f 4573 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
eac338cf
PB
4574 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4575 htab->srelplt2->contents +
4576 sizeof (Elf32_External_Rel));
4577 }
252b5132
RH
4578 }
4579
4580 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4581 really seem like the right value. */
6de2ae4a 4582 elf_section_data (htab->elf.splt->output_section)
6725bdbf 4583 ->this_hdr.sh_entsize = 4;
eac338cf
PB
4584
4585 /* Correct the .rel.plt.unloaded relocations. */
4586 if (htab->is_vxworks && !info->shared)
4587 {
6de2ae4a 4588 int num_plts = (htab->elf.splt->size / PLT_ENTRY_SIZE) - 1;
4c9b0de6 4589 unsigned char *p;
eac338cf
PB
4590
4591 p = htab->srelplt2->contents;
4592 if (info->shared)
4593 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4594 else
4595 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4596
4597 for (; num_plts; num_plts--)
4598 {
4599 Elf_Internal_Rela rel;
4600 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
7325306f 4601 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
eac338cf
PB
4602 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4603 p += sizeof (Elf32_External_Rel);
4604
4605 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
7325306f 4606 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
eac338cf
PB
4607 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4608 p += sizeof (Elf32_External_Rel);
4609 }
4610 }
252b5132
RH
4611 }
4612 }
4613
6de2ae4a 4614 if (htab->elf.sgotplt)
252b5132 4615 {
12d0ee4a 4616 /* Fill in the first three entries in the global offset table. */
6de2ae4a 4617 if (htab->elf.sgotplt->size > 0)
12d0ee4a
AM
4618 {
4619 bfd_put_32 (output_bfd,
55fd94b0 4620 (sdyn == NULL ? 0
12d0ee4a 4621 : sdyn->output_section->vma + sdyn->output_offset),
6de2ae4a
L
4622 htab->elf.sgotplt->contents);
4623 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
4624 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
12d0ee4a 4625 }
252b5132 4626
6de2ae4a 4627 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
12d0ee4a 4628 }
8c37241b 4629
6de2ae4a
L
4630 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4631 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
8c37241b 4632
c25bc9fc
L
4633 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4634 htab_traverse (htab->loc_hash_table,
4635 elf_i386_finish_local_dynamic_symbol,
4636 info);
4637
b34976b6 4638 return TRUE;
252b5132
RH
4639}
4640
4c45e5c9
JJ
4641/* Return address for Ith PLT stub in section PLT, for relocation REL
4642 or (bfd_vma) -1 if it should not be included. */
4643
4644static bfd_vma
4645elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
4646 const arelent *rel ATTRIBUTE_UNUSED)
4647{
4648 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4649}
4650
fdc90cb4
JJ
4651/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4652
4653static bfd_boolean
4654elf_i386_hash_symbol (struct elf_link_hash_entry *h)
4655{
4656 if (h->plt.offset != (bfd_vma) -1
4657 && !h->def_regular
4658 && !h->pointer_equality_needed)
4659 return FALSE;
4660
4661 return _bfd_elf_hash_symbol (h);
4662}
4c45e5c9 4663
d8045f23
NC
4664/* Hook called by the linker routine which adds symbols from an object
4665 file. */
4666
4667static bfd_boolean
c16153ae 4668elf_i386_add_symbol_hook (bfd * abfd,
d8045f23
NC
4669 struct bfd_link_info * info ATTRIBUTE_UNUSED,
4670 Elf_Internal_Sym * sym,
4671 const char ** namep ATTRIBUTE_UNUSED,
4672 flagword * flagsp ATTRIBUTE_UNUSED,
4673 asection ** secp ATTRIBUTE_UNUSED,
4674 bfd_vma * valp ATTRIBUTE_UNUSED)
4675{
c16153ae
L
4676 if ((abfd->flags & DYNAMIC) == 0
4677 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
d8045f23
NC
4678 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4679
4680 return TRUE;
4681}
4682
252b5132
RH
4683#define TARGET_LITTLE_SYM bfd_elf32_i386_vec
4684#define TARGET_LITTLE_NAME "elf32-i386"
4685#define ELF_ARCH bfd_arch_i386
4686#define ELF_MACHINE_CODE EM_386
4687#define ELF_MAXPAGESIZE 0x1000
252b5132
RH
4688
4689#define elf_backend_can_gc_sections 1
51b64d56 4690#define elf_backend_can_refcount 1
252b5132
RH
4691#define elf_backend_want_got_plt 1
4692#define elf_backend_plt_readonly 1
4693#define elf_backend_want_plt_sym 0
4694#define elf_backend_got_header_size 12
252b5132 4695
8c29f035
AM
4696/* Support RELA for objdump of prelink objects. */
4697#define elf_info_to_howto elf_i386_info_to_howto_rel
dd5724d5
AM
4698#define elf_info_to_howto_rel elf_i386_info_to_howto_rel
4699
13ae64f3 4700#define bfd_elf32_mkobject elf_i386_mkobject
13ae64f3 4701
dd5724d5
AM
4702#define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
4703#define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
c25bc9fc 4704#define bfd_elf32_bfd_link_hash_table_free elf_i386_link_hash_table_free
dd5724d5 4705#define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
13285a1b 4706#define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
dd5724d5
AM
4707
4708#define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
13285a1b 4709#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
dd5724d5 4710#define elf_backend_check_relocs elf_i386_check_relocs
0ac8d2ca 4711#define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
6725bdbf 4712#define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
0ac8d2ca 4713#define elf_backend_fake_sections elf_i386_fake_sections
dd5724d5
AM
4714#define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
4715#define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
4716#define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
4717#define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
c5fccbec
DJ
4718#define elf_backend_grok_prstatus elf_i386_grok_prstatus
4719#define elf_backend_grok_psinfo elf_i386_grok_psinfo
db6751f2 4720#define elf_backend_reloc_type_class elf_i386_reloc_type_class
0ac8d2ca
AM
4721#define elf_backend_relocate_section elf_i386_relocate_section
4722#define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
67a4f2b7 4723#define elf_backend_always_size_sections elf_i386_always_size_sections
74541ad4
AM
4724#define elf_backend_omit_section_dynsym \
4725 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4c45e5c9 4726#define elf_backend_plt_sym_val elf_i386_plt_sym_val
fdc90cb4 4727#define elf_backend_hash_symbol elf_i386_hash_symbol
d8045f23
NC
4728#define elf_backend_add_symbol_hook elf_i386_add_symbol_hook
4729#undef elf_backend_post_process_headers
4730#define elf_backend_post_process_headers _bfd_elf_set_osabi
dd5724d5 4731
252b5132 4732#include "elf32-target.h"
2bc3c89a
AM
4733
4734/* FreeBSD support. */
4735
4736#undef TARGET_LITTLE_SYM
4737#define TARGET_LITTLE_SYM bfd_elf32_i386_freebsd_vec
4738#undef TARGET_LITTLE_NAME
4739#define TARGET_LITTLE_NAME "elf32-i386-freebsd"
d1036acb
L
4740#undef ELF_OSABI
4741#define ELF_OSABI ELFOSABI_FREEBSD
2bc3c89a
AM
4742
4743/* The kernel recognizes executables as valid only if they carry a
4744 "FreeBSD" label in the ELF header. So we put this label on all
4745 executables and (for simplicity) also all other object files. */
4746
2bc3c89a 4747static void
d8045f23 4748elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
2bc3c89a 4749{
d8045f23 4750 _bfd_elf_set_osabi (abfd, info);
2bc3c89a 4751
2bc3c89a
AM
4752#ifdef OLD_FREEBSD_ABI_LABEL
4753 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
4754 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
caf47ea6 4755#endif
2bc3c89a
AM
4756}
4757
4758#undef elf_backend_post_process_headers
d8045f23 4759#define elf_backend_post_process_headers elf_i386_fbsd_post_process_headers
571fe01f
NC
4760#undef elf32_bed
4761#define elf32_bed elf32_i386_fbsd_bed
2bc3c89a 4762
d8045f23
NC
4763#undef elf_backend_add_symbol_hook
4764
2bc3c89a 4765#include "elf32-target.h"
eac338cf 4766
a6cc6b3b
RO
4767/* Solaris 2. */
4768
4769#undef TARGET_LITTLE_SYM
4770#define TARGET_LITTLE_SYM bfd_elf32_i386_sol2_vec
4771#undef TARGET_LITTLE_NAME
4772#define TARGET_LITTLE_NAME "elf32-i386-sol2"
4773
4774/* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4775 objects won't be recognized. */
4776#undef ELF_OSABI
4777
4778#undef elf32_bed
4779#define elf32_bed elf32_i386_sol2_bed
4780
4781/* The Solaris 2 ABI requires a plt symbol on all platforms.
4782
4783 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4784 File, p.63. */
4785#undef elf_backend_want_plt_sym
4786#define elf_backend_want_plt_sym 1
4787
4788#include "elf32-target.h"
4789
eac338cf
PB
4790/* VxWorks support. */
4791
4792#undef TARGET_LITTLE_SYM
4793#define TARGET_LITTLE_SYM bfd_elf32_i386_vxworks_vec
4794#undef TARGET_LITTLE_NAME
4795#define TARGET_LITTLE_NAME "elf32-i386-vxworks"
d1036acb 4796#undef ELF_OSABI
eac338cf
PB
4797
4798/* Like elf_i386_link_hash_table_create but with tweaks for VxWorks. */
4799
4800static struct bfd_link_hash_table *
4801elf_i386_vxworks_link_hash_table_create (bfd *abfd)
4802{
4803 struct bfd_link_hash_table *ret;
4804 struct elf_i386_link_hash_table *htab;
4805
4806 ret = elf_i386_link_hash_table_create (abfd);
4807 if (ret)
4808 {
4809 htab = (struct elf_i386_link_hash_table *) ret;
4810 htab->is_vxworks = 1;
4811 htab->plt0_pad_byte = 0x90;
4812 }
4813
4814 return ret;
4815}
4816
4817
13285a1b
AM
4818#undef elf_backend_relocs_compatible
4819#undef elf_backend_post_process_headers
eac338cf
PB
4820#undef bfd_elf32_bfd_link_hash_table_create
4821#define bfd_elf32_bfd_link_hash_table_create \
4822 elf_i386_vxworks_link_hash_table_create
4823#undef elf_backend_add_symbol_hook
4824#define elf_backend_add_symbol_hook \
4825 elf_vxworks_add_symbol_hook
4826#undef elf_backend_link_output_symbol_hook
4827#define elf_backend_link_output_symbol_hook \
9c72ff84 4828 elf_vxworks_link_output_symbol_hook
eac338cf
PB
4829#undef elf_backend_emit_relocs
4830#define elf_backend_emit_relocs elf_vxworks_emit_relocs
4831#undef elf_backend_final_write_processing
4832#define elf_backend_final_write_processing \
4833 elf_vxworks_final_write_processing
4834
4835/* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
4836 define it. */
4837#undef elf_backend_want_plt_sym
4838#define elf_backend_want_plt_sym 1
4839
4840#undef elf32_bed
4841#define elf32_bed elf32_i386_vxworks_bed
4842
4843#include "elf32-target.h"
This page took 1.82731 seconds and 4 git commands to generate.