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