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