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