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