Skip gdb.mi/mi-threads-interrupt.exp if nointerrupts.
[deliverable/binutils-gdb.git] / bfd / elf32-i386.c
CommitLineData
252b5132 1/* Intel 80386/80486-specific support for 32-bit ELF
2571583a 2 Copyright (C) 1993-2017 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
0afcef53 21#include "elfxx-x86.h"
5a68afcf 22#include "elf-nacl.h"
eac338cf 23#include "elf-vxworks.h"
e41b3a13 24#include "dwarf2.h"
02a86693 25#include "opcode/i386.h"
252b5132 26
55fd94b0
AM
27/* 386 uses REL relocations instead of RELA. */
28#define USE_REL 1
252b5132
RH
29
30#include "elf/i386.h"
31
32static reloc_howto_type elf_howto_table[]=
33{
6346d5ca 34 HOWTO(R_386_NONE, 0, 3, 0, FALSE, 0, complain_overflow_dont,
1b452ec6 35 bfd_elf_generic_reloc, "R_386_NONE",
b34976b6
AM
36 TRUE, 0x00000000, 0x00000000, FALSE),
37 HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
1b452ec6 38 bfd_elf_generic_reloc, "R_386_32",
b34976b6
AM
39 TRUE, 0xffffffff, 0xffffffff, FALSE),
40 HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
1b452ec6 41 bfd_elf_generic_reloc, "R_386_PC32",
b34976b6
AM
42 TRUE, 0xffffffff, 0xffffffff, TRUE),
43 HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
1b452ec6 44 bfd_elf_generic_reloc, "R_386_GOT32",
b34976b6
AM
45 TRUE, 0xffffffff, 0xffffffff, FALSE),
46 HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
1b452ec6 47 bfd_elf_generic_reloc, "R_386_PLT32",
b34976b6
AM
48 TRUE, 0xffffffff, 0xffffffff, TRUE),
49 HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
1b452ec6 50 bfd_elf_generic_reloc, "R_386_COPY",
b34976b6
AM
51 TRUE, 0xffffffff, 0xffffffff, FALSE),
52 HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
1b452ec6 53 bfd_elf_generic_reloc, "R_386_GLOB_DAT",
b34976b6
AM
54 TRUE, 0xffffffff, 0xffffffff, FALSE),
55 HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
1b452ec6 56 bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
b34976b6
AM
57 TRUE, 0xffffffff, 0xffffffff, FALSE),
58 HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
1b452ec6 59 bfd_elf_generic_reloc, "R_386_RELATIVE",
b34976b6
AM
60 TRUE, 0xffffffff, 0xffffffff, FALSE),
61 HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
1b452ec6 62 bfd_elf_generic_reloc, "R_386_GOTOFF",
b34976b6
AM
63 TRUE, 0xffffffff, 0xffffffff, FALSE),
64 HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
1b452ec6 65 bfd_elf_generic_reloc, "R_386_GOTPC",
b34976b6 66 TRUE, 0xffffffff, 0xffffffff, TRUE),
1b452ec6 67
dc47f327
AM
68 /* We have a gap in the reloc numbers here.
69 R_386_standard counts the number up to this point, and
70 R_386_ext_offset is the value to subtract from a reloc type of
71 R_386_16 thru R_386_PC8 to form an index into this table. */
55fd94b0
AM
72#define R_386_standard (R_386_GOTPC + 1)
73#define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
1b452ec6 74
37e55690 75 /* These relocs are a GNU extension. */
b34976b6 76 HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
37e55690 77 bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
b34976b6
AM
78 TRUE, 0xffffffff, 0xffffffff, FALSE),
79 HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
37e55690 80 bfd_elf_generic_reloc, "R_386_TLS_IE",
b34976b6
AM
81 TRUE, 0xffffffff, 0xffffffff, FALSE),
82 HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
37e55690 83 bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
b34976b6
AM
84 TRUE, 0xffffffff, 0xffffffff, FALSE),
85 HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 86 bfd_elf_generic_reloc, "R_386_TLS_LE",
b34976b6
AM
87 TRUE, 0xffffffff, 0xffffffff, FALSE),
88 HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 89 bfd_elf_generic_reloc, "R_386_TLS_GD",
b34976b6
AM
90 TRUE, 0xffffffff, 0xffffffff, FALSE),
91 HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 92 bfd_elf_generic_reloc, "R_386_TLS_LDM",
b34976b6
AM
93 TRUE, 0xffffffff, 0xffffffff, FALSE),
94 HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
1b452ec6 95 bfd_elf_generic_reloc, "R_386_16",
b34976b6 96 TRUE, 0xffff, 0xffff, FALSE),
b0360d8c 97 HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
1b452ec6 98 bfd_elf_generic_reloc, "R_386_PC16",
b34976b6
AM
99 TRUE, 0xffff, 0xffff, TRUE),
100 HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
1b452ec6 101 bfd_elf_generic_reloc, "R_386_8",
b34976b6
AM
102 TRUE, 0xff, 0xff, FALSE),
103 HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
1b452ec6 104 bfd_elf_generic_reloc, "R_386_PC8",
b34976b6 105 TRUE, 0xff, 0xff, TRUE),
dc47f327 106
55fd94b0
AM
107#define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
108#define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
13ae64f3 109 /* These are common with Solaris TLS implementation. */
b34976b6 110 HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 111 bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
b34976b6
AM
112 TRUE, 0xffffffff, 0xffffffff, FALSE),
113 HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 114 bfd_elf_generic_reloc, "R_386_TLS_IE_32",
b34976b6
AM
115 TRUE, 0xffffffff, 0xffffffff, FALSE),
116 HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 117 bfd_elf_generic_reloc, "R_386_TLS_LE_32",
b34976b6
AM
118 TRUE, 0xffffffff, 0xffffffff, FALSE),
119 HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 120 bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
b34976b6
AM
121 TRUE, 0xffffffff, 0xffffffff, FALSE),
122 HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 123 bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
b34976b6
AM
124 TRUE, 0xffffffff, 0xffffffff, FALSE),
125 HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 126 bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
b34976b6 127 TRUE, 0xffffffff, 0xffffffff, FALSE),
1788fc08
L
128 HOWTO(R_386_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
129 bfd_elf_generic_reloc, "R_386_SIZE32",
130 TRUE, 0xffffffff, 0xffffffff, FALSE),
67a4f2b7
AO
131 HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
132 bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
133 TRUE, 0xffffffff, 0xffffffff, FALSE),
134 HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
135 bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
136 FALSE, 0, 0, FALSE),
137 HOWTO(R_386_TLS_DESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
138 bfd_elf_generic_reloc, "R_386_TLS_DESC",
139 TRUE, 0xffffffff, 0xffffffff, FALSE),
cbe950e9
L
140 HOWTO(R_386_IRELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
141 bfd_elf_generic_reloc, "R_386_IRELATIVE",
142 TRUE, 0xffffffff, 0xffffffff, FALSE),
02a86693
L
143 HOWTO(R_386_GOT32X, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
144 bfd_elf_generic_reloc, "R_386_GOT32X",
145 TRUE, 0xffffffff, 0xffffffff, FALSE),
13ae64f3
JJ
146
147 /* Another gap. */
02a86693 148#define R_386_ext2 (R_386_GOT32X + 1 - R_386_tls_offset)
c74be520 149#define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_ext2)
252b5132
RH
150
151/* GNU extension to record C++ vtable hierarchy. */
252b5132
RH
152 HOWTO (R_386_GNU_VTINHERIT, /* type */
153 0, /* rightshift */
154 2, /* size (0 = byte, 1 = short, 2 = long) */
155 0, /* bitsize */
b34976b6 156 FALSE, /* pc_relative */
252b5132
RH
157 0, /* bitpos */
158 complain_overflow_dont, /* complain_on_overflow */
159 NULL, /* special_function */
160 "R_386_GNU_VTINHERIT", /* name */
b34976b6 161 FALSE, /* partial_inplace */
252b5132
RH
162 0, /* src_mask */
163 0, /* dst_mask */
b34976b6 164 FALSE), /* pcrel_offset */
252b5132
RH
165
166/* GNU extension to record C++ vtable member usage. */
252b5132
RH
167 HOWTO (R_386_GNU_VTENTRY, /* type */
168 0, /* rightshift */
169 2, /* size (0 = byte, 1 = short, 2 = long) */
170 0, /* bitsize */
b34976b6 171 FALSE, /* pc_relative */
252b5132
RH
172 0, /* bitpos */
173 complain_overflow_dont, /* complain_on_overflow */
174 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
175 "R_386_GNU_VTENTRY", /* name */
b34976b6 176 FALSE, /* partial_inplace */
252b5132
RH
177 0, /* src_mask */
178 0, /* dst_mask */
b34976b6 179 FALSE) /* pcrel_offset */
dc47f327 180
55fd94b0 181#define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
dc47f327
AM
182
183};
184
daf1c414
L
185#define X86_PCREL_TYPE_P(TYPE) ((TYPE) == R_386_PC32)
186
aebcc8ff
L
187#define X86_SIZE_TYPE_P(TYPE) ((TYPE) == R_386_SIZE32)
188
252b5132 189#ifdef DEBUG_GEN_RELOC
55fd94b0
AM
190#define TRACE(str) \
191 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
252b5132
RH
192#else
193#define TRACE(str)
194#endif
195
196static reloc_howto_type *
55fd94b0
AM
197elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
198 bfd_reloc_code_real_type code)
252b5132
RH
199{
200 switch (code)
201 {
202 case BFD_RELOC_NONE:
203 TRACE ("BFD_RELOC_NONE");
55fd94b0 204 return &elf_howto_table[R_386_NONE];
252b5132
RH
205
206 case BFD_RELOC_32:
207 TRACE ("BFD_RELOC_32");
55fd94b0 208 return &elf_howto_table[R_386_32];
252b5132
RH
209
210 case BFD_RELOC_CTOR:
211 TRACE ("BFD_RELOC_CTOR");
55fd94b0 212 return &elf_howto_table[R_386_32];
252b5132
RH
213
214 case BFD_RELOC_32_PCREL:
215 TRACE ("BFD_RELOC_PC32");
55fd94b0 216 return &elf_howto_table[R_386_PC32];
252b5132
RH
217
218 case BFD_RELOC_386_GOT32:
219 TRACE ("BFD_RELOC_386_GOT32");
55fd94b0 220 return &elf_howto_table[R_386_GOT32];
252b5132
RH
221
222 case BFD_RELOC_386_PLT32:
223 TRACE ("BFD_RELOC_386_PLT32");
55fd94b0 224 return &elf_howto_table[R_386_PLT32];
252b5132
RH
225
226 case BFD_RELOC_386_COPY:
227 TRACE ("BFD_RELOC_386_COPY");
55fd94b0 228 return &elf_howto_table[R_386_COPY];
252b5132
RH
229
230 case BFD_RELOC_386_GLOB_DAT:
231 TRACE ("BFD_RELOC_386_GLOB_DAT");
55fd94b0 232 return &elf_howto_table[R_386_GLOB_DAT];
252b5132
RH
233
234 case BFD_RELOC_386_JUMP_SLOT:
235 TRACE ("BFD_RELOC_386_JUMP_SLOT");
55fd94b0 236 return &elf_howto_table[R_386_JUMP_SLOT];
252b5132
RH
237
238 case BFD_RELOC_386_RELATIVE:
239 TRACE ("BFD_RELOC_386_RELATIVE");
55fd94b0 240 return &elf_howto_table[R_386_RELATIVE];
252b5132
RH
241
242 case BFD_RELOC_386_GOTOFF:
243 TRACE ("BFD_RELOC_386_GOTOFF");
55fd94b0 244 return &elf_howto_table[R_386_GOTOFF];
252b5132
RH
245
246 case BFD_RELOC_386_GOTPC:
247 TRACE ("BFD_RELOC_386_GOTPC");
55fd94b0 248 return &elf_howto_table[R_386_GOTPC];
252b5132 249
37e55690
JJ
250 /* These relocs are a GNU extension. */
251 case BFD_RELOC_386_TLS_TPOFF:
252 TRACE ("BFD_RELOC_386_TLS_TPOFF");
55fd94b0 253 return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
37e55690
JJ
254
255 case BFD_RELOC_386_TLS_IE:
256 TRACE ("BFD_RELOC_386_TLS_IE");
55fd94b0 257 return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
37e55690
JJ
258
259 case BFD_RELOC_386_TLS_GOTIE:
260 TRACE ("BFD_RELOC_386_TLS_GOTIE");
55fd94b0 261 return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
37e55690 262
13ae64f3
JJ
263 case BFD_RELOC_386_TLS_LE:
264 TRACE ("BFD_RELOC_386_TLS_LE");
55fd94b0 265 return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
13ae64f3
JJ
266
267 case BFD_RELOC_386_TLS_GD:
268 TRACE ("BFD_RELOC_386_TLS_GD");
55fd94b0 269 return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
13ae64f3
JJ
270
271 case BFD_RELOC_386_TLS_LDM:
272 TRACE ("BFD_RELOC_386_TLS_LDM");
55fd94b0 273 return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
13ae64f3 274
252b5132
RH
275 case BFD_RELOC_16:
276 TRACE ("BFD_RELOC_16");
55fd94b0 277 return &elf_howto_table[R_386_16 - R_386_ext_offset];
252b5132
RH
278
279 case BFD_RELOC_16_PCREL:
280 TRACE ("BFD_RELOC_16_PCREL");
55fd94b0 281 return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
252b5132
RH
282
283 case BFD_RELOC_8:
284 TRACE ("BFD_RELOC_8");
55fd94b0 285 return &elf_howto_table[R_386_8 - R_386_ext_offset];
252b5132
RH
286
287 case BFD_RELOC_8_PCREL:
288 TRACE ("BFD_RELOC_8_PCREL");
55fd94b0 289 return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
252b5132 290
13ae64f3
JJ
291 /* Common with Sun TLS implementation. */
292 case BFD_RELOC_386_TLS_LDO_32:
293 TRACE ("BFD_RELOC_386_TLS_LDO_32");
55fd94b0 294 return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
13ae64f3
JJ
295
296 case BFD_RELOC_386_TLS_IE_32:
297 TRACE ("BFD_RELOC_386_TLS_IE_32");
55fd94b0 298 return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
13ae64f3
JJ
299
300 case BFD_RELOC_386_TLS_LE_32:
301 TRACE ("BFD_RELOC_386_TLS_LE_32");
55fd94b0 302 return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
13ae64f3
JJ
303
304 case BFD_RELOC_386_TLS_DTPMOD32:
305 TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
55fd94b0 306 return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
13ae64f3
JJ
307
308 case BFD_RELOC_386_TLS_DTPOFF32:
309 TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
55fd94b0 310 return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
13ae64f3
JJ
311
312 case BFD_RELOC_386_TLS_TPOFF32:
313 TRACE ("BFD_RELOC_386_TLS_TPOFF32");
55fd94b0 314 return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
13ae64f3 315
1788fc08
L
316 case BFD_RELOC_SIZE32:
317 TRACE ("BFD_RELOC_SIZE32");
318 return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset];
319
67a4f2b7
AO
320 case BFD_RELOC_386_TLS_GOTDESC:
321 TRACE ("BFD_RELOC_386_TLS_GOTDESC");
322 return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
323
324 case BFD_RELOC_386_TLS_DESC_CALL:
325 TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
326 return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
327
328 case BFD_RELOC_386_TLS_DESC:
329 TRACE ("BFD_RELOC_386_TLS_DESC");
330 return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
331
cbe950e9
L
332 case BFD_RELOC_386_IRELATIVE:
333 TRACE ("BFD_RELOC_386_IRELATIVE");
2a750708 334 return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset];
cbe950e9 335
02a86693
L
336 case BFD_RELOC_386_GOT32X:
337 TRACE ("BFD_RELOC_386_GOT32X");
338 return &elf_howto_table[R_386_GOT32X - R_386_tls_offset];
339
252b5132
RH
340 case BFD_RELOC_VTABLE_INHERIT:
341 TRACE ("BFD_RELOC_VTABLE_INHERIT");
55fd94b0 342 return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
252b5132
RH
343
344 case BFD_RELOC_VTABLE_ENTRY:
345 TRACE ("BFD_RELOC_VTABLE_ENTRY");
55fd94b0 346 return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
252b5132
RH
347
348 default:
349 break;
350 }
351
352 TRACE ("Unknown");
353 return 0;
354}
355
157090f7
AM
356static reloc_howto_type *
357elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
358 const char *r_name)
359{
360 unsigned int i;
361
362 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
363 if (elf_howto_table[i].name != NULL
364 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
365 return &elf_howto_table[i];
366
367 return NULL;
368}
369
142411ca
L
370static reloc_howto_type *
371elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
252b5132 372{
dc47f327
AM
373 unsigned int indx;
374
375 if ((indx = r_type) >= R_386_standard
376 && ((indx = r_type - R_386_ext_offset) - R_386_standard
377 >= R_386_ext - R_386_standard)
13ae64f3 378 && ((indx = r_type - R_386_tls_offset) - R_386_ext
c74be520
L
379 >= R_386_ext2 - R_386_ext)
380 && ((indx = r_type - R_386_vt_offset) - R_386_ext2
381 >= R_386_vt - R_386_ext2))
252b5132 382 {
695344c0 383 /* xgettext:c-format */
4eca0228
AM
384 _bfd_error_handler (_("%B: invalid relocation type %d"),
385 abfd, (int) r_type);
55fd94b0 386 indx = R_386_NONE;
252b5132 387 }
06614111
NC
388 /* PR 17512: file: 0f67f69d. */
389 if (elf_howto_table [indx].type != r_type)
390 return NULL;
142411ca
L
391 return &elf_howto_table[indx];
392}
393
394static void
395elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
396 arelent *cache_ptr,
397 Elf_Internal_Rela *dst)
398{
399 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
400 cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type);
252b5132
RH
401}
402
403/* Return whether a symbol name implies a local label. The UnixWare
404 2.1 cc generates temporary symbols that start with .X, so we
405 recognize them here. FIXME: do other SVR4 compilers also use .X?.
406 If so, we should move the .X recognition into
407 _bfd_elf_is_local_label_name. */
408
b34976b6 409static bfd_boolean
55fd94b0 410elf_i386_is_local_label_name (bfd *abfd, const char *name)
252b5132
RH
411{
412 if (name[0] == '.' && name[1] == 'X')
b34976b6 413 return TRUE;
252b5132
RH
414
415 return _bfd_elf_is_local_label_name (abfd, name);
416}
417\f
38701953 418/* Support for core dump NOTE sections. */
61adc1a4 419
b34976b6 420static bfd_boolean
55fd94b0 421elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
38701953
AM
422{
423 int offset;
eea6121a 424 size_t size;
38701953 425
61adc1a4 426 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
38701953 427 {
61adc1a4
NC
428 int pr_version = bfd_get_32 (abfd, note->descdata);
429
430 if (pr_version != 1)
431 return FALSE;
432
433 /* pr_cursig */
228e534f 434 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20);
61adc1a4
NC
435
436 /* pr_pid */
228e534f 437 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
61adc1a4
NC
438
439 /* pr_reg */
440 offset = 28;
eea6121a 441 size = bfd_get_32 (abfd, note->descdata + 8);
61adc1a4
NC
442 }
443 else
444 {
445 switch (note->descsz)
446 {
447 default:
448 return FALSE;
38701953 449
61adc1a4
NC
450 case 144: /* Linux/i386 */
451 /* pr_cursig */
228e534f 452 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
38701953 453
61adc1a4 454 /* pr_pid */
228e534f 455 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
38701953 456
61adc1a4
NC
457 /* pr_reg */
458 offset = 72;
eea6121a 459 size = 68;
38701953 460
61adc1a4
NC
461 break;
462 }
38701953
AM
463 }
464
465 /* Make a ".reg/999" section. */
466 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 467 size, note->descpos + offset);
38701953
AM
468}
469
b34976b6 470static bfd_boolean
55fd94b0 471elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
38701953 472{
61adc1a4 473 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
38701953 474 {
61adc1a4
NC
475 int pr_version = bfd_get_32 (abfd, note->descdata);
476
477 if (pr_version != 1)
b34976b6 478 return FALSE;
38701953 479
228e534f 480 elf_tdata (abfd)->core->program
61adc1a4 481 = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
228e534f 482 elf_tdata (abfd)->core->command
61adc1a4
NC
483 = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
484 }
485 else
486 {
487 switch (note->descsz)
488 {
489 default:
490 return FALSE;
491
492 case 124: /* Linux/i386 elf_prpsinfo. */
228e534f 493 elf_tdata (abfd)->core->pid
261b8d08 494 = bfd_get_32 (abfd, note->descdata + 12);
228e534f 495 elf_tdata (abfd)->core->program
61adc1a4 496 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
228e534f 497 elf_tdata (abfd)->core->command
61adc1a4
NC
498 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
499 }
38701953
AM
500 }
501
502 /* Note that for some reason, a spurious space is tacked
503 onto the end of the args in some (at least one anyway)
504 implementations, so strip it off if it exists. */
38701953 505 {
228e534f 506 char *command = elf_tdata (abfd)->core->command;
38701953
AM
507 int n = strlen (command);
508
509 if (0 < n && command[n - 1] == ' ')
510 command[n - 1] = '\0';
511 }
512
b34976b6 513 return TRUE;
38701953
AM
514}
515\f
516/* Functions for the i386 ELF linker.
517
518 In order to gain some understanding of code in this file without
519 knowing all the intricate details of the linker, note the
520 following:
521
522 Functions named elf_i386_* are called by external routines, other
523 functions are only called locally. elf_i386_* functions appear
524 in this file more or less in the order in which they are called
525 from external routines. eg. elf_i386_check_relocs is called
526 early in the link process, elf_i386_finish_dynamic_sections is
527 one of the last functions. */
528
f604c2a2 529/* The size in bytes of an entry in the lazy procedure linkage table. */
252b5132 530
f604c2a2 531#define LAZY_PLT_ENTRY_SIZE 16
252b5132 532
f604c2a2
L
533/* The size in bytes of an entry in the non-lazy procedure linkage
534 table. */
252b5132 535
f604c2a2
L
536#define NON_LAZY_PLT_ENTRY_SIZE 8
537
538/* The first entry in an absolute lazy procedure linkage table looks
539 like this. See the SVR4 ABI i386 supplement to see how this works.
540 Will be padded to LAZY_PLT_ENTRY_SIZE with lazy_plt->plt0_pad_byte. */
541
542static const bfd_byte elf_i386_lazy_plt0_entry[12] =
252b5132
RH
543{
544 0xff, 0x35, /* pushl contents of address */
545 0, 0, 0, 0, /* replaced with address of .got + 4. */
546 0xff, 0x25, /* jmp indirect */
eac338cf 547 0, 0, 0, 0 /* replaced with address of .got + 8. */
252b5132
RH
548};
549
f604c2a2
L
550/* Subsequent entries in an absolute lazy procedure linkage table look
551 like this. */
252b5132 552
f604c2a2 553static const bfd_byte elf_i386_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
252b5132
RH
554{
555 0xff, 0x25, /* jmp indirect */
556 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
557 0x68, /* pushl immediate */
558 0, 0, 0, 0, /* replaced with offset into relocation table. */
559 0xe9, /* jmp relative */
560 0, 0, 0, 0 /* replaced with offset to start of .plt. */
561};
562
f604c2a2
L
563/* The first entry in a PIC lazy procedure linkage table look like
564 this. Will be padded to LAZY_PLT_ENTRY_SIZE with
565 lazy_plt->plt0_pad_byte. */
252b5132 566
f604c2a2 567static const bfd_byte elf_i386_pic_lazy_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
f604c2a2
L
573/* Subsequent entries in a PIC lazy procedure linkage table look like
574 this. */
252b5132 575
f604c2a2 576static const bfd_byte elf_i386_pic_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
252b5132
RH
577{
578 0xff, 0xa3, /* jmp *offset(%ebx) */
579 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
580 0x68, /* pushl immediate */
581 0, 0, 0, 0, /* replaced with offset into relocation table. */
582 0xe9, /* jmp relative */
583 0, 0, 0, 0 /* replaced with offset to start of .plt. */
584};
585
f604c2a2 586/* Entries in the non-lazy procedure linkage table look like this. */
dd7e64d4 587
f604c2a2 588static const bfd_byte elf_i386_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
dd7e64d4
L
589{
590 0xff, 0x25, /* jmp indirect */
591 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
592 0x66, 0x90 /* xchg %ax,%ax */
593};
594
f604c2a2
L
595/* Entries in the PIC non-lazy procedure linkage table look like
596 this. */
dd7e64d4 597
f604c2a2 598static const bfd_byte elf_i386_pic_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
dd7e64d4
L
599{
600 0xff, 0xa3, /* jmp *offset(%ebx) */
601 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
602 0x66, 0x90 /* xchg %ax,%ax */
603};
604
ee2fdd6f
L
605/* The first entry in an absolute IBT-enabled lazy procedure linkage
606 table looks like this. */
607
608static const bfd_byte elf_i386_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
609{
610 0xff, 0x35, 0, 0, 0, 0, /* pushl GOT[1] */
611 0xff, 0x25, 0, 0, 0, 0, /* jmp *GOT[2] */
612 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
613};
614
615/* Subsequent entries for an absolute IBT-enabled lazy procedure linkage
616 table look like this. Subsequent entries for a PIC IBT-enabled lazy
617 procedure linkage table are the same. */
618
619static const bfd_byte elf_i386_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
620{
621 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */
622 0x68, 0, 0, 0, 0, /* pushl immediate */
623 0xe9, 0, 0, 0, 0, /* jmp relative */
624 0x66, 0x90 /* xchg %ax,%ax */
625};
626
627/* The first entry in a PIC IBT-enabled lazy procedure linkage table
628 look like. */
629
630static const bfd_byte elf_i386_pic_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
631{
632 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
633 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */
634 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
635};
636
637/* Entries for branches with IBT-enabled in the absolute non-lazey
638 procedure linkage table look like this. They have the same size
639 as the lazy PLT entry. */
640
641static const bfd_byte elf_i386_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
642{
643 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */
644 0xff, 0x25, 0, 0, 0, 0, /* jmp *name@GOT */
645 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
646};
647
648/* Entries for branches with IBT-enabled in the PIC non-lazey procedure
649 linkage table look like this. They have the same size as the lazy
650 PLT entry. */
651
652static const bfd_byte elf_i386_pic_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
653{
654 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */
655 0xff, 0xa3, 0, 0, 0, 0, /* jmp *name@GOT(%ebx) */
656 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
657};
658
f604c2a2 659/* .eh_frame covering the lazy .plt section. */
e41b3a13 660
f604c2a2 661static const bfd_byte elf_i386_eh_frame_lazy_plt[] =
e41b3a13 662{
e41b3a13
JJ
663 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
664 0, 0, 0, 0, /* CIE ID */
665 1, /* CIE version */
666 'z', 'R', 0, /* Augmentation string */
667 1, /* Code alignment factor */
668 0x7c, /* Data alignment factor */
669 8, /* Return address column */
670 1, /* Augmentation size */
671 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
672 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
673 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
674 DW_CFA_nop, DW_CFA_nop,
675
676 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
677 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
678 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
679 0, 0, 0, 0, /* .plt size goes here */
680 0, /* Augmentation size */
681 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
682 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
683 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
684 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
685 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
686 11, /* Block length */
687 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
688 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
689 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
28ede8be 690 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
e41b3a13
JJ
691 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
692};
693
ee2fdd6f
L
694/* .eh_frame covering the lazy .plt section with IBT-enabled. */
695
696static const bfd_byte elf_i386_eh_frame_lazy_ibt_plt[] =
697{
698 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
699 0, 0, 0, 0, /* CIE ID */
700 1, /* CIE version */
701 'z', 'R', 0, /* Augmentation string */
702 1, /* Code alignment factor */
703 0x7c, /* Data alignment factor */
704 8, /* Return address column */
705 1, /* Augmentation size */
706 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
707 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
708 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
709 DW_CFA_nop, DW_CFA_nop,
710
711 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
712 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
713 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
714 0, 0, 0, 0, /* .plt size goes here */
715 0, /* Augmentation size */
716 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
717 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
718 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
719 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
720 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
721 11, /* Block length */
722 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
723 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
724 DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
725 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
726 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
727};
728
f604c2a2 729/* .eh_frame covering the non-lazy .plt section. */
fff53dae 730
f604c2a2 731static const bfd_byte elf_i386_eh_frame_non_lazy_plt[] =
fff53dae
L
732{
733#define PLT_GOT_FDE_LENGTH 16
734 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
735 0, 0, 0, 0, /* CIE ID */
736 1, /* CIE version */
737 'z', 'R', 0, /* Augmentation string */
738 1, /* Code alignment factor */
739 0x7c, /* Data alignment factor */
740 8, /* Return address column */
741 1, /* Augmentation size */
742 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
743 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
744 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
745 DW_CFA_nop, DW_CFA_nop,
746
747 PLT_GOT_FDE_LENGTH, 0, 0, 0, /* FDE length */
748 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
f604c2a2
L
749 0, 0, 0, 0, /* the start of non-lazy .plt goes here */
750 0, 0, 0, 0, /* non-lazy .plt size goes here */
fff53dae
L
751 0, /* Augmentation size */
752 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
753};
754
25e762b9 755/* These are the standard parameters. */
765e526c 756static const struct elf_x86_lazy_plt_layout elf_i386_lazy_plt =
25e762b9 757 {
f604c2a2
L
758 elf_i386_lazy_plt0_entry, /* plt0_entry */
759 sizeof (elf_i386_lazy_plt0_entry), /* plt0_entry_size */
f604c2a2
L
760 elf_i386_lazy_plt_entry, /* plt_entry */
761 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
765e526c
L
762 2, /* plt0_got1_offset */
763 8, /* plt0_got2_offset */
764 0, /* plt0_got2_insn_end */
25e762b9
RM
765 2, /* plt_got_offset */
766 7, /* plt_reloc_offset */
767 12, /* plt_plt_offset */
765e526c
L
768 0, /* plt_got_insn_size */
769 0, /* plt_plt_insn_end */
25e762b9 770 6, /* plt_lazy_offset */
f604c2a2
L
771 elf_i386_pic_lazy_plt0_entry, /* pic_plt0_entry */
772 elf_i386_pic_lazy_plt_entry, /* pic_plt_entry */
773 elf_i386_eh_frame_lazy_plt, /* eh_frame_plt */
774 sizeof (elf_i386_eh_frame_lazy_plt) /* eh_frame_plt_size */
775 };
776
765e526c 777static const struct elf_x86_non_lazy_plt_layout elf_i386_non_lazy_plt =
f604c2a2
L
778 {
779 elf_i386_non_lazy_plt_entry, /* plt_entry */
780 elf_i386_pic_non_lazy_plt_entry, /* pic_plt_entry */
781 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
782 2, /* plt_got_offset */
765e526c 783 0, /* plt_got_insn_size */
f604c2a2
L
784 elf_i386_eh_frame_non_lazy_plt, /* eh_frame_plt */
785 sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
25e762b9 786 };
ee2fdd6f 787
765e526c 788static const struct elf_x86_lazy_plt_layout elf_i386_lazy_ibt_plt =
ee2fdd6f
L
789 {
790 elf_i386_lazy_ibt_plt0_entry, /* plt0_entry */
791 sizeof (elf_i386_lazy_ibt_plt0_entry), /* plt0_entry_size */
ee2fdd6f
L
792 elf_i386_lazy_ibt_plt_entry, /* plt_entry */
793 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
765e526c
L
794 2, /* plt0_got1_offset */
795 8, /* plt0_got2_offset */
796 0, /* plt0_got2_insn_end */
ee2fdd6f
L
797 4+2, /* plt_got_offset */
798 4+1, /* plt_reloc_offset */
799 4+6, /* plt_plt_offset */
765e526c
L
800 0, /* plt_got_insn_size */
801 0, /* plt_plt_insn_end */
ee2fdd6f
L
802 0, /* plt_lazy_offset */
803 elf_i386_pic_lazy_ibt_plt0_entry, /* pic_plt0_entry */
804 elf_i386_lazy_ibt_plt_entry, /* pic_plt_entry */
805 elf_i386_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
806 sizeof (elf_i386_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
807 };
808
765e526c 809static const struct elf_x86_non_lazy_plt_layout elf_i386_non_lazy_ibt_plt =
ee2fdd6f
L
810 {
811 elf_i386_non_lazy_ibt_plt_entry, /* plt_entry */
812 elf_i386_pic_non_lazy_ibt_plt_entry,/* pic_plt_entry */
813 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
814 4+2, /* plt_got_offset */
765e526c 815 0, /* plt_got_insn_size */
ee2fdd6f
L
816 elf_i386_eh_frame_non_lazy_plt, /* eh_frame_plt */
817 sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
818 };
25e762b9
RM
819\f
820
eac338cf
PB
821/* On VxWorks, the .rel.plt.unloaded section has absolute relocations
822 for the PLTResolve stub and then for each PLT entry. */
823#define PLTRESOLVE_RELOCS_SHLIB 0
824#define PLTRESOLVE_RELOCS 2
825#define PLT_NON_JUMP_SLOT_RELOCS 2
826
23209a78
RM
827/* Architecture-specific backend data for i386. */
828
829struct elf_i386_backend_data
830{
831 /* Value used to fill the unused bytes of the first PLT entry. */
832 bfd_byte plt0_pad_byte;
833
f604c2a2
L
834 /* Target system. */
835 enum
836 {
837 is_normal,
838 is_vxworks,
839 is_nacl
840 } os;
23209a78
RM
841};
842
843#define get_elf_i386_backend_data(abfd) \
844 ((const struct elf_i386_backend_data *) \
845 get_elf_backend_data (abfd)->arch_data)
846
847/* These are the standard parameters. */
848static const struct elf_i386_backend_data elf_i386_arch_bed =
849 {
850 0, /* plt0_pad_byte */
f604c2a2 851 is_normal /* os */
23209a78
RM
852 };
853
854#define elf_backend_arch_data &elf_i386_arch_bed
855
142411ca
L
856/* Return TRUE if the TLS access code sequence support transition
857 from R_TYPE. */
858
859static bfd_boolean
bedfd056 860elf_i386_check_tls_transition (asection *sec,
142411ca
L
861 bfd_byte *contents,
862 Elf_Internal_Shdr *symtab_hdr,
863 struct elf_link_hash_entry **sym_hashes,
864 unsigned int r_type,
865 const Elf_Internal_Rela *rel,
866 const Elf_Internal_Rela *relend)
13ae64f3 867{
6eaa7fb5 868 unsigned int val, type, reg;
142411ca
L
869 unsigned long r_symndx;
870 struct elf_link_hash_entry *h;
871 bfd_vma offset;
6eaa7fb5 872 bfd_byte *call;
1d4af308 873 bfd_boolean indirect_call;
142411ca 874
142411ca 875 offset = rel->r_offset;
13ae64f3 876 switch (r_type)
142411ca
L
877 {
878 case R_386_TLS_GD:
879 case R_386_TLS_LDM:
880 if (offset < 2 || (rel + 1) >= relend)
881 return FALSE;
882
6eaa7fb5
L
883 indirect_call = FALSE;
884 call = contents + offset + 4;
885 val = *(call - 5);
886 type = *(call - 6);
142411ca
L
887 if (r_type == R_386_TLS_GD)
888 {
09e8c3bf 889 /* Check transition from GD access model. Only
6eaa7fb5
L
890 leal foo@tlsgd(,%ebx,1), %eax
891 call ___tls_get_addr@PLT
892 or
893 leal foo@tlsgd(%ebx) %eax
894 call ___tls_get_addr@PLT
895 nop
896 or
897 leal foo@tlsgd(%reg), %eax
898 call *___tls_get_addr@GOT(%reg)
899 which may be converted to
900 addr32 call ___tls_get_addr
142411ca 901 can transit to different access model. */
6eaa7fb5
L
902 if ((offset + 10) > sec->size
903 || (type != 0x8d && type != 0x04))
142411ca
L
904 return FALSE;
905
142411ca
L
906 if (type == 0x04)
907 {
6eaa7fb5
L
908 /* leal foo@tlsgd(,%ebx,1), %eax
909 call ___tls_get_addr@PLT */
142411ca
L
910 if (offset < 3)
911 return FALSE;
912
6eaa7fb5
L
913 if (*(call - 7) != 0x8d
914 || val != 0x1d
915 || call[0] != 0xe8)
142411ca
L
916 return FALSE;
917 }
918 else
919 {
6eaa7fb5
L
920 /* This must be
921 leal foo@tlsgd(%ebx), %eax
922 call ___tls_get_addr@PLT
923 nop
924 or
925 leal foo@tlsgd(%reg), %eax
926 call *___tls_get_addr@GOT(%reg)
927 which may be converted to
928 addr32 call ___tls_get_addr
929
930 %eax can't be used as the GOT base register since it
931 is used to pass parameter to ___tls_get_addr. */
932 reg = val & 7;
933 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
142411ca
L
934 return FALSE;
935
6eaa7fb5
L
936 indirect_call = call[0] == 0xff;
937 if (!(reg == 3 && call[0] == 0xe8 && call[5] == 0x90)
938 && !(call[0] == 0x67 && call[1] == 0xe8)
939 && !(indirect_call
940 && (call[1] & 0xf8) == 0x90
941 && (call[1] & 0x7) == reg))
142411ca
L
942 return FALSE;
943 }
944 }
945 else
946 {
947 /* Check transition from LD access model. Only
6eaa7fb5
L
948 leal foo@tlsldm(%ebx), %eax
949 call ___tls_get_addr@PLT
950 or
951 leal foo@tlsldm(%reg), %eax
952 call *___tls_get_addr@GOT(%reg)
953 which may be converted to
954 addr32 call ___tls_get_addr
142411ca
L
955 can transit to different access model. */
956 if (type != 0x8d || (offset + 9) > sec->size)
957 return FALSE;
958
6eaa7fb5
L
959 /* %eax can't be used as the GOT base register since it is
960 used to pass parameter to ___tls_get_addr. */
961 reg = val & 7;
962 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
142411ca 963 return FALSE;
142411ca 964
6eaa7fb5
L
965 indirect_call = call[0] == 0xff;
966 if (!(reg == 3 && call[0] == 0xe8)
967 && !(call[0] == 0x67 && call[1] == 0xe8)
968 && !(indirect_call
969 && (call[1] & 0xf8) == 0x90
970 && (call[1] & 0x7) == reg))
971 return FALSE;
972 }
142411ca
L
973
974 r_symndx = ELF32_R_SYM (rel[1].r_info);
975 if (r_symndx < symtab_hdr->sh_info)
976 return FALSE;
977
978 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1d4af308 979 if (h == NULL
0afcef53 980 || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
6eaa7fb5
L
981 return FALSE;
982 else if (indirect_call)
983 return (ELF32_R_TYPE (rel[1].r_info) == R_386_GOT32X);
984 else
985 return (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
986 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
142411ca
L
987
988 case R_386_TLS_IE:
989 /* Check transition from IE access model:
990 movl foo@indntpoff(%rip), %eax
991 movl foo@indntpoff(%rip), %reg
992 addl foo@indntpoff(%rip), %reg
993 */
994
995 if (offset < 1 || (offset + 4) > sec->size)
996 return FALSE;
997
998 /* Check "movl foo@tpoff(%rip), %eax" first. */
999 val = bfd_get_8 (abfd, contents + offset - 1);
1000 if (val == 0xa1)
1001 return TRUE;
1002
1003 if (offset < 2)
1004 return FALSE;
1005
1006 /* Check movl|addl foo@tpoff(%rip), %reg. */
1007 type = bfd_get_8 (abfd, contents + offset - 2);
1008 return ((type == 0x8b || type == 0x03)
1009 && (val & 0xc7) == 0x05);
1010
1011 case R_386_TLS_GOTIE:
1012 case R_386_TLS_IE_32:
1013 /* Check transition from {IE_32,GOTIE} access model:
1014 subl foo@{tpoff,gontoff}(%reg1), %reg2
1015 movl foo@{tpoff,gontoff}(%reg1), %reg2
1016 addl foo@{tpoff,gontoff}(%reg1), %reg2
1017 */
1018
1019 if (offset < 2 || (offset + 4) > sec->size)
1020 return FALSE;
1021
1022 val = bfd_get_8 (abfd, contents + offset - 1);
1023 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1024 return FALSE;
1025
1026 type = bfd_get_8 (abfd, contents + offset - 2);
1027 return type == 0x8b || type == 0x2b || type == 0x03;
1028
1029 case R_386_TLS_GOTDESC:
1030 /* Check transition from GDesc access model:
1031 leal x@tlsdesc(%ebx), %eax
1032
1033 Make sure it's a leal adding ebx to a 32-bit offset
1034 into any register, although it's probably almost always
1035 going to be eax. */
1036
1037 if (offset < 2 || (offset + 4) > sec->size)
1038 return FALSE;
1039
1040 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1041 return FALSE;
1042
1043 val = bfd_get_8 (abfd, contents + offset - 1);
1044 return (val & 0xc7) == 0x83;
1045
1046 case R_386_TLS_DESC_CALL:
1047 /* Check transition from GDesc access model:
6eaa7fb5 1048 call *x@tlsdesc(%eax)
142411ca
L
1049 */
1050 if (offset + 2 <= sec->size)
1051 {
6eaa7fb5
L
1052 /* Make sure that it's a call *x@tlsdesc(%eax). */
1053 call = contents + offset;
1054 return call[0] == 0xff && call[1] == 0x10;
142411ca
L
1055 }
1056
1057 return FALSE;
1058
1059 default:
1060 abort ();
1061 }
1062}
1063
1064/* Return TRUE if the TLS access transition is OK or no transition
1065 will be performed. Update R_TYPE if there is a transition. */
1066
1067static bfd_boolean
1068elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1069 asection *sec, bfd_byte *contents,
1070 Elf_Internal_Shdr *symtab_hdr,
1071 struct elf_link_hash_entry **sym_hashes,
1072 unsigned int *r_type, int tls_type,
1073 const Elf_Internal_Rela *rel,
1074 const Elf_Internal_Rela *relend,
4c544807 1075 struct elf_link_hash_entry *h,
bedfd056
L
1076 unsigned long r_symndx,
1077 bfd_boolean from_relocate_section)
142411ca
L
1078{
1079 unsigned int from_type = *r_type;
1080 unsigned int to_type = from_type;
1081 bfd_boolean check = TRUE;
1082
bb1cb422
L
1083 /* Skip TLS transition for functions. */
1084 if (h != NULL
1085 && (h->type == STT_FUNC
1086 || h->type == STT_GNU_IFUNC))
1087 return TRUE;
1088
142411ca 1089 switch (from_type)
13ae64f3
JJ
1090 {
1091 case R_386_TLS_GD:
67a4f2b7
AO
1092 case R_386_TLS_GOTDESC:
1093 case R_386_TLS_DESC_CALL:
13ae64f3 1094 case R_386_TLS_IE_32:
37e55690
JJ
1095 case R_386_TLS_IE:
1096 case R_386_TLS_GOTIE:
0e1862bb 1097 if (bfd_link_executable (info))
142411ca
L
1098 {
1099 if (h == NULL)
1100 to_type = R_386_TLS_LE_32;
1101 else if (from_type != R_386_TLS_IE
1102 && from_type != R_386_TLS_GOTIE)
1103 to_type = R_386_TLS_IE_32;
1104 }
1105
bedfd056
L
1106 /* When we are called from elf_i386_relocate_section, there may
1107 be additional transitions based on TLS_TYPE. */
1108 if (from_relocate_section)
142411ca
L
1109 {
1110 unsigned int new_to_type = to_type;
1111
51537393 1112 if (TLS_TRANSITION_IE_TO_LE_P (info, h, tls_type))
142411ca
L
1113 new_to_type = R_386_TLS_LE_32;
1114
1115 if (to_type == R_386_TLS_GD
1116 || to_type == R_386_TLS_GOTDESC
1117 || to_type == R_386_TLS_DESC_CALL)
1118 {
1119 if (tls_type == GOT_TLS_IE_POS)
1120 new_to_type = R_386_TLS_GOTIE;
1121 else if (tls_type & GOT_TLS_IE)
1122 new_to_type = R_386_TLS_IE_32;
1123 }
1124
1125 /* We checked the transition before when we were called from
1126 elf_i386_check_relocs. We only want to check the new
1127 transition which hasn't been checked before. */
1128 check = new_to_type != to_type && from_type == to_type;
1129 to_type = new_to_type;
1130 }
1131
1132 break;
1133
13ae64f3 1134 case R_386_TLS_LDM:
0e1862bb 1135 if (bfd_link_executable (info))
142411ca
L
1136 to_type = R_386_TLS_LE_32;
1137 break;
1138
1139 default:
1140 return TRUE;
1141 }
1142
1143 /* Return TRUE if there is no transition. */
1144 if (from_type == to_type)
1145 return TRUE;
1146
1147 /* Check if the transition can be performed. */
1148 if (check
bedfd056 1149 && ! elf_i386_check_tls_transition (sec, contents,
142411ca
L
1150 symtab_hdr, sym_hashes,
1151 from_type, rel, relend))
1152 {
2f629d23 1153 reloc_howto_type *from, *to;
4c544807 1154 const char *name;
142411ca
L
1155
1156 from = elf_i386_rtype_to_howto (abfd, from_type);
1157 to = elf_i386_rtype_to_howto (abfd, to_type);
1158
4c544807
L
1159 if (h)
1160 name = h->root.root.string;
1161 else
1162 {
0afcef53 1163 struct elf_x86_link_hash_table *htab;
4dfe6ac6 1164
0afcef53 1165 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6
NC
1166 if (htab == NULL)
1167 name = "*unknown*";
1168 else
1169 {
1170 Elf_Internal_Sym *isym;
1171
1172 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1173 abfd, r_symndx);
1174 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1175 }
4c544807
L
1176 }
1177
4eca0228 1178 _bfd_error_handler
695344c0 1179 /* xgettext:c-format */
d42c267e 1180 (_("%B: TLS transition from %s to %s against `%s' at %#Lx "
142411ca 1181 "in section `%A' failed"),
c08bb8dd 1182 abfd, from->name, to->name, name,
d42c267e 1183 rel->r_offset, sec);
142411ca
L
1184 bfd_set_error (bfd_error_bad_value);
1185 return FALSE;
13ae64f3
JJ
1186 }
1187
142411ca
L
1188 *r_type = to_type;
1189 return TRUE;
13ae64f3
JJ
1190}
1191
c175a657
L
1192/* With the local symbol, foo, we convert
1193 mov foo@GOT[(%reg1)], %reg2
1194 to
1195 lea foo[@GOTOFF(%reg1)], %reg2
1196 and convert
1197 call/jmp *foo@GOT[(%reg)]
1198 to
1199 nop call foo/jmp foo nop
1200 When PIC is false, convert
1201 test %reg1, foo@GOT[(%reg2)]
1202 to
1203 test $foo, %reg1
1204 and convert
1205 binop foo@GOT[(%reg1)], %reg2
1206 to
1207 binop $foo, %reg2
1208 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1209 instructions. */
1210
1211static
1212bfd_boolean
1213elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
1214 bfd_byte *contents,
6999821f 1215 unsigned int *r_type_p,
c175a657
L
1216 Elf_Internal_Rela *irel,
1217 struct elf_link_hash_entry *h,
5e5e02ae 1218 bfd_boolean *converted,
c175a657
L
1219 struct bfd_link_info *link_info)
1220{
0afcef53 1221 struct elf_x86_link_hash_table *htab;
c175a657
L
1222 unsigned int opcode;
1223 unsigned int modrm;
1224 bfd_boolean baseless;
1225 Elf_Internal_Sym *isym;
1226 unsigned int addend;
1227 unsigned int nop;
1228 bfd_vma nop_offset;
1229 bfd_boolean is_pic;
1230 bfd_boolean to_reloc_32;
1231 unsigned int r_type;
1232 unsigned int r_symndx;
1233 bfd_vma roff = irel->r_offset;
0a27fed7
L
1234 bfd_boolean local_ref;
1235 struct elf_x86_link_hash_entry *eh;
c175a657
L
1236
1237 if (roff < 2)
1238 return TRUE;
1239
7d4d9709 1240 /* Addend for R_386_GOT32X relocations must be 0. */
c175a657
L
1241 addend = bfd_get_32 (abfd, contents + roff);
1242 if (addend != 0)
1243 return TRUE;
1244
0afcef53 1245 htab = elf_x86_hash_table (link_info, I386_ELF_DATA);
c175a657
L
1246 is_pic = bfd_link_pic (link_info);
1247
6999821f 1248 r_type = *r_type_p;
c175a657
L
1249 r_symndx = ELF32_R_SYM (irel->r_info);
1250
1251 modrm = bfd_get_8 (abfd, contents + roff - 1);
1252 baseless = (modrm & 0xc7) == 0x5;
1253
7d4d9709 1254 if (baseless && is_pic)
c175a657
L
1255 {
1256 /* For PIC, disallow R_386_GOT32X without a base register
7d4d9709 1257 since we don't know what the GOT base is. */
c175a657
L
1258 const char *name;
1259
1260 if (h == NULL)
1261 {
1262 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd,
1263 r_symndx);
1264 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1265 }
1266 else
1267 name = h->root.root.string;
1268
4eca0228 1269 _bfd_error_handler
695344c0 1270 /* xgettext:c-format */
63a5468a
AM
1271 (_("%B: direct GOT relocation R_386_GOT32X against `%s' without base"
1272 " register can not be used when making a shared object"),
c175a657
L
1273 abfd, name);
1274 return FALSE;
1275 }
1276
1277 opcode = bfd_get_8 (abfd, contents + roff - 2);
1278
c175a657
L
1279 /* Convert to R_386_32 if PIC is false or there is no base
1280 register. */
1281 to_reloc_32 = !is_pic || baseless;
1282
0a27fed7
L
1283 eh = elf_x86_hash_entry (h);
1284
7d4d9709
L
1285 /* Try to convert R_386_GOT32X. Get the symbol referred to by the
1286 reloc. */
c175a657
L
1287 if (h == NULL)
1288 {
1289 if (opcode == 0x0ff)
1290 /* Convert "call/jmp *foo@GOT[(%reg)]". */
1291 goto convert_branch;
1292 else
1293 /* Convert "mov foo@GOT[(%reg1)], %reg2",
1294 "test %reg1, foo@GOT(%reg2)" and
1295 "binop foo@GOT[(%reg1)], %reg2". */
1296 goto convert_load;
1297 }
1298
0a27fed7
L
1299 /* NB: Also set linker_def via SYMBOL_REFERENCES_LOCAL_P. */
1300 local_ref = SYMBOL_REFERENCES_LOCAL_P (link_info, h);
1301
c175a657
L
1302 /* Undefined weak symbol is only bound locally in executable
1303 and its reference is resolved as 0. */
0a27fed7
L
1304 if (h->root.type == bfd_link_hash_undefweak
1305 && !eh->linker_def
1306 && local_ref)
c175a657
L
1307 {
1308 if (opcode == 0xff)
1309 {
1310 /* No direct branch to 0 for PIC. */
1311 if (is_pic)
1312 return TRUE;
1313 else
1314 goto convert_branch;
1315 }
1316 else
1317 {
1318 /* We can convert load of address 0 to R_386_32. */
1319 to_reloc_32 = TRUE;
1320 goto convert_load;
1321 }
1322 }
1323
1324 if (opcode == 0xff)
1325 {
1326 /* We have "call/jmp *foo@GOT[(%reg)]". */
1327 if ((h->root.type == bfd_link_hash_defined
1328 || h->root.type == bfd_link_hash_defweak)
0a27fed7 1329 && local_ref)
c175a657
L
1330 {
1331 /* The function is locally defined. */
1332convert_branch:
1333 /* Convert R_386_GOT32X to R_386_PC32. */
1334 if (modrm == 0x15 || (modrm & 0xf8) == 0x90)
1335 {
1336 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1337 is a nop prefix. */
1338 modrm = 0xe8;
6eaa7fb5
L
1339 /* To support TLS optimization, always use addr32 prefix
1340 for "call *___tls_get_addr@GOT(%reg)". */
1d4af308 1341 if (eh && eh->tls_get_addr)
c175a657 1342 {
6eaa7fb5
L
1343 nop = 0x67;
1344 nop_offset = irel->r_offset - 2;
c175a657
L
1345 }
1346 else
6eaa7fb5
L
1347 {
1348 nop = link_info->call_nop_byte;
1349 if (link_info->call_nop_as_suffix)
1350 {
1351 nop_offset = roff + 3;
1352 irel->r_offset -= 1;
1353 }
1354 else
1355 nop_offset = roff - 2;
1356 }
c175a657
L
1357 }
1358 else
1359 {
1360 /* Convert to "jmp foo nop". */
1361 modrm = 0xe9;
1362 nop = NOP_OPCODE;
1363 nop_offset = roff + 3;
1364 irel->r_offset -= 1;
1365 }
1366
1367 bfd_put_8 (abfd, nop, contents + nop_offset);
1368 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1369 /* When converting to PC-relative relocation, we
1370 need to adjust addend by -4. */
1371 bfd_put_32 (abfd, -4, contents + irel->r_offset);
1372 irel->r_info = ELF32_R_INFO (r_symndx, R_386_PC32);
6999821f 1373 *r_type_p = R_386_PC32;
5e5e02ae 1374 *converted = TRUE;
c175a657
L
1375 }
1376 }
1377 else
1378 {
1379 /* We have "mov foo@GOT[(%re1g)], %reg2",
1380 "test %reg1, foo@GOT(%reg2)" and
1381 "binop foo@GOT[(%reg1)], %reg2".
1382
1383 Avoid optimizing _DYNAMIC since ld.so may use its
1384 link-time address. */
1385 if (h == htab->elf.hdynamic)
1386 return TRUE;
1387
1388 /* def_regular is set by an assignment in a linker script in
cbd0eecf
L
1389 bfd_elf_record_link_assignment. start_stop is set on
1390 __start_SECNAME/__stop_SECNAME which mark section SECNAME. */
1391 if (h->start_stop
0a27fed7 1392 || eh->linker_def
cbd0eecf
L
1393 || ((h->def_regular
1394 || h->root.type == bfd_link_hash_defined
1395 || h->root.type == bfd_link_hash_defweak)
0a27fed7 1396 && local_ref))
c175a657
L
1397 {
1398convert_load:
1399 if (opcode == 0x8b)
1400 {
1401 if (to_reloc_32)
1402 {
1403 /* Convert "mov foo@GOT[(%reg1)], %reg2" to
1404 "mov $foo, %reg2" with R_386_32. */
1405 r_type = R_386_32;
1406 modrm = 0xc0 | (modrm & 0x38) >> 3;
1407 bfd_put_8 (abfd, modrm, contents + roff - 1);
1408 opcode = 0xc7;
1409 }
1410 else
1411 {
1412 /* Convert "mov foo@GOT(%reg1), %reg2" to
1413 "lea foo@GOTOFF(%reg1), %reg2". */
1414 r_type = R_386_GOTOFF;
1415 opcode = 0x8d;
1416 }
1417 }
1418 else
1419 {
1420 /* Only R_386_32 is supported. */
1421 if (!to_reloc_32)
1422 return TRUE;
1423
1424 if (opcode == 0x85)
1425 {
1426 /* Convert "test %reg1, foo@GOT(%reg2)" to
1427 "test $foo, %reg1". */
1428 modrm = 0xc0 | (modrm & 0x38) >> 3;
1429 opcode = 0xf7;
1430 }
1431 else
1432 {
1433 /* Convert "binop foo@GOT(%reg1), %reg2" to
1434 "binop $foo, %reg2". */
1435 modrm = (0xc0
1436 | (modrm & 0x38) >> 3
1437 | (opcode & 0x3c));
1438 opcode = 0x81;
1439 }
1440 bfd_put_8 (abfd, modrm, contents + roff - 1);
1441 r_type = R_386_32;
1442 }
1443
1444 bfd_put_8 (abfd, opcode, contents + roff - 2);
1445 irel->r_info = ELF32_R_INFO (r_symndx, r_type);
6999821f 1446 *r_type_p = r_type;
5e5e02ae 1447 *converted = TRUE;
c175a657
L
1448 }
1449 }
1450
1451 return TRUE;
1452}
1453
c1d11331
L
1454/* Rename some of the generic section flags to better document how they
1455 are used here. */
6999821f 1456#define check_relocs_failed sec_flg0
c1d11331 1457
252b5132 1458/* Look through the relocs for a section during the first phase, and
0ac8d2ca
AM
1459 calculate needed space in the global offset table, procedure linkage
1460 table, and dynamic reloc sections. */
252b5132 1461
b34976b6 1462static bfd_boolean
55fd94b0
AM
1463elf_i386_check_relocs (bfd *abfd,
1464 struct bfd_link_info *info,
1465 asection *sec,
1466 const Elf_Internal_Rela *relocs)
252b5132 1467{
0afcef53 1468 struct elf_x86_link_hash_table *htab;
252b5132
RH
1469 Elf_Internal_Shdr *symtab_hdr;
1470 struct elf_link_hash_entry **sym_hashes;
252b5132
RH
1471 const Elf_Internal_Rela *rel;
1472 const Elf_Internal_Rela *rel_end;
252b5132 1473 asection *sreloc;
bedfd056 1474 bfd_byte *contents;
5e5e02ae 1475 bfd_boolean converted;
252b5132 1476
0e1862bb 1477 if (bfd_link_relocatable (info))
b34976b6 1478 return TRUE;
252b5132 1479
081b1afe
L
1480 /* Don't do anything special with non-loaded, non-alloced sections.
1481 In particular, any relocs in such sections should not affect GOT
1482 and PLT reference counting (ie. we don't allow them to create GOT
1483 or PLT entries), there's no possibility or desire to optimize TLS
1484 relocs, and there's not much point in propagating relocs to shared
1485 libs that the dynamic linker won't relocate. */
1486 if ((sec->flags & SEC_ALLOC) == 0)
1487 return TRUE;
1488
0afcef53 1489 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6 1490 if (htab == NULL)
c337a162
L
1491 {
1492 sec->check_relocs_failed = 1;
1493 return FALSE;
1494 }
4dfe6ac6 1495
fe53b4a4
L
1496 BFD_ASSERT (is_x86_elf (abfd, htab));
1497
bedfd056
L
1498 /* Get the section contents. */
1499 if (elf_section_data (sec)->this_hdr.contents != NULL)
1500 contents = elf_section_data (sec)->this_hdr.contents;
1501 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1502 {
1503 sec->check_relocs_failed = 1;
1504 return FALSE;
1505 }
1506
0ffa91dd 1507 symtab_hdr = &elf_symtab_hdr (abfd);
252b5132 1508 sym_hashes = elf_sym_hashes (abfd);
252b5132 1509
5e5e02ae
L
1510 converted = FALSE;
1511
252b5132
RH
1512 sreloc = NULL;
1513
1514 rel_end = relocs + sec->reloc_count;
1515 for (rel = relocs; rel < rel_end; rel++)
1516 {
13ae64f3 1517 unsigned int r_type;
d42c267e 1518 unsigned int r_symndx;
252b5132 1519 struct elf_link_hash_entry *h;
0afcef53 1520 struct elf_x86_link_hash_entry *eh;
4c544807
L
1521 Elf_Internal_Sym *isym;
1522 const char *name;
06a6a421 1523 bfd_boolean size_reloc;
252b5132
RH
1524
1525 r_symndx = ELF32_R_SYM (rel->r_info);
13ae64f3 1526 r_type = ELF32_R_TYPE (rel->r_info);
252b5132 1527
d9bc7a44 1528 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
f5f31454 1529 {
695344c0 1530 /* xgettext:c-format */
4eca0228
AM
1531 _bfd_error_handler (_("%B: bad symbol index: %d"),
1532 abfd, r_symndx);
c337a162 1533 goto error_return;
f5f31454
L
1534 }
1535
252b5132 1536 if (r_symndx < symtab_hdr->sh_info)
c25bc9fc
L
1537 {
1538 /* A local symbol. */
c2e61a4e
L
1539 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1540 abfd, r_symndx);
1541 if (isym == NULL)
c337a162 1542 goto error_return;
c25bc9fc
L
1543
1544 /* Check relocation against local STT_GNU_IFUNC symbol. */
c25bc9fc
L
1545 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1546 {
0afcef53 1547 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel, TRUE);
c25bc9fc 1548 if (h == NULL)
c337a162 1549 goto error_return;
6bbec505 1550
c25bc9fc 1551 /* Fake a STT_GNU_IFUNC symbol. */
6322e5c5
L
1552 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1553 isym, NULL);
c25bc9fc
L
1554 h->type = STT_GNU_IFUNC;
1555 h->def_regular = 1;
1556 h->ref_regular = 1;
1557 h->forced_local = 1;
1558 h->root.type = bfd_link_hash_defined;
1559 }
1560 else
1561 h = NULL;
1562 }
252b5132 1563 else
71cb9464 1564 {
4c544807 1565 isym = NULL;
71cb9464
L
1566 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1567 while (h->root.type == bfd_link_hash_indirect
1568 || h->root.type == bfd_link_hash_warning)
1569 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c25bc9fc 1570 }
cbe950e9 1571
0afcef53 1572 eh = (struct elf_x86_link_hash_entry *) h;
c25bc9fc
L
1573 if (h != NULL)
1574 {
f604c2a2
L
1575 if (r_type == R_386_GOTOFF)
1576 eh->gotoff_ref = 1;
cbe950e9 1577
ad1e85de
L
1578 /* It is referenced by a non-shared object. */
1579 h->ref_regular = 1;
bc4e12de 1580 h->root.non_ir_ref_regular = 1;
13a2df29
L
1581
1582 if (h->type == STT_GNU_IFUNC)
1583 elf_tdata (info->output_bfd)->has_gnu_symbols
1584 |= elf_gnu_symbol_ifunc;
71cb9464 1585 }
252b5132 1586
6999821f
L
1587 if (r_type == R_386_GOT32X
1588 && (h == NULL || h->type != STT_GNU_IFUNC))
1589 {
1590 Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
1591 if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents,
5e5e02ae
L
1592 &r_type, irel, h,
1593 &converted, info))
6999821f
L
1594 goto error_return;
1595 }
1596
bedfd056 1597 if (! elf_i386_tls_transition (info, abfd, sec, contents,
142411ca
L
1598 symtab_hdr, sym_hashes,
1599 &r_type, GOT_UNKNOWN,
bedfd056 1600 rel, rel_end, h, r_symndx, FALSE))
c337a162 1601 goto error_return;
13ae64f3
JJ
1602
1603 switch (r_type)
252b5132 1604 {
37e55690 1605 case R_386_TLS_LDM:
0afcef53 1606 htab->tls_ld_or_ldm_got.refcount += 1;
37e55690
JJ
1607 goto create_got;
1608
1609 case R_386_PLT32:
1610 /* This symbol requires a procedure linkage table entry. We
1611 actually build the entry in adjust_dynamic_symbol,
1612 because this might be a case of linking PIC code which is
1613 never referenced by a dynamic object, in which case we
1614 don't need to generate a procedure linkage table entry
1615 after all. */
1616
1617 /* If this is a local symbol, we resolve it directly without
1618 creating a procedure linkage table entry. */
1619 if (h == NULL)
1620 continue;
1621
aec6b87e 1622 eh->has_got_reloc = 1;
f5385ebf 1623 h->needs_plt = 1;
37e55690
JJ
1624 h->plt.refcount += 1;
1625 break;
1626
6a3e1bae 1627 case R_386_SIZE32:
06a6a421 1628 size_reloc = TRUE;
6a3e1bae
L
1629 goto do_size;
1630
13ae64f3 1631 case R_386_TLS_IE_32:
37e55690
JJ
1632 case R_386_TLS_IE:
1633 case R_386_TLS_GOTIE:
0e1862bb 1634 if (!bfd_link_executable (info))
13ae64f3 1635 info->flags |= DF_STATIC_TLS;
37e55690
JJ
1636 /* Fall through */
1637
252b5132 1638 case R_386_GOT32:
02a86693 1639 case R_386_GOT32X:
13ae64f3 1640 case R_386_TLS_GD:
67a4f2b7
AO
1641 case R_386_TLS_GOTDESC:
1642 case R_386_TLS_DESC_CALL:
252b5132 1643 /* This symbol requires a global offset table entry. */
13ae64f3
JJ
1644 {
1645 int tls_type, old_tls_type;
1646
1647 switch (r_type)
1648 {
1649 default:
02a86693
L
1650 case R_386_GOT32:
1651 case R_386_GOT32X:
1652 tls_type = GOT_NORMAL;
1653 break;
13ae64f3 1654 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
67a4f2b7
AO
1655 case R_386_TLS_GOTDESC:
1656 case R_386_TLS_DESC_CALL:
1657 tls_type = GOT_TLS_GDESC; break;
37e55690
JJ
1658 case R_386_TLS_IE_32:
1659 if (ELF32_R_TYPE (rel->r_info) == r_type)
1660 tls_type = GOT_TLS_IE_NEG;
1661 else
ebcfb3c0
JJ
1662 /* If this is a GD->IE transition, we may use either of
1663 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
37e55690
JJ
1664 tls_type = GOT_TLS_IE;
1665 break;
1666 case R_386_TLS_IE:
1667 case R_386_TLS_GOTIE:
1668 tls_type = GOT_TLS_IE_POS; break;
13ae64f3
JJ
1669 }
1670
1671 if (h != NULL)
1672 {
1673 h->got.refcount += 1;
0afcef53 1674 old_tls_type = elf_x86_hash_entry (h)->tls_type;
13ae64f3
JJ
1675 }
1676 else
1677 {
1678 bfd_signed_vma *local_got_refcounts;
1679
1680 /* This is a global offset table entry for a local symbol. */
1681 local_got_refcounts = elf_local_got_refcounts (abfd);
1682 if (local_got_refcounts == NULL)
1683 {
1684 bfd_size_type size;
1685
1686 size = symtab_hdr->sh_info;
67a4f2b7
AO
1687 size *= (sizeof (bfd_signed_vma)
1688 + sizeof (bfd_vma) + sizeof(char));
a50b1753
NC
1689 local_got_refcounts = (bfd_signed_vma *)
1690 bfd_zalloc (abfd, size);
13ae64f3 1691 if (local_got_refcounts == NULL)
c337a162 1692 goto error_return;
13ae64f3 1693 elf_local_got_refcounts (abfd) = local_got_refcounts;
0afcef53 1694 elf_x86_local_tlsdesc_gotent (abfd)
67a4f2b7 1695 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
0afcef53 1696 elf_x86_local_got_tls_type (abfd)
67a4f2b7 1697 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
13ae64f3
JJ
1698 }
1699 local_got_refcounts[r_symndx] += 1;
0afcef53 1700 old_tls_type = elf_x86_local_got_tls_type (abfd) [r_symndx];
13ae64f3
JJ
1701 }
1702
37e55690
JJ
1703 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1704 tls_type |= old_tls_type;
13ae64f3
JJ
1705 /* If a TLS symbol is accessed using IE at least once,
1706 there is no point to use dynamic model for it. */
ebcfb3c0 1707 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
67a4f2b7 1708 && (! GOT_TLS_GD_ANY_P (old_tls_type)
37e55690 1709 || (tls_type & GOT_TLS_IE) == 0))
13ae64f3 1710 {
67a4f2b7 1711 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
37e55690 1712 tls_type = old_tls_type;
67a4f2b7
AO
1713 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1714 && GOT_TLS_GD_ANY_P (tls_type))
1715 tls_type |= old_tls_type;
13ae64f3
JJ
1716 else
1717 {
09a24cbf 1718 if (h)
4c544807
L
1719 name = h->root.root.string;
1720 else
1721 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1722 NULL);
4eca0228 1723 _bfd_error_handler
695344c0 1724 /* xgettext:c-format */
d003868e 1725 (_("%B: `%s' accessed both as normal and "
55fd94b0 1726 "thread local symbol"),
4c544807 1727 abfd, name);
68c4a57e 1728 bfd_set_error (bfd_error_bad_value);
c337a162 1729 goto error_return;
13ae64f3
JJ
1730 }
1731 }
1732
1733 if (old_tls_type != tls_type)
1734 {
1735 if (h != NULL)
0afcef53 1736 elf_x86_hash_entry (h)->tls_type = tls_type;
13ae64f3 1737 else
0afcef53 1738 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
13ae64f3
JJ
1739 }
1740 }
0ac8d2ca
AM
1741 /* Fall through */
1742
1743 case R_386_GOTOFF:
1744 case R_386_GOTPC:
13ae64f3 1745 create_got:
37e55690 1746 if (r_type != R_386_TLS_IE)
aec6b87e
L
1747 {
1748 if (eh != NULL)
1749 eh->has_got_reloc = 1;
1750 break;
1751 }
37e55690 1752 /* Fall through */
252b5132 1753
37e55690
JJ
1754 case R_386_TLS_LE_32:
1755 case R_386_TLS_LE:
aec6b87e
L
1756 if (eh != NULL)
1757 eh->has_got_reloc = 1;
0e1862bb 1758 if (bfd_link_executable (info))
37e55690 1759 break;
bffbf940 1760 info->flags |= DF_STATIC_TLS;
aec6b87e 1761 goto do_relocation;
252b5132
RH
1762
1763 case R_386_32:
1764 case R_386_PC32:
aec6b87e
L
1765 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
1766 eh->has_non_got_reloc = 1;
1767do_relocation:
bf52d7c7
L
1768 /* We are called after all symbols have been resolved. Only
1769 relocation against STT_GNU_IFUNC symbol must go through
1770 PLT. */
d1ed1c7d
L
1771 if (h != NULL
1772 && (bfd_link_executable (info)
bf52d7c7 1773 || h->type == STT_GNU_IFUNC))
6725bdbf 1774 {
12d0ee4a 1775 /* If this reloc is in a read-only section, we might
ebe50bae
AM
1776 need a copy reloc. We can't check reliably at this
1777 stage whether the section is read-only, as input
1778 sections have not yet been mapped to output sections.
1779 Tentatively set the flag for now, and correct in
1780 adjust_dynamic_symbol. */
f5385ebf 1781 h->non_got_ref = 1;
12d0ee4a 1782
233cc9c1
L
1783 /* We may need a .plt entry if the symbol is a function
1784 defined in a shared lib or is a STT_GNU_IFUNC function
1785 referenced from the code or read-only section. */
34bb2571
L
1786 if (!h->def_regular
1787 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
233cc9c1
L
1788 h->plt.refcount += 1;
1789
4b627c18
L
1790 if (r_type == R_386_PC32)
1791 {
1792 /* Since something like ".long foo - ." may be used
1793 as pointer, make sure that PLT is used if foo is
1794 a function defined in a shared library. */
1795 if ((sec->flags & SEC_CODE) == 0)
1796 h->pointer_equality_needed = 1;
74437ea2
L
1797 else if (h->type == STT_GNU_IFUNC
1798 && bfd_link_pic (info))
1799 {
6322e5c5
L
1800 _bfd_error_handler
1801 /* xgettext:c-format */
1802 (_("%B: unsupported non-PIC call to IFUNC `%s'"),
1803 abfd, h->root.root.string);
74437ea2
L
1804 bfd_set_error (bfd_error_bad_value);
1805 goto error_return;
1806 }
4b627c18
L
1807 }
1808 else
04ebc307
L
1809 {
1810 h->pointer_equality_needed = 1;
1811 /* R_386_32 can be resolved at run-time. */
1812 if (r_type == R_386_32
1813 && (sec->flags & SEC_READONLY) == 0)
1814 eh->func_pointer_refcount += 1;
1815 }
6725bdbf 1816 }
7843f00e 1817
06a6a421 1818 size_reloc = FALSE;
6a3e1bae 1819do_size:
daf1c414
L
1820 if (NEED_DYNAMIC_RELOCATION_P (info, h, sec, r_type,
1821 R_386_32))
252b5132 1822 {
e03a8ed8
L
1823 struct elf_dyn_relocs *p;
1824 struct elf_dyn_relocs **head;
ec338859 1825
12d0ee4a
AM
1826 /* We must copy these reloc types into the output file.
1827 Create a reloc section in dynobj and make room for
1828 this reloc. */
252b5132
RH
1829 if (sreloc == NULL)
1830 {
83bac4b0
NC
1831 sreloc = _bfd_elf_make_dynamic_reloc_section
1832 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
1833
252b5132 1834 if (sreloc == NULL)
c337a162 1835 goto error_return;
252b5132
RH
1836 }
1837
0c715baa
AM
1838 /* If this is a global symbol, we count the number of
1839 relocations we need for this symbol. */
1840 if (h != NULL)
252b5132 1841 {
d5597ebc 1842 head = &eh->dyn_relocs;
0c715baa
AM
1843 }
1844 else
1845 {
ec338859
AM
1846 /* Track dynamic relocs needed for local syms too.
1847 We really need local syms available to do this
1848 easily. Oh well. */
87d72d41 1849 void **vpp;
ec338859 1850 asection *s;
87d72d41
AM
1851
1852 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1853 abfd, r_symndx);
1854 if (isym == NULL)
c337a162 1855 goto error_return;
87d72d41
AM
1856
1857 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
ec338859 1858 if (s == NULL)
87d72d41 1859 s = sec;
ec338859 1860
e81d3500 1861 vpp = &elf_section_data (s)->local_dynrel;
e03a8ed8 1862 head = (struct elf_dyn_relocs **)vpp;
ec338859
AM
1863 }
1864
1865 p = *head;
1866 if (p == NULL || p->sec != sec)
1867 {
1868 bfd_size_type amt = sizeof *p;
a50b1753
NC
1869 p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
1870 amt);
ec338859 1871 if (p == NULL)
c337a162 1872 goto error_return;
ec338859
AM
1873 p->next = *head;
1874 *head = p;
1875 p->sec = sec;
1876 p->count = 0;
1877 p->pc_count = 0;
252b5132 1878 }
ec338859
AM
1879
1880 p->count += 1;
06a6a421
L
1881 /* Count size relocation as PC-relative relocation. */
1882 if (r_type == R_386_PC32 || size_reloc)
ec338859 1883 p->pc_count += 1;
252b5132 1884 }
252b5132
RH
1885 break;
1886
1887 /* This relocation describes the C++ object vtable hierarchy.
1888 Reconstruct it for later use during GC. */
1889 case R_386_GNU_VTINHERIT:
d17e0c6e 1890 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
c337a162 1891 goto error_return;
252b5132
RH
1892 break;
1893
1894 /* This relocation describes which C++ vtable entries are actually
1895 used. Record for later use during GC. */
1896 case R_386_GNU_VTENTRY:
c6aa130f
MS
1897 BFD_ASSERT (h != NULL);
1898 if (h != NULL
1899 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
c337a162 1900 goto error_return;
252b5132
RH
1901 break;
1902
1903 default:
1904 break;
1905 }
1906 }
1907
bedfd056
L
1908 if (elf_section_data (sec)->this_hdr.contents != contents)
1909 {
5e5e02ae 1910 if (!converted && !info->keep_memory)
bedfd056
L
1911 free (contents);
1912 else
1913 {
5e5e02ae
L
1914 /* Cache the section contents for elf_link_input_bfd if any
1915 load is converted or --no-keep-memory isn't used. */
bedfd056
L
1916 elf_section_data (sec)->this_hdr.contents = contents;
1917 }
1918 }
1919
5e5e02ae
L
1920 /* Cache relocations if any load is converted. */
1921 if (elf_section_data (sec)->relocs != relocs && converted)
1922 elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
1923
b34976b6 1924 return TRUE;
c337a162
L
1925
1926error_return:
bedfd056
L
1927 if (elf_section_data (sec)->this_hdr.contents != contents)
1928 free (contents);
c337a162
L
1929 sec->check_relocs_failed = 1;
1930 return FALSE;
252b5132
RH
1931}
1932
38701953
AM
1933/* Set the correct type for an x86 ELF section. We do this by the
1934 section name, which is a hack, but ought to work. */
1935
b34976b6 1936static bfd_boolean
55fd94b0
AM
1937elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1938 Elf_Internal_Shdr *hdr,
1939 asection *sec)
38701953 1940{
91d6fa6a 1941 const char *name;
38701953
AM
1942
1943 name = bfd_get_section_name (abfd, sec);
1944
1945 /* This is an ugly, but unfortunately necessary hack that is
1946 needed when producing EFI binaries on x86. It tells
1947 elf.c:elf_fake_sections() not to consider ".reloc" as a section
1948 containing ELF relocation info. We need this hack in order to
1949 be able to generate ELF binaries that can be translated into
1950 EFI applications (which are essentially COFF objects). Those
1951 files contain a COFF ".reloc" section inside an ELFNN object,
1952 which would normally cause BFD to segfault because it would
1953 attempt to interpret this section as containing relocation
1954 entries for section "oc". With this hack enabled, ".reloc"
1955 will be treated as a normal data section, which will avoid the
1956 segfault. However, you won't be able to create an ELFNN binary
1957 with a section named "oc" that needs relocations, but that's
1958 the kind of ugly side-effects you get when detecting section
1959 types based on their names... In practice, this limitation is
1960 unlikely to bite. */
1961 if (strcmp (name, ".reloc") == 0)
1962 hdr->sh_type = SHT_PROGBITS;
1963
b34976b6 1964 return TRUE;
38701953
AM
1965}
1966
13ae64f3
JJ
1967/* Return the relocation value for @tpoff relocation
1968 if STT_TLS virtual address is ADDRESS. */
1969
1970static bfd_vma
eb4ff4d6 1971elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
13ae64f3 1972{
e1918d23 1973 struct elf_link_hash_table *htab = elf_hash_table (info);
7dc98aea
RO
1974 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
1975 bfd_vma static_tls_size;
13ae64f3 1976
e1918d23
AM
1977 /* If tls_sec is NULL, we should have signalled an error already. */
1978 if (htab->tls_sec == NULL)
6a30718d 1979 return 0;
7dc98aea
RO
1980
1981 /* Consider special static TLS alignment requirements. */
1982 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
1983 return static_tls_size + htab->tls_sec->vma - address;
13ae64f3
JJ
1984}
1985
252b5132
RH
1986/* Relocate an i386 ELF section. */
1987
b34976b6 1988static bfd_boolean
55fd94b0
AM
1989elf_i386_relocate_section (bfd *output_bfd,
1990 struct bfd_link_info *info,
1991 bfd *input_bfd,
1992 asection *input_section,
1993 bfd_byte *contents,
1994 Elf_Internal_Rela *relocs,
1995 Elf_Internal_Sym *local_syms,
1996 asection **local_sections)
252b5132 1997{
0afcef53 1998 struct elf_x86_link_hash_table *htab;
252b5132
RH
1999 Elf_Internal_Shdr *symtab_hdr;
2000 struct elf_link_hash_entry **sym_hashes;
2001 bfd_vma *local_got_offsets;
67a4f2b7 2002 bfd_vma *local_tlsdesc_gotents;
252b5132 2003 Elf_Internal_Rela *rel;
60f2e42e 2004 Elf_Internal_Rela *wrel;
252b5132 2005 Elf_Internal_Rela *relend;
3348747a 2006 bfd_boolean is_vxworks_tls;
25e762b9 2007 unsigned plt_entry_size;
252b5132 2008
c337a162
L
2009 /* Skip if check_relocs failed. */
2010 if (input_section->check_relocs_failed)
2011 return FALSE;
2012
0afcef53 2013 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6
NC
2014 if (htab == NULL)
2015 return FALSE;
fe53b4a4
L
2016
2017 BFD_ASSERT (is_x86_elf (input_bfd, htab));
2018
0ffa91dd 2019 symtab_hdr = &elf_symtab_hdr (input_bfd);
252b5132
RH
2020 sym_hashes = elf_sym_hashes (input_bfd);
2021 local_got_offsets = elf_local_got_offsets (input_bfd);
0afcef53 2022 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
3348747a
NS
2023 /* We have to handle relocations in vxworks .tls_vars sections
2024 specially, because the dynamic loader is 'weird'. */
9a742a90 2025 is_vxworks_tls = (htab->is_vxworks
0e1862bb 2026 && bfd_link_pic (info)
3348747a
NS
2027 && !strcmp (input_section->output_section->name,
2028 ".tls_vars"));
252b5132 2029
0afcef53 2030 _bfd_x86_elf_set_tls_module_base (info);
9f03412a 2031
765e526c 2032 plt_entry_size = htab->plt.plt_entry_size;
25e762b9 2033
60f2e42e 2034 rel = wrel = relocs;
252b5132 2035 relend = relocs + input_section->reloc_count;
60f2e42e 2036 for (; rel < relend; wrel++, rel++)
252b5132 2037 {
419414ea 2038 unsigned int r_type, r_type_tls;
252b5132
RH
2039 reloc_howto_type *howto;
2040 unsigned long r_symndx;
2041 struct elf_link_hash_entry *h;
0afcef53 2042 struct elf_x86_link_hash_entry *eh;
252b5132
RH
2043 Elf_Internal_Sym *sym;
2044 asection *sec;
dd7e64d4 2045 bfd_vma off, offplt, plt_offset;
252b5132 2046 bfd_vma relocation;
b34976b6 2047 bfd_boolean unresolved_reloc;
252b5132 2048 bfd_reloc_status_type r;
1b452ec6 2049 unsigned int indx;
13ae64f3 2050 int tls_type;
1788fc08 2051 bfd_vma st_size;
dd7e64d4 2052 asection *resolved_plt;
aec6b87e 2053 bfd_boolean resolved_to_zero;
e133d005 2054 bfd_boolean relative_reloc;
252b5132
RH
2055
2056 r_type = ELF32_R_TYPE (rel->r_info);
55fd94b0
AM
2057 if (r_type == R_386_GNU_VTINHERIT
2058 || r_type == R_386_GNU_VTENTRY)
18954b29
L
2059 {
2060 if (wrel != rel)
2061 *wrel = *rel;
2062 continue;
2063 }
dc47f327 2064
55fd94b0 2065 if ((indx = r_type) >= R_386_standard
13ae64f3
JJ
2066 && ((indx = r_type - R_386_ext_offset) - R_386_standard
2067 >= R_386_ext - R_386_standard)
2068 && ((indx = r_type - R_386_tls_offset) - R_386_ext
c74be520 2069 >= R_386_ext2 - R_386_ext))
47aeb64c
NC
2070 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
2071
1b452ec6 2072 howto = elf_howto_table + indx;
252b5132
RH
2073
2074 r_symndx = ELF32_R_SYM (rel->r_info);
252b5132
RH
2075 h = NULL;
2076 sym = NULL;
2077 sec = NULL;
b34976b6 2078 unresolved_reloc = FALSE;
252b5132
RH
2079 if (r_symndx < symtab_hdr->sh_info)
2080 {
2081 sym = local_syms + r_symndx;
2082 sec = local_sections[r_symndx];
2083 relocation = (sec->output_section->vma
2084 + sec->output_offset
2085 + sym->st_value);
1788fc08 2086 st_size = sym->st_size;
ab96bf03
AM
2087
2088 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2089 && ((sec->flags & SEC_MERGE) != 0
0e1862bb 2090 || (bfd_link_relocatable (info)
ab96bf03 2091 && sec->output_offset != 0)))
f8df10f4 2092 {
f8df10f4 2093 bfd_vma addend;
4a335f3d 2094 bfd_byte *where = contents + rel->r_offset;
f8df10f4 2095
4a335f3d 2096 switch (howto->size)
f8df10f4 2097 {
4a335f3d
AM
2098 case 0:
2099 addend = bfd_get_8 (input_bfd, where);
2100 if (howto->pc_relative)
2101 {
2102 addend = (addend ^ 0x80) - 0x80;
2103 addend += 1;
2104 }
2105 break;
2106 case 1:
2107 addend = bfd_get_16 (input_bfd, where);
2108 if (howto->pc_relative)
2109 {
2110 addend = (addend ^ 0x8000) - 0x8000;
2111 addend += 2;
2112 }
2113 break;
2114 case 2:
2115 addend = bfd_get_32 (input_bfd, where);
2116 if (howto->pc_relative)
2117 {
2118 addend = (addend ^ 0x80000000) - 0x80000000;
2119 addend += 4;
2120 }
2121 break;
2122 default:
2123 abort ();
f8df10f4
JJ
2124 }
2125
0e1862bb 2126 if (bfd_link_relocatable (info))
ab96bf03
AM
2127 addend += sec->output_offset;
2128 else
2129 {
2130 asection *msec = sec;
2131 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2132 addend);
2133 addend -= relocation;
2134 addend += msec->output_section->vma + msec->output_offset;
2135 }
4a335f3d
AM
2136
2137 switch (howto->size)
2138 {
2139 case 0:
16a10388 2140 /* FIXME: overflow checks. */
4a335f3d
AM
2141 if (howto->pc_relative)
2142 addend -= 1;
2143 bfd_put_8 (input_bfd, addend, where);
4a335f3d
AM
2144 break;
2145 case 1:
2146 if (howto->pc_relative)
2147 addend -= 2;
2148 bfd_put_16 (input_bfd, addend, where);
4a335f3d
AM
2149 break;
2150 case 2:
2151 if (howto->pc_relative)
2152 addend -= 4;
2153 bfd_put_32 (input_bfd, addend, where);
2154 break;
2155 }
f8df10f4 2156 }
0e1862bb 2157 else if (!bfd_link_relocatable (info)
1f85278f 2158 && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
c25bc9fc
L
2159 {
2160 /* Relocate against local STT_GNU_IFUNC symbol. */
0afcef53
L
2161 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd, rel,
2162 FALSE);
c25bc9fc
L
2163 if (h == NULL)
2164 abort ();
2165
23209a78 2166 /* Set STT_GNU_IFUNC symbol value. */
c25bc9fc
L
2167 h->root.u.def.value = sym->st_value;
2168 h->root.u.def.section = sec;
2169 }
252b5132
RH
2170 }
2171 else
2172 {
3d540e93 2173 bfd_boolean warned ATTRIBUTE_UNUSED;
62d887d4 2174 bfd_boolean ignored ATTRIBUTE_UNUSED;
ffb2e45b 2175
b2a8e766
AM
2176 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2177 r_symndx, symtab_hdr, sym_hashes,
2178 h, sec, relocation,
62d887d4 2179 unresolved_reloc, warned, ignored);
1788fc08 2180 st_size = h->size;
252b5132
RH
2181 }
2182
dbaa2011 2183 if (sec != NULL && discarded_section (sec))
60f2e42e
L
2184 {
2185 _bfd_clear_contents (howto, input_bfd, input_section,
2186 contents + rel->r_offset);
2187 wrel->r_offset = rel->r_offset;
2188 wrel->r_info = 0;
2189 wrel->r_addend = 0;
2190
2191 /* For ld -r, remove relocations in debug sections against
2192 sections defined in discarded sections. Not done for
2193 eh_frame editing code expects to be present. */
2194 if (bfd_link_relocatable (info)
2195 && (input_section->flags & SEC_DEBUGGING))
2196 wrel--;
2197
2198 continue;
2199 }
9635fe29 2200
0e1862bb 2201 if (bfd_link_relocatable (info))
2d5da473
L
2202 {
2203 if (wrel != rel)
2204 *wrel = *rel;
2205 continue;
2206 }
ab96bf03 2207
0afcef53 2208 eh = (struct elf_x86_link_hash_entry *) h;
ee2fdd6f 2209
cbe950e9
L
2210 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2211 it here if it is defined in a non-shared object. */
2212 if (h != NULL
2213 && h->type == STT_GNU_IFUNC
2214 && h->def_regular)
2215 {
ee2fdd6f 2216 asection *gotplt, *base_got;
cbe950e9 2217 bfd_vma plt_index;
4c544807 2218 const char *name;
cbe950e9 2219
97dc35c8
L
2220 if ((input_section->flags & SEC_ALLOC) == 0)
2221 {
2222 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2223 sections because such sections are not SEC_ALLOC and
2224 thus ld.so will not process them. */
2225 if ((input_section->flags & SEC_DEBUGGING) != 0)
0eace210 2226 continue;
97dc35c8
L
2227 abort ();
2228 }
cbe950e9
L
2229
2230 /* STT_GNU_IFUNC symbol must go through PLT. */
6de2ae4a 2231 if (htab->elf.splt != NULL)
cbe950e9 2232 {
ee2fdd6f
L
2233 if (htab->plt_second != NULL)
2234 {
2235 resolved_plt = htab->plt_second;
2236 plt_offset = eh->plt_second.offset;
2237 }
2238 else
2239 {
2240 resolved_plt = htab->elf.splt;
2241 plt_offset = h->plt.offset;
2242 }
6de2ae4a 2243 gotplt = htab->elf.sgotplt;
cbe950e9
L
2244 }
2245 else
2246 {
ee2fdd6f
L
2247 resolved_plt = htab->elf.iplt;
2248 plt_offset = h->plt.offset;
6de2ae4a 2249 gotplt = htab->elf.igotplt;
cbe950e9
L
2250 }
2251
cbe950e9
L
2252 switch (r_type)
2253 {
2254 default:
233cc9c1 2255 break;
cbe950e9
L
2256
2257 case R_386_GOT32:
02a86693 2258 case R_386_GOT32X:
6de2ae4a 2259 base_got = htab->elf.sgot;
cbe950e9
L
2260 off = h->got.offset;
2261
7afd84dc 2262 if (base_got == NULL)
cbe950e9
L
2263 abort ();
2264
7afd84dc 2265 if (off == (bfd_vma) -1)
cbe950e9 2266 {
7afd84dc
L
2267 /* We can't use h->got.offset here to save state, or
2268 even just remember the offset, as finish_dynamic_symbol
2269 would use that as offset into .got. */
6bbec505 2270
233cc9c1
L
2271 if (h->plt.offset == (bfd_vma) -1)
2272 abort ();
2273
6de2ae4a 2274 if (htab->elf.splt != NULL)
7afd84dc 2275 {
f604c2a2 2276 plt_index = (h->plt.offset / plt_entry_size
765e526c 2277 - htab->plt.has_plt0);
7afd84dc 2278 off = (plt_index + 3) * 4;
6de2ae4a 2279 base_got = htab->elf.sgotplt;
7afd84dc 2280 }
cbe950e9
L
2281 else
2282 {
25e762b9 2283 plt_index = h->plt.offset / plt_entry_size;
7afd84dc 2284 off = plt_index * 4;
6de2ae4a 2285 base_got = htab->elf.igotplt;
7afd84dc
L
2286 }
2287
2288 if (h->dynindx == -1
2289 || h->forced_local
2290 || info->symbolic)
2291 {
2292 /* This references the local defitionion. We must
2293 initialize this entry in the global offset table.
2294 Since the offset must always be a multiple of 8,
2295 we use the least significant bit to record
2296 whether we have initialized it already.
2297
2298 When doing a dynamic link, we create a .rela.got
2299 relocation entry to initialize the value. This
2300 is done in the finish_dynamic_symbol routine. */
2301 if ((off & 1) != 0)
2302 off &= ~1;
2303 else
2304 {
2305 bfd_put_32 (output_bfd, relocation,
2306 base_got->contents + off);
2307 h->got.offset |= 1;
2308 }
cbe950e9 2309 }
cbe950e9 2310
7afd84dc 2311 relocation = off;
7afd84dc
L
2312 }
2313 else
ca8c86ef
L
2314 relocation = (base_got->output_section->vma
2315 + base_got->output_offset + off
2316 - gotplt->output_section->vma
2317 - gotplt->output_offset);
2318
2a568401
L
2319 if (rel->r_offset > 1
2320 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2321 && *(contents + rel->r_offset - 2) != 0x8d)
712ec279
L
2322 {
2323 if (bfd_link_pic (info))
2324 goto disallow_got32;
2325
2326 /* Add the GOT base if there is no base register. */
2327 relocation += (gotplt->output_section->vma
2328 + gotplt->output_offset);
2329 }
2330 else if (htab->elf.splt == NULL)
2331 {
2332 /* Adjust for static executables. */
2333 relocation += gotplt->output_offset;
2334 }
0018b0a3 2335
cbe950e9 2336 goto do_relocation;
233cc9c1
L
2337 }
2338
2339 if (h->plt.offset == (bfd_vma) -1)
2340 {
2341 /* Handle static pointers of STT_GNU_IFUNC symbols. */
2342 if (r_type == R_386_32
2343 && (input_section->flags & SEC_CODE) == 0)
2344 goto do_ifunc_pointer;
2345 goto bad_ifunc_reloc;
2346 }
2347
ee2fdd6f
L
2348 relocation = (resolved_plt->output_section->vma
2349 + resolved_plt->output_offset + plt_offset);
233cc9c1
L
2350
2351 switch (r_type)
2352 {
2353 default:
2354bad_ifunc_reloc:
2355 if (h->root.root.string)
2356 name = h->root.root.string;
2357 else
2358 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2359 NULL);
4eca0228 2360 _bfd_error_handler
695344c0 2361 /* xgettext:c-format */
233cc9c1
L
2362 (_("%B: relocation %s against STT_GNU_IFUNC "
2363 "symbol `%s' isn't supported"), input_bfd,
2364 howto->name, name);
2365 bfd_set_error (bfd_error_bad_value);
2366 return FALSE;
2367
2368 case R_386_32:
2369 /* Generate dynamic relcoation only when there is a
2370 non-GOT reference in a shared object. */
2371 if ((bfd_link_pic (info) && h->non_got_ref)
2372 || h->plt.offset == (bfd_vma) -1)
2373 {
2374 Elf_Internal_Rela outrel;
2375 asection *sreloc;
2376 bfd_vma offset;
2377
2378do_ifunc_pointer:
2379 /* Need a dynamic relocation to get the real function
2380 adddress. */
2381 offset = _bfd_elf_section_offset (output_bfd,
2382 info,
2383 input_section,
2384 rel->r_offset);
2385 if (offset == (bfd_vma) -1
2386 || offset == (bfd_vma) -2)
2387 abort ();
2388
2389 outrel.r_offset = (input_section->output_section->vma
2390 + input_section->output_offset
2391 + offset);
2392
cf1070f1 2393 if (POINTER_LOCAL_IFUNC_P (info, h))
233cc9c1 2394 {
6322e5c5
L
2395 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
2396 h->root.root.string,
2397 h->root.u.def.section->owner);
2398
233cc9c1
L
2399 /* This symbol is resolved locally. */
2400 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
2401 bfd_put_32 (output_bfd,
2402 (h->root.u.def.value
2403 + h->root.u.def.section->output_section->vma
2404 + h->root.u.def.section->output_offset),
2405 contents + offset);
2406 }
2407 else
2408 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2409
2410 /* Dynamic relocations are stored in
2411 1. .rel.ifunc section in PIC object.
2412 2. .rel.got section in dynamic executable.
2413 3. .rel.iplt section in static executable. */
2414 if (bfd_link_pic (info))
2415 sreloc = htab->elf.irelifunc;
2416 else if (htab->elf.splt != NULL)
2417 sreloc = htab->elf.srelgot;
2418 else
2419 sreloc = htab->elf.irelplt;
2420 elf_append_rel (output_bfd, sreloc, &outrel);
2421
2422 /* If this reloc is against an external symbol, we
2423 do not want to fiddle with the addend. Otherwise,
2424 we need to include the symbol value so that it
2425 becomes an addend for the dynamic reloc. For an
2426 internal symbol, we have updated addend. */
2427 continue;
2428 }
2429 /* FALLTHROUGH */
2430 case R_386_PC32:
2431 case R_386_PLT32:
2432 goto do_relocation;
cbe950e9
L
2433
2434 case R_386_GOTOFF:
2435 relocation -= (gotplt->output_section->vma
2436 + gotplt->output_offset);
2437 goto do_relocation;
2438 }
2439 }
2440
aec6b87e 2441 resolved_to_zero = (eh != NULL
c5bce5c6 2442 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
aec6b87e 2443
252b5132
RH
2444 switch (r_type)
2445 {
02a86693
L
2446 case R_386_GOT32X:
2447 /* Avoid optimizing _DYNAMIC since ld.so may use its
2448 link-time address. */
2449 if (h == htab->elf.hdynamic)
2450 goto r_386_got32;
2451
2452 if (bfd_link_pic (info))
2453 {
2454 /* It is OK to convert mov to lea and convert indirect
2455 branch to direct branch. It is OK to convert adc,
2456 add, and, cmp, or, sbb, sub, test, xor only when PIC
2457 is false. */
02e2aef8
L
2458 unsigned int opcode, addend;
2459 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
2460 if (addend != 0)
2461 goto r_386_got32;
2462 opcode = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
02a86693
L
2463 if (opcode != 0x8b && opcode != 0xff)
2464 goto r_386_got32;
2465 }
2466
2467 /* Resolve "mov GOT[(%reg)], %reg",
2468 "call/jmp *GOT[(%reg)]", "test %reg, foo@GOT[(%reg)]"
2469 and "binop foo@GOT[(%reg)], %reg". */
2470 if (h == NULL
2471 || (h->plt.offset == (bfd_vma) -1
2472 && h->got.offset == (bfd_vma) -1)
2473 || htab->elf.sgotplt == NULL)
2474 abort ();
2475
2476 offplt = (htab->elf.sgotplt->output_section->vma
2477 + htab->elf.sgotplt->output_offset);
2478
2479 /* It is relative to .got.plt section. */
2480 if (h->got.offset != (bfd_vma) -1)
32875eb1
L
2481 /* Use GOT entry. Mask off the least significant bit in
2482 GOT offset which may be set by R_386_GOT32 processing
2483 below. */
02a86693
L
2484 relocation = (htab->elf.sgot->output_section->vma
2485 + htab->elf.sgot->output_offset
32875eb1 2486 + (h->got.offset & ~1) - offplt);
02a86693
L
2487 else
2488 /* Use GOTPLT entry. */
f604c2a2 2489 relocation = (h->plt.offset / plt_entry_size
765e526c 2490 - htab->plt.has_plt0 + 3) * 4;
02a86693
L
2491
2492 if (!bfd_link_pic (info))
2493 {
2494 /* If not PIC, add the .got.plt section address for
a3718e9e 2495 baseless addressing. */
02a86693 2496 unsigned int modrm;
02e2aef8 2497 modrm = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
02a86693
L
2498 if ((modrm & 0xc7) == 0x5)
2499 relocation += offplt;
2500 }
2501
2502 unresolved_reloc = FALSE;
2503 break;
2504
252b5132 2505 case R_386_GOT32:
02a86693 2506r_386_got32:
252b5132
RH
2507 /* Relocation is to the entry for this symbol in the global
2508 offset table. */
6de2ae4a 2509 if (htab->elf.sgot == NULL)
ffb2e45b 2510 abort ();
252b5132 2511
e133d005 2512 relative_reloc = FALSE;
252b5132
RH
2513 if (h != NULL)
2514 {
252b5132 2515 off = h->got.offset;
2eba97c2 2516 if (RESOLVED_LOCALLY_P (info, h, htab))
252b5132 2517 {
2eba97c2
L
2518 /* We must initialize this entry in the global offset
2519 table. Since the offset must always be a multiple
2520 of 4, we use the least significant bit to record
2521 whether we have initialized it already.
252b5132
RH
2522
2523 When doing a dynamic link, we create a .rel.got
2524 relocation entry to initialize the value. This
2525 is done in the finish_dynamic_symbol routine. */
2526 if ((off & 1) != 0)
2527 off &= ~1;
2528 else
2529 {
2530 bfd_put_32 (output_bfd, relocation,
6de2ae4a 2531 htab->elf.sgot->contents + off);
252b5132 2532 h->got.offset |= 1;
e133d005 2533
f70656b2 2534 if (GENERATE_RELATIVE_RELOC_P (info, h))
e133d005
L
2535 {
2536 /* PR ld/21402: If this symbol isn't dynamic
0dc9a308 2537 in PIC, generate R_386_RELATIVE here. */
e133d005
L
2538 eh->no_finish_dynamic_symbol = 1;
2539 relative_reloc = TRUE;
2540 }
252b5132
RH
2541 }
2542 }
8c694914 2543 else
b34976b6 2544 unresolved_reloc = FALSE;
252b5132
RH
2545 }
2546 else
2547 {
ffb2e45b
AM
2548 if (local_got_offsets == NULL)
2549 abort ();
252b5132
RH
2550
2551 off = local_got_offsets[r_symndx];
2552
2553 /* The offset must always be a multiple of 4. We use
83be169b
AM
2554 the least significant bit to record whether we have
2555 already generated the necessary reloc. */
252b5132
RH
2556 if ((off & 1) != 0)
2557 off &= ~1;
2558 else
2559 {
6725bdbf 2560 bfd_put_32 (output_bfd, relocation,
6de2ae4a 2561 htab->elf.sgot->contents + off);
e133d005 2562 local_got_offsets[r_symndx] |= 1;
252b5132 2563
0e1862bb 2564 if (bfd_link_pic (info))
e133d005 2565 relative_reloc = TRUE;
252b5132 2566 }
252b5132
RH
2567 }
2568
e133d005
L
2569 if (relative_reloc)
2570 {
2571 asection *s;
2572 Elf_Internal_Rela outrel;
2573
2574 s = htab->elf.srelgot;
2575 if (s == NULL)
2576 abort ();
2577
2578 outrel.r_offset = (htab->elf.sgot->output_section->vma
2579 + htab->elf.sgot->output_offset
2580 + off);
2581 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2582 elf_append_rel (output_bfd, s, &outrel);
2583 }
2584
ffb2e45b
AM
2585 if (off >= (bfd_vma) -2)
2586 abort ();
2587
74d7f0aa
L
2588 relocation = (htab->elf.sgot->output_section->vma
2589 + htab->elf.sgot->output_offset + off);
2a568401
L
2590 if (rel->r_offset > 1
2591 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2592 && *(contents + rel->r_offset - 2) != 0x8d)
74d7f0aa
L
2593 {
2594 if (bfd_link_pic (info))
2595 {
2596 /* For PIC, disallow R_386_GOT32 without a base
2a568401
L
2597 register, except for "lea foo@GOT, %reg", since
2598 we don't know what the GOT base is. */
74d7f0aa
L
2599 const char *name;
2600
712ec279 2601disallow_got32:
a5def14f 2602 if (h == NULL || h->root.root.string == NULL)
74d7f0aa
L
2603 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2604 NULL);
2605 else
2606 name = h->root.root.string;
2607
4eca0228 2608 _bfd_error_handler
695344c0 2609 /* xgettext:c-format */
63a5468a
AM
2610 (_("%B: direct GOT relocation %s against `%s'"
2611 " without base register can not be used"
2612 " when making a shared object"),
712ec279 2613 input_bfd, howto->name, name);
74d7f0aa
L
2614 bfd_set_error (bfd_error_bad_value);
2615 return FALSE;
2616 }
2617 }
2618 else
2619 {
2620 /* Subtract the .got.plt section address only with a base
2621 register. */
2622 relocation -= (htab->elf.sgotplt->output_section->vma
2623 + htab->elf.sgotplt->output_offset);
2624 }
2625
252b5132
RH
2626 break;
2627
2628 case R_386_GOTOFF:
2629 /* Relocation is relative to the start of the global offset
2630 table. */
2631
3d949995
L
2632 /* Check to make sure it isn't a protected function or data
2633 symbol for shared library since it may not be local when
2634 used as function address or with copy relocation. We also
e3c0e327 2635 need to make sure that a symbol is referenced locally. */
0e1862bb 2636 if (!bfd_link_executable (info) && h)
90f487df 2637 {
41bed6dd
L
2638 if (!h->def_regular)
2639 {
2640 const char *v;
2641
2642 switch (ELF_ST_VISIBILITY (h->other))
2643 {
2644 case STV_HIDDEN:
2645 v = _("hidden symbol");
2646 break;
2647 case STV_INTERNAL:
2648 v = _("internal symbol");
2649 break;
2650 case STV_PROTECTED:
2651 v = _("protected symbol");
2652 break;
2653 default:
2654 v = _("symbol");
2655 break;
2656 }
2657
4eca0228 2658 _bfd_error_handler
695344c0 2659 /* xgettext:c-format */
63a5468a
AM
2660 (_("%B: relocation R_386_GOTOFF against undefined %s"
2661 " `%s' can not be used when making a shared object"),
41bed6dd
L
2662 input_bfd, v, h->root.root.string);
2663 bfd_set_error (bfd_error_bad_value);
2664 return FALSE;
2665 }
6999821f 2666 else if (!SYMBOL_REFERENCES_LOCAL_P (info, h)
3d949995
L
2667 && (h->type == STT_FUNC
2668 || h->type == STT_OBJECT)
41bed6dd
L
2669 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2670 {
4eca0228 2671 _bfd_error_handler
695344c0 2672 /* xgettext:c-format */
63a5468a
AM
2673 (_("%B: relocation R_386_GOTOFF against protected %s"
2674 " `%s' can not be used when making a shared object"),
3d949995
L
2675 input_bfd,
2676 h->type == STT_FUNC ? "function" : "data",
2677 h->root.root.string);
41bed6dd
L
2678 bfd_set_error (bfd_error_bad_value);
2679 return FALSE;
2680 }
90f487df
L
2681 }
2682
8c37241b
JJ
2683 /* Note that sgot is not involved in this
2684 calculation. We always want the start of .got.plt. If we
2685 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
252b5132
RH
2686 permitted by the ABI, we might have to change this
2687 calculation. */
6de2ae4a
L
2688 relocation -= htab->elf.sgotplt->output_section->vma
2689 + htab->elf.sgotplt->output_offset;
252b5132
RH
2690 break;
2691
2692 case R_386_GOTPC:
2693 /* Use global offset table as symbol value. */
6de2ae4a
L
2694 relocation = htab->elf.sgotplt->output_section->vma
2695 + htab->elf.sgotplt->output_offset;
b34976b6 2696 unresolved_reloc = FALSE;
252b5132
RH
2697 break;
2698
2699 case R_386_PLT32:
2700 /* Relocation is to the entry for this symbol in the
2701 procedure linkage table. */
2702
dd5724d5 2703 /* Resolve a PLT32 reloc against a local symbol directly,
83be169b 2704 without using the procedure linkage table. */
252b5132
RH
2705 if (h == NULL)
2706 break;
2707
dd7e64d4
L
2708 if ((h->plt.offset == (bfd_vma) -1
2709 && eh->plt_got.offset == (bfd_vma) -1)
6de2ae4a 2710 || htab->elf.splt == NULL)
252b5132
RH
2711 {
2712 /* We didn't make a PLT entry for this symbol. This
83be169b
AM
2713 happens when statically linking PIC code, or when
2714 using -Bsymbolic. */
252b5132
RH
2715 break;
2716 }
2717
dd7e64d4
L
2718 if (h->plt.offset != (bfd_vma) -1)
2719 {
ee2fdd6f
L
2720 if (htab->plt_second != NULL)
2721 {
2722 resolved_plt = htab->plt_second;
2723 plt_offset = eh->plt_second.offset;
2724 }
2725 else
2726 {
2727 resolved_plt = htab->elf.splt;
2728 plt_offset = h->plt.offset;
2729 }
dd7e64d4
L
2730 }
2731 else
2732 {
2733 resolved_plt = htab->plt_got;
2734 plt_offset = eh->plt_got.offset;
2735 }
2736
2737 relocation = (resolved_plt->output_section->vma
2738 + resolved_plt->output_offset
2739 + plt_offset);
b34976b6 2740 unresolved_reloc = FALSE;
252b5132
RH
2741 break;
2742
1788fc08 2743 case R_386_SIZE32:
1788fc08
L
2744 /* Set to symbol size. */
2745 relocation = st_size;
2746 /* Fall through. */
2747
252b5132
RH
2748 case R_386_32:
2749 case R_386_PC32:
3348747a
NS
2750 if ((input_section->flags & SEC_ALLOC) == 0
2751 || is_vxworks_tls)
ec338859
AM
2752 break;
2753
aebcc8ff
L
2754 if (GENERATE_DYNAMIC_RELOCATION_P (info, eh, r_type,
2755 FALSE, resolved_to_zero,
2756 (r_type == R_386_PC32)))
252b5132 2757 {
947216bf 2758 Elf_Internal_Rela outrel;
b34976b6 2759 bfd_boolean skip, relocate;
0c715baa 2760 asection *sreloc;
252b5132
RH
2761
2762 /* When generating a shared object, these relocations
2763 are copied into the output file to be resolved at run
2764 time. */
2765
b34976b6
AM
2766 skip = FALSE;
2767 relocate = FALSE;
252b5132 2768
c629eae0
JJ
2769 outrel.r_offset =
2770 _bfd_elf_section_offset (output_bfd, info, input_section,
2771 rel->r_offset);
2772 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 2773 skip = TRUE;
0bb2d96a 2774 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 2775 skip = TRUE, relocate = TRUE;
252b5132
RH
2776 outrel.r_offset += (input_section->output_section->vma
2777 + input_section->output_offset);
2778
2779 if (skip)
0bb2d96a 2780 memset (&outrel, 0, sizeof outrel);
e74399c4 2781 else if (COPY_INPUT_RELOC_P (info, h, r_type))
0bb2d96a 2782 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
252b5132
RH
2783 else
2784 {
5a15f56f 2785 /* This symbol is local, or marked to become local. */
b34976b6 2786 relocate = TRUE;
5a15f56f 2787 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
252b5132
RH
2788 }
2789
cbe950e9 2790 sreloc = elf_section_data (input_section)->sreloc;
e7c33416 2791
62d78908
L
2792 if (sreloc == NULL || sreloc->contents == NULL)
2793 {
2794 r = bfd_reloc_notsupported;
2795 goto check_relocation_error;
2796 }
0c715baa 2797
59d6ffb2 2798 elf_append_rel (output_bfd, sreloc, &outrel);
252b5132
RH
2799
2800 /* If this reloc is against an external symbol, we do
2801 not want to fiddle with the addend. Otherwise, we
2802 need to include the symbol value so that it becomes
2803 an addend for the dynamic reloc. */
0f88be7a 2804 if (! relocate)
252b5132
RH
2805 continue;
2806 }
252b5132
RH
2807 break;
2808
37e55690 2809 case R_386_TLS_IE:
0e1862bb 2810 if (!bfd_link_executable (info))
37e55690 2811 {
947216bf 2812 Elf_Internal_Rela outrel;
37e55690 2813 asection *sreloc;
37e55690
JJ
2814
2815 outrel.r_offset = rel->r_offset
2816 + input_section->output_section->vma
2817 + input_section->output_offset;
2818 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2819 sreloc = elf_section_data (input_section)->sreloc;
2820 if (sreloc == NULL)
2821 abort ();
59d6ffb2 2822 elf_append_rel (output_bfd, sreloc, &outrel);
37e55690
JJ
2823 }
2824 /* Fall through */
2825
13ae64f3 2826 case R_386_TLS_GD:
67a4f2b7
AO
2827 case R_386_TLS_GOTDESC:
2828 case R_386_TLS_DESC_CALL:
13ae64f3 2829 case R_386_TLS_IE_32:
37e55690 2830 case R_386_TLS_GOTIE:
13ae64f3
JJ
2831 tls_type = GOT_UNKNOWN;
2832 if (h == NULL && local_got_offsets)
0afcef53 2833 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
13ae64f3 2834 else if (h != NULL)
0afcef53 2835 tls_type = elf_x86_hash_entry(h)->tls_type;
37e55690
JJ
2836 if (tls_type == GOT_TLS_IE)
2837 tls_type = GOT_TLS_IE_NEG;
142411ca 2838
419414ea 2839 r_type_tls = r_type;
142411ca
L
2840 if (! elf_i386_tls_transition (info, input_bfd,
2841 input_section, contents,
2842 symtab_hdr, sym_hashes,
419414ea 2843 &r_type_tls, tls_type, rel,
bedfd056 2844 relend, h, r_symndx, TRUE))
142411ca 2845 return FALSE;
13ae64f3 2846
419414ea 2847 if (r_type_tls == R_386_TLS_LE_32)
13ae64f3 2848 {
82e51918 2849 BFD_ASSERT (! unresolved_reloc);
419414ea 2850 if (r_type == R_386_TLS_GD)
13ae64f3 2851 {
142411ca 2852 unsigned int type;
13ae64f3
JJ
2853 bfd_vma roff;
2854
2855 /* GD->LE transition. */
6eaa7fb5 2856 type = *(contents + rel->r_offset - 2);
13ae64f3
JJ
2857 if (type == 0x04)
2858 {
6eaa7fb5
L
2859 /* Change
2860 leal foo@tlsgd(,%ebx,1), %eax
2861 call ___tls_get_addr@PLT
2862 into:
2863 movl %gs:0, %eax
2864 subl $foo@tpoff, %eax
13ae64f3 2865 (6 byte form of subl). */
142411ca 2866 roff = rel->r_offset + 5;
13ae64f3
JJ
2867 }
2868 else
2869 {
6eaa7fb5
L
2870 /* Change
2871 leal foo@tlsgd(%ebx), %eax
2872 call ___tls_get_addr@PLT
2873 nop
2874 or
2875 leal foo@tlsgd(%reg), %eax
2876 call *___tls_get_addr@GOT(%reg)
2877 which may be converted to
2878 addr32 call ___tls_get_addr
2879 into:
2880 movl %gs:0, %eax; subl $foo@tpoff, %eax
142411ca 2881 (6 byte form of subl). */
142411ca 2882 roff = rel->r_offset + 6;
13ae64f3 2883 }
6eaa7fb5
L
2884 memcpy (contents + roff - 8,
2885 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
eb4ff4d6 2886 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
13ae64f3 2887 contents + roff);
6eaa7fb5 2888 /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X. */
13ae64f3 2889 rel++;
60f2e42e 2890 wrel++;
13ae64f3
JJ
2891 continue;
2892 }
419414ea 2893 else if (r_type == R_386_TLS_GOTDESC)
67a4f2b7
AO
2894 {
2895 /* GDesc -> LE transition.
2896 It's originally something like:
2897 leal x@tlsdesc(%ebx), %eax
2898
2899 leal x@ntpoff, %eax
2900
2901 Registers other than %eax may be set up here. */
2902
142411ca 2903 unsigned int val;
67a4f2b7
AO
2904 bfd_vma roff;
2905
67a4f2b7 2906 roff = rel->r_offset;
67a4f2b7 2907 val = bfd_get_8 (input_bfd, contents + roff - 1);
67a4f2b7
AO
2908
2909 /* Now modify the instruction as appropriate. */
2910 /* aoliva FIXME: remove the above and xor the byte
2911 below with 0x86. */
2912 bfd_put_8 (output_bfd, val ^ 0x86,
2913 contents + roff - 1);
eb4ff4d6 2914 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
67a4f2b7
AO
2915 contents + roff);
2916 continue;
2917 }
419414ea 2918 else if (r_type == R_386_TLS_DESC_CALL)
67a4f2b7
AO
2919 {
2920 /* GDesc -> LE transition.
2921 It's originally:
2922 call *(%eax)
2923 Turn it into:
142411ca 2924 xchg %ax,%ax */
67a4f2b7 2925
67a4f2b7 2926 bfd_vma roff;
6bbec505 2927
67a4f2b7 2928 roff = rel->r_offset;
10efb593 2929 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
2930 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2931 continue;
2932 }
419414ea 2933 else if (r_type == R_386_TLS_IE)
13ae64f3 2934 {
142411ca 2935 unsigned int val;
13ae64f3
JJ
2936
2937 /* IE->LE transition:
37e55690
JJ
2938 Originally it can be one of:
2939 movl foo, %eax
2940 movl foo, %reg
2941 addl foo, %reg
2942 We change it into:
2943 movl $foo, %eax
2944 movl $foo, %reg
2945 addl $foo, %reg. */
37e55690 2946 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
37e55690
JJ
2947 if (val == 0xa1)
2948 {
2949 /* movl foo, %eax. */
55fd94b0
AM
2950 bfd_put_8 (output_bfd, 0xb8,
2951 contents + rel->r_offset - 1);
37e55690 2952 }
299bf759 2953 else
37e55690 2954 {
142411ca
L
2955 unsigned int type;
2956
55fd94b0
AM
2957 type = bfd_get_8 (input_bfd,
2958 contents + rel->r_offset - 2);
299bf759 2959 switch (type)
26e41594 2960 {
299bf759
L
2961 case 0x8b:
2962 /* movl */
299bf759
L
2963 bfd_put_8 (output_bfd, 0xc7,
2964 contents + rel->r_offset - 2);
2965 bfd_put_8 (output_bfd,
2966 0xc0 | ((val >> 3) & 7),
2967 contents + rel->r_offset - 1);
2968 break;
2969 case 0x03:
2970 /* addl */
299bf759
L
2971 bfd_put_8 (output_bfd, 0x81,
2972 contents + rel->r_offset - 2);
2973 bfd_put_8 (output_bfd,
2974 0xc0 | ((val >> 3) & 7),
2975 contents + rel->r_offset - 1);
2976 break;
2977 default:
2978 BFD_FAIL ();
2979 break;
26e41594 2980 }
37e55690 2981 }
eb4ff4d6 2982 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
37e55690
JJ
2983 contents + rel->r_offset);
2984 continue;
2985 }
2986 else
2987 {
2988 unsigned int val, type;
2989
2990 /* {IE_32,GOTIE}->LE transition:
2991 Originally it can be one of:
13ae64f3 2992 subl foo(%reg1), %reg2
13ae64f3 2993 movl foo(%reg1), %reg2
37e55690 2994 addl foo(%reg1), %reg2
13ae64f3
JJ
2995 We change it into:
2996 subl $foo, %reg2
37e55690
JJ
2997 movl $foo, %reg2 (6 byte form)
2998 addl $foo, %reg2. */
13ae64f3
JJ
2999 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3000 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
13ae64f3
JJ
3001 if (type == 0x8b)
3002 {
3003 /* movl */
13ae64f3
JJ
3004 bfd_put_8 (output_bfd, 0xc7,
3005 contents + rel->r_offset - 2);
3006 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3007 contents + rel->r_offset - 1);
3008 }
3009 else if (type == 0x2b)
3010 {
3011 /* subl */
13ae64f3
JJ
3012 bfd_put_8 (output_bfd, 0x81,
3013 contents + rel->r_offset - 2);
3014 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3015 contents + rel->r_offset - 1);
3016 }
37e55690
JJ
3017 else if (type == 0x03)
3018 {
3019 /* addl */
3020 bfd_put_8 (output_bfd, 0x81,
3021 contents + rel->r_offset - 2);
3022 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3023 contents + rel->r_offset - 1);
3024 }
13ae64f3
JJ
3025 else
3026 BFD_FAIL ();
419414ea 3027 if (r_type == R_386_TLS_GOTIE)
eb4ff4d6 3028 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
37e55690
JJ
3029 contents + rel->r_offset);
3030 else
eb4ff4d6 3031 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
37e55690 3032 contents + rel->r_offset);
13ae64f3
JJ
3033 continue;
3034 }
3035 }
3036
6de2ae4a 3037 if (htab->elf.sgot == NULL)
13ae64f3
JJ
3038 abort ();
3039
3040 if (h != NULL)
67a4f2b7
AO
3041 {
3042 off = h->got.offset;
0afcef53 3043 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
67a4f2b7 3044 }
13ae64f3
JJ
3045 else
3046 {
3047 if (local_got_offsets == NULL)
3048 abort ();
3049
3050 off = local_got_offsets[r_symndx];
67a4f2b7 3051 offplt = local_tlsdesc_gotents[r_symndx];
13ae64f3
JJ
3052 }
3053
3054 if ((off & 1) != 0)
3055 off &= ~1;
26e41594 3056 else
13ae64f3 3057 {
947216bf 3058 Elf_Internal_Rela outrel;
91d6fa6a 3059 int dr_type;
67a4f2b7 3060 asection *sreloc;
13ae64f3 3061
6de2ae4a 3062 if (htab->elf.srelgot == NULL)
13ae64f3
JJ
3063 abort ();
3064
67a4f2b7
AO
3065 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3066
3067 if (GOT_TLS_GDESC_P (tls_type))
3068 {
59d6ffb2 3069 bfd_byte *loc;
67a4f2b7
AO
3070 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3071 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
6de2ae4a
L
3072 <= htab->elf.sgotplt->size);
3073 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3074 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
3075 + offplt
3076 + htab->sgotplt_jump_table_size);
6de2ae4a 3077 sreloc = htab->elf.srelplt;
67a4f2b7 3078 loc = sreloc->contents;
765e526c 3079 loc += (htab->next_tls_desc_index++
5ae0bfb6 3080 * sizeof (Elf32_External_Rel));
67a4f2b7 3081 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
c3b02097 3082 <= sreloc->contents + sreloc->size);
67a4f2b7
AO
3083 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3084 if (indx == 0)
3085 {
3086 BFD_ASSERT (! unresolved_reloc);
3087 bfd_put_32 (output_bfd,
0afcef53 3088 relocation - _bfd_x86_elf_dtpoff_base (info),
6de2ae4a 3089 htab->elf.sgotplt->contents + offplt
67a4f2b7
AO
3090 + htab->sgotplt_jump_table_size + 4);
3091 }
3092 else
3093 {
3094 bfd_put_32 (output_bfd, 0,
6de2ae4a 3095 htab->elf.sgotplt->contents + offplt
67a4f2b7
AO
3096 + htab->sgotplt_jump_table_size + 4);
3097 }
3098 }
3099
6de2ae4a 3100 sreloc = htab->elf.srelgot;
67a4f2b7 3101
6de2ae4a
L
3102 outrel.r_offset = (htab->elf.sgot->output_section->vma
3103 + htab->elf.sgot->output_offset + off);
13ae64f3 3104
67a4f2b7 3105 if (GOT_TLS_GD_P (tls_type))
13ae64f3 3106 dr_type = R_386_TLS_DTPMOD32;
67a4f2b7
AO
3107 else if (GOT_TLS_GDESC_P (tls_type))
3108 goto dr_done;
37e55690
JJ
3109 else if (tls_type == GOT_TLS_IE_POS)
3110 dr_type = R_386_TLS_TPOFF;
13ae64f3
JJ
3111 else
3112 dr_type = R_386_TLS_TPOFF32;
67a4f2b7 3113
37e55690 3114 if (dr_type == R_386_TLS_TPOFF && indx == 0)
eb4ff4d6 3115 bfd_put_32 (output_bfd,
0afcef53 3116 relocation - _bfd_x86_elf_dtpoff_base (info),
6de2ae4a 3117 htab->elf.sgot->contents + off);
37e55690 3118 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
23209a78 3119 bfd_put_32 (output_bfd,
0afcef53 3120 _bfd_x86_elf_dtpoff_base (info) - relocation,
6de2ae4a 3121 htab->elf.sgot->contents + off);
67a4f2b7 3122 else if (dr_type != R_386_TLS_DESC)
c366c25e 3123 bfd_put_32 (output_bfd, 0,
6de2ae4a 3124 htab->elf.sgot->contents + off);
13ae64f3 3125 outrel.r_info = ELF32_R_INFO (indx, dr_type);
67a4f2b7 3126
59d6ffb2 3127 elf_append_rel (output_bfd, sreloc, &outrel);
13ae64f3 3128
67a4f2b7 3129 if (GOT_TLS_GD_P (tls_type))
13ae64f3
JJ
3130 {
3131 if (indx == 0)
3132 {
82e51918 3133 BFD_ASSERT (! unresolved_reloc);
13ae64f3 3134 bfd_put_32 (output_bfd,
0afcef53 3135 relocation - _bfd_x86_elf_dtpoff_base (info),
6de2ae4a 3136 htab->elf.sgot->contents + off + 4);
13ae64f3
JJ
3137 }
3138 else
3139 {
3140 bfd_put_32 (output_bfd, 0,
6de2ae4a 3141 htab->elf.sgot->contents + off + 4);
13ae64f3
JJ
3142 outrel.r_info = ELF32_R_INFO (indx,
3143 R_386_TLS_DTPOFF32);
3144 outrel.r_offset += 4;
59d6ffb2 3145 elf_append_rel (output_bfd, sreloc, &outrel);
13ae64f3
JJ
3146 }
3147 }
37e55690
JJ
3148 else if (tls_type == GOT_TLS_IE_BOTH)
3149 {
3150 bfd_put_32 (output_bfd,
eb4ff4d6 3151 (indx == 0
0afcef53 3152 ? relocation - _bfd_x86_elf_dtpoff_base (info)
eb4ff4d6 3153 : 0),
6de2ae4a 3154 htab->elf.sgot->contents + off + 4);
37e55690
JJ
3155 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3156 outrel.r_offset += 4;
59d6ffb2 3157 elf_append_rel (output_bfd, sreloc, &outrel);
37e55690 3158 }
13ae64f3 3159
67a4f2b7 3160 dr_done:
13ae64f3
JJ
3161 if (h != NULL)
3162 h->got.offset |= 1;
3163 else
3164 local_got_offsets[r_symndx] |= 1;
3165 }
3166
67a4f2b7
AO
3167 if (off >= (bfd_vma) -2
3168 && ! GOT_TLS_GDESC_P (tls_type))
13ae64f3 3169 abort ();
419414ea
L
3170 if (r_type_tls == R_386_TLS_GOTDESC
3171 || r_type_tls == R_386_TLS_DESC_CALL)
67a4f2b7
AO
3172 {
3173 relocation = htab->sgotplt_jump_table_size + offplt;
3174 unresolved_reloc = FALSE;
3175 }
419414ea 3176 else if (r_type_tls == r_type)
13ae64f3 3177 {
6de2ae4a
L
3178 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
3179 + htab->elf.sgotplt->output_offset;
3180 relocation = htab->elf.sgot->output_section->vma
3181 + htab->elf.sgot->output_offset + off - g_o_t;
37e55690
JJ
3182 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3183 && tls_type == GOT_TLS_IE_BOTH)
3184 relocation += 4;
3185 if (r_type == R_386_TLS_IE)
8c37241b 3186 relocation += g_o_t;
b34976b6 3187 unresolved_reloc = FALSE;
13ae64f3 3188 }
419414ea 3189 else if (r_type == R_386_TLS_GD)
13ae64f3
JJ
3190 {
3191 unsigned int val, type;
3192 bfd_vma roff;
3193
3194 /* GD->IE transition. */
6eaa7fb5
L
3195 type = *(contents + rel->r_offset - 2);
3196 val = *(contents + rel->r_offset - 1);
13ae64f3
JJ
3197 if (type == 0x04)
3198 {
6eaa7fb5
L
3199 /* Change
3200 leal foo@tlsgd(,%ebx,1), %eax
3201 call ___tls_get_addr@PLT
3202 into:
3203 movl %gs:0, %eax
3204 subl $foo@gottpoff(%ebx), %eax. */
13ae64f3 3205 val >>= 3;
142411ca 3206 roff = rel->r_offset - 3;
13ae64f3
JJ
3207 }
3208 else
3209 {
6eaa7fb5
L
3210 /* Change
3211 leal foo@tlsgd(%ebx), %eax
3212 call ___tls_get_addr@PLT
3213 nop
3214 or
3215 leal foo@tlsgd(%reg), %eax
3216 call *___tls_get_addr@GOT(%reg)
3217 which may be converted to
3218 addr32 call ___tls_get_addr
3219 into:
3220 movl %gs:0, %eax;
3221 subl $foo@gottpoff(%reg), %eax. */
13ae64f3
JJ
3222 roff = rel->r_offset - 2;
3223 }
3224 memcpy (contents + roff,
3225 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3226 contents[roff + 7] = 0x80 | (val & 7);
37e55690
JJ
3227 /* If foo is used only with foo@gotntpoff(%reg) and
3228 foo@indntpoff, but not with foo@gottpoff(%reg), change
3229 subl $foo@gottpoff(%reg), %eax
3230 into:
3231 addl $foo@gotntpoff(%reg), %eax. */
ebcfb3c0
JJ
3232 if (tls_type == GOT_TLS_IE_POS)
3233 contents[roff + 6] = 0x03;
8c37241b 3234 bfd_put_32 (output_bfd,
6de2ae4a
L
3235 htab->elf.sgot->output_section->vma
3236 + htab->elf.sgot->output_offset + off
3237 - htab->elf.sgotplt->output_section->vma
3238 - htab->elf.sgotplt->output_offset,
13ae64f3 3239 contents + roff + 8);
6eaa7fb5 3240 /* Skip R_386_PLT32 and R_386_GOT32X. */
13ae64f3 3241 rel++;
60f2e42e 3242 wrel++;
13ae64f3
JJ
3243 continue;
3244 }
419414ea 3245 else if (r_type == R_386_TLS_GOTDESC)
67a4f2b7
AO
3246 {
3247 /* GDesc -> IE transition.
3248 It's originally something like:
3249 leal x@tlsdesc(%ebx), %eax
3250
3251 Change it to:
142411ca 3252 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
67a4f2b7
AO
3253 or:
3254 movl x@gottpoff(%ebx), %eax # before negl %eax
3255
3256 Registers other than %eax may be set up here. */
3257
67a4f2b7
AO
3258 bfd_vma roff;
3259
3260 /* First, make sure it's a leal adding ebx to a 32-bit
3261 offset into any register, although it's probably
3262 almost always going to be eax. */
3263 roff = rel->r_offset;
67a4f2b7
AO
3264
3265 /* Now modify the instruction as appropriate. */
3266 /* To turn a leal into a movl in the form we use it, it
3267 suffices to change the first byte from 0x8d to 0x8b.
3268 aoliva FIXME: should we decide to keep the leal, all
3269 we have to do is remove the statement below, and
3270 adjust the relaxation of R_386_TLS_DESC_CALL. */
3271 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3272
3273 if (tls_type == GOT_TLS_IE_BOTH)
3274 off += 4;
3275
3276 bfd_put_32 (output_bfd,
6de2ae4a
L
3277 htab->elf.sgot->output_section->vma
3278 + htab->elf.sgot->output_offset + off
3279 - htab->elf.sgotplt->output_section->vma
3280 - htab->elf.sgotplt->output_offset,
67a4f2b7
AO
3281 contents + roff);
3282 continue;
3283 }
419414ea 3284 else if (r_type == R_386_TLS_DESC_CALL)
67a4f2b7
AO
3285 {
3286 /* GDesc -> IE transition.
3287 It's originally:
3288 call *(%eax)
3289
3290 Change it to:
142411ca 3291 xchg %ax,%ax
67a4f2b7
AO
3292 or
3293 negl %eax
3294 depending on how we transformed the TLS_GOTDESC above.
3295 */
3296
67a4f2b7
AO
3297 bfd_vma roff;
3298
67a4f2b7 3299 roff = rel->r_offset;
67a4f2b7
AO
3300
3301 /* Now modify the instruction as appropriate. */
3302 if (tls_type != GOT_TLS_IE_NEG)
3303 {
10efb593
L
3304 /* xchg %ax,%ax */
3305 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
3306 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3307 }
3308 else
3309 {
3310 /* negl %eax */
3311 bfd_put_8 (output_bfd, 0xf7, contents + roff);
3312 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3313 }
3314
3315 continue;
3316 }
3317 else
3318 BFD_ASSERT (FALSE);
13ae64f3
JJ
3319 break;
3320
3321 case R_386_TLS_LDM:
142411ca
L
3322 if (! elf_i386_tls_transition (info, input_bfd,
3323 input_section, contents,
3324 symtab_hdr, sym_hashes,
3325 &r_type, GOT_UNKNOWN, rel,
bedfd056 3326 relend, h, r_symndx, TRUE))
142411ca 3327 return FALSE;
13ae64f3 3328
142411ca
L
3329 if (r_type != R_386_TLS_LDM)
3330 {
6eaa7fb5
L
3331 /* LD->LE transition. Change
3332 leal foo@tlsldm(%ebx) %eax
3333 call ___tls_get_addr@PLT
3334 into:
3335 movl %gs:0, %eax
3336 nop
3337 leal 0(%esi,1), %esi
3338 or change
3339 leal foo@tlsldm(%reg) %eax
3340 call *___tls_get_addr@GOT(%reg)
3341 which may be converted to
3342 addr32 call ___tls_get_addr
3343 into:
3344 movl %gs:0, %eax
3345 leal 0(%esi), %esi */
142411ca 3346 BFD_ASSERT (r_type == R_386_TLS_LE_32);
6eaa7fb5
L
3347 if (*(contents + rel->r_offset + 4) == 0xff
3348 || *(contents + rel->r_offset + 4) == 0x67)
3349 memcpy (contents + rel->r_offset - 2,
3350 "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
3351 else
3352 memcpy (contents + rel->r_offset - 2,
3353 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
a3fadc9a 3354 /* Skip R_386_PC32/R_386_PLT32. */
13ae64f3 3355 rel++;
60f2e42e 3356 wrel++;
13ae64f3
JJ
3357 continue;
3358 }
3359
6de2ae4a 3360 if (htab->elf.sgot == NULL)
13ae64f3
JJ
3361 abort ();
3362
0afcef53 3363 off = htab->tls_ld_or_ldm_got.offset;
13ae64f3
JJ
3364 if (off & 1)
3365 off &= ~1;
3366 else
3367 {
947216bf 3368 Elf_Internal_Rela outrel;
13ae64f3 3369
6de2ae4a 3370 if (htab->elf.srelgot == NULL)
13ae64f3
JJ
3371 abort ();
3372
6de2ae4a
L
3373 outrel.r_offset = (htab->elf.sgot->output_section->vma
3374 + htab->elf.sgot->output_offset + off);
13ae64f3
JJ
3375
3376 bfd_put_32 (output_bfd, 0,
6de2ae4a 3377 htab->elf.sgot->contents + off);
13ae64f3 3378 bfd_put_32 (output_bfd, 0,
6de2ae4a 3379 htab->elf.sgot->contents + off + 4);
13ae64f3 3380 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
59d6ffb2 3381 elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
0afcef53 3382 htab->tls_ld_or_ldm_got.offset |= 1;
13ae64f3 3383 }
6de2ae4a
L
3384 relocation = htab->elf.sgot->output_section->vma
3385 + htab->elf.sgot->output_offset + off
3386 - htab->elf.sgotplt->output_section->vma
3387 - htab->elf.sgotplt->output_offset;
b34976b6 3388 unresolved_reloc = FALSE;
13ae64f3
JJ
3389 break;
3390
3391 case R_386_TLS_LDO_32:
0e1862bb
L
3392 if (!bfd_link_executable (info)
3393 || (input_section->flags & SEC_CODE) == 0)
0afcef53 3394 relocation -= _bfd_x86_elf_dtpoff_base (info);
13ae64f3
JJ
3395 else
3396 /* When converting LDO to LE, we must negate. */
eb4ff4d6 3397 relocation = -elf_i386_tpoff (info, relocation);
13ae64f3
JJ
3398 break;
3399
3400 case R_386_TLS_LE_32:
13ae64f3 3401 case R_386_TLS_LE:
0e1862bb 3402 if (!bfd_link_executable (info))
37e55690 3403 {
947216bf 3404 Elf_Internal_Rela outrel;
37e55690 3405 asection *sreloc;
37e55690
JJ
3406
3407 outrel.r_offset = rel->r_offset
3408 + input_section->output_section->vma
3409 + input_section->output_offset;
3410 if (h != NULL && h->dynindx != -1)
3411 indx = h->dynindx;
3412 else
3413 indx = 0;
3414 if (r_type == R_386_TLS_LE_32)
3415 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3416 else
3417 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3418 sreloc = elf_section_data (input_section)->sreloc;
3419 if (sreloc == NULL)
3420 abort ();
59d6ffb2 3421 elf_append_rel (output_bfd, sreloc, &outrel);
37e55690
JJ
3422 if (indx)
3423 continue;
3424 else if (r_type == R_386_TLS_LE_32)
0afcef53 3425 relocation = _bfd_x86_elf_dtpoff_base (info) - relocation;
37e55690 3426 else
0afcef53 3427 relocation -= _bfd_x86_elf_dtpoff_base (info);
37e55690
JJ
3428 }
3429 else if (r_type == R_386_TLS_LE_32)
eb4ff4d6 3430 relocation = elf_i386_tpoff (info, relocation);
37e55690 3431 else
eb4ff4d6 3432 relocation = -elf_i386_tpoff (info, relocation);
13ae64f3
JJ
3433 break;
3434
252b5132
RH
3435 default:
3436 break;
3437 }
3438
239e1f3a
AM
3439 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3440 because such sections are not SEC_ALLOC and thus ld.so will
3441 not process them. */
8c694914 3442 if (unresolved_reloc
239e1f3a 3443 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
3444 && h->def_dynamic)
3445 && _bfd_elf_section_offset (output_bfd, info, input_section,
3446 rel->r_offset) != (bfd_vma) -1)
6a30718d 3447 {
4eca0228 3448 _bfd_error_handler
695344c0 3449 /* xgettext:c-format */
d42c267e 3450 (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
d003868e
AM
3451 input_bfd,
3452 input_section,
d42c267e 3453 rel->r_offset,
843fe662 3454 howto->name,
6a30718d 3455 h->root.root.string);
b34976b6 3456 return FALSE;
6a30718d 3457 }
83be169b 3458
cbe950e9 3459do_relocation:
252b5132
RH
3460 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3461 contents, rel->r_offset,
55fd94b0 3462 relocation, 0);
252b5132 3463
62d78908 3464check_relocation_error:
cf5c0c5b 3465 if (r != bfd_reloc_ok)
252b5132 3466 {
cf5c0c5b 3467 const char *name;
ffb2e45b 3468
cf5c0c5b
AM
3469 if (h != NULL)
3470 name = h->root.root.string;
3471 else
3472 {
3473 name = bfd_elf_string_from_elf_section (input_bfd,
3474 symtab_hdr->sh_link,
3475 sym->st_name);
3476 if (name == NULL)
b34976b6 3477 return FALSE;
cf5c0c5b
AM
3478 if (*name == '\0')
3479 name = bfd_section_name (input_bfd, sec);
3480 }
ffb2e45b 3481
cf5c0c5b 3482 if (r == bfd_reloc_overflow)
1a72702b
AM
3483 (*info->callbacks->reloc_overflow)
3484 (info, (h ? &h->root : NULL), name, howto->name,
3485 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
cf5c0c5b
AM
3486 else
3487 {
4eca0228 3488 _bfd_error_handler
695344c0 3489 /* xgettext:c-format */
d42c267e 3490 (_("%B(%A+%#Lx): reloc against `%s': error %d"),
d003868e 3491 input_bfd, input_section,
d42c267e 3492 rel->r_offset, name, (int) r);
b34976b6 3493 return FALSE;
cf5c0c5b 3494 }
252b5132 3495 }
60f2e42e
L
3496
3497 if (wrel != rel)
3498 *wrel = *rel;
3499 }
3500
3501 if (wrel != rel)
3502 {
3503 Elf_Internal_Shdr *rel_hdr;
3504 size_t deleted = rel - wrel;
3505
3506 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
3507 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3508 if (rel_hdr->sh_size == 0)
3509 {
3510 /* It is too late to remove an empty reloc section. Leave
3511 one NONE reloc.
3512 ??? What is wrong with an empty section??? */
3513 rel_hdr->sh_size = rel_hdr->sh_entsize;
3514 deleted -= 1;
3515 }
3516 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
3517 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3518 input_section->reloc_count -= deleted;
252b5132
RH
3519 }
3520
b34976b6 3521 return TRUE;
252b5132
RH
3522}
3523
3524/* Finish up dynamic symbol handling. We set the contents of various
3525 dynamic sections here. */
3526
b34976b6 3527static bfd_boolean
55fd94b0
AM
3528elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3529 struct bfd_link_info *info,
3530 struct elf_link_hash_entry *h,
3531 Elf_Internal_Sym *sym)
252b5132 3532{
0afcef53 3533 struct elf_x86_link_hash_table *htab;
25e762b9 3534 unsigned plt_entry_size;
0afcef53 3535 struct elf_x86_link_hash_entry *eh;
aec6b87e 3536 bfd_boolean local_undefweak;
ee2fdd6f 3537 bfd_boolean use_plt_second;
252b5132 3538
0afcef53 3539 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6
NC
3540 if (htab == NULL)
3541 return FALSE;
252b5132 3542
765e526c 3543 plt_entry_size = htab->plt.plt_entry_size;
25e762b9 3544
ee2fdd6f
L
3545 /* Use the second PLT section only if there is .plt section. */
3546 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
3547
0afcef53 3548 eh = (struct elf_x86_link_hash_entry *) h;
e133d005
L
3549 if (eh->no_finish_dynamic_symbol)
3550 abort ();
dd7e64d4 3551
aec6b87e
L
3552 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
3553 resolved undefined weak symbols in executable so that their
3554 references have value 0 at run-time. */
c5bce5c6 3555 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
aec6b87e 3556
252b5132
RH
3557 if (h->plt.offset != (bfd_vma) -1)
3558 {
ee2fdd6f 3559 bfd_vma plt_index, plt_offset;
252b5132 3560 bfd_vma got_offset;
947216bf
AM
3561 Elf_Internal_Rela rel;
3562 bfd_byte *loc;
ee2fdd6f 3563 asection *plt, *resolved_plt, *gotplt, *relplt;
cbe950e9
L
3564
3565 /* When building a static executable, use .iplt, .igot.plt and
3566 .rel.iplt sections for STT_GNU_IFUNC symbols. */
6de2ae4a 3567 if (htab->elf.splt != NULL)
cbe950e9 3568 {
6de2ae4a
L
3569 plt = htab->elf.splt;
3570 gotplt = htab->elf.sgotplt;
3571 relplt = htab->elf.srelplt;
cbe950e9
L
3572 }
3573 else
3574 {
6de2ae4a
L
3575 plt = htab->elf.iplt;
3576 gotplt = htab->elf.igotplt;
3577 relplt = htab->elf.irelplt;
cbe950e9 3578 }
252b5132 3579
f3180fa9 3580 VERIFY_PLT_ENTRY (info, h, plt, gotplt, relplt, local_undefweak)
252b5132
RH
3581
3582 /* Get the index in the procedure linkage table which
3583 corresponds to this symbol. This is the index of this symbol
3584 in all the symbols for which we are making plt entries. The
cbe950e9 3585 first entry in the procedure linkage table is reserved.
252b5132 3586
cbe950e9 3587 Get the offset into the .got table of the entry that
252b5132 3588 corresponds to this function. Each .got entry is 4 bytes.
cbe950e9 3589 The first three are reserved.
6bbec505 3590
cbe950e9
L
3591 For static executables, we don't reserve anything. */
3592
6de2ae4a 3593 if (plt == htab->elf.splt)
cbe950e9 3594 {
f604c2a2 3595 got_offset = (h->plt.offset / plt_entry_size
765e526c 3596 - htab->plt.has_plt0);
e1f98742 3597 got_offset = (got_offset + 3) * 4;
cbe950e9
L
3598 }
3599 else
3600 {
e1f98742
L
3601 got_offset = h->plt.offset / plt_entry_size;
3602 got_offset = got_offset * 4;
cbe950e9 3603 }
252b5132 3604
f604c2a2
L
3605 /* Fill in the entry in the procedure linkage table and update
3606 the first slot. */
765e526c 3607 memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
f604c2a2 3608 plt_entry_size);
ee2fdd6f
L
3609
3610 if (use_plt_second)
3611 {
3612 const bfd_byte *plt_entry;
3613 if (bfd_link_pic (info))
765e526c 3614 plt_entry = htab->non_lazy_plt->pic_plt_entry;
ee2fdd6f 3615 else
765e526c 3616 plt_entry = htab->non_lazy_plt->plt_entry;
ee2fdd6f 3617 memcpy (htab->plt_second->contents + eh->plt_second.offset,
765e526c 3618 plt_entry, htab->non_lazy_plt->plt_entry_size);
ee2fdd6f
L
3619
3620 resolved_plt = htab->plt_second;
3621 plt_offset = eh->plt_second.offset;
3622 }
3623 else
3624 {
3625 resolved_plt = plt;
3626 plt_offset = h->plt.offset;
3627 }
3628
0e1862bb 3629 if (! bfd_link_pic (info))
252b5132 3630 {
252b5132 3631 bfd_put_32 (output_bfd,
cbe950e9
L
3632 (gotplt->output_section->vma
3633 + gotplt->output_offset
252b5132 3634 + got_offset),
ee2fdd6f 3635 resolved_plt->contents + plt_offset
765e526c 3636 + htab->plt.plt_got_offset);
eac338cf 3637
9a742a90 3638 if (htab->is_vxworks)
eac338cf
PB
3639 {
3640 int s, k, reloc_index;
3641
3642 /* Create the R_386_32 relocation referencing the GOT
3643 for this PLT entry. */
3644
3645 /* S: Current slot number (zero-based). */
765e526c
L
3646 s = ((h->plt.offset - htab->plt.plt_entry_size)
3647 / htab->plt.plt_entry_size);
eac338cf 3648 /* K: Number of relocations for PLTResolve. */
0e1862bb 3649 if (bfd_link_pic (info))
eac338cf
PB
3650 k = PLTRESOLVE_RELOCS_SHLIB;
3651 else
3652 k = PLTRESOLVE_RELOCS;
3653 /* Skip the PLTresolve relocations, and the relocations for
3654 the other PLT slots. */
3655 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
765e526c 3656 loc = (htab->srelplt2->contents + reloc_index
eac338cf
PB
3657 * sizeof (Elf32_External_Rel));
3658
ee2fdd6f
L
3659 rel.r_offset = (plt->output_section->vma
3660 + plt->output_offset
eac338cf 3661 + h->plt.offset + 2),
7325306f 3662 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
eac338cf
PB
3663 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3664
3665 /* Create the R_386_32 relocation referencing the beginning of
3666 the PLT for this GOT entry. */
6de2ae4a
L
3667 rel.r_offset = (htab->elf.sgotplt->output_section->vma
3668 + htab->elf.sgotplt->output_offset
eac338cf 3669 + got_offset);
7325306f 3670 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
eac338cf 3671 bfd_elf32_swap_reloc_out (output_bfd, &rel,
13ca3149 3672 loc + sizeof (Elf32_External_Rel));
eac338cf 3673 }
252b5132
RH
3674 }
3675 else
3676 {
252b5132 3677 bfd_put_32 (output_bfd, got_offset,
ee2fdd6f 3678 resolved_plt->contents + plt_offset
765e526c 3679 + htab->plt.plt_got_offset);
252b5132
RH
3680 }
3681
aec6b87e
L
3682 /* Fill in the entry in the global offset table. Leave the entry
3683 as zero for undefined weak symbol in PIE. No PLT relocation
3684 against undefined weak symbol in PIE. */
3685 if (!local_undefweak)
cbe950e9 3686 {
765e526c 3687 if (htab->plt.has_plt0)
f604c2a2
L
3688 bfd_put_32 (output_bfd,
3689 (plt->output_section->vma
3690 + plt->output_offset
3691 + h->plt.offset
765e526c 3692 + htab->lazy_plt->plt_lazy_offset),
f604c2a2 3693 gotplt->contents + got_offset);
252b5132 3694
aec6b87e
L
3695 /* Fill in the entry in the .rel.plt section. */
3696 rel.r_offset = (gotplt->output_section->vma
3697 + gotplt->output_offset
3698 + got_offset);
cf1070f1 3699 if (PLT_LOCAL_IFUNC_P (info, h))
aec6b87e 3700 {
6322e5c5
L
3701 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
3702 h->root.root.string,
3703 h->root.u.def.section->owner);
3704
aec6b87e
L
3705 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3706 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
3707 in the .got.plt section. */
3708 bfd_put_32 (output_bfd,
3709 (h->root.u.def.value
3710 + h->root.u.def.section->output_section->vma
3711 + h->root.u.def.section->output_offset),
3712 gotplt->contents + got_offset);
3713 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3714 /* R_386_IRELATIVE comes last. */
3715 plt_index = htab->next_irelative_index--;
3716 }
3717 else
3718 {
3719 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3720 plt_index = htab->next_jump_slot_index++;
3721 }
3722
3723 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
3724 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3725
f604c2a2
L
3726 /* Don't fill the second and third slots in PLT entry for
3727 static executables nor without PLT0. */
765e526c 3728 if (plt == htab->elf.splt && htab->plt.has_plt0)
aec6b87e
L
3729 {
3730 bfd_put_32 (output_bfd,
3731 plt_index * sizeof (Elf32_External_Rel),
3732 plt->contents + h->plt.offset
765e526c 3733 + htab->lazy_plt->plt_reloc_offset);
f604c2a2
L
3734 bfd_put_32 (output_bfd,
3735 - (h->plt.offset
765e526c 3736 + htab->lazy_plt->plt_plt_offset + 4),
f604c2a2 3737 (plt->contents + h->plt.offset
765e526c 3738 + htab->lazy_plt->plt_plt_offset));
aec6b87e 3739 }
e1f98742 3740 }
dd7e64d4
L
3741 }
3742 else if (eh->plt_got.offset != (bfd_vma) -1)
3743 {
3744 bfd_vma got_offset, plt_offset;
3745 asection *plt, *got, *gotplt;
3746 const bfd_byte *got_plt_entry;
3747
dd7e64d4
L
3748 /* Set the entry in the GOT procedure linkage table. */
3749 plt = htab->plt_got;
3750 got = htab->elf.sgot;
3751 gotplt = htab->elf.sgotplt;
3752 got_offset = h->got.offset;
3753
3754 if (got_offset == (bfd_vma) -1
3755 || plt == NULL
3756 || got == NULL
3757 || gotplt == NULL)
3758 abort ();
3759
3760 /* Fill in the entry in the GOT procedure linkage table. */
0e1862bb 3761 if (! bfd_link_pic (info))
252b5132 3762 {
765e526c 3763 got_plt_entry = htab->non_lazy_plt->plt_entry;
dd7e64d4 3764 got_offset += got->output_section->vma + got->output_offset;
252b5132 3765 }
dd7e64d4
L
3766 else
3767 {
765e526c 3768 got_plt_entry = htab->non_lazy_plt->pic_plt_entry;
dd7e64d4
L
3769 got_offset += (got->output_section->vma
3770 + got->output_offset
3771 - gotplt->output_section->vma
3772 - gotplt->output_offset);
3773 }
3774
3775 plt_offset = eh->plt_got.offset;
3776 memcpy (plt->contents + plt_offset, got_plt_entry,
765e526c 3777 htab->non_lazy_plt->plt_entry_size);
dd7e64d4 3778 bfd_put_32 (output_bfd, got_offset,
f604c2a2 3779 (plt->contents + plt_offset
765e526c 3780 + htab->non_lazy_plt->plt_got_offset));
dd7e64d4
L
3781 }
3782
aec6b87e
L
3783 if (!local_undefweak
3784 && !h->def_regular
dd7e64d4
L
3785 && (h->plt.offset != (bfd_vma) -1
3786 || eh->plt_got.offset != (bfd_vma) -1))
3787 {
3788 /* Mark the symbol as undefined, rather than as defined in
3789 the .plt section. Leave the value if there were any
3790 relocations where pointer equality matters (this is a clue
3791 for the dynamic linker, to make function pointer
3792 comparisons work between an application and shared
3793 library), otherwise set it to zero. If a function is only
3794 called from a binary, there is no need to slow down
3795 shared libraries because of that. */
3796 sym->st_shndx = SHN_UNDEF;
3797 if (!h->pointer_equality_needed)
3798 sym->st_value = 0;
252b5132
RH
3799 }
3800
aec6b87e
L
3801 /* Don't generate dynamic GOT relocation against undefined weak
3802 symbol in executable. */
13ae64f3 3803 if (h->got.offset != (bfd_vma) -1
0afcef53
L
3804 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry(h)->tls_type)
3805 && (elf_x86_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
aec6b87e 3806 && !local_undefweak)
252b5132 3807 {
947216bf 3808 Elf_Internal_Rela rel;
233cc9c1 3809 asection *relgot = htab->elf.srelgot;
252b5132
RH
3810
3811 /* This symbol has an entry in the global offset table. Set it
3812 up. */
3813
6de2ae4a 3814 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
ffb2e45b 3815 abort ();
252b5132 3816
6de2ae4a
L
3817 rel.r_offset = (htab->elf.sgot->output_section->vma
3818 + htab->elf.sgot->output_offset
dc810e39 3819 + (h->got.offset & ~(bfd_vma) 1));
252b5132 3820
dd5724d5
AM
3821 /* If this is a static link, or it is a -Bsymbolic link and the
3822 symbol is defined locally or was forced to be local because
3823 of a version file, we just want to emit a RELATIVE reloc.
252b5132
RH
3824 The entry in the global offset table will already have been
3825 initialized in the relocate_section function. */
710ab287 3826 if (h->def_regular
0018b0a3
L
3827 && h->type == STT_GNU_IFUNC)
3828 {
233cc9c1
L
3829 if (h->plt.offset == (bfd_vma) -1)
3830 {
3831 /* STT_GNU_IFUNC is referenced without PLT. */
3832 if (htab->elf.splt == NULL)
3833 {
3834 /* use .rel[a].iplt section to store .got relocations
3835 in static executable. */
3836 relgot = htab->elf.irelplt;
3837 }
6999821f 3838 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
233cc9c1 3839 {
6322e5c5
L
3840 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
3841 h->root.root.string,
3842 h->root.u.def.section->owner);
3843
233cc9c1
L
3844 bfd_put_32 (output_bfd,
3845 (h->root.u.def.value
3846 + h->root.u.def.section->output_section->vma
3847 + h->root.u.def.section->output_offset),
3848 htab->elf.sgot->contents + h->got.offset);
3849 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3850 }
3851 else
3852 goto do_glob_dat;
3853 }
3854 else if (bfd_link_pic (info))
710ab287
L
3855 {
3856 /* Generate R_386_GLOB_DAT. */
3857 goto do_glob_dat;
3858 }
3859 else
3860 {
cd2b2c10 3861 asection *plt;
ee2fdd6f 3862 bfd_vma plt_offset;
cd2b2c10 3863
710ab287
L
3864 if (!h->pointer_equality_needed)
3865 abort ();
3866
3867 /* For non-shared object, we can't use .got.plt, which
3868 contains the real function addres if we need pointer
3869 equality. We load the GOT entry with the PLT entry. */
ee2fdd6f
L
3870 if (htab->plt_second != NULL)
3871 {
3872 plt = htab->plt_second;
3873 plt_offset = eh->plt_second.offset;
3874 }
3875 else
3876 {
3877 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3878 plt_offset = h->plt.offset;
3879 }
710ab287
L
3880 bfd_put_32 (output_bfd,
3881 (plt->output_section->vma
ee2fdd6f 3882 + plt->output_offset + plt_offset),
6de2ae4a 3883 htab->elf.sgot->contents + h->got.offset);
710ab287
L
3884 return TRUE;
3885 }
0018b0a3 3886 }
0e1862bb 3887 else if (bfd_link_pic (info)
6999821f 3888 && SYMBOL_REFERENCES_LOCAL_P (info, h))
dd5724d5 3889 {
6725bdbf 3890 BFD_ASSERT((h->got.offset & 1) != 0);
dd5724d5
AM
3891 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3892 }
252b5132
RH
3893 else
3894 {
dd5724d5 3895 BFD_ASSERT((h->got.offset & 1) == 0);
710ab287 3896do_glob_dat:
6725bdbf 3897 bfd_put_32 (output_bfd, (bfd_vma) 0,
6de2ae4a 3898 htab->elf.sgot->contents + h->got.offset);
252b5132
RH
3899 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3900 }
3901
233cc9c1 3902 elf_append_rel (output_bfd, relgot, &rel);
252b5132
RH
3903 }
3904
f5385ebf 3905 if (h->needs_copy)
252b5132 3906 {
947216bf 3907 Elf_Internal_Rela rel;
5474d94f 3908 asection *s;
252b5132
RH
3909
3910 /* This symbol needs a copy reloc. Set it up. */
ff38b4cc 3911 VERIFY_COPY_RELOC (h, htab)
252b5132
RH
3912
3913 rel.r_offset = (h->root.u.def.value
3914 + h->root.u.def.section->output_section->vma
3915 + h->root.u.def.section->output_offset);
3916 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
afbf7e8e 3917 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
3918 s = htab->elf.sreldynrelro;
3919 else
3920 s = htab->elf.srelbss;
3921 elf_append_rel (output_bfd, s, &rel);
252b5132
RH
3922 }
3923
b34976b6 3924 return TRUE;
252b5132
RH
3925}
3926
c25bc9fc
L
3927/* Finish up local dynamic symbol handling. We set the contents of
3928 various dynamic sections here. */
3929
3930static bfd_boolean
3931elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
3932{
3933 struct elf_link_hash_entry *h
3934 = (struct elf_link_hash_entry *) *slot;
3935 struct bfd_link_info *info
23209a78 3936 = (struct bfd_link_info *) inf;
c25bc9fc
L
3937
3938 return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
3939 h, NULL);
3940}
3941
aec6b87e
L
3942/* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
3943 here since undefined weak symbol may not be dynamic and may not be
3944 called for elf_i386_finish_dynamic_symbol. */
3945
3946static bfd_boolean
3947elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
3948 void *inf)
3949{
3950 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
3951 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3952
3953 if (h->root.type != bfd_link_hash_undefweak
3954 || h->dynindx != -1)
3955 return TRUE;
3956
3957 return elf_i386_finish_dynamic_symbol (info->output_bfd,
5f0f0847 3958 info, h, NULL);
aec6b87e
L
3959}
3960
38701953
AM
3961/* Used to decide how to sort relocs in an optimal manner for the
3962 dynamic linker, before writing them out. */
3963
3964static enum elf_reloc_type_class
cae1fbbb 3965elf_i386_reloc_type_class (const struct bfd_link_info *info,
7e612e98
AM
3966 const asection *rel_sec ATTRIBUTE_UNUSED,
3967 const Elf_Internal_Rela *rela)
38701953 3968{
cae1fbbb
L
3969 bfd *abfd = info->output_bfd;
3970 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3971 struct elf_link_hash_table *htab = elf_hash_table (info);
cae1fbbb 3972
d9e3b590
L
3973 if (htab->dynsym != NULL
3974 && htab->dynsym->contents != NULL)
3975 {
3976 /* Check relocation against STT_GNU_IFUNC symbol if there are
3977 dynamic symbols. */
3978 unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
c428ce9d
L
3979 if (r_symndx != STN_UNDEF)
3980 {
3981 Elf_Internal_Sym sym;
3982 if (!bed->s->swap_symbol_in (abfd,
3983 (htab->dynsym->contents
3984 + r_symndx * sizeof (Elf32_External_Sym)),
3985 0, &sym))
3986 abort ();
d9e3b590 3987
c428ce9d
L
3988 if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
3989 return reloc_class_ifunc;
3990 }
d9e3b590 3991 }
cae1fbbb 3992
55fd94b0 3993 switch (ELF32_R_TYPE (rela->r_info))
38701953 3994 {
c428ce9d
L
3995 case R_386_IRELATIVE:
3996 return reloc_class_ifunc;
38701953
AM
3997 case R_386_RELATIVE:
3998 return reloc_class_relative;
3999 case R_386_JUMP_SLOT:
4000 return reloc_class_plt;
4001 case R_386_COPY:
4002 return reloc_class_copy;
4003 default:
4004 return reloc_class_normal;
4005 }
4006}
4007
252b5132
RH
4008/* Finish up the dynamic sections. */
4009
b34976b6 4010static bfd_boolean
55fd94b0
AM
4011elf_i386_finish_dynamic_sections (bfd *output_bfd,
4012 struct bfd_link_info *info)
252b5132 4013{
0afcef53 4014 struct elf_x86_link_hash_table *htab;
252b5132 4015 bfd *dynobj;
252b5132 4016 asection *sdyn;
25e762b9 4017 const struct elf_i386_backend_data *abed;
252b5132 4018
0afcef53 4019 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6
NC
4020 if (htab == NULL)
4021 return FALSE;
4022
ebe50bae 4023 dynobj = htab->elf.dynobj;
3d4d4302 4024 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
25e762b9 4025 abed = get_elf_i386_backend_data (output_bfd);
252b5132 4026
ebe50bae 4027 if (htab->elf.dynamic_sections_created)
252b5132 4028 {
252b5132
RH
4029 Elf32_External_Dyn *dyncon, *dynconend;
4030
6de2ae4a 4031 if (sdyn == NULL || htab->elf.sgot == NULL)
ffb2e45b 4032 abort ();
252b5132
RH
4033
4034 dyncon = (Elf32_External_Dyn *) sdyn->contents;
eea6121a 4035 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
252b5132
RH
4036 for (; dyncon < dynconend; dyncon++)
4037 {
4038 Elf_Internal_Dyn dyn;
51b64d56 4039 asection *s;
252b5132
RH
4040
4041 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4042
4043 switch (dyn.d_tag)
4044 {
4045 default:
9a742a90 4046 if (htab->is_vxworks
25e762b9 4047 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
7a2b07ff 4048 break;
0ac8d2ca 4049 continue;
252b5132
RH
4050
4051 case DT_PLTGOT:
6de2ae4a 4052 s = htab->elf.sgotplt;
8c37241b 4053 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6725bdbf
AM
4054 break;
4055
252b5132 4056 case DT_JMPREL:
6de2ae4a 4057 s = htab->elf.srelplt;
6348e046 4058 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
252b5132
RH
4059 break;
4060
4061 case DT_PLTRELSZ:
6de2ae4a 4062 s = htab->elf.srelplt;
eea6121a 4063 dyn.d_un.d_val = s->size;
252b5132 4064 break;
252b5132 4065 }
0ac8d2ca
AM
4066
4067 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
252b5132
RH
4068 }
4069
6de2ae4a 4070 if (htab->elf.splt && htab->elf.splt->size > 0)
252b5132 4071 {
1f78f649
L
4072 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4073 really seem like the right value. */
4074 elf_section_data (htab->elf.splt->output_section)
4075 ->this_hdr.sh_entsize = 4;
4076
765e526c 4077 if (htab->plt.has_plt0)
252b5132 4078 {
f604c2a2
L
4079 /* Fill in the special first entry in the procedure linkage
4080 table. */
765e526c
L
4081 memcpy (htab->elf.splt->contents, htab->plt.plt0_entry,
4082 htab->lazy_plt->plt0_entry_size);
4083 memset (htab->elf.splt->contents + htab->lazy_plt->plt0_entry_size,
25e762b9 4084 abed->plt0_pad_byte,
765e526c 4085 htab->plt.plt_entry_size - htab->lazy_plt->plt0_entry_size);
f604c2a2 4086 if (!bfd_link_pic (info))
eac338cf 4087 {
f604c2a2
L
4088 bfd_put_32 (output_bfd,
4089 (htab->elf.sgotplt->output_section->vma
4090 + htab->elf.sgotplt->output_offset
4091 + 4),
4092 htab->elf.splt->contents
765e526c 4093 + htab->lazy_plt->plt0_got1_offset);
f604c2a2
L
4094 bfd_put_32 (output_bfd,
4095 (htab->elf.sgotplt->output_section->vma
4096 + htab->elf.sgotplt->output_offset
4097 + 8),
4098 htab->elf.splt->contents
765e526c 4099 + htab->lazy_plt->plt0_got2_offset);
eac338cf 4100
9a742a90 4101 if (htab->is_vxworks)
1f78f649 4102 {
f604c2a2
L
4103 Elf_Internal_Rela rel;
4104 int num_plts = (htab->elf.splt->size
765e526c 4105 / htab->plt.plt_entry_size) - 1;
f604c2a2 4106 unsigned char *p;
765e526c 4107 asection *srelplt2 = htab->srelplt2;
f604c2a2
L
4108
4109 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4110 + 4. On IA32 we use REL relocations so the
4111 addend goes in the PLT directly. */
4112 rel.r_offset = (htab->elf.splt->output_section->vma
4113 + htab->elf.splt->output_offset
765e526c 4114 + htab->lazy_plt->plt0_got1_offset);
1f78f649
L
4115 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4116 R_386_32);
f604c2a2 4117 bfd_elf32_swap_reloc_out (output_bfd, &rel,
0afcef53 4118 srelplt2->contents);
f604c2a2
L
4119 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4120 + 8. */
4121 rel.r_offset = (htab->elf.splt->output_section->vma
4122 + htab->elf.splt->output_offset
765e526c 4123 + htab->lazy_plt->plt0_got2_offset);
f604c2a2 4124 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
1f78f649 4125 R_386_32);
f604c2a2 4126 bfd_elf32_swap_reloc_out (output_bfd, &rel,
0afcef53 4127 srelplt2->contents +
f604c2a2
L
4128 sizeof (Elf32_External_Rel));
4129 /* Correct the .rel.plt.unloaded relocations. */
0afcef53 4130 p = srelplt2->contents;
f604c2a2
L
4131 if (bfd_link_pic (info))
4132 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4133 else
4134 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4135
4136 for (; num_plts; num_plts--)
4137 {
4138 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4139 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4140 R_386_32);
4141 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4142 p += sizeof (Elf32_External_Rel);
4143
4144 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4145 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
4146 R_386_32);
4147 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4148 p += sizeof (Elf32_External_Rel);
4149 }
1f78f649 4150 }
eac338cf
PB
4151 }
4152 }
252b5132 4153 }
f604c2a2
L
4154
4155 if (htab->plt_got != NULL && htab->plt_got->size > 0)
4156 elf_section_data (htab->plt_got->output_section)
765e526c 4157 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
ee2fdd6f
L
4158
4159 if (htab->plt_second != NULL && htab->plt_second->size > 0)
4160 elf_section_data (htab->plt_second->output_section)
765e526c 4161 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
252b5132
RH
4162 }
4163
f604c2a2
L
4164 /* Fill in the first three entries in the global offset table. */
4165 if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
252b5132 4166 {
56d4289c
L
4167 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
4168 {
4eca0228 4169 _bfd_error_handler
56d4289c
L
4170 (_("discarded output section: `%A'"), htab->elf.sgotplt);
4171 return FALSE;
4172 }
4173
f604c2a2
L
4174 bfd_put_32 (output_bfd,
4175 (sdyn == NULL ? 0
4176 : sdyn->output_section->vma + sdyn->output_offset),
4177 htab->elf.sgotplt->contents);
4178 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
4179 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
252b5132 4180
6de2ae4a 4181 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
12d0ee4a 4182 }
8c37241b 4183
e41b3a13 4184 /* Adjust .eh_frame for .plt section. */
9a2a56cc
AM
4185 if (htab->plt_eh_frame != NULL
4186 && htab->plt_eh_frame->contents != NULL)
e41b3a13
JJ
4187 {
4188 if (htab->elf.splt != NULL
4189 && htab->elf.splt->size != 0
4190 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
4191 && htab->elf.splt->output_section != NULL
4192 && htab->plt_eh_frame->output_section != NULL)
4193 {
4194 bfd_vma plt_start = htab->elf.splt->output_section->vma;
4195 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
4196 + htab->plt_eh_frame->output_offset
4197 + PLT_FDE_START_OFFSET;
4198 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4199 htab->plt_eh_frame->contents
4200 + PLT_FDE_START_OFFSET);
4201 }
4202 if (htab->plt_eh_frame->sec_info_type
dbaa2011 4203 == SEC_INFO_TYPE_EH_FRAME)
e41b3a13
JJ
4204 {
4205 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4206 htab->plt_eh_frame,
4207 htab->plt_eh_frame->contents))
4208 return FALSE;
4209 }
4210 }
4211
fff53dae
L
4212 /* Adjust .eh_frame for .plt.got section. */
4213 if (htab->plt_got_eh_frame != NULL
4214 && htab->plt_got_eh_frame->contents != NULL)
4215 {
4216 if (htab->plt_got != NULL
4217 && htab->plt_got->size != 0
4218 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
4219 && htab->plt_got->output_section != NULL
4220 && htab->plt_got_eh_frame->output_section != NULL)
4221 {
4222 bfd_vma plt_start = htab->plt_got->output_section->vma;
4223 bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
4224 + htab->plt_got_eh_frame->output_offset
4225 + PLT_FDE_START_OFFSET;
4226 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4227 htab->plt_got_eh_frame->contents
4228 + PLT_FDE_START_OFFSET);
4229 }
4230 if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
4231 {
4232 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4233 htab->plt_got_eh_frame,
4234 htab->plt_got_eh_frame->contents))
4235 return FALSE;
4236 }
4237 }
4238
ee2fdd6f
L
4239 /* Adjust .eh_frame for the second PLT section. */
4240 if (htab->plt_second_eh_frame != NULL
4241 && htab->plt_second_eh_frame->contents != NULL)
4242 {
4243 if (htab->plt_second != NULL
4244 && htab->plt_second->size != 0
4245 && (htab->plt_second->flags & SEC_EXCLUDE) == 0
4246 && htab->plt_second->output_section != NULL
4247 && htab->plt_second_eh_frame->output_section != NULL)
4248 {
4249 bfd_vma plt_start = htab->plt_second->output_section->vma;
4250 bfd_vma eh_frame_start
4251 = (htab->plt_second_eh_frame->output_section->vma
4252 + htab->plt_second_eh_frame->output_offset
4253 + PLT_FDE_START_OFFSET);
4254 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4255 htab->plt_second_eh_frame->contents
4256 + PLT_FDE_START_OFFSET);
4257 }
4258 if (htab->plt_second_eh_frame->sec_info_type
4259 == SEC_INFO_TYPE_EH_FRAME)
4260 {
4261 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4262 htab->plt_second_eh_frame,
4263 htab->plt_second_eh_frame->contents))
4264 return FALSE;
4265 }
4266 }
4267
6de2ae4a
L
4268 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4269 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
8c37241b 4270
aec6b87e
L
4271 /* Fill PLT entries for undefined weak symbols in PIE. */
4272 if (bfd_link_pie (info))
4273 bfd_hash_traverse (&info->hash->table,
4274 elf_i386_pie_finish_undefweak_symbol,
4275 info);
4276
b34976b6 4277 return TRUE;
252b5132
RH
4278}
4279
233cc9c1
L
4280/* Fill PLT/GOT entries and allocate dynamic relocations for local
4281 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4282 It has to be done before elf_link_sort_relocs is called so that
4283 dynamic relocations are properly sorted. */
4284
4285static bfd_boolean
4286elf_i386_output_arch_local_syms
4287 (bfd *output_bfd ATTRIBUTE_UNUSED,
4288 struct bfd_link_info *info,
4289 void *flaginfo ATTRIBUTE_UNUSED,
4290 int (*func) (void *, const char *,
4291 Elf_Internal_Sym *,
4292 asection *,
4293 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4294{
0afcef53
L
4295 struct elf_x86_link_hash_table *htab
4296 = elf_x86_hash_table (info, I386_ELF_DATA);
233cc9c1
L
4297 if (htab == NULL)
4298 return FALSE;
4299
4300 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4301 htab_traverse (htab->loc_hash_table,
4302 elf_i386_finish_local_dynamic_symbol,
4303 info);
4304
4305 return TRUE;
4306}
4307
f604c2a2 4308/* Forward declaration. */
765e526c 4309static const struct elf_x86_lazy_plt_layout elf_i386_nacl_plt;
f604c2a2
L
4310
4311/* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
4312 dynamic relocations. */
4313
4314static long
4315elf_i386_get_synthetic_symtab (bfd *abfd,
4316 long symcount ATTRIBUTE_UNUSED,
4317 asymbol **syms ATTRIBUTE_UNUSED,
4318 long dynsymcount,
4319 asymbol **dynsyms,
4320 asymbol **ret)
4321{
f493882d 4322 long count, i, n;
f604c2a2 4323 int j;
f604c2a2 4324 bfd_byte *plt_contents;
f493882d 4325 long relsize;
765e526c
L
4326 const struct elf_x86_lazy_plt_layout *lazy_plt;
4327 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4328 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4329 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
f604c2a2
L
4330 asection *plt;
4331 bfd_vma got_addr;
f493882d
L
4332 enum elf_x86_plt_type plt_type;
4333 struct elf_x86_plt plts[] =
3972882e 4334 {
f493882d
L
4335 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4336 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
4337 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4338 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
f604c2a2 4339 };
144bed8d 4340
f604c2a2 4341 *ret = NULL;
144bed8d 4342
f604c2a2
L
4343 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4344 return 0;
3972882e 4345
f604c2a2
L
4346 if (dynsymcount <= 0)
4347 return 0;
cca5b8b6 4348
f604c2a2
L
4349 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4350 if (relsize <= 0)
4351 return -1;
cca5b8b6 4352
f604c2a2
L
4353 non_lazy_plt = NULL;
4354 /* Silence GCC 6. */
4355 lazy_plt = NULL;
ee2fdd6f
L
4356 non_lazy_ibt_plt = NULL;
4357 lazy_ibt_plt = NULL;
f604c2a2 4358 switch (get_elf_i386_backend_data (abfd)->os)
144bed8d 4359 {
f604c2a2
L
4360 case is_normal:
4361 non_lazy_plt = &elf_i386_non_lazy_plt;
ee2fdd6f
L
4362 lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4363 non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
f604c2a2
L
4364 /* Fall through */
4365 case is_vxworks:
4366 lazy_plt = &elf_i386_lazy_plt;
4367 break;
4368 case is_nacl:
4369 lazy_plt = &elf_i386_nacl_plt;
4370 break;
4371 }
4372
4373 got_addr = 0;
3972882e 4374
f604c2a2
L
4375 count = 0;
4376 for (j = 0; plts[j].name != NULL; j++)
4377 {
4378 plt = bfd_get_section_by_name (abfd, plts[j].name);
90efb642 4379 if (plt == NULL || plt->size == 0)
6f25f223 4380 continue;
533d0af0 4381
f604c2a2
L
4382 /* Get the PLT section contents. */
4383 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
4384 if (plt_contents == NULL)
4385 break;
4386 if (!bfd_get_section_contents (abfd, (asection *) plt,
4387 plt_contents, 0, plt->size))
4388 {
4389 free (plt_contents);
4390 break;
4391 }
4392
4393 /* Check what kind of PLT it is. */
4394 plt_type = plt_unknown;
90efb642
L
4395 if (plts[j].type == plt_unknown
4396 && (plt->size >= (lazy_plt->plt0_entry_size
4397 + lazy_plt->plt_entry_size)))
f604c2a2
L
4398 {
4399 /* Match lazy PLT first. */
4400 if (memcmp (plt_contents, lazy_plt->plt0_entry,
4401 lazy_plt->plt0_got1_offset) == 0)
ee2fdd6f
L
4402 {
4403 /* The fist entry in the lazy IBT PLT is the same as the
4404 normal lazy PLT. */
4405 if (lazy_ibt_plt != NULL
90efb642 4406 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
ee2fdd6f
L
4407 lazy_ibt_plt->plt_entry,
4408 lazy_ibt_plt->plt_got_offset) == 0))
4409 plt_type = plt_lazy | plt_second;
4410 else
4411 plt_type = plt_lazy;
4412 }
f604c2a2
L
4413 else if (memcmp (plt_contents, lazy_plt->pic_plt0_entry,
4414 lazy_plt->plt0_got1_offset) == 0)
ee2fdd6f
L
4415 {
4416 /* The fist entry in the PIC lazy IBT PLT is the same as
4417 the normal PIC lazy PLT. */
4418 if (lazy_ibt_plt != NULL
90efb642 4419 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
ee2fdd6f
L
4420 lazy_ibt_plt->pic_plt_entry,
4421 lazy_ibt_plt->plt_got_offset) == 0))
4422 plt_type = plt_lazy | plt_pic | plt_second;
4423 else
4424 plt_type = plt_lazy | plt_pic;
4425 }
f604c2a2
L
4426 }
4427
4428 if (non_lazy_plt != NULL
90efb642
L
4429 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
4430 && plt->size >= non_lazy_plt->plt_entry_size)
f604c2a2
L
4431 {
4432 /* Match non-lazy PLT. */
4433 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
4434 non_lazy_plt->plt_got_offset) == 0)
4435 plt_type = plt_non_lazy;
4436 else if (memcmp (plt_contents, non_lazy_plt->pic_plt_entry,
4437 non_lazy_plt->plt_got_offset) == 0)
4438 plt_type = plt_pic;
4439 }
4440
ee2fdd6f 4441 if ((non_lazy_ibt_plt != NULL)
90efb642
L
4442 && (plt_type == plt_unknown || plt_type == plt_second)
4443 && plt->size >= non_lazy_ibt_plt->plt_entry_size)
ee2fdd6f
L
4444 {
4445 if (memcmp (plt_contents,
4446 non_lazy_ibt_plt->plt_entry,
4447 non_lazy_ibt_plt->plt_got_offset) == 0)
4448 {
4449 /* Match IBT PLT. */
4450 plt_type = plt_second;
4451 non_lazy_plt = non_lazy_ibt_plt;
4452 }
4453 else if (memcmp (plt_contents,
4454 non_lazy_ibt_plt->pic_plt_entry,
4455 non_lazy_ibt_plt->plt_got_offset) == 0)
4456 {
4457 /* Match PIC IBT PLT. */
4458 plt_type = plt_second | plt_pic;
4459 non_lazy_plt = non_lazy_ibt_plt;
4460 }
4461 }
4462
f604c2a2 4463 if (plt_type == plt_unknown)
37c0b6ee
L
4464 {
4465 free (plt_contents);
4466 continue;
4467 }
3972882e 4468
f604c2a2
L
4469 plts[j].sec = plt;
4470 plts[j].type = plt_type;
ec1f73bb 4471
f604c2a2
L
4472 if ((plt_type & plt_lazy))
4473 {
4474 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
4475 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
4476 /* Skip PLT0 in lazy PLT. */
4477 i = 1;
4478 }
4479 else
4480 {
4481 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
4482 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
4483 i = 0;
4484 }
fca6ae69 4485
ee2fdd6f
L
4486 /* Skip lazy PLT when the second PLT is used. */
4487 if ((plt_type & (plt_lazy | plt_second))
4488 == (plt_lazy | plt_second))
4489 plts[j].count = 0;
4490 else
4491 {
4492 n = plt->size / plts[j].plt_entry_size;
4493 plts[j].count = n;
4494 count += n - i;
4495 }
f604c2a2
L
4496
4497 plts[j].contents = plt_contents;
4498
4499 /* The _GLOBAL_OFFSET_TABLE_ address is needed. */
4500 if ((plt_type & plt_pic))
4501 got_addr = (bfd_vma) -1;
144bed8d
L
4502 }
4503
f493882d
L
4504 return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
4505 got_addr, plts, dynsyms,
4506 ret);
4c45e5c9
JJ
4507}
4508
f604c2a2
L
4509/* Set up i386 GNU properties. Return the first relocatable ELF input
4510 with GNU properties if found. Otherwise, return NULL. */
4511
4512static bfd *
4513elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
4514{
1de031c8 4515 struct elf_x86_init_table init_table;
f604c2a2 4516
1de031c8
L
4517 init_table.normal_target = FALSE;
4518 init_table.is_vxworks = FALSE;
f604c2a2
L
4519 switch (get_elf_i386_backend_data (info->output_bfd)->os)
4520 {
4521 case is_normal:
1de031c8
L
4522 init_table.lazy_plt = &elf_i386_lazy_plt;
4523 init_table.non_lazy_plt = &elf_i386_non_lazy_plt;
4524 init_table.lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4525 init_table.non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4526 init_table.normal_target = TRUE;
f604c2a2
L
4527 break;
4528 case is_vxworks:
1de031c8
L
4529 init_table.lazy_plt = &elf_i386_lazy_plt;
4530 init_table.non_lazy_plt = NULL;
4531 init_table.lazy_ibt_plt = NULL;
4532 init_table.non_lazy_ibt_plt = NULL;
4533 init_table.is_vxworks = TRUE;
f604c2a2
L
4534 break;
4535 case is_nacl:
1de031c8
L
4536 init_table.lazy_plt = &elf_i386_nacl_plt;
4537 init_table.non_lazy_plt = NULL;
4538 init_table.lazy_ibt_plt = NULL;
4539 init_table.non_lazy_ibt_plt = NULL;
f604c2a2
L
4540 break;
4541 }
4542
1de031c8
L
4543 init_table.r_info = elf32_r_info;
4544 init_table.r_sym = elf32_r_sym;
7a382c1c 4545
1de031c8 4546 return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
f604c2a2
L
4547}
4548
6d00b590 4549#define TARGET_LITTLE_SYM i386_elf32_vec
252b5132
RH
4550#define TARGET_LITTLE_NAME "elf32-i386"
4551#define ELF_ARCH bfd_arch_i386
ae95ffa6 4552#define ELF_TARGET_ID I386_ELF_DATA
252b5132
RH
4553#define ELF_MACHINE_CODE EM_386
4554#define ELF_MAXPAGESIZE 0x1000
252b5132
RH
4555
4556#define elf_backend_can_gc_sections 1
51b64d56 4557#define elf_backend_can_refcount 1
252b5132
RH
4558#define elf_backend_want_got_plt 1
4559#define elf_backend_plt_readonly 1
4560#define elf_backend_want_plt_sym 0
4561#define elf_backend_got_header_size 12
e41b3a13 4562#define elf_backend_plt_alignment 4
64f52338 4563#define elf_backend_dtrel_excludes_plt 1
f7483970 4564#define elf_backend_extern_protected_data 1
bedfd056 4565#define elf_backend_caches_rawsize 1
5474d94f 4566#define elf_backend_want_dynrelro 1
252b5132 4567
8c29f035
AM
4568/* Support RELA for objdump of prelink objects. */
4569#define elf_info_to_howto elf_i386_info_to_howto_rel
dd5724d5
AM
4570#define elf_info_to_howto_rel elf_i386_info_to_howto_rel
4571
dd5724d5 4572#define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
dd5724d5 4573#define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
13285a1b 4574#define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
3972882e 4575#define bfd_elf32_get_synthetic_symtab elf_i386_get_synthetic_symtab
dd5724d5 4576
13285a1b 4577#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
dd5724d5 4578#define elf_backend_check_relocs elf_i386_check_relocs
f604c2a2 4579#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
0ac8d2ca 4580#define elf_backend_fake_sections elf_i386_fake_sections
dd5724d5
AM
4581#define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
4582#define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
233cc9c1 4583#define elf_backend_output_arch_local_syms elf_i386_output_arch_local_syms
c5fccbec
DJ
4584#define elf_backend_grok_prstatus elf_i386_grok_prstatus
4585#define elf_backend_grok_psinfo elf_i386_grok_psinfo
db6751f2 4586#define elf_backend_reloc_type_class elf_i386_reloc_type_class
0ac8d2ca 4587#define elf_backend_relocate_section elf_i386_relocate_section
f604c2a2 4588#define elf_backend_setup_gnu_properties elf_i386_link_setup_gnu_properties
dd5724d5 4589
252b5132 4590#include "elf32-target.h"
2bc3c89a
AM
4591
4592/* FreeBSD support. */
4593
4594#undef TARGET_LITTLE_SYM
6d00b590 4595#define TARGET_LITTLE_SYM i386_elf32_fbsd_vec
2bc3c89a
AM
4596#undef TARGET_LITTLE_NAME
4597#define TARGET_LITTLE_NAME "elf32-i386-freebsd"
d1036acb
L
4598#undef ELF_OSABI
4599#define ELF_OSABI ELFOSABI_FREEBSD
2bc3c89a
AM
4600
4601/* The kernel recognizes executables as valid only if they carry a
4602 "FreeBSD" label in the ELF header. So we put this label on all
4603 executables and (for simplicity) also all other object files. */
4604
2bc3c89a 4605static void
d8045f23 4606elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
2bc3c89a 4607{
78245035 4608 _bfd_elf_post_process_headers (abfd, info);
2bc3c89a 4609
2bc3c89a 4610#ifdef OLD_FREEBSD_ABI_LABEL
cf7363b4
L
4611 {
4612 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
4613 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4614 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4615 }
caf47ea6 4616#endif
2bc3c89a
AM
4617}
4618
4619#undef elf_backend_post_process_headers
d8045f23 4620#define elf_backend_post_process_headers elf_i386_fbsd_post_process_headers
571fe01f
NC
4621#undef elf32_bed
4622#define elf32_bed elf32_i386_fbsd_bed
2bc3c89a 4623
d8045f23
NC
4624#undef elf_backend_add_symbol_hook
4625
2bc3c89a 4626#include "elf32-target.h"
eac338cf 4627
a6cc6b3b
RO
4628/* Solaris 2. */
4629
4630#undef TARGET_LITTLE_SYM
6d00b590 4631#define TARGET_LITTLE_SYM i386_elf32_sol2_vec
a6cc6b3b
RO
4632#undef TARGET_LITTLE_NAME
4633#define TARGET_LITTLE_NAME "elf32-i386-sol2"
4634
914082d1
L
4635#undef elf_backend_post_process_headers
4636
a6cc6b3b
RO
4637/* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4638 objects won't be recognized. */
4639#undef ELF_OSABI
4640
4641#undef elf32_bed
4642#define elf32_bed elf32_i386_sol2_bed
4643
7dc98aea
RO
4644/* The 32-bit static TLS arena size is rounded to the nearest 8-byte
4645 boundary. */
84865015 4646#undef elf_backend_static_tls_alignment
7dc98aea
RO
4647#define elf_backend_static_tls_alignment 8
4648
a6cc6b3b
RO
4649/* The Solaris 2 ABI requires a plt symbol on all platforms.
4650
4651 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4652 File, p.63. */
84865015 4653#undef elf_backend_want_plt_sym
a6cc6b3b
RO
4654#define elf_backend_want_plt_sym 1
4655
84865015
NC
4656#undef elf_backend_strtab_flags
4657#define elf_backend_strtab_flags SHF_STRINGS
4658
5522f910
NC
4659/* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
4660 has a type >= SHT_LOOS. Returns TRUE if these fields were initialised
4661 FALSE otherwise. ISECTION is the best guess matching section from the
4662 input bfd IBFD, but it might be NULL. */
4663
84865015 4664static bfd_boolean
5522f910
NC
4665elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
4666 bfd *obfd ATTRIBUTE_UNUSED,
4667 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
4668 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
84865015
NC
4669{
4670 /* PR 19938: FIXME: Need to add code for setting the sh_info
5522f910
NC
4671 and sh_link fields of Solaris specific section types. */
4672 return FALSE;
84865015 4673
5522f910 4674 /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13,
84865015
NC
4675 Object File Format, Table 13-9 ELF sh_link and sh_info Interpretation:
4676
4677http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc
4678
4679 The following values should be set:
4680
4681Type Link Info
4682-----------------------------------------------------------------------------
4683SHT_SUNW_ancillary The section header index of 0
4684 [0x6fffffee] the associated string table.
4685
4686SHT_SUNW_capinfo The section header index of For a dynamic object, the
4687 [0x6ffffff0] the associated symbol table. section header index of
4688 the associated
4689 SHT_SUNW_capchain table,
4690 otherwise 0.
4691
4692SHT_SUNW_symsort The section header index of 0
4693 [0x6ffffff1] the associated symbol table.
4694
4695SHT_SUNW_tlssort The section header index of 0
4696 [0x6ffffff2] the associated symbol table.
4697
4698SHT_SUNW_LDYNSYM The section header index of One greater than the
4699 [0x6ffffff3] the associated string table. symbol table index of the
4700 This index is the same string last local symbol,
4701 table used by the SHT_DYNSYM STB_LOCAL. Since
4702 section. SHT_SUNW_LDYNSYM only
4703 contains local symbols,
4704 sh_info is equivalent to
4705 the number of symbols in
4706 the table.
4707
4708SHT_SUNW_cap If symbol capabilities exist, If any capabilities refer
4709 [0x6ffffff5] the section header index of to named strings, the
4710 the associated section header index of
4711 SHT_SUNW_capinfo table, the associated string
4712 otherwise 0. table, otherwise 0.
4713
4714SHT_SUNW_move The section header index of 0
4715 [0x6ffffffa] the associated symbol table.
4716
4717SHT_SUNW_COMDAT 0 0
4718 [0x6ffffffb]
4719
4720SHT_SUNW_syminfo The section header index of The section header index
4721 [0x6ffffffc] the associated symbol table. of the associated
4722 .dynamic section.
4723
4724SHT_SUNW_verdef The section header index of The number of version
4725 [0x6ffffffd] the associated string table. definitions within the
4726 section.
4727
4728SHT_SUNW_verneed The section header index of The number of version
4729 [0x6ffffffe] the associated string table. dependencies within the
4730 section.
4731
4732SHT_SUNW_versym The section header index of 0
4733 [0x6fffffff] the associated symbol table. */
84865015
NC
4734}
4735
5522f910
NC
4736#undef elf_backend_copy_special_section_fields
4737#define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields
84865015 4738
a6cc6b3b
RO
4739#include "elf32-target.h"
4740
bf64a951
L
4741/* Intel MCU support. */
4742
4743static bfd_boolean
4744elf32_iamcu_elf_object_p (bfd *abfd)
4745{
4746 /* Set the right machine number for an IAMCU elf32 file. */
4747 bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
4748 return TRUE;
4749}
4750
4751#undef TARGET_LITTLE_SYM
4752#define TARGET_LITTLE_SYM iamcu_elf32_vec
4753#undef TARGET_LITTLE_NAME
4754#define TARGET_LITTLE_NAME "elf32-iamcu"
84865015 4755#undef ELF_ARCH
bf64a951
L
4756#define ELF_ARCH bfd_arch_iamcu
4757
4758#undef ELF_MACHINE_CODE
4759#define ELF_MACHINE_CODE EM_IAMCU
4760
4761#undef ELF_OSABI
4762
4763#undef elf32_bed
4764#define elf32_bed elf32_iamcu_bed
4765
4766#undef elf_backend_object_p
4767#define elf_backend_object_p elf32_iamcu_elf_object_p
4768
4769#undef elf_backend_static_tls_alignment
4770
4771#undef elf_backend_want_plt_sym
4772#define elf_backend_want_plt_sym 0
4773
84865015 4774#undef elf_backend_strtab_flags
5522f910 4775#undef elf_backend_copy_special_section_fields
84865015 4776
bf64a951
L
4777#include "elf32-target.h"
4778
4779/* Restore defaults. */
4780#undef ELF_ARCH
4781#define ELF_ARCH bfd_arch_i386
4782#undef ELF_MACHINE_CODE
4783#define ELF_MACHINE_CODE EM_386
4784
a27e4371
RM
4785/* Native Client support. */
4786
4787#undef TARGET_LITTLE_SYM
6d00b590 4788#define TARGET_LITTLE_SYM i386_elf32_nacl_vec
a27e4371
RM
4789#undef TARGET_LITTLE_NAME
4790#define TARGET_LITTLE_NAME "elf32-i386-nacl"
4791#undef elf32_bed
4792#define elf32_bed elf32_i386_nacl_bed
4793
4794#undef ELF_MAXPAGESIZE
4795#define ELF_MAXPAGESIZE 0x10000
4796
4797/* Restore defaults. */
4798#undef ELF_OSABI
4799#undef elf_backend_want_plt_sym
4800#define elf_backend_want_plt_sym 0
4801#undef elf_backend_post_process_headers
a27e4371
RM
4802#undef elf_backend_static_tls_alignment
4803
4804/* NaCl uses substantially different PLT entries for the same effects. */
4805
4806#undef elf_backend_plt_alignment
4807#define elf_backend_plt_alignment 5
4808#define NACL_PLT_ENTRY_SIZE 64
4809#define NACLMASK 0xe0 /* 32-byte alignment mask. */
4810
4811static const bfd_byte elf_i386_nacl_plt0_entry[] =
4812 {
4813 0xff, 0x35, /* pushl contents of address */
4814 0, 0, 0, 0, /* replaced with address of .got + 4. */
4815 0x8b, 0x0d, /* movl contents of address, %ecx */
4816 0, 0, 0, 0, /* replaced with address of .got + 8. */
4817 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
4818 0xff, 0xe1 /* jmp *%ecx */
4819 };
4820
4821static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
4822 {
4823 0x8b, 0x0d, /* movl contents of address, %ecx */
4824 0, 0, 0, 0, /* replaced with GOT slot address. */
4825 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
4826 0xff, 0xe1, /* jmp *%ecx */
4827
4828 /* Pad to the next 32-byte boundary with nop instructions. */
4829 0x90,
4830 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4831 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4832
4833 /* Lazy GOT entries point here (32-byte aligned). */
4834 0x68, /* pushl immediate */
4835 0, 0, 0, 0, /* replaced with reloc offset. */
4836 0xe9, /* jmp relative */
4837 0, 0, 0, 0, /* replaced with offset to .plt. */
4838
4839 /* Pad to the next 32-byte boundary with nop instructions. */
4840 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4841 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4842 0x90, 0x90
4843 };
4844
4845static const bfd_byte
4846elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] =
4847 {
4848 0xff, 0x73, 0x04, /* pushl 4(%ebx) */
4849 0x8b, 0x4b, 0x08, /* mov 0x8(%ebx), %ecx */
4850 0x83, 0xe1, 0xe0, /* and $NACLMASK, %ecx */
4851 0xff, 0xe1, /* jmp *%ecx */
caa0075c
RM
4852
4853 /* This is expected to be the same size as elf_i386_nacl_plt0_entry,
4854 so pad to that size with nop instructions. */
4855 0x90, 0x90, 0x90, 0x90, 0x90, 0x90
a27e4371
RM
4856 };
4857
4858static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] =
4859 {
4860 0x8b, 0x8b, /* movl offset(%ebx), %ecx */
4861 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
4862 0x83, 0xe1, 0xe0, /* andl $NACLMASK, %ecx */
4863 0xff, 0xe1, /* jmp *%ecx */
4864
4865 /* Pad to the next 32-byte boundary with nop instructions. */
4866 0x90,
4867 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4868 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4869
4870 /* Lazy GOT entries point here (32-byte aligned). */
4871 0x68, /* pushl immediate */
4872 0, 0, 0, 0, /* replaced with offset into relocation table. */
4873 0xe9, /* jmp relative */
4874 0, 0, 0, 0, /* replaced with offset to start of .plt. */
4875
4876 /* Pad to the next 32-byte boundary with nop instructions. */
4877 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4878 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4879 0x90, 0x90
4880 };
4881
4882static const bfd_byte elf_i386_nacl_eh_frame_plt[] =
4883 {
4884#if (PLT_CIE_LENGTH != 20 \
4885 || PLT_FDE_LENGTH != 36 \
4886 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
4887 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
4888# error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!"
4889#endif
4890 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
4891 0, 0, 0, 0, /* CIE ID */
4892 1, /* CIE version */
4893 'z', 'R', 0, /* Augmentation string */
4894 1, /* Code alignment factor */
4895 0x7c, /* Data alignment factor: -4 */
4896 8, /* Return address column */
4897 1, /* Augmentation size */
4898 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
4899 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
4900 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
4901 DW_CFA_nop, DW_CFA_nop,
4902
4903 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
4904 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
4905 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
4906 0, 0, 0, 0, /* .plt size goes here */
4907 0, /* Augmentation size */
4908 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
4909 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
4910 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
4911 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
4912 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
4913 13, /* Block length */
4914 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
4915 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
4916 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
4917 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
4918 DW_CFA_nop, DW_CFA_nop
4919 };
4920
765e526c 4921static const struct elf_x86_lazy_plt_layout elf_i386_nacl_plt =
a27e4371
RM
4922 {
4923 elf_i386_nacl_plt0_entry, /* plt0_entry */
4924 sizeof (elf_i386_nacl_plt0_entry), /* plt0_entry_size */
a27e4371
RM
4925 elf_i386_nacl_plt_entry, /* plt_entry */
4926 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
765e526c
L
4927 2, /* plt0_got1_offset */
4928 8, /* plt0_got2_offset */
4929 0, /* plt0_got2_insn_end */
a27e4371
RM
4930 2, /* plt_got_offset */
4931 33, /* plt_reloc_offset */
4932 38, /* plt_plt_offset */
765e526c
L
4933 0, /* plt_got_insn_size */
4934 0, /* plt_plt_insn_end */
a27e4371
RM
4935 32, /* plt_lazy_offset */
4936 elf_i386_nacl_pic_plt0_entry, /* pic_plt0_entry */
4937 elf_i386_nacl_pic_plt_entry, /* pic_plt_entry */
4938 elf_i386_nacl_eh_frame_plt, /* eh_frame_plt */
f604c2a2 4939 sizeof (elf_i386_nacl_eh_frame_plt) /* eh_frame_plt_size */
a27e4371
RM
4940 };
4941
4942static const struct elf_i386_backend_data elf_i386_nacl_arch_bed =
4943 {
f604c2a2
L
4944 0x90, /* plt0_pad_byte: nop insn */
4945 is_nacl /* os */
a27e4371
RM
4946 };
4947
64b384e1
RM
4948static bfd_boolean
4949elf32_i386_nacl_elf_object_p (bfd *abfd)
4950{
4951 /* Set the right machine number for a NaCl i386 ELF32 file. */
4952 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl);
4953 return TRUE;
4954}
4955
a27e4371
RM
4956#undef elf_backend_arch_data
4957#define elf_backend_arch_data &elf_i386_nacl_arch_bed
4958
64b384e1
RM
4959#undef elf_backend_object_p
4960#define elf_backend_object_p elf32_i386_nacl_elf_object_p
5a68afcf
RM
4961#undef elf_backend_modify_segment_map
4962#define elf_backend_modify_segment_map nacl_modify_segment_map
4963#undef elf_backend_modify_program_headers
4964#define elf_backend_modify_program_headers nacl_modify_program_headers
887badb3
RM
4965#undef elf_backend_final_write_processing
4966#define elf_backend_final_write_processing nacl_final_write_processing
5a68afcf 4967
a27e4371
RM
4968#include "elf32-target.h"
4969
5a68afcf 4970/* Restore defaults. */
64b384e1 4971#undef elf_backend_object_p
5a68afcf
RM
4972#undef elf_backend_modify_segment_map
4973#undef elf_backend_modify_program_headers
887badb3 4974#undef elf_backend_final_write_processing
5a68afcf 4975
eac338cf
PB
4976/* VxWorks support. */
4977
4978#undef TARGET_LITTLE_SYM
6d00b590 4979#define TARGET_LITTLE_SYM i386_elf32_vxworks_vec
eac338cf
PB
4980#undef TARGET_LITTLE_NAME
4981#define TARGET_LITTLE_NAME "elf32-i386-vxworks"
d1036acb 4982#undef ELF_OSABI
1587442d
L
4983#undef ELF_MAXPAGESIZE
4984#define ELF_MAXPAGESIZE 0x1000
a27e4371
RM
4985#undef elf_backend_plt_alignment
4986#define elf_backend_plt_alignment 4
eac338cf 4987
23209a78
RM
4988static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed =
4989 {
4990 0x90, /* plt0_pad_byte */
f604c2a2 4991 is_vxworks /* os */
23209a78 4992 };
eac338cf 4993
23209a78
RM
4994#undef elf_backend_arch_data
4995#define elf_backend_arch_data &elf_i386_vxworks_arch_bed
eac338cf 4996
13285a1b 4997#undef elf_backend_relocs_compatible
eac338cf
PB
4998#undef elf_backend_add_symbol_hook
4999#define elf_backend_add_symbol_hook \
5000 elf_vxworks_add_symbol_hook
5001#undef elf_backend_link_output_symbol_hook
5002#define elf_backend_link_output_symbol_hook \
9c72ff84 5003 elf_vxworks_link_output_symbol_hook
eac338cf
PB
5004#undef elf_backend_emit_relocs
5005#define elf_backend_emit_relocs elf_vxworks_emit_relocs
5006#undef elf_backend_final_write_processing
5007#define elf_backend_final_write_processing \
5008 elf_vxworks_final_write_processing
7dc98aea 5009#undef elf_backend_static_tls_alignment
eac338cf
PB
5010
5011/* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
5012 define it. */
5013#undef elf_backend_want_plt_sym
5014#define elf_backend_want_plt_sym 1
5015
5016#undef elf32_bed
5017#define elf32_bed elf32_i386_vxworks_bed
5018
5019#include "elf32-target.h"
This page took 1.456521 seconds and 4 git commands to generate.