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