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