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