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