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