NaCl changes
[nacl-binutils.git] / bfd / elf32-nacl.c
blob20223473b23dbfceee87b12f2a66267023326750
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.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
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 "elf/nacl.h"
30 /* NativeClient binary format description input. */
31 /* NativeClient format is based on elf32. */
34 /* 386 uses REL relocations instead of RELA. */
35 #define USE_REL 1
37 #include "elf/i386.h"
39 static reloc_howto_type elf_howto_table[]=
41 HOWTO(R_386_NONE, 0, 0, 0, FALSE, 0, complain_overflow_bitfield,
42 bfd_elf_generic_reloc, "R_386_NONE",
43 TRUE, 0x00000000, 0x00000000, FALSE),
44 HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
45 bfd_elf_generic_reloc, "R_386_32",
46 TRUE, 0xffffffff, 0xffffffff, FALSE),
47 HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
48 bfd_elf_generic_reloc, "R_386_PC32",
49 TRUE, 0xffffffff, 0xffffffff, TRUE),
50 HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
51 bfd_elf_generic_reloc, "R_386_GOT32",
52 TRUE, 0xffffffff, 0xffffffff, FALSE),
53 HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
54 bfd_elf_generic_reloc, "R_386_PLT32",
55 TRUE, 0xffffffff, 0xffffffff, TRUE),
56 HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
57 bfd_elf_generic_reloc, "R_386_COPY",
58 TRUE, 0xffffffff, 0xffffffff, FALSE),
59 HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
60 bfd_elf_generic_reloc, "R_386_GLOB_DAT",
61 TRUE, 0xffffffff, 0xffffffff, FALSE),
62 HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
63 bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
64 TRUE, 0xffffffff, 0xffffffff, FALSE),
65 HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
66 bfd_elf_generic_reloc, "R_386_RELATIVE",
67 TRUE, 0xffffffff, 0xffffffff, FALSE),
68 HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
69 bfd_elf_generic_reloc, "R_386_GOTOFF",
70 TRUE, 0xffffffff, 0xffffffff, FALSE),
71 HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
72 bfd_elf_generic_reloc, "R_386_GOTPC",
73 TRUE, 0xffffffff, 0xffffffff, TRUE),
75 /* We have a gap in the reloc numbers here.
76 R_386_standard counts the number up to this point, and
77 R_386_ext_offset is the value to subtract from a reloc type of
78 R_386_16 thru R_386_PC8 to form an index into this table. */
79 #define R_386_standard (R_386_GOTPC + 1)
80 #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
82 /* These relocs are a GNU extension. */
83 HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
84 bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
85 TRUE, 0xffffffff, 0xffffffff, FALSE),
86 HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
87 bfd_elf_generic_reloc, "R_386_TLS_IE",
88 TRUE, 0xffffffff, 0xffffffff, FALSE),
89 HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
90 bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
91 TRUE, 0xffffffff, 0xffffffff, FALSE),
92 HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
93 bfd_elf_generic_reloc, "R_386_TLS_LE",
94 TRUE, 0xffffffff, 0xffffffff, FALSE),
95 HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
96 bfd_elf_generic_reloc, "R_386_TLS_GD",
97 TRUE, 0xffffffff, 0xffffffff, FALSE),
98 HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
99 bfd_elf_generic_reloc, "R_386_TLS_LDM",
100 TRUE, 0xffffffff, 0xffffffff, FALSE),
101 HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
102 bfd_elf_generic_reloc, "R_386_16",
103 TRUE, 0xffff, 0xffff, FALSE),
104 HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
105 bfd_elf_generic_reloc, "R_386_PC16",
106 TRUE, 0xffff, 0xffff, TRUE),
107 HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
108 bfd_elf_generic_reloc, "R_386_8",
109 TRUE, 0xff, 0xff, FALSE),
110 HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
111 bfd_elf_generic_reloc, "R_386_PC8",
112 TRUE, 0xff, 0xff, TRUE),
114 #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
115 #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
116 /* These are common with Solaris TLS implementation. */
117 HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
118 bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
119 TRUE, 0xffffffff, 0xffffffff, FALSE),
120 HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
121 bfd_elf_generic_reloc, "R_386_TLS_IE_32",
122 TRUE, 0xffffffff, 0xffffffff, FALSE),
123 HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
124 bfd_elf_generic_reloc, "R_386_TLS_LE_32",
125 TRUE, 0xffffffff, 0xffffffff, FALSE),
126 HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
127 bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
128 TRUE, 0xffffffff, 0xffffffff, FALSE),
129 HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
130 bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
131 TRUE, 0xffffffff, 0xffffffff, FALSE),
132 HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
133 bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
134 TRUE, 0xffffffff, 0xffffffff, FALSE),
135 EMPTY_HOWTO (38),
136 HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
137 bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
138 TRUE, 0xffffffff, 0xffffffff, FALSE),
139 HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
140 bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
141 FALSE, 0, 0, FALSE),
142 HOWTO(R_386_TLS_DESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
143 bfd_elf_generic_reloc, "R_386_TLS_DESC",
144 TRUE, 0xffffffff, 0xffffffff, FALSE),
146 /* Another gap. */
147 #define R_386_tls (R_386_TLS_DESC + 1 - R_386_tls_offset)
148 #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_tls)
150 /* GNU extension to record C++ vtable hierarchy. */
151 HOWTO (R_386_GNU_VTINHERIT, /* type */
152 0, /* rightshift */
153 2, /* size (0 = byte, 1 = short, 2 = long) */
154 0, /* bitsize */
155 FALSE, /* pc_relative */
156 0, /* bitpos */
157 complain_overflow_dont, /* complain_on_overflow */
158 NULL, /* special_function */
159 "R_386_GNU_VTINHERIT", /* name */
160 FALSE, /* partial_inplace */
161 0, /* src_mask */
162 0, /* dst_mask */
163 FALSE), /* pcrel_offset */
165 /* GNU extension to record C++ vtable member usage. */
166 HOWTO (R_386_GNU_VTENTRY, /* type */
167 0, /* rightshift */
168 2, /* size (0 = byte, 1 = short, 2 = long) */
169 0, /* bitsize */
170 FALSE, /* pc_relative */
171 0, /* bitpos */
172 complain_overflow_dont, /* complain_on_overflow */
173 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
174 "R_386_GNU_VTENTRY", /* name */
175 FALSE, /* partial_inplace */
176 0, /* src_mask */
177 0, /* dst_mask */
178 FALSE) /* pcrel_offset */
180 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
184 #ifdef DEBUG_GEN_RELOC
185 #define TRACE(str) \
186 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
187 #else
188 #define TRACE(str)
189 #endif
191 static reloc_howto_type *
192 elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
193 bfd_reloc_code_real_type code)
195 switch (code)
197 case BFD_RELOC_NONE:
198 TRACE ("BFD_RELOC_NONE");
199 return &elf_howto_table[R_386_NONE];
201 case BFD_RELOC_32:
202 TRACE ("BFD_RELOC_32");
203 return &elf_howto_table[R_386_32];
205 case BFD_RELOC_CTOR:
206 TRACE ("BFD_RELOC_CTOR");
207 return &elf_howto_table[R_386_32];
209 case BFD_RELOC_32_PCREL:
210 TRACE ("BFD_RELOC_PC32");
211 return &elf_howto_table[R_386_PC32];
213 case BFD_RELOC_386_GOT32:
214 TRACE ("BFD_RELOC_386_GOT32");
215 return &elf_howto_table[R_386_GOT32];
217 case BFD_RELOC_386_PLT32:
218 TRACE ("BFD_RELOC_386_PLT32");
219 return &elf_howto_table[R_386_PLT32];
221 case BFD_RELOC_386_COPY:
222 TRACE ("BFD_RELOC_386_COPY");
223 return &elf_howto_table[R_386_COPY];
225 case BFD_RELOC_386_GLOB_DAT:
226 TRACE ("BFD_RELOC_386_GLOB_DAT");
227 return &elf_howto_table[R_386_GLOB_DAT];
229 case BFD_RELOC_386_JUMP_SLOT:
230 TRACE ("BFD_RELOC_386_JUMP_SLOT");
231 return &elf_howto_table[R_386_JUMP_SLOT];
233 case BFD_RELOC_386_RELATIVE:
234 TRACE ("BFD_RELOC_386_RELATIVE");
235 return &elf_howto_table[R_386_RELATIVE];
237 case BFD_RELOC_386_GOTOFF:
238 TRACE ("BFD_RELOC_386_GOTOFF");
239 return &elf_howto_table[R_386_GOTOFF];
241 case BFD_RELOC_386_GOTPC:
242 TRACE ("BFD_RELOC_386_GOTPC");
243 return &elf_howto_table[R_386_GOTPC];
245 /* These relocs are a GNU extension. */
246 case BFD_RELOC_386_TLS_TPOFF:
247 TRACE ("BFD_RELOC_386_TLS_TPOFF");
248 return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
250 case BFD_RELOC_386_TLS_IE:
251 TRACE ("BFD_RELOC_386_TLS_IE");
252 return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
254 case BFD_RELOC_386_TLS_GOTIE:
255 TRACE ("BFD_RELOC_386_TLS_GOTIE");
256 return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
258 case BFD_RELOC_386_TLS_LE:
259 TRACE ("BFD_RELOC_386_TLS_LE");
260 return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
262 case BFD_RELOC_386_TLS_GD:
263 TRACE ("BFD_RELOC_386_TLS_GD");
264 return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
266 case BFD_RELOC_386_TLS_LDM:
267 TRACE ("BFD_RELOC_386_TLS_LDM");
268 return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
270 case BFD_RELOC_16:
271 TRACE ("BFD_RELOC_16");
272 return &elf_howto_table[R_386_16 - R_386_ext_offset];
274 case BFD_RELOC_16_PCREL:
275 TRACE ("BFD_RELOC_16_PCREL");
276 return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
278 case BFD_RELOC_8:
279 TRACE ("BFD_RELOC_8");
280 return &elf_howto_table[R_386_8 - R_386_ext_offset];
282 case BFD_RELOC_8_PCREL:
283 TRACE ("BFD_RELOC_8_PCREL");
284 return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
286 /* Common with Sun TLS implementation. */
287 case BFD_RELOC_386_TLS_LDO_32:
288 TRACE ("BFD_RELOC_386_TLS_LDO_32");
289 return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
291 case BFD_RELOC_386_TLS_IE_32:
292 TRACE ("BFD_RELOC_386_TLS_IE_32");
293 return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
295 case BFD_RELOC_386_TLS_LE_32:
296 TRACE ("BFD_RELOC_386_TLS_LE_32");
297 return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
299 case BFD_RELOC_386_TLS_DTPMOD32:
300 TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
301 return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
303 case BFD_RELOC_386_TLS_DTPOFF32:
304 TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
305 return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
307 case BFD_RELOC_386_TLS_TPOFF32:
308 TRACE ("BFD_RELOC_386_TLS_TPOFF32");
309 return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
311 case BFD_RELOC_386_TLS_GOTDESC:
312 TRACE ("BFD_RELOC_386_TLS_GOTDESC");
313 return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
315 case BFD_RELOC_386_TLS_DESC_CALL:
316 TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
317 return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
319 case BFD_RELOC_386_TLS_DESC:
320 TRACE ("BFD_RELOC_386_TLS_DESC");
321 return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
323 case BFD_RELOC_VTABLE_INHERIT:
324 TRACE ("BFD_RELOC_VTABLE_INHERIT");
325 return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
327 case BFD_RELOC_VTABLE_ENTRY:
328 TRACE ("BFD_RELOC_VTABLE_ENTRY");
329 return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
331 default:
332 break;
335 TRACE ("Unknown");
336 return 0;
339 static reloc_howto_type *
340 elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
341 const char *r_name)
343 unsigned int i;
345 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
346 if (elf_howto_table[i].name != NULL
347 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
348 return &elf_howto_table[i];
350 return NULL;
353 static void
354 elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
355 arelent *cache_ptr,
356 Elf_Internal_Rela *dst)
358 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
359 unsigned int indx;
361 if ((indx = r_type) >= R_386_standard
362 && ((indx = r_type - R_386_ext_offset) - R_386_standard
363 >= R_386_ext - R_386_standard)
364 && ((indx = r_type - R_386_tls_offset) - R_386_ext
365 >= R_386_tls - R_386_ext)
366 && ((indx = r_type - R_386_vt_offset) - R_386_tls
367 >= R_386_vt - R_386_tls))
369 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
370 abfd, (int) r_type);
371 indx = R_386_NONE;
373 cache_ptr->howto = &elf_howto_table[indx];
376 /* Return whether a symbol name implies a local label. The UnixWare
377 2.1 cc generates temporary symbols that start with .X, so we
378 recognize them here. FIXME: do other SVR4 compilers also use .X?.
379 If so, we should move the .X recognition into
380 _bfd_elf_is_local_label_name. */
382 static bfd_boolean
383 elf_i386_is_local_label_name (bfd *abfd, const char *name)
385 if (name[0] == '.' && name[1] == 'X')
386 return TRUE;
388 return _bfd_elf_is_local_label_name (abfd, name);
391 /* Support for core dump NOTE sections. */
393 static bfd_boolean
394 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
396 int offset;
397 size_t size;
399 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
401 int pr_version = bfd_get_32 (abfd, note->descdata);
403 if (pr_version != 1)
404 return FALSE;
406 /* pr_cursig */
407 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 20);
409 /* pr_pid */
410 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
412 /* pr_reg */
413 offset = 28;
414 size = bfd_get_32 (abfd, note->descdata + 8);
416 else
418 switch (note->descsz)
420 default:
421 return FALSE;
423 case 144: /* Linux/i386 */
424 /* pr_cursig */
425 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
427 /* pr_pid */
428 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
430 /* pr_reg */
431 offset = 72;
432 size = 68;
434 break;
438 /* Make a ".reg/999" section. */
439 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
440 size, note->descpos + offset);
443 static bfd_boolean
444 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
446 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
448 int pr_version = bfd_get_32 (abfd, note->descdata);
450 if (pr_version != 1)
451 return FALSE;
453 elf_tdata (abfd)->core_program
454 = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
455 elf_tdata (abfd)->core_command
456 = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
458 else
460 switch (note->descsz)
462 default:
463 return FALSE;
465 case 124: /* Linux/i386 elf_prpsinfo. */
466 elf_tdata (abfd)->core_program
467 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
468 elf_tdata (abfd)->core_command
469 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
473 /* Note that for some reason, a spurious space is tacked
474 onto the end of the args in some (at least one anyway)
475 implementations, so strip it off if it exists. */
477 char *command = elf_tdata (abfd)->core_command;
478 int n = strlen (command);
480 if (0 < n && command[n - 1] == ' ')
481 command[n - 1] = '\0';
484 return TRUE;
487 /* Functions for the i386 ELF linker.
489 In order to gain some understanding of code in this file without
490 knowing all the intricate details of the linker, note the
491 following:
493 Functions named elf_i386_* are called by external routines, other
494 functions are only called locally. elf_i386_* functions appear
495 in this file more or less in the order in which they are called
496 from external routines. eg. elf_i386_check_relocs is called
497 early in the link process, elf_i386_finish_dynamic_sections is
498 one of the last functions. */
501 /* The name of the dynamic interpreter. This is put in the .interp
502 section. */
504 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
506 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
507 copying dynamic variables from a shared lib into an app's dynbss
508 section, and instead use a dynamic relocation to point into the
509 shared lib. */
510 #define ELIMINATE_COPY_RELOCS 1
512 /* The size in bytes of an entry in the procedure linkage table. */
514 #define PLT_ENTRY_SIZE 32
516 /* The first entry in an absolute procedure linkage table looks like
517 this. See the SVR4 ABI i386 supplement to see how this works.
518 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
520 static const bfd_byte elf_i386_plt0_entry[13] =
522 0xff, 0x35, /* pushl contents of address */
523 0, 0, 0, 0, /* replaced with address of .got + 4. */
524 0xb9, /* movl address, %ecx */
525 0, 0, 0, 0, /* replaced with address of .got + 8. */
526 0xff, 0xe1 /* jmp *%ecx */
529 /* Subsequent entries in an absolute procedure linkage table look like
530 this. */
532 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
534 0xb9, /* movl address, %ecx */
535 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
536 0x83, 0xe1, 0xe0, /* andl %ecx, NACLMASK */
537 0xff, 0xe1, /* jmp *%ecx */
538 0x90, /* nop, aligns pic/non-pic entries for relocation */
539 0x68, /* pushl immediate */
540 0, 0, 0, 0, /* replaced with offset into relocation table. */
541 0xe9, /* jmp relative */
542 0, 0, 0, 0, /* replaced with offset to start of .plt. */
543 0xf4, 0xf4, 0xf4, /* fill with hlt instructions. */
544 0xf4, 0xf4, 0xf4, 0xf4, /* fill with hlt instructions. */
545 0xf4, 0xf4, 0xf4, 0xf4 /* fill with hlt instructions. */
548 /* The first entry in a PIC procedure linkage table look like this.
549 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
551 static const bfd_byte elf_i386_pic_plt0_entry[12] =
553 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
554 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */
557 /* Subsequent entries in a PIC procedure linkage table look like this. */
559 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
561 0x8b, 0x8b, /* movl offset(%ebx), %ecx */
562 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
563 0x83, 0xe1, 0xe0, /* andl %ecx, NACLMASK */
564 0xff, 0xe1, /* jmp *%ecx */
565 0x68, /* pushl immediate */
566 0, 0, 0, 0, /* replaced with offset into relocation table. */
567 0xe9, /* jmp relative */
568 0, 0, 0, 0, /* replaced with offset to start of .plt. */
569 0xf4, 0xf4, 0xf4, /* fill with hlt instructions. */
570 0xf4, 0xf4, 0xf4, 0xf4, /* fill with hlt instructions. */
571 0xf4, 0xf4, 0xf4, 0xf4 /* fill with hlt instructions. */
574 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
575 for the PLTResolve stub and then for each PLT entry. */
576 #define PLTRESOLVE_RELOCS_SHLIB 0
577 #define PLTRESOLVE_RELOCS 2
578 #define PLT_NON_JUMP_SLOT_RELOCS 2
580 /* The i386 linker needs to keep track of the number of relocs that it
581 decides to copy as dynamic relocs in check_relocs for each symbol.
582 This is so that it can later discard them if they are found to be
583 unnecessary. We store the information in a field extending the
584 regular ELF linker hash table. */
586 struct elf_i386_dyn_relocs
588 struct elf_i386_dyn_relocs *next;
590 /* The input section of the reloc. */
591 asection *sec;
593 /* Total number of relocs copied for the input section. */
594 bfd_size_type count;
596 /* Number of pc-relative relocs copied for the input section. */
597 bfd_size_type pc_count;
600 /* i386 ELF linker hash entry. */
602 struct elf_i386_link_hash_entry
604 struct elf_link_hash_entry elf;
606 /* Track dynamic relocs copied for this symbol. */
607 struct elf_i386_dyn_relocs *dyn_relocs;
609 #define GOT_UNKNOWN 0
610 #define GOT_NORMAL 1
611 #define GOT_TLS_GD 2
612 #define GOT_TLS_IE 4
613 #define GOT_TLS_IE_POS 5
614 #define GOT_TLS_IE_NEG 6
615 #define GOT_TLS_IE_BOTH 7
616 #define GOT_TLS_GDESC 8
617 #define GOT_TLS_GD_BOTH_P(type) \
618 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
619 #define GOT_TLS_GD_P(type) \
620 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
621 #define GOT_TLS_GDESC_P(type) \
622 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
623 #define GOT_TLS_GD_ANY_P(type) \
624 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
625 unsigned char tls_type;
627 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
628 starting at the end of the jump table. */
629 bfd_vma tlsdesc_got;
632 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
634 struct elf_i386_obj_tdata
636 struct elf_obj_tdata root;
638 /* tls_type for each local got entry. */
639 char *local_got_tls_type;
641 /* GOTPLT entries for TLS descriptors. */
642 bfd_vma *local_tlsdesc_gotent;
645 #define elf_i386_tdata(abfd) \
646 ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
648 #define elf_i386_local_got_tls_type(abfd) \
649 (elf_i386_tdata (abfd)->local_got_tls_type)
651 #define elf_i386_local_tlsdesc_gotent(abfd) \
652 (elf_i386_tdata (abfd)->local_tlsdesc_gotent)
654 static bfd_boolean
655 elf_i386_mkobject (bfd *abfd)
657 if (abfd->tdata.any == NULL)
659 bfd_size_type amt = sizeof (struct elf_i386_obj_tdata);
660 abfd->tdata.any = bfd_zalloc (abfd, amt);
661 if (abfd->tdata.any == NULL)
662 return FALSE;
664 return bfd_elf_mkobject (abfd);
667 /* i386 ELF linker hash table. */
669 struct elf_i386_link_hash_table
671 struct elf_link_hash_table elf;
673 /* Short-cuts to get to dynamic linker sections. */
674 asection *sgot;
675 asection *sgotplt;
676 asection *srelgot;
677 asection *splt;
678 asection *srelplt;
679 asection *sdynbss;
680 asection *srelbss;
682 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks. */
683 asection *srelplt2;
685 /* True if the target system is VxWorks. */
686 int is_vxworks;
688 /* Value used to fill the last word of the first plt entry. */
689 bfd_byte plt0_pad_byte;
691 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. */
692 bfd_vma next_tls_desc_index;
694 union {
695 bfd_signed_vma refcount;
696 bfd_vma offset;
697 } tls_ldm_got;
699 /* The amount of space used by the reserved portion of the sgotplt
700 section, plus whatever space is used by the jump slots. */
701 bfd_vma sgotplt_jump_table_size;
703 /* Small local sym to section mapping cache. */
704 struct sym_sec_cache sym_sec;
707 /* Get the i386 ELF linker hash table from a link_info structure. */
709 #define elf_i386_hash_table(p) \
710 ((struct elf_i386_link_hash_table *) ((p)->hash))
712 #define elf_i386_compute_jump_table_size(htab) \
713 ((htab)->next_tls_desc_index * 4)
715 /* Create an entry in an i386 ELF linker hash table. */
717 static struct bfd_hash_entry *
718 link_hash_newfunc (struct bfd_hash_entry *entry,
719 struct bfd_hash_table *table,
720 const char *string)
722 /* Allocate the structure if it has not already been allocated by a
723 subclass. */
724 if (entry == NULL)
726 entry = bfd_hash_allocate (table,
727 sizeof (struct elf_i386_link_hash_entry));
728 if (entry == NULL)
729 return entry;
732 /* Call the allocation method of the superclass. */
733 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
734 if (entry != NULL)
736 struct elf_i386_link_hash_entry *eh;
738 eh = (struct elf_i386_link_hash_entry *) entry;
739 eh->dyn_relocs = NULL;
740 eh->tls_type = GOT_UNKNOWN;
741 eh->tlsdesc_got = (bfd_vma) -1;
744 return entry;
747 /* Create an i386 ELF linker hash table. */
749 static struct bfd_link_hash_table *
750 elf_i386_link_hash_table_create (bfd *abfd)
752 struct elf_i386_link_hash_table *ret;
753 bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
755 ret = bfd_malloc (amt);
756 if (ret == NULL)
757 return NULL;
759 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
760 sizeof (struct elf_i386_link_hash_entry)))
762 free (ret);
763 return NULL;
766 ret->sgot = NULL;
767 ret->sgotplt = NULL;
768 ret->srelgot = NULL;
769 ret->splt = NULL;
770 ret->srelplt = NULL;
771 ret->sdynbss = NULL;
772 ret->srelbss = NULL;
773 ret->tls_ldm_got.refcount = 0;
774 ret->next_tls_desc_index = 0;
775 ret->sgotplt_jump_table_size = 0;
776 ret->sym_sec.abfd = NULL;
777 ret->is_vxworks = 0;
778 ret->srelplt2 = NULL;
779 ret->plt0_pad_byte = 0xf4;
781 return &ret->elf.root;
784 /* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
785 shortcuts to them in our hash table. */
787 static bfd_boolean
788 create_got_section (bfd *dynobj, struct bfd_link_info *info)
790 struct elf_i386_link_hash_table *htab;
792 if (! _bfd_elf_create_got_section (dynobj, info))
793 return FALSE;
795 htab = elf_i386_hash_table (info);
796 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
797 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
798 if (!htab->sgot || !htab->sgotplt)
799 abort ();
801 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rel.got",
802 (SEC_ALLOC | SEC_LOAD
803 | SEC_HAS_CONTENTS
804 | SEC_IN_MEMORY
805 | SEC_LINKER_CREATED
806 | SEC_READONLY));
807 if (htab->srelgot == NULL
808 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
809 return FALSE;
810 return TRUE;
813 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
814 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
815 hash table. */
817 static bfd_boolean
818 elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
820 struct elf_i386_link_hash_table *htab;
822 htab = elf_i386_hash_table (info);
823 if (!htab->sgot && !create_got_section (dynobj, info))
824 return FALSE;
826 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
827 return FALSE;
829 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
830 htab->srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
831 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
832 if (!info->shared)
833 htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
835 if (!htab->splt || !htab->srelplt || !htab->sdynbss
836 || (!info->shared && !htab->srelbss))
837 abort ();
839 if (htab->is_vxworks
840 && !elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
841 return FALSE;
843 return TRUE;
846 /* Copy the extra info we tack onto an elf_link_hash_entry. */
848 static void
849 elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
850 struct elf_link_hash_entry *dir,
851 struct elf_link_hash_entry *ind)
853 struct elf_i386_link_hash_entry *edir, *eind;
855 edir = (struct elf_i386_link_hash_entry *) dir;
856 eind = (struct elf_i386_link_hash_entry *) ind;
858 if (eind->dyn_relocs != NULL)
860 if (edir->dyn_relocs != NULL)
862 struct elf_i386_dyn_relocs **pp;
863 struct elf_i386_dyn_relocs *p;
865 /* Add reloc counts against the indirect sym to the direct sym
866 list. Merge any entries against the same section. */
867 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
869 struct elf_i386_dyn_relocs *q;
871 for (q = edir->dyn_relocs; q != NULL; q = q->next)
872 if (q->sec == p->sec)
874 q->pc_count += p->pc_count;
875 q->count += p->count;
876 *pp = p->next;
877 break;
879 if (q == NULL)
880 pp = &p->next;
882 *pp = edir->dyn_relocs;
885 edir->dyn_relocs = eind->dyn_relocs;
886 eind->dyn_relocs = NULL;
889 if (ind->root.type == bfd_link_hash_indirect
890 && dir->got.refcount <= 0)
892 edir->tls_type = eind->tls_type;
893 eind->tls_type = GOT_UNKNOWN;
896 if (ELIMINATE_COPY_RELOCS
897 && ind->root.type != bfd_link_hash_indirect
898 && dir->dynamic_adjusted)
900 /* If called to transfer flags for a weakdef during processing
901 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
902 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
903 dir->ref_dynamic |= ind->ref_dynamic;
904 dir->ref_regular |= ind->ref_regular;
905 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
906 dir->needs_plt |= ind->needs_plt;
907 dir->pointer_equality_needed |= ind->pointer_equality_needed;
909 else
910 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
913 static int
914 elf_i386_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
916 if (info->shared)
917 return r_type;
919 switch (r_type)
921 case R_386_TLS_GD:
922 case R_386_TLS_GOTDESC:
923 case R_386_TLS_DESC_CALL:
924 case R_386_TLS_IE_32:
925 if (is_local)
926 return R_386_TLS_LE_32;
927 return R_386_TLS_IE_32;
928 case R_386_TLS_IE:
929 case R_386_TLS_GOTIE:
930 if (is_local)
931 return R_386_TLS_LE_32;
932 return r_type;
933 case R_386_TLS_LDM:
934 return R_386_TLS_LE_32;
937 return r_type;
940 /* Look through the relocs for a section during the first phase, and
941 calculate needed space in the global offset table, procedure linkage
942 table, and dynamic reloc sections. */
944 static bfd_boolean
945 elf_i386_check_relocs (bfd *abfd,
946 struct bfd_link_info *info,
947 asection *sec,
948 const Elf_Internal_Rela *relocs)
950 struct elf_i386_link_hash_table *htab;
951 Elf_Internal_Shdr *symtab_hdr;
952 struct elf_link_hash_entry **sym_hashes;
953 const Elf_Internal_Rela *rel;
954 const Elf_Internal_Rela *rel_end;
955 asection *sreloc;
957 if (info->relocatable)
958 return TRUE;
960 htab = elf_i386_hash_table (info);
961 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
962 sym_hashes = elf_sym_hashes (abfd);
964 sreloc = NULL;
966 rel_end = relocs + sec->reloc_count;
967 for (rel = relocs; rel < rel_end; rel++)
969 unsigned int r_type;
970 unsigned long r_symndx;
971 struct elf_link_hash_entry *h;
973 r_symndx = ELF32_R_SYM (rel->r_info);
974 r_type = ELF32_R_TYPE (rel->r_info);
976 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
978 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
979 abfd,
980 r_symndx);
981 return FALSE;
984 if (r_symndx < symtab_hdr->sh_info)
985 h = NULL;
986 else
988 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
989 while (h->root.type == bfd_link_hash_indirect
990 || h->root.type == bfd_link_hash_warning)
991 h = (struct elf_link_hash_entry *) h->root.u.i.link;
994 r_type = elf_i386_tls_transition (info, r_type, h == NULL);
996 switch (r_type)
998 case R_386_TLS_LDM:
999 htab->tls_ldm_got.refcount += 1;
1000 goto create_got;
1002 case R_386_PLT32:
1003 /* This symbol requires a procedure linkage table entry. We
1004 actually build the entry in adjust_dynamic_symbol,
1005 because this might be a case of linking PIC code which is
1006 never referenced by a dynamic object, in which case we
1007 don't need to generate a procedure linkage table entry
1008 after all. */
1010 /* If this is a local symbol, we resolve it directly without
1011 creating a procedure linkage table entry. */
1012 if (h == NULL)
1013 continue;
1015 h->needs_plt = 1;
1016 h->plt.refcount += 1;
1017 break;
1019 case R_386_TLS_IE_32:
1020 case R_386_TLS_IE:
1021 case R_386_TLS_GOTIE:
1022 if (info->shared)
1023 info->flags |= DF_STATIC_TLS;
1024 /* Fall through */
1026 case R_386_GOT32:
1027 case R_386_TLS_GD:
1028 case R_386_TLS_GOTDESC:
1029 case R_386_TLS_DESC_CALL:
1030 /* This symbol requires a global offset table entry. */
1032 int tls_type, old_tls_type;
1034 switch (r_type)
1036 default:
1037 case R_386_GOT32: tls_type = GOT_NORMAL; break;
1038 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1039 case R_386_TLS_GOTDESC:
1040 case R_386_TLS_DESC_CALL:
1041 tls_type = GOT_TLS_GDESC; break;
1042 case R_386_TLS_IE_32:
1043 if (ELF32_R_TYPE (rel->r_info) == r_type)
1044 tls_type = GOT_TLS_IE_NEG;
1045 else
1046 /* If this is a GD->IE transition, we may use either of
1047 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
1048 tls_type = GOT_TLS_IE;
1049 break;
1050 case R_386_TLS_IE:
1051 case R_386_TLS_GOTIE:
1052 tls_type = GOT_TLS_IE_POS; break;
1055 if (h != NULL)
1057 h->got.refcount += 1;
1058 old_tls_type = elf_i386_hash_entry(h)->tls_type;
1060 else
1062 bfd_signed_vma *local_got_refcounts;
1064 /* This is a global offset table entry for a local symbol. */
1065 local_got_refcounts = elf_local_got_refcounts (abfd);
1066 if (local_got_refcounts == NULL)
1068 bfd_size_type size;
1070 size = symtab_hdr->sh_info;
1071 size *= (sizeof (bfd_signed_vma)
1072 + sizeof (bfd_vma) + sizeof(char));
1073 local_got_refcounts = bfd_zalloc (abfd, size);
1074 if (local_got_refcounts == NULL)
1075 return FALSE;
1076 elf_local_got_refcounts (abfd) = local_got_refcounts;
1077 elf_i386_local_tlsdesc_gotent (abfd)
1078 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1079 elf_i386_local_got_tls_type (abfd)
1080 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1082 local_got_refcounts[r_symndx] += 1;
1083 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
1086 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1087 tls_type |= old_tls_type;
1088 /* If a TLS symbol is accessed using IE at least once,
1089 there is no point to use dynamic model for it. */
1090 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1091 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1092 || (tls_type & GOT_TLS_IE) == 0))
1094 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1095 tls_type = old_tls_type;
1096 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1097 && GOT_TLS_GD_ANY_P (tls_type))
1098 tls_type |= old_tls_type;
1099 else
1101 (*_bfd_error_handler)
1102 (_("%B: `%s' accessed both as normal and "
1103 "thread local symbol"),
1104 abfd,
1105 h ? h->root.root.string : "<local>");
1106 return FALSE;
1110 if (old_tls_type != tls_type)
1112 if (h != NULL)
1113 elf_i386_hash_entry (h)->tls_type = tls_type;
1114 else
1115 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
1118 /* Fall through */
1120 case R_386_GOTOFF:
1121 case R_386_GOTPC:
1122 create_got:
1123 if (htab->sgot == NULL)
1125 if (htab->elf.dynobj == NULL)
1126 htab->elf.dynobj = abfd;
1127 if (!create_got_section (htab->elf.dynobj, info))
1128 return FALSE;
1130 if (r_type != R_386_TLS_IE)
1131 break;
1132 /* Fall through */
1134 case R_386_TLS_LE_32:
1135 case R_386_TLS_LE:
1136 if (!info->shared)
1137 break;
1138 info->flags |= DF_STATIC_TLS;
1139 /* Fall through */
1141 case R_386_32:
1142 case R_386_PC32:
1143 if (h != NULL && !info->shared)
1145 /* If this reloc is in a read-only section, we might
1146 need a copy reloc. We can't check reliably at this
1147 stage whether the section is read-only, as input
1148 sections have not yet been mapped to output sections.
1149 Tentatively set the flag for now, and correct in
1150 adjust_dynamic_symbol. */
1151 h->non_got_ref = 1;
1153 /* We may need a .plt entry if the function this reloc
1154 refers to is in a shared lib. */
1155 h->plt.refcount += 1;
1156 if (r_type != R_386_PC32)
1157 h->pointer_equality_needed = 1;
1160 /* If we are creating a shared library, and this is a reloc
1161 against a global symbol, or a non PC relative reloc
1162 against a local symbol, then we need to copy the reloc
1163 into the shared library. However, if we are linking with
1164 -Bsymbolic, we do not need to copy a reloc against a
1165 global symbol which is defined in an object we are
1166 including in the link (i.e., DEF_REGULAR is set). At
1167 this point we have not seen all the input files, so it is
1168 possible that DEF_REGULAR is not set now but will be set
1169 later (it is never cleared). In case of a weak definition,
1170 DEF_REGULAR may be cleared later by a strong definition in
1171 a shared library. We account for that possibility below by
1172 storing information in the relocs_copied field of the hash
1173 table entry. A similar situation occurs when creating
1174 shared libraries and symbol visibility changes render the
1175 symbol local.
1177 If on the other hand, we are creating an executable, we
1178 may need to keep relocations for symbols satisfied by a
1179 dynamic library if we manage to avoid copy relocs for the
1180 symbol. */
1181 if ((info->shared
1182 && (sec->flags & SEC_ALLOC) != 0
1183 && (r_type != R_386_PC32
1184 || (h != NULL
1185 && (! SYMBOLIC_BIND (info, h)
1186 || h->root.type == bfd_link_hash_defweak
1187 || !h->def_regular))))
1188 || (ELIMINATE_COPY_RELOCS
1189 && !info->shared
1190 && (sec->flags & SEC_ALLOC) != 0
1191 && h != NULL
1192 && (h->root.type == bfd_link_hash_defweak
1193 || !h->def_regular)))
1195 struct elf_i386_dyn_relocs *p;
1196 struct elf_i386_dyn_relocs **head;
1198 /* We must copy these reloc types into the output file.
1199 Create a reloc section in dynobj and make room for
1200 this reloc. */
1201 if (sreloc == NULL)
1203 const char *name;
1204 bfd *dynobj;
1205 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
1206 unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
1208 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
1209 if (name == NULL)
1210 return FALSE;
1212 if (! CONST_STRNEQ (name, ".rel")
1213 || strcmp (bfd_get_section_name (abfd, sec),
1214 name + 4) != 0)
1216 (*_bfd_error_handler)
1217 (_("%B: bad relocation section name `%s\'"),
1218 abfd, name);
1221 if (htab->elf.dynobj == NULL)
1222 htab->elf.dynobj = abfd;
1224 dynobj = htab->elf.dynobj;
1225 sreloc = bfd_get_section_by_name (dynobj, name);
1226 if (sreloc == NULL)
1228 flagword flags;
1230 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1231 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1232 if ((sec->flags & SEC_ALLOC) != 0)
1233 flags |= SEC_ALLOC | SEC_LOAD;
1234 sreloc = bfd_make_section_with_flags (dynobj,
1235 name,
1236 flags);
1237 if (sreloc == NULL
1238 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
1239 return FALSE;
1241 elf_section_data (sec)->sreloc = sreloc;
1244 /* If this is a global symbol, we count the number of
1245 relocations we need for this symbol. */
1246 if (h != NULL)
1248 head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
1250 else
1252 void **vpp;
1253 /* Track dynamic relocs needed for local syms too.
1254 We really need local syms available to do this
1255 easily. Oh well. */
1257 asection *s;
1258 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1259 sec, r_symndx);
1260 if (s == NULL)
1261 return FALSE;
1263 vpp = &elf_section_data (s)->local_dynrel;
1264 head = (struct elf_i386_dyn_relocs **)vpp;
1267 p = *head;
1268 if (p == NULL || p->sec != sec)
1270 bfd_size_type amt = sizeof *p;
1271 p = bfd_alloc (htab->elf.dynobj, amt);
1272 if (p == NULL)
1273 return FALSE;
1274 p->next = *head;
1275 *head = p;
1276 p->sec = sec;
1277 p->count = 0;
1278 p->pc_count = 0;
1281 p->count += 1;
1282 if (r_type == R_386_PC32)
1283 p->pc_count += 1;
1285 break;
1287 /* This relocation describes the C++ object vtable hierarchy.
1288 Reconstruct it for later use during GC. */
1289 case R_386_GNU_VTINHERIT:
1290 BFD_ASSERT (h != NULL);
1291 if (h != NULL
1292 && !bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1293 return FALSE;
1294 break;
1296 /* This relocation describes which C++ vtable entries are actually
1297 used. Record for later use during GC. */
1298 case R_386_GNU_VTENTRY:
1299 BFD_ASSERT (h != NULL);
1300 if (h != NULL
1301 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1302 return FALSE;
1303 break;
1305 default:
1306 break;
1310 return TRUE;
1313 /* Return the section that should be marked against GC for a given
1314 relocation. */
1316 static asection *
1317 elf_i386_gc_mark_hook (asection *sec,
1318 struct bfd_link_info *info,
1319 Elf_Internal_Rela *rel,
1320 struct elf_link_hash_entry *h,
1321 Elf_Internal_Sym *sym)
1323 if (h != NULL)
1324 switch (ELF32_R_TYPE (rel->r_info))
1326 case R_386_GNU_VTINHERIT:
1327 case R_386_GNU_VTENTRY:
1328 return NULL;
1331 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1334 /* Update the got entry reference counts for the section being removed. */
1336 static bfd_boolean
1337 elf_i386_gc_sweep_hook (bfd *abfd,
1338 struct bfd_link_info *info,
1339 asection *sec,
1340 const Elf_Internal_Rela *relocs)
1342 Elf_Internal_Shdr *symtab_hdr;
1343 struct elf_link_hash_entry **sym_hashes;
1344 bfd_signed_vma *local_got_refcounts;
1345 const Elf_Internal_Rela *rel, *relend;
1347 elf_section_data (sec)->local_dynrel = NULL;
1349 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1350 sym_hashes = elf_sym_hashes (abfd);
1351 local_got_refcounts = elf_local_got_refcounts (abfd);
1353 relend = relocs + sec->reloc_count;
1354 for (rel = relocs; rel < relend; rel++)
1356 unsigned long r_symndx;
1357 unsigned int r_type;
1358 struct elf_link_hash_entry *h = NULL;
1360 r_symndx = ELF32_R_SYM (rel->r_info);
1361 if (r_symndx >= symtab_hdr->sh_info)
1363 struct elf_i386_link_hash_entry *eh;
1364 struct elf_i386_dyn_relocs **pp;
1365 struct elf_i386_dyn_relocs *p;
1367 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1368 while (h->root.type == bfd_link_hash_indirect
1369 || h->root.type == bfd_link_hash_warning)
1370 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1371 eh = (struct elf_i386_link_hash_entry *) h;
1373 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1374 if (p->sec == sec)
1376 /* Everything must go for SEC. */
1377 *pp = p->next;
1378 break;
1382 r_type = ELF32_R_TYPE (rel->r_info);
1383 r_type = elf_i386_tls_transition (info, r_type, h != NULL);
1384 switch (r_type)
1386 case R_386_TLS_LDM:
1387 if (elf_i386_hash_table (info)->tls_ldm_got.refcount > 0)
1388 elf_i386_hash_table (info)->tls_ldm_got.refcount -= 1;
1389 break;
1391 case R_386_TLS_GD:
1392 case R_386_TLS_GOTDESC:
1393 case R_386_TLS_DESC_CALL:
1394 case R_386_TLS_IE_32:
1395 case R_386_TLS_IE:
1396 case R_386_TLS_GOTIE:
1397 case R_386_GOT32:
1398 if (h != NULL)
1400 if (h->got.refcount > 0)
1401 h->got.refcount -= 1;
1403 else if (local_got_refcounts != NULL)
1405 if (local_got_refcounts[r_symndx] > 0)
1406 local_got_refcounts[r_symndx] -= 1;
1408 break;
1410 case R_386_32:
1411 case R_386_PC32:
1412 if (info->shared)
1413 break;
1414 /* Fall through */
1416 case R_386_PLT32:
1417 if (h != NULL)
1419 if (h->plt.refcount > 0)
1420 h->plt.refcount -= 1;
1422 break;
1424 default:
1425 break;
1429 return TRUE;
1432 /* Adjust a symbol defined by a dynamic object and referenced by a
1433 regular object. The current definition is in some section of the
1434 dynamic object, but we're not including those sections. We have to
1435 change the definition to something the rest of the link can
1436 understand. */
1438 static bfd_boolean
1439 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
1440 struct elf_link_hash_entry *h)
1442 struct elf_i386_link_hash_table *htab;
1443 asection *s;
1445 /* If this is a function, put it in the procedure linkage table. We
1446 will fill in the contents of the procedure linkage table later,
1447 when we know the address of the .got section. */
1448 if (h->type == STT_FUNC
1449 || h->needs_plt)
1451 if (h->plt.refcount <= 0
1452 || SYMBOL_CALLS_LOCAL (info, h)
1453 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1454 && h->root.type == bfd_link_hash_undefweak))
1456 /* This case can occur if we saw a PLT32 reloc in an input
1457 file, but the symbol was never referred to by a dynamic
1458 object, or if all references were garbage collected. In
1459 such a case, we don't actually need to build a procedure
1460 linkage table, and we can just do a PC32 reloc instead. */
1461 h->plt.offset = (bfd_vma) -1;
1462 h->needs_plt = 0;
1465 return TRUE;
1467 else
1468 /* It's possible that we incorrectly decided a .plt reloc was
1469 needed for an R_386_PC32 reloc to a non-function sym in
1470 check_relocs. We can't decide accurately between function and
1471 non-function syms in check-relocs; Objects loaded later in
1472 the link may change h->type. So fix it now. */
1473 h->plt.offset = (bfd_vma) -1;
1475 /* If this is a weak symbol, and there is a real definition, the
1476 processor independent code will have arranged for us to see the
1477 real definition first, and we can just use the same value. */
1478 if (h->u.weakdef != NULL)
1480 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1481 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1482 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1483 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1484 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1485 h->non_got_ref = h->u.weakdef->non_got_ref;
1486 return TRUE;
1489 /* This is a reference to a symbol defined by a dynamic object which
1490 is not a function. */
1492 /* If we are creating a shared library, we must presume that the
1493 only references to the symbol are via the global offset table.
1494 For such cases we need not do anything here; the relocations will
1495 be handled correctly by relocate_section. */
1496 if (info->shared)
1497 return TRUE;
1499 /* If there are no references to this symbol that do not use the
1500 GOT, we don't need to generate a copy reloc. */
1501 if (!h->non_got_ref)
1502 return TRUE;
1504 /* If -z nocopyreloc was given, we won't generate them either. */
1505 if (info->nocopyreloc)
1507 h->non_got_ref = 0;
1508 return TRUE;
1511 htab = elf_i386_hash_table (info);
1513 /* If there aren't any dynamic relocs in read-only sections, then
1514 we can keep the dynamic relocs and avoid the copy reloc. This
1515 doesn't work on VxWorks, where we can not have dynamic relocations
1516 (other than copy and jump slot relocations) in an executable. */
1517 if (ELIMINATE_COPY_RELOCS && !htab->is_vxworks)
1519 struct elf_i386_link_hash_entry * eh;
1520 struct elf_i386_dyn_relocs *p;
1522 eh = (struct elf_i386_link_hash_entry *) h;
1523 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1525 s = p->sec->output_section;
1526 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1527 break;
1530 if (p == NULL)
1532 h->non_got_ref = 0;
1533 return TRUE;
1537 if (h->size == 0)
1539 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1540 h->root.root.string);
1541 return TRUE;
1544 /* We must allocate the symbol in our .dynbss section, which will
1545 become part of the .bss section of the executable. There will be
1546 an entry for this symbol in the .dynsym section. The dynamic
1547 object will contain position independent code, so all references
1548 from the dynamic object to this symbol will go through the global
1549 offset table. The dynamic linker will use the .dynsym entry to
1550 determine the address it must put in the global offset table, so
1551 both the dynamic object and the regular object will refer to the
1552 same memory location for the variable. */
1554 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1555 copy the initial value out of the dynamic object and into the
1556 runtime process image. */
1557 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1559 htab->srelbss->size += sizeof (Elf32_External_Rel);
1560 h->needs_copy = 1;
1563 s = htab->sdynbss;
1565 return _bfd_elf_adjust_dynamic_copy (h, s);
1568 /* Allocate space in .plt, .got and associated reloc sections for
1569 dynamic relocs. */
1571 static bfd_boolean
1572 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1574 struct bfd_link_info *info;
1575 struct elf_i386_link_hash_table *htab;
1576 struct elf_i386_link_hash_entry *eh;
1577 struct elf_i386_dyn_relocs *p;
1579 if (h->root.type == bfd_link_hash_indirect)
1580 return TRUE;
1582 if (h->root.type == bfd_link_hash_warning)
1583 /* When warning symbols are created, they **replace** the "real"
1584 entry in the hash table, thus we never get to see the real
1585 symbol in a hash traversal. So look at it now. */
1586 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1588 info = (struct bfd_link_info *) inf;
1589 htab = elf_i386_hash_table (info);
1591 if (htab->elf.dynamic_sections_created
1592 && h->plt.refcount > 0)
1594 /* Make sure this symbol is output as a dynamic symbol.
1595 Undefined weak syms won't yet be marked as dynamic. */
1596 if (h->dynindx == -1
1597 && !h->forced_local)
1599 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1600 return FALSE;
1603 if (info->shared
1604 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1606 asection *s = htab->splt;
1608 /* If this is the first .plt entry, make room for the special
1609 first entry. */
1610 if (s->size == 0)
1611 s->size += PLT_ENTRY_SIZE;
1613 h->plt.offset = s->size;
1615 /* If this symbol is not defined in a regular file, and we are
1616 not generating a shared library, then set the symbol to this
1617 location in the .plt. This is required to make function
1618 pointers compare as equal between the normal executable and
1619 the shared library. */
1620 if (! info->shared
1621 && !h->def_regular)
1623 h->root.u.def.section = s;
1624 h->root.u.def.value = h->plt.offset;
1627 /* Make room for this entry. */
1628 s->size += PLT_ENTRY_SIZE;
1630 /* We also need to make an entry in the .got.plt section, which
1631 will be placed in the .got section by the linker script. */
1632 htab->sgotplt->size += 4;
1634 /* We also need to make an entry in the .rel.plt section. */
1635 htab->srelplt->size += sizeof (Elf32_External_Rel);
1636 htab->next_tls_desc_index++;
1638 if (htab->is_vxworks && !info->shared)
1640 /* VxWorks has a second set of relocations for each PLT entry
1641 in executables. They go in a separate relocation section,
1642 which is processed by the kernel loader. */
1644 /* There are two relocations for the initial PLT entry: an
1645 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
1646 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
1648 if (h->plt.offset == PLT_ENTRY_SIZE)
1649 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1651 /* There are two extra relocations for each subsequent PLT entry:
1652 an R_386_32 relocation for the GOT entry, and an R_386_32
1653 relocation for the PLT entry. */
1655 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1658 else
1660 h->plt.offset = (bfd_vma) -1;
1661 h->needs_plt = 0;
1664 else
1666 h->plt.offset = (bfd_vma) -1;
1667 h->needs_plt = 0;
1670 eh = (struct elf_i386_link_hash_entry *) h;
1671 eh->tlsdesc_got = (bfd_vma) -1;
1673 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
1674 make it a R_386_TLS_LE_32 requiring no TLS entry. */
1675 if (h->got.refcount > 0
1676 && !info->shared
1677 && h->dynindx == -1
1678 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
1679 h->got.offset = (bfd_vma) -1;
1680 else if (h->got.refcount > 0)
1682 asection *s;
1683 bfd_boolean dyn;
1684 int tls_type = elf_i386_hash_entry(h)->tls_type;
1686 /* Make sure this symbol is output as a dynamic symbol.
1687 Undefined weak syms won't yet be marked as dynamic. */
1688 if (h->dynindx == -1
1689 && !h->forced_local)
1691 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1692 return FALSE;
1695 s = htab->sgot;
1696 if (GOT_TLS_GDESC_P (tls_type))
1698 eh->tlsdesc_got = htab->sgotplt->size
1699 - elf_i386_compute_jump_table_size (htab);
1700 htab->sgotplt->size += 8;
1701 h->got.offset = (bfd_vma) -2;
1703 if (! GOT_TLS_GDESC_P (tls_type)
1704 || GOT_TLS_GD_P (tls_type))
1706 h->got.offset = s->size;
1707 s->size += 4;
1708 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
1709 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
1710 s->size += 4;
1712 dyn = htab->elf.dynamic_sections_created;
1713 /* R_386_TLS_IE_32 needs one dynamic relocation,
1714 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
1715 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
1716 need two), R_386_TLS_GD needs one if local symbol and two if
1717 global. */
1718 if (tls_type == GOT_TLS_IE_BOTH)
1719 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1720 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
1721 || (tls_type & GOT_TLS_IE))
1722 htab->srelgot->size += sizeof (Elf32_External_Rel);
1723 else if (GOT_TLS_GD_P (tls_type))
1724 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1725 else if (! GOT_TLS_GDESC_P (tls_type)
1726 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1727 || h->root.type != bfd_link_hash_undefweak)
1728 && (info->shared
1729 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1730 htab->srelgot->size += sizeof (Elf32_External_Rel);
1731 if (GOT_TLS_GDESC_P (tls_type))
1732 htab->srelplt->size += sizeof (Elf32_External_Rel);
1734 else
1735 h->got.offset = (bfd_vma) -1;
1737 if (eh->dyn_relocs == NULL)
1738 return TRUE;
1740 /* In the shared -Bsymbolic case, discard space allocated for
1741 dynamic pc-relative relocs against symbols which turn out to be
1742 defined in regular objects. For the normal shared case, discard
1743 space for pc-relative relocs that have become local due to symbol
1744 visibility changes. */
1746 if (info->shared)
1748 /* The only reloc that uses pc_count is R_386_PC32, which will
1749 appear on a call or on something like ".long foo - .". We
1750 want calls to protected symbols to resolve directly to the
1751 function rather than going via the plt. If people want
1752 function pointer comparisons to work as expected then they
1753 should avoid writing assembly like ".long foo - .". */
1754 if (SYMBOL_CALLS_LOCAL (info, h))
1756 struct elf_i386_dyn_relocs **pp;
1758 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1760 p->count -= p->pc_count;
1761 p->pc_count = 0;
1762 if (p->count == 0)
1763 *pp = p->next;
1764 else
1765 pp = &p->next;
1769 /* Also discard relocs on undefined weak syms with non-default
1770 visibility. */
1771 if (eh->dyn_relocs != NULL
1772 && h->root.type == bfd_link_hash_undefweak)
1774 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1775 eh->dyn_relocs = NULL;
1777 /* Make sure undefined weak symbols are output as a dynamic
1778 symbol in PIEs. */
1779 else if (h->dynindx == -1
1780 && !h->forced_local)
1782 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1783 return FALSE;
1787 else if (ELIMINATE_COPY_RELOCS)
1789 /* For the non-shared case, discard space for relocs against
1790 symbols which turn out to need copy relocs or are not
1791 dynamic. */
1793 if (!h->non_got_ref
1794 && ((h->def_dynamic
1795 && !h->def_regular)
1796 || (htab->elf.dynamic_sections_created
1797 && (h->root.type == bfd_link_hash_undefweak
1798 || h->root.type == bfd_link_hash_undefined))))
1800 /* Make sure this symbol is output as a dynamic symbol.
1801 Undefined weak syms won't yet be marked as dynamic. */
1802 if (h->dynindx == -1
1803 && !h->forced_local)
1805 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1806 return FALSE;
1809 /* If that succeeded, we know we'll be keeping all the
1810 relocs. */
1811 if (h->dynindx != -1)
1812 goto keep;
1815 eh->dyn_relocs = NULL;
1817 keep: ;
1820 /* Finally, allocate space. */
1821 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1823 asection *sreloc = elf_section_data (p->sec)->sreloc;
1824 sreloc->size += p->count * sizeof (Elf32_External_Rel);
1827 return TRUE;
1830 /* Find any dynamic relocs that apply to read-only sections. */
1832 static bfd_boolean
1833 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1835 struct elf_i386_link_hash_entry *eh;
1836 struct elf_i386_dyn_relocs *p;
1838 if (h->root.type == bfd_link_hash_warning)
1839 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1841 eh = (struct elf_i386_link_hash_entry *) h;
1842 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1844 asection *s = p->sec->output_section;
1846 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1848 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1850 info->flags |= DF_TEXTREL;
1852 /* Not an error, just cut short the traversal. */
1853 return FALSE;
1856 return TRUE;
1859 /* Set the sizes of the dynamic sections. */
1861 static bfd_boolean
1862 elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1863 struct bfd_link_info *info)
1865 struct elf_i386_link_hash_table *htab;
1866 bfd *dynobj;
1867 asection *s;
1868 bfd_boolean relocs;
1869 bfd *ibfd;
1871 htab = elf_i386_hash_table (info);
1872 dynobj = htab->elf.dynobj;
1873 if (dynobj == NULL)
1874 abort ();
1876 if (htab->elf.dynamic_sections_created)
1878 /* Set the contents of the .interp section to the interpreter. */
1879 if (info->executable)
1881 s = bfd_get_section_by_name (dynobj, ".interp");
1882 if (s == NULL)
1883 abort ();
1884 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1885 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1889 /* Set up .got offsets for local syms, and space for local dynamic
1890 relocs. */
1891 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1893 bfd_signed_vma *local_got;
1894 bfd_signed_vma *end_local_got;
1895 char *local_tls_type;
1896 bfd_vma *local_tlsdesc_gotent;
1897 bfd_size_type locsymcount;
1898 Elf_Internal_Shdr *symtab_hdr;
1899 asection *srel;
1901 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1902 continue;
1904 for (s = ibfd->sections; s != NULL; s = s->next)
1906 struct elf_i386_dyn_relocs *p;
1908 for (p = ((struct elf_i386_dyn_relocs *)
1909 elf_section_data (s)->local_dynrel);
1910 p != NULL;
1911 p = p->next)
1913 if (!bfd_is_abs_section (p->sec)
1914 && bfd_is_abs_section (p->sec->output_section))
1916 /* Input section has been discarded, either because
1917 it is a copy of a linkonce section or due to
1918 linker script /DISCARD/, so we'll be discarding
1919 the relocs too. */
1921 else if (p->count != 0)
1923 srel = elf_section_data (p->sec)->sreloc;
1924 srel->size += p->count * sizeof (Elf32_External_Rel);
1925 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1926 info->flags |= DF_TEXTREL;
1931 local_got = elf_local_got_refcounts (ibfd);
1932 if (!local_got)
1933 continue;
1935 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1936 locsymcount = symtab_hdr->sh_info;
1937 end_local_got = local_got + locsymcount;
1938 local_tls_type = elf_i386_local_got_tls_type (ibfd);
1939 local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
1940 s = htab->sgot;
1941 srel = htab->srelgot;
1942 for (; local_got < end_local_got;
1943 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
1945 *local_tlsdesc_gotent = (bfd_vma) -1;
1946 if (*local_got > 0)
1948 if (GOT_TLS_GDESC_P (*local_tls_type))
1950 *local_tlsdesc_gotent = htab->sgotplt->size
1951 - elf_i386_compute_jump_table_size (htab);
1952 htab->sgotplt->size += 8;
1953 *local_got = (bfd_vma) -2;
1955 if (! GOT_TLS_GDESC_P (*local_tls_type)
1956 || GOT_TLS_GD_P (*local_tls_type))
1958 *local_got = s->size;
1959 s->size += 4;
1960 if (GOT_TLS_GD_P (*local_tls_type)
1961 || *local_tls_type == GOT_TLS_IE_BOTH)
1962 s->size += 4;
1964 if (info->shared
1965 || GOT_TLS_GD_ANY_P (*local_tls_type)
1966 || (*local_tls_type & GOT_TLS_IE))
1968 if (*local_tls_type == GOT_TLS_IE_BOTH)
1969 srel->size += 2 * sizeof (Elf32_External_Rel);
1970 else if (GOT_TLS_GD_P (*local_tls_type)
1971 || ! GOT_TLS_GDESC_P (*local_tls_type))
1972 srel->size += sizeof (Elf32_External_Rel);
1973 if (GOT_TLS_GDESC_P (*local_tls_type))
1974 htab->srelplt->size += sizeof (Elf32_External_Rel);
1977 else
1978 *local_got = (bfd_vma) -1;
1982 if (htab->tls_ldm_got.refcount > 0)
1984 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
1985 relocs. */
1986 htab->tls_ldm_got.offset = htab->sgot->size;
1987 htab->sgot->size += 8;
1988 htab->srelgot->size += sizeof (Elf32_External_Rel);
1990 else
1991 htab->tls_ldm_got.offset = -1;
1993 /* Allocate global sym .plt and .got entries, and space for global
1994 sym dynamic relocs. */
1995 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1997 /* For every jump slot reserved in the sgotplt, reloc_count is
1998 incremented. However, when we reserve space for TLS descriptors,
1999 it's not incremented, so in order to compute the space reserved
2000 for them, it suffices to multiply the reloc count by the jump
2001 slot size. */
2002 if (htab->srelplt)
2003 htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
2005 /* We now have determined the sizes of the various dynamic sections.
2006 Allocate memory for them. */
2007 relocs = FALSE;
2008 for (s = dynobj->sections; s != NULL; s = s->next)
2010 bfd_boolean strip_section = TRUE;
2012 if ((s->flags & SEC_LINKER_CREATED) == 0)
2013 continue;
2015 if (s == htab->splt
2016 || s == htab->sgot
2017 || s == htab->sgotplt
2018 || s == htab->sdynbss)
2020 /* Strip this section if we don't need it; see the
2021 comment below. */
2022 /* We'd like to strip these sections if they aren't needed, but if
2023 we've exported dynamic symbols from them we must leave them.
2024 It's too late to tell BFD to get rid of the symbols. */
2026 if (htab->elf.hplt != NULL)
2027 strip_section = FALSE;
2029 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
2031 if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
2032 relocs = TRUE;
2034 /* We use the reloc_count field as a counter if we need
2035 to copy relocs into the output file. */
2036 s->reloc_count = 0;
2038 else
2040 /* It's not one of our sections, so don't allocate space. */
2041 continue;
2044 if (s->size == 0)
2046 /* If we don't need this section, strip it from the
2047 output file. This is mostly to handle .rel.bss and
2048 .rel.plt. We must create both sections in
2049 create_dynamic_sections, because they must be created
2050 before the linker maps input sections to output
2051 sections. The linker does that before
2052 adjust_dynamic_symbol is called, and it is that
2053 function which decides whether anything needs to go
2054 into these sections. */
2055 if (strip_section)
2056 s->flags |= SEC_EXCLUDE;
2057 continue;
2060 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2061 continue;
2063 /* Allocate memory for the section contents. We use bfd_zalloc
2064 here in case unused entries are not reclaimed before the
2065 section's contents are written out. This should not happen,
2066 but this way if it does, we get a R_386_NONE reloc instead
2067 of garbage. */
2068 s->contents = bfd_zalloc (dynobj, s->size);
2069 if (s->contents == NULL)
2070 return FALSE;
2073 if (htab->elf.dynamic_sections_created)
2075 /* Add some entries to the .dynamic section. We fill in the
2076 values later, in elf_i386_finish_dynamic_sections, but we
2077 must add the entries now so that we get the correct size for
2078 the .dynamic section. The DT_DEBUG entry is filled in by the
2079 dynamic linker and used by the debugger. */
2080 #define add_dynamic_entry(TAG, VAL) \
2081 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2083 if (info->executable)
2085 if (!add_dynamic_entry (DT_DEBUG, 0))
2086 return FALSE;
2089 if (htab->splt->size != 0)
2091 if (!add_dynamic_entry (DT_PLTGOT, 0)
2092 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2093 || !add_dynamic_entry (DT_PLTREL, DT_REL)
2094 || !add_dynamic_entry (DT_JMPREL, 0))
2095 return FALSE;
2098 if (relocs)
2100 if (!add_dynamic_entry (DT_REL, 0)
2101 || !add_dynamic_entry (DT_RELSZ, 0)
2102 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
2103 return FALSE;
2105 /* If any dynamic relocs apply to a read-only section,
2106 then we need a DT_TEXTREL entry. */
2107 if ((info->flags & DF_TEXTREL) == 0)
2108 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2109 (PTR) info);
2111 if ((info->flags & DF_TEXTREL) != 0)
2113 if (!add_dynamic_entry (DT_TEXTREL, 0))
2114 return FALSE;
2118 #undef add_dynamic_entry
2120 return TRUE;
2123 static bfd_boolean
2124 elf_i386_always_size_sections (bfd *output_bfd,
2125 struct bfd_link_info *info)
2127 asection *tls_sec = elf_hash_table (info)->tls_sec;
2129 if (tls_sec)
2131 struct elf_link_hash_entry *tlsbase;
2133 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2134 "_TLS_MODULE_BASE_",
2135 FALSE, FALSE, FALSE);
2137 if (tlsbase && tlsbase->type == STT_TLS)
2139 struct bfd_link_hash_entry *bh = NULL;
2140 const struct elf_backend_data *bed
2141 = get_elf_backend_data (output_bfd);
2143 if (!(_bfd_generic_link_add_one_symbol
2144 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2145 tls_sec, 0, NULL, FALSE,
2146 bed->collect, &bh)))
2147 return FALSE;
2148 tlsbase = (struct elf_link_hash_entry *)bh;
2149 tlsbase->def_regular = 1;
2150 tlsbase->other = STV_HIDDEN;
2151 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2155 return TRUE;
2158 /* Set the correct type for an x86 ELF section. We do this by the
2159 section name, which is a hack, but ought to work. */
2161 static bfd_boolean
2162 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2163 Elf_Internal_Shdr *hdr,
2164 asection *sec)
2166 register const char *name;
2168 name = bfd_get_section_name (abfd, sec);
2170 /* This is an ugly, but unfortunately necessary hack that is
2171 needed when producing EFI binaries on x86. It tells
2172 elf.c:elf_fake_sections() not to consider ".reloc" as a section
2173 containing ELF relocation info. We need this hack in order to
2174 be able to generate ELF binaries that can be translated into
2175 EFI applications (which are essentially COFF objects). Those
2176 files contain a COFF ".reloc" section inside an ELFNN object,
2177 which would normally cause BFD to segfault because it would
2178 attempt to interpret this section as containing relocation
2179 entries for section "oc". With this hack enabled, ".reloc"
2180 will be treated as a normal data section, which will avoid the
2181 segfault. However, you won't be able to create an ELFNN binary
2182 with a section named "oc" that needs relocations, but that's
2183 the kind of ugly side-effects you get when detecting section
2184 types based on their names... In practice, this limitation is
2185 unlikely to bite. */
2186 if (strcmp (name, ".reloc") == 0)
2187 hdr->sh_type = SHT_PROGBITS;
2189 return TRUE;
2192 /* Return the base VMA address which should be subtracted from real addresses
2193 when resolving @dtpoff relocation.
2194 This is PT_TLS segment p_vaddr. */
2196 static bfd_vma
2197 dtpoff_base (struct bfd_link_info *info)
2199 /* If tls_sec is NULL, we should have signalled an error already. */
2200 if (elf_hash_table (info)->tls_sec == NULL)
2201 return 0;
2202 return elf_hash_table (info)->tls_sec->vma;
2205 /* Return the relocation value for @tpoff relocation
2206 if STT_TLS virtual address is ADDRESS. */
2208 static bfd_vma
2209 tpoff (struct bfd_link_info *info, bfd_vma address)
2211 struct elf_link_hash_table *htab = elf_hash_table (info);
2213 /* If tls_sec is NULL, we should have signalled an error already. */
2214 if (htab->tls_sec == NULL)
2215 return 0;
2216 return htab->tls_size + htab->tls_sec->vma - address;
2219 /* Relocate an i386 ELF section. */
2221 static bfd_boolean
2222 elf_i386_relocate_section (bfd *output_bfd,
2223 struct bfd_link_info *info,
2224 bfd *input_bfd,
2225 asection *input_section,
2226 bfd_byte *contents,
2227 Elf_Internal_Rela *relocs,
2228 Elf_Internal_Sym *local_syms,
2229 asection **local_sections)
2231 struct elf_i386_link_hash_table *htab;
2232 Elf_Internal_Shdr *symtab_hdr;
2233 struct elf_link_hash_entry **sym_hashes;
2234 bfd_vma *local_got_offsets;
2235 bfd_vma *local_tlsdesc_gotents;
2236 Elf_Internal_Rela *rel;
2237 Elf_Internal_Rela *relend;
2239 htab = elf_i386_hash_table (info);
2240 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2241 sym_hashes = elf_sym_hashes (input_bfd);
2242 local_got_offsets = elf_local_got_offsets (input_bfd);
2243 local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
2245 rel = relocs;
2246 relend = relocs + input_section->reloc_count;
2247 for (; rel < relend; rel++)
2249 unsigned int r_type;
2250 reloc_howto_type *howto;
2251 unsigned long r_symndx;
2252 struct elf_link_hash_entry *h;
2253 Elf_Internal_Sym *sym;
2254 asection *sec;
2255 bfd_vma off, offplt;
2256 bfd_vma relocation;
2257 bfd_boolean unresolved_reloc;
2258 bfd_reloc_status_type r;
2259 unsigned int indx;
2260 int tls_type;
2262 r_type = ELF32_R_TYPE (rel->r_info);
2263 if (r_type == R_386_GNU_VTINHERIT
2264 || r_type == R_386_GNU_VTENTRY)
2265 continue;
2267 if ((indx = r_type) >= R_386_standard
2268 && ((indx = r_type - R_386_ext_offset) - R_386_standard
2269 >= R_386_ext - R_386_standard)
2270 && ((indx = r_type - R_386_tls_offset) - R_386_ext
2271 >= R_386_tls - R_386_ext))
2273 (*_bfd_error_handler)
2274 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
2275 input_bfd, input_section, r_type);
2276 bfd_set_error (bfd_error_bad_value);
2277 return FALSE;
2279 howto = elf_howto_table + indx;
2281 r_symndx = ELF32_R_SYM (rel->r_info);
2282 h = NULL;
2283 sym = NULL;
2284 sec = NULL;
2285 unresolved_reloc = FALSE;
2286 if (r_symndx < symtab_hdr->sh_info)
2288 sym = local_syms + r_symndx;
2289 sec = local_sections[r_symndx];
2290 relocation = (sec->output_section->vma
2291 + sec->output_offset
2292 + sym->st_value);
2294 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2295 && ((sec->flags & SEC_MERGE) != 0
2296 || (info->relocatable
2297 && sec->output_offset != 0)))
2299 bfd_vma addend;
2300 bfd_byte *where = contents + rel->r_offset;
2302 switch (howto->size)
2304 case 0:
2305 addend = bfd_get_8 (input_bfd, where);
2306 if (howto->pc_relative)
2308 addend = (addend ^ 0x80) - 0x80;
2309 addend += 1;
2311 break;
2312 case 1:
2313 addend = bfd_get_16 (input_bfd, where);
2314 if (howto->pc_relative)
2316 addend = (addend ^ 0x8000) - 0x8000;
2317 addend += 2;
2319 break;
2320 case 2:
2321 addend = bfd_get_32 (input_bfd, where);
2322 if (howto->pc_relative)
2324 addend = (addend ^ 0x80000000) - 0x80000000;
2325 addend += 4;
2327 break;
2328 default:
2329 abort ();
2332 if (info->relocatable)
2333 addend += sec->output_offset;
2334 else
2336 asection *msec = sec;
2337 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2338 addend);
2339 addend -= relocation;
2340 addend += msec->output_section->vma + msec->output_offset;
2343 switch (howto->size)
2345 case 0:
2346 /* FIXME: overflow checks. */
2347 if (howto->pc_relative)
2348 addend -= 1;
2349 bfd_put_8 (input_bfd, addend, where);
2350 break;
2351 case 1:
2352 if (howto->pc_relative)
2353 addend -= 2;
2354 bfd_put_16 (input_bfd, addend, where);
2355 break;
2356 case 2:
2357 if (howto->pc_relative)
2358 addend -= 4;
2359 bfd_put_32 (input_bfd, addend, where);
2360 break;
2364 else
2366 bfd_boolean warned;
2368 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2369 r_symndx, symtab_hdr, sym_hashes,
2370 h, sec, relocation,
2371 unresolved_reloc, warned);
2374 if (sec != NULL && elf_discarded_section (sec))
2376 /* For relocs against symbols from removed linkonce sections,
2377 or sections discarded by a linker script, we just want the
2378 section contents zeroed. Avoid any special processing. */
2379 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2380 rel->r_info = 0;
2381 rel->r_addend = 0;
2382 continue;
2385 if (info->relocatable)
2386 continue;
2388 switch (r_type)
2390 case R_386_GOT32:
2391 /* Relocation is to the entry for this symbol in the global
2392 offset table. */
2393 if (htab->sgot == NULL)
2394 abort ();
2396 if (h != NULL)
2398 bfd_boolean dyn;
2400 off = h->got.offset;
2401 dyn = htab->elf.dynamic_sections_created;
2402 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2403 || (info->shared
2404 && SYMBOL_REFERENCES_LOCAL (info, h))
2405 || (ELF_ST_VISIBILITY (h->other)
2406 && h->root.type == bfd_link_hash_undefweak))
2408 /* This is actually a static link, or it is a
2409 -Bsymbolic link and the symbol is defined
2410 locally, or the symbol was forced to be local
2411 because of a version file. We must initialize
2412 this entry in the global offset table. Since the
2413 offset must always be a multiple of 4, we use the
2414 least significant bit to record whether we have
2415 initialized it already.
2417 When doing a dynamic link, we create a .rel.got
2418 relocation entry to initialize the value. This
2419 is done in the finish_dynamic_symbol routine. */
2420 if ((off & 1) != 0)
2421 off &= ~1;
2422 else
2424 bfd_put_32 (output_bfd, relocation,
2425 htab->sgot->contents + off);
2426 h->got.offset |= 1;
2429 else
2430 unresolved_reloc = FALSE;
2432 else
2434 if (local_got_offsets == NULL)
2435 abort ();
2437 off = local_got_offsets[r_symndx];
2439 /* The offset must always be a multiple of 4. We use
2440 the least significant bit to record whether we have
2441 already generated the necessary reloc. */
2442 if ((off & 1) != 0)
2443 off &= ~1;
2444 else
2446 bfd_put_32 (output_bfd, relocation,
2447 htab->sgot->contents + off);
2449 if (info->shared)
2451 asection *s;
2452 Elf_Internal_Rela outrel;
2453 bfd_byte *loc;
2455 s = htab->srelgot;
2456 if (s == NULL)
2457 abort ();
2459 outrel.r_offset = (htab->sgot->output_section->vma
2460 + htab->sgot->output_offset
2461 + off);
2462 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2463 loc = s->contents;
2464 loc += s->reloc_count++ * sizeof (Elf32_External_Rel);
2465 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2468 local_got_offsets[r_symndx] |= 1;
2472 if (off >= (bfd_vma) -2)
2473 abort ();
2475 relocation = htab->sgot->output_section->vma
2476 + htab->sgot->output_offset + off
2477 - htab->sgotplt->output_section->vma
2478 - htab->sgotplt->output_offset;
2479 break;
2481 case R_386_GOTOFF:
2482 /* Relocation is relative to the start of the global offset
2483 table. */
2485 /* Check to make sure it isn't a protected function symbol
2486 for shared library since it may not be local when used
2487 as function address. */
2488 if (info->shared
2489 && !info->executable
2490 && h
2491 && h->def_regular
2492 && h->type == STT_FUNC
2493 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2495 (*_bfd_error_handler)
2496 (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
2497 input_bfd, h->root.root.string);
2498 bfd_set_error (bfd_error_bad_value);
2499 return FALSE;
2502 /* Note that sgot is not involved in this
2503 calculation. We always want the start of .got.plt. If we
2504 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2505 permitted by the ABI, we might have to change this
2506 calculation. */
2507 relocation -= htab->sgotplt->output_section->vma
2508 + htab->sgotplt->output_offset;
2509 break;
2511 case R_386_GOTPC:
2512 /* Use global offset table as symbol value. */
2513 relocation = htab->sgotplt->output_section->vma
2514 + htab->sgotplt->output_offset;
2515 unresolved_reloc = FALSE;
2516 break;
2518 case R_386_PLT32:
2519 /* Relocation is to the entry for this symbol in the
2520 procedure linkage table. */
2522 /* Resolve a PLT32 reloc against a local symbol directly,
2523 without using the procedure linkage table. */
2524 if (h == NULL)
2525 break;
2527 if (h->plt.offset == (bfd_vma) -1
2528 || htab->splt == NULL)
2530 /* We didn't make a PLT entry for this symbol. This
2531 happens when statically linking PIC code, or when
2532 using -Bsymbolic. */
2533 break;
2536 relocation = (htab->splt->output_section->vma
2537 + htab->splt->output_offset
2538 + h->plt.offset);
2539 unresolved_reloc = FALSE;
2540 break;
2542 case R_386_32:
2543 case R_386_PC32:
2544 if ((input_section->flags & SEC_ALLOC) == 0)
2545 break;
2547 if ((info->shared
2548 && (h == NULL
2549 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2550 || h->root.type != bfd_link_hash_undefweak)
2551 && (r_type != R_386_PC32
2552 || !SYMBOL_CALLS_LOCAL (info, h)))
2553 || (ELIMINATE_COPY_RELOCS
2554 && !info->shared
2555 && h != NULL
2556 && h->dynindx != -1
2557 && !h->non_got_ref
2558 && ((h->def_dynamic
2559 && !h->def_regular)
2560 || h->root.type == bfd_link_hash_undefweak
2561 || h->root.type == bfd_link_hash_undefined)))
2563 Elf_Internal_Rela outrel;
2564 bfd_byte *loc;
2565 bfd_boolean skip, relocate;
2566 asection *sreloc;
2568 /* When generating a shared object, these relocations
2569 are copied into the output file to be resolved at run
2570 time. */
2572 skip = FALSE;
2573 relocate = FALSE;
2575 outrel.r_offset =
2576 _bfd_elf_section_offset (output_bfd, info, input_section,
2577 rel->r_offset);
2578 if (outrel.r_offset == (bfd_vma) -1)
2579 skip = TRUE;
2580 else if (outrel.r_offset == (bfd_vma) -2)
2581 skip = TRUE, relocate = TRUE;
2582 outrel.r_offset += (input_section->output_section->vma
2583 + input_section->output_offset);
2585 if (skip)
2586 memset (&outrel, 0, sizeof outrel);
2587 else if (h != NULL
2588 && h->dynindx != -1
2589 && (r_type == R_386_PC32
2590 || !info->shared
2591 || !SYMBOLIC_BIND (info, h)
2592 || !h->def_regular))
2593 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2594 else
2596 /* This symbol is local, or marked to become local. */
2597 relocate = TRUE;
2598 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2601 sreloc = elf_section_data (input_section)->sreloc;
2602 if (sreloc == NULL)
2603 abort ();
2605 loc = sreloc->contents;
2606 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2607 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2609 /* If this reloc is against an external symbol, we do
2610 not want to fiddle with the addend. Otherwise, we
2611 need to include the symbol value so that it becomes
2612 an addend for the dynamic reloc. */
2613 if (! relocate)
2614 continue;
2616 break;
2618 case R_386_TLS_IE:
2619 if (info->shared)
2621 Elf_Internal_Rela outrel;
2622 bfd_byte *loc;
2623 asection *sreloc;
2625 outrel.r_offset = rel->r_offset
2626 + input_section->output_section->vma
2627 + input_section->output_offset;
2628 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2629 sreloc = elf_section_data (input_section)->sreloc;
2630 if (sreloc == NULL)
2631 abort ();
2632 loc = sreloc->contents;
2633 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2634 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2636 /* Fall through */
2638 case R_386_TLS_GD:
2639 case R_386_TLS_GOTDESC:
2640 case R_386_TLS_DESC_CALL:
2641 case R_386_TLS_IE_32:
2642 case R_386_TLS_GOTIE:
2643 r_type = elf_i386_tls_transition (info, r_type, h == NULL);
2644 tls_type = GOT_UNKNOWN;
2645 if (h == NULL && local_got_offsets)
2646 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
2647 else if (h != NULL)
2649 tls_type = elf_i386_hash_entry(h)->tls_type;
2650 if (!info->shared && h->dynindx == -1 && (tls_type & GOT_TLS_IE))
2651 r_type = R_386_TLS_LE_32;
2653 if (tls_type == GOT_TLS_IE)
2654 tls_type = GOT_TLS_IE_NEG;
2655 if (r_type == R_386_TLS_GD
2656 || r_type == R_386_TLS_GOTDESC
2657 || r_type == R_386_TLS_DESC_CALL)
2659 if (tls_type == GOT_TLS_IE_POS)
2660 r_type = R_386_TLS_GOTIE;
2661 else if (tls_type & GOT_TLS_IE)
2662 r_type = R_386_TLS_IE_32;
2665 if (r_type == R_386_TLS_LE_32)
2667 BFD_ASSERT (! unresolved_reloc);
2668 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
2670 unsigned int val, type;
2671 bfd_vma roff;
2673 /* GD->LE transition. */
2674 BFD_ASSERT (rel->r_offset >= 2);
2675 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2676 BFD_ASSERT (type == 0x8d || type == 0x04);
2677 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
2678 BFD_ASSERT (bfd_get_8 (input_bfd,
2679 contents + rel->r_offset + 4)
2680 == 0xe8);
2681 BFD_ASSERT (rel + 1 < relend);
2682 BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
2683 roff = rel->r_offset + 5;
2684 val = bfd_get_8 (input_bfd,
2685 contents + rel->r_offset - 1);
2686 if (type == 0x04)
2688 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
2689 Change it into:
2690 movl %gs:0, %eax; subl $foo@tpoff, %eax
2691 (6 byte form of subl). */
2692 BFD_ASSERT (rel->r_offset >= 3);
2693 BFD_ASSERT (bfd_get_8 (input_bfd,
2694 contents + rel->r_offset - 3)
2695 == 0x8d);
2696 BFD_ASSERT ((val & 0xc7) == 0x05 && val != (4 << 3));
2697 memcpy (contents + rel->r_offset - 3,
2698 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2700 else
2702 BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
2703 if (rel->r_offset + 10 <= input_section->size
2704 && bfd_get_8 (input_bfd,
2705 contents + rel->r_offset + 9) == 0x90)
2707 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
2708 Change it into:
2709 movl %gs:0, %eax; subl $foo@tpoff, %eax
2710 (6 byte form of subl). */
2711 memcpy (contents + rel->r_offset - 2,
2712 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2713 roff = rel->r_offset + 6;
2715 else
2717 /* leal foo(%reg), %eax; call ___tls_get_addr
2718 Change it into:
2719 movl %gs:0, %eax; subl $foo@tpoff, %eax
2720 (5 byte form of subl). */
2721 memcpy (contents + rel->r_offset - 2,
2722 "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2725 bfd_put_32 (output_bfd, tpoff (info, relocation),
2726 contents + roff);
2727 /* Skip R_386_PLT32. */
2728 rel++;
2729 continue;
2731 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
2733 /* GDesc -> LE transition.
2734 It's originally something like:
2735 leal x@tlsdesc(%ebx), %eax
2737 leal x@ntpoff, %eax
2739 Registers other than %eax may be set up here. */
2741 unsigned int val, type;
2742 bfd_vma roff;
2744 /* First, make sure it's a leal adding ebx to a
2745 32-bit offset into any register, although it's
2746 probably almost always going to be eax. */
2747 roff = rel->r_offset;
2748 BFD_ASSERT (roff >= 2);
2749 type = bfd_get_8 (input_bfd, contents + roff - 2);
2750 BFD_ASSERT (type == 0x8d);
2751 val = bfd_get_8 (input_bfd, contents + roff - 1);
2752 BFD_ASSERT ((val & 0xc7) == 0x83);
2753 BFD_ASSERT (roff + 4 <= input_section->size);
2755 /* Now modify the instruction as appropriate. */
2756 /* aoliva FIXME: remove the above and xor the byte
2757 below with 0x86. */
2758 bfd_put_8 (output_bfd, val ^ 0x86,
2759 contents + roff - 1);
2760 bfd_put_32 (output_bfd, -tpoff (info, relocation),
2761 contents + roff);
2762 continue;
2764 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
2766 /* GDesc -> LE transition.
2767 It's originally:
2768 call *(%eax)
2769 Turn it into:
2770 nop; nop */
2772 unsigned int val, type;
2773 bfd_vma roff;
2775 /* First, make sure it's a call *(%eax). */
2776 roff = rel->r_offset;
2777 BFD_ASSERT (roff + 2 <= input_section->size);
2778 type = bfd_get_8 (input_bfd, contents + roff);
2779 BFD_ASSERT (type == 0xff);
2780 val = bfd_get_8 (input_bfd, contents + roff + 1);
2781 BFD_ASSERT (val == 0x10);
2783 /* Now modify the instruction as appropriate. Use
2784 xchg %ax,%ax instead of 2 nops. */
2785 bfd_put_8 (output_bfd, 0x66, contents + roff);
2786 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2787 continue;
2789 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
2791 unsigned int val, type;
2793 /* IE->LE transition:
2794 Originally it can be one of:
2795 movl foo, %eax
2796 movl foo, %reg
2797 addl foo, %reg
2798 We change it into:
2799 movl $foo, %eax
2800 movl $foo, %reg
2801 addl $foo, %reg. */
2802 BFD_ASSERT (rel->r_offset >= 1);
2803 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2804 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
2805 if (val == 0xa1)
2807 /* movl foo, %eax. */
2808 bfd_put_8 (output_bfd, 0xb8,
2809 contents + rel->r_offset - 1);
2811 else
2813 BFD_ASSERT (rel->r_offset >= 2);
2814 type = bfd_get_8 (input_bfd,
2815 contents + rel->r_offset - 2);
2816 switch (type)
2818 case 0x8b:
2819 /* movl */
2820 BFD_ASSERT ((val & 0xc7) == 0x05);
2821 bfd_put_8 (output_bfd, 0xc7,
2822 contents + rel->r_offset - 2);
2823 bfd_put_8 (output_bfd,
2824 0xc0 | ((val >> 3) & 7),
2825 contents + rel->r_offset - 1);
2826 break;
2827 case 0x03:
2828 /* addl */
2829 BFD_ASSERT ((val & 0xc7) == 0x05);
2830 bfd_put_8 (output_bfd, 0x81,
2831 contents + rel->r_offset - 2);
2832 bfd_put_8 (output_bfd,
2833 0xc0 | ((val >> 3) & 7),
2834 contents + rel->r_offset - 1);
2835 break;
2836 default:
2837 BFD_FAIL ();
2838 break;
2841 bfd_put_32 (output_bfd, -tpoff (info, relocation),
2842 contents + rel->r_offset);
2843 continue;
2845 else
2847 unsigned int val, type;
2849 /* {IE_32,GOTIE}->LE transition:
2850 Originally it can be one of:
2851 subl foo(%reg1), %reg2
2852 movl foo(%reg1), %reg2
2853 addl foo(%reg1), %reg2
2854 We change it into:
2855 subl $foo, %reg2
2856 movl $foo, %reg2 (6 byte form)
2857 addl $foo, %reg2. */
2858 BFD_ASSERT (rel->r_offset >= 2);
2859 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2860 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2861 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
2862 BFD_ASSERT ((val & 0xc0) == 0x80 && (val & 7) != 4);
2863 if (type == 0x8b)
2865 /* movl */
2866 bfd_put_8 (output_bfd, 0xc7,
2867 contents + rel->r_offset - 2);
2868 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2869 contents + rel->r_offset - 1);
2871 else if (type == 0x2b)
2873 /* subl */
2874 bfd_put_8 (output_bfd, 0x81,
2875 contents + rel->r_offset - 2);
2876 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
2877 contents + rel->r_offset - 1);
2879 else if (type == 0x03)
2881 /* addl */
2882 bfd_put_8 (output_bfd, 0x81,
2883 contents + rel->r_offset - 2);
2884 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2885 contents + rel->r_offset - 1);
2887 else
2888 BFD_FAIL ();
2889 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
2890 bfd_put_32 (output_bfd, -tpoff (info, relocation),
2891 contents + rel->r_offset);
2892 else
2893 bfd_put_32 (output_bfd, tpoff (info, relocation),
2894 contents + rel->r_offset);
2895 continue;
2899 if (htab->sgot == NULL)
2900 abort ();
2902 if (h != NULL)
2904 off = h->got.offset;
2905 offplt = elf_i386_hash_entry (h)->tlsdesc_got;
2907 else
2909 if (local_got_offsets == NULL)
2910 abort ();
2912 off = local_got_offsets[r_symndx];
2913 offplt = local_tlsdesc_gotents[r_symndx];
2916 if ((off & 1) != 0)
2917 off &= ~1;
2918 else
2920 Elf_Internal_Rela outrel;
2921 bfd_byte *loc;
2922 int dr_type, indx;
2923 asection *sreloc;
2925 if (htab->srelgot == NULL)
2926 abort ();
2928 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2930 if (GOT_TLS_GDESC_P (tls_type))
2932 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
2933 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
2934 <= htab->sgotplt->size);
2935 outrel.r_offset = (htab->sgotplt->output_section->vma
2936 + htab->sgotplt->output_offset
2937 + offplt
2938 + htab->sgotplt_jump_table_size);
2939 sreloc = htab->srelplt;
2940 loc = sreloc->contents;
2941 loc += (htab->next_tls_desc_index++
2942 * sizeof (Elf32_External_Rel));
2943 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
2944 <= sreloc->contents + sreloc->size);
2945 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2946 if (indx == 0)
2948 BFD_ASSERT (! unresolved_reloc);
2949 bfd_put_32 (output_bfd,
2950 relocation - dtpoff_base (info),
2951 htab->sgotplt->contents + offplt
2952 + htab->sgotplt_jump_table_size + 4);
2954 else
2956 bfd_put_32 (output_bfd, 0,
2957 htab->sgotplt->contents + offplt
2958 + htab->sgotplt_jump_table_size + 4);
2962 sreloc = htab->srelgot;
2964 outrel.r_offset = (htab->sgot->output_section->vma
2965 + htab->sgot->output_offset + off);
2967 if (GOT_TLS_GD_P (tls_type))
2968 dr_type = R_386_TLS_DTPMOD32;
2969 else if (GOT_TLS_GDESC_P (tls_type))
2970 goto dr_done;
2971 else if (tls_type == GOT_TLS_IE_POS)
2972 dr_type = R_386_TLS_TPOFF;
2973 else
2974 dr_type = R_386_TLS_TPOFF32;
2976 if (dr_type == R_386_TLS_TPOFF && indx == 0)
2977 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
2978 htab->sgot->contents + off);
2979 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
2980 bfd_put_32 (output_bfd, dtpoff_base (info) - relocation,
2981 htab->sgot->contents + off);
2982 else if (dr_type != R_386_TLS_DESC)
2983 bfd_put_32 (output_bfd, 0,
2984 htab->sgot->contents + off);
2985 outrel.r_info = ELF32_R_INFO (indx, dr_type);
2987 loc = sreloc->contents;
2988 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2989 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
2990 <= sreloc->contents + sreloc->size);
2991 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2993 if (GOT_TLS_GD_P (tls_type))
2995 if (indx == 0)
2997 BFD_ASSERT (! unresolved_reloc);
2998 bfd_put_32 (output_bfd,
2999 relocation - dtpoff_base (info),
3000 htab->sgot->contents + off + 4);
3002 else
3004 bfd_put_32 (output_bfd, 0,
3005 htab->sgot->contents + off + 4);
3006 outrel.r_info = ELF32_R_INFO (indx,
3007 R_386_TLS_DTPOFF32);
3008 outrel.r_offset += 4;
3009 sreloc->reloc_count++;
3010 loc += sizeof (Elf32_External_Rel);
3011 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3012 <= sreloc->contents + sreloc->size);
3013 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3016 else if (tls_type == GOT_TLS_IE_BOTH)
3018 bfd_put_32 (output_bfd,
3019 indx == 0 ? relocation - dtpoff_base (info) : 0,
3020 htab->sgot->contents + off + 4);
3021 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3022 outrel.r_offset += 4;
3023 sreloc->reloc_count++;
3024 loc += sizeof (Elf32_External_Rel);
3025 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3028 dr_done:
3029 if (h != NULL)
3030 h->got.offset |= 1;
3031 else
3032 local_got_offsets[r_symndx] |= 1;
3035 if (off >= (bfd_vma) -2
3036 && ! GOT_TLS_GDESC_P (tls_type))
3037 abort ();
3038 if (r_type == R_386_TLS_GOTDESC
3039 || r_type == R_386_TLS_DESC_CALL)
3041 relocation = htab->sgotplt_jump_table_size + offplt;
3042 unresolved_reloc = FALSE;
3044 else if (r_type == ELF32_R_TYPE (rel->r_info))
3046 bfd_vma g_o_t = htab->sgotplt->output_section->vma
3047 + htab->sgotplt->output_offset;
3048 relocation = htab->sgot->output_section->vma
3049 + htab->sgot->output_offset + off - g_o_t;
3050 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3051 && tls_type == GOT_TLS_IE_BOTH)
3052 relocation += 4;
3053 if (r_type == R_386_TLS_IE)
3054 relocation += g_o_t;
3055 unresolved_reloc = FALSE;
3057 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3059 unsigned int val, type;
3060 bfd_vma roff;
3062 /* GD->IE transition. */
3063 BFD_ASSERT (rel->r_offset >= 2);
3064 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3065 BFD_ASSERT (type == 0x8d || type == 0x04);
3066 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
3067 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
3068 == 0xe8);
3069 BFD_ASSERT (rel + 1 < relend);
3070 BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
3071 roff = rel->r_offset - 3;
3072 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3073 if (type == 0x04)
3075 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3076 Change it into:
3077 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
3078 BFD_ASSERT (rel->r_offset >= 3);
3079 BFD_ASSERT (bfd_get_8 (input_bfd,
3080 contents + rel->r_offset - 3)
3081 == 0x8d);
3082 BFD_ASSERT ((val & 0xc7) == 0x05 && val != (4 << 3));
3083 val >>= 3;
3085 else
3087 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3088 Change it into:
3089 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
3090 BFD_ASSERT (rel->r_offset + 10 <= input_section->size);
3091 BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
3092 BFD_ASSERT (bfd_get_8 (input_bfd,
3093 contents + rel->r_offset + 9)
3094 == 0x90);
3095 roff = rel->r_offset - 2;
3097 memcpy (contents + roff,
3098 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3099 contents[roff + 7] = 0x80 | (val & 7);
3100 /* If foo is used only with foo@gotntpoff(%reg) and
3101 foo@indntpoff, but not with foo@gottpoff(%reg), change
3102 subl $foo@gottpoff(%reg), %eax
3103 into:
3104 addl $foo@gotntpoff(%reg), %eax. */
3105 if (tls_type == GOT_TLS_IE_POS)
3106 contents[roff + 6] = 0x03;
3107 bfd_put_32 (output_bfd,
3108 htab->sgot->output_section->vma
3109 + htab->sgot->output_offset + off
3110 - htab->sgotplt->output_section->vma
3111 - htab->sgotplt->output_offset,
3112 contents + roff + 8);
3113 /* Skip R_386_PLT32. */
3114 rel++;
3115 continue;
3117 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3119 /* GDesc -> IE transition.
3120 It's originally something like:
3121 leal x@tlsdesc(%ebx), %eax
3123 Change it to:
3124 movl x@gotntpoff(%ebx), %eax # before nop; nop
3126 movl x@gottpoff(%ebx), %eax # before negl %eax
3128 Registers other than %eax may be set up here. */
3130 unsigned int val, type;
3131 bfd_vma roff;
3133 /* First, make sure it's a leal adding ebx to a 32-bit
3134 offset into any register, although it's probably
3135 almost always going to be eax. */
3136 roff = rel->r_offset;
3137 BFD_ASSERT (roff >= 2);
3138 type = bfd_get_8 (input_bfd, contents + roff - 2);
3139 BFD_ASSERT (type == 0x8d);
3140 val = bfd_get_8 (input_bfd, contents + roff - 1);
3141 BFD_ASSERT ((val & 0xc7) == 0x83);
3142 BFD_ASSERT (roff + 4 <= input_section->size);
3144 /* Now modify the instruction as appropriate. */
3145 /* To turn a leal into a movl in the form we use it, it
3146 suffices to change the first byte from 0x8d to 0x8b.
3147 aoliva FIXME: should we decide to keep the leal, all
3148 we have to do is remove the statement below, and
3149 adjust the relaxation of R_386_TLS_DESC_CALL. */
3150 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3152 if (tls_type == GOT_TLS_IE_BOTH)
3153 off += 4;
3155 bfd_put_32 (output_bfd,
3156 htab->sgot->output_section->vma
3157 + htab->sgot->output_offset + off
3158 - htab->sgotplt->output_section->vma
3159 - htab->sgotplt->output_offset,
3160 contents + roff);
3161 continue;
3163 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3165 /* GDesc -> IE transition.
3166 It's originally:
3167 call *(%eax)
3169 Change it to:
3170 nop; nop
3172 negl %eax
3173 depending on how we transformed the TLS_GOTDESC above.
3176 unsigned int val, type;
3177 bfd_vma roff;
3179 /* First, make sure it's a call *(%eax). */
3180 roff = rel->r_offset;
3181 BFD_ASSERT (roff + 2 <= input_section->size);
3182 type = bfd_get_8 (input_bfd, contents + roff);
3183 BFD_ASSERT (type == 0xff);
3184 val = bfd_get_8 (input_bfd, contents + roff + 1);
3185 BFD_ASSERT (val == 0x10);
3187 /* Now modify the instruction as appropriate. */
3188 if (tls_type != GOT_TLS_IE_NEG)
3190 /* xchg %ax,%ax */
3191 bfd_put_8 (output_bfd, 0x66, contents + roff);
3192 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3194 else
3196 /* negl %eax */
3197 bfd_put_8 (output_bfd, 0xf7, contents + roff);
3198 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3201 continue;
3203 else
3204 BFD_ASSERT (FALSE);
3205 break;
3207 case R_386_TLS_LDM:
3208 if (! info->shared)
3210 unsigned int val;
3212 /* LD->LE transition:
3213 Ensure it is:
3214 leal foo(%reg), %eax; call ___tls_get_addr.
3215 We change it into:
3216 movl %gs:0, %eax; nop; leal 0(%esi,1), %esi. */
3217 BFD_ASSERT (rel->r_offset >= 2);
3218 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
3219 == 0x8d);
3220 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3221 BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
3222 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
3223 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
3224 == 0xe8);
3225 BFD_ASSERT (rel + 1 < relend);
3226 BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
3227 memcpy (contents + rel->r_offset - 2,
3228 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3229 /* Skip R_386_PLT32. */
3230 rel++;
3231 continue;
3234 if (htab->sgot == NULL)
3235 abort ();
3237 off = htab->tls_ldm_got.offset;
3238 if (off & 1)
3239 off &= ~1;
3240 else
3242 Elf_Internal_Rela outrel;
3243 bfd_byte *loc;
3245 if (htab->srelgot == NULL)
3246 abort ();
3248 outrel.r_offset = (htab->sgot->output_section->vma
3249 + htab->sgot->output_offset + off);
3251 bfd_put_32 (output_bfd, 0,
3252 htab->sgot->contents + off);
3253 bfd_put_32 (output_bfd, 0,
3254 htab->sgot->contents + off + 4);
3255 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3256 loc = htab->srelgot->contents;
3257 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3258 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3259 htab->tls_ldm_got.offset |= 1;
3261 relocation = htab->sgot->output_section->vma
3262 + htab->sgot->output_offset + off
3263 - htab->sgotplt->output_section->vma
3264 - htab->sgotplt->output_offset;
3265 unresolved_reloc = FALSE;
3266 break;
3268 case R_386_TLS_LDO_32:
3269 if (info->shared || (input_section->flags & SEC_CODE) == 0)
3270 relocation -= dtpoff_base (info);
3271 else
3272 /* When converting LDO to LE, we must negate. */
3273 relocation = -tpoff (info, relocation);
3274 break;
3276 case R_386_TLS_LE_32:
3277 case R_386_TLS_LE:
3278 if (info->shared)
3280 Elf_Internal_Rela outrel;
3281 asection *sreloc;
3282 bfd_byte *loc;
3283 int indx;
3285 outrel.r_offset = rel->r_offset
3286 + input_section->output_section->vma
3287 + input_section->output_offset;
3288 if (h != NULL && h->dynindx != -1)
3289 indx = h->dynindx;
3290 else
3291 indx = 0;
3292 if (r_type == R_386_TLS_LE_32)
3293 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3294 else
3295 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3296 sreloc = elf_section_data (input_section)->sreloc;
3297 if (sreloc == NULL)
3298 abort ();
3299 loc = sreloc->contents;
3300 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3301 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3302 if (indx)
3303 continue;
3304 else if (r_type == R_386_TLS_LE_32)
3305 relocation = dtpoff_base (info) - relocation;
3306 else
3307 relocation -= dtpoff_base (info);
3309 else if (r_type == R_386_TLS_LE_32)
3310 relocation = tpoff (info, relocation);
3311 else
3312 relocation = -tpoff (info, relocation);
3313 break;
3315 default:
3316 break;
3319 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3320 because such sections are not SEC_ALLOC and thus ld.so will
3321 not process them. */
3322 if (unresolved_reloc
3323 && !((input_section->flags & SEC_DEBUGGING) != 0
3324 && h->def_dynamic))
3326 (*_bfd_error_handler)
3327 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3328 input_bfd,
3329 input_section,
3330 (long) rel->r_offset,
3331 howto->name,
3332 h->root.root.string);
3333 return FALSE;
3336 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3337 contents, rel->r_offset,
3338 relocation, 0);
3340 if (r != bfd_reloc_ok)
3342 const char *name;
3344 if (h != NULL)
3345 name = h->root.root.string;
3346 else
3348 name = bfd_elf_string_from_elf_section (input_bfd,
3349 symtab_hdr->sh_link,
3350 sym->st_name);
3351 if (name == NULL)
3352 return FALSE;
3353 if (*name == '\0')
3354 name = bfd_section_name (input_bfd, sec);
3357 if (r == bfd_reloc_overflow)
3359 if (! ((*info->callbacks->reloc_overflow)
3360 (info, (h ? &h->root : NULL), name, howto->name,
3361 (bfd_vma) 0, input_bfd, input_section,
3362 rel->r_offset)))
3363 return FALSE;
3365 else
3367 (*_bfd_error_handler)
3368 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3369 input_bfd, input_section,
3370 (long) rel->r_offset, name, (int) r);
3371 return FALSE;
3376 return TRUE;
3379 /* Finish up dynamic symbol handling. We set the contents of various
3380 dynamic sections here. */
3382 static bfd_boolean
3383 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3384 struct bfd_link_info *info,
3385 struct elf_link_hash_entry *h,
3386 Elf_Internal_Sym *sym)
3388 struct elf_i386_link_hash_table *htab;
3390 htab = elf_i386_hash_table (info);
3392 if (h->plt.offset != (bfd_vma) -1)
3394 bfd_vma plt_index;
3395 bfd_vma got_offset;
3396 Elf_Internal_Rela rel;
3397 bfd_byte *loc;
3399 /* This symbol has an entry in the procedure linkage table. Set
3400 it up. */
3402 if (h->dynindx == -1
3403 || htab->splt == NULL
3404 || htab->sgotplt == NULL
3405 || htab->srelplt == NULL)
3406 abort ();
3408 /* Get the index in the procedure linkage table which
3409 corresponds to this symbol. This is the index of this symbol
3410 in all the symbols for which we are making plt entries. The
3411 first entry in the procedure linkage table is reserved. */
3412 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3414 /* Get the offset into the .got table of the entry that
3415 corresponds to this function. Each .got entry is 4 bytes.
3416 The first three are reserved. */
3417 got_offset = (plt_index + 3) * 4;
3419 /* Fill in the entry in the procedure linkage table. */
3420 if (! info->shared)
3422 memcpy (htab->splt->contents + h->plt.offset, elf_i386_plt_entry,
3423 PLT_ENTRY_SIZE);
3424 bfd_put_32 (output_bfd,
3425 (htab->sgotplt->output_section->vma
3426 + htab->sgotplt->output_offset
3427 + got_offset),
3428 htab->splt->contents + h->plt.offset + 1);
3430 if (htab->is_vxworks)
3432 int s, k, reloc_index;
3434 /* Create the R_386_32 relocation referencing the GOT
3435 for this PLT entry. */
3437 /* S: Current slot number (zero-based). */
3438 s = (h->plt.offset - PLT_ENTRY_SIZE) / PLT_ENTRY_SIZE;
3439 /* K: Number of relocations for PLTResolve. */
3440 if (info->shared)
3441 k = PLTRESOLVE_RELOCS_SHLIB;
3442 else
3443 k = PLTRESOLVE_RELOCS;
3444 /* Skip the PLTresolve relocations, and the relocations for
3445 the other PLT slots. */
3446 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3447 loc = (htab->srelplt2->contents + reloc_index
3448 * sizeof (Elf32_External_Rel));
3450 rel.r_offset = (htab->splt->output_section->vma
3451 + htab->splt->output_offset
3452 + h->plt.offset + 2),
3453 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3454 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3456 /* Create the R_386_32 relocation referencing the beginning of
3457 the PLT for this GOT entry. */
3458 rel.r_offset = (htab->sgotplt->output_section->vma
3459 + htab->sgotplt->output_offset
3460 + got_offset);
3461 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3462 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3463 loc + sizeof (Elf32_External_Rel));
3466 else
3468 memcpy (htab->splt->contents + h->plt.offset, elf_i386_pic_plt_entry,
3469 PLT_ENTRY_SIZE);
3470 bfd_put_32 (output_bfd, got_offset,
3471 htab->splt->contents + h->plt.offset + 2);
3474 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
3475 htab->splt->contents + h->plt.offset + 12);
3477 * The previous code assumed that the jump entry ended a PLT entry.
3478 * Forcing alignment to 0mod32 adds some hlt bytes at the end (11).
3479 * We therefore need to subtract those bytes from the jump offset.
3481 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE - 11),
3482 htab->splt->contents + h->plt.offset + 17);
3484 /* Fill in the entry in the global offset table. */
3485 bfd_put_32 (output_bfd,
3486 (htab->splt->output_section->vma
3487 + htab->splt->output_offset
3488 + h->plt.offset
3489 + 11),
3490 htab->sgotplt->contents + got_offset);
3492 /* Fill in the entry in the .rel.plt section. */
3493 rel.r_offset = (htab->sgotplt->output_section->vma
3494 + htab->sgotplt->output_offset
3495 + got_offset);
3496 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3497 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rel);
3498 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3500 if (!h->def_regular)
3502 /* Mark the symbol as undefined, rather than as defined in
3503 the .plt section. Leave the value if there were any
3504 relocations where pointer equality matters (this is a clue
3505 for the dynamic linker, to make function pointer
3506 comparisons work between an application and shared
3507 library), otherwise set it to zero. If a function is only
3508 called from a binary, there is no need to slow down
3509 shared libraries because of that. */
3510 sym->st_shndx = SHN_UNDEF;
3511 if (!h->pointer_equality_needed)
3512 sym->st_value = 0;
3516 if (h->got.offset != (bfd_vma) -1
3517 && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
3518 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
3520 Elf_Internal_Rela rel;
3521 bfd_byte *loc;
3523 /* This symbol has an entry in the global offset table. Set it
3524 up. */
3526 if (htab->sgot == NULL || htab->srelgot == NULL)
3527 abort ();
3529 rel.r_offset = (htab->sgot->output_section->vma
3530 + htab->sgot->output_offset
3531 + (h->got.offset & ~(bfd_vma) 1));
3533 /* If this is a static link, or it is a -Bsymbolic link and the
3534 symbol is defined locally or was forced to be local because
3535 of a version file, we just want to emit a RELATIVE reloc.
3536 The entry in the global offset table will already have been
3537 initialized in the relocate_section function. */
3538 if (info->shared
3539 && SYMBOL_REFERENCES_LOCAL (info, h))
3541 BFD_ASSERT((h->got.offset & 1) != 0);
3542 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3544 else
3546 BFD_ASSERT((h->got.offset & 1) == 0);
3547 bfd_put_32 (output_bfd, (bfd_vma) 0,
3548 htab->sgot->contents + h->got.offset);
3549 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3552 loc = htab->srelgot->contents;
3553 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3554 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3557 if (h->needs_copy)
3559 Elf_Internal_Rela rel;
3560 bfd_byte *loc;
3562 /* This symbol needs a copy reloc. Set it up. */
3564 if (h->dynindx == -1
3565 || (h->root.type != bfd_link_hash_defined
3566 && h->root.type != bfd_link_hash_defweak)
3567 || htab->srelbss == NULL)
3568 abort ();
3570 rel.r_offset = (h->root.u.def.value
3571 + h->root.u.def.section->output_section->vma
3572 + h->root.u.def.section->output_offset);
3573 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
3574 loc = htab->srelbss->contents;
3575 loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rel);
3576 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3579 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.
3580 On VxWorks, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it
3581 is relative to the ".got" section. */
3582 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3583 || (!htab->is_vxworks && h == htab->elf.hgot))
3584 sym->st_shndx = SHN_ABS;
3586 return TRUE;
3589 /* Used to decide how to sort relocs in an optimal manner for the
3590 dynamic linker, before writing them out. */
3592 static enum elf_reloc_type_class
3593 elf_i386_reloc_type_class (const Elf_Internal_Rela *rela)
3595 switch (ELF32_R_TYPE (rela->r_info))
3597 case R_386_RELATIVE:
3598 return reloc_class_relative;
3599 case R_386_JUMP_SLOT:
3600 return reloc_class_plt;
3601 case R_386_COPY:
3602 return reloc_class_copy;
3603 default:
3604 return reloc_class_normal;
3608 /* Finish up the dynamic sections. */
3610 static bfd_boolean
3611 elf_i386_finish_dynamic_sections (bfd *output_bfd,
3612 struct bfd_link_info *info)
3614 struct elf_i386_link_hash_table *htab;
3615 bfd *dynobj;
3616 asection *sdyn;
3618 htab = elf_i386_hash_table (info);
3619 dynobj = htab->elf.dynobj;
3620 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3622 if (htab->elf.dynamic_sections_created)
3624 Elf32_External_Dyn *dyncon, *dynconend;
3626 if (sdyn == NULL || htab->sgot == NULL)
3627 abort ();
3629 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3630 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3631 for (; dyncon < dynconend; dyncon++)
3633 Elf_Internal_Dyn dyn;
3634 asection *s;
3636 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3638 switch (dyn.d_tag)
3640 default:
3641 continue;
3643 case DT_PLTGOT:
3644 s = htab->sgotplt;
3645 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3646 break;
3648 case DT_JMPREL:
3649 s = htab->srelplt;
3650 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3651 break;
3653 case DT_PLTRELSZ:
3654 s = htab->srelplt;
3655 dyn.d_un.d_val = s->size;
3656 break;
3658 case DT_RELSZ:
3659 /* My reading of the SVR4 ABI indicates that the
3660 procedure linkage table relocs (DT_JMPREL) should be
3661 included in the overall relocs (DT_REL). This is
3662 what Solaris does. However, UnixWare can not handle
3663 that case. Therefore, we override the DT_RELSZ entry
3664 here to make it not include the JMPREL relocs. */
3665 s = htab->srelplt;
3666 if (s == NULL)
3667 continue;
3668 dyn.d_un.d_val -= s->size;
3669 break;
3671 case DT_REL:
3672 /* We may not be using the standard ELF linker script.
3673 If .rel.plt is the first .rel section, we adjust
3674 DT_REL to not include it. */
3675 s = htab->srelplt;
3676 if (s == NULL)
3677 continue;
3678 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
3679 continue;
3680 dyn.d_un.d_ptr += s->size;
3681 break;
3684 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3687 /* Fill in the first entry in the procedure linkage table. */
3688 if (htab->splt && htab->splt->size > 0)
3690 if (info->shared)
3692 memcpy (htab->splt->contents, elf_i386_pic_plt0_entry,
3693 sizeof (elf_i386_pic_plt0_entry));
3694 memset (htab->splt->contents + sizeof (elf_i386_pic_plt0_entry),
3695 htab->plt0_pad_byte,
3696 PLT_ENTRY_SIZE - sizeof (elf_i386_pic_plt0_entry));
3698 else
3700 memcpy (htab->splt->contents, elf_i386_plt0_entry,
3701 sizeof(elf_i386_plt0_entry));
3702 memset (htab->splt->contents + sizeof (elf_i386_plt0_entry),
3703 htab->plt0_pad_byte,
3704 PLT_ENTRY_SIZE - sizeof (elf_i386_plt0_entry));
3705 bfd_put_32 (output_bfd,
3706 (htab->sgotplt->output_section->vma
3707 + htab->sgotplt->output_offset
3708 + 4),
3709 htab->splt->contents + 2);
3710 bfd_put_32 (output_bfd,
3711 (htab->sgotplt->output_section->vma
3712 + htab->sgotplt->output_offset
3713 + 8),
3714 htab->splt->contents + 7);
3716 if (htab->is_vxworks)
3718 Elf_Internal_Rela rel;
3720 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
3721 On IA32 we use REL relocations so the addend goes in
3722 the PLT directly. */
3723 rel.r_offset = (htab->splt->output_section->vma
3724 + htab->splt->output_offset
3725 + 2);
3726 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3727 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3728 htab->srelplt2->contents);
3729 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
3730 rel.r_offset = (htab->splt->output_section->vma
3731 + htab->splt->output_offset
3732 + 8);
3733 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3734 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3735 htab->srelplt2->contents +
3736 sizeof (Elf32_External_Rel));
3740 /* UnixWare sets the entsize of .plt to 4, although that doesn't
3741 really seem like the right value. */
3742 elf_section_data (htab->splt->output_section)
3743 ->this_hdr.sh_entsize = 4;
3745 /* Correct the .rel.plt.unloaded relocations. */
3746 if (htab->is_vxworks && !info->shared)
3748 int num_plts = (htab->splt->size / PLT_ENTRY_SIZE) - 1;
3749 unsigned char *p;
3751 p = htab->srelplt2->contents;
3752 if (info->shared)
3753 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
3754 else
3755 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
3757 for (; num_plts; num_plts--)
3759 Elf_Internal_Rela rel;
3760 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
3761 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3762 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
3763 p += sizeof (Elf32_External_Rel);
3765 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
3766 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3767 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
3768 p += sizeof (Elf32_External_Rel);
3774 if (htab->sgotplt)
3776 /* Fill in the first three entries in the global offset table. */
3777 if (htab->sgotplt->size > 0)
3779 bfd_put_32 (output_bfd,
3780 (sdyn == NULL ? 0
3781 : sdyn->output_section->vma + sdyn->output_offset),
3782 htab->sgotplt->contents);
3783 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 4);
3784 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 8);
3787 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = 4;
3790 if (htab->sgot && htab->sgot->size > 0)
3791 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
3793 return TRUE;
3796 /* Return address for Ith PLT stub in section PLT, for relocation REL
3797 or (bfd_vma) -1 if it should not be included. */
3799 static bfd_vma
3800 elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
3801 const arelent *rel ATTRIBUTE_UNUSED)
3803 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3806 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3808 static bfd_boolean
3809 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
3811 if (h->plt.offset != (bfd_vma) -1
3812 && !h->def_regular
3813 && !h->pointer_equality_needed)
3814 return FALSE;
3816 return _bfd_elf_hash_symbol (h);
3819 #define TARGET_LITTLE_SYM bfd_elf32_nacl_vec
3820 #define TARGET_LITTLE_NAME "elf32-nacl"
3822 /* NativeClient defines its own ABI.*/
3823 #undef ELF_OSABI
3824 #define ELF_OSABI ELFOSABI_NACL
3826 #define ELF_ARCH bfd_arch_i386
3827 #define ELF_MACHINE_CODE EM_386
3828 #define ELF_MAXPAGESIZE 0x1000
3830 #define elf_backend_can_gc_sections 1
3831 #define elf_backend_can_refcount 1
3832 #define elf_backend_want_got_plt 1
3833 #define elf_backend_plt_readonly 1
3834 #define elf_backend_want_plt_sym 0
3835 #define elf_backend_got_header_size 12
3837 /* Support RELA for objdump of prelink objects. */
3838 #define elf_info_to_howto elf_i386_info_to_howto_rel
3839 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
3841 #define bfd_elf32_mkobject elf_i386_mkobject
3843 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
3844 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
3845 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
3846 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
3848 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
3849 #define elf_backend_check_relocs elf_i386_check_relocs
3850 #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
3851 #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
3852 #define elf_backend_fake_sections elf_i386_fake_sections
3853 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
3854 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
3855 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
3856 #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
3857 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
3858 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
3859 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
3860 #define elf_backend_relocate_section elf_i386_relocate_section
3861 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
3862 #define elf_backend_always_size_sections elf_i386_always_size_sections
3863 #define elf_backend_omit_section_dynsym \
3864 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
3865 #define elf_backend_plt_sym_val elf_i386_plt_sym_val
3866 #define elf_backend_hash_symbol elf_i386_hash_symbol
3868 #define bfd_elf32_bfd_merge_private_bfd_data \
3869 elf32_nacl_merge_private_bfd_data
3871 static unsigned long previous_ibfd_e_flags = (unsigned long) EF_NACL_ALIGN_LIB;
3872 static unsigned char previous_ibfd_abiversion = 0;
3874 static bfd_boolean
3875 elf32_nacl_merge_private_bfd_data (bfd *ibfd,
3876 bfd *obfd)
3878 unsigned long ibfd_e_flags;
3879 unsigned char ibfd_abiversion;
3881 ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_NACL_ALIGN_MASK;
3882 if ((ibfd_e_flags != EF_NACL_ALIGN_LIB) &&
3883 (previous_ibfd_e_flags != EF_NACL_ALIGN_LIB) &&
3884 (ibfd_e_flags != previous_ibfd_e_flags)) {
3885 (*_bfd_error_handler)
3886 (_("%B: linking files with incompatible alignments"), ibfd);
3887 bfd_set_error (bfd_error_bad_value);
3888 return FALSE;
3891 ibfd_abiversion = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
3892 if (ibfd_abiversion != 0 &&
3893 previous_ibfd_abiversion != 0 &&
3894 ibfd_abiversion != previous_ibfd_abiversion) {
3895 (*_bfd_error_handler)
3896 (_("%B: linking files with incompatible abi version"), ibfd);
3897 bfd_set_error (bfd_error_bad_value);
3898 return FALSE;
3901 previous_ibfd_e_flags = ibfd_e_flags;
3902 elf_elfheader (obfd)->e_flags |= previous_ibfd_e_flags;
3904 previous_ibfd_abiversion = ibfd_abiversion;
3905 elf_elfheader (obfd)->e_ident[EI_ABIVERSION] = previous_ibfd_abiversion;
3907 return TRUE;
3910 #define elf_backend_final_write_processing \
3911 elf32_nacl_backend_final_write_processing
3913 static void
3914 elf32_nacl_backend_final_write_processing (bfd *abfd,
3915 bfd_boolean linker ATTRIBUTE_UNUSED)
3917 elf_elfheader (abfd)->e_ident[EI_OSABI] = ELFOSABI_NACL;
3918 elf_elfheader (abfd)->e_ident[EI_ABIVERSION] = EF_NACL_ABIVERSION;
3919 elf_elfheader (abfd)->e_flags |= previous_ibfd_e_flags;
3922 #include "elf32-target.h"