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