daily update
[deliverable/binutils-gdb.git] / bfd / elf32-i386.c
CommitLineData
252b5132 1/* Intel 80386/80486-specific support for 32-bit ELF
b2a8e766 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
0ffa91dd 3 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
252b5132 4
571fe01f 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
571fe01f
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
571fe01f 10 (at your option) any later version.
252b5132 11
571fe01f
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
571fe01f
NC
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
252b5132 21
252b5132 22#include "sysdep.h"
3db64b00 23#include "bfd.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#include "elf-bfd.h"
eac338cf 27#include "elf-vxworks.h"
142411ca 28#include "bfd_stdint.h"
252b5132 29
55fd94b0
AM
30/* 386 uses REL relocations instead of RELA. */
31#define USE_REL 1
252b5132
RH
32
33#include "elf/i386.h"
34
35static reloc_howto_type elf_howto_table[]=
36{
b34976b6 37 HOWTO(R_386_NONE, 0, 0, 0, FALSE, 0, complain_overflow_bitfield,
1b452ec6 38 bfd_elf_generic_reloc, "R_386_NONE",
b34976b6
AM
39 TRUE, 0x00000000, 0x00000000, FALSE),
40 HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
1b452ec6 41 bfd_elf_generic_reloc, "R_386_32",
b34976b6
AM
42 TRUE, 0xffffffff, 0xffffffff, FALSE),
43 HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
1b452ec6 44 bfd_elf_generic_reloc, "R_386_PC32",
b34976b6
AM
45 TRUE, 0xffffffff, 0xffffffff, TRUE),
46 HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
1b452ec6 47 bfd_elf_generic_reloc, "R_386_GOT32",
b34976b6
AM
48 TRUE, 0xffffffff, 0xffffffff, FALSE),
49 HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
1b452ec6 50 bfd_elf_generic_reloc, "R_386_PLT32",
b34976b6
AM
51 TRUE, 0xffffffff, 0xffffffff, TRUE),
52 HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
1b452ec6 53 bfd_elf_generic_reloc, "R_386_COPY",
b34976b6
AM
54 TRUE, 0xffffffff, 0xffffffff, FALSE),
55 HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
1b452ec6 56 bfd_elf_generic_reloc, "R_386_GLOB_DAT",
b34976b6
AM
57 TRUE, 0xffffffff, 0xffffffff, FALSE),
58 HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
1b452ec6 59 bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
b34976b6
AM
60 TRUE, 0xffffffff, 0xffffffff, FALSE),
61 HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
1b452ec6 62 bfd_elf_generic_reloc, "R_386_RELATIVE",
b34976b6
AM
63 TRUE, 0xffffffff, 0xffffffff, FALSE),
64 HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
1b452ec6 65 bfd_elf_generic_reloc, "R_386_GOTOFF",
b34976b6
AM
66 TRUE, 0xffffffff, 0xffffffff, FALSE),
67 HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
1b452ec6 68 bfd_elf_generic_reloc, "R_386_GOTPC",
b34976b6 69 TRUE, 0xffffffff, 0xffffffff, TRUE),
1b452ec6 70
dc47f327
AM
71 /* We have a gap in the reloc numbers here.
72 R_386_standard counts the number up to this point, and
73 R_386_ext_offset is the value to subtract from a reloc type of
74 R_386_16 thru R_386_PC8 to form an index into this table. */
55fd94b0
AM
75#define R_386_standard (R_386_GOTPC + 1)
76#define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
1b452ec6 77
37e55690 78 /* These relocs are a GNU extension. */
b34976b6 79 HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
37e55690 80 bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
b34976b6
AM
81 TRUE, 0xffffffff, 0xffffffff, FALSE),
82 HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
37e55690 83 bfd_elf_generic_reloc, "R_386_TLS_IE",
b34976b6
AM
84 TRUE, 0xffffffff, 0xffffffff, FALSE),
85 HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
37e55690 86 bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
b34976b6
AM
87 TRUE, 0xffffffff, 0xffffffff, FALSE),
88 HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 89 bfd_elf_generic_reloc, "R_386_TLS_LE",
b34976b6
AM
90 TRUE, 0xffffffff, 0xffffffff, FALSE),
91 HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 92 bfd_elf_generic_reloc, "R_386_TLS_GD",
b34976b6
AM
93 TRUE, 0xffffffff, 0xffffffff, FALSE),
94 HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 95 bfd_elf_generic_reloc, "R_386_TLS_LDM",
b34976b6
AM
96 TRUE, 0xffffffff, 0xffffffff, FALSE),
97 HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
1b452ec6 98 bfd_elf_generic_reloc, "R_386_16",
b34976b6 99 TRUE, 0xffff, 0xffff, FALSE),
b0360d8c 100 HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
1b452ec6 101 bfd_elf_generic_reloc, "R_386_PC16",
b34976b6
AM
102 TRUE, 0xffff, 0xffff, TRUE),
103 HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
1b452ec6 104 bfd_elf_generic_reloc, "R_386_8",
b34976b6
AM
105 TRUE, 0xff, 0xff, FALSE),
106 HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
1b452ec6 107 bfd_elf_generic_reloc, "R_386_PC8",
b34976b6 108 TRUE, 0xff, 0xff, TRUE),
dc47f327 109
55fd94b0
AM
110#define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
111#define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
13ae64f3 112 /* These are common with Solaris TLS implementation. */
b34976b6 113 HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 114 bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
b34976b6
AM
115 TRUE, 0xffffffff, 0xffffffff, FALSE),
116 HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 117 bfd_elf_generic_reloc, "R_386_TLS_IE_32",
b34976b6
AM
118 TRUE, 0xffffffff, 0xffffffff, FALSE),
119 HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 120 bfd_elf_generic_reloc, "R_386_TLS_LE_32",
b34976b6
AM
121 TRUE, 0xffffffff, 0xffffffff, FALSE),
122 HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 123 bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
b34976b6
AM
124 TRUE, 0xffffffff, 0xffffffff, FALSE),
125 HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 126 bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
b34976b6
AM
127 TRUE, 0xffffffff, 0xffffffff, FALSE),
128 HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
13ae64f3 129 bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
b34976b6 130 TRUE, 0xffffffff, 0xffffffff, FALSE),
67a4f2b7
AO
131 EMPTY_HOWTO (38),
132 HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
133 bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
134 TRUE, 0xffffffff, 0xffffffff, FALSE),
135 HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
136 bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
137 FALSE, 0, 0, FALSE),
138 HOWTO(R_386_TLS_DESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
139 bfd_elf_generic_reloc, "R_386_TLS_DESC",
140 TRUE, 0xffffffff, 0xffffffff, FALSE),
13ae64f3
JJ
141
142 /* Another gap. */
67a4f2b7 143#define R_386_tls (R_386_TLS_DESC + 1 - R_386_tls_offset)
55fd94b0 144#define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_tls)
252b5132
RH
145
146/* GNU extension to record C++ vtable hierarchy. */
252b5132
RH
147 HOWTO (R_386_GNU_VTINHERIT, /* type */
148 0, /* rightshift */
149 2, /* size (0 = byte, 1 = short, 2 = long) */
150 0, /* bitsize */
b34976b6 151 FALSE, /* pc_relative */
252b5132
RH
152 0, /* bitpos */
153 complain_overflow_dont, /* complain_on_overflow */
154 NULL, /* special_function */
155 "R_386_GNU_VTINHERIT", /* name */
b34976b6 156 FALSE, /* partial_inplace */
252b5132
RH
157 0, /* src_mask */
158 0, /* dst_mask */
b34976b6 159 FALSE), /* pcrel_offset */
252b5132
RH
160
161/* GNU extension to record C++ vtable member usage. */
252b5132
RH
162 HOWTO (R_386_GNU_VTENTRY, /* type */
163 0, /* rightshift */
164 2, /* size (0 = byte, 1 = short, 2 = long) */
165 0, /* bitsize */
b34976b6 166 FALSE, /* pc_relative */
252b5132
RH
167 0, /* bitpos */
168 complain_overflow_dont, /* complain_on_overflow */
169 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
170 "R_386_GNU_VTENTRY", /* name */
b34976b6 171 FALSE, /* partial_inplace */
252b5132
RH
172 0, /* src_mask */
173 0, /* dst_mask */
b34976b6 174 FALSE) /* pcrel_offset */
dc47f327 175
55fd94b0 176#define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
dc47f327
AM
177
178};
179
252b5132 180#ifdef DEBUG_GEN_RELOC
55fd94b0
AM
181#define TRACE(str) \
182 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
252b5132
RH
183#else
184#define TRACE(str)
185#endif
186
187static reloc_howto_type *
55fd94b0
AM
188elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
189 bfd_reloc_code_real_type code)
252b5132
RH
190{
191 switch (code)
192 {
193 case BFD_RELOC_NONE:
194 TRACE ("BFD_RELOC_NONE");
55fd94b0 195 return &elf_howto_table[R_386_NONE];
252b5132
RH
196
197 case BFD_RELOC_32:
198 TRACE ("BFD_RELOC_32");
55fd94b0 199 return &elf_howto_table[R_386_32];
252b5132
RH
200
201 case BFD_RELOC_CTOR:
202 TRACE ("BFD_RELOC_CTOR");
55fd94b0 203 return &elf_howto_table[R_386_32];
252b5132
RH
204
205 case BFD_RELOC_32_PCREL:
206 TRACE ("BFD_RELOC_PC32");
55fd94b0 207 return &elf_howto_table[R_386_PC32];
252b5132
RH
208
209 case BFD_RELOC_386_GOT32:
210 TRACE ("BFD_RELOC_386_GOT32");
55fd94b0 211 return &elf_howto_table[R_386_GOT32];
252b5132
RH
212
213 case BFD_RELOC_386_PLT32:
214 TRACE ("BFD_RELOC_386_PLT32");
55fd94b0 215 return &elf_howto_table[R_386_PLT32];
252b5132
RH
216
217 case BFD_RELOC_386_COPY:
218 TRACE ("BFD_RELOC_386_COPY");
55fd94b0 219 return &elf_howto_table[R_386_COPY];
252b5132
RH
220
221 case BFD_RELOC_386_GLOB_DAT:
222 TRACE ("BFD_RELOC_386_GLOB_DAT");
55fd94b0 223 return &elf_howto_table[R_386_GLOB_DAT];
252b5132
RH
224
225 case BFD_RELOC_386_JUMP_SLOT:
226 TRACE ("BFD_RELOC_386_JUMP_SLOT");
55fd94b0 227 return &elf_howto_table[R_386_JUMP_SLOT];
252b5132
RH
228
229 case BFD_RELOC_386_RELATIVE:
230 TRACE ("BFD_RELOC_386_RELATIVE");
55fd94b0 231 return &elf_howto_table[R_386_RELATIVE];
252b5132
RH
232
233 case BFD_RELOC_386_GOTOFF:
234 TRACE ("BFD_RELOC_386_GOTOFF");
55fd94b0 235 return &elf_howto_table[R_386_GOTOFF];
252b5132
RH
236
237 case BFD_RELOC_386_GOTPC:
238 TRACE ("BFD_RELOC_386_GOTPC");
55fd94b0 239 return &elf_howto_table[R_386_GOTPC];
252b5132 240
37e55690
JJ
241 /* These relocs are a GNU extension. */
242 case BFD_RELOC_386_TLS_TPOFF:
243 TRACE ("BFD_RELOC_386_TLS_TPOFF");
55fd94b0 244 return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
37e55690
JJ
245
246 case BFD_RELOC_386_TLS_IE:
247 TRACE ("BFD_RELOC_386_TLS_IE");
55fd94b0 248 return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
37e55690
JJ
249
250 case BFD_RELOC_386_TLS_GOTIE:
251 TRACE ("BFD_RELOC_386_TLS_GOTIE");
55fd94b0 252 return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
37e55690 253
13ae64f3
JJ
254 case BFD_RELOC_386_TLS_LE:
255 TRACE ("BFD_RELOC_386_TLS_LE");
55fd94b0 256 return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
13ae64f3
JJ
257
258 case BFD_RELOC_386_TLS_GD:
259 TRACE ("BFD_RELOC_386_TLS_GD");
55fd94b0 260 return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
13ae64f3
JJ
261
262 case BFD_RELOC_386_TLS_LDM:
263 TRACE ("BFD_RELOC_386_TLS_LDM");
55fd94b0 264 return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
13ae64f3 265
252b5132
RH
266 case BFD_RELOC_16:
267 TRACE ("BFD_RELOC_16");
55fd94b0 268 return &elf_howto_table[R_386_16 - R_386_ext_offset];
252b5132
RH
269
270 case BFD_RELOC_16_PCREL:
271 TRACE ("BFD_RELOC_16_PCREL");
55fd94b0 272 return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
252b5132
RH
273
274 case BFD_RELOC_8:
275 TRACE ("BFD_RELOC_8");
55fd94b0 276 return &elf_howto_table[R_386_8 - R_386_ext_offset];
252b5132
RH
277
278 case BFD_RELOC_8_PCREL:
279 TRACE ("BFD_RELOC_8_PCREL");
55fd94b0 280 return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
252b5132 281
13ae64f3
JJ
282 /* Common with Sun TLS implementation. */
283 case BFD_RELOC_386_TLS_LDO_32:
284 TRACE ("BFD_RELOC_386_TLS_LDO_32");
55fd94b0 285 return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
13ae64f3
JJ
286
287 case BFD_RELOC_386_TLS_IE_32:
288 TRACE ("BFD_RELOC_386_TLS_IE_32");
55fd94b0 289 return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
13ae64f3
JJ
290
291 case BFD_RELOC_386_TLS_LE_32:
292 TRACE ("BFD_RELOC_386_TLS_LE_32");
55fd94b0 293 return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
13ae64f3
JJ
294
295 case BFD_RELOC_386_TLS_DTPMOD32:
296 TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
55fd94b0 297 return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
13ae64f3
JJ
298
299 case BFD_RELOC_386_TLS_DTPOFF32:
300 TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
55fd94b0 301 return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
13ae64f3
JJ
302
303 case BFD_RELOC_386_TLS_TPOFF32:
304 TRACE ("BFD_RELOC_386_TLS_TPOFF32");
55fd94b0 305 return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
13ae64f3 306
67a4f2b7
AO
307 case BFD_RELOC_386_TLS_GOTDESC:
308 TRACE ("BFD_RELOC_386_TLS_GOTDESC");
309 return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
310
311 case BFD_RELOC_386_TLS_DESC_CALL:
312 TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
313 return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
314
315 case BFD_RELOC_386_TLS_DESC:
316 TRACE ("BFD_RELOC_386_TLS_DESC");
317 return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
318
252b5132
RH
319 case BFD_RELOC_VTABLE_INHERIT:
320 TRACE ("BFD_RELOC_VTABLE_INHERIT");
55fd94b0 321 return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
252b5132
RH
322
323 case BFD_RELOC_VTABLE_ENTRY:
324 TRACE ("BFD_RELOC_VTABLE_ENTRY");
55fd94b0 325 return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
252b5132
RH
326
327 default:
328 break;
329 }
330
331 TRACE ("Unknown");
332 return 0;
333}
334
157090f7
AM
335static reloc_howto_type *
336elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
337 const char *r_name)
338{
339 unsigned int i;
340
341 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
342 if (elf_howto_table[i].name != NULL
343 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
344 return &elf_howto_table[i];
345
346 return NULL;
347}
348
142411ca
L
349static reloc_howto_type *
350elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
252b5132 351{
dc47f327
AM
352 unsigned int indx;
353
354 if ((indx = r_type) >= R_386_standard
355 && ((indx = r_type - R_386_ext_offset) - R_386_standard
356 >= R_386_ext - R_386_standard)
13ae64f3
JJ
357 && ((indx = r_type - R_386_tls_offset) - R_386_ext
358 >= R_386_tls - R_386_ext)
359 && ((indx = r_type - R_386_vt_offset) - R_386_tls
360 >= R_386_vt - R_386_tls))
252b5132 361 {
d003868e
AM
362 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
363 abfd, (int) r_type);
55fd94b0 364 indx = R_386_NONE;
252b5132 365 }
142411ca
L
366 BFD_ASSERT (elf_howto_table [indx].type == r_type);
367 return &elf_howto_table[indx];
368}
369
370static void
371elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
372 arelent *cache_ptr,
373 Elf_Internal_Rela *dst)
374{
375 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
376 cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type);
252b5132
RH
377}
378
379/* Return whether a symbol name implies a local label. The UnixWare
380 2.1 cc generates temporary symbols that start with .X, so we
381 recognize them here. FIXME: do other SVR4 compilers also use .X?.
382 If so, we should move the .X recognition into
383 _bfd_elf_is_local_label_name. */
384
b34976b6 385static bfd_boolean
55fd94b0 386elf_i386_is_local_label_name (bfd *abfd, const char *name)
252b5132
RH
387{
388 if (name[0] == '.' && name[1] == 'X')
b34976b6 389 return TRUE;
252b5132
RH
390
391 return _bfd_elf_is_local_label_name (abfd, name);
392}
393\f
38701953 394/* Support for core dump NOTE sections. */
61adc1a4 395
b34976b6 396static bfd_boolean
55fd94b0 397elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
38701953
AM
398{
399 int offset;
eea6121a 400 size_t size;
38701953 401
61adc1a4 402 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
38701953 403 {
61adc1a4
NC
404 int pr_version = bfd_get_32 (abfd, note->descdata);
405
406 if (pr_version != 1)
407 return FALSE;
408
409 /* pr_cursig */
410 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 20);
411
412 /* pr_pid */
413 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
414
415 /* pr_reg */
416 offset = 28;
eea6121a 417 size = bfd_get_32 (abfd, note->descdata + 8);
61adc1a4
NC
418 }
419 else
420 {
421 switch (note->descsz)
422 {
423 default:
424 return FALSE;
38701953 425
61adc1a4
NC
426 case 144: /* Linux/i386 */
427 /* pr_cursig */
428 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
38701953 429
61adc1a4
NC
430 /* pr_pid */
431 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
38701953 432
61adc1a4
NC
433 /* pr_reg */
434 offset = 72;
eea6121a 435 size = 68;
38701953 436
61adc1a4
NC
437 break;
438 }
38701953
AM
439 }
440
441 /* Make a ".reg/999" section. */
442 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 443 size, note->descpos + offset);
38701953
AM
444}
445
b34976b6 446static bfd_boolean
55fd94b0 447elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
38701953 448{
61adc1a4 449 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
38701953 450 {
61adc1a4
NC
451 int pr_version = bfd_get_32 (abfd, note->descdata);
452
453 if (pr_version != 1)
b34976b6 454 return FALSE;
38701953 455
61adc1a4
NC
456 elf_tdata (abfd)->core_program
457 = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
458 elf_tdata (abfd)->core_command
459 = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
460 }
461 else
462 {
463 switch (note->descsz)
464 {
465 default:
466 return FALSE;
467
468 case 124: /* Linux/i386 elf_prpsinfo. */
469 elf_tdata (abfd)->core_program
470 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
471 elf_tdata (abfd)->core_command
472 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
473 }
38701953
AM
474 }
475
476 /* Note that for some reason, a spurious space is tacked
477 onto the end of the args in some (at least one anyway)
478 implementations, so strip it off if it exists. */
38701953
AM
479 {
480 char *command = elf_tdata (abfd)->core_command;
481 int n = strlen (command);
482
483 if (0 < n && command[n - 1] == ' ')
484 command[n - 1] = '\0';
485 }
486
b34976b6 487 return TRUE;
38701953
AM
488}
489\f
490/* Functions for the i386 ELF linker.
491
492 In order to gain some understanding of code in this file without
493 knowing all the intricate details of the linker, note the
494 following:
495
496 Functions named elf_i386_* are called by external routines, other
497 functions are only called locally. elf_i386_* functions appear
498 in this file more or less in the order in which they are called
499 from external routines. eg. elf_i386_check_relocs is called
500 early in the link process, elf_i386_finish_dynamic_sections is
501 one of the last functions. */
502
252b5132
RH
503
504/* The name of the dynamic interpreter. This is put in the .interp
505 section. */
506
507#define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
508
a23b6845
AM
509/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
510 copying dynamic variables from a shared lib into an app's dynbss
511 section, and instead use a dynamic relocation to point into the
512 shared lib. */
513#define ELIMINATE_COPY_RELOCS 1
514
252b5132
RH
515/* The size in bytes of an entry in the procedure linkage table. */
516
517#define PLT_ENTRY_SIZE 16
518
519/* The first entry in an absolute procedure linkage table looks like
eac338cf
PB
520 this. See the SVR4 ABI i386 supplement to see how this works.
521 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
252b5132 522
eac338cf 523static const bfd_byte elf_i386_plt0_entry[12] =
252b5132
RH
524{
525 0xff, 0x35, /* pushl contents of address */
526 0, 0, 0, 0, /* replaced with address of .got + 4. */
527 0xff, 0x25, /* jmp indirect */
eac338cf 528 0, 0, 0, 0 /* replaced with address of .got + 8. */
252b5132
RH
529};
530
531/* Subsequent entries in an absolute procedure linkage table look like
532 this. */
533
534static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
535{
536 0xff, 0x25, /* jmp indirect */
537 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
538 0x68, /* pushl immediate */
539 0, 0, 0, 0, /* replaced with offset into relocation table. */
540 0xe9, /* jmp relative */
541 0, 0, 0, 0 /* replaced with offset to start of .plt. */
542};
543
eac338cf
PB
544/* The first entry in a PIC procedure linkage table look like this.
545 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
252b5132 546
eac338cf 547static const bfd_byte elf_i386_pic_plt0_entry[12] =
252b5132
RH
548{
549 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
eac338cf 550 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */
252b5132
RH
551};
552
553/* Subsequent entries in a PIC procedure linkage table look like this. */
554
555static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
556{
557 0xff, 0xa3, /* jmp *offset(%ebx) */
558 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
559 0x68, /* pushl immediate */
560 0, 0, 0, 0, /* replaced with offset into relocation table. */
561 0xe9, /* jmp relative */
562 0, 0, 0, 0 /* replaced with offset to start of .plt. */
563};
564
eac338cf
PB
565/* On VxWorks, the .rel.plt.unloaded section has absolute relocations
566 for the PLTResolve stub and then for each PLT entry. */
567#define PLTRESOLVE_RELOCS_SHLIB 0
568#define PLTRESOLVE_RELOCS 2
569#define PLT_NON_JUMP_SLOT_RELOCS 2
570
252b5132 571/* The i386 linker needs to keep track of the number of relocs that it
ffb2e45b
AM
572 decides to copy as dynamic relocs in check_relocs for each symbol.
573 This is so that it can later discard them if they are found to be
574 unnecessary. We store the information in a field extending the
575 regular ELF linker hash table. */
252b5132 576
ffb2e45b 577struct elf_i386_dyn_relocs
252b5132 578{
ffb2e45b 579 struct elf_i386_dyn_relocs *next;
0c715baa
AM
580
581 /* The input section of the reloc. */
582 asection *sec;
583
584 /* Total number of relocs copied for the input section. */
252b5132 585 bfd_size_type count;
0c715baa
AM
586
587 /* Number of pc-relative relocs copied for the input section. */
588 bfd_size_type pc_count;
252b5132
RH
589};
590
591/* i386 ELF linker hash entry. */
592
593struct elf_i386_link_hash_entry
594{
ebe50bae 595 struct elf_link_hash_entry elf;
252b5132 596
0c715baa 597 /* Track dynamic relocs copied for this symbol. */
ffb2e45b 598 struct elf_i386_dyn_relocs *dyn_relocs;
13ae64f3 599
37e55690
JJ
600#define GOT_UNKNOWN 0
601#define GOT_NORMAL 1
602#define GOT_TLS_GD 2
603#define GOT_TLS_IE 4
604#define GOT_TLS_IE_POS 5
605#define GOT_TLS_IE_NEG 6
606#define GOT_TLS_IE_BOTH 7
67a4f2b7
AO
607#define GOT_TLS_GDESC 8
608#define GOT_TLS_GD_BOTH_P(type) \
609 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
610#define GOT_TLS_GD_P(type) \
611 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
612#define GOT_TLS_GDESC_P(type) \
613 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
614#define GOT_TLS_GD_ANY_P(type) \
615 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
37e55690 616 unsigned char tls_type;
67a4f2b7
AO
617
618 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
619 starting at the end of the jump table. */
620 bfd_vma tlsdesc_got;
13ae64f3
JJ
621};
622
623#define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
624
625struct elf_i386_obj_tdata
626{
627 struct elf_obj_tdata root;
628
629 /* tls_type for each local got entry. */
630 char *local_got_tls_type;
67a4f2b7
AO
631
632 /* GOTPLT entries for TLS descriptors. */
633 bfd_vma *local_tlsdesc_gotent;
252b5132
RH
634};
635
13ae64f3
JJ
636#define elf_i386_tdata(abfd) \
637 ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
638
639#define elf_i386_local_got_tls_type(abfd) \
640 (elf_i386_tdata (abfd)->local_got_tls_type)
641
67a4f2b7
AO
642#define elf_i386_local_tlsdesc_gotent(abfd) \
643 (elf_i386_tdata (abfd)->local_tlsdesc_gotent)
644
0ffa91dd
NC
645#define is_i386_elf(bfd) \
646 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
647 && elf_tdata (bfd) != NULL \
648 && elf_object_id (bfd) == I386_ELF_TDATA)
649
b34976b6 650static bfd_boolean
55fd94b0 651elf_i386_mkobject (bfd *abfd)
13ae64f3 652{
0ffa91dd
NC
653 return bfd_elf_allocate_object (abfd, sizeof (struct elf_i386_obj_tdata),
654 I386_ELF_TDATA);
13ae64f3 655}
cedb70c5 656
252b5132
RH
657/* i386 ELF linker hash table. */
658
659struct elf_i386_link_hash_table
660{
ebe50bae 661 struct elf_link_hash_table elf;
252b5132 662
6725bdbf
AM
663 /* Short-cuts to get to dynamic linker sections. */
664 asection *sgot;
665 asection *sgotplt;
666 asection *srelgot;
667 asection *splt;
668 asection *srelplt;
669 asection *sdynbss;
670 asection *srelbss;
9635fe29 671
eac338cf
PB
672 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks. */
673 asection *srelplt2;
674
eac338cf
PB
675 /* True if the target system is VxWorks. */
676 int is_vxworks;
ec338859 677
eac338cf
PB
678 /* Value used to fill the last word of the first plt entry. */
679 bfd_byte plt0_pad_byte;
9635fe29 680
5ae0bfb6
RS
681 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. */
682 bfd_vma next_tls_desc_index;
683
13ae64f3
JJ
684 union {
685 bfd_signed_vma refcount;
686 bfd_vma offset;
687 } tls_ldm_got;
688
67a4f2b7
AO
689 /* The amount of space used by the reserved portion of the sgotplt
690 section, plus whatever space is used by the jump slots. */
691 bfd_vma sgotplt_jump_table_size;
692
ec338859
AM
693 /* Small local sym to section mapping cache. */
694 struct sym_sec_cache sym_sec;
6725bdbf 695};
252b5132
RH
696
697/* Get the i386 ELF linker hash table from a link_info structure. */
698
699#define elf_i386_hash_table(p) \
700 ((struct elf_i386_link_hash_table *) ((p)->hash))
701
67a4f2b7 702#define elf_i386_compute_jump_table_size(htab) \
5ae0bfb6 703 ((htab)->next_tls_desc_index * 4)
67a4f2b7 704
252b5132
RH
705/* Create an entry in an i386 ELF linker hash table. */
706
707static struct bfd_hash_entry *
55fd94b0
AM
708link_hash_newfunc (struct bfd_hash_entry *entry,
709 struct bfd_hash_table *table,
710 const char *string)
252b5132 711{
252b5132
RH
712 /* Allocate the structure if it has not already been allocated by a
713 subclass. */
ebe50bae
AM
714 if (entry == NULL)
715 {
716 entry = bfd_hash_allocate (table,
717 sizeof (struct elf_i386_link_hash_entry));
718 if (entry == NULL)
719 return entry;
720 }
252b5132
RH
721
722 /* Call the allocation method of the superclass. */
ebe50bae
AM
723 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
724 if (entry != NULL)
252b5132 725 {
ebe50bae
AM
726 struct elf_i386_link_hash_entry *eh;
727
728 eh = (struct elf_i386_link_hash_entry *) entry;
729 eh->dyn_relocs = NULL;
13ae64f3 730 eh->tls_type = GOT_UNKNOWN;
67a4f2b7 731 eh->tlsdesc_got = (bfd_vma) -1;
252b5132
RH
732 }
733
ebe50bae 734 return entry;
252b5132
RH
735}
736
737/* Create an i386 ELF linker hash table. */
738
739static struct bfd_link_hash_table *
55fd94b0 740elf_i386_link_hash_table_create (bfd *abfd)
252b5132
RH
741{
742 struct elf_i386_link_hash_table *ret;
dc810e39 743 bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
252b5132 744
55fd94b0 745 ret = bfd_malloc (amt);
ebe50bae 746 if (ret == NULL)
252b5132
RH
747 return NULL;
748
66eb6687
AM
749 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
750 sizeof (struct elf_i386_link_hash_entry)))
252b5132 751 {
e2d34d7d 752 free (ret);
252b5132
RH
753 return NULL;
754 }
755
6725bdbf
AM
756 ret->sgot = NULL;
757 ret->sgotplt = NULL;
758 ret->srelgot = NULL;
759 ret->splt = NULL;
760 ret->srelplt = NULL;
761 ret->sdynbss = NULL;
762 ret->srelbss = NULL;
7a624474 763 ret->tls_ldm_got.refcount = 0;
5ae0bfb6 764 ret->next_tls_desc_index = 0;
67a4f2b7 765 ret->sgotplt_jump_table_size = 0;
ec338859 766 ret->sym_sec.abfd = NULL;
eac338cf
PB
767 ret->is_vxworks = 0;
768 ret->srelplt2 = NULL;
eac338cf 769 ret->plt0_pad_byte = 0;
6725bdbf 770
ebe50bae 771 return &ret->elf.root;
252b5132
RH
772}
773
6725bdbf
AM
774/* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
775 shortcuts to them in our hash table. */
776
b34976b6 777static bfd_boolean
55fd94b0 778create_got_section (bfd *dynobj, struct bfd_link_info *info)
6725bdbf
AM
779{
780 struct elf_i386_link_hash_table *htab;
781
782 if (! _bfd_elf_create_got_section (dynobj, info))
b34976b6 783 return FALSE;
6725bdbf
AM
784
785 htab = elf_i386_hash_table (info);
786 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
787 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
788 if (!htab->sgot || !htab->sgotplt)
789 abort ();
790
3496cb2a
L
791 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rel.got",
792 (SEC_ALLOC | SEC_LOAD
793 | SEC_HAS_CONTENTS
794 | SEC_IN_MEMORY
795 | SEC_LINKER_CREATED
796 | SEC_READONLY));
6725bdbf 797 if (htab->srelgot == NULL
6725bdbf 798 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
b34976b6
AM
799 return FALSE;
800 return TRUE;
6725bdbf
AM
801}
802
803/* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
804 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
805 hash table. */
806
b34976b6 807static bfd_boolean
55fd94b0 808elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
6725bdbf
AM
809{
810 struct elf_i386_link_hash_table *htab;
811
812 htab = elf_i386_hash_table (info);
813 if (!htab->sgot && !create_got_section (dynobj, info))
b34976b6 814 return FALSE;
6725bdbf
AM
815
816 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 817 return FALSE;
6725bdbf
AM
818
819 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
820 htab->srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
821 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
822 if (!info->shared)
823 htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
824
825 if (!htab->splt || !htab->srelplt || !htab->sdynbss
826 || (!info->shared && !htab->srelbss))
827 abort ();
828
711de32c
RS
829 if (htab->is_vxworks
830 && !elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
831 return FALSE;
eac338cf 832
b34976b6 833 return TRUE;
6725bdbf
AM
834}
835
ebe50bae
AM
836/* Copy the extra info we tack onto an elf_link_hash_entry. */
837
51b64d56 838static void
fcfa13d2 839elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
55fd94b0
AM
840 struct elf_link_hash_entry *dir,
841 struct elf_link_hash_entry *ind)
ebe50bae
AM
842{
843 struct elf_i386_link_hash_entry *edir, *eind;
844
845 edir = (struct elf_i386_link_hash_entry *) dir;
846 eind = (struct elf_i386_link_hash_entry *) ind;
847
bbd7ec4a 848 if (eind->dyn_relocs != NULL)
ebe50bae 849 {
bbd7ec4a
AM
850 if (edir->dyn_relocs != NULL)
851 {
852 struct elf_i386_dyn_relocs **pp;
853 struct elf_i386_dyn_relocs *p;
854
fcfa13d2 855 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
856 list. Merge any entries against the same section. */
857 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
858 {
859 struct elf_i386_dyn_relocs *q;
860
861 for (q = edir->dyn_relocs; q != NULL; q = q->next)
862 if (q->sec == p->sec)
863 {
864 q->pc_count += p->pc_count;
865 q->count += p->count;
866 *pp = p->next;
867 break;
868 }
869 if (q == NULL)
870 pp = &p->next;
871 }
872 *pp = edir->dyn_relocs;
873 }
874
ebe50bae
AM
875 edir->dyn_relocs = eind->dyn_relocs;
876 eind->dyn_relocs = NULL;
877 }
ebe50bae 878
cd67d266
JJ
879 if (ind->root.type == bfd_link_hash_indirect
880 && dir->got.refcount <= 0)
881 {
882 edir->tls_type = eind->tls_type;
883 eind->tls_type = GOT_UNKNOWN;
884 }
81848ca0
AM
885
886 if (ELIMINATE_COPY_RELOCS
887 && ind->root.type != bfd_link_hash_indirect
f5385ebf
AM
888 && dir->dynamic_adjusted)
889 {
890 /* If called to transfer flags for a weakdef during processing
891 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
892 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
893 dir->ref_dynamic |= ind->ref_dynamic;
894 dir->ref_regular |= ind->ref_regular;
895 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
896 dir->needs_plt |= ind->needs_plt;
897 dir->pointer_equality_needed |= ind->pointer_equality_needed;
898 }
81848ca0 899 else
fcfa13d2 900 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
ebe50bae
AM
901}
902
142411ca
L
903typedef union
904 {
905 unsigned char c[2];
906 uint16_t i;
907 }
908i386_opcode16;
909
910/* Return TRUE if the TLS access code sequence support transition
911 from R_TYPE. */
912
913static bfd_boolean
914elf_i386_check_tls_transition (bfd *abfd, asection *sec,
915 bfd_byte *contents,
916 Elf_Internal_Shdr *symtab_hdr,
917 struct elf_link_hash_entry **sym_hashes,
918 unsigned int r_type,
919 const Elf_Internal_Rela *rel,
920 const Elf_Internal_Rela *relend)
13ae64f3 921{
142411ca
L
922 unsigned int val, type;
923 unsigned long r_symndx;
924 struct elf_link_hash_entry *h;
925 bfd_vma offset;
926
927 /* Get the section contents. */
928 if (contents == NULL)
929 {
930 if (elf_section_data (sec)->this_hdr.contents != NULL)
931 contents = elf_section_data (sec)->this_hdr.contents;
932 else
933 {
934 /* FIXME: How to better handle error condition? */
935 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
936 return FALSE;
13ae64f3 937
142411ca
L
938 /* Cache the section contents for elf_link_input_bfd. */
939 elf_section_data (sec)->this_hdr.contents = contents;
940 }
941 }
942
943 offset = rel->r_offset;
13ae64f3 944 switch (r_type)
142411ca
L
945 {
946 case R_386_TLS_GD:
947 case R_386_TLS_LDM:
948 if (offset < 2 || (rel + 1) >= relend)
949 return FALSE;
950
951 type = bfd_get_8 (abfd, contents + offset - 2);
952 if (r_type == R_386_TLS_GD)
953 {
954 /* Check transition from LD access model. Only
955 leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr
956 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop
957 can transit to different access model. */
958 if ((offset + 10) > sec->size ||
959 (type != 0x8d && type != 0x04))
960 return FALSE;
961
962 val = bfd_get_8 (abfd, contents + offset - 1);
963 if (type == 0x04)
964 {
965 /* leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr */
966 if (offset < 3)
967 return FALSE;
968
969 if (bfd_get_8 (abfd, contents + offset - 3) != 0x8d)
970 return FALSE;
971
972 if ((val & 0xc7) != 0x05 || val == (4 << 3))
973 return FALSE;
974 }
975 else
976 {
977 /* leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop */
978 if ((val & 0xf8) != 0x80 || (val & 7) == 4)
979 return FALSE;
980
981 if (bfd_get_8 (abfd, contents + offset + 9) != 0x90)
982 return FALSE;
983 }
984 }
985 else
986 {
987 /* Check transition from LD access model. Only
988 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr
989 can transit to different access model. */
990 if (type != 0x8d || (offset + 9) > sec->size)
991 return FALSE;
992
993 val = bfd_get_8 (abfd, contents + offset - 1);
994 if ((val & 0xf8) != 0x80 || (val & 7) == 4)
995 return FALSE;
996 }
997
998 if (bfd_get_8 (abfd, contents + offset + 4) != 0xe8)
999 return FALSE;
1000
1001 r_symndx = ELF32_R_SYM (rel[1].r_info);
1002 if (r_symndx < symtab_hdr->sh_info)
1003 return FALSE;
1004
1005 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1006 return (h != NULL
1007 && h->root.root.string != NULL
1008 && (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
1009 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32)
1010 && (strcmp (h->root.root.string, "___tls_get_addr") == 0));
1011
1012 case R_386_TLS_IE:
1013 /* Check transition from IE access model:
1014 movl foo@indntpoff(%rip), %eax
1015 movl foo@indntpoff(%rip), %reg
1016 addl foo@indntpoff(%rip), %reg
1017 */
1018
1019 if (offset < 1 || (offset + 4) > sec->size)
1020 return FALSE;
1021
1022 /* Check "movl foo@tpoff(%rip), %eax" first. */
1023 val = bfd_get_8 (abfd, contents + offset - 1);
1024 if (val == 0xa1)
1025 return TRUE;
1026
1027 if (offset < 2)
1028 return FALSE;
1029
1030 /* Check movl|addl foo@tpoff(%rip), %reg. */
1031 type = bfd_get_8 (abfd, contents + offset - 2);
1032 return ((type == 0x8b || type == 0x03)
1033 && (val & 0xc7) == 0x05);
1034
1035 case R_386_TLS_GOTIE:
1036 case R_386_TLS_IE_32:
1037 /* Check transition from {IE_32,GOTIE} access model:
1038 subl foo@{tpoff,gontoff}(%reg1), %reg2
1039 movl foo@{tpoff,gontoff}(%reg1), %reg2
1040 addl foo@{tpoff,gontoff}(%reg1), %reg2
1041 */
1042
1043 if (offset < 2 || (offset + 4) > sec->size)
1044 return FALSE;
1045
1046 val = bfd_get_8 (abfd, contents + offset - 1);
1047 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1048 return FALSE;
1049
1050 type = bfd_get_8 (abfd, contents + offset - 2);
1051 return type == 0x8b || type == 0x2b || type == 0x03;
1052
1053 case R_386_TLS_GOTDESC:
1054 /* Check transition from GDesc access model:
1055 leal x@tlsdesc(%ebx), %eax
1056
1057 Make sure it's a leal adding ebx to a 32-bit offset
1058 into any register, although it's probably almost always
1059 going to be eax. */
1060
1061 if (offset < 2 || (offset + 4) > sec->size)
1062 return FALSE;
1063
1064 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1065 return FALSE;
1066
1067 val = bfd_get_8 (abfd, contents + offset - 1);
1068 return (val & 0xc7) == 0x83;
1069
1070 case R_386_TLS_DESC_CALL:
1071 /* Check transition from GDesc access model:
1072 call *x@tlsdesc(%rax)
1073 */
1074 if (offset + 2 <= sec->size)
1075 {
1076 /* Make sure that it's a call *x@tlsdesc(%rax). */
1077 static i386_opcode16 call = { { 0xff, 0x10 } };
1078 return bfd_get_16 (abfd, contents + offset) == call.i;
1079 }
1080
1081 return FALSE;
1082
1083 default:
1084 abort ();
1085 }
1086}
1087
1088/* Return TRUE if the TLS access transition is OK or no transition
1089 will be performed. Update R_TYPE if there is a transition. */
1090
1091static bfd_boolean
1092elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1093 asection *sec, bfd_byte *contents,
1094 Elf_Internal_Shdr *symtab_hdr,
1095 struct elf_link_hash_entry **sym_hashes,
1096 unsigned int *r_type, int tls_type,
1097 const Elf_Internal_Rela *rel,
1098 const Elf_Internal_Rela *relend,
1099 struct elf_link_hash_entry *h)
1100{
1101 unsigned int from_type = *r_type;
1102 unsigned int to_type = from_type;
1103 bfd_boolean check = TRUE;
1104
1105 switch (from_type)
13ae64f3
JJ
1106 {
1107 case R_386_TLS_GD:
67a4f2b7
AO
1108 case R_386_TLS_GOTDESC:
1109 case R_386_TLS_DESC_CALL:
13ae64f3 1110 case R_386_TLS_IE_32:
37e55690
JJ
1111 case R_386_TLS_IE:
1112 case R_386_TLS_GOTIE:
142411ca
L
1113 if (!info->shared)
1114 {
1115 if (h == NULL)
1116 to_type = R_386_TLS_LE_32;
1117 else if (from_type != R_386_TLS_IE
1118 && from_type != R_386_TLS_GOTIE)
1119 to_type = R_386_TLS_IE_32;
1120 }
1121
1122 /* When we are called from elf_i386_relocate_section, CONTENTS
1123 isn't NULL and there may be additional transitions based on
1124 TLS_TYPE. */
1125 if (contents != NULL)
1126 {
1127 unsigned int new_to_type = to_type;
1128
1129 if (!info->shared
1130 && h != NULL
1131 && h->dynindx == -1
1132 && (tls_type & GOT_TLS_IE))
1133 new_to_type = R_386_TLS_LE_32;
1134
1135 if (to_type == R_386_TLS_GD
1136 || to_type == R_386_TLS_GOTDESC
1137 || to_type == R_386_TLS_DESC_CALL)
1138 {
1139 if (tls_type == GOT_TLS_IE_POS)
1140 new_to_type = R_386_TLS_GOTIE;
1141 else if (tls_type & GOT_TLS_IE)
1142 new_to_type = R_386_TLS_IE_32;
1143 }
1144
1145 /* We checked the transition before when we were called from
1146 elf_i386_check_relocs. We only want to check the new
1147 transition which hasn't been checked before. */
1148 check = new_to_type != to_type && from_type == to_type;
1149 to_type = new_to_type;
1150 }
1151
1152 break;
1153
13ae64f3 1154 case R_386_TLS_LDM:
142411ca
L
1155 if (!info->shared)
1156 to_type = R_386_TLS_LE_32;
1157 break;
1158
1159 default:
1160 return TRUE;
1161 }
1162
1163 /* Return TRUE if there is no transition. */
1164 if (from_type == to_type)
1165 return TRUE;
1166
1167 /* Check if the transition can be performed. */
1168 if (check
1169 && ! elf_i386_check_tls_transition (abfd, sec, contents,
1170 symtab_hdr, sym_hashes,
1171 from_type, rel, relend))
1172 {
2f629d23 1173 reloc_howto_type *from, *to;
142411ca
L
1174
1175 from = elf_i386_rtype_to_howto (abfd, from_type);
1176 to = elf_i386_rtype_to_howto (abfd, to_type);
1177
1178 (*_bfd_error_handler)
1179 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1180 "in section `%A' failed"),
1181 abfd, sec, from->name, to->name,
1182 h ? h->root.root.string : "a local symbol",
1183 (unsigned long) rel->r_offset);
1184 bfd_set_error (bfd_error_bad_value);
1185 return FALSE;
13ae64f3
JJ
1186 }
1187
142411ca
L
1188 *r_type = to_type;
1189 return TRUE;
13ae64f3
JJ
1190}
1191
252b5132 1192/* Look through the relocs for a section during the first phase, and
0ac8d2ca
AM
1193 calculate needed space in the global offset table, procedure linkage
1194 table, and dynamic reloc sections. */
252b5132 1195
b34976b6 1196static bfd_boolean
55fd94b0
AM
1197elf_i386_check_relocs (bfd *abfd,
1198 struct bfd_link_info *info,
1199 asection *sec,
1200 const Elf_Internal_Rela *relocs)
252b5132 1201{
6725bdbf 1202 struct elf_i386_link_hash_table *htab;
252b5132
RH
1203 Elf_Internal_Shdr *symtab_hdr;
1204 struct elf_link_hash_entry **sym_hashes;
252b5132
RH
1205 const Elf_Internal_Rela *rel;
1206 const Elf_Internal_Rela *rel_end;
252b5132
RH
1207 asection *sreloc;
1208
1049f94e 1209 if (info->relocatable)
b34976b6 1210 return TRUE;
252b5132 1211
0ffa91dd
NC
1212 BFD_ASSERT (is_i386_elf (abfd));
1213
6725bdbf 1214 htab = elf_i386_hash_table (info);
0ffa91dd 1215 symtab_hdr = &elf_symtab_hdr (abfd);
252b5132 1216 sym_hashes = elf_sym_hashes (abfd);
252b5132 1217
252b5132
RH
1218 sreloc = NULL;
1219
1220 rel_end = relocs + sec->reloc_count;
1221 for (rel = relocs; rel < rel_end; rel++)
1222 {
13ae64f3 1223 unsigned int r_type;
252b5132
RH
1224 unsigned long r_symndx;
1225 struct elf_link_hash_entry *h;
1226
1227 r_symndx = ELF32_R_SYM (rel->r_info);
13ae64f3 1228 r_type = ELF32_R_TYPE (rel->r_info);
252b5132 1229
d9bc7a44 1230 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
f5f31454 1231 {
d003868e
AM
1232 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1233 abfd,
8f615d07 1234 r_symndx);
b34976b6 1235 return FALSE;
f5f31454
L
1236 }
1237
252b5132
RH
1238 if (r_symndx < symtab_hdr->sh_info)
1239 h = NULL;
1240 else
71cb9464
L
1241 {
1242 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1243 while (h->root.type == bfd_link_hash_indirect
1244 || h->root.type == bfd_link_hash_warning)
1245 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1246 }
252b5132 1247
142411ca
L
1248 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1249 symtab_hdr, sym_hashes,
1250 &r_type, GOT_UNKNOWN,
1251 rel, rel_end, h))
1252 return FALSE;
13ae64f3
JJ
1253
1254 switch (r_type)
252b5132 1255 {
37e55690
JJ
1256 case R_386_TLS_LDM:
1257 htab->tls_ldm_got.refcount += 1;
1258 goto create_got;
1259
1260 case R_386_PLT32:
1261 /* This symbol requires a procedure linkage table entry. We
1262 actually build the entry in adjust_dynamic_symbol,
1263 because this might be a case of linking PIC code which is
1264 never referenced by a dynamic object, in which case we
1265 don't need to generate a procedure linkage table entry
1266 after all. */
1267
1268 /* If this is a local symbol, we resolve it directly without
1269 creating a procedure linkage table entry. */
1270 if (h == NULL)
1271 continue;
1272
f5385ebf 1273 h->needs_plt = 1;
37e55690
JJ
1274 h->plt.refcount += 1;
1275 break;
1276
13ae64f3 1277 case R_386_TLS_IE_32:
37e55690
JJ
1278 case R_386_TLS_IE:
1279 case R_386_TLS_GOTIE:
13ae64f3
JJ
1280 if (info->shared)
1281 info->flags |= DF_STATIC_TLS;
37e55690
JJ
1282 /* Fall through */
1283
252b5132 1284 case R_386_GOT32:
13ae64f3 1285 case R_386_TLS_GD:
67a4f2b7
AO
1286 case R_386_TLS_GOTDESC:
1287 case R_386_TLS_DESC_CALL:
252b5132 1288 /* This symbol requires a global offset table entry. */
13ae64f3
JJ
1289 {
1290 int tls_type, old_tls_type;
1291
1292 switch (r_type)
1293 {
1294 default:
1295 case R_386_GOT32: tls_type = GOT_NORMAL; break;
1296 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
67a4f2b7
AO
1297 case R_386_TLS_GOTDESC:
1298 case R_386_TLS_DESC_CALL:
1299 tls_type = GOT_TLS_GDESC; break;
37e55690
JJ
1300 case R_386_TLS_IE_32:
1301 if (ELF32_R_TYPE (rel->r_info) == r_type)
1302 tls_type = GOT_TLS_IE_NEG;
1303 else
ebcfb3c0
JJ
1304 /* If this is a GD->IE transition, we may use either of
1305 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
37e55690
JJ
1306 tls_type = GOT_TLS_IE;
1307 break;
1308 case R_386_TLS_IE:
1309 case R_386_TLS_GOTIE:
1310 tls_type = GOT_TLS_IE_POS; break;
13ae64f3
JJ
1311 }
1312
1313 if (h != NULL)
1314 {
1315 h->got.refcount += 1;
1316 old_tls_type = elf_i386_hash_entry(h)->tls_type;
1317 }
1318 else
1319 {
1320 bfd_signed_vma *local_got_refcounts;
1321
1322 /* This is a global offset table entry for a local symbol. */
1323 local_got_refcounts = elf_local_got_refcounts (abfd);
1324 if (local_got_refcounts == NULL)
1325 {
1326 bfd_size_type size;
1327
1328 size = symtab_hdr->sh_info;
67a4f2b7
AO
1329 size *= (sizeof (bfd_signed_vma)
1330 + sizeof (bfd_vma) + sizeof(char));
55fd94b0 1331 local_got_refcounts = bfd_zalloc (abfd, size);
13ae64f3 1332 if (local_got_refcounts == NULL)
b34976b6 1333 return FALSE;
13ae64f3 1334 elf_local_got_refcounts (abfd) = local_got_refcounts;
67a4f2b7
AO
1335 elf_i386_local_tlsdesc_gotent (abfd)
1336 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
13ae64f3 1337 elf_i386_local_got_tls_type (abfd)
67a4f2b7 1338 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
13ae64f3
JJ
1339 }
1340 local_got_refcounts[r_symndx] += 1;
1341 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
1342 }
1343
37e55690
JJ
1344 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1345 tls_type |= old_tls_type;
13ae64f3
JJ
1346 /* If a TLS symbol is accessed using IE at least once,
1347 there is no point to use dynamic model for it. */
ebcfb3c0 1348 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
67a4f2b7 1349 && (! GOT_TLS_GD_ANY_P (old_tls_type)
37e55690 1350 || (tls_type & GOT_TLS_IE) == 0))
13ae64f3 1351 {
67a4f2b7 1352 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
37e55690 1353 tls_type = old_tls_type;
67a4f2b7
AO
1354 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1355 && GOT_TLS_GD_ANY_P (tls_type))
1356 tls_type |= old_tls_type;
13ae64f3
JJ
1357 else
1358 {
1359 (*_bfd_error_handler)
d003868e 1360 (_("%B: `%s' accessed both as normal and "
55fd94b0 1361 "thread local symbol"),
d003868e 1362 abfd,
37e55690 1363 h ? h->root.root.string : "<local>");
b34976b6 1364 return FALSE;
13ae64f3
JJ
1365 }
1366 }
1367
1368 if (old_tls_type != tls_type)
1369 {
1370 if (h != NULL)
1371 elf_i386_hash_entry (h)->tls_type = tls_type;
1372 else
1373 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
1374 }
1375 }
0ac8d2ca
AM
1376 /* Fall through */
1377
1378 case R_386_GOTOFF:
1379 case R_386_GOTPC:
13ae64f3 1380 create_got:
0ac8d2ca
AM
1381 if (htab->sgot == NULL)
1382 {
1383 if (htab->elf.dynobj == NULL)
1384 htab->elf.dynobj = abfd;
1385 if (!create_got_section (htab->elf.dynobj, info))
b34976b6 1386 return FALSE;
0ac8d2ca 1387 }
37e55690
JJ
1388 if (r_type != R_386_TLS_IE)
1389 break;
1390 /* Fall through */
252b5132 1391
37e55690
JJ
1392 case R_386_TLS_LE_32:
1393 case R_386_TLS_LE:
1394 if (!info->shared)
1395 break;
bffbf940 1396 info->flags |= DF_STATIC_TLS;
b34976b6 1397 /* Fall through */
252b5132
RH
1398
1399 case R_386_32:
1400 case R_386_PC32:
12d0ee4a 1401 if (h != NULL && !info->shared)
6725bdbf 1402 {
12d0ee4a 1403 /* If this reloc is in a read-only section, we might
ebe50bae
AM
1404 need a copy reloc. We can't check reliably at this
1405 stage whether the section is read-only, as input
1406 sections have not yet been mapped to output sections.
1407 Tentatively set the flag for now, and correct in
1408 adjust_dynamic_symbol. */
f5385ebf 1409 h->non_got_ref = 1;
12d0ee4a
AM
1410
1411 /* We may need a .plt entry if the function this reloc
1412 refers to is in a shared lib. */
51b64d56 1413 h->plt.refcount += 1;
c6585bbb 1414 if (r_type != R_386_PC32)
f5385ebf 1415 h->pointer_equality_needed = 1;
6725bdbf 1416 }
7843f00e 1417
252b5132 1418 /* If we are creating a shared library, and this is a reloc
f69da49f
AM
1419 against a global symbol, or a non PC relative reloc
1420 against a local symbol, then we need to copy the reloc
1421 into the shared library. However, if we are linking with
1422 -Bsymbolic, we do not need to copy a reloc against a
1423 global symbol which is defined in an object we are
1424 including in the link (i.e., DEF_REGULAR is set). At
1425 this point we have not seen all the input files, so it is
1426 possible that DEF_REGULAR is not set now but will be set
1f655a09
L
1427 later (it is never cleared). In case of a weak definition,
1428 DEF_REGULAR may be cleared later by a strong definition in
ebe50bae 1429 a shared library. We account for that possibility below by
1f655a09
L
1430 storing information in the relocs_copied field of the hash
1431 table entry. A similar situation occurs when creating
1432 shared libraries and symbol visibility changes render the
12d0ee4a 1433 symbol local.
56882138 1434
12d0ee4a
AM
1435 If on the other hand, we are creating an executable, we
1436 may need to keep relocations for symbols satisfied by a
1437 dynamic library if we manage to avoid copy relocs for the
1438 symbol. */
1439 if ((info->shared
1440 && (sec->flags & SEC_ALLOC) != 0
13ae64f3 1441 && (r_type != R_386_PC32
12d0ee4a 1442 || (h != NULL
55255dae 1443 && (! SYMBOLIC_BIND (info, h)
12d0ee4a 1444 || h->root.type == bfd_link_hash_defweak
f5385ebf 1445 || !h->def_regular))))
a23b6845
AM
1446 || (ELIMINATE_COPY_RELOCS
1447 && !info->shared
12d0ee4a
AM
1448 && (sec->flags & SEC_ALLOC) != 0
1449 && h != NULL
12d0ee4a 1450 && (h->root.type == bfd_link_hash_defweak
f5385ebf 1451 || !h->def_regular)))
252b5132 1452 {
ec338859
AM
1453 struct elf_i386_dyn_relocs *p;
1454 struct elf_i386_dyn_relocs **head;
1455
12d0ee4a
AM
1456 /* We must copy these reloc types into the output file.
1457 Create a reloc section in dynobj and make room for
1458 this reloc. */
252b5132
RH
1459 if (sreloc == NULL)
1460 {
1461 const char *name;
0ac8d2ca 1462 bfd *dynobj;
e92d460e
AM
1463 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
1464 unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
252b5132 1465
e92d460e 1466 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
252b5132 1467 if (name == NULL)
b34976b6 1468 return FALSE;
252b5132 1469
0112cd26 1470 if (! CONST_STRNEQ (name, ".rel")
c8492176
L
1471 || strcmp (bfd_get_section_name (abfd, sec),
1472 name + 4) != 0)
1473 {
0c715baa 1474 (*_bfd_error_handler)
d003868e
AM
1475 (_("%B: bad relocation section name `%s\'"),
1476 abfd, name);
f5f31454 1477 }
252b5132 1478
0ac8d2ca
AM
1479 if (htab->elf.dynobj == NULL)
1480 htab->elf.dynobj = abfd;
1481
1482 dynobj = htab->elf.dynobj;
252b5132
RH
1483 sreloc = bfd_get_section_by_name (dynobj, name);
1484 if (sreloc == NULL)
1485 {
1486 flagword flags;
1487
252b5132
RH
1488 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1489 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1490 if ((sec->flags & SEC_ALLOC) != 0)
1491 flags |= SEC_ALLOC | SEC_LOAD;
3496cb2a
L
1492 sreloc = bfd_make_section_with_flags (dynobj,
1493 name,
1494 flags);
252b5132 1495 if (sreloc == NULL
252b5132 1496 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
b34976b6 1497 return FALSE;
252b5132 1498 }
0c715baa 1499 elf_section_data (sec)->sreloc = sreloc;
252b5132
RH
1500 }
1501
0c715baa
AM
1502 /* If this is a global symbol, we count the number of
1503 relocations we need for this symbol. */
1504 if (h != NULL)
252b5132 1505 {
ec338859 1506 head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
0c715baa
AM
1507 }
1508 else
1509 {
e81d3500 1510 void **vpp;
ec338859
AM
1511 /* Track dynamic relocs needed for local syms too.
1512 We really need local syms available to do this
1513 easily. Oh well. */
1514
1515 asection *s;
1516 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1517 sec, r_symndx);
1518 if (s == NULL)
b34976b6 1519 return FALSE;
ec338859 1520
e81d3500
DD
1521 vpp = &elf_section_data (s)->local_dynrel;
1522 head = (struct elf_i386_dyn_relocs **)vpp;
ec338859
AM
1523 }
1524
1525 p = *head;
1526 if (p == NULL || p->sec != sec)
1527 {
1528 bfd_size_type amt = sizeof *p;
55fd94b0 1529 p = bfd_alloc (htab->elf.dynobj, amt);
ec338859 1530 if (p == NULL)
b34976b6 1531 return FALSE;
ec338859
AM
1532 p->next = *head;
1533 *head = p;
1534 p->sec = sec;
1535 p->count = 0;
1536 p->pc_count = 0;
252b5132 1537 }
ec338859
AM
1538
1539 p->count += 1;
13ae64f3 1540 if (r_type == R_386_PC32)
ec338859 1541 p->pc_count += 1;
252b5132 1542 }
252b5132
RH
1543 break;
1544
1545 /* This relocation describes the C++ object vtable hierarchy.
1546 Reconstruct it for later use during GC. */
1547 case R_386_GNU_VTINHERIT:
d17e0c6e 1548 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 1549 return FALSE;
252b5132
RH
1550 break;
1551
1552 /* This relocation describes which C++ vtable entries are actually
1553 used. Record for later use during GC. */
1554 case R_386_GNU_VTENTRY:
c6aa130f
MS
1555 BFD_ASSERT (h != NULL);
1556 if (h != NULL
1557 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 1558 return FALSE;
252b5132
RH
1559 break;
1560
1561 default:
1562 break;
1563 }
1564 }
1565
b34976b6 1566 return TRUE;
252b5132
RH
1567}
1568
1569/* Return the section that should be marked against GC for a given
1570 relocation. */
1571
1572static asection *
55fd94b0 1573elf_i386_gc_mark_hook (asection *sec,
07adf181 1574 struct bfd_link_info *info,
55fd94b0
AM
1575 Elf_Internal_Rela *rel,
1576 struct elf_link_hash_entry *h,
1577 Elf_Internal_Sym *sym)
252b5132
RH
1578{
1579 if (h != NULL)
07adf181
AM
1580 switch (ELF32_R_TYPE (rel->r_info))
1581 {
1582 case R_386_GNU_VTINHERIT:
1583 case R_386_GNU_VTENTRY:
1584 return NULL;
1585 }
1586
1587 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
252b5132
RH
1588}
1589
1590/* Update the got entry reference counts for the section being removed. */
1591
b34976b6 1592static bfd_boolean
55fd94b0
AM
1593elf_i386_gc_sweep_hook (bfd *abfd,
1594 struct bfd_link_info *info,
1595 asection *sec,
1596 const Elf_Internal_Rela *relocs)
252b5132 1597{
dd5724d5
AM
1598 Elf_Internal_Shdr *symtab_hdr;
1599 struct elf_link_hash_entry **sym_hashes;
1600 bfd_signed_vma *local_got_refcounts;
1601 const Elf_Internal_Rela *rel, *relend;
dd5724d5 1602
7dda2462
TG
1603 if (info->relocatable)
1604 return TRUE;
1605
ec338859 1606 elf_section_data (sec)->local_dynrel = NULL;
dd5724d5 1607
0ffa91dd 1608 symtab_hdr = &elf_symtab_hdr (abfd);
6725bdbf
AM
1609 sym_hashes = elf_sym_hashes (abfd);
1610 local_got_refcounts = elf_local_got_refcounts (abfd);
dd5724d5
AM
1611
1612 relend = relocs + sec->reloc_count;
1613 for (rel = relocs; rel < relend; rel++)
26e41594
AM
1614 {
1615 unsigned long r_symndx;
1616 unsigned int r_type;
1617 struct elf_link_hash_entry *h = NULL;
37e55690 1618
26e41594
AM
1619 r_symndx = ELF32_R_SYM (rel->r_info);
1620 if (r_symndx >= symtab_hdr->sh_info)
1621 {
1622 struct elf_i386_link_hash_entry *eh;
1623 struct elf_i386_dyn_relocs **pp;
1624 struct elf_i386_dyn_relocs *p;
dd5724d5 1625
26e41594 1626 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3eb128b2
AM
1627 while (h->root.type == bfd_link_hash_indirect
1628 || h->root.type == bfd_link_hash_warning)
1629 h = (struct elf_link_hash_entry *) h->root.u.i.link;
26e41594 1630 eh = (struct elf_i386_link_hash_entry *) h;
0c715baa 1631
26e41594
AM
1632 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1633 if (p->sec == sec)
1634 {
1635 /* Everything must go for SEC. */
1636 *pp = p->next;
1637 break;
1638 }
1639 }
0c715baa 1640
26e41594 1641 r_type = ELF32_R_TYPE (rel->r_info);
142411ca
L
1642 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1643 symtab_hdr, sym_hashes,
1644 &r_type, GOT_UNKNOWN,
1645 rel, relend, h))
1646 return FALSE;
1647
26e41594
AM
1648 switch (r_type)
1649 {
1650 case R_386_TLS_LDM:
1651 if (elf_i386_hash_table (info)->tls_ldm_got.refcount > 0)
1652 elf_i386_hash_table (info)->tls_ldm_got.refcount -= 1;
1653 break;
0c715baa 1654
26e41594 1655 case R_386_TLS_GD:
67a4f2b7
AO
1656 case R_386_TLS_GOTDESC:
1657 case R_386_TLS_DESC_CALL:
26e41594
AM
1658 case R_386_TLS_IE_32:
1659 case R_386_TLS_IE:
1660 case R_386_TLS_GOTIE:
1661 case R_386_GOT32:
1662 if (h != NULL)
1663 {
1664 if (h->got.refcount > 0)
1665 h->got.refcount -= 1;
1666 }
1667 else if (local_got_refcounts != NULL)
1668 {
1669 if (local_got_refcounts[r_symndx] > 0)
1670 local_got_refcounts[r_symndx] -= 1;
1671 }
1672 break;
0c715baa 1673
26e41594
AM
1674 case R_386_32:
1675 case R_386_PC32:
1676 if (info->shared)
1677 break;
1678 /* Fall through */
6725bdbf 1679
26e41594
AM
1680 case R_386_PLT32:
1681 if (h != NULL)
1682 {
1683 if (h->plt.refcount > 0)
1684 h->plt.refcount -= 1;
1685 }
1686 break;
dd5724d5 1687
26e41594
AM
1688 default:
1689 break;
1690 }
1691 }
252b5132 1692
b34976b6 1693 return TRUE;
252b5132
RH
1694}
1695
1696/* Adjust a symbol defined by a dynamic object and referenced by a
1697 regular object. The current definition is in some section of the
1698 dynamic object, but we're not including those sections. We have to
1699 change the definition to something the rest of the link can
1700 understand. */
1701
b34976b6 1702static bfd_boolean
55fd94b0
AM
1703elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
1704 struct elf_link_hash_entry *h)
252b5132 1705{
6725bdbf 1706 struct elf_i386_link_hash_table *htab;
252b5132 1707 asection *s;
252b5132 1708
252b5132
RH
1709 /* If this is a function, put it in the procedure linkage table. We
1710 will fill in the contents of the procedure linkage table later,
1711 when we know the address of the .got section. */
1712 if (h->type == STT_FUNC
f5385ebf 1713 || h->needs_plt)
252b5132 1714 {
6725bdbf 1715 if (h->plt.refcount <= 0
9c7a29a3
AM
1716 || SYMBOL_CALLS_LOCAL (info, h)
1717 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1718 && h->root.type == bfd_link_hash_undefweak))
252b5132
RH
1719 {
1720 /* This case can occur if we saw a PLT32 reloc in an input
dd5724d5
AM
1721 file, but the symbol was never referred to by a dynamic
1722 object, or if all references were garbage collected. In
1723 such a case, we don't actually need to build a procedure
1724 linkage table, and we can just do a PC32 reloc instead. */
bbd7ec4a 1725 h->plt.offset = (bfd_vma) -1;
f5385ebf 1726 h->needs_plt = 0;
252b5132
RH
1727 }
1728
b34976b6 1729 return TRUE;
252b5132 1730 }
6725bdbf
AM
1731 else
1732 /* It's possible that we incorrectly decided a .plt reloc was
1733 needed for an R_386_PC32 reloc to a non-function sym in
1734 check_relocs. We can't decide accurately between function and
1735 non-function syms in check-relocs; Objects loaded later in
1736 the link may change h->type. So fix it now. */
bbd7ec4a 1737 h->plt.offset = (bfd_vma) -1;
252b5132
RH
1738
1739 /* If this is a weak symbol, and there is a real definition, the
1740 processor independent code will have arranged for us to see the
1741 real definition first, and we can just use the same value. */
f6e332e6 1742 if (h->u.weakdef != NULL)
252b5132 1743 {
f6e332e6
AM
1744 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1745 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1746 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1747 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 1748 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
f6e332e6 1749 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 1750 return TRUE;
252b5132
RH
1751 }
1752
1753 /* This is a reference to a symbol defined by a dynamic object which
1754 is not a function. */
1755
1756 /* If we are creating a shared library, we must presume that the
1757 only references to the symbol are via the global offset table.
1758 For such cases we need not do anything here; the relocations will
1759 be handled correctly by relocate_section. */
1760 if (info->shared)
b34976b6 1761 return TRUE;
252b5132 1762
7843f00e
ILT
1763 /* If there are no references to this symbol that do not use the
1764 GOT, we don't need to generate a copy reloc. */
f5385ebf 1765 if (!h->non_got_ref)
b34976b6 1766 return TRUE;
7843f00e 1767
8bd621d8
AM
1768 /* If -z nocopyreloc was given, we won't generate them either. */
1769 if (info->nocopyreloc)
1770 {
f5385ebf 1771 h->non_got_ref = 0;
b34976b6 1772 return TRUE;
8bd621d8
AM
1773 }
1774
643796e3
DJ
1775 htab = elf_i386_hash_table (info);
1776
1777 /* If there aren't any dynamic relocs in read-only sections, then
1778 we can keep the dynamic relocs and avoid the copy reloc. This
1779 doesn't work on VxWorks, where we can not have dynamic relocations
1780 (other than copy and jump slot relocations) in an executable. */
1781 if (ELIMINATE_COPY_RELOCS && !htab->is_vxworks)
ebe50bae 1782 {
a23b6845
AM
1783 struct elf_i386_link_hash_entry * eh;
1784 struct elf_i386_dyn_relocs *p;
ebe50bae 1785
a23b6845
AM
1786 eh = (struct elf_i386_link_hash_entry *) h;
1787 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1788 {
1789 s = p->sec->output_section;
1790 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1791 break;
1792 }
1793
a23b6845
AM
1794 if (p == NULL)
1795 {
f5385ebf 1796 h->non_got_ref = 0;
a23b6845
AM
1797 return TRUE;
1798 }
ebe50bae
AM
1799 }
1800
909272ee
AM
1801 if (h->size == 0)
1802 {
1803 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1804 h->root.root.string);
1805 return TRUE;
1806 }
1807
252b5132
RH
1808 /* We must allocate the symbol in our .dynbss section, which will
1809 become part of the .bss section of the executable. There will be
1810 an entry for this symbol in the .dynsym section. The dynamic
1811 object will contain position independent code, so all references
1812 from the dynamic object to this symbol will go through the global
1813 offset table. The dynamic linker will use the .dynsym entry to
1814 determine the address it must put in the global offset table, so
1815 both the dynamic object and the regular object will refer to the
1816 same memory location for the variable. */
1817
252b5132
RH
1818 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1819 copy the initial value out of the dynamic object and into the
0ac8d2ca 1820 runtime process image. */
252b5132
RH
1821 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1822 {
eea6121a 1823 htab->srelbss->size += sizeof (Elf32_External_Rel);
f5385ebf 1824 h->needs_copy = 1;
252b5132
RH
1825 }
1826
0ac8d2ca 1827 s = htab->sdynbss;
252b5132 1828
027297b7 1829 return _bfd_elf_adjust_dynamic_copy (h, s);
252b5132
RH
1830}
1831
6725bdbf 1832/* Allocate space in .plt, .got and associated reloc sections for
0c715baa 1833 dynamic relocs. */
6725bdbf 1834
b34976b6 1835static bfd_boolean
55fd94b0 1836allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
6725bdbf
AM
1837{
1838 struct bfd_link_info *info;
1839 struct elf_i386_link_hash_table *htab;
5a15f56f 1840 struct elf_i386_link_hash_entry *eh;
0c715baa 1841 struct elf_i386_dyn_relocs *p;
6725bdbf 1842
e92d460e 1843 if (h->root.type == bfd_link_hash_indirect)
b34976b6 1844 return TRUE;
6725bdbf 1845
e92d460e
AM
1846 if (h->root.type == bfd_link_hash_warning)
1847 /* When warning symbols are created, they **replace** the "real"
1848 entry in the hash table, thus we never get to see the real
1849 symbol in a hash traversal. So look at it now. */
1850 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1851
6725bdbf
AM
1852 info = (struct bfd_link_info *) inf;
1853 htab = elf_i386_hash_table (info);
1854
ebe50bae 1855 if (htab->elf.dynamic_sections_created
9c7a29a3 1856 && h->plt.refcount > 0)
6725bdbf 1857 {
5a15f56f
AM
1858 /* Make sure this symbol is output as a dynamic symbol.
1859 Undefined weak syms won't yet be marked as dynamic. */
1860 if (h->dynindx == -1
f5385ebf 1861 && !h->forced_local)
5a15f56f 1862 {
c152c796 1863 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1864 return FALSE;
5a15f56f
AM
1865 }
1866
4e795f50
AM
1867 if (info->shared
1868 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
ced53ee5 1869 {
0ac8d2ca 1870 asection *s = htab->splt;
6725bdbf 1871
ced53ee5
AM
1872 /* If this is the first .plt entry, make room for the special
1873 first entry. */
eea6121a
AM
1874 if (s->size == 0)
1875 s->size += PLT_ENTRY_SIZE;
6725bdbf 1876
eea6121a 1877 h->plt.offset = s->size;
6725bdbf 1878
ced53ee5
AM
1879 /* If this symbol is not defined in a regular file, and we are
1880 not generating a shared library, then set the symbol to this
1881 location in the .plt. This is required to make function
1882 pointers compare as equal between the normal executable and
1883 the shared library. */
1884 if (! info->shared
f5385ebf 1885 && !h->def_regular)
ced53ee5
AM
1886 {
1887 h->root.u.def.section = s;
1888 h->root.u.def.value = h->plt.offset;
1889 }
6725bdbf 1890
ced53ee5 1891 /* Make room for this entry. */
eea6121a 1892 s->size += PLT_ENTRY_SIZE;
6725bdbf 1893
ced53ee5
AM
1894 /* We also need to make an entry in the .got.plt section, which
1895 will be placed in the .got section by the linker script. */
eea6121a 1896 htab->sgotplt->size += 4;
6725bdbf 1897
6725bdbf 1898 /* We also need to make an entry in the .rel.plt section. */
eea6121a 1899 htab->srelplt->size += sizeof (Elf32_External_Rel);
5ae0bfb6 1900 htab->next_tls_desc_index++;
eac338cf
PB
1901
1902 if (htab->is_vxworks && !info->shared)
1903 {
1904 /* VxWorks has a second set of relocations for each PLT entry
1905 in executables. They go in a separate relocation section,
1906 which is processed by the kernel loader. */
1907
1908 /* There are two relocations for the initial PLT entry: an
1909 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
1910 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
1911
1912 if (h->plt.offset == PLT_ENTRY_SIZE)
1913 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1914
1915 /* There are two extra relocations for each subsequent PLT entry:
1916 an R_386_32 relocation for the GOT entry, and an R_386_32
1917 relocation for the PLT entry. */
1918
1919 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1920 }
6725bdbf 1921 }
ced53ee5
AM
1922 else
1923 {
51b64d56 1924 h->plt.offset = (bfd_vma) -1;
f5385ebf 1925 h->needs_plt = 0;
ced53ee5 1926 }
6725bdbf
AM
1927 }
1928 else
1929 {
51b64d56 1930 h->plt.offset = (bfd_vma) -1;
f5385ebf 1931 h->needs_plt = 0;
6725bdbf
AM
1932 }
1933
67a4f2b7
AO
1934 eh = (struct elf_i386_link_hash_entry *) h;
1935 eh->tlsdesc_got = (bfd_vma) -1;
1936
37e55690 1937 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
13ae64f3
JJ
1938 make it a R_386_TLS_LE_32 requiring no TLS entry. */
1939 if (h->got.refcount > 0
1940 && !info->shared
1941 && h->dynindx == -1
37e55690 1942 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
cedb70c5 1943 h->got.offset = (bfd_vma) -1;
13ae64f3 1944 else if (h->got.refcount > 0)
6725bdbf 1945 {
0ac8d2ca 1946 asection *s;
b34976b6 1947 bfd_boolean dyn;
13ae64f3 1948 int tls_type = elf_i386_hash_entry(h)->tls_type;
6725bdbf 1949
5a15f56f
AM
1950 /* Make sure this symbol is output as a dynamic symbol.
1951 Undefined weak syms won't yet be marked as dynamic. */
1952 if (h->dynindx == -1
f5385ebf 1953 && !h->forced_local)
5a15f56f 1954 {
c152c796 1955 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1956 return FALSE;
5a15f56f
AM
1957 }
1958
6725bdbf 1959 s = htab->sgot;
67a4f2b7
AO
1960 if (GOT_TLS_GDESC_P (tls_type))
1961 {
1962 eh->tlsdesc_got = htab->sgotplt->size
1963 - elf_i386_compute_jump_table_size (htab);
1964 htab->sgotplt->size += 8;
1965 h->got.offset = (bfd_vma) -2;
1966 }
1967 if (! GOT_TLS_GDESC_P (tls_type)
1968 || GOT_TLS_GD_P (tls_type))
1969 {
1970 h->got.offset = s->size;
1971 s->size += 4;
1972 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
1973 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
1974 s->size += 4;
1975 }
ebe50bae 1976 dyn = htab->elf.dynamic_sections_created;
13ae64f3 1977 /* R_386_TLS_IE_32 needs one dynamic relocation,
37e55690
JJ
1978 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
1979 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
1980 need two), R_386_TLS_GD needs one if local symbol and two if
1981 global. */
1982 if (tls_type == GOT_TLS_IE_BOTH)
eea6121a 1983 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
67a4f2b7 1984 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
37e55690 1985 || (tls_type & GOT_TLS_IE))
eea6121a 1986 htab->srelgot->size += sizeof (Elf32_External_Rel);
67a4f2b7 1987 else if (GOT_TLS_GD_P (tls_type))
eea6121a 1988 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
67a4f2b7
AO
1989 else if (! GOT_TLS_GDESC_P (tls_type)
1990 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1991 || h->root.type != bfd_link_hash_undefweak)
ef5aade5
L
1992 && (info->shared
1993 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
eea6121a 1994 htab->srelgot->size += sizeof (Elf32_External_Rel);
67a4f2b7
AO
1995 if (GOT_TLS_GDESC_P (tls_type))
1996 htab->srelplt->size += sizeof (Elf32_External_Rel);
6725bdbf
AM
1997 }
1998 else
51b64d56 1999 h->got.offset = (bfd_vma) -1;
6725bdbf 2000
5a15f56f 2001 if (eh->dyn_relocs == NULL)
b34976b6 2002 return TRUE;
5a15f56f 2003
0c715baa
AM
2004 /* In the shared -Bsymbolic case, discard space allocated for
2005 dynamic pc-relative relocs against symbols which turn out to be
2006 defined in regular objects. For the normal shared case, discard
0ac8d2ca
AM
2007 space for pc-relative relocs that have become local due to symbol
2008 visibility changes. */
0c715baa
AM
2009
2010 if (info->shared)
5a15f56f 2011 {
09695f56
AM
2012 /* The only reloc that uses pc_count is R_386_PC32, which will
2013 appear on a call or on something like ".long foo - .". We
2014 want calls to protected symbols to resolve directly to the
2015 function rather than going via the plt. If people want
2016 function pointer comparisons to work as expected then they
2017 should avoid writing assembly like ".long foo - .". */
2018 if (SYMBOL_CALLS_LOCAL (info, h))
5a15f56f 2019 {
0c715baa
AM
2020 struct elf_i386_dyn_relocs **pp;
2021
2022 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2023 {
2024 p->count -= p->pc_count;
2025 p->pc_count = 0;
2026 if (p->count == 0)
2027 *pp = p->next;
2028 else
2029 pp = &p->next;
2030 }
5a15f56f 2031 }
4e795f50 2032
3348747a
NS
2033 if (htab->is_vxworks)
2034 {
2035 struct elf_i386_dyn_relocs **pp;
2036 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2037 {
2038 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2039 *pp = p->next;
2040 else
2041 pp = &p->next;
2042 }
2043 }
2044
4e795f50 2045 /* Also discard relocs on undefined weak syms with non-default
3348747a 2046 visibility. */
22d606e9 2047 if (eh->dyn_relocs != NULL
4e795f50 2048 && h->root.type == bfd_link_hash_undefweak)
22d606e9
AM
2049 {
2050 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2051 eh->dyn_relocs = NULL;
2052
2053 /* Make sure undefined weak symbols are output as a dynamic
2054 symbol in PIEs. */
2055 else if (h->dynindx == -1
2056 && !h->forced_local)
2057 {
2058 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2059 return FALSE;
2060 }
2061 }
0c715baa 2062 }
a23b6845 2063 else if (ELIMINATE_COPY_RELOCS)
0c715baa
AM
2064 {
2065 /* For the non-shared case, discard space for relocs against
2066 symbols which turn out to need copy relocs or are not
2067 dynamic. */
2068
f5385ebf
AM
2069 if (!h->non_got_ref
2070 && ((h->def_dynamic
2071 && !h->def_regular)
ebe50bae 2072 || (htab->elf.dynamic_sections_created
0c715baa
AM
2073 && (h->root.type == bfd_link_hash_undefweak
2074 || h->root.type == bfd_link_hash_undefined))))
2075 {
2076 /* Make sure this symbol is output as a dynamic symbol.
2077 Undefined weak syms won't yet be marked as dynamic. */
2078 if (h->dynindx == -1
f5385ebf 2079 && !h->forced_local)
0c715baa 2080 {
c152c796 2081 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2082 return FALSE;
0c715baa 2083 }
5a15f56f 2084
0c715baa
AM
2085 /* If that succeeded, we know we'll be keeping all the
2086 relocs. */
2087 if (h->dynindx != -1)
2088 goto keep;
2089 }
2090
2091 eh->dyn_relocs = NULL;
2092
ec338859 2093 keep: ;
5a15f56f
AM
2094 }
2095
0c715baa
AM
2096 /* Finally, allocate space. */
2097 for (p = eh->dyn_relocs; p != NULL; p = p->next)
12d0ee4a 2098 {
0c715baa 2099 asection *sreloc = elf_section_data (p->sec)->sreloc;
eea6121a 2100 sreloc->size += p->count * sizeof (Elf32_External_Rel);
12d0ee4a
AM
2101 }
2102
b34976b6 2103 return TRUE;
6725bdbf
AM
2104}
2105
0c715baa
AM
2106/* Find any dynamic relocs that apply to read-only sections. */
2107
b34976b6 2108static bfd_boolean
55fd94b0 2109readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
0c715baa
AM
2110{
2111 struct elf_i386_link_hash_entry *eh;
2112 struct elf_i386_dyn_relocs *p;
2113
e92d460e
AM
2114 if (h->root.type == bfd_link_hash_warning)
2115 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2116
0c715baa
AM
2117 eh = (struct elf_i386_link_hash_entry *) h;
2118 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2119 {
2120 asection *s = p->sec->output_section;
2121
2122 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2123 {
2124 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2125
2126 info->flags |= DF_TEXTREL;
2127
2128 /* Not an error, just cut short the traversal. */
b34976b6 2129 return FALSE;
0c715baa
AM
2130 }
2131 }
b34976b6 2132 return TRUE;
0c715baa
AM
2133}
2134
252b5132
RH
2135/* Set the sizes of the dynamic sections. */
2136
b34976b6 2137static bfd_boolean
55fd94b0
AM
2138elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2139 struct bfd_link_info *info)
252b5132 2140{
6725bdbf 2141 struct elf_i386_link_hash_table *htab;
252b5132
RH
2142 bfd *dynobj;
2143 asection *s;
b34976b6 2144 bfd_boolean relocs;
0c715baa 2145 bfd *ibfd;
252b5132 2146
6725bdbf 2147 htab = elf_i386_hash_table (info);
ebe50bae 2148 dynobj = htab->elf.dynobj;
ffb2e45b
AM
2149 if (dynobj == NULL)
2150 abort ();
252b5132 2151
ebe50bae 2152 if (htab->elf.dynamic_sections_created)
252b5132
RH
2153 {
2154 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 2155 if (info->executable)
252b5132
RH
2156 {
2157 s = bfd_get_section_by_name (dynobj, ".interp");
ffb2e45b
AM
2158 if (s == NULL)
2159 abort ();
eea6121a 2160 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
252b5132
RH
2161 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2162 }
161d71a6 2163 }
6725bdbf 2164
0c715baa
AM
2165 /* Set up .got offsets for local syms, and space for local dynamic
2166 relocs. */
2167 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
161d71a6
L
2168 {
2169 bfd_signed_vma *local_got;
2170 bfd_signed_vma *end_local_got;
13ae64f3 2171 char *local_tls_type;
67a4f2b7 2172 bfd_vma *local_tlsdesc_gotent;
161d71a6
L
2173 bfd_size_type locsymcount;
2174 Elf_Internal_Shdr *symtab_hdr;
2175 asection *srel;
6725bdbf 2176
0ffa91dd 2177 if (! is_i386_elf (ibfd))
161d71a6 2178 continue;
6725bdbf 2179
0c715baa
AM
2180 for (s = ibfd->sections; s != NULL; s = s->next)
2181 {
ec338859 2182 struct elf_i386_dyn_relocs *p;
0c715baa 2183
e81d3500
DD
2184 for (p = ((struct elf_i386_dyn_relocs *)
2185 elf_section_data (s)->local_dynrel);
ec338859
AM
2186 p != NULL;
2187 p = p->next)
0c715baa 2188 {
ec338859
AM
2189 if (!bfd_is_abs_section (p->sec)
2190 && bfd_is_abs_section (p->sec->output_section))
2191 {
2192 /* Input section has been discarded, either because
2193 it is a copy of a linkonce section or due to
2194 linker script /DISCARD/, so we'll be discarding
2195 the relocs too. */
2196 }
3348747a
NS
2197 else if (htab->is_vxworks
2198 && strcmp (p->sec->output_section->name,
2199 ".tls_vars") == 0)
2200 {
2201 /* Relocations in vxworks .tls_vars sections are
2202 handled specially by the loader. */
2203 }
248866a8 2204 else if (p->count != 0)
ec338859
AM
2205 {
2206 srel = elf_section_data (p->sec)->sreloc;
eea6121a 2207 srel->size += p->count * sizeof (Elf32_External_Rel);
248866a8
AM
2208 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2209 info->flags |= DF_TEXTREL;
ec338859 2210 }
0c715baa
AM
2211 }
2212 }
2213
2214 local_got = elf_local_got_refcounts (ibfd);
161d71a6
L
2215 if (!local_got)
2216 continue;
6725bdbf 2217
0ffa91dd 2218 symtab_hdr = &elf_symtab_hdr (ibfd);
161d71a6
L
2219 locsymcount = symtab_hdr->sh_info;
2220 end_local_got = local_got + locsymcount;
13ae64f3 2221 local_tls_type = elf_i386_local_got_tls_type (ibfd);
67a4f2b7 2222 local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
161d71a6
L
2223 s = htab->sgot;
2224 srel = htab->srelgot;
67a4f2b7
AO
2225 for (; local_got < end_local_got;
2226 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
161d71a6 2227 {
67a4f2b7 2228 *local_tlsdesc_gotent = (bfd_vma) -1;
161d71a6 2229 if (*local_got > 0)
6725bdbf 2230 {
67a4f2b7
AO
2231 if (GOT_TLS_GDESC_P (*local_tls_type))
2232 {
2233 *local_tlsdesc_gotent = htab->sgotplt->size
2234 - elf_i386_compute_jump_table_size (htab);
2235 htab->sgotplt->size += 8;
2236 *local_got = (bfd_vma) -2;
2237 }
2238 if (! GOT_TLS_GDESC_P (*local_tls_type)
2239 || GOT_TLS_GD_P (*local_tls_type))
2240 {
2241 *local_got = s->size;
2242 s->size += 4;
2243 if (GOT_TLS_GD_P (*local_tls_type)
2244 || *local_tls_type == GOT_TLS_IE_BOTH)
2245 s->size += 4;
2246 }
13ae64f3 2247 if (info->shared
67a4f2b7 2248 || GOT_TLS_GD_ANY_P (*local_tls_type)
37e55690
JJ
2249 || (*local_tls_type & GOT_TLS_IE))
2250 {
2251 if (*local_tls_type == GOT_TLS_IE_BOTH)
eea6121a 2252 srel->size += 2 * sizeof (Elf32_External_Rel);
67a4f2b7
AO
2253 else if (GOT_TLS_GD_P (*local_tls_type)
2254 || ! GOT_TLS_GDESC_P (*local_tls_type))
eea6121a 2255 srel->size += sizeof (Elf32_External_Rel);
67a4f2b7
AO
2256 if (GOT_TLS_GDESC_P (*local_tls_type))
2257 htab->srelplt->size += sizeof (Elf32_External_Rel);
37e55690 2258 }
6725bdbf 2259 }
161d71a6
L
2260 else
2261 *local_got = (bfd_vma) -1;
6725bdbf 2262 }
252b5132 2263 }
6725bdbf 2264
13ae64f3
JJ
2265 if (htab->tls_ldm_got.refcount > 0)
2266 {
2267 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2268 relocs. */
eea6121a
AM
2269 htab->tls_ldm_got.offset = htab->sgot->size;
2270 htab->sgot->size += 8;
2271 htab->srelgot->size += sizeof (Elf32_External_Rel);
13ae64f3
JJ
2272 }
2273 else
2274 htab->tls_ldm_got.offset = -1;
2275
0c715baa
AM
2276 /* Allocate global sym .plt and .got entries, and space for global
2277 sym dynamic relocs. */
ebe50bae 2278 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
252b5132 2279
67a4f2b7
AO
2280 /* For every jump slot reserved in the sgotplt, reloc_count is
2281 incremented. However, when we reserve space for TLS descriptors,
2282 it's not incremented, so in order to compute the space reserved
2283 for them, it suffices to multiply the reloc count by the jump
2284 slot size. */
2285 if (htab->srelplt)
5ae0bfb6 2286 htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
67a4f2b7 2287
5a15f56f
AM
2288 /* We now have determined the sizes of the various dynamic sections.
2289 Allocate memory for them. */
b34976b6 2290 relocs = FALSE;
252b5132
RH
2291 for (s = dynobj->sections; s != NULL; s = s->next)
2292 {
eac338cf
PB
2293 bfd_boolean strip_section = TRUE;
2294
252b5132
RH
2295 if ((s->flags & SEC_LINKER_CREATED) == 0)
2296 continue;
2297
6725bdbf
AM
2298 if (s == htab->splt
2299 || s == htab->sgot
75ff4589
L
2300 || s == htab->sgotplt
2301 || s == htab->sdynbss)
252b5132 2302 {
6725bdbf
AM
2303 /* Strip this section if we don't need it; see the
2304 comment below. */
eac338cf
PB
2305 /* We'd like to strip these sections if they aren't needed, but if
2306 we've exported dynamic symbols from them we must leave them.
2307 It's too late to tell BFD to get rid of the symbols. */
2308
7325306f 2309 if (htab->elf.hplt != NULL)
eac338cf 2310 strip_section = FALSE;
252b5132 2311 }
0112cd26 2312 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
252b5132 2313 {
eac338cf 2314 if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
b34976b6 2315 relocs = TRUE;
252b5132 2316
0ac8d2ca
AM
2317 /* We use the reloc_count field as a counter if we need
2318 to copy relocs into the output file. */
5ae0bfb6 2319 s->reloc_count = 0;
252b5132 2320 }
6725bdbf 2321 else
252b5132
RH
2322 {
2323 /* It's not one of our sections, so don't allocate space. */
2324 continue;
2325 }
2326
c456f082 2327 if (s->size == 0)
252b5132 2328 {
0ac8d2ca
AM
2329 /* If we don't need this section, strip it from the
2330 output file. This is mostly to handle .rel.bss and
2331 .rel.plt. We must create both sections in
2332 create_dynamic_sections, because they must be created
2333 before the linker maps input sections to output
2334 sections. The linker does that before
2335 adjust_dynamic_symbol is called, and it is that
2336 function which decides whether anything needs to go
2337 into these sections. */
c456f082
AM
2338 if (strip_section)
2339 s->flags |= SEC_EXCLUDE;
252b5132
RH
2340 continue;
2341 }
2342
c456f082
AM
2343 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2344 continue;
2345
f69da49f
AM
2346 /* Allocate memory for the section contents. We use bfd_zalloc
2347 here in case unused entries are not reclaimed before the
2348 section's contents are written out. This should not happen,
2349 but this way if it does, we get a R_386_NONE reloc instead
2350 of garbage. */
eea6121a 2351 s->contents = bfd_zalloc (dynobj, s->size);
6725bdbf 2352 if (s->contents == NULL)
b34976b6 2353 return FALSE;
252b5132
RH
2354 }
2355
ebe50bae 2356 if (htab->elf.dynamic_sections_created)
252b5132
RH
2357 {
2358 /* Add some entries to the .dynamic section. We fill in the
2359 values later, in elf_i386_finish_dynamic_sections, but we
2360 must add the entries now so that we get the correct size for
2361 the .dynamic section. The DT_DEBUG entry is filled in by the
2362 dynamic linker and used by the debugger. */
dc810e39 2363#define add_dynamic_entry(TAG, VAL) \
5a580b3a 2364 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 2365
36af4a4e 2366 if (info->executable)
252b5132 2367 {
dc810e39 2368 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 2369 return FALSE;
252b5132
RH
2370 }
2371
eea6121a 2372 if (htab->splt->size != 0)
252b5132 2373 {
dc810e39
AM
2374 if (!add_dynamic_entry (DT_PLTGOT, 0)
2375 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2376 || !add_dynamic_entry (DT_PLTREL, DT_REL)
2377 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 2378 return FALSE;
252b5132
RH
2379 }
2380
2381 if (relocs)
2382 {
dc810e39
AM
2383 if (!add_dynamic_entry (DT_REL, 0)
2384 || !add_dynamic_entry (DT_RELSZ, 0)
2385 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
b34976b6 2386 return FALSE;
252b5132 2387
0c715baa
AM
2388 /* If any dynamic relocs apply to a read-only section,
2389 then we need a DT_TEXTREL entry. */
248866a8
AM
2390 if ((info->flags & DF_TEXTREL) == 0)
2391 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2392 (PTR) info);
0c715baa
AM
2393
2394 if ((info->flags & DF_TEXTREL) != 0)
2395 {
2396 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 2397 return FALSE;
0c715baa 2398 }
252b5132 2399 }
7a2b07ff
NS
2400 if (htab->is_vxworks
2401 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
2402 return FALSE;
252b5132 2403 }
dc810e39 2404#undef add_dynamic_entry
252b5132 2405
b34976b6 2406 return TRUE;
252b5132
RH
2407}
2408
67a4f2b7
AO
2409static bfd_boolean
2410elf_i386_always_size_sections (bfd *output_bfd,
2411 struct bfd_link_info *info)
2412{
2413 asection *tls_sec = elf_hash_table (info)->tls_sec;
2414
2415 if (tls_sec)
2416 {
2417 struct elf_link_hash_entry *tlsbase;
2418
2419 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2420 "_TLS_MODULE_BASE_",
2421 FALSE, FALSE, FALSE);
2422
2423 if (tlsbase && tlsbase->type == STT_TLS)
2424 {
2425 struct bfd_link_hash_entry *bh = NULL;
2426 const struct elf_backend_data *bed
2427 = get_elf_backend_data (output_bfd);
2428
2429 if (!(_bfd_generic_link_add_one_symbol
2430 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2431 tls_sec, 0, NULL, FALSE,
2432 bed->collect, &bh)))
2433 return FALSE;
2434 tlsbase = (struct elf_link_hash_entry *)bh;
2435 tlsbase->def_regular = 1;
2436 tlsbase->other = STV_HIDDEN;
2437 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2438 }
2439 }
2440
2441 return TRUE;
2442}
2443
38701953
AM
2444/* Set the correct type for an x86 ELF section. We do this by the
2445 section name, which is a hack, but ought to work. */
2446
b34976b6 2447static bfd_boolean
55fd94b0
AM
2448elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2449 Elf_Internal_Shdr *hdr,
2450 asection *sec)
38701953
AM
2451{
2452 register const char *name;
2453
2454 name = bfd_get_section_name (abfd, sec);
2455
2456 /* This is an ugly, but unfortunately necessary hack that is
2457 needed when producing EFI binaries on x86. It tells
2458 elf.c:elf_fake_sections() not to consider ".reloc" as a section
2459 containing ELF relocation info. We need this hack in order to
2460 be able to generate ELF binaries that can be translated into
2461 EFI applications (which are essentially COFF objects). Those
2462 files contain a COFF ".reloc" section inside an ELFNN object,
2463 which would normally cause BFD to segfault because it would
2464 attempt to interpret this section as containing relocation
2465 entries for section "oc". With this hack enabled, ".reloc"
2466 will be treated as a normal data section, which will avoid the
2467 segfault. However, you won't be able to create an ELFNN binary
2468 with a section named "oc" that needs relocations, but that's
2469 the kind of ugly side-effects you get when detecting section
2470 types based on their names... In practice, this limitation is
2471 unlikely to bite. */
2472 if (strcmp (name, ".reloc") == 0)
2473 hdr->sh_type = SHT_PROGBITS;
2474
b34976b6 2475 return TRUE;
38701953
AM
2476}
2477
13ae64f3
JJ
2478/* Return the base VMA address which should be subtracted from real addresses
2479 when resolving @dtpoff relocation.
2480 This is PT_TLS segment p_vaddr. */
2481
2482static bfd_vma
55fd94b0 2483dtpoff_base (struct bfd_link_info *info)
13ae64f3 2484{
e1918d23
AM
2485 /* If tls_sec is NULL, we should have signalled an error already. */
2486 if (elf_hash_table (info)->tls_sec == NULL)
6a30718d 2487 return 0;
e1918d23 2488 return elf_hash_table (info)->tls_sec->vma;
13ae64f3
JJ
2489}
2490
2491/* Return the relocation value for @tpoff relocation
2492 if STT_TLS virtual address is ADDRESS. */
2493
2494static bfd_vma
55fd94b0 2495tpoff (struct bfd_link_info *info, bfd_vma address)
13ae64f3 2496{
e1918d23 2497 struct elf_link_hash_table *htab = elf_hash_table (info);
13ae64f3 2498
e1918d23
AM
2499 /* If tls_sec is NULL, we should have signalled an error already. */
2500 if (htab->tls_sec == NULL)
6a30718d 2501 return 0;
e1918d23 2502 return htab->tls_size + htab->tls_sec->vma - address;
13ae64f3
JJ
2503}
2504
252b5132
RH
2505/* Relocate an i386 ELF section. */
2506
b34976b6 2507static bfd_boolean
55fd94b0
AM
2508elf_i386_relocate_section (bfd *output_bfd,
2509 struct bfd_link_info *info,
2510 bfd *input_bfd,
2511 asection *input_section,
2512 bfd_byte *contents,
2513 Elf_Internal_Rela *relocs,
2514 Elf_Internal_Sym *local_syms,
2515 asection **local_sections)
252b5132 2516{
6725bdbf 2517 struct elf_i386_link_hash_table *htab;
252b5132
RH
2518 Elf_Internal_Shdr *symtab_hdr;
2519 struct elf_link_hash_entry **sym_hashes;
2520 bfd_vma *local_got_offsets;
67a4f2b7 2521 bfd_vma *local_tlsdesc_gotents;
252b5132
RH
2522 Elf_Internal_Rela *rel;
2523 Elf_Internal_Rela *relend;
3348747a 2524 bfd_boolean is_vxworks_tls;
252b5132 2525
0ffa91dd
NC
2526 BFD_ASSERT (is_i386_elf (input_bfd));
2527
6725bdbf 2528 htab = elf_i386_hash_table (info);
0ffa91dd 2529 symtab_hdr = &elf_symtab_hdr (input_bfd);
252b5132
RH
2530 sym_hashes = elf_sym_hashes (input_bfd);
2531 local_got_offsets = elf_local_got_offsets (input_bfd);
67a4f2b7 2532 local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
3348747a
NS
2533 /* We have to handle relocations in vxworks .tls_vars sections
2534 specially, because the dynamic loader is 'weird'. */
2535 is_vxworks_tls = (htab->is_vxworks && info->shared
2536 && !strcmp (input_section->output_section->name,
2537 ".tls_vars"));
252b5132 2538
252b5132
RH
2539 rel = relocs;
2540 relend = relocs + input_section->reloc_count;
2541 for (; rel < relend; rel++)
2542 {
13ae64f3 2543 unsigned int r_type;
252b5132
RH
2544 reloc_howto_type *howto;
2545 unsigned long r_symndx;
2546 struct elf_link_hash_entry *h;
2547 Elf_Internal_Sym *sym;
2548 asection *sec;
67a4f2b7 2549 bfd_vma off, offplt;
252b5132 2550 bfd_vma relocation;
b34976b6 2551 bfd_boolean unresolved_reloc;
252b5132 2552 bfd_reloc_status_type r;
1b452ec6 2553 unsigned int indx;
13ae64f3 2554 int tls_type;
252b5132
RH
2555
2556 r_type = ELF32_R_TYPE (rel->r_info);
55fd94b0
AM
2557 if (r_type == R_386_GNU_VTINHERIT
2558 || r_type == R_386_GNU_VTENTRY)
252b5132 2559 continue;
dc47f327 2560
55fd94b0 2561 if ((indx = r_type) >= R_386_standard
13ae64f3
JJ
2562 && ((indx = r_type - R_386_ext_offset) - R_386_standard
2563 >= R_386_ext - R_386_standard)
2564 && ((indx = r_type - R_386_tls_offset) - R_386_ext
2565 >= R_386_tls - R_386_ext))
252b5132 2566 {
6ba842b6 2567 (*_bfd_error_handler)
d003868e
AM
2568 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
2569 input_bfd, input_section, r_type);
252b5132 2570 bfd_set_error (bfd_error_bad_value);
b34976b6 2571 return FALSE;
252b5132 2572 }
1b452ec6 2573 howto = elf_howto_table + indx;
252b5132
RH
2574
2575 r_symndx = ELF32_R_SYM (rel->r_info);
252b5132
RH
2576 h = NULL;
2577 sym = NULL;
2578 sec = NULL;
b34976b6 2579 unresolved_reloc = FALSE;
252b5132
RH
2580 if (r_symndx < symtab_hdr->sh_info)
2581 {
2582 sym = local_syms + r_symndx;
2583 sec = local_sections[r_symndx];
2584 relocation = (sec->output_section->vma
2585 + sec->output_offset
2586 + sym->st_value);
ab96bf03
AM
2587
2588 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2589 && ((sec->flags & SEC_MERGE) != 0
2590 || (info->relocatable
2591 && sec->output_offset != 0)))
f8df10f4 2592 {
f8df10f4 2593 bfd_vma addend;
4a335f3d 2594 bfd_byte *where = contents + rel->r_offset;
f8df10f4 2595
4a335f3d 2596 switch (howto->size)
f8df10f4 2597 {
4a335f3d
AM
2598 case 0:
2599 addend = bfd_get_8 (input_bfd, where);
2600 if (howto->pc_relative)
2601 {
2602 addend = (addend ^ 0x80) - 0x80;
2603 addend += 1;
2604 }
2605 break;
2606 case 1:
2607 addend = bfd_get_16 (input_bfd, where);
2608 if (howto->pc_relative)
2609 {
2610 addend = (addend ^ 0x8000) - 0x8000;
2611 addend += 2;
2612 }
2613 break;
2614 case 2:
2615 addend = bfd_get_32 (input_bfd, where);
2616 if (howto->pc_relative)
2617 {
2618 addend = (addend ^ 0x80000000) - 0x80000000;
2619 addend += 4;
2620 }
2621 break;
2622 default:
2623 abort ();
f8df10f4
JJ
2624 }
2625
ab96bf03
AM
2626 if (info->relocatable)
2627 addend += sec->output_offset;
2628 else
2629 {
2630 asection *msec = sec;
2631 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2632 addend);
2633 addend -= relocation;
2634 addend += msec->output_section->vma + msec->output_offset;
2635 }
4a335f3d
AM
2636
2637 switch (howto->size)
2638 {
2639 case 0:
16a10388 2640 /* FIXME: overflow checks. */
4a335f3d
AM
2641 if (howto->pc_relative)
2642 addend -= 1;
2643 bfd_put_8 (input_bfd, addend, where);
4a335f3d
AM
2644 break;
2645 case 1:
2646 if (howto->pc_relative)
2647 addend -= 2;
2648 bfd_put_16 (input_bfd, addend, where);
4a335f3d
AM
2649 break;
2650 case 2:
2651 if (howto->pc_relative)
2652 addend -= 4;
2653 bfd_put_32 (input_bfd, addend, where);
2654 break;
2655 }
f8df10f4 2656 }
252b5132
RH
2657 }
2658 else
2659 {
560e09e9 2660 bfd_boolean warned;
ffb2e45b 2661
b2a8e766
AM
2662 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2663 r_symndx, symtab_hdr, sym_hashes,
2664 h, sec, relocation,
2665 unresolved_reloc, warned);
252b5132
RH
2666 }
2667
ab96bf03 2668 if (sec != NULL && elf_discarded_section (sec))
9635fe29 2669 {
ab96bf03
AM
2670 /* For relocs against symbols from removed linkonce sections,
2671 or sections discarded by a linker script, we just want the
2672 section contents zeroed. Avoid any special processing. */
b1e24c02 2673 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
ab96bf03
AM
2674 rel->r_info = 0;
2675 rel->r_addend = 0;
b1e24c02 2676 continue;
9635fe29
AM
2677 }
2678
ab96bf03
AM
2679 if (info->relocatable)
2680 continue;
2681
252b5132
RH
2682 switch (r_type)
2683 {
2684 case R_386_GOT32:
2685 /* Relocation is to the entry for this symbol in the global
2686 offset table. */
ffb2e45b
AM
2687 if (htab->sgot == NULL)
2688 abort ();
252b5132
RH
2689
2690 if (h != NULL)
2691 {
b34976b6 2692 bfd_boolean dyn;
252b5132
RH
2693
2694 off = h->got.offset;
ebe50bae 2695 dyn = htab->elf.dynamic_sections_created;
26e41594 2696 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
252b5132 2697 || (info->shared
586119b3 2698 && SYMBOL_REFERENCES_LOCAL (info, h))
ef5aade5
L
2699 || (ELF_ST_VISIBILITY (h->other)
2700 && h->root.type == bfd_link_hash_undefweak))
252b5132
RH
2701 {
2702 /* This is actually a static link, or it is a
2703 -Bsymbolic link and the symbol is defined
2704 locally, or the symbol was forced to be local
2705 because of a version file. We must initialize
2706 this entry in the global offset table. Since the
2707 offset must always be a multiple of 4, we use the
2708 least significant bit to record whether we have
2709 initialized it already.
2710
2711 When doing a dynamic link, we create a .rel.got
2712 relocation entry to initialize the value. This
2713 is done in the finish_dynamic_symbol routine. */
2714 if ((off & 1) != 0)
2715 off &= ~1;
2716 else
2717 {
2718 bfd_put_32 (output_bfd, relocation,
6725bdbf 2719 htab->sgot->contents + off);
252b5132
RH
2720 h->got.offset |= 1;
2721 }
2722 }
8c694914 2723 else
b34976b6 2724 unresolved_reloc = FALSE;
252b5132
RH
2725 }
2726 else
2727 {
ffb2e45b
AM
2728 if (local_got_offsets == NULL)
2729 abort ();
252b5132
RH
2730
2731 off = local_got_offsets[r_symndx];
2732
2733 /* The offset must always be a multiple of 4. We use
83be169b
AM
2734 the least significant bit to record whether we have
2735 already generated the necessary reloc. */
252b5132
RH
2736 if ((off & 1) != 0)
2737 off &= ~1;
2738 else
2739 {
6725bdbf
AM
2740 bfd_put_32 (output_bfd, relocation,
2741 htab->sgot->contents + off);
252b5132
RH
2742
2743 if (info->shared)
2744 {
947216bf
AM
2745 asection *s;
2746 Elf_Internal_Rela outrel;
2747 bfd_byte *loc;
252b5132 2748
947216bf
AM
2749 s = htab->srelgot;
2750 if (s == NULL)
ffb2e45b 2751 abort ();
252b5132 2752
6725bdbf
AM
2753 outrel.r_offset = (htab->sgot->output_section->vma
2754 + htab->sgot->output_offset
252b5132
RH
2755 + off);
2756 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
947216bf
AM
2757 loc = s->contents;
2758 loc += s->reloc_count++ * sizeof (Elf32_External_Rel);
0ac8d2ca 2759 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
252b5132
RH
2760 }
2761
2762 local_got_offsets[r_symndx] |= 1;
2763 }
252b5132
RH
2764 }
2765
ffb2e45b
AM
2766 if (off >= (bfd_vma) -2)
2767 abort ();
2768
8c37241b
JJ
2769 relocation = htab->sgot->output_section->vma
2770 + htab->sgot->output_offset + off
2771 - htab->sgotplt->output_section->vma
2772 - htab->sgotplt->output_offset;
252b5132
RH
2773 break;
2774
2775 case R_386_GOTOFF:
2776 /* Relocation is relative to the start of the global offset
2777 table. */
2778
90f487df
L
2779 /* Check to make sure it isn't a protected function symbol
2780 for shared library since it may not be local when used
41bed6dd
L
2781 as function address. We also need to make sure that a
2782 symbol is defined locally. */
2783 if (info->shared && h)
90f487df 2784 {
41bed6dd
L
2785 if (!h->def_regular)
2786 {
2787 const char *v;
2788
2789 switch (ELF_ST_VISIBILITY (h->other))
2790 {
2791 case STV_HIDDEN:
2792 v = _("hidden symbol");
2793 break;
2794 case STV_INTERNAL:
2795 v = _("internal symbol");
2796 break;
2797 case STV_PROTECTED:
2798 v = _("protected symbol");
2799 break;
2800 default:
2801 v = _("symbol");
2802 break;
2803 }
2804
2805 (*_bfd_error_handler)
2806 (_("%B: relocation R_386_GOTOFF against undefined %s `%s' can not be used when making a shared object"),
2807 input_bfd, v, h->root.root.string);
2808 bfd_set_error (bfd_error_bad_value);
2809 return FALSE;
2810 }
2811 else if (!info->executable
2812 && h->type == STT_FUNC
2813 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2814 {
2815 (*_bfd_error_handler)
2816 (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
2817 input_bfd, h->root.root.string);
2818 bfd_set_error (bfd_error_bad_value);
2819 return FALSE;
2820 }
90f487df
L
2821 }
2822
8c37241b
JJ
2823 /* Note that sgot is not involved in this
2824 calculation. We always want the start of .got.plt. If we
2825 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
252b5132
RH
2826 permitted by the ABI, we might have to change this
2827 calculation. */
8c37241b
JJ
2828 relocation -= htab->sgotplt->output_section->vma
2829 + htab->sgotplt->output_offset;
252b5132
RH
2830 break;
2831
2832 case R_386_GOTPC:
2833 /* Use global offset table as symbol value. */
8c37241b
JJ
2834 relocation = htab->sgotplt->output_section->vma
2835 + htab->sgotplt->output_offset;
b34976b6 2836 unresolved_reloc = FALSE;
252b5132
RH
2837 break;
2838
2839 case R_386_PLT32:
2840 /* Relocation is to the entry for this symbol in the
2841 procedure linkage table. */
2842
dd5724d5 2843 /* Resolve a PLT32 reloc against a local symbol directly,
83be169b 2844 without using the procedure linkage table. */
252b5132
RH
2845 if (h == NULL)
2846 break;
2847
dd5724d5 2848 if (h->plt.offset == (bfd_vma) -1
6725bdbf 2849 || htab->splt == NULL)
252b5132
RH
2850 {
2851 /* We didn't make a PLT entry for this symbol. This
83be169b
AM
2852 happens when statically linking PIC code, or when
2853 using -Bsymbolic. */
252b5132
RH
2854 break;
2855 }
2856
6725bdbf
AM
2857 relocation = (htab->splt->output_section->vma
2858 + htab->splt->output_offset
252b5132 2859 + h->plt.offset);
b34976b6 2860 unresolved_reloc = FALSE;
252b5132
RH
2861 break;
2862
2863 case R_386_32:
2864 case R_386_PC32:
3348747a
NS
2865 if ((input_section->flags & SEC_ALLOC) == 0
2866 || is_vxworks_tls)
ec338859
AM
2867 break;
2868
12d0ee4a 2869 if ((info->shared
ef5aade5
L
2870 && (h == NULL
2871 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2872 || h->root.type != bfd_link_hash_undefweak)
12d0ee4a 2873 && (r_type != R_386_PC32
f6c52c13 2874 || !SYMBOL_CALLS_LOCAL (info, h)))
a23b6845
AM
2875 || (ELIMINATE_COPY_RELOCS
2876 && !info->shared
12d0ee4a
AM
2877 && h != NULL
2878 && h->dynindx != -1
f5385ebf
AM
2879 && !h->non_got_ref
2880 && ((h->def_dynamic
2881 && !h->def_regular)
28d0b90e
AM
2882 || h->root.type == bfd_link_hash_undefweak
2883 || h->root.type == bfd_link_hash_undefined)))
252b5132 2884 {
947216bf
AM
2885 Elf_Internal_Rela outrel;
2886 bfd_byte *loc;
b34976b6 2887 bfd_boolean skip, relocate;
0c715baa 2888 asection *sreloc;
252b5132
RH
2889
2890 /* When generating a shared object, these relocations
2891 are copied into the output file to be resolved at run
2892 time. */
2893
b34976b6
AM
2894 skip = FALSE;
2895 relocate = FALSE;
252b5132 2896
c629eae0
JJ
2897 outrel.r_offset =
2898 _bfd_elf_section_offset (output_bfd, info, input_section,
2899 rel->r_offset);
2900 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 2901 skip = TRUE;
0bb2d96a 2902 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 2903 skip = TRUE, relocate = TRUE;
252b5132
RH
2904 outrel.r_offset += (input_section->output_section->vma
2905 + input_section->output_offset);
2906
2907 if (skip)
0bb2d96a 2908 memset (&outrel, 0, sizeof outrel);
5a15f56f
AM
2909 else if (h != NULL
2910 && h->dynindx != -1
2911 && (r_type == R_386_PC32
2912 || !info->shared
55255dae 2913 || !SYMBOLIC_BIND (info, h)
f5385ebf 2914 || !h->def_regular))
0bb2d96a 2915 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
252b5132
RH
2916 else
2917 {
5a15f56f 2918 /* This symbol is local, or marked to become local. */
b34976b6 2919 relocate = TRUE;
5a15f56f 2920 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
252b5132
RH
2921 }
2922
0c715baa
AM
2923 sreloc = elf_section_data (input_section)->sreloc;
2924 if (sreloc == NULL)
2925 abort ();
2926
947216bf
AM
2927 loc = sreloc->contents;
2928 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
0c715baa 2929 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
252b5132
RH
2930
2931 /* If this reloc is against an external symbol, we do
2932 not want to fiddle with the addend. Otherwise, we
2933 need to include the symbol value so that it becomes
2934 an addend for the dynamic reloc. */
2935 if (! relocate)
2936 continue;
2937 }
252b5132
RH
2938 break;
2939
37e55690
JJ
2940 case R_386_TLS_IE:
2941 if (info->shared)
2942 {
947216bf
AM
2943 Elf_Internal_Rela outrel;
2944 bfd_byte *loc;
37e55690 2945 asection *sreloc;
37e55690
JJ
2946
2947 outrel.r_offset = rel->r_offset
2948 + input_section->output_section->vma
2949 + input_section->output_offset;
2950 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2951 sreloc = elf_section_data (input_section)->sreloc;
2952 if (sreloc == NULL)
2953 abort ();
947216bf
AM
2954 loc = sreloc->contents;
2955 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
37e55690
JJ
2956 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2957 }
2958 /* Fall through */
2959
13ae64f3 2960 case R_386_TLS_GD:
67a4f2b7
AO
2961 case R_386_TLS_GOTDESC:
2962 case R_386_TLS_DESC_CALL:
13ae64f3 2963 case R_386_TLS_IE_32:
37e55690 2964 case R_386_TLS_GOTIE:
13ae64f3
JJ
2965 tls_type = GOT_UNKNOWN;
2966 if (h == NULL && local_got_offsets)
2967 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
2968 else if (h != NULL)
142411ca 2969 tls_type = elf_i386_hash_entry(h)->tls_type;
37e55690
JJ
2970 if (tls_type == GOT_TLS_IE)
2971 tls_type = GOT_TLS_IE_NEG;
142411ca
L
2972
2973 if (! elf_i386_tls_transition (info, input_bfd,
2974 input_section, contents,
2975 symtab_hdr, sym_hashes,
2976 &r_type, tls_type, rel,
2977 relend, h))
2978 return FALSE;
13ae64f3
JJ
2979
2980 if (r_type == R_386_TLS_LE_32)
2981 {
82e51918 2982 BFD_ASSERT (! unresolved_reloc);
13ae64f3
JJ
2983 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
2984 {
142411ca 2985 unsigned int type;
13ae64f3
JJ
2986 bfd_vma roff;
2987
2988 /* GD->LE transition. */
13ae64f3 2989 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
13ae64f3
JJ
2990 if (type == 0x04)
2991 {
2992 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
2993 Change it into:
2994 movl %gs:0, %eax; subl $foo@tpoff, %eax
2995 (6 byte form of subl). */
13ae64f3
JJ
2996 memcpy (contents + rel->r_offset - 3,
2997 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
142411ca 2998 roff = rel->r_offset + 5;
13ae64f3
JJ
2999 }
3000 else
3001 {
142411ca
L
3002 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3003 Change it into:
3004 movl %gs:0, %eax; subl $foo@tpoff, %eax
3005 (6 byte form of subl). */
3006 memcpy (contents + rel->r_offset - 2,
3007 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3008 roff = rel->r_offset + 6;
13ae64f3
JJ
3009 }
3010 bfd_put_32 (output_bfd, tpoff (info, relocation),
3011 contents + roff);
a3fadc9a 3012 /* Skip R_386_PC32/R_386_PLT32. */
13ae64f3
JJ
3013 rel++;
3014 continue;
3015 }
67a4f2b7
AO
3016 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3017 {
3018 /* GDesc -> LE transition.
3019 It's originally something like:
3020 leal x@tlsdesc(%ebx), %eax
3021
3022 leal x@ntpoff, %eax
3023
3024 Registers other than %eax may be set up here. */
3025
142411ca 3026 unsigned int val;
67a4f2b7
AO
3027 bfd_vma roff;
3028
67a4f2b7 3029 roff = rel->r_offset;
67a4f2b7 3030 val = bfd_get_8 (input_bfd, contents + roff - 1);
67a4f2b7
AO
3031
3032 /* Now modify the instruction as appropriate. */
3033 /* aoliva FIXME: remove the above and xor the byte
3034 below with 0x86. */
3035 bfd_put_8 (output_bfd, val ^ 0x86,
3036 contents + roff - 1);
3037 bfd_put_32 (output_bfd, -tpoff (info, relocation),
3038 contents + roff);
3039 continue;
3040 }
3041 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3042 {
3043 /* GDesc -> LE transition.
3044 It's originally:
3045 call *(%eax)
3046 Turn it into:
142411ca 3047 xchg %ax,%ax */
67a4f2b7 3048
67a4f2b7 3049 bfd_vma roff;
142411ca 3050
67a4f2b7 3051 roff = rel->r_offset;
10efb593 3052 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
3053 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3054 continue;
3055 }
37e55690 3056 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
13ae64f3 3057 {
142411ca 3058 unsigned int val;
13ae64f3
JJ
3059
3060 /* IE->LE transition:
37e55690
JJ
3061 Originally it can be one of:
3062 movl foo, %eax
3063 movl foo, %reg
3064 addl foo, %reg
3065 We change it into:
3066 movl $foo, %eax
3067 movl $foo, %reg
3068 addl $foo, %reg. */
37e55690 3069 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
37e55690
JJ
3070 if (val == 0xa1)
3071 {
3072 /* movl foo, %eax. */
55fd94b0
AM
3073 bfd_put_8 (output_bfd, 0xb8,
3074 contents + rel->r_offset - 1);
37e55690 3075 }
299bf759 3076 else
37e55690 3077 {
142411ca
L
3078 unsigned int type;
3079
55fd94b0
AM
3080 type = bfd_get_8 (input_bfd,
3081 contents + rel->r_offset - 2);
299bf759 3082 switch (type)
26e41594 3083 {
299bf759
L
3084 case 0x8b:
3085 /* movl */
299bf759
L
3086 bfd_put_8 (output_bfd, 0xc7,
3087 contents + rel->r_offset - 2);
3088 bfd_put_8 (output_bfd,
3089 0xc0 | ((val >> 3) & 7),
3090 contents + rel->r_offset - 1);
3091 break;
3092 case 0x03:
3093 /* addl */
299bf759
L
3094 bfd_put_8 (output_bfd, 0x81,
3095 contents + rel->r_offset - 2);
3096 bfd_put_8 (output_bfd,
3097 0xc0 | ((val >> 3) & 7),
3098 contents + rel->r_offset - 1);
3099 break;
3100 default:
3101 BFD_FAIL ();
3102 break;
26e41594 3103 }
37e55690 3104 }
37e55690
JJ
3105 bfd_put_32 (output_bfd, -tpoff (info, relocation),
3106 contents + rel->r_offset);
3107 continue;
3108 }
3109 else
3110 {
3111 unsigned int val, type;
3112
3113 /* {IE_32,GOTIE}->LE transition:
3114 Originally it can be one of:
13ae64f3 3115 subl foo(%reg1), %reg2
13ae64f3 3116 movl foo(%reg1), %reg2
37e55690 3117 addl foo(%reg1), %reg2
13ae64f3
JJ
3118 We change it into:
3119 subl $foo, %reg2
37e55690
JJ
3120 movl $foo, %reg2 (6 byte form)
3121 addl $foo, %reg2. */
13ae64f3
JJ
3122 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3123 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
13ae64f3
JJ
3124 if (type == 0x8b)
3125 {
3126 /* movl */
13ae64f3
JJ
3127 bfd_put_8 (output_bfd, 0xc7,
3128 contents + rel->r_offset - 2);
3129 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3130 contents + rel->r_offset - 1);
3131 }
3132 else if (type == 0x2b)
3133 {
3134 /* subl */
13ae64f3
JJ
3135 bfd_put_8 (output_bfd, 0x81,
3136 contents + rel->r_offset - 2);
3137 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3138 contents + rel->r_offset - 1);
3139 }
37e55690
JJ
3140 else if (type == 0x03)
3141 {
3142 /* addl */
3143 bfd_put_8 (output_bfd, 0x81,
3144 contents + rel->r_offset - 2);
3145 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3146 contents + rel->r_offset - 1);
3147 }
13ae64f3
JJ
3148 else
3149 BFD_FAIL ();
37e55690
JJ
3150 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
3151 bfd_put_32 (output_bfd, -tpoff (info, relocation),
3152 contents + rel->r_offset);
3153 else
3154 bfd_put_32 (output_bfd, tpoff (info, relocation),
3155 contents + rel->r_offset);
13ae64f3
JJ
3156 continue;
3157 }
3158 }
3159
3160 if (htab->sgot == NULL)
3161 abort ();
3162
3163 if (h != NULL)
67a4f2b7
AO
3164 {
3165 off = h->got.offset;
3166 offplt = elf_i386_hash_entry (h)->tlsdesc_got;
3167 }
13ae64f3
JJ
3168 else
3169 {
3170 if (local_got_offsets == NULL)
3171 abort ();
3172
3173 off = local_got_offsets[r_symndx];
67a4f2b7 3174 offplt = local_tlsdesc_gotents[r_symndx];
13ae64f3
JJ
3175 }
3176
3177 if ((off & 1) != 0)
3178 off &= ~1;
26e41594 3179 else
13ae64f3 3180 {
947216bf
AM
3181 Elf_Internal_Rela outrel;
3182 bfd_byte *loc;
13ae64f3 3183 int dr_type, indx;
67a4f2b7 3184 asection *sreloc;
13ae64f3
JJ
3185
3186 if (htab->srelgot == NULL)
3187 abort ();
3188
67a4f2b7
AO
3189 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3190
3191 if (GOT_TLS_GDESC_P (tls_type))
3192 {
3193 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3194 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3195 <= htab->sgotplt->size);
3196 outrel.r_offset = (htab->sgotplt->output_section->vma
3197 + htab->sgotplt->output_offset
3198 + offplt
3199 + htab->sgotplt_jump_table_size);
3200 sreloc = htab->srelplt;
3201 loc = sreloc->contents;
5ae0bfb6
RS
3202 loc += (htab->next_tls_desc_index++
3203 * sizeof (Elf32_External_Rel));
67a4f2b7
AO
3204 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3205 <= sreloc->contents + sreloc->size);
3206 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3207 if (indx == 0)
3208 {
3209 BFD_ASSERT (! unresolved_reloc);
3210 bfd_put_32 (output_bfd,
3211 relocation - dtpoff_base (info),
3212 htab->sgotplt->contents + offplt
3213 + htab->sgotplt_jump_table_size + 4);
3214 }
3215 else
3216 {
3217 bfd_put_32 (output_bfd, 0,
3218 htab->sgotplt->contents + offplt
3219 + htab->sgotplt_jump_table_size + 4);
3220 }
3221 }
3222
3223 sreloc = htab->srelgot;
3224
13ae64f3
JJ
3225 outrel.r_offset = (htab->sgot->output_section->vma
3226 + htab->sgot->output_offset + off);
3227
67a4f2b7 3228 if (GOT_TLS_GD_P (tls_type))
13ae64f3 3229 dr_type = R_386_TLS_DTPMOD32;
67a4f2b7
AO
3230 else if (GOT_TLS_GDESC_P (tls_type))
3231 goto dr_done;
37e55690
JJ
3232 else if (tls_type == GOT_TLS_IE_POS)
3233 dr_type = R_386_TLS_TPOFF;
13ae64f3
JJ
3234 else
3235 dr_type = R_386_TLS_TPOFF32;
67a4f2b7 3236
37e55690
JJ
3237 if (dr_type == R_386_TLS_TPOFF && indx == 0)
3238 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
3239 htab->sgot->contents + off);
3240 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
c5c1f40c 3241 bfd_put_32 (output_bfd, dtpoff_base (info) - relocation,
c366c25e 3242 htab->sgot->contents + off);
67a4f2b7 3243 else if (dr_type != R_386_TLS_DESC)
c366c25e
JJ
3244 bfd_put_32 (output_bfd, 0,
3245 htab->sgot->contents + off);
13ae64f3 3246 outrel.r_info = ELF32_R_INFO (indx, dr_type);
67a4f2b7
AO
3247
3248 loc = sreloc->contents;
3249 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3250 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3251 <= sreloc->contents + sreloc->size);
13ae64f3
JJ
3252 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3253
67a4f2b7 3254 if (GOT_TLS_GD_P (tls_type))
13ae64f3
JJ
3255 {
3256 if (indx == 0)
3257 {
82e51918 3258 BFD_ASSERT (! unresolved_reloc);
13ae64f3
JJ
3259 bfd_put_32 (output_bfd,
3260 relocation - dtpoff_base (info),
3261 htab->sgot->contents + off + 4);
3262 }
3263 else
3264 {
3265 bfd_put_32 (output_bfd, 0,
3266 htab->sgot->contents + off + 4);
3267 outrel.r_info = ELF32_R_INFO (indx,
3268 R_386_TLS_DTPOFF32);
3269 outrel.r_offset += 4;
67a4f2b7 3270 sreloc->reloc_count++;
947216bf 3271 loc += sizeof (Elf32_External_Rel);
67a4f2b7
AO
3272 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3273 <= sreloc->contents + sreloc->size);
947216bf 3274 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
13ae64f3
JJ
3275 }
3276 }
37e55690
JJ
3277 else if (tls_type == GOT_TLS_IE_BOTH)
3278 {
3279 bfd_put_32 (output_bfd,
3280 indx == 0 ? relocation - dtpoff_base (info) : 0,
3281 htab->sgot->contents + off + 4);
3282 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3283 outrel.r_offset += 4;
67a4f2b7 3284 sreloc->reloc_count++;
947216bf 3285 loc += sizeof (Elf32_External_Rel);
37e55690
JJ
3286 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3287 }
13ae64f3 3288
67a4f2b7 3289 dr_done:
13ae64f3
JJ
3290 if (h != NULL)
3291 h->got.offset |= 1;
3292 else
3293 local_got_offsets[r_symndx] |= 1;
3294 }
3295
67a4f2b7
AO
3296 if (off >= (bfd_vma) -2
3297 && ! GOT_TLS_GDESC_P (tls_type))
13ae64f3 3298 abort ();
67a4f2b7
AO
3299 if (r_type == R_386_TLS_GOTDESC
3300 || r_type == R_386_TLS_DESC_CALL)
3301 {
3302 relocation = htab->sgotplt_jump_table_size + offplt;
3303 unresolved_reloc = FALSE;
3304 }
3305 else if (r_type == ELF32_R_TYPE (rel->r_info))
13ae64f3 3306 {
8c37241b
JJ
3307 bfd_vma g_o_t = htab->sgotplt->output_section->vma
3308 + htab->sgotplt->output_offset;
3309 relocation = htab->sgot->output_section->vma
67a4f2b7 3310 + htab->sgot->output_offset + off - g_o_t;
37e55690
JJ
3311 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3312 && tls_type == GOT_TLS_IE_BOTH)
3313 relocation += 4;
3314 if (r_type == R_386_TLS_IE)
8c37241b 3315 relocation += g_o_t;
b34976b6 3316 unresolved_reloc = FALSE;
13ae64f3 3317 }
67a4f2b7 3318 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
13ae64f3
JJ
3319 {
3320 unsigned int val, type;
3321 bfd_vma roff;
3322
3323 /* GD->IE transition. */
13ae64f3 3324 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
13ae64f3
JJ
3325 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3326 if (type == 0x04)
3327 {
3328 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3329 Change it into:
3330 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
13ae64f3 3331 val >>= 3;
142411ca 3332 roff = rel->r_offset - 3;
13ae64f3
JJ
3333 }
3334 else
3335 {
3336 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3337 Change it into:
3338 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
13ae64f3
JJ
3339 roff = rel->r_offset - 2;
3340 }
3341 memcpy (contents + roff,
3342 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3343 contents[roff + 7] = 0x80 | (val & 7);
37e55690
JJ
3344 /* If foo is used only with foo@gotntpoff(%reg) and
3345 foo@indntpoff, but not with foo@gottpoff(%reg), change
3346 subl $foo@gottpoff(%reg), %eax
3347 into:
3348 addl $foo@gotntpoff(%reg), %eax. */
ebcfb3c0
JJ
3349 if (tls_type == GOT_TLS_IE_POS)
3350 contents[roff + 6] = 0x03;
8c37241b
JJ
3351 bfd_put_32 (output_bfd,
3352 htab->sgot->output_section->vma
3353 + htab->sgot->output_offset + off
3354 - htab->sgotplt->output_section->vma
3355 - htab->sgotplt->output_offset,
13ae64f3
JJ
3356 contents + roff + 8);
3357 /* Skip R_386_PLT32. */
3358 rel++;
3359 continue;
3360 }
67a4f2b7
AO
3361 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3362 {
3363 /* GDesc -> IE transition.
3364 It's originally something like:
3365 leal x@tlsdesc(%ebx), %eax
3366
3367 Change it to:
142411ca 3368 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
67a4f2b7
AO
3369 or:
3370 movl x@gottpoff(%ebx), %eax # before negl %eax
3371
3372 Registers other than %eax may be set up here. */
3373
67a4f2b7
AO
3374 bfd_vma roff;
3375
3376 /* First, make sure it's a leal adding ebx to a 32-bit
3377 offset into any register, although it's probably
3378 almost always going to be eax. */
3379 roff = rel->r_offset;
67a4f2b7
AO
3380
3381 /* Now modify the instruction as appropriate. */
3382 /* To turn a leal into a movl in the form we use it, it
3383 suffices to change the first byte from 0x8d to 0x8b.
3384 aoliva FIXME: should we decide to keep the leal, all
3385 we have to do is remove the statement below, and
3386 adjust the relaxation of R_386_TLS_DESC_CALL. */
3387 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3388
3389 if (tls_type == GOT_TLS_IE_BOTH)
3390 off += 4;
3391
3392 bfd_put_32 (output_bfd,
3393 htab->sgot->output_section->vma
3394 + htab->sgot->output_offset + off
3395 - htab->sgotplt->output_section->vma
3396 - htab->sgotplt->output_offset,
3397 contents + roff);
3398 continue;
3399 }
3400 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3401 {
3402 /* GDesc -> IE transition.
3403 It's originally:
3404 call *(%eax)
3405
3406 Change it to:
142411ca 3407 xchg %ax,%ax
67a4f2b7
AO
3408 or
3409 negl %eax
3410 depending on how we transformed the TLS_GOTDESC above.
3411 */
3412
67a4f2b7
AO
3413 bfd_vma roff;
3414
67a4f2b7 3415 roff = rel->r_offset;
67a4f2b7
AO
3416
3417 /* Now modify the instruction as appropriate. */
3418 if (tls_type != GOT_TLS_IE_NEG)
3419 {
10efb593
L
3420 /* xchg %ax,%ax */
3421 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
3422 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3423 }
3424 else
3425 {
3426 /* negl %eax */
3427 bfd_put_8 (output_bfd, 0xf7, contents + roff);
3428 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3429 }
3430
3431 continue;
3432 }
3433 else
3434 BFD_ASSERT (FALSE);
13ae64f3
JJ
3435 break;
3436
3437 case R_386_TLS_LDM:
142411ca
L
3438 if (! elf_i386_tls_transition (info, input_bfd,
3439 input_section, contents,
3440 symtab_hdr, sym_hashes,
3441 &r_type, GOT_UNKNOWN, rel,
3442 relend, h))
3443 return FALSE;
13ae64f3 3444
142411ca
L
3445 if (r_type != R_386_TLS_LDM)
3446 {
13ae64f3 3447 /* LD->LE transition:
13ae64f3
JJ
3448 leal foo(%reg), %eax; call ___tls_get_addr.
3449 We change it into:
3450 movl %gs:0, %eax; nop; leal 0(%esi,1), %esi. */
142411ca 3451 BFD_ASSERT (r_type == R_386_TLS_LE_32);
13ae64f3
JJ
3452 memcpy (contents + rel->r_offset - 2,
3453 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
a3fadc9a 3454 /* Skip R_386_PC32/R_386_PLT32. */
13ae64f3
JJ
3455 rel++;
3456 continue;
3457 }
3458
3459 if (htab->sgot == NULL)
3460 abort ();
3461
3462 off = htab->tls_ldm_got.offset;
3463 if (off & 1)
3464 off &= ~1;
3465 else
3466 {
947216bf
AM
3467 Elf_Internal_Rela outrel;
3468 bfd_byte *loc;
13ae64f3
JJ
3469
3470 if (htab->srelgot == NULL)
3471 abort ();
3472
3473 outrel.r_offset = (htab->sgot->output_section->vma
3474 + htab->sgot->output_offset + off);
3475
3476 bfd_put_32 (output_bfd, 0,
3477 htab->sgot->contents + off);
3478 bfd_put_32 (output_bfd, 0,
3479 htab->sgot->contents + off + 4);
3480 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
947216bf
AM
3481 loc = htab->srelgot->contents;
3482 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
13ae64f3
JJ
3483 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3484 htab->tls_ldm_got.offset |= 1;
3485 }
8c37241b
JJ
3486 relocation = htab->sgot->output_section->vma
3487 + htab->sgot->output_offset + off
3488 - htab->sgotplt->output_section->vma
3489 - htab->sgotplt->output_offset;
b34976b6 3490 unresolved_reloc = FALSE;
13ae64f3
JJ
3491 break;
3492
3493 case R_386_TLS_LDO_32:
a45bb67d 3494 if (info->shared || (input_section->flags & SEC_CODE) == 0)
13ae64f3
JJ
3495 relocation -= dtpoff_base (info);
3496 else
3497 /* When converting LDO to LE, we must negate. */
3498 relocation = -tpoff (info, relocation);
3499 break;
3500
3501 case R_386_TLS_LE_32:
13ae64f3 3502 case R_386_TLS_LE:
37e55690
JJ
3503 if (info->shared)
3504 {
947216bf 3505 Elf_Internal_Rela outrel;
37e55690 3506 asection *sreloc;
947216bf 3507 bfd_byte *loc;
37e55690
JJ
3508 int indx;
3509
3510 outrel.r_offset = rel->r_offset
3511 + input_section->output_section->vma
3512 + input_section->output_offset;
3513 if (h != NULL && h->dynindx != -1)
3514 indx = h->dynindx;
3515 else
3516 indx = 0;
3517 if (r_type == R_386_TLS_LE_32)
3518 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3519 else
3520 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3521 sreloc = elf_section_data (input_section)->sreloc;
3522 if (sreloc == NULL)
3523 abort ();
947216bf
AM
3524 loc = sreloc->contents;
3525 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
37e55690
JJ
3526 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3527 if (indx)
3528 continue;
3529 else if (r_type == R_386_TLS_LE_32)
3530 relocation = dtpoff_base (info) - relocation;
3531 else
3532 relocation -= dtpoff_base (info);
3533 }
3534 else if (r_type == R_386_TLS_LE_32)
3535 relocation = tpoff (info, relocation);
3536 else
3537 relocation = -tpoff (info, relocation);
13ae64f3
JJ
3538 break;
3539
252b5132
RH
3540 default:
3541 break;
3542 }
3543
239e1f3a
AM
3544 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3545 because such sections are not SEC_ALLOC and thus ld.so will
3546 not process them. */
8c694914 3547 if (unresolved_reloc
239e1f3a 3548 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 3549 && h->def_dynamic))
6a30718d
JJ
3550 {
3551 (*_bfd_error_handler)
843fe662 3552 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
d003868e
AM
3553 input_bfd,
3554 input_section,
6a30718d 3555 (long) rel->r_offset,
843fe662 3556 howto->name,
6a30718d 3557 h->root.root.string);
b34976b6 3558 return FALSE;
6a30718d 3559 }
83be169b 3560
252b5132
RH
3561 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3562 contents, rel->r_offset,
55fd94b0 3563 relocation, 0);
252b5132 3564
cf5c0c5b 3565 if (r != bfd_reloc_ok)
252b5132 3566 {
cf5c0c5b 3567 const char *name;
ffb2e45b 3568
cf5c0c5b
AM
3569 if (h != NULL)
3570 name = h->root.root.string;
3571 else
3572 {
3573 name = bfd_elf_string_from_elf_section (input_bfd,
3574 symtab_hdr->sh_link,
3575 sym->st_name);
3576 if (name == NULL)
b34976b6 3577 return FALSE;
cf5c0c5b
AM
3578 if (*name == '\0')
3579 name = bfd_section_name (input_bfd, sec);
3580 }
ffb2e45b 3581
cf5c0c5b
AM
3582 if (r == bfd_reloc_overflow)
3583 {
cf5c0c5b 3584 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
3585 (info, (h ? &h->root : NULL), name, howto->name,
3586 (bfd_vma) 0, input_bfd, input_section,
3587 rel->r_offset)))
b34976b6 3588 return FALSE;
cf5c0c5b
AM
3589 }
3590 else
3591 {
3592 (*_bfd_error_handler)
d003868e
AM
3593 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3594 input_bfd, input_section,
cf5c0c5b 3595 (long) rel->r_offset, name, (int) r);
b34976b6 3596 return FALSE;
cf5c0c5b 3597 }
252b5132
RH
3598 }
3599 }
3600
b34976b6 3601 return TRUE;
252b5132
RH
3602}
3603
3604/* Finish up dynamic symbol handling. We set the contents of various
3605 dynamic sections here. */
3606
b34976b6 3607static bfd_boolean
55fd94b0
AM
3608elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3609 struct bfd_link_info *info,
3610 struct elf_link_hash_entry *h,
3611 Elf_Internal_Sym *sym)
252b5132 3612{
6725bdbf 3613 struct elf_i386_link_hash_table *htab;
252b5132 3614
6725bdbf 3615 htab = elf_i386_hash_table (info);
252b5132
RH
3616
3617 if (h->plt.offset != (bfd_vma) -1)
3618 {
252b5132
RH
3619 bfd_vma plt_index;
3620 bfd_vma got_offset;
947216bf
AM
3621 Elf_Internal_Rela rel;
3622 bfd_byte *loc;
252b5132
RH
3623
3624 /* This symbol has an entry in the procedure linkage table. Set
3625 it up. */
3626
ffb2e45b
AM
3627 if (h->dynindx == -1
3628 || htab->splt == NULL
3629 || htab->sgotplt == NULL
3630 || htab->srelplt == NULL)
3631 abort ();
252b5132
RH
3632
3633 /* Get the index in the procedure linkage table which
3634 corresponds to this symbol. This is the index of this symbol
3635 in all the symbols for which we are making plt entries. The
3636 first entry in the procedure linkage table is reserved. */
3637 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3638
3639 /* Get the offset into the .got table of the entry that
3640 corresponds to this function. Each .got entry is 4 bytes.
3641 The first three are reserved. */
3642 got_offset = (plt_index + 3) * 4;
3643
3644 /* Fill in the entry in the procedure linkage table. */
3645 if (! info->shared)
3646 {
6725bdbf 3647 memcpy (htab->splt->contents + h->plt.offset, elf_i386_plt_entry,
252b5132
RH
3648 PLT_ENTRY_SIZE);
3649 bfd_put_32 (output_bfd,
6725bdbf
AM
3650 (htab->sgotplt->output_section->vma
3651 + htab->sgotplt->output_offset
252b5132 3652 + got_offset),
6725bdbf 3653 htab->splt->contents + h->plt.offset + 2);
eac338cf
PB
3654
3655 if (htab->is_vxworks)
3656 {
3657 int s, k, reloc_index;
3658
3659 /* Create the R_386_32 relocation referencing the GOT
3660 for this PLT entry. */
3661
3662 /* S: Current slot number (zero-based). */
3663 s = (h->plt.offset - PLT_ENTRY_SIZE) / PLT_ENTRY_SIZE;
3664 /* K: Number of relocations for PLTResolve. */
3665 if (info->shared)
3666 k = PLTRESOLVE_RELOCS_SHLIB;
3667 else
3668 k = PLTRESOLVE_RELOCS;
3669 /* Skip the PLTresolve relocations, and the relocations for
3670 the other PLT slots. */
3671 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3672 loc = (htab->srelplt2->contents + reloc_index
3673 * sizeof (Elf32_External_Rel));
3674
3675 rel.r_offset = (htab->splt->output_section->vma
3676 + htab->splt->output_offset
3677 + h->plt.offset + 2),
7325306f 3678 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
eac338cf
PB
3679 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3680
3681 /* Create the R_386_32 relocation referencing the beginning of
3682 the PLT for this GOT entry. */
3683 rel.r_offset = (htab->sgotplt->output_section->vma
3684 + htab->sgotplt->output_offset
3685 + got_offset);
7325306f 3686 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
eac338cf
PB
3687 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3688 loc + sizeof (Elf32_External_Rel));
3689 }
252b5132
RH
3690 }
3691 else
3692 {
6725bdbf 3693 memcpy (htab->splt->contents + h->plt.offset, elf_i386_pic_plt_entry,
252b5132
RH
3694 PLT_ENTRY_SIZE);
3695 bfd_put_32 (output_bfd, got_offset,
6725bdbf 3696 htab->splt->contents + h->plt.offset + 2);
252b5132
RH
3697 }
3698
3699 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
6725bdbf 3700 htab->splt->contents + h->plt.offset + 7);
252b5132 3701 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
6725bdbf 3702 htab->splt->contents + h->plt.offset + 12);
252b5132
RH
3703
3704 /* Fill in the entry in the global offset table. */
3705 bfd_put_32 (output_bfd,
6725bdbf
AM
3706 (htab->splt->output_section->vma
3707 + htab->splt->output_offset
252b5132
RH
3708 + h->plt.offset
3709 + 6),
6725bdbf 3710 htab->sgotplt->contents + got_offset);
252b5132
RH
3711
3712 /* Fill in the entry in the .rel.plt section. */
6725bdbf
AM
3713 rel.r_offset = (htab->sgotplt->output_section->vma
3714 + htab->sgotplt->output_offset
252b5132
RH
3715 + got_offset);
3716 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
947216bf 3717 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rel);
0ac8d2ca 3718 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
252b5132 3719
f5385ebf 3720 if (!h->def_regular)
252b5132
RH
3721 {
3722 /* Mark the symbol as undefined, rather than as defined in
c6585bbb
JJ
3723 the .plt section. Leave the value if there were any
3724 relocations where pointer equality matters (this is a clue
51b64d56
AM
3725 for the dynamic linker, to make function pointer
3726 comparisons work between an application and shared
c6585bbb
JJ
3727 library), otherwise set it to zero. If a function is only
3728 called from a binary, there is no need to slow down
3729 shared libraries because of that. */
252b5132 3730 sym->st_shndx = SHN_UNDEF;
f5385ebf 3731 if (!h->pointer_equality_needed)
c6585bbb 3732 sym->st_value = 0;
252b5132
RH
3733 }
3734 }
3735
13ae64f3 3736 if (h->got.offset != (bfd_vma) -1
67a4f2b7 3737 && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
37e55690 3738 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
252b5132 3739 {
947216bf
AM
3740 Elf_Internal_Rela rel;
3741 bfd_byte *loc;
252b5132
RH
3742
3743 /* This symbol has an entry in the global offset table. Set it
3744 up. */
3745
ffb2e45b
AM
3746 if (htab->sgot == NULL || htab->srelgot == NULL)
3747 abort ();
252b5132 3748
6725bdbf
AM
3749 rel.r_offset = (htab->sgot->output_section->vma
3750 + htab->sgot->output_offset
dc810e39 3751 + (h->got.offset & ~(bfd_vma) 1));
252b5132 3752
dd5724d5
AM
3753 /* If this is a static link, or it is a -Bsymbolic link and the
3754 symbol is defined locally or was forced to be local because
3755 of a version file, we just want to emit a RELATIVE reloc.
252b5132
RH
3756 The entry in the global offset table will already have been
3757 initialized in the relocate_section function. */
6725bdbf 3758 if (info->shared
586119b3 3759 && SYMBOL_REFERENCES_LOCAL (info, h))
dd5724d5 3760 {
6725bdbf 3761 BFD_ASSERT((h->got.offset & 1) != 0);
dd5724d5
AM
3762 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3763 }
252b5132
RH
3764 else
3765 {
dd5724d5 3766 BFD_ASSERT((h->got.offset & 1) == 0);
6725bdbf
AM
3767 bfd_put_32 (output_bfd, (bfd_vma) 0,
3768 htab->sgot->contents + h->got.offset);
252b5132
RH
3769 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3770 }
3771
947216bf
AM
3772 loc = htab->srelgot->contents;
3773 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
0ac8d2ca 3774 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
252b5132
RH
3775 }
3776
f5385ebf 3777 if (h->needs_copy)
252b5132 3778 {
947216bf
AM
3779 Elf_Internal_Rela rel;
3780 bfd_byte *loc;
252b5132
RH
3781
3782 /* This symbol needs a copy reloc. Set it up. */
3783
ffb2e45b
AM
3784 if (h->dynindx == -1
3785 || (h->root.type != bfd_link_hash_defined
3786 && h->root.type != bfd_link_hash_defweak)
3787 || htab->srelbss == NULL)
3788 abort ();
252b5132
RH
3789
3790 rel.r_offset = (h->root.u.def.value
3791 + h->root.u.def.section->output_section->vma
3792 + h->root.u.def.section->output_offset);
3793 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
947216bf
AM
3794 loc = htab->srelbss->contents;
3795 loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rel);
0ac8d2ca 3796 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
252b5132
RH
3797 }
3798
eac338cf
PB
3799 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.
3800 On VxWorks, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it
3801 is relative to the ".got" section. */
252b5132 3802 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
22edb2f1 3803 || (!htab->is_vxworks && h == htab->elf.hgot))
252b5132
RH
3804 sym->st_shndx = SHN_ABS;
3805
b34976b6 3806 return TRUE;
252b5132
RH
3807}
3808
38701953
AM
3809/* Used to decide how to sort relocs in an optimal manner for the
3810 dynamic linker, before writing them out. */
3811
3812static enum elf_reloc_type_class
55fd94b0 3813elf_i386_reloc_type_class (const Elf_Internal_Rela *rela)
38701953 3814{
55fd94b0 3815 switch (ELF32_R_TYPE (rela->r_info))
38701953
AM
3816 {
3817 case R_386_RELATIVE:
3818 return reloc_class_relative;
3819 case R_386_JUMP_SLOT:
3820 return reloc_class_plt;
3821 case R_386_COPY:
3822 return reloc_class_copy;
3823 default:
3824 return reloc_class_normal;
3825 }
3826}
3827
252b5132
RH
3828/* Finish up the dynamic sections. */
3829
b34976b6 3830static bfd_boolean
55fd94b0
AM
3831elf_i386_finish_dynamic_sections (bfd *output_bfd,
3832 struct bfd_link_info *info)
252b5132 3833{
6725bdbf 3834 struct elf_i386_link_hash_table *htab;
252b5132 3835 bfd *dynobj;
252b5132
RH
3836 asection *sdyn;
3837
6725bdbf 3838 htab = elf_i386_hash_table (info);
ebe50bae 3839 dynobj = htab->elf.dynobj;
252b5132
RH
3840 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3841
ebe50bae 3842 if (htab->elf.dynamic_sections_created)
252b5132 3843 {
252b5132
RH
3844 Elf32_External_Dyn *dyncon, *dynconend;
3845
ffb2e45b
AM
3846 if (sdyn == NULL || htab->sgot == NULL)
3847 abort ();
252b5132
RH
3848
3849 dyncon = (Elf32_External_Dyn *) sdyn->contents;
eea6121a 3850 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
252b5132
RH
3851 for (; dyncon < dynconend; dyncon++)
3852 {
3853 Elf_Internal_Dyn dyn;
51b64d56 3854 asection *s;
252b5132
RH
3855
3856 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3857
3858 switch (dyn.d_tag)
3859 {
3860 default:
7a2b07ff
NS
3861 if (htab->is_vxworks
3862 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
3863 break;
0ac8d2ca 3864 continue;
252b5132
RH
3865
3866 case DT_PLTGOT:
8c37241b
JJ
3867 s = htab->sgotplt;
3868 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6725bdbf
AM
3869 break;
3870
252b5132 3871 case DT_JMPREL:
6348e046
AM
3872 s = htab->srelplt;
3873 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
252b5132
RH
3874 break;
3875
3876 case DT_PLTRELSZ:
6348e046 3877 s = htab->srelplt;
eea6121a 3878 dyn.d_un.d_val = s->size;
252b5132
RH
3879 break;
3880
3881 case DT_RELSZ:
3882 /* My reading of the SVR4 ABI indicates that the
3883 procedure linkage table relocs (DT_JMPREL) should be
3884 included in the overall relocs (DT_REL). This is
3885 what Solaris does. However, UnixWare can not handle
3886 that case. Therefore, we override the DT_RELSZ entry
6348e046
AM
3887 here to make it not include the JMPREL relocs. */
3888 s = htab->srelplt;
3889 if (s == NULL)
3890 continue;
eea6121a 3891 dyn.d_un.d_val -= s->size;
6348e046
AM
3892 break;
3893
3894 case DT_REL:
3895 /* We may not be using the standard ELF linker script.
3896 If .rel.plt is the first .rel section, we adjust
3897 DT_REL to not include it. */
3898 s = htab->srelplt;
3899 if (s == NULL)
3900 continue;
3901 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
3902 continue;
eea6121a 3903 dyn.d_un.d_ptr += s->size;
252b5132
RH
3904 break;
3905 }
0ac8d2ca
AM
3906
3907 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
252b5132
RH
3908 }
3909
3910 /* Fill in the first entry in the procedure linkage table. */
eea6121a 3911 if (htab->splt && htab->splt->size > 0)
252b5132
RH
3912 {
3913 if (info->shared)
eac338cf
PB
3914 {
3915 memcpy (htab->splt->contents, elf_i386_pic_plt0_entry,
3916 sizeof (elf_i386_pic_plt0_entry));
3917 memset (htab->splt->contents + sizeof (elf_i386_pic_plt0_entry),
3918 htab->plt0_pad_byte,
3919 PLT_ENTRY_SIZE - sizeof (elf_i386_pic_plt0_entry));
3920 }
252b5132
RH
3921 else
3922 {
eac338cf
PB
3923 memcpy (htab->splt->contents, elf_i386_plt0_entry,
3924 sizeof(elf_i386_plt0_entry));
3925 memset (htab->splt->contents + sizeof (elf_i386_plt0_entry),
3926 htab->plt0_pad_byte,
3927 PLT_ENTRY_SIZE - sizeof (elf_i386_plt0_entry));
252b5132 3928 bfd_put_32 (output_bfd,
6725bdbf
AM
3929 (htab->sgotplt->output_section->vma
3930 + htab->sgotplt->output_offset
3931 + 4),
3932 htab->splt->contents + 2);
252b5132 3933 bfd_put_32 (output_bfd,
6725bdbf
AM
3934 (htab->sgotplt->output_section->vma
3935 + htab->sgotplt->output_offset
3936 + 8),
3937 htab->splt->contents + 8);
eac338cf
PB
3938
3939 if (htab->is_vxworks)
3940 {
3941 Elf_Internal_Rela rel;
7325306f 3942
eac338cf
PB
3943 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
3944 On IA32 we use REL relocations so the addend goes in
3945 the PLT directly. */
3946 rel.r_offset = (htab->splt->output_section->vma
3947 + htab->splt->output_offset
3948 + 2);
7325306f 3949 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
eac338cf
PB
3950 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3951 htab->srelplt2->contents);
3952 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
3953 rel.r_offset = (htab->splt->output_section->vma
3954 + htab->splt->output_offset
3955 + 8);
7325306f 3956 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
eac338cf
PB
3957 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3958 htab->srelplt2->contents +
3959 sizeof (Elf32_External_Rel));
3960 }
252b5132
RH
3961 }
3962
3963 /* UnixWare sets the entsize of .plt to 4, although that doesn't
3964 really seem like the right value. */
6725bdbf
AM
3965 elf_section_data (htab->splt->output_section)
3966 ->this_hdr.sh_entsize = 4;
eac338cf
PB
3967
3968 /* Correct the .rel.plt.unloaded relocations. */
3969 if (htab->is_vxworks && !info->shared)
3970 {
3971 int num_plts = (htab->splt->size / PLT_ENTRY_SIZE) - 1;
4c9b0de6 3972 unsigned char *p;
eac338cf
PB
3973
3974 p = htab->srelplt2->contents;
3975 if (info->shared)
3976 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
3977 else
3978 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
3979
3980 for (; num_plts; num_plts--)
3981 {
3982 Elf_Internal_Rela rel;
3983 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
7325306f 3984 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
eac338cf
PB
3985 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
3986 p += sizeof (Elf32_External_Rel);
3987
3988 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
7325306f 3989 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
eac338cf
PB
3990 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
3991 p += sizeof (Elf32_External_Rel);
3992 }
3993 }
252b5132
RH
3994 }
3995 }
3996
12d0ee4a 3997 if (htab->sgotplt)
252b5132 3998 {
12d0ee4a 3999 /* Fill in the first three entries in the global offset table. */
eea6121a 4000 if (htab->sgotplt->size > 0)
12d0ee4a
AM
4001 {
4002 bfd_put_32 (output_bfd,
55fd94b0 4003 (sdyn == NULL ? 0
12d0ee4a
AM
4004 : sdyn->output_section->vma + sdyn->output_offset),
4005 htab->sgotplt->contents);
55fd94b0
AM
4006 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 4);
4007 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 8);
12d0ee4a 4008 }
252b5132 4009
12d0ee4a
AM
4010 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = 4;
4011 }
8c37241b 4012
eea6121a 4013 if (htab->sgot && htab->sgot->size > 0)
8c37241b
JJ
4014 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
4015
b34976b6 4016 return TRUE;
252b5132
RH
4017}
4018
4c45e5c9
JJ
4019/* Return address for Ith PLT stub in section PLT, for relocation REL
4020 or (bfd_vma) -1 if it should not be included. */
4021
4022static bfd_vma
4023elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
4024 const arelent *rel ATTRIBUTE_UNUSED)
4025{
4026 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4027}
4028
fdc90cb4
JJ
4029/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4030
4031static bfd_boolean
4032elf_i386_hash_symbol (struct elf_link_hash_entry *h)
4033{
4034 if (h->plt.offset != (bfd_vma) -1
4035 && !h->def_regular
4036 && !h->pointer_equality_needed)
4037 return FALSE;
4038
4039 return _bfd_elf_hash_symbol (h);
4040}
4c45e5c9 4041
252b5132
RH
4042#define TARGET_LITTLE_SYM bfd_elf32_i386_vec
4043#define TARGET_LITTLE_NAME "elf32-i386"
4044#define ELF_ARCH bfd_arch_i386
4045#define ELF_MACHINE_CODE EM_386
4046#define ELF_MAXPAGESIZE 0x1000
252b5132
RH
4047
4048#define elf_backend_can_gc_sections 1
51b64d56 4049#define elf_backend_can_refcount 1
252b5132
RH
4050#define elf_backend_want_got_plt 1
4051#define elf_backend_plt_readonly 1
4052#define elf_backend_want_plt_sym 0
4053#define elf_backend_got_header_size 12
252b5132 4054
8c29f035
AM
4055/* Support RELA for objdump of prelink objects. */
4056#define elf_info_to_howto elf_i386_info_to_howto_rel
dd5724d5
AM
4057#define elf_info_to_howto_rel elf_i386_info_to_howto_rel
4058
13ae64f3 4059#define bfd_elf32_mkobject elf_i386_mkobject
13ae64f3 4060
dd5724d5
AM
4061#define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
4062#define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
4063#define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
13285a1b 4064#define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
dd5724d5
AM
4065
4066#define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
13285a1b 4067#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
dd5724d5 4068#define elf_backend_check_relocs elf_i386_check_relocs
0ac8d2ca 4069#define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
6725bdbf 4070#define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
0ac8d2ca 4071#define elf_backend_fake_sections elf_i386_fake_sections
dd5724d5
AM
4072#define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
4073#define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
4074#define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
4075#define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
c5fccbec
DJ
4076#define elf_backend_grok_prstatus elf_i386_grok_prstatus
4077#define elf_backend_grok_psinfo elf_i386_grok_psinfo
db6751f2 4078#define elf_backend_reloc_type_class elf_i386_reloc_type_class
0ac8d2ca
AM
4079#define elf_backend_relocate_section elf_i386_relocate_section
4080#define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
67a4f2b7 4081#define elf_backend_always_size_sections elf_i386_always_size_sections
74541ad4
AM
4082#define elf_backend_omit_section_dynsym \
4083 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4c45e5c9 4084#define elf_backend_plt_sym_val elf_i386_plt_sym_val
fdc90cb4 4085#define elf_backend_hash_symbol elf_i386_hash_symbol
dd5724d5 4086
252b5132 4087#include "elf32-target.h"
2bc3c89a
AM
4088
4089/* FreeBSD support. */
4090
4091#undef TARGET_LITTLE_SYM
4092#define TARGET_LITTLE_SYM bfd_elf32_i386_freebsd_vec
4093#undef TARGET_LITTLE_NAME
4094#define TARGET_LITTLE_NAME "elf32-i386-freebsd"
d1036acb
L
4095#undef ELF_OSABI
4096#define ELF_OSABI ELFOSABI_FREEBSD
2bc3c89a
AM
4097
4098/* The kernel recognizes executables as valid only if they carry a
4099 "FreeBSD" label in the ELF header. So we put this label on all
4100 executables and (for simplicity) also all other object files. */
4101
2bc3c89a 4102static void
55fd94b0
AM
4103elf_i386_post_process_headers (bfd *abfd,
4104 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2bc3c89a
AM
4105{
4106 Elf_Internal_Ehdr *i_ehdrp;
4107
4108 i_ehdrp = elf_elfheader (abfd);
4109
4110 /* Put an ABI label supported by FreeBSD >= 4.1. */
d1036acb 4111 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
2bc3c89a
AM
4112#ifdef OLD_FREEBSD_ABI_LABEL
4113 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
4114 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
caf47ea6 4115#endif
2bc3c89a
AM
4116}
4117
4118#undef elf_backend_post_process_headers
571fe01f
NC
4119#define elf_backend_post_process_headers elf_i386_post_process_headers
4120#undef elf32_bed
4121#define elf32_bed elf32_i386_fbsd_bed
2bc3c89a
AM
4122
4123#include "elf32-target.h"
eac338cf
PB
4124
4125/* VxWorks support. */
4126
4127#undef TARGET_LITTLE_SYM
4128#define TARGET_LITTLE_SYM bfd_elf32_i386_vxworks_vec
4129#undef TARGET_LITTLE_NAME
4130#define TARGET_LITTLE_NAME "elf32-i386-vxworks"
d1036acb 4131#undef ELF_OSABI
eac338cf
PB
4132
4133/* Like elf_i386_link_hash_table_create but with tweaks for VxWorks. */
4134
4135static struct bfd_link_hash_table *
4136elf_i386_vxworks_link_hash_table_create (bfd *abfd)
4137{
4138 struct bfd_link_hash_table *ret;
4139 struct elf_i386_link_hash_table *htab;
4140
4141 ret = elf_i386_link_hash_table_create (abfd);
4142 if (ret)
4143 {
4144 htab = (struct elf_i386_link_hash_table *) ret;
4145 htab->is_vxworks = 1;
4146 htab->plt0_pad_byte = 0x90;
4147 }
4148
4149 return ret;
4150}
4151
4152
13285a1b
AM
4153#undef elf_backend_relocs_compatible
4154#undef elf_backend_post_process_headers
eac338cf
PB
4155#undef bfd_elf32_bfd_link_hash_table_create
4156#define bfd_elf32_bfd_link_hash_table_create \
4157 elf_i386_vxworks_link_hash_table_create
4158#undef elf_backend_add_symbol_hook
4159#define elf_backend_add_symbol_hook \
4160 elf_vxworks_add_symbol_hook
4161#undef elf_backend_link_output_symbol_hook
4162#define elf_backend_link_output_symbol_hook \
9c72ff84 4163 elf_vxworks_link_output_symbol_hook
eac338cf
PB
4164#undef elf_backend_emit_relocs
4165#define elf_backend_emit_relocs elf_vxworks_emit_relocs
4166#undef elf_backend_final_write_processing
4167#define elf_backend_final_write_processing \
4168 elf_vxworks_final_write_processing
4169
4170/* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
4171 define it. */
4172#undef elf_backend_want_plt_sym
4173#define elf_backend_want_plt_sym 1
4174
4175#undef elf32_bed
4176#define elf32_bed elf32_i386_vxworks_bed
4177
4178#include "elf32-target.h"
This page took 0.710015 seconds and 4 git commands to generate.