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