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