* gdb.linespec/ls-dollar.cc: New file.
[deliverable/binutils-gdb.git] / bfd / elf32-i386.c
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, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28 #include "elf-vxworks.h"
29 #include "bfd_stdint.h"
30 #include "objalloc.h"
31 #include "hashtab.h"
32 #include "dwarf2.h"
33
34 /* 386 uses REL relocations instead of RELA. */
35 #define USE_REL 1
36
37 #include "elf/i386.h"
38
39 static reloc_howto_type elf_howto_table[]=
40 {
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),
74
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)
81
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),
113
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),
145 HOWTO(R_386_IRELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
146 bfd_elf_generic_reloc, "R_386_IRELATIVE",
147 TRUE, 0xffffffff, 0xffffffff, FALSE),
148
149 /* Another gap. */
150 #define R_386_irelative (R_386_IRELATIVE + 1 - R_386_tls_offset)
151 #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_irelative)
152
153 /* GNU extension to record C++ vtable hierarchy. */
154 HOWTO (R_386_GNU_VTINHERIT, /* type */
155 0, /* rightshift */
156 2, /* size (0 = byte, 1 = short, 2 = long) */
157 0, /* bitsize */
158 FALSE, /* pc_relative */
159 0, /* bitpos */
160 complain_overflow_dont, /* complain_on_overflow */
161 NULL, /* special_function */
162 "R_386_GNU_VTINHERIT", /* name */
163 FALSE, /* partial_inplace */
164 0, /* src_mask */
165 0, /* dst_mask */
166 FALSE), /* pcrel_offset */
167
168 /* GNU extension to record C++ vtable member usage. */
169 HOWTO (R_386_GNU_VTENTRY, /* type */
170 0, /* rightshift */
171 2, /* size (0 = byte, 1 = short, 2 = long) */
172 0, /* bitsize */
173 FALSE, /* pc_relative */
174 0, /* bitpos */
175 complain_overflow_dont, /* complain_on_overflow */
176 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
177 "R_386_GNU_VTENTRY", /* name */
178 FALSE, /* partial_inplace */
179 0, /* src_mask */
180 0, /* dst_mask */
181 FALSE) /* pcrel_offset */
182
183 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
184
185 };
186
187 #ifdef DEBUG_GEN_RELOC
188 #define TRACE(str) \
189 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
190 #else
191 #define TRACE(str)
192 #endif
193
194 static reloc_howto_type *
195 elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
196 bfd_reloc_code_real_type code)
197 {
198 switch (code)
199 {
200 case BFD_RELOC_NONE:
201 TRACE ("BFD_RELOC_NONE");
202 return &elf_howto_table[R_386_NONE];
203
204 case BFD_RELOC_32:
205 TRACE ("BFD_RELOC_32");
206 return &elf_howto_table[R_386_32];
207
208 case BFD_RELOC_CTOR:
209 TRACE ("BFD_RELOC_CTOR");
210 return &elf_howto_table[R_386_32];
211
212 case BFD_RELOC_32_PCREL:
213 TRACE ("BFD_RELOC_PC32");
214 return &elf_howto_table[R_386_PC32];
215
216 case BFD_RELOC_386_GOT32:
217 TRACE ("BFD_RELOC_386_GOT32");
218 return &elf_howto_table[R_386_GOT32];
219
220 case BFD_RELOC_386_PLT32:
221 TRACE ("BFD_RELOC_386_PLT32");
222 return &elf_howto_table[R_386_PLT32];
223
224 case BFD_RELOC_386_COPY:
225 TRACE ("BFD_RELOC_386_COPY");
226 return &elf_howto_table[R_386_COPY];
227
228 case BFD_RELOC_386_GLOB_DAT:
229 TRACE ("BFD_RELOC_386_GLOB_DAT");
230 return &elf_howto_table[R_386_GLOB_DAT];
231
232 case BFD_RELOC_386_JUMP_SLOT:
233 TRACE ("BFD_RELOC_386_JUMP_SLOT");
234 return &elf_howto_table[R_386_JUMP_SLOT];
235
236 case BFD_RELOC_386_RELATIVE:
237 TRACE ("BFD_RELOC_386_RELATIVE");
238 return &elf_howto_table[R_386_RELATIVE];
239
240 case BFD_RELOC_386_GOTOFF:
241 TRACE ("BFD_RELOC_386_GOTOFF");
242 return &elf_howto_table[R_386_GOTOFF];
243
244 case BFD_RELOC_386_GOTPC:
245 TRACE ("BFD_RELOC_386_GOTPC");
246 return &elf_howto_table[R_386_GOTPC];
247
248 /* These relocs are a GNU extension. */
249 case BFD_RELOC_386_TLS_TPOFF:
250 TRACE ("BFD_RELOC_386_TLS_TPOFF");
251 return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
252
253 case BFD_RELOC_386_TLS_IE:
254 TRACE ("BFD_RELOC_386_TLS_IE");
255 return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
256
257 case BFD_RELOC_386_TLS_GOTIE:
258 TRACE ("BFD_RELOC_386_TLS_GOTIE");
259 return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
260
261 case BFD_RELOC_386_TLS_LE:
262 TRACE ("BFD_RELOC_386_TLS_LE");
263 return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
264
265 case BFD_RELOC_386_TLS_GD:
266 TRACE ("BFD_RELOC_386_TLS_GD");
267 return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
268
269 case BFD_RELOC_386_TLS_LDM:
270 TRACE ("BFD_RELOC_386_TLS_LDM");
271 return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
272
273 case BFD_RELOC_16:
274 TRACE ("BFD_RELOC_16");
275 return &elf_howto_table[R_386_16 - R_386_ext_offset];
276
277 case BFD_RELOC_16_PCREL:
278 TRACE ("BFD_RELOC_16_PCREL");
279 return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
280
281 case BFD_RELOC_8:
282 TRACE ("BFD_RELOC_8");
283 return &elf_howto_table[R_386_8 - R_386_ext_offset];
284
285 case BFD_RELOC_8_PCREL:
286 TRACE ("BFD_RELOC_8_PCREL");
287 return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
288
289 /* Common with Sun TLS implementation. */
290 case BFD_RELOC_386_TLS_LDO_32:
291 TRACE ("BFD_RELOC_386_TLS_LDO_32");
292 return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
293
294 case BFD_RELOC_386_TLS_IE_32:
295 TRACE ("BFD_RELOC_386_TLS_IE_32");
296 return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
297
298 case BFD_RELOC_386_TLS_LE_32:
299 TRACE ("BFD_RELOC_386_TLS_LE_32");
300 return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
301
302 case BFD_RELOC_386_TLS_DTPMOD32:
303 TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
304 return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
305
306 case BFD_RELOC_386_TLS_DTPOFF32:
307 TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
308 return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
309
310 case BFD_RELOC_386_TLS_TPOFF32:
311 TRACE ("BFD_RELOC_386_TLS_TPOFF32");
312 return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
313
314 case BFD_RELOC_386_TLS_GOTDESC:
315 TRACE ("BFD_RELOC_386_TLS_GOTDESC");
316 return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
317
318 case BFD_RELOC_386_TLS_DESC_CALL:
319 TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
320 return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
321
322 case BFD_RELOC_386_TLS_DESC:
323 TRACE ("BFD_RELOC_386_TLS_DESC");
324 return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
325
326 case BFD_RELOC_386_IRELATIVE:
327 TRACE ("BFD_RELOC_386_IRELATIVE");
328 return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset];
329
330 case BFD_RELOC_VTABLE_INHERIT:
331 TRACE ("BFD_RELOC_VTABLE_INHERIT");
332 return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
333
334 case BFD_RELOC_VTABLE_ENTRY:
335 TRACE ("BFD_RELOC_VTABLE_ENTRY");
336 return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
337
338 default:
339 break;
340 }
341
342 TRACE ("Unknown");
343 return 0;
344 }
345
346 static reloc_howto_type *
347 elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
348 const char *r_name)
349 {
350 unsigned int i;
351
352 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
353 if (elf_howto_table[i].name != NULL
354 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
355 return &elf_howto_table[i];
356
357 return NULL;
358 }
359
360 static reloc_howto_type *
361 elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
362 {
363 unsigned int indx;
364
365 if ((indx = r_type) >= R_386_standard
366 && ((indx = r_type - R_386_ext_offset) - R_386_standard
367 >= R_386_ext - R_386_standard)
368 && ((indx = r_type - R_386_tls_offset) - R_386_ext
369 >= R_386_irelative - R_386_ext)
370 && ((indx = r_type - R_386_vt_offset) - R_386_irelative
371 >= R_386_vt - R_386_irelative))
372 {
373 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
374 abfd, (int) r_type);
375 indx = R_386_NONE;
376 }
377 BFD_ASSERT (elf_howto_table [indx].type == r_type);
378 return &elf_howto_table[indx];
379 }
380
381 static void
382 elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
383 arelent *cache_ptr,
384 Elf_Internal_Rela *dst)
385 {
386 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
387 cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type);
388 }
389
390 /* Return whether a symbol name implies a local label. The UnixWare
391 2.1 cc generates temporary symbols that start with .X, so we
392 recognize them here. FIXME: do other SVR4 compilers also use .X?.
393 If so, we should move the .X recognition into
394 _bfd_elf_is_local_label_name. */
395
396 static bfd_boolean
397 elf_i386_is_local_label_name (bfd *abfd, const char *name)
398 {
399 if (name[0] == '.' && name[1] == 'X')
400 return TRUE;
401
402 return _bfd_elf_is_local_label_name (abfd, name);
403 }
404 \f
405 /* Support for core dump NOTE sections. */
406
407 static bfd_boolean
408 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
409 {
410 int offset;
411 size_t size;
412
413 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
414 {
415 int pr_version = bfd_get_32 (abfd, note->descdata);
416
417 if (pr_version != 1)
418 return FALSE;
419
420 /* pr_cursig */
421 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 20);
422
423 /* pr_pid */
424 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 24);
425
426 /* pr_reg */
427 offset = 28;
428 size = bfd_get_32 (abfd, note->descdata + 8);
429 }
430 else
431 {
432 switch (note->descsz)
433 {
434 default:
435 return FALSE;
436
437 case 144: /* Linux/i386 */
438 /* pr_cursig */
439 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
440
441 /* pr_pid */
442 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 24);
443
444 /* pr_reg */
445 offset = 72;
446 size = 68;
447
448 break;
449 }
450 }
451
452 /* Make a ".reg/999" section. */
453 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
454 size, note->descpos + offset);
455 }
456
457 static bfd_boolean
458 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
459 {
460 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
461 {
462 int pr_version = bfd_get_32 (abfd, note->descdata);
463
464 if (pr_version != 1)
465 return FALSE;
466
467 elf_tdata (abfd)->core_program
468 = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
469 elf_tdata (abfd)->core_command
470 = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
471 }
472 else
473 {
474 switch (note->descsz)
475 {
476 default:
477 return FALSE;
478
479 case 124: /* Linux/i386 elf_prpsinfo. */
480 elf_tdata (abfd)->core_pid
481 = bfd_get_32 (abfd, note->descdata + 12);
482 elf_tdata (abfd)->core_program
483 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
484 elf_tdata (abfd)->core_command
485 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
486 }
487 }
488
489 /* Note that for some reason, a spurious space is tacked
490 onto the end of the args in some (at least one anyway)
491 implementations, so strip it off if it exists. */
492 {
493 char *command = elf_tdata (abfd)->core_command;
494 int n = strlen (command);
495
496 if (0 < n && command[n - 1] == ' ')
497 command[n - 1] = '\0';
498 }
499
500 return TRUE;
501 }
502 \f
503 /* Functions for the i386 ELF linker.
504
505 In order to gain some understanding of code in this file without
506 knowing all the intricate details of the linker, note the
507 following:
508
509 Functions named elf_i386_* are called by external routines, other
510 functions are only called locally. elf_i386_* functions appear
511 in this file more or less in the order in which they are called
512 from external routines. eg. elf_i386_check_relocs is called
513 early in the link process, elf_i386_finish_dynamic_sections is
514 one of the last functions. */
515
516
517 /* The name of the dynamic interpreter. This is put in the .interp
518 section. */
519
520 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
521
522 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
523 copying dynamic variables from a shared lib into an app's dynbss
524 section, and instead use a dynamic relocation to point into the
525 shared lib. */
526 #define ELIMINATE_COPY_RELOCS 1
527
528 /* The size in bytes of an entry in the procedure linkage table. */
529
530 #define PLT_ENTRY_SIZE 16
531
532 /* The first entry in an absolute procedure linkage table looks like
533 this. See the SVR4 ABI i386 supplement to see how this works.
534 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
535
536 static const bfd_byte elf_i386_plt0_entry[12] =
537 {
538 0xff, 0x35, /* pushl contents of address */
539 0, 0, 0, 0, /* replaced with address of .got + 4. */
540 0xff, 0x25, /* jmp indirect */
541 0, 0, 0, 0 /* replaced with address of .got + 8. */
542 };
543
544 /* Subsequent entries in an absolute procedure linkage table look like
545 this. */
546
547 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
548 {
549 0xff, 0x25, /* jmp indirect */
550 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
551 0x68, /* pushl immediate */
552 0, 0, 0, 0, /* replaced with offset into relocation table. */
553 0xe9, /* jmp relative */
554 0, 0, 0, 0 /* replaced with offset to start of .plt. */
555 };
556
557 /* The first entry in a PIC procedure linkage table look like this.
558 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
559
560 static const bfd_byte elf_i386_pic_plt0_entry[12] =
561 {
562 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
563 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */
564 };
565
566 /* Subsequent entries in a PIC procedure linkage table look like this. */
567
568 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
569 {
570 0xff, 0xa3, /* jmp *offset(%ebx) */
571 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
572 0x68, /* pushl immediate */
573 0, 0, 0, 0, /* replaced with offset into relocation table. */
574 0xe9, /* jmp relative */
575 0, 0, 0, 0 /* replaced with offset to start of .plt. */
576 };
577
578 /* .eh_frame covering the .plt section. */
579
580 static const bfd_byte elf_i386_eh_frame_plt[] =
581 {
582 #define PLT_CIE_LENGTH 20
583 #define PLT_FDE_LENGTH 36
584 #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
585 #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
586 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
587 0, 0, 0, 0, /* CIE ID */
588 1, /* CIE version */
589 'z', 'R', 0, /* Augmentation string */
590 1, /* Code alignment factor */
591 0x7c, /* Data alignment factor */
592 8, /* Return address column */
593 1, /* Augmentation size */
594 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
595 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
596 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
597 DW_CFA_nop, DW_CFA_nop,
598
599 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
600 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
601 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
602 0, 0, 0, 0, /* .plt size goes here */
603 0, /* Augmentation size */
604 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
605 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
606 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
607 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
608 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
609 11, /* Block length */
610 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
611 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
612 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
613 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
614 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
615 };
616
617 struct elf_i386_plt_layout
618 {
619 /* The first entry in an absolute procedure linkage table looks like this. */
620 const bfd_byte *plt0_entry;
621 unsigned int plt0_entry_size;
622
623 /* Offsets into plt0_entry that are to be replaced with GOT[1] and GOT[2]. */
624 unsigned int plt0_got1_offset;
625 unsigned int plt0_got2_offset;
626
627 /* Later entries in an absolute procedure linkage table look like this. */
628 const bfd_byte *plt_entry;
629 unsigned int plt_entry_size;
630
631 /* Offsets into plt_entry that are to be replaced with... */
632 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
633 unsigned int plt_reloc_offset; /* ... offset into relocation table. */
634 unsigned int plt_plt_offset; /* ... offset to start of .plt. */
635
636 /* Offset into plt_entry where the initial value of the GOT entry points. */
637 unsigned int plt_lazy_offset;
638
639 /* The first entry in a PIC procedure linkage table looks like this. */
640 const bfd_byte *pic_plt0_entry;
641
642 /* Subsequent entries in a PIC procedure linkage table look like this. */
643 const bfd_byte *pic_plt_entry;
644
645 /* .eh_frame covering the .plt section. */
646 const bfd_byte *eh_frame_plt;
647 unsigned int eh_frame_plt_size;
648 };
649
650 #define GET_PLT_ENTRY_SIZE(abfd) \
651 get_elf_i386_backend_data (abfd)->plt->plt_entry_size
652
653 /* These are the standard parameters. */
654 static const struct elf_i386_plt_layout elf_i386_plt =
655 {
656 elf_i386_plt0_entry, /* plt0_entry */
657 sizeof (elf_i386_plt0_entry), /* plt0_entry_size */
658 2, /* plt0_got1_offset */
659 8, /* plt0_got2_offset */
660 elf_i386_plt_entry, /* plt_entry */
661 PLT_ENTRY_SIZE, /* plt_entry_size */
662 2, /* plt_got_offset */
663 7, /* plt_reloc_offset */
664 12, /* plt_plt_offset */
665 6, /* plt_lazy_offset */
666 elf_i386_pic_plt0_entry, /* pic_plt0_entry */
667 elf_i386_pic_plt_entry, /* pic_plt_entry */
668 elf_i386_eh_frame_plt, /* eh_frame_plt */
669 sizeof (elf_i386_eh_frame_plt), /* eh_frame_plt_size */
670 };
671 \f
672
673 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
674 for the PLTResolve stub and then for each PLT entry. */
675 #define PLTRESOLVE_RELOCS_SHLIB 0
676 #define PLTRESOLVE_RELOCS 2
677 #define PLT_NON_JUMP_SLOT_RELOCS 2
678
679 /* Architecture-specific backend data for i386. */
680
681 struct elf_i386_backend_data
682 {
683 /* Parameters describing PLT generation. */
684 const struct elf_i386_plt_layout *plt;
685
686 /* Value used to fill the unused bytes of the first PLT entry. */
687 bfd_byte plt0_pad_byte;
688
689 /* True if the target system is VxWorks. */
690 int is_vxworks;
691 };
692
693 #define get_elf_i386_backend_data(abfd) \
694 ((const struct elf_i386_backend_data *) \
695 get_elf_backend_data (abfd)->arch_data)
696
697 /* These are the standard parameters. */
698 static const struct elf_i386_backend_data elf_i386_arch_bed =
699 {
700 &elf_i386_plt, /* plt */
701 0, /* plt0_pad_byte */
702 0, /* is_vxworks */
703 };
704
705 #define elf_backend_arch_data &elf_i386_arch_bed
706
707 /* i386 ELF linker hash entry. */
708
709 struct elf_i386_link_hash_entry
710 {
711 struct elf_link_hash_entry elf;
712
713 /* Track dynamic relocs copied for this symbol. */
714 struct elf_dyn_relocs *dyn_relocs;
715
716 #define GOT_UNKNOWN 0
717 #define GOT_NORMAL 1
718 #define GOT_TLS_GD 2
719 #define GOT_TLS_IE 4
720 #define GOT_TLS_IE_POS 5
721 #define GOT_TLS_IE_NEG 6
722 #define GOT_TLS_IE_BOTH 7
723 #define GOT_TLS_GDESC 8
724 #define GOT_TLS_GD_BOTH_P(type) \
725 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
726 #define GOT_TLS_GD_P(type) \
727 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
728 #define GOT_TLS_GDESC_P(type) \
729 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
730 #define GOT_TLS_GD_ANY_P(type) \
731 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
732 unsigned char tls_type;
733
734 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
735 starting at the end of the jump table. */
736 bfd_vma tlsdesc_got;
737 };
738
739 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
740
741 struct elf_i386_obj_tdata
742 {
743 struct elf_obj_tdata root;
744
745 /* tls_type for each local got entry. */
746 char *local_got_tls_type;
747
748 /* GOTPLT entries for TLS descriptors. */
749 bfd_vma *local_tlsdesc_gotent;
750 };
751
752 #define elf_i386_tdata(abfd) \
753 ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
754
755 #define elf_i386_local_got_tls_type(abfd) \
756 (elf_i386_tdata (abfd)->local_got_tls_type)
757
758 #define elf_i386_local_tlsdesc_gotent(abfd) \
759 (elf_i386_tdata (abfd)->local_tlsdesc_gotent)
760
761 #define is_i386_elf(bfd) \
762 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
763 && elf_tdata (bfd) != NULL \
764 && elf_object_id (bfd) == I386_ELF_DATA)
765
766 static bfd_boolean
767 elf_i386_mkobject (bfd *abfd)
768 {
769 return bfd_elf_allocate_object (abfd, sizeof (struct elf_i386_obj_tdata),
770 I386_ELF_DATA);
771 }
772
773 /* i386 ELF linker hash table. */
774
775 struct elf_i386_link_hash_table
776 {
777 struct elf_link_hash_table elf;
778
779 /* Short-cuts to get to dynamic linker sections. */
780 asection *sdynbss;
781 asection *srelbss;
782 asection *plt_eh_frame;
783
784 union
785 {
786 bfd_signed_vma refcount;
787 bfd_vma offset;
788 } tls_ldm_got;
789
790 /* The amount of space used by the reserved portion of the sgotplt
791 section, plus whatever space is used by the jump slots. */
792 bfd_vma sgotplt_jump_table_size;
793
794 /* Small local sym cache. */
795 struct sym_cache sym_cache;
796
797 /* _TLS_MODULE_BASE_ symbol. */
798 struct bfd_link_hash_entry *tls_module_base;
799
800 /* Used by local STT_GNU_IFUNC symbols. */
801 htab_t loc_hash_table;
802 void * loc_hash_memory;
803
804 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks. */
805 asection *srelplt2;
806
807 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. */
808 bfd_vma next_tls_desc_index;
809
810 /* The index of the next unused R_386_JUMP_SLOT slot in .rel.plt. */
811 bfd_vma next_jump_slot_index;
812
813 /* The index of the next unused R_386_IRELATIVE slot in .rel.plt. */
814 bfd_vma next_irelative_index;
815 };
816
817 /* Get the i386 ELF linker hash table from a link_info structure. */
818
819 #define elf_i386_hash_table(p) \
820 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
821 == I386_ELF_DATA ? ((struct elf_i386_link_hash_table *) ((p)->hash)) : NULL)
822
823 #define elf_i386_compute_jump_table_size(htab) \
824 ((htab)->next_tls_desc_index * 4)
825
826 /* Create an entry in an i386 ELF linker hash table. */
827
828 static struct bfd_hash_entry *
829 elf_i386_link_hash_newfunc (struct bfd_hash_entry *entry,
830 struct bfd_hash_table *table,
831 const char *string)
832 {
833 /* Allocate the structure if it has not already been allocated by a
834 subclass. */
835 if (entry == NULL)
836 {
837 entry = (struct bfd_hash_entry *)
838 bfd_hash_allocate (table, sizeof (struct elf_i386_link_hash_entry));
839 if (entry == NULL)
840 return entry;
841 }
842
843 /* Call the allocation method of the superclass. */
844 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
845 if (entry != NULL)
846 {
847 struct elf_i386_link_hash_entry *eh;
848
849 eh = (struct elf_i386_link_hash_entry *) entry;
850 eh->dyn_relocs = NULL;
851 eh->tls_type = GOT_UNKNOWN;
852 eh->tlsdesc_got = (bfd_vma) -1;
853 }
854
855 return entry;
856 }
857
858 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
859 for local symbol so that we can handle local STT_GNU_IFUNC symbols
860 as global symbol. We reuse indx and dynstr_index for local symbol
861 hash since they aren't used by global symbols in this backend. */
862
863 static hashval_t
864 elf_i386_local_htab_hash (const void *ptr)
865 {
866 struct elf_link_hash_entry *h
867 = (struct elf_link_hash_entry *) ptr;
868 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
869 }
870
871 /* Compare local hash entries. */
872
873 static int
874 elf_i386_local_htab_eq (const void *ptr1, const void *ptr2)
875 {
876 struct elf_link_hash_entry *h1
877 = (struct elf_link_hash_entry *) ptr1;
878 struct elf_link_hash_entry *h2
879 = (struct elf_link_hash_entry *) ptr2;
880
881 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
882 }
883
884 /* Find and/or create a hash entry for local symbol. */
885
886 static struct elf_link_hash_entry *
887 elf_i386_get_local_sym_hash (struct elf_i386_link_hash_table *htab,
888 bfd *abfd, const Elf_Internal_Rela *rel,
889 bfd_boolean create)
890 {
891 struct elf_i386_link_hash_entry e, *ret;
892 asection *sec = abfd->sections;
893 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
894 ELF32_R_SYM (rel->r_info));
895 void **slot;
896
897 e.elf.indx = sec->id;
898 e.elf.dynstr_index = ELF32_R_SYM (rel->r_info);
899 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
900 create ? INSERT : NO_INSERT);
901
902 if (!slot)
903 return NULL;
904
905 if (*slot)
906 {
907 ret = (struct elf_i386_link_hash_entry *) *slot;
908 return &ret->elf;
909 }
910
911 ret = (struct elf_i386_link_hash_entry *)
912 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
913 sizeof (struct elf_i386_link_hash_entry));
914 if (ret)
915 {
916 memset (ret, 0, sizeof (*ret));
917 ret->elf.indx = sec->id;
918 ret->elf.dynstr_index = ELF32_R_SYM (rel->r_info);
919 ret->elf.dynindx = -1;
920 *slot = ret;
921 }
922 return &ret->elf;
923 }
924
925 /* Create an i386 ELF linker hash table. */
926
927 static struct bfd_link_hash_table *
928 elf_i386_link_hash_table_create (bfd *abfd)
929 {
930 struct elf_i386_link_hash_table *ret;
931 bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
932
933 ret = (struct elf_i386_link_hash_table *) bfd_malloc (amt);
934 if (ret == NULL)
935 return NULL;
936
937 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
938 elf_i386_link_hash_newfunc,
939 sizeof (struct elf_i386_link_hash_entry),
940 I386_ELF_DATA))
941 {
942 free (ret);
943 return NULL;
944 }
945
946 ret->sdynbss = NULL;
947 ret->srelbss = NULL;
948 ret->plt_eh_frame = NULL;
949 ret->tls_ldm_got.refcount = 0;
950 ret->next_tls_desc_index = 0;
951 ret->sgotplt_jump_table_size = 0;
952 ret->sym_cache.abfd = NULL;
953 ret->srelplt2 = NULL;
954 ret->tls_module_base = NULL;
955 ret->next_jump_slot_index = 0;
956 ret->next_irelative_index = 0;
957
958 ret->loc_hash_table = htab_try_create (1024,
959 elf_i386_local_htab_hash,
960 elf_i386_local_htab_eq,
961 NULL);
962 ret->loc_hash_memory = objalloc_create ();
963 if (!ret->loc_hash_table || !ret->loc_hash_memory)
964 {
965 free (ret);
966 return NULL;
967 }
968
969 return &ret->elf.root;
970 }
971
972 /* Destroy an i386 ELF linker hash table. */
973
974 static void
975 elf_i386_link_hash_table_free (struct bfd_link_hash_table *hash)
976 {
977 struct elf_i386_link_hash_table *htab
978 = (struct elf_i386_link_hash_table *) hash;
979
980 if (htab->loc_hash_table)
981 htab_delete (htab->loc_hash_table);
982 if (htab->loc_hash_memory)
983 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
984 _bfd_generic_link_hash_table_free (hash);
985 }
986
987 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
988 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
989 hash table. */
990
991 static bfd_boolean
992 elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
993 {
994 struct elf_i386_link_hash_table *htab;
995
996 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
997 return FALSE;
998
999 htab = elf_i386_hash_table (info);
1000 if (htab == NULL)
1001 return FALSE;
1002
1003 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
1004 if (!info->shared)
1005 htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
1006
1007 if (!htab->sdynbss
1008 || (!info->shared && !htab->srelbss))
1009 abort ();
1010
1011 if (get_elf_i386_backend_data (dynobj)->is_vxworks
1012 && !elf_vxworks_create_dynamic_sections (dynobj, info,
1013 &htab->srelplt2))
1014 return FALSE;
1015
1016 if (!info->no_ld_generated_unwind_info
1017 && bfd_get_section_by_name (dynobj, ".eh_frame") == NULL
1018 && htab->elf.splt != NULL)
1019 {
1020 flagword flags = get_elf_backend_data (dynobj)->dynamic_sec_flags;
1021 htab->plt_eh_frame
1022 = bfd_make_section_with_flags (dynobj, ".eh_frame",
1023 flags | SEC_READONLY);
1024 if (htab->plt_eh_frame == NULL
1025 || !bfd_set_section_alignment (dynobj, htab->plt_eh_frame, 2))
1026 return FALSE;
1027
1028 htab->plt_eh_frame->size = sizeof (elf_i386_eh_frame_plt);
1029 htab->plt_eh_frame->contents
1030 = bfd_alloc (dynobj, htab->plt_eh_frame->size);
1031 memcpy (htab->plt_eh_frame->contents, elf_i386_eh_frame_plt,
1032 sizeof (elf_i386_eh_frame_plt));
1033 }
1034
1035 return TRUE;
1036 }
1037
1038 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1039
1040 static void
1041 elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
1042 struct elf_link_hash_entry *dir,
1043 struct elf_link_hash_entry *ind)
1044 {
1045 struct elf_i386_link_hash_entry *edir, *eind;
1046
1047 edir = (struct elf_i386_link_hash_entry *) dir;
1048 eind = (struct elf_i386_link_hash_entry *) ind;
1049
1050 if (eind->dyn_relocs != NULL)
1051 {
1052 if (edir->dyn_relocs != NULL)
1053 {
1054 struct elf_dyn_relocs **pp;
1055 struct elf_dyn_relocs *p;
1056
1057 /* Add reloc counts against the indirect sym to the direct sym
1058 list. Merge any entries against the same section. */
1059 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1060 {
1061 struct elf_dyn_relocs *q;
1062
1063 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1064 if (q->sec == p->sec)
1065 {
1066 q->pc_count += p->pc_count;
1067 q->count += p->count;
1068 *pp = p->next;
1069 break;
1070 }
1071 if (q == NULL)
1072 pp = &p->next;
1073 }
1074 *pp = edir->dyn_relocs;
1075 }
1076
1077 edir->dyn_relocs = eind->dyn_relocs;
1078 eind->dyn_relocs = NULL;
1079 }
1080
1081 if (ind->root.type == bfd_link_hash_indirect
1082 && dir->got.refcount <= 0)
1083 {
1084 edir->tls_type = eind->tls_type;
1085 eind->tls_type = GOT_UNKNOWN;
1086 }
1087
1088 if (ELIMINATE_COPY_RELOCS
1089 && ind->root.type != bfd_link_hash_indirect
1090 && dir->dynamic_adjusted)
1091 {
1092 /* If called to transfer flags for a weakdef during processing
1093 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1094 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1095 dir->ref_dynamic |= ind->ref_dynamic;
1096 dir->ref_regular |= ind->ref_regular;
1097 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1098 dir->needs_plt |= ind->needs_plt;
1099 dir->pointer_equality_needed |= ind->pointer_equality_needed;
1100 }
1101 else
1102 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1103 }
1104
1105 /* Return TRUE if the TLS access code sequence support transition
1106 from R_TYPE. */
1107
1108 static bfd_boolean
1109 elf_i386_check_tls_transition (bfd *abfd, asection *sec,
1110 bfd_byte *contents,
1111 Elf_Internal_Shdr *symtab_hdr,
1112 struct elf_link_hash_entry **sym_hashes,
1113 unsigned int r_type,
1114 const Elf_Internal_Rela *rel,
1115 const Elf_Internal_Rela *relend)
1116 {
1117 unsigned int val, type;
1118 unsigned long r_symndx;
1119 struct elf_link_hash_entry *h;
1120 bfd_vma offset;
1121
1122 /* Get the section contents. */
1123 if (contents == NULL)
1124 {
1125 if (elf_section_data (sec)->this_hdr.contents != NULL)
1126 contents = elf_section_data (sec)->this_hdr.contents;
1127 else
1128 {
1129 /* FIXME: How to better handle error condition? */
1130 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1131 return FALSE;
1132
1133 /* Cache the section contents for elf_link_input_bfd. */
1134 elf_section_data (sec)->this_hdr.contents = contents;
1135 }
1136 }
1137
1138 offset = rel->r_offset;
1139 switch (r_type)
1140 {
1141 case R_386_TLS_GD:
1142 case R_386_TLS_LDM:
1143 if (offset < 2 || (rel + 1) >= relend)
1144 return FALSE;
1145
1146 type = bfd_get_8 (abfd, contents + offset - 2);
1147 if (r_type == R_386_TLS_GD)
1148 {
1149 /* Check transition from GD access model. Only
1150 leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr
1151 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop
1152 can transit to different access model. */
1153 if ((offset + 10) > sec->size ||
1154 (type != 0x8d && type != 0x04))
1155 return FALSE;
1156
1157 val = bfd_get_8 (abfd, contents + offset - 1);
1158 if (type == 0x04)
1159 {
1160 /* leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr */
1161 if (offset < 3)
1162 return FALSE;
1163
1164 if (bfd_get_8 (abfd, contents + offset - 3) != 0x8d)
1165 return FALSE;
1166
1167 if ((val & 0xc7) != 0x05 || val == (4 << 3))
1168 return FALSE;
1169 }
1170 else
1171 {
1172 /* leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop */
1173 if ((val & 0xf8) != 0x80 || (val & 7) == 4)
1174 return FALSE;
1175
1176 if (bfd_get_8 (abfd, contents + offset + 9) != 0x90)
1177 return FALSE;
1178 }
1179 }
1180 else
1181 {
1182 /* Check transition from LD access model. Only
1183 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr
1184 can transit to different access model. */
1185 if (type != 0x8d || (offset + 9) > sec->size)
1186 return FALSE;
1187
1188 val = bfd_get_8 (abfd, contents + offset - 1);
1189 if ((val & 0xf8) != 0x80 || (val & 7) == 4)
1190 return FALSE;
1191 }
1192
1193 if (bfd_get_8 (abfd, contents + offset + 4) != 0xe8)
1194 return FALSE;
1195
1196 r_symndx = ELF32_R_SYM (rel[1].r_info);
1197 if (r_symndx < symtab_hdr->sh_info)
1198 return FALSE;
1199
1200 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1201 /* Use strncmp to check ___tls_get_addr since ___tls_get_addr
1202 may be versioned. */
1203 return (h != NULL
1204 && h->root.root.string != NULL
1205 && (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
1206 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32)
1207 && (strncmp (h->root.root.string, "___tls_get_addr",
1208 15) == 0));
1209
1210 case R_386_TLS_IE:
1211 /* Check transition from IE access model:
1212 movl foo@indntpoff(%rip), %eax
1213 movl foo@indntpoff(%rip), %reg
1214 addl foo@indntpoff(%rip), %reg
1215 */
1216
1217 if (offset < 1 || (offset + 4) > sec->size)
1218 return FALSE;
1219
1220 /* Check "movl foo@tpoff(%rip), %eax" first. */
1221 val = bfd_get_8 (abfd, contents + offset - 1);
1222 if (val == 0xa1)
1223 return TRUE;
1224
1225 if (offset < 2)
1226 return FALSE;
1227
1228 /* Check movl|addl foo@tpoff(%rip), %reg. */
1229 type = bfd_get_8 (abfd, contents + offset - 2);
1230 return ((type == 0x8b || type == 0x03)
1231 && (val & 0xc7) == 0x05);
1232
1233 case R_386_TLS_GOTIE:
1234 case R_386_TLS_IE_32:
1235 /* Check transition from {IE_32,GOTIE} access model:
1236 subl foo@{tpoff,gontoff}(%reg1), %reg2
1237 movl foo@{tpoff,gontoff}(%reg1), %reg2
1238 addl foo@{tpoff,gontoff}(%reg1), %reg2
1239 */
1240
1241 if (offset < 2 || (offset + 4) > sec->size)
1242 return FALSE;
1243
1244 val = bfd_get_8 (abfd, contents + offset - 1);
1245 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1246 return FALSE;
1247
1248 type = bfd_get_8 (abfd, contents + offset - 2);
1249 return type == 0x8b || type == 0x2b || type == 0x03;
1250
1251 case R_386_TLS_GOTDESC:
1252 /* Check transition from GDesc access model:
1253 leal x@tlsdesc(%ebx), %eax
1254
1255 Make sure it's a leal adding ebx to a 32-bit offset
1256 into any register, although it's probably almost always
1257 going to be eax. */
1258
1259 if (offset < 2 || (offset + 4) > sec->size)
1260 return FALSE;
1261
1262 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1263 return FALSE;
1264
1265 val = bfd_get_8 (abfd, contents + offset - 1);
1266 return (val & 0xc7) == 0x83;
1267
1268 case R_386_TLS_DESC_CALL:
1269 /* Check transition from GDesc access model:
1270 call *x@tlsdesc(%rax)
1271 */
1272 if (offset + 2 <= sec->size)
1273 {
1274 /* Make sure that it's a call *x@tlsdesc(%rax). */
1275 static const unsigned char call[] = { 0xff, 0x10 };
1276 return memcmp (contents + offset, call, 2) == 0;
1277 }
1278
1279 return FALSE;
1280
1281 default:
1282 abort ();
1283 }
1284 }
1285
1286 /* Return TRUE if the TLS access transition is OK or no transition
1287 will be performed. Update R_TYPE if there is a transition. */
1288
1289 static bfd_boolean
1290 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1291 asection *sec, bfd_byte *contents,
1292 Elf_Internal_Shdr *symtab_hdr,
1293 struct elf_link_hash_entry **sym_hashes,
1294 unsigned int *r_type, int tls_type,
1295 const Elf_Internal_Rela *rel,
1296 const Elf_Internal_Rela *relend,
1297 struct elf_link_hash_entry *h,
1298 unsigned long r_symndx)
1299 {
1300 unsigned int from_type = *r_type;
1301 unsigned int to_type = from_type;
1302 bfd_boolean check = TRUE;
1303
1304 /* Skip TLS transition for functions. */
1305 if (h != NULL
1306 && (h->type == STT_FUNC
1307 || h->type == STT_GNU_IFUNC))
1308 return TRUE;
1309
1310 switch (from_type)
1311 {
1312 case R_386_TLS_GD:
1313 case R_386_TLS_GOTDESC:
1314 case R_386_TLS_DESC_CALL:
1315 case R_386_TLS_IE_32:
1316 case R_386_TLS_IE:
1317 case R_386_TLS_GOTIE:
1318 if (info->executable)
1319 {
1320 if (h == NULL)
1321 to_type = R_386_TLS_LE_32;
1322 else if (from_type != R_386_TLS_IE
1323 && from_type != R_386_TLS_GOTIE)
1324 to_type = R_386_TLS_IE_32;
1325 }
1326
1327 /* When we are called from elf_i386_relocate_section, CONTENTS
1328 isn't NULL and there may be additional transitions based on
1329 TLS_TYPE. */
1330 if (contents != NULL)
1331 {
1332 unsigned int new_to_type = to_type;
1333
1334 if (info->executable
1335 && h != NULL
1336 && h->dynindx == -1
1337 && (tls_type & GOT_TLS_IE))
1338 new_to_type = R_386_TLS_LE_32;
1339
1340 if (to_type == R_386_TLS_GD
1341 || to_type == R_386_TLS_GOTDESC
1342 || to_type == R_386_TLS_DESC_CALL)
1343 {
1344 if (tls_type == GOT_TLS_IE_POS)
1345 new_to_type = R_386_TLS_GOTIE;
1346 else if (tls_type & GOT_TLS_IE)
1347 new_to_type = R_386_TLS_IE_32;
1348 }
1349
1350 /* We checked the transition before when we were called from
1351 elf_i386_check_relocs. We only want to check the new
1352 transition which hasn't been checked before. */
1353 check = new_to_type != to_type && from_type == to_type;
1354 to_type = new_to_type;
1355 }
1356
1357 break;
1358
1359 case R_386_TLS_LDM:
1360 if (info->executable)
1361 to_type = R_386_TLS_LE_32;
1362 break;
1363
1364 default:
1365 return TRUE;
1366 }
1367
1368 /* Return TRUE if there is no transition. */
1369 if (from_type == to_type)
1370 return TRUE;
1371
1372 /* Check if the transition can be performed. */
1373 if (check
1374 && ! elf_i386_check_tls_transition (abfd, sec, contents,
1375 symtab_hdr, sym_hashes,
1376 from_type, rel, relend))
1377 {
1378 reloc_howto_type *from, *to;
1379 const char *name;
1380
1381 from = elf_i386_rtype_to_howto (abfd, from_type);
1382 to = elf_i386_rtype_to_howto (abfd, to_type);
1383
1384 if (h)
1385 name = h->root.root.string;
1386 else
1387 {
1388 struct elf_i386_link_hash_table *htab;
1389
1390 htab = elf_i386_hash_table (info);
1391 if (htab == NULL)
1392 name = "*unknown*";
1393 else
1394 {
1395 Elf_Internal_Sym *isym;
1396
1397 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1398 abfd, r_symndx);
1399 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1400 }
1401 }
1402
1403 (*_bfd_error_handler)
1404 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1405 "in section `%A' failed"),
1406 abfd, sec, from->name, to->name, name,
1407 (unsigned long) rel->r_offset);
1408 bfd_set_error (bfd_error_bad_value);
1409 return FALSE;
1410 }
1411
1412 *r_type = to_type;
1413 return TRUE;
1414 }
1415
1416 /* Look through the relocs for a section during the first phase, and
1417 calculate needed space in the global offset table, procedure linkage
1418 table, and dynamic reloc sections. */
1419
1420 static bfd_boolean
1421 elf_i386_check_relocs (bfd *abfd,
1422 struct bfd_link_info *info,
1423 asection *sec,
1424 const Elf_Internal_Rela *relocs)
1425 {
1426 struct elf_i386_link_hash_table *htab;
1427 Elf_Internal_Shdr *symtab_hdr;
1428 struct elf_link_hash_entry **sym_hashes;
1429 const Elf_Internal_Rela *rel;
1430 const Elf_Internal_Rela *rel_end;
1431 asection *sreloc;
1432
1433 if (info->relocatable)
1434 return TRUE;
1435
1436 BFD_ASSERT (is_i386_elf (abfd));
1437
1438 htab = elf_i386_hash_table (info);
1439 if (htab == NULL)
1440 return FALSE;
1441
1442 symtab_hdr = &elf_symtab_hdr (abfd);
1443 sym_hashes = elf_sym_hashes (abfd);
1444
1445 sreloc = NULL;
1446
1447 rel_end = relocs + sec->reloc_count;
1448 for (rel = relocs; rel < rel_end; rel++)
1449 {
1450 unsigned int r_type;
1451 unsigned long r_symndx;
1452 struct elf_link_hash_entry *h;
1453 Elf_Internal_Sym *isym;
1454 const char *name;
1455
1456 r_symndx = ELF32_R_SYM (rel->r_info);
1457 r_type = ELF32_R_TYPE (rel->r_info);
1458
1459 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1460 {
1461 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1462 abfd,
1463 r_symndx);
1464 return FALSE;
1465 }
1466
1467 if (r_symndx < symtab_hdr->sh_info)
1468 {
1469 /* A local symbol. */
1470 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1471 abfd, r_symndx);
1472 if (isym == NULL)
1473 return FALSE;
1474
1475 /* Check relocation against local STT_GNU_IFUNC symbol. */
1476 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1477 {
1478 h = elf_i386_get_local_sym_hash (htab, abfd, rel, TRUE);
1479 if (h == NULL)
1480 return FALSE;
1481
1482 /* Fake a STT_GNU_IFUNC symbol. */
1483 h->type = STT_GNU_IFUNC;
1484 h->def_regular = 1;
1485 h->ref_regular = 1;
1486 h->forced_local = 1;
1487 h->root.type = bfd_link_hash_defined;
1488 }
1489 else
1490 h = NULL;
1491 }
1492 else
1493 {
1494 isym = NULL;
1495 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1496 while (h->root.type == bfd_link_hash_indirect
1497 || h->root.type == bfd_link_hash_warning)
1498 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1499 }
1500
1501 if (h != NULL)
1502 {
1503 /* Create the ifunc sections for static executables. If we
1504 never see an indirect function symbol nor we are building
1505 a static executable, those sections will be empty and
1506 won't appear in output. */
1507 switch (r_type)
1508 {
1509 default:
1510 break;
1511
1512 case R_386_32:
1513 case R_386_PC32:
1514 case R_386_PLT32:
1515 case R_386_GOT32:
1516 case R_386_GOTOFF:
1517 if (htab->elf.dynobj == NULL)
1518 htab->elf.dynobj = abfd;
1519 if (!_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
1520 return FALSE;
1521 break;
1522 }
1523
1524 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1525 it here if it is defined in a non-shared object. */
1526 if (h->type == STT_GNU_IFUNC
1527 && h->def_regular)
1528 {
1529 /* It is referenced by a non-shared object. */
1530 h->ref_regular = 1;
1531 h->needs_plt = 1;
1532
1533 /* STT_GNU_IFUNC symbol must go through PLT. */
1534 h->plt.refcount += 1;
1535
1536 /* STT_GNU_IFUNC needs dynamic sections. */
1537 if (htab->elf.dynobj == NULL)
1538 htab->elf.dynobj = abfd;
1539
1540 switch (r_type)
1541 {
1542 default:
1543 if (h->root.root.string)
1544 name = h->root.root.string;
1545 else
1546 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1547 NULL);
1548 (*_bfd_error_handler)
1549 (_("%B: relocation %s against STT_GNU_IFUNC "
1550 "symbol `%s' isn't handled by %s"), abfd,
1551 elf_howto_table[r_type].name,
1552 name, __FUNCTION__);
1553 bfd_set_error (bfd_error_bad_value);
1554 return FALSE;
1555
1556 case R_386_32:
1557 h->non_got_ref = 1;
1558 h->pointer_equality_needed = 1;
1559 if (info->shared)
1560 {
1561 /* We must copy these reloc types into the
1562 output file. Create a reloc section in
1563 dynobj and make room for this reloc. */
1564 sreloc = _bfd_elf_create_ifunc_dyn_reloc
1565 (abfd, info, sec, sreloc,
1566 &((struct elf_i386_link_hash_entry *) h)->dyn_relocs);
1567 if (sreloc == NULL)
1568 return FALSE;
1569 }
1570 break;
1571
1572 case R_386_PC32:
1573 h->non_got_ref = 1;
1574 break;
1575
1576 case R_386_PLT32:
1577 break;
1578
1579 case R_386_GOT32:
1580 case R_386_GOTOFF:
1581 h->got.refcount += 1;
1582 if (htab->elf.sgot == NULL
1583 && !_bfd_elf_create_got_section (htab->elf.dynobj,
1584 info))
1585 return FALSE;
1586 break;
1587 }
1588
1589 continue;
1590 }
1591 }
1592
1593 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1594 symtab_hdr, sym_hashes,
1595 &r_type, GOT_UNKNOWN,
1596 rel, rel_end, h, r_symndx))
1597 return FALSE;
1598
1599 switch (r_type)
1600 {
1601 case R_386_TLS_LDM:
1602 htab->tls_ldm_got.refcount += 1;
1603 goto create_got;
1604
1605 case R_386_PLT32:
1606 /* This symbol requires a procedure linkage table entry. We
1607 actually build the entry in adjust_dynamic_symbol,
1608 because this might be a case of linking PIC code which is
1609 never referenced by a dynamic object, in which case we
1610 don't need to generate a procedure linkage table entry
1611 after all. */
1612
1613 /* If this is a local symbol, we resolve it directly without
1614 creating a procedure linkage table entry. */
1615 if (h == NULL)
1616 continue;
1617
1618 h->needs_plt = 1;
1619 h->plt.refcount += 1;
1620 break;
1621
1622 case R_386_TLS_IE_32:
1623 case R_386_TLS_IE:
1624 case R_386_TLS_GOTIE:
1625 if (!info->executable)
1626 info->flags |= DF_STATIC_TLS;
1627 /* Fall through */
1628
1629 case R_386_GOT32:
1630 case R_386_TLS_GD:
1631 case R_386_TLS_GOTDESC:
1632 case R_386_TLS_DESC_CALL:
1633 /* This symbol requires a global offset table entry. */
1634 {
1635 int tls_type, old_tls_type;
1636
1637 switch (r_type)
1638 {
1639 default:
1640 case R_386_GOT32: tls_type = GOT_NORMAL; break;
1641 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1642 case R_386_TLS_GOTDESC:
1643 case R_386_TLS_DESC_CALL:
1644 tls_type = GOT_TLS_GDESC; break;
1645 case R_386_TLS_IE_32:
1646 if (ELF32_R_TYPE (rel->r_info) == r_type)
1647 tls_type = GOT_TLS_IE_NEG;
1648 else
1649 /* If this is a GD->IE transition, we may use either of
1650 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
1651 tls_type = GOT_TLS_IE;
1652 break;
1653 case R_386_TLS_IE:
1654 case R_386_TLS_GOTIE:
1655 tls_type = GOT_TLS_IE_POS; break;
1656 }
1657
1658 if (h != NULL)
1659 {
1660 h->got.refcount += 1;
1661 old_tls_type = elf_i386_hash_entry(h)->tls_type;
1662 }
1663 else
1664 {
1665 bfd_signed_vma *local_got_refcounts;
1666
1667 /* This is a global offset table entry for a local symbol. */
1668 local_got_refcounts = elf_local_got_refcounts (abfd);
1669 if (local_got_refcounts == NULL)
1670 {
1671 bfd_size_type size;
1672
1673 size = symtab_hdr->sh_info;
1674 size *= (sizeof (bfd_signed_vma)
1675 + sizeof (bfd_vma) + sizeof(char));
1676 local_got_refcounts = (bfd_signed_vma *)
1677 bfd_zalloc (abfd, size);
1678 if (local_got_refcounts == NULL)
1679 return FALSE;
1680 elf_local_got_refcounts (abfd) = local_got_refcounts;
1681 elf_i386_local_tlsdesc_gotent (abfd)
1682 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1683 elf_i386_local_got_tls_type (abfd)
1684 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1685 }
1686 local_got_refcounts[r_symndx] += 1;
1687 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
1688 }
1689
1690 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1691 tls_type |= old_tls_type;
1692 /* If a TLS symbol is accessed using IE at least once,
1693 there is no point to use dynamic model for it. */
1694 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1695 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1696 || (tls_type & GOT_TLS_IE) == 0))
1697 {
1698 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1699 tls_type = old_tls_type;
1700 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1701 && GOT_TLS_GD_ANY_P (tls_type))
1702 tls_type |= old_tls_type;
1703 else
1704 {
1705 if (h)
1706 name = h->root.root.string;
1707 else
1708 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1709 NULL);
1710 (*_bfd_error_handler)
1711 (_("%B: `%s' accessed both as normal and "
1712 "thread local symbol"),
1713 abfd, name);
1714 return FALSE;
1715 }
1716 }
1717
1718 if (old_tls_type != tls_type)
1719 {
1720 if (h != NULL)
1721 elf_i386_hash_entry (h)->tls_type = tls_type;
1722 else
1723 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
1724 }
1725 }
1726 /* Fall through */
1727
1728 case R_386_GOTOFF:
1729 case R_386_GOTPC:
1730 create_got:
1731 if (htab->elf.sgot == NULL)
1732 {
1733 if (htab->elf.dynobj == NULL)
1734 htab->elf.dynobj = abfd;
1735 if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
1736 return FALSE;
1737 }
1738 if (r_type != R_386_TLS_IE)
1739 break;
1740 /* Fall through */
1741
1742 case R_386_TLS_LE_32:
1743 case R_386_TLS_LE:
1744 if (info->executable)
1745 break;
1746 info->flags |= DF_STATIC_TLS;
1747 /* Fall through */
1748
1749 case R_386_32:
1750 case R_386_PC32:
1751 if (h != NULL && info->executable)
1752 {
1753 /* If this reloc is in a read-only section, we might
1754 need a copy reloc. We can't check reliably at this
1755 stage whether the section is read-only, as input
1756 sections have not yet been mapped to output sections.
1757 Tentatively set the flag for now, and correct in
1758 adjust_dynamic_symbol. */
1759 h->non_got_ref = 1;
1760
1761 /* We may need a .plt entry if the function this reloc
1762 refers to is in a shared lib. */
1763 h->plt.refcount += 1;
1764 if (r_type != R_386_PC32)
1765 h->pointer_equality_needed = 1;
1766 }
1767
1768 /* If we are creating a shared library, and this is a reloc
1769 against a global symbol, or a non PC relative reloc
1770 against a local symbol, then we need to copy the reloc
1771 into the shared library. However, if we are linking with
1772 -Bsymbolic, we do not need to copy a reloc against a
1773 global symbol which is defined in an object we are
1774 including in the link (i.e., DEF_REGULAR is set). At
1775 this point we have not seen all the input files, so it is
1776 possible that DEF_REGULAR is not set now but will be set
1777 later (it is never cleared). In case of a weak definition,
1778 DEF_REGULAR may be cleared later by a strong definition in
1779 a shared library. We account for that possibility below by
1780 storing information in the relocs_copied field of the hash
1781 table entry. A similar situation occurs when creating
1782 shared libraries and symbol visibility changes render the
1783 symbol local.
1784
1785 If on the other hand, we are creating an executable, we
1786 may need to keep relocations for symbols satisfied by a
1787 dynamic library if we manage to avoid copy relocs for the
1788 symbol. */
1789 if ((info->shared
1790 && (sec->flags & SEC_ALLOC) != 0
1791 && (r_type != R_386_PC32
1792 || (h != NULL
1793 && (! SYMBOLIC_BIND (info, h)
1794 || h->root.type == bfd_link_hash_defweak
1795 || !h->def_regular))))
1796 || (ELIMINATE_COPY_RELOCS
1797 && !info->shared
1798 && (sec->flags & SEC_ALLOC) != 0
1799 && h != NULL
1800 && (h->root.type == bfd_link_hash_defweak
1801 || !h->def_regular)))
1802 {
1803 struct elf_dyn_relocs *p;
1804 struct elf_dyn_relocs **head;
1805
1806 /* We must copy these reloc types into the output file.
1807 Create a reloc section in dynobj and make room for
1808 this reloc. */
1809 if (sreloc == NULL)
1810 {
1811 if (htab->elf.dynobj == NULL)
1812 htab->elf.dynobj = abfd;
1813
1814 sreloc = _bfd_elf_make_dynamic_reloc_section
1815 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
1816
1817 if (sreloc == NULL)
1818 return FALSE;
1819 }
1820
1821 /* If this is a global symbol, we count the number of
1822 relocations we need for this symbol. */
1823 if (h != NULL)
1824 {
1825 head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
1826 }
1827 else
1828 {
1829 /* Track dynamic relocs needed for local syms too.
1830 We really need local syms available to do this
1831 easily. Oh well. */
1832 void **vpp;
1833 asection *s;
1834
1835 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1836 abfd, r_symndx);
1837 if (isym == NULL)
1838 return FALSE;
1839
1840 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1841 if (s == NULL)
1842 s = sec;
1843
1844 vpp = &elf_section_data (s)->local_dynrel;
1845 head = (struct elf_dyn_relocs **)vpp;
1846 }
1847
1848 p = *head;
1849 if (p == NULL || p->sec != sec)
1850 {
1851 bfd_size_type amt = sizeof *p;
1852 p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
1853 amt);
1854 if (p == NULL)
1855 return FALSE;
1856 p->next = *head;
1857 *head = p;
1858 p->sec = sec;
1859 p->count = 0;
1860 p->pc_count = 0;
1861 }
1862
1863 p->count += 1;
1864 if (r_type == R_386_PC32)
1865 p->pc_count += 1;
1866 }
1867 break;
1868
1869 /* This relocation describes the C++ object vtable hierarchy.
1870 Reconstruct it for later use during GC. */
1871 case R_386_GNU_VTINHERIT:
1872 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1873 return FALSE;
1874 break;
1875
1876 /* This relocation describes which C++ vtable entries are actually
1877 used. Record for later use during GC. */
1878 case R_386_GNU_VTENTRY:
1879 BFD_ASSERT (h != NULL);
1880 if (h != NULL
1881 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1882 return FALSE;
1883 break;
1884
1885 default:
1886 break;
1887 }
1888 }
1889
1890 return TRUE;
1891 }
1892
1893 /* Return the section that should be marked against GC for a given
1894 relocation. */
1895
1896 static asection *
1897 elf_i386_gc_mark_hook (asection *sec,
1898 struct bfd_link_info *info,
1899 Elf_Internal_Rela *rel,
1900 struct elf_link_hash_entry *h,
1901 Elf_Internal_Sym *sym)
1902 {
1903 if (h != NULL)
1904 switch (ELF32_R_TYPE (rel->r_info))
1905 {
1906 case R_386_GNU_VTINHERIT:
1907 case R_386_GNU_VTENTRY:
1908 return NULL;
1909 }
1910
1911 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1912 }
1913
1914 /* Update the got entry reference counts for the section being removed. */
1915
1916 static bfd_boolean
1917 elf_i386_gc_sweep_hook (bfd *abfd,
1918 struct bfd_link_info *info,
1919 asection *sec,
1920 const Elf_Internal_Rela *relocs)
1921 {
1922 struct elf_i386_link_hash_table *htab;
1923 Elf_Internal_Shdr *symtab_hdr;
1924 struct elf_link_hash_entry **sym_hashes;
1925 bfd_signed_vma *local_got_refcounts;
1926 const Elf_Internal_Rela *rel, *relend;
1927
1928 if (info->relocatable)
1929 return TRUE;
1930
1931 htab = elf_i386_hash_table (info);
1932 if (htab == NULL)
1933 return FALSE;
1934
1935 elf_section_data (sec)->local_dynrel = NULL;
1936
1937 symtab_hdr = &elf_symtab_hdr (abfd);
1938 sym_hashes = elf_sym_hashes (abfd);
1939 local_got_refcounts = elf_local_got_refcounts (abfd);
1940
1941 relend = relocs + sec->reloc_count;
1942 for (rel = relocs; rel < relend; rel++)
1943 {
1944 unsigned long r_symndx;
1945 unsigned int r_type;
1946 struct elf_link_hash_entry *h = NULL;
1947
1948 r_symndx = ELF32_R_SYM (rel->r_info);
1949 if (r_symndx >= symtab_hdr->sh_info)
1950 {
1951 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1952 while (h->root.type == bfd_link_hash_indirect
1953 || h->root.type == bfd_link_hash_warning)
1954 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1955 }
1956 else
1957 {
1958 /* A local symbol. */
1959 Elf_Internal_Sym *isym;
1960
1961 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1962 abfd, r_symndx);
1963
1964 /* Check relocation against local STT_GNU_IFUNC symbol. */
1965 if (isym != NULL
1966 && ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1967 {
1968 h = elf_i386_get_local_sym_hash (htab, abfd, rel, FALSE);
1969 if (h == NULL)
1970 abort ();
1971 }
1972 }
1973
1974 if (h)
1975 {
1976 struct elf_i386_link_hash_entry *eh;
1977 struct elf_dyn_relocs **pp;
1978 struct elf_dyn_relocs *p;
1979
1980 eh = (struct elf_i386_link_hash_entry *) h;
1981 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1982 if (p->sec == sec)
1983 {
1984 /* Everything must go for SEC. */
1985 *pp = p->next;
1986 break;
1987 }
1988 }
1989
1990 r_type = ELF32_R_TYPE (rel->r_info);
1991 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1992 symtab_hdr, sym_hashes,
1993 &r_type, GOT_UNKNOWN,
1994 rel, relend, h, r_symndx))
1995 return FALSE;
1996
1997 switch (r_type)
1998 {
1999 case R_386_TLS_LDM:
2000 if (htab->tls_ldm_got.refcount > 0)
2001 htab->tls_ldm_got.refcount -= 1;
2002 break;
2003
2004 case R_386_TLS_GD:
2005 case R_386_TLS_GOTDESC:
2006 case R_386_TLS_DESC_CALL:
2007 case R_386_TLS_IE_32:
2008 case R_386_TLS_IE:
2009 case R_386_TLS_GOTIE:
2010 case R_386_GOT32:
2011 if (h != NULL)
2012 {
2013 if (h->got.refcount > 0)
2014 h->got.refcount -= 1;
2015 if (h->type == STT_GNU_IFUNC)
2016 {
2017 if (h->plt.refcount > 0)
2018 h->plt.refcount -= 1;
2019 }
2020 }
2021 else if (local_got_refcounts != NULL)
2022 {
2023 if (local_got_refcounts[r_symndx] > 0)
2024 local_got_refcounts[r_symndx] -= 1;
2025 }
2026 break;
2027
2028 case R_386_32:
2029 case R_386_PC32:
2030 if (info->shared
2031 && (h == NULL || h->type != STT_GNU_IFUNC))
2032 break;
2033 /* Fall through */
2034
2035 case R_386_PLT32:
2036 if (h != NULL)
2037 {
2038 if (h->plt.refcount > 0)
2039 h->plt.refcount -= 1;
2040 }
2041 break;
2042
2043 case R_386_GOTOFF:
2044 if (h != NULL && h->type == STT_GNU_IFUNC)
2045 {
2046 if (h->got.refcount > 0)
2047 h->got.refcount -= 1;
2048 if (h->plt.refcount > 0)
2049 h->plt.refcount -= 1;
2050 }
2051 break;
2052
2053 default:
2054 break;
2055 }
2056 }
2057
2058 return TRUE;
2059 }
2060
2061 /* Adjust a symbol defined by a dynamic object and referenced by a
2062 regular object. The current definition is in some section of the
2063 dynamic object, but we're not including those sections. We have to
2064 change the definition to something the rest of the link can
2065 understand. */
2066
2067 static bfd_boolean
2068 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
2069 struct elf_link_hash_entry *h)
2070 {
2071 struct elf_i386_link_hash_table *htab;
2072 asection *s;
2073
2074 /* STT_GNU_IFUNC symbol must go through PLT. */
2075 if (h->type == STT_GNU_IFUNC)
2076 {
2077 if (h->plt.refcount <= 0)
2078 {
2079 h->plt.offset = (bfd_vma) -1;
2080 h->needs_plt = 0;
2081 }
2082 return TRUE;
2083 }
2084
2085 /* If this is a function, put it in the procedure linkage table. We
2086 will fill in the contents of the procedure linkage table later,
2087 when we know the address of the .got section. */
2088 if (h->type == STT_FUNC
2089 || h->needs_plt)
2090 {
2091 if (h->plt.refcount <= 0
2092 || SYMBOL_CALLS_LOCAL (info, h)
2093 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2094 && h->root.type == bfd_link_hash_undefweak))
2095 {
2096 /* This case can occur if we saw a PLT32 reloc in an input
2097 file, but the symbol was never referred to by a dynamic
2098 object, or if all references were garbage collected. In
2099 such a case, we don't actually need to build a procedure
2100 linkage table, and we can just do a PC32 reloc instead. */
2101 h->plt.offset = (bfd_vma) -1;
2102 h->needs_plt = 0;
2103 }
2104
2105 return TRUE;
2106 }
2107 else
2108 /* It's possible that we incorrectly decided a .plt reloc was
2109 needed for an R_386_PC32 reloc to a non-function sym in
2110 check_relocs. We can't decide accurately between function and
2111 non-function syms in check-relocs; Objects loaded later in
2112 the link may change h->type. So fix it now. */
2113 h->plt.offset = (bfd_vma) -1;
2114
2115 /* If this is a weak symbol, and there is a real definition, the
2116 processor independent code will have arranged for us to see the
2117 real definition first, and we can just use the same value. */
2118 if (h->u.weakdef != NULL)
2119 {
2120 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2121 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2122 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2123 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2124 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
2125 h->non_got_ref = h->u.weakdef->non_got_ref;
2126 return TRUE;
2127 }
2128
2129 /* This is a reference to a symbol defined by a dynamic object which
2130 is not a function. */
2131
2132 /* If we are creating a shared library, we must presume that the
2133 only references to the symbol are via the global offset table.
2134 For such cases we need not do anything here; the relocations will
2135 be handled correctly by relocate_section. */
2136 if (info->shared)
2137 return TRUE;
2138
2139 /* If there are no references to this symbol that do not use the
2140 GOT, we don't need to generate a copy reloc. */
2141 if (!h->non_got_ref)
2142 return TRUE;
2143
2144 /* If -z nocopyreloc was given, we won't generate them either. */
2145 if (info->nocopyreloc)
2146 {
2147 h->non_got_ref = 0;
2148 return TRUE;
2149 }
2150
2151 htab = elf_i386_hash_table (info);
2152 if (htab == NULL)
2153 return FALSE;
2154
2155 /* If there aren't any dynamic relocs in read-only sections, then
2156 we can keep the dynamic relocs and avoid the copy reloc. This
2157 doesn't work on VxWorks, where we can not have dynamic relocations
2158 (other than copy and jump slot relocations) in an executable. */
2159 if (ELIMINATE_COPY_RELOCS
2160 && !get_elf_i386_backend_data (info->output_bfd)->is_vxworks)
2161 {
2162 struct elf_i386_link_hash_entry * eh;
2163 struct elf_dyn_relocs *p;
2164
2165 eh = (struct elf_i386_link_hash_entry *) h;
2166 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2167 {
2168 s = p->sec->output_section;
2169 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2170 break;
2171 }
2172
2173 if (p == NULL)
2174 {
2175 h->non_got_ref = 0;
2176 return TRUE;
2177 }
2178 }
2179
2180 /* We must allocate the symbol in our .dynbss section, which will
2181 become part of the .bss section of the executable. There will be
2182 an entry for this symbol in the .dynsym section. The dynamic
2183 object will contain position independent code, so all references
2184 from the dynamic object to this symbol will go through the global
2185 offset table. The dynamic linker will use the .dynsym entry to
2186 determine the address it must put in the global offset table, so
2187 both the dynamic object and the regular object will refer to the
2188 same memory location for the variable. */
2189
2190 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
2191 copy the initial value out of the dynamic object and into the
2192 runtime process image. */
2193 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2194 {
2195 htab->srelbss->size += sizeof (Elf32_External_Rel);
2196 h->needs_copy = 1;
2197 }
2198
2199 s = htab->sdynbss;
2200
2201 return _bfd_elf_adjust_dynamic_copy (h, s);
2202 }
2203
2204 /* Allocate space in .plt, .got and associated reloc sections for
2205 dynamic relocs. */
2206
2207 static bfd_boolean
2208 elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2209 {
2210 struct bfd_link_info *info;
2211 struct elf_i386_link_hash_table *htab;
2212 struct elf_i386_link_hash_entry *eh;
2213 struct elf_dyn_relocs *p;
2214 unsigned plt_entry_size;
2215
2216 if (h->root.type == bfd_link_hash_indirect)
2217 return TRUE;
2218
2219 eh = (struct elf_i386_link_hash_entry *) h;
2220
2221 info = (struct bfd_link_info *) inf;
2222 htab = elf_i386_hash_table (info);
2223 if (htab == NULL)
2224 return FALSE;
2225
2226 plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
2227
2228 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2229 here if it is defined and referenced in a non-shared object. */
2230 if (h->type == STT_GNU_IFUNC
2231 && h->def_regular)
2232 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h, &eh->dyn_relocs,
2233 plt_entry_size, 4);
2234 else if (htab->elf.dynamic_sections_created
2235 && h->plt.refcount > 0)
2236 {
2237 /* Make sure this symbol is output as a dynamic symbol.
2238 Undefined weak syms won't yet be marked as dynamic. */
2239 if (h->dynindx == -1
2240 && !h->forced_local)
2241 {
2242 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2243 return FALSE;
2244 }
2245
2246 if (info->shared
2247 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2248 {
2249 asection *s = htab->elf.splt;
2250
2251 /* If this is the first .plt entry, make room for the special
2252 first entry. */
2253 if (s->size == 0)
2254 s->size += plt_entry_size;
2255
2256 h->plt.offset = s->size;
2257
2258 /* If this symbol is not defined in a regular file, and we are
2259 not generating a shared library, then set the symbol to this
2260 location in the .plt. This is required to make function
2261 pointers compare as equal between the normal executable and
2262 the shared library. */
2263 if (! info->shared
2264 && !h->def_regular)
2265 {
2266 h->root.u.def.section = s;
2267 h->root.u.def.value = h->plt.offset;
2268 }
2269
2270 /* Make room for this entry. */
2271 s->size += plt_entry_size;
2272
2273 /* We also need to make an entry in the .got.plt section, which
2274 will be placed in the .got section by the linker script. */
2275 htab->elf.sgotplt->size += 4;
2276
2277 /* We also need to make an entry in the .rel.plt section. */
2278 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2279 htab->elf.srelplt->reloc_count++;
2280
2281 if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks
2282 && !info->shared)
2283 {
2284 /* VxWorks has a second set of relocations for each PLT entry
2285 in executables. They go in a separate relocation section,
2286 which is processed by the kernel loader. */
2287
2288 /* There are two relocations for the initial PLT entry: an
2289 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
2290 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
2291
2292 if (h->plt.offset == plt_entry_size)
2293 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2294
2295 /* There are two extra relocations for each subsequent PLT entry:
2296 an R_386_32 relocation for the GOT entry, and an R_386_32
2297 relocation for the PLT entry. */
2298
2299 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2300 }
2301 }
2302 else
2303 {
2304 h->plt.offset = (bfd_vma) -1;
2305 h->needs_plt = 0;
2306 }
2307 }
2308 else
2309 {
2310 h->plt.offset = (bfd_vma) -1;
2311 h->needs_plt = 0;
2312 }
2313
2314 eh->tlsdesc_got = (bfd_vma) -1;
2315
2316 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
2317 make it a R_386_TLS_LE_32 requiring no TLS entry. */
2318 if (h->got.refcount > 0
2319 && info->executable
2320 && h->dynindx == -1
2321 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
2322 h->got.offset = (bfd_vma) -1;
2323 else if (h->got.refcount > 0)
2324 {
2325 asection *s;
2326 bfd_boolean dyn;
2327 int tls_type = elf_i386_hash_entry(h)->tls_type;
2328
2329 /* Make sure this symbol is output as a dynamic symbol.
2330 Undefined weak syms won't yet be marked as dynamic. */
2331 if (h->dynindx == -1
2332 && !h->forced_local)
2333 {
2334 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2335 return FALSE;
2336 }
2337
2338 s = htab->elf.sgot;
2339 if (GOT_TLS_GDESC_P (tls_type))
2340 {
2341 eh->tlsdesc_got = htab->elf.sgotplt->size
2342 - elf_i386_compute_jump_table_size (htab);
2343 htab->elf.sgotplt->size += 8;
2344 h->got.offset = (bfd_vma) -2;
2345 }
2346 if (! GOT_TLS_GDESC_P (tls_type)
2347 || GOT_TLS_GD_P (tls_type))
2348 {
2349 h->got.offset = s->size;
2350 s->size += 4;
2351 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
2352 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
2353 s->size += 4;
2354 }
2355 dyn = htab->elf.dynamic_sections_created;
2356 /* R_386_TLS_IE_32 needs one dynamic relocation,
2357 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
2358 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
2359 need two), R_386_TLS_GD needs one if local symbol and two if
2360 global. */
2361 if (tls_type == GOT_TLS_IE_BOTH)
2362 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2363 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2364 || (tls_type & GOT_TLS_IE))
2365 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2366 else if (GOT_TLS_GD_P (tls_type))
2367 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2368 else if (! GOT_TLS_GDESC_P (tls_type)
2369 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2370 || h->root.type != bfd_link_hash_undefweak)
2371 && (info->shared
2372 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2373 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2374 if (GOT_TLS_GDESC_P (tls_type))
2375 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2376 }
2377 else
2378 h->got.offset = (bfd_vma) -1;
2379
2380 if (eh->dyn_relocs == NULL)
2381 return TRUE;
2382
2383 /* In the shared -Bsymbolic case, discard space allocated for
2384 dynamic pc-relative relocs against symbols which turn out to be
2385 defined in regular objects. For the normal shared case, discard
2386 space for pc-relative relocs that have become local due to symbol
2387 visibility changes. */
2388
2389 if (info->shared)
2390 {
2391 /* The only reloc that uses pc_count is R_386_PC32, which will
2392 appear on a call or on something like ".long foo - .". We
2393 want calls to protected symbols to resolve directly to the
2394 function rather than going via the plt. If people want
2395 function pointer comparisons to work as expected then they
2396 should avoid writing assembly like ".long foo - .". */
2397 if (SYMBOL_CALLS_LOCAL (info, h))
2398 {
2399 struct elf_dyn_relocs **pp;
2400
2401 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2402 {
2403 p->count -= p->pc_count;
2404 p->pc_count = 0;
2405 if (p->count == 0)
2406 *pp = p->next;
2407 else
2408 pp = &p->next;
2409 }
2410 }
2411
2412 if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks)
2413 {
2414 struct elf_dyn_relocs **pp;
2415 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2416 {
2417 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2418 *pp = p->next;
2419 else
2420 pp = &p->next;
2421 }
2422 }
2423
2424 /* Also discard relocs on undefined weak syms with non-default
2425 visibility. */
2426 if (eh->dyn_relocs != NULL
2427 && h->root.type == bfd_link_hash_undefweak)
2428 {
2429 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2430 eh->dyn_relocs = NULL;
2431
2432 /* Make sure undefined weak symbols are output as a dynamic
2433 symbol in PIEs. */
2434 else if (h->dynindx == -1
2435 && !h->forced_local)
2436 {
2437 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2438 return FALSE;
2439 }
2440 }
2441 }
2442 else if (ELIMINATE_COPY_RELOCS)
2443 {
2444 /* For the non-shared case, discard space for relocs against
2445 symbols which turn out to need copy relocs or are not
2446 dynamic. */
2447
2448 if (!h->non_got_ref
2449 && ((h->def_dynamic
2450 && !h->def_regular)
2451 || (htab->elf.dynamic_sections_created
2452 && (h->root.type == bfd_link_hash_undefweak
2453 || h->root.type == bfd_link_hash_undefined))))
2454 {
2455 /* Make sure this symbol is output as a dynamic symbol.
2456 Undefined weak syms won't yet be marked as dynamic. */
2457 if (h->dynindx == -1
2458 && !h->forced_local)
2459 {
2460 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2461 return FALSE;
2462 }
2463
2464 /* If that succeeded, we know we'll be keeping all the
2465 relocs. */
2466 if (h->dynindx != -1)
2467 goto keep;
2468 }
2469
2470 eh->dyn_relocs = NULL;
2471
2472 keep: ;
2473 }
2474
2475 /* Finally, allocate space. */
2476 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2477 {
2478 asection *sreloc;
2479
2480 sreloc = elf_section_data (p->sec)->sreloc;
2481
2482 BFD_ASSERT (sreloc != NULL);
2483 sreloc->size += p->count * sizeof (Elf32_External_Rel);
2484 }
2485
2486 return TRUE;
2487 }
2488
2489 /* Allocate space in .plt, .got and associated reloc sections for
2490 local dynamic relocs. */
2491
2492 static bfd_boolean
2493 elf_i386_allocate_local_dynrelocs (void **slot, void *inf)
2494 {
2495 struct elf_link_hash_entry *h
2496 = (struct elf_link_hash_entry *) *slot;
2497
2498 if (h->type != STT_GNU_IFUNC
2499 || !h->def_regular
2500 || !h->ref_regular
2501 || !h->forced_local
2502 || h->root.type != bfd_link_hash_defined)
2503 abort ();
2504
2505 return elf_i386_allocate_dynrelocs (h, inf);
2506 }
2507
2508 /* Find any dynamic relocs that apply to read-only sections. */
2509
2510 static bfd_boolean
2511 elf_i386_readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2512 {
2513 struct elf_i386_link_hash_entry *eh;
2514 struct elf_dyn_relocs *p;
2515
2516 /* Skip local IFUNC symbols. */
2517 if (h->forced_local && h->type == STT_GNU_IFUNC)
2518 return TRUE;
2519
2520 eh = (struct elf_i386_link_hash_entry *) h;
2521 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2522 {
2523 asection *s = p->sec->output_section;
2524
2525 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2526 {
2527 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2528
2529 info->flags |= DF_TEXTREL;
2530
2531 if (info->warn_shared_textrel && info->shared)
2532 info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'.\n"),
2533 p->sec->owner, h->root.root.string,
2534 p->sec);
2535
2536 /* Not an error, just cut short the traversal. */
2537 return FALSE;
2538 }
2539 }
2540 return TRUE;
2541 }
2542
2543 /* Set the sizes of the dynamic sections. */
2544
2545 static bfd_boolean
2546 elf_i386_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
2547 {
2548 struct elf_i386_link_hash_table *htab;
2549 bfd *dynobj;
2550 asection *s;
2551 bfd_boolean relocs;
2552 bfd *ibfd;
2553
2554 htab = elf_i386_hash_table (info);
2555 if (htab == NULL)
2556 return FALSE;
2557 dynobj = htab->elf.dynobj;
2558 if (dynobj == NULL)
2559 abort ();
2560
2561 if (htab->elf.dynamic_sections_created)
2562 {
2563 /* Set the contents of the .interp section to the interpreter. */
2564 if (info->executable)
2565 {
2566 s = bfd_get_section_by_name (dynobj, ".interp");
2567 if (s == NULL)
2568 abort ();
2569 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2570 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2571 }
2572 }
2573
2574 /* Set up .got offsets for local syms, and space for local dynamic
2575 relocs. */
2576 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2577 {
2578 bfd_signed_vma *local_got;
2579 bfd_signed_vma *end_local_got;
2580 char *local_tls_type;
2581 bfd_vma *local_tlsdesc_gotent;
2582 bfd_size_type locsymcount;
2583 Elf_Internal_Shdr *symtab_hdr;
2584 asection *srel;
2585
2586 if (! is_i386_elf (ibfd))
2587 continue;
2588
2589 for (s = ibfd->sections; s != NULL; s = s->next)
2590 {
2591 struct elf_dyn_relocs *p;
2592
2593 for (p = ((struct elf_dyn_relocs *)
2594 elf_section_data (s)->local_dynrel);
2595 p != NULL;
2596 p = p->next)
2597 {
2598 if (!bfd_is_abs_section (p->sec)
2599 && bfd_is_abs_section (p->sec->output_section))
2600 {
2601 /* Input section has been discarded, either because
2602 it is a copy of a linkonce section or due to
2603 linker script /DISCARD/, so we'll be discarding
2604 the relocs too. */
2605 }
2606 else if (get_elf_i386_backend_data (output_bfd)->is_vxworks
2607 && strcmp (p->sec->output_section->name,
2608 ".tls_vars") == 0)
2609 {
2610 /* Relocations in vxworks .tls_vars sections are
2611 handled specially by the loader. */
2612 }
2613 else if (p->count != 0)
2614 {
2615 srel = elf_section_data (p->sec)->sreloc;
2616 srel->size += p->count * sizeof (Elf32_External_Rel);
2617 if ((p->sec->output_section->flags & SEC_READONLY) != 0
2618 && (info->flags & DF_TEXTREL) == 0)
2619 {
2620 info->flags |= DF_TEXTREL;
2621 if (info->warn_shared_textrel && info->shared)
2622 info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'.\n"),
2623 p->sec->owner, p->sec);
2624 }
2625 }
2626 }
2627 }
2628
2629 local_got = elf_local_got_refcounts (ibfd);
2630 if (!local_got)
2631 continue;
2632
2633 symtab_hdr = &elf_symtab_hdr (ibfd);
2634 locsymcount = symtab_hdr->sh_info;
2635 end_local_got = local_got + locsymcount;
2636 local_tls_type = elf_i386_local_got_tls_type (ibfd);
2637 local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
2638 s = htab->elf.sgot;
2639 srel = htab->elf.srelgot;
2640 for (; local_got < end_local_got;
2641 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2642 {
2643 *local_tlsdesc_gotent = (bfd_vma) -1;
2644 if (*local_got > 0)
2645 {
2646 if (GOT_TLS_GDESC_P (*local_tls_type))
2647 {
2648 *local_tlsdesc_gotent = htab->elf.sgotplt->size
2649 - elf_i386_compute_jump_table_size (htab);
2650 htab->elf.sgotplt->size += 8;
2651 *local_got = (bfd_vma) -2;
2652 }
2653 if (! GOT_TLS_GDESC_P (*local_tls_type)
2654 || GOT_TLS_GD_P (*local_tls_type))
2655 {
2656 *local_got = s->size;
2657 s->size += 4;
2658 if (GOT_TLS_GD_P (*local_tls_type)
2659 || *local_tls_type == GOT_TLS_IE_BOTH)
2660 s->size += 4;
2661 }
2662 if (info->shared
2663 || GOT_TLS_GD_ANY_P (*local_tls_type)
2664 || (*local_tls_type & GOT_TLS_IE))
2665 {
2666 if (*local_tls_type == GOT_TLS_IE_BOTH)
2667 srel->size += 2 * sizeof (Elf32_External_Rel);
2668 else if (GOT_TLS_GD_P (*local_tls_type)
2669 || ! GOT_TLS_GDESC_P (*local_tls_type))
2670 srel->size += sizeof (Elf32_External_Rel);
2671 if (GOT_TLS_GDESC_P (*local_tls_type))
2672 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2673 }
2674 }
2675 else
2676 *local_got = (bfd_vma) -1;
2677 }
2678 }
2679
2680 if (htab->tls_ldm_got.refcount > 0)
2681 {
2682 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2683 relocs. */
2684 htab->tls_ldm_got.offset = htab->elf.sgot->size;
2685 htab->elf.sgot->size += 8;
2686 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2687 }
2688 else
2689 htab->tls_ldm_got.offset = -1;
2690
2691 /* Allocate global sym .plt and .got entries, and space for global
2692 sym dynamic relocs. */
2693 elf_link_hash_traverse (&htab->elf, elf_i386_allocate_dynrelocs, info);
2694
2695 /* Allocate .plt and .got entries, and space for local symbols. */
2696 htab_traverse (htab->loc_hash_table,
2697 elf_i386_allocate_local_dynrelocs,
2698 info);
2699
2700 /* For every jump slot reserved in the sgotplt, reloc_count is
2701 incremented. However, when we reserve space for TLS descriptors,
2702 it's not incremented, so in order to compute the space reserved
2703 for them, it suffices to multiply the reloc count by the jump
2704 slot size.
2705
2706 PR ld/13302: We start next_irelative_index at the end of .rela.plt
2707 so that R_386_IRELATIVE entries come last. */
2708 if (htab->elf.srelplt)
2709 {
2710 htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
2711 htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
2712 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
2713 }
2714 else if (htab->elf.irelplt)
2715 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
2716
2717
2718 if (htab->elf.sgotplt)
2719 {
2720 struct elf_link_hash_entry *got;
2721 got = elf_link_hash_lookup (elf_hash_table (info),
2722 "_GLOBAL_OFFSET_TABLE_",
2723 FALSE, FALSE, FALSE);
2724
2725 /* Don't allocate .got.plt section if there are no GOT nor PLT
2726 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
2727 if ((got == NULL
2728 || !got->ref_regular_nonweak)
2729 && (htab->elf.sgotplt->size
2730 == get_elf_backend_data (output_bfd)->got_header_size)
2731 && (htab->elf.splt == NULL
2732 || htab->elf.splt->size == 0)
2733 && (htab->elf.sgot == NULL
2734 || htab->elf.sgot->size == 0)
2735 && (htab->elf.iplt == NULL
2736 || htab->elf.iplt->size == 0)
2737 && (htab->elf.igotplt == NULL
2738 || htab->elf.igotplt->size == 0))
2739 htab->elf.sgotplt->size = 0;
2740 }
2741
2742 /* We now have determined the sizes of the various dynamic sections.
2743 Allocate memory for them. */
2744 relocs = FALSE;
2745 for (s = dynobj->sections; s != NULL; s = s->next)
2746 {
2747 bfd_boolean strip_section = TRUE;
2748
2749 if ((s->flags & SEC_LINKER_CREATED) == 0)
2750 continue;
2751
2752 if (s == htab->elf.splt
2753 || s == htab->elf.sgot
2754 || s == htab->elf.sgotplt
2755 || s == htab->elf.iplt
2756 || s == htab->elf.igotplt
2757 || s == htab->sdynbss)
2758 {
2759 /* Strip this section if we don't need it; see the
2760 comment below. */
2761 /* We'd like to strip these sections if they aren't needed, but if
2762 we've exported dynamic symbols from them we must leave them.
2763 It's too late to tell BFD to get rid of the symbols. */
2764
2765 if (htab->elf.hplt != NULL)
2766 strip_section = FALSE;
2767 }
2768 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
2769 {
2770 if (s->size != 0
2771 && s != htab->elf.srelplt
2772 && s != htab->srelplt2)
2773 relocs = TRUE;
2774
2775 /* We use the reloc_count field as a counter if we need
2776 to copy relocs into the output file. */
2777 s->reloc_count = 0;
2778 }
2779 else
2780 {
2781 /* It's not one of our sections, so don't allocate space. */
2782 continue;
2783 }
2784
2785 if (s->size == 0)
2786 {
2787 /* If we don't need this section, strip it from the
2788 output file. This is mostly to handle .rel.bss and
2789 .rel.plt. We must create both sections in
2790 create_dynamic_sections, because they must be created
2791 before the linker maps input sections to output
2792 sections. The linker does that before
2793 adjust_dynamic_symbol is called, and it is that
2794 function which decides whether anything needs to go
2795 into these sections. */
2796 if (strip_section)
2797 s->flags |= SEC_EXCLUDE;
2798 continue;
2799 }
2800
2801 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2802 continue;
2803
2804 /* Allocate memory for the section contents. We use bfd_zalloc
2805 here in case unused entries are not reclaimed before the
2806 section's contents are written out. This should not happen,
2807 but this way if it does, we get a R_386_NONE reloc instead
2808 of garbage. */
2809 s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
2810 if (s->contents == NULL)
2811 return FALSE;
2812 }
2813
2814 if (htab->plt_eh_frame != NULL
2815 && htab->elf.splt != NULL
2816 && htab->elf.splt->size != 0
2817 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0)
2818 bfd_put_32 (dynobj, htab->elf.splt->size,
2819 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
2820
2821 if (htab->elf.dynamic_sections_created)
2822 {
2823 /* Add some entries to the .dynamic section. We fill in the
2824 values later, in elf_i386_finish_dynamic_sections, but we
2825 must add the entries now so that we get the correct size for
2826 the .dynamic section. The DT_DEBUG entry is filled in by the
2827 dynamic linker and used by the debugger. */
2828 #define add_dynamic_entry(TAG, VAL) \
2829 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2830
2831 if (info->executable)
2832 {
2833 if (!add_dynamic_entry (DT_DEBUG, 0))
2834 return FALSE;
2835 }
2836
2837 if (htab->elf.splt->size != 0)
2838 {
2839 if (!add_dynamic_entry (DT_PLTGOT, 0)
2840 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2841 || !add_dynamic_entry (DT_PLTREL, DT_REL)
2842 || !add_dynamic_entry (DT_JMPREL, 0))
2843 return FALSE;
2844 }
2845
2846 if (relocs)
2847 {
2848 if (!add_dynamic_entry (DT_REL, 0)
2849 || !add_dynamic_entry (DT_RELSZ, 0)
2850 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
2851 return FALSE;
2852
2853 /* If any dynamic relocs apply to a read-only section,
2854 then we need a DT_TEXTREL entry. */
2855 if ((info->flags & DF_TEXTREL) == 0)
2856 elf_link_hash_traverse (&htab->elf,
2857 elf_i386_readonly_dynrelocs, info);
2858
2859 if ((info->flags & DF_TEXTREL) != 0)
2860 {
2861 if (!add_dynamic_entry (DT_TEXTREL, 0))
2862 return FALSE;
2863 }
2864 }
2865 if (get_elf_i386_backend_data (output_bfd)->is_vxworks
2866 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
2867 return FALSE;
2868 }
2869 #undef add_dynamic_entry
2870
2871 return TRUE;
2872 }
2873
2874 static bfd_boolean
2875 elf_i386_always_size_sections (bfd *output_bfd,
2876 struct bfd_link_info *info)
2877 {
2878 asection *tls_sec = elf_hash_table (info)->tls_sec;
2879
2880 if (tls_sec)
2881 {
2882 struct elf_link_hash_entry *tlsbase;
2883
2884 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2885 "_TLS_MODULE_BASE_",
2886 FALSE, FALSE, FALSE);
2887
2888 if (tlsbase && tlsbase->type == STT_TLS)
2889 {
2890 struct elf_i386_link_hash_table *htab;
2891 struct bfd_link_hash_entry *bh = NULL;
2892 const struct elf_backend_data *bed
2893 = get_elf_backend_data (output_bfd);
2894
2895 htab = elf_i386_hash_table (info);
2896 if (htab == NULL)
2897 return FALSE;
2898
2899 if (!(_bfd_generic_link_add_one_symbol
2900 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2901 tls_sec, 0, NULL, FALSE,
2902 bed->collect, &bh)))
2903 return FALSE;
2904
2905 htab->tls_module_base = bh;
2906
2907 tlsbase = (struct elf_link_hash_entry *)bh;
2908 tlsbase->def_regular = 1;
2909 tlsbase->other = STV_HIDDEN;
2910 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2911 }
2912 }
2913
2914 return TRUE;
2915 }
2916
2917 /* Set the correct type for an x86 ELF section. We do this by the
2918 section name, which is a hack, but ought to work. */
2919
2920 static bfd_boolean
2921 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2922 Elf_Internal_Shdr *hdr,
2923 asection *sec)
2924 {
2925 const char *name;
2926
2927 name = bfd_get_section_name (abfd, sec);
2928
2929 /* This is an ugly, but unfortunately necessary hack that is
2930 needed when producing EFI binaries on x86. It tells
2931 elf.c:elf_fake_sections() not to consider ".reloc" as a section
2932 containing ELF relocation info. We need this hack in order to
2933 be able to generate ELF binaries that can be translated into
2934 EFI applications (which are essentially COFF objects). Those
2935 files contain a COFF ".reloc" section inside an ELFNN object,
2936 which would normally cause BFD to segfault because it would
2937 attempt to interpret this section as containing relocation
2938 entries for section "oc". With this hack enabled, ".reloc"
2939 will be treated as a normal data section, which will avoid the
2940 segfault. However, you won't be able to create an ELFNN binary
2941 with a section named "oc" that needs relocations, but that's
2942 the kind of ugly side-effects you get when detecting section
2943 types based on their names... In practice, this limitation is
2944 unlikely to bite. */
2945 if (strcmp (name, ".reloc") == 0)
2946 hdr->sh_type = SHT_PROGBITS;
2947
2948 return TRUE;
2949 }
2950
2951 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2952 executables. Rather than setting it to the beginning of the TLS
2953 section, we have to set it to the end. This function may be called
2954 multiple times, it is idempotent. */
2955
2956 static void
2957 elf_i386_set_tls_module_base (struct bfd_link_info *info)
2958 {
2959 struct elf_i386_link_hash_table *htab;
2960 struct bfd_link_hash_entry *base;
2961
2962 if (!info->executable)
2963 return;
2964
2965 htab = elf_i386_hash_table (info);
2966 if (htab == NULL)
2967 return;
2968
2969 base = htab->tls_module_base;
2970 if (base == NULL)
2971 return;
2972
2973 base->u.def.value = htab->elf.tls_size;
2974 }
2975
2976 /* Return the base VMA address which should be subtracted from real addresses
2977 when resolving @dtpoff relocation.
2978 This is PT_TLS segment p_vaddr. */
2979
2980 static bfd_vma
2981 elf_i386_dtpoff_base (struct bfd_link_info *info)
2982 {
2983 /* If tls_sec is NULL, we should have signalled an error already. */
2984 if (elf_hash_table (info)->tls_sec == NULL)
2985 return 0;
2986 return elf_hash_table (info)->tls_sec->vma;
2987 }
2988
2989 /* Return the relocation value for @tpoff relocation
2990 if STT_TLS virtual address is ADDRESS. */
2991
2992 static bfd_vma
2993 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
2994 {
2995 struct elf_link_hash_table *htab = elf_hash_table (info);
2996 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2997 bfd_vma static_tls_size;
2998
2999 /* If tls_sec is NULL, we should have signalled an error already. */
3000 if (htab->tls_sec == NULL)
3001 return 0;
3002
3003 /* Consider special static TLS alignment requirements. */
3004 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
3005 return static_tls_size + htab->tls_sec->vma - address;
3006 }
3007
3008 /* Relocate an i386 ELF section. */
3009
3010 static bfd_boolean
3011 elf_i386_relocate_section (bfd *output_bfd,
3012 struct bfd_link_info *info,
3013 bfd *input_bfd,
3014 asection *input_section,
3015 bfd_byte *contents,
3016 Elf_Internal_Rela *relocs,
3017 Elf_Internal_Sym *local_syms,
3018 asection **local_sections)
3019 {
3020 struct elf_i386_link_hash_table *htab;
3021 Elf_Internal_Shdr *symtab_hdr;
3022 struct elf_link_hash_entry **sym_hashes;
3023 bfd_vma *local_got_offsets;
3024 bfd_vma *local_tlsdesc_gotents;
3025 Elf_Internal_Rela *rel;
3026 Elf_Internal_Rela *relend;
3027 bfd_boolean is_vxworks_tls;
3028 unsigned plt_entry_size;
3029
3030 BFD_ASSERT (is_i386_elf (input_bfd));
3031
3032 htab = elf_i386_hash_table (info);
3033 if (htab == NULL)
3034 return FALSE;
3035 symtab_hdr = &elf_symtab_hdr (input_bfd);
3036 sym_hashes = elf_sym_hashes (input_bfd);
3037 local_got_offsets = elf_local_got_offsets (input_bfd);
3038 local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
3039 /* We have to handle relocations in vxworks .tls_vars sections
3040 specially, because the dynamic loader is 'weird'. */
3041 is_vxworks_tls = (get_elf_i386_backend_data (output_bfd)->is_vxworks
3042 && info->shared
3043 && !strcmp (input_section->output_section->name,
3044 ".tls_vars"));
3045
3046 elf_i386_set_tls_module_base (info);
3047
3048 plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd);
3049
3050 rel = relocs;
3051 relend = relocs + input_section->reloc_count;
3052 for (; rel < relend; rel++)
3053 {
3054 unsigned int r_type;
3055 reloc_howto_type *howto;
3056 unsigned long r_symndx;
3057 struct elf_link_hash_entry *h;
3058 Elf_Internal_Sym *sym;
3059 asection *sec;
3060 bfd_vma off, offplt;
3061 bfd_vma relocation;
3062 bfd_boolean unresolved_reloc;
3063 bfd_reloc_status_type r;
3064 unsigned int indx;
3065 int tls_type;
3066
3067 r_type = ELF32_R_TYPE (rel->r_info);
3068 if (r_type == R_386_GNU_VTINHERIT
3069 || r_type == R_386_GNU_VTENTRY)
3070 continue;
3071
3072 if ((indx = r_type) >= R_386_standard
3073 && ((indx = r_type - R_386_ext_offset) - R_386_standard
3074 >= R_386_ext - R_386_standard)
3075 && ((indx = r_type - R_386_tls_offset) - R_386_ext
3076 >= R_386_irelative - R_386_ext))
3077 {
3078 (*_bfd_error_handler)
3079 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
3080 input_bfd, input_section, r_type);
3081 bfd_set_error (bfd_error_bad_value);
3082 return FALSE;
3083 }
3084 howto = elf_howto_table + indx;
3085
3086 r_symndx = ELF32_R_SYM (rel->r_info);
3087 h = NULL;
3088 sym = NULL;
3089 sec = NULL;
3090 unresolved_reloc = FALSE;
3091 if (r_symndx < symtab_hdr->sh_info)
3092 {
3093 sym = local_syms + r_symndx;
3094 sec = local_sections[r_symndx];
3095 relocation = (sec->output_section->vma
3096 + sec->output_offset
3097 + sym->st_value);
3098
3099 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
3100 && ((sec->flags & SEC_MERGE) != 0
3101 || (info->relocatable
3102 && sec->output_offset != 0)))
3103 {
3104 bfd_vma addend;
3105 bfd_byte *where = contents + rel->r_offset;
3106
3107 switch (howto->size)
3108 {
3109 case 0:
3110 addend = bfd_get_8 (input_bfd, where);
3111 if (howto->pc_relative)
3112 {
3113 addend = (addend ^ 0x80) - 0x80;
3114 addend += 1;
3115 }
3116 break;
3117 case 1:
3118 addend = bfd_get_16 (input_bfd, where);
3119 if (howto->pc_relative)
3120 {
3121 addend = (addend ^ 0x8000) - 0x8000;
3122 addend += 2;
3123 }
3124 break;
3125 case 2:
3126 addend = bfd_get_32 (input_bfd, where);
3127 if (howto->pc_relative)
3128 {
3129 addend = (addend ^ 0x80000000) - 0x80000000;
3130 addend += 4;
3131 }
3132 break;
3133 default:
3134 abort ();
3135 }
3136
3137 if (info->relocatable)
3138 addend += sec->output_offset;
3139 else
3140 {
3141 asection *msec = sec;
3142 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
3143 addend);
3144 addend -= relocation;
3145 addend += msec->output_section->vma + msec->output_offset;
3146 }
3147
3148 switch (howto->size)
3149 {
3150 case 0:
3151 /* FIXME: overflow checks. */
3152 if (howto->pc_relative)
3153 addend -= 1;
3154 bfd_put_8 (input_bfd, addend, where);
3155 break;
3156 case 1:
3157 if (howto->pc_relative)
3158 addend -= 2;
3159 bfd_put_16 (input_bfd, addend, where);
3160 break;
3161 case 2:
3162 if (howto->pc_relative)
3163 addend -= 4;
3164 bfd_put_32 (input_bfd, addend, where);
3165 break;
3166 }
3167 }
3168 else if (!info->relocatable
3169 && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3170 {
3171 /* Relocate against local STT_GNU_IFUNC symbol. */
3172 h = elf_i386_get_local_sym_hash (htab, input_bfd, rel,
3173 FALSE);
3174 if (h == NULL)
3175 abort ();
3176
3177 /* Set STT_GNU_IFUNC symbol value. */
3178 h->root.u.def.value = sym->st_value;
3179 h->root.u.def.section = sec;
3180 }
3181 }
3182 else
3183 {
3184 bfd_boolean warned ATTRIBUTE_UNUSED;
3185
3186 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3187 r_symndx, symtab_hdr, sym_hashes,
3188 h, sec, relocation,
3189 unresolved_reloc, warned);
3190 }
3191
3192 if (sec != NULL && elf_discarded_section (sec))
3193 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3194 rel, relend, howto, contents);
3195
3196 if (info->relocatable)
3197 continue;
3198
3199 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3200 it here if it is defined in a non-shared object. */
3201 if (h != NULL
3202 && h->type == STT_GNU_IFUNC
3203 && h->def_regular)
3204 {
3205 asection *plt, *gotplt, *base_got;
3206 bfd_vma plt_index;
3207 const char *name;
3208
3209 if ((input_section->flags & SEC_ALLOC) == 0
3210 || h->plt.offset == (bfd_vma) -1)
3211 abort ();
3212
3213 /* STT_GNU_IFUNC symbol must go through PLT. */
3214 if (htab->elf.splt != NULL)
3215 {
3216 plt = htab->elf.splt;
3217 gotplt = htab->elf.sgotplt;
3218 }
3219 else
3220 {
3221 plt = htab->elf.iplt;
3222 gotplt = htab->elf.igotplt;
3223 }
3224
3225 relocation = (plt->output_section->vma
3226 + plt->output_offset + h->plt.offset);
3227
3228 switch (r_type)
3229 {
3230 default:
3231 if (h->root.root.string)
3232 name = h->root.root.string;
3233 else
3234 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3235 NULL);
3236 (*_bfd_error_handler)
3237 (_("%B: relocation %s against STT_GNU_IFUNC "
3238 "symbol `%s' isn't handled by %s"), input_bfd,
3239 elf_howto_table[r_type].name,
3240 name, __FUNCTION__);
3241 bfd_set_error (bfd_error_bad_value);
3242 return FALSE;
3243
3244 case R_386_32:
3245 /* Generate dynamic relcoation only when there is a
3246 non-GOT reference in a shared object. */
3247 if (info->shared && h->non_got_ref)
3248 {
3249 Elf_Internal_Rela outrel;
3250 bfd_byte *loc;
3251 asection *sreloc;
3252 bfd_vma offset;
3253
3254 /* Need a dynamic relocation to get the real function
3255 adddress. */
3256 offset = _bfd_elf_section_offset (output_bfd,
3257 info,
3258 input_section,
3259 rel->r_offset);
3260 if (offset == (bfd_vma) -1
3261 || offset == (bfd_vma) -2)
3262 abort ();
3263
3264 outrel.r_offset = (input_section->output_section->vma
3265 + input_section->output_offset
3266 + offset);
3267
3268 if (h->dynindx == -1
3269 || h->forced_local
3270 || info->executable)
3271 {
3272 /* This symbol is resolved locally. */
3273 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3274 bfd_put_32 (output_bfd,
3275 (h->root.u.def.value
3276 + h->root.u.def.section->output_section->vma
3277 + h->root.u.def.section->output_offset),
3278 contents + offset);
3279 }
3280 else
3281 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3282
3283 sreloc = htab->elf.irelifunc;
3284 loc = sreloc->contents;
3285 loc += (sreloc->reloc_count++
3286 * sizeof (Elf32_External_Rel));
3287 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3288
3289 /* If this reloc is against an external symbol, we
3290 do not want to fiddle with the addend. Otherwise,
3291 we need to include the symbol value so that it
3292 becomes an addend for the dynamic reloc. For an
3293 internal symbol, we have updated addend. */
3294 continue;
3295 }
3296 /* FALLTHROUGH */
3297 case R_386_PC32:
3298 case R_386_PLT32:
3299 goto do_relocation;
3300
3301 case R_386_GOT32:
3302 base_got = htab->elf.sgot;
3303 off = h->got.offset;
3304
3305 if (base_got == NULL)
3306 abort ();
3307
3308 if (off == (bfd_vma) -1)
3309 {
3310 /* We can't use h->got.offset here to save state, or
3311 even just remember the offset, as finish_dynamic_symbol
3312 would use that as offset into .got. */
3313
3314 if (htab->elf.splt != NULL)
3315 {
3316 plt_index = h->plt.offset / plt_entry_size - 1;
3317 off = (plt_index + 3) * 4;
3318 base_got = htab->elf.sgotplt;
3319 }
3320 else
3321 {
3322 plt_index = h->plt.offset / plt_entry_size;
3323 off = plt_index * 4;
3324 base_got = htab->elf.igotplt;
3325 }
3326
3327 if (h->dynindx == -1
3328 || h->forced_local
3329 || info->symbolic)
3330 {
3331 /* This references the local defitionion. We must
3332 initialize this entry in the global offset table.
3333 Since the offset must always be a multiple of 8,
3334 we use the least significant bit to record
3335 whether we have initialized it already.
3336
3337 When doing a dynamic link, we create a .rela.got
3338 relocation entry to initialize the value. This
3339 is done in the finish_dynamic_symbol routine. */
3340 if ((off & 1) != 0)
3341 off &= ~1;
3342 else
3343 {
3344 bfd_put_32 (output_bfd, relocation,
3345 base_got->contents + off);
3346 h->got.offset |= 1;
3347 }
3348 }
3349
3350 relocation = off;
3351
3352 /* Adjust for static executables. */
3353 if (htab->elf.splt == NULL)
3354 relocation += gotplt->output_offset;
3355 }
3356 else
3357 {
3358 relocation = (base_got->output_section->vma
3359 + base_got->output_offset + off
3360 - gotplt->output_section->vma
3361 - gotplt->output_offset);
3362 /* Adjust for static executables. */
3363 if (htab->elf.splt == NULL)
3364 relocation += gotplt->output_offset;
3365 }
3366
3367 goto do_relocation;
3368
3369 case R_386_GOTOFF:
3370 relocation -= (gotplt->output_section->vma
3371 + gotplt->output_offset);
3372 goto do_relocation;
3373 }
3374 }
3375
3376 switch (r_type)
3377 {
3378 case R_386_GOT32:
3379 /* Relocation is to the entry for this symbol in the global
3380 offset table. */
3381 if (htab->elf.sgot == NULL)
3382 abort ();
3383
3384 if (h != NULL)
3385 {
3386 bfd_boolean dyn;
3387
3388 off = h->got.offset;
3389 dyn = htab->elf.dynamic_sections_created;
3390 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3391 || (info->shared
3392 && SYMBOL_REFERENCES_LOCAL (info, h))
3393 || (ELF_ST_VISIBILITY (h->other)
3394 && h->root.type == bfd_link_hash_undefweak))
3395 {
3396 /* This is actually a static link, or it is a
3397 -Bsymbolic link and the symbol is defined
3398 locally, or the symbol was forced to be local
3399 because of a version file. We must initialize
3400 this entry in the global offset table. Since the
3401 offset must always be a multiple of 4, we use the
3402 least significant bit to record whether we have
3403 initialized it already.
3404
3405 When doing a dynamic link, we create a .rel.got
3406 relocation entry to initialize the value. This
3407 is done in the finish_dynamic_symbol routine. */
3408 if ((off & 1) != 0)
3409 off &= ~1;
3410 else
3411 {
3412 bfd_put_32 (output_bfd, relocation,
3413 htab->elf.sgot->contents + off);
3414 h->got.offset |= 1;
3415 }
3416 }
3417 else
3418 unresolved_reloc = FALSE;
3419 }
3420 else
3421 {
3422 if (local_got_offsets == NULL)
3423 abort ();
3424
3425 off = local_got_offsets[r_symndx];
3426
3427 /* The offset must always be a multiple of 4. We use
3428 the least significant bit to record whether we have
3429 already generated the necessary reloc. */
3430 if ((off & 1) != 0)
3431 off &= ~1;
3432 else
3433 {
3434 bfd_put_32 (output_bfd, relocation,
3435 htab->elf.sgot->contents + off);
3436
3437 if (info->shared)
3438 {
3439 asection *s;
3440 Elf_Internal_Rela outrel;
3441 bfd_byte *loc;
3442
3443 s = htab->elf.srelgot;
3444 if (s == NULL)
3445 abort ();
3446
3447 outrel.r_offset = (htab->elf.sgot->output_section->vma
3448 + htab->elf.sgot->output_offset
3449 + off);
3450 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3451 loc = s->contents;
3452 loc += s->reloc_count++ * sizeof (Elf32_External_Rel);
3453 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3454 }
3455
3456 local_got_offsets[r_symndx] |= 1;
3457 }
3458 }
3459
3460 if (off >= (bfd_vma) -2)
3461 abort ();
3462
3463 relocation = htab->elf.sgot->output_section->vma
3464 + htab->elf.sgot->output_offset + off
3465 - htab->elf.sgotplt->output_section->vma
3466 - htab->elf.sgotplt->output_offset;
3467 break;
3468
3469 case R_386_GOTOFF:
3470 /* Relocation is relative to the start of the global offset
3471 table. */
3472
3473 /* Check to make sure it isn't a protected function symbol
3474 for shared library since it may not be local when used
3475 as function address. We also need to make sure that a
3476 symbol is defined locally. */
3477 if (info->shared && h)
3478 {
3479 if (!h->def_regular)
3480 {
3481 const char *v;
3482
3483 switch (ELF_ST_VISIBILITY (h->other))
3484 {
3485 case STV_HIDDEN:
3486 v = _("hidden symbol");
3487 break;
3488 case STV_INTERNAL:
3489 v = _("internal symbol");
3490 break;
3491 case STV_PROTECTED:
3492 v = _("protected symbol");
3493 break;
3494 default:
3495 v = _("symbol");
3496 break;
3497 }
3498
3499 (*_bfd_error_handler)
3500 (_("%B: relocation R_386_GOTOFF against undefined %s `%s' can not be used when making a shared object"),
3501 input_bfd, v, h->root.root.string);
3502 bfd_set_error (bfd_error_bad_value);
3503 return FALSE;
3504 }
3505 else if (!info->executable
3506 && !SYMBOLIC_BIND (info, h)
3507 && h->type == STT_FUNC
3508 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3509 {
3510 (*_bfd_error_handler)
3511 (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
3512 input_bfd, h->root.root.string);
3513 bfd_set_error (bfd_error_bad_value);
3514 return FALSE;
3515 }
3516 }
3517
3518 /* Note that sgot is not involved in this
3519 calculation. We always want the start of .got.plt. If we
3520 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3521 permitted by the ABI, we might have to change this
3522 calculation. */
3523 relocation -= htab->elf.sgotplt->output_section->vma
3524 + htab->elf.sgotplt->output_offset;
3525 break;
3526
3527 case R_386_GOTPC:
3528 /* Use global offset table as symbol value. */
3529 relocation = htab->elf.sgotplt->output_section->vma
3530 + htab->elf.sgotplt->output_offset;
3531 unresolved_reloc = FALSE;
3532 break;
3533
3534 case R_386_PLT32:
3535 /* Relocation is to the entry for this symbol in the
3536 procedure linkage table. */
3537
3538 /* Resolve a PLT32 reloc against a local symbol directly,
3539 without using the procedure linkage table. */
3540 if (h == NULL)
3541 break;
3542
3543 if (h->plt.offset == (bfd_vma) -1
3544 || htab->elf.splt == NULL)
3545 {
3546 /* We didn't make a PLT entry for this symbol. This
3547 happens when statically linking PIC code, or when
3548 using -Bsymbolic. */
3549 break;
3550 }
3551
3552 relocation = (htab->elf.splt->output_section->vma
3553 + htab->elf.splt->output_offset
3554 + h->plt.offset);
3555 unresolved_reloc = FALSE;
3556 break;
3557
3558 case R_386_32:
3559 case R_386_PC32:
3560 if ((input_section->flags & SEC_ALLOC) == 0
3561 || is_vxworks_tls)
3562 break;
3563
3564 if ((info->shared
3565 && (h == NULL
3566 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3567 || h->root.type != bfd_link_hash_undefweak)
3568 && (r_type != R_386_PC32
3569 || !SYMBOL_CALLS_LOCAL (info, h)))
3570 || (ELIMINATE_COPY_RELOCS
3571 && !info->shared
3572 && h != NULL
3573 && h->dynindx != -1
3574 && !h->non_got_ref
3575 && ((h->def_dynamic
3576 && !h->def_regular)
3577 || h->root.type == bfd_link_hash_undefweak
3578 || h->root.type == bfd_link_hash_undefined)))
3579 {
3580 Elf_Internal_Rela outrel;
3581 bfd_byte *loc;
3582 bfd_boolean skip, relocate;
3583 asection *sreloc;
3584
3585 /* When generating a shared object, these relocations
3586 are copied into the output file to be resolved at run
3587 time. */
3588
3589 skip = FALSE;
3590 relocate = FALSE;
3591
3592 outrel.r_offset =
3593 _bfd_elf_section_offset (output_bfd, info, input_section,
3594 rel->r_offset);
3595 if (outrel.r_offset == (bfd_vma) -1)
3596 skip = TRUE;
3597 else if (outrel.r_offset == (bfd_vma) -2)
3598 skip = TRUE, relocate = TRUE;
3599 outrel.r_offset += (input_section->output_section->vma
3600 + input_section->output_offset);
3601
3602 if (skip)
3603 memset (&outrel, 0, sizeof outrel);
3604 else if (h != NULL
3605 && h->dynindx != -1
3606 && (r_type == R_386_PC32
3607 || !info->shared
3608 || !SYMBOLIC_BIND (info, h)
3609 || !h->def_regular))
3610 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3611 else
3612 {
3613 /* This symbol is local, or marked to become local. */
3614 relocate = TRUE;
3615 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3616 }
3617
3618 sreloc = elf_section_data (input_section)->sreloc;
3619
3620 if (sreloc == NULL || sreloc->contents == NULL)
3621 {
3622 r = bfd_reloc_notsupported;
3623 goto check_relocation_error;
3624 }
3625
3626 loc = sreloc->contents;
3627 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3628
3629 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3630
3631 /* If this reloc is against an external symbol, we do
3632 not want to fiddle with the addend. Otherwise, we
3633 need to include the symbol value so that it becomes
3634 an addend for the dynamic reloc. */
3635 if (! relocate)
3636 continue;
3637 }
3638 break;
3639
3640 case R_386_TLS_IE:
3641 if (!info->executable)
3642 {
3643 Elf_Internal_Rela outrel;
3644 bfd_byte *loc;
3645 asection *sreloc;
3646
3647 outrel.r_offset = rel->r_offset
3648 + input_section->output_section->vma
3649 + input_section->output_offset;
3650 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3651 sreloc = elf_section_data (input_section)->sreloc;
3652 if (sreloc == NULL)
3653 abort ();
3654 loc = sreloc->contents;
3655 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3656 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3657 }
3658 /* Fall through */
3659
3660 case R_386_TLS_GD:
3661 case R_386_TLS_GOTDESC:
3662 case R_386_TLS_DESC_CALL:
3663 case R_386_TLS_IE_32:
3664 case R_386_TLS_GOTIE:
3665 tls_type = GOT_UNKNOWN;
3666 if (h == NULL && local_got_offsets)
3667 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
3668 else if (h != NULL)
3669 tls_type = elf_i386_hash_entry(h)->tls_type;
3670 if (tls_type == GOT_TLS_IE)
3671 tls_type = GOT_TLS_IE_NEG;
3672
3673 if (! elf_i386_tls_transition (info, input_bfd,
3674 input_section, contents,
3675 symtab_hdr, sym_hashes,
3676 &r_type, tls_type, rel,
3677 relend, h, r_symndx))
3678 return FALSE;
3679
3680 if (r_type == R_386_TLS_LE_32)
3681 {
3682 BFD_ASSERT (! unresolved_reloc);
3683 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3684 {
3685 unsigned int type;
3686 bfd_vma roff;
3687
3688 /* GD->LE transition. */
3689 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3690 if (type == 0x04)
3691 {
3692 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3693 Change it into:
3694 movl %gs:0, %eax; subl $foo@tpoff, %eax
3695 (6 byte form of subl). */
3696 memcpy (contents + rel->r_offset - 3,
3697 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3698 roff = rel->r_offset + 5;
3699 }
3700 else
3701 {
3702 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3703 Change it into:
3704 movl %gs:0, %eax; subl $foo@tpoff, %eax
3705 (6 byte form of subl). */
3706 memcpy (contents + rel->r_offset - 2,
3707 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3708 roff = rel->r_offset + 6;
3709 }
3710 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3711 contents + roff);
3712 /* Skip R_386_PC32/R_386_PLT32. */
3713 rel++;
3714 continue;
3715 }
3716 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3717 {
3718 /* GDesc -> LE transition.
3719 It's originally something like:
3720 leal x@tlsdesc(%ebx), %eax
3721
3722 leal x@ntpoff, %eax
3723
3724 Registers other than %eax may be set up here. */
3725
3726 unsigned int val;
3727 bfd_vma roff;
3728
3729 roff = rel->r_offset;
3730 val = bfd_get_8 (input_bfd, contents + roff - 1);
3731
3732 /* Now modify the instruction as appropriate. */
3733 /* aoliva FIXME: remove the above and xor the byte
3734 below with 0x86. */
3735 bfd_put_8 (output_bfd, val ^ 0x86,
3736 contents + roff - 1);
3737 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3738 contents + roff);
3739 continue;
3740 }
3741 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3742 {
3743 /* GDesc -> LE transition.
3744 It's originally:
3745 call *(%eax)
3746 Turn it into:
3747 xchg %ax,%ax */
3748
3749 bfd_vma roff;
3750
3751 roff = rel->r_offset;
3752 bfd_put_8 (output_bfd, 0x66, contents + roff);
3753 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3754 continue;
3755 }
3756 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
3757 {
3758 unsigned int val;
3759
3760 /* IE->LE transition:
3761 Originally it can be one of:
3762 movl foo, %eax
3763 movl foo, %reg
3764 addl foo, %reg
3765 We change it into:
3766 movl $foo, %eax
3767 movl $foo, %reg
3768 addl $foo, %reg. */
3769 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3770 if (val == 0xa1)
3771 {
3772 /* movl foo, %eax. */
3773 bfd_put_8 (output_bfd, 0xb8,
3774 contents + rel->r_offset - 1);
3775 }
3776 else
3777 {
3778 unsigned int type;
3779
3780 type = bfd_get_8 (input_bfd,
3781 contents + rel->r_offset - 2);
3782 switch (type)
3783 {
3784 case 0x8b:
3785 /* movl */
3786 bfd_put_8 (output_bfd, 0xc7,
3787 contents + rel->r_offset - 2);
3788 bfd_put_8 (output_bfd,
3789 0xc0 | ((val >> 3) & 7),
3790 contents + rel->r_offset - 1);
3791 break;
3792 case 0x03:
3793 /* addl */
3794 bfd_put_8 (output_bfd, 0x81,
3795 contents + rel->r_offset - 2);
3796 bfd_put_8 (output_bfd,
3797 0xc0 | ((val >> 3) & 7),
3798 contents + rel->r_offset - 1);
3799 break;
3800 default:
3801 BFD_FAIL ();
3802 break;
3803 }
3804 }
3805 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3806 contents + rel->r_offset);
3807 continue;
3808 }
3809 else
3810 {
3811 unsigned int val, type;
3812
3813 /* {IE_32,GOTIE}->LE transition:
3814 Originally it can be one of:
3815 subl foo(%reg1), %reg2
3816 movl foo(%reg1), %reg2
3817 addl foo(%reg1), %reg2
3818 We change it into:
3819 subl $foo, %reg2
3820 movl $foo, %reg2 (6 byte form)
3821 addl $foo, %reg2. */
3822 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3823 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3824 if (type == 0x8b)
3825 {
3826 /* movl */
3827 bfd_put_8 (output_bfd, 0xc7,
3828 contents + rel->r_offset - 2);
3829 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3830 contents + rel->r_offset - 1);
3831 }
3832 else if (type == 0x2b)
3833 {
3834 /* subl */
3835 bfd_put_8 (output_bfd, 0x81,
3836 contents + rel->r_offset - 2);
3837 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3838 contents + rel->r_offset - 1);
3839 }
3840 else if (type == 0x03)
3841 {
3842 /* addl */
3843 bfd_put_8 (output_bfd, 0x81,
3844 contents + rel->r_offset - 2);
3845 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3846 contents + rel->r_offset - 1);
3847 }
3848 else
3849 BFD_FAIL ();
3850 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
3851 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3852 contents + rel->r_offset);
3853 else
3854 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3855 contents + rel->r_offset);
3856 continue;
3857 }
3858 }
3859
3860 if (htab->elf.sgot == NULL)
3861 abort ();
3862
3863 if (h != NULL)
3864 {
3865 off = h->got.offset;
3866 offplt = elf_i386_hash_entry (h)->tlsdesc_got;
3867 }
3868 else
3869 {
3870 if (local_got_offsets == NULL)
3871 abort ();
3872
3873 off = local_got_offsets[r_symndx];
3874 offplt = local_tlsdesc_gotents[r_symndx];
3875 }
3876
3877 if ((off & 1) != 0)
3878 off &= ~1;
3879 else
3880 {
3881 Elf_Internal_Rela outrel;
3882 bfd_byte *loc;
3883 int dr_type;
3884 asection *sreloc;
3885
3886 if (htab->elf.srelgot == NULL)
3887 abort ();
3888
3889 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3890
3891 if (GOT_TLS_GDESC_P (tls_type))
3892 {
3893 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3894 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3895 <= htab->elf.sgotplt->size);
3896 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3897 + htab->elf.sgotplt->output_offset
3898 + offplt
3899 + htab->sgotplt_jump_table_size);
3900 sreloc = htab->elf.srelplt;
3901 loc = sreloc->contents;
3902 loc += (htab->next_tls_desc_index++
3903 * sizeof (Elf32_External_Rel));
3904 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3905 <= sreloc->contents + sreloc->size);
3906 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3907 if (indx == 0)
3908 {
3909 BFD_ASSERT (! unresolved_reloc);
3910 bfd_put_32 (output_bfd,
3911 relocation - elf_i386_dtpoff_base (info),
3912 htab->elf.sgotplt->contents + offplt
3913 + htab->sgotplt_jump_table_size + 4);
3914 }
3915 else
3916 {
3917 bfd_put_32 (output_bfd, 0,
3918 htab->elf.sgotplt->contents + offplt
3919 + htab->sgotplt_jump_table_size + 4);
3920 }
3921 }
3922
3923 sreloc = htab->elf.srelgot;
3924
3925 outrel.r_offset = (htab->elf.sgot->output_section->vma
3926 + htab->elf.sgot->output_offset + off);
3927
3928 if (GOT_TLS_GD_P (tls_type))
3929 dr_type = R_386_TLS_DTPMOD32;
3930 else if (GOT_TLS_GDESC_P (tls_type))
3931 goto dr_done;
3932 else if (tls_type == GOT_TLS_IE_POS)
3933 dr_type = R_386_TLS_TPOFF;
3934 else
3935 dr_type = R_386_TLS_TPOFF32;
3936
3937 if (dr_type == R_386_TLS_TPOFF && indx == 0)
3938 bfd_put_32 (output_bfd,
3939 relocation - elf_i386_dtpoff_base (info),
3940 htab->elf.sgot->contents + off);
3941 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3942 bfd_put_32 (output_bfd,
3943 elf_i386_dtpoff_base (info) - relocation,
3944 htab->elf.sgot->contents + off);
3945 else if (dr_type != R_386_TLS_DESC)
3946 bfd_put_32 (output_bfd, 0,
3947 htab->elf.sgot->contents + off);
3948 outrel.r_info = ELF32_R_INFO (indx, dr_type);
3949
3950 loc = sreloc->contents;
3951 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3952 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3953 <= sreloc->contents + sreloc->size);
3954 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3955
3956 if (GOT_TLS_GD_P (tls_type))
3957 {
3958 if (indx == 0)
3959 {
3960 BFD_ASSERT (! unresolved_reloc);
3961 bfd_put_32 (output_bfd,
3962 relocation - elf_i386_dtpoff_base (info),
3963 htab->elf.sgot->contents + off + 4);
3964 }
3965 else
3966 {
3967 bfd_put_32 (output_bfd, 0,
3968 htab->elf.sgot->contents + off + 4);
3969 outrel.r_info = ELF32_R_INFO (indx,
3970 R_386_TLS_DTPOFF32);
3971 outrel.r_offset += 4;
3972 sreloc->reloc_count++;
3973 loc += sizeof (Elf32_External_Rel);
3974 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3975 <= sreloc->contents + sreloc->size);
3976 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3977 }
3978 }
3979 else if (tls_type == GOT_TLS_IE_BOTH)
3980 {
3981 bfd_put_32 (output_bfd,
3982 (indx == 0
3983 ? relocation - elf_i386_dtpoff_base (info)
3984 : 0),
3985 htab->elf.sgot->contents + off + 4);
3986 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3987 outrel.r_offset += 4;
3988 sreloc->reloc_count++;
3989 loc += sizeof (Elf32_External_Rel);
3990 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3991 }
3992
3993 dr_done:
3994 if (h != NULL)
3995 h->got.offset |= 1;
3996 else
3997 local_got_offsets[r_symndx] |= 1;
3998 }
3999
4000 if (off >= (bfd_vma) -2
4001 && ! GOT_TLS_GDESC_P (tls_type))
4002 abort ();
4003 if (r_type == R_386_TLS_GOTDESC
4004 || r_type == R_386_TLS_DESC_CALL)
4005 {
4006 relocation = htab->sgotplt_jump_table_size + offplt;
4007 unresolved_reloc = FALSE;
4008 }
4009 else if (r_type == ELF32_R_TYPE (rel->r_info))
4010 {
4011 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
4012 + htab->elf.sgotplt->output_offset;
4013 relocation = htab->elf.sgot->output_section->vma
4014 + htab->elf.sgot->output_offset + off - g_o_t;
4015 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
4016 && tls_type == GOT_TLS_IE_BOTH)
4017 relocation += 4;
4018 if (r_type == R_386_TLS_IE)
4019 relocation += g_o_t;
4020 unresolved_reloc = FALSE;
4021 }
4022 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
4023 {
4024 unsigned int val, type;
4025 bfd_vma roff;
4026
4027 /* GD->IE transition. */
4028 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
4029 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4030 if (type == 0x04)
4031 {
4032 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
4033 Change it into:
4034 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
4035 val >>= 3;
4036 roff = rel->r_offset - 3;
4037 }
4038 else
4039 {
4040 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
4041 Change it into:
4042 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
4043 roff = rel->r_offset - 2;
4044 }
4045 memcpy (contents + roff,
4046 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
4047 contents[roff + 7] = 0x80 | (val & 7);
4048 /* If foo is used only with foo@gotntpoff(%reg) and
4049 foo@indntpoff, but not with foo@gottpoff(%reg), change
4050 subl $foo@gottpoff(%reg), %eax
4051 into:
4052 addl $foo@gotntpoff(%reg), %eax. */
4053 if (tls_type == GOT_TLS_IE_POS)
4054 contents[roff + 6] = 0x03;
4055 bfd_put_32 (output_bfd,
4056 htab->elf.sgot->output_section->vma
4057 + htab->elf.sgot->output_offset + off
4058 - htab->elf.sgotplt->output_section->vma
4059 - htab->elf.sgotplt->output_offset,
4060 contents + roff + 8);
4061 /* Skip R_386_PLT32. */
4062 rel++;
4063 continue;
4064 }
4065 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
4066 {
4067 /* GDesc -> IE transition.
4068 It's originally something like:
4069 leal x@tlsdesc(%ebx), %eax
4070
4071 Change it to:
4072 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
4073 or:
4074 movl x@gottpoff(%ebx), %eax # before negl %eax
4075
4076 Registers other than %eax may be set up here. */
4077
4078 bfd_vma roff;
4079
4080 /* First, make sure it's a leal adding ebx to a 32-bit
4081 offset into any register, although it's probably
4082 almost always going to be eax. */
4083 roff = rel->r_offset;
4084
4085 /* Now modify the instruction as appropriate. */
4086 /* To turn a leal into a movl in the form we use it, it
4087 suffices to change the first byte from 0x8d to 0x8b.
4088 aoliva FIXME: should we decide to keep the leal, all
4089 we have to do is remove the statement below, and
4090 adjust the relaxation of R_386_TLS_DESC_CALL. */
4091 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
4092
4093 if (tls_type == GOT_TLS_IE_BOTH)
4094 off += 4;
4095
4096 bfd_put_32 (output_bfd,
4097 htab->elf.sgot->output_section->vma
4098 + htab->elf.sgot->output_offset + off
4099 - htab->elf.sgotplt->output_section->vma
4100 - htab->elf.sgotplt->output_offset,
4101 contents + roff);
4102 continue;
4103 }
4104 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
4105 {
4106 /* GDesc -> IE transition.
4107 It's originally:
4108 call *(%eax)
4109
4110 Change it to:
4111 xchg %ax,%ax
4112 or
4113 negl %eax
4114 depending on how we transformed the TLS_GOTDESC above.
4115 */
4116
4117 bfd_vma roff;
4118
4119 roff = rel->r_offset;
4120
4121 /* Now modify the instruction as appropriate. */
4122 if (tls_type != GOT_TLS_IE_NEG)
4123 {
4124 /* xchg %ax,%ax */
4125 bfd_put_8 (output_bfd, 0x66, contents + roff);
4126 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4127 }
4128 else
4129 {
4130 /* negl %eax */
4131 bfd_put_8 (output_bfd, 0xf7, contents + roff);
4132 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
4133 }
4134
4135 continue;
4136 }
4137 else
4138 BFD_ASSERT (FALSE);
4139 break;
4140
4141 case R_386_TLS_LDM:
4142 if (! elf_i386_tls_transition (info, input_bfd,
4143 input_section, contents,
4144 symtab_hdr, sym_hashes,
4145 &r_type, GOT_UNKNOWN, rel,
4146 relend, h, r_symndx))
4147 return FALSE;
4148
4149 if (r_type != R_386_TLS_LDM)
4150 {
4151 /* LD->LE transition:
4152 leal foo(%reg), %eax; call ___tls_get_addr.
4153 We change it into:
4154 movl %gs:0, %eax; nop; leal 0(%esi,1), %esi. */
4155 BFD_ASSERT (r_type == R_386_TLS_LE_32);
4156 memcpy (contents + rel->r_offset - 2,
4157 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
4158 /* Skip R_386_PC32/R_386_PLT32. */
4159 rel++;
4160 continue;
4161 }
4162
4163 if (htab->elf.sgot == NULL)
4164 abort ();
4165
4166 off = htab->tls_ldm_got.offset;
4167 if (off & 1)
4168 off &= ~1;
4169 else
4170 {
4171 Elf_Internal_Rela outrel;
4172 bfd_byte *loc;
4173
4174 if (htab->elf.srelgot == NULL)
4175 abort ();
4176
4177 outrel.r_offset = (htab->elf.sgot->output_section->vma
4178 + htab->elf.sgot->output_offset + off);
4179
4180 bfd_put_32 (output_bfd, 0,
4181 htab->elf.sgot->contents + off);
4182 bfd_put_32 (output_bfd, 0,
4183 htab->elf.sgot->contents + off + 4);
4184 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
4185 loc = htab->elf.srelgot->contents;
4186 loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
4187 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
4188 htab->tls_ldm_got.offset |= 1;
4189 }
4190 relocation = htab->elf.sgot->output_section->vma
4191 + htab->elf.sgot->output_offset + off
4192 - htab->elf.sgotplt->output_section->vma
4193 - htab->elf.sgotplt->output_offset;
4194 unresolved_reloc = FALSE;
4195 break;
4196
4197 case R_386_TLS_LDO_32:
4198 if (!info->executable || (input_section->flags & SEC_CODE) == 0)
4199 relocation -= elf_i386_dtpoff_base (info);
4200 else
4201 /* When converting LDO to LE, we must negate. */
4202 relocation = -elf_i386_tpoff (info, relocation);
4203 break;
4204
4205 case R_386_TLS_LE_32:
4206 case R_386_TLS_LE:
4207 if (!info->executable)
4208 {
4209 Elf_Internal_Rela outrel;
4210 asection *sreloc;
4211 bfd_byte *loc;
4212
4213 outrel.r_offset = rel->r_offset
4214 + input_section->output_section->vma
4215 + input_section->output_offset;
4216 if (h != NULL && h->dynindx != -1)
4217 indx = h->dynindx;
4218 else
4219 indx = 0;
4220 if (r_type == R_386_TLS_LE_32)
4221 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
4222 else
4223 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
4224 sreloc = elf_section_data (input_section)->sreloc;
4225 if (sreloc == NULL)
4226 abort ();
4227 loc = sreloc->contents;
4228 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
4229 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
4230 if (indx)
4231 continue;
4232 else if (r_type == R_386_TLS_LE_32)
4233 relocation = elf_i386_dtpoff_base (info) - relocation;
4234 else
4235 relocation -= elf_i386_dtpoff_base (info);
4236 }
4237 else if (r_type == R_386_TLS_LE_32)
4238 relocation = elf_i386_tpoff (info, relocation);
4239 else
4240 relocation = -elf_i386_tpoff (info, relocation);
4241 break;
4242
4243 default:
4244 break;
4245 }
4246
4247 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4248 because such sections are not SEC_ALLOC and thus ld.so will
4249 not process them. */
4250 if (unresolved_reloc
4251 && !((input_section->flags & SEC_DEBUGGING) != 0
4252 && h->def_dynamic)
4253 && _bfd_elf_section_offset (output_bfd, info, input_section,
4254 rel->r_offset) != (bfd_vma) -1)
4255 {
4256 (*_bfd_error_handler)
4257 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4258 input_bfd,
4259 input_section,
4260 (long) rel->r_offset,
4261 howto->name,
4262 h->root.root.string);
4263 return FALSE;
4264 }
4265
4266 do_relocation:
4267 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4268 contents, rel->r_offset,
4269 relocation, 0);
4270
4271 check_relocation_error:
4272 if (r != bfd_reloc_ok)
4273 {
4274 const char *name;
4275
4276 if (h != NULL)
4277 name = h->root.root.string;
4278 else
4279 {
4280 name = bfd_elf_string_from_elf_section (input_bfd,
4281 symtab_hdr->sh_link,
4282 sym->st_name);
4283 if (name == NULL)
4284 return FALSE;
4285 if (*name == '\0')
4286 name = bfd_section_name (input_bfd, sec);
4287 }
4288
4289 if (r == bfd_reloc_overflow)
4290 {
4291 if (! ((*info->callbacks->reloc_overflow)
4292 (info, (h ? &h->root : NULL), name, howto->name,
4293 (bfd_vma) 0, input_bfd, input_section,
4294 rel->r_offset)))
4295 return FALSE;
4296 }
4297 else
4298 {
4299 (*_bfd_error_handler)
4300 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
4301 input_bfd, input_section,
4302 (long) rel->r_offset, name, (int) r);
4303 return FALSE;
4304 }
4305 }
4306 }
4307
4308 return TRUE;
4309 }
4310
4311 /* Finish up dynamic symbol handling. We set the contents of various
4312 dynamic sections here. */
4313
4314 static bfd_boolean
4315 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
4316 struct bfd_link_info *info,
4317 struct elf_link_hash_entry *h,
4318 Elf_Internal_Sym *sym)
4319 {
4320 struct elf_i386_link_hash_table *htab;
4321 unsigned plt_entry_size;
4322 const struct elf_i386_backend_data *abed;
4323
4324 htab = elf_i386_hash_table (info);
4325 if (htab == NULL)
4326 return FALSE;
4327
4328 abed = get_elf_i386_backend_data (output_bfd);
4329 plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd);
4330
4331 if (h->plt.offset != (bfd_vma) -1)
4332 {
4333 bfd_vma plt_index;
4334 bfd_vma got_offset;
4335 Elf_Internal_Rela rel;
4336 bfd_byte *loc;
4337 asection *plt, *gotplt, *relplt;
4338
4339 /* When building a static executable, use .iplt, .igot.plt and
4340 .rel.iplt sections for STT_GNU_IFUNC symbols. */
4341 if (htab->elf.splt != NULL)
4342 {
4343 plt = htab->elf.splt;
4344 gotplt = htab->elf.sgotplt;
4345 relplt = htab->elf.srelplt;
4346 }
4347 else
4348 {
4349 plt = htab->elf.iplt;
4350 gotplt = htab->elf.igotplt;
4351 relplt = htab->elf.irelplt;
4352 }
4353
4354 /* This symbol has an entry in the procedure linkage table. Set
4355 it up. */
4356
4357 if ((h->dynindx == -1
4358 && !((h->forced_local || info->executable)
4359 && h->def_regular
4360 && h->type == STT_GNU_IFUNC))
4361 || plt == NULL
4362 || gotplt == NULL
4363 || relplt == NULL)
4364 return FALSE;
4365
4366 /* Get the index in the procedure linkage table which
4367 corresponds to this symbol. This is the index of this symbol
4368 in all the symbols for which we are making plt entries. The
4369 first entry in the procedure linkage table is reserved.
4370
4371 Get the offset into the .got table of the entry that
4372 corresponds to this function. Each .got entry is 4 bytes.
4373 The first three are reserved.
4374
4375 For static executables, we don't reserve anything. */
4376
4377 if (plt == htab->elf.splt)
4378 {
4379 got_offset = h->plt.offset / plt_entry_size - 1;
4380 got_offset = (got_offset + 3) * 4;
4381 }
4382 else
4383 {
4384 got_offset = h->plt.offset / plt_entry_size;
4385 got_offset = got_offset * 4;
4386 }
4387
4388 /* Fill in the entry in the procedure linkage table. */
4389 if (! info->shared)
4390 {
4391 memcpy (plt->contents + h->plt.offset, abed->plt->plt_entry,
4392 abed->plt->plt_entry_size);
4393 bfd_put_32 (output_bfd,
4394 (gotplt->output_section->vma
4395 + gotplt->output_offset
4396 + got_offset),
4397 plt->contents + h->plt.offset
4398 + abed->plt->plt_got_offset);
4399
4400 if (abed->is_vxworks)
4401 {
4402 int s, k, reloc_index;
4403
4404 /* Create the R_386_32 relocation referencing the GOT
4405 for this PLT entry. */
4406
4407 /* S: Current slot number (zero-based). */
4408 s = ((h->plt.offset - abed->plt->plt_entry_size)
4409 / abed->plt->plt_entry_size);
4410 /* K: Number of relocations for PLTResolve. */
4411 if (info->shared)
4412 k = PLTRESOLVE_RELOCS_SHLIB;
4413 else
4414 k = PLTRESOLVE_RELOCS;
4415 /* Skip the PLTresolve relocations, and the relocations for
4416 the other PLT slots. */
4417 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
4418 loc = (htab->srelplt2->contents + reloc_index
4419 * sizeof (Elf32_External_Rel));
4420
4421 rel.r_offset = (htab->elf.splt->output_section->vma
4422 + htab->elf.splt->output_offset
4423 + h->plt.offset + 2),
4424 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4425 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4426
4427 /* Create the R_386_32 relocation referencing the beginning of
4428 the PLT for this GOT entry. */
4429 rel.r_offset = (htab->elf.sgotplt->output_section->vma
4430 + htab->elf.sgotplt->output_offset
4431 + got_offset);
4432 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
4433 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4434 loc + sizeof (Elf32_External_Rel));
4435 }
4436 }
4437 else
4438 {
4439 memcpy (plt->contents + h->plt.offset, abed->plt->pic_plt_entry,
4440 abed->plt->plt_entry_size);
4441 bfd_put_32 (output_bfd, got_offset,
4442 plt->contents + h->plt.offset
4443 + abed->plt->plt_got_offset);
4444 }
4445
4446 /* Fill in the entry in the global offset table. */
4447 bfd_put_32 (output_bfd,
4448 (plt->output_section->vma
4449 + plt->output_offset
4450 + h->plt.offset
4451 + abed->plt->plt_lazy_offset),
4452 gotplt->contents + got_offset);
4453
4454 /* Fill in the entry in the .rel.plt section. */
4455 rel.r_offset = (gotplt->output_section->vma
4456 + gotplt->output_offset
4457 + got_offset);
4458 if (h->dynindx == -1
4459 || ((info->executable
4460 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4461 && h->def_regular
4462 && h->type == STT_GNU_IFUNC))
4463 {
4464 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4465 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
4466 in the .got.plt section. */
4467 bfd_put_32 (output_bfd,
4468 (h->root.u.def.value
4469 + h->root.u.def.section->output_section->vma
4470 + h->root.u.def.section->output_offset),
4471 gotplt->contents + got_offset);
4472 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
4473 /* R_386_IRELATIVE comes last. */
4474 plt_index = htab->next_irelative_index--;
4475 }
4476 else
4477 {
4478 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
4479 plt_index = htab->next_jump_slot_index++;
4480 }
4481 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
4482 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4483
4484 /* Don't fill PLT entry for static executables. */
4485 if (plt == htab->elf.splt)
4486 {
4487 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
4488 plt->contents + h->plt.offset
4489 + abed->plt->plt_reloc_offset);
4490 bfd_put_32 (output_bfd, - (h->plt.offset
4491 + abed->plt->plt_plt_offset + 4),
4492 plt->contents + h->plt.offset
4493 + abed->plt->plt_plt_offset);
4494 }
4495
4496 if (!h->def_regular)
4497 {
4498 /* Mark the symbol as undefined, rather than as defined in
4499 the .plt section. Leave the value if there were any
4500 relocations where pointer equality matters (this is a clue
4501 for the dynamic linker, to make function pointer
4502 comparisons work between an application and shared
4503 library), otherwise set it to zero. If a function is only
4504 called from a binary, there is no need to slow down
4505 shared libraries because of that. */
4506 sym->st_shndx = SHN_UNDEF;
4507 if (!h->pointer_equality_needed)
4508 sym->st_value = 0;
4509 }
4510 }
4511
4512 if (h->got.offset != (bfd_vma) -1
4513 && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
4514 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
4515 {
4516 Elf_Internal_Rela rel;
4517 bfd_byte *loc;
4518
4519 /* This symbol has an entry in the global offset table. Set it
4520 up. */
4521
4522 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
4523 abort ();
4524
4525 rel.r_offset = (htab->elf.sgot->output_section->vma
4526 + htab->elf.sgot->output_offset
4527 + (h->got.offset & ~(bfd_vma) 1));
4528
4529 /* If this is a static link, or it is a -Bsymbolic link and the
4530 symbol is defined locally or was forced to be local because
4531 of a version file, we just want to emit a RELATIVE reloc.
4532 The entry in the global offset table will already have been
4533 initialized in the relocate_section function. */
4534 if (h->def_regular
4535 && h->type == STT_GNU_IFUNC)
4536 {
4537 if (info->shared)
4538 {
4539 /* Generate R_386_GLOB_DAT. */
4540 goto do_glob_dat;
4541 }
4542 else
4543 {
4544 asection *plt;
4545
4546 if (!h->pointer_equality_needed)
4547 abort ();
4548
4549 /* For non-shared object, we can't use .got.plt, which
4550 contains the real function addres if we need pointer
4551 equality. We load the GOT entry with the PLT entry. */
4552 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4553 bfd_put_32 (output_bfd,
4554 (plt->output_section->vma
4555 + plt->output_offset + h->plt.offset),
4556 htab->elf.sgot->contents + h->got.offset);
4557 return TRUE;
4558 }
4559 }
4560 else if (info->shared
4561 && SYMBOL_REFERENCES_LOCAL (info, h))
4562 {
4563 BFD_ASSERT((h->got.offset & 1) != 0);
4564 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4565 }
4566 else
4567 {
4568 BFD_ASSERT((h->got.offset & 1) == 0);
4569 do_glob_dat:
4570 bfd_put_32 (output_bfd, (bfd_vma) 0,
4571 htab->elf.sgot->contents + h->got.offset);
4572 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
4573 }
4574
4575 loc = htab->elf.srelgot->contents;
4576 loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
4577 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4578 }
4579
4580 if (h->needs_copy)
4581 {
4582 Elf_Internal_Rela rel;
4583 bfd_byte *loc;
4584
4585 /* This symbol needs a copy reloc. Set it up. */
4586
4587 if (h->dynindx == -1
4588 || (h->root.type != bfd_link_hash_defined
4589 && h->root.type != bfd_link_hash_defweak)
4590 || htab->srelbss == NULL)
4591 abort ();
4592
4593 rel.r_offset = (h->root.u.def.value
4594 + h->root.u.def.section->output_section->vma
4595 + h->root.u.def.section->output_offset);
4596 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
4597 loc = htab->srelbss->contents;
4598 loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rel);
4599 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4600 }
4601
4602 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
4603 be NULL for local symbols.
4604
4605 On VxWorks, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it
4606 is relative to the ".got" section. */
4607 if (sym != NULL
4608 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
4609 || (!abed->is_vxworks
4610 && h == htab->elf.hgot)))
4611 sym->st_shndx = SHN_ABS;
4612
4613 return TRUE;
4614 }
4615
4616 /* Finish up local dynamic symbol handling. We set the contents of
4617 various dynamic sections here. */
4618
4619 static bfd_boolean
4620 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
4621 {
4622 struct elf_link_hash_entry *h
4623 = (struct elf_link_hash_entry *) *slot;
4624 struct bfd_link_info *info
4625 = (struct bfd_link_info *) inf;
4626
4627 return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
4628 h, NULL);
4629 }
4630
4631 /* Used to decide how to sort relocs in an optimal manner for the
4632 dynamic linker, before writing them out. */
4633
4634 static enum elf_reloc_type_class
4635 elf_i386_reloc_type_class (const Elf_Internal_Rela *rela)
4636 {
4637 switch (ELF32_R_TYPE (rela->r_info))
4638 {
4639 case R_386_RELATIVE:
4640 return reloc_class_relative;
4641 case R_386_JUMP_SLOT:
4642 return reloc_class_plt;
4643 case R_386_COPY:
4644 return reloc_class_copy;
4645 default:
4646 return reloc_class_normal;
4647 }
4648 }
4649
4650 /* Finish up the dynamic sections. */
4651
4652 static bfd_boolean
4653 elf_i386_finish_dynamic_sections (bfd *output_bfd,
4654 struct bfd_link_info *info)
4655 {
4656 struct elf_i386_link_hash_table *htab;
4657 bfd *dynobj;
4658 asection *sdyn;
4659 const struct elf_i386_backend_data *abed;
4660
4661 htab = elf_i386_hash_table (info);
4662 if (htab == NULL)
4663 return FALSE;
4664
4665 dynobj = htab->elf.dynobj;
4666 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4667 abed = get_elf_i386_backend_data (output_bfd);
4668
4669 if (htab->elf.dynamic_sections_created)
4670 {
4671 Elf32_External_Dyn *dyncon, *dynconend;
4672
4673 if (sdyn == NULL || htab->elf.sgot == NULL)
4674 abort ();
4675
4676 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4677 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4678 for (; dyncon < dynconend; dyncon++)
4679 {
4680 Elf_Internal_Dyn dyn;
4681 asection *s;
4682
4683 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4684
4685 switch (dyn.d_tag)
4686 {
4687 default:
4688 if (abed->is_vxworks
4689 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
4690 break;
4691 continue;
4692
4693 case DT_PLTGOT:
4694 s = htab->elf.sgotplt;
4695 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4696 break;
4697
4698 case DT_JMPREL:
4699 s = htab->elf.srelplt;
4700 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4701 break;
4702
4703 case DT_PLTRELSZ:
4704 s = htab->elf.srelplt;
4705 dyn.d_un.d_val = s->size;
4706 break;
4707
4708 case DT_RELSZ:
4709 /* My reading of the SVR4 ABI indicates that the
4710 procedure linkage table relocs (DT_JMPREL) should be
4711 included in the overall relocs (DT_REL). This is
4712 what Solaris does. However, UnixWare can not handle
4713 that case. Therefore, we override the DT_RELSZ entry
4714 here to make it not include the JMPREL relocs. */
4715 s = htab->elf.srelplt;
4716 if (s == NULL)
4717 continue;
4718 dyn.d_un.d_val -= s->size;
4719 break;
4720
4721 case DT_REL:
4722 /* We may not be using the standard ELF linker script.
4723 If .rel.plt is the first .rel section, we adjust
4724 DT_REL to not include it. */
4725 s = htab->elf.srelplt;
4726 if (s == NULL)
4727 continue;
4728 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
4729 continue;
4730 dyn.d_un.d_ptr += s->size;
4731 break;
4732 }
4733
4734 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4735 }
4736
4737 /* Fill in the first entry in the procedure linkage table. */
4738 if (htab->elf.splt && htab->elf.splt->size > 0)
4739 {
4740 if (info->shared)
4741 {
4742 memcpy (htab->elf.splt->contents, abed->plt->pic_plt0_entry,
4743 abed->plt->plt0_entry_size);
4744 memset (htab->elf.splt->contents + abed->plt->plt0_entry_size,
4745 abed->plt0_pad_byte,
4746 abed->plt->plt_entry_size - abed->plt->plt0_entry_size);
4747 }
4748 else
4749 {
4750 memcpy (htab->elf.splt->contents, abed->plt->plt0_entry,
4751 abed->plt->plt0_entry_size);
4752 memset (htab->elf.splt->contents + abed->plt->plt0_entry_size,
4753 abed->plt0_pad_byte,
4754 abed->plt->plt_entry_size - abed->plt->plt0_entry_size);
4755 bfd_put_32 (output_bfd,
4756 (htab->elf.sgotplt->output_section->vma
4757 + htab->elf.sgotplt->output_offset
4758 + 4),
4759 htab->elf.splt->contents
4760 + abed->plt->plt0_got1_offset);
4761 bfd_put_32 (output_bfd,
4762 (htab->elf.sgotplt->output_section->vma
4763 + htab->elf.sgotplt->output_offset
4764 + 8),
4765 htab->elf.splt->contents
4766 + abed->plt->plt0_got2_offset);
4767
4768 if (abed->is_vxworks)
4769 {
4770 Elf_Internal_Rela rel;
4771
4772 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
4773 On IA32 we use REL relocations so the addend goes in
4774 the PLT directly. */
4775 rel.r_offset = (htab->elf.splt->output_section->vma
4776 + htab->elf.splt->output_offset
4777 + abed->plt->plt0_got1_offset);
4778 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4779 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4780 htab->srelplt2->contents);
4781 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
4782 rel.r_offset = (htab->elf.splt->output_section->vma
4783 + htab->elf.splt->output_offset
4784 + abed->plt->plt0_got2_offset);
4785 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4786 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4787 htab->srelplt2->contents +
4788 sizeof (Elf32_External_Rel));
4789 }
4790 }
4791
4792 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4793 really seem like the right value. */
4794 elf_section_data (htab->elf.splt->output_section)
4795 ->this_hdr.sh_entsize = 4;
4796
4797 /* Correct the .rel.plt.unloaded relocations. */
4798 if (abed->is_vxworks && !info->shared)
4799 {
4800 int num_plts = (htab->elf.splt->size
4801 / abed->plt->plt_entry_size) - 1;
4802 unsigned char *p;
4803
4804 p = htab->srelplt2->contents;
4805 if (info->shared)
4806 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4807 else
4808 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4809
4810 for (; num_plts; num_plts--)
4811 {
4812 Elf_Internal_Rela rel;
4813 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4814 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4815 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4816 p += sizeof (Elf32_External_Rel);
4817
4818 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4819 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
4820 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4821 p += sizeof (Elf32_External_Rel);
4822 }
4823 }
4824 }
4825 }
4826
4827 if (htab->elf.sgotplt)
4828 {
4829 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
4830 {
4831 (*_bfd_error_handler)
4832 (_("discarded output section: `%A'"), htab->elf.sgotplt);
4833 return FALSE;
4834 }
4835
4836 /* Fill in the first three entries in the global offset table. */
4837 if (htab->elf.sgotplt->size > 0)
4838 {
4839 bfd_put_32 (output_bfd,
4840 (sdyn == NULL ? 0
4841 : sdyn->output_section->vma + sdyn->output_offset),
4842 htab->elf.sgotplt->contents);
4843 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
4844 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
4845 }
4846
4847 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
4848 }
4849
4850 /* Adjust .eh_frame for .plt section. */
4851 if (htab->plt_eh_frame != NULL)
4852 {
4853 if (htab->elf.splt != NULL
4854 && htab->elf.splt->size != 0
4855 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
4856 && htab->elf.splt->output_section != NULL
4857 && htab->plt_eh_frame->output_section != NULL)
4858 {
4859 bfd_vma plt_start = htab->elf.splt->output_section->vma;
4860 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
4861 + htab->plt_eh_frame->output_offset
4862 + PLT_FDE_START_OFFSET;
4863 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4864 htab->plt_eh_frame->contents
4865 + PLT_FDE_START_OFFSET);
4866 }
4867 if (htab->plt_eh_frame->sec_info_type
4868 == ELF_INFO_TYPE_EH_FRAME)
4869 {
4870 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4871 htab->plt_eh_frame,
4872 htab->plt_eh_frame->contents))
4873 return FALSE;
4874 }
4875 }
4876
4877 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4878 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
4879
4880 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4881 htab_traverse (htab->loc_hash_table,
4882 elf_i386_finish_local_dynamic_symbol,
4883 info);
4884
4885 return TRUE;
4886 }
4887
4888 /* Return address for Ith PLT stub in section PLT, for relocation REL
4889 or (bfd_vma) -1 if it should not be included. */
4890
4891 static bfd_vma
4892 elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
4893 const arelent *rel ATTRIBUTE_UNUSED)
4894 {
4895 return plt->vma + (i + 1) * GET_PLT_ENTRY_SIZE (plt->owner);
4896 }
4897
4898 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4899
4900 static bfd_boolean
4901 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
4902 {
4903 if (h->plt.offset != (bfd_vma) -1
4904 && !h->def_regular
4905 && !h->pointer_equality_needed)
4906 return FALSE;
4907
4908 return _bfd_elf_hash_symbol (h);
4909 }
4910
4911 /* Hook called by the linker routine which adds symbols from an object
4912 file. */
4913
4914 static bfd_boolean
4915 elf_i386_add_symbol_hook (bfd * abfd,
4916 struct bfd_link_info * info ATTRIBUTE_UNUSED,
4917 Elf_Internal_Sym * sym,
4918 const char ** namep ATTRIBUTE_UNUSED,
4919 flagword * flagsp ATTRIBUTE_UNUSED,
4920 asection ** secp ATTRIBUTE_UNUSED,
4921 bfd_vma * valp ATTRIBUTE_UNUSED)
4922 {
4923 if ((abfd->flags & DYNAMIC) == 0
4924 && (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
4925 || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE))
4926 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4927
4928 return TRUE;
4929 }
4930
4931 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
4932 #define TARGET_LITTLE_NAME "elf32-i386"
4933 #define ELF_ARCH bfd_arch_i386
4934 #define ELF_TARGET_ID I386_ELF_DATA
4935 #define ELF_MACHINE_CODE EM_386
4936 #define ELF_MAXPAGESIZE 0x1000
4937
4938 #define elf_backend_can_gc_sections 1
4939 #define elf_backend_can_refcount 1
4940 #define elf_backend_want_got_plt 1
4941 #define elf_backend_plt_readonly 1
4942 #define elf_backend_want_plt_sym 0
4943 #define elf_backend_got_header_size 12
4944 #define elf_backend_plt_alignment 4
4945
4946 /* Support RELA for objdump of prelink objects. */
4947 #define elf_info_to_howto elf_i386_info_to_howto_rel
4948 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
4949
4950 #define bfd_elf32_mkobject elf_i386_mkobject
4951
4952 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
4953 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
4954 #define bfd_elf32_bfd_link_hash_table_free elf_i386_link_hash_table_free
4955 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
4956 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
4957
4958 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
4959 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4960 #define elf_backend_check_relocs elf_i386_check_relocs
4961 #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
4962 #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
4963 #define elf_backend_fake_sections elf_i386_fake_sections
4964 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
4965 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
4966 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
4967 #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
4968 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
4969 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
4970 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
4971 #define elf_backend_relocate_section elf_i386_relocate_section
4972 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
4973 #define elf_backend_always_size_sections elf_i386_always_size_sections
4974 #define elf_backend_omit_section_dynsym \
4975 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4976 #define elf_backend_plt_sym_val elf_i386_plt_sym_val
4977 #define elf_backend_hash_symbol elf_i386_hash_symbol
4978 #define elf_backend_add_symbol_hook elf_i386_add_symbol_hook
4979 #undef elf_backend_post_process_headers
4980 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4981
4982 #include "elf32-target.h"
4983
4984 /* FreeBSD support. */
4985
4986 #undef TARGET_LITTLE_SYM
4987 #define TARGET_LITTLE_SYM bfd_elf32_i386_freebsd_vec
4988 #undef TARGET_LITTLE_NAME
4989 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
4990 #undef ELF_OSABI
4991 #define ELF_OSABI ELFOSABI_FREEBSD
4992
4993 /* The kernel recognizes executables as valid only if they carry a
4994 "FreeBSD" label in the ELF header. So we put this label on all
4995 executables and (for simplicity) also all other object files. */
4996
4997 static void
4998 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
4999 {
5000 _bfd_elf_set_osabi (abfd, info);
5001
5002 #ifdef OLD_FREEBSD_ABI_LABEL
5003 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
5004 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
5005 #endif
5006 }
5007
5008 #undef elf_backend_post_process_headers
5009 #define elf_backend_post_process_headers elf_i386_fbsd_post_process_headers
5010 #undef elf32_bed
5011 #define elf32_bed elf32_i386_fbsd_bed
5012
5013 #undef elf_backend_add_symbol_hook
5014
5015 #include "elf32-target.h"
5016
5017 /* Solaris 2. */
5018
5019 #undef TARGET_LITTLE_SYM
5020 #define TARGET_LITTLE_SYM bfd_elf32_i386_sol2_vec
5021 #undef TARGET_LITTLE_NAME
5022 #define TARGET_LITTLE_NAME "elf32-i386-sol2"
5023
5024 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
5025 objects won't be recognized. */
5026 #undef ELF_OSABI
5027
5028 #undef elf32_bed
5029 #define elf32_bed elf32_i386_sol2_bed
5030
5031 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
5032 boundary. */
5033 #undef elf_backend_static_tls_alignment
5034 #define elf_backend_static_tls_alignment 8
5035
5036 /* The Solaris 2 ABI requires a plt symbol on all platforms.
5037
5038 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
5039 File, p.63. */
5040 #undef elf_backend_want_plt_sym
5041 #define elf_backend_want_plt_sym 1
5042
5043 #include "elf32-target.h"
5044
5045 /* Native Client support. */
5046
5047 #undef TARGET_LITTLE_SYM
5048 #define TARGET_LITTLE_SYM bfd_elf32_i386_nacl_vec
5049 #undef TARGET_LITTLE_NAME
5050 #define TARGET_LITTLE_NAME "elf32-i386-nacl"
5051 #undef elf32_bed
5052 #define elf32_bed elf32_i386_nacl_bed
5053
5054 #undef ELF_MAXPAGESIZE
5055 #define ELF_MAXPAGESIZE 0x10000
5056
5057 /* Restore defaults. */
5058 #undef ELF_OSABI
5059 #undef elf_backend_want_plt_sym
5060 #define elf_backend_want_plt_sym 0
5061 #undef elf_backend_post_process_headers
5062 #define elf_backend_post_process_headers _bfd_elf_set_osabi
5063 #undef elf_backend_static_tls_alignment
5064
5065 /* NaCl uses substantially different PLT entries for the same effects. */
5066
5067 #undef elf_backend_plt_alignment
5068 #define elf_backend_plt_alignment 5
5069 #define NACL_PLT_ENTRY_SIZE 64
5070 #define NACLMASK 0xe0 /* 32-byte alignment mask. */
5071
5072 static const bfd_byte elf_i386_nacl_plt0_entry[] =
5073 {
5074 0xff, 0x35, /* pushl contents of address */
5075 0, 0, 0, 0, /* replaced with address of .got + 4. */
5076 0x8b, 0x0d, /* movl contents of address, %ecx */
5077 0, 0, 0, 0, /* replaced with address of .got + 8. */
5078 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
5079 0xff, 0xe1 /* jmp *%ecx */
5080 };
5081
5082 static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
5083 {
5084 0x8b, 0x0d, /* movl contents of address, %ecx */
5085 0, 0, 0, 0, /* replaced with GOT slot address. */
5086 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
5087 0xff, 0xe1, /* jmp *%ecx */
5088
5089 /* Pad to the next 32-byte boundary with nop instructions. */
5090 0x90,
5091 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5092 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5093
5094 /* Lazy GOT entries point here (32-byte aligned). */
5095 0x68, /* pushl immediate */
5096 0, 0, 0, 0, /* replaced with reloc offset. */
5097 0xe9, /* jmp relative */
5098 0, 0, 0, 0, /* replaced with offset to .plt. */
5099
5100 /* Pad to the next 32-byte boundary with nop instructions. */
5101 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5102 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5103 0x90, 0x90
5104 };
5105
5106 static const bfd_byte
5107 elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] =
5108 {
5109 0xff, 0x73, 0x04, /* pushl 4(%ebx) */
5110 0x8b, 0x4b, 0x08, /* mov 0x8(%ebx), %ecx */
5111 0x83, 0xe1, 0xe0, /* and $NACLMASK, %ecx */
5112 0xff, 0xe1, /* jmp *%ecx */
5113
5114 /* This is expected to be the same size as elf_i386_nacl_plt0_entry,
5115 so pad to that size with nop instructions. */
5116 0x90, 0x90, 0x90, 0x90, 0x90, 0x90
5117 };
5118
5119 static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] =
5120 {
5121 0x8b, 0x8b, /* movl offset(%ebx), %ecx */
5122 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
5123 0x83, 0xe1, 0xe0, /* andl $NACLMASK, %ecx */
5124 0xff, 0xe1, /* jmp *%ecx */
5125
5126 /* Pad to the next 32-byte boundary with nop instructions. */
5127 0x90,
5128 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5129 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5130
5131 /* Lazy GOT entries point here (32-byte aligned). */
5132 0x68, /* pushl immediate */
5133 0, 0, 0, 0, /* replaced with offset into relocation table. */
5134 0xe9, /* jmp relative */
5135 0, 0, 0, 0, /* replaced with offset to start of .plt. */
5136
5137 /* Pad to the next 32-byte boundary with nop instructions. */
5138 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5139 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5140 0x90, 0x90
5141 };
5142
5143 static const bfd_byte elf_i386_nacl_eh_frame_plt[] =
5144 {
5145 #if (PLT_CIE_LENGTH != 20 \
5146 || PLT_FDE_LENGTH != 36 \
5147 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
5148 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
5149 # error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!"
5150 #endif
5151 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
5152 0, 0, 0, 0, /* CIE ID */
5153 1, /* CIE version */
5154 'z', 'R', 0, /* Augmentation string */
5155 1, /* Code alignment factor */
5156 0x7c, /* Data alignment factor: -4 */
5157 8, /* Return address column */
5158 1, /* Augmentation size */
5159 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
5160 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
5161 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
5162 DW_CFA_nop, DW_CFA_nop,
5163
5164 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
5165 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
5166 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
5167 0, 0, 0, 0, /* .plt size goes here */
5168 0, /* Augmentation size */
5169 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
5170 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
5171 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
5172 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
5173 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
5174 13, /* Block length */
5175 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
5176 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
5177 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
5178 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
5179 DW_CFA_nop, DW_CFA_nop
5180 };
5181
5182 static const struct elf_i386_plt_layout elf_i386_nacl_plt =
5183 {
5184 elf_i386_nacl_plt0_entry, /* plt0_entry */
5185 sizeof (elf_i386_nacl_plt0_entry), /* plt0_entry_size */
5186 2, /* plt0_got1_offset */
5187 8, /* plt0_got2_offset */
5188 elf_i386_nacl_plt_entry, /* plt_entry */
5189 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
5190 2, /* plt_got_offset */
5191 33, /* plt_reloc_offset */
5192 38, /* plt_plt_offset */
5193 32, /* plt_lazy_offset */
5194 elf_i386_nacl_pic_plt0_entry, /* pic_plt0_entry */
5195 elf_i386_nacl_pic_plt_entry, /* pic_plt_entry */
5196 elf_i386_nacl_eh_frame_plt, /* eh_frame_plt */
5197 sizeof (elf_i386_nacl_eh_frame_plt),/* eh_frame_plt_size */
5198 };
5199
5200 static const struct elf_i386_backend_data elf_i386_nacl_arch_bed =
5201 {
5202 &elf_i386_nacl_plt, /* plt */
5203 0x90, /* plt0_pad_byte: nop insn */
5204 0, /* is_vxworks */
5205 };
5206
5207 #undef elf_backend_arch_data
5208 #define elf_backend_arch_data &elf_i386_nacl_arch_bed
5209
5210 #include "elf32-target.h"
5211
5212 /* VxWorks support. */
5213
5214 #undef TARGET_LITTLE_SYM
5215 #define TARGET_LITTLE_SYM bfd_elf32_i386_vxworks_vec
5216 #undef TARGET_LITTLE_NAME
5217 #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
5218 #undef ELF_OSABI
5219 #undef elf_backend_plt_alignment
5220 #define elf_backend_plt_alignment 4
5221
5222 static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed =
5223 {
5224 &elf_i386_plt, /* plt */
5225 0x90, /* plt0_pad_byte */
5226 1, /* is_vxworks */
5227 };
5228
5229 #undef elf_backend_arch_data
5230 #define elf_backend_arch_data &elf_i386_vxworks_arch_bed
5231
5232 #undef elf_backend_relocs_compatible
5233 #undef elf_backend_post_process_headers
5234 #undef elf_backend_add_symbol_hook
5235 #define elf_backend_add_symbol_hook \
5236 elf_vxworks_add_symbol_hook
5237 #undef elf_backend_link_output_symbol_hook
5238 #define elf_backend_link_output_symbol_hook \
5239 elf_vxworks_link_output_symbol_hook
5240 #undef elf_backend_emit_relocs
5241 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
5242 #undef elf_backend_final_write_processing
5243 #define elf_backend_final_write_processing \
5244 elf_vxworks_final_write_processing
5245 #undef elf_backend_static_tls_alignment
5246
5247 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
5248 define it. */
5249 #undef elf_backend_want_plt_sym
5250 #define elf_backend_want_plt_sym 1
5251
5252 #undef elf32_bed
5253 #define elf32_bed elf32_i386_vxworks_bed
5254
5255 #include "elf32-target.h"
This page took 0.138959 seconds and 4 git commands to generate.