x86: Check target_id instead of elf_machine_code
[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
252b5132 185#ifdef DEBUG_GEN_RELOC
55fd94b0
AM
186#define TRACE(str) \
187 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
252b5132
RH
188#else
189#define TRACE(str)
190#endif
191
192static reloc_howto_type *
55fd94b0
AM
193elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
194 bfd_reloc_code_real_type code)
252b5132
RH
195{
196 switch (code)
197 {
198 case BFD_RELOC_NONE:
199 TRACE ("BFD_RELOC_NONE");
55fd94b0 200 return &elf_howto_table[R_386_NONE];
252b5132
RH
201
202 case BFD_RELOC_32:
203 TRACE ("BFD_RELOC_32");
55fd94b0 204 return &elf_howto_table[R_386_32];
252b5132
RH
205
206 case BFD_RELOC_CTOR:
207 TRACE ("BFD_RELOC_CTOR");
55fd94b0 208 return &elf_howto_table[R_386_32];
252b5132
RH
209
210 case BFD_RELOC_32_PCREL:
211 TRACE ("BFD_RELOC_PC32");
55fd94b0 212 return &elf_howto_table[R_386_PC32];
252b5132
RH
213
214 case BFD_RELOC_386_GOT32:
215 TRACE ("BFD_RELOC_386_GOT32");
55fd94b0 216 return &elf_howto_table[R_386_GOT32];
252b5132
RH
217
218 case BFD_RELOC_386_PLT32:
219 TRACE ("BFD_RELOC_386_PLT32");
55fd94b0 220 return &elf_howto_table[R_386_PLT32];
252b5132
RH
221
222 case BFD_RELOC_386_COPY:
223 TRACE ("BFD_RELOC_386_COPY");
55fd94b0 224 return &elf_howto_table[R_386_COPY];
252b5132
RH
225
226 case BFD_RELOC_386_GLOB_DAT:
227 TRACE ("BFD_RELOC_386_GLOB_DAT");
55fd94b0 228 return &elf_howto_table[R_386_GLOB_DAT];
252b5132
RH
229
230 case BFD_RELOC_386_JUMP_SLOT:
231 TRACE ("BFD_RELOC_386_JUMP_SLOT");
55fd94b0 232 return &elf_howto_table[R_386_JUMP_SLOT];
252b5132
RH
233
234 case BFD_RELOC_386_RELATIVE:
235 TRACE ("BFD_RELOC_386_RELATIVE");
55fd94b0 236 return &elf_howto_table[R_386_RELATIVE];
252b5132
RH
237
238 case BFD_RELOC_386_GOTOFF:
239 TRACE ("BFD_RELOC_386_GOTOFF");
55fd94b0 240 return &elf_howto_table[R_386_GOTOFF];
252b5132
RH
241
242 case BFD_RELOC_386_GOTPC:
243 TRACE ("BFD_RELOC_386_GOTPC");
55fd94b0 244 return &elf_howto_table[R_386_GOTPC];
252b5132 245
37e55690
JJ
246 /* These relocs are a GNU extension. */
247 case BFD_RELOC_386_TLS_TPOFF:
248 TRACE ("BFD_RELOC_386_TLS_TPOFF");
55fd94b0 249 return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
37e55690
JJ
250
251 case BFD_RELOC_386_TLS_IE:
252 TRACE ("BFD_RELOC_386_TLS_IE");
55fd94b0 253 return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
37e55690
JJ
254
255 case BFD_RELOC_386_TLS_GOTIE:
256 TRACE ("BFD_RELOC_386_TLS_GOTIE");
55fd94b0 257 return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
37e55690 258
13ae64f3
JJ
259 case BFD_RELOC_386_TLS_LE:
260 TRACE ("BFD_RELOC_386_TLS_LE");
55fd94b0 261 return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
13ae64f3
JJ
262
263 case BFD_RELOC_386_TLS_GD:
264 TRACE ("BFD_RELOC_386_TLS_GD");
55fd94b0 265 return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
13ae64f3
JJ
266
267 case BFD_RELOC_386_TLS_LDM:
268 TRACE ("BFD_RELOC_386_TLS_LDM");
55fd94b0 269 return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
13ae64f3 270
252b5132
RH
271 case BFD_RELOC_16:
272 TRACE ("BFD_RELOC_16");
55fd94b0 273 return &elf_howto_table[R_386_16 - R_386_ext_offset];
252b5132
RH
274
275 case BFD_RELOC_16_PCREL:
276 TRACE ("BFD_RELOC_16_PCREL");
55fd94b0 277 return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
252b5132
RH
278
279 case BFD_RELOC_8:
280 TRACE ("BFD_RELOC_8");
55fd94b0 281 return &elf_howto_table[R_386_8 - R_386_ext_offset];
252b5132
RH
282
283 case BFD_RELOC_8_PCREL:
284 TRACE ("BFD_RELOC_8_PCREL");
55fd94b0 285 return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
252b5132 286
13ae64f3
JJ
287 /* Common with Sun TLS implementation. */
288 case BFD_RELOC_386_TLS_LDO_32:
289 TRACE ("BFD_RELOC_386_TLS_LDO_32");
55fd94b0 290 return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
13ae64f3
JJ
291
292 case BFD_RELOC_386_TLS_IE_32:
293 TRACE ("BFD_RELOC_386_TLS_IE_32");
55fd94b0 294 return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
13ae64f3
JJ
295
296 case BFD_RELOC_386_TLS_LE_32:
297 TRACE ("BFD_RELOC_386_TLS_LE_32");
55fd94b0 298 return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
13ae64f3
JJ
299
300 case BFD_RELOC_386_TLS_DTPMOD32:
301 TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
55fd94b0 302 return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
13ae64f3
JJ
303
304 case BFD_RELOC_386_TLS_DTPOFF32:
305 TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
55fd94b0 306 return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
13ae64f3
JJ
307
308 case BFD_RELOC_386_TLS_TPOFF32:
309 TRACE ("BFD_RELOC_386_TLS_TPOFF32");
55fd94b0 310 return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
13ae64f3 311
1788fc08
L
312 case BFD_RELOC_SIZE32:
313 TRACE ("BFD_RELOC_SIZE32");
314 return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset];
315
67a4f2b7
AO
316 case BFD_RELOC_386_TLS_GOTDESC:
317 TRACE ("BFD_RELOC_386_TLS_GOTDESC");
318 return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
319
320 case BFD_RELOC_386_TLS_DESC_CALL:
321 TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
322 return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
323
324 case BFD_RELOC_386_TLS_DESC:
325 TRACE ("BFD_RELOC_386_TLS_DESC");
326 return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
327
cbe950e9
L
328 case BFD_RELOC_386_IRELATIVE:
329 TRACE ("BFD_RELOC_386_IRELATIVE");
2a750708 330 return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset];
cbe950e9 331
02a86693
L
332 case BFD_RELOC_386_GOT32X:
333 TRACE ("BFD_RELOC_386_GOT32X");
334 return &elf_howto_table[R_386_GOT32X - R_386_tls_offset];
335
252b5132
RH
336 case BFD_RELOC_VTABLE_INHERIT:
337 TRACE ("BFD_RELOC_VTABLE_INHERIT");
55fd94b0 338 return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
252b5132
RH
339
340 case BFD_RELOC_VTABLE_ENTRY:
341 TRACE ("BFD_RELOC_VTABLE_ENTRY");
55fd94b0 342 return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
252b5132
RH
343
344 default:
345 break;
346 }
347
348 TRACE ("Unknown");
349 return 0;
350}
351
157090f7
AM
352static reloc_howto_type *
353elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
354 const char *r_name)
355{
356 unsigned int i;
357
358 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
359 if (elf_howto_table[i].name != NULL
360 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
361 return &elf_howto_table[i];
362
363 return NULL;
364}
365
142411ca
L
366static reloc_howto_type *
367elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
252b5132 368{
dc47f327
AM
369 unsigned int indx;
370
371 if ((indx = r_type) >= R_386_standard
372 && ((indx = r_type - R_386_ext_offset) - R_386_standard
373 >= R_386_ext - R_386_standard)
13ae64f3 374 && ((indx = r_type - R_386_tls_offset) - R_386_ext
c74be520
L
375 >= R_386_ext2 - R_386_ext)
376 && ((indx = r_type - R_386_vt_offset) - R_386_ext2
377 >= R_386_vt - R_386_ext2))
252b5132 378 {
695344c0 379 /* xgettext:c-format */
4eca0228
AM
380 _bfd_error_handler (_("%B: invalid relocation type %d"),
381 abfd, (int) r_type);
55fd94b0 382 indx = R_386_NONE;
252b5132 383 }
06614111
NC
384 /* PR 17512: file: 0f67f69d. */
385 if (elf_howto_table [indx].type != r_type)
386 return NULL;
142411ca
L
387 return &elf_howto_table[indx];
388}
389
390static void
391elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
392 arelent *cache_ptr,
393 Elf_Internal_Rela *dst)
394{
395 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
396 cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type);
252b5132
RH
397}
398
399/* Return whether a symbol name implies a local label. The UnixWare
400 2.1 cc generates temporary symbols that start with .X, so we
401 recognize them here. FIXME: do other SVR4 compilers also use .X?.
402 If so, we should move the .X recognition into
403 _bfd_elf_is_local_label_name. */
404
b34976b6 405static bfd_boolean
55fd94b0 406elf_i386_is_local_label_name (bfd *abfd, const char *name)
252b5132
RH
407{
408 if (name[0] == '.' && name[1] == 'X')
b34976b6 409 return TRUE;
252b5132
RH
410
411 return _bfd_elf_is_local_label_name (abfd, name);
412}
413\f
38701953 414/* Support for core dump NOTE sections. */
61adc1a4 415
b34976b6 416static bfd_boolean
55fd94b0 417elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
38701953
AM
418{
419 int offset;
eea6121a 420 size_t size;
38701953 421
61adc1a4 422 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
38701953 423 {
61adc1a4
NC
424 int pr_version = bfd_get_32 (abfd, note->descdata);
425
426 if (pr_version != 1)
427 return FALSE;
428
429 /* pr_cursig */
228e534f 430 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20);
61adc1a4
NC
431
432 /* pr_pid */
228e534f 433 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
61adc1a4
NC
434
435 /* pr_reg */
436 offset = 28;
eea6121a 437 size = bfd_get_32 (abfd, note->descdata + 8);
61adc1a4
NC
438 }
439 else
440 {
441 switch (note->descsz)
442 {
443 default:
444 return FALSE;
38701953 445
61adc1a4
NC
446 case 144: /* Linux/i386 */
447 /* pr_cursig */
228e534f 448 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
38701953 449
61adc1a4 450 /* pr_pid */
228e534f 451 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
38701953 452
61adc1a4
NC
453 /* pr_reg */
454 offset = 72;
eea6121a 455 size = 68;
38701953 456
61adc1a4
NC
457 break;
458 }
38701953
AM
459 }
460
461 /* Make a ".reg/999" section. */
462 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 463 size, note->descpos + offset);
38701953
AM
464}
465
b34976b6 466static bfd_boolean
55fd94b0 467elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
38701953 468{
61adc1a4 469 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
38701953 470 {
61adc1a4
NC
471 int pr_version = bfd_get_32 (abfd, note->descdata);
472
473 if (pr_version != 1)
b34976b6 474 return FALSE;
38701953 475
228e534f 476 elf_tdata (abfd)->core->program
61adc1a4 477 = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
228e534f 478 elf_tdata (abfd)->core->command
61adc1a4
NC
479 = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
480 }
481 else
482 {
483 switch (note->descsz)
484 {
485 default:
486 return FALSE;
487
488 case 124: /* Linux/i386 elf_prpsinfo. */
228e534f 489 elf_tdata (abfd)->core->pid
261b8d08 490 = bfd_get_32 (abfd, note->descdata + 12);
228e534f 491 elf_tdata (abfd)->core->program
61adc1a4 492 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
228e534f 493 elf_tdata (abfd)->core->command
61adc1a4
NC
494 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
495 }
38701953
AM
496 }
497
498 /* Note that for some reason, a spurious space is tacked
499 onto the end of the args in some (at least one anyway)
500 implementations, so strip it off if it exists. */
38701953 501 {
228e534f 502 char *command = elf_tdata (abfd)->core->command;
38701953
AM
503 int n = strlen (command);
504
505 if (0 < n && command[n - 1] == ' ')
506 command[n - 1] = '\0';
507 }
508
b34976b6 509 return TRUE;
38701953
AM
510}
511\f
512/* Functions for the i386 ELF linker.
513
514 In order to gain some understanding of code in this file without
515 knowing all the intricate details of the linker, note the
516 following:
517
518 Functions named elf_i386_* are called by external routines, other
519 functions are only called locally. elf_i386_* functions appear
520 in this file more or less in the order in which they are called
521 from external routines. eg. elf_i386_check_relocs is called
522 early in the link process, elf_i386_finish_dynamic_sections is
523 one of the last functions. */
524
f604c2a2 525/* The size in bytes of an entry in the lazy procedure linkage table. */
252b5132 526
f604c2a2 527#define LAZY_PLT_ENTRY_SIZE 16
252b5132 528
f604c2a2
L
529/* The size in bytes of an entry in the non-lazy procedure linkage
530 table. */
252b5132 531
f604c2a2
L
532#define NON_LAZY_PLT_ENTRY_SIZE 8
533
534/* The first entry in an absolute lazy procedure linkage table looks
535 like this. See the SVR4 ABI i386 supplement to see how this works.
536 Will be padded to LAZY_PLT_ENTRY_SIZE with lazy_plt->plt0_pad_byte. */
537
538static const bfd_byte elf_i386_lazy_plt0_entry[12] =
252b5132
RH
539{
540 0xff, 0x35, /* pushl contents of address */
541 0, 0, 0, 0, /* replaced with address of .got + 4. */
542 0xff, 0x25, /* jmp indirect */
eac338cf 543 0, 0, 0, 0 /* replaced with address of .got + 8. */
252b5132
RH
544};
545
f604c2a2
L
546/* Subsequent entries in an absolute lazy procedure linkage table look
547 like this. */
252b5132 548
f604c2a2 549static const bfd_byte elf_i386_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
252b5132
RH
550{
551 0xff, 0x25, /* jmp indirect */
552 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
553 0x68, /* pushl immediate */
554 0, 0, 0, 0, /* replaced with offset into relocation table. */
555 0xe9, /* jmp relative */
556 0, 0, 0, 0 /* replaced with offset to start of .plt. */
557};
558
f604c2a2
L
559/* The first entry in a PIC lazy procedure linkage table look like
560 this. Will be padded to LAZY_PLT_ENTRY_SIZE with
561 lazy_plt->plt0_pad_byte. */
252b5132 562
f604c2a2 563static const bfd_byte elf_i386_pic_lazy_plt0_entry[12] =
252b5132
RH
564{
565 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
eac338cf 566 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */
252b5132
RH
567};
568
f604c2a2
L
569/* Subsequent entries in a PIC lazy procedure linkage table look like
570 this. */
252b5132 571
f604c2a2 572static const bfd_byte elf_i386_pic_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
252b5132
RH
573{
574 0xff, 0xa3, /* jmp *offset(%ebx) */
575 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
576 0x68, /* pushl immediate */
577 0, 0, 0, 0, /* replaced with offset into relocation table. */
578 0xe9, /* jmp relative */
579 0, 0, 0, 0 /* replaced with offset to start of .plt. */
580};
581
f604c2a2 582/* Entries in the non-lazy procedure linkage table look like this. */
dd7e64d4 583
f604c2a2 584static const bfd_byte elf_i386_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
dd7e64d4
L
585{
586 0xff, 0x25, /* jmp indirect */
587 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
588 0x66, 0x90 /* xchg %ax,%ax */
589};
590
f604c2a2
L
591/* Entries in the PIC non-lazy procedure linkage table look like
592 this. */
dd7e64d4 593
f604c2a2 594static const bfd_byte elf_i386_pic_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
dd7e64d4
L
595{
596 0xff, 0xa3, /* jmp *offset(%ebx) */
597 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
598 0x66, 0x90 /* xchg %ax,%ax */
599};
600
ee2fdd6f
L
601/* The first entry in an absolute IBT-enabled lazy procedure linkage
602 table looks like this. */
603
604static const bfd_byte elf_i386_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
605{
606 0xff, 0x35, 0, 0, 0, 0, /* pushl GOT[1] */
607 0xff, 0x25, 0, 0, 0, 0, /* jmp *GOT[2] */
608 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
609};
610
611/* Subsequent entries for an absolute IBT-enabled lazy procedure linkage
612 table look like this. Subsequent entries for a PIC IBT-enabled lazy
613 procedure linkage table are the same. */
614
615static const bfd_byte elf_i386_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
616{
617 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */
618 0x68, 0, 0, 0, 0, /* pushl immediate */
619 0xe9, 0, 0, 0, 0, /* jmp relative */
620 0x66, 0x90 /* xchg %ax,%ax */
621};
622
623/* The first entry in a PIC IBT-enabled lazy procedure linkage table
624 look like. */
625
626static const bfd_byte elf_i386_pic_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
627{
628 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
629 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */
630 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
631};
632
633/* Entries for branches with IBT-enabled in the absolute non-lazey
634 procedure linkage table look like this. They have the same size
635 as the lazy PLT entry. */
636
637static const bfd_byte elf_i386_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
638{
639 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */
640 0xff, 0x25, 0, 0, 0, 0, /* jmp *name@GOT */
641 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
642};
643
644/* Entries for branches with IBT-enabled in the PIC non-lazey procedure
645 linkage table look like this. They have the same size as the lazy
646 PLT entry. */
647
648static const bfd_byte elf_i386_pic_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
649{
650 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */
651 0xff, 0xa3, 0, 0, 0, 0, /* jmp *name@GOT(%ebx) */
652 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
653};
654
f604c2a2 655/* .eh_frame covering the lazy .plt section. */
e41b3a13 656
f604c2a2 657static const bfd_byte elf_i386_eh_frame_lazy_plt[] =
e41b3a13
JJ
658{
659#define PLT_CIE_LENGTH 20
660#define PLT_FDE_LENGTH 36
661#define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
662#define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
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
0afcef53 856/* Values in tls_type of x86 ELF linker hash entry. */
37e55690
JJ
857#define GOT_TLS_IE 4
858#define GOT_TLS_IE_POS 5
859#define GOT_TLS_IE_NEG 6
860#define GOT_TLS_IE_BOTH 7
67a4f2b7
AO
861#define GOT_TLS_GDESC 8
862#define GOT_TLS_GD_BOTH_P(type) \
863 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
864#define GOT_TLS_GD_P(type) \
865 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
866#define GOT_TLS_GDESC_P(type) \
867 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
868#define GOT_TLS_GD_ANY_P(type) \
869 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
67a4f2b7 870
0ffa91dd
NC
871#define is_i386_elf(bfd) \
872 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
873 && elf_tdata (bfd) != NULL \
4dfe6ac6 874 && elf_object_id (bfd) == I386_ELF_DATA)
0ffa91dd 875
b34976b6 876static bfd_boolean
55fd94b0 877elf_i386_mkobject (bfd *abfd)
13ae64f3 878{
0afcef53
L
879 return bfd_elf_allocate_object (abfd,
880 sizeof (struct elf_x86_obj_tdata),
4dfe6ac6 881 I386_ELF_DATA);
13ae64f3 882}
cedb70c5 883
67a4f2b7 884#define elf_i386_compute_jump_table_size(htab) \
998d811a 885 ((htab)->elf.srelplt->reloc_count * 4)
67a4f2b7 886
142411ca
L
887/* Return TRUE if the TLS access code sequence support transition
888 from R_TYPE. */
889
890static bfd_boolean
bedfd056 891elf_i386_check_tls_transition (asection *sec,
142411ca
L
892 bfd_byte *contents,
893 Elf_Internal_Shdr *symtab_hdr,
894 struct elf_link_hash_entry **sym_hashes,
895 unsigned int r_type,
896 const Elf_Internal_Rela *rel,
897 const Elf_Internal_Rela *relend)
13ae64f3 898{
6eaa7fb5 899 unsigned int val, type, reg;
142411ca
L
900 unsigned long r_symndx;
901 struct elf_link_hash_entry *h;
902 bfd_vma offset;
6eaa7fb5 903 bfd_byte *call;
1d4af308 904 bfd_boolean indirect_call;
142411ca 905
142411ca 906 offset = rel->r_offset;
13ae64f3 907 switch (r_type)
142411ca
L
908 {
909 case R_386_TLS_GD:
910 case R_386_TLS_LDM:
911 if (offset < 2 || (rel + 1) >= relend)
912 return FALSE;
913
6eaa7fb5
L
914 indirect_call = FALSE;
915 call = contents + offset + 4;
916 val = *(call - 5);
917 type = *(call - 6);
142411ca
L
918 if (r_type == R_386_TLS_GD)
919 {
09e8c3bf 920 /* Check transition from GD access model. Only
6eaa7fb5
L
921 leal foo@tlsgd(,%ebx,1), %eax
922 call ___tls_get_addr@PLT
923 or
924 leal foo@tlsgd(%ebx) %eax
925 call ___tls_get_addr@PLT
926 nop
927 or
928 leal foo@tlsgd(%reg), %eax
929 call *___tls_get_addr@GOT(%reg)
930 which may be converted to
931 addr32 call ___tls_get_addr
142411ca 932 can transit to different access model. */
6eaa7fb5
L
933 if ((offset + 10) > sec->size
934 || (type != 0x8d && type != 0x04))
142411ca
L
935 return FALSE;
936
142411ca
L
937 if (type == 0x04)
938 {
6eaa7fb5
L
939 /* leal foo@tlsgd(,%ebx,1), %eax
940 call ___tls_get_addr@PLT */
142411ca
L
941 if (offset < 3)
942 return FALSE;
943
6eaa7fb5
L
944 if (*(call - 7) != 0x8d
945 || val != 0x1d
946 || call[0] != 0xe8)
142411ca
L
947 return FALSE;
948 }
949 else
950 {
6eaa7fb5
L
951 /* This must be
952 leal foo@tlsgd(%ebx), %eax
953 call ___tls_get_addr@PLT
954 nop
955 or
956 leal foo@tlsgd(%reg), %eax
957 call *___tls_get_addr@GOT(%reg)
958 which may be converted to
959 addr32 call ___tls_get_addr
960
961 %eax can't be used as the GOT base register since it
962 is used to pass parameter to ___tls_get_addr. */
963 reg = val & 7;
964 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
142411ca
L
965 return FALSE;
966
6eaa7fb5
L
967 indirect_call = call[0] == 0xff;
968 if (!(reg == 3 && call[0] == 0xe8 && call[5] == 0x90)
969 && !(call[0] == 0x67 && call[1] == 0xe8)
970 && !(indirect_call
971 && (call[1] & 0xf8) == 0x90
972 && (call[1] & 0x7) == reg))
142411ca
L
973 return FALSE;
974 }
975 }
976 else
977 {
978 /* Check transition from LD access model. Only
6eaa7fb5
L
979 leal foo@tlsldm(%ebx), %eax
980 call ___tls_get_addr@PLT
981 or
982 leal foo@tlsldm(%reg), %eax
983 call *___tls_get_addr@GOT(%reg)
984 which may be converted to
985 addr32 call ___tls_get_addr
142411ca
L
986 can transit to different access model. */
987 if (type != 0x8d || (offset + 9) > sec->size)
988 return FALSE;
989
6eaa7fb5
L
990 /* %eax can't be used as the GOT base register since it is
991 used to pass parameter to ___tls_get_addr. */
992 reg = val & 7;
993 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
142411ca 994 return FALSE;
142411ca 995
6eaa7fb5
L
996 indirect_call = call[0] == 0xff;
997 if (!(reg == 3 && call[0] == 0xe8)
998 && !(call[0] == 0x67 && call[1] == 0xe8)
999 && !(indirect_call
1000 && (call[1] & 0xf8) == 0x90
1001 && (call[1] & 0x7) == reg))
1002 return FALSE;
1003 }
142411ca
L
1004
1005 r_symndx = ELF32_R_SYM (rel[1].r_info);
1006 if (r_symndx < symtab_hdr->sh_info)
1007 return FALSE;
1008
1009 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1d4af308 1010 if (h == NULL
0afcef53 1011 || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
6eaa7fb5
L
1012 return FALSE;
1013 else if (indirect_call)
1014 return (ELF32_R_TYPE (rel[1].r_info) == R_386_GOT32X);
1015 else
1016 return (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
1017 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
142411ca
L
1018
1019 case R_386_TLS_IE:
1020 /* Check transition from IE access model:
1021 movl foo@indntpoff(%rip), %eax
1022 movl foo@indntpoff(%rip), %reg
1023 addl foo@indntpoff(%rip), %reg
1024 */
1025
1026 if (offset < 1 || (offset + 4) > sec->size)
1027 return FALSE;
1028
1029 /* Check "movl foo@tpoff(%rip), %eax" first. */
1030 val = bfd_get_8 (abfd, contents + offset - 1);
1031 if (val == 0xa1)
1032 return TRUE;
1033
1034 if (offset < 2)
1035 return FALSE;
1036
1037 /* Check movl|addl foo@tpoff(%rip), %reg. */
1038 type = bfd_get_8 (abfd, contents + offset - 2);
1039 return ((type == 0x8b || type == 0x03)
1040 && (val & 0xc7) == 0x05);
1041
1042 case R_386_TLS_GOTIE:
1043 case R_386_TLS_IE_32:
1044 /* Check transition from {IE_32,GOTIE} access model:
1045 subl foo@{tpoff,gontoff}(%reg1), %reg2
1046 movl foo@{tpoff,gontoff}(%reg1), %reg2
1047 addl foo@{tpoff,gontoff}(%reg1), %reg2
1048 */
1049
1050 if (offset < 2 || (offset + 4) > sec->size)
1051 return FALSE;
1052
1053 val = bfd_get_8 (abfd, contents + offset - 1);
1054 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1055 return FALSE;
1056
1057 type = bfd_get_8 (abfd, contents + offset - 2);
1058 return type == 0x8b || type == 0x2b || type == 0x03;
1059
1060 case R_386_TLS_GOTDESC:
1061 /* Check transition from GDesc access model:
1062 leal x@tlsdesc(%ebx), %eax
1063
1064 Make sure it's a leal adding ebx to a 32-bit offset
1065 into any register, although it's probably almost always
1066 going to be eax. */
1067
1068 if (offset < 2 || (offset + 4) > sec->size)
1069 return FALSE;
1070
1071 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1072 return FALSE;
1073
1074 val = bfd_get_8 (abfd, contents + offset - 1);
1075 return (val & 0xc7) == 0x83;
1076
1077 case R_386_TLS_DESC_CALL:
1078 /* Check transition from GDesc access model:
6eaa7fb5 1079 call *x@tlsdesc(%eax)
142411ca
L
1080 */
1081 if (offset + 2 <= sec->size)
1082 {
6eaa7fb5
L
1083 /* Make sure that it's a call *x@tlsdesc(%eax). */
1084 call = contents + offset;
1085 return call[0] == 0xff && call[1] == 0x10;
142411ca
L
1086 }
1087
1088 return FALSE;
1089
1090 default:
1091 abort ();
1092 }
1093}
1094
1095/* Return TRUE if the TLS access transition is OK or no transition
1096 will be performed. Update R_TYPE if there is a transition. */
1097
1098static bfd_boolean
1099elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1100 asection *sec, bfd_byte *contents,
1101 Elf_Internal_Shdr *symtab_hdr,
1102 struct elf_link_hash_entry **sym_hashes,
1103 unsigned int *r_type, int tls_type,
1104 const Elf_Internal_Rela *rel,
1105 const Elf_Internal_Rela *relend,
4c544807 1106 struct elf_link_hash_entry *h,
bedfd056
L
1107 unsigned long r_symndx,
1108 bfd_boolean from_relocate_section)
142411ca
L
1109{
1110 unsigned int from_type = *r_type;
1111 unsigned int to_type = from_type;
1112 bfd_boolean check = TRUE;
1113
bb1cb422
L
1114 /* Skip TLS transition for functions. */
1115 if (h != NULL
1116 && (h->type == STT_FUNC
1117 || h->type == STT_GNU_IFUNC))
1118 return TRUE;
1119
142411ca 1120 switch (from_type)
13ae64f3
JJ
1121 {
1122 case R_386_TLS_GD:
67a4f2b7
AO
1123 case R_386_TLS_GOTDESC:
1124 case R_386_TLS_DESC_CALL:
13ae64f3 1125 case R_386_TLS_IE_32:
37e55690
JJ
1126 case R_386_TLS_IE:
1127 case R_386_TLS_GOTIE:
0e1862bb 1128 if (bfd_link_executable (info))
142411ca
L
1129 {
1130 if (h == NULL)
1131 to_type = R_386_TLS_LE_32;
1132 else if (from_type != R_386_TLS_IE
1133 && from_type != R_386_TLS_GOTIE)
1134 to_type = R_386_TLS_IE_32;
1135 }
1136
bedfd056
L
1137 /* When we are called from elf_i386_relocate_section, there may
1138 be additional transitions based on TLS_TYPE. */
1139 if (from_relocate_section)
142411ca
L
1140 {
1141 unsigned int new_to_type = to_type;
1142
0e1862bb 1143 if (bfd_link_executable (info)
142411ca
L
1144 && h != NULL
1145 && h->dynindx == -1
1146 && (tls_type & GOT_TLS_IE))
1147 new_to_type = R_386_TLS_LE_32;
1148
1149 if (to_type == R_386_TLS_GD
1150 || to_type == R_386_TLS_GOTDESC
1151 || to_type == R_386_TLS_DESC_CALL)
1152 {
1153 if (tls_type == GOT_TLS_IE_POS)
1154 new_to_type = R_386_TLS_GOTIE;
1155 else if (tls_type & GOT_TLS_IE)
1156 new_to_type = R_386_TLS_IE_32;
1157 }
1158
1159 /* We checked the transition before when we were called from
1160 elf_i386_check_relocs. We only want to check the new
1161 transition which hasn't been checked before. */
1162 check = new_to_type != to_type && from_type == to_type;
1163 to_type = new_to_type;
1164 }
1165
1166 break;
1167
13ae64f3 1168 case R_386_TLS_LDM:
0e1862bb 1169 if (bfd_link_executable (info))
142411ca
L
1170 to_type = R_386_TLS_LE_32;
1171 break;
1172
1173 default:
1174 return TRUE;
1175 }
1176
1177 /* Return TRUE if there is no transition. */
1178 if (from_type == to_type)
1179 return TRUE;
1180
1181 /* Check if the transition can be performed. */
1182 if (check
bedfd056 1183 && ! elf_i386_check_tls_transition (sec, contents,
142411ca
L
1184 symtab_hdr, sym_hashes,
1185 from_type, rel, relend))
1186 {
2f629d23 1187 reloc_howto_type *from, *to;
4c544807 1188 const char *name;
142411ca
L
1189
1190 from = elf_i386_rtype_to_howto (abfd, from_type);
1191 to = elf_i386_rtype_to_howto (abfd, to_type);
1192
4c544807
L
1193 if (h)
1194 name = h->root.root.string;
1195 else
1196 {
0afcef53 1197 struct elf_x86_link_hash_table *htab;
4dfe6ac6 1198
0afcef53 1199 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6
NC
1200 if (htab == NULL)
1201 name = "*unknown*";
1202 else
1203 {
1204 Elf_Internal_Sym *isym;
1205
1206 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1207 abfd, r_symndx);
1208 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1209 }
4c544807
L
1210 }
1211
4eca0228 1212 _bfd_error_handler
695344c0 1213 /* xgettext:c-format */
d42c267e 1214 (_("%B: TLS transition from %s to %s against `%s' at %#Lx "
142411ca 1215 "in section `%A' failed"),
c08bb8dd 1216 abfd, from->name, to->name, name,
d42c267e 1217 rel->r_offset, sec);
142411ca
L
1218 bfd_set_error (bfd_error_bad_value);
1219 return FALSE;
13ae64f3
JJ
1220 }
1221
142411ca
L
1222 *r_type = to_type;
1223 return TRUE;
13ae64f3
JJ
1224}
1225
c175a657
L
1226/* With the local symbol, foo, we convert
1227 mov foo@GOT[(%reg1)], %reg2
1228 to
1229 lea foo[@GOTOFF(%reg1)], %reg2
1230 and convert
1231 call/jmp *foo@GOT[(%reg)]
1232 to
1233 nop call foo/jmp foo nop
1234 When PIC is false, convert
1235 test %reg1, foo@GOT[(%reg2)]
1236 to
1237 test $foo, %reg1
1238 and convert
1239 binop foo@GOT[(%reg1)], %reg2
1240 to
1241 binop $foo, %reg2
1242 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1243 instructions. */
1244
1245static
1246bfd_boolean
1247elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
1248 bfd_byte *contents,
1249 Elf_Internal_Rela *irel,
1250 struct elf_link_hash_entry *h,
1251 bfd_boolean *converted,
1252 struct bfd_link_info *link_info)
1253{
0afcef53 1254 struct elf_x86_link_hash_table *htab;
c175a657
L
1255 unsigned int opcode;
1256 unsigned int modrm;
1257 bfd_boolean baseless;
1258 Elf_Internal_Sym *isym;
1259 unsigned int addend;
1260 unsigned int nop;
1261 bfd_vma nop_offset;
1262 bfd_boolean is_pic;
1263 bfd_boolean to_reloc_32;
1264 unsigned int r_type;
1265 unsigned int r_symndx;
1266 bfd_vma roff = irel->r_offset;
1267
1268 if (roff < 2)
1269 return TRUE;
1270
7d4d9709 1271 /* Addend for R_386_GOT32X relocations must be 0. */
c175a657
L
1272 addend = bfd_get_32 (abfd, contents + roff);
1273 if (addend != 0)
1274 return TRUE;
1275
0afcef53 1276 htab = elf_x86_hash_table (link_info, I386_ELF_DATA);
c175a657
L
1277 is_pic = bfd_link_pic (link_info);
1278
1279 r_type = ELF32_R_TYPE (irel->r_info);
1280 r_symndx = ELF32_R_SYM (irel->r_info);
1281
1282 modrm = bfd_get_8 (abfd, contents + roff - 1);
1283 baseless = (modrm & 0xc7) == 0x5;
1284
7d4d9709 1285 if (baseless && is_pic)
c175a657
L
1286 {
1287 /* For PIC, disallow R_386_GOT32X without a base register
7d4d9709 1288 since we don't know what the GOT base is. */
c175a657
L
1289 const char *name;
1290
1291 if (h == NULL)
1292 {
1293 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd,
1294 r_symndx);
1295 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1296 }
1297 else
1298 name = h->root.root.string;
1299
4eca0228 1300 _bfd_error_handler
695344c0 1301 /* xgettext:c-format */
63a5468a
AM
1302 (_("%B: direct GOT relocation R_386_GOT32X against `%s' without base"
1303 " register can not be used when making a shared object"),
c175a657
L
1304 abfd, name);
1305 return FALSE;
1306 }
1307
1308 opcode = bfd_get_8 (abfd, contents + roff - 2);
1309
c175a657
L
1310 /* Convert to R_386_32 if PIC is false or there is no base
1311 register. */
1312 to_reloc_32 = !is_pic || baseless;
1313
7d4d9709
L
1314 /* Try to convert R_386_GOT32X. Get the symbol referred to by the
1315 reloc. */
c175a657
L
1316 if (h == NULL)
1317 {
1318 if (opcode == 0x0ff)
1319 /* Convert "call/jmp *foo@GOT[(%reg)]". */
1320 goto convert_branch;
1321 else
1322 /* Convert "mov foo@GOT[(%reg1)], %reg2",
1323 "test %reg1, foo@GOT(%reg2)" and
1324 "binop foo@GOT[(%reg1)], %reg2". */
1325 goto convert_load;
1326 }
1327
1328 /* Undefined weak symbol is only bound locally in executable
1329 and its reference is resolved as 0. */
0afcef53
L
1330 if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (link_info, I386_ELF_DATA, TRUE,
1331 elf_x86_hash_entry (h)))
c175a657
L
1332 {
1333 if (opcode == 0xff)
1334 {
1335 /* No direct branch to 0 for PIC. */
1336 if (is_pic)
1337 return TRUE;
1338 else
1339 goto convert_branch;
1340 }
1341 else
1342 {
1343 /* We can convert load of address 0 to R_386_32. */
1344 to_reloc_32 = TRUE;
1345 goto convert_load;
1346 }
1347 }
1348
1349 if (opcode == 0xff)
1350 {
1351 /* We have "call/jmp *foo@GOT[(%reg)]". */
1352 if ((h->root.type == bfd_link_hash_defined
1353 || h->root.type == bfd_link_hash_defweak)
1354 && SYMBOL_REFERENCES_LOCAL (link_info, h))
1355 {
1356 /* The function is locally defined. */
1357convert_branch:
1358 /* Convert R_386_GOT32X to R_386_PC32. */
1359 if (modrm == 0x15 || (modrm & 0xf8) == 0x90)
1360 {
0afcef53
L
1361 struct elf_x86_link_hash_entry *eh
1362 = (struct elf_x86_link_hash_entry *) h;
6eaa7fb5 1363
c175a657
L
1364 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1365 is a nop prefix. */
1366 modrm = 0xe8;
6eaa7fb5
L
1367 /* To support TLS optimization, always use addr32 prefix
1368 for "call *___tls_get_addr@GOT(%reg)". */
1d4af308 1369 if (eh && eh->tls_get_addr)
c175a657 1370 {
6eaa7fb5
L
1371 nop = 0x67;
1372 nop_offset = irel->r_offset - 2;
c175a657
L
1373 }
1374 else
6eaa7fb5
L
1375 {
1376 nop = link_info->call_nop_byte;
1377 if (link_info->call_nop_as_suffix)
1378 {
1379 nop_offset = roff + 3;
1380 irel->r_offset -= 1;
1381 }
1382 else
1383 nop_offset = roff - 2;
1384 }
c175a657
L
1385 }
1386 else
1387 {
1388 /* Convert to "jmp foo nop". */
1389 modrm = 0xe9;
1390 nop = NOP_OPCODE;
1391 nop_offset = roff + 3;
1392 irel->r_offset -= 1;
1393 }
1394
1395 bfd_put_8 (abfd, nop, contents + nop_offset);
1396 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1397 /* When converting to PC-relative relocation, we
1398 need to adjust addend by -4. */
1399 bfd_put_32 (abfd, -4, contents + irel->r_offset);
1400 irel->r_info = ELF32_R_INFO (r_symndx, R_386_PC32);
1401
1402 *converted = TRUE;
1403 }
1404 }
1405 else
1406 {
1407 /* We have "mov foo@GOT[(%re1g)], %reg2",
1408 "test %reg1, foo@GOT(%reg2)" and
1409 "binop foo@GOT[(%reg1)], %reg2".
1410
1411 Avoid optimizing _DYNAMIC since ld.so may use its
1412 link-time address. */
1413 if (h == htab->elf.hdynamic)
1414 return TRUE;
1415
1416 /* def_regular is set by an assignment in a linker script in
cbd0eecf
L
1417 bfd_elf_record_link_assignment. start_stop is set on
1418 __start_SECNAME/__stop_SECNAME which mark section SECNAME. */
1419 if (h->start_stop
1420 || ((h->def_regular
1421 || h->root.type == bfd_link_hash_defined
1422 || h->root.type == bfd_link_hash_defweak)
1423 && SYMBOL_REFERENCES_LOCAL (link_info, h)))
c175a657
L
1424 {
1425convert_load:
1426 if (opcode == 0x8b)
1427 {
1428 if (to_reloc_32)
1429 {
1430 /* Convert "mov foo@GOT[(%reg1)], %reg2" to
1431 "mov $foo, %reg2" with R_386_32. */
1432 r_type = R_386_32;
1433 modrm = 0xc0 | (modrm & 0x38) >> 3;
1434 bfd_put_8 (abfd, modrm, contents + roff - 1);
1435 opcode = 0xc7;
1436 }
1437 else
1438 {
1439 /* Convert "mov foo@GOT(%reg1), %reg2" to
1440 "lea foo@GOTOFF(%reg1), %reg2". */
1441 r_type = R_386_GOTOFF;
1442 opcode = 0x8d;
1443 }
1444 }
1445 else
1446 {
1447 /* Only R_386_32 is supported. */
1448 if (!to_reloc_32)
1449 return TRUE;
1450
1451 if (opcode == 0x85)
1452 {
1453 /* Convert "test %reg1, foo@GOT(%reg2)" to
1454 "test $foo, %reg1". */
1455 modrm = 0xc0 | (modrm & 0x38) >> 3;
1456 opcode = 0xf7;
1457 }
1458 else
1459 {
1460 /* Convert "binop foo@GOT(%reg1), %reg2" to
1461 "binop $foo, %reg2". */
1462 modrm = (0xc0
1463 | (modrm & 0x38) >> 3
1464 | (opcode & 0x3c));
1465 opcode = 0x81;
1466 }
1467 bfd_put_8 (abfd, modrm, contents + roff - 1);
1468 r_type = R_386_32;
1469 }
1470
1471 bfd_put_8 (abfd, opcode, contents + roff - 2);
1472 irel->r_info = ELF32_R_INFO (r_symndx, r_type);
1473
1474 *converted = TRUE;
1475 }
1476 }
1477
1478 return TRUE;
1479}
1480
c1d11331
L
1481/* Rename some of the generic section flags to better document how they
1482 are used here. */
c337a162
L
1483#define need_convert_load sec_flg0
1484#define check_relocs_failed sec_flg1
c1d11331 1485
252b5132 1486/* Look through the relocs for a section during the first phase, and
0ac8d2ca
AM
1487 calculate needed space in the global offset table, procedure linkage
1488 table, and dynamic reloc sections. */
252b5132 1489
b34976b6 1490static bfd_boolean
55fd94b0
AM
1491elf_i386_check_relocs (bfd *abfd,
1492 struct bfd_link_info *info,
1493 asection *sec,
1494 const Elf_Internal_Rela *relocs)
252b5132 1495{
0afcef53 1496 struct elf_x86_link_hash_table *htab;
252b5132
RH
1497 Elf_Internal_Shdr *symtab_hdr;
1498 struct elf_link_hash_entry **sym_hashes;
252b5132
RH
1499 const Elf_Internal_Rela *rel;
1500 const Elf_Internal_Rela *rel_end;
252b5132 1501 asection *sreloc;
bedfd056 1502 bfd_byte *contents;
252b5132 1503
0e1862bb 1504 if (bfd_link_relocatable (info))
b34976b6 1505 return TRUE;
252b5132 1506
081b1afe
L
1507 /* Don't do anything special with non-loaded, non-alloced sections.
1508 In particular, any relocs in such sections should not affect GOT
1509 and PLT reference counting (ie. we don't allow them to create GOT
1510 or PLT entries), there's no possibility or desire to optimize TLS
1511 relocs, and there's not much point in propagating relocs to shared
1512 libs that the dynamic linker won't relocate. */
1513 if ((sec->flags & SEC_ALLOC) == 0)
1514 return TRUE;
1515
0ffa91dd
NC
1516 BFD_ASSERT (is_i386_elf (abfd));
1517
0afcef53 1518 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6 1519 if (htab == NULL)
c337a162
L
1520 {
1521 sec->check_relocs_failed = 1;
1522 return FALSE;
1523 }
4dfe6ac6 1524
bedfd056
L
1525 /* Get the section contents. */
1526 if (elf_section_data (sec)->this_hdr.contents != NULL)
1527 contents = elf_section_data (sec)->this_hdr.contents;
1528 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1529 {
1530 sec->check_relocs_failed = 1;
1531 return FALSE;
1532 }
1533
0ffa91dd 1534 symtab_hdr = &elf_symtab_hdr (abfd);
252b5132 1535 sym_hashes = elf_sym_hashes (abfd);
252b5132 1536
252b5132
RH
1537 sreloc = NULL;
1538
1539 rel_end = relocs + sec->reloc_count;
1540 for (rel = relocs; rel < rel_end; rel++)
1541 {
13ae64f3 1542 unsigned int r_type;
d42c267e 1543 unsigned int r_symndx;
252b5132 1544 struct elf_link_hash_entry *h;
0afcef53 1545 struct elf_x86_link_hash_entry *eh;
4c544807
L
1546 Elf_Internal_Sym *isym;
1547 const char *name;
06a6a421 1548 bfd_boolean size_reloc;
252b5132
RH
1549
1550 r_symndx = ELF32_R_SYM (rel->r_info);
13ae64f3 1551 r_type = ELF32_R_TYPE (rel->r_info);
252b5132 1552
d9bc7a44 1553 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
f5f31454 1554 {
695344c0 1555 /* xgettext:c-format */
4eca0228
AM
1556 _bfd_error_handler (_("%B: bad symbol index: %d"),
1557 abfd, r_symndx);
c337a162 1558 goto error_return;
f5f31454
L
1559 }
1560
252b5132 1561 if (r_symndx < symtab_hdr->sh_info)
c25bc9fc
L
1562 {
1563 /* A local symbol. */
c2e61a4e
L
1564 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1565 abfd, r_symndx);
1566 if (isym == NULL)
c337a162 1567 goto error_return;
c25bc9fc
L
1568
1569 /* Check relocation against local STT_GNU_IFUNC symbol. */
c25bc9fc
L
1570 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1571 {
0afcef53 1572 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel, TRUE);
c25bc9fc 1573 if (h == NULL)
c337a162 1574 goto error_return;
6bbec505 1575
c25bc9fc 1576 /* Fake a STT_GNU_IFUNC symbol. */
6322e5c5
L
1577 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1578 isym, NULL);
c25bc9fc
L
1579 h->type = STT_GNU_IFUNC;
1580 h->def_regular = 1;
1581 h->ref_regular = 1;
1582 h->forced_local = 1;
1583 h->root.type = bfd_link_hash_defined;
1584 }
1585 else
1586 h = NULL;
1587 }
252b5132 1588 else
71cb9464 1589 {
4c544807 1590 isym = NULL;
71cb9464
L
1591 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1592 while (h->root.type == bfd_link_hash_indirect
1593 || h->root.type == bfd_link_hash_warning)
1594 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c25bc9fc 1595 }
cbe950e9 1596
0afcef53 1597 eh = (struct elf_x86_link_hash_entry *) h;
c25bc9fc
L
1598 if (h != NULL)
1599 {
f604c2a2
L
1600 if (r_type == R_386_GOTOFF)
1601 eh->gotoff_ref = 1;
cbe950e9 1602
ad1e85de
L
1603 /* It is referenced by a non-shared object. */
1604 h->ref_regular = 1;
bc4e12de 1605 h->root.non_ir_ref_regular = 1;
13a2df29
L
1606
1607 if (h->type == STT_GNU_IFUNC)
1608 elf_tdata (info->output_bfd)->has_gnu_symbols
1609 |= elf_gnu_symbol_ifunc;
71cb9464 1610 }
252b5132 1611
bedfd056 1612 if (! elf_i386_tls_transition (info, abfd, sec, contents,
142411ca
L
1613 symtab_hdr, sym_hashes,
1614 &r_type, GOT_UNKNOWN,
bedfd056 1615 rel, rel_end, h, r_symndx, FALSE))
c337a162 1616 goto error_return;
13ae64f3
JJ
1617
1618 switch (r_type)
252b5132 1619 {
37e55690 1620 case R_386_TLS_LDM:
0afcef53 1621 htab->tls_ld_or_ldm_got.refcount += 1;
37e55690
JJ
1622 goto create_got;
1623
1624 case R_386_PLT32:
1625 /* This symbol requires a procedure linkage table entry. We
1626 actually build the entry in adjust_dynamic_symbol,
1627 because this might be a case of linking PIC code which is
1628 never referenced by a dynamic object, in which case we
1629 don't need to generate a procedure linkage table entry
1630 after all. */
1631
1632 /* If this is a local symbol, we resolve it directly without
1633 creating a procedure linkage table entry. */
1634 if (h == NULL)
1635 continue;
1636
aec6b87e 1637 eh->has_got_reloc = 1;
f5385ebf 1638 h->needs_plt = 1;
37e55690
JJ
1639 h->plt.refcount += 1;
1640 break;
1641
6a3e1bae 1642 case R_386_SIZE32:
06a6a421 1643 size_reloc = TRUE;
6a3e1bae
L
1644 goto do_size;
1645
13ae64f3 1646 case R_386_TLS_IE_32:
37e55690
JJ
1647 case R_386_TLS_IE:
1648 case R_386_TLS_GOTIE:
0e1862bb 1649 if (!bfd_link_executable (info))
13ae64f3 1650 info->flags |= DF_STATIC_TLS;
37e55690
JJ
1651 /* Fall through */
1652
252b5132 1653 case R_386_GOT32:
02a86693 1654 case R_386_GOT32X:
13ae64f3 1655 case R_386_TLS_GD:
67a4f2b7
AO
1656 case R_386_TLS_GOTDESC:
1657 case R_386_TLS_DESC_CALL:
252b5132 1658 /* This symbol requires a global offset table entry. */
13ae64f3
JJ
1659 {
1660 int tls_type, old_tls_type;
1661
1662 switch (r_type)
1663 {
1664 default:
02a86693
L
1665 case R_386_GOT32:
1666 case R_386_GOT32X:
1667 tls_type = GOT_NORMAL;
1668 break;
13ae64f3 1669 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
67a4f2b7
AO
1670 case R_386_TLS_GOTDESC:
1671 case R_386_TLS_DESC_CALL:
1672 tls_type = GOT_TLS_GDESC; break;
37e55690
JJ
1673 case R_386_TLS_IE_32:
1674 if (ELF32_R_TYPE (rel->r_info) == r_type)
1675 tls_type = GOT_TLS_IE_NEG;
1676 else
ebcfb3c0
JJ
1677 /* If this is a GD->IE transition, we may use either of
1678 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
37e55690
JJ
1679 tls_type = GOT_TLS_IE;
1680 break;
1681 case R_386_TLS_IE:
1682 case R_386_TLS_GOTIE:
1683 tls_type = GOT_TLS_IE_POS; break;
13ae64f3
JJ
1684 }
1685
1686 if (h != NULL)
1687 {
1688 h->got.refcount += 1;
0afcef53 1689 old_tls_type = elf_x86_hash_entry (h)->tls_type;
13ae64f3
JJ
1690 }
1691 else
1692 {
1693 bfd_signed_vma *local_got_refcounts;
1694
1695 /* This is a global offset table entry for a local symbol. */
1696 local_got_refcounts = elf_local_got_refcounts (abfd);
1697 if (local_got_refcounts == NULL)
1698 {
1699 bfd_size_type size;
1700
1701 size = symtab_hdr->sh_info;
67a4f2b7
AO
1702 size *= (sizeof (bfd_signed_vma)
1703 + sizeof (bfd_vma) + sizeof(char));
a50b1753
NC
1704 local_got_refcounts = (bfd_signed_vma *)
1705 bfd_zalloc (abfd, size);
13ae64f3 1706 if (local_got_refcounts == NULL)
c337a162 1707 goto error_return;
13ae64f3 1708 elf_local_got_refcounts (abfd) = local_got_refcounts;
0afcef53 1709 elf_x86_local_tlsdesc_gotent (abfd)
67a4f2b7 1710 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
0afcef53 1711 elf_x86_local_got_tls_type (abfd)
67a4f2b7 1712 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
13ae64f3
JJ
1713 }
1714 local_got_refcounts[r_symndx] += 1;
0afcef53 1715 old_tls_type = elf_x86_local_got_tls_type (abfd) [r_symndx];
13ae64f3
JJ
1716 }
1717
37e55690
JJ
1718 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1719 tls_type |= old_tls_type;
13ae64f3
JJ
1720 /* If a TLS symbol is accessed using IE at least once,
1721 there is no point to use dynamic model for it. */
ebcfb3c0 1722 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
67a4f2b7 1723 && (! GOT_TLS_GD_ANY_P (old_tls_type)
37e55690 1724 || (tls_type & GOT_TLS_IE) == 0))
13ae64f3 1725 {
67a4f2b7 1726 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
37e55690 1727 tls_type = old_tls_type;
67a4f2b7
AO
1728 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1729 && GOT_TLS_GD_ANY_P (tls_type))
1730 tls_type |= old_tls_type;
13ae64f3
JJ
1731 else
1732 {
09a24cbf 1733 if (h)
4c544807
L
1734 name = h->root.root.string;
1735 else
1736 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1737 NULL);
4eca0228 1738 _bfd_error_handler
695344c0 1739 /* xgettext:c-format */
d003868e 1740 (_("%B: `%s' accessed both as normal and "
55fd94b0 1741 "thread local symbol"),
4c544807 1742 abfd, name);
68c4a57e 1743 bfd_set_error (bfd_error_bad_value);
c337a162 1744 goto error_return;
13ae64f3
JJ
1745 }
1746 }
1747
1748 if (old_tls_type != tls_type)
1749 {
1750 if (h != NULL)
0afcef53 1751 elf_x86_hash_entry (h)->tls_type = tls_type;
13ae64f3 1752 else
0afcef53 1753 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
13ae64f3
JJ
1754 }
1755 }
0ac8d2ca
AM
1756 /* Fall through */
1757
1758 case R_386_GOTOFF:
1759 case R_386_GOTPC:
13ae64f3 1760 create_got:
37e55690 1761 if (r_type != R_386_TLS_IE)
aec6b87e
L
1762 {
1763 if (eh != NULL)
1764 eh->has_got_reloc = 1;
1765 break;
1766 }
37e55690 1767 /* Fall through */
252b5132 1768
37e55690
JJ
1769 case R_386_TLS_LE_32:
1770 case R_386_TLS_LE:
aec6b87e
L
1771 if (eh != NULL)
1772 eh->has_got_reloc = 1;
0e1862bb 1773 if (bfd_link_executable (info))
37e55690 1774 break;
bffbf940 1775 info->flags |= DF_STATIC_TLS;
aec6b87e 1776 goto do_relocation;
252b5132
RH
1777
1778 case R_386_32:
1779 case R_386_PC32:
aec6b87e
L
1780 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
1781 eh->has_non_got_reloc = 1;
1782do_relocation:
bf52d7c7
L
1783 /* We are called after all symbols have been resolved. Only
1784 relocation against STT_GNU_IFUNC symbol must go through
1785 PLT. */
d1ed1c7d
L
1786 if (h != NULL
1787 && (bfd_link_executable (info)
bf52d7c7 1788 || h->type == STT_GNU_IFUNC))
6725bdbf 1789 {
12d0ee4a 1790 /* If this reloc is in a read-only section, we might
ebe50bae
AM
1791 need a copy reloc. We can't check reliably at this
1792 stage whether the section is read-only, as input
1793 sections have not yet been mapped to output sections.
1794 Tentatively set the flag for now, and correct in
1795 adjust_dynamic_symbol. */
f5385ebf 1796 h->non_got_ref = 1;
12d0ee4a 1797
233cc9c1
L
1798 /* We may need a .plt entry if the symbol is a function
1799 defined in a shared lib or is a STT_GNU_IFUNC function
1800 referenced from the code or read-only section. */
34bb2571
L
1801 if (!h->def_regular
1802 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
233cc9c1
L
1803 h->plt.refcount += 1;
1804
4b627c18
L
1805 if (r_type == R_386_PC32)
1806 {
1807 /* Since something like ".long foo - ." may be used
1808 as pointer, make sure that PLT is used if foo is
1809 a function defined in a shared library. */
1810 if ((sec->flags & SEC_CODE) == 0)
1811 h->pointer_equality_needed = 1;
74437ea2
L
1812 else if (h->type == STT_GNU_IFUNC
1813 && bfd_link_pic (info))
1814 {
6322e5c5
L
1815 _bfd_error_handler
1816 /* xgettext:c-format */
1817 (_("%B: unsupported non-PIC call to IFUNC `%s'"),
1818 abfd, h->root.root.string);
74437ea2
L
1819 bfd_set_error (bfd_error_bad_value);
1820 goto error_return;
1821 }
4b627c18
L
1822 }
1823 else
04ebc307
L
1824 {
1825 h->pointer_equality_needed = 1;
1826 /* R_386_32 can be resolved at run-time. */
1827 if (r_type == R_386_32
1828 && (sec->flags & SEC_READONLY) == 0)
1829 eh->func_pointer_refcount += 1;
1830 }
6725bdbf 1831 }
7843f00e 1832
06a6a421 1833 size_reloc = FALSE;
6a3e1bae 1834do_size:
252b5132 1835 /* If we are creating a shared library, and this is a reloc
f69da49f
AM
1836 against a global symbol, or a non PC relative reloc
1837 against a local symbol, then we need to copy the reloc
1838 into the shared library. However, if we are linking with
1839 -Bsymbolic, we do not need to copy a reloc against a
1840 global symbol which is defined in an object we are
1841 including in the link (i.e., DEF_REGULAR is set). At
1842 this point we have not seen all the input files, so it is
1843 possible that DEF_REGULAR is not set now but will be set
1f655a09
L
1844 later (it is never cleared). In case of a weak definition,
1845 DEF_REGULAR may be cleared later by a strong definition in
ebe50bae 1846 a shared library. We account for that possibility below by
1f655a09
L
1847 storing information in the relocs_copied field of the hash
1848 table entry. A similar situation occurs when creating
1849 shared libraries and symbol visibility changes render the
12d0ee4a 1850 symbol local.
56882138 1851
12d0ee4a
AM
1852 If on the other hand, we are creating an executable, we
1853 may need to keep relocations for symbols satisfied by a
1854 dynamic library if we manage to avoid copy relocs for the
233cc9c1
L
1855 symbol.
1856
1857 Generate dynamic pointer relocation against STT_GNU_IFUNC
1858 symbol in the non-code section. */
081b1afe
L
1859 if ((bfd_link_pic (info)
1860 && (r_type != R_386_PC32
1861 || (h != NULL
1862 && (! (bfd_link_pie (info)
1863 || SYMBOLIC_BIND (info, h))
1864 || h->root.type == bfd_link_hash_defweak
1865 || !h->def_regular))))
233cc9c1
L
1866 || (h != NULL
1867 && h->type == STT_GNU_IFUNC
1868 && r_type == R_386_32
1869 && (sec->flags & SEC_CODE) == 0)
081b1afe
L
1870 || (ELIMINATE_COPY_RELOCS
1871 && !bfd_link_pic (info)
1872 && h != NULL
1873 && (h->root.type == bfd_link_hash_defweak
1874 || !h->def_regular)))
252b5132 1875 {
e03a8ed8
L
1876 struct elf_dyn_relocs *p;
1877 struct elf_dyn_relocs **head;
ec338859 1878
12d0ee4a
AM
1879 /* We must copy these reloc types into the output file.
1880 Create a reloc section in dynobj and make room for
1881 this reloc. */
252b5132
RH
1882 if (sreloc == NULL)
1883 {
83bac4b0
NC
1884 sreloc = _bfd_elf_make_dynamic_reloc_section
1885 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
1886
252b5132 1887 if (sreloc == NULL)
c337a162 1888 goto error_return;
252b5132
RH
1889 }
1890
0c715baa
AM
1891 /* If this is a global symbol, we count the number of
1892 relocations we need for this symbol. */
1893 if (h != NULL)
252b5132 1894 {
d5597ebc 1895 head = &eh->dyn_relocs;
0c715baa
AM
1896 }
1897 else
1898 {
ec338859
AM
1899 /* Track dynamic relocs needed for local syms too.
1900 We really need local syms available to do this
1901 easily. Oh well. */
87d72d41 1902 void **vpp;
ec338859 1903 asection *s;
87d72d41
AM
1904
1905 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1906 abfd, r_symndx);
1907 if (isym == NULL)
c337a162 1908 goto error_return;
87d72d41
AM
1909
1910 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
ec338859 1911 if (s == NULL)
87d72d41 1912 s = sec;
ec338859 1913
e81d3500 1914 vpp = &elf_section_data (s)->local_dynrel;
e03a8ed8 1915 head = (struct elf_dyn_relocs **)vpp;
ec338859
AM
1916 }
1917
1918 p = *head;
1919 if (p == NULL || p->sec != sec)
1920 {
1921 bfd_size_type amt = sizeof *p;
a50b1753
NC
1922 p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
1923 amt);
ec338859 1924 if (p == NULL)
c337a162 1925 goto error_return;
ec338859
AM
1926 p->next = *head;
1927 *head = p;
1928 p->sec = sec;
1929 p->count = 0;
1930 p->pc_count = 0;
252b5132 1931 }
ec338859
AM
1932
1933 p->count += 1;
06a6a421
L
1934 /* Count size relocation as PC-relative relocation. */
1935 if (r_type == R_386_PC32 || size_reloc)
ec338859 1936 p->pc_count += 1;
252b5132 1937 }
252b5132
RH
1938 break;
1939
1940 /* This relocation describes the C++ object vtable hierarchy.
1941 Reconstruct it for later use during GC. */
1942 case R_386_GNU_VTINHERIT:
d17e0c6e 1943 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
c337a162 1944 goto error_return;
252b5132
RH
1945 break;
1946
1947 /* This relocation describes which C++ vtable entries are actually
1948 used. Record for later use during GC. */
1949 case R_386_GNU_VTENTRY:
c6aa130f
MS
1950 BFD_ASSERT (h != NULL);
1951 if (h != NULL
1952 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
c337a162 1953 goto error_return;
252b5132
RH
1954 break;
1955
1956 default:
1957 break;
1958 }
dd7e64d4 1959
52bf37dd 1960 if (r_type == R_386_GOT32X
c1d11331 1961 && (h == NULL || h->type != STT_GNU_IFUNC))
02a86693 1962 sec->need_convert_load = 1;
252b5132
RH
1963 }
1964
bedfd056
L
1965 if (elf_section_data (sec)->this_hdr.contents != contents)
1966 {
1967 if (!info->keep_memory)
1968 free (contents);
1969 else
1970 {
1971 /* Cache the section contents for elf_link_input_bfd. */
1972 elf_section_data (sec)->this_hdr.contents = contents;
1973 }
1974 }
1975
b34976b6 1976 return TRUE;
c337a162
L
1977
1978error_return:
bedfd056
L
1979 if (elf_section_data (sec)->this_hdr.contents != contents)
1980 free (contents);
c337a162
L
1981 sec->check_relocs_failed = 1;
1982 return FALSE;
252b5132
RH
1983}
1984
1985/* Return the section that should be marked against GC for a given
1986 relocation. */
1987
1988static asection *
55fd94b0 1989elf_i386_gc_mark_hook (asection *sec,
07adf181 1990 struct bfd_link_info *info,
55fd94b0
AM
1991 Elf_Internal_Rela *rel,
1992 struct elf_link_hash_entry *h,
1993 Elf_Internal_Sym *sym)
252b5132
RH
1994{
1995 if (h != NULL)
07adf181
AM
1996 switch (ELF32_R_TYPE (rel->r_info))
1997 {
1998 case R_386_GNU_VTINHERIT:
1999 case R_386_GNU_VTENTRY:
2000 return NULL;
2001 }
2002
2003 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
252b5132
RH
2004}
2005
252b5132
RH
2006/* Adjust a symbol defined by a dynamic object and referenced by a
2007 regular object. The current definition is in some section of the
2008 dynamic object, but we're not including those sections. We have to
2009 change the definition to something the rest of the link can
2010 understand. */
2011
b34976b6 2012static bfd_boolean
55fd94b0
AM
2013elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
2014 struct elf_link_hash_entry *h)
252b5132 2015{
0afcef53 2016 struct elf_x86_link_hash_table *htab;
5474d94f 2017 asection *s, *srel;
0afcef53 2018 struct elf_x86_link_hash_entry *eh;
5ca5bb35 2019 struct elf_dyn_relocs *p;
252b5132 2020
cbe950e9
L
2021 /* STT_GNU_IFUNC symbol must go through PLT. */
2022 if (h->type == STT_GNU_IFUNC)
2023 {
73bcf233
L
2024 /* All local STT_GNU_IFUNC references must be treate as local
2025 calls via local PLT. */
5ca5bb35
L
2026 if (h->ref_regular
2027 && SYMBOL_CALLS_LOCAL (info, h))
2028 {
73bcf233 2029 bfd_size_type pc_count = 0, count = 0;
5ca5bb35
L
2030 struct elf_dyn_relocs **pp;
2031
0afcef53 2032 eh = (struct elf_x86_link_hash_entry *) h;
5ca5bb35
L
2033 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2034 {
2035 pc_count += p->pc_count;
2036 p->count -= p->pc_count;
2037 p->pc_count = 0;
73bcf233 2038 count += p->count;
5ca5bb35
L
2039 if (p->count == 0)
2040 *pp = p->next;
2041 else
2042 pp = &p->next;
2043 }
2044
73bcf233 2045 if (pc_count || count)
5ca5bb35 2046 {
5ca5bb35 2047 h->non_got_ref = 1;
233cc9c1
L
2048 if (pc_count)
2049 {
2050 /* Increment PLT reference count only for PC-relative
2051 references. */
2052 h->needs_plt = 1;
2053 if (h->plt.refcount <= 0)
2054 h->plt.refcount = 1;
2055 else
2056 h->plt.refcount += 1;
2057 }
5ca5bb35
L
2058 }
2059 }
2060
cbe950e9
L
2061 if (h->plt.refcount <= 0)
2062 {
2063 h->plt.offset = (bfd_vma) -1;
2064 h->needs_plt = 0;
2065 }
2066 return TRUE;
2067 }
2068
252b5132
RH
2069 /* If this is a function, put it in the procedure linkage table. We
2070 will fill in the contents of the procedure linkage table later,
2071 when we know the address of the .got section. */
2072 if (h->type == STT_FUNC
f5385ebf 2073 || h->needs_plt)
252b5132 2074 {
6725bdbf 2075 if (h->plt.refcount <= 0
9c7a29a3
AM
2076 || SYMBOL_CALLS_LOCAL (info, h)
2077 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2078 && h->root.type == bfd_link_hash_undefweak))
252b5132
RH
2079 {
2080 /* This case can occur if we saw a PLT32 reloc in an input
dd5724d5
AM
2081 file, but the symbol was never referred to by a dynamic
2082 object, or if all references were garbage collected. In
2083 such a case, we don't actually need to build a procedure
2084 linkage table, and we can just do a PC32 reloc instead. */
bbd7ec4a 2085 h->plt.offset = (bfd_vma) -1;
f5385ebf 2086 h->needs_plt = 0;
252b5132
RH
2087 }
2088
b34976b6 2089 return TRUE;
252b5132 2090 }
6725bdbf
AM
2091 else
2092 /* It's possible that we incorrectly decided a .plt reloc was
2093 needed for an R_386_PC32 reloc to a non-function sym in
2094 check_relocs. We can't decide accurately between function and
2095 non-function syms in check-relocs; Objects loaded later in
2096 the link may change h->type. So fix it now. */
bbd7ec4a 2097 h->plt.offset = (bfd_vma) -1;
252b5132 2098
0afcef53 2099 eh = (struct elf_x86_link_hash_entry *) h;
a5b4ee94 2100
252b5132
RH
2101 /* If this is a weak symbol, and there is a real definition, the
2102 processor independent code will have arranged for us to see the
2103 real definition first, and we can just use the same value. */
f6e332e6 2104 if (h->u.weakdef != NULL)
252b5132 2105 {
f6e332e6
AM
2106 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2107 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2108 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2109 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a5b4ee94
L
2110 if (ELIMINATE_COPY_RELOCS
2111 || info->nocopyreloc
2112 || SYMBOL_NO_COPYRELOC (info, eh))
f6e332e6 2113 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 2114 return TRUE;
252b5132
RH
2115 }
2116
2117 /* This is a reference to a symbol defined by a dynamic object which
2118 is not a function. */
2119
2120 /* If we are creating a shared library, we must presume that the
2121 only references to the symbol are via the global offset table.
2122 For such cases we need not do anything here; the relocations will
2123 be handled correctly by relocate_section. */
0e1862bb 2124 if (!bfd_link_executable (info))
b34976b6 2125 return TRUE;
252b5132 2126
7843f00e 2127 /* If there are no references to this symbol that do not use the
d5597ebc
L
2128 GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
2129 reloc. */
d5597ebc 2130 if (!h->non_got_ref && !eh->gotoff_ref)
b34976b6 2131 return TRUE;
7843f00e 2132
8bd621d8 2133 /* If -z nocopyreloc was given, we won't generate them either. */
a5b4ee94 2134 if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
8bd621d8 2135 {
f5385ebf 2136 h->non_got_ref = 0;
b34976b6 2137 return TRUE;
8bd621d8
AM
2138 }
2139
0afcef53 2140 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6
NC
2141 if (htab == NULL)
2142 return FALSE;
643796e3 2143
d5597ebc
L
2144 /* If there aren't any dynamic relocs in read-only sections nor
2145 R_386_GOTOFF relocation, then we can keep the dynamic relocs and
2146 avoid the copy reloc. This doesn't work on VxWorks, where we can
2147 not have dynamic relocations (other than copy and jump slot
2148 relocations) in an executable. */
23209a78 2149 if (ELIMINATE_COPY_RELOCS
d5597ebc 2150 && !eh->gotoff_ref
f604c2a2 2151 && get_elf_i386_backend_data (info->output_bfd)->os != is_vxworks)
ebe50bae 2152 {
a23b6845
AM
2153 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2154 {
2155 s = p->sec->output_section;
2156 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2157 break;
2158 }
2159
a23b6845
AM
2160 if (p == NULL)
2161 {
f5385ebf 2162 h->non_got_ref = 0;
a23b6845
AM
2163 return TRUE;
2164 }
ebe50bae
AM
2165 }
2166
252b5132
RH
2167 /* We must allocate the symbol in our .dynbss section, which will
2168 become part of the .bss section of the executable. There will be
2169 an entry for this symbol in the .dynsym section. The dynamic
2170 object will contain position independent code, so all references
2171 from the dynamic object to this symbol will go through the global
2172 offset table. The dynamic linker will use the .dynsym entry to
2173 determine the address it must put in the global offset table, so
2174 both the dynamic object and the regular object will refer to the
2175 same memory location for the variable. */
2176
252b5132
RH
2177 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
2178 copy the initial value out of the dynamic object and into the
0ac8d2ca 2179 runtime process image. */
5474d94f
AM
2180 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2181 {
2182 s = htab->elf.sdynrelro;
2183 srel = htab->elf.sreldynrelro;
2184 }
2185 else
2186 {
2187 s = htab->elf.sdynbss;
2188 srel = htab->elf.srelbss;
2189 }
1d7e9d18 2190 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
252b5132 2191 {
5474d94f 2192 srel->size += sizeof (Elf32_External_Rel);
f5385ebf 2193 h->needs_copy = 1;
252b5132
RH
2194 }
2195
6cabe1ea 2196 return _bfd_elf_adjust_dynamic_copy (info, h, s);
252b5132
RH
2197}
2198
6725bdbf 2199/* Allocate space in .plt, .got and associated reloc sections for
0c715baa 2200 dynamic relocs. */
6725bdbf 2201
b34976b6 2202static bfd_boolean
eb4ff4d6 2203elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
6725bdbf
AM
2204{
2205 struct bfd_link_info *info;
0afcef53
L
2206 struct elf_x86_link_hash_table *htab;
2207 struct elf_x86_link_hash_entry *eh;
e03a8ed8 2208 struct elf_dyn_relocs *p;
25e762b9 2209 unsigned plt_entry_size;
aec6b87e 2210 bfd_boolean resolved_to_zero;
f604c2a2 2211 const struct elf_i386_backend_data *bed;
6725bdbf 2212
e92d460e 2213 if (h->root.type == bfd_link_hash_indirect)
b34976b6 2214 return TRUE;
6725bdbf 2215
0afcef53 2216 eh = (struct elf_x86_link_hash_entry *) h;
e92d460e 2217
6725bdbf 2218 info = (struct bfd_link_info *) inf;
0afcef53 2219 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6
NC
2220 if (htab == NULL)
2221 return FALSE;
6725bdbf 2222
f604c2a2
L
2223 bed = get_elf_i386_backend_data (info->output_bfd);
2224
765e526c 2225 plt_entry_size = htab->plt.plt_entry_size;
25e762b9 2226
e62b9723 2227 resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
0afcef53 2228 I386_ELF_DATA,
e62b9723
L
2229 eh->has_got_reloc,
2230 eh);
aec6b87e 2231
04ebc307
L
2232 /* Clear the reference count of function pointer relocations if
2233 symbol isn't a normal function. */
2234 if (h->type != STT_FUNC)
2235 eh->func_pointer_refcount = 0;
2236
dd7e64d4
L
2237 /* We can't use the GOT PLT if pointer equality is needed since
2238 finish_dynamic_symbol won't clear symbol value and the dynamic
2239 linker won't update the GOT slot. We will get into an infinite
2240 loop at run-time. */
2241 if (htab->plt_got != NULL
2242 && h->type != STT_GNU_IFUNC
2243 && !h->pointer_equality_needed
2244 && h->plt.refcount > 0
2245 && h->got.refcount > 0)
2246 {
2247 /* Don't use the regular PLT if there are both GOT and GOTPLT
2248 reloctions. */
2249 h->plt.offset = (bfd_vma) -1;
2250
2251 /* Use the GOT PLT. */
2252 eh->plt_got.refcount = 1;
2253 }
2254
cbe950e9
L
2255 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2256 here if it is defined and referenced in a non-shared object. */
2257 if (h->type == STT_GNU_IFUNC
2258 && h->def_regular)
ee2fdd6f
L
2259 {
2260 if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h, &eh->dyn_relocs,
2261 &htab->readonly_dynrelocs_against_ifunc,
2262 plt_entry_size,
765e526c 2263 (htab->plt.has_plt0
0afcef53 2264 * plt_entry_size),
ee2fdd6f
L
2265 4, TRUE))
2266 {
2267 asection *s = htab->plt_second;
2268 if (h->plt.offset != (bfd_vma) -1 && s != NULL)
2269 {
2270 /* Use the second PLT section if it is created. */
2271 eh->plt_second.offset = s->size;
2272
2273 /* Make room for this entry in the second PLT section. */
765e526c 2274 s->size += htab->non_lazy_plt->plt_entry_size;
ee2fdd6f
L
2275 }
2276
2277 return TRUE;
2278 }
2279 else
2280 return FALSE;
2281 }
04ebc307
L
2282 /* Don't create the PLT entry if there are only function pointer
2283 relocations which can be resolved at run-time. */
cbe950e9 2284 else if (htab->elf.dynamic_sections_created
04ebc307
L
2285 && (h->plt.refcount > eh->func_pointer_refcount
2286 || eh->plt_got.refcount > 0))
6725bdbf 2287 {
dc2be3d2 2288 bfd_boolean use_plt_got = eh->plt_got.refcount > 0;
25070364 2289
04ebc307
L
2290 /* Clear the reference count of function pointer relocations
2291 if PLT is used. */
2292 eh->func_pointer_refcount = 0;
2293
5a15f56f
AM
2294 /* Make sure this symbol is output as a dynamic symbol.
2295 Undefined weak syms won't yet be marked as dynamic. */
2296 if (h->dynindx == -1
aec6b87e 2297 && !h->forced_local
0dc9a308
L
2298 && !resolved_to_zero
2299 && h->root.type == bfd_link_hash_undefweak)
5a15f56f 2300 {
c152c796 2301 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2302 return FALSE;
5a15f56f
AM
2303 }
2304
0e1862bb 2305 if (bfd_link_pic (info)
4e795f50 2306 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
ced53ee5 2307 {
6de2ae4a 2308 asection *s = htab->elf.splt;
ee2fdd6f 2309 asection *second_s = htab->plt_second;
dd7e64d4 2310 asection *got_s = htab->plt_got;
6725bdbf 2311
7c1e8d3e
L
2312 /* If this is the first .plt entry, make room for the special
2313 first entry. The .plt section is used by prelink to undo
2314 prelinking for dynamic relocations. */
2315 if (s->size == 0)
765e526c 2316 s->size = htab->plt.has_plt0 * plt_entry_size;
7c1e8d3e 2317
dd7e64d4
L
2318 if (use_plt_got)
2319 eh->plt_got.offset = got_s->size;
2320 else
ee2fdd6f
L
2321 {
2322 h->plt.offset = s->size;
2323 if (second_s)
2324 eh->plt_second.offset = second_s->size;
2325 }
6725bdbf 2326
ced53ee5
AM
2327 /* If this symbol is not defined in a regular file, and we are
2328 not generating a shared library, then set the symbol to this
2329 location in the .plt. This is required to make function
2330 pointers compare as equal between the normal executable and
2331 the shared library. */
0e1862bb 2332 if (! bfd_link_pic (info)
f5385ebf 2333 && !h->def_regular)
ced53ee5 2334 {
dd7e64d4
L
2335 if (use_plt_got)
2336 {
2337 /* We need to make a call to the entry of the GOT PLT
2338 instead of regular PLT entry. */
2339 h->root.u.def.section = got_s;
2340 h->root.u.def.value = eh->plt_got.offset;
2341 }
2342 else
2343 {
ee2fdd6f
L
2344 if (second_s)
2345 {
2346 /* We need to make a call to the entry of the
2347 second PLT instead of regular PLT entry. */
2348 h->root.u.def.section = second_s;
2349 h->root.u.def.value = eh->plt_second.offset;
2350 }
2351 else
2352 {
2353 h->root.u.def.section = s;
2354 h->root.u.def.value = h->plt.offset;
2355 }
dd7e64d4 2356 }
ced53ee5 2357 }
6725bdbf 2358
ced53ee5 2359 /* Make room for this entry. */
dd7e64d4 2360 if (use_plt_got)
765e526c 2361 got_s->size += htab->non_lazy_plt->plt_entry_size;
dd7e64d4
L
2362 else
2363 {
2364 s->size += plt_entry_size;
ee2fdd6f 2365 if (second_s)
765e526c 2366 second_s->size += htab->non_lazy_plt->plt_entry_size;
6725bdbf 2367
dd7e64d4
L
2368 /* We also need to make an entry in the .got.plt section,
2369 which will be placed in the .got section by the linker
2370 script. */
2371 htab->elf.sgotplt->size += 4;
6725bdbf 2372
aec6b87e
L
2373 /* There should be no PLT relocation against resolved
2374 undefined weak symbol in executable. */
2375 if (!resolved_to_zero)
2376 {
2377 /* We also need to make an entry in the .rel.plt
2378 section. */
2379 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2380 htab->elf.srelplt->reloc_count++;
2381 }
dd7e64d4 2382 }
eac338cf 2383
f604c2a2 2384 if (bed->os == is_vxworks && !bfd_link_pic (info))
eac338cf
PB
2385 {
2386 /* VxWorks has a second set of relocations for each PLT entry
2387 in executables. They go in a separate relocation section,
2388 which is processed by the kernel loader. */
2389
2390 /* There are two relocations for the initial PLT entry: an
2391 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
2392 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
2393
765e526c 2394 asection *srelplt2 = htab->srelplt2;
25e762b9 2395 if (h->plt.offset == plt_entry_size)
0afcef53 2396 srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
eac338cf
PB
2397
2398 /* There are two extra relocations for each subsequent PLT entry:
2399 an R_386_32 relocation for the GOT entry, and an R_386_32
2400 relocation for the PLT entry. */
2401
0afcef53 2402 srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
eac338cf 2403 }
6725bdbf 2404 }
ced53ee5
AM
2405 else
2406 {
13f42277 2407 eh->plt_got.offset = (bfd_vma) -1;
51b64d56 2408 h->plt.offset = (bfd_vma) -1;
f5385ebf 2409 h->needs_plt = 0;
ced53ee5 2410 }
6725bdbf
AM
2411 }
2412 else
2413 {
13f42277 2414 eh->plt_got.offset = (bfd_vma) -1;
51b64d56 2415 h->plt.offset = (bfd_vma) -1;
f5385ebf 2416 h->needs_plt = 0;
6725bdbf
AM
2417 }
2418
67a4f2b7
AO
2419 eh->tlsdesc_got = (bfd_vma) -1;
2420
37e55690 2421 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
13ae64f3
JJ
2422 make it a R_386_TLS_LE_32 requiring no TLS entry. */
2423 if (h->got.refcount > 0
0e1862bb 2424 && bfd_link_executable (info)
13ae64f3 2425 && h->dynindx == -1
0afcef53 2426 && (elf_x86_hash_entry (h)->tls_type & GOT_TLS_IE))
cedb70c5 2427 h->got.offset = (bfd_vma) -1;
13ae64f3 2428 else if (h->got.refcount > 0)
6725bdbf 2429 {
0ac8d2ca 2430 asection *s;
b34976b6 2431 bfd_boolean dyn;
0afcef53 2432 int tls_type = elf_x86_hash_entry (h)->tls_type;
6725bdbf 2433
5a15f56f 2434 /* Make sure this symbol is output as a dynamic symbol.
0dc9a308 2435 Undefined weak syms won't yet be marked as dynamic. */
5a15f56f 2436 if (h->dynindx == -1
aec6b87e 2437 && !h->forced_local
e133d005 2438 && !resolved_to_zero
0dc9a308 2439 && h->root.type == bfd_link_hash_undefweak)
5a15f56f 2440 {
c152c796 2441 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2442 return FALSE;
5a15f56f
AM
2443 }
2444
6de2ae4a 2445 s = htab->elf.sgot;
67a4f2b7
AO
2446 if (GOT_TLS_GDESC_P (tls_type))
2447 {
6de2ae4a 2448 eh->tlsdesc_got = htab->elf.sgotplt->size
67a4f2b7 2449 - elf_i386_compute_jump_table_size (htab);
6de2ae4a 2450 htab->elf.sgotplt->size += 8;
67a4f2b7
AO
2451 h->got.offset = (bfd_vma) -2;
2452 }
2453 if (! GOT_TLS_GDESC_P (tls_type)
2454 || GOT_TLS_GD_P (tls_type))
2455 {
2456 h->got.offset = s->size;
2457 s->size += 4;
2458 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
2459 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
2460 s->size += 4;
2461 }
ebe50bae 2462 dyn = htab->elf.dynamic_sections_created;
13ae64f3 2463 /* R_386_TLS_IE_32 needs one dynamic relocation,
37e55690
JJ
2464 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
2465 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
2466 need two), R_386_TLS_GD needs one if local symbol and two if
aec6b87e
L
2467 global. No dynamic relocation against resolved undefined weak
2468 symbol in executable. */
37e55690 2469 if (tls_type == GOT_TLS_IE_BOTH)
6de2ae4a 2470 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
67a4f2b7 2471 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
37e55690 2472 || (tls_type & GOT_TLS_IE))
6de2ae4a 2473 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
67a4f2b7 2474 else if (GOT_TLS_GD_P (tls_type))
6de2ae4a 2475 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
67a4f2b7 2476 else if (! GOT_TLS_GDESC_P (tls_type)
aec6b87e
L
2477 && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2478 && !resolved_to_zero)
67a4f2b7 2479 || h->root.type != bfd_link_hash_undefweak)
0e1862bb 2480 && (bfd_link_pic (info)
ef5aade5 2481 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
6de2ae4a 2482 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
67a4f2b7 2483 if (GOT_TLS_GDESC_P (tls_type))
6de2ae4a 2484 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
6725bdbf
AM
2485 }
2486 else
51b64d56 2487 h->got.offset = (bfd_vma) -1;
6725bdbf 2488
5a15f56f 2489 if (eh->dyn_relocs == NULL)
b34976b6 2490 return TRUE;
5a15f56f 2491
0c715baa
AM
2492 /* In the shared -Bsymbolic case, discard space allocated for
2493 dynamic pc-relative relocs against symbols which turn out to be
2494 defined in regular objects. For the normal shared case, discard
0ac8d2ca
AM
2495 space for pc-relative relocs that have become local due to symbol
2496 visibility changes. */
0c715baa 2497
0e1862bb 2498 if (bfd_link_pic (info))
5a15f56f 2499 {
09695f56
AM
2500 /* The only reloc that uses pc_count is R_386_PC32, which will
2501 appear on a call or on something like ".long foo - .". We
2502 want calls to protected symbols to resolve directly to the
2503 function rather than going via the plt. If people want
2504 function pointer comparisons to work as expected then they
2505 should avoid writing assembly like ".long foo - .". */
2506 if (SYMBOL_CALLS_LOCAL (info, h))
5a15f56f 2507 {
e03a8ed8 2508 struct elf_dyn_relocs **pp;
0c715baa
AM
2509
2510 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2511 {
c3ce498c
L
2512 p->count -= p->pc_count;
2513 p->pc_count = 0;
0c715baa
AM
2514 if (p->count == 0)
2515 *pp = p->next;
2516 else
2517 pp = &p->next;
2518 }
5a15f56f 2519 }
4e795f50 2520
f604c2a2 2521 if (bed->os == is_vxworks)
3348747a 2522 {
e03a8ed8 2523 struct elf_dyn_relocs **pp;
3348747a
NS
2524 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2525 {
2526 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2527 *pp = p->next;
2528 else
2529 pp = &p->next;
2530 }
2531 }
2532
4e795f50 2533 /* Also discard relocs on undefined weak syms with non-default
aec6b87e 2534 visibility or in PIE. */
22d606e9 2535 if (eh->dyn_relocs != NULL
c353e543 2536 && h->root.type == bfd_link_hash_undefweak)
22d606e9 2537 {
aec6b87e
L
2538 /* Undefined weak symbol is never bound locally in shared
2539 library. */
2540 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2541 || resolved_to_zero)
2542 {
2543 if (h->non_got_ref)
2544 {
2545 /* Keep dynamic non-GOT/non-PLT relocation so that we
2546 can branch to 0 without PLT. */
2547 struct elf_dyn_relocs **pp;
2548
2549 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2550 if (p->pc_count == 0)
2551 *pp = p->next;
2552 else
2553 {
2554 /* Remove non-R_386_PC32 relocation. */
2555 p->count = p->pc_count;
2556 pp = &p->next;
2557 }
22d606e9 2558
aec6b87e
L
2559 if (eh->dyn_relocs != NULL)
2560 {
2561 /* Make sure undefined weak symbols are output
2562 as dynamic symbols in PIEs for dynamic non-GOT
2563 non-PLT reloations. */
2564 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2565 return FALSE;
2566 }
2567 }
2568 else
2569 eh->dyn_relocs = NULL;
2570 }
22d606e9
AM
2571 else if (h->dynindx == -1
2572 && !h->forced_local)
2573 {
2574 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2575 return FALSE;
2576 }
2577 }
0c715baa 2578 }
a23b6845 2579 else if (ELIMINATE_COPY_RELOCS)
0c715baa
AM
2580 {
2581 /* For the non-shared case, discard space for relocs against
2582 symbols which turn out to need copy relocs or are not
04ebc307
L
2583 dynamic. Keep dynamic relocations for run-time function
2584 pointer initialization. */
0c715baa 2585
aec6b87e
L
2586 if ((!h->non_got_ref
2587 || eh->func_pointer_refcount > 0
2588 || (h->root.type == bfd_link_hash_undefweak
2589 && !resolved_to_zero))
f5385ebf
AM
2590 && ((h->def_dynamic
2591 && !h->def_regular)
ebe50bae 2592 || (htab->elf.dynamic_sections_created
0c715baa
AM
2593 && (h->root.type == bfd_link_hash_undefweak
2594 || h->root.type == bfd_link_hash_undefined))))
2595 {
2596 /* Make sure this symbol is output as a dynamic symbol.
2597 Undefined weak syms won't yet be marked as dynamic. */
2598 if (h->dynindx == -1
aec6b87e 2599 && !h->forced_local
0dc9a308
L
2600 && !resolved_to_zero
2601 && h->root.type == bfd_link_hash_undefweak)
0c715baa 2602 {
c152c796 2603 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2604 return FALSE;
0c715baa 2605 }
5a15f56f 2606
0c715baa
AM
2607 /* If that succeeded, we know we'll be keeping all the
2608 relocs. */
2609 if (h->dynindx != -1)
2610 goto keep;
2611 }
2612
2613 eh->dyn_relocs = NULL;
04ebc307 2614 eh->func_pointer_refcount = 0;
0c715baa 2615
ec338859 2616 keep: ;
5a15f56f
AM
2617 }
2618
0c715baa
AM
2619 /* Finally, allocate space. */
2620 for (p = eh->dyn_relocs; p != NULL; p = p->next)
12d0ee4a 2621 {
e7c33416
NC
2622 asection *sreloc;
2623
cbe950e9 2624 sreloc = elf_section_data (p->sec)->sreloc;
e7c33416
NC
2625
2626 BFD_ASSERT (sreloc != NULL);
eea6121a 2627 sreloc->size += p->count * sizeof (Elf32_External_Rel);
12d0ee4a
AM
2628 }
2629
b34976b6 2630 return TRUE;
6725bdbf
AM
2631}
2632
c25bc9fc
L
2633/* Allocate space in .plt, .got and associated reloc sections for
2634 local dynamic relocs. */
2635
2636static bfd_boolean
2637elf_i386_allocate_local_dynrelocs (void **slot, void *inf)
2638{
2639 struct elf_link_hash_entry *h
2640 = (struct elf_link_hash_entry *) *slot;
2641
2642 if (h->type != STT_GNU_IFUNC
2643 || !h->def_regular
2644 || !h->ref_regular
2645 || !h->forced_local
2646 || h->root.type != bfd_link_hash_defined)
2647 abort ();
2648
2649 return elf_i386_allocate_dynrelocs (h, inf);
2650}
2651
c175a657 2652/* Convert load via the GOT slot to load immediate. */
daa67607
L
2653
2654static bfd_boolean
02a86693 2655elf_i386_convert_load (bfd *abfd, asection *sec,
a6af384b 2656 struct bfd_link_info *link_info)
daa67607 2657{
0afcef53 2658 struct elf_x86_link_hash_table *htab;
daa67607
L
2659 Elf_Internal_Shdr *symtab_hdr;
2660 Elf_Internal_Rela *internal_relocs;
2661 Elf_Internal_Rela *irel, *irelend;
2662 bfd_byte *contents;
c175a657 2663 bfd_boolean changed;
daa67607
L
2664 bfd_signed_vma *local_got_refcounts;
2665
2666 /* Don't even try to convert non-ELF outputs. */
2667 if (!is_elf_hash_table (link_info->hash))
2668 return FALSE;
2669
c1d11331 2670 /* Nothing to do if there is no need or no output. */
daa67607 2671 if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
02a86693 2672 || sec->need_convert_load == 0
c8831961 2673 || bfd_is_abs_section (sec->output_section))
daa67607
L
2674 return TRUE;
2675
2676 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2677
2678 /* Load the relocations for this section. */
2679 internal_relocs = (_bfd_elf_link_read_relocs
2680 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
2681 link_info->keep_memory));
2682 if (internal_relocs == NULL)
2683 return FALSE;
2684
c175a657 2685 changed = FALSE;
0afcef53 2686 htab = elf_x86_hash_table (link_info, I386_ELF_DATA);
daa67607
L
2687 local_got_refcounts = elf_local_got_refcounts (abfd);
2688
2689 /* Get the section contents. */
2690 if (elf_section_data (sec)->this_hdr.contents != NULL)
2691 contents = elf_section_data (sec)->this_hdr.contents;
2692 else
2693 {
2694 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2695 goto error_return;
2696 }
2697
2698 irelend = internal_relocs + sec->reloc_count;
2699 for (irel = internal_relocs; irel < irelend; irel++)
2700 {
2701 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
c175a657 2702 unsigned int r_symndx;
daa67607 2703 struct elf_link_hash_entry *h;
c175a657 2704 bfd_boolean converted;
daa67607 2705
7d4d9709
L
2706 /* Don't convert R_386_GOT32 since we can't tell if it is applied
2707 to "mov $foo@GOT, %reg" which isn't a load via GOT. */
2708 if (r_type != R_386_GOT32X)
daa67607
L
2709 continue;
2710
c175a657 2711 r_symndx = ELF32_R_SYM (irel->r_info);
02a86693 2712 if (r_symndx < symtab_hdr->sh_info)
0afcef53
L
2713 h = _bfd_elf_x86_get_local_sym_hash (htab, sec->owner,
2714 (const Elf_Internal_Rela *) irel,
2715 FALSE);
c175a657 2716 else
02a86693 2717 {
c175a657
L
2718 h = elf_sym_hashes (abfd)[r_symndx - symtab_hdr->sh_info];
2719 while (h->root.type == bfd_link_hash_indirect
2720 || h->root.type == bfd_link_hash_warning)
2721 h = (struct elf_link_hash_entry *) h->root.u.i.link;
daa67607
L
2722 }
2723
02a86693 2724 /* STT_GNU_IFUNC must keep GOT32 relocations. */
c175a657 2725 if (h != NULL && h->type == STT_GNU_IFUNC)
02a86693
L
2726 continue;
2727
c175a657
L
2728 converted = FALSE;
2729 if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents,
2730 irel, h, &converted, link_info))
2731 goto error_return;
bae420ef 2732
c175a657 2733 if (converted)
02a86693 2734 {
c175a657
L
2735 changed = converted;
2736 if (h)
02a86693 2737 {
c175a657
L
2738 if (h->got.refcount > 0)
2739 h->got.refcount -= 1;
02a86693 2740 }
c175a657 2741 else
02a86693 2742 {
c175a657
L
2743 if (local_got_refcounts != NULL
2744 && local_got_refcounts[r_symndx] > 0)
2745 local_got_refcounts[r_symndx] -= 1;
02a86693 2746 }
daa67607
L
2747 }
2748 }
2749
2750 if (contents != NULL
2751 && elf_section_data (sec)->this_hdr.contents != contents)
2752 {
c175a657 2753 if (!changed && !link_info->keep_memory)
daa67607
L
2754 free (contents);
2755 else
2756 {
2757 /* Cache the section contents for elf_link_input_bfd. */
2758 elf_section_data (sec)->this_hdr.contents = contents;
2759 }
2760 }
2761
2762 if (elf_section_data (sec)->relocs != internal_relocs)
2763 {
c175a657 2764 if (!changed)
daa67607
L
2765 free (internal_relocs);
2766 else
2767 elf_section_data (sec)->relocs = internal_relocs;
2768 }
2769
2770 return TRUE;
2771
2772 error_return:
2773 if (contents != NULL
2774 && elf_section_data (sec)->this_hdr.contents != contents)
2775 free (contents);
2776 if (internal_relocs != NULL
2777 && elf_section_data (sec)->relocs != internal_relocs)
2778 free (internal_relocs);
2779 return FALSE;
2780}
2781
252b5132
RH
2782/* Set the sizes of the dynamic sections. */
2783
b34976b6 2784static bfd_boolean
23209a78 2785elf_i386_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
252b5132 2786{
0afcef53 2787 struct elf_x86_link_hash_table *htab;
252b5132
RH
2788 bfd *dynobj;
2789 asection *s;
b34976b6 2790 bfd_boolean relocs;
0c715baa 2791 bfd *ibfd;
252b5132 2792
0afcef53 2793 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6
NC
2794 if (htab == NULL)
2795 return FALSE;
ebe50bae 2796 dynobj = htab->elf.dynobj;
ffb2e45b
AM
2797 if (dynobj == NULL)
2798 abort ();
252b5132 2799
0c715baa
AM
2800 /* Set up .got offsets for local syms, and space for local dynamic
2801 relocs. */
c72f2fb2 2802 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
161d71a6
L
2803 {
2804 bfd_signed_vma *local_got;
2805 bfd_signed_vma *end_local_got;
13ae64f3 2806 char *local_tls_type;
67a4f2b7 2807 bfd_vma *local_tlsdesc_gotent;
161d71a6
L
2808 bfd_size_type locsymcount;
2809 Elf_Internal_Shdr *symtab_hdr;
2810 asection *srel;
6725bdbf 2811
0ffa91dd 2812 if (! is_i386_elf (ibfd))
161d71a6 2813 continue;
6725bdbf 2814
0c715baa
AM
2815 for (s = ibfd->sections; s != NULL; s = s->next)
2816 {
e03a8ed8 2817 struct elf_dyn_relocs *p;
0c715baa 2818
02a86693 2819 if (!elf_i386_convert_load (ibfd, s, info))
daa67607
L
2820 return FALSE;
2821
e03a8ed8 2822 for (p = ((struct elf_dyn_relocs *)
e81d3500 2823 elf_section_data (s)->local_dynrel);
ec338859
AM
2824 p != NULL;
2825 p = p->next)
0c715baa 2826 {
ec338859
AM
2827 if (!bfd_is_abs_section (p->sec)
2828 && bfd_is_abs_section (p->sec->output_section))
2829 {
2830 /* Input section has been discarded, either because
2831 it is a copy of a linkonce section or due to
2832 linker script /DISCARD/, so we'll be discarding
2833 the relocs too. */
2834 }
f604c2a2
L
2835 else if ((get_elf_i386_backend_data (output_bfd)->os
2836 == is_vxworks)
3348747a
NS
2837 && strcmp (p->sec->output_section->name,
2838 ".tls_vars") == 0)
2839 {
2840 /* Relocations in vxworks .tls_vars sections are
2841 handled specially by the loader. */
2842 }
248866a8 2843 else if (p->count != 0)
ec338859
AM
2844 {
2845 srel = elf_section_data (p->sec)->sreloc;
eea6121a 2846 srel->size += p->count * sizeof (Elf32_External_Rel);
4b819e1f
L
2847 if ((p->sec->output_section->flags & SEC_READONLY) != 0
2848 && (info->flags & DF_TEXTREL) == 0)
b70321a2
L
2849 {
2850 info->flags |= DF_TEXTREL;
0e1862bb 2851 if ((info->warn_shared_textrel && bfd_link_pic (info))
1952c5cd 2852 || info->error_textrel)
695344c0 2853 /* xgettext:c-format */
1952c5cd 2854 info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
b70321a2 2855 p->sec->owner, p->sec);
b70321a2 2856 }
ec338859 2857 }
0c715baa
AM
2858 }
2859 }
2860
2861 local_got = elf_local_got_refcounts (ibfd);
161d71a6
L
2862 if (!local_got)
2863 continue;
6725bdbf 2864
0ffa91dd 2865 symtab_hdr = &elf_symtab_hdr (ibfd);
161d71a6
L
2866 locsymcount = symtab_hdr->sh_info;
2867 end_local_got = local_got + locsymcount;
0afcef53
L
2868 local_tls_type = elf_x86_local_got_tls_type (ibfd);
2869 local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
6de2ae4a
L
2870 s = htab->elf.sgot;
2871 srel = htab->elf.srelgot;
67a4f2b7
AO
2872 for (; local_got < end_local_got;
2873 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
161d71a6 2874 {
67a4f2b7 2875 *local_tlsdesc_gotent = (bfd_vma) -1;
161d71a6 2876 if (*local_got > 0)
6725bdbf 2877 {
67a4f2b7
AO
2878 if (GOT_TLS_GDESC_P (*local_tls_type))
2879 {
6de2ae4a 2880 *local_tlsdesc_gotent = htab->elf.sgotplt->size
67a4f2b7 2881 - elf_i386_compute_jump_table_size (htab);
6de2ae4a 2882 htab->elf.sgotplt->size += 8;
67a4f2b7
AO
2883 *local_got = (bfd_vma) -2;
2884 }
2885 if (! GOT_TLS_GDESC_P (*local_tls_type)
2886 || GOT_TLS_GD_P (*local_tls_type))
2887 {
2888 *local_got = s->size;
2889 s->size += 4;
2890 if (GOT_TLS_GD_P (*local_tls_type)
2891 || *local_tls_type == GOT_TLS_IE_BOTH)
2892 s->size += 4;
2893 }
0e1862bb 2894 if (bfd_link_pic (info)
67a4f2b7 2895 || GOT_TLS_GD_ANY_P (*local_tls_type)
37e55690
JJ
2896 || (*local_tls_type & GOT_TLS_IE))
2897 {
2898 if (*local_tls_type == GOT_TLS_IE_BOTH)
eea6121a 2899 srel->size += 2 * sizeof (Elf32_External_Rel);
67a4f2b7
AO
2900 else if (GOT_TLS_GD_P (*local_tls_type)
2901 || ! GOT_TLS_GDESC_P (*local_tls_type))
eea6121a 2902 srel->size += sizeof (Elf32_External_Rel);
67a4f2b7 2903 if (GOT_TLS_GDESC_P (*local_tls_type))
6de2ae4a 2904 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
37e55690 2905 }
6725bdbf 2906 }
161d71a6
L
2907 else
2908 *local_got = (bfd_vma) -1;
6725bdbf 2909 }
252b5132 2910 }
6725bdbf 2911
0afcef53 2912 if (htab->tls_ld_or_ldm_got.refcount > 0)
13ae64f3
JJ
2913 {
2914 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2915 relocs. */
0afcef53 2916 htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
6de2ae4a
L
2917 htab->elf.sgot->size += 8;
2918 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
13ae64f3
JJ
2919 }
2920 else
0afcef53 2921 htab->tls_ld_or_ldm_got.offset = -1;
13ae64f3 2922
0c715baa
AM
2923 /* Allocate global sym .plt and .got entries, and space for global
2924 sym dynamic relocs. */
eb4ff4d6 2925 elf_link_hash_traverse (&htab->elf, elf_i386_allocate_dynrelocs, info);
252b5132 2926
c25bc9fc
L
2927 /* Allocate .plt and .got entries, and space for local symbols. */
2928 htab_traverse (htab->loc_hash_table,
2929 elf_i386_allocate_local_dynrelocs,
2930 info);
2931
67a4f2b7
AO
2932 /* For every jump slot reserved in the sgotplt, reloc_count is
2933 incremented. However, when we reserve space for TLS descriptors,
2934 it's not incremented, so in order to compute the space reserved
2935 for them, it suffices to multiply the reloc count by the jump
e1f98742 2936 slot size.
caa0075c 2937
e1f98742
L
2938 PR ld/13302: We start next_irelative_index at the end of .rela.plt
2939 so that R_386_IRELATIVE entries come last. */
6de2ae4a 2940 if (htab->elf.srelplt)
e1f98742 2941 {
765e526c
L
2942 htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
2943 htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
e1f98742
L
2944 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
2945 }
2946 else if (htab->elf.irelplt)
2947 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
2948
67a4f2b7 2949
a7b16ceb
L
2950 if (htab->elf.sgotplt)
2951 {
2952 /* Don't allocate .got.plt section if there are no GOT nor PLT
9a2a56cc 2953 entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */
9d4b2dba
L
2954 if ((htab->elf.hgot == NULL
2955 || !htab->elf.hgot->ref_regular_nonweak)
e28df02b
L
2956 && (htab->elf.sgotplt->size
2957 == get_elf_backend_data (output_bfd)->got_header_size)
a7b16ceb
L
2958 && (htab->elf.splt == NULL
2959 || htab->elf.splt->size == 0)
2960 && (htab->elf.sgot == NULL
2961 || htab->elf.sgot->size == 0)
2962 && (htab->elf.iplt == NULL
2963 || htab->elf.iplt->size == 0)
2964 && (htab->elf.igotplt == NULL
2965 || htab->elf.igotplt->size == 0))
2966 htab->elf.sgotplt->size = 0;
2967 }
2968
fff53dae
L
2969 if (_bfd_elf_eh_frame_present (info))
2970 {
2971 if (htab->plt_eh_frame != NULL
2972 && htab->elf.splt != NULL
2973 && htab->elf.splt->size != 0
2974 && !bfd_is_abs_section (htab->elf.splt->output_section))
765e526c 2975 htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
fff53dae
L
2976
2977 if (htab->plt_got_eh_frame != NULL
2978 && htab->plt_got != NULL
2979 && htab->plt_got->size != 0
2980 && !bfd_is_abs_section (htab->plt_got->output_section))
2981 htab->plt_got_eh_frame->size
765e526c 2982 = htab->non_lazy_plt->eh_frame_plt_size;
ee2fdd6f
L
2983
2984 /* Unwind info for the second PLT and .plt.got sections are
2985 identical. */
2986 if (htab->plt_second_eh_frame != NULL
2987 && htab->plt_second != NULL
2988 && htab->plt_second->size != 0
2989 && !bfd_is_abs_section (htab->plt_second->output_section))
2990 htab->plt_second_eh_frame->size
765e526c 2991 = htab->non_lazy_plt->eh_frame_plt_size;
fff53dae 2992 }
9a2a56cc 2993
5a15f56f
AM
2994 /* We now have determined the sizes of the various dynamic sections.
2995 Allocate memory for them. */
b34976b6 2996 relocs = FALSE;
252b5132
RH
2997 for (s = dynobj->sections; s != NULL; s = s->next)
2998 {
eac338cf
PB
2999 bfd_boolean strip_section = TRUE;
3000
252b5132
RH
3001 if ((s->flags & SEC_LINKER_CREATED) == 0)
3002 continue;
3003
6de2ae4a 3004 if (s == htab->elf.splt
9a2a56cc 3005 || s == htab->elf.sgot)
252b5132 3006 {
6725bdbf
AM
3007 /* Strip this section if we don't need it; see the
3008 comment below. */
eac338cf
PB
3009 /* We'd like to strip these sections if they aren't needed, but if
3010 we've exported dynamic symbols from them we must leave them.
3011 It's too late to tell BFD to get rid of the symbols. */
3012
7325306f 3013 if (htab->elf.hplt != NULL)
eac338cf 3014 strip_section = FALSE;
252b5132 3015 }
9a2a56cc
AM
3016 else if (s == htab->elf.sgotplt
3017 || s == htab->elf.iplt
3018 || s == htab->elf.igotplt
ee2fdd6f 3019 || s == htab->plt_second
dd7e64d4 3020 || s == htab->plt_got
9a2a56cc 3021 || s == htab->plt_eh_frame
fff53dae 3022 || s == htab->plt_got_eh_frame
ee2fdd6f 3023 || s == htab->plt_second_eh_frame
5474d94f
AM
3024 || s == htab->elf.sdynbss
3025 || s == htab->elf.sdynrelro)
9a2a56cc
AM
3026 {
3027 /* Strip these too. */
3028 }
0112cd26 3029 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
252b5132 3030 {
6de2ae4a
L
3031 if (s->size != 0
3032 && s != htab->elf.srelplt
765e526c 3033 && s != htab->srelplt2)
b34976b6 3034 relocs = TRUE;
252b5132 3035
0ac8d2ca
AM
3036 /* We use the reloc_count field as a counter if we need
3037 to copy relocs into the output file. */
5ae0bfb6 3038 s->reloc_count = 0;
252b5132 3039 }
6725bdbf 3040 else
252b5132
RH
3041 {
3042 /* It's not one of our sections, so don't allocate space. */
3043 continue;
3044 }
3045
c456f082 3046 if (s->size == 0)
252b5132 3047 {
0ac8d2ca
AM
3048 /* If we don't need this section, strip it from the
3049 output file. This is mostly to handle .rel.bss and
3050 .rel.plt. We must create both sections in
3051 create_dynamic_sections, because they must be created
3052 before the linker maps input sections to output
3053 sections. The linker does that before
3054 adjust_dynamic_symbol is called, and it is that
3055 function which decides whether anything needs to go
3056 into these sections. */
c456f082
AM
3057 if (strip_section)
3058 s->flags |= SEC_EXCLUDE;
252b5132
RH
3059 continue;
3060 }
3061
c456f082
AM
3062 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3063 continue;
3064
f69da49f
AM
3065 /* Allocate memory for the section contents. We use bfd_zalloc
3066 here in case unused entries are not reclaimed before the
3067 section's contents are written out. This should not happen,
3068 but this way if it does, we get a R_386_NONE reloc instead
3069 of garbage. */
a50b1753 3070 s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
6725bdbf 3071 if (s->contents == NULL)
b34976b6 3072 return FALSE;
252b5132
RH
3073 }
3074
e41b3a13 3075 if (htab->plt_eh_frame != NULL
9a2a56cc
AM
3076 && htab->plt_eh_frame->contents != NULL)
3077 {
f129e49f 3078 memcpy (htab->plt_eh_frame->contents,
765e526c 3079 htab->plt.eh_frame_plt,
f129e49f 3080 htab->plt_eh_frame->size);
9a2a56cc
AM
3081 bfd_put_32 (dynobj, htab->elf.splt->size,
3082 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
3083 }
e41b3a13 3084
fff53dae
L
3085 if (htab->plt_got_eh_frame != NULL
3086 && htab->plt_got_eh_frame->contents != NULL)
3087 {
3088 memcpy (htab->plt_got_eh_frame->contents,
765e526c 3089 htab->non_lazy_plt->eh_frame_plt,
fff53dae
L
3090 htab->plt_got_eh_frame->size);
3091 bfd_put_32 (dynobj, htab->plt_got->size,
3092 (htab->plt_got_eh_frame->contents
3093 + PLT_FDE_LEN_OFFSET));
3094 }
3095
ee2fdd6f
L
3096 if (htab->plt_second_eh_frame != NULL
3097 && htab->plt_second_eh_frame->contents != NULL)
3098 {
3099 memcpy (htab->plt_second_eh_frame->contents,
765e526c 3100 htab->non_lazy_plt->eh_frame_plt,
ee2fdd6f
L
3101 htab->plt_second_eh_frame->size);
3102 bfd_put_32 (dynobj, htab->plt_second->size,
3103 (htab->plt_second_eh_frame->contents
3104 + PLT_FDE_LEN_OFFSET));
3105 }
3106
ebe50bae 3107 if (htab->elf.dynamic_sections_created)
252b5132
RH
3108 {
3109 /* Add some entries to the .dynamic section. We fill in the
3110 values later, in elf_i386_finish_dynamic_sections, but we
3111 must add the entries now so that we get the correct size for
3112 the .dynamic section. The DT_DEBUG entry is filled in by the
3113 dynamic linker and used by the debugger. */
dc810e39 3114#define add_dynamic_entry(TAG, VAL) \
5a580b3a 3115 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 3116
0e1862bb 3117 if (bfd_link_executable (info))
252b5132 3118 {
dc810e39 3119 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 3120 return FALSE;
252b5132
RH
3121 }
3122
6de2ae4a 3123 if (htab->elf.splt->size != 0)
252b5132 3124 {
7c1e8d3e
L
3125 /* DT_PLTGOT is used by prelink even if there is no PLT
3126 relocation. */
3127 if (!add_dynamic_entry (DT_PLTGOT, 0))
b34976b6 3128 return FALSE;
97d343d4 3129 }
7c1e8d3e 3130
97d343d4
L
3131 if (htab->elf.srelplt->size != 0)
3132 {
3133 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
3134 || !add_dynamic_entry (DT_PLTREL, DT_REL)
3135 || !add_dynamic_entry (DT_JMPREL, 0))
3136 return FALSE;
252b5132
RH
3137 }
3138
3139 if (relocs)
3140 {
dc810e39
AM
3141 if (!add_dynamic_entry (DT_REL, 0)
3142 || !add_dynamic_entry (DT_RELSZ, 0)
3143 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
b34976b6 3144 return FALSE;
252b5132 3145
0c715baa
AM
3146 /* If any dynamic relocs apply to a read-only section,
3147 then we need a DT_TEXTREL entry. */
248866a8 3148 if ((info->flags & DF_TEXTREL) == 0)
eb4ff4d6 3149 elf_link_hash_traverse (&htab->elf,
0afcef53
L
3150 _bfd_x86_elf_readonly_dynrelocs,
3151 info);
0c715baa
AM
3152
3153 if ((info->flags & DF_TEXTREL) != 0)
3154 {
2df3368d 3155 if (htab->readonly_dynrelocs_against_ifunc)
8efa2874
L
3156 {
3157 info->callbacks->einfo
3158 (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
3159 bfd_set_error (bfd_error_bad_value);
3160 return FALSE;
3161 }
3162
0c715baa 3163 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 3164 return FALSE;
0c715baa 3165 }
252b5132 3166 }
f604c2a2 3167 if (get_elf_i386_backend_data (output_bfd)->os == is_vxworks
7a2b07ff
NS
3168 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
3169 return FALSE;
252b5132 3170 }
dc810e39 3171#undef add_dynamic_entry
252b5132 3172
b34976b6 3173 return TRUE;
252b5132
RH
3174}
3175
38701953
AM
3176/* Set the correct type for an x86 ELF section. We do this by the
3177 section name, which is a hack, but ought to work. */
3178
b34976b6 3179static bfd_boolean
55fd94b0
AM
3180elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
3181 Elf_Internal_Shdr *hdr,
3182 asection *sec)
38701953 3183{
91d6fa6a 3184 const char *name;
38701953
AM
3185
3186 name = bfd_get_section_name (abfd, sec);
3187
3188 /* This is an ugly, but unfortunately necessary hack that is
3189 needed when producing EFI binaries on x86. It tells
3190 elf.c:elf_fake_sections() not to consider ".reloc" as a section
3191 containing ELF relocation info. We need this hack in order to
3192 be able to generate ELF binaries that can be translated into
3193 EFI applications (which are essentially COFF objects). Those
3194 files contain a COFF ".reloc" section inside an ELFNN object,
3195 which would normally cause BFD to segfault because it would
3196 attempt to interpret this section as containing relocation
3197 entries for section "oc". With this hack enabled, ".reloc"
3198 will be treated as a normal data section, which will avoid the
3199 segfault. However, you won't be able to create an ELFNN binary
3200 with a section named "oc" that needs relocations, but that's
3201 the kind of ugly side-effects you get when detecting section
3202 types based on their names... In practice, this limitation is
3203 unlikely to bite. */
3204 if (strcmp (name, ".reloc") == 0)
3205 hdr->sh_type = SHT_PROGBITS;
3206
b34976b6 3207 return TRUE;
38701953
AM
3208}
3209
13ae64f3
JJ
3210/* Return the relocation value for @tpoff relocation
3211 if STT_TLS virtual address is ADDRESS. */
3212
3213static bfd_vma
eb4ff4d6 3214elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
13ae64f3 3215{
e1918d23 3216 struct elf_link_hash_table *htab = elf_hash_table (info);
7dc98aea
RO
3217 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
3218 bfd_vma static_tls_size;
13ae64f3 3219
e1918d23
AM
3220 /* If tls_sec is NULL, we should have signalled an error already. */
3221 if (htab->tls_sec == NULL)
6a30718d 3222 return 0;
7dc98aea
RO
3223
3224 /* Consider special static TLS alignment requirements. */
3225 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
3226 return static_tls_size + htab->tls_sec->vma - address;
13ae64f3
JJ
3227}
3228
252b5132
RH
3229/* Relocate an i386 ELF section. */
3230
b34976b6 3231static bfd_boolean
55fd94b0
AM
3232elf_i386_relocate_section (bfd *output_bfd,
3233 struct bfd_link_info *info,
3234 bfd *input_bfd,
3235 asection *input_section,
3236 bfd_byte *contents,
3237 Elf_Internal_Rela *relocs,
3238 Elf_Internal_Sym *local_syms,
3239 asection **local_sections)
252b5132 3240{
0afcef53 3241 struct elf_x86_link_hash_table *htab;
252b5132
RH
3242 Elf_Internal_Shdr *symtab_hdr;
3243 struct elf_link_hash_entry **sym_hashes;
3244 bfd_vma *local_got_offsets;
67a4f2b7 3245 bfd_vma *local_tlsdesc_gotents;
252b5132 3246 Elf_Internal_Rela *rel;
60f2e42e 3247 Elf_Internal_Rela *wrel;
252b5132 3248 Elf_Internal_Rela *relend;
3348747a 3249 bfd_boolean is_vxworks_tls;
25e762b9 3250 unsigned plt_entry_size;
252b5132 3251
0ffa91dd 3252 BFD_ASSERT (is_i386_elf (input_bfd));
6bbec505 3253
c337a162
L
3254 /* Skip if check_relocs failed. */
3255 if (input_section->check_relocs_failed)
3256 return FALSE;
3257
0afcef53 3258 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6
NC
3259 if (htab == NULL)
3260 return FALSE;
0ffa91dd 3261 symtab_hdr = &elf_symtab_hdr (input_bfd);
252b5132
RH
3262 sym_hashes = elf_sym_hashes (input_bfd);
3263 local_got_offsets = elf_local_got_offsets (input_bfd);
0afcef53 3264 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
3348747a
NS
3265 /* We have to handle relocations in vxworks .tls_vars sections
3266 specially, because the dynamic loader is 'weird'. */
f604c2a2
L
3267 is_vxworks_tls = ((get_elf_i386_backend_data (output_bfd)->os
3268 == is_vxworks)
0e1862bb 3269 && bfd_link_pic (info)
3348747a
NS
3270 && !strcmp (input_section->output_section->name,
3271 ".tls_vars"));
252b5132 3272
0afcef53 3273 _bfd_x86_elf_set_tls_module_base (info);
9f03412a 3274
765e526c 3275 plt_entry_size = htab->plt.plt_entry_size;
25e762b9 3276
60f2e42e 3277 rel = wrel = relocs;
252b5132 3278 relend = relocs + input_section->reloc_count;
60f2e42e 3279 for (; rel < relend; wrel++, rel++)
252b5132 3280 {
13ae64f3 3281 unsigned int r_type;
252b5132
RH
3282 reloc_howto_type *howto;
3283 unsigned long r_symndx;
3284 struct elf_link_hash_entry *h;
0afcef53 3285 struct elf_x86_link_hash_entry *eh;
252b5132
RH
3286 Elf_Internal_Sym *sym;
3287 asection *sec;
dd7e64d4 3288 bfd_vma off, offplt, plt_offset;
252b5132 3289 bfd_vma relocation;
b34976b6 3290 bfd_boolean unresolved_reloc;
252b5132 3291 bfd_reloc_status_type r;
1b452ec6 3292 unsigned int indx;
13ae64f3 3293 int tls_type;
1788fc08 3294 bfd_vma st_size;
dd7e64d4 3295 asection *resolved_plt;
aec6b87e 3296 bfd_boolean resolved_to_zero;
e133d005 3297 bfd_boolean relative_reloc;
252b5132
RH
3298
3299 r_type = ELF32_R_TYPE (rel->r_info);
55fd94b0
AM
3300 if (r_type == R_386_GNU_VTINHERIT
3301 || r_type == R_386_GNU_VTENTRY)
18954b29
L
3302 {
3303 if (wrel != rel)
3304 *wrel = *rel;
3305 continue;
3306 }
dc47f327 3307
55fd94b0 3308 if ((indx = r_type) >= R_386_standard
13ae64f3
JJ
3309 && ((indx = r_type - R_386_ext_offset) - R_386_standard
3310 >= R_386_ext - R_386_standard)
3311 && ((indx = r_type - R_386_tls_offset) - R_386_ext
c74be520 3312 >= R_386_ext2 - R_386_ext))
47aeb64c
NC
3313 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
3314
1b452ec6 3315 howto = elf_howto_table + indx;
252b5132
RH
3316
3317 r_symndx = ELF32_R_SYM (rel->r_info);
252b5132
RH
3318 h = NULL;
3319 sym = NULL;
3320 sec = NULL;
b34976b6 3321 unresolved_reloc = FALSE;
252b5132
RH
3322 if (r_symndx < symtab_hdr->sh_info)
3323 {
3324 sym = local_syms + r_symndx;
3325 sec = local_sections[r_symndx];
3326 relocation = (sec->output_section->vma
3327 + sec->output_offset
3328 + sym->st_value);
1788fc08 3329 st_size = sym->st_size;
ab96bf03
AM
3330
3331 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
3332 && ((sec->flags & SEC_MERGE) != 0
0e1862bb 3333 || (bfd_link_relocatable (info)
ab96bf03 3334 && sec->output_offset != 0)))
f8df10f4 3335 {
f8df10f4 3336 bfd_vma addend;
4a335f3d 3337 bfd_byte *where = contents + rel->r_offset;
f8df10f4 3338
4a335f3d 3339 switch (howto->size)
f8df10f4 3340 {
4a335f3d
AM
3341 case 0:
3342 addend = bfd_get_8 (input_bfd, where);
3343 if (howto->pc_relative)
3344 {
3345 addend = (addend ^ 0x80) - 0x80;
3346 addend += 1;
3347 }
3348 break;
3349 case 1:
3350 addend = bfd_get_16 (input_bfd, where);
3351 if (howto->pc_relative)
3352 {
3353 addend = (addend ^ 0x8000) - 0x8000;
3354 addend += 2;
3355 }
3356 break;
3357 case 2:
3358 addend = bfd_get_32 (input_bfd, where);
3359 if (howto->pc_relative)
3360 {
3361 addend = (addend ^ 0x80000000) - 0x80000000;
3362 addend += 4;
3363 }
3364 break;
3365 default:
3366 abort ();
f8df10f4
JJ
3367 }
3368
0e1862bb 3369 if (bfd_link_relocatable (info))
ab96bf03
AM
3370 addend += sec->output_offset;
3371 else
3372 {
3373 asection *msec = sec;
3374 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
3375 addend);
3376 addend -= relocation;
3377 addend += msec->output_section->vma + msec->output_offset;
3378 }
4a335f3d
AM
3379
3380 switch (howto->size)
3381 {
3382 case 0:
16a10388 3383 /* FIXME: overflow checks. */
4a335f3d
AM
3384 if (howto->pc_relative)
3385 addend -= 1;
3386 bfd_put_8 (input_bfd, addend, where);
4a335f3d
AM
3387 break;
3388 case 1:
3389 if (howto->pc_relative)
3390 addend -= 2;
3391 bfd_put_16 (input_bfd, addend, where);
4a335f3d
AM
3392 break;
3393 case 2:
3394 if (howto->pc_relative)
3395 addend -= 4;
3396 bfd_put_32 (input_bfd, addend, where);
3397 break;
3398 }
f8df10f4 3399 }
0e1862bb 3400 else if (!bfd_link_relocatable (info)
1f85278f 3401 && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
c25bc9fc
L
3402 {
3403 /* Relocate against local STT_GNU_IFUNC symbol. */
0afcef53
L
3404 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd, rel,
3405 FALSE);
c25bc9fc
L
3406 if (h == NULL)
3407 abort ();
3408
23209a78 3409 /* Set STT_GNU_IFUNC symbol value. */
c25bc9fc
L
3410 h->root.u.def.value = sym->st_value;
3411 h->root.u.def.section = sec;
3412 }
252b5132
RH
3413 }
3414 else
3415 {
3d540e93 3416 bfd_boolean warned ATTRIBUTE_UNUSED;
62d887d4 3417 bfd_boolean ignored ATTRIBUTE_UNUSED;
ffb2e45b 3418
b2a8e766
AM
3419 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3420 r_symndx, symtab_hdr, sym_hashes,
3421 h, sec, relocation,
62d887d4 3422 unresolved_reloc, warned, ignored);
1788fc08 3423 st_size = h->size;
252b5132
RH
3424 }
3425
dbaa2011 3426 if (sec != NULL && discarded_section (sec))
60f2e42e
L
3427 {
3428 _bfd_clear_contents (howto, input_bfd, input_section,
3429 contents + rel->r_offset);
3430 wrel->r_offset = rel->r_offset;
3431 wrel->r_info = 0;
3432 wrel->r_addend = 0;
3433
3434 /* For ld -r, remove relocations in debug sections against
3435 sections defined in discarded sections. Not done for
3436 eh_frame editing code expects to be present. */
3437 if (bfd_link_relocatable (info)
3438 && (input_section->flags & SEC_DEBUGGING))
3439 wrel--;
3440
3441 continue;
3442 }
9635fe29 3443
0e1862bb 3444 if (bfd_link_relocatable (info))
2d5da473
L
3445 {
3446 if (wrel != rel)
3447 *wrel = *rel;
3448 continue;
3449 }
ab96bf03 3450
0afcef53 3451 eh = (struct elf_x86_link_hash_entry *) h;
ee2fdd6f 3452
cbe950e9
L
3453 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3454 it here if it is defined in a non-shared object. */
3455 if (h != NULL
3456 && h->type == STT_GNU_IFUNC
3457 && h->def_regular)
3458 {
ee2fdd6f 3459 asection *gotplt, *base_got;
cbe950e9 3460 bfd_vma plt_index;
4c544807 3461 const char *name;
cbe950e9 3462
97dc35c8
L
3463 if ((input_section->flags & SEC_ALLOC) == 0)
3464 {
3465 /* Dynamic relocs are not propagated for SEC_DEBUGGING
3466 sections because such sections are not SEC_ALLOC and
3467 thus ld.so will not process them. */
3468 if ((input_section->flags & SEC_DEBUGGING) != 0)
0eace210 3469 continue;
97dc35c8
L
3470 abort ();
3471 }
cbe950e9
L
3472
3473 /* STT_GNU_IFUNC symbol must go through PLT. */
6de2ae4a 3474 if (htab->elf.splt != NULL)
cbe950e9 3475 {
ee2fdd6f
L
3476 if (htab->plt_second != NULL)
3477 {
3478 resolved_plt = htab->plt_second;
3479 plt_offset = eh->plt_second.offset;
3480 }
3481 else
3482 {
3483 resolved_plt = htab->elf.splt;
3484 plt_offset = h->plt.offset;
3485 }
6de2ae4a 3486 gotplt = htab->elf.sgotplt;
cbe950e9
L
3487 }
3488 else
3489 {
ee2fdd6f
L
3490 resolved_plt = htab->elf.iplt;
3491 plt_offset = h->plt.offset;
6de2ae4a 3492 gotplt = htab->elf.igotplt;
cbe950e9
L
3493 }
3494
cbe950e9
L
3495 switch (r_type)
3496 {
3497 default:
233cc9c1 3498 break;
cbe950e9
L
3499
3500 case R_386_GOT32:
02a86693 3501 case R_386_GOT32X:
6de2ae4a 3502 base_got = htab->elf.sgot;
cbe950e9
L
3503 off = h->got.offset;
3504
7afd84dc 3505 if (base_got == NULL)
cbe950e9
L
3506 abort ();
3507
7afd84dc 3508 if (off == (bfd_vma) -1)
cbe950e9 3509 {
7afd84dc
L
3510 /* We can't use h->got.offset here to save state, or
3511 even just remember the offset, as finish_dynamic_symbol
3512 would use that as offset into .got. */
6bbec505 3513
233cc9c1
L
3514 if (h->plt.offset == (bfd_vma) -1)
3515 abort ();
3516
6de2ae4a 3517 if (htab->elf.splt != NULL)
7afd84dc 3518 {
f604c2a2 3519 plt_index = (h->plt.offset / plt_entry_size
765e526c 3520 - htab->plt.has_plt0);
7afd84dc 3521 off = (plt_index + 3) * 4;
6de2ae4a 3522 base_got = htab->elf.sgotplt;
7afd84dc 3523 }
cbe950e9
L
3524 else
3525 {
25e762b9 3526 plt_index = h->plt.offset / plt_entry_size;
7afd84dc 3527 off = plt_index * 4;
6de2ae4a 3528 base_got = htab->elf.igotplt;
7afd84dc
L
3529 }
3530
3531 if (h->dynindx == -1
3532 || h->forced_local
3533 || info->symbolic)
3534 {
3535 /* This references the local defitionion. We must
3536 initialize this entry in the global offset table.
3537 Since the offset must always be a multiple of 8,
3538 we use the least significant bit to record
3539 whether we have initialized it already.
3540
3541 When doing a dynamic link, we create a .rela.got
3542 relocation entry to initialize the value. This
3543 is done in the finish_dynamic_symbol routine. */
3544 if ((off & 1) != 0)
3545 off &= ~1;
3546 else
3547 {
3548 bfd_put_32 (output_bfd, relocation,
3549 base_got->contents + off);
3550 h->got.offset |= 1;
3551 }
cbe950e9 3552 }
cbe950e9 3553
7afd84dc 3554 relocation = off;
7afd84dc
L
3555 }
3556 else
ca8c86ef
L
3557 relocation = (base_got->output_section->vma
3558 + base_got->output_offset + off
3559 - gotplt->output_section->vma
3560 - gotplt->output_offset);
3561
2a568401
L
3562 if (rel->r_offset > 1
3563 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
3564 && *(contents + rel->r_offset - 2) != 0x8d)
712ec279
L
3565 {
3566 if (bfd_link_pic (info))
3567 goto disallow_got32;
3568
3569 /* Add the GOT base if there is no base register. */
3570 relocation += (gotplt->output_section->vma
3571 + gotplt->output_offset);
3572 }
3573 else if (htab->elf.splt == NULL)
3574 {
3575 /* Adjust for static executables. */
3576 relocation += gotplt->output_offset;
3577 }
0018b0a3 3578
cbe950e9 3579 goto do_relocation;
233cc9c1
L
3580 }
3581
3582 if (h->plt.offset == (bfd_vma) -1)
3583 {
3584 /* Handle static pointers of STT_GNU_IFUNC symbols. */
3585 if (r_type == R_386_32
3586 && (input_section->flags & SEC_CODE) == 0)
3587 goto do_ifunc_pointer;
3588 goto bad_ifunc_reloc;
3589 }
3590
ee2fdd6f
L
3591 relocation = (resolved_plt->output_section->vma
3592 + resolved_plt->output_offset + plt_offset);
233cc9c1
L
3593
3594 switch (r_type)
3595 {
3596 default:
3597bad_ifunc_reloc:
3598 if (h->root.root.string)
3599 name = h->root.root.string;
3600 else
3601 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3602 NULL);
4eca0228 3603 _bfd_error_handler
695344c0 3604 /* xgettext:c-format */
233cc9c1
L
3605 (_("%B: relocation %s against STT_GNU_IFUNC "
3606 "symbol `%s' isn't supported"), input_bfd,
3607 howto->name, name);
3608 bfd_set_error (bfd_error_bad_value);
3609 return FALSE;
3610
3611 case R_386_32:
3612 /* Generate dynamic relcoation only when there is a
3613 non-GOT reference in a shared object. */
3614 if ((bfd_link_pic (info) && h->non_got_ref)
3615 || h->plt.offset == (bfd_vma) -1)
3616 {
3617 Elf_Internal_Rela outrel;
3618 asection *sreloc;
3619 bfd_vma offset;
3620
3621do_ifunc_pointer:
3622 /* Need a dynamic relocation to get the real function
3623 adddress. */
3624 offset = _bfd_elf_section_offset (output_bfd,
3625 info,
3626 input_section,
3627 rel->r_offset);
3628 if (offset == (bfd_vma) -1
3629 || offset == (bfd_vma) -2)
3630 abort ();
3631
3632 outrel.r_offset = (input_section->output_section->vma
3633 + input_section->output_offset
3634 + offset);
3635
3636 if (h->dynindx == -1
3637 || h->forced_local
3638 || bfd_link_executable (info))
3639 {
6322e5c5
L
3640 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
3641 h->root.root.string,
3642 h->root.u.def.section->owner);
3643
233cc9c1
L
3644 /* This symbol is resolved locally. */
3645 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3646 bfd_put_32 (output_bfd,
3647 (h->root.u.def.value
3648 + h->root.u.def.section->output_section->vma
3649 + h->root.u.def.section->output_offset),
3650 contents + offset);
3651 }
3652 else
3653 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3654
3655 /* Dynamic relocations are stored in
3656 1. .rel.ifunc section in PIC object.
3657 2. .rel.got section in dynamic executable.
3658 3. .rel.iplt section in static executable. */
3659 if (bfd_link_pic (info))
3660 sreloc = htab->elf.irelifunc;
3661 else if (htab->elf.splt != NULL)
3662 sreloc = htab->elf.srelgot;
3663 else
3664 sreloc = htab->elf.irelplt;
3665 elf_append_rel (output_bfd, sreloc, &outrel);
3666
3667 /* If this reloc is against an external symbol, we
3668 do not want to fiddle with the addend. Otherwise,
3669 we need to include the symbol value so that it
3670 becomes an addend for the dynamic reloc. For an
3671 internal symbol, we have updated addend. */
3672 continue;
3673 }
3674 /* FALLTHROUGH */
3675 case R_386_PC32:
3676 case R_386_PLT32:
3677 goto do_relocation;
cbe950e9
L
3678
3679 case R_386_GOTOFF:
3680 relocation -= (gotplt->output_section->vma
3681 + gotplt->output_offset);
3682 goto do_relocation;
3683 }
3684 }
3685
aec6b87e 3686 resolved_to_zero = (eh != NULL
e62b9723 3687 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
0afcef53 3688 I386_ELF_DATA,
e62b9723
L
3689 eh->has_got_reloc,
3690 eh));
aec6b87e 3691
252b5132
RH
3692 switch (r_type)
3693 {
02a86693
L
3694 case R_386_GOT32X:
3695 /* Avoid optimizing _DYNAMIC since ld.so may use its
3696 link-time address. */
3697 if (h == htab->elf.hdynamic)
3698 goto r_386_got32;
3699
3700 if (bfd_link_pic (info))
3701 {
3702 /* It is OK to convert mov to lea and convert indirect
3703 branch to direct branch. It is OK to convert adc,
3704 add, and, cmp, or, sbb, sub, test, xor only when PIC
3705 is false. */
02e2aef8
L
3706 unsigned int opcode, addend;
3707 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
3708 if (addend != 0)
3709 goto r_386_got32;
3710 opcode = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
02a86693
L
3711 if (opcode != 0x8b && opcode != 0xff)
3712 goto r_386_got32;
3713 }
3714
3715 /* Resolve "mov GOT[(%reg)], %reg",
3716 "call/jmp *GOT[(%reg)]", "test %reg, foo@GOT[(%reg)]"
3717 and "binop foo@GOT[(%reg)], %reg". */
3718 if (h == NULL
3719 || (h->plt.offset == (bfd_vma) -1
3720 && h->got.offset == (bfd_vma) -1)
3721 || htab->elf.sgotplt == NULL)
3722 abort ();
3723
3724 offplt = (htab->elf.sgotplt->output_section->vma
3725 + htab->elf.sgotplt->output_offset);
3726
3727 /* It is relative to .got.plt section. */
3728 if (h->got.offset != (bfd_vma) -1)
32875eb1
L
3729 /* Use GOT entry. Mask off the least significant bit in
3730 GOT offset which may be set by R_386_GOT32 processing
3731 below. */
02a86693
L
3732 relocation = (htab->elf.sgot->output_section->vma
3733 + htab->elf.sgot->output_offset
32875eb1 3734 + (h->got.offset & ~1) - offplt);
02a86693
L
3735 else
3736 /* Use GOTPLT entry. */
f604c2a2 3737 relocation = (h->plt.offset / plt_entry_size
765e526c 3738 - htab->plt.has_plt0 + 3) * 4;
02a86693
L
3739
3740 if (!bfd_link_pic (info))
3741 {
3742 /* If not PIC, add the .got.plt section address for
a3718e9e 3743 baseless addressing. */
02a86693 3744 unsigned int modrm;
02e2aef8 3745 modrm = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
02a86693
L
3746 if ((modrm & 0xc7) == 0x5)
3747 relocation += offplt;
3748 }
3749
3750 unresolved_reloc = FALSE;
3751 break;
3752
252b5132 3753 case R_386_GOT32:
02a86693 3754r_386_got32:
252b5132
RH
3755 /* Relocation is to the entry for this symbol in the global
3756 offset table. */
6de2ae4a 3757 if (htab->elf.sgot == NULL)
ffb2e45b 3758 abort ();
252b5132 3759
e133d005 3760 relative_reloc = FALSE;
252b5132
RH
3761 if (h != NULL)
3762 {
b34976b6 3763 bfd_boolean dyn;
252b5132
RH
3764
3765 off = h->got.offset;
ebe50bae 3766 dyn = htab->elf.dynamic_sections_created;
0e1862bb
L
3767 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
3768 bfd_link_pic (info),
3769 h)
3770 || (bfd_link_pic (info)
586119b3 3771 && SYMBOL_REFERENCES_LOCAL (info, h))
ef5aade5
L
3772 || (ELF_ST_VISIBILITY (h->other)
3773 && h->root.type == bfd_link_hash_undefweak))
252b5132
RH
3774 {
3775 /* This is actually a static link, or it is a
3776 -Bsymbolic link and the symbol is defined
3777 locally, or the symbol was forced to be local
3778 because of a version file. We must initialize
3779 this entry in the global offset table. Since the
3780 offset must always be a multiple of 4, we use the
3781 least significant bit to record whether we have
3782 initialized it already.
3783
3784 When doing a dynamic link, we create a .rel.got
3785 relocation entry to initialize the value. This
3786 is done in the finish_dynamic_symbol routine. */
3787 if ((off & 1) != 0)
3788 off &= ~1;
3789 else
3790 {
3791 bfd_put_32 (output_bfd, relocation,
6de2ae4a 3792 htab->elf.sgot->contents + off);
252b5132 3793 h->got.offset |= 1;
e133d005
L
3794
3795 if (h->dynindx == -1
3796 && !h->forced_local
3797 && h->root.type != bfd_link_hash_undefweak
0dc9a308 3798 && bfd_link_pic (info))
e133d005
L
3799 {
3800 /* PR ld/21402: If this symbol isn't dynamic
0dc9a308 3801 in PIC, generate R_386_RELATIVE here. */
e133d005
L
3802 eh->no_finish_dynamic_symbol = 1;
3803 relative_reloc = TRUE;
3804 }
252b5132
RH
3805 }
3806 }
8c694914 3807 else
b34976b6 3808 unresolved_reloc = FALSE;
252b5132
RH
3809 }
3810 else
3811 {
ffb2e45b
AM
3812 if (local_got_offsets == NULL)
3813 abort ();
252b5132
RH
3814
3815 off = local_got_offsets[r_symndx];
3816
3817 /* The offset must always be a multiple of 4. We use
83be169b
AM
3818 the least significant bit to record whether we have
3819 already generated the necessary reloc. */
252b5132
RH
3820 if ((off & 1) != 0)
3821 off &= ~1;
3822 else
3823 {
6725bdbf 3824 bfd_put_32 (output_bfd, relocation,
6de2ae4a 3825 htab->elf.sgot->contents + off);
e133d005 3826 local_got_offsets[r_symndx] |= 1;
252b5132 3827
0e1862bb 3828 if (bfd_link_pic (info))
e133d005 3829 relative_reloc = TRUE;
252b5132 3830 }
252b5132
RH
3831 }
3832
e133d005
L
3833 if (relative_reloc)
3834 {
3835 asection *s;
3836 Elf_Internal_Rela outrel;
3837
3838 s = htab->elf.srelgot;
3839 if (s == NULL)
3840 abort ();
3841
3842 outrel.r_offset = (htab->elf.sgot->output_section->vma
3843 + htab->elf.sgot->output_offset
3844 + off);
3845 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3846 elf_append_rel (output_bfd, s, &outrel);
3847 }
3848
ffb2e45b
AM
3849 if (off >= (bfd_vma) -2)
3850 abort ();
3851
74d7f0aa
L
3852 relocation = (htab->elf.sgot->output_section->vma
3853 + htab->elf.sgot->output_offset + off);
2a568401
L
3854 if (rel->r_offset > 1
3855 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
3856 && *(contents + rel->r_offset - 2) != 0x8d)
74d7f0aa
L
3857 {
3858 if (bfd_link_pic (info))
3859 {
3860 /* For PIC, disallow R_386_GOT32 without a base
2a568401
L
3861 register, except for "lea foo@GOT, %reg", since
3862 we don't know what the GOT base is. */
74d7f0aa
L
3863 const char *name;
3864
712ec279 3865disallow_got32:
a5def14f 3866 if (h == NULL || h->root.root.string == NULL)
74d7f0aa
L
3867 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3868 NULL);
3869 else
3870 name = h->root.root.string;
3871
4eca0228 3872 _bfd_error_handler
695344c0 3873 /* xgettext:c-format */
63a5468a
AM
3874 (_("%B: direct GOT relocation %s against `%s'"
3875 " without base register can not be used"
3876 " when making a shared object"),
712ec279 3877 input_bfd, howto->name, name);
74d7f0aa
L
3878 bfd_set_error (bfd_error_bad_value);
3879 return FALSE;
3880 }
3881 }
3882 else
3883 {
3884 /* Subtract the .got.plt section address only with a base
3885 register. */
3886 relocation -= (htab->elf.sgotplt->output_section->vma
3887 + htab->elf.sgotplt->output_offset);
3888 }
3889
252b5132
RH
3890 break;
3891
3892 case R_386_GOTOFF:
3893 /* Relocation is relative to the start of the global offset
3894 table. */
3895
3d949995
L
3896 /* Check to make sure it isn't a protected function or data
3897 symbol for shared library since it may not be local when
3898 used as function address or with copy relocation. We also
e3c0e327 3899 need to make sure that a symbol is referenced locally. */
0e1862bb 3900 if (!bfd_link_executable (info) && h)
90f487df 3901 {
41bed6dd
L
3902 if (!h->def_regular)
3903 {
3904 const char *v;
3905
3906 switch (ELF_ST_VISIBILITY (h->other))
3907 {
3908 case STV_HIDDEN:
3909 v = _("hidden symbol");
3910 break;
3911 case STV_INTERNAL:
3912 v = _("internal symbol");
3913 break;
3914 case STV_PROTECTED:
3915 v = _("protected symbol");
3916 break;
3917 default:
3918 v = _("symbol");
3919 break;
3920 }
3921
4eca0228 3922 _bfd_error_handler
695344c0 3923 /* xgettext:c-format */
63a5468a
AM
3924 (_("%B: relocation R_386_GOTOFF against undefined %s"
3925 " `%s' can not be used when making a shared object"),
41bed6dd
L
3926 input_bfd, v, h->root.root.string);
3927 bfd_set_error (bfd_error_bad_value);
3928 return FALSE;
3929 }
d5597ebc 3930 else if (!SYMBOL_REFERENCES_LOCAL (info, h)
3d949995
L
3931 && (h->type == STT_FUNC
3932 || h->type == STT_OBJECT)
41bed6dd
L
3933 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3934 {
4eca0228 3935 _bfd_error_handler
695344c0 3936 /* xgettext:c-format */
63a5468a
AM
3937 (_("%B: relocation R_386_GOTOFF against protected %s"
3938 " `%s' can not be used when making a shared object"),
3d949995
L
3939 input_bfd,
3940 h->type == STT_FUNC ? "function" : "data",
3941 h->root.root.string);
41bed6dd
L
3942 bfd_set_error (bfd_error_bad_value);
3943 return FALSE;
3944 }
90f487df
L
3945 }
3946
8c37241b
JJ
3947 /* Note that sgot is not involved in this
3948 calculation. We always want the start of .got.plt. If we
3949 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
252b5132
RH
3950 permitted by the ABI, we might have to change this
3951 calculation. */
6de2ae4a
L
3952 relocation -= htab->elf.sgotplt->output_section->vma
3953 + htab->elf.sgotplt->output_offset;
252b5132
RH
3954 break;
3955
3956 case R_386_GOTPC:
3957 /* Use global offset table as symbol value. */
6de2ae4a
L
3958 relocation = htab->elf.sgotplt->output_section->vma
3959 + htab->elf.sgotplt->output_offset;
b34976b6 3960 unresolved_reloc = FALSE;
252b5132
RH
3961 break;
3962
3963 case R_386_PLT32:
3964 /* Relocation is to the entry for this symbol in the
3965 procedure linkage table. */
3966
dd5724d5 3967 /* Resolve a PLT32 reloc against a local symbol directly,
83be169b 3968 without using the procedure linkage table. */
252b5132
RH
3969 if (h == NULL)
3970 break;
3971
dd7e64d4
L
3972 if ((h->plt.offset == (bfd_vma) -1
3973 && eh->plt_got.offset == (bfd_vma) -1)
6de2ae4a 3974 || htab->elf.splt == NULL)
252b5132
RH
3975 {
3976 /* We didn't make a PLT entry for this symbol. This
83be169b
AM
3977 happens when statically linking PIC code, or when
3978 using -Bsymbolic. */
252b5132
RH
3979 break;
3980 }
3981
dd7e64d4
L
3982 if (h->plt.offset != (bfd_vma) -1)
3983 {
ee2fdd6f
L
3984 if (htab->plt_second != NULL)
3985 {
3986 resolved_plt = htab->plt_second;
3987 plt_offset = eh->plt_second.offset;
3988 }
3989 else
3990 {
3991 resolved_plt = htab->elf.splt;
3992 plt_offset = h->plt.offset;
3993 }
dd7e64d4
L
3994 }
3995 else
3996 {
3997 resolved_plt = htab->plt_got;
3998 plt_offset = eh->plt_got.offset;
3999 }
4000
4001 relocation = (resolved_plt->output_section->vma
4002 + resolved_plt->output_offset
4003 + plt_offset);
b34976b6 4004 unresolved_reloc = FALSE;
252b5132
RH
4005 break;
4006
1788fc08 4007 case R_386_SIZE32:
1788fc08
L
4008 /* Set to symbol size. */
4009 relocation = st_size;
4010 /* Fall through. */
4011
252b5132
RH
4012 case R_386_32:
4013 case R_386_PC32:
3348747a
NS
4014 if ((input_section->flags & SEC_ALLOC) == 0
4015 || is_vxworks_tls)
ec338859
AM
4016 break;
4017
aec6b87e
L
4018 /* Copy dynamic function pointer relocations. Don't generate
4019 dynamic relocations against resolved undefined weak symbols
4020 in PIE, except for R_386_PC32. */
0e1862bb 4021 if ((bfd_link_pic (info)
ef5aade5 4022 && (h == NULL
aec6b87e
L
4023 || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4024 && (!resolved_to_zero
4025 || r_type == R_386_PC32))
4026 || h->root.type != bfd_link_hash_undefweak))
1788fc08 4027 && ((r_type != R_386_PC32 && r_type != R_386_SIZE32)
f6c52c13 4028 || !SYMBOL_CALLS_LOCAL (info, h)))
a23b6845 4029 || (ELIMINATE_COPY_RELOCS
0e1862bb 4030 && !bfd_link_pic (info)
12d0ee4a
AM
4031 && h != NULL
4032 && h->dynindx != -1
aec6b87e
L
4033 && (!h->non_got_ref
4034 || eh->func_pointer_refcount > 0
4035 || (h->root.type == bfd_link_hash_undefweak
4036 && !resolved_to_zero))
bae420ef
L
4037 && ((h->def_dynamic && !h->def_regular)
4038 /* Undefined weak symbol is bound locally when
4039 PIC is false. */
4040 || h->root.type == bfd_link_hash_undefweak)))
252b5132 4041 {
947216bf 4042 Elf_Internal_Rela outrel;
b34976b6 4043 bfd_boolean skip, relocate;
0c715baa 4044 asection *sreloc;
252b5132
RH
4045
4046 /* When generating a shared object, these relocations
4047 are copied into the output file to be resolved at run
4048 time. */
4049
b34976b6
AM
4050 skip = FALSE;
4051 relocate = FALSE;
252b5132 4052
c629eae0
JJ
4053 outrel.r_offset =
4054 _bfd_elf_section_offset (output_bfd, info, input_section,
4055 rel->r_offset);
4056 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 4057 skip = TRUE;
0bb2d96a 4058 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 4059 skip = TRUE, relocate = TRUE;
252b5132
RH
4060 outrel.r_offset += (input_section->output_section->vma
4061 + input_section->output_offset);
4062
4063 if (skip)
0bb2d96a 4064 memset (&outrel, 0, sizeof outrel);
5a15f56f
AM
4065 else if (h != NULL
4066 && h->dynindx != -1
4067 && (r_type == R_386_PC32
4e0c91e4
L
4068 || !(bfd_link_executable (info)
4069 || SYMBOLIC_BIND (info, h))
f5385ebf 4070 || !h->def_regular))
0bb2d96a 4071 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
252b5132
RH
4072 else
4073 {
5a15f56f 4074 /* This symbol is local, or marked to become local. */
b34976b6 4075 relocate = TRUE;
5a15f56f 4076 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
252b5132
RH
4077 }
4078
cbe950e9 4079 sreloc = elf_section_data (input_section)->sreloc;
e7c33416 4080
62d78908
L
4081 if (sreloc == NULL || sreloc->contents == NULL)
4082 {
4083 r = bfd_reloc_notsupported;
4084 goto check_relocation_error;
4085 }
0c715baa 4086
59d6ffb2 4087 elf_append_rel (output_bfd, sreloc, &outrel);
252b5132
RH
4088
4089 /* If this reloc is against an external symbol, we do
4090 not want to fiddle with the addend. Otherwise, we
4091 need to include the symbol value so that it becomes
4092 an addend for the dynamic reloc. */
0f88be7a 4093 if (! relocate)
252b5132
RH
4094 continue;
4095 }
252b5132
RH
4096 break;
4097
37e55690 4098 case R_386_TLS_IE:
0e1862bb 4099 if (!bfd_link_executable (info))
37e55690 4100 {
947216bf 4101 Elf_Internal_Rela outrel;
37e55690 4102 asection *sreloc;
37e55690
JJ
4103
4104 outrel.r_offset = rel->r_offset
4105 + input_section->output_section->vma
4106 + input_section->output_offset;
4107 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4108 sreloc = elf_section_data (input_section)->sreloc;
4109 if (sreloc == NULL)
4110 abort ();
59d6ffb2 4111 elf_append_rel (output_bfd, sreloc, &outrel);
37e55690
JJ
4112 }
4113 /* Fall through */
4114
13ae64f3 4115 case R_386_TLS_GD:
67a4f2b7
AO
4116 case R_386_TLS_GOTDESC:
4117 case R_386_TLS_DESC_CALL:
13ae64f3 4118 case R_386_TLS_IE_32:
37e55690 4119 case R_386_TLS_GOTIE:
13ae64f3
JJ
4120 tls_type = GOT_UNKNOWN;
4121 if (h == NULL && local_got_offsets)
0afcef53 4122 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
13ae64f3 4123 else if (h != NULL)
0afcef53 4124 tls_type = elf_x86_hash_entry(h)->tls_type;
37e55690
JJ
4125 if (tls_type == GOT_TLS_IE)
4126 tls_type = GOT_TLS_IE_NEG;
142411ca
L
4127
4128 if (! elf_i386_tls_transition (info, input_bfd,
4129 input_section, contents,
4130 symtab_hdr, sym_hashes,
4131 &r_type, tls_type, rel,
bedfd056 4132 relend, h, r_symndx, TRUE))
142411ca 4133 return FALSE;
13ae64f3
JJ
4134
4135 if (r_type == R_386_TLS_LE_32)
4136 {
82e51918 4137 BFD_ASSERT (! unresolved_reloc);
13ae64f3
JJ
4138 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
4139 {
142411ca 4140 unsigned int type;
13ae64f3
JJ
4141 bfd_vma roff;
4142
4143 /* GD->LE transition. */
6eaa7fb5 4144 type = *(contents + rel->r_offset - 2);
13ae64f3
JJ
4145 if (type == 0x04)
4146 {
6eaa7fb5
L
4147 /* Change
4148 leal foo@tlsgd(,%ebx,1), %eax
4149 call ___tls_get_addr@PLT
4150 into:
4151 movl %gs:0, %eax
4152 subl $foo@tpoff, %eax
13ae64f3 4153 (6 byte form of subl). */
142411ca 4154 roff = rel->r_offset + 5;
13ae64f3
JJ
4155 }
4156 else
4157 {
6eaa7fb5
L
4158 /* Change
4159 leal foo@tlsgd(%ebx), %eax
4160 call ___tls_get_addr@PLT
4161 nop
4162 or
4163 leal foo@tlsgd(%reg), %eax
4164 call *___tls_get_addr@GOT(%reg)
4165 which may be converted to
4166 addr32 call ___tls_get_addr
4167 into:
4168 movl %gs:0, %eax; subl $foo@tpoff, %eax
142411ca 4169 (6 byte form of subl). */
142411ca 4170 roff = rel->r_offset + 6;
13ae64f3 4171 }
6eaa7fb5
L
4172 memcpy (contents + roff - 8,
4173 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
eb4ff4d6 4174 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
13ae64f3 4175 contents + roff);
6eaa7fb5 4176 /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X. */
13ae64f3 4177 rel++;
60f2e42e 4178 wrel++;
13ae64f3
JJ
4179 continue;
4180 }
67a4f2b7
AO
4181 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
4182 {
4183 /* GDesc -> LE transition.
4184 It's originally something like:
4185 leal x@tlsdesc(%ebx), %eax
4186
4187 leal x@ntpoff, %eax
4188
4189 Registers other than %eax may be set up here. */
4190
142411ca 4191 unsigned int val;
67a4f2b7
AO
4192 bfd_vma roff;
4193
67a4f2b7 4194 roff = rel->r_offset;
67a4f2b7 4195 val = bfd_get_8 (input_bfd, contents + roff - 1);
67a4f2b7
AO
4196
4197 /* Now modify the instruction as appropriate. */
4198 /* aoliva FIXME: remove the above and xor the byte
4199 below with 0x86. */
4200 bfd_put_8 (output_bfd, val ^ 0x86,
4201 contents + roff - 1);
eb4ff4d6 4202 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
67a4f2b7
AO
4203 contents + roff);
4204 continue;
4205 }
4206 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
4207 {
4208 /* GDesc -> LE transition.
4209 It's originally:
4210 call *(%eax)
4211 Turn it into:
142411ca 4212 xchg %ax,%ax */
67a4f2b7 4213
67a4f2b7 4214 bfd_vma roff;
6bbec505 4215
67a4f2b7 4216 roff = rel->r_offset;
10efb593 4217 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
4218 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4219 continue;
4220 }
37e55690 4221 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
13ae64f3 4222 {
142411ca 4223 unsigned int val;
13ae64f3
JJ
4224
4225 /* IE->LE transition:
37e55690
JJ
4226 Originally it can be one of:
4227 movl foo, %eax
4228 movl foo, %reg
4229 addl foo, %reg
4230 We change it into:
4231 movl $foo, %eax
4232 movl $foo, %reg
4233 addl $foo, %reg. */
37e55690 4234 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
37e55690
JJ
4235 if (val == 0xa1)
4236 {
4237 /* movl foo, %eax. */
55fd94b0
AM
4238 bfd_put_8 (output_bfd, 0xb8,
4239 contents + rel->r_offset - 1);
37e55690 4240 }
299bf759 4241 else
37e55690 4242 {
142411ca
L
4243 unsigned int type;
4244
55fd94b0
AM
4245 type = bfd_get_8 (input_bfd,
4246 contents + rel->r_offset - 2);
299bf759 4247 switch (type)
26e41594 4248 {
299bf759
L
4249 case 0x8b:
4250 /* movl */
299bf759
L
4251 bfd_put_8 (output_bfd, 0xc7,
4252 contents + rel->r_offset - 2);
4253 bfd_put_8 (output_bfd,
4254 0xc0 | ((val >> 3) & 7),
4255 contents + rel->r_offset - 1);
4256 break;
4257 case 0x03:
4258 /* addl */
299bf759
L
4259 bfd_put_8 (output_bfd, 0x81,
4260 contents + rel->r_offset - 2);
4261 bfd_put_8 (output_bfd,
4262 0xc0 | ((val >> 3) & 7),
4263 contents + rel->r_offset - 1);
4264 break;
4265 default:
4266 BFD_FAIL ();
4267 break;
26e41594 4268 }
37e55690 4269 }
eb4ff4d6 4270 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
37e55690
JJ
4271 contents + rel->r_offset);
4272 continue;
4273 }
4274 else
4275 {
4276 unsigned int val, type;
4277
4278 /* {IE_32,GOTIE}->LE transition:
4279 Originally it can be one of:
13ae64f3 4280 subl foo(%reg1), %reg2
13ae64f3 4281 movl foo(%reg1), %reg2
37e55690 4282 addl foo(%reg1), %reg2
13ae64f3
JJ
4283 We change it into:
4284 subl $foo, %reg2
37e55690
JJ
4285 movl $foo, %reg2 (6 byte form)
4286 addl $foo, %reg2. */
13ae64f3
JJ
4287 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
4288 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
13ae64f3
JJ
4289 if (type == 0x8b)
4290 {
4291 /* movl */
13ae64f3
JJ
4292 bfd_put_8 (output_bfd, 0xc7,
4293 contents + rel->r_offset - 2);
4294 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4295 contents + rel->r_offset - 1);
4296 }
4297 else if (type == 0x2b)
4298 {
4299 /* subl */
13ae64f3
JJ
4300 bfd_put_8 (output_bfd, 0x81,
4301 contents + rel->r_offset - 2);
4302 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
4303 contents + rel->r_offset - 1);
4304 }
37e55690
JJ
4305 else if (type == 0x03)
4306 {
4307 /* addl */
4308 bfd_put_8 (output_bfd, 0x81,
4309 contents + rel->r_offset - 2);
4310 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4311 contents + rel->r_offset - 1);
4312 }
13ae64f3
JJ
4313 else
4314 BFD_FAIL ();
37e55690 4315 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
eb4ff4d6 4316 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
37e55690
JJ
4317 contents + rel->r_offset);
4318 else
eb4ff4d6 4319 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
37e55690 4320 contents + rel->r_offset);
13ae64f3
JJ
4321 continue;
4322 }
4323 }
4324
6de2ae4a 4325 if (htab->elf.sgot == NULL)
13ae64f3
JJ
4326 abort ();
4327
4328 if (h != NULL)
67a4f2b7
AO
4329 {
4330 off = h->got.offset;
0afcef53 4331 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
67a4f2b7 4332 }
13ae64f3
JJ
4333 else
4334 {
4335 if (local_got_offsets == NULL)
4336 abort ();
4337
4338 off = local_got_offsets[r_symndx];
67a4f2b7 4339 offplt = local_tlsdesc_gotents[r_symndx];
13ae64f3
JJ
4340 }
4341
4342 if ((off & 1) != 0)
4343 off &= ~1;
26e41594 4344 else
13ae64f3 4345 {
947216bf 4346 Elf_Internal_Rela outrel;
91d6fa6a 4347 int dr_type;
67a4f2b7 4348 asection *sreloc;
13ae64f3 4349
6de2ae4a 4350 if (htab->elf.srelgot == NULL)
13ae64f3
JJ
4351 abort ();
4352
67a4f2b7
AO
4353 indx = h && h->dynindx != -1 ? h->dynindx : 0;
4354
4355 if (GOT_TLS_GDESC_P (tls_type))
4356 {
59d6ffb2 4357 bfd_byte *loc;
67a4f2b7
AO
4358 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
4359 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
6de2ae4a
L
4360 <= htab->elf.sgotplt->size);
4361 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
4362 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
4363 + offplt
4364 + htab->sgotplt_jump_table_size);
6de2ae4a 4365 sreloc = htab->elf.srelplt;
67a4f2b7 4366 loc = sreloc->contents;
765e526c 4367 loc += (htab->next_tls_desc_index++
5ae0bfb6 4368 * sizeof (Elf32_External_Rel));
67a4f2b7 4369 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
c3b02097 4370 <= sreloc->contents + sreloc->size);
67a4f2b7
AO
4371 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
4372 if (indx == 0)
4373 {
4374 BFD_ASSERT (! unresolved_reloc);
4375 bfd_put_32 (output_bfd,
0afcef53 4376 relocation - _bfd_x86_elf_dtpoff_base (info),
6de2ae4a 4377 htab->elf.sgotplt->contents + offplt
67a4f2b7
AO
4378 + htab->sgotplt_jump_table_size + 4);
4379 }
4380 else
4381 {
4382 bfd_put_32 (output_bfd, 0,
6de2ae4a 4383 htab->elf.sgotplt->contents + offplt
67a4f2b7
AO
4384 + htab->sgotplt_jump_table_size + 4);
4385 }
4386 }
4387
6de2ae4a 4388 sreloc = htab->elf.srelgot;
67a4f2b7 4389
6de2ae4a
L
4390 outrel.r_offset = (htab->elf.sgot->output_section->vma
4391 + htab->elf.sgot->output_offset + off);
13ae64f3 4392
67a4f2b7 4393 if (GOT_TLS_GD_P (tls_type))
13ae64f3 4394 dr_type = R_386_TLS_DTPMOD32;
67a4f2b7
AO
4395 else if (GOT_TLS_GDESC_P (tls_type))
4396 goto dr_done;
37e55690
JJ
4397 else if (tls_type == GOT_TLS_IE_POS)
4398 dr_type = R_386_TLS_TPOFF;
13ae64f3
JJ
4399 else
4400 dr_type = R_386_TLS_TPOFF32;
67a4f2b7 4401
37e55690 4402 if (dr_type == R_386_TLS_TPOFF && indx == 0)
eb4ff4d6 4403 bfd_put_32 (output_bfd,
0afcef53 4404 relocation - _bfd_x86_elf_dtpoff_base (info),
6de2ae4a 4405 htab->elf.sgot->contents + off);
37e55690 4406 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
23209a78 4407 bfd_put_32 (output_bfd,
0afcef53 4408 _bfd_x86_elf_dtpoff_base (info) - relocation,
6de2ae4a 4409 htab->elf.sgot->contents + off);
67a4f2b7 4410 else if (dr_type != R_386_TLS_DESC)
c366c25e 4411 bfd_put_32 (output_bfd, 0,
6de2ae4a 4412 htab->elf.sgot->contents + off);
13ae64f3 4413 outrel.r_info = ELF32_R_INFO (indx, dr_type);
67a4f2b7 4414
59d6ffb2 4415 elf_append_rel (output_bfd, sreloc, &outrel);
13ae64f3 4416
67a4f2b7 4417 if (GOT_TLS_GD_P (tls_type))
13ae64f3
JJ
4418 {
4419 if (indx == 0)
4420 {
82e51918 4421 BFD_ASSERT (! unresolved_reloc);
13ae64f3 4422 bfd_put_32 (output_bfd,
0afcef53 4423 relocation - _bfd_x86_elf_dtpoff_base (info),
6de2ae4a 4424 htab->elf.sgot->contents + off + 4);
13ae64f3
JJ
4425 }
4426 else
4427 {
4428 bfd_put_32 (output_bfd, 0,
6de2ae4a 4429 htab->elf.sgot->contents + off + 4);
13ae64f3
JJ
4430 outrel.r_info = ELF32_R_INFO (indx,
4431 R_386_TLS_DTPOFF32);
4432 outrel.r_offset += 4;
59d6ffb2 4433 elf_append_rel (output_bfd, sreloc, &outrel);
13ae64f3
JJ
4434 }
4435 }
37e55690
JJ
4436 else if (tls_type == GOT_TLS_IE_BOTH)
4437 {
4438 bfd_put_32 (output_bfd,
eb4ff4d6 4439 (indx == 0
0afcef53 4440 ? relocation - _bfd_x86_elf_dtpoff_base (info)
eb4ff4d6 4441 : 0),
6de2ae4a 4442 htab->elf.sgot->contents + off + 4);
37e55690
JJ
4443 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
4444 outrel.r_offset += 4;
59d6ffb2 4445 elf_append_rel (output_bfd, sreloc, &outrel);
37e55690 4446 }
13ae64f3 4447
67a4f2b7 4448 dr_done:
13ae64f3
JJ
4449 if (h != NULL)
4450 h->got.offset |= 1;
4451 else
4452 local_got_offsets[r_symndx] |= 1;
4453 }
4454
67a4f2b7
AO
4455 if (off >= (bfd_vma) -2
4456 && ! GOT_TLS_GDESC_P (tls_type))
13ae64f3 4457 abort ();
67a4f2b7
AO
4458 if (r_type == R_386_TLS_GOTDESC
4459 || r_type == R_386_TLS_DESC_CALL)
4460 {
4461 relocation = htab->sgotplt_jump_table_size + offplt;
4462 unresolved_reloc = FALSE;
4463 }
4464 else if (r_type == ELF32_R_TYPE (rel->r_info))
13ae64f3 4465 {
6de2ae4a
L
4466 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
4467 + htab->elf.sgotplt->output_offset;
4468 relocation = htab->elf.sgot->output_section->vma
4469 + htab->elf.sgot->output_offset + off - g_o_t;
37e55690
JJ
4470 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
4471 && tls_type == GOT_TLS_IE_BOTH)
4472 relocation += 4;
4473 if (r_type == R_386_TLS_IE)
8c37241b 4474 relocation += g_o_t;
b34976b6 4475 unresolved_reloc = FALSE;
13ae64f3 4476 }
67a4f2b7 4477 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
13ae64f3
JJ
4478 {
4479 unsigned int val, type;
4480 bfd_vma roff;
4481
4482 /* GD->IE transition. */
6eaa7fb5
L
4483 type = *(contents + rel->r_offset - 2);
4484 val = *(contents + rel->r_offset - 1);
13ae64f3
JJ
4485 if (type == 0x04)
4486 {
6eaa7fb5
L
4487 /* Change
4488 leal foo@tlsgd(,%ebx,1), %eax
4489 call ___tls_get_addr@PLT
4490 into:
4491 movl %gs:0, %eax
4492 subl $foo@gottpoff(%ebx), %eax. */
13ae64f3 4493 val >>= 3;
142411ca 4494 roff = rel->r_offset - 3;
13ae64f3
JJ
4495 }
4496 else
4497 {
6eaa7fb5
L
4498 /* Change
4499 leal foo@tlsgd(%ebx), %eax
4500 call ___tls_get_addr@PLT
4501 nop
4502 or
4503 leal foo@tlsgd(%reg), %eax
4504 call *___tls_get_addr@GOT(%reg)
4505 which may be converted to
4506 addr32 call ___tls_get_addr
4507 into:
4508 movl %gs:0, %eax;
4509 subl $foo@gottpoff(%reg), %eax. */
13ae64f3
JJ
4510 roff = rel->r_offset - 2;
4511 }
4512 memcpy (contents + roff,
4513 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
4514 contents[roff + 7] = 0x80 | (val & 7);
37e55690
JJ
4515 /* If foo is used only with foo@gotntpoff(%reg) and
4516 foo@indntpoff, but not with foo@gottpoff(%reg), change
4517 subl $foo@gottpoff(%reg), %eax
4518 into:
4519 addl $foo@gotntpoff(%reg), %eax. */
ebcfb3c0
JJ
4520 if (tls_type == GOT_TLS_IE_POS)
4521 contents[roff + 6] = 0x03;
8c37241b 4522 bfd_put_32 (output_bfd,
6de2ae4a
L
4523 htab->elf.sgot->output_section->vma
4524 + htab->elf.sgot->output_offset + off
4525 - htab->elf.sgotplt->output_section->vma
4526 - htab->elf.sgotplt->output_offset,
13ae64f3 4527 contents + roff + 8);
6eaa7fb5 4528 /* Skip R_386_PLT32 and R_386_GOT32X. */
13ae64f3 4529 rel++;
60f2e42e 4530 wrel++;
13ae64f3
JJ
4531 continue;
4532 }
67a4f2b7
AO
4533 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
4534 {
4535 /* GDesc -> IE transition.
4536 It's originally something like:
4537 leal x@tlsdesc(%ebx), %eax
4538
4539 Change it to:
142411ca 4540 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
67a4f2b7
AO
4541 or:
4542 movl x@gottpoff(%ebx), %eax # before negl %eax
4543
4544 Registers other than %eax may be set up here. */
4545
67a4f2b7
AO
4546 bfd_vma roff;
4547
4548 /* First, make sure it's a leal adding ebx to a 32-bit
4549 offset into any register, although it's probably
4550 almost always going to be eax. */
4551 roff = rel->r_offset;
67a4f2b7
AO
4552
4553 /* Now modify the instruction as appropriate. */
4554 /* To turn a leal into a movl in the form we use it, it
4555 suffices to change the first byte from 0x8d to 0x8b.
4556 aoliva FIXME: should we decide to keep the leal, all
4557 we have to do is remove the statement below, and
4558 adjust the relaxation of R_386_TLS_DESC_CALL. */
4559 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
4560
4561 if (tls_type == GOT_TLS_IE_BOTH)
4562 off += 4;
4563
4564 bfd_put_32 (output_bfd,
6de2ae4a
L
4565 htab->elf.sgot->output_section->vma
4566 + htab->elf.sgot->output_offset + off
4567 - htab->elf.sgotplt->output_section->vma
4568 - htab->elf.sgotplt->output_offset,
67a4f2b7
AO
4569 contents + roff);
4570 continue;
4571 }
4572 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
4573 {
4574 /* GDesc -> IE transition.
4575 It's originally:
4576 call *(%eax)
4577
4578 Change it to:
142411ca 4579 xchg %ax,%ax
67a4f2b7
AO
4580 or
4581 negl %eax
4582 depending on how we transformed the TLS_GOTDESC above.
4583 */
4584
67a4f2b7
AO
4585 bfd_vma roff;
4586
67a4f2b7 4587 roff = rel->r_offset;
67a4f2b7
AO
4588
4589 /* Now modify the instruction as appropriate. */
4590 if (tls_type != GOT_TLS_IE_NEG)
4591 {
10efb593
L
4592 /* xchg %ax,%ax */
4593 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
4594 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4595 }
4596 else
4597 {
4598 /* negl %eax */
4599 bfd_put_8 (output_bfd, 0xf7, contents + roff);
4600 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
4601 }
4602
4603 continue;
4604 }
4605 else
4606 BFD_ASSERT (FALSE);
13ae64f3
JJ
4607 break;
4608
4609 case R_386_TLS_LDM:
142411ca
L
4610 if (! elf_i386_tls_transition (info, input_bfd,
4611 input_section, contents,
4612 symtab_hdr, sym_hashes,
4613 &r_type, GOT_UNKNOWN, rel,
bedfd056 4614 relend, h, r_symndx, TRUE))
142411ca 4615 return FALSE;
13ae64f3 4616
142411ca
L
4617 if (r_type != R_386_TLS_LDM)
4618 {
6eaa7fb5
L
4619 /* LD->LE transition. Change
4620 leal foo@tlsldm(%ebx) %eax
4621 call ___tls_get_addr@PLT
4622 into:
4623 movl %gs:0, %eax
4624 nop
4625 leal 0(%esi,1), %esi
4626 or change
4627 leal foo@tlsldm(%reg) %eax
4628 call *___tls_get_addr@GOT(%reg)
4629 which may be converted to
4630 addr32 call ___tls_get_addr
4631 into:
4632 movl %gs:0, %eax
4633 leal 0(%esi), %esi */
142411ca 4634 BFD_ASSERT (r_type == R_386_TLS_LE_32);
6eaa7fb5
L
4635 if (*(contents + rel->r_offset + 4) == 0xff
4636 || *(contents + rel->r_offset + 4) == 0x67)
4637 memcpy (contents + rel->r_offset - 2,
4638 "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
4639 else
4640 memcpy (contents + rel->r_offset - 2,
4641 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
a3fadc9a 4642 /* Skip R_386_PC32/R_386_PLT32. */
13ae64f3 4643 rel++;
60f2e42e 4644 wrel++;
13ae64f3
JJ
4645 continue;
4646 }
4647
6de2ae4a 4648 if (htab->elf.sgot == NULL)
13ae64f3
JJ
4649 abort ();
4650
0afcef53 4651 off = htab->tls_ld_or_ldm_got.offset;
13ae64f3
JJ
4652 if (off & 1)
4653 off &= ~1;
4654 else
4655 {
947216bf 4656 Elf_Internal_Rela outrel;
13ae64f3 4657
6de2ae4a 4658 if (htab->elf.srelgot == NULL)
13ae64f3
JJ
4659 abort ();
4660
6de2ae4a
L
4661 outrel.r_offset = (htab->elf.sgot->output_section->vma
4662 + htab->elf.sgot->output_offset + off);
13ae64f3
JJ
4663
4664 bfd_put_32 (output_bfd, 0,
6de2ae4a 4665 htab->elf.sgot->contents + off);
13ae64f3 4666 bfd_put_32 (output_bfd, 0,
6de2ae4a 4667 htab->elf.sgot->contents + off + 4);
13ae64f3 4668 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
59d6ffb2 4669 elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
0afcef53 4670 htab->tls_ld_or_ldm_got.offset |= 1;
13ae64f3 4671 }
6de2ae4a
L
4672 relocation = htab->elf.sgot->output_section->vma
4673 + htab->elf.sgot->output_offset + off
4674 - htab->elf.sgotplt->output_section->vma
4675 - htab->elf.sgotplt->output_offset;
b34976b6 4676 unresolved_reloc = FALSE;
13ae64f3
JJ
4677 break;
4678
4679 case R_386_TLS_LDO_32:
0e1862bb
L
4680 if (!bfd_link_executable (info)
4681 || (input_section->flags & SEC_CODE) == 0)
0afcef53 4682 relocation -= _bfd_x86_elf_dtpoff_base (info);
13ae64f3
JJ
4683 else
4684 /* When converting LDO to LE, we must negate. */
eb4ff4d6 4685 relocation = -elf_i386_tpoff (info, relocation);
13ae64f3
JJ
4686 break;
4687
4688 case R_386_TLS_LE_32:
13ae64f3 4689 case R_386_TLS_LE:
0e1862bb 4690 if (!bfd_link_executable (info))
37e55690 4691 {
947216bf 4692 Elf_Internal_Rela outrel;
37e55690 4693 asection *sreloc;
37e55690
JJ
4694
4695 outrel.r_offset = rel->r_offset
4696 + input_section->output_section->vma
4697 + input_section->output_offset;
4698 if (h != NULL && h->dynindx != -1)
4699 indx = h->dynindx;
4700 else
4701 indx = 0;
4702 if (r_type == R_386_TLS_LE_32)
4703 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
4704 else
4705 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
4706 sreloc = elf_section_data (input_section)->sreloc;
4707 if (sreloc == NULL)
4708 abort ();
59d6ffb2 4709 elf_append_rel (output_bfd, sreloc, &outrel);
37e55690
JJ
4710 if (indx)
4711 continue;
4712 else if (r_type == R_386_TLS_LE_32)
0afcef53 4713 relocation = _bfd_x86_elf_dtpoff_base (info) - relocation;
37e55690 4714 else
0afcef53 4715 relocation -= _bfd_x86_elf_dtpoff_base (info);
37e55690
JJ
4716 }
4717 else if (r_type == R_386_TLS_LE_32)
eb4ff4d6 4718 relocation = elf_i386_tpoff (info, relocation);
37e55690 4719 else
eb4ff4d6 4720 relocation = -elf_i386_tpoff (info, relocation);
13ae64f3
JJ
4721 break;
4722
252b5132
RH
4723 default:
4724 break;
4725 }
4726
239e1f3a
AM
4727 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4728 because such sections are not SEC_ALLOC and thus ld.so will
4729 not process them. */
8c694914 4730 if (unresolved_reloc
239e1f3a 4731 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
4732 && h->def_dynamic)
4733 && _bfd_elf_section_offset (output_bfd, info, input_section,
4734 rel->r_offset) != (bfd_vma) -1)
6a30718d 4735 {
4eca0228 4736 _bfd_error_handler
695344c0 4737 /* xgettext:c-format */
d42c267e 4738 (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
d003868e
AM
4739 input_bfd,
4740 input_section,
d42c267e 4741 rel->r_offset,
843fe662 4742 howto->name,
6a30718d 4743 h->root.root.string);
b34976b6 4744 return FALSE;
6a30718d 4745 }
83be169b 4746
cbe950e9 4747do_relocation:
252b5132
RH
4748 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4749 contents, rel->r_offset,
55fd94b0 4750 relocation, 0);
252b5132 4751
62d78908 4752check_relocation_error:
cf5c0c5b 4753 if (r != bfd_reloc_ok)
252b5132 4754 {
cf5c0c5b 4755 const char *name;
ffb2e45b 4756
cf5c0c5b
AM
4757 if (h != NULL)
4758 name = h->root.root.string;
4759 else
4760 {
4761 name = bfd_elf_string_from_elf_section (input_bfd,
4762 symtab_hdr->sh_link,
4763 sym->st_name);
4764 if (name == NULL)
b34976b6 4765 return FALSE;
cf5c0c5b
AM
4766 if (*name == '\0')
4767 name = bfd_section_name (input_bfd, sec);
4768 }
ffb2e45b 4769
cf5c0c5b 4770 if (r == bfd_reloc_overflow)
1a72702b
AM
4771 (*info->callbacks->reloc_overflow)
4772 (info, (h ? &h->root : NULL), name, howto->name,
4773 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
cf5c0c5b
AM
4774 else
4775 {
4eca0228 4776 _bfd_error_handler
695344c0 4777 /* xgettext:c-format */
d42c267e 4778 (_("%B(%A+%#Lx): reloc against `%s': error %d"),
d003868e 4779 input_bfd, input_section,
d42c267e 4780 rel->r_offset, name, (int) r);
b34976b6 4781 return FALSE;
cf5c0c5b 4782 }
252b5132 4783 }
60f2e42e
L
4784
4785 if (wrel != rel)
4786 *wrel = *rel;
4787 }
4788
4789 if (wrel != rel)
4790 {
4791 Elf_Internal_Shdr *rel_hdr;
4792 size_t deleted = rel - wrel;
4793
4794 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
4795 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
4796 if (rel_hdr->sh_size == 0)
4797 {
4798 /* It is too late to remove an empty reloc section. Leave
4799 one NONE reloc.
4800 ??? What is wrong with an empty section??? */
4801 rel_hdr->sh_size = rel_hdr->sh_entsize;
4802 deleted -= 1;
4803 }
4804 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
4805 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
4806 input_section->reloc_count -= deleted;
252b5132
RH
4807 }
4808
b34976b6 4809 return TRUE;
252b5132
RH
4810}
4811
4812/* Finish up dynamic symbol handling. We set the contents of various
4813 dynamic sections here. */
4814
b34976b6 4815static bfd_boolean
55fd94b0
AM
4816elf_i386_finish_dynamic_symbol (bfd *output_bfd,
4817 struct bfd_link_info *info,
4818 struct elf_link_hash_entry *h,
4819 Elf_Internal_Sym *sym)
252b5132 4820{
0afcef53 4821 struct elf_x86_link_hash_table *htab;
25e762b9
RM
4822 unsigned plt_entry_size;
4823 const struct elf_i386_backend_data *abed;
0afcef53 4824 struct elf_x86_link_hash_entry *eh;
aec6b87e 4825 bfd_boolean local_undefweak;
ee2fdd6f 4826 bfd_boolean use_plt_second;
252b5132 4827
0afcef53 4828 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6
NC
4829 if (htab == NULL)
4830 return FALSE;
252b5132 4831
25e762b9 4832 abed = get_elf_i386_backend_data (output_bfd);
765e526c 4833 plt_entry_size = htab->plt.plt_entry_size;
25e762b9 4834
ee2fdd6f
L
4835 /* Use the second PLT section only if there is .plt section. */
4836 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
4837
0afcef53 4838 eh = (struct elf_x86_link_hash_entry *) h;
e133d005
L
4839 if (eh->no_finish_dynamic_symbol)
4840 abort ();
dd7e64d4 4841
aec6b87e
L
4842 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
4843 resolved undefined weak symbols in executable so that their
4844 references have value 0 at run-time. */
0afcef53 4845 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, I386_ELF_DATA,
e62b9723
L
4846 eh->has_got_reloc,
4847 eh);
aec6b87e 4848
252b5132
RH
4849 if (h->plt.offset != (bfd_vma) -1)
4850 {
ee2fdd6f 4851 bfd_vma plt_index, plt_offset;
252b5132 4852 bfd_vma got_offset;
947216bf
AM
4853 Elf_Internal_Rela rel;
4854 bfd_byte *loc;
ee2fdd6f 4855 asection *plt, *resolved_plt, *gotplt, *relplt;
cbe950e9
L
4856
4857 /* When building a static executable, use .iplt, .igot.plt and
4858 .rel.iplt sections for STT_GNU_IFUNC symbols. */
6de2ae4a 4859 if (htab->elf.splt != NULL)
cbe950e9 4860 {
6de2ae4a
L
4861 plt = htab->elf.splt;
4862 gotplt = htab->elf.sgotplt;
4863 relplt = htab->elf.srelplt;
cbe950e9
L
4864 }
4865 else
4866 {
6de2ae4a
L
4867 plt = htab->elf.iplt;
4868 gotplt = htab->elf.igotplt;
4869 relplt = htab->elf.irelplt;
cbe950e9 4870 }
252b5132
RH
4871
4872 /* This symbol has an entry in the procedure linkage table. Set
4873 it up. */
4874
cbe950e9 4875 if ((h->dynindx == -1
aec6b87e 4876 && !local_undefweak
0e1862bb 4877 && !((h->forced_local || bfd_link_executable (info))
cbe950e9
L
4878 && h->def_regular
4879 && h->type == STT_GNU_IFUNC))
4880 || plt == NULL
4881 || gotplt == NULL
4882 || relplt == NULL)
cec7f46a 4883 abort ();
252b5132
RH
4884
4885 /* Get the index in the procedure linkage table which
4886 corresponds to this symbol. This is the index of this symbol
4887 in all the symbols for which we are making plt entries. The
cbe950e9 4888 first entry in the procedure linkage table is reserved.
252b5132 4889
cbe950e9 4890 Get the offset into the .got table of the entry that
252b5132 4891 corresponds to this function. Each .got entry is 4 bytes.
cbe950e9 4892 The first three are reserved.
6bbec505 4893
cbe950e9
L
4894 For static executables, we don't reserve anything. */
4895
6de2ae4a 4896 if (plt == htab->elf.splt)
cbe950e9 4897 {
f604c2a2 4898 got_offset = (h->plt.offset / plt_entry_size
765e526c 4899 - htab->plt.has_plt0);
e1f98742 4900 got_offset = (got_offset + 3) * 4;
cbe950e9
L
4901 }
4902 else
4903 {
e1f98742
L
4904 got_offset = h->plt.offset / plt_entry_size;
4905 got_offset = got_offset * 4;
cbe950e9 4906 }
252b5132 4907
f604c2a2
L
4908 /* Fill in the entry in the procedure linkage table and update
4909 the first slot. */
765e526c 4910 memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
f604c2a2 4911 plt_entry_size);
ee2fdd6f
L
4912
4913 if (use_plt_second)
4914 {
4915 const bfd_byte *plt_entry;
4916 if (bfd_link_pic (info))
765e526c 4917 plt_entry = htab->non_lazy_plt->pic_plt_entry;
ee2fdd6f 4918 else
765e526c 4919 plt_entry = htab->non_lazy_plt->plt_entry;
ee2fdd6f 4920 memcpy (htab->plt_second->contents + eh->plt_second.offset,
765e526c 4921 plt_entry, htab->non_lazy_plt->plt_entry_size);
ee2fdd6f
L
4922
4923 resolved_plt = htab->plt_second;
4924 plt_offset = eh->plt_second.offset;
4925 }
4926 else
4927 {
4928 resolved_plt = plt;
4929 plt_offset = h->plt.offset;
4930 }
4931
0e1862bb 4932 if (! bfd_link_pic (info))
252b5132 4933 {
252b5132 4934 bfd_put_32 (output_bfd,
cbe950e9
L
4935 (gotplt->output_section->vma
4936 + gotplt->output_offset
252b5132 4937 + got_offset),
ee2fdd6f 4938 resolved_plt->contents + plt_offset
765e526c 4939 + htab->plt.plt_got_offset);
eac338cf 4940
f604c2a2 4941 if (abed->os == is_vxworks)
eac338cf
PB
4942 {
4943 int s, k, reloc_index;
4944
4945 /* Create the R_386_32 relocation referencing the GOT
4946 for this PLT entry. */
4947
4948 /* S: Current slot number (zero-based). */
765e526c
L
4949 s = ((h->plt.offset - htab->plt.plt_entry_size)
4950 / htab->plt.plt_entry_size);
eac338cf 4951 /* K: Number of relocations for PLTResolve. */
0e1862bb 4952 if (bfd_link_pic (info))
eac338cf
PB
4953 k = PLTRESOLVE_RELOCS_SHLIB;
4954 else
4955 k = PLTRESOLVE_RELOCS;
4956 /* Skip the PLTresolve relocations, and the relocations for
4957 the other PLT slots. */
4958 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
765e526c 4959 loc = (htab->srelplt2->contents + reloc_index
eac338cf
PB
4960 * sizeof (Elf32_External_Rel));
4961
ee2fdd6f
L
4962 rel.r_offset = (plt->output_section->vma
4963 + plt->output_offset
eac338cf 4964 + h->plt.offset + 2),
7325306f 4965 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
eac338cf
PB
4966 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4967
4968 /* Create the R_386_32 relocation referencing the beginning of
4969 the PLT for this GOT entry. */
6de2ae4a
L
4970 rel.r_offset = (htab->elf.sgotplt->output_section->vma
4971 + htab->elf.sgotplt->output_offset
eac338cf 4972 + got_offset);
7325306f 4973 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
eac338cf 4974 bfd_elf32_swap_reloc_out (output_bfd, &rel,
13ca3149 4975 loc + sizeof (Elf32_External_Rel));
eac338cf 4976 }
252b5132
RH
4977 }
4978 else
4979 {
252b5132 4980 bfd_put_32 (output_bfd, got_offset,
ee2fdd6f 4981 resolved_plt->contents + plt_offset
765e526c 4982 + htab->plt.plt_got_offset);
252b5132
RH
4983 }
4984
aec6b87e
L
4985 /* Fill in the entry in the global offset table. Leave the entry
4986 as zero for undefined weak symbol in PIE. No PLT relocation
4987 against undefined weak symbol in PIE. */
4988 if (!local_undefweak)
cbe950e9 4989 {
765e526c 4990 if (htab->plt.has_plt0)
f604c2a2
L
4991 bfd_put_32 (output_bfd,
4992 (plt->output_section->vma
4993 + plt->output_offset
4994 + h->plt.offset
765e526c 4995 + htab->lazy_plt->plt_lazy_offset),
f604c2a2 4996 gotplt->contents + got_offset);
252b5132 4997
aec6b87e
L
4998 /* Fill in the entry in the .rel.plt section. */
4999 rel.r_offset = (gotplt->output_section->vma
5000 + gotplt->output_offset
5001 + got_offset);
5002 if (h->dynindx == -1
5003 || ((bfd_link_executable (info)
5004 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5005 && h->def_regular
5006 && h->type == STT_GNU_IFUNC))
5007 {
6322e5c5
L
5008 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
5009 h->root.root.string,
5010 h->root.u.def.section->owner);
5011
aec6b87e
L
5012 /* If an STT_GNU_IFUNC symbol is locally defined, generate
5013 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
5014 in the .got.plt section. */
5015 bfd_put_32 (output_bfd,
5016 (h->root.u.def.value
5017 + h->root.u.def.section->output_section->vma
5018 + h->root.u.def.section->output_offset),
5019 gotplt->contents + got_offset);
5020 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
5021 /* R_386_IRELATIVE comes last. */
5022 plt_index = htab->next_irelative_index--;
5023 }
5024 else
5025 {
5026 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
5027 plt_index = htab->next_jump_slot_index++;
5028 }
5029
5030 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
5031 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5032
f604c2a2
L
5033 /* Don't fill the second and third slots in PLT entry for
5034 static executables nor without PLT0. */
765e526c 5035 if (plt == htab->elf.splt && htab->plt.has_plt0)
aec6b87e
L
5036 {
5037 bfd_put_32 (output_bfd,
5038 plt_index * sizeof (Elf32_External_Rel),
5039 plt->contents + h->plt.offset
765e526c 5040 + htab->lazy_plt->plt_reloc_offset);
f604c2a2
L
5041 bfd_put_32 (output_bfd,
5042 - (h->plt.offset
765e526c 5043 + htab->lazy_plt->plt_plt_offset + 4),
f604c2a2 5044 (plt->contents + h->plt.offset
765e526c 5045 + htab->lazy_plt->plt_plt_offset));
aec6b87e 5046 }
e1f98742 5047 }
dd7e64d4
L
5048 }
5049 else if (eh->plt_got.offset != (bfd_vma) -1)
5050 {
5051 bfd_vma got_offset, plt_offset;
5052 asection *plt, *got, *gotplt;
5053 const bfd_byte *got_plt_entry;
5054
dd7e64d4
L
5055 /* Set the entry in the GOT procedure linkage table. */
5056 plt = htab->plt_got;
5057 got = htab->elf.sgot;
5058 gotplt = htab->elf.sgotplt;
5059 got_offset = h->got.offset;
5060
5061 if (got_offset == (bfd_vma) -1
5062 || plt == NULL
5063 || got == NULL
5064 || gotplt == NULL)
5065 abort ();
5066
5067 /* Fill in the entry in the GOT procedure linkage table. */
0e1862bb 5068 if (! bfd_link_pic (info))
252b5132 5069 {
765e526c 5070 got_plt_entry = htab->non_lazy_plt->plt_entry;
dd7e64d4 5071 got_offset += got->output_section->vma + got->output_offset;
252b5132 5072 }
dd7e64d4
L
5073 else
5074 {
765e526c 5075 got_plt_entry = htab->non_lazy_plt->pic_plt_entry;
dd7e64d4
L
5076 got_offset += (got->output_section->vma
5077 + got->output_offset
5078 - gotplt->output_section->vma
5079 - gotplt->output_offset);
5080 }
5081
5082 plt_offset = eh->plt_got.offset;
5083 memcpy (plt->contents + plt_offset, got_plt_entry,
765e526c 5084 htab->non_lazy_plt->plt_entry_size);
dd7e64d4 5085 bfd_put_32 (output_bfd, got_offset,
f604c2a2 5086 (plt->contents + plt_offset
765e526c 5087 + htab->non_lazy_plt->plt_got_offset));
dd7e64d4
L
5088 }
5089
aec6b87e
L
5090 if (!local_undefweak
5091 && !h->def_regular
dd7e64d4
L
5092 && (h->plt.offset != (bfd_vma) -1
5093 || eh->plt_got.offset != (bfd_vma) -1))
5094 {
5095 /* Mark the symbol as undefined, rather than as defined in
5096 the .plt section. Leave the value if there were any
5097 relocations where pointer equality matters (this is a clue
5098 for the dynamic linker, to make function pointer
5099 comparisons work between an application and shared
5100 library), otherwise set it to zero. If a function is only
5101 called from a binary, there is no need to slow down
5102 shared libraries because of that. */
5103 sym->st_shndx = SHN_UNDEF;
5104 if (!h->pointer_equality_needed)
5105 sym->st_value = 0;
252b5132
RH
5106 }
5107
aec6b87e
L
5108 /* Don't generate dynamic GOT relocation against undefined weak
5109 symbol in executable. */
13ae64f3 5110 if (h->got.offset != (bfd_vma) -1
0afcef53
L
5111 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry(h)->tls_type)
5112 && (elf_x86_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
aec6b87e 5113 && !local_undefweak)
252b5132 5114 {
947216bf 5115 Elf_Internal_Rela rel;
233cc9c1 5116 asection *relgot = htab->elf.srelgot;
252b5132
RH
5117
5118 /* This symbol has an entry in the global offset table. Set it
5119 up. */
5120
6de2ae4a 5121 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
ffb2e45b 5122 abort ();
252b5132 5123
6de2ae4a
L
5124 rel.r_offset = (htab->elf.sgot->output_section->vma
5125 + htab->elf.sgot->output_offset
dc810e39 5126 + (h->got.offset & ~(bfd_vma) 1));
252b5132 5127
dd5724d5
AM
5128 /* If this is a static link, or it is a -Bsymbolic link and the
5129 symbol is defined locally or was forced to be local because
5130 of a version file, we just want to emit a RELATIVE reloc.
252b5132
RH
5131 The entry in the global offset table will already have been
5132 initialized in the relocate_section function. */
710ab287 5133 if (h->def_regular
0018b0a3
L
5134 && h->type == STT_GNU_IFUNC)
5135 {
233cc9c1
L
5136 if (h->plt.offset == (bfd_vma) -1)
5137 {
5138 /* STT_GNU_IFUNC is referenced without PLT. */
5139 if (htab->elf.splt == NULL)
5140 {
5141 /* use .rel[a].iplt section to store .got relocations
5142 in static executable. */
5143 relgot = htab->elf.irelplt;
5144 }
5145 if (SYMBOL_REFERENCES_LOCAL (info, h))
5146 {
6322e5c5
L
5147 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
5148 h->root.root.string,
5149 h->root.u.def.section->owner);
5150
233cc9c1
L
5151 bfd_put_32 (output_bfd,
5152 (h->root.u.def.value
5153 + h->root.u.def.section->output_section->vma
5154 + h->root.u.def.section->output_offset),
5155 htab->elf.sgot->contents + h->got.offset);
5156 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
5157 }
5158 else
5159 goto do_glob_dat;
5160 }
5161 else if (bfd_link_pic (info))
710ab287
L
5162 {
5163 /* Generate R_386_GLOB_DAT. */
5164 goto do_glob_dat;
5165 }
5166 else
5167 {
cd2b2c10 5168 asection *plt;
ee2fdd6f 5169 bfd_vma plt_offset;
cd2b2c10 5170
710ab287
L
5171 if (!h->pointer_equality_needed)
5172 abort ();
5173
5174 /* For non-shared object, we can't use .got.plt, which
5175 contains the real function addres if we need pointer
5176 equality. We load the GOT entry with the PLT entry. */
ee2fdd6f
L
5177 if (htab->plt_second != NULL)
5178 {
5179 plt = htab->plt_second;
5180 plt_offset = eh->plt_second.offset;
5181 }
5182 else
5183 {
5184 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
5185 plt_offset = h->plt.offset;
5186 }
710ab287
L
5187 bfd_put_32 (output_bfd,
5188 (plt->output_section->vma
ee2fdd6f 5189 + plt->output_offset + plt_offset),
6de2ae4a 5190 htab->elf.sgot->contents + h->got.offset);
710ab287
L
5191 return TRUE;
5192 }
0018b0a3 5193 }
0e1862bb 5194 else if (bfd_link_pic (info)
0018b0a3 5195 && SYMBOL_REFERENCES_LOCAL (info, h))
dd5724d5 5196 {
6725bdbf 5197 BFD_ASSERT((h->got.offset & 1) != 0);
dd5724d5
AM
5198 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
5199 }
252b5132
RH
5200 else
5201 {
dd5724d5 5202 BFD_ASSERT((h->got.offset & 1) == 0);
710ab287 5203do_glob_dat:
6725bdbf 5204 bfd_put_32 (output_bfd, (bfd_vma) 0,
6de2ae4a 5205 htab->elf.sgot->contents + h->got.offset);
252b5132
RH
5206 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
5207 }
5208
233cc9c1 5209 elf_append_rel (output_bfd, relgot, &rel);
252b5132
RH
5210 }
5211
f5385ebf 5212 if (h->needs_copy)
252b5132 5213 {
947216bf 5214 Elf_Internal_Rela rel;
5474d94f 5215 asection *s;
252b5132
RH
5216
5217 /* This symbol needs a copy reloc. Set it up. */
5218
ffb2e45b
AM
5219 if (h->dynindx == -1
5220 || (h->root.type != bfd_link_hash_defined
5221 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
5222 || htab->elf.srelbss == NULL
5223 || htab->elf.sreldynrelro == NULL)
ffb2e45b 5224 abort ();
252b5132
RH
5225
5226 rel.r_offset = (h->root.u.def.value
5227 + h->root.u.def.section->output_section->vma
5228 + h->root.u.def.section->output_offset);
5229 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
afbf7e8e 5230 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
5231 s = htab->elf.sreldynrelro;
5232 else
5233 s = htab->elf.srelbss;
5234 elf_append_rel (output_bfd, s, &rel);
252b5132
RH
5235 }
5236
b34976b6 5237 return TRUE;
252b5132
RH
5238}
5239
c25bc9fc
L
5240/* Finish up local dynamic symbol handling. We set the contents of
5241 various dynamic sections here. */
5242
5243static bfd_boolean
5244elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
5245{
5246 struct elf_link_hash_entry *h
5247 = (struct elf_link_hash_entry *) *slot;
5248 struct bfd_link_info *info
23209a78 5249 = (struct bfd_link_info *) inf;
c25bc9fc
L
5250
5251 return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
5252 h, NULL);
5253}
5254
aec6b87e
L
5255/* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
5256 here since undefined weak symbol may not be dynamic and may not be
5257 called for elf_i386_finish_dynamic_symbol. */
5258
5259static bfd_boolean
5260elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
5261 void *inf)
5262{
5263 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
5264 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5265
5266 if (h->root.type != bfd_link_hash_undefweak
5267 || h->dynindx != -1)
5268 return TRUE;
5269
5270 return elf_i386_finish_dynamic_symbol (info->output_bfd,
5f0f0847 5271 info, h, NULL);
aec6b87e
L
5272}
5273
38701953
AM
5274/* Used to decide how to sort relocs in an optimal manner for the
5275 dynamic linker, before writing them out. */
5276
5277static enum elf_reloc_type_class
cae1fbbb 5278elf_i386_reloc_type_class (const struct bfd_link_info *info,
7e612e98
AM
5279 const asection *rel_sec ATTRIBUTE_UNUSED,
5280 const Elf_Internal_Rela *rela)
38701953 5281{
cae1fbbb
L
5282 bfd *abfd = info->output_bfd;
5283 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5284 struct elf_link_hash_table *htab = elf_hash_table (info);
cae1fbbb 5285
d9e3b590
L
5286 if (htab->dynsym != NULL
5287 && htab->dynsym->contents != NULL)
5288 {
5289 /* Check relocation against STT_GNU_IFUNC symbol if there are
5290 dynamic symbols. */
5291 unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
c428ce9d
L
5292 if (r_symndx != STN_UNDEF)
5293 {
5294 Elf_Internal_Sym sym;
5295 if (!bed->s->swap_symbol_in (abfd,
5296 (htab->dynsym->contents
5297 + r_symndx * sizeof (Elf32_External_Sym)),
5298 0, &sym))
5299 abort ();
d9e3b590 5300
c428ce9d
L
5301 if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
5302 return reloc_class_ifunc;
5303 }
d9e3b590 5304 }
cae1fbbb 5305
55fd94b0 5306 switch (ELF32_R_TYPE (rela->r_info))
38701953 5307 {
c428ce9d
L
5308 case R_386_IRELATIVE:
5309 return reloc_class_ifunc;
38701953
AM
5310 case R_386_RELATIVE:
5311 return reloc_class_relative;
5312 case R_386_JUMP_SLOT:
5313 return reloc_class_plt;
5314 case R_386_COPY:
5315 return reloc_class_copy;
5316 default:
5317 return reloc_class_normal;
5318 }
5319}
5320
252b5132
RH
5321/* Finish up the dynamic sections. */
5322
b34976b6 5323static bfd_boolean
55fd94b0
AM
5324elf_i386_finish_dynamic_sections (bfd *output_bfd,
5325 struct bfd_link_info *info)
252b5132 5326{
0afcef53 5327 struct elf_x86_link_hash_table *htab;
252b5132 5328 bfd *dynobj;
252b5132 5329 asection *sdyn;
25e762b9 5330 const struct elf_i386_backend_data *abed;
252b5132 5331
0afcef53 5332 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6
NC
5333 if (htab == NULL)
5334 return FALSE;
5335
ebe50bae 5336 dynobj = htab->elf.dynobj;
3d4d4302 5337 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
25e762b9 5338 abed = get_elf_i386_backend_data (output_bfd);
252b5132 5339
ebe50bae 5340 if (htab->elf.dynamic_sections_created)
252b5132 5341 {
252b5132
RH
5342 Elf32_External_Dyn *dyncon, *dynconend;
5343
6de2ae4a 5344 if (sdyn == NULL || htab->elf.sgot == NULL)
ffb2e45b 5345 abort ();
252b5132
RH
5346
5347 dyncon = (Elf32_External_Dyn *) sdyn->contents;
eea6121a 5348 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
252b5132
RH
5349 for (; dyncon < dynconend; dyncon++)
5350 {
5351 Elf_Internal_Dyn dyn;
51b64d56 5352 asection *s;
252b5132
RH
5353
5354 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5355
5356 switch (dyn.d_tag)
5357 {
5358 default:
f604c2a2 5359 if (abed->os == is_vxworks
25e762b9 5360 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
7a2b07ff 5361 break;
0ac8d2ca 5362 continue;
252b5132
RH
5363
5364 case DT_PLTGOT:
6de2ae4a 5365 s = htab->elf.sgotplt;
8c37241b 5366 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6725bdbf
AM
5367 break;
5368
252b5132 5369 case DT_JMPREL:
6de2ae4a 5370 s = htab->elf.srelplt;
6348e046 5371 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
252b5132
RH
5372 break;
5373
5374 case DT_PLTRELSZ:
6de2ae4a 5375 s = htab->elf.srelplt;
eea6121a 5376 dyn.d_un.d_val = s->size;
252b5132 5377 break;
252b5132 5378 }
0ac8d2ca
AM
5379
5380 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
252b5132
RH
5381 }
5382
6de2ae4a 5383 if (htab->elf.splt && htab->elf.splt->size > 0)
252b5132 5384 {
1f78f649
L
5385 /* UnixWare sets the entsize of .plt to 4, although that doesn't
5386 really seem like the right value. */
5387 elf_section_data (htab->elf.splt->output_section)
5388 ->this_hdr.sh_entsize = 4;
5389
765e526c 5390 if (htab->plt.has_plt0)
252b5132 5391 {
f604c2a2
L
5392 /* Fill in the special first entry in the procedure linkage
5393 table. */
765e526c
L
5394 memcpy (htab->elf.splt->contents, htab->plt.plt0_entry,
5395 htab->lazy_plt->plt0_entry_size);
5396 memset (htab->elf.splt->contents + htab->lazy_plt->plt0_entry_size,
25e762b9 5397 abed->plt0_pad_byte,
765e526c 5398 htab->plt.plt_entry_size - htab->lazy_plt->plt0_entry_size);
f604c2a2 5399 if (!bfd_link_pic (info))
eac338cf 5400 {
f604c2a2
L
5401 bfd_put_32 (output_bfd,
5402 (htab->elf.sgotplt->output_section->vma
5403 + htab->elf.sgotplt->output_offset
5404 + 4),
5405 htab->elf.splt->contents
765e526c 5406 + htab->lazy_plt->plt0_got1_offset);
f604c2a2
L
5407 bfd_put_32 (output_bfd,
5408 (htab->elf.sgotplt->output_section->vma
5409 + htab->elf.sgotplt->output_offset
5410 + 8),
5411 htab->elf.splt->contents
765e526c 5412 + htab->lazy_plt->plt0_got2_offset);
eac338cf 5413
f604c2a2 5414 if (abed->os == is_vxworks)
1f78f649 5415 {
f604c2a2
L
5416 Elf_Internal_Rela rel;
5417 int num_plts = (htab->elf.splt->size
765e526c 5418 / htab->plt.plt_entry_size) - 1;
f604c2a2 5419 unsigned char *p;
765e526c 5420 asection *srelplt2 = htab->srelplt2;
f604c2a2
L
5421
5422 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
5423 + 4. On IA32 we use REL relocations so the
5424 addend goes in the PLT directly. */
5425 rel.r_offset = (htab->elf.splt->output_section->vma
5426 + htab->elf.splt->output_offset
765e526c 5427 + htab->lazy_plt->plt0_got1_offset);
1f78f649
L
5428 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
5429 R_386_32);
f604c2a2 5430 bfd_elf32_swap_reloc_out (output_bfd, &rel,
0afcef53 5431 srelplt2->contents);
f604c2a2
L
5432 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
5433 + 8. */
5434 rel.r_offset = (htab->elf.splt->output_section->vma
5435 + htab->elf.splt->output_offset
765e526c 5436 + htab->lazy_plt->plt0_got2_offset);
f604c2a2 5437 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
1f78f649 5438 R_386_32);
f604c2a2 5439 bfd_elf32_swap_reloc_out (output_bfd, &rel,
0afcef53 5440 srelplt2->contents +
f604c2a2
L
5441 sizeof (Elf32_External_Rel));
5442 /* Correct the .rel.plt.unloaded relocations. */
0afcef53 5443 p = srelplt2->contents;
f604c2a2
L
5444 if (bfd_link_pic (info))
5445 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
5446 else
5447 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
5448
5449 for (; num_plts; num_plts--)
5450 {
5451 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
5452 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
5453 R_386_32);
5454 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
5455 p += sizeof (Elf32_External_Rel);
5456
5457 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
5458 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
5459 R_386_32);
5460 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
5461 p += sizeof (Elf32_External_Rel);
5462 }
1f78f649 5463 }
eac338cf
PB
5464 }
5465 }
252b5132 5466 }
f604c2a2
L
5467
5468 if (htab->plt_got != NULL && htab->plt_got->size > 0)
5469 elf_section_data (htab->plt_got->output_section)
765e526c 5470 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
ee2fdd6f
L
5471
5472 if (htab->plt_second != NULL && htab->plt_second->size > 0)
5473 elf_section_data (htab->plt_second->output_section)
765e526c 5474 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
252b5132
RH
5475 }
5476
f604c2a2
L
5477 /* Fill in the first three entries in the global offset table. */
5478 if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
252b5132 5479 {
56d4289c
L
5480 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
5481 {
4eca0228 5482 _bfd_error_handler
56d4289c
L
5483 (_("discarded output section: `%A'"), htab->elf.sgotplt);
5484 return FALSE;
5485 }
5486
f604c2a2
L
5487 bfd_put_32 (output_bfd,
5488 (sdyn == NULL ? 0
5489 : sdyn->output_section->vma + sdyn->output_offset),
5490 htab->elf.sgotplt->contents);
5491 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
5492 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
252b5132 5493
6de2ae4a 5494 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
12d0ee4a 5495 }
8c37241b 5496
e41b3a13 5497 /* Adjust .eh_frame for .plt section. */
9a2a56cc
AM
5498 if (htab->plt_eh_frame != NULL
5499 && htab->plt_eh_frame->contents != NULL)
e41b3a13
JJ
5500 {
5501 if (htab->elf.splt != NULL
5502 && htab->elf.splt->size != 0
5503 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
5504 && htab->elf.splt->output_section != NULL
5505 && htab->plt_eh_frame->output_section != NULL)
5506 {
5507 bfd_vma plt_start = htab->elf.splt->output_section->vma;
5508 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
5509 + htab->plt_eh_frame->output_offset
5510 + PLT_FDE_START_OFFSET;
5511 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
5512 htab->plt_eh_frame->contents
5513 + PLT_FDE_START_OFFSET);
5514 }
5515 if (htab->plt_eh_frame->sec_info_type
dbaa2011 5516 == SEC_INFO_TYPE_EH_FRAME)
e41b3a13
JJ
5517 {
5518 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
5519 htab->plt_eh_frame,
5520 htab->plt_eh_frame->contents))
5521 return FALSE;
5522 }
5523 }
5524
fff53dae
L
5525 /* Adjust .eh_frame for .plt.got section. */
5526 if (htab->plt_got_eh_frame != NULL
5527 && htab->plt_got_eh_frame->contents != NULL)
5528 {
5529 if (htab->plt_got != NULL
5530 && htab->plt_got->size != 0
5531 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
5532 && htab->plt_got->output_section != NULL
5533 && htab->plt_got_eh_frame->output_section != NULL)
5534 {
5535 bfd_vma plt_start = htab->plt_got->output_section->vma;
5536 bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
5537 + htab->plt_got_eh_frame->output_offset
5538 + PLT_FDE_START_OFFSET;
5539 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
5540 htab->plt_got_eh_frame->contents
5541 + PLT_FDE_START_OFFSET);
5542 }
5543 if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
5544 {
5545 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
5546 htab->plt_got_eh_frame,
5547 htab->plt_got_eh_frame->contents))
5548 return FALSE;
5549 }
5550 }
5551
ee2fdd6f
L
5552 /* Adjust .eh_frame for the second PLT section. */
5553 if (htab->plt_second_eh_frame != NULL
5554 && htab->plt_second_eh_frame->contents != NULL)
5555 {
5556 if (htab->plt_second != NULL
5557 && htab->plt_second->size != 0
5558 && (htab->plt_second->flags & SEC_EXCLUDE) == 0
5559 && htab->plt_second->output_section != NULL
5560 && htab->plt_second_eh_frame->output_section != NULL)
5561 {
5562 bfd_vma plt_start = htab->plt_second->output_section->vma;
5563 bfd_vma eh_frame_start
5564 = (htab->plt_second_eh_frame->output_section->vma
5565 + htab->plt_second_eh_frame->output_offset
5566 + PLT_FDE_START_OFFSET);
5567 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
5568 htab->plt_second_eh_frame->contents
5569 + PLT_FDE_START_OFFSET);
5570 }
5571 if (htab->plt_second_eh_frame->sec_info_type
5572 == SEC_INFO_TYPE_EH_FRAME)
5573 {
5574 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
5575 htab->plt_second_eh_frame,
5576 htab->plt_second_eh_frame->contents))
5577 return FALSE;
5578 }
5579 }
5580
6de2ae4a
L
5581 if (htab->elf.sgot && htab->elf.sgot->size > 0)
5582 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
8c37241b 5583
aec6b87e
L
5584 /* Fill PLT entries for undefined weak symbols in PIE. */
5585 if (bfd_link_pie (info))
5586 bfd_hash_traverse (&info->hash->table,
5587 elf_i386_pie_finish_undefweak_symbol,
5588 info);
5589
b34976b6 5590 return TRUE;
252b5132
RH
5591}
5592
233cc9c1
L
5593/* Fill PLT/GOT entries and allocate dynamic relocations for local
5594 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
5595 It has to be done before elf_link_sort_relocs is called so that
5596 dynamic relocations are properly sorted. */
5597
5598static bfd_boolean
5599elf_i386_output_arch_local_syms
5600 (bfd *output_bfd ATTRIBUTE_UNUSED,
5601 struct bfd_link_info *info,
5602 void *flaginfo ATTRIBUTE_UNUSED,
5603 int (*func) (void *, const char *,
5604 Elf_Internal_Sym *,
5605 asection *,
5606 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
5607{
0afcef53
L
5608 struct elf_x86_link_hash_table *htab
5609 = elf_x86_hash_table (info, I386_ELF_DATA);
233cc9c1
L
5610 if (htab == NULL)
5611 return FALSE;
5612
5613 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
5614 htab_traverse (htab->loc_hash_table,
5615 elf_i386_finish_local_dynamic_symbol,
5616 info);
5617
5618 return TRUE;
5619}
5620
f604c2a2
L
5621enum elf_i386_plt_type
5622{
5623 plt_non_lazy = 0,
5624 plt_lazy = 1 << 0,
5625 plt_pic = 1 << 1,
ee2fdd6f 5626 plt_second = 1 << 2,
f604c2a2
L
5627 plt_unknown = -1
5628};
5629
5630struct elf_i386_plt
5631{
5632 const char *name;
5633 asection *sec;
5634 bfd_byte *contents;
5635 enum elf_i386_plt_type type;
5636 unsigned int plt_got_offset;
5637 unsigned int plt_entry_size;
5638 long count;
5639};
5640
5641/* Forward declaration. */
765e526c 5642static const struct elf_x86_lazy_plt_layout elf_i386_nacl_plt;
f604c2a2
L
5643
5644/* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
5645 dynamic relocations. */
5646
5647static long
5648elf_i386_get_synthetic_symtab (bfd *abfd,
5649 long symcount ATTRIBUTE_UNUSED,
5650 asymbol **syms ATTRIBUTE_UNUSED,
5651 long dynsymcount,
5652 asymbol **dynsyms,
5653 asymbol **ret)
5654{
98c5dfc9 5655 long size, count, i, n, len;
f604c2a2
L
5656 int j;
5657 unsigned int plt_got_offset, plt_entry_size;
5658 asymbol *s;
5659 bfd_byte *plt_contents;
5660 long dynrelcount, relsize;
98c5dfc9 5661 arelent **dynrelbuf, *p;
765e526c
L
5662 const struct elf_x86_lazy_plt_layout *lazy_plt;
5663 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
5664 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
5665 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
f604c2a2
L
5666 asection *plt;
5667 bfd_vma got_addr;
5668 char *names;
5669 enum elf_i386_plt_type plt_type;
5670 struct elf_i386_plt plts[] =
3972882e 5671 {
f604c2a2
L
5672 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0 },
5673 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0 },
ee2fdd6f 5674 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0 },
dd9e66ee 5675 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0 }
f604c2a2 5676 };
144bed8d 5677
f604c2a2 5678 *ret = NULL;
144bed8d 5679
f604c2a2
L
5680 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
5681 return 0;
3972882e 5682
f604c2a2
L
5683 if (dynsymcount <= 0)
5684 return 0;
cca5b8b6 5685
f604c2a2
L
5686 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
5687 if (relsize <= 0)
5688 return -1;
cca5b8b6 5689
f604c2a2
L
5690 non_lazy_plt = NULL;
5691 /* Silence GCC 6. */
5692 lazy_plt = NULL;
ee2fdd6f
L
5693 non_lazy_ibt_plt = NULL;
5694 lazy_ibt_plt = NULL;
f604c2a2 5695 switch (get_elf_i386_backend_data (abfd)->os)
144bed8d 5696 {
f604c2a2
L
5697 case is_normal:
5698 non_lazy_plt = &elf_i386_non_lazy_plt;
ee2fdd6f
L
5699 lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
5700 non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
f604c2a2
L
5701 /* Fall through */
5702 case is_vxworks:
5703 lazy_plt = &elf_i386_lazy_plt;
5704 break;
5705 case is_nacl:
5706 lazy_plt = &elf_i386_nacl_plt;
5707 break;
5708 }
5709
5710 got_addr = 0;
3972882e 5711
f604c2a2
L
5712 count = 0;
5713 for (j = 0; plts[j].name != NULL; j++)
5714 {
5715 plt = bfd_get_section_by_name (abfd, plts[j].name);
90efb642 5716 if (plt == NULL || plt->size == 0)
6f25f223 5717 continue;
533d0af0 5718
f604c2a2
L
5719 /* Get the PLT section contents. */
5720 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
5721 if (plt_contents == NULL)
5722 break;
5723 if (!bfd_get_section_contents (abfd, (asection *) plt,
5724 plt_contents, 0, plt->size))
5725 {
5726 free (plt_contents);
5727 break;
5728 }
5729
5730 /* Check what kind of PLT it is. */
5731 plt_type = plt_unknown;
90efb642
L
5732 if (plts[j].type == plt_unknown
5733 && (plt->size >= (lazy_plt->plt0_entry_size
5734 + lazy_plt->plt_entry_size)))
f604c2a2
L
5735 {
5736 /* Match lazy PLT first. */
5737 if (memcmp (plt_contents, lazy_plt->plt0_entry,
5738 lazy_plt->plt0_got1_offset) == 0)
ee2fdd6f
L
5739 {
5740 /* The fist entry in the lazy IBT PLT is the same as the
5741 normal lazy PLT. */
5742 if (lazy_ibt_plt != NULL
90efb642 5743 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
ee2fdd6f
L
5744 lazy_ibt_plt->plt_entry,
5745 lazy_ibt_plt->plt_got_offset) == 0))
5746 plt_type = plt_lazy | plt_second;
5747 else
5748 plt_type = plt_lazy;
5749 }
f604c2a2
L
5750 else if (memcmp (plt_contents, lazy_plt->pic_plt0_entry,
5751 lazy_plt->plt0_got1_offset) == 0)
ee2fdd6f
L
5752 {
5753 /* The fist entry in the PIC lazy IBT PLT is the same as
5754 the normal PIC lazy PLT. */
5755 if (lazy_ibt_plt != NULL
90efb642 5756 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
ee2fdd6f
L
5757 lazy_ibt_plt->pic_plt_entry,
5758 lazy_ibt_plt->plt_got_offset) == 0))
5759 plt_type = plt_lazy | plt_pic | plt_second;
5760 else
5761 plt_type = plt_lazy | plt_pic;
5762 }
f604c2a2
L
5763 }
5764
5765 if (non_lazy_plt != NULL
90efb642
L
5766 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
5767 && plt->size >= non_lazy_plt->plt_entry_size)
f604c2a2
L
5768 {
5769 /* Match non-lazy PLT. */
5770 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
5771 non_lazy_plt->plt_got_offset) == 0)
5772 plt_type = plt_non_lazy;
5773 else if (memcmp (plt_contents, non_lazy_plt->pic_plt_entry,
5774 non_lazy_plt->plt_got_offset) == 0)
5775 plt_type = plt_pic;
5776 }
5777
ee2fdd6f 5778 if ((non_lazy_ibt_plt != NULL)
90efb642
L
5779 && (plt_type == plt_unknown || plt_type == plt_second)
5780 && plt->size >= non_lazy_ibt_plt->plt_entry_size)
ee2fdd6f
L
5781 {
5782 if (memcmp (plt_contents,
5783 non_lazy_ibt_plt->plt_entry,
5784 non_lazy_ibt_plt->plt_got_offset) == 0)
5785 {
5786 /* Match IBT PLT. */
5787 plt_type = plt_second;
5788 non_lazy_plt = non_lazy_ibt_plt;
5789 }
5790 else if (memcmp (plt_contents,
5791 non_lazy_ibt_plt->pic_plt_entry,
5792 non_lazy_ibt_plt->plt_got_offset) == 0)
5793 {
5794 /* Match PIC IBT PLT. */
5795 plt_type = plt_second | plt_pic;
5796 non_lazy_plt = non_lazy_ibt_plt;
5797 }
5798 }
5799
f604c2a2 5800 if (plt_type == plt_unknown)
37c0b6ee
L
5801 {
5802 free (plt_contents);
5803 continue;
5804 }
3972882e 5805
f604c2a2
L
5806 plts[j].sec = plt;
5807 plts[j].type = plt_type;
ec1f73bb 5808
f604c2a2
L
5809 if ((plt_type & plt_lazy))
5810 {
5811 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
5812 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
5813 /* Skip PLT0 in lazy PLT. */
5814 i = 1;
5815 }
5816 else
5817 {
5818 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
5819 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
5820 i = 0;
5821 }
fca6ae69 5822
ee2fdd6f
L
5823 /* Skip lazy PLT when the second PLT is used. */
5824 if ((plt_type & (plt_lazy | plt_second))
5825 == (plt_lazy | plt_second))
5826 plts[j].count = 0;
5827 else
5828 {
5829 n = plt->size / plts[j].plt_entry_size;
5830 plts[j].count = n;
5831 count += n - i;
5832 }
f604c2a2
L
5833
5834 plts[j].contents = plt_contents;
5835
5836 /* The _GLOBAL_OFFSET_TABLE_ address is needed. */
5837 if ((plt_type & plt_pic))
5838 got_addr = (bfd_vma) -1;
144bed8d
L
5839 }
5840
90efb642
L
5841 if (count == 0)
5842 return -1;
5843
37c0b6ee
L
5844 dynrelbuf = (arelent **) bfd_malloc (relsize);
5845 if (dynrelbuf == NULL)
5846 return -1;
5847
5848 dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
5849 dynsyms);
5850
5851 /* Sort the relocs by address. */
0afcef53
L
5852 qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
5853 _bfd_x86_elf_compare_relocs);
37c0b6ee 5854
f604c2a2 5855 size = count * sizeof (asymbol);
98c5dfc9
L
5856
5857 /* Allocate space for @plt suffixes. */
5858 n = 0;
5859 for (i = 0; i < dynrelcount; i++)
5860 {
5861 p = dynrelbuf[i];
5862 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
5863 if (p->addend != 0)
5864 size += sizeof ("+0x") - 1 + 8;
5865 }
5866
f604c2a2
L
5867 s = *ret = (asymbol *) bfd_zmalloc (size);
5868 if (s == NULL)
823cb067 5869 goto bad_return;
3972882e 5870
f604c2a2
L
5871 if (got_addr)
5872 {
5873 /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
5874 address. */
5875 asection *sec = bfd_get_section_by_name (abfd, ".got.plt");
5876 if (sec != NULL)
5877 got_addr = sec->vma;
5878 else
5879 {
5880 sec = bfd_get_section_by_name (abfd, ".got");
5881 if (sec != NULL)
5882 got_addr = sec->vma;
5883 }
3972882e 5884
f604c2a2
L
5885 if (got_addr == (bfd_vma) -1)
5886 goto bad_return;
5887 }
3972882e 5888
f604c2a2 5889 /* Check for each PLT section. */
98c5dfc9 5890 names = (char *) (s + count);
f604c2a2
L
5891 size = 0;
5892 n = 0;
5893 for (j = 0; plts[j].name != NULL; j++)
5894 if ((plt_contents = plts[j].contents) != NULL)
5895 {
5896 long k;
5897 bfd_vma offset;
5898
5899 plt_got_offset = plts[j].plt_got_offset;
5900 plt_entry_size = plts[j].plt_entry_size;
5901
5902 plt = plts[j].sec;
5903
5904 if ((plts[j].type & plt_lazy))
5905 {
5906 /* Skip PLT0 in lazy PLT. */
5907 k = 1;
5908 offset = plt_entry_size;
5909 }
5910 else
5911 {
5912 k = 0;
5913 offset = 0;
5914 }
5915
5916 /* Check each PLT entry against dynamic relocations. */
5917 for (; k < plts[j].count; k++)
5918 {
5919 int off;
5920 bfd_vma got_vma;
5921 long min, max, mid;
f604c2a2
L
5922
5923 /* Get the GOT offset, a signed 32-bit integer. */
5924 off = H_GET_32 (abfd, (plt_contents + offset
5925 + plt_got_offset));
5926 got_vma = got_addr + off;
5927
5928 /* Binary search. */
5929 p = dynrelbuf[0];
5930 min = 0;
5931 max = dynrelcount;
5932 while ((min + 1) < max)
5933 {
5934 arelent *r;
5935
5936 mid = (min + max) / 2;
5937 r = dynrelbuf[mid];
5938 if (got_vma > r->address)
5939 min = mid;
5940 else if (got_vma < r->address)
5941 max = mid;
5942 else
5943 {
5944 p = r;
5945 break;
5946 }
5947 }
5948
5949 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
5950 if (got_vma == p->address
5951 && p->howto != NULL
5952 && (p->howto->type == R_386_JUMP_SLOT
5953 || p->howto->type == R_386_GLOB_DAT
5954 || p->howto->type == R_386_IRELATIVE))
5955 {
5956 *s = **p->sym_ptr_ptr;
5957 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
5958 set. Since we are defining a symbol, ensure one
5959 of them is set. */
5960 if ((s->flags & BSF_LOCAL) == 0)
5961 s->flags |= BSF_GLOBAL;
5962 s->flags |= BSF_SYNTHETIC;
5963 /* This is no longer a section symbol. */
5964 s->flags &= ~BSF_SECTION_SYM;
5965 s->section = plt;
5966 s->the_bfd = plt->owner;
5967 s->value = offset;
98c5dfc9
L
5968 s->udata.p = NULL;
5969 s->name = names;
5970 len = strlen ((*p->sym_ptr_ptr)->name);
5971 memcpy (names, (*p->sym_ptr_ptr)->name, len);
5972 names += len;
f604c2a2 5973 if (p->addend != 0)
98c5dfc9
L
5974 {
5975 char buf[30], *a;
5976
5977 memcpy (names, "+0x", sizeof ("+0x") - 1);
5978 names += sizeof ("+0x") - 1;
5979 bfd_sprintf_vma (abfd, buf, p->addend);
5980 for (a = buf; *a == '0'; ++a)
5981 ;
5982 size = strlen (a);
5983 memcpy (names, a, size);
5984 names += size;
5985 }
5986 memcpy (names, "@plt", sizeof ("@plt"));
5987 names += sizeof ("@plt");
f604c2a2
L
5988 n++;
5989 s++;
5990 }
5991 offset += plt_entry_size;
5992 }
5993 }
5994
5995 /* PLT entries with R_386_TLS_DESC relocations are skipped. */
5996 if (n == 0)
823cb067
L
5997 {
5998bad_return:
5999 count = -1;
6000 }
6001 else
6002 count = n;
f604c2a2 6003
f604c2a2
L
6004 for (j = 0; plts[j].name != NULL; j++)
6005 if (plts[j].contents != NULL)
6006 free (plts[j].contents);
6007
6008 free (dynrelbuf);
6009
6010 return count;
4c45e5c9
JJ
6011}
6012
f604c2a2
L
6013/* Set up i386 GNU properties. Return the first relocatable ELF input
6014 with GNU properties if found. Otherwise, return NULL. */
6015
6016static bfd *
6017elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
6018{
6019 bfd_boolean normal_target;
ee2fdd6f 6020 bfd_boolean lazy_plt;
f604c2a2
L
6021 asection *sec, *pltsec;
6022 bfd *dynobj;
ee2fdd6f
L
6023 bfd_boolean use_ibt_plt;
6024 unsigned int plt_alignment, features;
0afcef53 6025 struct elf_x86_link_hash_table *htab;
ee2fdd6f 6026 bfd *pbfd;
73784fa5
L
6027 bfd *ebfd = NULL;
6028 elf_property *prop;
ee2fdd6f
L
6029
6030 features = 0;
6031 if (info->ibt)
6032 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
48580982
L
6033 if (info->shstk)
6034 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
ee2fdd6f 6035
73784fa5
L
6036 /* Find a normal input file with GNU property note. */
6037 for (pbfd = info->input_bfds;
6038 pbfd != NULL;
6039 pbfd = pbfd->link.next)
6040 if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
6041 && bfd_count_sections (pbfd) != 0)
6042 {
6043 ebfd = pbfd;
ee2fdd6f 6044
73784fa5
L
6045 if (elf_properties (pbfd) != NULL)
6046 break;
6047 }
6048
a5b4ee94 6049 if (ebfd != NULL && features)
73784fa5 6050 {
a5b4ee94
L
6051 /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT and
6052 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
6053 prop = _bfd_elf_get_property (ebfd,
6054 GNU_PROPERTY_X86_FEATURE_1_AND,
6055 4);
6056 prop->u.number |= features;
6057 prop->pr_kind = property_number;
6058
6059 /* Create the GNU property note section if needed. */
6060 if (pbfd == NULL)
ee2fdd6f 6061 {
a5b4ee94
L
6062 sec = bfd_make_section_with_flags (ebfd,
6063 NOTE_GNU_PROPERTY_SECTION_NAME,
6064 (SEC_ALLOC
6065 | SEC_LOAD
6066 | SEC_IN_MEMORY
6067 | SEC_READONLY
6068 | SEC_HAS_CONTENTS
6069 | SEC_DATA));
6070 if (sec == NULL)
6071 info->callbacks->einfo (_("%F: failed to create GNU property section\n"));
ee2fdd6f 6072
a5b4ee94
L
6073 if (!bfd_set_section_alignment (ebfd, sec, 2))
6074 {
73784fa5 6075error_alignment:
a5b4ee94
L
6076 info->callbacks->einfo (_("%F%A: failed to align section\n"),
6077 sec);
73784fa5 6078 }
73784fa5 6079
a5b4ee94
L
6080 elf_section_type (sec) = SHT_NOTE;
6081 }
ee2fdd6f
L
6082 }
6083
6084 pbfd = _bfd_elf_link_setup_gnu_properties (info);
f604c2a2
L
6085
6086 if (bfd_link_relocatable (info))
6087 return pbfd;
6088
0afcef53 6089 htab = elf_x86_hash_table (info, I386_ELF_DATA);
f604c2a2
L
6090 if (htab == NULL)
6091 return pbfd;
6092
ee2fdd6f
L
6093 use_ibt_plt = info->ibtplt || info->ibt;
6094 if (!use_ibt_plt && pbfd != NULL)
6095 {
6096 /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */
6097 elf_property_list *p;
6098
6099 /* The property list is sorted in order of type. */
6100 for (p = elf_properties (pbfd); p; p = p->next)
6101 {
6102 if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
6103 {
6104 use_ibt_plt = !!(p->property.u.number
6105 & GNU_PROPERTY_X86_FEATURE_1_IBT);
6106 break;
6107 }
6108 else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
6109 break;
6110 }
6111 }
6112
f604c2a2
L
6113 dynobj = htab->elf.dynobj;
6114
6115 /* Set htab->elf.dynobj here so that there is no need to check and
6116 set it in check_relocs. */
6117 if (dynobj == NULL)
6118 {
b7a18930
NC
6119 if (pbfd != NULL)
6120 {
6121 htab->elf.dynobj = pbfd;
6122 dynobj = pbfd;
6123 }
6124 else
6125 {
6126 bfd *abfd;
6127
6128 /* Find a normal input file to hold linker created
6129 sections. */
6130 for (abfd = info->input_bfds;
6131 abfd != NULL;
6132 abfd = abfd->link.next)
6133 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6134 && (abfd->flags
6135 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
6136 {
6137 htab->elf.dynobj = abfd;
6138 dynobj = abfd;
6139 break;
6140 }
6141 }
f604c2a2
L
6142 }
6143
6144 /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
6145 still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
6146 canonical function address. */
765e526c 6147 htab->plt.has_plt0 = 1;
f604c2a2
L
6148 normal_target = FALSE;
6149
6150 switch (get_elf_i386_backend_data (info->output_bfd)->os)
6151 {
6152 case is_normal:
ee2fdd6f
L
6153 if (use_ibt_plt)
6154 {
765e526c
L
6155 htab->lazy_plt = &elf_i386_lazy_ibt_plt;
6156 htab->non_lazy_plt = &elf_i386_non_lazy_ibt_plt;
ee2fdd6f
L
6157 }
6158 else
6159 {
765e526c
L
6160 htab->lazy_plt = &elf_i386_lazy_plt;
6161 htab->non_lazy_plt = &elf_i386_non_lazy_plt;
ee2fdd6f 6162 }
f604c2a2
L
6163 normal_target = TRUE;
6164 break;
6165 case is_vxworks:
765e526c
L
6166 htab->lazy_plt = &elf_i386_lazy_plt;
6167 htab->non_lazy_plt = NULL;
f604c2a2 6168 if (!elf_vxworks_create_dynamic_sections (dynobj, info,
765e526c 6169 &htab->srelplt2))
f604c2a2
L
6170 info->callbacks->einfo (_("%F: failed to create VxWorks dynamic sections\n"));
6171 break;
6172 case is_nacl:
765e526c
L
6173 htab->lazy_plt = &elf_i386_nacl_plt;
6174 htab->non_lazy_plt = NULL;
f604c2a2
L
6175 break;
6176 }
6177
6178 pltsec = htab->elf.splt;
6179
6180 /* If the non-lazy PLT is available, use it for all PLT entries if
6181 there are no PLT0 or no .plt section. */
765e526c
L
6182 if (htab->non_lazy_plt != NULL
6183 && (!htab->plt.has_plt0 || pltsec == NULL))
f604c2a2 6184 {
ee2fdd6f 6185 lazy_plt = FALSE;
f604c2a2 6186 if (bfd_link_pic (info))
765e526c 6187 htab->plt.plt_entry = htab->non_lazy_plt->pic_plt_entry;
f604c2a2 6188 else
765e526c
L
6189 htab->plt.plt_entry = htab->non_lazy_plt->plt_entry;
6190 htab->plt.plt_entry_size = htab->non_lazy_plt->plt_entry_size;
6191 htab->plt.plt_got_offset = htab->non_lazy_plt->plt_got_offset;
6192 htab->plt.eh_frame_plt_size
6193 = htab->non_lazy_plt->eh_frame_plt_size;
6194 htab->plt.eh_frame_plt = htab->non_lazy_plt->eh_frame_plt;
f604c2a2
L
6195 }
6196 else
6197 {
ee2fdd6f 6198 lazy_plt = TRUE;
f604c2a2
L
6199 if (bfd_link_pic (info))
6200 {
765e526c
L
6201 htab->plt.plt0_entry = htab->lazy_plt->pic_plt0_entry;
6202 htab->plt.plt_entry = htab->lazy_plt->pic_plt_entry;
f604c2a2
L
6203 }
6204 else
6205 {
765e526c
L
6206 htab->plt.plt0_entry = htab->lazy_plt->plt0_entry;
6207 htab->plt.plt_entry = htab->lazy_plt->plt_entry;
f604c2a2 6208 }
765e526c
L
6209
6210 htab->plt.plt_entry_size = htab->lazy_plt->plt_entry_size;
6211 htab->plt.plt_got_offset = htab->lazy_plt->plt_got_offset;
6212 htab->plt.eh_frame_plt_size = htab->lazy_plt->eh_frame_plt_size;
6213 htab->plt.eh_frame_plt = htab->lazy_plt->eh_frame_plt;
f604c2a2
L
6214 }
6215
765e526c
L
6216 /* This is unused for i386. */
6217 htab->plt.plt_got_insn_size = 0;
6218
f604c2a2
L
6219 /* Return if there are no normal input files. */
6220 if (dynobj == NULL)
6221 return pbfd;
6222
6223 /* Since create_dynamic_sections isn't always called, but GOT
b7a18930 6224 relocations need GOT sections, create them here so that we
f604c2a2
L
6225 don't need to do it in check_relocs. */
6226 if (htab->elf.sgot == NULL
6227 && !_bfd_elf_create_got_section (dynobj, info))
6228 info->callbacks->einfo (_("%F: failed to create GOT sections\n"));
6229
6230 /* Create the ifunc sections here so that check_relocs can be
6231 simplified. */
6232 if (!_bfd_elf_create_ifunc_sections (dynobj, info))
6233 info->callbacks->einfo (_("%F: failed to create ifunc sections\n"));
6234
765e526c 6235 plt_alignment = bfd_log2 (htab->plt.plt_entry_size);
f604c2a2
L
6236
6237 if (pltsec != NULL)
6238 {
6239 /* Whe creating executable, set the contents of the .interp
6240 section to the interpreter. */
6241 if (bfd_link_executable (info) && !info->nointerp)
6242 {
6243 asection *s = bfd_get_linker_section (dynobj, ".interp");
6244 if (s == NULL)
6245 abort ();
0afcef53
L
6246 s->size = htab->dynamic_interpreter_size;
6247 s->contents = (unsigned char *) htab->dynamic_interpreter;
f604c2a2
L
6248 htab->interp = s;
6249 }
6250
6251 /* Don't change PLT section alignment for NaCl since it uses
6252 64-byte PLT entry and sets PLT section alignment to 32
6253 bytes. */
6254 if (normal_target)
6255 {
6256 const struct elf_backend_data *bed
6257 = get_elf_backend_data (dynobj);
6258 flagword pltflags = (bed->dynamic_sec_flags
6259 | SEC_ALLOC
6260 | SEC_CODE
6261 | SEC_LOAD
6262 | SEC_READONLY);
6263 unsigned int non_lazy_plt_alignment
765e526c 6264 = bfd_log2 (htab->non_lazy_plt->plt_entry_size);
f604c2a2
L
6265
6266 sec = pltsec;
6267 if (!bfd_set_section_alignment (sec->owner, sec,
6268 plt_alignment))
6269 goto error_alignment;
6270
6271 /* Create the GOT procedure linkage table. */
6272 sec = bfd_make_section_anyway_with_flags (dynobj,
6273 ".plt.got",
6274 pltflags);
6275 if (sec == NULL)
6276 info->callbacks->einfo (_("%F: failed to create GOT PLT section\n"));
6277
6278 if (!bfd_set_section_alignment (dynobj, sec,
6279 non_lazy_plt_alignment))
6280 goto error_alignment;
6281
6282 htab->plt_got = sec;
ee2fdd6f
L
6283
6284 if (lazy_plt)
6285 {
6286 sec = NULL;
6287
6288 if (use_ibt_plt)
6289 {
6290 /* Create the second PLT for Intel IBT support. IBT
6291 PLT is supported only for non-NaCl target and is
6292 is needed only for lazy binding. */
6293 sec = bfd_make_section_anyway_with_flags (dynobj,
6294 ".plt.sec",
6295 pltflags);
6296 if (sec == NULL)
6297 info->callbacks->einfo (_("%F: failed to create IBT-enabled PLT section\n"));
6298
6299 if (!bfd_set_section_alignment (dynobj, sec,
6300 plt_alignment))
6301 goto error_alignment;
6302 }
6303
6304 htab->plt_second = sec;
6305 }
f604c2a2
L
6306 }
6307
6308 if (!info->no_ld_generated_unwind_info)
6309 {
6310 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
6311 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6312 | SEC_LINKER_CREATED);
6313
6314 sec = bfd_make_section_anyway_with_flags (dynobj,
6315 ".eh_frame",
6316 flags);
6317 if (sec == NULL)
6318 info->callbacks->einfo (_("%F: failed to create PLT .eh_frame section\n"));
6319
6320 if (!bfd_set_section_alignment (dynobj, sec, 2))
6321 goto error_alignment;
6322
6323 htab->plt_eh_frame = sec;
6324
6325 if (htab->plt_got != NULL)
6326 {
6327 sec = bfd_make_section_anyway_with_flags (dynobj,
6328 ".eh_frame",
6329 flags);
6330 if (sec == NULL)
6331 info->callbacks->einfo (_("%F: failed to create GOT PLT .eh_frame section\n"));
6332
6333 if (!bfd_set_section_alignment (dynobj, sec, 2))
6334 goto error_alignment;
6335
6336 htab->plt_got_eh_frame = sec;
6337 }
6338 }
6339 }
6340
6341 if (normal_target)
6342 {
6343 /* The .iplt section is used for IFUNC symbols in static
6344 executables. */
6345 sec = htab->elf.iplt;
6346 if (sec != NULL
6347 && !bfd_set_section_alignment (sec->owner, sec,
6348 plt_alignment))
73784fa5 6349 goto error_alignment;
f604c2a2
L
6350 }
6351
6352 return pbfd;
6353}
6354
6d00b590 6355#define TARGET_LITTLE_SYM i386_elf32_vec
252b5132
RH
6356#define TARGET_LITTLE_NAME "elf32-i386"
6357#define ELF_ARCH bfd_arch_i386
ae95ffa6 6358#define ELF_TARGET_ID I386_ELF_DATA
252b5132
RH
6359#define ELF_MACHINE_CODE EM_386
6360#define ELF_MAXPAGESIZE 0x1000
252b5132
RH
6361
6362#define elf_backend_can_gc_sections 1
51b64d56 6363#define elf_backend_can_refcount 1
252b5132
RH
6364#define elf_backend_want_got_plt 1
6365#define elf_backend_plt_readonly 1
6366#define elf_backend_want_plt_sym 0
6367#define elf_backend_got_header_size 12
e41b3a13 6368#define elf_backend_plt_alignment 4
64f52338 6369#define elf_backend_dtrel_excludes_plt 1
f7483970 6370#define elf_backend_extern_protected_data 1
bedfd056 6371#define elf_backend_caches_rawsize 1
5474d94f 6372#define elf_backend_want_dynrelro 1
252b5132 6373
8c29f035
AM
6374/* Support RELA for objdump of prelink objects. */
6375#define elf_info_to_howto elf_i386_info_to_howto_rel
dd5724d5
AM
6376#define elf_info_to_howto_rel elf_i386_info_to_howto_rel
6377
13ae64f3 6378#define bfd_elf32_mkobject elf_i386_mkobject
13ae64f3 6379
dd5724d5 6380#define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
dd5724d5 6381#define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
13285a1b 6382#define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
3972882e 6383#define bfd_elf32_get_synthetic_symtab elf_i386_get_synthetic_symtab
dd5724d5
AM
6384
6385#define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
13285a1b 6386#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
dd5724d5 6387#define elf_backend_check_relocs elf_i386_check_relocs
f604c2a2 6388#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
0ac8d2ca 6389#define elf_backend_fake_sections elf_i386_fake_sections
dd5724d5
AM
6390#define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
6391#define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
233cc9c1 6392#define elf_backend_output_arch_local_syms elf_i386_output_arch_local_syms
dd5724d5 6393#define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
c5fccbec
DJ
6394#define elf_backend_grok_prstatus elf_i386_grok_prstatus
6395#define elf_backend_grok_psinfo elf_i386_grok_psinfo
db6751f2 6396#define elf_backend_reloc_type_class elf_i386_reloc_type_class
0ac8d2ca
AM
6397#define elf_backend_relocate_section elf_i386_relocate_section
6398#define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
f604c2a2 6399#define elf_backend_setup_gnu_properties elf_i386_link_setup_gnu_properties
dd5724d5 6400
252b5132 6401#include "elf32-target.h"
2bc3c89a
AM
6402
6403/* FreeBSD support. */
6404
6405#undef TARGET_LITTLE_SYM
6d00b590 6406#define TARGET_LITTLE_SYM i386_elf32_fbsd_vec
2bc3c89a
AM
6407#undef TARGET_LITTLE_NAME
6408#define TARGET_LITTLE_NAME "elf32-i386-freebsd"
d1036acb
L
6409#undef ELF_OSABI
6410#define ELF_OSABI ELFOSABI_FREEBSD
2bc3c89a
AM
6411
6412/* The kernel recognizes executables as valid only if they carry a
6413 "FreeBSD" label in the ELF header. So we put this label on all
6414 executables and (for simplicity) also all other object files. */
6415
2bc3c89a 6416static void
d8045f23 6417elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
2bc3c89a 6418{
78245035 6419 _bfd_elf_post_process_headers (abfd, info);
2bc3c89a 6420
2bc3c89a 6421#ifdef OLD_FREEBSD_ABI_LABEL
cf7363b4
L
6422 {
6423 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
6424 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6425 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
6426 }
caf47ea6 6427#endif
2bc3c89a
AM
6428}
6429
6430#undef elf_backend_post_process_headers
d8045f23 6431#define elf_backend_post_process_headers elf_i386_fbsd_post_process_headers
571fe01f
NC
6432#undef elf32_bed
6433#define elf32_bed elf32_i386_fbsd_bed
2bc3c89a 6434
d8045f23
NC
6435#undef elf_backend_add_symbol_hook
6436
2bc3c89a 6437#include "elf32-target.h"
eac338cf 6438
a6cc6b3b
RO
6439/* Solaris 2. */
6440
6441#undef TARGET_LITTLE_SYM
6d00b590 6442#define TARGET_LITTLE_SYM i386_elf32_sol2_vec
a6cc6b3b
RO
6443#undef TARGET_LITTLE_NAME
6444#define TARGET_LITTLE_NAME "elf32-i386-sol2"
6445
914082d1
L
6446#undef elf_backend_post_process_headers
6447
a6cc6b3b
RO
6448/* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
6449 objects won't be recognized. */
6450#undef ELF_OSABI
6451
6452#undef elf32_bed
6453#define elf32_bed elf32_i386_sol2_bed
6454
7dc98aea
RO
6455/* The 32-bit static TLS arena size is rounded to the nearest 8-byte
6456 boundary. */
84865015 6457#undef elf_backend_static_tls_alignment
7dc98aea
RO
6458#define elf_backend_static_tls_alignment 8
6459
a6cc6b3b
RO
6460/* The Solaris 2 ABI requires a plt symbol on all platforms.
6461
6462 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
6463 File, p.63. */
84865015 6464#undef elf_backend_want_plt_sym
a6cc6b3b
RO
6465#define elf_backend_want_plt_sym 1
6466
84865015
NC
6467#undef elf_backend_strtab_flags
6468#define elf_backend_strtab_flags SHF_STRINGS
6469
5522f910
NC
6470/* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
6471 has a type >= SHT_LOOS. Returns TRUE if these fields were initialised
6472 FALSE otherwise. ISECTION is the best guess matching section from the
6473 input bfd IBFD, but it might be NULL. */
6474
84865015 6475static bfd_boolean
5522f910
NC
6476elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
6477 bfd *obfd ATTRIBUTE_UNUSED,
6478 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
6479 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
84865015
NC
6480{
6481 /* PR 19938: FIXME: Need to add code for setting the sh_info
5522f910
NC
6482 and sh_link fields of Solaris specific section types. */
6483 return FALSE;
84865015 6484
5522f910 6485 /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13,
84865015
NC
6486 Object File Format, Table 13-9 ELF sh_link and sh_info Interpretation:
6487
6488http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc
6489
6490 The following values should be set:
6491
6492Type Link Info
6493-----------------------------------------------------------------------------
6494SHT_SUNW_ancillary The section header index of 0
6495 [0x6fffffee] the associated string table.
6496
6497SHT_SUNW_capinfo The section header index of For a dynamic object, the
6498 [0x6ffffff0] the associated symbol table. section header index of
6499 the associated
6500 SHT_SUNW_capchain table,
6501 otherwise 0.
6502
6503SHT_SUNW_symsort The section header index of 0
6504 [0x6ffffff1] the associated symbol table.
6505
6506SHT_SUNW_tlssort The section header index of 0
6507 [0x6ffffff2] the associated symbol table.
6508
6509SHT_SUNW_LDYNSYM The section header index of One greater than the
6510 [0x6ffffff3] the associated string table. symbol table index of the
6511 This index is the same string last local symbol,
6512 table used by the SHT_DYNSYM STB_LOCAL. Since
6513 section. SHT_SUNW_LDYNSYM only
6514 contains local symbols,
6515 sh_info is equivalent to
6516 the number of symbols in
6517 the table.
6518
6519SHT_SUNW_cap If symbol capabilities exist, If any capabilities refer
6520 [0x6ffffff5] the section header index of to named strings, the
6521 the associated section header index of
6522 SHT_SUNW_capinfo table, the associated string
6523 otherwise 0. table, otherwise 0.
6524
6525SHT_SUNW_move The section header index of 0
6526 [0x6ffffffa] the associated symbol table.
6527
6528SHT_SUNW_COMDAT 0 0
6529 [0x6ffffffb]
6530
6531SHT_SUNW_syminfo The section header index of The section header index
6532 [0x6ffffffc] the associated symbol table. of the associated
6533 .dynamic section.
6534
6535SHT_SUNW_verdef The section header index of The number of version
6536 [0x6ffffffd] the associated string table. definitions within the
6537 section.
6538
6539SHT_SUNW_verneed The section header index of The number of version
6540 [0x6ffffffe] the associated string table. dependencies within the
6541 section.
6542
6543SHT_SUNW_versym The section header index of 0
6544 [0x6fffffff] the associated symbol table. */
84865015
NC
6545}
6546
5522f910
NC
6547#undef elf_backend_copy_special_section_fields
6548#define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields
84865015 6549
a6cc6b3b
RO
6550#include "elf32-target.h"
6551
bf64a951
L
6552/* Intel MCU support. */
6553
6554static bfd_boolean
6555elf32_iamcu_elf_object_p (bfd *abfd)
6556{
6557 /* Set the right machine number for an IAMCU elf32 file. */
6558 bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
6559 return TRUE;
6560}
6561
6562#undef TARGET_LITTLE_SYM
6563#define TARGET_LITTLE_SYM iamcu_elf32_vec
6564#undef TARGET_LITTLE_NAME
6565#define TARGET_LITTLE_NAME "elf32-iamcu"
84865015 6566#undef ELF_ARCH
bf64a951
L
6567#define ELF_ARCH bfd_arch_iamcu
6568
6569#undef ELF_MACHINE_CODE
6570#define ELF_MACHINE_CODE EM_IAMCU
6571
6572#undef ELF_OSABI
6573
6574#undef elf32_bed
6575#define elf32_bed elf32_iamcu_bed
6576
6577#undef elf_backend_object_p
6578#define elf_backend_object_p elf32_iamcu_elf_object_p
6579
6580#undef elf_backend_static_tls_alignment
6581
6582#undef elf_backend_want_plt_sym
6583#define elf_backend_want_plt_sym 0
6584
84865015 6585#undef elf_backend_strtab_flags
5522f910 6586#undef elf_backend_copy_special_section_fields
84865015 6587
bf64a951
L
6588#include "elf32-target.h"
6589
6590/* Restore defaults. */
6591#undef ELF_ARCH
6592#define ELF_ARCH bfd_arch_i386
6593#undef ELF_MACHINE_CODE
6594#define ELF_MACHINE_CODE EM_386
6595
a27e4371
RM
6596/* Native Client support. */
6597
6598#undef TARGET_LITTLE_SYM
6d00b590 6599#define TARGET_LITTLE_SYM i386_elf32_nacl_vec
a27e4371
RM
6600#undef TARGET_LITTLE_NAME
6601#define TARGET_LITTLE_NAME "elf32-i386-nacl"
6602#undef elf32_bed
6603#define elf32_bed elf32_i386_nacl_bed
6604
6605#undef ELF_MAXPAGESIZE
6606#define ELF_MAXPAGESIZE 0x10000
6607
6608/* Restore defaults. */
6609#undef ELF_OSABI
6610#undef elf_backend_want_plt_sym
6611#define elf_backend_want_plt_sym 0
6612#undef elf_backend_post_process_headers
a27e4371
RM
6613#undef elf_backend_static_tls_alignment
6614
6615/* NaCl uses substantially different PLT entries for the same effects. */
6616
6617#undef elf_backend_plt_alignment
6618#define elf_backend_plt_alignment 5
6619#define NACL_PLT_ENTRY_SIZE 64
6620#define NACLMASK 0xe0 /* 32-byte alignment mask. */
6621
6622static const bfd_byte elf_i386_nacl_plt0_entry[] =
6623 {
6624 0xff, 0x35, /* pushl contents of address */
6625 0, 0, 0, 0, /* replaced with address of .got + 4. */
6626 0x8b, 0x0d, /* movl contents of address, %ecx */
6627 0, 0, 0, 0, /* replaced with address of .got + 8. */
6628 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
6629 0xff, 0xe1 /* jmp *%ecx */
6630 };
6631
6632static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
6633 {
6634 0x8b, 0x0d, /* movl contents of address, %ecx */
6635 0, 0, 0, 0, /* replaced with GOT slot address. */
6636 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
6637 0xff, 0xe1, /* jmp *%ecx */
6638
6639 /* Pad to the next 32-byte boundary with nop instructions. */
6640 0x90,
6641 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6642 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6643
6644 /* Lazy GOT entries point here (32-byte aligned). */
6645 0x68, /* pushl immediate */
6646 0, 0, 0, 0, /* replaced with reloc offset. */
6647 0xe9, /* jmp relative */
6648 0, 0, 0, 0, /* replaced with offset to .plt. */
6649
6650 /* Pad to the next 32-byte boundary with nop instructions. */
6651 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6652 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6653 0x90, 0x90
6654 };
6655
6656static const bfd_byte
6657elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] =
6658 {
6659 0xff, 0x73, 0x04, /* pushl 4(%ebx) */
6660 0x8b, 0x4b, 0x08, /* mov 0x8(%ebx), %ecx */
6661 0x83, 0xe1, 0xe0, /* and $NACLMASK, %ecx */
6662 0xff, 0xe1, /* jmp *%ecx */
caa0075c
RM
6663
6664 /* This is expected to be the same size as elf_i386_nacl_plt0_entry,
6665 so pad to that size with nop instructions. */
6666 0x90, 0x90, 0x90, 0x90, 0x90, 0x90
a27e4371
RM
6667 };
6668
6669static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] =
6670 {
6671 0x8b, 0x8b, /* movl offset(%ebx), %ecx */
6672 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
6673 0x83, 0xe1, 0xe0, /* andl $NACLMASK, %ecx */
6674 0xff, 0xe1, /* jmp *%ecx */
6675
6676 /* Pad to the next 32-byte boundary with nop instructions. */
6677 0x90,
6678 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6679 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6680
6681 /* Lazy GOT entries point here (32-byte aligned). */
6682 0x68, /* pushl immediate */
6683 0, 0, 0, 0, /* replaced with offset into relocation table. */
6684 0xe9, /* jmp relative */
6685 0, 0, 0, 0, /* replaced with offset to start of .plt. */
6686
6687 /* Pad to the next 32-byte boundary with nop instructions. */
6688 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6689 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6690 0x90, 0x90
6691 };
6692
6693static const bfd_byte elf_i386_nacl_eh_frame_plt[] =
6694 {
6695#if (PLT_CIE_LENGTH != 20 \
6696 || PLT_FDE_LENGTH != 36 \
6697 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
6698 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
6699# error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!"
6700#endif
6701 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
6702 0, 0, 0, 0, /* CIE ID */
6703 1, /* CIE version */
6704 'z', 'R', 0, /* Augmentation string */
6705 1, /* Code alignment factor */
6706 0x7c, /* Data alignment factor: -4 */
6707 8, /* Return address column */
6708 1, /* Augmentation size */
6709 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
6710 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
6711 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
6712 DW_CFA_nop, DW_CFA_nop,
6713
6714 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
6715 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
6716 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
6717 0, 0, 0, 0, /* .plt size goes here */
6718 0, /* Augmentation size */
6719 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
6720 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
6721 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
6722 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
6723 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
6724 13, /* Block length */
6725 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
6726 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
6727 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
6728 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
6729 DW_CFA_nop, DW_CFA_nop
6730 };
6731
765e526c 6732static const struct elf_x86_lazy_plt_layout elf_i386_nacl_plt =
a27e4371
RM
6733 {
6734 elf_i386_nacl_plt0_entry, /* plt0_entry */
6735 sizeof (elf_i386_nacl_plt0_entry), /* plt0_entry_size */
a27e4371
RM
6736 elf_i386_nacl_plt_entry, /* plt_entry */
6737 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
765e526c
L
6738 2, /* plt0_got1_offset */
6739 8, /* plt0_got2_offset */
6740 0, /* plt0_got2_insn_end */
a27e4371
RM
6741 2, /* plt_got_offset */
6742 33, /* plt_reloc_offset */
6743 38, /* plt_plt_offset */
765e526c
L
6744 0, /* plt_got_insn_size */
6745 0, /* plt_plt_insn_end */
a27e4371
RM
6746 32, /* plt_lazy_offset */
6747 elf_i386_nacl_pic_plt0_entry, /* pic_plt0_entry */
6748 elf_i386_nacl_pic_plt_entry, /* pic_plt_entry */
6749 elf_i386_nacl_eh_frame_plt, /* eh_frame_plt */
f604c2a2 6750 sizeof (elf_i386_nacl_eh_frame_plt) /* eh_frame_plt_size */
a27e4371
RM
6751 };
6752
6753static const struct elf_i386_backend_data elf_i386_nacl_arch_bed =
6754 {
f604c2a2
L
6755 0x90, /* plt0_pad_byte: nop insn */
6756 is_nacl /* os */
a27e4371
RM
6757 };
6758
64b384e1
RM
6759static bfd_boolean
6760elf32_i386_nacl_elf_object_p (bfd *abfd)
6761{
6762 /* Set the right machine number for a NaCl i386 ELF32 file. */
6763 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl);
6764 return TRUE;
6765}
6766
a27e4371
RM
6767#undef elf_backend_arch_data
6768#define elf_backend_arch_data &elf_i386_nacl_arch_bed
6769
64b384e1
RM
6770#undef elf_backend_object_p
6771#define elf_backend_object_p elf32_i386_nacl_elf_object_p
5a68afcf
RM
6772#undef elf_backend_modify_segment_map
6773#define elf_backend_modify_segment_map nacl_modify_segment_map
6774#undef elf_backend_modify_program_headers
6775#define elf_backend_modify_program_headers nacl_modify_program_headers
887badb3
RM
6776#undef elf_backend_final_write_processing
6777#define elf_backend_final_write_processing nacl_final_write_processing
5a68afcf 6778
a27e4371
RM
6779#include "elf32-target.h"
6780
5a68afcf 6781/* Restore defaults. */
64b384e1 6782#undef elf_backend_object_p
5a68afcf
RM
6783#undef elf_backend_modify_segment_map
6784#undef elf_backend_modify_program_headers
887badb3 6785#undef elf_backend_final_write_processing
5a68afcf 6786
eac338cf
PB
6787/* VxWorks support. */
6788
6789#undef TARGET_LITTLE_SYM
6d00b590 6790#define TARGET_LITTLE_SYM i386_elf32_vxworks_vec
eac338cf
PB
6791#undef TARGET_LITTLE_NAME
6792#define TARGET_LITTLE_NAME "elf32-i386-vxworks"
d1036acb 6793#undef ELF_OSABI
1587442d
L
6794#undef ELF_MAXPAGESIZE
6795#define ELF_MAXPAGESIZE 0x1000
a27e4371
RM
6796#undef elf_backend_plt_alignment
6797#define elf_backend_plt_alignment 4
eac338cf 6798
23209a78
RM
6799static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed =
6800 {
6801 0x90, /* plt0_pad_byte */
f604c2a2 6802 is_vxworks /* os */
23209a78 6803 };
eac338cf 6804
23209a78
RM
6805#undef elf_backend_arch_data
6806#define elf_backend_arch_data &elf_i386_vxworks_arch_bed
eac338cf 6807
13285a1b 6808#undef elf_backend_relocs_compatible
eac338cf
PB
6809#undef elf_backend_add_symbol_hook
6810#define elf_backend_add_symbol_hook \
6811 elf_vxworks_add_symbol_hook
6812#undef elf_backend_link_output_symbol_hook
6813#define elf_backend_link_output_symbol_hook \
9c72ff84 6814 elf_vxworks_link_output_symbol_hook
eac338cf
PB
6815#undef elf_backend_emit_relocs
6816#define elf_backend_emit_relocs elf_vxworks_emit_relocs
6817#undef elf_backend_final_write_processing
6818#define elf_backend_final_write_processing \
6819 elf_vxworks_final_write_processing
7dc98aea 6820#undef elf_backend_static_tls_alignment
eac338cf
PB
6821
6822/* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
6823 define it. */
6824#undef elf_backend_want_plt_sym
6825#define elf_backend_want_plt_sym 1
6826
6827#undef elf32_bed
6828#define elf32_bed elf32_i386_vxworks_bed
6829
6830#include "elf32-target.h"
This page took 2.045684 seconds and 4 git commands to generate.