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