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