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