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