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