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