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