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