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