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