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