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