abac6b2d10a974fcd5e427f2691f9fa6b01dd6d3
[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 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1274 return FALSE;
1275 break;
1276
1277 /* This relocation describes which C++ vtable entries are actually
1278 used. Record for later use during GC. */
1279 case R_386_GNU_VTENTRY:
1280 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1281 return FALSE;
1282 break;
1283
1284 default:
1285 break;
1286 }
1287 }
1288
1289 return TRUE;
1290 }
1291
1292 /* Return the section that should be marked against GC for a given
1293 relocation. */
1294
1295 static asection *
1296 elf_i386_gc_mark_hook (asection *sec,
1297 struct bfd_link_info *info,
1298 Elf_Internal_Rela *rel,
1299 struct elf_link_hash_entry *h,
1300 Elf_Internal_Sym *sym)
1301 {
1302 if (h != NULL)
1303 switch (ELF32_R_TYPE (rel->r_info))
1304 {
1305 case R_386_GNU_VTINHERIT:
1306 case R_386_GNU_VTENTRY:
1307 return NULL;
1308 }
1309
1310 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1311 }
1312
1313 /* Update the got entry reference counts for the section being removed. */
1314
1315 static bfd_boolean
1316 elf_i386_gc_sweep_hook (bfd *abfd,
1317 struct bfd_link_info *info,
1318 asection *sec,
1319 const Elf_Internal_Rela *relocs)
1320 {
1321 Elf_Internal_Shdr *symtab_hdr;
1322 struct elf_link_hash_entry **sym_hashes;
1323 bfd_signed_vma *local_got_refcounts;
1324 const Elf_Internal_Rela *rel, *relend;
1325
1326 elf_section_data (sec)->local_dynrel = NULL;
1327
1328 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1329 sym_hashes = elf_sym_hashes (abfd);
1330 local_got_refcounts = elf_local_got_refcounts (abfd);
1331
1332 relend = relocs + sec->reloc_count;
1333 for (rel = relocs; rel < relend; rel++)
1334 {
1335 unsigned long r_symndx;
1336 unsigned int r_type;
1337 struct elf_link_hash_entry *h = NULL;
1338
1339 r_symndx = ELF32_R_SYM (rel->r_info);
1340 if (r_symndx >= symtab_hdr->sh_info)
1341 {
1342 struct elf_i386_link_hash_entry *eh;
1343 struct elf_i386_dyn_relocs **pp;
1344 struct elf_i386_dyn_relocs *p;
1345
1346 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1347 while (h->root.type == bfd_link_hash_indirect
1348 || h->root.type == bfd_link_hash_warning)
1349 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1350 eh = (struct elf_i386_link_hash_entry *) h;
1351
1352 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1353 if (p->sec == sec)
1354 {
1355 /* Everything must go for SEC. */
1356 *pp = p->next;
1357 break;
1358 }
1359 }
1360
1361 r_type = ELF32_R_TYPE (rel->r_info);
1362 r_type = elf_i386_tls_transition (info, r_type, h != NULL);
1363 switch (r_type)
1364 {
1365 case R_386_TLS_LDM:
1366 if (elf_i386_hash_table (info)->tls_ldm_got.refcount > 0)
1367 elf_i386_hash_table (info)->tls_ldm_got.refcount -= 1;
1368 break;
1369
1370 case R_386_TLS_GD:
1371 case R_386_TLS_GOTDESC:
1372 case R_386_TLS_DESC_CALL:
1373 case R_386_TLS_IE_32:
1374 case R_386_TLS_IE:
1375 case R_386_TLS_GOTIE:
1376 case R_386_GOT32:
1377 if (h != NULL)
1378 {
1379 if (h->got.refcount > 0)
1380 h->got.refcount -= 1;
1381 }
1382 else if (local_got_refcounts != NULL)
1383 {
1384 if (local_got_refcounts[r_symndx] > 0)
1385 local_got_refcounts[r_symndx] -= 1;
1386 }
1387 break;
1388
1389 case R_386_32:
1390 case R_386_PC32:
1391 if (info->shared)
1392 break;
1393 /* Fall through */
1394
1395 case R_386_PLT32:
1396 if (h != NULL)
1397 {
1398 if (h->plt.refcount > 0)
1399 h->plt.refcount -= 1;
1400 }
1401 break;
1402
1403 default:
1404 break;
1405 }
1406 }
1407
1408 return TRUE;
1409 }
1410
1411 /* Adjust a symbol defined by a dynamic object and referenced by a
1412 regular object. The current definition is in some section of the
1413 dynamic object, but we're not including those sections. We have to
1414 change the definition to something the rest of the link can
1415 understand. */
1416
1417 static bfd_boolean
1418 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
1419 struct elf_link_hash_entry *h)
1420 {
1421 struct elf_i386_link_hash_table *htab;
1422 asection *s;
1423
1424 /* If this is a function, put it in the procedure linkage table. We
1425 will fill in the contents of the procedure linkage table later,
1426 when we know the address of the .got section. */
1427 if (h->type == STT_FUNC
1428 || h->needs_plt)
1429 {
1430 if (h->plt.refcount <= 0
1431 || SYMBOL_CALLS_LOCAL (info, h)
1432 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1433 && h->root.type == bfd_link_hash_undefweak))
1434 {
1435 /* This case can occur if we saw a PLT32 reloc in an input
1436 file, but the symbol was never referred to by a dynamic
1437 object, or if all references were garbage collected. In
1438 such a case, we don't actually need to build a procedure
1439 linkage table, and we can just do a PC32 reloc instead. */
1440 h->plt.offset = (bfd_vma) -1;
1441 h->needs_plt = 0;
1442 }
1443
1444 return TRUE;
1445 }
1446 else
1447 /* It's possible that we incorrectly decided a .plt reloc was
1448 needed for an R_386_PC32 reloc to a non-function sym in
1449 check_relocs. We can't decide accurately between function and
1450 non-function syms in check-relocs; Objects loaded later in
1451 the link may change h->type. So fix it now. */
1452 h->plt.offset = (bfd_vma) -1;
1453
1454 /* If this is a weak symbol, and there is a real definition, the
1455 processor independent code will have arranged for us to see the
1456 real definition first, and we can just use the same value. */
1457 if (h->u.weakdef != NULL)
1458 {
1459 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1460 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1461 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1462 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1463 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1464 h->non_got_ref = h->u.weakdef->non_got_ref;
1465 return TRUE;
1466 }
1467
1468 /* This is a reference to a symbol defined by a dynamic object which
1469 is not a function. */
1470
1471 /* If we are creating a shared library, we must presume that the
1472 only references to the symbol are via the global offset table.
1473 For such cases we need not do anything here; the relocations will
1474 be handled correctly by relocate_section. */
1475 if (info->shared)
1476 return TRUE;
1477
1478 /* If there are no references to this symbol that do not use the
1479 GOT, we don't need to generate a copy reloc. */
1480 if (!h->non_got_ref)
1481 return TRUE;
1482
1483 /* If -z nocopyreloc was given, we won't generate them either. */
1484 if (info->nocopyreloc)
1485 {
1486 h->non_got_ref = 0;
1487 return TRUE;
1488 }
1489
1490 htab = elf_i386_hash_table (info);
1491
1492 /* If there aren't any dynamic relocs in read-only sections, then
1493 we can keep the dynamic relocs and avoid the copy reloc. This
1494 doesn't work on VxWorks, where we can not have dynamic relocations
1495 (other than copy and jump slot relocations) in an executable. */
1496 if (ELIMINATE_COPY_RELOCS && !htab->is_vxworks)
1497 {
1498 struct elf_i386_link_hash_entry * eh;
1499 struct elf_i386_dyn_relocs *p;
1500
1501 eh = (struct elf_i386_link_hash_entry *) h;
1502 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1503 {
1504 s = p->sec->output_section;
1505 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1506 break;
1507 }
1508
1509 if (p == NULL)
1510 {
1511 h->non_got_ref = 0;
1512 return TRUE;
1513 }
1514 }
1515
1516 if (h->size == 0)
1517 {
1518 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1519 h->root.root.string);
1520 return TRUE;
1521 }
1522
1523 /* We must allocate the symbol in our .dynbss section, which will
1524 become part of the .bss section of the executable. There will be
1525 an entry for this symbol in the .dynsym section. The dynamic
1526 object will contain position independent code, so all references
1527 from the dynamic object to this symbol will go through the global
1528 offset table. The dynamic linker will use the .dynsym entry to
1529 determine the address it must put in the global offset table, so
1530 both the dynamic object and the regular object will refer to the
1531 same memory location for the variable. */
1532
1533 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1534 copy the initial value out of the dynamic object and into the
1535 runtime process image. */
1536 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1537 {
1538 htab->srelbss->size += sizeof (Elf32_External_Rel);
1539 h->needs_copy = 1;
1540 }
1541
1542 s = htab->sdynbss;
1543
1544 return _bfd_elf_adjust_dynamic_copy (h, s);
1545 }
1546
1547 /* Allocate space in .plt, .got and associated reloc sections for
1548 dynamic relocs. */
1549
1550 static bfd_boolean
1551 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1552 {
1553 struct bfd_link_info *info;
1554 struct elf_i386_link_hash_table *htab;
1555 struct elf_i386_link_hash_entry *eh;
1556 struct elf_i386_dyn_relocs *p;
1557
1558 if (h->root.type == bfd_link_hash_indirect)
1559 return TRUE;
1560
1561 if (h->root.type == bfd_link_hash_warning)
1562 /* When warning symbols are created, they **replace** the "real"
1563 entry in the hash table, thus we never get to see the real
1564 symbol in a hash traversal. So look at it now. */
1565 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1566
1567 info = (struct bfd_link_info *) inf;
1568 htab = elf_i386_hash_table (info);
1569
1570 if (htab->elf.dynamic_sections_created
1571 && h->plt.refcount > 0)
1572 {
1573 /* Make sure this symbol is output as a dynamic symbol.
1574 Undefined weak syms won't yet be marked as dynamic. */
1575 if (h->dynindx == -1
1576 && !h->forced_local)
1577 {
1578 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1579 return FALSE;
1580 }
1581
1582 if (info->shared
1583 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1584 {
1585 asection *s = htab->splt;
1586
1587 /* If this is the first .plt entry, make room for the special
1588 first entry. */
1589 if (s->size == 0)
1590 s->size += PLT_ENTRY_SIZE;
1591
1592 h->plt.offset = s->size;
1593
1594 /* If this symbol is not defined in a regular file, and we are
1595 not generating a shared library, then set the symbol to this
1596 location in the .plt. This is required to make function
1597 pointers compare as equal between the normal executable and
1598 the shared library. */
1599 if (! info->shared
1600 && !h->def_regular)
1601 {
1602 h->root.u.def.section = s;
1603 h->root.u.def.value = h->plt.offset;
1604 }
1605
1606 /* Make room for this entry. */
1607 s->size += PLT_ENTRY_SIZE;
1608
1609 /* We also need to make an entry in the .got.plt section, which
1610 will be placed in the .got section by the linker script. */
1611 htab->sgotplt->size += 4;
1612
1613 /* We also need to make an entry in the .rel.plt section. */
1614 htab->srelplt->size += sizeof (Elf32_External_Rel);
1615 htab->next_tls_desc_index++;
1616
1617 if (htab->is_vxworks && !info->shared)
1618 {
1619 /* VxWorks has a second set of relocations for each PLT entry
1620 in executables. They go in a separate relocation section,
1621 which is processed by the kernel loader. */
1622
1623 /* There are two relocations for the initial PLT entry: an
1624 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
1625 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
1626
1627 if (h->plt.offset == PLT_ENTRY_SIZE)
1628 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1629
1630 /* There are two extra relocations for each subsequent PLT entry:
1631 an R_386_32 relocation for the GOT entry, and an R_386_32
1632 relocation for the PLT entry. */
1633
1634 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1635 }
1636 }
1637 else
1638 {
1639 h->plt.offset = (bfd_vma) -1;
1640 h->needs_plt = 0;
1641 }
1642 }
1643 else
1644 {
1645 h->plt.offset = (bfd_vma) -1;
1646 h->needs_plt = 0;
1647 }
1648
1649 eh = (struct elf_i386_link_hash_entry *) h;
1650 eh->tlsdesc_got = (bfd_vma) -1;
1651
1652 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
1653 make it a R_386_TLS_LE_32 requiring no TLS entry. */
1654 if (h->got.refcount > 0
1655 && !info->shared
1656 && h->dynindx == -1
1657 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
1658 h->got.offset = (bfd_vma) -1;
1659 else if (h->got.refcount > 0)
1660 {
1661 asection *s;
1662 bfd_boolean dyn;
1663 int tls_type = elf_i386_hash_entry(h)->tls_type;
1664
1665 /* Make sure this symbol is output as a dynamic symbol.
1666 Undefined weak syms won't yet be marked as dynamic. */
1667 if (h->dynindx == -1
1668 && !h->forced_local)
1669 {
1670 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1671 return FALSE;
1672 }
1673
1674 s = htab->sgot;
1675 if (GOT_TLS_GDESC_P (tls_type))
1676 {
1677 eh->tlsdesc_got = htab->sgotplt->size
1678 - elf_i386_compute_jump_table_size (htab);
1679 htab->sgotplt->size += 8;
1680 h->got.offset = (bfd_vma) -2;
1681 }
1682 if (! GOT_TLS_GDESC_P (tls_type)
1683 || GOT_TLS_GD_P (tls_type))
1684 {
1685 h->got.offset = s->size;
1686 s->size += 4;
1687 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
1688 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
1689 s->size += 4;
1690 }
1691 dyn = htab->elf.dynamic_sections_created;
1692 /* R_386_TLS_IE_32 needs one dynamic relocation,
1693 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
1694 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
1695 need two), R_386_TLS_GD needs one if local symbol and two if
1696 global. */
1697 if (tls_type == GOT_TLS_IE_BOTH)
1698 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1699 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
1700 || (tls_type & GOT_TLS_IE))
1701 htab->srelgot->size += sizeof (Elf32_External_Rel);
1702 else if (GOT_TLS_GD_P (tls_type))
1703 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1704 else if (! GOT_TLS_GDESC_P (tls_type)
1705 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1706 || h->root.type != bfd_link_hash_undefweak)
1707 && (info->shared
1708 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1709 htab->srelgot->size += sizeof (Elf32_External_Rel);
1710 if (GOT_TLS_GDESC_P (tls_type))
1711 htab->srelplt->size += sizeof (Elf32_External_Rel);
1712 }
1713 else
1714 h->got.offset = (bfd_vma) -1;
1715
1716 if (eh->dyn_relocs == NULL)
1717 return TRUE;
1718
1719 /* In the shared -Bsymbolic case, discard space allocated for
1720 dynamic pc-relative relocs against symbols which turn out to be
1721 defined in regular objects. For the normal shared case, discard
1722 space for pc-relative relocs that have become local due to symbol
1723 visibility changes. */
1724
1725 if (info->shared)
1726 {
1727 /* The only reloc that uses pc_count is R_386_PC32, which will
1728 appear on a call or on something like ".long foo - .". We
1729 want calls to protected symbols to resolve directly to the
1730 function rather than going via the plt. If people want
1731 function pointer comparisons to work as expected then they
1732 should avoid writing assembly like ".long foo - .". */
1733 if (SYMBOL_CALLS_LOCAL (info, h))
1734 {
1735 struct elf_i386_dyn_relocs **pp;
1736
1737 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1738 {
1739 p->count -= p->pc_count;
1740 p->pc_count = 0;
1741 if (p->count == 0)
1742 *pp = p->next;
1743 else
1744 pp = &p->next;
1745 }
1746 }
1747
1748 /* Also discard relocs on undefined weak syms with non-default
1749 visibility. */
1750 if (eh->dyn_relocs != NULL
1751 && h->root.type == bfd_link_hash_undefweak)
1752 {
1753 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1754 eh->dyn_relocs = NULL;
1755
1756 /* Make sure undefined weak symbols are output as a dynamic
1757 symbol in PIEs. */
1758 else if (h->dynindx == -1
1759 && !h->forced_local)
1760 {
1761 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1762 return FALSE;
1763 }
1764 }
1765 }
1766 else if (ELIMINATE_COPY_RELOCS)
1767 {
1768 /* For the non-shared case, discard space for relocs against
1769 symbols which turn out to need copy relocs or are not
1770 dynamic. */
1771
1772 if (!h->non_got_ref
1773 && ((h->def_dynamic
1774 && !h->def_regular)
1775 || (htab->elf.dynamic_sections_created
1776 && (h->root.type == bfd_link_hash_undefweak
1777 || h->root.type == bfd_link_hash_undefined))))
1778 {
1779 /* Make sure this symbol is output as a dynamic symbol.
1780 Undefined weak syms won't yet be marked as dynamic. */
1781 if (h->dynindx == -1
1782 && !h->forced_local)
1783 {
1784 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1785 return FALSE;
1786 }
1787
1788 /* If that succeeded, we know we'll be keeping all the
1789 relocs. */
1790 if (h->dynindx != -1)
1791 goto keep;
1792 }
1793
1794 eh->dyn_relocs = NULL;
1795
1796 keep: ;
1797 }
1798
1799 /* Finally, allocate space. */
1800 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1801 {
1802 asection *sreloc = elf_section_data (p->sec)->sreloc;
1803 sreloc->size += p->count * sizeof (Elf32_External_Rel);
1804 }
1805
1806 return TRUE;
1807 }
1808
1809 /* Find any dynamic relocs that apply to read-only sections. */
1810
1811 static bfd_boolean
1812 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1813 {
1814 struct elf_i386_link_hash_entry *eh;
1815 struct elf_i386_dyn_relocs *p;
1816
1817 if (h->root.type == bfd_link_hash_warning)
1818 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1819
1820 eh = (struct elf_i386_link_hash_entry *) h;
1821 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1822 {
1823 asection *s = p->sec->output_section;
1824
1825 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1826 {
1827 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1828
1829 info->flags |= DF_TEXTREL;
1830
1831 /* Not an error, just cut short the traversal. */
1832 return FALSE;
1833 }
1834 }
1835 return TRUE;
1836 }
1837
1838 /* Set the sizes of the dynamic sections. */
1839
1840 static bfd_boolean
1841 elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1842 struct bfd_link_info *info)
1843 {
1844 struct elf_i386_link_hash_table *htab;
1845 bfd *dynobj;
1846 asection *s;
1847 bfd_boolean relocs;
1848 bfd *ibfd;
1849
1850 htab = elf_i386_hash_table (info);
1851 dynobj = htab->elf.dynobj;
1852 if (dynobj == NULL)
1853 abort ();
1854
1855 if (htab->elf.dynamic_sections_created)
1856 {
1857 /* Set the contents of the .interp section to the interpreter. */
1858 if (info->executable)
1859 {
1860 s = bfd_get_section_by_name (dynobj, ".interp");
1861 if (s == NULL)
1862 abort ();
1863 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1864 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1865 }
1866 }
1867
1868 /* Set up .got offsets for local syms, and space for local dynamic
1869 relocs. */
1870 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1871 {
1872 bfd_signed_vma *local_got;
1873 bfd_signed_vma *end_local_got;
1874 char *local_tls_type;
1875 bfd_vma *local_tlsdesc_gotent;
1876 bfd_size_type locsymcount;
1877 Elf_Internal_Shdr *symtab_hdr;
1878 asection *srel;
1879
1880 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1881 continue;
1882
1883 for (s = ibfd->sections; s != NULL; s = s->next)
1884 {
1885 struct elf_i386_dyn_relocs *p;
1886
1887 for (p = ((struct elf_i386_dyn_relocs *)
1888 elf_section_data (s)->local_dynrel);
1889 p != NULL;
1890 p = p->next)
1891 {
1892 if (!bfd_is_abs_section (p->sec)
1893 && bfd_is_abs_section (p->sec->output_section))
1894 {
1895 /* Input section has been discarded, either because
1896 it is a copy of a linkonce section or due to
1897 linker script /DISCARD/, so we'll be discarding
1898 the relocs too. */
1899 }
1900 else if (p->count != 0)
1901 {
1902 srel = elf_section_data (p->sec)->sreloc;
1903 srel->size += p->count * sizeof (Elf32_External_Rel);
1904 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1905 info->flags |= DF_TEXTREL;
1906 }
1907 }
1908 }
1909
1910 local_got = elf_local_got_refcounts (ibfd);
1911 if (!local_got)
1912 continue;
1913
1914 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1915 locsymcount = symtab_hdr->sh_info;
1916 end_local_got = local_got + locsymcount;
1917 local_tls_type = elf_i386_local_got_tls_type (ibfd);
1918 local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
1919 s = htab->sgot;
1920 srel = htab->srelgot;
1921 for (; local_got < end_local_got;
1922 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
1923 {
1924 *local_tlsdesc_gotent = (bfd_vma) -1;
1925 if (*local_got > 0)
1926 {
1927 if (GOT_TLS_GDESC_P (*local_tls_type))
1928 {
1929 *local_tlsdesc_gotent = htab->sgotplt->size
1930 - elf_i386_compute_jump_table_size (htab);
1931 htab->sgotplt->size += 8;
1932 *local_got = (bfd_vma) -2;
1933 }
1934 if (! GOT_TLS_GDESC_P (*local_tls_type)
1935 || GOT_TLS_GD_P (*local_tls_type))
1936 {
1937 *local_got = s->size;
1938 s->size += 4;
1939 if (GOT_TLS_GD_P (*local_tls_type)
1940 || *local_tls_type == GOT_TLS_IE_BOTH)
1941 s->size += 4;
1942 }
1943 if (info->shared
1944 || GOT_TLS_GD_ANY_P (*local_tls_type)
1945 || (*local_tls_type & GOT_TLS_IE))
1946 {
1947 if (*local_tls_type == GOT_TLS_IE_BOTH)
1948 srel->size += 2 * sizeof (Elf32_External_Rel);
1949 else if (GOT_TLS_GD_P (*local_tls_type)
1950 || ! GOT_TLS_GDESC_P (*local_tls_type))
1951 srel->size += sizeof (Elf32_External_Rel);
1952 if (GOT_TLS_GDESC_P (*local_tls_type))
1953 htab->srelplt->size += sizeof (Elf32_External_Rel);
1954 }
1955 }
1956 else
1957 *local_got = (bfd_vma) -1;
1958 }
1959 }
1960
1961 if (htab->tls_ldm_got.refcount > 0)
1962 {
1963 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
1964 relocs. */
1965 htab->tls_ldm_got.offset = htab->sgot->size;
1966 htab->sgot->size += 8;
1967 htab->srelgot->size += sizeof (Elf32_External_Rel);
1968 }
1969 else
1970 htab->tls_ldm_got.offset = -1;
1971
1972 /* Allocate global sym .plt and .got entries, and space for global
1973 sym dynamic relocs. */
1974 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1975
1976 /* For every jump slot reserved in the sgotplt, reloc_count is
1977 incremented. However, when we reserve space for TLS descriptors,
1978 it's not incremented, so in order to compute the space reserved
1979 for them, it suffices to multiply the reloc count by the jump
1980 slot size. */
1981 if (htab->srelplt)
1982 htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
1983
1984 /* We now have determined the sizes of the various dynamic sections.
1985 Allocate memory for them. */
1986 relocs = FALSE;
1987 for (s = dynobj->sections; s != NULL; s = s->next)
1988 {
1989 bfd_boolean strip_section = TRUE;
1990
1991 if ((s->flags & SEC_LINKER_CREATED) == 0)
1992 continue;
1993
1994 if (s == htab->splt
1995 || s == htab->sgot
1996 || s == htab->sgotplt
1997 || s == htab->sdynbss)
1998 {
1999 /* Strip this section if we don't need it; see the
2000 comment below. */
2001 /* We'd like to strip these sections if they aren't needed, but if
2002 we've exported dynamic symbols from them we must leave them.
2003 It's too late to tell BFD to get rid of the symbols. */
2004
2005 if (htab->elf.hplt != NULL)
2006 strip_section = FALSE;
2007 }
2008 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
2009 {
2010 if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
2011 relocs = TRUE;
2012
2013 /* We use the reloc_count field as a counter if we need
2014 to copy relocs into the output file. */
2015 s->reloc_count = 0;
2016 }
2017 else
2018 {
2019 /* It's not one of our sections, so don't allocate space. */
2020 continue;
2021 }
2022
2023 if (s->size == 0)
2024 {
2025 /* If we don't need this section, strip it from the
2026 output file. This is mostly to handle .rel.bss and
2027 .rel.plt. We must create both sections in
2028 create_dynamic_sections, because they must be created
2029 before the linker maps input sections to output
2030 sections. The linker does that before
2031 adjust_dynamic_symbol is called, and it is that
2032 function which decides whether anything needs to go
2033 into these sections. */
2034 if (strip_section)
2035 s->flags |= SEC_EXCLUDE;
2036 continue;
2037 }
2038
2039 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2040 continue;
2041
2042 /* Allocate memory for the section contents. We use bfd_zalloc
2043 here in case unused entries are not reclaimed before the
2044 section's contents are written out. This should not happen,
2045 but this way if it does, we get a R_386_NONE reloc instead
2046 of garbage. */
2047 s->contents = bfd_zalloc (dynobj, s->size);
2048 if (s->contents == NULL)
2049 return FALSE;
2050 }
2051
2052 if (htab->elf.dynamic_sections_created)
2053 {
2054 /* Add some entries to the .dynamic section. We fill in the
2055 values later, in elf_i386_finish_dynamic_sections, but we
2056 must add the entries now so that we get the correct size for
2057 the .dynamic section. The DT_DEBUG entry is filled in by the
2058 dynamic linker and used by the debugger. */
2059 #define add_dynamic_entry(TAG, VAL) \
2060 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2061
2062 if (info->executable)
2063 {
2064 if (!add_dynamic_entry (DT_DEBUG, 0))
2065 return FALSE;
2066 }
2067
2068 if (htab->splt->size != 0)
2069 {
2070 if (!add_dynamic_entry (DT_PLTGOT, 0)
2071 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2072 || !add_dynamic_entry (DT_PLTREL, DT_REL)
2073 || !add_dynamic_entry (DT_JMPREL, 0))
2074 return FALSE;
2075 }
2076
2077 if (relocs)
2078 {
2079 if (!add_dynamic_entry (DT_REL, 0)
2080 || !add_dynamic_entry (DT_RELSZ, 0)
2081 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
2082 return FALSE;
2083
2084 /* If any dynamic relocs apply to a read-only section,
2085 then we need a DT_TEXTREL entry. */
2086 if ((info->flags & DF_TEXTREL) == 0)
2087 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2088 (PTR) info);
2089
2090 if ((info->flags & DF_TEXTREL) != 0)
2091 {
2092 if (!add_dynamic_entry (DT_TEXTREL, 0))
2093 return FALSE;
2094 }
2095 }
2096 }
2097 #undef add_dynamic_entry
2098
2099 return TRUE;
2100 }
2101
2102 static bfd_boolean
2103 elf_i386_always_size_sections (bfd *output_bfd,
2104 struct bfd_link_info *info)
2105 {
2106 asection *tls_sec = elf_hash_table (info)->tls_sec;
2107
2108 if (tls_sec)
2109 {
2110 struct elf_link_hash_entry *tlsbase;
2111
2112 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2113 "_TLS_MODULE_BASE_",
2114 FALSE, FALSE, FALSE);
2115
2116 if (tlsbase && tlsbase->type == STT_TLS)
2117 {
2118 struct bfd_link_hash_entry *bh = NULL;
2119 const struct elf_backend_data *bed
2120 = get_elf_backend_data (output_bfd);
2121
2122 if (!(_bfd_generic_link_add_one_symbol
2123 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2124 tls_sec, 0, NULL, FALSE,
2125 bed->collect, &bh)))
2126 return FALSE;
2127 tlsbase = (struct elf_link_hash_entry *)bh;
2128 tlsbase->def_regular = 1;
2129 tlsbase->other = STV_HIDDEN;
2130 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2131 }
2132 }
2133
2134 return TRUE;
2135 }
2136
2137 /* Set the correct type for an x86 ELF section. We do this by the
2138 section name, which is a hack, but ought to work. */
2139
2140 static bfd_boolean
2141 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2142 Elf_Internal_Shdr *hdr,
2143 asection *sec)
2144 {
2145 register const char *name;
2146
2147 name = bfd_get_section_name (abfd, sec);
2148
2149 /* This is an ugly, but unfortunately necessary hack that is
2150 needed when producing EFI binaries on x86. It tells
2151 elf.c:elf_fake_sections() not to consider ".reloc" as a section
2152 containing ELF relocation info. We need this hack in order to
2153 be able to generate ELF binaries that can be translated into
2154 EFI applications (which are essentially COFF objects). Those
2155 files contain a COFF ".reloc" section inside an ELFNN object,
2156 which would normally cause BFD to segfault because it would
2157 attempt to interpret this section as containing relocation
2158 entries for section "oc". With this hack enabled, ".reloc"
2159 will be treated as a normal data section, which will avoid the
2160 segfault. However, you won't be able to create an ELFNN binary
2161 with a section named "oc" that needs relocations, but that's
2162 the kind of ugly side-effects you get when detecting section
2163 types based on their names... In practice, this limitation is
2164 unlikely to bite. */
2165 if (strcmp (name, ".reloc") == 0)
2166 hdr->sh_type = SHT_PROGBITS;
2167
2168 return TRUE;
2169 }
2170
2171 /* Return the base VMA address which should be subtracted from real addresses
2172 when resolving @dtpoff relocation.
2173 This is PT_TLS segment p_vaddr. */
2174
2175 static bfd_vma
2176 dtpoff_base (struct bfd_link_info *info)
2177 {
2178 /* If tls_sec is NULL, we should have signalled an error already. */
2179 if (elf_hash_table (info)->tls_sec == NULL)
2180 return 0;
2181 return elf_hash_table (info)->tls_sec->vma;
2182 }
2183
2184 /* Return the relocation value for @tpoff relocation
2185 if STT_TLS virtual address is ADDRESS. */
2186
2187 static bfd_vma
2188 tpoff (struct bfd_link_info *info, bfd_vma address)
2189 {
2190 struct elf_link_hash_table *htab = elf_hash_table (info);
2191
2192 /* If tls_sec is NULL, we should have signalled an error already. */
2193 if (htab->tls_sec == NULL)
2194 return 0;
2195 return htab->tls_size + htab->tls_sec->vma - address;
2196 }
2197
2198 /* Relocate an i386 ELF section. */
2199
2200 static bfd_boolean
2201 elf_i386_relocate_section (bfd *output_bfd,
2202 struct bfd_link_info *info,
2203 bfd *input_bfd,
2204 asection *input_section,
2205 bfd_byte *contents,
2206 Elf_Internal_Rela *relocs,
2207 Elf_Internal_Sym *local_syms,
2208 asection **local_sections)
2209 {
2210 struct elf_i386_link_hash_table *htab;
2211 Elf_Internal_Shdr *symtab_hdr;
2212 struct elf_link_hash_entry **sym_hashes;
2213 bfd_vma *local_got_offsets;
2214 bfd_vma *local_tlsdesc_gotents;
2215 Elf_Internal_Rela *rel;
2216 Elf_Internal_Rela *relend;
2217
2218 htab = elf_i386_hash_table (info);
2219 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2220 sym_hashes = elf_sym_hashes (input_bfd);
2221 local_got_offsets = elf_local_got_offsets (input_bfd);
2222 local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
2223
2224 rel = relocs;
2225 relend = relocs + input_section->reloc_count;
2226 for (; rel < relend; rel++)
2227 {
2228 unsigned int r_type;
2229 reloc_howto_type *howto;
2230 unsigned long r_symndx;
2231 struct elf_link_hash_entry *h;
2232 Elf_Internal_Sym *sym;
2233 asection *sec;
2234 bfd_vma off, offplt;
2235 bfd_vma relocation;
2236 bfd_boolean unresolved_reloc;
2237 bfd_reloc_status_type r;
2238 unsigned int indx;
2239 int tls_type;
2240
2241 r_type = ELF32_R_TYPE (rel->r_info);
2242 if (r_type == R_386_GNU_VTINHERIT
2243 || r_type == R_386_GNU_VTENTRY)
2244 continue;
2245
2246 if ((indx = r_type) >= R_386_standard
2247 && ((indx = r_type - R_386_ext_offset) - R_386_standard
2248 >= R_386_ext - R_386_standard)
2249 && ((indx = r_type - R_386_tls_offset) - R_386_ext
2250 >= R_386_tls - R_386_ext))
2251 {
2252 (*_bfd_error_handler)
2253 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
2254 input_bfd, input_section, r_type);
2255 bfd_set_error (bfd_error_bad_value);
2256 return FALSE;
2257 }
2258 howto = elf_howto_table + indx;
2259
2260 r_symndx = ELF32_R_SYM (rel->r_info);
2261 h = NULL;
2262 sym = NULL;
2263 sec = NULL;
2264 unresolved_reloc = FALSE;
2265 if (r_symndx < symtab_hdr->sh_info)
2266 {
2267 sym = local_syms + r_symndx;
2268 sec = local_sections[r_symndx];
2269 relocation = (sec->output_section->vma
2270 + sec->output_offset
2271 + sym->st_value);
2272
2273 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2274 && ((sec->flags & SEC_MERGE) != 0
2275 || (info->relocatable
2276 && sec->output_offset != 0)))
2277 {
2278 bfd_vma addend;
2279 bfd_byte *where = contents + rel->r_offset;
2280
2281 switch (howto->size)
2282 {
2283 case 0:
2284 addend = bfd_get_8 (input_bfd, where);
2285 if (howto->pc_relative)
2286 {
2287 addend = (addend ^ 0x80) - 0x80;
2288 addend += 1;
2289 }
2290 break;
2291 case 1:
2292 addend = bfd_get_16 (input_bfd, where);
2293 if (howto->pc_relative)
2294 {
2295 addend = (addend ^ 0x8000) - 0x8000;
2296 addend += 2;
2297 }
2298 break;
2299 case 2:
2300 addend = bfd_get_32 (input_bfd, where);
2301 if (howto->pc_relative)
2302 {
2303 addend = (addend ^ 0x80000000) - 0x80000000;
2304 addend += 4;
2305 }
2306 break;
2307 default:
2308 abort ();
2309 }
2310
2311 if (info->relocatable)
2312 addend += sec->output_offset;
2313 else
2314 {
2315 asection *msec = sec;
2316 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2317 addend);
2318 addend -= relocation;
2319 addend += msec->output_section->vma + msec->output_offset;
2320 }
2321
2322 switch (howto->size)
2323 {
2324 case 0:
2325 /* FIXME: overflow checks. */
2326 if (howto->pc_relative)
2327 addend -= 1;
2328 bfd_put_8 (input_bfd, addend, where);
2329 break;
2330 case 1:
2331 if (howto->pc_relative)
2332 addend -= 2;
2333 bfd_put_16 (input_bfd, addend, where);
2334 break;
2335 case 2:
2336 if (howto->pc_relative)
2337 addend -= 4;
2338 bfd_put_32 (input_bfd, addend, where);
2339 break;
2340 }
2341 }
2342 }
2343 else
2344 {
2345 bfd_boolean warned;
2346
2347 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2348 r_symndx, symtab_hdr, sym_hashes,
2349 h, sec, relocation,
2350 unresolved_reloc, warned);
2351 }
2352
2353 if (sec != NULL && elf_discarded_section (sec))
2354 {
2355 /* For relocs against symbols from removed linkonce sections,
2356 or sections discarded by a linker script, we just want the
2357 section contents zeroed. Avoid any special processing. */
2358 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2359 rel->r_info = 0;
2360 rel->r_addend = 0;
2361 continue;
2362 }
2363
2364 if (info->relocatable)
2365 continue;
2366
2367 switch (r_type)
2368 {
2369 case R_386_GOT32:
2370 /* Relocation is to the entry for this symbol in the global
2371 offset table. */
2372 if (htab->sgot == NULL)
2373 abort ();
2374
2375 if (h != NULL)
2376 {
2377 bfd_boolean dyn;
2378
2379 off = h->got.offset;
2380 dyn = htab->elf.dynamic_sections_created;
2381 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2382 || (info->shared
2383 && SYMBOL_REFERENCES_LOCAL (info, h))
2384 || (ELF_ST_VISIBILITY (h->other)
2385 && h->root.type == bfd_link_hash_undefweak))
2386 {
2387 /* This is actually a static link, or it is a
2388 -Bsymbolic link and the symbol is defined
2389 locally, or the symbol was forced to be local
2390 because of a version file. We must initialize
2391 this entry in the global offset table. Since the
2392 offset must always be a multiple of 4, we use the
2393 least significant bit to record whether we have
2394 initialized it already.
2395
2396 When doing a dynamic link, we create a .rel.got
2397 relocation entry to initialize the value. This
2398 is done in the finish_dynamic_symbol routine. */
2399 if ((off & 1) != 0)
2400 off &= ~1;
2401 else
2402 {
2403 bfd_put_32 (output_bfd, relocation,
2404 htab->sgot->contents + off);
2405 h->got.offset |= 1;
2406 }
2407 }
2408 else
2409 unresolved_reloc = FALSE;
2410 }
2411 else
2412 {
2413 if (local_got_offsets == NULL)
2414 abort ();
2415
2416 off = local_got_offsets[r_symndx];
2417
2418 /* The offset must always be a multiple of 4. We use
2419 the least significant bit to record whether we have
2420 already generated the necessary reloc. */
2421 if ((off & 1) != 0)
2422 off &= ~1;
2423 else
2424 {
2425 bfd_put_32 (output_bfd, relocation,
2426 htab->sgot->contents + off);
2427
2428 if (info->shared)
2429 {
2430 asection *s;
2431 Elf_Internal_Rela outrel;
2432 bfd_byte *loc;
2433
2434 s = htab->srelgot;
2435 if (s == NULL)
2436 abort ();
2437
2438 outrel.r_offset = (htab->sgot->output_section->vma
2439 + htab->sgot->output_offset
2440 + off);
2441 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2442 loc = s->contents;
2443 loc += s->reloc_count++ * sizeof (Elf32_External_Rel);
2444 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2445 }
2446
2447 local_got_offsets[r_symndx] |= 1;
2448 }
2449 }
2450
2451 if (off >= (bfd_vma) -2)
2452 abort ();
2453
2454 relocation = htab->sgot->output_section->vma
2455 + htab->sgot->output_offset + off
2456 - htab->sgotplt->output_section->vma
2457 - htab->sgotplt->output_offset;
2458 break;
2459
2460 case R_386_GOTOFF:
2461 /* Relocation is relative to the start of the global offset
2462 table. */
2463
2464 /* Check to make sure it isn't a protected function symbol
2465 for shared library since it may not be local when used
2466 as function address. */
2467 if (info->shared
2468 && !info->executable
2469 && h
2470 && h->def_regular
2471 && h->type == STT_FUNC
2472 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2473 {
2474 (*_bfd_error_handler)
2475 (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
2476 input_bfd, h->root.root.string);
2477 bfd_set_error (bfd_error_bad_value);
2478 return FALSE;
2479 }
2480
2481 /* Note that sgot is not involved in this
2482 calculation. We always want the start of .got.plt. If we
2483 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2484 permitted by the ABI, we might have to change this
2485 calculation. */
2486 relocation -= htab->sgotplt->output_section->vma
2487 + htab->sgotplt->output_offset;
2488 break;
2489
2490 case R_386_GOTPC:
2491 /* Use global offset table as symbol value. */
2492 relocation = htab->sgotplt->output_section->vma
2493 + htab->sgotplt->output_offset;
2494 unresolved_reloc = FALSE;
2495 break;
2496
2497 case R_386_PLT32:
2498 /* Relocation is to the entry for this symbol in the
2499 procedure linkage table. */
2500
2501 /* Resolve a PLT32 reloc against a local symbol directly,
2502 without using the procedure linkage table. */
2503 if (h == NULL)
2504 break;
2505
2506 if (h->plt.offset == (bfd_vma) -1
2507 || htab->splt == NULL)
2508 {
2509 /* We didn't make a PLT entry for this symbol. This
2510 happens when statically linking PIC code, or when
2511 using -Bsymbolic. */
2512 break;
2513 }
2514
2515 relocation = (htab->splt->output_section->vma
2516 + htab->splt->output_offset
2517 + h->plt.offset);
2518 unresolved_reloc = FALSE;
2519 break;
2520
2521 case R_386_32:
2522 case R_386_PC32:
2523 if ((input_section->flags & SEC_ALLOC) == 0)
2524 break;
2525
2526 if ((info->shared
2527 && (h == NULL
2528 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2529 || h->root.type != bfd_link_hash_undefweak)
2530 && (r_type != R_386_PC32
2531 || !SYMBOL_CALLS_LOCAL (info, h)))
2532 || (ELIMINATE_COPY_RELOCS
2533 && !info->shared
2534 && h != NULL
2535 && h->dynindx != -1
2536 && !h->non_got_ref
2537 && ((h->def_dynamic
2538 && !h->def_regular)
2539 || h->root.type == bfd_link_hash_undefweak
2540 || h->root.type == bfd_link_hash_undefined)))
2541 {
2542 Elf_Internal_Rela outrel;
2543 bfd_byte *loc;
2544 bfd_boolean skip, relocate;
2545 asection *sreloc;
2546
2547 /* When generating a shared object, these relocations
2548 are copied into the output file to be resolved at run
2549 time. */
2550
2551 skip = FALSE;
2552 relocate = FALSE;
2553
2554 outrel.r_offset =
2555 _bfd_elf_section_offset (output_bfd, info, input_section,
2556 rel->r_offset);
2557 if (outrel.r_offset == (bfd_vma) -1)
2558 skip = TRUE;
2559 else if (outrel.r_offset == (bfd_vma) -2)
2560 skip = TRUE, relocate = TRUE;
2561 outrel.r_offset += (input_section->output_section->vma
2562 + input_section->output_offset);
2563
2564 if (skip)
2565 memset (&outrel, 0, sizeof outrel);
2566 else if (h != NULL
2567 && h->dynindx != -1
2568 && (r_type == R_386_PC32
2569 || !info->shared
2570 || !SYMBOLIC_BIND (info, h)
2571 || !h->def_regular))
2572 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2573 else
2574 {
2575 /* This symbol is local, or marked to become local. */
2576 relocate = TRUE;
2577 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2578 }
2579
2580 sreloc = elf_section_data (input_section)->sreloc;
2581 if (sreloc == NULL)
2582 abort ();
2583
2584 loc = sreloc->contents;
2585 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2586 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2587
2588 /* If this reloc is against an external symbol, we do
2589 not want to fiddle with the addend. Otherwise, we
2590 need to include the symbol value so that it becomes
2591 an addend for the dynamic reloc. */
2592 if (! relocate)
2593 continue;
2594 }
2595 break;
2596
2597 case R_386_TLS_IE:
2598 if (info->shared)
2599 {
2600 Elf_Internal_Rela outrel;
2601 bfd_byte *loc;
2602 asection *sreloc;
2603
2604 outrel.r_offset = rel->r_offset
2605 + input_section->output_section->vma
2606 + input_section->output_offset;
2607 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2608 sreloc = elf_section_data (input_section)->sreloc;
2609 if (sreloc == NULL)
2610 abort ();
2611 loc = sreloc->contents;
2612 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2613 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2614 }
2615 /* Fall through */
2616
2617 case R_386_TLS_GD:
2618 case R_386_TLS_GOTDESC:
2619 case R_386_TLS_DESC_CALL:
2620 case R_386_TLS_IE_32:
2621 case R_386_TLS_GOTIE:
2622 r_type = elf_i386_tls_transition (info, r_type, h == NULL);
2623 tls_type = GOT_UNKNOWN;
2624 if (h == NULL && local_got_offsets)
2625 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
2626 else if (h != NULL)
2627 {
2628 tls_type = elf_i386_hash_entry(h)->tls_type;
2629 if (!info->shared && h->dynindx == -1 && (tls_type & GOT_TLS_IE))
2630 r_type = R_386_TLS_LE_32;
2631 }
2632 if (tls_type == GOT_TLS_IE)
2633 tls_type = GOT_TLS_IE_NEG;
2634 if (r_type == R_386_TLS_GD
2635 || r_type == R_386_TLS_GOTDESC
2636 || r_type == R_386_TLS_DESC_CALL)
2637 {
2638 if (tls_type == GOT_TLS_IE_POS)
2639 r_type = R_386_TLS_GOTIE;
2640 else if (tls_type & GOT_TLS_IE)
2641 r_type = R_386_TLS_IE_32;
2642 }
2643
2644 if (r_type == R_386_TLS_LE_32)
2645 {
2646 BFD_ASSERT (! unresolved_reloc);
2647 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
2648 {
2649 unsigned int val, type;
2650 bfd_vma roff;
2651
2652 /* GD->LE transition. */
2653 BFD_ASSERT (rel->r_offset >= 2);
2654 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2655 BFD_ASSERT (type == 0x8d || type == 0x04);
2656 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
2657 BFD_ASSERT (bfd_get_8 (input_bfd,
2658 contents + rel->r_offset + 4)
2659 == 0xe8);
2660 BFD_ASSERT (rel + 1 < relend);
2661 BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
2662 roff = rel->r_offset + 5;
2663 val = bfd_get_8 (input_bfd,
2664 contents + rel->r_offset - 1);
2665 if (type == 0x04)
2666 {
2667 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
2668 Change it into:
2669 movl %gs:0, %eax; subl $foo@tpoff, %eax
2670 (6 byte form of subl). */
2671 BFD_ASSERT (rel->r_offset >= 3);
2672 BFD_ASSERT (bfd_get_8 (input_bfd,
2673 contents + rel->r_offset - 3)
2674 == 0x8d);
2675 BFD_ASSERT ((val & 0xc7) == 0x05 && val != (4 << 3));
2676 memcpy (contents + rel->r_offset - 3,
2677 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2678 }
2679 else
2680 {
2681 BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
2682 if (rel->r_offset + 10 <= input_section->size
2683 && bfd_get_8 (input_bfd,
2684 contents + rel->r_offset + 9) == 0x90)
2685 {
2686 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
2687 Change it into:
2688 movl %gs:0, %eax; subl $foo@tpoff, %eax
2689 (6 byte form of subl). */
2690 memcpy (contents + rel->r_offset - 2,
2691 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2692 roff = rel->r_offset + 6;
2693 }
2694 else
2695 {
2696 /* leal foo(%reg), %eax; call ___tls_get_addr
2697 Change it into:
2698 movl %gs:0, %eax; subl $foo@tpoff, %eax
2699 (5 byte form of subl). */
2700 memcpy (contents + rel->r_offset - 2,
2701 "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2702 }
2703 }
2704 bfd_put_32 (output_bfd, tpoff (info, relocation),
2705 contents + roff);
2706 /* Skip R_386_PLT32. */
2707 rel++;
2708 continue;
2709 }
2710 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
2711 {
2712 /* GDesc -> LE transition.
2713 It's originally something like:
2714 leal x@tlsdesc(%ebx), %eax
2715
2716 leal x@ntpoff, %eax
2717
2718 Registers other than %eax may be set up here. */
2719
2720 unsigned int val, type;
2721 bfd_vma roff;
2722
2723 /* First, make sure it's a leal adding ebx to a
2724 32-bit offset into any register, although it's
2725 probably almost always going to be eax. */
2726 roff = rel->r_offset;
2727 BFD_ASSERT (roff >= 2);
2728 type = bfd_get_8 (input_bfd, contents + roff - 2);
2729 BFD_ASSERT (type == 0x8d);
2730 val = bfd_get_8 (input_bfd, contents + roff - 1);
2731 BFD_ASSERT ((val & 0xc7) == 0x83);
2732 BFD_ASSERT (roff + 4 <= input_section->size);
2733
2734 /* Now modify the instruction as appropriate. */
2735 /* aoliva FIXME: remove the above and xor the byte
2736 below with 0x86. */
2737 bfd_put_8 (output_bfd, val ^ 0x86,
2738 contents + roff - 1);
2739 bfd_put_32 (output_bfd, -tpoff (info, relocation),
2740 contents + roff);
2741 continue;
2742 }
2743 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
2744 {
2745 /* GDesc -> LE transition.
2746 It's originally:
2747 call *(%eax)
2748 Turn it into:
2749 nop; nop */
2750
2751 unsigned int val, type;
2752 bfd_vma roff;
2753
2754 /* First, make sure it's a call *(%eax). */
2755 roff = rel->r_offset;
2756 BFD_ASSERT (roff + 2 <= input_section->size);
2757 type = bfd_get_8 (input_bfd, contents + roff);
2758 BFD_ASSERT (type == 0xff);
2759 val = bfd_get_8 (input_bfd, contents + roff + 1);
2760 BFD_ASSERT (val == 0x10);
2761
2762 /* Now modify the instruction as appropriate. Use
2763 xchg %ax,%ax instead of 2 nops. */
2764 bfd_put_8 (output_bfd, 0x66, contents + roff);
2765 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2766 continue;
2767 }
2768 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
2769 {
2770 unsigned int val, type;
2771
2772 /* IE->LE transition:
2773 Originally it can be one of:
2774 movl foo, %eax
2775 movl foo, %reg
2776 addl foo, %reg
2777 We change it into:
2778 movl $foo, %eax
2779 movl $foo, %reg
2780 addl $foo, %reg. */
2781 BFD_ASSERT (rel->r_offset >= 1);
2782 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2783 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
2784 if (val == 0xa1)
2785 {
2786 /* movl foo, %eax. */
2787 bfd_put_8 (output_bfd, 0xb8,
2788 contents + rel->r_offset - 1);
2789 }
2790 else
2791 {
2792 BFD_ASSERT (rel->r_offset >= 2);
2793 type = bfd_get_8 (input_bfd,
2794 contents + rel->r_offset - 2);
2795 switch (type)
2796 {
2797 case 0x8b:
2798 /* movl */
2799 BFD_ASSERT ((val & 0xc7) == 0x05);
2800 bfd_put_8 (output_bfd, 0xc7,
2801 contents + rel->r_offset - 2);
2802 bfd_put_8 (output_bfd,
2803 0xc0 | ((val >> 3) & 7),
2804 contents + rel->r_offset - 1);
2805 break;
2806 case 0x03:
2807 /* addl */
2808 BFD_ASSERT ((val & 0xc7) == 0x05);
2809 bfd_put_8 (output_bfd, 0x81,
2810 contents + rel->r_offset - 2);
2811 bfd_put_8 (output_bfd,
2812 0xc0 | ((val >> 3) & 7),
2813 contents + rel->r_offset - 1);
2814 break;
2815 default:
2816 BFD_FAIL ();
2817 break;
2818 }
2819 }
2820 bfd_put_32 (output_bfd, -tpoff (info, relocation),
2821 contents + rel->r_offset);
2822 continue;
2823 }
2824 else
2825 {
2826 unsigned int val, type;
2827
2828 /* {IE_32,GOTIE}->LE transition:
2829 Originally it can be one of:
2830 subl foo(%reg1), %reg2
2831 movl foo(%reg1), %reg2
2832 addl foo(%reg1), %reg2
2833 We change it into:
2834 subl $foo, %reg2
2835 movl $foo, %reg2 (6 byte form)
2836 addl $foo, %reg2. */
2837 BFD_ASSERT (rel->r_offset >= 2);
2838 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2839 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2840 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
2841 BFD_ASSERT ((val & 0xc0) == 0x80 && (val & 7) != 4);
2842 if (type == 0x8b)
2843 {
2844 /* movl */
2845 bfd_put_8 (output_bfd, 0xc7,
2846 contents + rel->r_offset - 2);
2847 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2848 contents + rel->r_offset - 1);
2849 }
2850 else if (type == 0x2b)
2851 {
2852 /* subl */
2853 bfd_put_8 (output_bfd, 0x81,
2854 contents + rel->r_offset - 2);
2855 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
2856 contents + rel->r_offset - 1);
2857 }
2858 else if (type == 0x03)
2859 {
2860 /* addl */
2861 bfd_put_8 (output_bfd, 0x81,
2862 contents + rel->r_offset - 2);
2863 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2864 contents + rel->r_offset - 1);
2865 }
2866 else
2867 BFD_FAIL ();
2868 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
2869 bfd_put_32 (output_bfd, -tpoff (info, relocation),
2870 contents + rel->r_offset);
2871 else
2872 bfd_put_32 (output_bfd, tpoff (info, relocation),
2873 contents + rel->r_offset);
2874 continue;
2875 }
2876 }
2877
2878 if (htab->sgot == NULL)
2879 abort ();
2880
2881 if (h != NULL)
2882 {
2883 off = h->got.offset;
2884 offplt = elf_i386_hash_entry (h)->tlsdesc_got;
2885 }
2886 else
2887 {
2888 if (local_got_offsets == NULL)
2889 abort ();
2890
2891 off = local_got_offsets[r_symndx];
2892 offplt = local_tlsdesc_gotents[r_symndx];
2893 }
2894
2895 if ((off & 1) != 0)
2896 off &= ~1;
2897 else
2898 {
2899 Elf_Internal_Rela outrel;
2900 bfd_byte *loc;
2901 int dr_type, indx;
2902 asection *sreloc;
2903
2904 if (htab->srelgot == NULL)
2905 abort ();
2906
2907 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2908
2909 if (GOT_TLS_GDESC_P (tls_type))
2910 {
2911 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
2912 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
2913 <= htab->sgotplt->size);
2914 outrel.r_offset = (htab->sgotplt->output_section->vma
2915 + htab->sgotplt->output_offset
2916 + offplt
2917 + htab->sgotplt_jump_table_size);
2918 sreloc = htab->srelplt;
2919 loc = sreloc->contents;
2920 loc += (htab->next_tls_desc_index++
2921 * sizeof (Elf32_External_Rel));
2922 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
2923 <= sreloc->contents + sreloc->size);
2924 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2925 if (indx == 0)
2926 {
2927 BFD_ASSERT (! unresolved_reloc);
2928 bfd_put_32 (output_bfd,
2929 relocation - dtpoff_base (info),
2930 htab->sgotplt->contents + offplt
2931 + htab->sgotplt_jump_table_size + 4);
2932 }
2933 else
2934 {
2935 bfd_put_32 (output_bfd, 0,
2936 htab->sgotplt->contents + offplt
2937 + htab->sgotplt_jump_table_size + 4);
2938 }
2939 }
2940
2941 sreloc = htab->srelgot;
2942
2943 outrel.r_offset = (htab->sgot->output_section->vma
2944 + htab->sgot->output_offset + off);
2945
2946 if (GOT_TLS_GD_P (tls_type))
2947 dr_type = R_386_TLS_DTPMOD32;
2948 else if (GOT_TLS_GDESC_P (tls_type))
2949 goto dr_done;
2950 else if (tls_type == GOT_TLS_IE_POS)
2951 dr_type = R_386_TLS_TPOFF;
2952 else
2953 dr_type = R_386_TLS_TPOFF32;
2954
2955 if (dr_type == R_386_TLS_TPOFF && indx == 0)
2956 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
2957 htab->sgot->contents + off);
2958 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
2959 bfd_put_32 (output_bfd, dtpoff_base (info) - relocation,
2960 htab->sgot->contents + off);
2961 else if (dr_type != R_386_TLS_DESC)
2962 bfd_put_32 (output_bfd, 0,
2963 htab->sgot->contents + off);
2964 outrel.r_info = ELF32_R_INFO (indx, dr_type);
2965
2966 loc = sreloc->contents;
2967 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2968 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
2969 <= sreloc->contents + sreloc->size);
2970 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2971
2972 if (GOT_TLS_GD_P (tls_type))
2973 {
2974 if (indx == 0)
2975 {
2976 BFD_ASSERT (! unresolved_reloc);
2977 bfd_put_32 (output_bfd,
2978 relocation - dtpoff_base (info),
2979 htab->sgot->contents + off + 4);
2980 }
2981 else
2982 {
2983 bfd_put_32 (output_bfd, 0,
2984 htab->sgot->contents + off + 4);
2985 outrel.r_info = ELF32_R_INFO (indx,
2986 R_386_TLS_DTPOFF32);
2987 outrel.r_offset += 4;
2988 sreloc->reloc_count++;
2989 loc += sizeof (Elf32_External_Rel);
2990 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
2991 <= sreloc->contents + sreloc->size);
2992 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2993 }
2994 }
2995 else if (tls_type == GOT_TLS_IE_BOTH)
2996 {
2997 bfd_put_32 (output_bfd,
2998 indx == 0 ? relocation - dtpoff_base (info) : 0,
2999 htab->sgot->contents + off + 4);
3000 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3001 outrel.r_offset += 4;
3002 sreloc->reloc_count++;
3003 loc += sizeof (Elf32_External_Rel);
3004 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3005 }
3006
3007 dr_done:
3008 if (h != NULL)
3009 h->got.offset |= 1;
3010 else
3011 local_got_offsets[r_symndx] |= 1;
3012 }
3013
3014 if (off >= (bfd_vma) -2
3015 && ! GOT_TLS_GDESC_P (tls_type))
3016 abort ();
3017 if (r_type == R_386_TLS_GOTDESC
3018 || r_type == R_386_TLS_DESC_CALL)
3019 {
3020 relocation = htab->sgotplt_jump_table_size + offplt;
3021 unresolved_reloc = FALSE;
3022 }
3023 else if (r_type == ELF32_R_TYPE (rel->r_info))
3024 {
3025 bfd_vma g_o_t = htab->sgotplt->output_section->vma
3026 + htab->sgotplt->output_offset;
3027 relocation = htab->sgot->output_section->vma
3028 + htab->sgot->output_offset + off - g_o_t;
3029 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3030 && tls_type == GOT_TLS_IE_BOTH)
3031 relocation += 4;
3032 if (r_type == R_386_TLS_IE)
3033 relocation += g_o_t;
3034 unresolved_reloc = FALSE;
3035 }
3036 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3037 {
3038 unsigned int val, type;
3039 bfd_vma roff;
3040
3041 /* GD->IE transition. */
3042 BFD_ASSERT (rel->r_offset >= 2);
3043 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3044 BFD_ASSERT (type == 0x8d || type == 0x04);
3045 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
3046 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
3047 == 0xe8);
3048 BFD_ASSERT (rel + 1 < relend);
3049 BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
3050 roff = rel->r_offset - 3;
3051 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3052 if (type == 0x04)
3053 {
3054 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3055 Change it into:
3056 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
3057 BFD_ASSERT (rel->r_offset >= 3);
3058 BFD_ASSERT (bfd_get_8 (input_bfd,
3059 contents + rel->r_offset - 3)
3060 == 0x8d);
3061 BFD_ASSERT ((val & 0xc7) == 0x05 && val != (4 << 3));
3062 val >>= 3;
3063 }
3064 else
3065 {
3066 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3067 Change it into:
3068 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
3069 BFD_ASSERT (rel->r_offset + 10 <= input_section->size);
3070 BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
3071 BFD_ASSERT (bfd_get_8 (input_bfd,
3072 contents + rel->r_offset + 9)
3073 == 0x90);
3074 roff = rel->r_offset - 2;
3075 }
3076 memcpy (contents + roff,
3077 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3078 contents[roff + 7] = 0x80 | (val & 7);
3079 /* If foo is used only with foo@gotntpoff(%reg) and
3080 foo@indntpoff, but not with foo@gottpoff(%reg), change
3081 subl $foo@gottpoff(%reg), %eax
3082 into:
3083 addl $foo@gotntpoff(%reg), %eax. */
3084 if (tls_type == GOT_TLS_IE_POS)
3085 contents[roff + 6] = 0x03;
3086 bfd_put_32 (output_bfd,
3087 htab->sgot->output_section->vma
3088 + htab->sgot->output_offset + off
3089 - htab->sgotplt->output_section->vma
3090 - htab->sgotplt->output_offset,
3091 contents + roff + 8);
3092 /* Skip R_386_PLT32. */
3093 rel++;
3094 continue;
3095 }
3096 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3097 {
3098 /* GDesc -> IE transition.
3099 It's originally something like:
3100 leal x@tlsdesc(%ebx), %eax
3101
3102 Change it to:
3103 movl x@gotntpoff(%ebx), %eax # before nop; nop
3104 or:
3105 movl x@gottpoff(%ebx), %eax # before negl %eax
3106
3107 Registers other than %eax may be set up here. */
3108
3109 unsigned int val, type;
3110 bfd_vma roff;
3111
3112 /* First, make sure it's a leal adding ebx to a 32-bit
3113 offset into any register, although it's probably
3114 almost always going to be eax. */
3115 roff = rel->r_offset;
3116 BFD_ASSERT (roff >= 2);
3117 type = bfd_get_8 (input_bfd, contents + roff - 2);
3118 BFD_ASSERT (type == 0x8d);
3119 val = bfd_get_8 (input_bfd, contents + roff - 1);
3120 BFD_ASSERT ((val & 0xc7) == 0x83);
3121 BFD_ASSERT (roff + 4 <= input_section->size);
3122
3123 /* Now modify the instruction as appropriate. */
3124 /* To turn a leal into a movl in the form we use it, it
3125 suffices to change the first byte from 0x8d to 0x8b.
3126 aoliva FIXME: should we decide to keep the leal, all
3127 we have to do is remove the statement below, and
3128 adjust the relaxation of R_386_TLS_DESC_CALL. */
3129 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3130
3131 if (tls_type == GOT_TLS_IE_BOTH)
3132 off += 4;
3133
3134 bfd_put_32 (output_bfd,
3135 htab->sgot->output_section->vma
3136 + htab->sgot->output_offset + off
3137 - htab->sgotplt->output_section->vma
3138 - htab->sgotplt->output_offset,
3139 contents + roff);
3140 continue;
3141 }
3142 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3143 {
3144 /* GDesc -> IE transition.
3145 It's originally:
3146 call *(%eax)
3147
3148 Change it to:
3149 nop; nop
3150 or
3151 negl %eax
3152 depending on how we transformed the TLS_GOTDESC above.
3153 */
3154
3155 unsigned int val, type;
3156 bfd_vma roff;
3157
3158 /* First, make sure it's a call *(%eax). */
3159 roff = rel->r_offset;
3160 BFD_ASSERT (roff + 2 <= input_section->size);
3161 type = bfd_get_8 (input_bfd, contents + roff);
3162 BFD_ASSERT (type == 0xff);
3163 val = bfd_get_8 (input_bfd, contents + roff + 1);
3164 BFD_ASSERT (val == 0x10);
3165
3166 /* Now modify the instruction as appropriate. */
3167 if (tls_type != GOT_TLS_IE_NEG)
3168 {
3169 /* xchg %ax,%ax */
3170 bfd_put_8 (output_bfd, 0x66, contents + roff);
3171 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3172 }
3173 else
3174 {
3175 /* negl %eax */
3176 bfd_put_8 (output_bfd, 0xf7, contents + roff);
3177 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3178 }
3179
3180 continue;
3181 }
3182 else
3183 BFD_ASSERT (FALSE);
3184 break;
3185
3186 case R_386_TLS_LDM:
3187 if (! info->shared)
3188 {
3189 unsigned int val;
3190
3191 /* LD->LE transition:
3192 Ensure it is:
3193 leal foo(%reg), %eax; call ___tls_get_addr.
3194 We change it into:
3195 movl %gs:0, %eax; nop; leal 0(%esi,1), %esi. */
3196 BFD_ASSERT (rel->r_offset >= 2);
3197 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
3198 == 0x8d);
3199 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3200 BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
3201 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
3202 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
3203 == 0xe8);
3204 BFD_ASSERT (rel + 1 < relend);
3205 BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
3206 memcpy (contents + rel->r_offset - 2,
3207 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3208 /* Skip R_386_PLT32. */
3209 rel++;
3210 continue;
3211 }
3212
3213 if (htab->sgot == NULL)
3214 abort ();
3215
3216 off = htab->tls_ldm_got.offset;
3217 if (off & 1)
3218 off &= ~1;
3219 else
3220 {
3221 Elf_Internal_Rela outrel;
3222 bfd_byte *loc;
3223
3224 if (htab->srelgot == NULL)
3225 abort ();
3226
3227 outrel.r_offset = (htab->sgot->output_section->vma
3228 + htab->sgot->output_offset + off);
3229
3230 bfd_put_32 (output_bfd, 0,
3231 htab->sgot->contents + off);
3232 bfd_put_32 (output_bfd, 0,
3233 htab->sgot->contents + off + 4);
3234 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3235 loc = htab->srelgot->contents;
3236 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3237 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3238 htab->tls_ldm_got.offset |= 1;
3239 }
3240 relocation = htab->sgot->output_section->vma
3241 + htab->sgot->output_offset + off
3242 - htab->sgotplt->output_section->vma
3243 - htab->sgotplt->output_offset;
3244 unresolved_reloc = FALSE;
3245 break;
3246
3247 case R_386_TLS_LDO_32:
3248 if (info->shared || (input_section->flags & SEC_CODE) == 0)
3249 relocation -= dtpoff_base (info);
3250 else
3251 /* When converting LDO to LE, we must negate. */
3252 relocation = -tpoff (info, relocation);
3253 break;
3254
3255 case R_386_TLS_LE_32:
3256 case R_386_TLS_LE:
3257 if (info->shared)
3258 {
3259 Elf_Internal_Rela outrel;
3260 asection *sreloc;
3261 bfd_byte *loc;
3262 int indx;
3263
3264 outrel.r_offset = rel->r_offset
3265 + input_section->output_section->vma
3266 + input_section->output_offset;
3267 if (h != NULL && h->dynindx != -1)
3268 indx = h->dynindx;
3269 else
3270 indx = 0;
3271 if (r_type == R_386_TLS_LE_32)
3272 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3273 else
3274 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3275 sreloc = elf_section_data (input_section)->sreloc;
3276 if (sreloc == NULL)
3277 abort ();
3278 loc = sreloc->contents;
3279 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3280 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3281 if (indx)
3282 continue;
3283 else if (r_type == R_386_TLS_LE_32)
3284 relocation = dtpoff_base (info) - relocation;
3285 else
3286 relocation -= dtpoff_base (info);
3287 }
3288 else if (r_type == R_386_TLS_LE_32)
3289 relocation = tpoff (info, relocation);
3290 else
3291 relocation = -tpoff (info, relocation);
3292 break;
3293
3294 default:
3295 break;
3296 }
3297
3298 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3299 because such sections are not SEC_ALLOC and thus ld.so will
3300 not process them. */
3301 if (unresolved_reloc
3302 && !((input_section->flags & SEC_DEBUGGING) != 0
3303 && h->def_dynamic))
3304 {
3305 (*_bfd_error_handler)
3306 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3307 input_bfd,
3308 input_section,
3309 (long) rel->r_offset,
3310 howto->name,
3311 h->root.root.string);
3312 return FALSE;
3313 }
3314
3315 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3316 contents, rel->r_offset,
3317 relocation, 0);
3318
3319 if (r != bfd_reloc_ok)
3320 {
3321 const char *name;
3322
3323 if (h != NULL)
3324 name = h->root.root.string;
3325 else
3326 {
3327 name = bfd_elf_string_from_elf_section (input_bfd,
3328 symtab_hdr->sh_link,
3329 sym->st_name);
3330 if (name == NULL)
3331 return FALSE;
3332 if (*name == '\0')
3333 name = bfd_section_name (input_bfd, sec);
3334 }
3335
3336 if (r == bfd_reloc_overflow)
3337 {
3338 if (! ((*info->callbacks->reloc_overflow)
3339 (info, (h ? &h->root : NULL), name, howto->name,
3340 (bfd_vma) 0, input_bfd, input_section,
3341 rel->r_offset)))
3342 return FALSE;
3343 }
3344 else
3345 {
3346 (*_bfd_error_handler)
3347 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3348 input_bfd, input_section,
3349 (long) rel->r_offset, name, (int) r);
3350 return FALSE;
3351 }
3352 }
3353 }
3354
3355 return TRUE;
3356 }
3357
3358 /* Finish up dynamic symbol handling. We set the contents of various
3359 dynamic sections here. */
3360
3361 static bfd_boolean
3362 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3363 struct bfd_link_info *info,
3364 struct elf_link_hash_entry *h,
3365 Elf_Internal_Sym *sym)
3366 {
3367 struct elf_i386_link_hash_table *htab;
3368
3369 htab = elf_i386_hash_table (info);
3370
3371 if (h->plt.offset != (bfd_vma) -1)
3372 {
3373 bfd_vma plt_index;
3374 bfd_vma got_offset;
3375 Elf_Internal_Rela rel;
3376 bfd_byte *loc;
3377
3378 /* This symbol has an entry in the procedure linkage table. Set
3379 it up. */
3380
3381 if (h->dynindx == -1
3382 || htab->splt == NULL
3383 || htab->sgotplt == NULL
3384 || htab->srelplt == NULL)
3385 abort ();
3386
3387 /* Get the index in the procedure linkage table which
3388 corresponds to this symbol. This is the index of this symbol
3389 in all the symbols for which we are making plt entries. The
3390 first entry in the procedure linkage table is reserved. */
3391 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3392
3393 /* Get the offset into the .got table of the entry that
3394 corresponds to this function. Each .got entry is 4 bytes.
3395 The first three are reserved. */
3396 got_offset = (plt_index + 3) * 4;
3397
3398 /* Fill in the entry in the procedure linkage table. */
3399 if (! info->shared)
3400 {
3401 memcpy (htab->splt->contents + h->plt.offset, elf_i386_plt_entry,
3402 PLT_ENTRY_SIZE);
3403 bfd_put_32 (output_bfd,
3404 (htab->sgotplt->output_section->vma
3405 + htab->sgotplt->output_offset
3406 + got_offset),
3407 htab->splt->contents + h->plt.offset + 2);
3408
3409 if (htab->is_vxworks)
3410 {
3411 int s, k, reloc_index;
3412
3413 /* Create the R_386_32 relocation referencing the GOT
3414 for this PLT entry. */
3415
3416 /* S: Current slot number (zero-based). */
3417 s = (h->plt.offset - PLT_ENTRY_SIZE) / PLT_ENTRY_SIZE;
3418 /* K: Number of relocations for PLTResolve. */
3419 if (info->shared)
3420 k = PLTRESOLVE_RELOCS_SHLIB;
3421 else
3422 k = PLTRESOLVE_RELOCS;
3423 /* Skip the PLTresolve relocations, and the relocations for
3424 the other PLT slots. */
3425 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3426 loc = (htab->srelplt2->contents + reloc_index
3427 * sizeof (Elf32_External_Rel));
3428
3429 rel.r_offset = (htab->splt->output_section->vma
3430 + htab->splt->output_offset
3431 + h->plt.offset + 2),
3432 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3433 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3434
3435 /* Create the R_386_32 relocation referencing the beginning of
3436 the PLT for this GOT entry. */
3437 rel.r_offset = (htab->sgotplt->output_section->vma
3438 + htab->sgotplt->output_offset
3439 + got_offset);
3440 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3441 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3442 loc + sizeof (Elf32_External_Rel));
3443 }
3444 }
3445 else
3446 {
3447 memcpy (htab->splt->contents + h->plt.offset, elf_i386_pic_plt_entry,
3448 PLT_ENTRY_SIZE);
3449 bfd_put_32 (output_bfd, got_offset,
3450 htab->splt->contents + h->plt.offset + 2);
3451 }
3452
3453 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
3454 htab->splt->contents + h->plt.offset + 7);
3455 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3456 htab->splt->contents + h->plt.offset + 12);
3457
3458 /* Fill in the entry in the global offset table. */
3459 bfd_put_32 (output_bfd,
3460 (htab->splt->output_section->vma
3461 + htab->splt->output_offset
3462 + h->plt.offset
3463 + 6),
3464 htab->sgotplt->contents + got_offset);
3465
3466 /* Fill in the entry in the .rel.plt section. */
3467 rel.r_offset = (htab->sgotplt->output_section->vma
3468 + htab->sgotplt->output_offset
3469 + got_offset);
3470 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3471 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rel);
3472 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3473
3474 if (!h->def_regular)
3475 {
3476 /* Mark the symbol as undefined, rather than as defined in
3477 the .plt section. Leave the value if there were any
3478 relocations where pointer equality matters (this is a clue
3479 for the dynamic linker, to make function pointer
3480 comparisons work between an application and shared
3481 library), otherwise set it to zero. If a function is only
3482 called from a binary, there is no need to slow down
3483 shared libraries because of that. */
3484 sym->st_shndx = SHN_UNDEF;
3485 if (!h->pointer_equality_needed)
3486 sym->st_value = 0;
3487 }
3488 }
3489
3490 if (h->got.offset != (bfd_vma) -1
3491 && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
3492 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
3493 {
3494 Elf_Internal_Rela rel;
3495 bfd_byte *loc;
3496
3497 /* This symbol has an entry in the global offset table. Set it
3498 up. */
3499
3500 if (htab->sgot == NULL || htab->srelgot == NULL)
3501 abort ();
3502
3503 rel.r_offset = (htab->sgot->output_section->vma
3504 + htab->sgot->output_offset
3505 + (h->got.offset & ~(bfd_vma) 1));
3506
3507 /* If this is a static link, or it is a -Bsymbolic link and the
3508 symbol is defined locally or was forced to be local because
3509 of a version file, we just want to emit a RELATIVE reloc.
3510 The entry in the global offset table will already have been
3511 initialized in the relocate_section function. */
3512 if (info->shared
3513 && SYMBOL_REFERENCES_LOCAL (info, h))
3514 {
3515 BFD_ASSERT((h->got.offset & 1) != 0);
3516 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3517 }
3518 else
3519 {
3520 BFD_ASSERT((h->got.offset & 1) == 0);
3521 bfd_put_32 (output_bfd, (bfd_vma) 0,
3522 htab->sgot->contents + h->got.offset);
3523 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3524 }
3525
3526 loc = htab->srelgot->contents;
3527 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3528 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3529 }
3530
3531 if (h->needs_copy)
3532 {
3533 Elf_Internal_Rela rel;
3534 bfd_byte *loc;
3535
3536 /* This symbol needs a copy reloc. Set it up. */
3537
3538 if (h->dynindx == -1
3539 || (h->root.type != bfd_link_hash_defined
3540 && h->root.type != bfd_link_hash_defweak)
3541 || htab->srelbss == NULL)
3542 abort ();
3543
3544 rel.r_offset = (h->root.u.def.value
3545 + h->root.u.def.section->output_section->vma
3546 + h->root.u.def.section->output_offset);
3547 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
3548 loc = htab->srelbss->contents;
3549 loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rel);
3550 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3551 }
3552
3553 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.
3554 On VxWorks, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it
3555 is relative to the ".got" section. */
3556 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3557 || (!htab->is_vxworks && h == htab->elf.hgot))
3558 sym->st_shndx = SHN_ABS;
3559
3560 return TRUE;
3561 }
3562
3563 /* Used to decide how to sort relocs in an optimal manner for the
3564 dynamic linker, before writing them out. */
3565
3566 static enum elf_reloc_type_class
3567 elf_i386_reloc_type_class (const Elf_Internal_Rela *rela)
3568 {
3569 switch (ELF32_R_TYPE (rela->r_info))
3570 {
3571 case R_386_RELATIVE:
3572 return reloc_class_relative;
3573 case R_386_JUMP_SLOT:
3574 return reloc_class_plt;
3575 case R_386_COPY:
3576 return reloc_class_copy;
3577 default:
3578 return reloc_class_normal;
3579 }
3580 }
3581
3582 /* Finish up the dynamic sections. */
3583
3584 static bfd_boolean
3585 elf_i386_finish_dynamic_sections (bfd *output_bfd,
3586 struct bfd_link_info *info)
3587 {
3588 struct elf_i386_link_hash_table *htab;
3589 bfd *dynobj;
3590 asection *sdyn;
3591
3592 htab = elf_i386_hash_table (info);
3593 dynobj = htab->elf.dynobj;
3594 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3595
3596 if (htab->elf.dynamic_sections_created)
3597 {
3598 Elf32_External_Dyn *dyncon, *dynconend;
3599
3600 if (sdyn == NULL || htab->sgot == NULL)
3601 abort ();
3602
3603 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3604 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3605 for (; dyncon < dynconend; dyncon++)
3606 {
3607 Elf_Internal_Dyn dyn;
3608 asection *s;
3609
3610 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3611
3612 switch (dyn.d_tag)
3613 {
3614 default:
3615 continue;
3616
3617 case DT_PLTGOT:
3618 s = htab->sgotplt;
3619 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3620 break;
3621
3622 case DT_JMPREL:
3623 s = htab->srelplt;
3624 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3625 break;
3626
3627 case DT_PLTRELSZ:
3628 s = htab->srelplt;
3629 dyn.d_un.d_val = s->size;
3630 break;
3631
3632 case DT_RELSZ:
3633 /* My reading of the SVR4 ABI indicates that the
3634 procedure linkage table relocs (DT_JMPREL) should be
3635 included in the overall relocs (DT_REL). This is
3636 what Solaris does. However, UnixWare can not handle
3637 that case. Therefore, we override the DT_RELSZ entry
3638 here to make it not include the JMPREL relocs. */
3639 s = htab->srelplt;
3640 if (s == NULL)
3641 continue;
3642 dyn.d_un.d_val -= s->size;
3643 break;
3644
3645 case DT_REL:
3646 /* We may not be using the standard ELF linker script.
3647 If .rel.plt is the first .rel section, we adjust
3648 DT_REL to not include it. */
3649 s = htab->srelplt;
3650 if (s == NULL)
3651 continue;
3652 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
3653 continue;
3654 dyn.d_un.d_ptr += s->size;
3655 break;
3656 }
3657
3658 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3659 }
3660
3661 /* Fill in the first entry in the procedure linkage table. */
3662 if (htab->splt && htab->splt->size > 0)
3663 {
3664 if (info->shared)
3665 {
3666 memcpy (htab->splt->contents, elf_i386_pic_plt0_entry,
3667 sizeof (elf_i386_pic_plt0_entry));
3668 memset (htab->splt->contents + sizeof (elf_i386_pic_plt0_entry),
3669 htab->plt0_pad_byte,
3670 PLT_ENTRY_SIZE - sizeof (elf_i386_pic_plt0_entry));
3671 }
3672 else
3673 {
3674 memcpy (htab->splt->contents, elf_i386_plt0_entry,
3675 sizeof(elf_i386_plt0_entry));
3676 memset (htab->splt->contents + sizeof (elf_i386_plt0_entry),
3677 htab->plt0_pad_byte,
3678 PLT_ENTRY_SIZE - sizeof (elf_i386_plt0_entry));
3679 bfd_put_32 (output_bfd,
3680 (htab->sgotplt->output_section->vma
3681 + htab->sgotplt->output_offset
3682 + 4),
3683 htab->splt->contents + 2);
3684 bfd_put_32 (output_bfd,
3685 (htab->sgotplt->output_section->vma
3686 + htab->sgotplt->output_offset
3687 + 8),
3688 htab->splt->contents + 8);
3689
3690 if (htab->is_vxworks)
3691 {
3692 Elf_Internal_Rela rel;
3693
3694 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
3695 On IA32 we use REL relocations so the addend goes in
3696 the PLT directly. */
3697 rel.r_offset = (htab->splt->output_section->vma
3698 + htab->splt->output_offset
3699 + 2);
3700 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3701 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3702 htab->srelplt2->contents);
3703 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
3704 rel.r_offset = (htab->splt->output_section->vma
3705 + htab->splt->output_offset
3706 + 8);
3707 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3708 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3709 htab->srelplt2->contents +
3710 sizeof (Elf32_External_Rel));
3711 }
3712 }
3713
3714 /* UnixWare sets the entsize of .plt to 4, although that doesn't
3715 really seem like the right value. */
3716 elf_section_data (htab->splt->output_section)
3717 ->this_hdr.sh_entsize = 4;
3718
3719 /* Correct the .rel.plt.unloaded relocations. */
3720 if (htab->is_vxworks && !info->shared)
3721 {
3722 int num_plts = (htab->splt->size / PLT_ENTRY_SIZE) - 1;
3723 unsigned char *p;
3724
3725 p = htab->srelplt2->contents;
3726 if (info->shared)
3727 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
3728 else
3729 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
3730
3731 for (; num_plts; num_plts--)
3732 {
3733 Elf_Internal_Rela rel;
3734 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
3735 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3736 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
3737 p += sizeof (Elf32_External_Rel);
3738
3739 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
3740 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3741 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
3742 p += sizeof (Elf32_External_Rel);
3743 }
3744 }
3745 }
3746 }
3747
3748 if (htab->sgotplt)
3749 {
3750 /* Fill in the first three entries in the global offset table. */
3751 if (htab->sgotplt->size > 0)
3752 {
3753 bfd_put_32 (output_bfd,
3754 (sdyn == NULL ? 0
3755 : sdyn->output_section->vma + sdyn->output_offset),
3756 htab->sgotplt->contents);
3757 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 4);
3758 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 8);
3759 }
3760
3761 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = 4;
3762 }
3763
3764 if (htab->sgot && htab->sgot->size > 0)
3765 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
3766
3767 return TRUE;
3768 }
3769
3770 /* Return address for Ith PLT stub in section PLT, for relocation REL
3771 or (bfd_vma) -1 if it should not be included. */
3772
3773 static bfd_vma
3774 elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
3775 const arelent *rel ATTRIBUTE_UNUSED)
3776 {
3777 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3778 }
3779
3780 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3781
3782 static bfd_boolean
3783 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
3784 {
3785 if (h->plt.offset != (bfd_vma) -1
3786 && !h->def_regular
3787 && !h->pointer_equality_needed)
3788 return FALSE;
3789
3790 return _bfd_elf_hash_symbol (h);
3791 }
3792
3793 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
3794 #define TARGET_LITTLE_NAME "elf32-i386"
3795 #define ELF_ARCH bfd_arch_i386
3796 #define ELF_MACHINE_CODE EM_386
3797 #define ELF_MAXPAGESIZE 0x1000
3798
3799 #define elf_backend_can_gc_sections 1
3800 #define elf_backend_can_refcount 1
3801 #define elf_backend_want_got_plt 1
3802 #define elf_backend_plt_readonly 1
3803 #define elf_backend_want_plt_sym 0
3804 #define elf_backend_got_header_size 12
3805
3806 /* Support RELA for objdump of prelink objects. */
3807 #define elf_info_to_howto elf_i386_info_to_howto_rel
3808 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
3809
3810 #define bfd_elf32_mkobject elf_i386_mkobject
3811
3812 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
3813 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
3814 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
3815 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
3816
3817 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
3818 #define elf_backend_check_relocs elf_i386_check_relocs
3819 #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
3820 #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
3821 #define elf_backend_fake_sections elf_i386_fake_sections
3822 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
3823 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
3824 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
3825 #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
3826 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
3827 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
3828 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
3829 #define elf_backend_relocate_section elf_i386_relocate_section
3830 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
3831 #define elf_backend_always_size_sections elf_i386_always_size_sections
3832 #define elf_backend_omit_section_dynsym \
3833 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
3834 #define elf_backend_plt_sym_val elf_i386_plt_sym_val
3835 #define elf_backend_hash_symbol elf_i386_hash_symbol
3836
3837 #include "elf32-target.h"
3838
3839 /* FreeBSD support. */
3840
3841 #undef TARGET_LITTLE_SYM
3842 #define TARGET_LITTLE_SYM bfd_elf32_i386_freebsd_vec
3843 #undef TARGET_LITTLE_NAME
3844 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
3845 #undef ELF_OSABI
3846 #define ELF_OSABI ELFOSABI_FREEBSD
3847
3848 /* The kernel recognizes executables as valid only if they carry a
3849 "FreeBSD" label in the ELF header. So we put this label on all
3850 executables and (for simplicity) also all other object files. */
3851
3852 static void
3853 elf_i386_post_process_headers (bfd *abfd,
3854 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3855 {
3856 Elf_Internal_Ehdr *i_ehdrp;
3857
3858 i_ehdrp = elf_elfheader (abfd);
3859
3860 /* Put an ABI label supported by FreeBSD >= 4.1. */
3861 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
3862 #ifdef OLD_FREEBSD_ABI_LABEL
3863 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
3864 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
3865 #endif
3866 }
3867
3868 #undef elf_backend_post_process_headers
3869 #define elf_backend_post_process_headers elf_i386_post_process_headers
3870 #undef elf32_bed
3871 #define elf32_bed elf32_i386_fbsd_bed
3872
3873 #include "elf32-target.h"
3874
3875 /* VxWorks support. */
3876
3877 #undef TARGET_LITTLE_SYM
3878 #define TARGET_LITTLE_SYM bfd_elf32_i386_vxworks_vec
3879 #undef TARGET_LITTLE_NAME
3880 #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
3881 #undef ELF_OSABI
3882
3883 /* Like elf_i386_link_hash_table_create but with tweaks for VxWorks. */
3884
3885 static struct bfd_link_hash_table *
3886 elf_i386_vxworks_link_hash_table_create (bfd *abfd)
3887 {
3888 struct bfd_link_hash_table *ret;
3889 struct elf_i386_link_hash_table *htab;
3890
3891 ret = elf_i386_link_hash_table_create (abfd);
3892 if (ret)
3893 {
3894 htab = (struct elf_i386_link_hash_table *) ret;
3895 htab->is_vxworks = 1;
3896 htab->plt0_pad_byte = 0x90;
3897 }
3898
3899 return ret;
3900 }
3901
3902
3903 #undef elf_backend_post_process_headers
3904 #undef bfd_elf32_bfd_link_hash_table_create
3905 #define bfd_elf32_bfd_link_hash_table_create \
3906 elf_i386_vxworks_link_hash_table_create
3907 #undef elf_backend_add_symbol_hook
3908 #define elf_backend_add_symbol_hook \
3909 elf_vxworks_add_symbol_hook
3910 #undef elf_backend_link_output_symbol_hook
3911 #define elf_backend_link_output_symbol_hook \
3912 elf_vxworks_link_output_symbol_hook
3913 #undef elf_backend_emit_relocs
3914 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
3915 #undef elf_backend_final_write_processing
3916 #define elf_backend_final_write_processing \
3917 elf_vxworks_final_write_processing
3918
3919 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
3920 define it. */
3921 #undef elf_backend_want_plt_sym
3922 #define elf_backend_want_plt_sym 1
3923
3924 #undef elf32_bed
3925 #define elf32_bed elf32_i386_vxworks_bed
3926
3927 #include "elf32-target.h"
This page took 0.175233 seconds and 4 git commands to generate.