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