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