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