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