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