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