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